Advanced Strategic Command
spawnobject.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 #include "spawnobject.h"
22 #include "action-registry.h"
23 
24 #include "removeobject.h"
25 #include "../vehicle.h"
26 #include "../gamemap.h"
27 
28 SpawnObject::SpawnObject( GameMap* gamemap, const MapCoordinate& position, int objectID, int direction )
29  : GameAction( gamemap ), pos(position)
30 {
31  this->objectID = objectID;
32  this->direction = direction;
33  objectLaid = false;
34 
35  objectAvailableBeforehand = false;
36  oldObjectDirection = 0;
37  objectImmediatelyDisappearsAgain = false;
38 }
39 
40 
42 {
43  const ObjectType* object = getMap()->getobjecttype_byid( objectID );
44  ASCString res ;
45  if ( object )
46  res = "Spawn Object of type " + object->getName();
47  else
48  res = "Spawn Object " ;
49  res += " at " + pos.toString();
50  return res;
51 }
52 
53 static const int spawnObjectStreamVersion = 3;
54 
56 {
57  int version = stream.readInt();
58  if ( version < 1 || version > spawnObjectStreamVersion )
59  throw tinvalidversion ( "SpawnObject", spawnObjectStreamVersion, version );
60 
61  objectID = stream.readInt();
62  pos.read( stream );
63  direction = stream.readInt();
64  objectLaid = stream.readInt();
65  if ( version >= 2 ) {
66  objectAvailableBeforehand = stream.readInt();
67  oldObjectDirection = stream.readInt();
68  } else {
69  objectAvailableBeforehand = false;
70  oldObjectDirection = 0;
71  }
72 
73  if ( version >= 3 )
74  objectImmediatelyDisappearsAgain = stream.readInt();
75  else
76  objectImmediatelyDisappearsAgain = false;
77 };
78 
79 
80 void SpawnObject::writeData ( tnstream& stream ) const
81 {
83  stream.writeInt( objectID );
84  pos.write( stream );
85  stream.writeInt( direction );
86  stream.writeInt( objectLaid );
87  stream.writeInt( objectAvailableBeforehand );
88  stream.writeInt( oldObjectDirection );
89  stream.writeInt( objectImmediatelyDisappearsAgain );
90 };
91 
92 
94 {
96 }
97 
99  const Context& context;
100  const ObjectType* originalObject;
101  bool immediateRemoval;
102  public:
103  ActionObjectRemovalStrategy( const Context& actionContext, const ObjectType* object )
104  : context( actionContext ), originalObject( object ), immediateRemoval(false)
105  {}
106 
107  virtual void removeObject( MapField* fld, const ObjectType* objectType ) {
108  if ( objectType != originalObject )
109  (new RemoveObject( fld->getMap(), fld->getPosition(), objectType->id ))->execute( context );
110  else {
111  for ( MapField::ObjectContainer::iterator o = fld->objects.begin(); o != fld->objects.end(); ) {
112  if ( o->typ == objectType ) {
113  o = fld->objects.erase( o );
114  } else
115  ++o ;
116  }
117 
118  immediateRemoval = true;
119  }
120  };
121 
123  {
124  return immediateRemoval;
125  }
126 };
127 
129 {
130  MapField* fld = getMap()->getField(pos);
131  if ( !fld )
132  return ActionResult( 21002, pos );
133 
134  const ObjectType* object = getMap()->getobjecttype_byid( objectID );
135  if ( !object )
136  return ActionResult( 21201, "Object id is " + ASCString::toString(objectID));
137 
138  Object* old = fld->checkForObject( object );
139  if ( old ) {
140  objectAvailableBeforehand = true;
141  oldObjectDirection = old->dir;
142  } else {
143  objectAvailableBeforehand = false;
144  oldObjectDirection = 0;
145  }
146 
147  ActionObjectRemovalStrategy aors( context, object );
148  objectLaid = fld->addobject( object, direction, false, &aors );
149 
150  objectImmediatelyDisappearsAgain = aors.getImmediateRemoval();
151  return ActionResult(0);
152 }
153 
154 
156 {
157  MapField* fld = getMap()->getField(pos);
158  if ( !fld )
159  return ActionResult( 21002, pos );
160 
161  const ObjectType* object = getMap()->getobjecttype_byid( objectID );
162  if ( !object )
163  return ActionResult( 21201, "Object id is " + ASCString::toString(objectID));
164 
165  if ( objectAvailableBeforehand ) {
166  Object* o = fld->checkForObject( object );
167  if ( !o )
168  return ActionResult( 21505 );
169  o->dir = oldObjectDirection;
170  } else
171  if ( !objectImmediatelyDisappearsAgain )
172  fld->removeObject( object, true );
173 
174  return ActionResult(0);
175 }
176 
178 {
179  return ActionResult(0);
180 }
181 
182 
183 namespace {
184  const bool r1 = registerAction<SpawnObject> ( ActionRegistry::SpawnObject );
185 }
186 
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 id
the id of the object, used when referencing objects in files
Definition: objecttype.h:35
int GameActionID
Definition: action.h:35
virtual ActionResult runAction(const Context &context)
virtual void readData(tnstream &stream)
Definition: spawnobject.cpp:55
virtual void writeData(tnstream &stream) const
Definition: spawnobject.cpp:80
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 GameActionID getID() const
Definition: spawnobject.cpp:93
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
Interface for removing objects from a field when it turns out that they can no longer exist...
Definition: mapfield.h:127
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
virtual ActionResult verify()
static const int spawnObjectStreamVersion
Definition: spawnobject.cpp:53
ActionObjectRemovalStrategy(const Context &actionContext, const ObjectType *object)
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
void write(tnstream &stream) const
Definition: typen.h:212
GameMap * getMap() const
Definition: mapfield.h:38
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 void removeObject(MapField *fld, const ObjectType *objectType)
int dir
Definition: objects.h:51
bool addobject(const ObjectType *obj, int dir=-1, bool force=false, ObjectRemovalStrategy *objectRemovalStrategy=NULL)
add an object to the field
Definition: mapfield.cpp:212
virtual ActionResult undoAction(const Context &context)
ASCString getDescription() const
Definition: spawnobject.cpp:41
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
MapCoordinate getPosition()
Definition: mapfield.cpp:578
ObjectType * getobjecttype_byid(int id)
Definition: gamemap.cpp:1783
Object * checkForObject(const ObjectType *o)
checks if there are objects from the given type on the field and returns them
Definition: mapfield.cpp:648
bool removeObject(const ObjectType *obj, bool force=false, ObjectRemovalStrategy *objectRemovalStrategy=NULL)
removes all objects of the given type from the field
Definition: mapfield.cpp:256
ObjectContainer objects
Definition: mapfield.h:124
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