fuckin hell
[carveJwlIkooP6JGAAIwe30JlM.git] / world.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #include "common.h"
6
7 VG_STATIC int ray_world( v3f pos, v3f dir, ray_hit *hit );
8
9 #ifndef WORLD_H
10 #define WORLD_H
11
12 #include "vg/vg_loader.h"
13
14 #include "network.h"
15 #include "network_msg.h"
16 #include "scene.h"
17 #include "render.h"
18 #include "rigidbody.h"
19 #include "bvh.h"
20 #include "model.h"
21
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"
27
28 #include "shaders/model_sky.h"
29
30 typedef struct teleport_gate teleport_gate;
31
32 enum { k_max_ui_segments = 8 };
33
34 enum { k_max_ui_elements = k_max_ui_segments };
35 enum { k_max_element_verts = 10 };
36 enum { k_max_element_indices = 20 };
37
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 };
40
41 enum logic_type
42 {
43 k_logic_type_relay = 1,
44 k_logic_type_chance = 2,
45 k_logic_type_achievement = 3
46 };
47
48 enum geo_type
49 {
50 k_geo_type_solid = 0,
51 k_geo_type_nonsolid = 1,
52 k_geo_type_water = 2
53 };
54
55 VG_STATIC struct gworld
56 {
57 /*
58 * Allocated as system memory
59 * --------------------------------------------------------------------------
60 */
61
62 /* rendering */
63 glmesh skydome;
64 mdl_submesh dome_upper, dome_lower;
65
66 glmesh mesh_gate_surface;
67
68 double sky_time, sky_rate, sky_target_rate;
69
70 /* water rendering */
71 struct
72 {
73 struct framebuffer fbreflect, fbdepth;
74
75 boxf depthbounds;
76 int depth_computed;
77
78 float height;
79 int enabled;
80 v4f plane;
81 }
82 water;
83
84 /* split flap display */
85 struct
86 {
87 mdl_submesh *sm_module, *sm_card;
88 glmesh mesh_base, mesh_display;
89
90 u32 w, h;
91 float *buffer;
92 }
93 sfd;
94
95 /* timing bars, fixed maximum amount */
96 struct route_ui_bar
97 {
98 GLuint vao, vbo, ebo;
99
100 u32 indices_head;
101 u32 vertex_head;
102
103 struct route_ui_segment
104 {
105 float length;
106 u32 vertex_start, vertex_count,
107 index_start, index_count, notches;
108 }
109 segments[k_max_ui_segments];
110
111 u32 segment_start, segment_count, fade_start, fade_count;
112 double fade_timer_start;
113 float xpos;
114 }
115 ui_bars[16];
116
117 v3f render_gate_pos;
118 int active_route_board;
119 int in_trigger;
120
121 /* This is a small flag we use to changelevel.
122 * It will not be cleared until all sounds stop playing
123 */
124 int switching_to_new_world;
125 char world_name[ 64 ];
126
127 /*
128 * Dynamically allocated when world_load is called.
129 *
130 * the following arrays index somewhere into this linear
131 * allocator
132 *
133 * (world_gen.h)
134 * --------------------------------------------------------------------------
135 */
136 void *dynamic_vgl,
137 *audio_vgl; /* sub buffer of the audio buffer */
138
139 /*
140 * Main world .mdl
141 */
142 mdl_context *meta;
143
144 /*
145 * Materials / textures
146 */
147
148 GLuint *textures;
149 u32 texture_count;
150
151 struct world_material
152 {
153 mdl_material info;
154 mdl_submesh sm_geo,
155 sm_no_collide;
156 }
157 * materials;
158 u32 material_count;
159
160 /*
161 * Named safe places to respawn
162 */
163 struct respawn_point
164 {
165 v3f co;
166 v4f q;
167 const char *name;
168 }
169 * spawns;
170 u32 spawn_count;
171
172 /*
173 * Audio player entities
174 */
175 struct world_audio_thing
176 {
177 v3f pos;
178 float volume;
179 u32 flags;
180
181 audio_player player;
182 audio_clip temp_embedded_clip;
183 }
184 * audio_things;
185 u32 audio_things_count;
186
187 /*
188 * Relays
189 */
190 struct logic_relay
191 {
192 v3f pos;
193
194 struct relay_target
195 {
196 u32 sub_id;
197 enum classtype classtype;
198 }
199 targets[4];
200 u32 target_count;
201 }
202 * logic_relays;
203 u32 relay_count;
204
205 /*
206 * Box trigger entities
207 */
208 struct trigger_zone
209 {
210 m4x3f transform, inv_transform;
211
212 struct relay_target target;
213 }
214 * triggers;
215 u32 trigger_count;
216
217 /*
218 * Achievements
219 */
220 struct logic_achievement
221 {
222 v3f pos;
223 const char *achievement_id;
224 u32 achieved;
225 }
226 * logic_achievements;
227 u32 achievement_count;
228
229 /*
230 * Lights
231 */
232 struct world_light
233 {
234 v3f co;
235 v4f colour;
236 }
237 * lights;
238 u32 light_count;
239
240 /*
241 * Routes (world_routes.h)
242 * --------------------------------------------------------------------------
243 */
244 struct route_node
245 {
246 v3f co, right, up, h;
247 u32 next[2];
248
249 u32 special_type, special_id, current_refs, ref_count;
250 u32 route_ids[4]; /* Gates can be linked into up to four routes */
251 }
252 *nodes;
253 u32 node_count;
254
255 struct route
256 {
257 u32 track_id;
258 v4f colour;
259
260 u32 start;
261 mdl_submesh sm;
262
263 int active;
264 float factive;
265
266 double best_lap, latest_pass; /* Session */
267
268 m4x3f scoreboard_transform;
269 }
270 *routes;
271 u32 route_count;
272
273 struct route_gate
274 {
275 struct teleport_gate
276 {
277 v3f co[2];
278 v4f q[2];
279 v2f dims;
280
281 m4x3f to_world, recv_to_world, transport;
282 }
283 gate;
284
285 u32 node_id;
286
287 struct route_timing
288 {
289 u32 version; /* Incremented on every teleport */
290 double time;
291 }
292 timing;
293 }
294 *gates;
295 u32 gate_count;
296
297 struct route_collector
298 {
299 struct route_timing timing;
300 }
301 *collectors;
302 u32 collector_count;
303
304
305 /* logic
306 * ----------------------------------------------------
307 */
308
309 u32 active_gate,
310 current_run_version;
311 double time, rewind_from, rewind_to, last_use;
312
313 /* world geometry */
314 scene *scene_geo,
315 *scene_no_collide,
316 *scene_lines;
317
318 /* spacial mappings */
319 bh_tree *audio_bh,
320 *trigger_bh,
321 *geo_bh;
322
323 /* graphics */
324 glmesh mesh_route_lines;
325
326 glmesh mesh_geo,
327 mesh_no_collide,
328 mesh_water;
329
330 mdl_submesh sm_foliage_main;
331 rigidbody rb_geo; /* todo.. ... */
332 }
333 world;
334
335
336 /*
337 * API
338 */
339
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 );
344
345 /*
346 * Submodules
347 */
348
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"
355
356 /*
357 * -----------------------------------------------------------------------------
358 * Events
359 * -----------------------------------------------------------------------------
360 */
361
362 VG_STATIC int world_stop_sound( int argc, const char *argv[] )
363 {
364 /*
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
367 * disconnected
368 */
369 audio_lock();
370 for( int i=0; i<world.audio_things_count; i++ )
371 {
372 struct world_audio_thing *at = &world.audio_things[i];
373
374 if( audio_player_is_playing( &at->player ) )
375 {
376 u32 cflags = audio_player_get_flags( &at->player );
377 audio_player_set_flags( &at->player, cflags | AUDIO_FLAG_KILL );
378 }
379 }
380 audio_unlock();
381
382 return 0;
383 }
384
385 VG_STATIC int world_change_world( int argc, const char *argv[] )
386 {
387 if( argc == 0 )
388 {
389 vg_info( "%s\n", world.world_name );
390 return 0;
391 }
392 else
393 {
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 );
398 }
399
400 return 0;
401 }
402
403 VG_STATIC void world_init(void)
404 {
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 },
410 .persistent = 0
411 });
412
413 vg_function_push( (struct vg_cmd)
414 {
415 .name = "world_stop_sound",
416 .function = world_stop_sound
417 });
418
419 vg_function_push( (struct vg_cmd)
420 {
421 .name = "world",
422 .function = world_change_world
423 });
424
425 world.sky_rate = 1.0;
426 world.sky_target_rate = 1.0;
427
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();
433
434 shader_model_sky_register();
435
436 vg_info( "Loading world resources\n" );
437
438 vg_linear_clear( vg_mem.scratch );
439 mdl_context *msky = mdl_load_full( vg_mem.scratch, "models/rs_skydome.mdl" );
440
441 mdl_node *nupper = mdl_node_from_name( msky, "dome_complete" );
442 world.dome_upper = *mdl_node_submesh( msky, nupper, 0 );
443
444 vg_acquire_thread_sync();
445 {
446 mdl_unpack_glmesh( msky, &world.skydome );
447 }
448 vg_release_thread_sync();
449
450 /* Other systems */
451 vg_info( "Loading other world systems\n" );
452
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 );
458
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,
462 VG_MEMORY_SYSTEM );
463 }
464
465 VG_STATIC void world_audio_init(void)
466 {
467 u32 size = vg_linear_remaining( vg_audio.audio_pool )
468 - sizeof(vg_linear_allocator);
469
470 world.audio_vgl = vg_create_linear_allocator( vg_audio.audio_pool,
471 size, VG_MEMORY_SYSTEM );
472 }
473
474 VG_STATIC void world_trigger_achievement( u32 uid )
475 {
476 struct logic_achievement *ach = &world.logic_achievements[ uid ];
477
478 if( ach->achieved )
479 return;
480
481 steam_set_achievement( ach->achievement_id );
482 steam_store_achievements();
483
484 ach->achieved = 1;
485 }
486
487 VG_STATIC void world_run_relay( struct relay_target *rt );
488 VG_STATIC void world_trigger_relay( u32 uid )
489 {
490 struct logic_relay *relay = &world.logic_relays[ uid ];
491
492 for( int i=0; i<relay->target_count; i++ )
493 {
494 world_run_relay( &relay->targets[i] );
495 }
496 }
497
498 VG_STATIC void world_trigger_audio( u32 uid )
499 {
500 struct world_audio_thing *wat = &world.audio_things[ uid ];
501
502 audio_lock();
503 audio_player_playclip( &wat->player,
504 &wat->temp_embedded_clip );
505 audio_unlock();
506 }
507
508 VG_STATIC void world_run_relay( struct relay_target *rt )
509 {
510 struct entity_instruction
511 {
512 enum classtype classtype;
513 void (*p_trigger)( u32 uid );
514 }
515 entity_instructions[] =
516 {
517 { k_classtype_logic_achievement, world_trigger_achievement },
518 { k_classtype_logic_relay, world_trigger_relay },
519 { k_classtype_audio, world_trigger_audio }
520 };
521
522 for( int i=0; i<vg_list_size(entity_instructions); i++ )
523 {
524 struct entity_instruction *instr = &entity_instructions[i];
525
526 if( instr->classtype == rt->classtype )
527 {
528 instr->p_trigger( rt->sub_id );
529 return;
530 }
531 }
532
533 vg_error( "Don't know how to trigger classtype %d\n", rt->classtype );
534 }
535
536 VG_STATIC void world_update( v3f pos )
537 {
538 if( world.switching_to_new_world )
539 {
540 int all_stopped = 1;
541
542 audio_lock();
543 for( int i=0; i<world.audio_things_count; i++ )
544 {
545 struct world_audio_thing *at = &world.audio_things[i];
546
547 if( audio_player_is_playing( &at->player ) )
548 {
549 all_stopped = 0;
550 break;
551 }
552 }
553 audio_unlock();
554
555 if( all_stopped )
556 {
557 world.switching_to_new_world = 0;
558 world_unload();
559 vg_loader_start( world_load );
560 return;
561 }
562 }
563
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 );
567
568 world_routes_update();
569 #if 0
570 world_routes_debug();
571 #endif
572
573 if( world.route_count > 0 )
574 {
575 int closest = 0;
576 float min_dist = INFINITY;
577
578 for( int i=0; i<world.route_count; i++ )
579 {
580 float d = v3_dist2( world.routes[i].scoreboard_transform[3], pos );
581
582 if( d < min_dist )
583 {
584 min_dist = d;
585 closest = i;
586 }
587 }
588
589 if( (world.active_route_board != closest) || network_scores_updated )
590 {
591 network_scores_updated = 0;
592 world.active_route_board = closest;
593
594 struct route *route = &world.routes[closest];
595
596 u32 id = route->track_id;
597
598 if( id != 0xffffffff )
599 {
600 struct netmsg_board *local_board =
601 &scoreboard_client_data.boards[id];
602
603 for( int i=0; i<13; i++ )
604 {
605 sfd_encode( i, &local_board->data[27*i] );
606 }
607 }
608 }
609 }
610
611 int in_trigger = 0;
612 for( int i=0; i<world.trigger_count; i++ )
613 {
614 struct trigger_zone *zone = &world.triggers[i];
615
616 v3f local;
617 m4x3_mulv( zone->inv_transform, pos, local );
618
619 if( (fabsf(local[0]) <= 1.0f) &&
620 (fabsf(local[1]) <= 1.0f) &&
621 (fabsf(local[2]) <= 1.0f) )
622 {
623 in_trigger = 1;
624
625 if( !world.in_trigger )
626 {
627 world_run_relay( &zone->target );
628 }
629 }
630
631 vg_line_boxf_transformed( zone->transform, (boxf){{-1.0f,-1.0f,-1.0f},
632 { 1.0f, 1.0f, 1.0f}},
633 0xff00ff00 );
634 }
635
636 for( int i=0; i<world.light_count; i++ )
637 {
638 struct world_light *light = &world.lights[i];
639
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;
644
645 colour |= r;
646 colour |= g << 8;
647 colour |= b << 16;
648
649 vg_line_pt3( light->co, 0.25f, colour );
650 }
651
652 world.in_trigger = in_trigger;
653 sfd_update();
654 }
655
656 /*
657 * -----------------------------------------------------------------------------
658 * API implementation
659 * -----------------------------------------------------------------------------
660 */
661
662 VG_STATIC void ray_world_get_tri( ray_hit *hit, v3f tri[3] )
663 {
664 for( int i=0; i<3; i++ )
665 v3_copy( world.scene_geo->arrvertices[ hit->tri[i] ].co, tri[i] );
666 }
667
668 VG_STATIC int ray_world( v3f pos, v3f dir, ray_hit *hit )
669 {
670 return scene_raycast( world.scene_geo, world.geo_bh, pos, dir, hit );
671 }
672
673 /*
674 * Cast a sphere from a to b and see what time it hits
675 */
676 VG_STATIC int spherecast_world( v3f pa, v3f pb, float r, float *t, v3f n )
677 {
678 bh_iter it;
679 bh_iter_init( 0, &it );
680
681 boxf region;
682 box_init_inf( region );
683 box_addpt( region, pa );
684 box_addpt( region, pb );
685
686 v3_add( (v3f){ r, r, r}, region[1], region[1] );
687 v3_add( (v3f){-r,-r,-r}, region[0], region[0] );
688
689 v3f dir;
690 v3_sub( pb, pa, dir );
691
692 v3f dir_inv;
693 dir_inv[0] = 1.0f/dir[0];
694 dir_inv[1] = 1.0f/dir[1];
695 dir_inv[2] = 1.0f/dir[2];
696
697 int hit = -1;
698 float min_t = 1.0f;
699
700 int idx;
701 while( bh_next( world.geo_bh, &it, region, &idx ) )
702 {
703 u32 *ptri = &world.scene_geo->arrindices[ idx*3 ];
704 v3f tri[3];
705
706 boxf box;
707 box_init_inf( box );
708
709 for( int j=0; j<3; j++ )
710 {
711 v3_copy( world.scene_geo->arrvertices[ptri[j]].co, tri[j] );
712 box_addpt( box, tri[j] );
713 }
714
715 v3_add( (v3f){ r, r, r}, box[1], box[1] );
716 v3_add( (v3f){-r,-r,-r}, box[0], box[0] );
717
718 if( !ray_aabb1( box, pa, dir_inv, 1.0f ) )
719 continue;
720
721 float t;
722 v3f n1;
723 if( spherecast_triangle( tri, pa, dir, r, &t, n1 ) )
724 {
725 if( t < min_t )
726 {
727 min_t = t;
728 hit = idx;
729 v3_copy( n1, n );
730 }
731 }
732 }
733
734 *t = min_t;
735 return hit;
736 }
737
738 VG_STATIC struct world_material *world_tri_index_material( u32 index )
739 {
740 for( int i=1; i<world.material_count; i++ )
741 {
742 struct world_material *mat = &world.materials[i];
743
744 if( (index >= mat->sm_geo.vertex_start) &&
745 (index < mat->sm_geo.vertex_start+mat->sm_geo.vertex_count ) )
746 {
747 return mat;
748 }
749 }
750
751 /* error material */
752 return &world.materials[0];
753 }
754
755 VG_STATIC struct world_material *world_contact_material( rb_ct *ct )
756 {
757 return world_tri_index_material( ct->element_id );
758 }
759
760 VG_STATIC struct world_material *ray_hit_material( ray_hit *hit )
761 {
762 return world_tri_index_material( hit->tri[0] );
763 }
764
765 #endif /* WORLD_H */