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/terrain.h"
23 #include "shaders/sky.h"
24 #include "shaders/planeinf.h"
25 #include "shaders/standard.h"
26 #include "shaders/vblend.h"
27 #include "shaders/gpos.h"
28 #include "shaders/fscolour.h"
29 #include "shaders/alphatest.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
49 VG_STATIC
struct gworld
52 * Allocated as system memory
53 * --------------------------------------------------------------------------
58 mdl_submesh dome_upper
, dome_lower
;
60 glmesh mesh_gate_surface
;
62 double sky_time
, sky_rate
, sky_target_rate
;
67 struct framebuffer fbreflect
, fbdepth
;
78 /* split flap display */
81 mdl_submesh
*sm_module
, *sm_card
;
82 glmesh mesh_base
, mesh_display
;
89 /* timing bars, fixed maximum amount */
97 struct route_ui_segment
100 u32 vertex_start
, vertex_count
,
101 index_start
, index_count
, notches
;
103 segments
[k_max_ui_segments
];
105 u32 segment_start
, segment_count
, fade_start
, fade_count
;
106 double fade_timer_start
;
112 int active_route_board
;
115 /* This is a small flag we use to changelevel.
116 * It will not be cleared until all sounds stop playing
118 int switching_to_new_world
;
119 char world_name
[ 64 ];
122 * Dynamically allocated when world_load is called.
124 * the following arrays index somewhere into this linear
128 * --------------------------------------------------------------------------
131 *audio_vgl
; /* sub buffer of the audio buffer */
139 * Named safe places to respawn
151 * Audio player entities
153 struct world_audio_thing
160 audio_clip temp_embedded_clip
;
163 u32 audio_things_count
;
175 enum classtype classtype
;
184 * Box trigger entities
188 m4x3f transform
, inv_transform
;
190 struct relay_target target
;
198 struct logic_achievement
201 const char *achievement_id
;
204 * logic_achievements
;
205 u32 achievement_count
;
209 * Routes (world_routes.h)
210 * --------------------------------------------------------------------------
214 v3f co
, right
, up
, h
;
217 u32 special_type
, special_id
, current_refs
, ref_count
;
218 u32 route_ids
[4]; /* Gates can be linked into up to four routes */
234 double best_lap
, latest_pass
; /* Session */
236 m4x3f scoreboard_transform
;
249 m4x3f to_world
, recv_to_world
, transport
;
257 u32 version
; /* Incremented on every teleport */
265 struct route_collector
267 struct route_timing timing
;
274 * ----------------------------------------------------
279 double time
, rewind_from
, rewind_to
, last_use
;
286 /* spacial mappings */
299 /* TODO Maybe make this less hardcoded...
301 mdl_submesh sm_geo_std_oob
, sm_geo_std
, sm_geo_vb
,
302 sm_foliage_main
, sm_foliage_alphatest
,
303 sm_graffiti
, sm_subworld
, sm_terrain
;
313 VG_STATIC
int ray_hit_is_ramp( ray_hit
*hit
);
314 VG_STATIC
int ray_hit_is_terrain( ray_hit
*hit
);
315 VG_STATIC
void ray_world_get_tri( ray_hit
*hit
, v3f tri
[3] );
316 VG_STATIC
int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
);
322 #include "world_routes.h"
323 #include "world_sfd.h"
324 #include "world_render.h"
325 #include "world_water.h"
326 #include "world_gen.h"
327 #include "world_gate.h"
330 * -----------------------------------------------------------------------------
332 * -----------------------------------------------------------------------------
335 VG_STATIC
int world_stop_sound( int argc
, const char *argv
[] )
338 * None of our world audio runs as one shots, they always have a player.
339 * Therefore it is safe to delete clip data after the players are
343 for( int i
=0; i
<world
.audio_things_count
; i
++ )
345 struct world_audio_thing
*at
= &world
.audio_things
[i
];
347 if( audio_player_is_playing( &at
->player
) )
349 u32 cflags
= audio_player_get_flags( &at
->player
);
350 audio_player_set_flags( &at
->player
, cflags
| AUDIO_FLAG_KILL
);
358 VG_STATIC
int world_change_world( int argc
, const char *argv
[] )
362 vg_info( "%s\n", world
.world_name
);
367 vg_info( "Switching world...\n" );
368 strcpy( world
.world_name
, argv
[0] );
369 world
.switching_to_new_world
= 1;
370 world_stop_sound( 0, NULL
);
376 VG_STATIC
void world_init(void)
378 vg_convar_push( (struct vg_convar
){
379 .name
= "water_enable",
380 .data
= &world
.water
.enabled
,
381 .data_type
= k_convar_dtype_i32
,
382 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
386 vg_function_push( (struct vg_cmd
)
388 .name
= "world_stop_sound",
389 .function
= world_stop_sound
392 vg_function_push( (struct vg_cmd
)
395 .function
= world_change_world
398 world
.sky_rate
= 1.0;
399 world
.sky_target_rate
= 1.0;
401 shader_terrain_register();
402 shader_sky_register();
403 shader_planeinf_register();
404 shader_gpos_register();
405 shader_fscolour_register();
406 shader_alphatest_register();
408 vg_info( "Loading world resources\n" );
410 vg_linear_clear( vg_mem
.scratch
);
411 mdl_context
*msky
= mdl_load_full( vg_mem
.scratch
, "models/rs_skydome.mdl" );
413 mdl_node
*nlower
= mdl_node_from_name( msky
, "dome_lower" ),
414 *nupper
= mdl_node_from_name( msky
, "dome_upper" );
416 world
.dome_lower
= *mdl_node_submesh( msky
, nlower
, 0 );
417 world
.dome_upper
= *mdl_node_submesh( msky
, nupper
, 0 );
419 vg_acquire_thread_sync();
421 mdl_unpack_glmesh( msky
, &world
.skydome
);
423 vg_release_thread_sync();
426 vg_info( "Loading other world systems\n" );
428 vg_loader_highwater( world_render_init
, NULL
, NULL
);
429 vg_loader_highwater( world_sfd_init
, NULL
, NULL
);
430 vg_loader_highwater( world_water_init
, NULL
, NULL
);
431 vg_loader_highwater( world_gates_init
, NULL
, NULL
);
432 vg_loader_highwater( world_routes_init
, NULL
, NULL
);
434 /* Allocate dynamic world memory arena */
435 u32 max_size
= 72*1024*1024;
436 world
.dynamic_vgl
= vg_create_linear_allocator( vg_mem
.rtmemory
, max_size
,
440 VG_STATIC
void world_audio_init(void)
442 u32 size
= vg_linear_remaining( vg_audio
.audio_pool
)
443 - sizeof(vg_linear_allocator
);
445 world
.audio_vgl
= vg_create_linear_allocator( vg_audio
.audio_pool
,
446 size
, VG_MEMORY_SYSTEM
);
449 VG_STATIC
void world_trigger_achievement( u32 uid
)
451 struct logic_achievement
*ach
= &world
.logic_achievements
[ uid
];
456 steam_set_achievement( ach
->achievement_id
);
457 steam_store_achievements();
462 VG_STATIC
void world_run_relay( struct relay_target
*rt
);
463 VG_STATIC
void world_trigger_relay( u32 uid
)
465 struct logic_relay
*relay
= &world
.logic_relays
[ uid
];
467 for( int i
=0; i
<relay
->target_count
; i
++ )
469 world_run_relay( &relay
->targets
[i
] );
473 VG_STATIC
void world_trigger_audio( u32 uid
)
475 struct world_audio_thing
*wat
= &world
.audio_things
[ uid
];
478 audio_player_playclip( &wat
->player
,
479 &wat
->temp_embedded_clip
);
483 VG_STATIC
void world_run_relay( struct relay_target
*rt
)
485 struct entity_instruction
487 enum classtype classtype
;
488 void (*p_trigger
)( u32 uid
);
490 entity_instructions
[] =
492 { k_classtype_logic_achievement
, world_trigger_achievement
},
493 { k_classtype_logic_relay
, world_trigger_relay
},
494 { k_classtype_audio
, world_trigger_audio
}
497 for( int i
=0; i
<vg_list_size(entity_instructions
); i
++ )
499 struct entity_instruction
*instr
= &entity_instructions
[i
];
501 if( instr
->classtype
== rt
->classtype
)
503 instr
->p_trigger( rt
->sub_id
);
508 vg_error( "Don't know how to trigger classtype %d\n", rt
->classtype
);
511 VG_STATIC
void world_update( v3f pos
)
513 if( world
.switching_to_new_world
)
518 for( int i
=0; i
<world
.audio_things_count
; i
++ )
520 struct world_audio_thing
*at
= &world
.audio_things
[i
];
522 if( audio_player_is_playing( &at
->player
) )
532 world
.switching_to_new_world
= 0;
539 world
.sky_time
+= world
.sky_rate
* vg
.time_delta
;
540 world
.sky_rate
= vg_lerp( world
.sky_rate
, world
.sky_target_rate
,
541 vg
.time_delta
* 5.0 );
543 world_routes_update();
545 world_routes_debug();
549 float min_dist
= INFINITY
;
551 for( int i
=0; i
<world
.route_count
; i
++ )
553 float d
= v3_dist2( world
.routes
[i
].scoreboard_transform
[3], pos
);
562 if( (world
.active_route_board
!= closest
) || network_scores_updated
)
564 network_scores_updated
= 0;
565 world
.active_route_board
= closest
;
567 struct route
*route
= &world
.routes
[closest
];
569 u32 id
= route
->track_id
;
571 if( id
!= 0xffffffff )
573 struct netmsg_board
*local_board
= &scoreboard_client_data
.boards
[id
];
575 for( int i
=0; i
<13; i
++ )
577 sfd_encode( i
, &local_board
->data
[27*i
] );
583 for( int i
=0; i
<world
.trigger_count
; i
++ )
585 struct trigger_zone
*zone
= &world
.triggers
[i
];
588 m4x3_mulv( zone
->inv_transform
, pos
, local
);
590 if( (fabsf(local
[0]) <= 1.0f
) &&
591 (fabsf(local
[1]) <= 1.0f
) &&
592 (fabsf(local
[2]) <= 1.0f
) )
596 if( !world
.in_trigger
)
598 world_run_relay( &zone
->target
);
602 vg_line_boxf_transformed( zone
->transform
, (boxf
){{-1.0f
,-1.0f
,-1.0f
},
603 { 1.0f
, 1.0f
, 1.0f
}},
607 world
.in_trigger
= in_trigger
;
612 * -----------------------------------------------------------------------------
614 * -----------------------------------------------------------------------------
617 VG_STATIC
void ray_world_get_tri( ray_hit
*hit
, v3f tri
[3] )
619 for( int i
=0; i
<3; i
++ )
620 v3_copy( world
.scene_geo
->arrvertices
[ hit
->tri
[i
] ].co
, tri
[i
] );
623 VG_STATIC
int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
)
625 return scene_raycast( world
.scene_geo
, world
.geo_bh
, pos
, dir
, hit
);
628 VG_STATIC
int ray_hit_is_terrain( ray_hit
*hit
)
631 valid_end
= world
.sm_terrain
.vertex_count
;
633 return (hit
->tri
[0] >= valid_start
) &&
634 (hit
->tri
[0] < valid_end
);
637 VG_STATIC
int ray_hit_is_ramp( ray_hit
*hit
)
639 u32 valid_start
= world
.sm_geo_std
.vertex_start
,
640 valid_end
= world
.sm_geo_vb
.vertex_start
;
642 return (hit
->tri
[0] >= valid_start
) &&
643 (hit
->tri
[0] < valid_end
);