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"
28 #include "shaders/model_sky.h"
30 typedef struct teleport_gate teleport_gate
;
32 enum { k_max_ui_segments
= 8 };
34 enum { k_max_ui_elements
= k_max_ui_segments
};
35 enum { k_max_element_verts
= 10 };
36 enum { k_max_element_indices
= 20 };
38 enum { k_route_ui_max_verts
= k_max_ui_elements
*k_max_element_verts
};
39 enum { k_route_ui_max_indices
= k_max_ui_elements
*k_max_element_indices
};
43 k_logic_type_relay
= 1,
44 k_logic_type_chance
= 2,
45 k_logic_type_achievement
= 3
51 k_geo_type_nonsolid
= 1,
57 /* This is a small flag we use to changelevel.
58 * It will not be cleared until all sounds stop playing
62 * -------------------------------------------------------
65 char world_name
[ 64 ];
79 struct ub_world_lighting
82 v4f g_light_colours
[3],
83 g_light_directions
[3],
94 v4f g_point_light_positions
[32];
95 v4f g_point_light_colours
[32];
101 struct framebuffer heightmap
;
104 * Dynamically allocated when world_load is called.
106 * the following arrays index somewhere into this linear
110 * --------------------------------------------------------------------------
118 * Materials / textures
124 struct world_material
134 * Named safe places to respawn
146 * Audio player entities
148 struct world_audio_thing
155 audio_clip temp_embedded_clip
;
158 u32 audio_things_count
;
170 enum classtype classtype
;
179 * Box trigger entities
183 m4x3f transform
, inv_transform
;
185 struct relay_target target
;
193 struct logic_achievement
196 const char *achievement_id
;
199 * logic_achievements
;
200 u32 achievement_count
;
214 * Routes (world_routes.h)
215 * --------------------------------------------------------------------------
219 v3f co
, right
, up
, h
;
222 u32 special_type
, special_id
, current_refs
, ref_count
;
223 u32 route_ids
[4]; /* Gates can be linked into up to four routes */
239 double best_lap
, latest_pass
; /* Session */
241 m4x3f scoreboard_transform
;
254 m4x3f to_world
, recv_to_world
, /* TODO: can probably remove these */
264 u32 version
; /* Incremented on every teleport */
274 struct teleport_gate gate
;
277 u32 target_map_index
, working
;
280 u32 nonlocalgate_count
;
282 struct route_collector
284 struct route_timing timing
;
291 * ----------------------------------------------------
299 /* spacial mappings */
305 glmesh mesh_route_lines
;
310 rigidbody rb_geo
; /* todo.. ... */
313 VG_STATIC
struct world_global
316 * Allocated as system memory
317 * --------------------------------------------------------------------------
320 *audio_heap
; /* sub buffer of the audio buffer */
324 mdl_submesh dome_upper
, dome_lower
;
326 glmesh mesh_gate_surface
;
328 double sky_time
, sky_rate
, sky_target_rate
;
330 /* gates, TODO: active_gate should also know which instance */
333 double time
, rewind_from
, rewind_to
, last_use
;
335 /* water rendering */
338 struct framebuffer fbreflect
, fbdepth
;
342 /* split flap display */
345 mdl_submesh
*sm_module
, *sm_card
;
346 glmesh mesh_base
, mesh_display
;
353 /* timing bars, fixed maximum amount */
356 GLuint vao
, vbo
, ebo
;
361 struct route_ui_segment
364 u32 vertex_start
, vertex_count
,
365 index_start
, index_count
, notches
;
367 segments
[k_max_ui_segments
];
369 u32 segment_start
, segment_count
, fade_start
, fade_count
;
370 double fade_timer_start
;
376 int active_route_board
;
379 int switching_to_new_world
;
381 world_instance worlds
[4];
387 VG_STATIC world_instance
*get_active_world( void )
389 return &world_global
.worlds
[ world_global
.active_world
];
397 int ray_hit_is_ramp( world_instance
*world
, ray_hit
*hit
);
400 struct world_material
*ray_hit_material( world_instance
*world
, ray_hit
*hit
);
403 void ray_world_get_tri( world_instance
*world
, ray_hit
*hit
, v3f tri
[3] );
406 int ray_world( world_instance
*world
, v3f pos
, v3f dir
, ray_hit
*hit
);
412 #include "world_routes.h"
413 #include "world_sfd.h"
414 #include "world_render.h"
415 #include "world_water.h"
416 #include "world_gen.h"
417 #include "world_gate.h"
420 * -----------------------------------------------------------------------------
422 * -----------------------------------------------------------------------------
425 VG_STATIC
int world_stop_sound( int argc
, const char *argv
[] )
427 world_instance
*world
= get_active_world();
430 * None of our world audio runs as one shots, they always have a player.
431 * Therefore it is safe to delete clip data after the players are
435 for( int i
=0; i
<world
->audio_things_count
; i
++ )
437 struct world_audio_thing
*at
= &world
->audio_things
[i
];
439 if( audio_player_is_playing( &at
->player
) )
441 u32 cflags
= audio_player_get_flags( &at
->player
);
442 audio_player_set_flags( &at
->player
, cflags
| AUDIO_FLAG_KILL
);
450 VG_STATIC
int world_change_world( int argc
, const char *argv
[] )
453 world_instance
*world
= get_active_world();
457 vg_info( "%s\n", world
.world_name
);
462 vg_info( "Switching world...\n" );
463 strcpy( world
.world_name
, argv
[0] );
464 world
.switching_to_new_world
= 1;
465 world_stop_sound( 0, NULL
);
472 VG_STATIC
void world_init(void)
475 vg_var_push( (struct vg_var
){
476 .name
= "water_enable",
477 .data
= &world
.water
.enabled
,
478 .data_type
= k_var_dtype_i32
,
479 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
484 vg_function_push( (struct vg_cmd
)
486 .name
= "world_stop_sound",
487 .function
= world_stop_sound
490 vg_function_push( (struct vg_cmd
)
493 .function
= world_change_world
496 world_global
.sky_rate
= 1.0;
497 world_global
.sky_target_rate
= 1.0;
499 shader_scene_standard_register();
500 shader_scene_standard_alphatest_register();
501 shader_scene_vertex_blend_register();
502 shader_scene_terrain_register();
503 shader_scene_depth_register();
505 shader_model_sky_register();
507 vg_info( "Loading world resources\n" );
509 vg_linear_clear( vg_mem
.scratch
);
510 mdl_context
*msky
= mdl_load_full( vg_mem
.scratch
, "models/rs_skydome.mdl" );
512 mdl_node
*nupper
= mdl_node_from_name( msky
, "dome_complete" );
513 world_global
.dome_upper
= *mdl_node_submesh( msky
, nupper
, 0 );
515 vg_acquire_thread_sync();
517 mdl_unpack_glmesh( msky
, &world_global
.skydome
);
519 vg_release_thread_sync();
522 vg_info( "Loading other world systems\n" );
524 vg_loader_step( world_render_init
, NULL
);
525 vg_loader_step( world_sfd_init
, NULL
);
526 vg_loader_step( world_water_init
, NULL
);
527 vg_loader_step( world_gates_init
, NULL
);
528 vg_loader_step( world_routes_init
, NULL
);
530 /* Allocate dynamic world memory arena */
531 u32 max_size
= 76*1024*1024;
532 world_global
.generic_heap
= vg_create_linear_allocator( vg_mem
.rtmemory
,
537 VG_STATIC
void world_audio_init(void)
539 u32 size
= vg_linear_remaining( vg_audio
.audio_pool
)
540 - sizeof(vg_linear_allocator
);
542 world_global
.audio_heap
= vg_create_linear_allocator( vg_audio
.audio_pool
,
547 VG_STATIC
void world_trigger_achievement( world_instance
*world
, u32 uid
)
549 struct logic_achievement
*ach
= &world
->logic_achievements
[ uid
];
554 steam_set_achievement( ach
->achievement_id
);
555 steam_store_achievements();
560 VG_STATIC
void world_run_relay( world_instance
*world
,
561 struct relay_target
*rt
);
563 VG_STATIC
void world_trigger_relay( world_instance
*world
, u32 uid
)
565 struct logic_relay
*relay
= &world
->logic_relays
[ uid
];
567 for( int i
=0; i
<relay
->target_count
; i
++ )
569 world_run_relay( world
, &relay
->targets
[i
] );
573 VG_STATIC
void world_trigger_audio( world_instance
*world
, u32 uid
)
575 struct world_audio_thing
*wat
= &world
->audio_things
[ uid
];
578 audio_player_playclip( &wat
->player
,
579 &wat
->temp_embedded_clip
);
583 VG_STATIC
void world_run_relay( world_instance
*world
,
584 struct relay_target
*rt
)
586 struct entity_instruction
588 enum classtype classtype
;
589 void (*p_trigger
)( world_instance
*world
, u32 uid
);
591 entity_instructions
[] =
593 { k_classtype_logic_achievement
, world_trigger_achievement
},
594 { k_classtype_logic_relay
, world_trigger_relay
},
595 { k_classtype_audio
, world_trigger_audio
}
598 for( int i
=0; i
<vg_list_size(entity_instructions
); i
++ )
600 struct entity_instruction
*instr
= &entity_instructions
[i
];
602 if( instr
->classtype
== rt
->classtype
)
604 instr
->p_trigger( world
, rt
->sub_id
);
609 vg_error( "Don't know how to trigger classtype %d\n", rt
->classtype
);
612 VG_STATIC
void world_update( world_instance
*world
, v3f pos
)
615 if( world
.switching_to_new_world
)
620 for( int i
=0; i
<world
.audio_things_count
; i
++ )
622 struct world_audio_thing
*at
= &world
.audio_things
[i
];
624 if( audio_player_is_playing( &at
->player
) )
634 world
.switching_to_new_world
= 0;
636 vg_loader_start( world_load
);
641 world
.sky_time
+= world
.sky_rate
* vg
.time_delta
;
642 world
.sky_rate
= vg_lerp( world
.sky_rate
, world
.sky_target_rate
,
643 vg
.time_delta
* 5.0 );
646 world_routes_update( world
);
648 world_routes_debug();
651 if( world
->route_count
> 0 )
654 float min_dist
= INFINITY
;
656 for( int i
=0; i
<world
->route_count
; i
++ )
658 float d
= v3_dist2( world
->routes
[i
].scoreboard_transform
[3], pos
);
667 if( (world_global
.active_route_board
!= closest
)
668 || network_scores_updated
)
670 network_scores_updated
= 0;
671 world_global
.active_route_board
= closest
;
673 struct route
*route
= &world
->routes
[closest
];
675 u32 id
= route
->track_id
;
677 if( id
!= 0xffffffff )
679 struct netmsg_board
*local_board
=
680 &scoreboard_client_data
.boards
[id
];
682 for( int i
=0; i
<13; i
++ )
684 sfd_encode( i
, &local_board
->data
[27*i
] );
691 for( int i
=0; i
<world
->trigger_count
; i
++ )
693 struct trigger_zone
*zone
= &world
->triggers
[i
];
696 m4x3_mulv( zone
->inv_transform
, pos
, local
);
698 if( (fabsf(local
[0]) <= 1.0f
) &&
699 (fabsf(local
[1]) <= 1.0f
) &&
700 (fabsf(local
[2]) <= 1.0f
) )
704 if( !world_global
.in_trigger
)
706 world_run_relay( world
, &zone
->target
);
710 vg_line_boxf_transformed( zone
->transform
, (boxf
){{-1.0f
,-1.0f
,-1.0f
},
711 { 1.0f
, 1.0f
, 1.0f
}},
715 for( int i
=0; i
<world
->light_count
; i
++ )
717 struct world_light
*light
= &world
->lights
[i
];
719 u32 colour
= 0xff000000;
720 u8 r
= light
->colour
[0] * 255.0f
,
721 g
= light
->colour
[1] * 255.0f
,
722 b
= light
->colour
[2] * 255.0f
;
728 vg_line_pt3( light
->co
, 0.25f
, colour
);
731 world_global
.in_trigger
= in_trigger
;
736 * -----------------------------------------------------------------------------
738 * -----------------------------------------------------------------------------
741 VG_STATIC
void ray_world_get_tri( world_instance
*world
,
742 ray_hit
*hit
, v3f tri
[3] )
744 for( int i
=0; i
<3; i
++ )
745 v3_copy( world
->scene_geo
->arrvertices
[ hit
->tri
[i
] ].co
, tri
[i
] );
748 VG_STATIC
int ray_world( world_instance
*world
,
749 v3f pos
, v3f dir
, ray_hit
*hit
)
751 return scene_raycast( world
->scene_geo
, world
->geo_bh
, pos
, dir
, hit
);
755 * Cast a sphere from a to b and see what time it hits
757 VG_STATIC
int spherecast_world( world_instance
*world
,
758 v3f pa
, v3f pb
, float r
, float *t
, v3f n
)
761 bh_iter_init( 0, &it
);
764 box_init_inf( region
);
765 box_addpt( region
, pa
);
766 box_addpt( region
, pb
);
768 v3_add( (v3f
){ r
, r
, r
}, region
[1], region
[1] );
769 v3_add( (v3f
){-r
,-r
,-r
}, region
[0], region
[0] );
772 v3_sub( pb
, pa
, dir
);
775 dir_inv
[0] = 1.0f
/dir
[0];
776 dir_inv
[1] = 1.0f
/dir
[1];
777 dir_inv
[2] = 1.0f
/dir
[2];
783 while( bh_next( world
->geo_bh
, &it
, region
, &idx
) )
785 u32
*ptri
= &world
->scene_geo
->arrindices
[ idx
*3 ];
791 for( int j
=0; j
<3; j
++ )
793 v3_copy( world
->scene_geo
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
794 box_addpt( box
, tri
[j
] );
797 v3_add( (v3f
){ r
, r
, r
}, box
[1], box
[1] );
798 v3_add( (v3f
){-r
,-r
,-r
}, box
[0], box
[0] );
800 if( !ray_aabb1( box
, pa
, dir_inv
, 1.0f
) )
805 if( spherecast_triangle( tri
, pa
, dir
, r
, &t
, n1
) )
821 struct world_material
*world_tri_index_material( world_instance
*world
,
824 for( int i
=1; i
<world
->material_count
; i
++ )
826 struct world_material
*mat
= &world
->materials
[i
];
828 if( (index
>= mat
->sm_geo
.vertex_start
) &&
829 (index
< mat
->sm_geo
.vertex_start
+mat
->sm_geo
.vertex_count
) )
836 return &world
->materials
[0];
839 VG_STATIC
struct world_material
*world_contact_material( world_instance
*world
,
842 return world_tri_index_material( world
, ct
->element_id
);
845 VG_STATIC
struct world_material
*ray_hit_material( world_instance
*world
,
848 return world_tri_index_material( world
, hit
->tri
[0] );