Advanced Strategic Command
changecontainerproperty.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 
23 #include "action-registry.h"
24 
25 ChangeContainerProperty::ChangeContainerProperty( ContainerBase* container, Property property, int value, bool valueIsAbsolute )
26  : ContainerAction( container ), originalValue(-1), resultingValue(-1)
27 {
28  this->property = property;
29  this->value = value;
30  this->valueIsAbsolute = valueIsAbsolute;
31 }
32 
33 ASCString ChangeContainerProperty::getPropertyName( Property property )
34 {
35  switch ( property ) {
36  case RepairedThisTurn: return "RepairedThisTurn";
37  case Damage: return "Damage";
38  };
39  return "";
40 }
41 
42 
44 {
45  ASCString res = "Change property " + getPropertyName(property);
46  if ( getContainer(false) )
47  res += " of container " + getContainer(false)->getName();
48 
49  if ( !valueIsAbsolute )
50  res += " by ";
51  else
52  res += " to ";
53  res += ASCString::toString(value);
54  return res;
55 }
56 
57 
59 {
60  ContainerAction::readData( stream );
61  int version = stream.readInt();
62  if ( version != 1 )
63  throw tinvalidversion ( "ChangeContainerProperty", 1, version );
64 
65  property = (Property) stream.readInt();
66  value = stream.readInt();
67  valueIsAbsolute = stream.readInt();
68  originalValue = stream.readInt();
69  resultingValue = stream.readInt();
70 
71 };
72 
73 
75 {
77  stream.writeInt( 1 );
78  stream.writeInt( property );
79  stream.writeInt( value );
80  stream.writeInt( valueIsAbsolute );
81  stream.writeInt( originalValue );
82  stream.writeInt( resultingValue );
83 };
84 
85 
87 {
89 }
90 
91 int ChangeContainerProperty::getUnitProperty()
92 {
93 
94  switch ( property ) {
95  case Damage: return getContainer()->damage;
97  default:
98  throw ActionResult(21203, getContainer() );
99  };
100 }
101 
102 void ChangeContainerProperty::setUnitProperty( int value, const Context& context )
103 {
104  switch ( property ) {
105  case Damage:
106  getContainer()->damage = value;
107  break;
108  case RepairedThisTurn:
109  getContainer()->repairedThisTurn = value;
110  break;
111  default:
112  throw ActionResult(21203, getContainer() );
113  };
114 }
115 
116 
118 {
119  originalValue = getUnitProperty();
120 
121  if ( !valueIsAbsolute )
122  setUnitProperty( originalValue + value, context );
123  else
124  setUnitProperty( value, context );
125 
126  resultingValue = getUnitProperty();
127  return ActionResult(0);
128 }
129 
130 
132 {
133  setUnitProperty ( originalValue, context );
134  return ActionResult(0);
135 }
136 
138 {
139  if ( getUnitProperty() != originalValue )
140  throw ActionResult( 21204, getContainer(), getPropertyName( property ) );
141 
142  return ActionResult(0);
143 }
144 
146 {
147  if ( getUnitProperty() != resultingValue )
148  throw ActionResult( 21204, getContainer(), getPropertyName( property ) );
149 
150  return ActionResult(0);
151 }
152 
153 
154 namespace {
155  const bool r1 = registerAction<ChangeContainerProperty> ( ActionRegistry::ChangeContainerProperty );
156 }
void writeData(tnstream &stream) const
virtual void writeData(tnstream &stream) const
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
virtual ActionResult preCheck()
preCheck is called before an action is redone and should check the preconditions for the action...
int GameActionID
Definition: action.h:35
int damage
Damage. 0 is no damage, when damage reaches 100 the container is destroyed.
virtual ASCString getName() const =0
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
virtual void readData(tnstream &stream)
The interface for all kinds of IO stream.
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
int repairedThisTurn
the percantage that this container has already been repaired this turn.
Definition: containerbase.h:49
virtual ActionResult postCheck()
postCheck is called after an action has been redone and should check that the state of the map is exa...
ContainerBase * getContainer(bool dontThrow=false)
The parent class of Vehicle and Building; The name Container originates from Battle Isle...
Definition: containerbase.h:40
virtual ActionResult runAction(const Context &context)
void readData(tnstream &stream)
virtual ActionResult undoAction(const Context &context)
virtual GameActionID getID() const