process triggers on leave world
[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
11 static world_instance *world_current_instance(void){
12 return &world_static.instances[ world_static.active_instance ];
13 }
14
15 static void world_init(void)
16 {
17 vg_loader_step( world_render_init, NULL );
18 vg_loader_step( world_sfd_init, NULL );
19 vg_loader_step( world_water_init, NULL );
20 vg_loader_step( world_gates_init, NULL );
21 vg_loader_step( world_routes_init, NULL );
22
23 /* Allocate dynamic world memory arena */
24 u32 max_size = 76*1024*1024;
25 world_static.heap = vg_create_linear_allocator( vg_mem.rtmemory, max_size,
26 VG_MEMORY_SYSTEM );
27 }
28
29 static void world_switch_instance( u32 index ){
30 assert( localplayer.subsystem == k_player_subsystem_walk );
31
32 if( index >= vg_list_size(world_static.instances) ){
33 vg_error( "Instance ID out of range (%u)\n", index );
34 return;
35 }
36
37 world_instance *new = &world_static.instances[ index ];
38
39 if( new->status != k_world_status_loaded ){
40 vg_error( "Instance is not loaded (%u)\n", index );
41 return;
42 }
43
44 world_instance *current =
45 &world_static.instances[ world_static.active_instance ];
46
47 if( index != world_static.active_instance ){
48 v3_copy( localplayer.rb.co, current->player_co );
49 v3_copy( localplayer.angles, current->player_angles );
50 v3_copy( localplayer.cam.pos, current->cam_co );
51 current->player_angles[3] = player_get_heading_yaw();
52 }
53
54 v3_copy( new->player_co, localplayer.rb.co );
55 v3_copy( new->player_angles, localplayer.angles );
56 v3_copy( new->cam_co, localplayer.cam.pos );
57 q_axis_angle( localplayer.rb.q, (v3f){0,1,0}, new->player_angles[3] );
58
59 /* run exit events on triggers */
60 for( u32 i=0; i<world_static.active_trigger_volume_count; i++ ){
61 i32 idx = world_static.active_trigger_volumes[i];
62 ent_volume *volume = mdl_arritm( &current->ent_volume, idx );
63
64 ent_call basecall;
65 basecall.function = k_ent_function_trigger_leave;
66 basecall.id = mdl_entity_id( k_ent_volume, idx );
67 basecall.data = NULL;
68 entity_call( current, &basecall );
69 }
70
71 world_static.active_instance = index;
72
73 player__reset();
74 }
75
76 static int skaterift_switch_instance_cmd( int argc, const char *argv[] ){
77 if( argc )
78 world_switch_instance( atoi(argv[0]) );
79 else
80 vg_info( "switch_active_instance <id>\n" );
81 return 0;
82 }
83
84 static void skaterift_world_get_save_path( enum world_purpose which,
85 char buf[128] ){
86 addon_reg *reg = world_static.instance_addons[ which ];
87 assert( reg );
88
89 char id[76];
90 addon_alias_uid( &reg->alias, id );
91 snprintf( buf, 128, "savedata/%s.bkv", id );
92 }
93
94 #include "world_entity.c"
95 #include "world_gate.c"
96 #include "world_gen.c"
97 #include "world_load.c"
98 #include "world_physics.c"
99 #include "world_render.c"
100 #include "world_sfd.c"
101 #include "world_volumes.c"
102 #include "world_water.c"
103 #include "world_audio.c"
104 #include "world_routes.c"
105 #include "world_traffic.c"
106
107 static void world_update( world_instance *world, v3f pos ){
108 world_render.sky_time += world_render.sky_rate * vg.time_delta;
109 world_render.sky_rate = vg_lerp( world_render.sky_rate,
110 world_render.sky_target_rate,
111 vg.time_delta * 5.0 );
112
113 world_routes_update_timer_texts( world );
114 world_routes_update( world );
115 world_traffic_update( world, pos );
116 world_sfd_update( world, pos );
117 world_volumes_update( world, pos );
118 }
119
120 #endif /* WORLD_C */