Repository navigation
Expand file tree
/
Copy pathUfopaedia.cpp
More file actions
270 lines (257 loc) · 8.66 KB
/
Copy pathUfopaedia.cpp
File metadata and controls
270 lines (257 loc) · 8.66 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
/*
* 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 "Ufopaedia.h"
#include "UfopaediaStartState.h"
#include "../Savegame/SavedGame.h"
#include "../Mod/Mod.h"
#include "../Mod/ArticleDefinition.h"
#include "ArticleState.h"
#include "ArticleStateBaseFacility.h"
#include "ArticleStateCraft.h"
#include "ArticleStateCraftWeapon.h"
#include "ArticleStateItem.h"
#include "ArticleStateArmor.h"
#include "ArticleStateText.h"
#include "ArticleStateTextImage.h"
#include "ArticleStateUfo.h"
#include "ArticleStateVehicle.h"
#include "ArticleStateTFTD.h"
#include "ArticleStateTFTDArmor.h"
#include "ArticleStateTFTDVehicle.h"
#include "ArticleStateTFTDItem.h"
#include "ArticleStateTFTDFacility.h"
#include "ArticleStateTFTDCraft.h"
#include "ArticleStateTFTDCraftWeapon.h"
#include "ArticleStateTFTDUso.h"
#include "../Engine/Game.h"
namespace OpenXcom
{
size_t Ufopaedia::_current_index = 0;
/**
* Checks, if an article has already been released.
* @param save Pointer to saved game.
* @param article Article definition to release.
* @returns true, if the article is available.
*/
bool Ufopaedia::isArticleAvailable(SavedGame *save, ArticleDefinition *article)
{
return save->isResearched(article->requires);
}
/**
* Gets the index of the selected article_id in the visible list.
* If the id is not found, returns -1.
* @param save Pointer to saved game.
* @param mod Pointer to mod.
* @param article_id Article id to find.
* @returns Index of the given article id in the internal list, -1 if not found.
*/
size_t Ufopaedia::getArticleIndex(SavedGame *save, Mod *mod, std::string &article_id)
{
std::string UC_ID = article_id + "_UC";
ArticleDefinitionList articles = getAvailableArticles(save, mod);
for (size_t it=0; it<articles.size(); ++it)
{
if (articles[it]->id == article_id)
{
return it;
}
}
for (size_t it = 0; it<articles.size(); ++it)
{
if (articles[it]->id == UC_ID)
{
article_id = UC_ID;
return it;
}
}
for (size_t it = 0; it<articles.size(); ++it)
{
for (std::vector<std::string>::iterator j = articles[it]->requires.begin(); j != articles[it]->requires.end(); ++j)
{
if (article_id == *j)
{
article_id = articles[it]->id;
return it;
}
}
}
return -1;
}
/**
* Creates a new article state dependent on the given article definition.
* @param game Pointer to actual game.
* @param article Article definition to create from.
* @returns Article state object if created, 0 otherwise.
*/
ArticleState *Ufopaedia::createArticleState(ArticleDefinition *article)
{
switch (article->getType())
{
case UFOPAEDIA_TYPE_CRAFT:
return new ArticleStateCraft(dynamic_cast<ArticleDefinitionCraft *> (article));
case UFOPAEDIA_TYPE_CRAFT_WEAPON:
return new ArticleStateCraftWeapon(dynamic_cast<ArticleDefinitionCraftWeapon *> (article));
case UFOPAEDIA_TYPE_VEHICLE:
return new ArticleStateVehicle(dynamic_cast<ArticleDefinitionVehicle *> (article));
case UFOPAEDIA_TYPE_ITEM:
return new ArticleStateItem(dynamic_cast<ArticleDefinitionItem *> (article));
case UFOPAEDIA_TYPE_ARMOR:
return new ArticleStateArmor(dynamic_cast<ArticleDefinitionArmor *> (article));
case UFOPAEDIA_TYPE_BASE_FACILITY:
return new ArticleStateBaseFacility(dynamic_cast<ArticleDefinitionBaseFacility *> (article));
case UFOPAEDIA_TYPE_TEXT:
return new ArticleStateText(dynamic_cast<ArticleDefinitionText *> (article));
case UFOPAEDIA_TYPE_TEXTIMAGE:
return new ArticleStateTextImage(dynamic_cast<ArticleDefinitionTextImage *> (article));
case UFOPAEDIA_TYPE_UFO:
return new ArticleStateUfo(dynamic_cast<ArticleDefinitionUfo *> (article));
case UFOPAEDIA_TYPE_TFTD:
return new ArticleStateTFTD(dynamic_cast<ArticleDefinitionTFTD *> (article));
case UFOPAEDIA_TYPE_TFTD_CRAFT:
return new ArticleStateTFTDCraft(dynamic_cast<ArticleDefinitionTFTD *> (article));
case UFOPAEDIA_TYPE_TFTD_CRAFT_WEAPON:
return new ArticleStateTFTDCraftWeapon(dynamic_cast<ArticleDefinitionTFTD *> (article));
case UFOPAEDIA_TYPE_TFTD_VEHICLE:
return new ArticleStateTFTDVehicle(dynamic_cast<ArticleDefinitionTFTD *> (article));
case UFOPAEDIA_TYPE_TFTD_ITEM:
return new ArticleStateTFTDItem(dynamic_cast<ArticleDefinitionTFTD *> (article));
case UFOPAEDIA_TYPE_TFTD_ARMOR:
return new ArticleStateTFTDArmor(dynamic_cast<ArticleDefinitionTFTD *> (article));
case UFOPAEDIA_TYPE_TFTD_BASE_FACILITY:
return new ArticleStateTFTDFacility(dynamic_cast<ArticleDefinitionTFTD *> (article));
case UFOPAEDIA_TYPE_TFTD_USO:
return new ArticleStateTFTDUso(dynamic_cast<ArticleDefinitionTFTD *> (article));
default: break;
}
return 0;
}
/**
* Set UPSaved index and open the new state.
* @param game Pointer to actual game.
* @param article Article definition of the article to open.
*/
void Ufopaedia::openArticle(Game *game, ArticleDefinition *article)
{
std::string id = article->id;
_current_index = getArticleIndex(game->getSavedGame(), game->getMod(), id);
if (_current_index != (size_t) -1)
{
game->pushState(createArticleState(article));
}
}
/**
* Checks if selected article_id is available -> if yes, open it.
* @param game Pointer to actual game.
* @param article_id Article id to find.
*/
void Ufopaedia::openArticle(Game *game, const std::string &article_id)
{
std::string id = article_id;
_current_index = getArticleIndex(game->getSavedGame(), game->getMod(), id);
if (_current_index != (size_t) -1)
{
ArticleDefinition *article = game->getMod()->getUfopaediaArticle(id);
game->pushState(createArticleState(article));
}
}
/**
* Open Ufopaedia start state, presenting the section selection buttons.
* @param game Pointer to actual game.
*/
void Ufopaedia::open(Game *game)
{
game->pushState(new UfopaediaStartState);
}
/**
* Open the next article in the list. Loops to the first.
* @param game Pointer to actual game.
*/
void Ufopaedia::next(Game *game)
{
ArticleDefinitionList articles = getAvailableArticles(game->getSavedGame(), game->getMod());
if (_current_index >= articles.size() - 1)
{
// goto first
_current_index = 0;
}
else
{
_current_index++;
}
game->popState();
game->pushState(createArticleState(articles[_current_index]));
}
/**
* Open the previous article in the list. Loops to the last.
* @param game Pointer to actual game.
*/
void Ufopaedia::prev(Game *game)
{
ArticleDefinitionList articles = getAvailableArticles(game->getSavedGame(), game->getMod());
if (_current_index == 0)
{
// goto last
_current_index = articles.size() - 1;
}
else
{
_current_index--;
}
game->popState();
game->pushState(createArticleState(articles[_current_index]));
}
/**
* Fill an ArticleList with the currently visible ArticleIds of the given section.
* @param save Pointer to saved game.
* @param mod Pointer to mod.
* @param section Article section to find, e.g. "XCOM Crafts & Armaments", "Alien Lifeforms", etc.
* @param data Article definition list object to fill data in.
*/
void Ufopaedia::list(SavedGame *save, Mod *mod, const std::string §ion, ArticleDefinitionList &data)
{
ArticleDefinitionList articles = getAvailableArticles(save, mod);
for (ArticleDefinitionList::iterator it=articles.begin(); it!=articles.end(); ++it)
{
if ((*it)->section == section)
{
data.push_back(*it);
}
}
}
/**
* Return an ArticleList with all the currently visible ArticleIds.
* @param save Pointer to saved game.
* @param mod Pointer to mod.
* @return List of visible ArticleDefinitions.
*/
ArticleDefinitionList Ufopaedia::getAvailableArticles(SavedGame *save, Mod *mod)
{
const std::vector<std::string> &list = mod->getUfopaediaList();
ArticleDefinitionList articles;
for (std::vector<std::string>::const_iterator it=list.begin(); it!=list.end(); ++it)
{
ArticleDefinition *article = mod->getUfopaediaArticle(*it);
if (isArticleAvailable(save, article) && article->section != UFOPAEDIA_NOT_AVAILABLE)
{
articles.push_back(article);
}
}
return articles;
}
}