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,
58 /* This is a small flag we use to changelevel.
59 * It will not be cleared until all sounds stop playing
63 * -------------------------------------------------------
66 char world_name
[ 64 ];
80 struct ub_world_lighting
83 v4f g_light_colours
[3],
84 g_light_directions
[3],
97 int g_debug_complexity
;
100 v4f g_point_light_positions
[32];
101 v4f g_point_light_colours
[32];
108 GLuint tbo_light_entities
,
111 struct framebuffer heightmap
;
114 * Dynamically allocated when world_load is called.
116 * the following arrays index somewhere into this linear
120 * --------------------------------------------------------------------------
128 * Materials / textures
134 struct world_material
144 * Named safe places to respawn
156 * Audio player entities
158 struct world_audio_thing
165 audio_clip temp_embedded_clip
;
168 u32 audio_things_count
;
180 enum classtype classtype
;
189 * Box trigger entities
193 m4x3f transform
, inv_transform
;
195 struct relay_target target
;
203 struct logic_achievement
206 const char *achievement_id
;
209 * logic_achievements
;
210 u32 achievement_count
;
218 struct classtype_world_light
*inf
;
221 * TODO: we should order entities in the binary by their type */
227 * Routes (world_routes.h)
228 * --------------------------------------------------------------------------
232 v3f co
, right
, up
, h
;
235 u32 special_type
, special_id
, current_refs
, ref_count
;
236 u32 route_ids
[4]; /* Gates can be linked into up to four routes */
252 double best_lap
, latest_pass
; /* Session */
254 m4x3f scoreboard_transform
;
267 m4x3f to_world
, transport
;
275 u32 version
; /* Incremented on every teleport */
285 struct teleport_gate gate
;
288 u32 target_map_index
, working
;
291 u32 nonlocalgate_count
;
293 struct route_collector
295 struct route_timing timing
;
302 * ----------------------------------------------------
310 /* spacial mappings */
316 glmesh mesh_route_lines
;
321 rigidbody rb_geo
; /* todo.. ... */
324 VG_STATIC
struct world_global
327 * Allocated as system memory
328 * --------------------------------------------------------------------------
331 *audio_heap
; /* sub buffer of the audio buffer */
335 mdl_submesh dome_upper
, dome_lower
;
337 glmesh mesh_gate_surface
;
339 double sky_time
, sky_rate
, sky_target_rate
;
341 /* gates, TODO: active_gate should also know which instance */
344 double time
, rewind_from
, rewind_to
, last_use
;
346 /* water rendering */
349 struct framebuffer fbreflect
, fbdepth
;
353 /* split flap display */
356 mdl_submesh
*sm_module
, *sm_card
;
357 glmesh mesh_base
, mesh_display
;
364 /* timing bars, fixed maximum amount */
367 GLuint vao
, vbo
, ebo
;
372 struct route_ui_segment
375 u32 vertex_start
, vertex_count
,
376 index_start
, index_count
, notches
;
378 segments
[k_max_ui_segments
];
380 u32 segment_start
, segment_count
, fade_start
, fade_count
;
381 double fade_timer_start
;
387 int active_route_board
;
390 int switching_to_new_world
;
392 world_instance worlds
[4];
398 VG_STATIC world_instance
*get_active_world( void )
400 return &world_global
.worlds
[ world_global
.active_world
];
408 int ray_hit_is_ramp( world_instance
*world
, ray_hit
*hit
);
411 struct world_material
*ray_hit_material( world_instance
*world
, ray_hit
*hit
);
414 void ray_world_get_tri( world_instance
*world
, ray_hit
*hit
, v3f tri
[3] );
417 int ray_world( world_instance
*world
, v3f pos
, v3f dir
, ray_hit
*hit
);
423 #include "world_routes.h"
424 #include "world_sfd.h"
425 #include "world_render.h"
426 #include "world_water.h"
427 #include "world_gen.h"
428 #include "world_gate.h"
431 * -----------------------------------------------------------------------------
433 * -----------------------------------------------------------------------------
436 VG_STATIC
int world_stop_sound( int argc
, const char *argv
[] )
438 world_instance
*world
= get_active_world();
441 * None of our world audio runs as one shots, they always have a player.
442 * Therefore it is safe to delete clip data after the players are
446 for( int i
=0; i
<world
->audio_things_count
; i
++ )
448 struct world_audio_thing
*at
= &world
->audio_things
[i
];
450 if( audio_player_is_playing( &at
->player
) )
452 u32 cflags
= audio_player_get_flags( &at
->player
);
453 audio_player_set_flags( &at
->player
, cflags
| AUDIO_FLAG_KILL
);
461 VG_STATIC
int world_change_world( int argc
, const char *argv
[] )
464 world_instance
*world
= get_active_world();
468 vg_info( "%s\n", world
.world_name
);
473 vg_info( "Switching world...\n" );
474 strcpy( world
.world_name
, argv
[0] );
475 world
.switching_to_new_world
= 1;
476 world_stop_sound( 0, NULL
);
483 VG_STATIC
void world_init(void)
486 vg_var_push( (struct vg_var
){
487 .name
= "water_enable",
488 .data
= &world
.water
.enabled
,
489 .data_type
= k_var_dtype_i32
,
490 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
495 vg_function_push( (struct vg_cmd
)
497 .name
= "world_stop_sound",
498 .function
= world_stop_sound
501 vg_function_push( (struct vg_cmd
)
504 .function
= world_change_world
507 world_global
.sky_rate
= 1.0;
508 world_global
.sky_target_rate
= 1.0;
510 shader_scene_standard_register();
511 shader_scene_standard_alphatest_register();
512 shader_scene_vertex_blend_register();
513 shader_scene_terrain_register();
514 shader_scene_depth_register();
515 shader_scene_position_register();
517 shader_model_sky_register();
519 vg_info( "Loading world resources\n" );
521 vg_linear_clear( vg_mem
.scratch
);
522 mdl_context
*msky
= mdl_load_full( vg_mem
.scratch
, "models/rs_skydome.mdl" );
524 mdl_node
*nupper
= mdl_node_from_name( msky
, "dome_complete" );
525 world_global
.dome_upper
= *mdl_node_submesh( msky
, nupper
, 0 );
527 vg_acquire_thread_sync();
529 mdl_unpack_glmesh( msky
, &world_global
.skydome
);
531 vg_release_thread_sync();
534 vg_info( "Loading other world systems\n" );
536 vg_loader_step( world_render_init
, NULL
);
537 vg_loader_step( world_sfd_init
, NULL
);
538 vg_loader_step( world_water_init
, NULL
);
539 vg_loader_step( world_gates_init
, NULL
);
540 vg_loader_step( world_routes_init
, NULL
);
542 /* Allocate dynamic world memory arena */
543 u32 max_size
= 76*1024*1024;
544 world_global
.generic_heap
= vg_create_linear_allocator( vg_mem
.rtmemory
,
549 VG_STATIC
void world_audio_init(void)
551 u32 size
= vg_linear_remaining( vg_audio
.audio_pool
)
552 - sizeof(vg_linear_allocator
);
554 world_global
.audio_heap
= vg_create_linear_allocator( vg_audio
.audio_pool
,
559 VG_STATIC
void world_trigger_achievement( world_instance
*world
, u32 uid
)
561 struct logic_achievement
*ach
= &world
->logic_achievements
[ uid
];
566 steam_set_achievement( ach
->achievement_id
);
567 steam_store_achievements();
572 VG_STATIC
void world_run_relay( world_instance
*world
,
573 struct relay_target
*rt
);
575 VG_STATIC
void world_trigger_relay( world_instance
*world
, u32 uid
)
577 struct logic_relay
*relay
= &world
->logic_relays
[ uid
];
579 for( int i
=0; i
<relay
->target_count
; i
++ )
581 world_run_relay( world
, &relay
->targets
[i
] );
585 VG_STATIC
void world_trigger_audio( world_instance
*world
, u32 uid
)
587 struct world_audio_thing
*wat
= &world
->audio_things
[ uid
];
590 audio_player_playclip( &wat
->player
,
591 &wat
->temp_embedded_clip
);
595 VG_STATIC
void world_run_relay( world_instance
*world
,
596 struct relay_target
*rt
)
598 struct entity_instruction
600 enum classtype classtype
;
601 void (*p_trigger
)( world_instance
*world
, u32 uid
);
603 entity_instructions
[] =
605 { k_classtype_logic_achievement
, world_trigger_achievement
},
606 { k_classtype_logic_relay
, world_trigger_relay
},
607 { k_classtype_audio
, world_trigger_audio
}
610 for( int i
=0; i
<vg_list_size(entity_instructions
); i
++ )
612 struct entity_instruction
*instr
= &entity_instructions
[i
];
614 if( instr
->classtype
== rt
->classtype
)
616 instr
->p_trigger( world
, rt
->sub_id
);
621 vg_error( "Don't know how to trigger classtype %d\n", rt
->classtype
);
624 VG_STATIC
void world_update( world_instance
*world
, v3f pos
)
627 static double g_time
= 0.0;
628 g_time
+= vg
.time_delta
* (1.0/(k_day_length
*60.0));
630 world
->ub_lighting
.g_time
= g_time
;
631 world
->ub_lighting
.g_debug_indices
= k_debug_light_indices
;
632 world
->ub_lighting
.g_debug_complexity
= k_debug_light_complexity
;
634 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
635 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
636 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
641 if( world
.switching_to_new_world
)
646 for( int i
=0; i
<world
.audio_things_count
; i
++ )
648 struct world_audio_thing
*at
= &world
.audio_things
[i
];
650 if( audio_player_is_playing( &at
->player
) )
660 world
.switching_to_new_world
= 0;
662 vg_loader_start( world_load
);
668 world_global
.sky_time
+= world_global
.sky_rate
* vg
.time_delta
;
669 world_global
.sky_rate
= vg_lerp( world_global
.sky_rate
,
670 world_global
.sky_target_rate
,
671 vg
.time_delta
* 5.0 );
673 world_routes_update( world
);
675 world_routes_debug();
678 if( world
->route_count
> 0 )
681 float min_dist
= INFINITY
;
683 for( int i
=0; i
<world
->route_count
; i
++ )
685 float d
= v3_dist2( world
->routes
[i
].scoreboard_transform
[3], pos
);
694 if( (world_global
.active_route_board
!= closest
)
695 || network_scores_updated
)
697 network_scores_updated
= 0;
698 world_global
.active_route_board
= closest
;
700 struct route
*route
= &world
->routes
[closest
];
702 u32 id
= route
->track_id
;
704 if( id
!= 0xffffffff )
706 struct netmsg_board
*local_board
=
707 &scoreboard_client_data
.boards
[id
];
709 for( int i
=0; i
<13; i
++ )
711 sfd_encode( i
, &local_board
->data
[27*i
] );
718 for( int i
=0; i
<world
->trigger_count
; i
++ )
720 struct trigger_zone
*zone
= &world
->triggers
[i
];
723 m4x3_mulv( zone
->inv_transform
, pos
, local
);
725 if( (fabsf(local
[0]) <= 1.0f
) &&
726 (fabsf(local
[1]) <= 1.0f
) &&
727 (fabsf(local
[2]) <= 1.0f
) )
731 if( !world_global
.in_trigger
)
733 world_run_relay( world
, &zone
->target
);
737 vg_line_boxf_transformed( zone
->transform
, (boxf
){{-1.0f
,-1.0f
,-1.0f
},
738 { 1.0f
, 1.0f
, 1.0f
}},
742 if( k_debug_light_indices
)
744 for( int i
=0; i
<world
->light_count
; i
++ )
746 struct world_light
*light
= &world
->lights
[i
];
747 struct classtype_world_light
*inf
= light
->inf
;
749 u32 colour
= 0xff000000;
750 u8 r
= inf
->colour
[0] * 255.0f
,
751 g
= inf
->colour
[1] * 255.0f
,
752 b
= inf
->colour
[2] * 255.0f
;
758 vg_line_pt3( light
->node
->co
, 0.25f
, colour
);
762 world_global
.in_trigger
= in_trigger
;
767 * -----------------------------------------------------------------------------
769 * -----------------------------------------------------------------------------
772 VG_STATIC
void ray_world_get_tri( world_instance
*world
,
773 ray_hit
*hit
, v3f tri
[3] )
775 for( int i
=0; i
<3; i
++ )
776 v3_copy( world
->scene_geo
->arrvertices
[ hit
->tri
[i
] ].co
, tri
[i
] );
779 VG_STATIC
int ray_world( world_instance
*world
,
780 v3f pos
, v3f dir
, ray_hit
*hit
)
782 return scene_raycast( world
->scene_geo
, world
->geo_bh
, pos
, dir
, hit
);
786 * Cast a sphere from a to b and see what time it hits
788 VG_STATIC
int spherecast_world( world_instance
*world
,
789 v3f pa
, v3f pb
, float r
, float *t
, v3f n
)
792 bh_iter_init( 0, &it
);
795 box_init_inf( region
);
796 box_addpt( region
, pa
);
797 box_addpt( region
, pb
);
799 v3_add( (v3f
){ r
, r
, r
}, region
[1], region
[1] );
800 v3_add( (v3f
){-r
,-r
,-r
}, region
[0], region
[0] );
803 v3_sub( pb
, pa
, dir
);
806 dir_inv
[0] = 1.0f
/dir
[0];
807 dir_inv
[1] = 1.0f
/dir
[1];
808 dir_inv
[2] = 1.0f
/dir
[2];
814 while( bh_next( world
->geo_bh
, &it
, region
, &idx
) )
816 u32
*ptri
= &world
->scene_geo
->arrindices
[ idx
*3 ];
822 for( int j
=0; j
<3; j
++ )
824 v3_copy( world
->scene_geo
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
825 box_addpt( box
, tri
[j
] );
828 v3_add( (v3f
){ r
, r
, r
}, box
[1], box
[1] );
829 v3_add( (v3f
){-r
,-r
,-r
}, box
[0], box
[0] );
831 if( !ray_aabb1( box
, pa
, dir_inv
, 1.0f
) )
836 if( spherecast_triangle( tri
, pa
, dir
, r
, &t
, n1
) )
852 struct world_material
*world_tri_index_material( world_instance
*world
,
855 for( int i
=1; i
<world
->material_count
; i
++ )
857 struct world_material
*mat
= &world
->materials
[i
];
859 if( (index
>= mat
->sm_geo
.vertex_start
) &&
860 (index
< mat
->sm_geo
.vertex_start
+mat
->sm_geo
.vertex_count
) )
867 return &world
->materials
[0];
870 VG_STATIC
struct world_material
*world_contact_material( world_instance
*world
,
873 return world_tri_index_material( world
, ct
->element_id
);
876 VG_STATIC
struct world_material
*ray_hit_material( world_instance
*world
,
879 return world_tri_index_material( world
, hit
->tri
[0] );