chaos caused by async
[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 #ifndef WORLD_H
8 #define WORLD_H
9
10 typedef struct world_instance world_instance;
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 #include "entity.h"
22 #include "font.h"
23
24 #include "shaders/scene_standard.h"
25 #include "shaders/scene_standard_alphatest.h"
26 #include "shaders/scene_vertex_blend.h"
27 #include "shaders/scene_terrain.h"
28 #include "shaders/scene_depth.h"
29 #include "shaders/scene_position.h"
30
31 #include "shaders/model_sky.h"
32
33 enum { k_max_ui_segments = 8 };
34
35 enum { k_max_ui_elements = k_max_ui_segments };
36 enum { k_max_element_verts = 10 };
37 enum { k_max_element_indices = 20 };
38
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 };
41
42 enum logic_type
43 {
44 k_logic_type_relay = 1,
45 k_logic_type_chance = 2,
46 k_logic_type_achievement = 3
47 };
48
49 enum geo_type
50 {
51 k_geo_type_solid = 0,
52 k_geo_type_nonsolid = 1,
53 k_geo_type_water = 2
54 };
55
56 static const float k_light_cube_size = 8.0f;
57
58 struct world_instance {
59 /* Fixed items
60 * -------------------------------------------------------
61 */
62
63 void *heap;
64 char world_name[ 64 ];
65 enum world_status{
66 k_world_status_unloaded = 0,
67 k_world_status_loading = 1,
68 k_world_status_loaded = 2
69 }
70 status;
71
72 struct{
73 boxf depthbounds;
74 int depth_computed;
75
76 float height;
77 int enabled;
78 v4f plane;
79 }
80 water;
81
82 /* STD140 */
83 struct ub_world_lighting{
84 v4f g_cube_min,
85 g_cube_inv_range;
86
87 v4f g_water_plane,
88 g_depth_bounds;
89
90 v4f g_daysky_colour;
91 v4f g_nightsky_colour;
92 v4f g_sunset_colour;
93 v4f g_ambient_colour;
94 v4f g_sunset_ambient;
95 v4f g_sun_colour;
96 v4f g_sun_dir;
97 v4f g_board_0;
98 v4f g_board_1;
99
100 float g_water_fog;
101 float g_time;
102 float g_realtime;
103 float g_shadow_length;
104 float g_shadow_spread;
105
106 float g_time_of_day;
107 float g_day_phase;
108 float g_sunset_phase;
109
110 int g_light_preview;
111 int g_shadow_samples;
112
113 int g_debug_indices;
114 int g_debug_complexity;
115 }
116 ub_lighting;
117 GLuint ubo_lighting;
118 int ubo_bind_point;
119
120 GLuint tbo_light_entities,
121 tex_light_entities,
122 tex_light_cubes;
123
124 float probabilities[3];
125
126 v3i light_cubes;
127
128 struct framebuffer heightmap;
129
130 /*
131 * Dynamically allocated when world_load is called.
132 *
133 * the following arrays index somewhere into this linear
134 * allocator
135 *
136 * (world_gen.h)
137 * --------------------------------------------------------------------------
138 */
139
140 /*
141 * Main world .mdl
142 */
143 mdl_context meta;
144
145 GLuint *textures;
146 u32 texture_count;
147
148 struct world_surface{
149 mdl_material info;
150 mdl_submesh sm_geo,
151 sm_no_collide;
152 }
153 * surfaces;
154 u32 surface_count;
155
156 mdl_array_ptr ent_spawn,
157 ent_gate,
158 ent_light,
159 ent_route_node,
160 ent_path_index,
161 ent_checkpoint,
162 ent_route,
163 ent_water,
164
165 ent_audio_clip,
166 ent_audio,
167 ent_volume,
168 ent_traffic;
169
170 ent_gate *rendering_gate;
171
172 /* logic
173 * ----------------------------------------------------
174 */
175
176 /* world geometry */
177 scene_context scene_geo,
178 scene_no_collide,
179 scene_lines;
180
181 /* spacial mappings */
182 bh_tree *audio_bh,
183 *volume_bh,
184 *geo_bh;
185
186 /* graphics */
187 glmesh mesh_route_lines;
188 glmesh mesh_geo,
189 mesh_no_collide,
190 mesh_water;
191
192 rb_object rb_geo;
193 };
194
195 struct world_global{
196 /*
197 * Allocated as system memory
198 * --------------------------------------------------------------------------
199 */
200 void *heap;
201
202 /* rendering */
203 glmesh skydome;
204 glmesh mesh_gate;
205 mdl_submesh sm_gate_surface,
206 sm_gate_marker[4];
207
208 double sky_time, sky_rate, sky_target_rate;
209
210 u32 current_run_version;
211 double time, rewind_from, rewind_to, last_use;
212
213 /* water rendering */
214 struct{
215 struct framebuffer fbreflect, fbdepth;
216 }
217 water;
218
219 /* split flap display */
220 struct{
221 glmesh mesh_base, mesh_display;
222 mdl_submesh sm_base;
223 u32 active_route_board;
224 scene_context scene;
225
226 u32 w, h;
227 float *buffer;
228 }
229 sfd;
230
231 v3f render_gate_pos;
232 int in_volume;
233
234 int switching_to_new_world;
235
236 world_instance worlds[4];
237 u32 world_count;
238 u32 active_world;
239
240 /* text particles */
241 font3d font;
242
243 struct timer_text{
244 char text[8];
245 m4x3f transform;
246 ent_gate *gate;
247 ent_route *route;
248 }
249 timer_texts[4];
250 u32 timer_text_count;
251
252 struct text_particle{
253 rb_object obj;
254 m4x3f mlocal;
255 ent_glyph *glyph;
256 v4f colour;
257
258 m4x3f mdl;
259 }
260 text_particles[6*4];
261 u32 text_particle_count;
262 }
263 static world_global;
264
265 VG_STATIC world_instance *get_active_world( void )
266 {
267 return &world_global.worlds[ world_global.active_world ];
268 }
269
270 /*
271 * API
272 */
273
274 VG_STATIC
275 int ray_hit_is_ramp( world_instance *world, ray_hit *hit );
276
277 VG_STATIC
278 struct world_surface *ray_hit_surface( world_instance *world, ray_hit *hit );
279
280 VG_STATIC
281 void ray_world_get_tri( world_instance *world, ray_hit *hit, v3f tri[3] );
282
283 VG_STATIC
284 int ray_world( world_instance *world, v3f pos, v3f dir, ray_hit *hit );
285
286 VG_STATIC
287 ent_spawn *world_find_closest_spawn( world_instance *world, v3f position )
288 {
289 ent_spawn *rp = NULL, *r;
290 float min_dist = INFINITY;
291
292 for( u32 i=0; i<mdl_arrcount(&world->ent_spawn); i++ ){
293 r = mdl_arritm( &world->ent_spawn, i );
294 float d = v3_dist2( r->transform.co, position );
295
296 if( d < min_dist ){
297 min_dist = d;
298 rp = r;
299 }
300 }
301
302 if( !rp ){
303 if( mdl_arrcount(&world->ent_spawn) ){
304 vg_warn( "Invalid distances to spawns.. defaulting to first one.\n" );
305 return mdl_arritm( &world->ent_spawn, 0 );
306 }
307 else{
308 vg_error( "There are no spawns in the level!\n" );
309 }
310 }
311
312 return rp;
313 }
314
315 VG_STATIC
316 ent_spawn *world_find_spawn_by_name( world_instance *world, const char *name )
317 {
318 ent_spawn *rp = NULL, *r;
319 for( u32 i=0; i<mdl_arrcount(&world->ent_spawn); i++ ){
320 r = mdl_arritm( &world->ent_spawn, i );
321 if( !strcmp( mdl_pstr(&world->meta, r->pstr_name), name ) ){
322 rp = r;
323 break;
324 }
325 }
326
327 if( !rp )
328 vg_warn( "No spawn named '%s'\n", name );
329
330 return rp;
331 }
332
333 /*
334 * Submodules
335 */
336
337 VG_STATIC float
338 k_day_length = 30.0f; /* minutes */
339
340 VG_STATIC int k_debug_light_indices = 0,
341 k_debug_light_complexity = 0,
342 k_light_preview = 0;
343
344 #include "world_routes.h"
345 #include "world_sfd.h"
346 #include "world_render.h"
347 #include "world_water.h"
348 #include "world_volumes.h"
349 #include "world_gen.h"
350 #include "world_gate.h"
351
352 /*
353 * -----------------------------------------------------------------------------
354 * Events
355 * -----------------------------------------------------------------------------
356 */
357
358 VG_STATIC int world_stop_sound( int argc, const char *argv[] )
359 {
360 world_instance *world = get_active_world();
361 return 0;
362 }
363
364 VG_STATIC void world_init(void)
365 {
366 VG_VAR_F32( k_day_length );
367 VG_VAR_I32( k_debug_light_indices );
368 VG_VAR_I32( k_debug_light_complexity );
369 VG_VAR_I32( k_light_preview );
370
371 world_global.sky_rate = 1.0;
372 world_global.sky_target_rate = 1.0;
373
374 shader_scene_standard_register();
375 shader_scene_standard_alphatest_register();
376 shader_scene_vertex_blend_register();
377 shader_scene_terrain_register();
378 shader_scene_depth_register();
379 shader_scene_position_register();
380
381 shader_model_sky_register();
382
383 vg_info( "Loading world resources\n" );
384
385 vg_linear_clear( vg_mem.scratch );
386
387 mdl_context msky;
388 mdl_open( &msky, "models/rs_skydome.mdl", vg_mem.scratch );
389 mdl_load_metadata_block( &msky, vg_mem.scratch );
390 mdl_async_load_glmesh( &msky, &world_global.skydome );
391 mdl_close( &msky );
392
393 /* Other systems */
394 vg_info( "Loading other world systems\n" );
395
396 vg_loader_step( world_render_init, NULL );
397 vg_loader_step( world_sfd_init, NULL );
398 vg_loader_step( world_water_init, NULL );
399 vg_loader_step( world_gates_init, NULL );
400 vg_loader_step( world_routes_init, NULL );
401
402 /* Allocate dynamic world memory arena */
403 u32 max_size = 76*1024*1024;
404 world_global.heap = vg_create_linear_allocator( vg_mem.rtmemory, max_size,
405 VG_MEMORY_SYSTEM );
406 }
407
408 typedef struct ent_call ent_call;
409 struct ent_call{
410 ent_index ent;
411 u32 function;
412 void *data;
413 };
414
415 VG_STATIC void entity_call( world_instance *world, ent_call *call );
416
417 VG_STATIC void ent_volume_call( world_instance *world, ent_call *call )
418 {
419 ent_volume *volume = mdl_arritm( &world->ent_volume, call->ent.index );
420 if( !volume->target.type ) return;
421
422 if( call->function == k_ent_function_trigger ){
423 call->ent = volume->target;
424
425 if( volume->type == k_volume_subtype_particle ){
426 float *co = alloca( sizeof(float)*3 );
427 co[0] = vg_randf()*2.0f-1.0f;
428 co[1] = vg_randf()*2.0f-1.0f;
429 co[2] = vg_randf()*2.0f-1.0f;
430 m4x3_mulv( volume->to_world, co, co );
431
432 call->function = k_ent_function_particle_spawn;
433 call->data = co;
434 entity_call( world, call );
435 }
436 else
437 entity_call( world, call );
438 }
439 }
440
441 VG_STATIC void ent_audio_call( world_instance *world, ent_call *call )
442 {
443 ent_audio *audio = mdl_arritm( &world->ent_audio, call->ent.index );
444
445 v3f sound_co;
446
447 if( call->function == k_ent_function_particle_spawn ){
448 v3_copy( call->data, sound_co );
449 }
450 else if( call->function == k_ent_function_trigger ){
451 v3_copy( audio->transform.co, sound_co );
452 }
453 else
454 vg_fatal_error( "ent_audio_call (invalid function id)" );
455
456 float chance = vg_randf()*100.0f,
457 bar = 0.0f;
458
459 for( u32 i=0; i<audio->clip_count; i++ ){
460 ent_audio_clip *clip = mdl_arritm( &world->ent_audio_clip,
461 audio->clip_start+i );
462
463 float mod = world->probabilities[ audio->probability_curve ],
464 p = clip->probability * mod;
465
466 bar += p;
467
468 if( chance < bar ){
469
470 audio_lock();
471
472 if( audio->behaviour == k_channel_behaviour_unlimited ){
473 audio_oneshot_3d( &clip->clip, sound_co,
474 audio->transform.s[0],
475 audio->volume );
476 }
477 else if( audio->behaviour == k_channel_behaviour_discard_if_full ){
478 audio_channel *ch =
479 audio_get_group_idle_channel( audio->group,
480 audio->max_channels );
481
482 if( ch ){
483 audio_channel_init( ch, &clip->clip, audio->flags );
484 audio_channel_group( ch, audio->group );
485 audio_channel_set_spacial( ch, sound_co, audio->transform.s[0] );
486 audio_channel_edit_volume( ch, audio->volume, 1 );
487 ch = audio_relinquish_channel( ch );
488 }
489 }
490 else if( audio->behaviour == k_channel_behaviour_crossfade_if_full){
491 audio_channel *ch =
492 audio_get_group_idle_channel( audio->group,
493 audio->max_channels );
494
495 /* group is full */
496 if( !ch ){
497 audio_channel *existing =
498 audio_get_group_first_active_channel( audio->group );
499
500 if( existing ){
501 if( existing->source == &clip->clip ){
502 audio_unlock();
503 return;
504 }
505
506 existing->group = 0;
507 existing = audio_channel_fadeout(existing, audio->crossfade);
508 }
509
510 ch = audio_get_first_idle_channel();
511 }
512
513 if( ch ){
514 audio_channel_init( ch, &clip->clip, audio->flags );
515 audio_channel_group( ch, audio->group );
516 audio_channel_fadein( ch, audio->crossfade );
517 ch = audio_relinquish_channel( ch );
518 }
519 }
520
521 audio_unlock();
522 return;
523 }
524 }
525 }
526
527 VG_STATIC void entity_call( world_instance *world, ent_call *call )
528 {
529 if( call->ent.type == k_ent_volume ){
530 ent_volume_call( world, call );
531 } else if( call->ent.type == k_ent_audio ){
532 ent_audio_call( world, call );
533 }
534 }
535
536 VG_STATIC void world_update( world_instance *world, v3f pos )
537 {
538 world_global.sky_time += world_global.sky_rate * vg.time_delta;
539 world_global.sky_rate = vg_lerp( world_global.sky_rate,
540 world_global.sky_target_rate,
541 vg.time_delta * 5.0 );
542
543 world_routes_update_timer_texts( world );
544 world_routes_update( world );
545 //world_routes_debug( world );
546
547 /* ---- traffic -------- */
548
549 for( u32 i=0; i<mdl_arrcount( &world->ent_traffic ); i++ ){
550 ent_traffic *traffic = mdl_arritm( &world->ent_traffic, i );
551
552 u32 i1 = traffic->index,
553 i0,
554 i2 = i1+1;
555
556 if( i1 == 0 ) i0 = traffic->node_count-1;
557 else i0 = i1-1;
558
559 if( i2 >= traffic->node_count ) i2 = 0;
560
561 i0 += traffic->start_node;
562 i1 += traffic->start_node;
563 i2 += traffic->start_node;
564
565 v3f h[3];
566
567 ent_route_node *rn0 = mdl_arritm( &world->ent_route_node, i0 ),
568 *rn1 = mdl_arritm( &world->ent_route_node, i1 ),
569 *rn2 = mdl_arritm( &world->ent_route_node, i2 );
570
571 v3_copy( rn1->co, h[1] );
572 v3_lerp( rn0->co, rn1->co, 0.5f, h[0] );
573 v3_lerp( rn1->co, rn2->co, 0.5f, h[2] );
574
575 float const k_sample_dist = 0.0025f;
576 v3f pc, pd;
577 eval_bezier3( h[0], h[1], h[2], traffic->t, pc );
578 eval_bezier3( h[0], h[1], h[2], traffic->t+k_sample_dist, pd );
579
580 v3f v0;
581 v3_sub( pd, pc, v0 );
582 float length = vg_maxf( 0.0001f, v3_length( v0 ) );
583 v3_muls( v0, 1.0f/length, v0 );
584
585 float mod = k_sample_dist / length;
586
587 traffic->t += traffic->speed * vg.time_delta * mod;
588
589 if( traffic->t > 1.0f ){
590 traffic->t -= 1.0f;
591
592 if( traffic->t > 1.0f ) traffic->t = 0.0f;
593
594 traffic->index ++;
595
596 if( traffic->index >= traffic->node_count )
597 traffic->index = 0;
598 }
599
600 v3_copy( pc, traffic->transform.co );
601
602 float a = atan2f( -v0[0], v0[2] );
603 q_axis_angle( traffic->transform.q, (v3f){0.0f,1.0f,0.0f}, -a );
604
605 vg_line_pt3( traffic->transform.co, 0.3f, VG__BLUE );
606 }
607
608 /* ---- SFD ------------ */
609
610 if( mdl_arrcount( &world->ent_route ) ){
611 u32 closest = 0;
612 float min_dist = INFINITY;
613
614 for( u32 i=0; i<mdl_arrcount( &world->ent_route ); i++ ){
615 ent_route *route = mdl_arritm( &world->ent_route, i );
616 float dist = v3_dist2( route->board_transform[3], pos );
617
618 if( dist < min_dist ){
619 min_dist = dist;
620 closest = i;
621 }
622 }
623
624 if( (world_global.sfd.active_route_board != closest)
625 || network_scores_updated )
626 {
627 network_scores_updated = 0;
628 world_global.sfd.active_route_board = closest;
629
630 ent_route *route = mdl_arritm( &world->ent_route, closest );
631 u32 id = route->official_track_id;
632
633 if( id != 0xffffffff ){
634 struct netmsg_board *local_board =
635 &scoreboard_client_data.boards[id];
636
637 for( int i=0; i<13; i++ ){
638 sfd_encode( i, &local_board->data[27*i] );
639 }
640 }else{
641 sfd_encode( 0, mdl_pstr( &world->meta, route->pstr_name ) );
642 sfd_encode( 1, "No data" );
643 }
644 }
645 }
646 sfd_update();
647
648 static float random_accum = 0.0f;
649 random_accum += vg.time_delta;
650
651 u32 random_ticks = 0;
652
653 while( random_accum > 0.1f ){
654 random_accum -= 0.1f;
655 random_ticks ++;
656 }
657
658 float radius = 25.0f;
659 boxf volume_proximity;
660 v3_add( pos, (v3f){ radius, radius, radius }, volume_proximity[1] );
661 v3_sub( pos, (v3f){ radius, radius, radius }, volume_proximity[0] );
662
663 bh_iter it;
664 bh_iter_init( 0, &it );
665 int idx;
666
667 int in_volume = 0;
668
669 while( bh_next( world->volume_bh, &it, volume_proximity, &idx ) ){
670 ent_volume *volume = mdl_arritm( &world->ent_volume, idx );
671
672 boxf cube = {{-1.0f,-1.0f,-1.0f},{1.0f,1.0f,1.0f}};
673
674 if( volume->type == k_volume_subtype_trigger ){
675 v3f local;
676 m4x3_mulv( volume->to_local, pos, local );
677
678 if( (fabsf(local[0]) <= 1.0f) &&
679 (fabsf(local[1]) <= 1.0f) &&
680 (fabsf(local[2]) <= 1.0f) )
681 {
682 in_volume = 1;
683 vg_line_boxf_transformed( volume->to_world, cube, 0xff00ff00 );
684
685 if( !world_global.in_volume ){
686 ent_call basecall;
687 basecall.ent.index = idx;
688 basecall.ent.type = k_ent_volume;
689 basecall.function = k_ent_function_trigger;
690 basecall.data = NULL;
691
692 entity_call( world, &basecall );
693 }
694 }
695 else
696 vg_line_boxf_transformed( volume->to_world, cube, 0xff0000ff );
697 }
698 else if( volume->type == k_volume_subtype_particle ){
699 vg_line_boxf_transformed( volume->to_world, cube, 0xff00c0ff );
700
701 for( int j=0; j<random_ticks; j++ ){
702 ent_call basecall;
703 basecall.ent.index = idx;
704 basecall.ent.type = k_ent_volume;
705 basecall.function = k_ent_function_trigger;
706 basecall.data = NULL;
707
708 entity_call( world, &basecall );
709 }
710 }
711 }
712 world_global.in_volume = in_volume;
713
714 #if 0
715 if( k_debug_light_indices )
716 {
717 for( int i=0; i<world->light_count; i++ ){
718 struct world_light *light = &world->lights[i];
719 struct classtype_world_light *inf = light->inf;
720
721 u32 colour = 0xff000000;
722 u8 r = inf->colour[0] * 255.0f,
723 g = inf->colour[1] * 255.0f,
724 b = inf->colour[2] * 255.0f;
725
726 colour |= r;
727 colour |= g << 8;
728 colour |= b << 16;
729
730 vg_line_pt3( light->node->co, 0.25f, colour );
731 }
732 }
733
734 #endif
735 }
736
737 /*
738 * -----------------------------------------------------------------------------
739 * API implementation
740 * -----------------------------------------------------------------------------
741 */
742
743 VG_STATIC void ray_world_get_tri( world_instance *world,
744 ray_hit *hit, v3f tri[3] )
745 {
746 for( int i=0; i<3; i++ )
747 v3_copy( world->scene_geo.arrvertices[ hit->tri[i] ].co, tri[i] );
748 }
749
750 VG_STATIC int ray_world( world_instance *world,
751 v3f pos, v3f dir, ray_hit *hit )
752 {
753 return scene_raycast( &world->scene_geo, world->geo_bh, pos, dir, hit );
754 }
755
756 /*
757 * Cast a sphere from a to b and see what time it hits
758 */
759 VG_STATIC int spherecast_world( world_instance *world,
760 v3f pa, v3f pb, float r, float *t, v3f n )
761 {
762 bh_iter it;
763 bh_iter_init( 0, &it );
764
765 boxf region;
766 box_init_inf( region );
767 box_addpt( region, pa );
768 box_addpt( region, pb );
769
770 v3_add( (v3f){ r, r, r}, region[1], region[1] );
771 v3_add( (v3f){-r,-r,-r}, region[0], region[0] );
772
773 v3f dir;
774 v3_sub( pb, pa, dir );
775
776 v3f dir_inv;
777 dir_inv[0] = 1.0f/dir[0];
778 dir_inv[1] = 1.0f/dir[1];
779 dir_inv[2] = 1.0f/dir[2];
780
781 int hit = -1;
782 float min_t = 1.0f;
783
784 int idx;
785 while( bh_next( world->geo_bh, &it, region, &idx ) ){
786 u32 *ptri = &world->scene_geo.arrindices[ idx*3 ];
787 v3f tri[3];
788
789 boxf box;
790 box_init_inf( box );
791
792 for( int j=0; j<3; j++ ){
793 v3_copy( world->scene_geo.arrvertices[ptri[j]].co, tri[j] );
794 box_addpt( box, tri[j] );
795 }
796
797 v3_add( (v3f){ r, r, r}, box[1], box[1] );
798 v3_add( (v3f){-r,-r,-r}, box[0], box[0] );
799
800 if( !ray_aabb1( box, pa, dir_inv, 1.0f ) )
801 continue;
802
803 float t;
804 v3f n1;
805 if( spherecast_triangle( tri, pa, dir, r, &t, n1 ) ){
806 if( t < min_t ){
807 min_t = t;
808 hit = idx;
809 v3_copy( n1, n );
810 }
811 }
812 }
813
814 *t = min_t;
815 return hit;
816 }
817
818 VG_STATIC
819 struct world_surface *world_tri_index_surface( world_instance *world,
820 u32 index )
821 {
822 for( int i=1; i<world->surface_count; i++ ){
823 struct world_surface *surf = &world->surfaces[i];
824
825 if( (index >= surf->sm_geo.vertex_start) &&
826 (index < surf->sm_geo.vertex_start+surf->sm_geo.vertex_count ) )
827 {
828 return surf;
829 }
830 }
831
832 return &world->surfaces[0];
833 }
834
835 VG_STATIC struct world_surface *world_contact_surface( world_instance *world,
836 rb_ct *ct )
837 {
838 return world_tri_index_surface( world, ct->element_id );
839 }
840
841 VG_STATIC struct world_surface *ray_hit_surface( world_instance *world,
842 ray_hit *hit )
843 {
844 return world_tri_index_surface( world, hit->tri[0] );
845 }
846
847 /*
848 * -----------------------------------------------------------------------------
849 * Audio sampling
850 * -----------------------------------------------------------------------------
851 */
852
853 VG_STATIC
854 enum audio_sprite_type world_audio_sample_sprite_random(v3f origin, v3f output);
855 VG_STATIC void world_audio_sample_distances( v3f co, int *index, float *value );
856
857 #include "audio.h"
858
859 /*
860 * Trace out a random point, near the player to try and determine water areas
861 */
862 VG_STATIC
863 enum audio_sprite_type world_audio_sample_sprite_random(v3f origin, v3f output)
864 {
865 v3f chance = { (vg_randf()-0.5f) * 30.0f,
866 8.0f,
867 (vg_randf()-0.5f) * 30.0f };
868
869 v3f pos;
870 v3_add( chance, origin, pos );
871
872 ray_hit contact;
873 contact.dist = vg_minf( 16.0f, pos[1] );
874
875 world_instance *world = get_active_world();
876
877 if( ray_world( world, pos, (v3f){0.0f,-1.0f,0.0f}, &contact ) ){
878 struct world_surface *mat = ray_hit_surface( world, &contact );
879
880 if( mat->info.surface_prop == k_surface_prop_grass){
881 v3_copy( contact.pos, output );
882 return k_audio_sprite_type_grass;
883 }
884 else{
885 return k_audio_sprite_type_none;
886 }
887 }
888
889 output[0] = pos[0];
890 output[1] = 0.0f;
891 output[2] = pos[2];
892
893 float dist = fabsf(output[1] - origin[1]);
894
895 if( world->water.enabled && dist<=40.0f )
896 return k_audio_sprite_type_water;
897 else
898 return k_audio_sprite_type_none;
899 }
900
901 VG_STATIC void world_audio_sample_distances( v3f co, int *index, float *value )
902 {
903 float inr3 = 0.57735027,
904 inr2 = 0.70710678118;
905
906 v3f sample_directions[] = {
907 { -1.0f, 0.0f, 0.0f },
908 { 1.0f, 0.0f, 0.0f },
909 { 0.0f, 0.0f, 1.0f },
910 { 0.0f, 0.0f, -1.0f },
911 { 0.0f, 1.0f, 0.0f },
912 { 0.0f, -1.0f, 0.0f },
913 { -inr3, inr3, inr3 },
914 { inr3, inr3, inr3 },
915 { -inr3, inr3, -inr3 },
916 { inr3, inr3, -inr3 },
917 { -inr2, 0.0f, inr2 },
918 { inr2, 0.0f, inr2 },
919 { -inr2, 0.0f, -inr2 },
920 { inr2, 0.0f, -inr2 },
921 };
922
923 static int si = 0;
924 static float distances[16];
925
926 ray_hit ray;
927 ray.dist = 5.0f;
928
929 v3f rc, rd, ro;
930 v3_copy( sample_directions[ si ], rd );
931 v3_add( co, (v3f){0.0f,1.5f,0.0f}, ro );
932 v3_copy( ro, rc );
933
934 float dist = 200.0f;
935
936 for( int i=0; i<10; i++ ){
937 if( ray_world( get_active_world(), rc, rd, &ray ) ){
938 dist = (float)i*5.0f + ray.dist;
939 break;
940 }
941 else{
942 v3_muladds( rc, rd, ray.dist, rc );
943 }
944 }
945
946 distances[si] = dist;
947
948 if( vg_lines.draw ){
949 for( int i=0; i<14; i++ ){
950 if( distances[i] != 200.0f ){
951 u32 colours[] = { VG__RED, VG__BLUE, VG__GREEN,
952 VG__CYAN, VG__YELOW, VG__PINK,
953 VG__WHITE };
954
955 u32 colour = colours[i%7];
956
957 v3f p1;
958 v3_muladds( ro, sample_directions[i], distances[i], p1 );
959 vg_line( ro, p1, colour );
960 vg_line_pt3( p1, 0.1f, colour );
961 }
962 }
963 }
964
965 *index = si;
966 *value = dist;
967
968 si ++;
969 if( si >= 14 )
970 si = 0;
971 }
972
973 #endif /* WORLD_H */