Advanced Strategic Command
spawnmine.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 "spawnmine.h"
23 #include "action-registry.h"
24 
25 #include "../vehicle.h"
26 #include "../gamemap.h"
27 
28 SpawnMine::SpawnMine( GameMap* gamemap, const MapCoordinate& position, MineTypes mineType, int owner, int strength )
29  : GameAction( gamemap ), pos(position)
30 {
31  this->type = mineType;
32  this->owner = owner;
33  this->strength = strength;
34  stackPosition = -1;
35  mapNetworkIdCounterAfter = -1;
36  mapNetworkIdCounterBefore = -1;
37 }
38 
39 
41 {
42  return "Spawn mine at position " + pos.toString();
43 }
44 
45 static const int mineDataVersion = 2;
46 
47 void SpawnMine::readData ( tnstream& stream )
48 {
49  int version = stream.readInt();
50  if ( version < 1 || version > mineDataVersion )
51  throw tinvalidversion ( "SpawnMine", mineDataVersion, version );
52 
53  type = (MineTypes) stream.readInt();
54  pos.read( stream );
55  owner = stream.readInt();
56  strength = stream.readInt();
57  stackPosition = stream.readInt();
58  if ( version >= 2 ) {
59  mapNetworkIdCounterBefore = stream.readInt();
60  mapNetworkIdCounterAfter = stream.readInt();
61  }
62 };
63 
64 
65 void SpawnMine::writeData ( tnstream& stream ) const
66 {
67  stream.writeInt( mineDataVersion );
68  stream.writeInt( type );
69  pos.write( stream );
70  stream.writeInt( owner );
71  stream.writeInt( strength );
72  stream.writeInt( stackPosition );
73  stream.writeInt( mapNetworkIdCounterBefore );
74  stream.writeInt( mapNetworkIdCounterAfter );
75 };
76 
77 
79 {
81 }
82 
84 {
85  MapField* fld = getMap()->getField(pos);
86  if ( !fld )
87  return ActionResult( 21002, pos );
88 
89  mapNetworkIdCounterBefore = getMap()->idManager.unitnetworkid;
90 
91  bool result = fld->putmine( owner, type, strength );
92 
93  mapNetworkIdCounterAfter = getMap()->idManager.unitnetworkid;
94 
95 
96  if ( result ) {
97  stackPosition = fld->mines.size();
98  return ActionResult(0);
99  } else
100  return ActionResult(21600);
101 }
102 
103 
105 {
106  MapField* fld = getMap()->getField(pos);
107  if ( !fld )
108  return ActionResult( 21002, pos );
109 
110  if ( fld->mines.size() != stackPosition )
111  return ActionResult(21601);
112 
113  if ( getMap()->idManager.unitnetworkid != mapNetworkIdCounterAfter )
114  return ActionResult( 21605 );
115 
116  getMap()->idManager.unitnetworkid = mapNetworkIdCounterBefore;
117 
118  fld->mines.pop_back();
119 
120 
121 
122  return ActionResult(0);
123 }
124 
126 {
127  if ( getMap()->idManager.unitnetworkid != mapNetworkIdCounterAfter )
128  return ActionResult( 21605 );
129 
130  return ActionResult(0);
131 }
132 
133 
134 namespace {
135  const bool r1 = registerAction<SpawnMine> ( ActionRegistry::SpawnMine );
136 }
137 
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
bool putmine(int owner, MineTypes typ, int strength)
put a mine of type typ for player owner and a punch of strength on the field. Strength is an absolute...
Definition: mapfield.cpp:441
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
virtual GameActionID getID() const
Definition: spawnmine.cpp:78
virtual void writeData(tnstream &stream) const
Definition: spawnmine.cpp:65
virtual ActionResult runAction(const Context &context)
Definition: spawnmine.cpp:83
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
static const int mineDataVersion
Definition: spawnmine.cpp:45
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
MineTypes
Definition: objects.h:70
void write(tnstream &stream) const
Definition: typen.h:212
Coordinate on the twodimensional map.
Definition: typen.h:202
ASCString getDescription() const
Definition: spawnmine.cpp:40
virtual ActionResult undoAction(const Context &context)
Definition: spawnmine.cpp:104
MineContainer mines
Definition: mapfield.h:117
class GameMap::IDManager idManager
virtual ActionResult verify()
Definition: spawnmine.cpp:125
A GameAction is an modification of something on a map.
Definition: action.h:45
virtual void readData(tnstream &stream)
Definition: spawnmine.cpp:47
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