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