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
52 k_geo_type_nonsolid
= 1,
58 k_material_flag_skate_surface
= 0x1,
59 k_material_flag_collision
= 0x2
62 VG_STATIC
struct gworld
65 * Allocated as system memory
66 * --------------------------------------------------------------------------
71 mdl_submesh dome_upper
, dome_lower
;
73 glmesh mesh_gate_surface
;
75 double sky_time
, sky_rate
, sky_target_rate
;
80 struct framebuffer fbreflect
, fbdepth
;
91 /* split flap display */
94 mdl_submesh
*sm_module
, *sm_card
;
95 glmesh mesh_base
, mesh_display
;
102 /* timing bars, fixed maximum amount */
105 GLuint vao
, vbo
, ebo
;
110 struct route_ui_segment
113 u32 vertex_start
, vertex_count
,
114 index_start
, index_count
, notches
;
116 segments
[k_max_ui_segments
];
118 u32 segment_start
, segment_count
, fade_start
, fade_count
;
119 double fade_timer_start
;
125 int active_route_board
;
128 /* This is a small flag we use to changelevel.
129 * It will not be cleared until all sounds stop playing
131 int switching_to_new_world
;
132 char world_name
[ 64 ];
135 * Dynamically allocated when world_load is called.
137 * the following arrays index somewhere into this linear
141 * --------------------------------------------------------------------------
144 *audio_vgl
; /* sub buffer of the audio buffer */
152 * Materials / textures
158 struct world_material
168 * Named safe places to respawn
180 * Audio player entities
182 struct world_audio_thing
189 audio_clip temp_embedded_clip
;
192 u32 audio_things_count
;
204 enum classtype classtype
;
213 * Box trigger entities
217 m4x3f transform
, inv_transform
;
219 struct relay_target target
;
227 struct logic_achievement
230 const char *achievement_id
;
233 * logic_achievements
;
234 u32 achievement_count
;
238 * Routes (world_routes.h)
239 * --------------------------------------------------------------------------
243 v3f co
, right
, up
, h
;
246 u32 special_type
, special_id
, current_refs
, ref_count
;
247 u32 route_ids
[4]; /* Gates can be linked into up to four routes */
263 double best_lap
, latest_pass
; /* Session */
265 m4x3f scoreboard_transform
;
278 m4x3f to_world
, recv_to_world
, transport
;
286 u32 version
; /* Incremented on every teleport */
294 struct route_collector
296 struct route_timing timing
;
303 * ----------------------------------------------------
308 double time
, rewind_from
, rewind_to
, last_use
;
315 /* spacial mappings */
321 glmesh mesh_route_lines
;
327 mdl_submesh sm_foliage_main
;
337 VG_STATIC
int ray_hit_is_ramp( ray_hit
*hit
);
338 VG_STATIC
int ray_hit_is_terrain( ray_hit
*hit
);
339 VG_STATIC
void ray_world_get_tri( ray_hit
*hit
, v3f tri
[3] );
340 VG_STATIC
int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
);
346 #include "world_routes.h"
347 #include "world_sfd.h"
348 #include "world_render.h"
349 #include "world_water.h"
350 #include "world_gen.h"
351 #include "world_gate.h"
354 * -----------------------------------------------------------------------------
356 * -----------------------------------------------------------------------------
359 VG_STATIC
int world_stop_sound( int argc
, const char *argv
[] )
362 * None of our world audio runs as one shots, they always have a player.
363 * Therefore it is safe to delete clip data after the players are
367 for( int i
=0; i
<world
.audio_things_count
; i
++ )
369 struct world_audio_thing
*at
= &world
.audio_things
[i
];
371 if( audio_player_is_playing( &at
->player
) )
373 u32 cflags
= audio_player_get_flags( &at
->player
);
374 audio_player_set_flags( &at
->player
, cflags
| AUDIO_FLAG_KILL
);
382 VG_STATIC
int world_change_world( int argc
, const char *argv
[] )
386 vg_info( "%s\n", world
.world_name
);
391 vg_info( "Switching world...\n" );
392 strcpy( world
.world_name
, argv
[0] );
393 world
.switching_to_new_world
= 1;
394 world_stop_sound( 0, NULL
);
400 VG_STATIC
void world_init(void)
402 vg_convar_push( (struct vg_convar
){
403 .name
= "water_enable",
404 .data
= &world
.water
.enabled
,
405 .data_type
= k_convar_dtype_i32
,
406 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
410 vg_function_push( (struct vg_cmd
)
412 .name
= "world_stop_sound",
413 .function
= world_stop_sound
416 vg_function_push( (struct vg_cmd
)
419 .function
= world_change_world
422 world
.sky_rate
= 1.0;
423 world
.sky_target_rate
= 1.0;
425 shader_terrain_register();
426 shader_sky_register();
427 shader_planeinf_register();
428 shader_gpos_register();
429 shader_fscolour_register();
430 shader_alphatest_register();
432 vg_info( "Loading world resources\n" );
434 vg_linear_clear( vg_mem
.scratch
);
435 mdl_context
*msky
= mdl_load_full( vg_mem
.scratch
, "models/rs_skydome.mdl" );
437 mdl_node
*nlower
= mdl_node_from_name( msky
, "dome_lower" ),
438 *nupper
= mdl_node_from_name( msky
, "dome_upper" );
440 world
.dome_lower
= *mdl_node_submesh( msky
, nlower
, 0 );
441 world
.dome_upper
= *mdl_node_submesh( msky
, nupper
, 0 );
443 vg_acquire_thread_sync();
445 mdl_unpack_glmesh( msky
, &world
.skydome
);
447 vg_release_thread_sync();
450 vg_info( "Loading other world systems\n" );
452 vg_loader_highwater( world_render_init
, NULL
, NULL
);
453 vg_loader_highwater( world_sfd_init
, NULL
, NULL
);
454 vg_loader_highwater( world_water_init
, NULL
, NULL
);
455 vg_loader_highwater( world_gates_init
, NULL
, NULL
);
456 vg_loader_highwater( world_routes_init
, NULL
, NULL
);
458 /* Allocate dynamic world memory arena */
459 u32 max_size
= 72*1024*1024;
460 world
.dynamic_vgl
= vg_create_linear_allocator( vg_mem
.rtmemory
, max_size
,
464 VG_STATIC
void world_audio_init(void)
466 u32 size
= vg_linear_remaining( vg_audio
.audio_pool
)
467 - sizeof(vg_linear_allocator
);
469 world
.audio_vgl
= vg_create_linear_allocator( vg_audio
.audio_pool
,
470 size
, VG_MEMORY_SYSTEM
);
473 VG_STATIC
void world_trigger_achievement( u32 uid
)
475 struct logic_achievement
*ach
= &world
.logic_achievements
[ uid
];
480 steam_set_achievement( ach
->achievement_id
);
481 steam_store_achievements();
486 VG_STATIC
void world_run_relay( struct relay_target
*rt
);
487 VG_STATIC
void world_trigger_relay( u32 uid
)
489 struct logic_relay
*relay
= &world
.logic_relays
[ uid
];
491 for( int i
=0; i
<relay
->target_count
; i
++ )
493 world_run_relay( &relay
->targets
[i
] );
497 VG_STATIC
void world_trigger_audio( u32 uid
)
499 struct world_audio_thing
*wat
= &world
.audio_things
[ uid
];
502 audio_player_playclip( &wat
->player
,
503 &wat
->temp_embedded_clip
);
507 VG_STATIC
void world_run_relay( struct relay_target
*rt
)
509 struct entity_instruction
511 enum classtype classtype
;
512 void (*p_trigger
)( u32 uid
);
514 entity_instructions
[] =
516 { k_classtype_logic_achievement
, world_trigger_achievement
},
517 { k_classtype_logic_relay
, world_trigger_relay
},
518 { k_classtype_audio
, world_trigger_audio
}
521 for( int i
=0; i
<vg_list_size(entity_instructions
); i
++ )
523 struct entity_instruction
*instr
= &entity_instructions
[i
];
525 if( instr
->classtype
== rt
->classtype
)
527 instr
->p_trigger( rt
->sub_id
);
532 vg_error( "Don't know how to trigger classtype %d\n", rt
->classtype
);
535 VG_STATIC
void world_update( v3f pos
)
537 if( world
.switching_to_new_world
)
542 for( int i
=0; i
<world
.audio_things_count
; i
++ )
544 struct world_audio_thing
*at
= &world
.audio_things
[i
];
546 if( audio_player_is_playing( &at
->player
) )
556 world
.switching_to_new_world
= 0;
563 world
.sky_time
+= world
.sky_rate
* vg
.time_delta
;
564 world
.sky_rate
= vg_lerp( world
.sky_rate
, world
.sky_target_rate
,
565 vg
.time_delta
* 5.0 );
567 world_routes_update();
569 world_routes_debug();
573 float min_dist
= INFINITY
;
575 for( int i
=0; i
<world
.route_count
; i
++ )
577 float d
= v3_dist2( world
.routes
[i
].scoreboard_transform
[3], pos
);
586 if( (world
.active_route_board
!= closest
) || network_scores_updated
)
588 network_scores_updated
= 0;
589 world
.active_route_board
= closest
;
591 struct route
*route
= &world
.routes
[closest
];
593 u32 id
= route
->track_id
;
595 if( id
!= 0xffffffff )
597 struct netmsg_board
*local_board
= &scoreboard_client_data
.boards
[id
];
599 for( int i
=0; i
<13; i
++ )
601 sfd_encode( i
, &local_board
->data
[27*i
] );
607 for( int i
=0; i
<world
.trigger_count
; i
++ )
609 struct trigger_zone
*zone
= &world
.triggers
[i
];
612 m4x3_mulv( zone
->inv_transform
, pos
, local
);
614 if( (fabsf(local
[0]) <= 1.0f
) &&
615 (fabsf(local
[1]) <= 1.0f
) &&
616 (fabsf(local
[2]) <= 1.0f
) )
620 if( !world
.in_trigger
)
622 world_run_relay( &zone
->target
);
626 vg_line_boxf_transformed( zone
->transform
, (boxf
){{-1.0f
,-1.0f
,-1.0f
},
627 { 1.0f
, 1.0f
, 1.0f
}},
631 world
.in_trigger
= in_trigger
;
636 * -----------------------------------------------------------------------------
638 * -----------------------------------------------------------------------------
641 VG_STATIC
void ray_world_get_tri( ray_hit
*hit
, v3f tri
[3] )
643 for( int i
=0; i
<3; i
++ )
644 v3_copy( world
.scene_geo
->arrvertices
[ hit
->tri
[i
] ].co
, tri
[i
] );
647 VG_STATIC
int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
)
649 return scene_raycast( world
.scene_geo
, world
.geo_bh
, pos
, dir
, hit
);
652 VG_STATIC
int ray_hit_is_terrain( ray_hit
*hit
)
657 valid_end
= world
.sm_terrain
.vertex_count
;
659 return (hit
->tri
[0] >= valid_start
) &&
660 (hit
->tri
[0] < valid_end
);
664 VG_STATIC
int ray_hit_is_ramp( ray_hit
*hit
)
668 u32 valid_start
= world
.sm_geo_std
.vertex_start
,
669 valid_end
= world
.sm_geo_vb
.vertex_start
;
671 return (hit
->tri
[0] >= valid_start
) &&
672 (hit
->tri
[0] < valid_end
);