1 /*
2  * Copyright (C) 2006-2019 Christopho, Solarus - http://www.solarus-games.org
3  *
4  * Solarus 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  * Solarus 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 along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #ifndef SOLARUS_HERO_CUSTOM_STATE_H
18 #define SOLARUS_HERO_CUSTOM_STATE_H
19 
20 #include "solarus/core/Common.h"
21 #include "solarus/entities/Ground.h"
22 #include "solarus/entities/TraversableInfo.h"
23 #include "solarus/hero/HeroState.h"
24 #include <set>
25 
26 namespace Solarus {
27 
28 class InputEvent;
29 class PlayerMovement;
30 
31 /**
32  * \brief The state "custom" of the hero.
33  *
34  * Delegates everything to a Lua object of type state.
35  */
36 class CustomState: public HeroState {
37 
38   public:
39 
40     CustomState(LuaContext& lua_context, const std::string& description);
41     const std::string& get_lua_type_name() const override;
42 
43     LuaContext& get_lua_context();
44 
45     void start(const State* previous_state) override;
46     void stop(const State* next_state) override;
47 
48     void set_map(Map& map) override;
49     void notify_map_started(
50         Map& map, const std::shared_ptr<Destination>& destination) override;
51     void notify_map_opening_transition_finished(
52         Map& map, const std::shared_ptr<Destination>& destination) override;
53     void notify_map_finished() override;
54     void update() override;
55     void set_suspended(bool suspended) override;
56     bool notify_input(const InputEvent& event) override;
57     void notify_command_pressed(GameCommand command) override;
58     void notify_command_released(GameCommand command) override;
59     void draw_on_map() override;
60 
61     const std::string& get_description() const;
62     void set_description(const std::string& description);
63     bool is_visible() const override;
64     void set_visible(bool visible);
65     ScopedLuaRef get_draw_override() const;
66     void set_draw_override(const ScopedLuaRef& draw_override);
67 
68     bool get_can_control_direction() const;
69     void set_can_control_direction(bool can_control_direction);
70     bool is_direction_locked() const override;
71     int get_wanted_movement_direction8() const override;
72 
73     void set_can_control_movement(bool can_control_movement);
74     bool get_can_control_movement() const override;
75 
76     void set_can_traverse_entities(bool traversable);
77     void set_can_traverse_entities(const ScopedLuaRef& traversable_test_ref);
78     void reset_can_traverse_entities();
79     void set_can_traverse_entities(EntityType type, bool traversable);
80     void set_can_traverse_entities(
81         EntityType type,
82         const ScopedLuaRef& traversable_test_ref
83     );
84     void reset_can_traverse_entities(EntityType type);
85 
86     bool is_hero_obstacle(Hero& hero) override;
87     bool is_block_obstacle(Block& block) override;
88     bool is_teletransporter_obstacle(Teletransporter& teletransporter) override;
89     bool is_stream_obstacle(Stream& stream) override;
90     bool is_stairs_obstacle(Stairs& stairs) override;
91     bool is_sensor_obstacle(Sensor& sensor) override;
92     bool is_switch_obstacle(Switch& sw) override;
93     bool is_raised_block_obstacle(CrystalBlock& raised_block) override;
94     bool is_crystal_obstacle(Crystal& crystal) override;
95     bool is_npc_obstacle(Npc& npc) override;
96     bool is_door_obstacle(Door& block) override;
97     bool is_enemy_obstacle(Enemy& enemy) override;
98     bool is_jumper_obstacle(Jumper& jumper, const Rectangle& candidate_position) override;
99     bool is_destructible_obstacle(Destructible& destructible) override;
100     bool is_separator_obstacle(Separator& separator) override;
101 
102     bool get_can_traverse_ground(Ground ground) const;
103     void set_can_traverse_ground(Ground ground, bool traversable);
104 
105     bool is_traversable_obstacle() const override;
106     bool is_wall_obstacle() const override;
107     bool is_low_wall_obstacle() const override;
108     bool is_grass_obstacle() const override;
109     bool is_shallow_water_obstacle() const override;
110     bool is_deep_water_obstacle() const override;
111     bool is_hole_obstacle() const override;
112     bool is_ice_obstacle() const override;
113     bool is_lava_obstacle() const override;
114     bool is_prickle_obstacle() const override;
115     bool is_ladder_obstacle() const override;
116 
117     bool is_touching_ground() const override;
118     void set_touching_ground(bool touching_ground);
119     bool is_affected_by_ground(Ground ground) const;
120     void set_affected_by_ground(Ground ground, bool affected);
121     bool get_can_come_from_bad_ground() const override;
122     void set_can_come_from_bad_ground(bool can_come_from_bad_ground);
123     bool can_avoid_deep_water() const override;
124     bool can_avoid_hole() const override;
125     bool can_avoid_ice() const override;
126     bool can_avoid_lava() const override;
127     bool can_avoid_prickle() const override;
128     bool is_affected_by_shallow_water() const override;
129     bool is_affected_by_grass() const override;
130     bool is_affected_by_ladder() const override;
131 
132     bool get_can_be_hurt(Entity* attacker) override;
133     void set_can_be_hurt(bool can_be_hurt);
134     void set_can_be_hurt(const ScopedLuaRef& can_be_hurt_callback);
135     bool get_can_start_sword() const override;
136     void set_can_start_sword(bool can_start_sword);
137     bool is_cutting_with_sword(Entity& entity) override;
138     bool get_can_cut(Entity* entity);
139     void set_can_cut(bool can_cut);
140     void set_can_cut(const ScopedLuaRef& can_cut_callback);
141     bool get_can_use_shield() const override;
142     void set_can_use_shield(bool can_use_shield);
143     bool get_can_start_item(EquipmentItem& item) const override;
144     bool get_can_start_item(const std::string& item_id) const;
145     void set_can_start_item(const std::string& item_id, bool can_start_item);
146     bool get_can_interact() const;
147     void set_can_interact(bool can_interact);
148     bool get_can_grab() const;
149     void set_can_grab(bool can_grab);
150     bool get_can_push() const;
151     void set_can_push(bool can_push);
152     uint32_t get_pushing_delay() const;
153     void set_pushing_delay(uint32_t pushing_delay);
154     bool get_can_pick_treasure() const;
155     bool get_can_pick_treasure(EquipmentItem& item) const override;
156     void set_can_pick_treasure(bool can_pick_treasure);
157     bool can_avoid_teletransporter() const override;
158     bool get_can_take_teletransporter() const;
159     void set_can_take_teletransporter(bool can_take_teletransporter);
160     bool can_avoid_switch() const override;
161     bool get_can_take_switch() const;
162     void set_can_take_switch(bool can_take_switch);
163     bool can_avoid_stream(const Stream& stream) const override;
164     bool get_can_take_stream() const;
165     void set_can_take_stream(bool can_take_stream);
166     bool get_can_take_stairs() const override;
167     void set_can_take_stairs(bool can_take_stairs);
168     bool get_can_take_jumper() const override;
169     void set_can_take_jumper(bool can_take_jumper);
170     uint32_t get_jumper_delay() const;
171     void set_jumper_delay(uint32_t jumper_delay);
172     void notify_jumper_activated(Jumper& jumper) override;
173     CarriedObject::Behavior get_previous_carried_object_behavior() const override;
174     void set_previous_carried_object_behavior(CarriedObject::Behavior behavior);
175     std::shared_ptr<CarriedObject> get_carried_object() const override;
176     void notify_position_changed() override;
177     void notify_layer_changed() override;
178     void notify_ground_below_changed() override;
179     void notify_obstacle_reached() override;
180     void notify_movement_started() override;
181     void notify_movement_finished() override;
182     void notify_movement_changed() override;
183     void notify_walking_speed_changed() override;
184     void notify_attacked_enemy(
185         EnemyAttack attack,
186         Enemy& victim,
187         Sprite* victim_sprite,
188         const EnemyReaction::Reaction& result,
189         bool killed) override;
190 
191   private:
192 
193     const TraversableInfo& get_can_traverse_entity_info(EntityType type);
194 
195     void start_player_movement();
196     void update_pushing();
197     void update_jumper();
198     void cancel_jumper();
199 
200     LuaContext& lua_context;               /**< The Lua context. */
201     std::string description;               /**< Description of this state or an empty string. */
202     bool visible;                          /**< Whether the entity is visible during this state. */
203     ScopedLuaRef draw_override;            /**< Optional Lua function that draws the entity during this state. */
204     bool can_control_direction;            /**< Whether the player controls the sprites direction. */
205     bool can_control_movement;             /**< Whether the player controls the hero's movement. */
206     std::shared_ptr<PlayerMovement>
207         player_movement;                   /**< The movement, if controlled by the player. */
208     bool touching_ground;                  /**< Whether the entity is in contact with the ground. */
209     std::set<Ground> ignored_grounds;      /**< Grounds whose effect does not affect this state. */
210     bool can_come_from_bad_ground;         /**< Whether solid ground position is considered as
211                                             * a place where to come back from bad grounds. */
212     bool can_be_hurt;                      /**< Whether the entity be hurt in this state. */
213     ScopedLuaRef can_be_hurt_callback;     /**< Boolean function deciding if the entity can be hurt. */
214     bool can_start_sword;                  /**< Whether the sword can be used in this state. */
215     bool can_cut;                          /**< Whether the sword can cut destructibles in this state. */
216     ScopedLuaRef can_cut_callback;         /**< Boolean function deciding if destructibles can be cut. */
217     bool can_use_shield;                   /**< Whether the shield can be used in this state. */
218     bool can_start_item;                   /**< Whether items in general can be used in this state. */
219     std::map<std::string, bool>
220         can_start_items;                   /**< Whether specific items can be used in this state. */
221     bool can_interact;                     /**< Whether interacting with the facing entity
222                                             * is allowed in this state. */
223     bool can_grab;                         /**< Whether grabbing is allowed in this state. */
224     bool can_push;                         /**< Whether pushing is allowed in this state. */
225     uint32_t pushing_delay;                /**< Delay before pushing. */
226     int pushing_direction4;                /**< Direction where trying to
227                                             * push an obstacle: 0 to 3 or -1 */
228     uint32_t start_pushing_date;           /**< Date when state pushing should start. */
229     bool can_pick_treasure;                /**< Whether treasures can be picked in this state. */
230     bool can_take_teletransporter;         /**< Whether teletransporters can be used in this state. */
231     bool can_take_switch;                  /**< Whether switches can be used in this state. */
232     bool can_take_stream;                  /**< Whether streams can be used in this state. */
233     bool can_take_stairs;                  /**< Whether stairs can be used in this state. */
234     bool can_take_jumper;                  /**< Whether jumpers can be used in this state. */
235     std::shared_ptr<Jumper>
236         current_jumper;                    /**< The jumper about to be triggered or nullptr. */
237     uint32_t jumper_start_date;            /**< Date when to trigger the jumper
238                                             * (in case there is a delay). */
239     uint32_t jumper_delay;                 /**< Delay before a jumper activates. */
240     CarriedObject::Behavior
241         previous_carried_object_behavior;  /**< What happens during this state to an object that
242                                             * was carried in the previous state. */
243     std::shared_ptr<CarriedObject>
244         carried_object;                    /**< Object carried by the entity if any. */
245     TraversableInfo
246         can_traverse_entities_general;     /**< Whether the entity can traverse other entities by default. */
247     std::map<EntityType, TraversableInfo>
248         can_traverse_entities_type;        /**< Whether the entity can traverse entities of a type. */
249     std::map<Ground, bool>
250         can_traverse_grounds;              /**< Whether the entity can traverse each kind of ground. */
251 };
252 
253 }
254 
255 #endif
256 
257