refactor model things
[carveJwlIkooP6JGAAIwe30JlM.git] / world_gen.h
index ccd3eda875feb36294f81108447610e2ecf730b2..16d64fdefdf8a53af2a1cbb98b28eed71d43702a 100644 (file)
@@ -8,8 +8,7 @@
 #include "world.h"
 
 /* load world TODO: Put back vg back in loading state while this happens */
-VG_STATIC void world_load( const char *mdl_file );
-
+VG_STATIC void world_load(void);
 
 
 VG_STATIC void world_add_all_if_material( m4x3f transform, scene *pscene, 
@@ -31,24 +30,6 @@ VG_STATIC void world_add_all_if_material( m4x3f transform, scene *pscene,
             scene_add_submesh( pscene, mdl, sm, transform2 );
          }
       }
-
-#if 0
-      if( pnode->classtype == k_classtype_instance )
-      {
-         if( pnode->sub_uid )
-         {
-            u32 instance_id = pnode->sub_uid -1;
-            struct instance_cache *cache = &world.instance_cache[instance_id];
-            mdl_context *mdl2 = cache->mdl;
-
-            m4x3f transform2;
-            mdl_node_transform( pnode, transform2 );
-            m4x3_mul( transform, transform2, transform2 );
-
-            world_add_all_if_material( transform2, pscene, mdl2, id );
-         }
-      }
-#endif
    }
 }
 
@@ -134,6 +115,16 @@ VG_STATIC void world_ents_allocate(void)
          k_classtype_trigger,
          (void*)&world.triggers,
          sizeof(struct trigger_zone)
+      },
+      {
+         k_classtype_logic_relay,
+         (void*)&world.logic_relays,
+         sizeof(struct logic_relay)
+      },
+      {
+         k_classtype_logic_achievement,
+         (void*)&world.logic_achievements,
+         sizeof(struct logic_achievement)
       }
    };
 
@@ -148,6 +139,7 @@ VG_STATIC void world_ents_allocate(void)
       {
          if( pnode->classtype == entity_counts[j].ct )
          {
+            pnode->sub_uid = entity_counts[j].count;
             entity_counts[j].count ++;
             break;
          }
@@ -199,51 +191,6 @@ VG_STATIC void world_pct_water( mdl_node *pnode )
    }
 }
 
