forked from miki151/keeperrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
743 lines (693 loc) · 27.4 KB
/
player.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
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
#include "stdafx.h"
#include "player.h"
#include "location.h"
#include "level.h"
#include "message_buffer.h"
#include "ranged_weapon.h"
#include "name_generator.h"
#include "model.h"
#include "options.h"
using namespace std;
Player::Player(Creature* c, View* v, Model* m, bool greet, map<const Level*, MapMemory>* memory) :
creature(c), view(v), displayGreeting(greet), levelMemory(memory), model(m) {
EventListener::addListener(this);
}
Player::~Player() {
EventListener::removeListener(this);
}
const Level* Player::getListenerLevel() const {
return creature->getLevel();
}
void Player::onThrowEvent(const Creature* thrower, const Item* item, const vector<Vec2>& trajectory) {
for (Vec2 v : trajectory)
if (creature->canSee(v)) {
view->animateObject(trajectory, item->getViewObject());
return;
}
}
void Player::learnLocation(const Location* loc) {
for (Vec2 v : loc->getBounds())
remember(v, creature->getLevel()->getSquare(v)->getViewObject());
}
void Player::onExplosionEvent(const Level* level, Vec2 pos) {
if (creature->canSee(pos))
view->animation(pos, AnimationId::EXPLOSION);
else
creature->privateMessage("BOOM!");
}
void Player::onKillEvent(const Creature* victim, const Creature* killer) {
if (killer == creature)
points += victim->getDifficultyPoints();
}
ControllerFactory Player::getFactory(View* f, Model *m, map<const Level*, MapMemory>* levelMemory) {
return ControllerFactory([=](Creature* c) { return new Player(c, f, m, true, levelMemory);});
}
map<EquipmentSlot, string> slotSuffixes = {
{EquipmentSlot::WEAPON, "(weapon ready)"},
{EquipmentSlot::RANGED_WEAPON, "(ranged weapon ready)"},
{EquipmentSlot::BODY_ARMOR, "(being worn)"},
{EquipmentSlot::HELMET, "(being worn)"},
{EquipmentSlot::BOOTS, "(being worn)"},
{EquipmentSlot::AMULET, "(being worn)"}};
map<EquipmentSlot, string> slotTitles = {
{EquipmentSlot::WEAPON, "Weapon"},
{EquipmentSlot::RANGED_WEAPON, "Ranged weapon"},
{EquipmentSlot::HELMET, "Helmet"},
{EquipmentSlot::BODY_ARMOR, "Body armor"},
{EquipmentSlot::BOOTS, "Boots"},
{EquipmentSlot::AMULET, "Amulet"}};
void Player::onBump(Creature*) {
FAIL << "Shouldn't call onBump on a player";
}
void Player::getItemNames(vector<Item*> items, vector<View::ListElem>& names, vector<vector<Item*> >& groups,
ItemPredicate predicate) {
map<string, vector<Item*> > ret = groupBy<Item*, string>(items,
[this] (Item* const& item) {
if (creature->getEquipment().isEquiped(item))
return item->getNameAndModifiers(false, creature->isBlind()) + " "
+ slotSuffixes[creature->getEquipment().getSlot(item)];
else
return item->getNameAndModifiers(false, creature->isBlind());});
for (auto elem : ret) {
if (elem.second.size() == 1)
names.emplace_back(elem.first, predicate(elem.second[0]) ? View::NORMAL : View::INACTIVE);
else
names.emplace_back(convertToString<int>(elem.second.size()) + " "
+ elem.second[0]->getNameAndModifiers(true, creature->isBlind()),
predicate(elem.second[0]) ? View::NORMAL : View::INACTIVE);
groups.push_back(elem.second);
}
}
string Player::getPluralName(Item* item, int num) {
if (num == 1)
return item->getTheName(false, creature->isBlind());
else
return convertToString(num) + " " + item->getTheName(true, creature->isBlind());
}
static string getSquareQuestion(SquareApplyType type, string name) {
switch (type) {
case SquareApplyType::DESCEND: return "Descend the " + name;
case SquareApplyType::ASCEND: return "Ascend the " + name;
case SquareApplyType::USE_CHEST: return "Open the " + name;
case SquareApplyType::DRINK: return "Drink from the " + name;
case SquareApplyType::PRAY: return "Pray at the " + name;
case SquareApplyType::SLEEP: return "Go to sleep on the " + name;
default: FAIL << "Unhandled";
}
return "";
}
static bool canUseSquare(SquareApplyType type) {
return !contains({SquareApplyType::TRAIN, SquareApplyType::WORKSHOP}, type);
}
void Player::pickUpAction(bool extended) {
auto items = creature->getPickUpOptions();
const Square* square = creature->getConstSquare();
if (square->getApplyType(creature) && canUseSquare(*square->getApplyType(creature)) &&
(items.empty() ||
view->yesOrNoPrompt(getSquareQuestion(*square->getApplyType(creature), square->getName())))) {
creature->applySquare();
return;
}
vector<View::ListElem> names;
vector<vector<Item*> > groups;
getItemNames(creature->getPickUpOptions(), names, groups);
if (names.empty())
return;
int index = 0;
if (names.size() > 1) {
Optional<int> res = view->chooseFromList("Choose an item to pick up:", names);
if (!res)
return;
else
index = *res;
}
int num = groups[index].size();
if (num < 1)
return;
if (extended && num > 1) {
Optional<int> res = view->getNumber("Pick up how many " + groups[index][0]->getName(true) + "?", num);
if (!res)
return;
num = *res;
}
vector<Item*> pickUpItems = getPrefix(groups[index], 0, num);
if (creature->canPickUp(pickUpItems)) {
creature->privateMessage("You pick up " + getPluralName(groups[index][0], num));
creature->pickUp(pickUpItems);
}
}
void Player::itemsMessage() {
vector<View::ListElem> names;
vector<vector<Item*> > groups;
getItemNames(creature->getPickUpOptions(), names, groups);
if (names.size() > 1)
privateMessage(creature->isBlind() ? "You feel here some items" : "You see here some items.");
else if (names.size() == 1)
privateMessage((creature->isBlind() ? string("You feel here ") : ("You see here ")) +
(groups[0].size() == 1 ? "a " + groups[0][0]->getNameAndModifiers(false, creature->isBlind()) :
names[0].getText()));
}
ItemType typeDisplayOrder[] { ItemType::WEAPON, ItemType::RANGED_WEAPON, ItemType::AMMO, ItemType::ARMOR, ItemType::POTION, ItemType::SCROLL, ItemType::FOOD, ItemType::BOOK, ItemType::AMULET, ItemType::TOOL, ItemType::CORPSE, ItemType::OTHER, ItemType::GOLD };
static string getText(ItemType type) {
switch (type) {
case ItemType::WEAPON: return "Weapons";
case ItemType::RANGED_WEAPON: return "Ranged weapons";
case ItemType::AMMO: return "Projectiles";
case ItemType::AMULET: return "Amulets";
case ItemType::ARMOR: return "Armor";
case ItemType::SCROLL: return "Scrolls";
case ItemType::POTION: return "Potions";
case ItemType::FOOD: return "Comestibles";
case ItemType::BOOK: return "Books";
case ItemType::TOOL: return "Tools";
case ItemType::CORPSE: return "Corpses";
case ItemType::OTHER: return "Other";
case ItemType::GOLD: return "Gold";
}
FAIL << int(type);
return "";
}
vector<Item*> Player::chooseItem(const string& text, ItemPredicate predicate, Optional<ActionId> exitAction) {
map<ItemType, vector<Item*> > typeGroups = groupBy<Item*, ItemType>(
creature->getEquipment().getItems(), [](Item* const& item) { return item->getType();});
vector<View::ListElem> names;
vector<vector<Item*> > groups;
for (auto elem : typeDisplayOrder)
if (typeGroups[elem].size() > 0) {
names.push_back(View::ListElem(getText(elem), View::TITLE));
getItemNames(typeGroups[elem], names, groups, predicate);
}
Optional<int> index = view->chooseFromList(text, names, 0, exitAction);
if (index)
return groups[*index];
return vector<Item*>();
}
void Player::dropAction(bool extended) {
vector<Item*> items = chooseItem("Choose an item to drop:", [this](const Item* item) {
return !creature->getEquipment().isEquiped(item) || item->getType() == ItemType::WEAPON;}, ActionId::DROP);
int num = items.size();
if (num < 1)
return;
if (extended && num > 1) {
Optional<int> res = view->getNumber("Drop how many " + items[0]->getName(true, creature->isBlind()) + "?", num);
if (!res)
return;
num = *res;
}
creature->privateMessage("You drop " + getPluralName(items[0], num));
creature->drop(getPrefix(items, 0, num));
}
void Player::onItemsAppeared(vector<Item*> items, const Creature* from) {
if (!creature->canPickUp(items))
return;
vector<View::ListElem> names;
vector<vector<Item*> > groups;
getItemNames(items, names, groups);
CHECK(!names.empty());
Optional<int> index = view->chooseFromList("Do you want to take it?", names);
if (!index) {
return;
}
int num = groups[*index].size(); //groups[index].size() == 1 ? 1 : howMany(view, groups[index].size());
if (num < 1)
return;
creature->privateMessage("You take " + getPluralName(groups[*index][0], num));
creature->pickUp(getPrefix(groups[*index], 0, num), false);
}
void Player::applyAction() {
if (!creature->isHumanoid())
return;
if (creature->numGoodArms() == 0) {
privateMessage("You don't have hands!");
return;
}
vector<Item*> items = chooseItem("Choose an item to apply:", [this](const Item* item) {
return creature->canApplyItem(item);}, ActionId::APPLY_ITEM);
if (items.size() == 0)
return;
applyItem(items);
}
void Player::applyItem(vector<Item*> items) {
if (creature->isBlind() && contains({ItemType::SCROLL, ItemType::BOOK}, items[0]->getType())) {
privateMessage("You can't read while blind!");
return;
}
if (items[0]->getApplyTime() > 1) {
for (const Creature* c : creature->getVisibleEnemies())
if ((c->getPosition() - creature->getPosition()).length8() < 3) {
if (!view->yesOrNoPrompt("Applying " + items[0]->getAName() + " takes " +
convertToString(items[0]->getApplyTime()) + " turns. Are you sure you want to continue?"))
return;
else
break;
}
}
if (creature->canApplyItem(items[0])) {
privateMessage("You " + items[0]->getApplyMsgFirstPerson());
creature->applyItem(items[0]);
}
}
void Player::throwAction(Optional<Vec2> dir) {
vector<Item*> items = chooseItem("Choose an item to throw:", [this](const Item* item) {
return !creature->getEquipment().isEquiped(item);}, ActionId::THROW);
if (items.size() == 0)
return;
throwItem(items, dir);
}
void Player::throwItem(vector<Item*> items, Optional<Vec2> dir) {
if (!dir) {
auto cDir = view->chooseDirection("Which direction do you want to throw?");
if (!cDir)
return;
dir = *cDir;
}
if (creature->canThrowItem(items[0])) {
creature->privateMessage("You throw " + items[0]->getAName(false, creature->isBlind()));
creature->throwItem(items[0], *dir);
}
}
void Player::equipmentAction() {
vector<EquipmentSlot> slots;
for (auto slot : slotTitles)
slots.push_back(slot.first);
int index = 0;
creature->startEquipChain();
while (1) {
vector<View::ListElem> list;
for (auto slot : slots) {
list.push_back(View::ListElem(slotTitles.at(slot), View::TITLE));
Item* item = creature->getEquipment().getItem(slot);
if (item)
list.push_back(item->getNameAndModifiers());
else
list.push_back("[Nothing]");
}
view->updateView(creature);
Optional<int> newIndex = view->chooseFromList("Equipment", list, index, ActionId::EQUIPMENT);
if (!newIndex) {
creature->finishEquipChain();
return;
}
index = *newIndex;
EquipmentSlot slot = slots[index];
if (Item* item = creature->getEquipment().getItem(slot)) {
if (creature->canUnequip(item))
creature->unequip(item);
} else {
vector<Item*> items = chooseItem("Choose an item to equip:", [=](const Item* item) {
return item->canEquip()
&& !creature->getEquipment().isEquiped(item)
&& item->getEquipmentSlot() == slot;});
if (items.size() == 0) {
continue;
// finishEquipChain();
// return;
}
// messageBuffer.addMessage("You equip " + items[0]->getTheName());
if (creature->canEquip(items[0])) {
creature->equip(items[0]);
}
}
}
}
void Player::grantIdentify(int numItems) {
auto unidentFun = [this](const Item* item) { return item->canIdentify() && !item->isIdentified();};
vector<Item*> unIded = creature->getEquipment().getItems(unidentFun);
if (unIded.empty()) {
privateMessage("All your posessions are already identified");
return;
}
if (numItems > unIded.size()) {
privateMessage("You identify all your posessions");
for (Item* it : unIded)
it->identify();
} else
for (int i : Range(numItems)) {
vector<Item*> items = chooseItem("Choose an item to identify:", unidentFun);
if (items.size() == 0)
return;
items[0]->identify();
privateMessage("You identify " + items[0]->getTheName());
}
}
void Player::displayInventory() {
if (creature->getEquipment().isEmpty()) {
view->presentText("", "Your inventory is empty.");
return;
}
vector<Item*> item = chooseItem("Inventory:", alwaysTrue<const Item*>(), ActionId::SHOW_INVENTORY);
if (item.size() == 0) {
return;
}
vector<View::ListElem> options;
if (creature->canEquip(item[0])) {
options.push_back("equip");
}
if (creature->canApplyItem(item[0])) {
options.push_back("apply");
}
if (creature->canUnequip(item[0]))
options.push_back("remove");
else {
options.push_back("throw");
options.push_back("drop");
}
auto index = view->chooseFromList("What to do with " + getPluralName(item[0], item.size()) + "?", options);
if (!index) {
displayInventory();
return;
}
if (options[*index].getText() == "drop") {
creature->privateMessage("You drop " + getPluralName(item[0], item.size()));
creature->drop(item);
}
if (options[*index].getText() == "throw") {
throwItem(item);
}
if (options[*index].getText() == "apply") {
applyItem(item);
}
if (options[*index].getText() == "remove") {
creature->privateMessage("You remove " + getPluralName(item[0], item.size()));
creature->unequip(getOnlyElement(item));
}
if (options[*index].getText() == "equip") {
creature->privateMessage("You equip " + getPluralName(item[0], item.size()));
creature->equip(item[0]);
}
}
void Player::hideAction() {
if (creature->canHide()) {
privateMessage("You hide behind the " + creature->getConstSquare()->getName());
creature->hide();
} else {
if (!creature->hasSkill(Skill::ambush))
privateMessage("You don't have this skill.");
else
privateMessage("You can't hide here.");
}
}
bool Player::interruptedByEnemy() {
vector<const Creature*> enemies = creature->getVisibleEnemies();
vector<string> ignoreCreatures { "a boar" ,"a deer", "a fox", "a vulture", "a rat", "a jackal", "a boulder" };
if (enemies.size() > 0) {
for (const Creature* c : enemies)
if (!contains(ignoreCreatures, c->getAName())) {
view->refreshView(creature);
privateMessage("You notice " + c->getAName());
return true;
}
}
return false;
}
void Player::travelAction() {
if (!creature->canMove(travelDir) || view->travelInterrupt() || interruptedByEnemy()) {
travelling = false;
return;
}
creature->move(travelDir);
itemsMessage();
const Location* currentLocation = creature->getLevel()->getLocation(creature->getPosition());
if (lastLocation != currentLocation && currentLocation != nullptr && currentLocation->hasName()) {
privateMessage("You arrive at " + addAParticle(currentLocation->getName()));
travelling = false;
return;
}
vector<Vec2> squareDirs = creature->getConstSquare()->getTravelDir();
if (squareDirs.size() != 2) {
travelling = false;
Debug() << "Stopped by multiple routes";
return;
}
Optional<int> myIndex = findElement(squareDirs, -travelDir);
CHECK(myIndex) << "Bad travel data in square";
travelDir = squareDirs[(*myIndex + 1) % 2];
}
void Player::targetAction() {
CHECK(target);
if (creature->getPosition() == *target || view->travelInterrupt()) {
target = Nothing();
return;
}
Optional<Vec2> move = creature->getMoveTowards(*target);
if (move)
creature->move(*move);
else
target = Nothing();
itemsMessage();
if (interruptedByEnemy())
target = Nothing();
}
void Player::payDebtAction() {
for (Vec2 v : Vec2::directions8())
if (const Creature* c = creature->getConstSquare(v)->getCreature()) {
if (int debt = c->getDebt(creature)) {
vector<Item*> gold = creature->getGold(debt);
if (gold.size() < debt) {
privateMessage("You don't have enough gold to pay.");
} else if (view->yesOrNoPrompt("Buy items for " + convertToString(debt) + " zorkmids?")) {
privateMessage("You pay " + c->getName() + " " + convertToString(debt) + " zorkmids.");
creature->give(c, gold);
}
} else {
Debug() << "No debt " << c->getName();
}
}
}
void Player::chatAction(Optional<Vec2> dir) {
vector<const Creature*> creatures;
for (Vec2 v : Vec2::directions8())
if (const Creature* c = creature->getConstSquare(v)->getCreature())
creatures.push_back(c);
if (creatures.size() == 1 && !dir) {
privateMessage("You chat with " + creatures[0]->getTheName());
creature->chatTo(creatures[0]->getPosition() - creature->getPosition());
} else
if (creatures.size() > 1 || dir) {
if (!dir)
dir = view->chooseDirection("Which direction?");
if (!dir)
return;
if (const Creature* c = creature->getConstSquare(*dir)->getCreature()) {
privateMessage("You chat with " + c->getTheName());
creature->chatTo(*dir);
}
}
}
void Player::fireAction(Vec2 dir) {
if (creature->canFire(dir))
creature->fire(dir);
}
void Player::spellAction() {
vector<View::ListElem> list;
auto spells = creature->getSpells();
for (int i : All(spells))
list.push_back(View::ListElem(spells[i].name + " " + (!creature->canCastSpell(i) ? "(ready in " +
convertToString(int(spells[i].ready - creature->getTime() + 0.9999)) + " turns)" : ""),
creature->canCastSpell(i) ? View::NORMAL : View::INACTIVE));
auto index = view->chooseFromList("Cast a spell:", list);
if (!index)
return;
creature->privateMessage("You cast " + spells[*index].name);
creature->castSpell(*index);
}
const MapMemory& Player::getMemory(const Level* l) const {
if (l == nullptr)
l = creature->getLevel();
return (*levelMemory)[l];
}
void Player::remember(Vec2 pos, const ViewObject& object) {
(*levelMemory)[creature->getLevel()].addObject(pos, object);
}
void Player::sleeping() {
if (creature->isHallucinating())
ViewObject::setHallu(true);
else
ViewObject::setHallu(false);
MEASURE(
view->refreshView(creature),
"level render time");
}
void Player::makeMove() {
vector<Vec2> squareDirs = creature->getConstSquare()->getTravelDir();
const vector<Creature*>& creatures = creature->getLevel()->getAllCreatures();
if (creature->isHallucinating())
ViewObject::setHallu(true);
else
ViewObject::setHallu(false);
MEASURE(
view->refreshView(creature),
"level render time");
if (Options::getValue(OptionId::HINTS) && displayTravelInfo && creature->getConstSquare()->getName() == "road") {
view->presentText("", "Use ctrl + arrows to travel quickly on roads and corridors.");
displayTravelInfo = false;
}
static bool greeting = false;
if (Options::getValue(OptionId::HINTS) && displayGreeting) {
CHECK(creature->getFirstName());
view->presentText("", "Dear " + *creature->getFirstName() + ",\n \n \tIf you are reading this letter, then you have arrived in the valley of " + NameGenerator::worldNames.getNext() + ". There is a band of dwarves dwelling in caves under a mountain. Find them, talk to them, they will help you. Let your sword guide you.\n \n \nYours, " + NameGenerator::firstNames.getNext() + "\n \nPS.: Beware the goblins!");
view->presentText("", "Every settlement that you find has a leader, and they may have quests for you."
"\n \nYou can turn these messages off in the options (press F2).");
displayGreeting = false;
}
for (const Creature* c : creature->getVisibleEnemies()) {
if (c->isSpecialMonster() && !contains(specialCreatures, c)) {
privateMessage(MessageBuffer::important(c->getDescription()));
specialCreatures.push_back(c);
}
}
if (travelling)
travelAction();
else if (target)
targetAction();
else {
Action action = view->getAction();
vector<Vec2> direction;
bool travel = false;
switch (action.getId()) {
case ActionId::FIRE: fireAction(action.getDirection()); break;
case ActionId::TRAVEL: travel = true;
case ActionId::MOVE: direction.push_back(action.getDirection()); break;
case ActionId::MOVE_TO: if (action.getDirection().dist8(creature->getPosition()) == 1) {
Vec2 dir = action.getDirection() - creature->getPosition();
if (const Creature* c = creature->getConstSquare(dir)->getCreature()) {
if (!creature->isEnemy(c)) {
chatAction(dir);
break;
}
}
direction.push_back(dir);
} else
if (action.getDirection() != creature->getPosition()) {
target = action.getDirection();
target = Vec2(min(creature->getLevel()->getBounds().getKX() - 1, max(0, target->x)),
min(creature->getLevel()->getBounds().getKY() - 1, max(0, target->y)));
// Just in case
if (!target->inRectangle(creature->getLevel()->getBounds()))
target = Nothing();
}
else
pickUpAction(false);
break;
case ActionId::SHOW_INVENTORY: displayInventory(); break;
case ActionId::PICK_UP: pickUpAction(false); break;
case ActionId::EXT_PICK_UP: pickUpAction(true); break;
case ActionId::DROP: dropAction(false); break;
case ActionId::EXT_DROP: dropAction(true); break;
case ActionId::WAIT: creature->wait(); break;
case ActionId::APPLY_ITEM: applyAction(); break;
case ActionId::THROW: throwAction(); break;
case ActionId::THROW_DIR: throwAction(action.getDirection()); break;
case ActionId::EQUIPMENT: equipmentAction(); break;
case ActionId::HIDE: hideAction(); break;
case ActionId::PAY_DEBT: payDebtAction(); break;
case ActionId::CHAT: chatAction(); break;
case ActionId::SHOW_HISTORY: messageBuffer.showHistory(); break;
case ActionId::UNPOSSESS: if (creature->canPopController()) {
creature->popController();
return;
} break;
case ActionId::CAST_SPELL: spellAction(); break;
case ActionId::DRAW_LEVEL_MAP: view->drawLevelMap(creature->getLevel(), creature); break;
case ActionId::IDLE: break;
}
if (creature->isSleeping() && creature->canPopController()) {
if (view->yesOrNoPrompt("You fell asleep. Do you want to leave your minion?"))
creature->popController();
return;
}
for (Vec2 dir : direction)
if (travel) {
vector<Vec2> squareDirs = creature->getConstSquare()->getTravelDir();
if (findElement(squareDirs, dir)) {
travelDir = dir;
lastLocation = creature->getLevel()->getLocation(creature->getPosition());
travelling = true;
travelAction();
}
} else
if (creature->canMove(dir)) {
creature->move(dir);
itemsMessage();
break;
} else {
const Creature *c = creature->getConstSquare(dir)->getCreature();
if (creature->canBumpInto(dir)) {
creature->bumpInto(dir);
break;
}
}
}
for (Vec2 pos : creature->getLevel()->getVisibleTiles(creature)) {
ViewIndex index = creature->getLevel()->getSquare(pos)->getViewIndex(creature);
(*levelMemory)[creature->getLevel()].clearSquare(pos);
for (ViewLayer l : { ViewLayer::ITEM, ViewLayer::FLOOR_BACKGROUND, ViewLayer::FLOOR, ViewLayer::LARGE_ITEM})
if (index.hasObject(l))
remember(pos, index.getObject(l));
}
}
bool Player::isPlayer() const {
return true;
}
void Player::privateMessage(const string& message) const {
messageBuffer.addMessage(message);
}
void Player::you(const string& param) const {
privateMessage("You " + param);
}
void Player::you(MsgType type, const string& param) const {
string msg;
switch (type) {
case MsgType::ARE: msg = "You are " + param; break;
case MsgType::YOUR: msg = "Your " + param; break;
case MsgType::FEEL: msg = "You feel " + param; break;
case MsgType::FALL_ASLEEP: msg = "You fall asleep" + (param.size() > 0 ? " on the " + param : "."); break;
case MsgType::WAKE_UP: msg = "You wake up."; break;
case MsgType::FALL_APART: msg = "You fall apart."; break;
case MsgType::FALL: msg = "You fall on the " + param; break;
case MsgType::DIE: messageBuffer.addMessage("You die!!"); break;
case MsgType::TELE_DISAPPEAR: msg = "You are standing somewhere else!"; break;
case MsgType::BLEEDING_STOPS: msg = "Your bleeding stops."; break;
case MsgType::DIE_OF: msg = "You die" + (param.empty() ? string(".") : " of " + param); break;
case MsgType::MISS_ATTACK: msg = "You miss " + param; break;
case MsgType::MISS_THROWN_ATTACK: msg = param + " misses you"; break;
case MsgType::HIT_THROWN_ITEM: msg = param + " hits you"; break;
case MsgType::CRASH_THROWN_ITEM: msg = param + " crashes on your head"; break;
case MsgType::GET_HIT_NODAMAGE: msg = "The " + param + " is harmless."; break;
case MsgType::COLLAPSE: msg = "You collapse."; break;
case MsgType::TRIGGER_TRAP: msg = "You trigger something."; break;
case MsgType::SWING_WEAPON: msg = "You swing your " + param; break;
case MsgType::THRUST_WEAPON: msg = "You thrust your " + param; break;
case MsgType::ATTACK_SURPRISE: msg = "You sneak attack " + param; break;
case MsgType::KICK: msg = "You kick " + param; break;
case MsgType::BITE: msg = "You bite " + param; break;
case MsgType::PUNCH: msg = "You punch " + param; break;
case MsgType::PANIC:
msg = !creature->isHallucinating() ? "You are suddenly very afraid" : "You freak out completely"; break;
case MsgType::RAGE:
msg = !creature->isHallucinating() ?"You are suddenly very angry" : "This will be a very long trip."; break;
case MsgType::CRAWL: msg = "You are crawling"; break;
case MsgType::STAND_UP: msg = "You are back on your feet"; break;
case MsgType::CAN_SEE_HIDING: msg = param + " can see you hiding"; break;
case MsgType::TURN_INVISIBLE: msg = "You can see through yourself!"; break;
case MsgType::TURN_VISIBLE: msg = "You are no longer invisible"; break;
case MsgType::DROP_WEAPON: msg = "You drop your " + param; break;
case MsgType::ITEM_CRASHES: msg = param + " crashes on you."; break;
case MsgType::ENTER_PORTAL: msg = "You enter the portal"; break;
case MsgType::HAPPENS_TO: msg = param + " you."; break;
case MsgType::BURN: msg = "You burn in the " + param; break;
case MsgType::DROWN: msg = "You drown in the " + param; break;
case MsgType::SET_UP_TRAP: msg = "You set up the trap"; break;
case MsgType::KILLED_BY: msg = "You are killed by " + param; break;
case MsgType::TURN: msg = "You turn into " + param; break;
case MsgType::HIT: msg = "You hit " + param; break;
default: break;
}
messageBuffer.addMessage(msg);
}
void Player::onKilled(const Creature* attacker) {
if (!creature->canPopController()) {
model->gameOver(creature, creature->getKills().size(), "monsters", points);
} else
creature->popController();
}