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 CREATUREEFFECT_H
19 #define CREATUREEFFECT_H
20 
21 #include <cstdint>
22 #include <istream>
23 
24 class Creature;
25 
26 class CreatureEffect
27 {
28 public:
29     // If particleEffectName is not empty, the corresponding particle effect will be
30     // sent when the effect is added to the creature for nbTurnsEffect turns.
CreatureEffect(uint32_t nbTurnsEffect,const std::string & particleEffectScript)31     CreatureEffect(uint32_t nbTurnsEffect, const std::string& particleEffectScript) :
32         mNbTurnsEffect(nbTurnsEffect),
33         mParticleEffectScript(particleEffectScript)
34     {}
35 
~CreatureEffect()36     virtual ~CreatureEffect()
37     {}
38 
39     virtual const std::string& getEffectName() const = 0;
40 
getNbTurnsEffect()41     inline uint32_t getNbTurnsEffect() const
42     { return mNbTurnsEffect; }
43 
getParticleEffectScript()44     inline const std::string& getParticleEffectScript() const
45     { return mParticleEffectScript; }
46 
47     //! This function will be called when the effect is added to the creature
startEffect(Creature & creature)48     virtual void startEffect(Creature& creature)
49     {}
50 
51     //! This function will be called during the creature upkeep
52     bool upkeepEffect(Creature& creature);
53 
54     //! This function will be called when the effect is released
releaseEffect(Creature & creature)55     virtual void releaseEffect(Creature& creature)
56     {}
57 
58     static void write(const CreatureEffect& effect, std::ostream& os);
59     //! loads a CreatureEffect from the given stream. Note that the stream might contain
60     //! more data than just the CreatureEffect. Thus, we should not use optional data
61     //! by reading the stream and expecting an EOF. If the data may vary, we should
62     //! write how many data there is and read them accordingly
63     static CreatureEffect* load(std::istream& is);
64 
65     //! Writes the CreatureEffect to the stream
66     virtual void exportToStream(std::ostream& os) const;
67     virtual bool importFromStream(std::istream& is);
68 
69 protected:
70     //! This function will be called during the creature upkeep and should apply
71     //! the wanted effect
72     virtual void applyEffect(Creature& creature) = 0;
CreatureEffect()73     CreatureEffect() :
74         mNbTurnsEffect(0)
75     {}
76 
77 private:
78     uint32_t mNbTurnsEffect;
79     std::string mParticleEffectScript;
80 };
81 
82 #endif // CREATUREEFFECT_H
83