1 /* Copyright (C) 2013-2014 Michal Brzozowski (rusolis@poczta.fm)
2 
3    This file is part of KeeperRL.
4 
5    KeeperRL is free software; you can redistribute it and/or modify it under the terms of the
6    GNU General Public License as published by the Free Software Foundation; either version 2
7    of the License, or (at your option) any later version.
8 
9    KeeperRL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10    even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License along with this program.
14    If not, see http://www.gnu.org/licenses/ . */
15 
16 #pragma once
17 
18 #include "creature_action.h"
19 #include "controller.h"
20 #include "user_input.h"
21 #include "creature_view.h"
22 #include "map_memory.h"
23 #include "position.h"
24 #include "event_listener.h"
25 #include "game_info.h"
26 
27 class View;
28 class Model;
29 class Creature;
30 class Item;
31 class ListElem;
32 struct ItemInfo;
33 class Game;
34 class VisibilityMap;
35 class Tutorial;
36 class MessageBuffer;
37 
38 class Player : public Controller, public CreatureView, public EventListener<Player> {
39   public:
40   virtual ~Player() override;
41 
42   Player(WCreature, bool adventurer, SMapMemory, SMessageBuffer, SVisibilityMap, STutorial = nullptr);
43 
44   void onEvent(const GameEvent&);
45 
46   SERIALIZATION_DECL(Player)
47 
48   protected:
49 
50   virtual void moveAction(Vec2 direction);
51 
52   // from CreatureView
53   virtual void getViewIndex(Vec2 pos, ViewIndex&) const override;
54   virtual const MapMemory& getMemory() const override;
55   virtual void refreshGameInfo(GameInfo&) const override;
56   virtual Vec2 getPosition() const override;
57   virtual WLevel getLevel() const override;
58   virtual vector<Vec2> getVisibleEnemies() const override;
59   virtual double getLocalTime() const override;
60   virtual CenterType getCenterType() const override;
61   virtual vector<Vec2> getUnknownLocations(WConstLevel) const override;
62 
63   // from Controller
64   virtual void onKilled(WConstCreature attacker) override;
65   virtual void makeMove() override;
66   virtual void sleeping() override;
67   virtual bool isPlayer() const override;
68   virtual void privateMessage(const PlayerMessage& message) override;
69   virtual MessageGenerator& getMessageGenerator() const override;
70   virtual void onStartedControl() override;
71   virtual void onEndedControl() override;
72 
73   // overridden by subclasses
74   struct CommandInfo {
75     PlayerInfo::CommandInfo commandInfo;
76     function<void(Player*)> perform;
77     bool actionKillsController;
78   };
79   virtual vector<CommandInfo> getCommands() const;
80   virtual void onFellAsleep();
81   virtual vector<WCreature> getTeam() const;
82   virtual bool isTravelEnabled() const;
83 
84   optional<Vec2> chooseDirection(const string& question);
85 
86   SMapMemory SERIAL(levelMemory);
87   void showHistory();
88   WGame getGame() const;
89   View* getView() const;
90 
91   bool tryToPerform(CreatureAction);
92 
93   private:
94 
95   void considerAdventurerMusic();
96   void extendedAttackAction(UniqueEntity<Creature>::Id);
97   void extendedAttackAction(WCreature other);
98   void creatureAction(UniqueEntity<Creature>::Id);
99   void pickUpItemAction(int item, bool multi = false);
100   void equipmentAction();
101   void applyItem(vector<WItem> item);
102   void throwItem(vector<WItem> item, optional<Vec2> dir = none);
103   void takeOffAction();
104   void hideAction();
105   void displayInventory();
106   void handleItems(const EntitySet<Item>&, ItemAction);
107   vector<ItemAction> getItemActions(const vector<WItem>&) const;
108   bool interruptedByEnemy();
109   void travelAction();
110   void targetAction();
111   void payForAllItemsAction();
112   void payForItemAction(const vector<WItem>&);
113   void chatAction(optional<Vec2> dir = none);
114   void giveAction(vector<WItem>);
115   void spellAction(SpellId);
116   void fireAction();
117   void fireAction(Vec2 dir);
118   vector<WItem> chooseItem(const string& text, ItemPredicate, optional<UserInputId> exitAction = none);
119   void getItemNames(vector<WItem> it, vector<ListElem>& names, vector<vector<WItem> >& groups,
120       ItemPredicate = alwaysTrue<WConstItem>());
121   string getInventoryItemName(WConstItem, bool plural) const;
122   string getPluralName(WItem item, int num);
123   bool SERIAL(travelling) = false;
124   Vec2 SERIAL(travelDir);
125   optional<Position> SERIAL(target);
126   bool SERIAL(adventurer);
127   bool SERIAL(displayGreeting);
128   bool updateView = true;
129   void retireMessages();
130   SMessageBuffer SERIAL(messageBuffer);
131   string getRemainingString(LastingEffect) const;
132   vector<ItemInfo> getItemInfos(const vector<WItem>&) const;
133   ItemInfo getItemInfo(const vector<WItem>&) const;
134   ItemInfo getFurnitureUsageInfo(const string& question, ViewId viewId) const;
135   optional<FurnitureUsageType> getUsableUsageType() const;
136   SVisibilityMap SERIAL(visibilityMap);
137   STutorial SERIAL(tutorial);
138 };
139 
140