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