forked from OpenXcom/OpenXcom
-
Notifications
You must be signed in to change notification settings - Fork 81
/
RuleResearch.cpp
326 lines (292 loc) · 8.98 KB
/
RuleResearch.cpp
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
/*
* Copyright 2010-2016 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 "RuleResearch.h"
#include "../Engine/Exception.h"
#include "../Engine/Collections.h"
#include "../Engine/ScriptBind.h"
#include "Mod.h"
namespace OpenXcom
{
RuleResearch::RuleResearch(const std::string &name, int listOrder) : _name(name), _spawnedItemCount(1), _cost(0), _points(0), _sequentialGetOneFree(false), _needItem(false), _destroyItem(false), _unlockFinalMission(false), _listOrder(listOrder)
{
}
/**
* Loads the research project from a YAML file.
* @param node YAML node.
* @param listOrder The list weight for this research.
*/
void RuleResearch::load(const YAML::Node &node, Mod* mod, const ModScript& parsers)
{
if (const YAML::Node &parent = node["refNode"])
{
load(parent, mod, parsers);
}
_lookup = node["lookup"].as<std::string>(_lookup);
_cutscene = node["cutscene"].as<std::string>(_cutscene);
_spawnedItem = node["spawnedItem"].as<std::string>(_spawnedItem);
_spawnedItemCount = node["spawnedItemCount"].as<int>(_spawnedItemCount);
mod->loadUnorderedNames(_name, _spawnedItemList, node["spawnedItemList"]);
mod->loadUnorderedNames(_name, _decreaseCounter, node["decreaseCounter"]);
mod->loadUnorderedNames(_name, _increaseCounter, node["increaseCounter"]);
_spawnedEvent = node["spawnedEvent"].as<std::string>(_spawnedEvent);
_cost = node["cost"].as<int>(_cost);
_points = node["points"].as<int>(_points);
mod->loadUnorderedNames(_name, _dependenciesName, node["dependencies"]);
mod->loadUnorderedNames(_name, _unlocksName, node["unlocks"]);
mod->loadUnorderedNames(_name, _disablesName, node["disables"]);
mod->loadUnorderedNames(_name, _reenablesName, node["reenables"]);
mod->loadUnorderedNames(_name, _getOneFreeName, node["getOneFree"]);
mod->loadUnorderedNames(_name, _requiresName, node["requires"]);
mod->loadBaseFunction(_name, _requiresBaseFunc, node["requiresBaseFunc"]);
_sequentialGetOneFree = node["sequentialGetOneFree"].as<bool>(_sequentialGetOneFree);
mod->loadNamesToNames(_name, _getOneFreeProtectedName, node["getOneFreeProtected"]);
mod->loadNameNull(_name, _neededItemName, node["neededItem"]);
_needItem = node["needItem"].as<bool>(_needItem);
_destroyItem = node["destroyItem"].as<bool>(_destroyItem);
_unlockFinalMission = node["unlockFinalMission"].as<bool>(_unlockFinalMission);
_listOrder = node["listOrder"].as<int>(_listOrder);
// This is necessary, research code assumes it!
if (!_requiresName.empty() && _cost != 0)
{
throw Exception("Research topic " + _name + " has requirements, but the cost is not zero. Sorry, this is not allowed!");
}
_scriptValues.load(node, parsers.getShared());
}
/**
* Cross link with other Rules.
*/
void RuleResearch::afterLoad(const Mod* mod)
{
if (_lookup == _name)
{
_lookup = "";
}
if (_needItem)
{
// FIXME: this would break all mods unfortunately, maybe one day...
//mod->linkRule(_neededItem, _neededItemName.empty() ? _name : _neededItemName);
if (_neededItemName.empty())
{
_neededItem = mod->getItem(_name, false); // false, because even vanilla ruleset is a mess
}
else
{
_neededItem = mod->getItem(_neededItemName, true);
}
}
_dependencies = mod->getResearch(_dependenciesName);
_unlocks = mod->getResearch(_unlocksName);
_disables = mod->getResearch(_disablesName);
_reenables = mod->getResearch(_reenablesName);
_getOneFree = mod->getResearch(_getOneFreeName);
_requires = mod->getResearch(_requiresName);
_getOneFreeProtected.reserve(_getOneFreeProtectedName.size());
for (auto& n : _getOneFreeProtectedName)
{
auto* left = mod->getResearch(n.first, false);
if (left)
{
auto right = mod->getResearch(n.second);
_getOneFreeProtected.push_back(std::make_pair(left, right));
}
else
{
throw Exception("Unknown research '" + n.first + "'");
}
}
//remove not needed data
Collections::removeAll(_dependenciesName);
Collections::removeAll(_unlocksName);
Collections::removeAll(_disablesName);
Collections::removeAll(_reenablesName);
Collections::removeAll(_getOneFreeName);
Collections::removeAll(_requiresName);
Collections::removeAll(_getOneFreeProtectedName);
}
/**
* Gets the cost of this ResearchProject.
* @return The cost of this ResearchProject (in man/day).
*/
int RuleResearch::getCost() const
{
return _cost;
}
/**
* Gets the name of this ResearchProject.
* @return The name of this ResearchProject.
*/
const std::string &RuleResearch::getName() const
{
return _name;
}
/**
* Gets the list of dependencies, i.e. ResearchProjects, that must be discovered before this one.
* @return The list of ResearchProjects.
*/
const std::vector<const RuleResearch*> &RuleResearch::getDependencies() const
{
return _dependencies;
}
/**
* Checks if this ResearchProject gives free topics in sequential order (or random order).
* @return True if the ResearchProject gives free topics in sequential order.
*/
bool RuleResearch::sequentialGetOneFree() const
{
return _sequentialGetOneFree;
}
/**
* Checks if this ResearchProject needs a corresponding Item to be researched.
* @return True if the ResearchProject needs a corresponding item.
*/
bool RuleResearch::needItem() const
{
return _needItem;
}
/**
* Checks if this ResearchProject needs a corresponding Item to be researched.
* @return True if the ResearchProject needs a corresponding item.
*/
bool RuleResearch::destroyItem() const
{
return _destroyItem;
}
/**
* Gets the list of ResearchProjects unlocked by this research.
* @return The list of ResearchProjects.
*/
const std::vector<const RuleResearch*> &RuleResearch::getUnlocked() const
{
return _unlocks;
}
/**
* Gets the list of ResearchProjects disabled by this research.
* @return The list of ResearchProjects.
*/
const std::vector<const RuleResearch*> &RuleResearch::getDisabled() const
{
return _disables;
}
/**
* Gets the list of ResearchProjects reenabled by this research.
* @return The list of ResearchProjects.
*/
const std::vector<const RuleResearch*> &RuleResearch::getReenabled() const
{
return _reenables;
}
/**
* Get the points earned for this ResearchProject.
* @return The points earned for this ResearchProject.
*/
int RuleResearch::getPoints() const
{
return _points;
}
/**
* Gets the list of ResearchProjects granted at random for free by this research.
* @return The list of ResearchProjects.
*/
const std::vector<const RuleResearch*> &RuleResearch::getGetOneFree() const
{
return _getOneFree;
}
/**
* Gets the list(s) of ResearchProjects granted at random for free by this research (if a defined prerequisite is met).
* @return The list(s) of ResearchProjects.
*/
const std::vector<std::pair<const RuleResearch*, std::vector<const RuleResearch*> > > &RuleResearch::getGetOneFreeProtected() const
{
return _getOneFreeProtected;
}
/**
* Gets what article to look up in the ufopedia.
* @return The article to look up in the ufopaedia
*/
const std::string &RuleResearch::getLookup() const
{
return _lookup;
}
/**
* Gets the requirements for this ResearchProject.
* @return The requirement for this research.
*/
const std::vector<const RuleResearch*> &RuleResearch::getRequirements() const
{
return _requires;
}
/**
* Gets the list weight for this research item.
* @return The list weight for this research item.
*/
int RuleResearch::getListOrder() const
{
return _listOrder;
}
/**
* Gets the cutscene to play when this research item is completed.
* @return The cutscene id.
*/
const std::string & RuleResearch::getCutscene() const
{
return _cutscene;
}
/**
* Gets the item to spawn in the base stores when this topic is researched.
* @return The item id.
*/
const std::string & RuleResearch::getSpawnedItem() const
{
return _spawnedItem;
}
////////////////////////////////////////////////////////////
// Script binding
////////////////////////////////////////////////////////////
namespace
{
std::string debugDisplayScript(const RuleResearch* ru)
{
if (ru)
{
std::string s;
s += RuleResearch::ScriptName;
s += "(name: \"";
s += ru->getName();
s += "\")";
return s;
}
else
{
return "null";
}
}
}
/**
* Register RuleResearch in script parser.
* @param parser Script parser.
*/
void RuleResearch::ScriptRegister(ScriptParserBase* parser)
{
Bind<RuleResearch> ar = { parser };
ar.add<&RuleResearch::getCost>("getCost");
ar.add<&RuleResearch::getPoints>("getPoints");
ar.addScriptValue<BindBase::OnlyGet, &RuleResearch::_scriptValues>();
ar.addDebugDisplay<&debugDisplayScript>();
}
}