-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAi.cpp
285 lines (250 loc) · 8.6 KB
/
Ai.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
#include <stdio.h>
#include <math.h>
#include "main.hpp"
// how many turns the monster chases the player after losing sight
static const int TRACKING_TURNS = 3;
Ai *Ai::create(TCODZip &zip) {
AiType type = static_cast<AiType>(zip.getInt());
Ai *ai = nullptr;
switch (type) {
case PLAYER: ai = new PlayerAi(); break;
case MONSTER: ai = new MonsterAi(); break;
case CONFUSED_MONSTER: ai = new ConfusedMonsterAi(0, nullptr); break;
}
ai -> load(zip);
return ai;
}
PlayerAi::PlayerAi() : xpLevel(1) {
}
const int LEVEL_UP_BASE=200;
const int LEVEL_UP_FACTOR=150;
int PlayerAi::getNextLevelXp() {
return LEVEL_UP_BASE + xpLevel*LEVEL_UP_FACTOR;
}
void PlayerAi::update(Actor *owner) {
int levelUpXp = getNextLevelXp();
if (owner -> destructible -> xp >= levelUpXp) {
xpLevel++;
owner->destructible->xp -= levelUpXp;
engine.gui->message(TCODColor::yellow, "Your hacking skills grow stronger! You reached level %d", xpLevel);
engine.gui->menu.clear();
engine.gui->menu.addItem(Menu::REDUNDANCY, "Redundancy (+20HP)");
engine.gui->menu.addItem(Menu::SIGNAL, "Signal (+1 attack)");
engine.gui->menu.addItem(Menu::FIREWALL, "Firewall (+1 defense)");
Menu::MenuItemCode menuItem = engine.gui->menu.pick(Menu::PAUSE);
switch (menuItem) {
case Menu::REDUNDANCY:
owner->destructible->maxHp += 20;
owner->destructible->hp += 20;
break;
case Menu::SIGNAL:
owner->attacker->power += 1;
break;
case Menu::FIREWALL:
owner->destructible->defense += 1;
break;
default: break;
}
}
if ( owner->destructible && owner->destructible->isDead() ) {
return;
}
int dx=0,dy=0;
switch(engine.lastKey.vk) {
case TCODK_UP : dy=-1; break;
case TCODK_DOWN : dy=1; break;
case TCODK_LEFT : dx=-1; break;
case TCODK_RIGHT : dx=1; break;
case TCODK_CHAR : handleActionKey(owner, engine.lastKey.c); break;
default:break;
}
if (dx != 0 || dy != 0) {
engine.gameStatus=Engine::NEW_TURN;
if (moveOrAttack(owner, owner->x+dx,owner->y+dy)) {
engine.map->computeFov();
}
}
}
bool PlayerAi::moveOrAttack(Actor *owner, int targetx, int targety) {
if (engine.map->isWall(targetx,targety)) return false;
// look for living actors to attack
for (auto actor : engine.actors) {
if (actor->destructible && !actor->destructible->isDead() && actor->x == targetx && actor->y == targety) {
owner -> attacker -> attack(owner, actor);
return false;
}
}
// look for corpses or items
for (auto actor : engine.actors) {
bool corpseOrItem = (actor->destructible && actor->destructible->isDead()) || actor->pickable;
if (corpseOrItem && actor->x == targetx && actor->y == targety) {
engine.gui->message(TCODColor::lightGrey,"There's a %s here",actor->name);
}
}
owner->x = targetx;
owner->y = targety;
return true;
}
void PlayerAi::handleActionKey(Actor *owner, int ascii) {
switch (ascii) {
case 'g': //pickup item
{
bool found = false;
for (auto actor : engine.actors) {
if (actor->pickable && actor->x == owner->x && actor->y == owner->y) {
if (actor->pickable->pick(actor,owner)) {
found = true;
engine.gui->message(TCODColor::lightGrey, "You pick up the %s", actor->name);
break;
} else if (! found) {
found = true;
engine.gui->message(TCODColor::red, "Your inventory is full.");
}
}
}
if (!found) {
engine.gui->message(TCODColor::lightGrey, "There's nothing here that you can pick up.");
}
engine.gameStatus = Engine::NEW_TURN;
}
break;
case 'i' : // display inventory
{
Actor *actor = choseFromInventory(owner);
if (actor) {
actor->pickable->use(actor,owner);
engine.gameStatus = Engine::NEW_TURN;
}
}
break;
case 'd': // drop item
{
Actor *actor = choseFromInventory(owner);
if (actor) {
actor->pickable->drop(actor, owner);
engine.gameStatus = Engine::NEW_TURN;
}
}
break;
case '.':
if (engine.lastKey.shift && engine.stairs->x == owner->x && engine.stairs->y == owner->y) {
engine.nextLevel();
} else if (engine.lastKey.shift) {
engine.gui->message(TCODColor::lightGrey, "There are no stairs here.");
}
break;
}
}
Actor *PlayerAi::choseFromInventory(Actor *owner) {
static const int INVENTORY_WIDTH = 50;
static const int INVENTORY_HEIGHT = 28;
static TCODConsole con(INVENTORY_WIDTH, INVENTORY_HEIGHT);
// display the inventory frame
con.setDefaultForeground(TCODColor(200,180,50));
con.printFrame(0,0,INVENTORY_WIDTH,INVENTORY_HEIGHT, true, TCOD_BKGND_DEFAULT, "inventory");
//display the items with their keyboard shortcut
con.setDefaultForeground(TCODColor(200,180,50));
int shortcut = 'a';
int y = 1;
for (auto actor : owner->container->inventory) {
con.print(2,y,"(%c) %s", shortcut, actor->name);
y++;
shortcut++;
}
//blit the inventory console on the root console
TCODConsole::blit(&con, 0, 0, INVENTORY_WIDTH, INVENTORY_HEIGHT,
TCODConsole::root, engine.screenWidth/2 - INVENTORY_WIDTH/2,
engine.screenHeight/2 - INVENTORY_HEIGHT/2);
TCODConsole::flush();
// wait for a key press
TCOD_key_t key;
TCODSystem::waitForEvent(TCOD_EVENT_KEY_PRESS, &key, nullptr, true);
if (key.vk == TCODK_CHAR) {
int actorIndex = key.c - 'a';
if ( actorIndex >= 0 && actorIndex < owner->container->inventory.size()) {
return owner->container->inventory.get(actorIndex);
}
}
return nullptr;
}
void PlayerAi::load(TCODZip &zip) {
}
void PlayerAi::save(TCODZip &zip) {
zip.putInt(PLAYER);
}
void MonsterAi::update(Actor *owner) {
if (owner->destructible && owner->destructible->isDead()) {
return;
}
if (engine.map->isInFov(owner->x, owner->y)) {
// we can see the player, move towards it
moveCount = TRACKING_TURNS;
} else {
moveCount--;
}
if (moveCount > 0) {
moveOrAttack(owner, engine.player->x, engine.player->y);
}
}
void MonsterAi::moveOrAttack(Actor *owner, int targetx, int targety) {
int dx = targetx - owner->x;
int dy = targety - owner->y;
int stepdx = dx > 0 ? 1 : -1;
int stepdy = dy > 0 ? 1 : -1;
float distance = sqrtf(dx*dx+dy*dy);
if (distance >= 2) {
dx = (int)(round(dx / distance));
dy = (int)(round(dy / distance));
if (engine.map->canWalk(owner->x+dx, owner->y+dy)) {
owner->x += dx;
owner->y += dy;
} else if (engine.map->canWalk(owner->x+stepdx, owner->y)) {
owner->x += stepdx;
} else if (engine.map->canWalk(owner->x, owner->y+stepdy)) {
owner->y += stepdy;
}
} else if (owner->attacker) {
owner -> attacker -> attack(owner,engine.player);
}
}
void MonsterAi::load(TCODZip &zip) {
moveCount = zip.getInt();
}
void MonsterAi::save(TCODZip &zip) {
zip.putInt(MONSTER);
zip.putInt(moveCount);
}
ConfusedMonsterAi::ConfusedMonsterAi(int nbTurns, Ai *oldAi) : nbTurns(nbTurns), oldAi(oldAi) {
}
void ConfusedMonsterAi::update(Actor *owner) {
TCODRandom *rng = TCODRandom::getInstance();
int dx = rng->getInt(-1,1);
int dy = rng-> getInt(-1,1);
if (dx != 0 || dy != 0) {
int destx = owner->x+dx;
int desty = owner->y+dy;
if (engine.map->canWalk(destx,desty) ) {
owner->x = destx;
owner->y = desty;
} else {
Actor *actor = engine.getActor(destx,desty);
if (actor) {
owner->attacker->attack(owner, actor);
}
}
}
nbTurns--;
if (nbTurns == 0) {
owner->ai = oldAi;
delete this;
}
}
void ConfusedMonsterAi::load(TCODZip &zip) {
nbTurns = zip.getInt();
oldAi = Ai::create(zip);
}
void ConfusedMonsterAi::save(TCODZip &zip) {
zip.putInt(CONFUSED_MONSTER);
zip.putInt(nbTurns);
oldAi->save(zip);
}