1 #pragma once
2 #ifndef CATA_SRC_MONSTER_H
3 #define CATA_SRC_MONSTER_H
4 
5 #include <bitset>
6 #include <climits>
7 #include <cstddef>
8 #include <functional>
9 #include <iosfwd>
10 #include <map>
11 #include <new>
12 #include <set>
13 #include <utility>
14 #include <vector>
15 
16 #include "calendar.h"
17 #include "character_id.h"
18 #include "color.h"
19 #include "creature.h"
20 #include "damage.h"
21 #include "enums.h"
22 #include "item.h"
23 #include "mtype.h"
24 #include "optional.h"
25 #include "point.h"
26 #include "type_id.h"
27 #include "units_fwd.h"
28 #include "value_ptr.h"
29 
30 class Character;
31 class JsonIn;
32 class JsonObject;
33 class JsonOut;
34 class effect;
35 class effect_source;
36 class player;
37 namespace catacurses
38 {
39 class window;
40 }  // namespace catacurses
41 struct dealt_projectile_attack;
42 struct pathfinding_settings;
43 struct trap;
44 
45 enum class mon_trigger : int;
46 
47 class mon_special_attack
48 {
49     public:
50         int cooldown = 0;
51         bool enabled = true;
52 
53         void serialize( JsonOut &json ) const;
54         // deserialize inline in monster::load due to backwards/forwards compatibility concerns
55 };
56 
57 enum monster_attitude {
58     MATT_NULL = 0,
59     MATT_FRIEND,
60     MATT_FPASSIVE,
61     MATT_FLEE,
62     MATT_IGNORE,
63     MATT_FOLLOW,
64     MATT_ATTACK,
65     NUM_MONSTER_ATTITUDES
66 };
67 
68 enum monster_effect_cache_fields {
69     MOVEMENT_IMPAIRED = 0,
70     FLEEING,
71     VISION_IMPAIRED,
72     NUM_MEFF
73 };
74 
75 enum monster_horde_attraction {
76     MHA_NULL = 0,
77     MHA_ALWAYS,
78     MHA_LARGE,
79     MHA_OUTDOORS,
80     MHA_OUTDOORS_AND_LARGE,
81     MHA_NEVER,
82     NUM_MONSTER_HORDE_ATTRACTION
83 };
84 
85 class monster : public Creature
86 {
87         friend class editmap;
88     public:
89         monster();
90         explicit monster( const mtype_id &id );
91         monster( const mtype_id &id, const tripoint &pos );
92         monster( const monster & );
93         monster( monster && );
94         ~monster() override;
95         monster &operator=( const monster & );
96         monster &operator=( monster && );
97 
is_monster()98         bool is_monster() const override {
99             return true;
100         }
as_monster()101         monster *as_monster() override {
102             return this;
103         }
as_monster()104         const monster *as_monster() const override {
105             return this;
106         }
107 
108         void poly( const mtype_id &id );
109         bool can_upgrade() const;
110         void hasten_upgrade();
111         int get_upgrade_time() const;
112         void allow_upgrade();
113         void try_upgrade( bool pin_time );
114         void try_reproduce();
115         void try_biosignature();
116         void refill_udders();
117         void spawn( const tripoint &p );
118         creature_size get_size() const override;
119         units::mass get_weight() const override;
120         units::mass weight_capacity() const override;
121         units::volume get_volume() const;
122         int get_hp( const bodypart_id & ) const override;
123         int get_hp() const override;
124         int get_hp_max( const bodypart_id & ) const override;
125         int get_hp_max() const override;
126         int hp_percentage() const override;
127 
128         float get_mountable_weight_ratio() const;
129 
130         // Access
131         std::string get_name() const override;
132         std::string name( unsigned int quantity = 1 ) const; // Returns the monster's formal name
133         std::string name_with_armor() const; // Name, with whatever our armor is called
134         // the creature-class versions of the above
135         std::string disp_name( bool possessive = false, bool capitalize_first = false ) const override;
136         std::string skin_name() const override;
137         void get_HP_Bar( nc_color &color, std::string &text ) const;
138         std::pair<std::string, nc_color> get_attitude() const;
139         int print_info( const catacurses::window &w, int vStart, int vLines, int column ) const override;
140 
141         // Information on how our symbol should appear
142         nc_color basic_symbol_color() const override;
143         nc_color symbol_color() const override;
144         const std::string &symbol() const override;
145         bool is_symbol_highlighted() const override;
146 
147         nc_color color_with_effects() const; // Color with fire, beartrapped, etc.
148 
149         std::string extended_description() const override;
150         // Inverts color if inv==true
151         bool has_flag( m_flag f ) const override; // Returns true if f is set (see mtype.h)
152         bool can_see() const;      // MF_SEES and no MF_BLIND
153         bool can_hear() const;     // MF_HEARS and no MF_DEAF
154         bool can_submerge() const; // MF_AQUATIC or swims() or MF_NO_BREATH, and not MF_ELECTRONIC
155         bool can_drown() const;    // MF_AQUATIC or swims() or MF_NO_BREATHE or flies()
156         bool can_climb() const;         // climbs() or flies()
157         bool digging() const override;  // digs() or can_dig() and diggable terrain
158         bool can_dig() const;
159         bool digs() const;
160         bool flies() const;
161         bool climbs() const;
162         bool swims() const;
163         // Returns false if the monster is stunned, has 0 moves or otherwise wouldn't act this turn
164         bool can_act() const;
165         int sight_range( int light_level ) const override;
166         bool made_of( const material_id &m ) const override; // Returns true if it's made of m
167         bool made_of_any( const std::set<material_id> &ms ) const override;
168         bool made_of( phase_id p ) const; // Returns true if its phase is p
169 
170         bool avoid_trap( const tripoint &pos, const trap &tr ) const override;
171 
172         void serialize( JsonOut &json ) const;
173         void deserialize( JsonIn &jsin );
174 
175         tripoint move_target(); // Returns point at the end of the monster's current plans
176         Creature *attack_target(); // Returns the creature at the end of plans (if hostile)
177 
178         // Movement
179         void shift( const point &sm_shift ); // Shifts the monster to the appropriate submap
180         void set_goal( const tripoint &p );
181         // Updates current pos AND our plans
182         bool wander(); // Returns true if we have no plans
183 
184         /**
185          * Checks whether we can move to/through p. This does not account for bashing.
186          *
187          * This is used in pathfinding and ONLY checks the terrain. It ignores players
188          * and monsters, which might only block this tile temporarily.
189          * will_move_to() checks for impassable terrain etc
190          * can_reach_to() checks for z-level difference.
191          * can_move_to() is a wrapper for both of them.
192          */
193         bool can_move_to( const tripoint &p ) const;
194         bool can_reach_to( const tripoint &p ) const;
195         bool will_move_to( const tripoint &p ) const;
196 
197         bool will_reach( const point &p ); // Do we have plans to get to (x, y)?
198         int  turns_to_reach( const point &p ); // How long will it take?
199 
200         // Go in a straight line to p
201         void set_dest( const tripoint &p );
202         // Reset our plans, we've become aimless.
203         void unset_dest();
204 
205         /**
206          * Set p as wander destination.
207          *
208          * This will cause the monster to slowly move towards the destination,
209          * unless there is an overriding smell or plan.
210          *
211          * @param p Destination of monster's wanderings
212          * @param f The priority of the destination, as well as how long we should
213          *          wander towards there.
214          */
215         void wander_to( const tripoint &p, int f ); // Try to get to (x, y), we don't know
216         // the route.  Give up after f steps.
217 
218         // How good of a target is given creature (checks for visibility)
219         float rate_target( Creature &c, float best, bool smart = false ) const;
220         void plan();
221         void move(); // Actual movement
222         void footsteps( const tripoint &p ); // noise made by movement
223         void shove_vehicle( const tripoint &remote_destination,
224                             const tripoint &nearby_destination ); // shove vehicles out of the way
225 
226         // check if the given square could drown a drownable monster
227         bool is_aquatic_danger( const tripoint &at_pos );
228 
229         // check if a monster at a position will drown and kill it if necessary
230         // returns true if the monster dies
231         // chance is the one_in( chance ) that the monster will drown
232         bool die_if_drowning( const tripoint &at_pos, int chance = 1 );
233 
234         tripoint scent_move();
235         int calc_movecost( const tripoint &f, const tripoint &t ) const;
236         int calc_climb_cost( const tripoint &f, const tripoint &t ) const;
237 
238         bool is_immune_field( const field_type_id &fid ) const override;
239 
240         /**
241          * Attempt to move to p.
242          *
243          * If there's something blocking the movement, such as infinite move
244          * costs at the target, an existing NPC or monster, this function simply
245          * aborts and does nothing.
246          *
247          * @param p Destination of movement
248          * @param force If this is set to true, the movement will happen even if
249          *              there's currently something, else than a creature, blocking the destination.
250          * @param step_on_critter If this is set to true, the movement will happen even if
251          *              there's currently a creature blocking the destination.
252          *
253          * @param stagger_adjustment is a multiplier for move cost to compensate for staggering.
254          *
255          * @return true if movement successful, false otherwise
256          */
257         bool move_to( const tripoint &p, bool force = false, bool step_on_critter = false,
258                       float stagger_adjustment = 1.0 );
259 
260         /**
261          * Attack any enemies at the given location.
262          *
263          * Attacks only if there is a creature at the given location towards
264          * we are hostile.
265          *
266          * @return true if something was attacked, false otherwise
267          */
268         bool attack_at( const tripoint &p );
269 
270         /**
271          * Try to smash/bash/destroy your way through the terrain at p.
272          *
273          * @return true if we destroyed something, false otherwise.
274          */
275         bool bash_at( const tripoint &p );
276 
277         /**
278          * Try to push away whatever occupies p, then step in.
279          * May recurse and try to make the creature at p push further.
280          *
281          * @param p Location of pushed object
282          * @param boost A bonus on the roll to represent a horde pushing from behind
283          * @param depth Number of recursions so far
284          *
285          * @return True if we managed to push something and took its place, false otherwise.
286          */
287         bool push_to( const tripoint &p, int boost, size_t depth );
288 
289         /** Returns innate monster bash skill, without calculating additional from helpers */
290         int bash_skill();
291         int bash_estimate();
292         /** Returns ability of monster and any cooperative helpers to
293          * bash the designated target.  **/
294         int group_bash_skill( const tripoint &target );
295 
296         void stumble();
297         void knock_back_to( const tripoint &to ) override;
298 
299         // Combat
300         bool is_fleeing( Character &u ) const;
301         monster_attitude attitude( const Character *u = nullptr ) const; // See the enum above
302         Attitude attitude_to( const Creature &other ) const override;
303         void process_triggers(); // Process things that anger/scare us
304 
305         bool is_underwater() const override;
306         bool is_on_ground() const override;
307         bool is_warm() const override;
308         bool in_species( const species_id &spec ) const override;
309 
310         bool has_weapon() const override;
311         bool is_dead_state() const override; // check if we should be dead or not
312         bool is_elec_immune() const override;
313         bool is_immune_effect( const efftype_id & ) const override;
314         bool is_immune_damage( damage_type ) const override;
315 
316         void absorb_hit( const bodypart_id &bp, damage_instance &dam ) override;
317         bool block_hit( Creature *source, bodypart_id &bp_hit, damage_instance &d ) override;
318         bool melee_attack( Creature &target );
319         bool melee_attack( Creature &target, float accuracy );
320         void melee_attack( Creature &p, bool ) = delete;
321         void deal_projectile_attack( Creature *source, dealt_projectile_attack &attack,
322                                      bool print_messages = true ) override;
323         void deal_damage_handle_type( const effect_source &source, const damage_unit &du, bodypart_id bp,
324                                       int &damage, int &pain ) override;
325         void apply_damage( Creature *source, bodypart_id bp, int dam,
326                            bool bypass_med = false ) override;
327         // create gibs/meat chunks/blood etc all over the place, does not kill, can be called on a dead monster.
328         void explode();
329         // Let the monster die and let its body explode into gibs
330         void die_in_explosion( Creature *source );
331 
332         void heal_bp( bodypart_id bp, int dam ) override;
333         /**
334          * Flat addition to the monsters @ref hp. If `overheal` is true, this is not capped by max hp.
335          * Returns actually healed hp.
336          */
337         int heal( int delta_hp, bool overheal = false );
338         /**
339          * Directly set the current @ref hp of the monster (not capped at the maximal hp).
340          * You might want to use @ref heal / @ref apply_damage or @ref deal_damage instead.
341          */
342         void set_hp( int hp );
343 
344         /** Processes monster-specific effects before calling Creature::process_effects(). */
345         void process_effects() override;
346 
347         /** Returns true if the monster has its movement impaired */
348         bool movement_impaired();
349         /** Processes effects which may prevent the monster from moving (bear traps, crushed, etc.).
350          *  Returns false if movement is stopped. */
351         bool move_effects( bool attacking ) override;
352 
353         /** Returns a std::string containing effects for descriptions */
354         std::string get_effect_status() const;
355 
356         float power_rating() const override;
357         float speed_rating() const override;
358 
359         int get_worn_armor_val( damage_type dt ) const;
360         int  get_armor_cut( bodypart_id bp ) const override; // Natural armor, plus any worn armor
361         int  get_armor_bash( bodypart_id bp ) const override; // Natural armor, plus any worn armor
362         int  get_armor_bullet( bodypart_id bp ) const override; // Natural armor, plus any worn armor
363         int  get_armor_type( damage_type dt, bodypart_id bp ) const override;
364 
365         float get_hit_base() const override;
366         float get_dodge_base() const override;
367 
368         float  get_dodge() const override;       // Natural dodge, or 0 if we're occupied
369         float  get_melee() const override; // For determining attack skill when awarding dodge practice.
370         float  hit_roll() const override;  // For the purposes of comparing to player::dodge_roll()
371         float  dodge_roll() const override;  // For the purposes of comparing to player::hit_roll()
372 
373         int get_grab_strength() const; // intensity of grabbed effect
374 
375         monster_horde_attraction get_horde_attraction();
376         void set_horde_attraction( monster_horde_attraction mha );
377         bool will_join_horde( int size );
378 
379         /** Returns multiplier on fall damage at low velocity (knockback/pit/1 z-level, not 5 z-levels) */
380         float fall_damage_mod() const override;
381         /** Deals falling/collision damage with terrain/creature at pos */
382         int impact( int force, const tripoint &pos ) override;
383 
384         bool has_grab_break_tec() const override;
385 
386         float stability_roll() const override;
387         // We just dodged an attack from something
388         void on_dodge( Creature *source, float difficulty ) override;
389         // Something hit us (possibly null source)
390         void on_hit( Creature *source, bodypart_id bp_hit,
391                      float difficulty = INT_MIN, dealt_projectile_attack const *proj = nullptr ) override;
392 
393         /** Resets a given special to its monster type cooldown value */
394         void reset_special( const std::string &special_name );
395         /** Resets a given special to a value between 0 and its monster type cooldown value. */
396         void reset_special_rng( const std::string &special_name );
397         /** Sets a given special to the given value */
398         void set_special( const std::string &special_name, int time );
399         /** Sets the enabled flag for the given special to false */
400         void disable_special( const std::string &special_name );
401         /** Test whether the specified special is ready. */
402         bool special_available( const std::string &special_name ) const;
403 
404         void process_turn() override;
405         /** Resets the value of all bonus fields to 0, clears special effect flags. */
406         void reset_bonuses() override;
407         /** Resets stats, and applies effects in an idempotent manner */
408         void reset_stats() override;
409 
410         void die( Creature *killer ) override; //this is the die from Creature, it calls kill_mo
411         void drop_items_on_death();
412 
413         // Other
414         /**
415          * Makes this monster into a fungus version
416          * Returns false if no such monster exists
417          */
418         bool make_fungus();
419         void make_friendly();
420         /** Makes this monster an ally of the given monster. */
421         void make_ally( const monster &z );
422         // Add an item to inventory
423         void add_item( const item &it );
424         // check mech power levels and modify it.
425         bool use_mech_power( int amt );
426         bool check_mech_powered() const;
427         int mech_str_addition() const;
428 
429         /**
430          * Makes monster react to heard sound
431          *
432          * @param source Location of the sound source
433          * @param vol Volume at the center of the sound source
434          * @param distance Distance to sound source (currently just rl_dist)
435          */
436         void hear_sound( const tripoint &source, int vol, int distance );
437 
438         bool is_hallucination() const override;    // true if the monster isn't actually real
439 
440         field_type_id bloodType() const override;
441         field_type_id gibType() const override;
442 
443         using Creature::add_msg_if_npc;
444         void add_msg_if_npc( const std::string &msg ) const override;
445         void add_msg_if_npc( const game_message_params &params, const std::string &msg ) const override;
446         using Creature::add_msg_player_or_npc;
447         void add_msg_player_or_npc( const std::string &player_msg,
448                                     const std::string &npc_msg ) const override;
449         void add_msg_player_or_npc( const game_message_params &params, const std::string &player_msg,
450                                     const std::string &npc_msg ) const override;
451         // TEMP VALUES
452         tripoint wander_pos; // Wander destination - Just try to move in that direction
453         int wandf = 0;       // Urge to wander - Increased by sound, decrements each move
454         std::vector<item> inv; // Inventory
455         Character *mounted_player = nullptr; // player that is mounting this creature
456         character_id mounted_player_id; // id of player that is mounting this creature ( for save/load )
457         character_id dragged_foe_id; // id of character being dragged by the monster
458         cata::value_ptr<item> tied_item; // item used to tie the monster
459         cata::value_ptr<item> tack_item; // item representing saddle and reins and such
460         cata::value_ptr<item> armor_item; // item of armor the monster may be wearing
461         cata::value_ptr<item> storage_item; // storage item for monster carrying items
462         cata::value_ptr<item> battery_item; // item to power mechs
463         units::mass get_carried_weight();
464         units::volume get_carried_volume();
465         void move_special_item_to_inv( cata::value_ptr<item> &it );
466 
467         // DEFINING VALUES
468         int friendly = 0;
469         int anger = 0;
470         int morale = 0;
471         // Our faction (species, for most monsters)
472         mfaction_id faction;
473         // If we're related to a mission
474         std::set<int> mission_ids;
475         // Names of mission monsters fused with this monster
476         std::vector<std::string> mission_fused;
477         const mtype *type;
478         // If true, don't spawn loot items as part of death.
479         bool no_extra_death_drops = false;
480         // If true, monster dies quietly and leaves no corpse.
481         bool no_corpse_quiet = false;
482         // Turned to false for simulating monsters during distant missions so they don't drop in sight.
483         bool death_drops = true;
484         // If true, sound and message is suppressed for monster death.
485         bool quiet_death = false;
486         bool is_dead() const;
487         bool made_footstep = false;
488         // If we're unique
489         std::string unique_name;
490         bool hallucination = false;
491         // abstract for a fish monster representing a hidden stock of population in that area.
492         int fish_population = 1;
493 
494         void setpos( const tripoint &p ) override;
pos()495         inline const tripoint &pos() const override {
496             return position;
497         }
posx()498         inline int posx() const override {
499             return position.x;
500         }
posy()501         inline int posy() const override {
502             return position.y;
503         }
posz()504         inline int posz() const override {
505             return position.z;
506         }
507 
508         short ignoring = 0;
509         cata::optional<time_point> lastseen_turn;
510 
511         // Stair data.
512         int staircount = 0;
513 
514         // Ammunition if we use a gun.
515         std::map<itype_id, int> ammo;
516 
517         /**
518          * Convert this monster into an item (see @ref mtype::revert_to_itype).
519          * Only useful for robots and the like, the monster must have at least
520          * a non-empty item id as revert_to_itype.
521          */
522         item to_item() const;
523         /**
524          * Initialize values like speed / hp from data of an item.
525          * This applies to robotic monsters that are spawned by invoking an item (e.g. turret),
526          * and to reviving monsters that spawn from a corpse.
527          */
528         void init_from_item( const item &itm );
529 
530         /**
531          * Do some cleanup and caching as monster is being unloaded from map.
532          */
533         void on_unload();
534         /**
535          * Retroactively update monster.
536          * Call this after a preexisting monster has been placed on map.
537          * Don't call for monsters that have been freshly created, it may cause
538          * the monster to upgrade itself into another monster type.
539          */
540         void on_load();
541 
542         const pathfinding_settings &get_pathfinding_settings() const override;
543         std::set<tripoint> get_path_avoid() const override;
544         // summoned monsters via spells
545         void set_summon_time( const time_duration &length );
546         // handles removing the monster if the timer runs out
547         void decrement_summon_timer();
548     private:
549         void process_trigger( mon_trigger trig, int amount );
550         void process_trigger( mon_trigger trig, const std::function<int()> &amount_func );
551 
552     private:
553         int hp = 0;
554         std::map<std::string, mon_special_attack> special_attacks;
555         tripoint goal;
556         tripoint position;
557         bool dead = false;
558         /** Normal upgrades **/
559         int next_upgrade_time();
560         bool upgrades = false;
561         int upgrade_time = 0;
562         bool reproduces = false;
563         cata::optional<time_point> baby_timer;
564         bool biosignatures = false;
565         cata::optional<time_point> biosig_timer;
566         time_point udder_timer;
567         monster_horde_attraction horde_attraction = MHA_NULL;
568         /** Found path. Note: Not used by monsters that don't pathfind! **/
569         std::vector<tripoint> path;
570         std::bitset<NUM_MEFF> effect_cache;
571         cata::optional<time_duration> summon_time_limit = cata::nullopt;
572         int turns_since_target = 0;
573 
574         player *find_dragged_foe();
575         void nursebot_operate( player *dragged_foe );
576 
577     protected:
578         void store( JsonOut &json ) const;
579         void load( const JsonObject &data );
580 
581         /** Processes monster-specific effects of an effect. */
582         void process_one_effect( effect &it, bool is_new ) override;
583 };
584 
585 #endif // CATA_SRC_MONSTER_H
586