7212714090f6f82a61beec225fbed109e0994e94
2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
9 #include "world_gate.h"
11 #include "skaterift.h"
17 #include "world_water.h"
18 #include "player_remote.h"
19 #include "shaders/model_gate_unlinked.h"
21 struct world_gates world_gates
;
24 * Update the transform matrices for gate
26 void gate_transform_update( ent_gate
*gate
)
28 if( gate
->flags
& k_ent_gate_flip
){
30 q_axis_angle( qflip
, (v3f
){0.0f
,1.0f
,0.0f
}, VG_PIf
);
31 q_mul( gate
->q
[1], qflip
, gate
->q
[1] );
34 m4x3f to_local
, recv_to_world
;
36 q_m3x3( gate
->q
[0], gate
->to_world
);
37 v3_copy( gate
->co
[0], gate
->to_world
[3] );
39 m4x3_invert_affine( gate
->to_world
, to_local
);
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
);
46 void world_gates_init(void)
48 vg_info( "world_gates_init\n" );
49 vg_linear_clear( vg_mem
.scratch
);
52 mdl_open( &mgate
, "models/rs_gate.mdl", vg_mem
.scratch
);
53 mdl_load_metadata_block( &mgate
, vg_mem
.scratch
);
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
;
59 const char *names
[] = { "rs_gate_marker", "rs_gate_marker.001",
60 "rs_gate_marker.002", "rs_gate_marker.003" };
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
;
68 mdl_async_load_glmesh( &mgate
, &world_gates
.mesh
, NULL
);
72 void ent_gate_get_mdl_mtx( ent_gate
*gate
, m4x3f mmdl
)
74 m4x3_copy( gate
->to_world
, mmdl
);
76 if( !(gate
->flags
& k_ent_gate_custom_mesh
) ){
77 m3x3_scale( mmdl
, (v3f
){ gate
->dimensions
[0],
78 gate
->dimensions
[1], 1.0f
} );
82 static void render_gate_mesh( world_instance
*world
, ent_gate
*gate
)
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
);
93 mesh_bind( &world_gates
.mesh
);
94 mdl_draw_submesh( &world_gates
.sm_surface
);
99 * Render the view through a gate
101 int render_gate( world_instance
*world
, world_instance
*world_inside
,
102 ent_gate
*gate
, vg_camera
*cam
)
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
);
109 v3_sub( cam
->pos
, gate
->co
[0], v0
);
111 float dist
= v3_dot(v0
, gatedir
);
117 if( v3_dist( cam
->pos
, gate
->co
[0] ) > 100.0f
)
121 f32 w
= gate
->dimensions
[0],
122 h
= gate
->dimensions
[1];
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
);
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 );
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
;
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
);
146 /* Add special clipping plane to projection */
148 q_mulv( gate
->q
[1], (v3f
){0.0f
,0.0f
,-1.0f
}, surface
);
149 surface
[3] = v3_dot( surface
, gate
->co
[1] );
151 m4x3_mulp( world_gates
.cam
.transform_inverse
, surface
, surface
);
152 surface
[3] = -fabsf(surface
[3]);
155 m4x4_clip_projection( world_gates
.cam
.mtx
.p
, surface
);
157 /* Ready to draw with new camrea */
158 vg_camera_finalize( &world_gates
.cam
);
160 vg_line_point( world_gates
.cam
.transform
[3], 0.3f
, 0xff00ff00 );
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
});
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
);
178 ent_gate_get_mdl_mtx( gate
, mmdl
);
179 shader_model_gate_uMdl( mmdl
);
180 render_gate_mesh( world
, gate
);
182 render_world( world_inside
, &world_gates
.cam
,
183 1, !localplayer
.gate_waiting
, 1, 1 );
188 void render_gate_unlinked( world_instance
*world
,
189 ent_gate
*gate
, vg_camera
*cam
)
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
);
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
);
204 vg_line_point( gate
->co
[0], 0.1f
, 0xffffff00 );
206 render_gate_mesh( world
, gate
);
210 * Intersect the plane of a gate with a line segment, plane coordinate result
213 static int gate_intersect_plane( ent_gate
*gate
,
214 v3f pos
, v3f last
, v2f where
)
217 q_mulv( gate
->q
[0], (v3f
){0.0f
,0.0f
,-1.0f
}, surface
);
218 surface
[3] = v3_dot( surface
, gate
->co
[0] );
220 v3f v0
, c
, delta
, p0
;
221 v3_sub( pos
, last
, v0
);
222 float l
= v3_length( v0
);
227 v3_divs( v0
, l
, v0
);
229 v3_muls( surface
, surface
[3], c
);
230 v3_sub( c
, last
, delta
);
232 float d
= v3_dot( surface
, v0
);
235 float t
= v3_dot(delta
, surface
) / d
;
236 if( t
>= 0.0f
&& t
<= l
){
238 v3_muladds( last
, v0
, t
, local
);
239 v3_sub( gate
->co
[0], local
, rel
);
241 where
[0] = v3_dot( rel
, gate
->to_world
[0] );
242 where
[1] = v3_dot( rel
, gate
->to_world
[1] );
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] );
255 * Intersect specific gate
257 int gate_intersect( ent_gate
*gate
, v3f pos
, v3f last
)
261 if( gate_intersect_plane( gate
, pos
, last
, xy
) ){
262 if( (fabsf(xy
[0]) <= gate
->dimensions
[0]) &&
263 (fabsf(xy
[1]) <= gate
->dimensions
[1]) ){
272 * Intersect all gates in the world
274 u32
world_intersect_gates( world_instance
*world
, v3f pos
, v3f last
)
276 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
277 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, i
);
279 if( !(gate
->flags
& k_ent_gate_linked
) ) continue;
280 if( gate
->flags
& k_ent_gate_locked
) continue;
282 if( gate
->flags
& k_ent_gate_nonlocal
){
283 if( world_static
.instances
[gate
->target
].status
284 != k_world_status_loaded
)
288 if( gate_intersect( gate
, pos
, last
) )
289 return mdl_entity_id( k_ent_gate
, i
);
295 void ent_gate_call( world_instance
*world
, ent_call
*call
)
297 u32 index
= mdl_entity_id_id( call
->id
);
298 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, index
);
300 if( call
->function
== 0 ){ /* unlock() */
301 gate
->flags
&= ~k_ent_gate_locked
;
304 vg_print_backtrace();
305 vg_error( "Unhandled function id: %u\n", call
->function
);
311 * detatches any nonlocal gates
313 void world_unlink_nonlocal( world_instance
*world
)
315 for( u32 j
=0; j
<mdl_arrcount(&world
->ent_gate
); j
++ ){
316 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, j
);
318 if( gate
->flags
& k_ent_gate_nonlocal
){
319 gate
->flags
&= ~k_ent_gate_linked
;
325 * attatches nonlocal gates, to be called from main thread ONLY!
327 void world_link_nonlocal_async( void *payload
, u32 size
)
329 world_instance
*world
= payload
;
330 u32 world_id
= world
- world_static
.instances
;
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
);
336 if( skaterift
.demo_mode
)
337 if( world_static
.instance_addons
[world_id
]->flags
& ADDON_REG_PREMIUM
)
340 if( !(gate
->flags
& k_ent_gate_nonlocal
) ) continue;
341 if( gate
->flags
& k_ent_gate_linked
) continue;
343 const char *key
= mdl_pstr( &world
->meta
, gate
->key
);
344 vg_info( "key: %s\n", key
);
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
);
352 for( u32 k
=0; k
<mdl_arrcount( &other
->ent_gate
); k
++ ){
353 ent_gate
*gate2
= mdl_arritm( &other
->ent_gate
, k
);
355 if( !(gate2
->flags
& k_ent_gate_nonlocal
) ) continue;
356 if( gate2
->flags
& k_ent_gate_linked
) continue;
358 const char *key2
= mdl_pstr( &other
->meta
, gate2
->key
);
359 vg_info( " key2: %s\n", key2
);
361 if( strcmp( key
, key2
) ) continue;
363 vg_success( "Non-local matching pair '%s' found. (%u:%u)\n",
366 gate
->flags
|= k_ent_gate_linked
;
367 gate2
->flags
|= k_ent_gate_linked
;
369 gate2
->target
= world_id
;
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] );
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.
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] );
390 gate_transform_update( gate
);
391 gate_transform_update( gate2
);
399 #endif /* WORLD_GATE_C */