2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
10 typedef struct world_instance world_instance
;
12 #include "vg/vg_loader.h"
15 #include "network_msg.h"
18 #include "rigidbody.h"
22 #include "shaders/scene_standard.h"
23 #include "shaders/scene_standard_alphatest.h"
24 #include "shaders/scene_vertex_blend.h"
25 #include "shaders/scene_terrain.h"
26 #include "shaders/scene_depth.h"
27 #include "shaders/scene_position.h"
29 #include "shaders/model_sky.h"
31 typedef struct teleport_gate teleport_gate
;
33 enum { k_max_ui_segments
= 8 };
35 enum { k_max_ui_elements
= k_max_ui_segments
};
36 enum { k_max_element_verts
= 10 };
37 enum { k_max_element_indices
= 20 };
39 enum { k_route_ui_max_verts
= k_max_ui_elements
*k_max_element_verts
};
40 enum { k_route_ui_max_indices
= k_max_ui_elements
*k_max_element_indices
};
44 k_logic_type_relay
= 1,
45 k_logic_type_chance
= 2,
46 k_logic_type_achievement
= 3
52 k_geo_type_nonsolid
= 1,
56 static const float k_light_cube_size
= 8.0f
;
60 /* This is a small flag we use to changelevel.
61 * It will not be cleared until all sounds stop playing
65 * -------------------------------------------------------
68 char world_name
[ 64 ];
82 struct ub_world_lighting
91 v4f g_nightsky_colour
;
100 float g_shadow_length
;
101 float g_shadow_spread
;
105 float g_sunset_phase
;
108 int g_shadow_samples
;
111 int g_debug_complexity
;
117 GLuint tbo_light_entities
,
123 struct framebuffer heightmap
;
126 * Dynamically allocated when world_load is called.
128 * the following arrays index somewhere into this linear
132 * --------------------------------------------------------------------------
140 * Materials / textures
146 struct world_material
156 * Named safe places to respawn
168 * Audio player entities
170 struct world_audio_thing
175 audio_clip temp_embedded_clip
;
178 u32 audio_things_count
;
183 audio_channel
*channels
[4];
185 /* accessable without locking */
190 u32 allow_transitions
;
191 float transition_duration
;
195 u32 soundscape_count
;
198 * Box volume entities
202 m4x3f transform
, inv_transform
;
214 struct classtype_world_light
*inf
;
219 * TODO: we should order entities in the binary by their type */
225 * Routes (world_routes.h)
226 * --------------------------------------------------------------------------
230 v3f co
, right
, up
, h
;
233 u32 special_type
, special_id
, current_refs
, ref_count
;
234 u32 route_ids
[4]; /* Gates can be linked into up to four routes */
250 double best_lap
, latest_pass
; /* Session */
252 m4x3f scoreboard_transform
;
265 m4x3f to_world
, transport
;
273 u32 version
; /* Incremented on every teleport */
283 struct teleport_gate gate
;
286 u32 target_map_index
, working
;
289 u32 nonlocalgate_count
;
291 struct route_collector
293 struct route_timing timing
;
300 * ----------------------------------------------------
308 /* spacial mappings */
314 glmesh mesh_route_lines
;
319 rigidbody rb_geo
; /* todo.. ... */
322 VG_STATIC
struct world_global
325 * Allocated as system memory
326 * --------------------------------------------------------------------------
332 mdl_submesh dome_upper
, dome_lower
;
334 glmesh mesh_gate_surface
;
336 double sky_time
, sky_rate
, sky_target_rate
;
338 /* gates, TODO: active_gate should also know which instance */
341 double time
, rewind_from
, rewind_to
, last_use
;
343 /* water rendering */
346 struct framebuffer fbreflect
, fbdepth
;
350 /* split flap display */
353 mdl_submesh
*sm_module
, *sm_card
;
354 glmesh mesh_base
, mesh_display
;
361 /* timing bars, fixed maximum amount */
364 GLuint vao
, vbo
, ebo
;
369 struct route_ui_segment
372 u32 vertex_start
, vertex_count
,
373 index_start
, index_count
, notches
;
375 segments
[k_max_ui_segments
];
377 u32 segment_start
, segment_count
, fade_start
, fade_count
;
378 double fade_timer_start
;
384 int active_route_board
;
387 int switching_to_new_world
;
389 world_instance worlds
[4];
395 VG_STATIC world_instance
*get_active_world( void )
397 return &world_global
.worlds
[ world_global
.active_world
];
405 int ray_hit_is_ramp( world_instance
*world
, ray_hit
*hit
);
408 struct world_material
*ray_hit_material( world_instance
*world
, ray_hit
*hit
);
411 void ray_world_get_tri( world_instance
*world
, ray_hit
*hit
, v3f tri
[3] );
414 int ray_world( world_instance
*world
, v3f pos
, v3f dir
, ray_hit
*hit
);
420 #include "world_routes.h"
421 #include "world_sfd.h"
422 #include "world_render.h"
423 #include "world_water.h"
424 #include "world_volumes.h"
425 #include "world_gen.h"
426 #include "world_gate.h"
429 * -----------------------------------------------------------------------------
431 * -----------------------------------------------------------------------------
434 VG_STATIC
int world_stop_sound( int argc
, const char *argv
[] )
436 world_instance
*world
= get_active_world();
440 VG_STATIC
void world_init(void)
442 world_global
.sky_rate
= 1.0;
443 world_global
.sky_target_rate
= 1.0;
445 shader_scene_standard_register();
446 shader_scene_standard_alphatest_register();
447 shader_scene_vertex_blend_register();
448 shader_scene_terrain_register();
449 shader_scene_depth_register();
450 shader_scene_position_register();
452 shader_model_sky_register();
454 vg_info( "Loading world resources\n" );
456 vg_linear_clear( vg_mem
.scratch
);
457 mdl_context
*msky
= mdl_load_full( vg_mem
.scratch
, "models/rs_skydome.mdl" );
459 mdl_node
*nupper
= mdl_node_from_name( msky
, "dome_complete" );
460 world_global
.dome_upper
= *mdl_node_submesh( msky
, nupper
, 0 );
462 vg_acquire_thread_sync();
464 mdl_unpack_glmesh( msky
, &world_global
.skydome
);
466 vg_release_thread_sync();
469 vg_info( "Loading other world systems\n" );
471 vg_loader_step( world_render_init
, NULL
);
472 vg_loader_step( world_sfd_init
, NULL
);
473 vg_loader_step( world_water_init
, NULL
);
474 vg_loader_step( world_gates_init
, NULL
);
475 vg_loader_step( world_routes_init
, NULL
);
477 /* Allocate dynamic world memory arena */
478 u32 max_size
= 76*1024*1024;
479 world_global
.generic_heap
= vg_create_linear_allocator( vg_mem
.rtmemory
,
484 VG_STATIC
void world_update( world_instance
*world
, v3f pos
)
487 static double g_time
= 0.0;
488 g_time
+= vg
.time_delta
* (1.0/(k_day_length
*60.0));
491 struct ub_world_lighting
*state
= &world
->ub_lighting
;
493 state
->g_time
= g_time
;
494 state
->g_debug_indices
= k_debug_light_indices
;
495 state
->g_light_preview
= k_light_preview
;
496 state
->g_debug_complexity
= k_debug_light_complexity
;
498 state
->g_time_of_day
= vg_fractf( g_time
);
499 state
->g_day_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 2.0f
);
500 state
->g_sunset_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 4.0f
+ VG_PIf
);
502 state
->g_day_phase
= state
->g_day_phase
* 0.5f
+ 0.5f
;
503 state
->g_sunset_phase
= powf( state
->g_sunset_phase
* 0.5f
+ 0.5f
, 6.0f
);
505 float a
= state
->g_time_of_day
* VG_PIf
* 2.0f
;
506 state
->g_sun_dir
[0] = sinf( a
);
507 state
->g_sun_dir
[1] = cosf( a
);
508 state
->g_sun_dir
[2] = 0.2f
;
509 v3_normalize( state
->g_sun_dir
);
513 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
514 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
515 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
518 world_global
.sky_time
+= world_global
.sky_rate
* vg
.time_delta
;
519 world_global
.sky_rate
= vg_lerp( world_global
.sky_rate
,
520 world_global
.sky_target_rate
,
521 vg
.time_delta
* 5.0 );
523 world_routes_update( world
);
525 world_routes_debug();
528 if( world
->route_count
> 0 )
531 float min_dist
= INFINITY
;
533 for( int i
=0; i
<world
->route_count
; i
++ )
535 float d
= v3_dist2( world
->routes
[i
].scoreboard_transform
[3], pos
);
544 if( (world_global
.active_route_board
!= closest
)
545 || network_scores_updated
)
547 network_scores_updated
= 0;
548 world_global
.active_route_board
= closest
;
550 struct route
*route
= &world
->routes
[closest
];
552 u32 id
= route
->track_id
;
554 if( id
!= 0xffffffff )
556 struct netmsg_board
*local_board
=
557 &scoreboard_client_data
.boards
[id
];
559 for( int i
=0; i
<13; i
++ )
561 sfd_encode( i
, &local_board
->data
[27*i
] );
569 static float random_accum
= 0.0f
;
570 random_accum
+= vg
.time_delta
;
572 u32 random_ticks
= 0;
574 while( random_accum
> 0.1f
)
576 random_accum
-= 0.1f
;
580 float radius
= 25.0f
;
581 boxf volume_proximity
;
582 v3_add( pos
, (v3f
){ radius
, radius
, radius
}, volume_proximity
[1] );
583 v3_sub( pos
, (v3f
){ radius
, radius
, radius
}, volume_proximity
[0] );
586 bh_iter_init( 0, &it
);
591 while( bh_next( world
->volume_bh
, &it
, volume_proximity
, &idx
) )
593 struct world_volume
*zone
= &world
->volumes
[idx
];
595 if( zone
->node
->classtype
== k_classtype_volume_audio
)
597 vg_line_boxf_transformed( zone
->transform
, (boxf
){{-1.0f
,-1.0f
,-1.0f
},
598 { 1.0f
, 1.0f
, 1.0f
}},
601 for( int j
=0; j
<random_ticks
; j
++ )
604 packet
.location
= zone
->target_logic_brick
;
607 packet
.type
= k_mdl_128bit_datatype_vec3
;
608 packet
.data
._v4f
[0] = vg_randf()*2.0f
-1.0f
;
609 packet
.data
._v4f
[1] = vg_randf()*2.0f
-1.0f
;
610 packet
.data
._v4f
[2] = vg_randf()*2.0f
-1.0f
;
611 m4x3_mulv( zone
->transform
, packet
.data
._v4f
, packet
.data
._v4f
);
613 logic_bricks_send_packet( world
, &packet
);
620 m4x3_mulv( zone
->inv_transform
, pos
, local
);
622 if( (fabsf(local
[0]) <= 1.0f
) &&
623 (fabsf(local
[1]) <= 1.0f
) &&
624 (fabsf(local
[2]) <= 1.0f
) )
628 if( !world_global
.in_volume
)
632 packet
.location
= zone
->target_logic_brick
;
635 packet
.type
= k_mdl_128bit_datatype_vec3
;
636 v3_copy( pos
, packet
.data
._v4f
);
638 logic_bricks_send_packet( world
, &packet
);
642 vg_line_boxf_transformed( zone
->transform
, (boxf
){{-1.0f
,-1.0f
,-1.0f
},
643 { 1.0f
, 1.0f
, 1.0f
}},
648 vg_line_boxf_transformed( zone
->transform
, (boxf
){{-1.0f
,-1.0f
,-1.0f
},
649 { 1.0f
, 1.0f
, 1.0f
}},
654 if( k_debug_light_indices
)
656 for( int i
=0; i
<world
->light_count
; i
++ )
658 struct world_light
*light
= &world
->lights
[i
];
659 struct classtype_world_light
*inf
= light
->inf
;
661 u32 colour
= 0xff000000;
662 u8 r
= inf
->colour
[0] * 255.0f
,
663 g
= inf
->colour
[1] * 255.0f
,
664 b
= inf
->colour
[2] * 255.0f
;
670 vg_line_pt3( light
->node
->co
, 0.25f
, colour
);
674 world_global
.in_volume
= in_volume
;
677 /* process soundscape transactions */
679 for( int i
=0; i
<world
->soundscape_count
; i
++ )
681 struct soundscape
*s
= &world
->soundscapes
[i
];
684 for( int j
=0; j
<s
->max_instances
; j
++ )
688 if( audio_channel_finished(s
->channels
[j
]) )
689 s
->channels
[j
] = audio_relinquish_channel( s
->channels
[j
] );
699 * -----------------------------------------------------------------------------
701 * -----------------------------------------------------------------------------
704 VG_STATIC
void ray_world_get_tri( world_instance
*world
,
705 ray_hit
*hit
, v3f tri
[3] )
707 for( int i
=0; i
<3; i
++ )
708 v3_copy( world
->scene_geo
->arrvertices
[ hit
->tri
[i
] ].co
, tri
[i
] );
711 VG_STATIC
int ray_world( world_instance
*world
,
712 v3f pos
, v3f dir
, ray_hit
*hit
)
714 return scene_raycast( world
->scene_geo
, world
->geo_bh
, pos
, dir
, hit
);
718 * Cast a sphere from a to b and see what time it hits
720 VG_STATIC
int spherecast_world( world_instance
*world
,
721 v3f pa
, v3f pb
, float r
, float *t
, v3f n
)
724 bh_iter_init( 0, &it
);
727 box_init_inf( region
);
728 box_addpt( region
, pa
);
729 box_addpt( region
, pb
);
731 v3_add( (v3f
){ r
, r
, r
}, region
[1], region
[1] );
732 v3_add( (v3f
){-r
,-r
,-r
}, region
[0], region
[0] );
735 v3_sub( pb
, pa
, dir
);
738 dir_inv
[0] = 1.0f
/dir
[0];
739 dir_inv
[1] = 1.0f
/dir
[1];
740 dir_inv
[2] = 1.0f
/dir
[2];
746 while( bh_next( world
->geo_bh
, &it
, region
, &idx
) )
748 u32
*ptri
= &world
->scene_geo
->arrindices
[ idx
*3 ];
754 for( int j
=0; j
<3; j
++ )
756 v3_copy( world
->scene_geo
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
757 box_addpt( box
, tri
[j
] );
760 v3_add( (v3f
){ r
, r
, r
}, box
[1], box
[1] );
761 v3_add( (v3f
){-r
,-r
,-r
}, box
[0], box
[0] );
763 if( !ray_aabb1( box
, pa
, dir_inv
, 1.0f
) )
768 if( spherecast_triangle( tri
, pa
, dir
, r
, &t
, n1
) )
784 struct world_material
*world_tri_index_material( world_instance
*world
,
787 for( int i
=1; i
<world
->material_count
; i
++ )
789 struct world_material
*mat
= &world
->materials
[i
];
791 if( (index
>= mat
->sm_geo
.vertex_start
) &&
792 (index
< mat
->sm_geo
.vertex_start
+mat
->sm_geo
.vertex_count
) )
799 return &world
->materials
[0];
802 VG_STATIC
struct world_material
*world_contact_material( world_instance
*world
,
805 return world_tri_index_material( world
, ct
->element_id
);
808 VG_STATIC
struct world_material
*ray_hit_material( world_instance
*world
,
811 return world_tri_index_material( world
, hit
->tri
[0] );