forked from OpenXcom/OpenXcom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlienStrategy.cpp
More file actions
162 lines (150 loc) · 4.42 KB
/
Copy pathAlienStrategy.cpp
File metadata and controls
162 lines (150 loc) · 4.42 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
/*
* Copyright 2010-2015 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/>.
*/
#include <assert.h>
#include "AlienStrategy.h"
#include "SavedGame.h"
#include "WeightedOptions.h"
#include "../Engine/RNG.h"
#include "../Ruleset/Ruleset.h"
#include "../Ruleset/RuleRegion.h"
namespace OpenXcom
{
typedef std::map<std::string, WeightedOptions*> MissionsByRegion;
/**
* Create an AlienStrategy with no values.
* Running a game like this will most likely crash.
*/
AlienStrategy::AlienStrategy()
{
// Empty by design!
}
/**
* Free all resources used by this AlienStrategy.
*/
AlienStrategy::~AlienStrategy()
{
// Free allocated memory.
for (MissionsByRegion::iterator ii = _regionMissions.begin(); ii != _regionMissions.end(); ++ii)
{
delete ii->second;
}
}
/**
* Get starting values from the rules.
* @param rules Pointer to the game ruleset.
*/
void AlienStrategy::init(const Ruleset *rules)
{
std::vector<std::string> regions = rules->getRegionsList();
for (std::vector<std::string>::const_iterator rr = regions.begin(); rr != regions.end(); ++rr)
{
RuleRegion *region = rules->getRegion(*rr);
_regionChances.set(*rr, region->getWeight());
WeightedOptions *missions = new WeightedOptions(region->getAvailableMissions());
_regionMissions.insert(std::make_pair(*rr, missions));
}
}
/**
* Loads the data from a YAML file.
* @param rules Pointer to the game ruleset.
* @param node YAML node.
*/
void AlienStrategy::load(const Ruleset *, const YAML::Node &node)
{
// Free allocated memory.
for (MissionsByRegion::iterator ii = _regionMissions.begin(); ii != _regionMissions.end(); ++ii)
{
delete ii->second;
}
_regionMissions.clear();
_regionChances.clear();
_regionChances.load(node["regions"]);
const YAML::Node &strat = node["possibleMissions"];
for (YAML::const_iterator nn = strat.begin(); nn != strat.end(); ++nn)
{
std::string region = (*nn)["region"].as<std::string>();
const YAML::Node &missions = (*nn)["missions"];
std::auto_ptr<WeightedOptions> options(new WeightedOptions());
options->load(missions);
_regionMissions.insert(std::make_pair(region, options.release()));
}
}
/**
* Saves the alien data to a YAML file.
* @return YAML node.
*/
YAML::Node AlienStrategy::save() const
{
YAML::Node node;
node["regions"] = _regionChances.save();
for (MissionsByRegion::const_iterator ii = _regionMissions.begin(); ii != _regionMissions.end(); ++ii)
{
YAML::Node subnode;
subnode["region"] = ii->first;
subnode["missions"] = ii->second->save();
node["possibleMissions"].push_back(subnode);
}
return node;
}
/**
* Choose one of the regions for a mission.
* @param rules Pointer to the ruleset.
* @return The region id.
*/
const std::string AlienStrategy::chooseRandomRegion(const Ruleset *rules)
{
std::string chosen = _regionChances.choose();
if (chosen.empty())
{
init(rules);
chosen = _regionChances.choose();
}
assert ("" != chosen);
return chosen;
}
/**
* Choose one missions available for @a region.
* @param region The region id.
* @return The mission id.
*/
const std::string AlienStrategy::chooseRandomMission(const std::string ®ion) const
{
MissionsByRegion::const_iterator found = _regionMissions.find(region);
assert(found != _regionMissions.end());
return found->second->choose();
}
/**
* Remove @a mission from the list of possible missions for @a region.
* @param region The region id.
* @param mission The mission id.
* @return If there are no more regions with missions available.
*/
bool AlienStrategy::removeMission(const std::string ®ion, const std::string &mission)
{
MissionsByRegion::iterator found = _regionMissions.find(region);
assert(found != _regionMissions.end());
found->second->set(mission, 0);
if (found->second->empty())
{
_regionMissions.erase(found);
_regionChances.set(region, 0);
}
return _regionMissions.empty();
}
}