-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersistent.cpp
More file actions
512 lines (446 loc) · 12.6 KB
/
Copy pathPersistent.cpp
File metadata and controls
512 lines (446 loc) · 12.6 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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#include "main.hpp"
//-------------------------------------------------- MAP --------------------------------------------------//
void Map::load(TCODZip& t_zip)
{
// Recuperate seed
m_rng_seed = t_zip.getInt();
// Generate map with no actors
init(false);
// Restore player character's memory of the map
for (int i{ 0 }; i < m_width * m_height; i++)
{
m_tiles[i].m_explored = t_zip.getInt();
}
}
void Map::save(TCODZip& t_zip)
{
t_zip.putInt(m_rng_seed);
for (int i{ 0 }; i < m_width * m_height; i++)
{
t_zip.putInt(m_tiles[i].m_explored);
}
}
//-------------------------------------------------- map --------------------------------------------------//
//-------------------------------------------------- ACTOR --------------------------------------------------//
void Actor::load(TCODZip& t_zip)
{
// Regular members
m_x = t_zip.getInt();
m_y = t_zip.getInt();
m_char = t_zip.getInt();
m_color = t_zip.getColor();
m_name = t_zip.getString();
m_blocks = t_zip.getInt();
m_fov_only = t_zip.getInt();
// Components' booleans
bool hasAttacker{ static_cast<bool>(t_zip.getInt()) };
bool hasDestructible{ static_cast<bool>(t_zip.getInt()) };
bool hasAi{ static_cast<bool>(t_zip.getInt()) };
bool hasPickable{ static_cast<bool>(t_zip.getInt()) };
bool hasContainer{ static_cast<bool>(t_zip.getInt()) };
// Components themselves
if (hasAttacker)
{
m_attacker = Attacker::create(t_zip);
}
if (hasDestructible)
{
m_destructible = Destructible::create(t_zip);
}
if (hasAi)
{
m_ai = Ai::create(t_zip);
}
if (hasPickable)
{
m_pickable = Pickable::create(t_zip);
}
if (hasContainer)
{
m_container = std::make_unique<Container>(0);
m_container->load(t_zip);
}
}
void Actor::save(TCODZip& t_zip)
{
// Regular members
t_zip.putInt(m_x);
t_zip.putInt(m_y);
t_zip.putInt(m_char);
t_zip.putColor(&m_color);
t_zip.putString(m_name.c_str());
t_zip.putInt(m_blocks);
t_zip.putInt(m_fov_only);
// Boolean of components
t_zip.putInt(m_attacker != nullptr);
t_zip.putInt(m_destructible != nullptr);
t_zip.putInt(m_ai != nullptr);
t_zip.putInt(m_pickable != nullptr);
t_zip.putInt(m_container != nullptr);
// Components themselves
if (m_attacker)
{
m_attacker->save(t_zip);
}
if (m_destructible)
{
m_destructible->save(t_zip);
}
if (m_ai)
{
m_ai->save(t_zip);
}
if (m_pickable)
{
m_pickable->save(t_zip);
}
if (m_container)
{
m_container->save(t_zip);
}
}
//-------------------------------------------------- actor --------------------------------------------------//
//-------------------------------------------------- CONTAINER --------------------------------------------------//
void Container::load(TCODZip& t_zip)
{
m_size = t_zip.getInt();
int num_actors = t_zip.getInt();
while (num_actors > 0)
{
m_inventory.emplace_back(std::make_unique<Actor>(0, 0, 0, "", TCODColor::white));
m_inventory.back()->load(t_zip);
num_actors--;
}
}
void Container::save(TCODZip& t_zip)
{
t_zip.putInt(m_size);
t_zip.putInt(m_inventory.size());
for (auto it_actor{ m_inventory.begin() }; it_actor < m_inventory.end(); it_actor++)
{
(*it_actor)->save(t_zip);
}
}
//-------------------------------------------------- container --------------------------------------------------//
//-------------------------------------------------- DESTRUCTIBLE --------------------------------------------------//
void Destructible::load(TCODZip& t_zip)
{
m_max_hp = t_zip.getFloat();
m_hp = t_zip.getFloat();
m_corpse_name = t_zip.getString();
m_xp = t_zip.getInt();
m_resistanceModel = ResistanceModel();
for (int i{ 0 }; i < (int)DamageType::MAX_DAMAGE_TYPES; i++) {
m_resistanceModel.setFlatRes((DamageType)i, t_zip.getFloat());
m_resistanceModel.setMultRes((DamageType)i, t_zip.getFloat());
}
}
void Destructible::save(TCODZip& t_zip)
{
t_zip.putFloat(m_max_hp);
t_zip.putFloat(m_hp);
t_zip.putString(m_corpse_name.c_str());
t_zip.putInt(m_xp);
for (int i{ 0 }; i < (int)DamageType::MAX_DAMAGE_TYPES; i++) {
t_zip.putFloat(m_resistanceModel.flatRes((DamageType)i));
t_zip.putFloat(m_resistanceModel.dmgMult((DamageType)i));
}
}
void PlayerDestructible::save(TCODZip& t_zip)
{
t_zip.putInt(DestructibleType::PLAYER);
Destructible::save(t_zip);
}
void MonsterDestructible::save(TCODZip& t_zip)
{
t_zip.putInt(DestructibleType::MONSTER);
Destructible::save(t_zip);
}
std::unique_ptr<Destructible> Destructible::create(TCODZip& t_zip)
{
DestructibleType type{ (DestructibleType)t_zip.getInt() };
std::unique_ptr<Destructible> destructible{ nullptr };
switch (type)
{
case DestructibleType::MONSTER:
destructible = std::make_unique<MonsterDestructible>(0.0f, "", 0, make_DmgArr(0.0f), make_DmgArr(1.0f));
break;
case DestructibleType::PLAYER:
destructible = std::make_unique<PlayerDestructible>(0.0f, "", 0, make_DmgArr(0.0f), make_DmgArr(1.0f));
break;
};
destructible->load(t_zip);
return std::move(destructible);
}
//-------------------------------------------------- destructible --------------------------------------------------//
//-------------------------------------------------- ATTACK --------------------------------------------------//
void Attack::save(TCODZip& t_zip) {
// Save dmg type
t_zip.putInt((int)m_dmgType);
// Save verb str
t_zip.putString(m_verb.c_str());
// Save dice
t_zip.putInt(m_dice.nb_rolls);
t_zip.putInt(m_dice.nb_faces);
t_zip.putFloat(m_dice.multiplier);
t_zip.putFloat(m_dice.addsub);
}
void Attack::load(TCODZip& t_zip) {
// Load dmg type
m_dmgType = (DamageType)t_zip.getInt();
// Load verb str
m_verb = t_zip.getString();
// Load dice
m_dice.nb_rolls = t_zip.getInt();
m_dice.nb_faces = t_zip.getInt();
m_dice.multiplier = t_zip.getFloat();
m_dice.addsub = t_zip.getFloat();
}
//-------------------------------------------------- attack --------------------------------------------------//
//-------------------------------------------------- ATTACKER --------------------------------------------------//
void Attacker::load(TCODZip& t_zip)
{
m_attack = Attack(DamageType::MAX_DAMAGE_TYPES, TCOD_dice_t(), "");
m_attack.load(t_zip);
}
void Attacker::save(TCODZip& t_zip)
{
m_attack.save(t_zip);
}
std::unique_ptr<Attacker> Attacker::create(TCODZip& t_zip) {
std::unique_ptr<Attacker> attacker{ std::make_unique<Attacker>(DamageType::MAX_DAMAGE_TYPES, TCOD_dice_t(), "") };
attacker->load(t_zip);
return std::move(attacker);
}
//-------------------------------------------------- attacker --------------------------------------------------//
//-------------------------------------------------- AI --------------------------------------------------//
void MonsterAi::load(TCODZip& t_zip)
{
m_move_count = t_zip.getInt();
}
void MonsterAi::save(TCODZip& t_zip)
{
t_zip.putInt(static_cast<int>(AiType::MONSTER));
t_zip.putInt(m_move_count);
}
void ConfusedMonsterAi::load(TCODZip& t_zip)
{
m_num_turns = t_zip.getInt();
m_old_ai = Ai::create(t_zip);
}
void ConfusedMonsterAi::save(TCODZip& t_zip)
{
t_zip.putInt(static_cast<int>(AiType::CONFUSED_MONSTER));
t_zip.putInt(m_num_turns);
m_old_ai->save(t_zip);
}
void PlayerAi::load(TCODZip& t_zip)
{
}
void PlayerAi::save(TCODZip& t_zip)
{
t_zip.putInt(static_cast<int>(AiType::PLAYER));
}
std::unique_ptr<Ai> Ai::create(TCODZip& t_zip)
{
AiType type{ (AiType)t_zip.getInt() };
std::unique_ptr<Ai> ai{ nullptr };
switch (type)
{
case AiType::PLAYER:
ai = std::make_unique<PlayerAi>();
break;
case AiType::MONSTER:
ai = std::make_unique<MonsterAi>();
break;
case AiType::CONFUSED_MONSTER:
ai = std::make_unique<ConfusedMonsterAi>(0, nullptr);
break;
};
ai->load(t_zip);
return std::move(ai);
}
//-------------------------------------------------- ai --------------------------------------------------//
//-------------------------------------------------- PICKABLE --------------------------------------------------//
void Healer::load(TCODZip& t_zip)
{
m_amount = t_zip.getFloat();
}
void Healer::save(TCODZip& t_zip)
{
t_zip.putInt(static_cast<int>(PickableType::HEALER));
t_zip.putFloat(m_amount);
}
void LightingBolt::load(TCODZip& t_zip)
{
m_range = t_zip.getFloat();
m_damage = t_zip.getFloat();
}
void LightingBolt::save(TCODZip& t_zip)
{
t_zip.putInt(static_cast<int>(PickableType::LIGHTING_BOLT));
t_zip.putFloat(m_range);
t_zip.putFloat(m_damage);
}
void Confuser::load(TCODZip& t_zip)
{
m_num_turns = t_zip.getInt();
m_range = t_zip.getFloat();
}
void Confuser::save(TCODZip& t_zip)
{
t_zip.putInt(static_cast<int>(PickableType::CONFUSER));
t_zip.putInt(m_num_turns);
t_zip.putFloat(m_range);
}
void FireBall::save(TCODZip& t_zip)
{
t_zip.putInt(static_cast<int>(PickableType::FIREBALL));
t_zip.putFloat(m_range);
t_zip.putFloat(m_damage);
}
std::unique_ptr<Pickable> Pickable::create(TCODZip& t_zip)
{
PickableType type{ (PickableType)t_zip.getInt() };
std::unique_ptr<Pickable> pickable{ nullptr };
switch (type)
{
case PickableType::HEALER:
pickable = std::make_unique<Healer>(0.0f);
break;
case PickableType::LIGHTING_BOLT:
pickable = std::make_unique<LightingBolt>(0.0f, 0.0f);
break;
case PickableType::CONFUSER:
pickable = std::make_unique<Confuser>(0, 0.0f);
break;
case PickableType::FIREBALL:
pickable = std::make_unique<FireBall>(0.0f, 0.0f);
break;
};
pickable->load(t_zip);
return std::move(pickable);
}
//-------------------------------------------------- pickable --------------------------------------------------//
//-------------------------------------------------- GUI --------------------------------------------------//
void Gui::save(TCODZip& t_zip)
{
t_zip.putInt(m_log.size());
for (auto it_msg{ m_log.begin() }; it_msg < m_log.end(); it_msg++)
{
t_zip.putString((*it_msg)->m_text->c_str());
t_zip.putColor(&(*it_msg)->m_color);
}
}
void Gui::load(TCODZip& t_zip)
{
int num_msg{ t_zip.getInt() };
while (num_msg > 0)
{
const char* text{ t_zip.getString() };
TCODColor color{ t_zip.getColor() };
message(color, text);
num_msg--;
}
}
//-------------------------------------------------- gui --------------------------------------------------//
//-------------------------------------------------- ENGINE --------------------------------------------------//
const int SAVEGAME_VERSION{ 0x1100 };
void Engine::load(const bool t_pause)
{
TCODZip zip;
m_gui->m_menu.clear();
// New Game option
m_gui->m_menu.addItem(Menu::MenuItemCode::NEW_GAME, "New Game");
if (utils::fexists("game.sav"))
{
zip.loadFromFile("game.sav");
int version{ zip.getInt() };
if (SAVEGAME_VERSION == version)
{
m_gui->m_menu.addItem(Menu::MenuItemCode::CONTINUE, "Continue");
}
}
// Exit option
m_gui->m_menu.addItem(Menu::MenuItemCode::EXIT, "Exit");
// Ask for user selection
Menu::MenuItemCode menu_item{ m_gui->m_menu.pick(t_pause ? Menu::DisplayMode::PAUSE : Menu::DisplayMode::MAIN) };
if (menu_item == Menu::MenuItemCode::EXIT || menu_item == Menu::MenuItemCode::NONE)
{
// Exit or window closed
exit(0);
}
else if (menu_item == Menu::MenuItemCode::NEW_GAME)
{
term();
init();
}
else
{
// Continue saved game
term();
// Load map
m_level = zip.getInt();
int w{ zip.getInt() };
int h{ zip.getInt() };
m_map = std::make_unique<Map>(w, h);
m_map->load(zip);
// Load player
m_actors.emplace_back(Actor::s_builder.createActor(0, 0, 0, "", TCODColor::white).construct());
m_player = m_actors.back().get();
m_player->load(zip);
// Load stairs
m_actors.emplace_back(Actor::s_builder.createActor(0, 0, 0, "", TCODColor::white).construct());
m_stairs = m_actors.back().get();
m_stairs->load(zip);
// Actors
int num_actors{ zip.getInt() };
while (num_actors > 0)
{
// Create dummies
m_actors.emplace_back(Actor::s_builder.createActor(0, 0, 0, "", TCODColor::white).construct());
m_actors.back()->load(zip);
num_actors--;
}
// Load message log
m_gui->load(zip);
// Force FOV recomputation
m_game_status = Engine::GAME_STATUS::STARTUP;
}
}
void Engine::save()
{
// Handle permadeath
if (m_player->m_destructible->isDead())
{
std::remove("game.sav");
}
else
{
TCODZip zip;
zip.putInt(SAVEGAME_VERSION);
zip.putInt(m_level);
// Save map
zip.putInt(m_map->m_width);
zip.putInt(m_map->m_height);
m_map->save(zip);
// Save player
m_player->save(zip);
// Save stairs
m_stairs->save(zip);
// Save actors (-2 for the player and stairs)
zip.putInt(m_actors.size() - 2);
for (auto it_actor{ m_actors.begin() }; it_actor < m_actors.end(); it_actor++)
{
Actor* ptr_actor{ (*it_actor).get() };
if (ptr_actor != m_player && ptr_actor != m_stairs)
{
(*it_actor)->save(zip);
}
}
// Save message log
m_gui->save(zip);
zip.saveToFile("game.sav");
}
}
//-------------------------------------------------- engine --------------------------------------------------//