-#include "common.h"
-
-static int ray_world( v3f pos, v3f dir, ray_hit *hit );
+/*
+ * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
+ */
#ifndef WORLD_H
#define WORLD_H
-#include "network.h"
-#include "network_msg.h"
-#include "scene.h"
-#include "terrain.h"
#include "render.h"
-#include "rigidbody.h"
-#include "gate.h"
-#include "bvh.h"
-#include "lighting.h"
-#include "model.h"
-
-#include "traffic.h" /*TODO: -> world_traffic.h */
-
-#include "shaders/terrain.h"
-#include "shaders/sky.h"
-#include "shaders/planeinf.h"
-#include "shaders/standard.h"
-#include "shaders/vblend.h"
-#include "shaders/gpos.h"
-#include "shaders/fscolour.h"
-#include "shaders/alphatest.h"
-
-enum { k_max_ui_segments = 32 };
-enum { k_route_ui_max_verts = 2000 };
-enum { k_route_ui_max_indices = 3000 };
-
-static struct gworld
-{
- /* gameplay */
- struct respawn_point
- {
- v3f co;
- v4f q;
- char name[32];
- }
- spawns[32];
- u32 spawn_count;
-
- struct subworld_routes
- {
- struct route_node
- {
- v3f co, right, up, h;
- u32 next[2];
-
- u32 special_type, special_id, current_refs, ref_count;
- u32 route_ids[4]; /* Gates can be linked into up to four routes */
- }
- *nodes;
-
- u32 node_count,
- node_cap;
-
- struct route
- {
- u32 track_id;
- v4f colour;
-
- u32 start;
- mdl_submesh sm;
-
- int active;
- float factive;
-
- double best_lap, latest_pass; /* Session */
-
- struct
- {
- GLuint vao, vbo, ebo;
-
- u32 indices_head;
- u32 vertex_head;
-
- float last_notch;
-
- struct route_ui_segment
- {
- float length;
- u32 vertex_start, vertex_count,
- index_start, index_count;
- }
- segments[k_max_ui_segments];
-
- u32 segment_start, segment_count, fade_start, fade_count;
- double fade_timer_start;
- float xpos;
- }
- ui;
-
- m4x3f scoreboard_transform;
- }
- *routes;
-
- u32 route_count,
- route_cap;
-
- struct route_gate
- {
- teleport_gate gate;
-
- u32 node_id;
-
- struct route_timing
- {
- u32 version; /* Incremented on every teleport */
- double time;
- }
- timing;
- }
- *gates;
-
- struct route_collector
- {
- struct route_timing timing;
- }
- *collectors;
-
- u32 gate_count,
- gate_cap,
- collector_count,
- collector_cap;
-
- u32 active_gate,
- current_run_version;
-
- scene scene_lines;
- }
- routes;
-
- struct subworld_sfd
- {
- scene mesh;
- mdl_submesh *sm_module, *sm_card;
- glmesh temp;
-
- struct sfd_instance
- {
- float *buffer;
-
- u32 w,h;
- }
- tester;
- }
- sfd;
-
- /* Paths */
- traffic_node traffic[128];
- u32 traffic_count;
-
-#if 0
- traffic_driver van_man[6];
-#endif
-
- /* Physics */
-
- /* Rendering & geometry */
- scene geo, foliage;
- rigidbody rb_geo;
-
- /* TODO Maybe make this less hardcoded */
- mdl_submesh sm_geo_std_oob, sm_geo_std, sm_geo_vb,
- sm_foliage_main, sm_foliage_alphatest,
- sm_graffiti, sm_subworld, sm_terrain;
-
- glmesh skybox, skydome;
- mdl_submesh dome_upper, dome_lower;
-
- glmesh cars;
- mdl_submesh car_holden;
-
- rigidbody mr_ball;
-
- /* Load time */
-
- struct instance_cache
- {
- mdl_header *mdl;
- u32 pstr_file;
- }
- * instance_cache;
- u32 instance_cache_count,
- instance_cache_cap;
-
- v3f render_gate_pos;
- int active_route_board;
-}
-world;
+#include "network_msg.h"
-/*
- * API
+/* types
*/
-static int ray_hit_is_ramp( ray_hit *hit );
-static int ray_hit_is_terrain( ray_hit *hit );
-static void ray_world_get_tri( ray_hit *hit, v3f tri[3] );
-static int ray_world( v3f pos, v3f dir, ray_hit *hit );
-
-/*
- * Submodules
- */
-#include "world_routes.h"
-#include "world_sfd.h"
-#include "world_audio.h"
+enum world_geo_type{
+ k_world_geo_type_solid = 0,
+ k_world_geo_type_nonsolid = 1,
+ k_world_geo_type_water = 2
+};
+
+enum world_purpose{
+ k_world_purpose_invalid = -1,
+ k_world_purpose_hub = 0,
+ k_world_purpose_client = 1,
+ k_world_max
+};
+
+struct leaderboard_cache {
+ enum request_status status;
+ f64 cache_time;
+ u8 *data;
+ u32 data_len;
+};
+
+typedef struct world_instance world_instance;
+
+static void skaterift_world_get_save_path( enum world_purpose which,
+ char buf[128] );
+
+/* submodule headers */
+#include "world_entity.h"
+#include "world_gate.h"
+#include "world_gen.h"
+#include "world_info.h"
+#include "world_load.h"
+#include "world_physics.h"
#include "world_render.h"
+#include "world_sfd.h"
+#include "world_volumes.h"
#include "world_water.h"
-#include "world_gen.h"
-
-/*
- * -----------------------------------------------------------------------------
- * Events
- * -----------------------------------------------------------------------------
- */
-static void world_register(void)
-{
- shader_terrain_register();
- shader_sky_register();
- shader_planeinf_register();
- shader_gpos_register();
- shader_fscolour_register();
- shader_alphatest_register();
-
- world_routes_register();
- world_sfd_register();
- world_water_register();
-}
-
-static void world_free(void)
-{
- /* TODO.. */
-
- world_sfd_free();
-}
-
+#include "world_audio.h"
+#include "world_routes.h"
-static void world_init(void)
-{
- mdl_header *mcars = mdl_load( "models/rs_cars.mdl" );
- mdl_unpack_glmesh( mcars, &world.cars );
- mdl_node *nholden = mdl_node_from_name( mcars, "holden" );
- world.car_holden = *mdl_node_submesh( mcars, nholden, 0 );
- free(mcars);
+/* console variables */
+static f32 k_day_length = 30.0f; /* minutes */
+static i32 k_debug_light_indices = 0,
+ k_debug_light_complexity= 0,
+ k_light_preview = 0;
- mdl_header *msky = mdl_load("models/rs_skydome.mdl");
- mdl_unpack_glmesh( msky, &world.skydome );
- mdl_node *nlower = mdl_node_from_name( msky, "dome_lower" ),
- *nupper = mdl_node_from_name( msky, "dome_upper" );
-
- world.dome_lower = *mdl_node_submesh( msky, nlower, 0 );
- world.dome_upper = *mdl_node_submesh( msky, nupper, 0 );
- free(msky);
+struct world_instance {
+ /* Fixed items
+ * -------------------------------------------------------
+ */
+ v4f player_co;
- /* Other systems */
- world_render_init();
- world_sfd_init();
- world_audio_init();
-}
-
-static void world_update( v3f pos )
-{
- world_routes_update();
- world_routes_debug();
-
- int closest = 0;
- float min_dist = INFINITY;
-
- for( int i=0; i<world.routes.route_count; i++ )
- {
- float d = v3_dist2( world.routes.routes[i].scoreboard_transform[3], pos );
-
- if( d < min_dist )
- {
- min_dist = d;
- closest = i;
- }
+ void *heap;
+ enum world_status{
+ k_world_status_unloaded = 0,
+ k_world_status_loading = 1,
+ k_world_status_loaded = 2,
+ k_world_status_unloading = 3 /* dont spawn sounds and stuff */
}
+ status;
- if( (world.active_route_board != closest) || network_scores_updated )
- {
- network_scores_updated = 0;
- world.active_route_board = closest;
- struct subworld_sfd *sfd = &world.sfd;
-
- struct route *route = &world.routes.routes[closest];
-
- u32 id = route->track_id;
+ struct{
+ boxf depthbounds;
+ int depth_computed;
- if( id != 0xffffffff )
- {
- struct netmsg_board *local_board = &scoreboard_client_data.boards[id];
-
- for( int i=0; i<13; i++ )
- {
- sfd_encode( &sfd->tester, i, &local_board->data[27*i] );
- }
- }
+ float height;
+ int enabled;
+ v4f plane;
}
-
- sfd_update( &world.sfd.tester );
-
-#if 0
- rb_solver_reset();
- rb_build_manifold_terrain_sphere( &world.mr_ball );
-
- for( int i=0; i<5; i++ )
- rb_solve_contacts( rb_contact_buffer, rb_contact_count );
-
- rb_iter( &world.mr_ball );
- rb_update_transform( &world.mr_ball );
- rb_debug( &world.mr_ball, 0 );
-
- for( int i=0; i<vg_list_size(world.van_man); i++ )
- {
- traffic_drive( &world.van_man[i] );
- traffic_visualize_car( &world.van_man[i] );
+ water;
+
+ f64 time;
+
+ /* STD140 */
+ struct ub_world_lighting{
+ v4f g_cube_min,
+ g_cube_inv_range;
+
+ v4f g_water_plane,
+ g_depth_bounds;
+
+ v4f g_daysky_colour;
+ v4f g_nightsky_colour;
+ v4f g_sunset_colour;
+ v4f g_ambient_colour;
+ v4f g_sunset_ambient;
+ v4f g_sun_colour;
+ v4f g_sun_dir;
+ v4f g_board_0;
+ v4f g_board_1;
+
+ float g_water_fog;
+ float g_time;
+ float g_realtime;
+ float g_shadow_length;
+ float g_shadow_spread;
+
+ float g_time_of_day;
+ float g_day_phase;
+ float g_sunset_phase;
+
+ int g_light_preview;
+ int g_shadow_samples;
+
+ int g_debug_indices;
+ int g_debug_complexity;
}
-#endif
-}
-
-/*
- * -----------------------------------------------------------------------------
- * API implementation
- * -----------------------------------------------------------------------------
- */
-
-static void ray_world_get_tri( ray_hit *hit, v3f tri[3] )
-{
- for( int i=0; i<3; i++ )
- v3_copy( world.geo.verts[ hit->tri[i] ].co, tri[i] );
-}
-
-static int ray_world( v3f pos, v3f dir, ray_hit *hit )
-{
- return scene_raycast( &world.geo, pos, dir, hit );
-}
-
-static int ray_hit_is_terrain( ray_hit *hit )
-{
- u32 valid_start = 0,
- valid_end = world.sm_terrain.vertex_count;
-
- return (hit->tri[0] >= valid_start) &&
- (hit->tri[0] < valid_end);
+ ub_lighting;
+ GLuint ubo_lighting;
+ int ubo_bind_point;
+
+ GLuint tbo_light_entities,
+ tex_light_entities,
+ tex_light_cubes;
+
+ float probabilities[3];
+
+ v3i light_cubes;
+ struct framebuffer heightmap;
+
+ /*
+ * Dynamically allocated when world_load is called.
+ *
+ * the following arrays index somewhere into this linear
+ * allocator
+ * --------------------------------------------------------------------------
+ */
+
+ /*
+ * Main world .mdl
+ */
+ mdl_context meta;
+
+ GLuint *textures;
+ u32 texture_count;
+
+ struct world_surface{
+ mdl_material info;
+ mdl_submesh sm_geo,
+ sm_no_collide;
+ }
+ * surfaces;
+ u32 surface_count;
+
+ ent_worldinfo info;
+ mdl_array_ptr ent_spawn,
+ ent_gate,
+ ent_light,
+ ent_route_node,
+ ent_path_index,
+ ent_checkpoint,
+ ent_route,
+ ent_water,
+
+ ent_audio_clip,
+ ent_audio,
+ ent_volume,
+ ent_traffic,
+ ent_skateshop,
+ ent_marker,
+ ent_camera,
+ ent_swspreview,
+ ent_ccmd,
+ ent_objective,
+ ent_challenge,
+ ent_relay,
+ ent_cubemap,
+ ent_miniworld,
+ ent_prop;
+
+ enum skybox {
+ k_skybox_default,
+ k_skybox_space
+ } skybox;
+
+ ent_gate *rendering_gate;
+
+ /* logic
+ * ----------------------------------------------------
+ */
+
+ /* world geometry */
+ scene_context scene_geo,
+ scene_no_collide,
+ scene_lines;
+
+ /* spacial mappings */
+ bh_tree *geo_bh,
+ *entity_bh;
+ u32 *entity_list;
+
+ /* graphics */
+ glmesh mesh_route_lines;
+ glmesh mesh_geo,
+ mesh_no_collide,
+ mesh_water;
+ u32 cubemap_cooldown, cubemap_side;
+
+ rb_object rb_geo;
+
+ /* leaderboards */
+ struct leaderboard_cache *leaderboard_cache;
+};
+
+struct world_static {
+ /*
+ * Allocated as system memory
+ * --------------------------------------------------------------------------
+ */
+ void *heap;
+
+ u32 current_run_version;
+ double time, rewind_from, rewind_to, last_use;
+
+ u32 active_trigger_volumes[8];
+ u32 active_trigger_volume_count;
+
+ addon_reg *instance_addons[ k_world_max ];
+ world_instance instances[ k_world_max ];
+
+ enum world_purpose active_instance;
+ u32 focused_entity; /* like skateshop, challenge.. */
+ f32 focus_strength;
+ camera focus_cam;
+
+ /* challenges */
+ ent_objective *challenge_target;
+ f32 challenge_timer;
+
+ enum world_loader_state{
+ k_world_loader_none,
+ k_world_loader_preload,
+ k_world_loader_load
+ }
+ load_state;
}
+static world_static;
-static int ray_hit_is_ramp( ray_hit *hit )
-{
- u32 valid_start = world.sm_geo_std.vertex_start,
- valid_end = world.sm_geo_vb.vertex_start;
-
- return (hit->tri[0] >= valid_start) &&
- (hit->tri[0] < valid_end);
-}
+static void world_init(void);
+static world_instance *world_current_instance(void);
+static void world_switch_instance( u32 index );
#endif /* WORLD_H */