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 v4f g_point_light_positions
[32];
98 v4f g_point_light_colours
[32];
105 GLuint tbo_light_entities
,
108 struct framebuffer heightmap
;
111 * Dynamically allocated when world_load is called.
113 * the following arrays index somewhere into this linear
117 * --------------------------------------------------------------------------
125 * Materials / textures
131 struct world_material
141 * Named safe places to respawn
153 * Audio player entities
155 struct world_audio_thing
162 audio_clip temp_embedded_clip
;
165 u32 audio_things_count
;
177 enum classtype classtype
;
186 * Box trigger entities
190 m4x3f transform
, inv_transform
;
192 struct relay_target target
;
200 struct logic_achievement
203 const char *achievement_id
;
206 * logic_achievements
;
207 u32 achievement_count
;
215 struct classtype_world_light
*inf
;
218 * TODO: we should order entities in the binary by their type */
224 * Routes (world_routes.h)
225 * --------------------------------------------------------------------------
229 v3f co
, right
, up
, h
;
232 u32 special_type
, special_id
, current_refs
, ref_count
;
233 u32 route_ids
[4]; /* Gates can be linked into up to four routes */
249 double best_lap
, latest_pass
; /* Session */
251 m4x3f scoreboard_transform
;
264 m4x3f to_world
, transport
;
272 u32 version
; /* Incremented on every teleport */
282 struct teleport_gate gate
;
285 u32 target_map_index
, working
;
288 u32 nonlocalgate_count
;
290 struct route_collector
292 struct route_timing timing
;
299 * ----------------------------------------------------
307 /* spacial mappings */
313 glmesh mesh_route_lines
;
318 rigidbody rb_geo
; /* todo.. ... */
321 VG_STATIC
struct world_global
324 * Allocated as system memory
325 * --------------------------------------------------------------------------
328 *audio_heap
; /* sub buffer of the audio buffer */
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_gen.h"
425 #include "world_gate.h"
428 * -----------------------------------------------------------------------------
430 * -----------------------------------------------------------------------------
433 VG_STATIC
int world_stop_sound( int argc
, const char *argv
[] )
435 world_instance
*world
= get_active_world();
438 * None of our world audio runs as one shots, they always have a player.
439 * Therefore it is safe to delete clip data after the players are
443 for( int i
=0; i
<world
->audio_things_count
; i
++ )
445 struct world_audio_thing
*at
= &world
->audio_things
[i
];
447 if( audio_player_is_playing( &at
->player
) )
449 u32 cflags
= audio_player_get_flags( &at
->player
);
450 audio_player_set_flags( &at
->player
, cflags
| AUDIO_FLAG_KILL
);
458 VG_STATIC
int world_change_world( int argc
, const char *argv
[] )
461 world_instance
*world
= get_active_world();
465 vg_info( "%s\n", world
.world_name
);
470 vg_info( "Switching world...\n" );
471 strcpy( world
.world_name
, argv
[0] );
472 world
.switching_to_new_world
= 1;
473 world_stop_sound( 0, NULL
);
480 VG_STATIC
void world_init(void)
483 vg_var_push( (struct vg_var
){
484 .name
= "water_enable",
485 .data
= &world
.water
.enabled
,
486 .data_type
= k_var_dtype_i32
,
487 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
492 vg_function_push( (struct vg_cmd
)
494 .name
= "world_stop_sound",
495 .function
= world_stop_sound
498 vg_function_push( (struct vg_cmd
)
501 .function
= world_change_world
504 world_global
.sky_rate
= 1.0;
505 world_global
.sky_target_rate
= 1.0;
507 shader_scene_standard_register();
508 shader_scene_standard_alphatest_register();
509 shader_scene_vertex_blend_register();
510 shader_scene_terrain_register();
511 shader_scene_depth_register();
512 shader_scene_position_register();
514 shader_model_sky_register();
516 vg_info( "Loading world resources\n" );
518 vg_linear_clear( vg_mem
.scratch
);
519 mdl_context
*msky
= mdl_load_full( vg_mem
.scratch
, "models/rs_skydome.mdl" );
521 mdl_node
*nupper
= mdl_node_from_name( msky
, "dome_complete" );
522 world_global
.dome_upper
= *mdl_node_submesh( msky
, nupper
, 0 );
524 vg_acquire_thread_sync();
526 mdl_unpack_glmesh( msky
, &world_global
.skydome
);
528 vg_release_thread_sync();
531 vg_info( "Loading other world systems\n" );
533 vg_loader_step( world_render_init
, NULL
);
534 vg_loader_step( world_sfd_init
, NULL
);
535 vg_loader_step( world_water_init
, NULL
);
536 vg_loader_step( world_gates_init
, NULL
);
537 vg_loader_step( world_routes_init
, NULL
);
539 /* Allocate dynamic world memory arena */
540 u32 max_size
= 76*1024*1024;
541 world_global
.generic_heap
= vg_create_linear_allocator( vg_mem
.rtmemory
,
546 VG_STATIC
void world_audio_init(void)
548 u32 size
= vg_linear_remaining( vg_audio
.audio_pool
)
549 - sizeof(vg_linear_allocator
);
551 world_global
.audio_heap
= vg_create_linear_allocator( vg_audio
.audio_pool
,
556 VG_STATIC
void world_trigger_achievement( world_instance
*world
, u32 uid
)
558 struct logic_achievement
*ach
= &world
->logic_achievements
[ uid
];
563 steam_set_achievement( ach
->achievement_id
);
564 steam_store_achievements();
569 VG_STATIC
void world_run_relay( world_instance
*world
,
570 struct relay_target
*rt
);
572 VG_STATIC
void world_trigger_relay( world_instance
*world
, u32 uid
)
574 struct logic_relay
*relay
= &world
->logic_relays
[ uid
];
576 for( int i
=0; i
<relay
->target_count
; i
++ )
578 world_run_relay( world
, &relay
->targets
[i
] );
582 VG_STATIC
void world_trigger_audio( world_instance
*world
, u32 uid
)
584 struct world_audio_thing
*wat
= &world
->audio_things
[ uid
];
587 audio_player_playclip( &wat
->player
,
588 &wat
->temp_embedded_clip
);
592 VG_STATIC
void world_run_relay( world_instance
*world
,
593 struct relay_target
*rt
)
595 struct entity_instruction
597 enum classtype classtype
;
598 void (*p_trigger
)( world_instance
*world
, u32 uid
);
600 entity_instructions
[] =
602 { k_classtype_logic_achievement
, world_trigger_achievement
},
603 { k_classtype_logic_relay
, world_trigger_relay
},
604 { k_classtype_audio
, world_trigger_audio
}
607 for( int i
=0; i
<vg_list_size(entity_instructions
); i
++ )
609 struct entity_instruction
*instr
= &entity_instructions
[i
];
611 if( instr
->classtype
== rt
->classtype
)
613 instr
->p_trigger( world
, rt
->sub_id
);
618 vg_error( "Don't know how to trigger classtype %d\n", rt
->classtype
);
621 VG_STATIC
void world_update( world_instance
*world
, v3f pos
)
624 static double g_time
= 0.0;
625 g_time
+= vg
.time_delta
* (1.0/(k_day_length
*60.0));
627 world
->ub_lighting
.g_time
= g_time
;
629 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
630 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
631 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
636 if( world
.switching_to_new_world
)
641 for( int i
=0; i
<world
.audio_things_count
; i
++ )
643 struct world_audio_thing
*at
= &world
.audio_things
[i
];
645 if( audio_player_is_playing( &at
->player
) )
655 world
.switching_to_new_world
= 0;
657 vg_loader_start( world_load
);
663 world_global
.sky_time
+= world_global
.sky_rate
* vg
.time_delta
;
664 world_global
.sky_rate
= vg_lerp( world_global
.sky_rate
,
665 world_global
.sky_target_rate
,
666 vg
.time_delta
* 5.0 );
668 world_routes_update( world
);
670 world_routes_debug();
673 if( world
->route_count
> 0 )
676 float min_dist
= INFINITY
;
678 for( int i
=0; i
<world
->route_count
; i
++ )
680 float d
= v3_dist2( world
->routes
[i
].scoreboard_transform
[3], pos
);
689 if( (world_global
.active_route_board
!= closest
)
690 || network_scores_updated
)
692 network_scores_updated
= 0;
693 world_global
.active_route_board
= closest
;
695 struct route
*route
= &world
->routes
[closest
];
697 u32 id
= route
->track_id
;
699 if( id
!= 0xffffffff )
701 struct netmsg_board
*local_board
=
702 &scoreboard_client_data
.boards
[id
];
704 for( int i
=0; i
<13; i
++ )
706 sfd_encode( i
, &local_board
->data
[27*i
] );
713 for( int i
=0; i
<world
->trigger_count
; i
++ )
715 struct trigger_zone
*zone
= &world
->triggers
[i
];
718 m4x3_mulv( zone
->inv_transform
, pos
, local
);
720 if( (fabsf(local
[0]) <= 1.0f
) &&
721 (fabsf(local
[1]) <= 1.0f
) &&
722 (fabsf(local
[2]) <= 1.0f
) )
726 if( !world_global
.in_trigger
)
728 world_run_relay( world
, &zone
->target
);
732 vg_line_boxf_transformed( zone
->transform
, (boxf
){{-1.0f
,-1.0f
,-1.0f
},
733 { 1.0f
, 1.0f
, 1.0f
}},
737 if( k_debug_light_index
)
739 for( int i
=0; i
<world
->light_count
; i
++ )
741 struct world_light
*light
= &world
->lights
[i
];
742 struct classtype_world_light
*inf
= light
->inf
;
744 u32 colour
= 0xff000000;
745 u8 r
= inf
->colour
[0] * 255.0f
,
746 g
= inf
->colour
[1] * 255.0f
,
747 b
= inf
->colour
[2] * 255.0f
;
753 vg_line_pt3( light
->node
->co
, 0.25f
, colour
);
757 world_global
.in_trigger
= in_trigger
;
762 * -----------------------------------------------------------------------------
764 * -----------------------------------------------------------------------------
767 VG_STATIC
void ray_world_get_tri( world_instance
*world
,
768 ray_hit
*hit
, v3f tri
[3] )
770 for( int i
=0; i
<3; i
++ )
771 v3_copy( world
->scene_geo
->arrvertices
[ hit
->tri
[i
] ].co
, tri
[i
] );
774 VG_STATIC
int ray_world( world_instance
*world
,
775 v3f pos
, v3f dir
, ray_hit
*hit
)
777 return scene_raycast( world
->scene_geo
, world
->geo_bh
, pos
, dir
, hit
);
781 * Cast a sphere from a to b and see what time it hits
783 VG_STATIC
int spherecast_world( world_instance
*world
,
784 v3f pa
, v3f pb
, float r
, float *t
, v3f n
)
787 bh_iter_init( 0, &it
);
790 box_init_inf( region
);
791 box_addpt( region
, pa
);
792 box_addpt( region
, pb
);
794 v3_add( (v3f
){ r
, r
, r
}, region
[1], region
[1] );
795 v3_add( (v3f
){-r
,-r
,-r
}, region
[0], region
[0] );
798 v3_sub( pb
, pa
, dir
);
801 dir_inv
[0] = 1.0f
/dir
[0];
802 dir_inv
[1] = 1.0f
/dir
[1];
803 dir_inv
[2] = 1.0f
/dir
[2];
809 while( bh_next( world
->geo_bh
, &it
, region
, &idx
) )
811 u32
*ptri
= &world
->scene_geo
->arrindices
[ idx
*3 ];
817 for( int j
=0; j
<3; j
++ )
819 v3_copy( world
->scene_geo
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
820 box_addpt( box
, tri
[j
] );
823 v3_add( (v3f
){ r
, r
, r
}, box
[1], box
[1] );
824 v3_add( (v3f
){-r
,-r
,-r
}, box
[0], box
[0] );
826 if( !ray_aabb1( box
, pa
, dir_inv
, 1.0f
) )
831 if( spherecast_triangle( tri
, pa
, dir
, r
, &t
, n1
) )
847 struct world_material
*world_tri_index_material( world_instance
*world
,
850 for( int i
=1; i
<world
->material_count
; i
++ )
852 struct world_material
*mat
= &world
->materials
[i
];
854 if( (index
>= mat
->sm_geo
.vertex_start
) &&
855 (index
< mat
->sm_geo
.vertex_start
+mat
->sm_geo
.vertex_count
) )
862 return &world
->materials
[0];
865 VG_STATIC
struct world_material
*world_contact_material( world_instance
*world
,
868 return world_tri_index_material( world
, ct
->element_id
);
871 VG_STATIC
struct world_material
*ray_hit_material( world_instance
*world
,
874 return world_tri_index_material( world
, hit
->tri
[0] );