Advanced Strategic Command
changeunitproperty.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.de
3  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 
22 #include "changeunitproperty.h"
23 #include "action-registry.h"
24 
25 #include "../vehicle.h"
26 #include "../gamemap.h"
27 
28 ChangeUnitProperty::ChangeUnitProperty( Vehicle* vehicle, Property property, int value, bool valueIsAbsolute )
29  : UnitAction( vehicle->getMap(), vehicle->networkid ), originalValue(-1), resultingValue(-1)
30 {
31  this->property = property;
32  this->value = value;
33  this->valueIsAbsolute = valueIsAbsolute;
34 }
35 
36 ASCString ChangeUnitProperty::getPropertyName( Property property )
37 {
38  switch ( property ) {
39  case ExperienceOffensive: return "Offensive Experience";
40  case ExperienceOffensive_Raw: return "Offensive Experience (RAW)";
41  case ExperienceDefensive: return "Defensive Experience";
42  case ExperienceDefensive_Raw: return "Defensive Experience (RAW)";
43  case Movement: return "Movement";
44  case AttackedFlag: return "AttackedFlag";
45  case Height: return "Height";
46  case Direction: return "Direction";
47  case ReactionFire: return "ReactionFire";
48  case PowerGeneration: return "PowerGeneration";
49  };
50  return "";
51 }
52 
53 
55 {
56  ASCString res = "Change property " + getPropertyName(property);
57  if ( getUnit(false) )
58  res += " of unit " + getUnit(false)->getName();
59 
60  if ( !valueIsAbsolute )
61  res += " by ";
62  else
63  res += " to ";
64  res += ASCString::toString(value);
65  return res;
66 }
67 
68 
70 {
71  UnitAction::readData( stream );
72  int version = stream.readInt();
73  if ( version != 1 )
74  throw tinvalidversion ( "ChangeUnitProperty", 1, version );
75 
76  property = (Property) stream.readInt();
77  value = stream.readInt();
78  valueIsAbsolute = stream.readInt();
79  originalValue = stream.readInt();
80  resultingValue = stream.readInt();
81 
82 };
83 
84 
86 {
87  UnitAction::writeData( stream );
88  stream.writeInt( 1 );
89  stream.writeInt( property );
90  stream.writeInt( value );
91  stream.writeInt( valueIsAbsolute );
92  stream.writeInt( originalValue );
93  stream.writeInt( resultingValue );
94 };
95 
96 
98 {
100 }
101 
102 int ChangeUnitProperty::getUnitProperty()
103 {
104  switch ( property ) {
109  case Movement: return getUnit()->getMovement( false, false );
110  case AttackedFlag: return getUnit()->attacked;
111  case Height: return getUnit()->height;
112  case Direction: return getUnit()->direction;
113  case ReactionFire: return getUnit()->reactionfire.status;
114  case PowerGeneration: return getUnit()->getGeneratorStatus();
115  };
116  throw ActionResult(21203, getUnit() );
117 }
118 
119 void ChangeUnitProperty::setUnitProperty( Property property, int value, const Context& context )
120 {
121  switch ( property ) {
122  case ExperienceOffensive:
123  getUnit()->setExperience_offensive ( value );
124  break;
125  case ExperienceDefensive:
126  getUnit()->setExperience_defensive ( value );
127  break;
130  break;
133  break;
134  case Movement:
135  getUnit()->setMovement( value, 0 );
136  break;
137  case AttackedFlag:
138  getUnit()->attacked = (value != 0) ;
139  break;
140  case Height:
141  getUnit()->height = value;
142  break;
143  case Direction:
144  getUnit()->direction = value;
145  break;
146  case ReactionFire:
148  break;
149  case PowerGeneration:
150  getUnit()->setGeneratorStatus( value );
151  break;
152  default:
153  throw ActionResult(21203, getUnit() );
154  };
155 }
156 
157 
159 {
160  originalValue = getUnitProperty();
161 
162  if ( !valueIsAbsolute )
163  setUnitProperty( property, originalValue + value, context );
164  else
165  setUnitProperty( property, value, context );
166 
167  resultingValue = getUnitProperty();
168  return ActionResult(0);
169 }
170 
171 
173 {
174  setUnitProperty ( property, originalValue, context );
175 
176  return ActionResult(0);
177 }
178 
180 {
181  if ( getUnitProperty() != originalValue )
182  throw ActionResult( 21204, getUnit(), getPropertyName( property ) );
183 
184  return ActionResult(0);
185 }
186 
188 {
189  if ( getUnitProperty() != resultingValue )
190  throw ActionResult( 21204, getUnit(), getPropertyName( property ) );
191 
192  return ActionResult(0);
193 }
194 
195 
196 namespace {
197  const bool r1 = registerAction<ChangeUnitProperty> ( ActionRegistry::ChangeUnitProperty );
198 }
199 
int getExperience_offensive_raw() const
Definition: vehicle.cpp:263
int getExperience_defensive() const
Definition: vehicle.cpp:258
virtual void writeInt(int i)
Writes a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is p...
Definition: basestrm.cpp:363
int GameActionID
Definition: action.h:35
virtual ActionResult preCheck()
preCheck is called before an action is redone and should check the preconditions for the action...
ASCString getDescription() const
bool getGeneratorStatus() const
Definition: vehicle.h:323
virtual ActionResult postCheck()
postCheck is called after an action has been redone and should check that the state of the map is exa...
virtual int readInt(void)
Reads a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is pe...
Definition: basestrm.cpp:284
void writeData(tnstream &stream) const
Definition: unitaction.cpp:75
void setMovement(int newmove, double cargoDivisor=-1)
sets a new distance that the unit can move
Definition: vehicle.cpp:527
int getExperience_defensive_raw() const
Definition: vehicle.cpp:268
void setExperience_defensive(int experience)
Definition: vehicle.cpp:279
virtual GameActionID getID() const
void setGeneratorStatus(bool status)
sets the status of the unit's energy generator
Definition: vehicle.cpp:385
The interface for all kinds of IO stream.
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
int height
the current level of height ( BITMAPPED ! )
Definition: vehicle.h:118
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
class Vehicle::ReactionFire reactionfire
virtual ActionResult runAction(const Context &context)
void setExperience_offensive(int experience)
Definition: vehicle.cpp:274
Vehicle * getUnit(bool dontThrow=false)
Definition: unitaction.cpp:47
ASCString getName() const
returns the units name or, if it does not exist, the unit type's name or description ...
Definition: vehicle.cpp:1569
virtual void readData(tnstream &stream)
virtual void writeData(tnstream &stream) const
void setExperience_offensive_raw(int experience)
Definition: vehicle.cpp:285
Uint8 direction
the direction in which the unit is facing
Definition: vehicle.h:121
bool attacked
did the unit already attack this turn
Definition: vehicle.h:109
Direction
Definition: weatherarea.h:84
void setExperience_defensive_raw(int experience)
Definition: vehicle.cpp:290
virtual ActionResult undoAction(const Context &context)
int getMovement(bool checkFuel=true, bool checkRF=true) const
returns the movement points the unit has left for this turn. CheckFuel should almost always be true...
Definition: vehicle.cpp:558
void readData(tnstream &stream)
Definition: unitaction.cpp:65
int getExperience_offensive() const
Definition: vehicle.cpp:253