npcs and tutorial stuff
[carveJwlIkooP6JGAAIwe30JlM.git] / world_gate.c
1 /*
2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef WORLD_GATE_C
6 #define WORLD_GATE_C
7
8 #include "world.h"
9 #include "world_gate.h"
10
11 #include "skaterift.h"
12 #include "common.h"
13 #include "model.h"
14 #include "entity.h"
15 #include "render.h"
16
17 #include "world_water.h"
18 #include "player_remote.h"
19 #include "shaders/model_gate_unlinked.h"
20
21 struct world_gates world_gates;
22
23 /*
24 * Update the transform matrices for gate
25 */
26 void gate_transform_update( ent_gate *gate )
27 {
28 if( gate->flags & k_ent_gate_flip ){
29 v4f qflip;
30 q_axis_angle( qflip, (v3f){0.0f,1.0f,0.0f}, VG_PIf );
31 q_mul( gate->q[1], qflip, gate->q[1] );
32 }
33
34 m4x3f to_local, recv_to_world;
35
36 q_m3x3( gate->q[0], gate->to_world );
37 v3_copy( gate->co[0], gate->to_world[3] );
38
39 m4x3_invert_affine( gate->to_world, to_local );
40
41 q_m3x3( gate->q[1], recv_to_world );
42 v3_copy( gate->co[1], recv_to_world[3] );
43 m4x3_mul( recv_to_world, to_local, gate->transport );
44 }
45
46 void world_gates_init(void)
47 {
48 vg_info( "world_gates_init\n" );
49 vg_linear_clear( vg_mem.scratch );
50
51 mdl_context mgate;
52 mdl_open( &mgate, "models/rs_gate.mdl", vg_mem.scratch );
53 mdl_load_metadata_block( &mgate, vg_mem.scratch );
54
55 mdl_mesh *surface = mdl_find_mesh( &mgate, "rs_gate" );
56 mdl_submesh *sm = mdl_arritm(&mgate.submeshs,surface->submesh_start);
57 world_gates.sm_surface = *sm;
58
59 const char *names[] = { "rs_gate_marker", "rs_gate_marker.001",
60 "rs_gate_marker.002", "rs_gate_marker.003" };
61
62 for( int i=0; i<4; i++ ){
63 mdl_mesh *marker = mdl_find_mesh( &mgate, names[i] );
64 sm = mdl_arritm( &mgate.submeshs, marker->submesh_start );
65 world_gates.sm_marker[i] = *sm;
66 }
67
68 mdl_async_load_glmesh( &mgate, &world_gates.mesh, NULL );
69 mdl_close( &mgate );
70 }
71
72 void ent_gate_get_mdl_mtx( ent_gate *gate, m4x3f mmdl )
73 {
74 m4x3_copy( gate->to_world, mmdl );
75
76 if( !(gate->flags & k_ent_gate_custom_mesh) ){
77 m3x3_scale( mmdl, (v3f){ gate->dimensions[0],
78 gate->dimensions[1], 1.0f } );
79 }
80 }
81
82 static void render_gate_mesh( world_instance *world, ent_gate *gate )
83 {
84 if( gate->flags & k_ent_gate_custom_mesh ){
85 mesh_bind( &world->mesh_no_collide );
86 for( u32 i=0; i<gate->submesh_count; i++ ){
87 mdl_submesh *sm = mdl_arritm( &world->meta.submeshs,
88 gate->submesh_start+i );
89 mdl_draw_submesh( sm );
90 }
91 }
92 else {
93 mesh_bind( &world_gates.mesh );
94 mdl_draw_submesh( &world_gates.sm_surface );
95 }
96 }
97
98 /*
99 * Render the view through a gate
100 */
101 int render_gate( world_instance *world, world_instance *world_inside,
102 ent_gate *gate, vg_camera *cam )
103 {
104 v3f viewdir, gatedir;
105 m3x3_mulv( cam->transform, (v3f){0.0f,0.0f,-1.0f}, viewdir );
106 q_mulv( gate->q[0], (v3f){0.0f,0.0f,-1.0f}, gatedir );
107
108 v3f v0;
109 v3_sub( cam->pos, gate->co[0], v0 );
110
111 float dist = v3_dot(v0, gatedir);
112
113 /* Hard cutoff */
114 if( dist > 3.0f )
115 return 0;
116
117 if( v3_dist( cam->pos, gate->co[0] ) > 100.0f )
118 return 0;
119
120 {
121 f32 w = gate->dimensions[0],
122 h = gate->dimensions[1];
123
124 v3f a,b,c,d;
125 m4x3_mulv( gate->to_world, (v3f){-w,-h,0.0f}, a );
126 m4x3_mulv( gate->to_world, (v3f){ w,-h,0.0f}, b );
127 m4x3_mulv( gate->to_world, (v3f){ w, h,0.0f}, c );
128 m4x3_mulv( gate->to_world, (v3f){-w, h,0.0f}, d );
129
130 vg_line( a,b, 0xffffa000 );
131 vg_line( b,c, 0xffffa000 );
132 vg_line( c,d, 0xffffa000 );
133 vg_line( d,a, 0xffffa000 );
134 vg_line( gate->co[0], gate->co[1], 0xff0000ff );
135 }
136
137 /* update gate camera */
138 world_gates.cam.fov = cam->fov;
139 world_gates.cam.nearz = 0.1f;
140 world_gates.cam.farz = 2000.0f;
141
142 m4x3_mul( gate->transport, cam->transform, world_gates.cam.transform );
143 vg_camera_update_view( &world_gates.cam );
144 vg_camera_update_projection( &world_gates.cam );
145
146 /* Add special clipping plane to projection */
147 v4f surface;
148 q_mulv( gate->q[1], (v3f){0.0f,0.0f,-1.0f}, surface );
149 surface[3] = v3_dot( surface, gate->co[1] );
150
151 m4x3_mulp( world_gates.cam.transform_inverse, surface, surface );
152 surface[3] = -fabsf(surface[3]);
153
154 if( dist < -0.5f )
155 m4x4_clip_projection( world_gates.cam.mtx.p, surface );
156
157 /* Ready to draw with new camrea */
158 vg_camera_finalize( &world_gates.cam );
159
160 vg_line_point( world_gates.cam.transform[3], 0.3f, 0xff00ff00 );
161
162 shader_model_gate_use();
163 shader_model_gate_uPv( cam->mtx.pv );
164 shader_model_gate_uCam( cam->pos );
165 shader_model_gate_uColour( (v4f){0.0f,1.0f,0.0f,0.0f} );
166 shader_model_gate_uTime( vg.time*0.25f );
167 shader_model_gate_uInvRes( (v2f){
168 1.0f / (float)vg.window_x,
169 1.0f / (float)vg.window_y });
170
171 glEnable( GL_STENCIL_TEST );
172 glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
173 glStencilFunc( GL_ALWAYS, 1, 0xFF );
174 glStencilMask( 0xFF );
175 glEnable( GL_CULL_FACE );
176
177 m4x3f mmdl;
178 ent_gate_get_mdl_mtx( gate, mmdl );
179 shader_model_gate_uMdl( mmdl );
180 render_gate_mesh( world, gate );
181
182 render_world( world_inside, &world_gates.cam,
183 1, !localplayer.gate_waiting, 1, 1 );
184
185 return 1;
186 }
187
188 void render_gate_unlinked( world_instance *world,
189 ent_gate *gate, vg_camera *cam )
190 {
191 m4x3f mmdl; m4x4f m4mdl;
192 ent_gate_get_mdl_mtx( gate, mmdl );
193 m4x3_expand( mmdl, m4mdl );
194 m4x4_mul( cam->mtx_prev.pv, m4mdl, m4mdl );
195
196 shader_model_gate_unlinked_use();
197 shader_model_gate_unlinked_uPv( cam->mtx.pv );
198 shader_model_gate_unlinked_uPvmPrev( m4mdl );
199 shader_model_gate_unlinked_uCam( cam->pos );
200 shader_model_gate_unlinked_uColour( (v4f){0.0f,1.0f,0.0f,0.0f} );
201 shader_model_gate_unlinked_uTime( vg.time*0.25f );
202 shader_model_gate_unlinked_uMdl( mmdl );
203
204 vg_line_point( gate->co[0], 0.1f, 0xffffff00 );
205
206 render_gate_mesh( world, gate );
207 }
208
209 /*
210 * Intersect the plane of a gate with a line segment, plane coordinate result
211 * stored in 'where'
212 */
213 static int gate_intersect_plane( ent_gate *gate,
214 v3f pos, v3f last, v2f where )
215 {
216 v4f surface;
217 q_mulv( gate->q[0], (v3f){0.0f,0.0f,-1.0f}, surface );
218 surface[3] = v3_dot( surface, gate->co[0] );
219
220 v3f v0, c, delta, p0;
221 v3_sub( pos, last, v0 );
222 float l = v3_length( v0 );
223
224 if( l == 0.0f )
225 return 0;
226
227 v3_divs( v0, l, v0 );
228
229 v3_muls( surface, surface[3], c );
230 v3_sub( c, last, delta );
231
232 float d = v3_dot( surface, v0 );
233
234 if( d > 0.00001f ){
235 float t = v3_dot(delta, surface) / d;
236 if( t >= 0.0f && t <= l ){
237 v3f local, rel;
238 v3_muladds( last, v0, t, local );
239 v3_sub( gate->co[0], local, rel );
240
241 where[0] = v3_dot( rel, gate->to_world[0] );
242 where[1] = v3_dot( rel, gate->to_world[1] );
243
244 where[0] /= v3_dot( gate->to_world[0], gate->to_world[0] );
245 where[1] /= v3_dot( gate->to_world[1], gate->to_world[1] );
246
247 return 1;
248 }
249 }
250
251 return 0;
252 }
253
254 /*
255 * Intersect specific gate
256 */
257 int gate_intersect( ent_gate *gate, v3f pos, v3f last )
258 {
259 v2f xy;
260
261 if( gate_intersect_plane( gate, pos, last, xy ) ){
262 if( (fabsf(xy[0]) <= gate->dimensions[0]) &&
263 (fabsf(xy[1]) <= gate->dimensions[1]) ){
264 return 1;
265 }
266 }
267
268 return 0;
269 }
270
271 /*
272 * Intersect all gates in the world
273 */
274 u32 world_intersect_gates( world_instance *world, v3f pos, v3f last )
275 {
276 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
277 ent_gate *gate = mdl_arritm( &world->ent_gate, i );
278
279 if( !(gate->flags & k_ent_gate_linked) ) continue;
280 if( gate->flags & k_ent_gate_locked ) continue;
281
282 if( gate->flags & k_ent_gate_nonlocal ){
283 if( world_static.instances[gate->target].status
284 != k_world_status_loaded )
285 continue;
286 }
287
288 if( gate_intersect( gate, pos, last ) )
289 return mdl_entity_id( k_ent_gate, i );
290 }
291
292 return 0;
293 }
294
295 void ent_gate_call( world_instance *world, ent_call *call )
296 {
297 u32 index = mdl_entity_id_id( call->id );
298 ent_gate *gate = mdl_arritm( &world->ent_gate, index );
299
300 if( call->function == 0 ){ /* unlock() */
301 gate->flags &= ~k_ent_gate_locked;
302 }
303 else {
304 vg_print_backtrace();
305 vg_error( "Unhandled function id: %u\n", call->function );
306 }
307 }
308
309
310 /*
311 * detatches any nonlocal gates
312 */
313 void world_unlink_nonlocal( world_instance *world )
314 {
315 for( u32 j=0; j<mdl_arrcount(&world->ent_gate); j ++ ){
316 ent_gate *gate = mdl_arritm( &world->ent_gate, j );
317
318 if( gate->flags & k_ent_gate_nonlocal ){
319 gate->flags &= ~k_ent_gate_linked;
320 }
321 }
322 }
323
324 /*
325 * attatches nonlocal gates, to be called from main thread ONLY!
326 */
327 void world_link_nonlocal_async( void *payload, u32 size )
328 {
329 world_instance *world = payload;
330 u32 world_id = world - world_static.instances;
331
332 for( u32 j=0; j<mdl_arrcount(&world->ent_gate); j ++ ){
333 ent_gate *gate = mdl_arritm( &world->ent_gate, j );
334 gate_transform_update( gate );
335
336 if( skaterift.demo_mode )
337 if( world_static.instance_addons[world_id]->flags & ADDON_REG_PREMIUM )
338 continue;
339
340 if( !(gate->flags & k_ent_gate_nonlocal) ) continue;
341 if( gate->flags & k_ent_gate_linked ) continue;
342
343 const char *key = mdl_pstr( &world->meta, gate->key );
344 vg_info( "key: %s\n", key );
345
346 for( u32 i=0; i<vg_list_size(world_static.instances); i++ ){
347 world_instance *other = &world_static.instances[i];
348 if( other == world ) continue;
349 if( other->status != k_world_status_loaded ) continue;
350 vg_info( "Checking world %u for key matches\n", i );
351
352 for( u32 k=0; k<mdl_arrcount( &other->ent_gate ); k++ ){
353 ent_gate *gate2 = mdl_arritm( &other->ent_gate, k );
354
355 if( !(gate2->flags & k_ent_gate_nonlocal) ) continue;
356 if( gate2->flags & k_ent_gate_linked ) continue;
357
358 const char *key2 = mdl_pstr( &other->meta, gate2->key );
359 vg_info( " key2: %s\n", key2 );
360
361 if( strcmp( key, key2 ) ) continue;
362
363 vg_success( "Non-local matching pair '%s' found. (%u:%u)\n",
364 key, world_id, i );
365
366 gate->flags |= k_ent_gate_linked;
367 gate2->flags |= k_ent_gate_linked;
368 gate->target = i;
369 gate2->target = world_id;
370
371 v3_copy( gate->co[0], gate2->co[1] );
372 v3_copy( gate2->co[0], gate->co[1] );
373 v4_copy( gate->q[0], gate2->q[1] );
374 v4_copy( gate2->q[0], gate->q[1] );
375
376 if( world->meta.info.version < 102 ){
377 /* LEGACY BEHAVIOUR: v101
378 * this would flip both the client worlds portal's entrance and
379 * exit. effectively the clients portal would be the opposite
380 * to the hub worlds one. new behaviour is to just flip the
381 * destinations so the rules are consistent in each world.
382 */
383 v4f qflip;
384 q_axis_angle( qflip, (v3f){0.0f,1.0f,0.0f}, VG_PIf );
385 q_mul( gate->q[0], qflip, gate->q[0] );
386 q_mul( gate->q[1], qflip, gate->q[1] );
387 q_mul( gate2->q[1], qflip, gate2->q[1] );
388 }
389
390 gate_transform_update( gate );
391 gate_transform_update( gate2 );
392
393 goto matched;
394 }
395 } matched:;
396 }
397 }
398
399 #endif /* WORLD_GATE_C */