Advanced Strategic Command
spawnunit.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 "spawnunit.h"
23 #include "viewregistration.h"
24 #include "unitfieldregistration.h"
25 #include "action-registry.h"
26 
27 #include "../vehicle.h"
28 #include "../gamemap.h"
29 #include "../viewcalculation.h"
30 
31 SpawnUnit::SpawnUnit( GameMap* gamemap, const MapCoordinate3D& position, int vehicleTypeID, int owner )
32  : GameAction( gamemap ), pos(position), networkid(-1), carrierID(0), carrier( false ), mapNetworkIdCounterBefore(0), mapNetworkIdCounterAfter(0)
33 {
34  this->vehicleTypeID = vehicleTypeID;
35  this->owner = owner;
36 
37 }
38 
39 SpawnUnit::SpawnUnit( GameMap* gamemap, const ContainerBase* carrier, int vehicleTypeID )
40  : GameAction( gamemap ), pos( carrier->getPosition() ), networkid(-1), carrierID( carrier->getIdentification() ), carrier(true), mapNetworkIdCounterBefore(0), mapNetworkIdCounterAfter(0)
41 {
42  this->vehicleTypeID = vehicleTypeID;
43  this->owner = carrier->getOwner();
44 }
45 
46 
47 ContainerBase* SpawnUnit::getCarrier( bool dontThrow )
48 {
49  ContainerBase* veh = getMap()->getContainer( carrierID );
50  if ( !veh && !dontThrow )
51  throw ActionResult(21001, "ID is " + ASCString::toString(carrierID) );
52  else
53  return veh;
54 }
55 
56 
58 {
59  const VehicleType* veh = getMap()->getvehicletype_byid( vehicleTypeID );
60  ASCString res ;
61  if ( veh )
62  res = "Spawn Unit of type " + veh->getName();
63  else
64  res = "Spawn Object " ;
65  res += " at " + pos.toString();
66  return res;
67 }
68 
69 
70 static const int spawnUnitStreamVersion = 2;
71 
72 void SpawnUnit::readData ( tnstream& stream )
73 {
74  int version = stream.readInt();
75  if ( version < 1 || version > spawnUnitStreamVersion )
76  throw tinvalidversion ( "SpawnUnit", spawnUnitStreamVersion, version );
77 
78  vehicleTypeID = stream.readInt();
79  pos.read( stream );
80  owner = stream.readInt();
81  networkid = stream.readInt();
82  carrierID = stream.readInt();
83  carrier = stream.readInt();
84 
85  if ( version >= 2 ) {
86  mapNetworkIdCounterAfter = stream.readInt();
87  mapNetworkIdCounterBefore = stream.readInt();
88  }
89 };
90 
91 
92 void SpawnUnit::writeData ( tnstream& stream ) const
93 {
94  stream.writeInt( spawnUnitStreamVersion );
95  stream.writeInt( vehicleTypeID );
96  pos.write( stream );
97  stream.writeInt( owner );
98  stream.writeInt( networkid );
99  stream.writeInt( carrierID );
100  stream.writeInt( carrier );
101  stream.writeInt( mapNetworkIdCounterAfter );
102  stream.writeInt( mapNetworkIdCounterBefore );
103 };
104 
105 
107 {
109 }
110 
112 {
113  MapField* fld = getMap()->getField(pos);
114  if ( !fld )
115  return ActionResult( 21002, pos );
116 
117  const VehicleType* vehicleType = getMap()->getvehicletype_byid( vehicleTypeID );
118  if ( !vehicleType )
119  return ActionResult( 21801, "Vehicle id is " + ASCString::toString(vehicleTypeID));
120 
121  mapNetworkIdCounterBefore = getMap()->idManager.unitnetworkid;
122 
123  Vehicle* v = new Vehicle( vehicleType, getMap(), owner );
124  networkid= v->networkid;
125 
126  mapNetworkIdCounterAfter = getMap()->idManager.unitnetworkid;
127 
128  v->setMovement ( 0 );
129 
130 
131  if ( !carrier ) {
132  v->xpos = pos.x;
133  v->ypos = pos.y;
134  v->height = pos.getBitmappedHeight();
135  // RecalculateAreaView rav( getMap(), pos, vehicleType->view/10, &context );
137  if ( res.successful() )
138  res = (new ViewRegistration( v, ViewRegistration::AddView ))->execute( context );
139 
140  evaluateviewcalculation(getMap(), pos, vehicleType->view, 0, false, &context);
141  return res;
142  } else {
143  ContainerBase* hostingCarrier = getMap()->getContainer( carrierID );
144  if ( !hostingCarrier )
145  return ActionResult( 21303 );
146 
147  ActionResult res = (new UnitFieldRegistration(v, pos, UnitFieldRegistration::RegisterInCarrier, hostingCarrier ))->execute( context );
148  return res;
149  }
150 
151  return ActionResult(0);
152 }
153 
155 {
156  return getMap()->getUnit( networkid );
157 }
158 
159 
161 {
162  MapField* fld = getMap()->getField(pos);
163  if ( !fld )
164  return ActionResult( 21002, pos );
165 
166  if ( getMap()->idManager.unitnetworkid != mapNetworkIdCounterAfter )
167  return ActionResult( 21805 );
168 
169  getMap()->idManager.unitnetworkid = mapNetworkIdCounterBefore;
170 
171  const VehicleType* vehicleType = getMap()->getvehicletype_byid( vehicleTypeID );
172  if ( !vehicleType )
173  return ActionResult( 21801, "Vehicle id is " + ASCString::toString(vehicleTypeID));
174 
175  Vehicle* veh = getUnit();
176  if ( !veh )
177  return ActionResult( 21802 );
178 
179  if ( veh->typ->id != vehicleTypeID )
180  return ActionResult( 21803 );
181 
182  delete veh;
183  return ActionResult(0);
184 }
185 
187 {
188  if ( getMap()->idManager.unitnetworkid != mapNetworkIdCounterAfter )
189  return ActionResult( 21805 );
190 
191  return ActionResult(0);
192 }
193 
194 
195 namespace {
196  const bool r1 = registerAction<SpawnUnit> ( ActionRegistry::SpawnUnit );
197 }
198 
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 runAction(const Context &context)
Definition: spawnunit.cpp:111
ASCString toString(bool coordinates=false) const
Definition: typen.cpp:314
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: spawnunit.cpp:106
ASCString getDescription() const
Definition: spawnunit.cpp:57
void setMovement(int newmove, double cargoDivisor=-1)
sets a new distance that the unit can move
Definition: vehicle.cpp:527
int getOwner() const
returns the number of the player this vehicle/building belongs to
ContainerBase * getContainer(int nwid)
Definition: gamemap.cpp:1231
GameMap * getMap()
Definition: action.h:92
ASCString getName() const
The interface for all kinds of IO stream.
static const int spawnUnitStreamVersion
Definition: spawnunit.cpp:70
a single field of the map
Definition: mapfield.h:26
void write(tnstream &stream) const
Definition: typen.h:253
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
void read(tnstream &stream)
Definition: typen.h:254
int view
the visibility range
int height
the current level of height ( BITMAPPED ! )
Definition: vehicle.h:118
virtual ActionResult verify()
Definition: spawnunit.cpp:186
The class describing properties that are common to all vehicles of a certain kind.
Definition: vehicletype.h:177
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
Vehicle * getUnit()
Definition: spawnunit.cpp:154
virtual void writeData(tnstream &stream) const
Definition: spawnunit.cpp:92
ActionResult execute(const Context &context)
Definition: action.cpp:41
virtual void readData(tnstream &stream)
Definition: spawnunit.cpp:72
int evaluateviewcalculation(GameMap *gamemap, int player_fieldcount_mask, bool disableShareView, const Context *context)
evaluates the view on the whole map.
VehicleType * getvehicletype_byid(int id)
Definition: gamemap.cpp:1794
bool successful() const
Vehicle * getUnit(int x, int y, int nwid)
Definition: gamemap.cpp:1215
class GameMap::IDManager idManager
int xpos
the position on the map
Definition: vehicle.h:124
int getBitmappedHeight() const
Definition: typen.h:241
const VehicleType * typ
Definition: vehicle.h:83
Coordinate on the map including height.
Definition: typen.h:238
The parent class of Vehicle and Building; The name Container originates from Battle Isle...
Definition: containerbase.h:40
A GameAction is an modification of something on a map.
Definition: action.h:45
int ypos
Definition: vehicle.h:124
int networkid
a unique identification of the unit that is used everywhere in ASC (and not only the network protocol...
Definition: vehicle.h:140
virtual ActionResult undoAction(const Context &context)
Definition: spawnunit.cpp:160
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