Repository navigation
Expand file tree
/
Copy pathCraft.cpp
More file actions
621 lines (563 loc) · 12.7 KB
/
Copy pathCraft.cpp
File metadata and controls
621 lines (563 loc) · 12.7 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/*
* Copyright 2010 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenXcom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#define _USE_MATH_DEFINES
#include "Craft.h"
#include <cmath>
#include <sstream>
#include "../Engine/Language.h"
#include "../Ruleset/RuleCraft.h"
#include "CraftWeapon.h"
#include "../Ruleset/RuleCraftWeapon.h"
#include "../Ruleset/Ruleset.h"
#include "Item.h"
#include "Soldier.h"
#include "Base.h"
namespace OpenXcom
{
#define CRAFT_RANGE 600
/**
* Initializes a craft of the specified type with no ID.
* @param rules Pointer to ruleset.
* @param base Pointer to base of origin.
*/
Craft::Craft(RuleCraft *rules, Base *base) : MovingTarget(), _rules(rules), _base(base), _id(0), _fuel(0), _damage(0), _weapons(), _items(), _status("STR_READY"), _lowFuel(false), _inBattlescape(false)
{
}
/**
* Initializes a craft of the specified type and
* assigns it the latest craft ID available.
* @param rules Pointer to ruleset.
* @param id List of craft IDs.
* @param base Pointer to base of origin.
*/
Craft::Craft(RuleCraft *rules, std::map<std::string, int> *id, Base *base) : MovingTarget(), _rules(rules), _base(base), _fuel(0), _damage(0), _weapons(), _items(), _status("STR_READY"), _lowFuel(false), _inBattlescape(false)
{
_id = (*id)[_rules->getType()];
(*id)[_rules->getType()]++;
for (int i = 0; i < _rules->getWeapons(); i++)
{
_weapons.push_back(0);
}
}
/**
* Delete the contents of the craft from memory.
*/
Craft::~Craft()
{
for (std::vector<CraftWeapon*>::iterator i = _weapons.begin(); i != _weapons.end(); i++)
{
delete *i;
}
for (std::map<std::string, Item*>::iterator i = _items.begin(); i != _items.end(); i++)
{
delete i->second;
}
}
/**
* Loads the craft from a YAML file.
* @param node YAML node.
* @param rule Ruleset for the saved game.
*/
void Craft::load(const YAML::Node &node, Ruleset *rule)
{
unsigned int size = 0;
MovingTarget::load(node);
node["id"] >> _id;
node["fuel"] >> _fuel;
node["damage"] >> _damage;
size = node["weapons"].size();
for (unsigned i = 0; i < size; i++)
{
std::string type;
node["weapons"][i]["type"] >> type;
if (type == "0")
{
_weapons.push_back(0);
}
else
{
CraftWeapon *w = new CraftWeapon(rule->getCraftWeapon(type), 0);
w->load(node["weapons"][i]);
_weapons.push_back(w);
}
}
size = node["items"].size();
for (unsigned i = 0; i < size; i++)
{
int qty;
node["items"][i]["qty"] >> qty;
std::string type;
node["items"][i]["type"] >> type;
Item *it = new Item(rule->getItem(type), qty);
it->load(node["items"][i]);
_items.insert(std::pair<std::string, Item*>(type, it));
}
node["status"] >> _status;
node["lowFuel"] >> _lowFuel;
node["inBattlescape"] >> _inBattlescape;
}
/**
* Saves the craft to a YAML file.
* @param out YAML emitter.
*/
void Craft::save(YAML::Emitter &out) const
{
MovingTarget::save(out);
out << YAML::Key << "type" << YAML::Value << _rules->getType();
out << YAML::Key << "id" << YAML::Value << _id;
out << YAML::Key << "fuel" << YAML::Value << _fuel;
out << YAML::Key << "damage" << YAML::Value << _damage;
out << YAML::Key << "weapons" << YAML::Value;
out << YAML::BeginSeq;
for (std::vector<CraftWeapon*>::const_iterator i = _weapons.begin(); i != _weapons.end(); i++)
{
if (*i != 0)
{
(*i)->save(out);
}
else
{
out << YAML::BeginMap;
out << YAML::Key << "type" << YAML::Value << "0";
out << YAML::EndMap;
}
}
out << YAML::EndSeq;
out << YAML::Key << "items" << YAML::Value;
out << YAML::BeginSeq;
for (std::map<std::string, Item*>::const_iterator i = _items.begin(); i != _items.end(); i++)
{
(*i).second->save(out);
}
out << YAML::EndSeq;
out << YAML::Key << "status" << YAML::Value << _status;
out << YAML::Key << "lowFuel" << YAML::Value << _lowFuel;
out << YAML::Key << "inBattlescape" << YAML::Value << _inBattlescape;
out << YAML::EndMap;
}
/**
* Saves the craft's unique identifiers to a YAML file.
* @param out YAML emitter.
*/
void Craft::saveId(YAML::Emitter &out) const
{
MovingTarget::saveId(out);
out << YAML::Key << "type" << YAML::Value << _rules->getType();
out << YAML::Key << "id" << YAML::Value << _id;
out << YAML::EndMap;
}
/**
* Returns the ruleset for the craft's type.
* @return Pointer to ruleset.
*/
RuleCraft *const Craft::getRules() const
{
return _rules;
}
/**
* Returns the craft's unique ID. Each craft
* can be identified by its type and ID.
* @return Unique ID.
*/
int Craft::getId() const
{
return _id;
}
/**
* Returns the craft's unique identifying name.
* @param lang Language to get strings from.
* @return Full name.
*/
std::wstring Craft::getName(Language *lang) const
{
std::wstringstream name;
name << lang->getString(_rules->getType()) << "-" << _id;
return name.str();
}
/**
* Returns the base the craft belongs to.
* @return Pointer to base.
*/
Base *const Craft::getBase() const
{
return _base;
}
/**
* Changes the base the craft belongs to.
* @param base Pointer to base.
*/
void Craft::setBase(Base *base)
{
_base = base;
}
/**
* Returns the current status of the craft.
* @return Status string.
*/
std::string Craft::getStatus() const
{
return _status;
}
/**
* Changes the current status of the craft.
* @param status Status string.
*/
void Craft::setStatus(std::string status)
{
_status = status;
}
/**
* Changes the destination the craft is heading to.
* @param dest Pointer to new destination.
*/
void Craft::setDestination(Target *dest)
{
MovingTarget::setDestination(dest);
setSpeed(_rules->getMaxSpeed());
}
/**
* Returns the amount of weapons currently
* equipped on this craft.
* @return Number of weapons.
*/
int Craft::getNumWeapons() const
{
if (_rules->getWeapons() == 0)
return 0;
int total = 0;
for (std::vector<CraftWeapon*>::const_iterator i = _weapons.begin(); i != _weapons.end(); i++)
{
if ((*i) != 0)
{
total++;
}
}
return total;
}
/**
* Returns the amount of soldiers from a list
* that are currently attached to this craft.
* @return Number of soldiers.
*/
int Craft::getNumSoldiers() const
{
if (_rules->getSoldiers() == 0)
return 0;
int total = 0;
for (std::vector<Soldier*>::iterator i = _base->getSoldiers()->begin(); i != _base->getSoldiers()->end(); i++)
{
if ((*i)->getCraft() == this)
total++;
}
return total;
}
/**
* Returns the amount of equipment currently
* equipped on this craft.
* @return Number of items.
*/
int Craft::getNumEquipment() const
{
int total = 0;
for (std::map<std::string, Item*>::const_iterator i = _items.begin(); i != _items.end(); i++)
{
total += i->second->getQuantity();
}
return total;
}
/**
* Returns the amount of HWPs currently
* contained in this craft.
* @return Number of HWPs.
*/
int Craft::getNumHWPs() const
{
return 0;
}
/**
* Returns the list of weapons currently equipped
* in the craft.
* @return Pointer to weapon list.
*/
std::vector<CraftWeapon*> *Craft::getWeapons()
{
return &_weapons;
}
/**
* Returns the list of items in the craft.
* @return Pointer to the item list.
*/
std::map<std::string, Item*> *Craft::getItems()
{
return &_items;
}
/**
* Returns the amount of fuel currently contained
* in this craft.
* @return Amount of fuel.
*/
int Craft::getFuel() const
{
return _fuel;
}
/**
* Changes the amount of fuel currently contained
* in this craft.
* @param fuel Amount of fuel.
*/
void Craft::setFuel(int fuel)
{
_fuel = fuel;
if (_fuel > _rules->getMaxFuel())
{
_fuel = _rules->getMaxFuel();
}
else if (_fuel < 0)
{
_fuel = 0;
}
}
/**
* Returns the ratio between the amount of fuel currently
* contained in this craft and the total it can carry.
* @return Percentage of fuel.
*/
int Craft::getFuelPercentage() const
{
return (int)floor((double)_fuel / _rules->getMaxFuel() * 100.0);
}
/**
* Returns the amount of damage this craft has taken.
* @return Amount of damage.
*/
int Craft::getDamage() const
{
return _damage;
}
/**
* Changes the amount of damage this craft has taken.
* @param damage Amount of damage.
*/
void Craft::setDamage(int damage)
{
_damage = damage;
if (_damage < 0)
{
_damage = 0;
}
}
/**
* Returns the ratio between the amount of damage this
* craft can take and the total it can take before it's
* destroyed.
* @return Percentage of damage.
*/
int Craft::getDamagePercentage() const
{
return (int)floor((double)_damage / _rules->getMaxDamage() * 100);
}
/**
* Returns whether the craft is currently low on fuel
* (only has enough to head back to base).
* @return True if it's low, false otherwise.
*/
bool Craft::getLowFuel() const
{
return _lowFuel;
}
/**
* Changes whether the craft is currently low on fuel
* (only has enough to head back to base).
* @param low True if it's low, false otherwise.
*/
void Craft::setLowFuel(bool low)
{
_lowFuel = low;
}
/**
* Returns the current distance between the craft
* and the base it belongs to.
* @return Distance in radian.
*/
double Craft::getDistanceFromBase() const
{
double dLon, dLat;
return getDistance(_base, &dLon, &dLat);
}
/**
* Returns the amount of fuel the craft uses up
* while it's on the air, based on its speed.
* @return Fuel amount.
*/
int Craft::getFuelConsumption() const
{
return (int)floor(_speed / 100.0);
}
/**
* Returns the minimum required fuel for the
* craft to make it back to base.
* @return Fuel amount.
*/
int Craft::getFuelLimit() const
{
return (int)floor(getFuelConsumption() * getDistanceFromBase() / (getRadianSpeed() * 120));
}
/**
* Sends the craft back to its origin base.
*/
void Craft::returnToBase()
{
setDestination(_base);
}
/**
* Moves the craft to its destination.
*/
void Craft::think()
{
if (_dest != 0)
{
calculateSpeed();
}
setLongitude(_lon + _speedLon);
setLatitude(_lat + _speedLat);
if (_dest != 0 && finishedRoute())
{
_lon = _dest->getLongitude();
_lat = _dest->getLatitude();
if (_dest == (Target*)_base)
{
int available = 0, full = 0;
for (std::vector<CraftWeapon*>::iterator i = _weapons.begin(); i != _weapons.end(); i++)
{
if ((*i) == 0)
continue;
available++;
if ((*i)->getAmmo() >= (*i)->getRules()->getAmmoMax())
{
full++;
}
}
if (_damage > 0)
{
setStatus("STR_REPAIRS");
}
else if (available != full)
{
setStatus("STR_REARMING");
}
else
{
setStatus("STR_REFUELLING");
}
setSpeed(0);
setDestination(0);
_lowFuel = false;
}
}
}
/**
* Returns if a certain point is covered by the craft's
* radar range, taking in account the positions of both.
* @param target Pointer to target to compare.
* @return True if it's within range, False otherwise.
*/
bool Craft::insideRadarRange(Target *target) const
{
bool inside = false;
double newrange = CRAFT_RANGE * (1 / 60.0) * (M_PI / 180);
for (double lon = target->getLongitude() - 2*M_PI; lon <= target->getLongitude() + 2*M_PI; lon += 2*M_PI)
{
double dLon = lon - _lon;
double dLat = target->getLatitude() - _lat;
inside = inside || (dLon * dLon + dLat * dLat <= newrange * newrange);
}
return inside;
}
/**
* Consumes the craft's fuel every 10 minutes
* while it's on the air.
*/
void Craft::consumeFuel()
{
setFuel(_fuel - getFuelConsumption());
}
/**
* Repairs the craft's damage every hour
* while it's docked in the base.
*/
void Craft::repair()
{
setDamage(_damage - _rules->getRepairRate());
if (_damage <= 0)
{
_status = "STR_REARMING";
}
}
/**
* Refuels the craft every 30 minutes
* while it's docked in the base.
*/
void Craft::refuel()
{
setFuel(_fuel + _rules->getRefuelRate());
if (_fuel >= _rules->getMaxFuel())
{
_status = "STR_READY";
}
}
/**
* Rearms the craft's weapons by adding ammo every hour
* while it's docked in the base.
*/
void Craft::rearm()
{
int available = 0, full = 0;
for (std::vector<CraftWeapon*>::iterator i = _weapons.begin(); i != _weapons.end(); i++)
{
if ((*i) == 0)
continue;
available++;
(*i)->rearm();
if ((*i)->getAmmo() >= (*i)->getRules()->getAmmoMax())
{
full++;
}
}
if (full == available)
{
_status = "STR_REFUELLING";
}
}
/**
* Gets the craft's battlescape status.
* @return bool
*/
bool Craft::isInBattlescape() const
{
return _inBattlescape;
}
/**
* Sets the craft's battlescape status.
* @param inbattle .
*/
void Craft::setInBattlescape(bool inbattle)
{
_inBattlescape = inbattle;
}
}