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 #include "creatureeffect/CreatureEffectStrengthChange.h"
19 
20 #include "creatureeffect/CreatureEffectManager.h"
21 #include "entities/Creature.h"
22 #include "utils/LogManager.h"
23 
24 static const std::string CreatureEffectStrengthChangeName = "StrengthChange";
25 
26 namespace
27 {
28 class CreatureEffectStrengthChangeFactory : public CreatureEffectFactory
29 {
createCreatureEffect() const30     CreatureEffect* createCreatureEffect() const override
31     { return new CreatureEffectStrengthChange; }
32 
getCreatureEffectName() const33     const std::string& getCreatureEffectName() const override
34     {
35         return CreatureEffectStrengthChangeName;
36     }
37 };
38 
39 // Register the factory
40 static CreatureEffectRegister reg(new CreatureEffectStrengthChangeFactory);
41 }
42 
getEffectName() const43 const std::string& CreatureEffectStrengthChange::getEffectName() const
44 {
45     return CreatureEffectStrengthChangeName;
46 }
47 
applyEffect(Creature & creature)48 void CreatureEffectStrengthChange::applyEffect(Creature& creature)
49 {
50     if(!creature.isAlive())
51         return;
52 
53     if(mEffectValue == 1.0)
54         return;
55 
56     creature.setStrengthModifier(mEffectValue);
57     mEffectValue = 1.0;
58 }
59 
releaseEffect(Creature & creature)60 void CreatureEffectStrengthChange::releaseEffect(Creature& creature)
61 {
62     if(!creature.isAlive())
63         return;
64 
65     creature.clearStrengthModifier();
66     mEffectValue = 1.0;
67 }
68 
load(std::istream & is)69 CreatureEffectStrengthChange* CreatureEffectStrengthChange::load(std::istream& is)
70 {
71     CreatureEffectStrengthChange* effect = new CreatureEffectStrengthChange;
72     effect->importFromStream(is);
73     return effect;
74 }
75 
exportToStream(std::ostream & os) const76 void CreatureEffectStrengthChange::exportToStream(std::ostream& os) const
77 {
78     CreatureEffect::exportToStream(os);
79     os << "\t" << mEffectValue;
80 }
81 
importFromStream(std::istream & is)82 bool CreatureEffectStrengthChange::importFromStream(std::istream& is)
83 {
84     if(!CreatureEffect::importFromStream(is))
85         return false;
86     if(!(is >> mEffectValue))
87         return false;
88 
89     return true;
90 }
91