Advanced Strategic Command
cargomovecommand.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 "cargomovecommand.h"
23 
24 #include "../vehicle.h"
25 #include "../gamemap.h"
26 #include "../mapdisplayinterface.h"
27 #include "action-registry.h"
28 #include "../containercontrols.h"
29 #include "unitfieldregistration.h"
30 
31 
32 bool CargoMoveCommand :: moveOutAvail( const Vehicle* movingUnit )
33 {
34  if ( !movingUnit )
35  return false;
36 
37  ContainerBase* carr = movingUnit ->getCarrier();
38  if ( carr ) {
39  ContainerBase* carr2 = carr->getCarrier();
40  if ( carr2 )
41  return carr2->doesVehicleFit( movingUnit );
42  }
43  return false;
44 
45 }
46 
47 bool CargoMoveCommand :: moveInAvail( const Vehicle* movingUnit , Vehicle* newCarrier )
48 {
49  if ( movingUnit && newCarrier )
50  return newCarrier->doesVehicleFit( movingUnit );
51  else
52  return false;
53 }
54 
55 
57 {
58  vector<Vehicle*> targets;
59 
60  ContainerBase* container = getUnit()->getCarrier();
61  if ( !container )
62  return targets;
63 
64  for ( ContainerBase::Cargo::const_iterator i = container->getCargo().begin(); i != container->getCargo().end(); ++i )
65  if ( *i != getUnit() && *i )
66  if ( moveInAvail ( getUnit(), *i ))
67  targets.push_back( *i );
68 
69  return targets;
70 }
71 
72 
73 CargoMoveCommand :: CargoMoveCommand ( Vehicle* unit )
74  : UnitCommand ( unit ), mode( undefined ), targetCarrier(-1)
75 {
76 
77 }
78 
79 
80 
81 
82 
84 {
85  if ( getState() != SetUp )
86  return ActionResult(22000);
87 
88  if( mode == undefined )
89  return ActionResult(21900);
90 
91  if ( mode == moveOutwards ) {
92  if ( !moveOutAvail( getUnit() ) )
93  return ActionResult( 21901 );
94 
95  Vehicle* targetContainer1 = dynamic_cast<Vehicle*>(getUnit()->getCarrier());
96  if ( !targetContainer1 )
97  return ActionResult( 21902 );
98 
99  ContainerBase* targetContainer2 = targetContainer1->getCarrier();
100  if ( !targetContainer2 )
101  return ActionResult( 21902 );
102 
104  if ( !res.successful() )
105  return res;
106 
107  res = (new UnitFieldRegistration( getUnit(), getUnit()->getPosition(), UnitFieldRegistration::RegisterInCarrier, targetContainer2 ))->execute ( context );
108  return res;
109 
110  } else {
111  Vehicle* targetContainer = getMap()->getUnit( targetCarrier );
112  if ( !targetContainer )
113  return ActionResult( 21902 );
114 
115  if ( !moveInAvail( getUnit(), targetContainer ) )
116  return ActionResult( 21901 );
117 
119  if ( !res.successful() )
120  return res;
121 
122  res = (new UnitFieldRegistration( getUnit(), getUnit()->getPosition(), UnitFieldRegistration::RegisterInCarrier, targetContainer ))->execute ( context );
123  return res;
124  }
125 
126 }
127 
129 {
130  this->mode = mode;
131  if ( mode == moveOutwards || targetCarrier > 0 )
132  setState( SetUp );
133 };
134 
135 
136 static const int CargoMoveCommandVersion = 1;
137 
139 {
140  UnitCommand::readData( stream );
141  int version = stream.readInt();
142  if ( version > CargoMoveCommandVersion )
143  throw tinvalidversion ( "CargoMoveCommand", CargoMoveCommandVersion, version );
144  targetCarrier = stream.readInt();
145  mode = (Mode) stream.readInt();
146 }
147 
149 {
150  UnitCommand::writeData( stream );
152  stream.writeInt( targetCarrier );
153  stream.writeInt( mode );
154 }
155 
156 
158 {
159  if ( targetCarrier ) {
160  this->targetCarrier = targetCarrier->networkid;
161  if ( mode == moveInwards )
162  setState( SetUp );
163  } else
164  this->targetCarrier = -1;
165 }
166 
167 
169 {
170  if ( mode == moveOutwards ) {
171  ASCString c;
172  c.format("cargoUnitMove ( map, %d, -1 )", getUnitID() );
173  return c;
174  }
175  if ( mode == moveInwards ) {
176  ASCString c;
177  c.format("cargoUnitMove ( map, %d, %d )", getUnitID(), targetCarrier );
178  return c;
179  }
180  return "";
181 }
182 
184 {
186 }
187 
189 {
190  ASCString s = "Move ";
191  if ( getUnit() )
192  s += getUnit()->getName() ;
193  else
194  s += "unit nwid " + ASCString::toString(getUnitID() );
195 
196  if ( mode == moveOutwards )
197  s += " into outer carrier ";
198  else
199  s += " into inner carrier with ID " + ASCString::toString( targetCarrier );
200 
201  if ( getUnit() )
202  s += " at " + getUnit()->getPosition().toString();
203 
204  return s;
205 }
206 
207 namespace
208 {
209  const bool r1 = registerAction<CargoMoveCommand> ( ActionRegistry::CargoMoveCommand );
210 }
211 
void writeData(tnstream &stream) const
Definition: unitcommand.cpp:67
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
void readData(tnstream &stream)
int getUnitID() const
Definition: unitcommand.h:37
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
static bool moveOutAvail(const Vehicle *movingUnit)
static bool moveInAvail(const Vehicle *movingUnit, Vehicle *newCarrier)
State getState() const
Definition: command.h:125
static const int CargoMoveCommandVersion
MapCoordinate3D getPosition() const
returns the units position
Definition: vehicle.cpp:1552
GameMap * getMap()
Definition: action.h:92
The interface for all kinds of IO stream.
void writeData(tnstream &stream) const
ASCString & format(const charT *pFormat,...)
Definition: ascstring.cpp:78
void setMode(Mode mode)
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
ActionResult execute(const Context &context)
Definition: action.cpp:41
ASCString getDescription() const
ASCString getName() const
returns the units name or, if it does not exist, the unit type's name or description ...
Definition: vehicle.cpp:1569
void setState(State state)
Definition: command.cpp:44
bool successful() const
Vehicle * getUnit(int x, int y, int nwid)
Definition: gamemap.cpp:1215
vector< Vehicle * > getTargetCarriers()
bool doesVehicleFit(const Vehicle *vehicle) const
Does the vehicle fit into the container? This does not include checking if it can reach the entry...
ContainerBase * getCarrier() const
if this is a unit and it is inside a building or transport, returns the transport. NULL otherwise.
void readData(tnstream &stream)
Definition: unitcommand.cpp:52
ASCString getCommandString() const
The parent class of Vehicle and Building; The name Container originates from Battle Isle...
Definition: containerbase.h:40
void setTargetCarrier(Vehicle *targetCarrier)
const Cargo & getCargo() const
GameActionID getID() const
int networkid
a unique identification of the unit that is used everywhere in ASC (and not only the network protocol...
Definition: vehicle.h:140
const Vehicle * getUnit() const
Definition: unitcommand.cpp:26
ActionResult go(const Context &context)
virtual MapCoordinate3D getPosition() const =0