~starkingdoms/starkingdoms

ref: afeb7c8562239af02974fe4fa7cd9d5f962117c0 starkingdoms/server/src/timer.rs -rw-r--r-- 11.9 KiB
afeb7c85 — ghostlyzsh merged because oops 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use std::{time::Duration, sync::Arc, f64::consts::PI};
use log::{debug, warn};
use nalgebra::{vector, point};
use rand::Rng;
use rapier2d_f64::prelude::{PhysicsPipeline, ColliderBuilder, RigidBodyBuilder};
use async_std::sync::RwLock;
use async_std::task::sleep;
use starkingdoms_protocol::{player::Player, planet::PlanetType, module::ModuleType};
use crate::{manager::{ClientHandlerMessage, ClientManager, PhysicsData, Module}, SCALE, planet::{Planets, Planet}, entity::{get_entity_id, Entity}};
use crate::entity::EntityHandler;
use crate::orbit::constants::{GAME_ORBITS_ENABLED, MOON_APOAPSIS, MOON_ORBIT_TIME, MOON_PERIAPSIS};
use crate::orbit::orbit::{calculate_point_on_orbit, calculate_world_position_of_orbit};

pub const ROTATIONAL_FORCE: f64 = 100.0;
pub const LATERAL_FORCE: f64 = 100.0;
pub const MODULE_SPAWN: f64 = 1000.0;
pub const MODULE_MAX: u32 = 10;

