Advanced Strategic Command
changeobjectproperty.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 "changeobjectproperty.h"
23 #include "removeobject.h"
24 #include "action-registry.h"
25 
26 #include "../gamemap.h"
27 
28 ChangeObjectProperty::ChangeObjectProperty( GameMap* map, MapCoordinate& pos, Object* object, Property property, int value, bool valueIsAbsolute )
29  : GameAction( map ), originalValue(-1), resultingValue(-1)
30 {
31  this->position = pos;
32  this->value = value;
33  this->property = property;
34  this->objectID = object->typ->id;
35  this->valueIsAbsolute = valueIsAbsolute;
36 }
37 
38 ASCString ChangeObjectProperty::getPropertyName( Property property )
39 {
40  switch ( property ) {
41  case Damage: return "Damage";
42  };
43  return "";
44 }
45 
46 
48 {
49  ASCString res = "Change property " + getPropertyName(property) + " of object with id " + ASCString::toString(objectID)
50  + " on field " + position.toString(false);
51 
52  if ( !valueIsAbsolute )
53  res += " by ";
54  else
55  res += " to ";
56  res += ASCString::toString(value);
57  return res;
58 }
59 
60 
62 {
63  int version = stream.readInt();
64  if ( version != 1 )
65  throw tinvalidversion ( "ChangeObjectProperty", 1, version );
66 
67  position.read( stream );
68  objectID = stream.readInt();
69  property = (Property) stream.readInt();
70  value = stream.readInt();
71  valueIsAbsolute = stream.readInt();
72  originalValue = stream.readInt();
73  resultingValue = stream.readInt();
74 
75 };
76 
77 
79 {
80  stream.writeInt( 1 );
81  position.write( stream );
82  stream.writeInt( objectID );
83  stream.writeInt( property );
84  stream.writeInt( value );
85  stream.writeInt( valueIsAbsolute );
86  stream.writeInt( originalValue );
87  stream.writeInt( resultingValue );
88 };
89 
90 
92 {
94 }
95 
96 Object* ChangeObjectProperty::getObject()
97 {
98  MapField* fld = getMap()->getField( position );
99  if ( !fld )
100  throw ActionResult(21002);
101 
102  ObjectType* ot = getMap()->getobjecttype_byid( objectID );
103  if ( !ot )
104  throw ActionResult( 21501 );
105 
106  Object* o = fld->checkForObject( ot );
107  if ( !o )
108  throw ActionResult ( 21502 );
109 
110  return o;
111 }
112 
113 
114 int ChangeObjectProperty::getProperty()
115 {
116  switch ( property ) {
117  case Damage: return getObject()->damage;
118  };
119  throw ActionResult(21503 );
120 }
121 
122 ActionResult ChangeObjectProperty::setProperty( Property property, int value, const Context& context )
123 {
124  switch ( property ) {
125  case Damage:
126  getObject()->damage = value;
127  break;
128  default:
129  throw ActionResult(21503 );
130  };
131  return ActionResult(0);
132 }
133 
134 
136 {
137  originalValue = getProperty();
138 
139  ActionResult res(0);
140  if ( !valueIsAbsolute )
141  res = setProperty( property, originalValue + value, context );
142  else
143  res = setProperty( property, value, context );
144 
145  resultingValue = getProperty();
146  return res;
147 }
148 
149 
151 {
152  return setProperty ( property, originalValue, context );
153 }
154 
156 {
157  if ( getProperty() != originalValue )
158  throw ActionResult( 21204, getPropertyName( property ) );
159 
160  return ActionResult(0);
161 }
162 
164 {
165  if ( getProperty() != resultingValue )
166  throw ActionResult( 21204, getPropertyName( property ) );
167 
168  return ActionResult(0);
169 }
170 
171 
172 namespace {
173  const bool r1 = registerAction<ChangeObjectProperty> ( ActionRegistry::ChangeObjectProperty );
174 }
175 
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 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
int damage
Definition: objects.h:49
void read(tnstream &stream)
Definition: typen.h:213
GameMap * getMap()
Definition: action.h:92
ASCString toString(bool coordinates=false) const
Definition: typen.cpp:304
The interface for all kinds of IO stream.
a single field of the map
Definition: mapfield.h:26
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
virtual void readData(tnstream &stream)
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
void write(tnstream &stream) const
Definition: typen.h:212
An object that can be placed on fields. Roads, pipelines and ditches are examples of objects...
Definition: objecttype.h:30
Coordinate on the twodimensional map.
Definition: typen.h:202
virtual ActionResult preCheck()
preCheck is called before an action is redone and should check the preconditions for the action...
virtual ActionResult postCheck()
postCheck is called after an action has been redone and should check that the state of the map is exa...
virtual ActionResult runAction(const Context &context)
an instance of an object type (ObjectType) on the map
Definition: objects.h:46
A GameAction is an modification of something on a map.
Definition: action.h:45
ObjectType * getobjecttype_byid(int id)
Definition: gamemap.cpp:1783
ASCString getDescription() const
virtual void writeData(tnstream &stream) const
Object * checkForObject(const ObjectType *o)
checks if there are objects from the given type on the field and returns them
Definition: mapfield.cpp:648
virtual ActionResult undoAction(const Context &context)
virtual GameActionID getID() const
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182
MapField * getField(int x, int y)
Definition: gamemap.h:465