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