summaryrefslogtreecommitdiff
path: root/src/mass.rs
blob: 7cc7d7038a2fdf889e327f12eba00795e29efb73 (plain)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
extern crate rand;

use self::rand::distributions::Uniform;
use self::rand::Rng;
use std::collections::HashMap;

use crate::constants;
use crate::item::{Item, ItemType};
use crate::math::Vector;
use crate::modules::construction::Construction;
use crate::modules::dashboard::Dashboard;
use crate::modules::engines::Engines;
use crate::modules::mining::Mining;
use crate::modules::navigation::Navigation;
use crate::modules::refinery::Refinery;
use crate::modules::tractorbeam::Tractorbeam;
use crate::modules::types::ModuleType;
use crate::storage::Storage;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Mass {
    pub mass_type: MassType,
    pub position: Vector,
    pub velocity: Vector,
    pub effects: Effects,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Effects {
    acceleration: Vector,
}

impl Effects {
    pub fn new() -> Effects {
        Effects {
            acceleration: Vector::default(),
        }
    }

    pub fn give_acceleration(&mut self, acceleration: Vector) {
        self.acceleration += acceleration;
    }

    pub fn take_acceleration(&mut self) -> Vector {
        let acceleration = self.acceleration.clone();
        self.acceleration = Vector::default();
        acceleration
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum MassType {
    Ship {
        storage: Storage,
        mining: Mining,
        engines: Engines,
        refinery: Refinery,
        dashboard: Dashboard,
        navigation: Navigation,
        tractorbeam: Tractorbeam,
        construction: Construction,
    },
    Astroid {
        resources: Storage,
    },
    Item {
        item: Item,
    },
    Station {
        module_type: ModuleType,
    },
}

impl Mass {
    pub fn new_astroid() -> Mass {
        let mut rng = rand::thread_rng();

        let p_range = Uniform::new(
            constants::ASTROID_STARTING_POSITION_MAX * -1.0,
            constants::ASTROID_STARTING_POSITION_MAX,
        );
        let v_range = Uniform::new(
            constants::ASTROID_STARTING_VELOCITY_MAX * -1.0,
            constants::ASTROID_STARTING_VELOCITY_MAX,
        );

        let mut resources = Vec::new();
        for _ in 0..rng.gen_range(
            constants::ASTROID_STARTING_MINERALS_MIN,
            constants::ASTROID_STARTING_MINERALS_MAX,
        ) {
            resources.push(Item::new(ItemType::CrudeMinerals));
        }

        let astroid = MassType::Astroid {
            resources: Storage::new(resources, constants::ASTROID_STORAGE_CAPACITY),
        };

        Mass {
            mass_type: astroid,
            position: Vector::new((
                rng.sample(p_range),
                rng.sample(p_range),
                rng.sample(p_range),
            )),
            velocity: Vector::new((
                rng.sample(v_range),
                rng.sample(v_range),
                rng.sample(v_range),
            )),
            effects: Effects::new(),
        }
    }

    pub fn new_ship() -> Mass {
        let ship = MassType::Ship {
            mining: Mining::new(),
            engines: Engines::new(),
            refinery: Refinery::new(),
            dashboard: Dashboard::new(),
            navigation: Navigation::new(),
            tractorbeam: Tractorbeam::new(),
            construction: Construction::new(),
            storage: Storage::new(Vec::new(), constants::SHIP_STORAGE_CAPACITY),
        };

        Mass {
            mass_type: ship,
            position: Vector::default(),
            velocity: Vector::default(),
            effects: Effects::new(),
        }
    }

    pub fn new_item(item: Item, position: Vector, velocity: Vector) -> Mass {
        Mass {
            mass_type: MassType::Item { item },
            position,
            velocity,
            effects: Effects::new(),
        }
    }

    pub fn new_station(module_type: ModuleType, position: Vector, velocity: Vector) -> Mass {
        let mass_type = MassType::Station { module_type };

        Mass {
            mass_type,
            position,
            velocity,
            effects: Effects::new(),
        }
    }

    pub fn get_modules(&self) -> Vec<ModuleType> {
        let mut modules = Vec::new();
        modules.push(ModuleType::Mining);
        modules.push(ModuleType::Engines);
        modules.push(ModuleType::Refinery);
        modules.push(ModuleType::Dashboard);
        modules.push(ModuleType::Navigation);
        modules.push(ModuleType::Tractorbeam);
        modules.push(ModuleType::Construction);
        modules
    }

    pub fn process(&mut self, masses: &mut HashMap<String, Mass>) {
        if let MassType::Ship {
            ref mut navigation,
            ref mut engines,
            ref mut mining,
            ref mut refinery,
            ref mut construction,
            ref mut storage,
            ref mut tractorbeam,
            ..
        } = self.mass_type
        {
            if let Some(target_name) = &navigation.target_name {
                let mut target = masses.remove(target_name).unwrap();
                mining.process(self.position.clone(), masses, &mut target, storage);
                tractorbeam.process();
                let acceleration =
                    tractorbeam.get_acceleration(self.position.clone(), target.position.clone());
                target.effects.give_acceleration(acceleration);
                masses.insert(target_name.to_string(), target);
            }

            refinery.process(storage);
            navigation.process(self.position.clone(), masses);
            construction.process(
                self.velocity.clone(),
                self.position.clone(),
                masses,
                storage,
            );
            engines.process(self.velocity.clone());
            self.effects.give_acceleration(engines.take_acceleration());
        }

        self.velocity += self.effects.take_acceleration();
        self.position += self.velocity.clone();
    }

    pub fn get_client_data(
        &self,
        module_type: ModuleType,
        masses: &HashMap<String, Mass>,
    ) -> String {
        if let MassType::Ship {
            ref navigation,
            ref engines,
            ref mining,
            ref refinery,
            ref construction,
            ref storage,
            ref tractorbeam,
            ..
        } = self.mass_type
        {
            let target = match &navigation.target_name {
                Some(target_name) => masses.get(target_name),
                None => None,
            };
            match module_type {
                ModuleType::Navigation => navigation.get_client_data(self.position.clone(), masses),
                ModuleType::Engines => engines.get_client_data(navigation.status.clone()),
                ModuleType::Mining => mining.get_client_data(self.position.clone(), target),
                ModuleType::Dashboard => serde_json::to_string(&self).unwrap() + "\n",
                ModuleType::Construction => construction.get_client_data(storage),
                ModuleType::Refinery => refinery.get_client_data(storage),
                ModuleType::Tractorbeam => tractorbeam.get_client_data(target),
            }
        } else {
            String::new()
        }
    }

    pub fn give_received_data(
        &mut self,
        module_type: ModuleType,
        recv: String,
        masses: &HashMap<String, Mass>,
    ) {
        if let MassType::Ship {
            ref mut navigation,
            ref mut engines,
            ref mut mining,
            ref mut refinery,
            ref mut construction,
            ref mut tractorbeam,
            ..
        } = self.mass_type
        {
            let target = match &navigation.target_name {
                Some(target_name) => masses.get(target_name),
                None => None,
            };
            match module_type {
                ModuleType::Navigation => navigation.give_received_data(recv),
                ModuleType::Engines => engines.give_received_data(
                    recv,
                    self.position.clone(),
                    self.velocity.clone(),
                    target,
                ),
                ModuleType::Mining => mining.give_received_data(recv),
                ModuleType::Construction => construction.give_received_data(recv),
                ModuleType::Refinery => refinery.give_received_data(recv),
                ModuleType::Tractorbeam => tractorbeam.give_received_data(recv),
                ModuleType::Dashboard => (),
            }
        }
    }

    pub fn take_item(&mut self, item_type: ItemType) -> Option<Item> {
        match self.mass_type {
            MassType::Ship {
                ref mut storage, ..
            } => storage.take_item(item_type),
            MassType::Astroid {
                ref mut resources, ..
            } => resources.take_item(item_type),
            _ => None,
        }
    }

    pub fn give_item(&mut self, item: Item) -> bool {
        match self.mass_type {
            MassType::Ship {
                ref mut storage, ..
            } => storage.give_item(item),
            MassType::Astroid {
                ref mut resources, ..
            } => resources.give_item(item),
            _ => false,
        }
    }

    pub fn item_count(&self, item_type: ItemType) -> usize {
        match &self.mass_type {
            MassType::Ship {
                storage, ..
            } => storage.item_count(item_type),
            MassType::Astroid {
                resources, ..
            } => resources.item_count(item_type),
            _ => 0,
        }
    }
}