9ff62bcb3df1b3471462542c21cda5ea53b82d51
[carveJwlIkooP6JGAAIwe30JlM.git] / world.c
1 /*
2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef WORLD_C
6 #define WORLD_C
7
8 #include "world.h"
9 #include "network.h"
10 #include "vg/vg_loader.h"
11 #include "vg/vg_mem.h"
12
13 static world_instance *world_current_instance(void){
14 return &world_static.instances[ world_static.active_instance ];
15 }
16
17 static void world_init(void)
18 {
19 vg_loader_step( world_render_init, NULL );
20 vg_loader_step( world_sfd_init, NULL );
21 vg_loader_step( world_water_init, NULL );
22 vg_loader_step( world_gates_init, NULL );
23 vg_loader_step( world_routes_init, NULL );
24
25 /* Allocate dynamic world memory arena */
26 u32 max_size = 76*1024*1024;
27 world_static.heap = vg_create_linear_allocator( vg_mem.rtmemory, max_size,
28 VG_MEMORY_SYSTEM );
29 }
30
31 static void world_switch_instance( u32 index ){
32 localplayer.subsystem = k_player_subsystem_walk;
33
34 if( index >= vg_list_size(world_static.instances) ){
35 vg_error( "Instance ID out of range (%u)\n", index );
36 return;
37 }
38
39 world_instance *new = &world_static.instances[ index ];
40
41 if( new->status != k_world_status_loaded ){
42 vg_error( "Instance is not loaded (%u)\n", index );
43 return;
44 }
45
46 if( skaterift.demo_mode ){
47 if( world_static.instance_addons[index]->flags & ADDON_REG_PREMIUM ){
48 vg_error( "Can't switch to a premium world in the demo version\n" );
49 return;
50 }
51 }
52
53 world_instance *current =
54 &world_static.instances[ world_static.active_instance ];
55
56 if( index != world_static.active_instance ){
57 v3_copy( localplayer.rb.co, current->player_co );
58 skaterift_autosave(1);
59 }
60
61 v3_copy( new->player_co, localplayer.rb.co );
62
63 world_static.active_instance = index;
64 player__reset();
65 }
66
67 static int skaterift_switch_instance_cmd( int argc, const char *argv[] ){
68 if( argc )
69 world_switch_instance( atoi(argv[0]) );
70 else
71 vg_info( "switch_active_instance <id>\n" );
72 return 0;
73 }
74
75 static void skaterift_world_get_save_path( enum world_purpose which,
76 char buf[128] ){
77 addon_reg *reg = world_static.instance_addons[ which ];
78
79 if( !reg )
80 vg_fatal_error( "Looking up addon for world without one\n" );
81
82 char id[76];
83 addon_alias_uid( &reg->alias, id );
84 snprintf( buf, 128, "savedata/%s.bkv", id );
85 }
86
87 #include "world_entity.c"
88 #include "world_gate.c"
89 #include "world_gen.c"
90 #include "world_load.c"
91 #include "world_physics.c"
92 #include "world_render.c"
93 #include "world_sfd.c"
94 #include "world_volumes.c"
95 #include "world_water.c"
96 #include "world_audio.c"
97 #include "world_routes.c"
98
99 static void world_update( world_instance *world, v3f pos ){
100 world_render.sky_time += world_render.sky_rate * vg.time_delta;
101 world_render.sky_rate = vg_lerp( world_render.sky_rate,
102 world_render.sky_target_rate,
103 vg.time_delta * 5.0 );
104
105 world_routes_update_timer_texts( world );
106 world_routes_update( world );
107 ent_traffic_update( world, pos );
108 world_sfd_update( world, pos );
109 world_volumes_update( world, pos );
110 }
111
112 #endif /* WORLD_C */