2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
7 VG_STATIC
int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
);
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,
55 VG_STATIC
struct gworld
58 * Allocated as system memory
59 * --------------------------------------------------------------------------
64 mdl_submesh dome_upper
, dome_lower
;
66 glmesh mesh_gate_surface
;
68 double sky_time
, sky_rate
, sky_target_rate
;
73 struct framebuffer fbreflect
, fbdepth
;
84 /* split flap display */
87 mdl_submesh
*sm_module
, *sm_card
;
88 glmesh mesh_base
, mesh_display
;
95 /* timing bars, fixed maximum amount */
103 struct route_ui_segment
106 u32 vertex_start
, vertex_count
,
107 index_start
, index_count
, notches
;
109 segments
[k_max_ui_segments
];
111 u32 segment_start
, segment_count
, fade_start
, fade_count
;
112 double fade_timer_start
;
118 int active_route_board
;
121 /* This is a small flag we use to changelevel.
122 * It will not be cleared until all sounds stop playing
124 int switching_to_new_world
;
125 char world_name
[ 64 ];
128 * Dynamically allocated when world_load is called.
130 * the following arrays index somewhere into this linear
134 * --------------------------------------------------------------------------
137 *audio_vgl
; /* sub buffer of the audio buffer */
145 * Materials / textures
151 struct world_material
161 * Named safe places to respawn
173 * Audio player entities
175 struct world_audio_thing
182 audio_clip temp_embedded_clip
;
185 u32 audio_things_count
;
197 enum classtype classtype
;
206 * Box trigger entities
210 m4x3f transform
, inv_transform
;
212 struct relay_target target
;
220 struct logic_achievement
223 const char *achievement_id
;
226 * logic_achievements
;
227 u32 achievement_count
;
241 * Routes (world_routes.h)
242 * --------------------------------------------------------------------------
246 v3f co
, right
, up
, h
;
249 u32 special_type
, special_id
, current_refs
, ref_count
;
250 u32 route_ids
[4]; /* Gates can be linked into up to four routes */
266 double best_lap
, latest_pass
; /* Session */
268 m4x3f scoreboard_transform
;
281 m4x3f to_world
, recv_to_world
, transport
;
289 u32 version
; /* Incremented on every teleport */
297 struct route_collector
299 struct route_timing timing
;
306 * ----------------------------------------------------
311 double time
, rewind_from
, rewind_to
, last_use
;
318 /* spacial mappings */
324 glmesh mesh_route_lines
;
330 mdl_submesh sm_foliage_main
;
331 rigidbody rb_geo
; /* todo.. ... */
340 VG_STATIC
int ray_hit_is_ramp( ray_hit
*hit
);
341 VG_STATIC
struct world_material
*ray_hit_material( ray_hit
*hit
);
342 VG_STATIC
void ray_world_get_tri( ray_hit
*hit
, v3f tri
[3] );
343 VG_STATIC
int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
);
349 #include "world_routes.h"
350 #include "world_sfd.h"
351 #include "world_render.h"
352 #include "world_water.h"
353 #include "world_gen.h"
354 #include "world_gate.h"
357 * -----------------------------------------------------------------------------
359 * -----------------------------------------------------------------------------
362 VG_STATIC
int world_stop_sound( int argc
, const char *argv
[] )
365 * None of our world audio runs as one shots, they always have a player.
366 * Therefore it is safe to delete clip data after the players are
370 for( int i
=0; i
<world
.audio_things_count
; i
++ )
372 struct world_audio_thing
*at
= &world
.audio_things
[i
];
374 if( audio_player_is_playing( &at
->player
) )
376 u32 cflags
= audio_player_get_flags( &at
->player
);
377 audio_player_set_flags( &at
->player
, cflags
| AUDIO_FLAG_KILL
);
385 VG_STATIC
int world_change_world( int argc
, const char *argv
[] )
389 vg_info( "%s\n", world
.world_name
);
394 vg_info( "Switching world...\n" );
395 strcpy( world
.world_name
, argv
[0] );
396 world
.switching_to_new_world
= 1;
397 world_stop_sound( 0, NULL
);
403 VG_STATIC
void world_init(void)
405 vg_var_push( (struct vg_var
){
406 .name
= "water_enable",
407 .data
= &world
.water
.enabled
,
408 .data_type
= k_var_dtype_i32
,
409 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
413 vg_function_push( (struct vg_cmd
)
415 .name
= "world_stop_sound",
416 .function
= world_stop_sound
419 vg_function_push( (struct vg_cmd
)
422 .function
= world_change_world
425 world
.sky_rate
= 1.0;
426 world
.sky_target_rate
= 1.0;
428 shader_scene_standard_register();
429 shader_scene_standard_alphatest_register();
430 shader_scene_vertex_blend_register();
431 shader_scene_terrain_register();
432 shader_scene_depth_register();
434 shader_model_sky_register();
436 vg_info( "Loading world resources\n" );
438 vg_linear_clear( vg_mem
.scratch
);
439 mdl_context
*msky
= mdl_load_full( vg_mem
.scratch
, "models/rs_skydome.mdl" );
441 mdl_node
*nupper
= mdl_node_from_name( msky
, "dome_complete" );
442 world
.dome_upper
= *mdl_node_submesh( msky
, nupper
, 0 );
444 vg_acquire_thread_sync();
446 mdl_unpack_glmesh( msky
, &world
.skydome
);
448 vg_release_thread_sync();
451 vg_info( "Loading other world systems\n" );
453 vg_loader_step( world_render_init
, NULL
);
454 vg_loader_step( world_sfd_init
, NULL
);
455 vg_loader_step( world_water_init
, NULL
);
456 vg_loader_step( world_gates_init
, NULL
);
457 vg_loader_step( world_routes_init
, NULL
);
459 /* Allocate dynamic world memory arena */
460 u32 max_size
= 76*1024*1024;
461 world
.dynamic_vgl
= vg_create_linear_allocator( vg_mem
.rtmemory
, max_size
,
465 VG_STATIC
void world_audio_init(void)
467 u32 size
= vg_linear_remaining( vg_audio
.audio_pool
)
468 - sizeof(vg_linear_allocator
);
470 world
.audio_vgl
= vg_create_linear_allocator( vg_audio
.audio_pool
,
471 size
, VG_MEMORY_SYSTEM
);
474 VG_STATIC
void world_trigger_achievement( u32 uid
)
476 struct logic_achievement
*ach
= &world
.logic_achievements
[ uid
];
481 steam_set_achievement( ach
->achievement_id
);
482 steam_store_achievements();
487 VG_STATIC
void world_run_relay( struct relay_target
*rt
);
488 VG_STATIC
void world_trigger_relay( u32 uid
)
490 struct logic_relay
*relay
= &world
.logic_relays
[ uid
];
492 for( int i
=0; i
<relay
->target_count
; i
++ )
494 world_run_relay( &relay
->targets
[i
] );
498 VG_STATIC
void world_trigger_audio( u32 uid
)
500 struct world_audio_thing
*wat
= &world
.audio_things
[ uid
];
503 audio_player_playclip( &wat
->player
,
504 &wat
->temp_embedded_clip
);
508 VG_STATIC
void world_run_relay( struct relay_target
*rt
)
510 struct entity_instruction
512 enum classtype classtype
;
513 void (*p_trigger
)( u32 uid
);
515 entity_instructions
[] =
517 { k_classtype_logic_achievement
, world_trigger_achievement
},
518 { k_classtype_logic_relay
, world_trigger_relay
},
519 { k_classtype_audio
, world_trigger_audio
}
522 for( int i
=0; i
<vg_list_size(entity_instructions
); i
++ )
524 struct entity_instruction
*instr
= &entity_instructions
[i
];
526 if( instr
->classtype
== rt
->classtype
)
528 instr
->p_trigger( rt
->sub_id
);
533 vg_error( "Don't know how to trigger classtype %d\n", rt
->classtype
);
536 VG_STATIC
void world_update( v3f pos
)
538 if( world
.switching_to_new_world
)
543 for( int i
=0; i
<world
.audio_things_count
; i
++ )
545 struct world_audio_thing
*at
= &world
.audio_things
[i
];
547 if( audio_player_is_playing( &at
->player
) )
557 world
.switching_to_new_world
= 0;
559 vg_loader_start( world_load
);
564 world
.sky_time
+= world
.sky_rate
* vg
.time_delta
;
565 world
.sky_rate
= vg_lerp( world
.sky_rate
, world
.sky_target_rate
,
566 vg
.time_delta
* 5.0 );
568 world_routes_update();
570 world_routes_debug();
573 if( world
.route_count
> 0 )
576 float min_dist
= INFINITY
;
578 for( int i
=0; i
<world
.route_count
; i
++ )
580 float d
= v3_dist2( world
.routes
[i
].scoreboard_transform
[3], pos
);
589 if( (world
.active_route_board
!= closest
) || network_scores_updated
)
591 network_scores_updated
= 0;
592 world
.active_route_board
= closest
;
594 struct route
*route
= &world
.routes
[closest
];
596 u32 id
= route
->track_id
;
598 if( id
!= 0xffffffff )
600 struct netmsg_board
*local_board
=
601 &scoreboard_client_data
.boards
[id
];
603 for( int i
=0; i
<13; i
++ )
605 sfd_encode( i
, &local_board
->data
[27*i
] );
612 for( int i
=0; i
<world
.trigger_count
; i
++ )
614 struct trigger_zone
*zone
= &world
.triggers
[i
];
617 m4x3_mulv( zone
->inv_transform
, pos
, local
);
619 if( (fabsf(local
[0]) <= 1.0f
) &&
620 (fabsf(local
[1]) <= 1.0f
) &&
621 (fabsf(local
[2]) <= 1.0f
) )
625 if( !world
.in_trigger
)
627 world_run_relay( &zone
->target
);
631 vg_line_boxf_transformed( zone
->transform
, (boxf
){{-1.0f
,-1.0f
,-1.0f
},
632 { 1.0f
, 1.0f
, 1.0f
}},
636 for( int i
=0; i
<world
.light_count
; i
++ )
638 struct world_light
*light
= &world
.lights
[i
];
640 u32 colour
= 0xff000000;
641 u8 r
= light
->colour
[0] * 255.0f
,
642 g
= light
->colour
[1] * 255.0f
,
643 b
= light
->colour
[2] * 255.0f
;
649 vg_line_pt3( light
->co
, 0.25f
, colour
);
652 world
.in_trigger
= in_trigger
;
657 * -----------------------------------------------------------------------------
659 * -----------------------------------------------------------------------------
662 VG_STATIC
void ray_world_get_tri( ray_hit
*hit
, v3f tri
[3] )
664 for( int i
=0; i
<3; i
++ )
665 v3_copy( world
.scene_geo
->arrvertices
[ hit
->tri
[i
] ].co
, tri
[i
] );
668 VG_STATIC
int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
)
670 return scene_raycast( world
.scene_geo
, world
.geo_bh
, pos
, dir
, hit
);
674 * Cast a sphere from a to b and see what time it hits
676 VG_STATIC
int spherecast_world( v3f pa
, v3f pb
, float r
, float *t
, v3f n
)
679 bh_iter_init( 0, &it
);
682 box_init_inf( region
);
683 box_addpt( region
, pa
);
684 box_addpt( region
, pb
);
686 v3_add( (v3f
){ r
, r
, r
}, region
[1], region
[1] );
687 v3_add( (v3f
){-r
,-r
,-r
}, region
[0], region
[0] );
690 v3_sub( pb
, pa
, dir
);
693 dir_inv
[0] = 1.0f
/dir
[0];
694 dir_inv
[1] = 1.0f
/dir
[1];
695 dir_inv
[2] = 1.0f
/dir
[2];
701 while( bh_next( world
.geo_bh
, &it
, region
, &idx
) )
703 u32
*ptri
= &world
.scene_geo
->arrindices
[ idx
*3 ];
709 for( int j
=0; j
<3; j
++ )
711 v3_copy( world
.scene_geo
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
712 box_addpt( box
, tri
[j
] );
715 v3_add( (v3f
){ r
, r
, r
}, box
[1], box
[1] );
716 v3_add( (v3f
){-r
,-r
,-r
}, box
[0], box
[0] );
718 if( !ray_aabb1( box
, pa
, dir_inv
, 1.0f
) )
723 if( spherecast_triangle( tri
, pa
, dir
, r
, &t
, n1
) )
738 VG_STATIC
struct world_material
*world_tri_index_material( u32 index
)
740 for( int i
=1; i
<world
.material_count
; i
++ )
742 struct world_material
*mat
= &world
.materials
[i
];
744 if( (index
>= mat
->sm_geo
.vertex_start
) &&
745 (index
< mat
->sm_geo
.vertex_start
+mat
->sm_geo
.vertex_count
) )
752 return &world
.materials
[0];
755 VG_STATIC
struct world_material
*world_contact_material( rb_ct
*ct
)
757 return world_tri_index_material( ct
->element_id
);
760 VG_STATIC
struct world_material
*ray_hit_material( ray_hit
*hit
)
762 return world_tri_index_material( hit
->tri
[0] );