pub async fn timer_main(mgr: ClientManager, physics_data_orig: Arc<RwLock<PhysicsData>>, entities: Arc<RwLock<EntityHandler>>) {
    let mut pipeline = PhysicsPipeline::new();

    let mut time = 0.0;
    let mut module_timer = 0.0;
    let _planet_ids;

    {
        let mut data_handle = physics_data_orig.write().await;

        let mut rigid_body_set = data_handle.rigid_body_set.clone();
        let mut collider_set = data_handle.collider_set.clone();

        _planet_ids = Planets::create_planets(&mut rigid_body_set, &mut collider_set, &mut entities.write().await.entities).await;

        data_handle.rigid_body_set = rigid_body_set;
        data_handle.collider_set = collider_set;
    }

    loop {
        sleep(Duration::from_millis(5)).await;
        time += 5.0 / 1000.0; // 5ms, time is in seconds
        module_timer += 5.0 / 1000.0;

        let mut physics_data = physics_data_orig.write().await;

        // update orbits (but dont, actually, yet)
        // DO NOT SIMPLIFY EXPRESSION
        // IT MAY ALWAYS BE TRUE
        // THATS FINE
        if GAME_ORBITS_ENABLED {
            let planets = entities.write().await;

            // update earth (nothing changes, yet)
            let earth = planets.get_planet(PlanetType::Earth).unwrap();
            let new_earth_position = vector![earth.position.0, earth.position.1];

            // update moon
            let moon: &mut Planet = &mut planets.get_planet(PlanetType::Moon).unwrap();
            let new_moon_position = calculate_world_position_of_orbit(calculate_point_on_orbit(MOON_PERIAPSIS, MOON_APOAPSIS, time / MOON_ORBIT_TIME), new_earth_position);
            let moon_body = physics_data.rigid_body_set.get_mut(moon.body_handle).unwrap();
            moon_body.set_next_kinematic_position(new_moon_position.into());
            moon.position = (moon_body.translation()[0] / SCALE, moon_body.translation()[1] / SCALE);
        }

        physics_data.tick(&mut pipeline);

        let mut protocol_players = vec![];

        {
            if module_timer > 1.0 && entities.read().await.get_module_count() < MODULE_MAX {
                debug!("module spawn");
                module_timer = 0.;

                let mut rigid_body_set = physics_data.rigid_body_set.clone();
                let mut collider_set = physics_data.collider_set.clone();

                let module_collider = ColliderBuilder::cuboid(25. / SCALE, 25. / SCALE);
                let angle: f64 = {
                    let mut rng = rand::thread_rng();
                    rng.gen::<f64>() * PI * 2.
                };
                let module_body = RigidBodyBuilder::dynamic()
                    .translation(vector![angle.cos() * 2050. / SCALE, angle.sin() * 2050.0/SCALE])
                    .build();
                let module_handler = rigid_body_set.insert(module_body);
                collider_set.insert_with_parent(module_collider, module_handler, &mut rigid_body_set);

                physics_data.rigid_body_set = rigid_body_set;
                physics_data.collider_set = collider_set;

                let module = Module {
                    handle: module_handler,
                    module_type: ModuleType::Cargo,
                };
                entities.write().await.entities.insert(get_entity_id(), Entity::Module(module));
            }
            for module in entities.read().await.get_modules().iter() {
                let module_handle = module.handle;
                let module_body = physics_data.rigid_body_set.get_mut(module_handle).unwrap();
                module_body.reset_forces(true);
                module_body.reset_torques(true);
                let planets = entities.read().await;
                let grav_force = planets.gravity((module_body.translation().x, module_body.translation().y), module_body.mass());
                module_body.apply_impulse(vector![grav_force.0, grav_force.1], true);
            }
        }

        {
            for (player_id, player) in entities.read().await.get_players().iter() {
                let player_handle = player.handle;
                let player_body = physics_data.rigid_body_set.get_mut(player_handle).unwrap();
                player_body.reset_forces(true);
                player_body.reset_torques(true);
                let planets = entities.read().await;
                let grav_force = planets.gravity((player_body.translation().x, player_body.translation().y), player_body.mass());
                player_body.apply_impulse(vector![grav_force.0, grav_force.1], true);

                let mut left_top_thruster: f64 = 0.0;
                let mut right_top_thruster: f64 = 0.0;
                let mut left_bottom_thruster: f64 = 0.0;
                let mut right_bottom_thruster: f64 = 0.0;

                if player.input.right {
                    left_top_thruster -= 1.0;
                    right_bottom_thruster += 1.0;
                }
                if player.input.left {
                    right_top_thruster -= 1.0;
                    left_bottom_thruster += 1.0;
                }

                if player.input.up {
                    left_bottom_thruster -= 1.0;
                    right_bottom_thruster -= 1.0;
                }
                if player.input.down {
                    left_top_thruster += 1.0;
                    right_top_thruster += 1.0;
                }
                left_top_thruster = LATERAL_FORCE * left_top_thruster.clamp(-1.0, 1.0);
                right_top_thruster = LATERAL_FORCE * right_top_thruster.clamp(-1.0, 1.0);
                left_bottom_thruster = LATERAL_FORCE * left_bottom_thruster.clamp(-1.0, 1.0);
                right_bottom_thruster = LATERAL_FORCE * right_bottom_thruster.clamp(-1.0, 1.0);

                let rotation = player_body.rotation().clone().angle();
                let left_top_thruster = vector![
                    -left_top_thruster * rotation.sin(),
                    left_top_thruster * rotation.cos()
                ];
                let right_top_thruster = vector![
                    -right_top_thruster * rotation.sin(),
                    right_top_thruster * rotation.cos()
                ];
                let left_bottom_thruster = vector![
                    -left_bottom_thruster * rotation.sin(),
                    left_bottom_thruster * rotation.cos()
                ];
                let right_bottom_thruster = vector![
                    -right_bottom_thruster * rotation.sin(),
                    right_bottom_thruster * rotation.cos()
                ];
                let scale = SCALE;
                let top_left_point = point![
                    -25. / scale * rotation.cos() +25. / scale * rotation.sin(), 
                    -25. / scale * rotation.sin() -25. / scale * rotation.cos()
                ] + player_body.translation();
                let top_right_point = point![
                     25. / scale * rotation.cos() +25. / scale * rotation.sin(), 
                     25. / scale * rotation.sin() -25. / scale * rotation.cos()
                ] + player_body.translation();
                let bottom_left_point = point![
                    -25. / scale * rotation.cos() -25. / scale * rotation.sin(), 
                    -25. / scale * rotation.sin() +25. / scale * rotation.cos()
                ] + player_body.translation();
                let bottom_right_point = point![
                     25. / scale * rotation.cos() -25. / scale * rotation.sin(), 
                     25. / scale * rotation.sin() +25. / scale * rotation.cos()
                ] + player_body.translation();

                player_body.add_force_at_point(
                    left_top_thruster,
                    top_left_point, true);
                player_body.add_force_at_point(
                    right_top_thruster,
                    top_right_point, true);
                player_body.add_force_at_point(
                    left_bottom_thruster,
                    bottom_left_point, true);
                player_body.add_force_at_point(
                    right_bottom_thruster,
                    bottom_right_point, true);


                let translation = player_body.translation();

                let username;
                {
                    let usernames = mgr.usernames.read().await;
                    username = usernames.get(player_id).unwrap().clone();
                }

                // TODO: Figure out how to adjust codegen to use f64
                protocol_players.push(Player {
                    rotation: rotation as f32,
                    x: (translation.x * SCALE) as f32,
                    y: (translation.y * SCALE) as f32,
                    username,
                    special_fields: Default::default(),
                });
            }
        }

        let mut to_remove = vec![];

        let mut mgr_w = mgr.handlers.write().await;
        let mgr_r = mgr_w.clone();

        for (addr, client_thread) in mgr_r.iter() {
            match client_thread.tx.send(ClientHandlerMessage::Tick).await {
                Ok(_) => {
                    match client_thread.tx.send(ClientHandlerMessage::PlayersUpdate {players: protocol_players.clone()}).await {
                        Ok(_) => (),
                        Err(e) => {
                            warn!("unable to send position packet: {}", e);
                        }
                    };
                    let modules = entities.read().await.get_modules();
                    let protocol_modules: Vec<starkingdoms_protocol::module::Module> = modules.iter()
                        .map(|module| {
                            let body = physics_data.rigid_body_set.get(module.handle).unwrap();
                            return starkingdoms_protocol::module::Module {
                                module_type: module.module_type.into(),
                                rotation: body.rotation().angle() as f32,
                                x: (body.translation().x * SCALE) as f32,
                                y: (body.translation().y * SCALE) as f32,
                                special_fields: Default::default(),
                            };
                        }).collect();
                    match client_thread.tx.send(ClientHandlerMessage::ModulesUpdate { modules: protocol_modules.clone() }).await {
                        Ok(_) => (),
                        Err(e) => {
                            warn!("unable to send module position packet: {}", e);
                        }
                    };

                    let world = entities.read().await;
                    let planet_data = world.to_protocol();
                    match client_thread.tx.send(planet_data).await {
                        Ok(_) => (),
                        Err(e) => {
                            warn!("unable to send earth packet: {}", e);
                        }
                    };
                }
                Err(e) => {
                    warn!("unable to update a client thread: {}", e);
                    to_remove.push(addr);
                }
            }
        }

        for pending_removal in to_remove {
            mgr_w.remove(pending_removal);
        }
    }
}