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
180 audio_clip temp_embedded_clip
;
183 u32 audio_things_count
;
187 * Relays [ DEPRECATED ]
196 enum classtype classtype
;
208 audio_channel
*channels
[4];
210 /* accessable without locking */
215 u32 allow_transitions
;
216 float transition_duration
;
220 u32 soundscape_count
;
222 struct logic_brick_ref
226 u32 internal_id
; /* used for things like soundscapes where another
227 allocation is made on top */
230 u32 logic_brick_count
;
233 * Box trigger entities
237 m4x3f transform
, inv_transform
;
239 u32 target_logic_brick
;
240 enum classtype classtype
;
248 struct logic_achievement
251 const char *achievement_id
;
254 * logic_achievements
;
255 u32 achievement_count
;
263 struct classtype_world_light
*inf
;
268 * TODO: we should order entities in the binary by their type */
274 * Routes (world_routes.h)
275 * --------------------------------------------------------------------------
279 v3f co
, right
, up
, h
;
282 u32 special_type
, special_id
, current_refs
, ref_count
;
283 u32 route_ids
[4]; /* Gates can be linked into up to four routes */
299 double best_lap
, latest_pass
; /* Session */
301 m4x3f scoreboard_transform
;
314 m4x3f to_world
, transport
;
322 u32 version
; /* Incremented on every teleport */
332 struct teleport_gate gate
;
335 u32 target_map_index
, working
;
338 u32 nonlocalgate_count
;
340 struct route_collector
342 struct route_timing timing
;
349 * ----------------------------------------------------
357 /* spacial mappings */
363 glmesh mesh_route_lines
;
368 rigidbody rb_geo
; /* todo.. ... */
371 VG_STATIC
struct world_global
374 * Allocated as system memory
375 * --------------------------------------------------------------------------
381 mdl_submesh dome_upper
, dome_lower
;
383 glmesh mesh_gate_surface
;
385 double sky_time
, sky_rate
, sky_target_rate
;
387 /* gates, TODO: active_gate should also know which instance */
390 double time
, rewind_from
, rewind_to
, last_use
;
392 /* water rendering */
395 struct framebuffer fbreflect
, fbdepth
;
399 /* split flap display */
402 mdl_submesh
*sm_module
, *sm_card
;
403 glmesh mesh_base
, mesh_display
;
410 /* timing bars, fixed maximum amount */
413 GLuint vao
, vbo
, ebo
;
418 struct route_ui_segment
421 u32 vertex_start
, vertex_count
,
422 index_start
, index_count
, notches
;
424 segments
[k_max_ui_segments
];
426 u32 segment_start
, segment_count
, fade_start
, fade_count
;
427 double fade_timer_start
;
433 int active_route_board
;
436 int switching_to_new_world
;
438 world_instance worlds
[4];
444 VG_STATIC world_instance
*get_active_world( void )
446 return &world_global
.worlds
[ world_global
.active_world
];
454 int ray_hit_is_ramp( world_instance
*world
, ray_hit
*hit
);
457 struct world_material
*ray_hit_material( world_instance
*world
, ray_hit
*hit
);
460 void ray_world_get_tri( world_instance
*world
, ray_hit
*hit
, v3f tri
[3] );
463 int ray_world( world_instance
*world
, v3f pos
, v3f dir
, ray_hit
*hit
);
469 #include "world_routes.h"
470 #include "world_sfd.h"
471 #include "world_render.h"
472 #include "world_water.h"
473 #include "world_logic_bricks.h"
474 #include "world_gen.h"
475 #include "world_gate.h"
478 * -----------------------------------------------------------------------------
480 * -----------------------------------------------------------------------------
483 VG_STATIC
int world_stop_sound( int argc
, const char *argv
[] )
485 world_instance
*world
= get_active_world();
488 * None of our world audio runs as one shots, they always have a player.
489 * Therefore it is safe to delete clip data after the players are
494 for( int i
=0; i
<world
->audio_things_count
; i
++ )
496 struct world_audio_thing
*at
= &world
->audio_things
[i
];
498 if( audio_player_is_playing( &at
->player
) )
500 u32 cflags
= audio_player_get_flags( &at
->player
);
501 audio_player_set_flags( &at
->player
, cflags
| AUDIO_FLAG_KILL
);
510 VG_STATIC
int world_change_world( int argc
, const char *argv
[] )
513 world_instance
*world
= get_active_world();
517 vg_info( "%s\n", world
.world_name
);
522 vg_info( "Switching world...\n" );
523 strcpy( world
.world_name
, argv
[0] );
524 world
.switching_to_new_world
= 1;
525 world_stop_sound( 0, NULL
);
532 VG_STATIC
void world_init(void)
535 vg_var_push( (struct vg_var
){
536 .name
= "water_enable",
537 .data
= &world
.water
.enabled
,
538 .data_type
= k_var_dtype_i32
,
539 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
544 vg_function_push( (struct vg_cmd
)
546 .name
= "world_stop_sound",
547 .function
= world_stop_sound
550 vg_function_push( (struct vg_cmd
)
553 .function
= world_change_world
556 world_global
.sky_rate
= 1.0;
557 world_global
.sky_target_rate
= 1.0;
559 shader_scene_standard_register();
560 shader_scene_standard_alphatest_register();
561 shader_scene_vertex_blend_register();
562 shader_scene_terrain_register();
563 shader_scene_depth_register();
564 shader_scene_position_register();
566 shader_model_sky_register();
568 vg_info( "Loading world resources\n" );
570 vg_linear_clear( vg_mem
.scratch
);
571 mdl_context
*msky
= mdl_load_full( vg_mem
.scratch
, "models/rs_skydome.mdl" );
573 mdl_node
*nupper
= mdl_node_from_name( msky
, "dome_complete" );
574 world_global
.dome_upper
= *mdl_node_submesh( msky
, nupper
, 0 );
576 vg_acquire_thread_sync();
578 mdl_unpack_glmesh( msky
, &world_global
.skydome
);
580 vg_release_thread_sync();
583 vg_info( "Loading other world systems\n" );
585 vg_loader_step( world_render_init
, NULL
);
586 vg_loader_step( world_sfd_init
, NULL
);
587 vg_loader_step( world_water_init
, NULL
);
588 vg_loader_step( world_gates_init
, NULL
);
589 vg_loader_step( world_routes_init
, NULL
);
591 /* Allocate dynamic world memory arena */
592 u32 max_size
= 76*1024*1024;
593 world_global
.generic_heap
= vg_create_linear_allocator( vg_mem
.rtmemory
,
598 VG_STATIC
void world_audio_init(void)
601 u32 size
= vg_linear_remaining( vg_audio
.audio_pool
)
602 - sizeof(vg_linear_allocator
);
604 world_global
.audio_heap
= vg_create_linear_allocator( vg_audio
.audio_pool
,
611 VG_STATIC
void world_trigger_achievement( world_instance
*world
, u32 uid
)
613 struct logic_achievement
*ach
= &world
->logic_achievements
[ uid
];
618 steam_set_achievement( ach
->achievement_id
);
619 steam_store_achievements();
626 VG_STATIC
void world_run_relay( world_instance
*world
,
627 struct relay_target
*rt
);
629 VG_STATIC
void world_trigger_relay( world_instance
*world
, u32 uid
)
631 struct logic_relay
*relay
= &world
->logic_relays
[ uid
];
633 for( int i
=0; i
<relay
->target_count
; i
++ )
635 world_run_relay( world
, &relay
->targets
[i
] );
641 VG_STATIC
void world_trigger_audio( world_instance
*world
, u32 uid
)
643 struct world_audio_thing
*wat
= &world
->audio_things
[ uid
];
646 audio_player_playclip( &wat
->player
,
647 &wat
->temp_embedded_clip
);
653 VG_STATIC
void world_run_relay( world_instance
*world
,
654 struct relay_target
*rt
)
656 struct entity_instruction
658 enum classtype classtype
;
659 void (*p_trigger
)( world_instance
*world
, u32 uid
);
661 entity_instructions
[] =
663 { k_classtype_logic_achievement
, world_trigger_achievement
},
664 { k_classtype_logic_relay
, world_trigger_relay
},
665 { k_classtype_audio
, world_trigger_audio
},
666 { k_classtype_logic_wire
, logic_bricks_trigger_brick
}
669 for( int i
=0; i
<vg_list_size(entity_instructions
); i
++ )
671 struct entity_instruction
*instr
= &entity_instructions
[i
];
673 if( instr
->classtype
== rt
->classtype
)
675 instr
->p_trigger( world
, rt
->sub_id
);
680 vg_error( "Don't know how to trigger classtype %d\n", rt
->classtype
);
684 VG_STATIC
void world_update( world_instance
*world
, v3f pos
)
687 static double g_time
= 0.0;
688 g_time
+= vg
.time_delta
* (1.0/(k_day_length
*60.0));
691 struct ub_world_lighting
*state
= &world
->ub_lighting
;
693 state
->g_time
= g_time
;
694 state
->g_debug_indices
= k_debug_light_indices
;
695 state
->g_light_preview
= k_light_preview
;
696 state
->g_debug_complexity
= k_debug_light_complexity
;
698 state
->g_time_of_day
= vg_fractf( g_time
);
699 state
->g_day_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 2.0f
);
700 state
->g_sunset_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 4.0f
+ VG_PIf
);
702 state
->g_day_phase
= state
->g_day_phase
* 0.5f
+ 0.5f
;
703 state
->g_sunset_phase
= powf( state
->g_sunset_phase
* 0.5f
+ 0.5f
, 6.0f
);
705 float a
= state
->g_time_of_day
* VG_PIf
* 2.0f
;
706 state
->g_sun_dir
[0] = sinf( a
);
707 state
->g_sun_dir
[1] = cosf( a
);
708 state
->g_sun_dir
[2] = 0.2f
;
709 v3_normalize( state
->g_sun_dir
);
713 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
714 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
715 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
720 if( world
.switching_to_new_world
)
725 for( int i
=0; i
<world
.audio_things_count
; i
++ )
727 struct world_audio_thing
*at
= &world
.audio_things
[i
];
729 if( audio_player_is_playing( &at
->player
) )
739 world
.switching_to_new_world
= 0;
741 vg_loader_start( world_load
);
747 world_global
.sky_time
+= world_global
.sky_rate
* vg
.time_delta
;
748 world_global
.sky_rate
= vg_lerp( world_global
.sky_rate
,
749 world_global
.sky_target_rate
,
750 vg
.time_delta
* 5.0 );
752 world_routes_update( world
);
754 world_routes_debug();
757 if( world
->route_count
> 0 )
760 float min_dist
= INFINITY
;
762 for( int i
=0; i
<world
->route_count
; i
++ )
764 float d
= v3_dist2( world
->routes
[i
].scoreboard_transform
[3], pos
);
773 if( (world_global
.active_route_board
!= closest
)
774 || network_scores_updated
)
776 network_scores_updated
= 0;
777 world_global
.active_route_board
= closest
;
779 struct route
*route
= &world
->routes
[closest
];
781 u32 id
= route
->track_id
;
783 if( id
!= 0xffffffff )
785 struct netmsg_board
*local_board
=
786 &scoreboard_client_data
.boards
[id
];
788 for( int i
=0; i
<13; i
++ )
790 sfd_encode( i
, &local_board
->data
[27*i
] );
798 static float random_accum
= 0.0f
;
799 random_accum
+= vg
.time_delta
;
801 u32 random_ticks
= 0;
803 while( random_accum
> 0.1f
)
805 random_accum
-= 0.1f
;
810 for( int i
=0; i
<world
->trigger_count
; i
++ )
812 struct trigger_zone
*zone
= &world
->triggers
[i
];
814 for( int j
=0; j
<random_ticks
; j
++ )
817 packet
.location
= zone
->target_logic_brick
;
820 packet
.type
= k_mdl_128bit_datatype_vec3
;
821 packet
.data
._v4f
[0] = vg_randf()*2.0f
-1.0f
;
822 packet
.data
._v4f
[1] = vg_randf()*2.0f
-1.0f
;
823 packet
.data
._v4f
[2] = vg_randf()*2.0f
-1.0f
;
824 m4x3_mulv( zone
->transform
, packet
.data
._v4f
, packet
.data
._v4f
);
826 logic_bricks_send_packet( world
, &packet
);
831 m4x3_mulv( zone
->inv_transform
, pos
, local
);
833 if( (fabsf(local
[0]) <= 1.0f
) &&
834 (fabsf(local
[1]) <= 1.0f
) &&
835 (fabsf(local
[2]) <= 1.0f
) )
839 if( !world_global
.in_trigger
)
842 packet
.location
= zone
->target_logic_brick
;
845 packet
.type
= k_mdl_128bit_datatype_vec3
;
846 v3_copy( pos
, packet
.data
._v4f
);
848 logic_bricks_send_packet( world
, &packet
);
852 vg_line_boxf_transformed( zone
->transform
, (boxf
){{-1.0f
,-1.0f
,-1.0f
},
853 { 1.0f
, 1.0f
, 1.0f
}},
857 if( k_debug_light_indices
)
859 for( int i
=0; i
<world
->light_count
; i
++ )
861 struct world_light
*light
= &world
->lights
[i
];
862 struct classtype_world_light
*inf
= light
->inf
;
864 u32 colour
= 0xff000000;
865 u8 r
= inf
->colour
[0] * 255.0f
,
866 g
= inf
->colour
[1] * 255.0f
,
867 b
= inf
->colour
[2] * 255.0f
;
873 vg_line_pt3( light
->node
->co
, 0.25f
, colour
);
877 world_global
.in_trigger
= in_trigger
;
880 if( debug_logic_bricks
)
881 logic_bricks_debug( world
);
883 /* process soundscape transactions */
885 for( int i
=0; i
<world
->soundscape_count
; i
++ )
887 struct soundscape
*s
= &world
->soundscapes
[i
];
890 for( int j
=0; j
<s
->max_instances
; j
++ )
894 if( audio_channel_finished(s
->channels
[j
]) )
895 s
->channels
[j
] = audio_relinquish_channel( s
->channels
[j
] );
905 * -----------------------------------------------------------------------------
907 * -----------------------------------------------------------------------------
910 VG_STATIC
void ray_world_get_tri( world_instance
*world
,
911 ray_hit
*hit
, v3f tri
[3] )
913 for( int i
=0; i
<3; i
++ )
914 v3_copy( world
->scene_geo
->arrvertices
[ hit
->tri
[i
] ].co
, tri
[i
] );
917 VG_STATIC
int ray_world( world_instance
*world
,
918 v3f pos
, v3f dir
, ray_hit
*hit
)
920 return scene_raycast( world
->scene_geo
, world
->geo_bh
, pos
, dir
, hit
);
924 * Cast a sphere from a to b and see what time it hits
926 VG_STATIC
int spherecast_world( world_instance
*world
,
927 v3f pa
, v3f pb
, float r
, float *t
, v3f n
)
930 bh_iter_init( 0, &it
);
933 box_init_inf( region
);
934 box_addpt( region
, pa
);
935 box_addpt( region
, pb
);
937 v3_add( (v3f
){ r
, r
, r
}, region
[1], region
[1] );
938 v3_add( (v3f
){-r
,-r
,-r
}, region
[0], region
[0] );
941 v3_sub( pb
, pa
, dir
);
944 dir_inv
[0] = 1.0f
/dir
[0];
945 dir_inv
[1] = 1.0f
/dir
[1];
946 dir_inv
[2] = 1.0f
/dir
[2];
952 while( bh_next( world
->geo_bh
, &it
, region
, &idx
) )
954 u32
*ptri
= &world
->scene_geo
->arrindices
[ idx
*3 ];
960 for( int j
=0; j
<3; j
++ )
962 v3_copy( world
->scene_geo
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
963 box_addpt( box
, tri
[j
] );
966 v3_add( (v3f
){ r
, r
, r
}, box
[1], box
[1] );
967 v3_add( (v3f
){-r
,-r
,-r
}, box
[0], box
[0] );
969 if( !ray_aabb1( box
, pa
, dir_inv
, 1.0f
) )
974 if( spherecast_triangle( tri
, pa
, dir
, r
, &t
, n1
) )
990 struct world_material
*world_tri_index_material( world_instance
*world
,
993 for( int i
=1; i
<world
->material_count
; i
++ )
995 struct world_material
*mat
= &world
->materials
[i
];
997 if( (index
>= mat
->sm_geo
.vertex_start
) &&
998 (index
< mat
->sm_geo
.vertex_start
+mat
->sm_geo
.vertex_count
) )
1004 /* error material */
1005 return &world
->materials
[0];
1008 VG_STATIC
struct world_material
*world_contact_material( world_instance
*world
,
1011 return world_tri_index_material( world
, ct
->element_id
);
1014 VG_STATIC
struct world_material
*ray_hit_material( world_instance
*world
,
1017 return world_tri_index_material( world
, hit
->tri
[0] );
1020 #endif /* WORLD_H */