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
88 v4f g_light_colours
[3],
89 g_light_directions
[3],
102 int g_debug_complexity
;
105 v4f g_point_light_positions
[32];
106 v4f g_point_light_colours
[32];
113 GLuint tbo_light_entities
,
119 struct framebuffer heightmap
;
122 * Dynamically allocated when world_load is called.
124 * the following arrays index somewhere into this linear
128 * --------------------------------------------------------------------------
136 * Materials / textures
142 struct world_material
152 * Named safe places to respawn
164 * Audio player entities
166 struct world_audio_thing
173 audio_clip temp_embedded_clip
;
176 u32 audio_things_count
;
188 enum classtype classtype
;
197 * Box trigger entities
201 m4x3f transform
, inv_transform
;
203 struct relay_target target
;
211 struct logic_achievement
214 const char *achievement_id
;
217 * logic_achievements
;
218 u32 achievement_count
;
226 struct classtype_world_light
*inf
;
231 * TODO: we should order entities in the binary by their type */
237 * Routes (world_routes.h)
238 * --------------------------------------------------------------------------
242 v3f co
, right
, up
, h
;
245 u32 special_type
, special_id
, current_refs
, ref_count
;
246 u32 route_ids
[4]; /* Gates can be linked into up to four routes */
262 double best_lap
, latest_pass
; /* Session */
264 m4x3f scoreboard_transform
;
277 m4x3f to_world
, transport
;
285 u32 version
; /* Incremented on every teleport */
295 struct teleport_gate gate
;
298 u32 target_map_index
, working
;
301 u32 nonlocalgate_count
;
303 struct route_collector
305 struct route_timing timing
;
312 * ----------------------------------------------------
320 /* spacial mappings */
326 glmesh mesh_route_lines
;
331 rigidbody rb_geo
; /* todo.. ... */
334 VG_STATIC
struct world_global
337 * Allocated as system memory
338 * --------------------------------------------------------------------------
341 *audio_heap
; /* sub buffer of the audio buffer */
345 mdl_submesh dome_upper
, dome_lower
;
347 glmesh mesh_gate_surface
;
349 double sky_time
, sky_rate
, sky_target_rate
;
351 /* gates, TODO: active_gate should also know which instance */
354 double time
, rewind_from
, rewind_to
, last_use
;
356 /* water rendering */
359 struct framebuffer fbreflect
, fbdepth
;
363 /* split flap display */
366 mdl_submesh
*sm_module
, *sm_card
;
367 glmesh mesh_base
, mesh_display
;
374 /* timing bars, fixed maximum amount */
377 GLuint vao
, vbo
, ebo
;
382 struct route_ui_segment
385 u32 vertex_start
, vertex_count
,
386 index_start
, index_count
, notches
;
388 segments
[k_max_ui_segments
];
390 u32 segment_start
, segment_count
, fade_start
, fade_count
;
391 double fade_timer_start
;
397 int active_route_board
;
400 int switching_to_new_world
;
402 world_instance worlds
[4];
408 VG_STATIC world_instance
*get_active_world( void )
410 return &world_global
.worlds
[ world_global
.active_world
];
418 int ray_hit_is_ramp( world_instance
*world
, ray_hit
*hit
);
421 struct world_material
*ray_hit_material( world_instance
*world
, ray_hit
*hit
);
424 void ray_world_get_tri( world_instance
*world
, ray_hit
*hit
, v3f tri
[3] );
427 int ray_world( world_instance
*world
, v3f pos
, v3f dir
, ray_hit
*hit
);
433 #include "world_routes.h"
434 #include "world_sfd.h"
435 #include "world_render.h"
436 #include "world_water.h"
437 #include "world_gen.h"
438 #include "world_gate.h"
441 * -----------------------------------------------------------------------------
443 * -----------------------------------------------------------------------------
446 VG_STATIC
int world_stop_sound( int argc
, const char *argv
[] )
448 world_instance
*world
= get_active_world();
451 * None of our world audio runs as one shots, they always have a player.
452 * Therefore it is safe to delete clip data after the players are
456 for( int i
=0; i
<world
->audio_things_count
; i
++ )
458 struct world_audio_thing
*at
= &world
->audio_things
[i
];
460 if( audio_player_is_playing( &at
->player
) )
462 u32 cflags
= audio_player_get_flags( &at
->player
);
463 audio_player_set_flags( &at
->player
, cflags
| AUDIO_FLAG_KILL
);
471 VG_STATIC
int world_change_world( int argc
, const char *argv
[] )
474 world_instance
*world
= get_active_world();
478 vg_info( "%s\n", world
.world_name
);
483 vg_info( "Switching world...\n" );
484 strcpy( world
.world_name
, argv
[0] );
485 world
.switching_to_new_world
= 1;
486 world_stop_sound( 0, NULL
);
493 VG_STATIC
void world_init(void)
496 vg_var_push( (struct vg_var
){
497 .name
= "water_enable",
498 .data
= &world
.water
.enabled
,
499 .data_type
= k_var_dtype_i32
,
500 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
505 vg_function_push( (struct vg_cmd
)
507 .name
= "world_stop_sound",
508 .function
= world_stop_sound
511 vg_function_push( (struct vg_cmd
)
514 .function
= world_change_world
517 world_global
.sky_rate
= 1.0;
518 world_global
.sky_target_rate
= 1.0;
520 shader_scene_standard_register();
521 shader_scene_standard_alphatest_register();
522 shader_scene_vertex_blend_register();
523 shader_scene_terrain_register();
524 shader_scene_depth_register();
525 shader_scene_position_register();
527 shader_model_sky_register();
529 vg_info( "Loading world resources\n" );
531 vg_linear_clear( vg_mem
.scratch
);
532 mdl_context
*msky
= mdl_load_full( vg_mem
.scratch
, "models/rs_skydome.mdl" );
534 mdl_node
*nupper
= mdl_node_from_name( msky
, "dome_complete" );
535 world_global
.dome_upper
= *mdl_node_submesh( msky
, nupper
, 0 );
537 vg_acquire_thread_sync();
539 mdl_unpack_glmesh( msky
, &world_global
.skydome
);
541 vg_release_thread_sync();
544 vg_info( "Loading other world systems\n" );
546 vg_loader_step( world_render_init
, NULL
);
547 vg_loader_step( world_sfd_init
, NULL
);
548 vg_loader_step( world_water_init
, NULL
);
549 vg_loader_step( world_gates_init
, NULL
);
550 vg_loader_step( world_routes_init
, NULL
);
552 /* Allocate dynamic world memory arena */
553 u32 max_size
= 76*1024*1024;
554 world_global
.generic_heap
= vg_create_linear_allocator( vg_mem
.rtmemory
,
559 VG_STATIC
void world_audio_init(void)
561 u32 size
= vg_linear_remaining( vg_audio
.audio_pool
)
562 - sizeof(vg_linear_allocator
);
564 world_global
.audio_heap
= vg_create_linear_allocator( vg_audio
.audio_pool
,
569 VG_STATIC
void world_trigger_achievement( world_instance
*world
, u32 uid
)
571 struct logic_achievement
*ach
= &world
->logic_achievements
[ uid
];
576 steam_set_achievement( ach
->achievement_id
);
577 steam_store_achievements();
582 VG_STATIC
void world_run_relay( world_instance
*world
,
583 struct relay_target
*rt
);
585 VG_STATIC
void world_trigger_relay( world_instance
*world
, u32 uid
)
587 struct logic_relay
*relay
= &world
->logic_relays
[ uid
];
589 for( int i
=0; i
<relay
->target_count
; i
++ )
591 world_run_relay( world
, &relay
->targets
[i
] );
595 VG_STATIC
void world_trigger_audio( world_instance
*world
, u32 uid
)
597 struct world_audio_thing
*wat
= &world
->audio_things
[ uid
];
600 audio_player_playclip( &wat
->player
,
601 &wat
->temp_embedded_clip
);
605 VG_STATIC
void world_run_relay( world_instance
*world
,
606 struct relay_target
*rt
)
608 struct entity_instruction
610 enum classtype classtype
;
611 void (*p_trigger
)( world_instance
*world
, u32 uid
);
613 entity_instructions
[] =
615 { k_classtype_logic_achievement
, world_trigger_achievement
},
616 { k_classtype_logic_relay
, world_trigger_relay
},
617 { k_classtype_audio
, world_trigger_audio
}
620 for( int i
=0; i
<vg_list_size(entity_instructions
); i
++ )
622 struct entity_instruction
*instr
= &entity_instructions
[i
];
624 if( instr
->classtype
== rt
->classtype
)
626 instr
->p_trigger( world
, rt
->sub_id
);
631 vg_error( "Don't know how to trigger classtype %d\n", rt
->classtype
);
634 VG_STATIC
void world_update( world_instance
*world
, v3f pos
)
637 static double g_time
= 0.0;
638 g_time
+= vg
.time_delta
* (1.0/(k_day_length
*60.0));
640 world
->ub_lighting
.g_time
= g_time
;
641 world
->ub_lighting
.g_debug_indices
= k_debug_light_indices
;
642 world
->ub_lighting
.g_debug_complexity
= k_debug_light_complexity
;
644 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
645 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
646 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
651 if( world
.switching_to_new_world
)
656 for( int i
=0; i
<world
.audio_things_count
; i
++ )
658 struct world_audio_thing
*at
= &world
.audio_things
[i
];
660 if( audio_player_is_playing( &at
->player
) )
670 world
.switching_to_new_world
= 0;
672 vg_loader_start( world_load
);
678 world_global
.sky_time
+= world_global
.sky_rate
* vg
.time_delta
;
679 world_global
.sky_rate
= vg_lerp( world_global
.sky_rate
,
680 world_global
.sky_target_rate
,
681 vg
.time_delta
* 5.0 );
683 world_routes_update( world
);
685 world_routes_debug();
688 if( world
->route_count
> 0 )
691 float min_dist
= INFINITY
;
693 for( int i
=0; i
<world
->route_count
; i
++ )
695 float d
= v3_dist2( world
->routes
[i
].scoreboard_transform
[3], pos
);
704 if( (world_global
.active_route_board
!= closest
)
705 || network_scores_updated
)
707 network_scores_updated
= 0;
708 world_global
.active_route_board
= closest
;
710 struct route
*route
= &world
->routes
[closest
];
712 u32 id
= route
->track_id
;
714 if( id
!= 0xffffffff )
716 struct netmsg_board
*local_board
=
717 &scoreboard_client_data
.boards
[id
];
719 for( int i
=0; i
<13; i
++ )
721 sfd_encode( i
, &local_board
->data
[27*i
] );
728 for( int i
=0; i
<world
->trigger_count
; i
++ )
730 struct trigger_zone
*zone
= &world
->triggers
[i
];
733 m4x3_mulv( zone
->inv_transform
, pos
, local
);
735 if( (fabsf(local
[0]) <= 1.0f
) &&
736 (fabsf(local
[1]) <= 1.0f
) &&
737 (fabsf(local
[2]) <= 1.0f
) )
741 if( !world_global
.in_trigger
)
743 world_run_relay( world
, &zone
->target
);
747 vg_line_boxf_transformed( zone
->transform
, (boxf
){{-1.0f
,-1.0f
,-1.0f
},
748 { 1.0f
, 1.0f
, 1.0f
}},
752 if( k_debug_light_indices
)
754 for( int i
=0; i
<world
->light_count
; i
++ )
756 struct world_light
*light
= &world
->lights
[i
];
757 struct classtype_world_light
*inf
= light
->inf
;
759 u32 colour
= 0xff000000;
760 u8 r
= inf
->colour
[0] * 255.0f
,
761 g
= inf
->colour
[1] * 255.0f
,
762 b
= inf
->colour
[2] * 255.0f
;
768 vg_line_pt3( light
->node
->co
, 0.25f
, colour
);
772 world_global
.in_trigger
= in_trigger
;
777 * -----------------------------------------------------------------------------
779 * -----------------------------------------------------------------------------
782 VG_STATIC
void ray_world_get_tri( world_instance
*world
,
783 ray_hit
*hit
, v3f tri
[3] )
785 for( int i
=0; i
<3; i
++ )
786 v3_copy( world
->scene_geo
->arrvertices
[ hit
->tri
[i
] ].co
, tri
[i
] );
789 VG_STATIC
int ray_world( world_instance
*world
,
790 v3f pos
, v3f dir
, ray_hit
*hit
)
792 return scene_raycast( world
->scene_geo
, world
->geo_bh
, pos
, dir
, hit
);
796 * Cast a sphere from a to b and see what time it hits
798 VG_STATIC
int spherecast_world( world_instance
*world
,
799 v3f pa
, v3f pb
, float r
, float *t
, v3f n
)
802 bh_iter_init( 0, &it
);
805 box_init_inf( region
);
806 box_addpt( region
, pa
);
807 box_addpt( region
, pb
);
809 v3_add( (v3f
){ r
, r
, r
}, region
[1], region
[1] );
810 v3_add( (v3f
){-r
,-r
,-r
}, region
[0], region
[0] );
813 v3_sub( pb
, pa
, dir
);
816 dir_inv
[0] = 1.0f
/dir
[0];
817 dir_inv
[1] = 1.0f
/dir
[1];
818 dir_inv
[2] = 1.0f
/dir
[2];
824 while( bh_next( world
->geo_bh
, &it
, region
, &idx
) )
826 u32
*ptri
= &world
->scene_geo
->arrindices
[ idx
*3 ];
832 for( int j
=0; j
<3; j
++ )
834 v3_copy( world
->scene_geo
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
835 box_addpt( box
, tri
[j
] );
838 v3_add( (v3f
){ r
, r
, r
}, box
[1], box
[1] );
839 v3_add( (v3f
){-r
,-r
,-r
}, box
[0], box
[0] );
841 if( !ray_aabb1( box
, pa
, dir_inv
, 1.0f
) )
846 if( spherecast_triangle( tri
, pa
, dir
, r
, &t
, n1
) )
862 struct world_material
*world_tri_index_material( world_instance
*world
,
865 for( int i
=1; i
<world
->material_count
; i
++ )
867 struct world_material
*mat
= &world
->materials
[i
];
869 if( (index
>= mat
->sm_geo
.vertex_start
) &&
870 (index
< mat
->sm_geo
.vertex_start
+mat
->sm_geo
.vertex_count
) )
877 return &world
->materials
[0];
880 VG_STATIC
struct world_material
*world_contact_material( world_instance
*world
,
883 return world_tri_index_material( world
, ct
->element_id
);
886 VG_STATIC
struct world_material
*ray_hit_material( world_instance
*world
,
889 return world_tri_index_material( world
, hit
->tri
[0] );