-
Notifications
You must be signed in to change notification settings - Fork 6
/
c_world.cpp
executable file
·377 lines (312 loc) · 8.84 KB
/
c_world.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
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
#include "c_world.h"
c_World::c_World(QObject *parent) :
QObject(parent)
{
debug = pq_debug_none;
Player = new Entity;
Monster = new c_Monster;
Act = 0;
Action = "";
State = pq_state_reserved_1;
pb_action = 0;
pb_encumbrance = 0;
pb_experience = 0;
pb_plot = 0;
pb_quest = 0;
isLoaded = false;
}
void c_World::initPlayer()
/*
* Player creation
*
* calls: makeEqByGrade
* rtns: void
* affects: c_World::Player
*
* Relies on entity constructor to baseline some values,
* fills in and overwrites others that are player
* specific.
*/
{
c_Spell *spell = new c_Spell;
//c_Item *weapon = new c_Item;
//c_Item *sheild = new c_Item;
//QList<c_Item*> armor;
int index(0), i(0);
// rand traits
Player->Name = Player->nameRand();
Player->Race = Player->raceRand();
Player->Voc = Player->vocRand();
// add starting spell
spell->setRandName();
Player->Spells.append(spell);
// add starting weapon (a weak one =D )
Player->Weapon = c_World::makeEqByGrade(pq_equip_weapon, -4);
// Look ma, no sheild!
Player->Sheild = new c_Item;
// add two starting armors (weak, random)
for(int i(0); i < 9; i++) {
Player->Armor.append(new c_Item);
}
// 2 *unique* armors - not the same one twice
do {
index = rand() % Player->Armor.size();
if (Player->Armor.at(index)->Name() == tr("")) {
delete Player->Armor[index];
Player->Armor[index] = c_World::makeEqByGrade(pq_equip_armor, -2);
i++;
}
} while (i < 2);
// finally, money to drink at the bar with
Player->Gold = 25;
}
c_Item* c_World::makeEqByGrade(t_pq_equip eqtype, int grade)
/*
* Make Equipment By Grade
*
* calls: c_Item methods
* rtns: c_Item* (of specified level)
* affects: none
*
* Used by various to produce items of specific type at
* requested level (grade)
*/
{
c_Item* equip = new c_Item;
t_pq_equip eqSelect = eqtype;
// handle "any" type
if (eqSelect == pq_equip_any)
eqSelect = static_cast<t_pq_equip>(rand() % 3);
// main types
switch(eqSelect) {
case pq_equip_weapon:
equip->makeClosestGrade(eqSelect, grade);
// 2 possible mods, 50% chnc each: player level affects pos/neg
for(int i(0); i < 2; i++) {
if (rand() % 2 == 0) {
if (rand() % Player->Level.toInt() < 5) equip->addWeaponNegMod();
else equip->addWeaponMod();
}
}
// set bonus to make up differance in grade
equip->setBonus(grade - equip->Grade());
break;;
case pq_equip_shield:
equip->makeClosestGrade(eqSelect, grade);
// 2 possible mods, 50% chnc each: player level affects pos/neg
for(int i(0); i < 2; i++) {
if (rand() % 2 == 0) {
if (rand() % Player->Level.toInt() < 5) equip->addDefNegMod();
else equip->addDefMod();
}
}
// set bonus to make up differance in grade
equip->setBonus(grade - equip->Grade());
break;;
case pq_equip_armor:
equip->makeClosestGrade(eqSelect, grade);
// 2 possible mods, 50% chnc each: player level affects pos/neg
for(int i(0); i < 2; i++) {
if (rand() % 2 == 0) {
if (rand() % Player->Level.toInt() < 5) equip->addDefNegMod();
else equip->addDefMod();
}
}
// set bonus to make up differance in grade
equip->setBonus(grade - equip->Grade());
break;;
case pq_equip_any:
break;;
}
return equip;
}
void c_World::save()
{
c_World::save("pq_savefile.pqd");
}
void c_World::save(QString filename)
{
Json::Value root;
std::string mKey = "World";
std::ofstream saveFile;
saveFile.open(filename.toStdString().c_str(),std::ios::trunc);
if (saveFile.is_open()) {
/*
int Act;
QString Action;
t_pq_state State;
*/
root[mKey]["Act"] = Act;
root[mKey]["Action"] = Action.toStdString();
root[mKey]["ActionTime"] = actionTime;
root[mKey]["State"] = (int)State;
/*
int pb_action;
int pb_experience;
int pb_encumbrance;
int pb_plot;
int pb_quest;
*/
root[mKey]["pb_action"] = pb_action;
root[mKey]["pb_experience"] = pb_experience;
root[mKey]["pb_encumbrance"] = pb_encumbrance;
root[mKey]["pb_plot"] = pb_plot;
root[mKey]["pb_quest"] = pb_quest;
/*
quint32 debug
*/
root[mKey]["Debug"] = debug;
/*
QStringList quests;
*/
root[mKey]["Quests"] = c_World::listToArray(quests);
//Entity* Player;
root[mKey]["Player"] = Player->save();
//c_Monster* Monster;
root[mKey]["Monster"] = Monster->save();
}
else
{
// file failed to open... wtf
}
saveFile << root << std::endl;
saveFile.close();
}
void c_World::load()
{
c_World::load(QString::fromStdString("pq_savefile.pqd"));
}
void c_World::load(QString filename)
{
Json::Value root;
Json::Reader readSave;
std::ifstream saveFile;
saveFile.open(filename.toStdString().c_str());
// open file
if (saveFile.is_open())
{
// parse json
if (! readSave.parse(saveFile, root, false))
{
// failed to read
//std::cerr << "JSON Parse fail on file " << filename << ":" << std::endl;
//std::cerr << readSave.getFormatedErrorMessages();
//exit (1);
QMessageBox mBox;
mBox.setWindowTitle(QString::fromStdString("JSON Parse fail on file ") + filename );
mBox.setText(QString::fromStdString(readSave.getFormatedErrorMessages()));
mBox.exec();
exit(1);
}
}
else
{
// file fail
//std::cerr << "file " << filename << " failed to open" << std::endl;
//exit(1);
QMessageBox mBox;
mBox.setWindowTitle(QString::fromStdString("Progress Quest load ") + filename);
mBox.setText(QString::fromStdString("Failed to open"));
mBox.exec();
exit(1);
}
saveFile.close();
// end file parsing
//chain the json loader
c_World::load(root);
}
void c_World::load(Json::Value root)
{
// unpack world values from anonymous main json object
root = root.get("World", Json::Value::null);
//Entity* Player;
Player->load(root.get("Player", Json::objectValue));
//c_Monster* Monster;
Monster->load(root.get("Monster", Json::objectValue));
/*
int Act;
QString Action;
t_pq_state State;
*/
Act = root.get("Act", 99).asInt();
Action = QString::fromStdString(root.get("Action", "Knock knock...").asString());
actionTime = root.get("ActionTime", 50).asInt();
State = (t_pq_state)root.get("State", 0).asInt();
/*
quint32 debug
*/
debug = (quint32) root.get("Debug", 0).asInt();
/*
QStringList quests;
*/
// if not found, passes empty json array to helper which will return empty qstringlist
quests = c_World::arrayToList(root.get("Quests", Json::arrayValue));
/*
int pb_action;
int pb_experience;
int pb_encumbrance;
int pb_plot;
int pb_quest;
*/
pb_action = root.get("pb_action", 99).asInt();
pb_experience = root.get("pb_experience", 99).asInt();
pb_encumbrance = root.get("pb_encumbrance", 99).asInt();
pb_plot = root.get("pb_plot", 99).asInt();
pb_quest = root.get("pb_quest", 99).asInt();
// set loaded flag
isLoaded = true;
}
// save helper for quest list
Json::Value c_World::listToArray(QStringList &list)
{
Json::Value array;
array.clear();
for (int i=0; i < list.size(); i++)
{
array.append(list.at(i).toStdString());
}
return array;
}
// load helper for quest list
QStringList c_World::arrayToList(Json::Value array)
{
QStringList list;
list.clear();
// load json array to qstring list
for (unsigned int i=0; i < array.size(); i++)
{
list.append( QString::fromStdString(array[i].asString()) );
}
return list;
}
bool c_World::isDebugFlagSet(t_pq_debug flag)
{
return (debug & flag);
}
bool c_World::isDebugFlagReset(t_pq_debug flag)
{
return ! (debug & flag);
}
void c_World::setDebugFlag(t_pq_debug flag)
{
debug |= flag;
}
void c_World::resetDebugFlag(t_pq_debug flag)
{
debug &= ~flag;
}
void c_World::toggleDebugFlag(t_pq_debug flag)
{
if (c_World::isDebugFlagReset(flag))
c_World::setDebugFlag(flag);
else
c_World::resetDebugFlag(flag);
}
void c_World::debugClear()
{
debug = pq_debug_none;
}
void c_World::debugActive()
{
debug = pq_debug_active;
}