-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
697 lines (621 loc) · 22.5 KB
/
Copy pathmain.cpp
File metadata and controls
697 lines (621 loc) · 22.5 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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
#include <iostream>
#include <vector>
#include <string>
#include <chrono>
#include <thread>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <limits>
#include <fstream>
#include <functional>
#include <random>
#include <cctype>
#include "io.h"
using namespace std;
// —— Configuration ——
const int ROW_W = 4;
const int FRAME_TIME = 250;
// —— Forward Declarations ——
struct Monster;
struct Tower;
struct Projectile;
struct Item;
void damageUpgrade();
void arcBoostThisWave();
void placeSlowTrap(double seconds);
void roadBomb();
void placeFrostTowerItem();
void clearScreen();
void initPlusCols();
void draw();
void spawnMon();
void moveMon();
void updateProjectiles();
void fire(int tid, Tower& t);
void updateTowers(double dt);
void placeTower();
void waitPlacement();
bool isOccupied(int r, int c);
void usePrepareItems();
void showShop();
// —— Data Structures ——
struct Monster {
double row, col;
int hp;
bool alive, isElite;
double slowTimer = 0.0;
};
struct Tower {
int row, col, damage;
bool isAOE;
double rate, cooldown;
bool isFrost = false;
};
struct Projectile {
int target, owner;
double row, col;
char ch;
int damage;
bool alive;
};
enum ItemType { Immediate, Prepare };
struct Item {
string name;
int cost;
ItemType type;
function<void()> applyImmediate;
function<void()> applyPrepare;
};
// —— Global Variables ——
int playerHP, playerGold;
int spawnCd, waveNumber, toSpawn, baseHP;
bool paused;
int difficulty; // Difficulty
int maxHP; // Maximum HP
double goldMultiplier; // Current wave gold multiplier
double goldMultiplierNextWave;// Next wave gold multiplier
int arcBoostThisWaveCount; // Additional targets for Arc towers this wave
vector<Tower> towers;
vector<Monster> monsters;
vector<Projectile> projectiles;
vector<Item> allItems;
vector<Item> inventoryPrepare;
int damageUpgradeCount = 0;
bool arcBoostThisWaveFlag = false;
double slowTrapRemaining = 0.0;
void startNewGame(int diff) {
difficulty = diff;
maxHP = 10;
playerHP = maxHP;
playerGold = 100;
goldMultiplier = goldMultiplierNextWave = 1.0;
arcBoostThisWaveCount = 0;
towers.clear();
monsters.clear();
projectiles.clear();
waveNumber = 0;
}
// Map and Coordinates
vector<string> gameMap = {
" + + + + + + + + + + + + + ",
"--------------------------------------------------------",
"S |",
"S |",
"S |",
"S |",
"S |",
"--------------------------------------------------------",
" + + + + + + + + + + + + + ",
"--------------------------------------------------------",
"| |",
"| |",
"| |",
"| |",
"| |",
"--------------------------------------------------------",
" + + + + + + + + + + + + + ",
"--------------------------------------------------------",
"| H",
"| H",
"| H",
"| H",
"| H",
"--------------------------------------------------------",
" + + + + + + + + + + + + + "
};
vector<int> plusCols;
vector<int> plusRows = { 0,8,16,24 };
// —— Implementation ——
// Item Effects
void damageUpgrade() {
++damageUpgradeCount;
for (auto& t : towers) t.damage += 1;
}
void arcBoostThisWave() {
arcBoostThisWaveFlag = true;
}
void placeSlowTrap(double seconds) {
slowTrapRemaining = seconds;
}
void roadBomb() {
for (auto& m : monsters) if (m.alive) m.hp = 0;
}
void placeFrostTowerItem() {
int r; char lc;
cout << "Select frost tower row (0/8/16/24): "; cin >> r; cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Select frost tower column (0-9,A-C): "; cin >> lc; cin.ignore(numeric_limits<streamsize>::max(), '\n');
int idx = isdigit(lc) ? lc - '0' : toupper(lc) - 'A' + 10;
int col = plusCols[idx];
if (isOccupied(r, col)) {
cout << "This position is already occupied!\n";
return;
}
towers.push_back({ r,col,1,true,1.0,0.0,true });
}
// Utilities
void clearScreen() {
cout << "\x1B[2J\x1B[H";
}
void initPlusCols() {
plusCols.clear();
for (int c = 0; c < (int)gameMap[0].size(); ++c)
if (gameMap[0][c] == '+')
plusCols.push_back(c);
}
bool isOccupied(int r, int c) {
for (auto& t : towers)
if (t.row == r && t.col == c) return true;
return false;
}
// Rendering
void draw() {
clearScreen();
// Top numbering
cout << string(ROW_W, ' ');
for (int c = 0;c < (int)gameMap[0].size();++c) {
if (gameMap[0][c] == '+') {
int idx = find(plusCols.begin(), plusCols.end(), c) - plusCols.begin();
cout << char(idx < 10 ? '0' + idx : 'A' + idx - 10);
}
else cout << ' ';
}
cout << "\n";
// Make a copy
auto buf = gameMap;
// Towers
for (auto& t : towers)
buf[t.row][t.col] = t.isFrost ? 'F' : (t.isAOE ? 'E' : 'A');
// Monsters
for (auto& m : monsters)
if (m.alive)
buf[(int)round(m.row)][(int)round(m.col)] = m.isElite ? 'E' : 'M';
// Projectiles
for (auto& p : projectiles) if (p.alive) {
int pr = (int)round(p.row), pc = (int)round(p.col);
if (pr >= 0 && pr < (int)buf.size() && pc >= 0 && pc < (int)buf[0].size())
buf[pr][pc] = p.ch;
}
// Output rows
for (int r = 0;r < (int)buf.size();++r) {
bool show = find(plusRows.begin(), plusRows.end(), r) != plusRows.end();
if (show) cout << setw(3) << r << ' ';
else cout << " ";
cout << buf[r] << "\n";
}
cout << "HP:" << playerHP << "/" << maxHP
<< " Gold:" << playerGold
<< " Wave:" << waveNumber
<< (paused ? " [Paused]" : "")
<< " Diff:" << (difficulty == 1 ? "Easy" : difficulty == 2 ? "Normal" : "Hard")
<< "\n";
}
// Monster Spawning and Movement
void spawnMon() {
if (toSpawn > 0 && spawnCd == 0) {
int rs[3] = { 3,4,5 };
int r = rs[rand() % 3];
bool e = (rand() % 5) == 0;
monsters.push_back({ (double)r,0.0,baseHP * (e ? 2 : 1),true,e });
--toSpawn; spawnCd = 6;
}
else if (spawnCd > 0) --spawnCd;
}
void moveMon() {
for (auto& m : monsters) if (m.alive) {
double speed = 1.0;
if (m.slowTimer > 0) {
speed *= 0.5;
m.slowTimer = max(0.0, m.slowTimer - FRAME_TIME / 1000.0);
}
m.col += speed;
if ((int)m.col >= (int)gameMap[(int)m.row].size() - 1) {
if (m.row < 16) { m.row += 8; m.col = 0; }
else { --playerHP; m.alive = false; }
}
}
}
// Tower Firing
void fire(int tid, Tower& t) {
int idx = find(plusRows.begin(), plusRows.end(), t.row) - plusRows.begin();
vector<int> segs;
if (idx > 0) segs.push_back(idx - 1);
if (idx < (int)plusRows.size() - 1) segs.push_back(idx);
if (!t.isAOE) {
for (auto& p : projectiles)
if (p.owner == tid && p.alive) return;
}
vector<pair<double, int>> cand;
for (int i = 0;i < (int)monsters.size();++i) {
auto& m = monsters[i];
if (!m.alive) continue;
int seg = -1;
for (int j = 0;j + 1 < (int)plusRows.size();++j)
if (m.row > plusRows[j] && m.row < plusRows[j + 1]) { seg = j; break; }
if (find(segs.begin(), segs.end(), seg) == segs.end()) continue;
double d = fabs(m.row - t.row) + fabs(m.col - t.col);
cand.emplace_back(d, i);
}
if (cand.empty()) return;
sort(cand.begin(), cand.end());
char ch = t.isFrost ? '*' : (t.isAOE ? '~' : '.');
int baseShots = t.isAOE ? 2 : 1;
int shots = min((int)cand.size(), baseShots + arcBoostThisWaveCount);
for (int k = 0;k < shots;++k) {
int mi = cand[k].second;
projectiles.push_back({ mi,tid,(double)t.row,(double)t.col,ch,t.damage,true });
}
t.cooldown = 1.0 / t.rate;
}
void updateTowers(double dt) {
for (int i = 0;i < (int)towers.size();++i) {
towers[i].cooldown -= dt;
if (towers[i].cooldown <= 0) fire(i, towers[i]);
}
}
// Projectile Updates
void updateProjectiles() {
const double speed = 2.0;
for (auto& p : projectiles) if (p.alive) {
if (p.target < 0 || p.target >= (int)monsters.size() || !monsters[p.target].alive) {
p.alive = false; continue;
}
auto& m = monsters[p.target];
double dx = m.row - p.row, dy = m.col - p.col;
double dist = sqrt(dx * dx + dy * dy);
if (dist <= speed) { p.row = m.row; p.col = m.col; }
else {
p.row += dx / dist * speed;
p.col += dy / dist * speed;
}
int pr = (int)round(p.row), pc = (int)round(p.col);
if (pr == (int)round(m.row) && pc == (int)round(m.col)) {
m.hp -= p.damage;
if (m.hp <= 0) {
m.alive = false;
int drop = m.isElite ? 10 : 5;
playerGold += int(drop * goldMultiplier);
}
if (p.ch == '*') m.slowTimer = 2.0;
p.alive = false;
}
}
projectiles.erase(
remove_if(projectiles.begin(), projectiles.end(), [](auto& p) {return !p.alive;}),
projectiles.end()
);
}
// Placement Phase
void placeTower() {
draw();
char ch;
// —— Modified here ——
do {
cout << "Place Tower: (1) Arrow (20) 2) Arc (30)\n";
cin >> ch;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (ch != '1' && ch != '2') {
cout << "Invalid selection, please enter 1 or 2.\n";
}
} while (ch != '1' && ch != '2');
int cost = (ch == '2') ? 30 : 20;
// —— Check if enough gold, return if not ——
if (playerGold < cost) {
cout << "Not enough gold!\n";
this_thread::sleep_for(chrono::milliseconds(800));
return;
}
int r;
do {
cout << "Row? (0/8/16/24) ";
cin >> r; cin.ignore(numeric_limits<streamsize>::max(), '\n');
} while (find(plusRows.begin(), plusRows.end(), r) == plusRows.end());
char lc; int idx, col;
do {
cout << "Col? (0-9,A-C) ";
cin >> lc; cin.ignore(numeric_limits<streamsize>::max(), '\n');
idx = isdigit(lc) ? lc - '0' : toupper(lc) - 'A' + 10;
} while (idx < 0 || idx >= (int)plusCols.size());
col = plusCols[idx];
if (isOccupied(r, col)) {
cout << "This position is already occupied!\n";
this_thread::sleep_for(chrono::milliseconds(800));
return;
}
towers.push_back({ r,col,(ch == '2' ? 1 : 2),(ch == '2'),(ch == '2' ? 1.0 : 2.0),0.0,false });
playerGold -= cost;
}
// Wait for Placement
void waitPlacement() {
draw();
cout << "Placement Phase:\n [Enter] Start Wave [b] Place Tower [i] Inventory\n";
string s;
while (true) {
getline(cin, s);
if (s.empty()) break;
char c = tolower(s[0]);
if (c == 'b') placeTower();
else if (c == 'i') usePrepareItems();
draw();
cout << "Placement Phase:\n [Enter] Start Wave [b] Place Tower [i] Inventory\n";
}
}
// Inventory and Shop
void usePrepareItems() {
if (inventoryPrepare.empty()) {
cout << "Inventory is empty!\n";
return;
}
while (true) {
cout << "\n=== Inventory ===\n";
for (int i = 0; i < (int)inventoryPrepare.size(); ++i)
cout << i + 1 << ") " << inventoryPrepare[i].name << "\n";
cout << "0) Return\nSelect: ";
int sel;
cin >> sel;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (sel == 0) break;
if (sel < 1 || sel >(int)inventoryPrepare.size()) continue;
// Record current tower count
size_t beforeTowers = towers.size();
// Try to use the item
inventoryPrepare[sel - 1].applyPrepare();
// If it's "Frost Fortress", check if tower placement was successful
if (inventoryPrepare[sel - 1].name == "Frost Fortress (Place frost tower)") {
if (towers.size() > beforeTowers) {
// Placement successful: remove from inventory
inventoryPrepare.erase(inventoryPrepare.begin() + (sel - 1));
}
else {
// Placement failed: position occupied, keep in inventory
cout << "Placement failed, position already occupied, returned to inventory.\n";
this_thread::sleep_for(chrono::milliseconds(800));
}
}
else {
// Other prepare-type items are consumed directly
inventoryPrepare.erase(inventoryPrepare.begin() + (sel - 1));
}
break;
}
}
void showShop() {
static mt19937 rng((unsigned)time(nullptr));
if (allItems.size() < 3) {
// Initialize item list once
allItems = {
// **Healing Item**
{ "Healing Potion (Restore 10HP)", 50, Immediate,
[]() {
playerHP = min(playerHP + 10, maxHP);
}, nullptr
},
// **Increase Max HP**
{ "Sturdy Amulet (+5 Max HP)", 80, Immediate,
[]() {
maxHP += 5;
playerHP = maxHP;
}, nullptr
},
// **Arc Tower +1 Target This Wave**
{ "Rift Arc+ (+1 Target)", 75, Immediate,
[]() {
// Immediately give Arc towers +1 target this wave
arcBoostThisWaveCount += 1;
},
nullptr
},
// **All Towers Permanent +1 Damage**
{ "Destruction Amplifier (+1 All Tower Damage)",100, Immediate,
[]() {
for (auto& t : towers) t.damage += 1;
// Future towers also get +1, can add damageUpgradeCount variable for new towers
}, nullptr
},
// **Next Wave Gold Income ×2**
{ "Wealth Badge+ (Next Wave Gold *2)",120, Prepare,
nullptr,
[]() {
goldMultiplierNextWave = 2.0;
}
},
// **Place Frost Tower**
{ "Frost Fortress (Place frost tower)",150, Prepare,
nullptr,
placeFrostTowerItem
}
};
}
shuffle(allItems.begin(), allItems.end(), rng);
vector<Item> offer(allItems.begin(), allItems.begin() + 3);
while (true) {
clearScreen();
cout << "\n--- Shop ---\nCurrent Gold: " << playerGold << "\n";
// Only show remaining items in current offer
for (int i = 0; i < (int)offer.size(); ++i) {
cout << (i + 1) << ") " << offer[i].name
<< " Price:" << offer[i].cost << "\n";
}
cout << "0) Exit Shop\nSelect: ";
int sel;
cin >> sel;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (sel == 0) {
clearScreen();
break;
}
if (sel < 1 || sel >(int)offer.size()) {
cout << "Invalid selection\n";
this_thread::sleep_for(chrono::milliseconds(500));
continue;
}
// Player selected offer[sel-1]
Item it = offer[sel - 1];
if (playerGold < it.cost) {
cout << "Not enough gold!\n";
this_thread::sleep_for(chrono::milliseconds(500));
continue;
}
// Deduct gold and apply item
playerGold -= it.cost;
if (it.type == Immediate) {
it.applyImmediate();
}
else {
inventoryPrepare.push_back(it);
}
// **Key step**: Remove purchased item from current offer
offer.erase(offer.begin() + (sel - 1));
cout << "Purchased: " << it.name << "\n";
this_thread::sleep_for(chrono::milliseconds(500));
// If offer is empty, exit shop
if (offer.empty()) {
clearScreen();
break;
}
}
}
// Main Loop
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
srand((unsigned)time(nullptr));
initPlusCols();
while (true) {
clearScreen();
cout << "=== Tower Defense ===\n"
<< "1) New Game\n"
<< "2) Load Slot 1" << (ifstream(slotFiles[0]) ? "\n" : " (empty)\n")
<< "3) Load Slot 2" << (ifstream(slotFiles[1]) ? "\n" : " (empty)\n")
<< "4) Load Slot 3" << (ifstream(slotFiles[2]) ? "\n" : " (empty)\n")
<< "5) Quit\n"
<< "Select: ";
int choice; cin >> choice; cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (choice == 5) break;
if (choice >= 2 && choice <= 4) {
if (!loadGame(choice - 2,
difficulty,
playerHP,
playerGold,
waveNumber))
{
cout << "Load failed, press Enter to continue";
cin.get();
continue;
}
}
else if (choice == 1) {
cout << "Select Difficulty (1-Easy 2-Normal 3-Hard): ";
cin >> difficulty; cin.ignore(numeric_limits<streamsize>::max(), '\n');
startNewGame(difficulty);
}
else continue;
clearScreen();
cout << "Press Enter to enter placement phase..."; cin.get();
waitPlacement();
auto last = chrono::steady_clock::now();
bool running = true;
while (running && playerHP > 0) {
arcBoostThisWaveFlag = false;
arcBoostThisWaveCount = 0;
goldMultiplier = goldMultiplierNextWave;
goldMultiplierNextWave = 1.0;
++waveNumber;
draw();
baseHP = 5 + (waveNumber - 1) * 2;
toSpawn = waveNumber * (difficulty == 1 ? 3 : (difficulty == 2 ? 5 : 7));
spawnCd = 0; monsters.clear(); projectiles.clear();
while (running && playerHP > 0 &&
(toSpawn > 0 || any_of(monsters.begin(), monsters.end(), [](auto& m) {return m.alive;})))
{
if (cin.rdbuf()->in_avail() > 0) {
char cmd = cin.get();
if (cmd == 'b' || cmd == 'B') placeTower();
else if (cmd == 'p' || cmd == 'P') paused = !paused;
}
if (paused) {
draw();
this_thread::sleep_for(chrono::milliseconds(100));
continue;
}
auto now = chrono::steady_clock::now();
double dt = chrono::duration<double>(now - last).count();
last = now;
spawnMon();
moveMon();
updateTowers(dt);
updateProjectiles();
draw();
this_thread::sleep_for(chrono::milliseconds(FRAME_TIME));
monsters.erase(
remove_if(monsters.begin(), monsters.end(), [](auto& m) {return !m.alive;}),
monsters.end()
);
}
if (playerHP <= 0) break;
// Check whether it has aalready achieved the maximum round
int maxWaves = (difficulty == 1) ? 3 : (difficulty == 2) ? 5 : 7;
if (waveNumber >= maxWaves) {
draw();
cout << "===== VICTORY! =====\n";
cout << "Congratulations! You've successfully defended against all " << maxWaves << " waves!\n";
cout << "Final Stats:\n";
cout << "- Difficulty: " << (difficulty == 1 ? "Easy" : difficulty == 2 ? "Normal" : "Hard") << "\n";
cout << "- HP Remaining: " << playerHP << "/" << maxHP << "\n";
cout << "- Gold Remaining: " << playerGold << "\n";
cout << "- Towers Built: " << towers.size() << "\n";
cout << "\nPress Enter to return to main menu...";
cin.get();
break; // Quit the game cycle
}
char post;
do {
draw();
cout << "Wave " << waveNumber << " completed! S)Save N)Next Wave Q)Quit\nSelect: ";
cin >> post; cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (post == 'S' || post == 's') {
cout << "Save slot (1-3): ";
int sl; cin >> sl; cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (sl >= 1 && sl <= 3) {
saveGame(sl - 1,
difficulty,
playerHP,
playerGold,
waveNumber);
cout << "Saved\n";
}
}
} while (post == 'S' || post == 's');
if (post == 'Q' || post == 'q') running = false;
if (running) {
showShop();
cout << "Shop closed, press Enter to continue placement";cin.get();
waitPlacement();
}
}
if (playerHP <= 0) { clearScreen(); cout << "Game Over! Press Enter to exit"; cin.get(); }
}
return 0;
}