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 */
99 struct route_ui_segment
102 u32 vertex_start
, vertex_count
,
103 index_start
, index_count
, notches
;
105 segments
[k_max_ui_segments
];
107 u32 segment_start
, segment_count
, fade_start
, fade_count
;
108 double fade_timer_start
;
114 int active_route_board
;
117 * Dynamically allocated when world_load is called.
119 * the following arrays index somewhere into this linear
123 * --------------------------------------------------------------------------
133 * Named safe places to respawn
145 * Audio player entities
147 struct world_audio_thing
154 audio_clip temp_embedded_clip
;
157 u32 audio_things_count
;
160 * Relays, random, etc
165 enum logic_type logic_type
;
168 /* indexes the action array */
169 u32 action_start
, action_count
;
172 u32 logic_entity_count
;
179 u32 event
, /* on trigger, on enable, etc (TODO: Enum) */
180 target_id
; /* thing to target, 0: self */
184 u32 logic_action_count
;
187 * Box trigger entities
191 m4x3f transform
, inv_transform
;
199 * Routes (world_routes.h)
200 * --------------------------------------------------------------------------
204 v3f co
, right
, up
, h
;
207 u32 special_type
, special_id
, current_refs
, ref_count
;
208 u32 route_ids
[4]; /* Gates can be linked into up to four routes */
224 double best_lap
, latest_pass
; /* Session */
226 m4x3f scoreboard_transform
;
239 m4x3f to_world
, recv_to_world
, transport
;
247 u32 version
; /* Incremented on every teleport */
255 struct route_collector
257 struct route_timing timing
;
264 * ----------------------------------------------------
269 double time
, rewind_from
, rewind_to
, last_use
;
276 /* spacial mappings */
289 /* TODO Maybe make this less hardcoded */
290 mdl_submesh sm_geo_std_oob
, sm_geo_std
, sm_geo_vb
,
291 sm_foliage_main
, sm_foliage_alphatest
,
292 sm_graffiti
, sm_subworld
, sm_terrain
;
295 * Allocated AFTER all previous buffers are done
296 * --------------------------------------------------------------------------
299 struct instance_cache
305 u32 instance_cache_count
;
314 VG_STATIC
int ray_hit_is_ramp( ray_hit
*hit
);
315 VG_STATIC
int ray_hit_is_terrain( ray_hit
*hit
);
316 VG_STATIC
void ray_world_get_tri( ray_hit
*hit
, v3f tri
[3] );
317 VG_STATIC
int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
);
323 #include "world_routes.h"
324 #include "world_sfd.h"
325 #include "world_render.h"
326 #include "world_water.h"
327 #include "world_gen.h"
328 #include "world_gate.h"
331 * -----------------------------------------------------------------------------
333 * -----------------------------------------------------------------------------
336 VG_STATIC
void world_init(void)
338 vg_convar_push( (struct vg_convar
){
339 .name
= "water_enable",
340 .data
= &world
.water
.enabled
,
341 .data_type
= k_convar_dtype_i32
,
342 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
346 world
.sky_rate
= 1.0;
347 world
.sky_target_rate
= 1.0;
349 shader_terrain_register();
350 shader_sky_register();
351 shader_planeinf_register();
352 shader_gpos_register();
353 shader_fscolour_register();
354 shader_alphatest_register();
356 vg_info( "Loading world resources\n" );
358 vg_linear_clear( vg_mem
.scratch
);
359 mdl_context
*msky
= mdl_load_full( vg_mem
.scratch
, "models/rs_skydome.mdl" );
361 mdl_node
*nlower
= mdl_node_from_name( msky
, "dome_lower" ),
362 *nupper
= mdl_node_from_name( msky
, "dome_upper" );
364 world
.dome_lower
= *mdl_node_submesh( msky
, nlower
, 0 );
365 world
.dome_upper
= *mdl_node_submesh( msky
, nupper
, 0 );
367 vg_acquire_thread_sync();
369 mdl_unpack_glmesh( msky
, &world
.skydome
);
371 vg_release_thread_sync();
374 vg_info( "Loading other world systems\n" );
376 vg_loader_highwater( world_render_init
, NULL
, NULL
);
377 vg_loader_highwater( world_sfd_init
, NULL
, NULL
);
378 vg_loader_highwater( world_water_init
, NULL
, NULL
);
379 vg_loader_highwater( world_gates_init
, NULL
, NULL
);
380 vg_loader_highwater( world_routes_init
, NULL
, NULL
);
382 /* Allocate dynamic world memory arena */
383 u32 max_size
= 72*1024*1024;
384 world
.dynamic_vgl
= vg_create_linear_allocator( vg_mem
.rtmemory
, max_size
);
387 VG_STATIC
void world_update( v3f pos
)
389 world
.sky_time
+= world
.sky_rate
* vg
.time_delta
;
390 world
.sky_rate
= vg_lerp( world
.sky_rate
, world
.sky_target_rate
,
391 vg
.time_delta
* 5.0 );
393 world_routes_update();
395 world_routes_debug();
399 float min_dist
= INFINITY
;
401 for( int i
=0; i
<world
.route_count
; i
++ )
403 float d
= v3_dist2( world
.routes
[i
].scoreboard_transform
[3], pos
);
412 if( (world
.active_route_board
!= closest
) || network_scores_updated
)
414 network_scores_updated
= 0;
415 world
.active_route_board
= closest
;
417 struct route
*route
= &world
.routes
[closest
];
419 u32 id
= route
->track_id
;
421 if( id
!= 0xffffffff )
423 struct netmsg_board
*local_board
= &scoreboard_client_data
.boards
[id
];
425 for( int i
=0; i
<13; i
++ )
427 sfd_encode( i
, &local_board
->data
[27*i
] );
433 VG_STATIC
int in_zone
= 0;
435 int in_zone_this_time
= 0;
437 for( int i
=0; i
<world
.achievement_zones_count
; i
++ )
439 struct achievement_zone
*zone
= &world
.achievement_zones
[i
];
442 m4x3_mulv( zone
->inv_transform
, pos
, local
);
444 if( (fabsf(local
[0]) <= 1.0f
) &&
445 (fabsf(local
[1]) <= 1.0f
) &&
446 (fabsf(local
[2]) <= 1.0f
) )
448 in_zone_this_time
= 1;
450 if( !in_zone
&& zone
->ptarget
)
453 audio_player_playclip( &zone
->ptarget
->player
,
454 &zone
->ptarget
->temp_embedded_clip
);
458 if( !zone
->triggered
)
460 steam_set_achievement( zone
->name
);
461 steam_store_achievements();
467 vg_line_boxf_transformed( zone
->transform
, (boxf
){{-1.0f
,-1.0f
,-1.0f
},
468 { 1.0f
, 1.0f
, 1.0f
}},
472 in_zone
= in_zone_this_time
;
479 * -----------------------------------------------------------------------------
481 * -----------------------------------------------------------------------------
484 VG_STATIC
void ray_world_get_tri( ray_hit
*hit
, v3f tri
[3] )
486 for( int i
=0; i
<3; i
++ )
487 v3_copy( world
.scene_geo
->arrvertices
[ hit
->tri
[i
] ].co
, tri
[i
] );
490 VG_STATIC
int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
)
492 return scene_raycast( world
.scene_geo
, world
.geo_bh
, pos
, dir
, hit
);
495 VG_STATIC
int ray_hit_is_terrain( ray_hit
*hit
)
498 valid_end
= world
.sm_terrain
.vertex_count
;
500 return (hit
->tri
[0] >= valid_start
) &&
501 (hit
->tri
[0] < valid_end
);
504 VG_STATIC
int ray_hit_is_ramp( ray_hit
*hit
)
506 u32 valid_start
= world
.sm_geo_std
.vertex_start
,
507 valid_end
= world
.sm_geo_vb
.vertex_start
;
509 return (hit
->tri
[0] >= valid_start
) &&
510 (hit
->tri
[0] < valid_end
);