-VG_STATIC void world_pct_instance( mdl_node *pnode )
-{
-   struct classtype_instance *inst = mdl_get_entdata( world.meta, pnode );
-   pnode->sub_uid = 0;
-   
-   int cache_entry = 0;
-   for( int i=0; i<world.instance_cache_count; i++ )
-   {
-      struct instance_cache *cache = &world.instance_cache[i];
-      if( inst->pstr_file == cache->pstr_file )
-      {
-         cache_entry = 1;
-         pnode->sub_uid = i+1;
-         break;
-      }
-   }
-
-   if( !cache_entry )
-   {
-      if( world.instance_cache_count == vg_list_size(world.instance_cache) )
-         vg_fatal_exit_loop( "Instance cache is full!" );
-
-      struct instance_cache *cache = 
-         &world.instance_cache[world.instance_cache_count ++ ];
-
-      cache->pstr_file = inst->pstr_file;
-
-#if 0
-      cache->mdl = mdl_load( filename );
-
-      if( cache->mdl )
-      {
-         world.instance_cache_count ++;
-         pnode->sub_uid = world.instance_cache_count;
-         mdl_link_materials( mworld, cache->mdl );
-         vg_success( "Cached %s\n", filename );
-      }
-      else
-      {
-         vg_warn( "Failed to cache %s\n", filename );
-      }
-#endif
-   }
-}
-
 VG_STATIC void world_pct_audio( mdl_node *pnode )
 {
    struct world_audio_thing *thing = &world.audio_things[
@@ -261,7 +208,9 @@ VG_STATIC void world_pct_audio( mdl_node *pnode )
 
    thing->flags = aud->flags;
    thing->temp_embedded_clip.path = mdl_pstr( world.meta, aud->pstr_file );
-   audio_clip_load( &thing->temp_embedded_clip );
+   thing->temp_embedded_clip.source_mode = k_audio_source_mono;
+
+   audio_clip_load( &thing->temp_embedded_clip, world.audio_vgl );
    thing->player.name = mdl_pstr( world.meta, pnode->pstr_name );
    thing->player.enqued = 0;
 
@@ -269,6 +218,71 @@ VG_STATIC void world_pct_audio( mdl_node *pnode )
    world.audio_things_count ++;
 }
 
+
+VG_STATIC void world_pct_trigger( mdl_node *pnode )
+{
+   struct trigger_zone *trigger = &world.triggers[ world.trigger_count ];
+   struct classtype_trigger *inf = mdl_get_entdata( world.meta, pnode );
+   
+   if( inf->target )
+   {
+      mdl_node *target_node = mdl_node_from_id( world.meta, inf->target );
+
+      trigger->target.sub_id = target_node->sub_uid;
+      trigger->target.classtype = target_node->classtype;
+   }
+   else
+   {
+      vg_warn( "Trigger with no target...\n" );
+      return;
+   }
+
+   mdl_node_transform( pnode, trigger->transform );
+   m4x3_invert_full( trigger->transform, trigger->inv_transform );
+
+   world.trigger_count ++;
+}
+
+
+VG_STATIC void world_pct_relay( mdl_node *pnode )
+{
+   struct logic_relay *relay = &world.logic_relays[ world.relay_count ];
+   struct classtype_logic_relay *inf = mdl_get_entdata( world.meta, pnode );
+
+   relay->target_count = 0;
+
+   for( int i=0; i<vg_list_size(relay->targets); i++ )
+   {
+      if( inf->targets[i] )
+      {
+         struct relay_target *target = &relay->targets[relay->target_count ++];
+         mdl_node *other = mdl_node_from_id( world.meta, inf->targets[i] );
+         
+         target->classtype = other->classtype;
+         target->sub_id = other->sub_uid;
+      }
+   }
+
+   v3_copy( pnode->co, relay->pos );
+   world.relay_count ++;
+}
+
+
+VG_STATIC void world_pct_achievement( mdl_node *pnode )
+{
+   struct logic_achievement *ach = 
+      &world.logic_achievements[ world.achievement_count ];
+   struct classtype_logic_achievement *inf = 
+      mdl_get_entdata( world.meta, pnode );
+
+   v3_copy( pnode->co, ach->pos );
+   ach->achievement_id = mdl_pstr( world.meta, inf->pstr_name );
+   ach->achieved = 0;
+
+   world.achievement_count ++;
+}
+
+
 VG_STATIC void world_entities_process(void)
 {
    struct entity_instruction
@@ -280,8 +294,10 @@ VG_STATIC void world_entities_process(void)
    {
       { k_classtype_spawn,    world_pct_spawn },
       { k_classtype_water,    world_pct_water },
-      { k_classtype_instance, world_pct_instance },
       { k_classtype_audio,    world_pct_audio },
+      { k_classtype_trigger,  world_pct_trigger },
+      { k_classtype_logic_relay, world_pct_relay },
+      { k_classtype_logic_achievement, world_pct_achievement }
    };
 
    for( int i=0; i<world.meta->info.node_count; i++ )
@@ -298,62 +314,6 @@ VG_STATIC void world_entities_process(void)
             break;
          }
       }
-      
-#if 0
-      else if( pnode->classtype == k_classtype_achievement_box )
-      {
-         world.achievement_zones = 
-            buffer_reserve( world.achievement_zones,
-                            world.achievement_zones_count,
-                            &world.achievement_zones_cap, 1, 
-                            sizeof(struct achievement_zone) );
-
-         struct achievement_zone *zone = &world.achievement_zones[ 
-                                          world.achievement_zones_count ++ ];
-
-
-         struct classtype_achievement_box *box = mdl_get_entdata(mworld,pnode);
-
-         mdl_node_transform( pnode, zone->transform );
-         m4x3_invert_full( zone->transform, zone->inv_transform );
-         vg_strncpy( mdl_pstr(mworld, box->pstr_name), zone->name, 31 );
-         zone->name[31] = 0x00;
-         zone->triggered = 0;
-
-         if( box->trigger )
-            zone->ptarget_delegated = mdl_node_from_id( mworld, box->trigger );
-         else
-            zone->ptarget_delegated = NULL;
-      }
-#endif
-   }
-
-#if 0
-   /* fixup links */
-   for( int i=0; i<world.achievement_zones_count; i ++ )
-   {
-      struct achievement_zone *ach = &world.achievement_zones[ i ];
-      if( ach->ptarget_delegated )
-      {
-         u32 id = ach->ptarget_delegated->sub_uid;
-         ach->ptarget = &world.audio_things[ id ];
-      }
-      else
-         ach->ptarget = NULL;
-   }
-#endif
-}
-
-VG_STATIC void world_load_instance_cache(void)
-{
-   vg_linear_clear( vg_mem.scratch );
-
-   for( int i=0; i<world.instance_cache_count; i++ )
-   {
-      struct instance_cache *inst = &world.instance_cache[i];
-      
-      const char *filename = mdl_pstr( world.meta, inst->pstr_file );
-      inst->mdl = mdl_load_full( vg_mem.scratch, filename );
    }
 }
 
@@ -362,7 +322,7 @@ VG_STATIC void world_generate(void)
    /* 
     * Compile meshes into the world scenes
     */
