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