nonlocal stuff again
[carveJwlIkooP6JGAAIwe30JlM.git] / world.h
diff --git a/world.h b/world.h
index c99cfdc1807ca6a752c5121ce0ff71156f3f1657..809397151e854c64b4508ceaf139a8e0e1c40c88 100644 (file)
--- a/world.h
+++ b/world.h
@@ -60,7 +60,14 @@ struct world_instance {
     * -------------------------------------------------------
     */
 
+   void *heap;
    char  world_name[ 64 ];
+   enum world_status{
+      k_world_status_unloaded = 0,
+      k_world_status_loading = 1,
+      k_world_status_loaded = 2
+   }
+   status;
 
    struct{
       boxf depthbounds;
@@ -87,6 +94,8 @@ struct world_instance {
       v4f g_sunset_ambient;
       v4f g_sun_colour;
       v4f g_sun_dir;
+      v4f g_board_0;
+      v4f g_board_1;
 
       float g_water_fog;
       float g_time;
@@ -182,12 +191,12 @@ struct world_instance {
    rb_object rb_geo;
 };
 
-VG_STATIC struct world_global{
+struct world_global{
    /*
     * Allocated as system memory
     * --------------------------------------------------------------------------
     */
-   void *generic_heap;
+   void *heap;
 
    /* rendering */
    glmesh skydome;
@@ -239,14 +248,17 @@ VG_STATIC struct world_global{
    u32 timer_text_count;
 
    struct text_particle{
-      rigidbody *rb;
+      rb_object obj;
+      m4x3f mlocal;
       ent_glyph *glyph;
+      v4f colour;
+
+      m4x3f mdl;
    }
    text_particles[6*4];
    u32 text_particle_count;
-
 }
-world_global;
+static world_global;
 
 VG_STATIC world_instance *get_active_world( void )
 {
@@ -269,10 +281,64 @@ void ray_world_get_tri( world_instance *world, ray_hit *hit, v3f tri[3] );
 VG_STATIC 
 int ray_world( world_instance *world, v3f pos, v3f dir, ray_hit *hit );
 
+VG_STATIC
+ent_spawn *world_find_closest_spawn( world_instance *world, v3f position )
+{
+   ent_spawn *rp = NULL, *r;
+   float min_dist = INFINITY;
+
+   for( u32 i=0; i<mdl_arrcount(&world->ent_spawn); i++ ){
+      r = mdl_arritm( &world->ent_spawn, i );
+      float d = v3_dist2( r->transform.co, position );
+      
+      if( d < min_dist ){
+         min_dist = d;
+         rp = r;
+      }
+   }
+
+   if( !rp ){
+      if( mdl_arrcount(&world->ent_spawn) ){
+         vg_warn( "Invalid distances to spawns.. defaulting to first one.\n" );
+         return mdl_arritm( &world->ent_spawn, 0 );
+      }
+      else{
+         vg_error( "There are no spawns in the level!\n" );
+      }
+   }
+
+   return rp;
+}
+
+VG_STATIC
+ent_spawn *world_find_spawn_by_name( world_instance *world, const char *name )
+{
+   ent_spawn *rp = NULL, *r;
+   for( u32 i=0; i<mdl_arrcount(&world->ent_spawn); i++ ){
+      r = mdl_arritm( &world->ent_spawn, i );
+      if( !strcmp( mdl_pstr(&world->meta, r->pstr_name), name ) ){
+         rp = r;
+         break;
+      }
+   }
+
+   if( !rp )
+      vg_warn( "No spawn named '%s'\n", name );
+
+   return rp;
+}
+
 /*
  * Submodules
  */
 
+VG_STATIC float
+   k_day_length            = 30.0f; /* minutes */
+
+VG_STATIC int   k_debug_light_indices = 0,
+                k_debug_light_complexity = 0,
+                k_light_preview = 0;
+
 #include "world_routes.h"
 #include "world_sfd.h"
 #include "world_render.h"
@@ -295,6 +361,11 @@ VG_STATIC int world_stop_sound( int argc, const char *argv[] )
 
 VG_STATIC void world_init(void)
 {
+   VG_VAR_F32( k_day_length );
+   VG_VAR_I32( k_debug_light_indices );
+   VG_VAR_I32( k_debug_light_complexity );
+   VG_VAR_I32( k_light_preview );
+
    world_global.sky_rate = 1.0;
    world_global.sky_target_rate = 1.0;
 
@@ -334,9 +405,8 @@ VG_STATIC void world_init(void)
 
    /* Allocate dynamic world memory arena */
    u32 max_size = 76*1024*1024;
-   world_global.generic_heap = vg_create_linear_allocator( vg_mem.rtmemory, 
-                                                           max_size,
-                                                           VG_MEMORY_SYSTEM );
+   world_global.heap = vg_create_linear_allocator( vg_mem.rtmemory, max_size,
+                                                   VG_MEMORY_SYSTEM );
 }
 
 typedef struct ent_call ent_call;
@@ -469,48 +539,6 @@ VG_STATIC void entity_call( world_instance *world, ent_call *call )
 
 VG_STATIC void world_update( world_instance *world, v3f pos )
 {
-   /* TEMP!!!!!! */
-   static double g_time = 0.0;
-   g_time += vg.time_delta * (1.0/(k_day_length*60.0));
-
-
-   struct ub_world_lighting *state = &world->ub_lighting;
-
-   state->g_time = g_time;
-   state->g_realtime = vg.time;
-   state->g_debug_indices = k_debug_light_indices;
-   state->g_light_preview = k_light_preview;
-   state->g_debug_complexity = k_debug_light_complexity;
-
-   state->g_time_of_day = vg_fractf( g_time );
-   state->g_day_phase   = cosf( state->g_time_of_day * VG_PIf * 2.0f );
-   state->g_sunset_phase= cosf( state->g_time_of_day * VG_PIf * 4.0f + VG_PIf );
-
-   state->g_day_phase    =       state->g_day_phase    * 0.5f + 0.5f;
-   state->g_sunset_phase = powf( state->g_sunset_phase * 0.5f + 0.5f, 6.0f );
-
-   float a = state->g_time_of_day * VG_PIf * 2.0f;
-   state->g_sun_dir[0] = sinf( a );
-   state->g_sun_dir[1] = cosf( a );
-   state->g_sun_dir[2] = 0.2f;
-   v3_normalize( state->g_sun_dir );
-
-
-   world->probabilities[ k_probability_curve_constant ] = 1.0f;
-
-   float dp = state->g_day_phase;
-
-   world->probabilities[ k_probability_curve_wildlife_day ] =
-      (dp*dp*0.8f+state->g_sunset_phase)*0.8f;
-   world->probabilities[ k_probability_curve_wildlife_night ] = 
-      1.0f-powf(fabsf((state->g_time_of_day-0.5f)*5.0f),5.0f);
-      
-
-   glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
-   glBufferSubData( GL_UNIFORM_BUFFER, 0, 
-                    sizeof(struct ub_world_lighting), &world->ub_lighting );
-   /* TEMP!!!!!! */
-
    world_global.sky_time += world_global.sky_rate * vg.time_delta;
    world_global.sky_rate = vg_lerp( world_global.sky_rate, 
                                     world_global.sky_target_rate, 
@@ -559,8 +587,6 @@ VG_STATIC void world_update( world_instance *world, v3f pos )
       }
    }
    sfd_update();
-   
-
 
    static float random_accum = 0.0f;
    random_accum += vg.time_delta;
@@ -649,29 +675,6 @@ VG_STATIC void world_update( world_instance *world, v3f pos )
    }
 
 #endif
-
-#if 0
-
-   /* process soundscape transactions */
-   audio_lock();
-   for( int i=0; i<world->soundscape_count; i++ )
-   {
-      struct soundscape *s = &world->soundscapes[i];
-      s->usage_count = 0;
-
-      for( int j=0; j<s->max_instances; j++ )
-      {
-         if( s->channels[j] )
-         {
-            if( audio_channel_finished(s->channels[j]) )
-               s->channels[j] = audio_relinquish_channel( s->channels[j] );
-            else
-               s->usage_count ++;
-         }
-      }
-   }
-   audio_unlock();
-#endif
 }
 
 /* 
@@ -784,4 +787,130 @@ VG_STATIC struct world_surface *ray_hit_surface( world_instance *world,
    return world_tri_index_surface( world, hit->tri[0] );
 }
 
+/* 
+ * -----------------------------------------------------------------------------
+ *                              Audio sampling
+ * -----------------------------------------------------------------------------
+ */
+
+VG_STATIC 
+enum audio_sprite_type world_audio_sample_sprite_random(v3f origin, v3f output);
+VG_STATIC void world_audio_sample_distances( v3f co, int *index, float *value );
+
+#include "audio.h"
+
+/*
+ * Trace out a random point, near the player to try and determine water areas
+ */
+VG_STATIC 
+enum audio_sprite_type world_audio_sample_sprite_random(v3f origin, v3f output)
+{
+   v3f chance = { (vg_randf()-0.5f) * 30.0f, 
+                  8.0f,
+                  (vg_randf()-0.5f) * 30.0f };
+   
+   v3f pos;
+   v3_add( chance, origin, pos );
+
+   ray_hit contact;
+   contact.dist = vg_minf( 16.0f, pos[1] );
+
+   world_instance *world = get_active_world();
+   
+   if( ray_world( world, pos, (v3f){0.0f,-1.0f,0.0f}, &contact ) ){
+      struct world_surface *mat = ray_hit_surface( world, &contact );
+
+      if( mat->info.surface_prop == k_surface_prop_grass){
+         v3_copy( contact.pos, output );
+         return k_audio_sprite_type_grass;
+      }
+      else{
+         return k_audio_sprite_type_none;
+      }
+   }
+
+   output[0] = pos[0];
+   output[1] = 0.0f;
+   output[2] = pos[2];
+
+   float dist = fabsf(output[1] - origin[1]);
+   
+   if( world->water.enabled && dist<=40.0f )
+      return k_audio_sprite_type_water;
+   else
+      return k_audio_sprite_type_none;
+}
+
+VG_STATIC void world_audio_sample_distances( v3f co, int *index, float *value )
+{
+   float inr3 = 0.57735027,
+         inr2 = 0.70710678118;
+
+   v3f sample_directions[] = {
+      { -1.0f,  0.0f,  0.0f },
+      {  1.0f,  0.0f,  0.0f },
+      {  0.0f,  0.0f,  1.0f },
+      {  0.0f,  0.0f, -1.0f },
+      {  0.0f,  1.0f,  0.0f },
+      {  0.0f, -1.0f,  0.0f },
+      { -inr3,  inr3,  inr3 },
+      {  inr3,  inr3,  inr3 },
+      { -inr3,  inr3, -inr3 },
+      {  inr3,  inr3, -inr3 },
+      { -inr2,  0.0f,  inr2 },
+      {  inr2,  0.0f,  inr2 },
+      { -inr2,  0.0f, -inr2 },
+      {  inr2,  0.0f, -inr2 },
+   };
+
+   static int si = 0;
+   static float distances[16];
+
+   ray_hit ray;
+   ray.dist = 5.0f;
+
+   v3f rc, rd, ro;
+   v3_copy( sample_directions[ si ], rd );
+   v3_add( co, (v3f){0.0f,1.5f,0.0f}, ro );
+   v3_copy( ro, rc );
+
+   float dist = 200.0f;
+
+   for( int i=0; i<10; i++ ){
+      if( ray_world( get_active_world(), rc, rd, &ray ) ){
+         dist = (float)i*5.0f + ray.dist;
+         break;
+      }
+      else{
+         v3_muladds( rc, rd, ray.dist, rc );
+      }
+   }
+
+   distances[si] = dist;
+
+   if( vg_lines.draw ){
+      for( int i=0; i<14; i++ ){
+         if( distances[i] != 200.0f ){
+            u32 colours[] = { VG__RED, VG__BLUE, VG__GREEN,
+                              VG__CYAN, VG__YELOW, VG__PINK,
+                              VG__WHITE };
+
+            u32 colour = colours[i%7];
+
+            v3f p1;
+            v3_muladds( ro, sample_directions[i], distances[i], p1 );
+            vg_line( ro, p1, colour );
+            vg_line_pt3( p1, 0.1f, colour );
+         }
+      }
+   }
+
+   *index = si;
+   *value = dist;
+
+   si ++;
+   if( si >= 14 )
+      si = 0;
+}
+
 #endif /* WORLD_H */