Advanced Strategic Command
internalammotransfercommand.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 
23 
24 #include "../vehicle.h"
25 #include "../mapfield.h"
26 #include "../gamemap.h"
27 #include "../viewcalculation.h"
28 #include "../spfst.h"
29 #include "changeunitmovement.h"
30 #include "../mapdisplayinterface.h"
31 #include "action-registry.h"
32 #include "../reactionfire.h"
33 #include "../soundList.h"
34 #include "consumeammo.h"
35 #include "servicecommand.h"
36 
38 {
39  if ( unit ) {
40  for( int i=0; i<unit->typ->weapons.count; i++ )
41  {
42  const SingleWeapon* weapon = unit->getWeapon( i );
43  if( weapon->canRefuel() )
44  return true;
45 
46  for( int j=0; j<i; j++ )
47  {
48  const SingleWeapon* otherWeapon = unit->getWeapon( j );
49  if( weapon->equals( otherWeapon ) )
50  return true;
51  }
52  }
53  return false;
54  }
55 
56  return false;
57 }
58 
59 
60 InternalAmmoTransferCommand :: InternalAmmoTransferCommand ( Vehicle* unit)
61  : UnitCommand ( unit )
62 {
63 
64 }
65 
66 
67 
68 bool InternalAmmoTransferCommand::checkConsistency()
69 {
70  map<int,int> amount;
71 
72  Vehicle* v = getUnit();
73  for ( int w = 0; w < v->typ->weapons.count; ++w ) {
74  if ( ammoAmount.size() < w )
75  return false;
76 
77  int type = v->typ->weapons.weapon[w].getScalarWeaponType();
78  amount[type] += v->ammo[w] - ammoAmount[w];
79  }
80 
81  for ( map<int,int>::iterator i = amount.begin(); i != amount.end(); ++i )
82  if ( i->second != 0 )
83  return false;
84 
85  return true;
86 
87 }
88 
89 
90 bool InternalAmmoTransferCommand::setAmmounts( const vector<int>& ammo )
91 {
92  ammoAmount = ammo;
93 
94  return checkConsistency();
95 }
96 
97 
99 {
100  if ( getState() != SetUp )
101  return ActionResult(22000);
102 
103  Vehicle* unit = getUnit();
104 
105  if ( !avail( unit ))
106  return ActionResult( 22600 );
107 
108  if ( !checkConsistency())
109  return ActionResult( 23300 );
110 
111  for ( int w = 0; w < unit->typ->weapons.count; ++w ) {
112  if ( unit->ammo[w] != ammoAmount[w] ) {
113  auto_ptr<ConsumeAmmo> cr ( new ConsumeAmmo( unit, unit->typ->weapons.weapon[w].getScalarWeaponType(), w, unit->ammo[w] - ammoAmount[w] ));
114  ActionResult res = cr->execute( context );
115  if ( !res.successful() )
116  return res;
117  else
118  cr.release();
119  }
120  }
121 
122  setState( Finished );
123 
124  return ActionResult(0);
125 }
126 
127 
128 
130 
132 {
133  UnitCommand::readData( stream );
134  int version = stream.readInt();
135  if ( version > InternalAmmoTransferCommandVersion )
136  throw tinvalidversion ( "InternalAmmoTransferCommand", InternalAmmoTransferCommandVersion, version );
137 
138  readClassContainer( ammoAmount, stream );
139 }
140 
142 {
143  UnitCommand::writeData( stream );
144  stream.writeInt( InternalAmmoTransferCommandVersion );
145  writeClassContainer( ammoAmount, stream );
146 }
147 
148 
150 {
151  ASCString c;
152  c.format("InternalAmmoTransfer ( %d )", getUnitID() );
153  return c;
154 
155 }
156 
158 {
160 }
161 
163 {
164  ASCString s = "InternalAmmoTransfer ";
165 
166  if ( getUnit())
167  s += " for unit " + getUnit()->getName();
168 
169  return s;
170 }
171 
172 namespace
173 {
174  const bool r1 = registerAction<InternalAmmoTransferCommand> ( ActionRegistry::InternalAmmoTransferCommand );
175 }
176 
void writeData(tnstream &stream) const
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
the command is totally done
Definition: command.h:120
int getUnitID() const
Definition: unitcommand.h:37
int ammo[16]
Definition: vehicle.h:87
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
UnitWeapon weapons
The weapons.
Definition: vehicletype.h:248
bool canRefuel(void) const
State getState() const
Definition: command.h:125
SingleWeapon weapon[16]
Definition: vehicletype.h:170
The interface for all kinds of IO stream.
ASCString & format(const charT *pFormat,...)
Definition: ascstring.cpp:78
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
int getScalarWeaponType(void) const
static const int InternalAmmoTransferCommandVersion
ActionResult go(const Context &context)
static bool avail(const Vehicle *unit)
ASCString getName() const
returns the units name or, if it does not exist, the unit type's name or description ...
Definition: vehicle.cpp:1569
bool setAmmounts(const vector< int > &ammo)
A single weapon of a #Vehicletype.
Definition: vehicletype.h:100
void setState(State state)
Definition: command.cpp:44
bool successful() const
void readClassContainer(C &c, tnstream &stream)
Definition: basestrm.h:752
bool equals(const SingleWeapon *otherWeapon) const
const VehicleType * typ
Definition: vehicle.h:83
void readData(tnstream &stream)
Definition: unitcommand.cpp:52
void writeClassContainer(const C &c, tnstream &stream)
Definition: basestrm.h:742
const SingleWeapon * getWeapon(unsigned weaponNum) const
Returns the SingleWeapon corresponding to the weaponNum for this vehicle.
Definition: vehicle.cpp:1760
const Vehicle * getUnit() const
Definition: unitcommand.cpp:26