-   world.scene_geo = scene_init( world.dynamic_vgl, 500000, 1200000 );
+   world.scene_geo = scene_init( world.dynamic_vgl, 350000, 1200000 );
    
    /*
     * TODO: System to dynamically allocate these 
@@ -397,8 +357,6 @@ VG_STATIC void world_generate(void)
    m4x3f midentity;
    m4x3_identity( midentity );
 
-   world_load_instance_cache();
-
    /*
     * Generate scene: collidable geometry
     * ----------------------------------------------------------------
@@ -465,6 +423,7 @@ VG_STATIC void world_generate(void)
    world.scene_no_collide = scene_init( world.dynamic_vgl, 200000, 500000 );
 
    vg_info( "Applying foliage\n" );
+   srand(0);
    world_apply_procedural_foliage();
    scene_copy_slice( world.scene_no_collide, &world.sm_foliage_main );
 
@@ -490,6 +449,7 @@ VG_STATIC void world_generate(void)
    world.scene_no_collide = NULL;
 }
 
+VG_STATIC int reset_player( int argc, char const *argv[] );
 VG_STATIC void world_post_process(void)
 {
    /* initialize audio if need be */
@@ -537,13 +497,16 @@ VG_STATIC void world_post_process(void)
       ortho[3][3] = 1.0f;
       m4x3_identity( camera );
 
-      glViewport( 0, 0, 1024, 1024 );
       glDisable(GL_DEPTH_TEST);
+      glDisable(GL_BLEND);
+      glDisable(GL_CULL_FACE);
       glBindFramebuffer( GL_FRAMEBUFFER, gpipeline.fb_depthmap );
+      glViewport( 0, 0, 1024, 1024 );
       shader_fscolour_use();
       shader_fscolour_uColour( (v4f){-9999.0f,-9999.0f,-9999.0f,-9999.0f} );
       render_fsquad();
 
+      /* todo: hmm?? */
       glEnable(GL_BLEND);
       glBlendFunc(GL_ONE, GL_ONE);
       glBlendEquation(GL_MAX);
@@ -579,6 +542,8 @@ VG_STATIC void world_post_process(void)
    /*
     * Setup scene collider 
     */
+
+   reset_player( 1, (const char *[]){"start"} );
 }
 
 
@@ -590,8 +555,29 @@ VG_STATIC void world_unload(void)
    mesh_free( &world.mesh_route_lines );
    mesh_free( &world.mesh_water );
 
+   world.time = 0.0;
+   world.rewind_from = 0.0;
+   world.rewind_to = 0.0;
+   world.last_use = 0.0;
+   world.active_gate = 0;
+   world.current_run_version = 2;
+   world.active_route_board = 0;
+   v3_zero( world.render_gate_pos );
+
+   for( int i=0; i<vg_list_size(world.ui_bars); i++ )
+   {
+      struct route_ui_bar *uib = &world.ui_bars[i];
+      uib->segment_start = 0;
+      uib->segment_count = 0;
+      uib->fade_start = 0;
+      uib->fade_count = 0;
+      uib->fade_timer_start = 0.0;
+      uib->xpos = 0.0f;
+   }
+
    /* delete the entire block of memory */
    vg_linear_clear( world.dynamic_vgl );
+   vg_linear_clear( world.audio_vgl );
 
    /* clean dangling pointers */
    world.meta = NULL;
@@ -610,15 +596,15 @@ VG_STATIC void world_unload(void)
    world.audio_things = NULL;
    world.audio_things_count = 0;
 
-   world.logic_entities = NULL;
-   world.logic_entity_count = 0;
-
-   world.logic_actions = NULL;
-   world.logic_action_count = 0;
-
    world.triggers = NULL;
    world.trigger_count = 0;
 
+   world.logic_relays = NULL;
+   world.relay_count = 0;
+
+   world.logic_achievements = NULL;
+   world.achievement_count = 0;
+
    world.nodes = NULL;
    world.node_count = 0;
 
@@ -631,16 +617,15 @@ VG_STATIC void world_unload(void)
    world.collectors = NULL;
    world.collector_count = 0;
 
-   world.instance_cache_count = 0;
    world.water.enabled = 0;
 }
 
-VG_STATIC void world_load( const char *mdl_file )
+VG_STATIC void world_load(void)
 {
    world_unload();
 
-   world.meta = mdl_load_full( world.dynamic_vgl, mdl_file );
-   vg_info( "Loading world: %s\n", mdl_file );
+   world.meta = mdl_load_full( world.dynamic_vgl, world.world_name );
+   vg_info( "Loading world: %s\n", world.world_name );
 
    /* dynamic allocations */
    world_ents_allocate();