the coolest fucking thing ive ever made
[carveJwlIkooP6JGAAIwe30JlM.git] / world.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #include "common.h"
6
7 static int ray_world( v3f pos, v3f dir, ray_hit *hit );
8
9 #ifndef WORLD_H
10 #define WORLD_H
11
12 #include "vg/vg_loader.h"
13
14 #include "network.h"
15 #include "network_msg.h"
16 #include "scene.h"
17 #include "render.h"
18 #include "rigidbody.h"
19 #include "bvh.h"
20 #include "model.h"
21
22 #include "traffic.h" /*TODO: -> world_traffic.h */
23
24 #include "shaders/terrain.h"
25 #include "shaders/sky.h"
26 #include "shaders/planeinf.h"
27 #include "shaders/standard.h"
28 #include "shaders/vblend.h"
29 #include "shaders/gpos.h"
30 #include "shaders/fscolour.h"
31 #include "shaders/alphatest.h"
32
33 enum { k_max_ui_segments = 8 };
34 enum { k_max_ui_splits_per_segment = 16 };
35
36 enum { k_max_ui_elements = k_max_ui_segments*k_max_ui_splits_per_segment };
37 enum { k_max_element_verts = 10 };
38 enum { k_max_element_indices = 20 };
39
40 enum { k_route_ui_max_verts = k_max_ui_elements*k_max_element_verts };
41 enum { k_route_ui_max_indices = k_max_ui_elements*k_max_element_indices };
42
43 static struct gworld
44 {
45 struct subworld_gen
46 {
47
48 }
49 subworld_gen;
50
51 /* gameplay */
52 struct respawn_point
53 {
54 v3f co;
55 v4f q;
56 char name[32];
57 }
58 spawns[32];
59 u32 spawn_count;
60
61 struct achievement_zone
62 {
63 m4x3f transform, inv_transform;
64 char name[32];
65 int triggered;
66 }
67 * achievement_zones;
68
69 u32 achievement_zones_count,
70 achievement_zones_cap;
71
72 struct subworld_routes
73 {
74 struct route_node
75 {
76 v3f co, right, up, h;
77 u32 next[2];
78
79 u32 special_type, special_id, current_refs, ref_count;
80 u32 route_ids[4]; /* Gates can be linked into up to four routes */
81 }
82 *nodes;
83
84 u32 node_count,
85 node_cap;
86
87 struct route
88 {
89 u32 track_id;
90 v4f colour;
91
92 u32 start;
93 mdl_submesh sm;
94
95 int active;
96 float factive;
97
98 double best_lap, latest_pass; /* Session */
99
100 struct
101 {
102 GLuint vao, vbo, ebo;
103
104 u32 indices_head;
105 u32 vertex_head;
106
107 float last_notch;
108
109 struct route_ui_segment
110 {
111 float length;
112 u32 vertex_start, vertex_count,
113 index_start, index_count, notches;
114 }
115 segments[k_max_ui_segments];
116
117 u32 segment_start, segment_count, fade_start, fade_count;
118 double fade_timer_start;
119 float xpos;
120 }
121 ui;
122
123 m4x3f scoreboard_transform;
124 }
125 *routes;
126
127 double time, rewind_from, rewind_to, last_use;
128
129 u32 route_count,
130 route_cap;
131
132 struct route_gate
133 {
134 struct teleport_gate
135 {
136 v3f co[2];
137 v4f q[2];
138 v2f dims;
139
140 m4x3f to_world, recv_to_world, transport;
141 }
142 gate;
143
144 u32 node_id;
145
146 struct route_timing
147 {
148 u32 version; /* Incremented on every teleport */
149 double time;
150 }
151 timing;
152 }
153 *gates;
154
155 struct route_collector
156 {
157 struct route_timing timing;
158 }
159 *collectors;
160
161 u32 gate_count,
162 gate_cap,
163 collector_count,
164 collector_cap;
165
166 u32 active_gate,
167 current_run_version;
168
169 scene scene_lines;
170 }
171 routes;
172
173 struct subworld_sfd
174 {
175 scene mesh;
176 mdl_submesh *sm_module, *sm_card;
177 glmesh temp;
178
179 struct sfd_instance
180 {
181 float *buffer;
182
183 u32 w,h;
184 }
185 tester;
186 }
187 sfd;
188
189 /* Paths */
190 traffic_node traffic[128];
191 u32 traffic_count;
192
193 #if 0
194 traffic_driver van_man[6];
195 #endif
196
197 double sky_time, sky_rate, sky_target_rate;
198
199 /* Physics */
200
201 /* Rendering & geometry */
202 scene geo, foliage;
203 rigidbody rb_geo;
204
205 /* TODO Maybe make this less hardcoded */
206 mdl_submesh sm_geo_std_oob, sm_geo_std, sm_geo_vb,
207 sm_foliage_main, sm_foliage_alphatest,
208 sm_graffiti, sm_subworld, sm_terrain;
209
210 glmesh skybox, skydome;
211 mdl_submesh dome_upper, dome_lower;
212
213 glmesh cars;
214 mdl_submesh car_holden;
215
216 /* Load time */
217
218 struct instance_cache
219 {
220 mdl_header *mdl;
221 u32 pstr_file;
222 }
223 * instance_cache;
224 u32 instance_cache_count,
225 instance_cache_cap;
226
227 v3f render_gate_pos;
228 int active_route_board;
229 }
230 world ;
231
232 /*
233 * API
234 */
235
236 static int ray_hit_is_ramp( ray_hit *hit );
237 static int ray_hit_is_terrain( ray_hit *hit );
238 static void ray_world_get_tri( ray_hit *hit, v3f tri[3] );
239 static int ray_world( v3f pos, v3f dir, ray_hit *hit );
240
241 /*
242 * Submodules
243 */
244 #include "world_routes.h"
245 #include "world_sfd.h"
246 #include "world_render.h"
247 #include "world_water.h"
248 #include "world_gen.h"
249 #include "world_gate.h"
250
251 /*
252 * -----------------------------------------------------------------------------
253 * Events
254 * -----------------------------------------------------------------------------
255 */
256
257 static void world_init(void)
258 {
259 world.sky_rate = 1.0;
260 world.sky_target_rate = 1.0;
261
262 shader_terrain_register();
263 shader_sky_register();
264 shader_planeinf_register();
265 shader_gpos_register();
266 shader_fscolour_register();
267 shader_alphatest_register();
268
269 vg_info( "Loading world resources\n" );
270
271 VG_REQUIRED_ASSET( mdl_header*, mcars, mdl_load, "models/rs_cars.mdl" );
272 VG_REQUIRED_ASSET( mdl_header*, msky, mdl_load, "models/rs_skydome.mdl" );
273
274 mdl_node *nholden = mdl_node_from_name( mcars, "holden" );
275 world.car_holden = *mdl_node_submesh( mcars, nholden, 0 );
276
277 mdl_node *nlower = mdl_node_from_name( msky, "dome_lower" ),
278 *nupper = mdl_node_from_name( msky, "dome_upper" );
279
280 world.dome_lower = *mdl_node_submesh( msky, nlower, 0 );
281 world.dome_upper = *mdl_node_submesh( msky, nupper, 0 );
282
283 vg_acquire_thread_sync();
284 {
285 mdl_unpack_glmesh( mcars, &world.cars );
286 mdl_unpack_glmesh( msky, &world.skydome );
287 }
288 vg_release_thread_sync();
289
290 vg_free(mcars);
291 vg_free(msky);
292
293 /* Other systems */
294 vg_info( "Loading other world systems\n" );
295
296 vg_loader_highwater( world_render_init, world_render_free, NULL );
297 vg_loader_highwater( world_sfd_init, world_sfd_free, NULL );
298 vg_loader_highwater( world_water_init, world_water_free, NULL );
299 vg_loader_highwater( world_gates_init, world_gates_free, NULL );
300 vg_loader_highwater( world_routes_init, world_routes_free, NULL );
301 }
302
303 static void world_free( void *_ )
304 {
305 mesh_free( &world.cars );
306 mesh_free( &world.skydome );
307 vg_free( world.achievement_zones );
308 }
309
310 static void world_update( v3f pos )
311 {
312 world.sky_time += world.sky_rate * vg.time_delta;
313 world.sky_rate = vg_lerp( world.sky_rate, world.sky_target_rate,
314 vg.time_delta * 10.0 );
315
316 world_routes_update();
317 #if 0
318 world_routes_debug();
319 #endif
320
321 int closest = 0;
322 float min_dist = INFINITY;
323
324 for( int i=0; i<world.routes.route_count; i++ )
325 {
326 float d = v3_dist2( world.routes.routes[i].scoreboard_transform[3], pos );
327
328 if( d < min_dist )
329 {
330 min_dist = d;
331 closest = i;
332 }
333 }
334
335 if( (world.active_route_board != closest) || network_scores_updated )
336 {
337 network_scores_updated = 0;
338 world.active_route_board = closest;
339 struct subworld_sfd *sfd = &world.sfd;
340
341 struct route *route = &world.routes.routes[closest];
342
343 u32 id = route->track_id;
344
345 if( id != 0xffffffff )
346 {
347 struct netmsg_board *local_board = &scoreboard_client_data.boards[id];
348
349 for( int i=0; i<13; i++ )
350 {
351 sfd_encode( &sfd->tester, i, &local_board->data[27*i] );
352 }
353 }
354 }
355
356 for( int i=0; i<world.achievement_zones_count; i++ )
357 {
358 struct achievement_zone *zone = &world.achievement_zones[i];
359
360 if( zone->triggered )
361 continue;
362
363 v3f local;
364 m4x3_mulv( zone->inv_transform, pos, local );
365
366 if( (fabsf(local[0]) <= 1.0f) &&
367 (fabsf(local[1]) <= 1.0f) &&
368 (fabsf(local[2]) <= 1.0f) )
369 {
370 zone->triggered = 1;
371 steam_set_achievement( zone->name );
372 steam_store_achievements();
373 }
374
375 vg_line_boxf_transformed( zone->transform, (boxf){{-1.0f,-1.0f,-1.0f},
376 { 1.0f, 1.0f, 1.0f}},
377 0xff00ff00 );
378 }
379
380 sfd_update( &world.sfd.tester );
381 }
382
383 /*
384 * -----------------------------------------------------------------------------
385 * API implementation
386 * -----------------------------------------------------------------------------
387 */
388
389 static void ray_world_get_tri( ray_hit *hit, v3f tri[3] )
390 {
391 for( int i=0; i<3; i++ )
392 v3_copy( world.geo.verts[ hit->tri[i] ].co, tri[i] );
393 }
394
395 static int ray_world( v3f pos, v3f dir, ray_hit *hit )
396 {
397 return scene_raycast( &world.geo, pos, dir, hit );
398 }
399
400 static int ray_hit_is_terrain( ray_hit *hit )
401 {
402 u32 valid_start = 0,
403 valid_end = world.sm_terrain.vertex_count;
404
405 return (hit->tri[0] >= valid_start) &&
406 (hit->tri[0] < valid_end);
407 }
408
409 static int ray_hit_is_ramp( ray_hit *hit )
410 {
411 u32 valid_start = world.sm_geo_std.vertex_start,
412 valid_end = world.sm_geo_vb.vertex_start;
413
414 return (hit->tri[0] >= valid_start) &&
415 (hit->tri[0] < valid_end);
416 }
417
418 #endif /* WORLD_H */