the luxuries of a modern C compiler
[carveJwlIkooP6JGAAIwe30JlM.git] / world_entity.c
1 #ifndef WORLD_ENTITY_C
2 #define WORLD_ENTITY_C
3
4 #include "world.h"
5 #include "world_load.h"
6 #include "entity.h"
7
8 VG_STATIC void world_gen_entities_init(void)
9 {
10 world_instance *world = world_loading_instance();
11
12 /* lights */
13 for( u32 j=0; j<mdl_arrcount(&world->ent_light); j ++ ){
14 ent_light *light = mdl_arritm( &world->ent_light, j );
15
16 m4x3f to_world;
17 q_m3x3( light->transform.q, to_world );
18 v3_copy( light->transform.co, to_world[3] );
19 m4x3_invert_affine( to_world, light->inverse_world );
20
21 light->angle_sin_cos[0] = sinf( light->angle * 0.5f );
22 light->angle_sin_cos[1] = cosf( light->angle * 0.5f );
23 }
24
25 /* gates */
26 for( u32 j=0; j<mdl_arrcount(&world->ent_gate); j ++ ){
27 ent_gate *gate = mdl_arritm( &world->ent_gate, j );
28
29 if( gate->type == k_gate_type_teleport ){
30 gate_transform_update( gate );
31 }
32 }
33 vg_async_call( world_link_nonlocal_async, world, 0 );
34
35 /* water */
36 for( u32 j=0; j<mdl_arrcount(&world->ent_water); j++ ){
37 ent_water *water = mdl_arritm( &world->ent_water, j );
38 if( world->water.enabled ){
39 vg_warn( "Multiple water surfaces in level!\n" );
40 break;
41 }
42
43 world->water.enabled = 1;
44 water_set_surface( world, water->transform.co[1] );
45 }
46
47 /* volumes */
48 for( u32 j=0; j<mdl_arrcount(&world->ent_volume); j++ ){
49 ent_volume *volume = mdl_arritm( &world->ent_volume, j );
50 mdl_transform_m4x3( &volume->transform, volume->to_world );
51 m4x3_invert_full( volume->to_world, volume->to_local );
52 }
53
54 /* audio packs */
55 for( u32 j=0; j<mdl_arrcount(&world->ent_audio); j++ ){
56 ent_audio *audio = mdl_arritm( &world->ent_audio, j );
57
58 for( u32 k=0; k<audio->clip_count; k++ ){
59 ent_audio_clip *clip = mdl_arritm( &world->ent_audio_clip,
60 audio->clip_start+k );
61
62 if( clip->file.pack_size ){
63 u32 size = clip->file.pack_size,
64 offset = clip->file.pack_offset;
65
66 /* embedded files are fine to clear the scratch buffer, only
67 * external audio uses it */
68
69 vg_linear_clear( vg_mem.scratch );
70 void *data = vg_linear_alloc( vg_mem.scratch,
71 clip->file.pack_size );
72
73 mdl_fread_pack_file( &world->meta, &clip->file, data );
74
75 clip->clip.path = NULL;
76 clip->clip.flags = audio->flags;
77 clip->clip.data = data;
78 clip->clip.size = size;
79 }
80 else{
81 clip->clip.path = mdl_pstr( &world->meta, clip->file.pstr_path );
82 clip->clip.flags = audio->flags;
83 clip->clip.data = NULL;
84 clip->clip.size = 0;
85 }
86
87 audio_clip_load( &clip->clip, world->heap );
88 }
89 }
90 }
91
92 VG_STATIC
93 ent_spawn *world_find_closest_spawn( world_instance *world, v3f position )
94 {
95 ent_spawn *rp = NULL, *r;
96 float min_dist = INFINITY;
97
98 for( u32 i=0; i<mdl_arrcount(&world->ent_spawn); i++ ){
99 r = mdl_arritm( &world->ent_spawn, i );
100 float d = v3_dist2( r->transform.co, position );
101
102 if( d < min_dist ){
103 min_dist = d;
104 rp = r;
105 }
106 }
107
108 if( !rp ){
109 if( mdl_arrcount(&world->ent_spawn) ){
110 vg_warn( "Invalid distances to spawns.. defaulting to first one.\n" );
111 return mdl_arritm( &world->ent_spawn, 0 );
112 }
113 else{
114 vg_error( "There are no spawns in the level!\n" );
115 }
116 }
117
118 return rp;
119 }
120
121 VG_STATIC
122 ent_spawn *world_find_spawn_by_name( world_instance *world, const char *name )
123 {
124 ent_spawn *rp = NULL, *r;
125 for( u32 i=0; i<mdl_arrcount(&world->ent_spawn); i++ ){
126 r = mdl_arritm( &world->ent_spawn, i );
127 if( !strcmp( mdl_pstr(&world->meta, r->pstr_name), name ) ){
128 rp = r;
129 break;
130 }
131 }
132
133 if( !rp )
134 vg_warn( "No spawn named '%s'\n", name );
135
136 return rp;
137 }
138
139 VG_STATIC void ent_volume_call( world_instance *world, ent_call *call )
140 {
141 u32 index = mdl_entity_id_id( call->id );
142 ent_volume *volume = mdl_arritm( &world->ent_volume, index );
143 if( !volume->target ) return;
144
145 if( call->function == k_ent_function_trigger ){
146 call->id = volume->target;
147
148 if( volume->type == k_volume_subtype_particle ){
149 float *co = alloca( sizeof(float)*3 );
150 co[0] = vg_randf64()*2.0f-1.0f;
151 co[1] = vg_randf64()*2.0f-1.0f;
152 co[2] = vg_randf64()*2.0f-1.0f;
153 m4x3_mulv( volume->to_world, co, co );
154
155 call->function = k_ent_function_particle_spawn;
156 call->data = co;
157 entity_call( world, call );
158 }
159 else{
160 entity_call( world, call );
161 }
162 }
163 }
164
165 VG_STATIC void ent_audio_call( world_instance *world, ent_call *call )
166 {
167 if( world->status == k_world_status_unloading ){
168 vg_warn( "cannot modify audio while unloading world\n" );
169 return;
170 }
171
172 u8 world_id = (world - world_static.worlds) + 1;
173 u32 index = mdl_entity_id_id( call->id );
174 ent_audio *audio = mdl_arritm( &world->ent_audio, index );
175
176 v3f sound_co;
177
178 if( call->function == k_ent_function_particle_spawn ){
179 v3_copy( call->data, sound_co );
180 }
181 else if( call->function == k_ent_function_trigger ){
182 v3_copy( audio->transform.co, sound_co );
183 }
184 else
185 vg_fatal_error( "ent_audio_call (invalid function id)" );
186
187 float chance = vg_randf64()*100.0f,
188 bar = 0.0f;
189
190 for( u32 i=0; i<audio->clip_count; i++ ){
191 ent_audio_clip *clip = mdl_arritm( &world->ent_audio_clip,
192 audio->clip_start+i );
193
194 float mod = world->probabilities[ audio->probability_curve ],
195 p = clip->probability * mod;
196
197 bar += p;
198
199 if( chance < bar ){
200 audio_lock();
201
202 if( audio->behaviour == k_channel_behaviour_unlimited ){
203 audio_oneshot_3d( &clip->clip, sound_co,
204 audio->transform.s[0],
205 audio->volume );
206 }
207 else if( audio->behaviour == k_channel_behaviour_discard_if_full ){
208 audio_channel *ch =
209 audio_get_group_idle_channel( audio->group,
210 audio->max_channels );
211
212 if( ch ){
213 audio_channel_init( ch, &clip->clip, audio->flags );
214 audio_channel_group( ch, audio->group );
215 audio_channel_world( ch, world_id );
216 audio_channel_set_spacial( ch, sound_co, audio->transform.s[0] );
217 audio_channel_edit_volume( ch, audio->volume, 1 );
218 ch = audio_relinquish_channel( ch );
219 }
220 }
221 else if( audio->behaviour == k_channel_behaviour_crossfade_if_full){
222 audio_channel *ch =
223 audio_get_group_idle_channel( audio->group,
224 audio->max_channels );
225
226 /* group is full */
227 if( !ch ){
228 audio_channel *existing =
229 audio_get_group_first_active_channel( audio->group );
230
231 if( existing ){
232 if( existing->source == &clip->clip ){
233 audio_unlock();
234 return;
235 }
236
237 existing->group = 0;
238 existing = audio_channel_fadeout(existing, audio->crossfade);
239 }
240
241 ch = audio_get_first_idle_channel();
242 }
243
244 if( ch ){
245 audio_channel_init( ch, &clip->clip, audio->flags );
246 audio_channel_group( ch, audio->group );
247 audio_channel_world( ch, world_id );
248 audio_channel_fadein( ch, audio->crossfade );
249 ch = audio_relinquish_channel( ch );
250 }
251 }
252
253 audio_unlock();
254 return;
255 }
256 }
257 }
258
259 #endif /* WORLD_ENTITY_C */