-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGame.cc
More file actions
322 lines (303 loc) · 11.1 KB
/
Copy pathGame.cc
File metadata and controls
322 lines (303 loc) · 11.1 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
#ifndef GAME_CC
#define GAME_CC
#include <algorithm>
#include <ctime>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include "Game.h"
#include "AnsiRenderer.h"
#include "EffectManager.h"
#include "Quest.h"
#include "QuestManager.h"
void Game::processCommand() {
std::cout << "Enter a command: ";
std::string command;
std::getline(std::cin, command);
dialog.clear();
if (command == "r") {
state = GameState::RESTART;
return;
}
if (command == "q") {
state = GameState::QUIT;
return;
}
std::istringstream iss(command);
std::string action, dirStr;
iss >> action >> dirStr;
if (moveCommands.count(command)) {
Direction dir=toDirection(command);
board->movePc(dir);
}
else if (action == "u" && moveCommands.count(dirStr)) {
auto dir=toDirection(dirStr);
board->pickUp(dir);
}
else if (action == "a" && moveCommands.count(dirStr)) {
auto pcTile = board->getPc();
auto dir = toDirection(dirStr);
auto pos = board->convertDirection(*pcTile, dir);
auto enemyTile = board->getTile(pos);
if (enemyTile && TypeCategories::isEnemy(enemyTile->getType())) {
auto enemy = std::dynamic_pointer_cast<Enemy>(enemyTile->getEntity());
if (enemy) board->attackEnemy(*enemyTile);
}
}
else {
std::cout << "Invalid command!" << std::endl;
}
board->updateEnemies();
dialog=board->diaHelp();
}
void Game::quit() {
std::cout << "Goodbye!" << std::endl;
return;
}
void Game::victory() {
auto player=std::dynamic_pointer_cast<PlayerCharacter>(board->getPc()->getEntity());
if (!player) { state = GameState::QUIT; return; }
int gold = player->getInfo().find("Gold")->second;
double score = gold*2;
if(player->getRace()==Race::HUMAN) score*=1.5;
std::cout << "Congratulations! You have reached the top." << std::endl;
std::cout << "Score: " << score << " Gold: "<< gold << std::endl;
std::cout << "Would you like to play again? (y/n)" << std::endl;
std::string command;
std::getline(std::cin, command);
if (command == "y") {
state = GameState::RESTART;
} else {
state = GameState::QUIT;
}
}
void Game::defeat() {
std::cout << "Game over! You died." << std::endl;
std::cout << "Would you like to play again? (y/n)" << std::endl;
std::string command;
std::getline(std::cin, command);
if (command == "y") {
state = GameState::RESTART;
} else {
state = GameState::QUIT;
}
}
bool Game::gameOver() {
auto player=std::dynamic_pointer_cast<PlayerCharacter>(board->getPc()->getEntity());
if (!player) return true;
if (!player->isAlive()) {
state = GameState::DEFEAT;
return true;
}else if (filename!="./files/default.txt"&&board->getFloorId()==board->getMaxFloorId()) {
state = GameState::VICTORY;
return true;
}else if(filename=="./files/default.txt"&&board->getFloorId()==5){
state = GameState::VICTORY;
return true;
}else
return false;
}
Game::Game(int seed, std::string fn, bool wealtherEnabled, bool questEnabled,
std::string savePath_, std::optional<cc3k::SaveData> pendingLoad_)
: board(nullptr), state(GameState::RESTART), filename(fn),
wealtherEnabled(wealtherEnabled), questEnabled(questEnabled),
seedValue(static_cast<std::uint32_t>(seed)),
savePath(std::move(savePath_)),
pendingLoad(std::move(pendingLoad_)),
renderer(std::make_unique<cc3k::AnsiRenderer>(std::cout)) {
PRNG::seed(seedValue);
if (questEnabled) {
initializeQuests();
}
}
void Game::writeSaveIfRequested() {
if (savePath.empty() || !board) return;
auto player = std::dynamic_pointer_cast<PlayerCharacter>(board->getPc()->getEntity());
if (!player) return;
cc3k::SaveData d;
d.seed = seedValue;
d.filename = filename;
d.weatherEnabled = wealtherEnabled;
d.questEnabled = questEnabled;
d.floorId = board->getFloorId();
d.player.race = static_cast<int>(player->getRace());
d.player.maxHp = player->getMaxHp();
d.player.hp = player->getHp();
d.player.atk = player->getAtk();
d.player.def = player->getDef();
d.player.gold = player->getGold();
d.player.score = player->getScore();
d.player.goldModifier = player->getGoldModifier();
d.player.hasBarrierSuit = player->hasSuit();
d.player.visibility = player->getVisibility();
d.player.movementSpeed = player->getMovementSpeed();
if (cc3k::save(savePath, d)) {
std::cout << "[cc3k] saved to " << savePath << std::endl;
} else {
std::cerr << "[cc3k] failed to write save file: " << savePath << std::endl;
}
}
void Game::restart() {
board=std::make_shared<Board>(filename,0); //filename is "files/potions.txt", ctor for board is Board(std::string filename = "./files/default.txt", int floorId = 0);
board->setFloorId(0);
board->setCompassPickedUp(false);
board->setMaxFloorId(filename);
if(filename!="./files/default.txt"){
board->loadBoard(board->getFloorId(),filename,toTypeBoard);
board->initByFile(board->getFloorId());
}else{
board->loadBoard(board->getFloorId(),filename,toTypeBoard);
board->initFloor();
}
// Render the populated board and proper HUD now that the player exists.
renderer->drawInitialBoard(*board);
state = GameState::PLAYING;
std::cout << "Game started." << std::endl;
auto entity = board->getPc()->getEntity();
auto player = std::dynamic_pointer_cast<PlayerCharacter>(entity);
if (!player) {
std::cerr << "Error: No valid player character found!" << std::endl;
return;
}
if (pendingLoad) {
// Restore player snapshot and replay floor advancement so the
// dungeon is regenerated to the saved floor under the loaded seed.
const auto& s = pendingLoad->player;
player->applyLoadedState(static_cast<Race>(s.race),
s.maxHp, s.hp, s.atk, s.def, s.gold, s.score,
s.goldModifier, s.hasBarrierSuit,
s.visibility, s.movementSpeed);
board->getPc()->setEntity(player);
std::cout << "[cc3k] loaded save: floor=" << (pendingLoad->floorId + 1)
<< " race=" << toString(player->getRace())
<< " gold=" << player->getGold() << std::endl;
for (int i = 0; i < pendingLoad->floorId; ++i) {
if (!board->nextFloor()) break;
}
pendingLoad.reset(); // only apply once; subsequent restarts are normal
return;
}
std::cout << "Choose your race h[Human]|e[Elf]|o[Orc]|d[Dwarf]: ";
char c;
std::cin >> c;
while(true){
switch(c) {
case 'h': player->setAttributes(Race::HUMAN);break;
case 'e': player->setAttributes(Race::ELF);break;
case 'o': player->setAttributes(Race::ORC);break;
case 'd': player->setAttributes(Race::DWARF);break;
default:
std::cout << "Only h[Human]|e[Elf]|o[Orc]|d[Dwarf]: ";
std::cin >> c; // Prompt for new input
continue; // Skip the rest and restart the loop
}
break;
}
if(wealtherEnabled) player->setVisibility(8);
board->getPc()->setEntity(player);
}
void Game::renderInfo() {
if(!board) return;
auto player = std::dynamic_pointer_cast<PlayerCharacter>(board->getPc()->getEntity());
if(!player) return;
cc3k::HudInfo hud;
hud.race = toString(player->getRace());
hud.gold = player->getGold();
hud.floor = board->getFloorId() + 1;
hud.hp = player->getHp();
hud.atk = player->getAtk();
hud.def = player->getDef();
hud.action = dialog;
hud.questEnabled = questEnabled;
if (questEnabled) {
const auto& q = QuestManager::getInstance()->getActiveQuests();
hud.activeQuests.reserve(q.size());
for (const auto& quest : q) hud.activeQuests.push_back(quest->getDescription());
}
hud.weatherEnabled = wealtherEnabled;
if (wealtherEnabled) {
hud.weather = EffectManager::getInstance()->getCurrentWeatherDescription();
hud.movementSpeed = player->getMovementSpeed();
}
renderer->drawHud(hud);
}
void Game::renderBoard() {
if(board) renderer->drawBoard(*board);
}
void Game::run() {
while (state!=QUIT) {
switch (state) {
case GameState::PLAYING:
if(wealtherEnabled){
++loopCounter;
std::cout<<loopCounter<<std::endl;
if(loopCounter%5==0) {
auto weatherManager=EffectManager::getInstance();
if(PRNG::randInt(2) || weatherManager->getWeatherEffectsCnt()>1) EffectManager::getInstance()->clearWealtherEffects();
auto weather = generateWeather();
auto player=std::dynamic_pointer_cast<PlayerCharacter>(board->getPc()->getEntity());
weather->apply(player);
EffectManager::getInstance()->addWeatherEffect(std::move(weather));
}
}
processCommand();
if (gameOver()) {
break;
}
renderBoard();
renderInfo();
updateQuests();
break;
case GameState::VICTORY:
victory();
break;
case GameState::DEFEAT:
defeat();
break;
case GameState::RESTART:
restart();
break;
case GameState::QUIT:
quit();
break;
default:
std::cerr << "Unknown game state!" << std::endl;
break;
}
}
// Write the save AFTER the main loop. The loop exits when state == QUIT
// (the case above may never be reached because the while-condition is
// checked before the switch on the iteration that flips state to QUIT).
writeSaveIfRequested();
}
void Game::initializeQuests() {
auto questManager = QuestManager::getInstance();
questManager->addQuest(std::make_unique<KillQuest>(
Type::GOBLIN, 5,
"Defeat 5 Goblins (Reward: Small Gold Hoard)"
));
questManager->addQuest(std::make_unique<CollectQuest>(
Type::RH, 1,
"Collect 1 Health Potion (Reward: Restore Health Potion)"
));
questManager->addQuest(std::make_unique<CollectQuest>(
Type::PH, 1,
"Collect 1 Poison Potion (Reward: Restore Health Potion)"
));
questManager->addQuest(std::make_unique<KillQuest>(
Type::VAMPIRE, 1,
"Defeat 1 Vampire (Reward: Small Gold Hoard)"
));
questManager->addQuest(std::make_unique<CollectQuest>(
Type::BARRIER_SUIT, 1,
"Find the Barrier Suit (Reward: Restore Health Potion)"
));
}
void Game::updateQuests() {
if (questEnabled) {
auto player = std::dynamic_pointer_cast<PlayerCharacter>(board->getPc()->getEntity());
QuestManager::getInstance()->updateQuests(player);
}
}
#endif