Skip to content

Commit f6536d1

Browse files
committed
Merge pull request OpenXcom#943 from hackbunny/unique_craft_ids_redux
Give crafts unique ids in the form of a type+id tuple
2 parents 2cb4623 + 2f8e813 commit f6536d1

9 files changed

Lines changed: 98 additions & 18 deletions

File tree

‎src/Geoscape/DogfightState.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ void DogfightState::move()
829829
_ufo->setDamage(_ufo->getDamage() + damage);
830830
if(_ufo->isCrashed())
831831
{
832-
_ufo->setShotDownByCraft(_craft);
832+
_ufo->setShotDownByCraftId(_craft->getUniqueId());
833833
_ufoBreakingOff = false;
834834
_ufo->setSpeed(0);
835835
}
@@ -1047,7 +1047,7 @@ void DogfightState::move()
10471047
_ufoEscapeTimer->stop();
10481048
if (_ufo->isDestroyed())
10491049
{
1050-
if(_ufo->getShotDownByCraft() == _craft)
1050+
if(_ufo->getShotDownByCraftId() == _craft->getUniqueId())
10511051
{
10521052
for(std::vector<Country*>::iterator country = _game->getSavedGame()->getCountries()->begin(); country != _game->getSavedGame()->getCountries()->end(); ++country)
10531053
{
@@ -1072,7 +1072,7 @@ void DogfightState::move()
10721072
}
10731073
else
10741074
{
1075-
if(_ufo->getShotDownByCraft() == _craft)
1075+
if(_ufo->getShotDownByCraftId() == _craft->getUniqueId())
10761076
{
10771077
setStatus("STR_UFO_CRASH_LANDS");
10781078
_game->getResourcePack()->getSound("GEO.CAT", ResourcePack::UFO_CRASH)->play(); //10
@@ -1109,7 +1109,7 @@ void DogfightState::move()
11091109
}
11101110
}
11111111
_timeout += 30;
1112-
if(_ufo->getShotDownByCraft() != _craft)
1112+
if(_ufo->getShotDownByCraftId() != _craft->getUniqueId())
11131113
{
11141114
_timeout += 50;
11151115
_ufo->setHitFrame(3);

‎src/Savegame/Base.cpp‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,10 @@ void Base::load(const YAML::Node &node, SavedGame *save, bool newGame, bool newB
140140
s->setCraft(0);
141141
if (const YAML::Node &craft = (*i)["craft"])
142142
{
143-
std::string type = craft["type"].as<std::string>();
144-
int id = craft["id"].as<int>();
143+
CraftId craftId = Craft::loadId(craft);
145144
for (std::vector<Craft*>::iterator j = _crafts.begin(); j != _crafts.end(); ++j)
146145
{
147-
if ((*j)->getRules()->getType() == type && (*j)->getId() == id)
146+
if ((*j)->getUniqueId() == craftId)
148147
{
149148
s->setCraft(*j);
150149
break;

‎src/Savegame/Craft.cpp‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,26 @@ YAML::Node Craft::save() const
243243
return node;
244244
}
245245

246+
/**
247+
* Loads a craft unique identifier from a YAML file.
248+
* @param node YAML node.
249+
* @return Unique craft id.
250+
*/
251+
CraftId Craft::loadId(const YAML::Node &node)
252+
{
253+
return std::make_pair(node["type"].as<std::string>(), node["id"].as<int>());
254+
}
255+
246256
/**
247257
* Saves the craft's unique identifiers to a YAML file.
248258
* @return YAML node.
249259
*/
250260
YAML::Node Craft::saveId() const
251261
{
252262
YAML::Node node = MovingTarget::saveId();
253-
node["type"] = _rules->getType();
254-
node["id"] = _id;
263+
CraftId uniqueId = getUniqueId();
264+
node["type"] = uniqueId.first;
265+
node["id"] = uniqueId.second;
255266
return node;
256267
}
257268

@@ -933,4 +944,13 @@ int Craft::getInterceptionOrder() const
933944
return _interceptionOrder;
934945
}
935946

947+
/**
948+
* Gets the craft's unique id.
949+
* @return A tuple of the craft's type and per-type id.
950+
*/
951+
CraftId Craft::getUniqueId() const
952+
{
953+
return std::make_pair(_rules->getType(), _id);
954+
}
955+
936956
}

‎src/Savegame/Craft.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "MovingTarget.h"
2323
#include <vector>
2424
#include <string>
25+
#include "CraftId.h"
2526

2627
namespace OpenXcom
2728
{
@@ -64,6 +65,8 @@ class Craft : public MovingTarget
6465
YAML::Node save() const;
6566
/// Saves the craft's ID to YAML.
6667
YAML::Node saveId() const;
68+
/// Loads a craft ID from YAML.
69+
static CraftId loadId(const YAML::Node &node);
6770
/// Gets the craft's ruleset.
6871
RuleCraft *getRules() const;
6972
/// Sets the craft's ruleset.
@@ -166,6 +169,8 @@ class Craft : public MovingTarget
166169
void setInterceptionOrder(const int order);
167170
/// Gets interception number.
168171
int getInterceptionOrder() const;
172+
/// Gets the craft's unique id.
173+
CraftId getUniqueId() const;
169174
};
170175

171176
}

‎src/Savegame/CraftId.h‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2010-2014 OpenXcom Developers.
3+
*
4+
* This file is part of OpenXcom.
5+
*
6+
* OpenXcom is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* OpenXcom is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
#ifndef OPENXCOM_CRAFTID_H
20+
#define OPENXCOM_CRAFTID_H
21+
22+
#include <utility>
23+
#include <string>
24+
25+
namespace OpenXcom
26+
{
27+
28+
typedef std::pair<std::string, int> CraftId;
29+
30+
}
31+
32+
#endif

‎src/Savegame/SavedGame.cpp‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,4 +1689,24 @@ std::string SavedGame::getLastSelectedArmor()
16891689
return _lastselectedArmor;
16901690
}
16911691

1692+
/**
1693+
* Returns the craft corresponding to the specified unique id.
1694+
* @param craftId The unique craft id to look up.
1695+
* @return The craft with the specified id, or NULL.
1696+
*/
1697+
Craft *SavedGame::findCraftByUniqueId(const CraftId& craftId) const
1698+
{
1699+
for (std::vector<Base*>::const_iterator base = _bases.begin(); base != _bases.end(); ++base)
1700+
{
1701+
for (std::vector<Craft*>::const_iterator craft = (*base)->getCrafts()->begin(); craft != (*base)->getCrafts()->end(); ++craft)
1702+
{
1703+
if ((*craft)->getUniqueId() == craftId)
1704+
return *craft;
1705+
}
1706+
}
1707+
1708+
return NULL;
1709+
}
1710+
1711+
16921712
}

‎src/Savegame/SavedGame.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <string>
2525
#include <time.h>
2626
#include <stdint.h>
27+
#include "CraftId.h"
2728

2829
namespace OpenXcom
2930
{
@@ -48,6 +49,7 @@ class AlienStrategy;
4849
class AlienMission;
4950
class Target;
5051
class Soldier;
52+
class Craft;
5153

5254
/**
5355
* Enumerator containing all the possible game difficulties.
@@ -278,6 +280,8 @@ class SavedGame
278280
void setLastSelectedArmor(const std::string &value);
279281
//Gets the the last selected armour
280282
std::string getLastSelectedArmor();
283+
/// Returns the craft corresponding to the specified unique id.
284+
Craft *findCraftByUniqueId(const CraftId& craftId) const;
281285
};
282286

283287
}

‎src/Savegame/Ufo.cpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace OpenXcom
4343
Ufo::Ufo(RuleUfo *rules)
4444
: MovingTarget(), _rules(rules), _id(0), _crashId(0), _landId(0), _damage(0), _direction("STR_NORTH")
4545
, _altitude("STR_HIGH_UC"), _status(FLYING), _secondsRemaining(0)
46-
, _inBattlescape(false), _shotDownByCraft(NULL), _mission(0), _trajectory(0)
46+
, _inBattlescape(false), _mission(0), _trajectory(0)
4747
, _trajectoryPoint(0), _detected(false), _hyperDetected(false), _shootingAt(0), _hitFrame(0)
4848
{
4949
}
@@ -557,14 +557,14 @@ const std::string &Ufo::getAlienRace() const
557557
return _mission->getRace();
558558
}
559559

560-
void Ufo::setShotDownByCraft(Craft *craft)
560+
void Ufo::setShotDownByCraftId(const CraftId& craft)
561561
{
562-
_shotDownByCraft = craft;
562+
_shotDownByCraftId = craft;
563563
}
564564

565-
Craft *Ufo::getShotDownByCraft() const
565+
CraftId Ufo::getShotDownByCraftId() const
566566
{
567-
return _shotDownByCraft;
567+
return _shotDownByCraftId;
568568
}
569569

570570
/**

‎src/Savegame/Ufo.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "MovingTarget.h"
2323
#include <string>
2424
#include <yaml-cpp/yaml.h>
25+
#include "CraftId.h"
2526

2627
namespace OpenXcom
2728
{
@@ -31,7 +32,6 @@ class AlienMission;
3132
class UfoTrajectory;
3233
class SavedGame;
3334
class Ruleset;
34-
class Craft;
3535

3636
/**
3737
* Represents an alien UFO on the map.
@@ -50,7 +50,7 @@ class Ufo : public MovingTarget
5050
enum UfoStatus _status;
5151
size_t _secondsRemaining;
5252
bool _inBattlescape;
53-
Craft *_shotDownByCraft;
53+
CraftId _shotDownByCraftId;
5454
AlienMission *_mission;
5555
const UfoTrajectory *_trajectory;
5656
size_t _trajectoryPoint;
@@ -114,9 +114,9 @@ class Ufo : public MovingTarget
114114
/// Gets the UFO's alien race.
115115
const std::string &getAlienRace() const;
116116
/// Sets the ID of craft which shot down the UFO.
117-
void setShotDownByCraft(Craft *craft);
117+
void setShotDownByCraftId(const CraftId& craftId);
118118
/// Gets the ID of craft which shot down the UFO.
119-
Craft *getShotDownByCraft() const;
119+
CraftId getShotDownByCraftId() const;
120120
/// Gets the UFO's visibility.
121121
int getVisibility() const;
122122
/// Gets the UFO's Mission type.

0 commit comments

Comments
 (0)