1 /*
2  *  Copyright (C) 2011-2016  OpenDungeons Team
3  *
4  *  This program is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef CREATUREACTION_H
19 #define CREATUREACTION_H
20 
21 #include <cstdint>
22 #include <functional>
23 #include <istream>
24 
25 class Creature;
26 
27 enum class CreatureActionType
28 {
29     walkToTile, // Calculate a path to the tile and follow it each turn.
30     fight, // When seeing enemy objects, the creature might decide to fight
31     fightFriendly, // Fight a friendly unit (useful for arena). Allows to stop if an enemy comes
32     searchTileToDig, // (worker only) Searches a tile to dig
33     digTile, // (worker only) Digs a tile
34     searchGroundTileToClaim, // (worker only) Searches a ground tile to claim
35     claimGroundTile, // (worker only) "Dance" on tile to change its color.
36     searchWallTileToClaim, // (worker only) Searches a wall tile to claim
37     claimWallTile, // (worker only) "Dance" around the tile to change its color and reinforce it.
38     findHome, // (fighters only) Try to find a "home" tile in a dormitory somewhere where the creature can sleep.
39     sleep, // (fighters only) Try to go to its home tile to and sleep when it gets there.
40     searchJob, // (fighters only) Check to see if our seat controls a room where we can work (train, workshop, forge, search, ...)
41     useRoom, // (fighters only) use the given room (working, eating, ...)
42     searchFood, // (fighters only) Try to find a hatchery to eat or a free chicken
43     eatChicken, // (fighters only) Eats a wandering free chicken
44     flee, // If a fighter is weak (low hp) or a worker is attacked by a fighter, he will flee
45     searchEntityToCarry, // (worker only) Searches around for an entity to carry
46     grabEntity, // (worker only) Try to take the entity to carry
47     carryEntity, // (worker only) Carries the entity to some building needing it
48     getFee, // (fighter only) Gets the creature fee
49     leaveDungeon, // (fighter only) Try to go to the portal to leave the dungeon
50     stealFreeGold, // (fighters only) check in the visible tiles if there is gold not protected by a treasury
51     nb // Must be the last value of this enum
52 };
53 
54 class CreatureAction
55 {
56 public:
CreatureAction(Creature & creature)57     CreatureAction(Creature& creature) :
58         mCreature(creature),
59         mNbTurns(0),
60         mNbTurnsActive(0)
61     {}
62 
~CreatureAction()63     virtual ~CreatureAction()
64     {}
65 
66     virtual CreatureActionType getType() const = 0;
67 
increaseNbTurn()68     inline void increaseNbTurn()
69     { ++mNbTurns; }
70 
getNbTurns()71     inline int32_t getNbTurns() const
72     { return mNbTurns; }
73 
increaseNbTurnActive()74     inline void increaseNbTurnActive()
75     { ++mNbTurnsActive; }
76 
getNbTurnsActive()77     inline int32_t getNbTurnsActive() const
78     { return mNbTurnsActive; }
79 
80     //! Returns a pointer to the given action from CreatureAction class. Note that
81     //! we don't want to do stuff in the child classes because many actions will
82     //! pop themselves which might result in errors. Instead, we expect every action
83     //! to call the expected action from CreatureAction with the good parameters.
84     virtual std::function<bool()> action() = 0;
85 
86     static std::string toString(CreatureActionType actionType);
87 
88 protected:
89     Creature& mCreature;
90 
91 private:
92     CreatureAction(const CreatureAction&) = delete;
93 
94     int32_t mNbTurns;
95     int32_t mNbTurnsActive;
96 };
97 
98 #endif // CREATUREACTION_H
99