2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
9 #include "world_gate.h"
11 #include "skaterift.h"
18 #include "world_water.h"
19 #include "player_remote.h"
22 * Update the transform matrices for gate
24 static void gate_transform_update( ent_gate
*gate
){
25 if( gate
->flags
& k_ent_gate_flip
){
27 q_axis_angle( qflip
, (v3f
){0.0f
,1.0f
,0.0f
}, VG_PIf
);
28 q_mul( gate
->q
[1], qflip
, gate
->q
[1] );
31 m4x3f to_local
, recv_to_world
;
33 q_m3x3( gate
->q
[0], gate
->to_world
);
34 v3_copy( gate
->co
[0], gate
->to_world
[3] );
36 m4x3_invert_affine( gate
->to_world
, to_local
);
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
);
43 static void world_gates_init(void)
45 vg_info( "world_gates_init\n" );
47 shader_model_gate_register();
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 static void ent_gate_get_mdl_mtx( ent_gate
*gate
, m4x3f mmdl
){
73 m4x3_copy( gate
->to_world
, mmdl
);
75 if( !(gate
->flags
& k_ent_gate_custom_mesh
) ){
76 m3x3_scale( mmdl
, (v3f
){ gate
->dimensions
[0],
77 gate
->dimensions
[1], 1.0f
} );
82 * Render the view through a gate
84 static int render_gate( world_instance
*world
, world_instance
*world_inside
,
85 ent_gate
*gate
, camera
*cam
){
87 m3x3_mulv( cam
->transform
, (v3f
){0.0f
,0.0f
,-1.0f
}, viewdir
);
88 q_mulv( gate
->q
[0], (v3f
){0.0f
,0.0f
,-1.0f
}, gatedir
);
91 v3_sub( cam
->pos
, gate
->co
[0], v0
);
93 float dist
= v3_dot(v0
, gatedir
);
99 if( v3_dist( cam
->pos
, gate
->co
[0] ) > 100.0f
)
103 f32 w
= gate
->dimensions
[0],
104 h
= gate
->dimensions
[1];
107 m4x3_mulv( gate
->to_world
, (v3f
){-w
,-h
,0.0f
}, a
);
108 m4x3_mulv( gate
->to_world
, (v3f
){ w
,-h
,0.0f
}, b
);
109 m4x3_mulv( gate
->to_world
, (v3f
){ w
, h
,0.0f
}, c
);
110 m4x3_mulv( gate
->to_world
, (v3f
){-w
, h
,0.0f
}, d
);
112 vg_line( a
,b
, 0xffffa000 );
113 vg_line( b
,c
, 0xffffa000 );
114 vg_line( c
,d
, 0xffffa000 );
115 vg_line( d
,a
, 0xffffa000 );
116 vg_line( gate
->co
[0], gate
->co
[1], 0xff0000ff );
119 /* update gate camera */
120 world_gates
.cam
.fov
= cam
->fov
;
121 world_gates
.cam
.nearz
= 0.1f
;
122 world_gates
.cam
.farz
= 2000.0f
;
124 m4x3_mul( gate
->transport
, cam
->transform
, world_gates
.cam
.transform
);
125 camera_update_view( &world_gates
.cam
);
126 camera_update_projection( &world_gates
.cam
);
128 /* Add special clipping plane to projection */
130 q_mulv( gate
->q
[1], (v3f
){0.0f
,0.0f
,-1.0f
}, surface
);
131 surface
[3] = v3_dot( surface
, gate
->co
[1] );
133 m4x3_mulp( world_gates
.cam
.transform_inverse
, surface
, surface
);
134 surface
[3] = -fabsf(surface
[3]);
137 m4x4_clip_projection( world_gates
.cam
.mtx
.p
, surface
);
139 /* Ready to draw with new camrea */
140 camera_finalize( &world_gates
.cam
);
142 vg_line_point( world_gates
.cam
.transform
[3], 0.3f
, 0xff00ff00 );
144 shader_model_gate_use();
145 shader_model_gate_uPv( cam
->mtx
.pv
);
146 shader_model_gate_uCam( cam
->pos
);
147 shader_model_gate_uColour( (v4f
){0.0f
,1.0f
,0.0f
,0.0f
} );
148 shader_model_gate_uTime( vg
.time
*0.25f
);
149 shader_model_gate_uInvRes( (v2f
){
150 1.0f
/ (float)vg
.window_x
,
151 1.0f
/ (float)vg
.window_y
});
153 glEnable( GL_STENCIL_TEST
);
154 glStencilOp( GL_KEEP
, GL_KEEP
, GL_REPLACE
);
155 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
156 glStencilMask( 0xFF );
157 glDisable( GL_CULL_FACE
);
160 ent_gate_get_mdl_mtx( gate
, mmdl
);
161 shader_model_gate_uMdl( mmdl
);
163 if( gate
->flags
& k_ent_gate_custom_mesh
){
164 mesh_bind( &world
->mesh_no_collide
);
165 for( u32 i
=0; i
<gate
->submesh_count
; i
++ ){
166 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
,
167 gate
->submesh_start
+i
);
168 mdl_draw_submesh( sm
);
172 mesh_bind( &world_gates
.mesh
);
173 mdl_draw_submesh( &world_gates
.sm_surface
);
176 render_world( world_inside
, &world_gates
.cam
,
177 1, !localplayer
.gate_waiting
, 1, 1 );
183 * Intersect the plane of a gate with a line segment, plane coordinate result
186 static int gate_intersect_plane( ent_gate
*gate
,
187 v3f pos
, v3f last
, v2f where
)
190 q_mulv( gate
->q
[0], (v3f
){0.0f
,0.0f
,-1.0f
}, surface
);
191 surface
[3] = v3_dot( surface
, gate
->co
[0] );
193 v3f v0
, c
, delta
, p0
;
194 v3_sub( pos
, last
, v0
);
195 float l
= v3_length( v0
);
200 v3_divs( v0
, l
, v0
);
202 v3_muls( surface
, surface
[3], c
);
203 v3_sub( c
, last
, delta
);
205 float d
= v3_dot( surface
, v0
);
208 float t
= v3_dot(delta
, surface
) / d
;
209 if( t
>= 0.0f
&& t
<= l
){
211 v3_muladds( last
, v0
, t
, local
);
212 v3_sub( gate
->co
[0], local
, rel
);
214 where
[0] = v3_dot( rel
, gate
->to_world
[0] );
215 where
[1] = v3_dot( rel
, gate
->to_world
[1] );
217 where
[0] /= v3_dot( gate
->to_world
[0], gate
->to_world
[0] );
218 where
[1] /= v3_dot( gate
->to_world
[1], gate
->to_world
[1] );
228 * Intersect specific gate
230 static int gate_intersect( ent_gate
*gate
, v3f pos
, v3f last
){
233 if( gate_intersect_plane( gate
, pos
, last
, xy
) ){
234 if( (fabsf(xy
[0]) <= gate
->dimensions
[0]) &&
235 (fabsf(xy
[1]) <= gate
->dimensions
[1]) ){
244 * Intersect all gates in the world
246 static u32
world_intersect_gates( world_instance
*world
, v3f pos
, v3f last
){
247 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
248 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, i
);
250 if( !(gate
->flags
& k_ent_gate_linked
) ) continue;
251 if( gate
->flags
& k_ent_gate_locked
) continue;
253 if( gate
->flags
& k_ent_gate_nonlocal
){
254 if( world_static
.instances
[gate
->target
].status
255 != k_world_status_loaded
)
259 if( gate_intersect( gate
, pos
, last
) )
260 return mdl_entity_id( k_ent_gate
, i
);
266 static void ent_gate_call( world_instance
*world
, ent_call
*call
){
267 u32 index
= mdl_entity_id_id( call
->id
);
268 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, index
);
270 if( call
->function
== 0 ){ /* unlock() */
271 gate
->flags
&= ~k_ent_gate_locked
;
274 vg_print_backtrace();
275 vg_error( "Unhandled function id: %u\n", call
->function
);
281 * detatches any nonlocal gates
283 static void world_unlink_nonlocal( world_instance
*world
){
284 for( u32 j
=0; j
<mdl_arrcount(&world
->ent_gate
); j
++ ){
285 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, j
);
287 if( gate
->flags
& k_ent_gate_nonlocal
){
288 gate
->flags
&= ~k_ent_gate_linked
;
294 * attatches nonlocal gates, to be called from main thread ONLY!
296 static void world_link_nonlocal_async( void *payload
, u32 size
){
297 world_instance
*world
= payload
;
298 u32 world_id
= world
- world_static
.instances
;
300 for( u32 j
=0; j
<mdl_arrcount(&world
->ent_gate
); j
++ ){
301 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, j
);
303 if( !(gate
->flags
& k_ent_gate_nonlocal
) ) continue;
304 if( gate
->flags
& k_ent_gate_linked
) continue;
306 const char *key
= mdl_pstr( &world
->meta
, gate
->key
);
307 vg_info( "key: %s\n", key
);
309 for( u32 i
=0; i
<vg_list_size(world_static
.instances
); i
++ ){
310 world_instance
*other
= &world_static
.instances
[i
];
311 if( other
== world
) continue;
312 if( other
->status
!= k_world_status_loaded
) continue;
313 vg_info( "Checking world %u for key matches\n", i
);
315 for( u32 k
=0; k
<mdl_arrcount( &other
->ent_gate
); k
++ ){
316 ent_gate
*gate2
= mdl_arritm( &other
->ent_gate
, k
);
318 if( !(gate2
->flags
& k_ent_gate_nonlocal
) ) continue;
319 if( gate2
->flags
& k_ent_gate_linked
) continue;
321 const char *key2
= mdl_pstr( &other
->meta
, gate2
->key
);
322 vg_info( " key2: %s\n", key2
);
324 if( strcmp( key
, key2
) ) continue;
326 vg_success( "Non-local matching pair '%s' found. (%u:%u)\n",
329 gate
->flags
|= k_ent_gate_linked
;
330 gate2
->flags
|= k_ent_gate_linked
;
332 gate2
->target
= world_id
;
334 v3_copy( gate
->co
[0], gate2
->co
[1] );
335 v3_copy( gate2
->co
[0], gate
->co
[1] );
336 v4_copy( gate
->q
[0], gate2
->q
[1] );
337 v4_copy( gate2
->q
[0], gate
->q
[1] );
339 if( world
->meta
.info
.version
< 102 ){
340 /* LEGACY BEHAVIOUR: v101
341 * this would flip both the client worlds portal's entrance and
342 * exit. effectively the clients portal would be the opposite
343 * to the hub worlds one. new behaviour is to just flip the
344 * destinations so the rules are consistent in each world.
347 q_axis_angle( qflip
, (v3f
){0.0f
,1.0f
,0.0f
}, VG_PIf
);
348 q_mul( gate
->q
[0], qflip
, gate
->q
[0] );
349 q_mul( gate
->q
[1], qflip
, gate
->q
[1] );
350 q_mul( gate2
->q
[1], qflip
, gate2
->q
[1] );
353 gate_transform_update( gate
);
354 gate_transform_update( gate2
);
362 #endif /* WORLD_GATE_C */