achievements and stuff
[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 last_interaction;
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 /* Physics */
198
199 /* Rendering & geometry */
200 scene geo, foliage;
201 rigidbody rb_geo;
202
203 /* TODO Maybe make this less hardcoded */
204 mdl_submesh sm_geo_std_oob, sm_geo_std, sm_geo_vb,
205 sm_foliage_main, sm_foliage_alphatest,
206 sm_graffiti, sm_subworld, sm_terrain;
207
208 glmesh skybox, skydome;
209 mdl_submesh dome_upper, dome_lower;
210
211 glmesh cars;
212 mdl_submesh car_holden;
213
214 /* Load time */
215
216 struct instance_cache
217 {
218 mdl_header *mdl;
219 u32 pstr_file;
220 }
221 * instance_cache;
222 u32 instance_cache_count,
223 instance_cache_cap;
224
225 v3f render_gate_pos;
226 int active_route_board;
227 }
228 world;
229
230 /*
231 * API
232 */
233
234 static int ray_hit_is_ramp( ray_hit *hit );
235 static int ray_hit_is_terrain( ray_hit *hit );
236 static void ray_world_get_tri( ray_hit *hit, v3f tri[3] );
237 static int ray_world( v3f pos, v3f dir, ray_hit *hit );
238
239 /*
240 * Submodules
241 */
242 #include "world_routes.h"
243 #include "world_sfd.h"
244 #include "world_render.h"
245 #include "world_water.h"
246 #include "world_gen.h"
247 #include "world_gate.h"
248
249 /*
250 * -----------------------------------------------------------------------------
251 * Events
252 * -----------------------------------------------------------------------------
253 */
254
255 static void world_init(void)
256 {
257 shader_terrain_register();
258 shader_sky_register();
259 shader_planeinf_register();
260 shader_gpos_register();
261 shader_fscolour_register();
262 shader_alphatest_register();
263
264 vg_info( "Loading world resources\n" );
265
266 VG_REQUIRED_ASSET( mdl_header*, mcars, mdl_load, "models/rs_cars.mdl" );
267 VG_REQUIRED_ASSET( mdl_header*, msky, mdl_load, "models/rs_skydome.mdl" );
268
269 mdl_node *nholden = mdl_node_from_name( mcars, "holden" );
270 world.car_holden = *mdl_node_submesh( mcars, nholden, 0 );
271
272 mdl_node *nlower = mdl_node_from_name( msky, "dome_lower" ),
273 *nupper = mdl_node_from_name( msky, "dome_upper" );
274
275 world.dome_lower = *mdl_node_submesh( msky, nlower, 0 );
276 world.dome_upper = *mdl_node_submesh( msky, nupper, 0 );
277
278 vg_acquire_thread_sync();
279 {
280 mdl_unpack_glmesh( mcars, &world.cars );
281 mdl_unpack_glmesh( msky, &world.skydome );
282 }
283 vg_release_thread_sync();
284
285 vg_free(mcars);
286 vg_free(msky);
287
288 /* Other systems */
289 vg_info( "Loading other world systems\n" );
290
291 vg_loader_highwater( world_render_init, world_render_free, NULL );
292 vg_loader_highwater( world_sfd_init, world_sfd_free, NULL );
293 vg_loader_highwater( world_water_init, world_water_free, NULL );
294 vg_loader_highwater( world_gates_init, world_gates_free, NULL );
295 vg_loader_highwater( world_routes_init, world_routes_free, NULL );
296 }
297
298 static void world_free( void *_ )
299 {
300 mesh_free( &world.cars );
301 mesh_free( &world.skydome );
302 vg_free( world.achievement_zones );
303 }
304
305 static void world_update( v3f pos )
306 {
307 world_routes_update();
308 #if 0
309 world_routes_debug();
310 #endif
311
312 int closest = 0;
313 float min_dist = INFINITY;
314
315 for( int i=0; i<world.routes.route_count; i++ )
316 {
317 float d = v3_dist2( world.routes.routes[i].scoreboard_transform[3], pos );
318
319 if( d < min_dist )
320 {
321 min_dist = d;
322 closest = i;
323 }
324 }
325
326 if( (world.active_route_board != closest) || network_scores_updated )
327 {
328 network_scores_updated = 0;
329 world.active_route_board = closest;
330 struct subworld_sfd *sfd = &world.sfd;
331
332 struct route *route = &world.routes.routes[closest];
333
334 u32 id = route->track_id;
335
336 if( id != 0xffffffff )
337 {
338 struct netmsg_board *local_board = &scoreboard_client_data.boards[id];
339
340 for( int i=0; i<13; i++ )
341 {
342 sfd_encode( &sfd->tester, i, &local_board->data[27*i] );
343 }
344 }
345 }
346
347 for( int i=0; i<world.achievement_zones_count; i++ )
348 {
349 struct achievement_zone *zone = &world.achievement_zones[i];
350
351 if( zone->triggered )
352 continue;
353
354 v3f local;
355 m4x3_mulv( zone->inv_transform, pos, local );
356
357 if( (fabsf(local[0]) <= 1.0f) &&
358 (fabsf(local[1]) <= 1.0f) &&
359 (fabsf(local[2]) <= 1.0f) )
360 {
361 zone->triggered = 1;
362 steam_set_achievement( zone->name );
363 steam_store_achievements();
364 }
365
366 vg_line_boxf_transformed( zone->transform, (boxf){{-1.0f,-1.0f,-1.0f},
367 { 1.0f, 1.0f, 1.0f}},
368 0xff00ff00 );
369 }
370
371 sfd_update( &world.sfd.tester );
372 }
373
374 /*
375 * -----------------------------------------------------------------------------
376 * API implementation
377 * -----------------------------------------------------------------------------
378 */
379
380 static void ray_world_get_tri( ray_hit *hit, v3f tri[3] )
381 {
382 for( int i=0; i<3; i++ )
383 v3_copy( world.geo.verts[ hit->tri[i] ].co, tri[i] );
384 }
385
386 static int ray_world( v3f pos, v3f dir, ray_hit *hit )
387 {
388 return scene_raycast( &world.geo, pos, dir, hit );
389 }
390
391 static int ray_hit_is_terrain( ray_hit *hit )
392 {
393 u32 valid_start = 0,
394 valid_end = world.sm_terrain.vertex_count;
395
396 return (hit->tri[0] >= valid_start) &&
397 (hit->tri[0] < valid_end);
398 }
399
400 static int ray_hit_is_ramp( ray_hit *hit )
401 {
402 u32 valid_start = world.sm_geo_std.vertex_start,
403 valid_end = world.sm_geo_vb.vertex_start;
404
405 return (hit->tri[0] >= valid_start) &&
406 (hit->tri[0] < valid_end);
407 }
408
409 #endif /* WORLD_H */