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
;
114 /* This is a small flag we use to changelevel.
115 * It will not be cleared until all sounds stop playing
117 int switching_to_new_world
;
118 char world_name
[ 64 ];
121 * Dynamically allocated when world_load is called.
123 * the following arrays index somewhere into this linear
127 * --------------------------------------------------------------------------
137 * Named safe places to respawn
149 * Audio player entities
151 struct world_audio_thing
158 audio_clip temp_embedded_clip
;
161 u32 audio_things_count
;
164 * Relays, random, etc
169 enum logic_type logic_type
;
172 /* indexes the action array */
173 u32 action_start
, action_count
;
176 u32 logic_entity_count
;
183 u32 event
, /* on trigger, on enable, etc (TODO: Enum) */
184 target_id
; /* thing to target, 0: self */
188 u32 logic_action_count
;
191 * Box trigger entities
195 m4x3f transform
, inv_transform
;
203 * Routes (world_routes.h)
204 * --------------------------------------------------------------------------
208 v3f co
, right
, up
, h
;
211 u32 special_type
, special_id
, current_refs
, ref_count
;
212 u32 route_ids
[4]; /* Gates can be linked into up to four routes */
228 double best_lap
, latest_pass
; /* Session */
230 m4x3f scoreboard_transform
;
243 m4x3f to_world
, recv_to_world
, transport
;
251 u32 version
; /* Incremented on every teleport */
259 struct route_collector
261 struct route_timing timing
;
268 * ----------------------------------------------------
273 double time
, rewind_from
, rewind_to
, last_use
;
280 /* spacial mappings */
293 /* TODO Maybe make this less hardcoded...
295 mdl_submesh sm_geo_std_oob
, sm_geo_std
, sm_geo_vb
,
296 sm_foliage_main
, sm_foliage_alphatest
,
297 sm_graffiti
, sm_subworld
, sm_terrain
;
307 VG_STATIC
int ray_hit_is_ramp( ray_hit
*hit
);
308 VG_STATIC
int ray_hit_is_terrain( ray_hit
*hit
);
309 VG_STATIC
void ray_world_get_tri( ray_hit
*hit
, v3f tri
[3] );
310 VG_STATIC
int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
);
316 #include "world_routes.h"
317 #include "world_sfd.h"
318 #include "world_render.h"
319 #include "world_water.h"
320 #include "world_gen.h"
321 #include "world_gate.h"
324 * -----------------------------------------------------------------------------
326 * -----------------------------------------------------------------------------
329 VG_STATIC
int world_stop_sound( int argc
, const char *argv
[] )
332 * None of our world audio runs as one shots, they always have a player.
333 * Therefore it is safe to delete clip data after the players are
337 for( int i
=0; i
<world
.audio_things_count
; i
++ )
339 struct world_audio_thing
*at
= &world
.audio_things
[i
];
341 if( audio_player_is_playing( &at
->player
) )
343 u32 cflags
= audio_player_get_flags( &at
->player
);
344 audio_player_set_flags( &at
->player
, cflags
| AUDIO_FLAG_KILL
);
352 VG_STATIC
int world_change_world( int argc
, const char *argv
[] )
356 vg_info( "%s\n", world
.world_name
);
361 vg_info( "Switching world...\n" );
362 strcpy( world
.world_name
, argv
[0] );
363 world
.switching_to_new_world
= 1;
364 world_stop_sound( 0, NULL
);
370 VG_STATIC
void world_init(void)
372 vg_convar_push( (struct vg_convar
){
373 .name
= "water_enable",
374 .data
= &world
.water
.enabled
,
375 .data_type
= k_convar_dtype_i32
,
376 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
380 vg_function_push( (struct vg_cmd
)
382 .name
= "world_stop_sound",
383 .function
= world_stop_sound
386 vg_function_push( (struct vg_cmd
)
389 .function
= world_change_world
392 world
.sky_rate
= 1.0;
393 world
.sky_target_rate
= 1.0;
395 shader_terrain_register();
396 shader_sky_register();
397 shader_planeinf_register();
398 shader_gpos_register();
399 shader_fscolour_register();
400 shader_alphatest_register();
402 vg_info( "Loading world resources\n" );
404 vg_linear_clear( vg_mem
.scratch
);
405 mdl_context
*msky
= mdl_load_full( vg_mem
.scratch
, "models/rs_skydome.mdl" );
407 mdl_node
*nlower
= mdl_node_from_name( msky
, "dome_lower" ),
408 *nupper
= mdl_node_from_name( msky
, "dome_upper" );
410 world
.dome_lower
= *mdl_node_submesh( msky
, nlower
, 0 );
411 world
.dome_upper
= *mdl_node_submesh( msky
, nupper
, 0 );
413 vg_acquire_thread_sync();
415 mdl_unpack_glmesh( msky
, &world
.skydome
);
417 vg_release_thread_sync();
420 vg_info( "Loading other world systems\n" );
422 vg_loader_highwater( world_render_init
, NULL
, NULL
);
423 vg_loader_highwater( world_sfd_init
, NULL
, NULL
);
424 vg_loader_highwater( world_water_init
, NULL
, NULL
);
425 vg_loader_highwater( world_gates_init
, NULL
, NULL
);
426 vg_loader_highwater( world_routes_init
, NULL
, NULL
);
428 /* Allocate dynamic world memory arena */
429 u32 max_size
= 72*1024*1024;
430 world
.dynamic_vgl
= vg_create_linear_allocator( vg_mem
.rtmemory
, max_size
,
434 VG_STATIC
void world_update( v3f pos
)
436 if( world
.switching_to_new_world
)
441 for( int i
=0; i
<world
.audio_things_count
; i
++ )
443 struct world_audio_thing
*at
= &world
.audio_things
[i
];
445 if( audio_player_is_playing( &at
->player
) )
455 world
.switching_to_new_world
= 0;
462 world
.sky_time
+= world
.sky_rate
* vg
.time_delta
;
463 world
.sky_rate
= vg_lerp( world
.sky_rate
, world
.sky_target_rate
,
464 vg
.time_delta
* 5.0 );
466 world_routes_update();
468 world_routes_debug();
472 float min_dist
= INFINITY
;
474 for( int i
=0; i
<world
.route_count
; i
++ )
476 float d
= v3_dist2( world
.routes
[i
].scoreboard_transform
[3], pos
);
485 if( (world
.active_route_board
!= closest
) || network_scores_updated
)
487 network_scores_updated
= 0;
488 world
.active_route_board
= closest
;
490 struct route
*route
= &world
.routes
[closest
];
492 u32 id
= route
->track_id
;
494 if( id
!= 0xffffffff )
496 struct netmsg_board
*local_board
= &scoreboard_client_data
.boards
[id
];
498 for( int i
=0; i
<13; i
++ )
500 sfd_encode( i
, &local_board
->data
[27*i
] );
506 VG_STATIC
int in_zone
= 0;
508 int in_zone_this_time
= 0;
510 for( int i
=0; i
<world
.achievement_zones_count
; i
++ )
512 struct achievement_zone
*zone
= &world
.achievement_zones
[i
];
515 m4x3_mulv( zone
->inv_transform
, pos
, local
);
517 if( (fabsf(local
[0]) <= 1.0f
) &&
518 (fabsf(local
[1]) <= 1.0f
) &&
519 (fabsf(local
[2]) <= 1.0f
) )
521 in_zone_this_time
= 1;
523 if( !in_zone
&& zone
->ptarget
)
526 audio_player_playclip( &zone
->ptarget
->player
,
527 &zone
->ptarget
->temp_embedded_clip
);
531 if( !zone
->triggered
)
533 steam_set_achievement( zone
->name
);
534 steam_store_achievements();
540 vg_line_boxf_transformed( zone
->transform
, (boxf
){{-1.0f
,-1.0f
,-1.0f
},
541 { 1.0f
, 1.0f
, 1.0f
}},
545 in_zone
= in_zone_this_time
;
552 * -----------------------------------------------------------------------------
554 * -----------------------------------------------------------------------------
557 VG_STATIC
void ray_world_get_tri( ray_hit
*hit
, v3f tri
[3] )
559 for( int i
=0; i
<3; i
++ )
560 v3_copy( world
.scene_geo
->arrvertices
[ hit
->tri
[i
] ].co
, tri
[i
] );
563 VG_STATIC
int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
)
565 return scene_raycast( world
.scene_geo
, world
.geo_bh
, pos
, dir
, hit
);
568 VG_STATIC
int ray_hit_is_terrain( ray_hit
*hit
)
571 valid_end
= world
.sm_terrain
.vertex_count
;
573 return (hit
->tri
[0] >= valid_start
) &&
574 (hit
->tri
[0] < valid_end
);
577 VG_STATIC
int ray_hit_is_ramp( ray_hit
*hit
)
579 u32 valid_start
= world
.sm_geo_std
.vertex_start
,
580 valid_end
= world
.sm_geo_vb
.vertex_start
;
582 return (hit
->tri
[0] >= valid_start
) &&
583 (hit
->tri
[0] < valid_end
);