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],
96 v4f g_point_light_positions
[32];
97 v4f g_point_light_colours
[32];
104 GLuint tbo_light_entities
,
107 struct framebuffer heightmap
;
110 * Dynamically allocated when world_load is called.
112 * the following arrays index somewhere into this linear
116 * --------------------------------------------------------------------------
124 * Materials / textures
130 struct world_material
140 * Named safe places to respawn
152 * Audio player entities
154 struct world_audio_thing
161 audio_clip temp_embedded_clip
;
164 u32 audio_things_count
;
176 enum classtype classtype
;
185 * Box trigger entities
189 m4x3f transform
, inv_transform
;
191 struct relay_target target
;
199 struct logic_achievement
202 const char *achievement_id
;
205 * logic_achievements
;
206 u32 achievement_count
;
220 * Routes (world_routes.h)
221 * --------------------------------------------------------------------------
225 v3f co
, right
, up
, h
;
228 u32 special_type
, special_id
, current_refs
, ref_count
;
229 u32 route_ids
[4]; /* Gates can be linked into up to four routes */
245 double best_lap
, latest_pass
; /* Session */
247 m4x3f scoreboard_transform
;
260 m4x3f to_world
, transport
;
268 u32 version
; /* Incremented on every teleport */
278 struct teleport_gate gate
;
281 u32 target_map_index
, working
;
284 u32 nonlocalgate_count
;
286 struct route_collector
288 struct route_timing timing
;
295 * ----------------------------------------------------
303 /* spacial mappings */
309 glmesh mesh_route_lines
;
314 rigidbody rb_geo
; /* todo.. ... */
317 VG_STATIC
struct world_global
320 * Allocated as system memory
321 * --------------------------------------------------------------------------
324 *audio_heap
; /* sub buffer of the audio buffer */
328 mdl_submesh dome_upper
, dome_lower
;
330 glmesh mesh_gate_surface
;
332 double sky_time
, sky_rate
, sky_target_rate
;
334 /* gates, TODO: active_gate should also know which instance */
337 double time
, rewind_from
, rewind_to
, last_use
;
339 /* water rendering */
342 struct framebuffer fbreflect
, fbdepth
;
346 /* split flap display */
349 mdl_submesh
*sm_module
, *sm_card
;
350 glmesh mesh_base
, mesh_display
;
357 /* timing bars, fixed maximum amount */
360 GLuint vao
, vbo
, ebo
;
365 struct route_ui_segment
368 u32 vertex_start
, vertex_count
,
369 index_start
, index_count
, notches
;
371 segments
[k_max_ui_segments
];
373 u32 segment_start
, segment_count
, fade_start
, fade_count
;
374 double fade_timer_start
;
380 int active_route_board
;
383 int switching_to_new_world
;
385 world_instance worlds
[4];
391 VG_STATIC world_instance
*get_active_world( void )
393 return &world_global
.worlds
[ world_global
.active_world
];
401 int ray_hit_is_ramp( world_instance
*world
, ray_hit
*hit
);
404 struct world_material
*ray_hit_material( world_instance
*world
, ray_hit
*hit
);
407 void ray_world_get_tri( world_instance
*world
, ray_hit
*hit
, v3f tri
[3] );
410 int ray_world( world_instance
*world
, v3f pos
, v3f dir
, ray_hit
*hit
);
416 #include "world_routes.h"
417 #include "world_sfd.h"
418 #include "world_render.h"
419 #include "world_water.h"
420 #include "world_gen.h"
421 #include "world_gate.h"
424 * -----------------------------------------------------------------------------
426 * -----------------------------------------------------------------------------
429 VG_STATIC
int world_stop_sound( int argc
, const char *argv
[] )
431 world_instance
*world
= get_active_world();
434 * None of our world audio runs as one shots, they always have a player.
435 * Therefore it is safe to delete clip data after the players are
439 for( int i
=0; i
<world
->audio_things_count
; i
++ )
441 struct world_audio_thing
*at
= &world
->audio_things
[i
];
443 if( audio_player_is_playing( &at
->player
) )
445 u32 cflags
= audio_player_get_flags( &at
->player
);
446 audio_player_set_flags( &at
->player
, cflags
| AUDIO_FLAG_KILL
);
454 VG_STATIC
int world_change_world( int argc
, const char *argv
[] )
457 world_instance
*world
= get_active_world();
461 vg_info( "%s\n", world
.world_name
);
466 vg_info( "Switching world...\n" );
467 strcpy( world
.world_name
, argv
[0] );
468 world
.switching_to_new_world
= 1;
469 world_stop_sound( 0, NULL
);
476 VG_STATIC
void world_init(void)
479 vg_var_push( (struct vg_var
){
480 .name
= "water_enable",
481 .data
= &world
.water
.enabled
,
482 .data_type
= k_var_dtype_i32
,
483 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
488 vg_function_push( (struct vg_cmd
)
490 .name
= "world_stop_sound",
491 .function
= world_stop_sound
494 vg_function_push( (struct vg_cmd
)
497 .function
= world_change_world
500 world_global
.sky_rate
= 1.0;
501 world_global
.sky_target_rate
= 1.0;
503 shader_scene_standard_register();
504 shader_scene_standard_alphatest_register();
505 shader_scene_vertex_blend_register();
506 shader_scene_terrain_register();
507 shader_scene_depth_register();
508 shader_scene_position_register();
510 shader_model_sky_register();
512 vg_info( "Loading world resources\n" );
514 vg_linear_clear( vg_mem
.scratch
);
515 mdl_context
*msky
= mdl_load_full( vg_mem
.scratch
, "models/rs_skydome.mdl" );
517 mdl_node
*nupper
= mdl_node_from_name( msky
, "dome_complete" );
518 world_global
.dome_upper
= *mdl_node_submesh( msky
, nupper
, 0 );
520 vg_acquire_thread_sync();
522 mdl_unpack_glmesh( msky
, &world_global
.skydome
);
524 vg_release_thread_sync();
527 vg_info( "Loading other world systems\n" );
529 vg_loader_step( world_render_init
, NULL
);
530 vg_loader_step( world_sfd_init
, NULL
);
531 vg_loader_step( world_water_init
, NULL
);
532 vg_loader_step( world_gates_init
, NULL
);
533 vg_loader_step( world_routes_init
, NULL
);
535 /* Allocate dynamic world memory arena */
536 u32 max_size
= 76*1024*1024;
537 world_global
.generic_heap
= vg_create_linear_allocator( vg_mem
.rtmemory
,
542 VG_STATIC
void world_audio_init(void)
544 u32 size
= vg_linear_remaining( vg_audio
.audio_pool
)
545 - sizeof(vg_linear_allocator
);
547 world_global
.audio_heap
= vg_create_linear_allocator( vg_audio
.audio_pool
,
552 VG_STATIC
void world_trigger_achievement( world_instance
*world
, u32 uid
)
554 struct logic_achievement
*ach
= &world
->logic_achievements
[ uid
];
559 steam_set_achievement( ach
->achievement_id
);
560 steam_store_achievements();
565 VG_STATIC
void world_run_relay( world_instance
*world
,
566 struct relay_target
*rt
);
568 VG_STATIC
void world_trigger_relay( world_instance
*world
, u32 uid
)
570 struct logic_relay
*relay
= &world
->logic_relays
[ uid
];
572 for( int i
=0; i
<relay
->target_count
; i
++ )
574 world_run_relay( world
, &relay
->targets
[i
] );
578 VG_STATIC
void world_trigger_audio( world_instance
*world
, u32 uid
)
580 struct world_audio_thing
*wat
= &world
->audio_things
[ uid
];
583 audio_player_playclip( &wat
->player
,
584 &wat
->temp_embedded_clip
);
588 VG_STATIC
void world_run_relay( world_instance
*world
,
589 struct relay_target
*rt
)
591 struct entity_instruction
593 enum classtype classtype
;
594 void (*p_trigger
)( world_instance
*world
, u32 uid
);
596 entity_instructions
[] =
598 { k_classtype_logic_achievement
, world_trigger_achievement
},
599 { k_classtype_logic_relay
, world_trigger_relay
},
600 { k_classtype_audio
, world_trigger_audio
}
603 for( int i
=0; i
<vg_list_size(entity_instructions
); i
++ )
605 struct entity_instruction
*instr
= &entity_instructions
[i
];
607 if( instr
->classtype
== rt
->classtype
)
609 instr
->p_trigger( world
, rt
->sub_id
);
614 vg_error( "Don't know how to trigger classtype %d\n", rt
->classtype
);
617 VG_STATIC
void world_update( world_instance
*world
, v3f pos
)
620 if( world
.switching_to_new_world
)
625 for( int i
=0; i
<world
.audio_things_count
; i
++ )
627 struct world_audio_thing
*at
= &world
.audio_things
[i
];
629 if( audio_player_is_playing( &at
->player
) )
639 world
.switching_to_new_world
= 0;
641 vg_loader_start( world_load
);
647 world_global
.sky_time
+= world_global
.sky_rate
* vg
.time_delta
;
648 world_global
.sky_rate
= vg_lerp( world_global
.sky_rate
,
649 world_global
.sky_target_rate
,
650 vg
.time_delta
* 5.0 );
652 world_routes_update( world
);
654 world_routes_debug();
657 if( world
->route_count
> 0 )
660 float min_dist
= INFINITY
;
662 for( int i
=0; i
<world
->route_count
; i
++ )
664 float d
= v3_dist2( world
->routes
[i
].scoreboard_transform
[3], pos
);
673 if( (world_global
.active_route_board
!= closest
)
674 || network_scores_updated
)
676 network_scores_updated
= 0;
677 world_global
.active_route_board
= closest
;
679 struct route
*route
= &world
->routes
[closest
];
681 u32 id
= route
->track_id
;
683 if( id
!= 0xffffffff )
685 struct netmsg_board
*local_board
=
686 &scoreboard_client_data
.boards
[id
];
688 for( int i
=0; i
<13; i
++ )
690 sfd_encode( i
, &local_board
->data
[27*i
] );
697 for( int i
=0; i
<world
->trigger_count
; i
++ )
699 struct trigger_zone
*zone
= &world
->triggers
[i
];
702 m4x3_mulv( zone
->inv_transform
, pos
, local
);
704 if( (fabsf(local
[0]) <= 1.0f
) &&
705 (fabsf(local
[1]) <= 1.0f
) &&
706 (fabsf(local
[2]) <= 1.0f
) )
710 if( !world_global
.in_trigger
)
712 world_run_relay( world
, &zone
->target
);
716 vg_line_boxf_transformed( zone
->transform
, (boxf
){{-1.0f
,-1.0f
,-1.0f
},
717 { 1.0f
, 1.0f
, 1.0f
}},
723 for( int i
=0; i
<world
->light_count
; i
++ )
725 struct world_light
*light
= &world
->lights
[i
];
727 u32 colour
= 0xff000000;
728 u8 r
= light
->colour
[0] * 255.0f
,
729 g
= light
->colour
[1] * 255.0f
,
730 b
= light
->colour
[2] * 255.0f
;
736 vg_line_pt3( light
->co
, 0.25f
, colour
);
740 world_global
.in_trigger
= in_trigger
;
745 * -----------------------------------------------------------------------------
747 * -----------------------------------------------------------------------------
750 VG_STATIC
void ray_world_get_tri( world_instance
*world
,
751 ray_hit
*hit
, v3f tri
[3] )
753 for( int i
=0; i
<3; i
++ )
754 v3_copy( world
->scene_geo
->arrvertices
[ hit
->tri
[i
] ].co
, tri
[i
] );
757 VG_STATIC
int ray_world( world_instance
*world
,
758 v3f pos
, v3f dir
, ray_hit
*hit
)
760 return scene_raycast( world
->scene_geo
, world
->geo_bh
, pos
, dir
, hit
);
764 * Cast a sphere from a to b and see what time it hits
766 VG_STATIC
int spherecast_world( world_instance
*world
,
767 v3f pa
, v3f pb
, float r
, float *t
, v3f n
)
770 bh_iter_init( 0, &it
);
773 box_init_inf( region
);
774 box_addpt( region
, pa
);
775 box_addpt( region
, pb
);
777 v3_add( (v3f
){ r
, r
, r
}, region
[1], region
[1] );
778 v3_add( (v3f
){-r
,-r
,-r
}, region
[0], region
[0] );
781 v3_sub( pb
, pa
, dir
);
784 dir_inv
[0] = 1.0f
/dir
[0];
785 dir_inv
[1] = 1.0f
/dir
[1];
786 dir_inv
[2] = 1.0f
/dir
[2];
792 while( bh_next( world
->geo_bh
, &it
, region
, &idx
) )
794 u32
*ptri
= &world
->scene_geo
->arrindices
[ idx
*3 ];
800 for( int j
=0; j
<3; j
++ )
802 v3_copy( world
->scene_geo
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
803 box_addpt( box
, tri
[j
] );
806 v3_add( (v3f
){ r
, r
, r
}, box
[1], box
[1] );
807 v3_add( (v3f
){-r
,-r
,-r
}, box
[0], box
[0] );
809 if( !ray_aabb1( box
, pa
, dir_inv
, 1.0f
) )
814 if( spherecast_triangle( tri
, pa
, dir
, r
, &t
, n1
) )
830 struct world_material
*world_tri_index_material( world_instance
*world
,
833 for( int i
=1; i
<world
->material_count
; i
++ )
835 struct world_material
*mat
= &world
->materials
[i
];
837 if( (index
>= mat
->sm_geo
.vertex_start
) &&
838 (index
< mat
->sm_geo
.vertex_start
+mat
->sm_geo
.vertex_count
) )
845 return &world
->materials
[0];
848 VG_STATIC
struct world_material
*world_contact_material( world_instance
*world
,
851 return world_tri_index_material( world
, ct
->element_id
);
854 VG_STATIC
struct world_material
*ray_hit_material( world_instance
*world
,
857 return world_tri_index_material( world
, hit
->tri
[0] );