-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave_game.cpp
More file actions
402 lines (338 loc) · 16.2 KB
/
save_game.cpp
File metadata and controls
402 lines (338 loc) · 16.2 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
#include "save_game.h"
#include "direct_damage_spell.h"
#include "trap_spell.h"
#include "area_damage_spell.h"
#include "summon_spell.h"
#include "i_levelup_spell.h"
#include <iostream>
bool SaveGame::save(const GameMap& map, EntityManager* entities) {
try {
std::ofstream file(filename, std::ios::binary);
if (!file.is_open()) {
throw std::runtime_error("Ошибка открытия файла сохранения");
}
std::time_t saveTime = std::time(nullptr);
file.write(reinterpret_cast<const char*>(&saveTime), sizeof(saveTime));
if (!savePlayer(file, entities->getPlayer())) return false;
if (!saveMap(file, map)) return false;
if (!saveEntities(file, entities)) return false;
file.close();
std::cout << "Игра сохранена\n";
return true;
} catch (const std::exception& e) {
std::cout << "Ошибка сохранения: " << e.what() << std::endl;
return false;
}
}
bool SaveGame::load(GameMap& map, EntityManager* entities) {
try {
std::ifstream file(filename, std::ios::binary);
if (!file.is_open()) {
throw std::runtime_error("Файла сохранения нет");
}
if (!checkFileIntegrity()) {
throw std::runtime_error("Файл сохранения поврежден");
}
std::time_t savedTime;
file.read(reinterpret_cast<char*>(&savedTime), sizeof(savedTime));
if (!loadPlayer(file, entities)) return false;
if (!loadMap(file, map)) return false;
if (!loadEntities(file, map, entities)) return false;
file.close();
std::cout << "Игра загружена\n";
return true;
} catch (const std::exception& e) {
std::cout << "Ошибка загрузки: " << e.what() << std::endl;
return false;
}
}
bool SaveGame::checkFileIntegrity() {
struct stat fileInfo;
if (stat(filename.c_str(), &fileInfo) != 0) {
return false;
}
std::ifstream file(filename, std::ios::binary);
if (!file.is_open()) {
return false;
}
std::time_t savedTime;
file.read(reinterpret_cast<char*>(&savedTime), sizeof(savedTime));
std::time_t fileModTime = fileInfo.st_mtime;
file.close();
return savedTime == fileModTime;
}
bool SaveGame::savePlayer(std::ofstream& file, Player& player) {
try {
Position playerPos = player.getPosition();
int playerX = playerPos.getX();
int playerY = playerPos.getY();
int playerHealth = player.getHealth();
int playerLevel = player.getLevel();
bool skipNextTurn = player.shouldSkipTurn();
int enemiesKilled = player.getEnemiesKilled();
bool mode = player.isRangedMode();
int playerMaxHealth = player.getMaxHealth();
int damage = player.getdamage();
file.write(reinterpret_cast<const char*>(&playerX), sizeof(playerX));
file.write(reinterpret_cast<const char*>(&playerY), sizeof(playerY));
file.write(reinterpret_cast<const char*>(&playerHealth), sizeof(playerHealth));
file.write(reinterpret_cast<const char*>(&playerLevel), sizeof(playerLevel));
file.write(reinterpret_cast<const char*>(&skipNextTurn), sizeof(skipNextTurn));
file.write(reinterpret_cast<const char*>(&enemiesKilled), sizeof(enemiesKilled));
file.write(reinterpret_cast<const char*>(&mode), sizeof(mode));
file.write(reinterpret_cast<const char*>(&playerMaxHealth), sizeof(playerMaxHealth));
file.write(reinterpret_cast<const char*>(&damage), sizeof(damage));
int spellCount = player.getHand().getSpellCount();
file.write(reinterpret_cast<const char*>(&spellCount), sizeof(spellCount));
for (int i = 0; i < spellCount; i++) {
ISpell* spell = player.getHand().getSpell(i);
std::string spellType = spell->getName();
int typeLength = spellType.size();
file.write(reinterpret_cast<const char*>(&typeLength), sizeof(typeLength));
file.write(spellType.c_str(), typeLength);
}
return true;
} catch (const std::exception& e) {
std::cout << "Ошибка сохранения игрока: " << e.what() << std::endl;
return false;
}
}
bool SaveGame::loadPlayer(std::ifstream& file, EntityManager* entities) {
try {
int playerX, playerY, playerHealth, playerLevel, enemiesKilled, playerMaxHealth, damage;
bool skipNextTurn;
bool mode;
file.read(reinterpret_cast<char*>(&playerX), sizeof(playerX));
file.read(reinterpret_cast<char*>(&playerY), sizeof(playerY));
file.read(reinterpret_cast<char*>(&playerHealth), sizeof(playerHealth));
file.read(reinterpret_cast<char*>(&playerLevel), sizeof(playerLevel));
file.read(reinterpret_cast<char*>(&skipNextTurn), sizeof(skipNextTurn));
file.read(reinterpret_cast<char*>(&enemiesKilled), sizeof(enemiesKilled));
file.read(reinterpret_cast<char*>(&mode), sizeof(mode));
file.read(reinterpret_cast<char*>(&playerMaxHealth), sizeof(playerMaxHealth));
file.read(reinterpret_cast<char*>(&damage), sizeof(damage));
entities->createPlayer(playerX, playerY, playerHealth);
Player& loadedPlayer = entities->getPlayer();
loadedPlayer.setLevel(playerLevel);
loadedPlayer.setSkipTurn(skipNextTurn);
loadedPlayer.setEnemiesKilled(enemiesKilled);
loadedPlayer.setAttackRange(mode);
loadedPlayer.setMaxHealth(playerMaxHealth);
loadedPlayer.setDamage(damage);
if (mode && !loadedPlayer.isRangedMode()) {
loadedPlayer.switchCombatMode();
}
int spellCount;
file.read(reinterpret_cast<char*>(&spellCount), sizeof(spellCount));
for (int i = 0; i < spellCount; i++) {
int typeLength;
file.read(reinterpret_cast<char*>(&typeLength), sizeof(typeLength));
std::string spellType(typeLength, ' ');
file.read(&spellType[0], typeLength);
if (spellType == "Прямой урон") {
loadedPlayer.getHand().addSpell(new DirectDamageSpell());
} else if (spellType == "Область") {
loadedPlayer.getHand().addSpell(new AreaDamageSpell());
} else if (spellType == "Призыв союзника") {
loadedPlayer.getHand().addSpell(new SummonSpell());
} else if (spellType == "Ловушка") {
loadedPlayer.getHand().addSpell(new TrapSpell());
} else if (spellType == "Улучшение уровня") {
loadedPlayer.getHand().addSpell(new ILevelUpSpell());
}
}
return true;
} catch (const std::exception& e) {
std::cout << "Ошибка загрузки игрока: " << e.what() << std::endl;
return false;
}
}
bool SaveGame::saveMap(std::ofstream& file, const GameMap& map) {
try {
int width = map.getWidth();
int height = map.getHeight();
file.write(reinterpret_cast<const char*>(&width), sizeof(width));
file.write(reinterpret_cast<const char*>(&height), sizeof(height));
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
MapCell cell = map.getCell(Position(x, y));
MapCell::Type type = cell.getType();
bool used = cell.isUsed();
int trapDamage = cell.getTrapDamage();
file.write(reinterpret_cast<const char*>(&type), sizeof(type));
file.write(reinterpret_cast<const char*>(&used), sizeof(used));
file.write(reinterpret_cast<const char*>(&trapDamage), sizeof(trapDamage));
}
}
return true;
} catch (const std::exception& e) {
std::cout << "Ошибка сохранения карты: " << e.what() << std::endl;
return false;
}
}
bool SaveGame::loadMap(std::ifstream& file, GameMap& map) {
try {
int width, height;
file.read(reinterpret_cast<char*>(&width), sizeof(width));
file.read(reinterpret_cast<char*>(&height), sizeof(height));
map = GameMap(width, height);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
MapCell::Type type;
bool used;
int trapDamage;
file.read(reinterpret_cast<char*>(&type), sizeof(type));
file.read(reinterpret_cast<char*>(&used), sizeof(used));
file.read(reinterpret_cast<char*>(&trapDamage), sizeof(trapDamage));
MapCell cell(type, used);
cell.setTrapDamage(trapDamage);
map.getCell(Position(x, y)) = cell;
}
}
return true;
} catch (const std::exception& e) {
std::cout << "Ошибка загрузки карты: " << e.what() << std::endl;
return false;
}
}
bool SaveGame::saveEntities(std::ofstream& file, EntityManager* entities) {
try {
int enemyCount = entities->getEnemies().size();
file.write(reinterpret_cast<const char*>(&enemyCount), sizeof(enemyCount));
for (Enemy* enemy : entities->getEnemies()) {
Position pos = enemy->getPosition();
int x = pos.getX();
int y = pos.getY();
int health = enemy->getHealth();
bool enemySkip = enemy->shouldSkipTurn();
int damage = enemy->getDamage();
file.write(reinterpret_cast<const char*>(&x), sizeof(x));
file.write(reinterpret_cast<const char*>(&y), sizeof(y));
file.write(reinterpret_cast<const char*>(&health), sizeof(health));
file.write(reinterpret_cast<const char*>(&enemySkip), sizeof(enemySkip));
file.write(reinterpret_cast<const char*>(&damage), sizeof(damage));
}
int towerCount = entities->getTowers().size();
file.write(reinterpret_cast<const char*>(&towerCount), sizeof(towerCount));
for (EnemyTower* tower : entities->getTowers()) {
Position pos = tower->getPosition();
int x = pos.getX();
int y = pos.getY();
int health = tower->getHealth();
file.write(reinterpret_cast<const char*>(&x), sizeof(x));
file.write(reinterpret_cast<const char*>(&y), sizeof(y));
file.write(reinterpret_cast<const char*>(&health), sizeof(health));
}
int allyCount = entities->getFollowers().size();
file.write(reinterpret_cast<const char*>(&allyCount), sizeof(allyCount));
for (Follower* ally : entities->getFollowers()) {
Position pos = ally->getPosition();
int x = pos.getX();
int y = pos.getY();
int health = ally->getHealth();
int damage = ally->getDamage();
bool allySkip = ally->shouldSkipTurn();
file.write(reinterpret_cast<const char*>(&x), sizeof(x));
file.write(reinterpret_cast<const char*>(&y), sizeof(y));
file.write(reinterpret_cast<const char*>(&health), sizeof(health));
file.write(reinterpret_cast<const char*>(&damage), sizeof(damage));
file.write(reinterpret_cast<const char*>(&allySkip), sizeof(allySkip));
}
int buildingCount = entities->getBuildings().size();
file.write(reinterpret_cast<const char*>(&buildingCount), sizeof(buildingCount));
for (EnemyBuilding* building : entities->getBuildings()) {
Position pos = building->getPosition();
int x = pos.getX();
int y = pos.getY();
int spawnInterval = building->getSpawnInterval();
file.write(reinterpret_cast<const char*>(&x), sizeof(x));
file.write(reinterpret_cast<const char*>(&y), sizeof(y));
file.write(reinterpret_cast<const char*>(&spawnInterval), sizeof(spawnInterval));
}
return true;
} catch (const std::exception& e) {
std::cout << "Ошибка сохранения сущностей: " << e.what() << std::endl;
return false;
}
}
bool SaveGame::loadEntities(std::ifstream& file, GameMap& map, EntityManager* entities) {
try {
Player& loadedPlayer = entities->getPlayer();
Position playerPos = loadedPlayer.getPosition();
if (map.isPositionValid(playerPos)) {
map.getCell(playerPos).setEntity(&loadedPlayer);
map.getCell(playerPos).setUsed(true);
}
int enemyCount;
file.read(reinterpret_cast<char*>(&enemyCount), sizeof(enemyCount));
for (int i = 0; i < enemyCount; i++) {
int x, y, health, damage;
bool enemySkip;
file.read(reinterpret_cast<char*>(&x), sizeof(x));
file.read(reinterpret_cast<char*>(&y), sizeof(y));
file.read(reinterpret_cast<char*>(&health), sizeof(health));
file.read(reinterpret_cast<char*>(&enemySkip), sizeof(enemySkip));
file.read(reinterpret_cast<char*>(&damage), sizeof(damage));
Enemy* newEnemy = new Enemy(x, y, 1, health, damage);
newEnemy->setSkipTurn(enemySkip);
entities->getEnemies().push_back(newEnemy);
Position enemyPos(x, y);
if (map.isPositionValid(enemyPos)) {
map.getCell(enemyPos).setEntity(newEnemy);
map.getCell(enemyPos).setUsed(true);
}
}
int towerCount;
file.read(reinterpret_cast<char*>(&towerCount), sizeof(towerCount));
for (int i = 0; i < towerCount; i++) {
int x, y, health;
file.read(reinterpret_cast<char*>(&x), sizeof(x));
file.read(reinterpret_cast<char*>(&y), sizeof(y));
file.read(reinterpret_cast<char*>(&health), sizeof(health));
EnemyTower* newTower = new EnemyTower(x, y, map.getWidth() - 9);
entities->getTowers().push_back(newTower);
Position towerPos(x, y);
if (map.isPositionValid(towerPos)) {
map.getCell(towerPos).setEntity(newTower);
map.getCell(towerPos).setUsed(true);
}
}
int allyCount;
file.read(reinterpret_cast<char*>(&allyCount), sizeof(allyCount));
for (int i = 0; i < allyCount; i++) {
int x, y, health, damage;
bool allySkip;
file.read(reinterpret_cast<char*>(&x), sizeof(x));
file.read(reinterpret_cast<char*>(&y), sizeof(y));
file.read(reinterpret_cast<char*>(&health), sizeof(health));
file.read(reinterpret_cast<char*>(&damage), sizeof(damage));
file.read(reinterpret_cast<char*>(&allySkip), sizeof(allySkip));
Follower* newAlly = new Follower(x, y, health, damage);
newAlly->setSkipTurn(allySkip);
entities->getFollowers().push_back(newAlly);
Position allyPos(x, y);
if (map.isPositionValid(allyPos)) {
map.getCell(allyPos).setEntity(newAlly);
map.getCell(allyPos).setUsed(true);
}
}
int buildingCount;
file.read(reinterpret_cast<char*>(&buildingCount), sizeof(buildingCount));
for (int i = 0; i < buildingCount; i++) {
int x, y, spawnInterval;
file.read(reinterpret_cast<char*>(&x), sizeof(x));
file.read(reinterpret_cast<char*>(&y), sizeof(y));
file.read(reinterpret_cast<char*>(&spawnInterval), sizeof(spawnInterval));
EnemyBuilding* newBuilding = new EnemyBuilding(x, y, map.getWidth() - 9);
entities->getBuildings().push_back(newBuilding);
Position buildingPos(x, y);
if (map.isPositionValid(buildingPos)) {
map.getCell(buildingPos).setUsed(true);
}
}
return true;
} catch (const std::exception& e) {
std::cout << "Ошибка загрузки сущностей: " << e.what() << std::endl;
return false;
}
}