Advanced Strategic Command
repairunitcommand.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 "repairunitcommand.h"
23 
24 #include "../vehicle.h"
25 #include "../mapfield.h"
26 #include "../gamemap.h"
27 #include "../viewcalculation.h"
28 #include "../spfst.h"
29 #include "../mapdisplayinterface.h"
30 #include "action-registry.h"
31 #include "changeunitproperty.h"
33 #include "servicing.h"
34 #include "consumeresource.h"
35 
36 
38 {
40 }
41 
42 
43 bool RepairUnitCommand :: avail ( const ContainerBase* servicer )
44 {
45  if ( !servicer )
46  return false;
47 
48  if ( availInternally( servicer ))
49  return true;
50 
51  const Vehicle* v = dynamic_cast<const Vehicle*>(servicer);
52  if ( v )
53  return availExternally( v );
54  else
55  return false;
56 }
57 
58 
60 {
61  if ( !veh )
62  return false;
63 
64  if ( veh && !veh->attacked && veh->reactionfire.getStatus() == Vehicle::ReactionFire::off )
66  for ( int i = 0; i < veh->typ->weapons.count; i++ )
67  if ( veh->typ->weapons.weapon[i].service() )
68  return true;
69 
70  return false;
71 }
72 
73 
74 
75 RepairUnitCommand :: RepairUnitCommand ( ContainerBase* container )
76  : ContainerCommand ( container )
77 {
78 }
79 
80 
81 
83 {
85  sts.startSearch();
86  externalTargets.clear();
87 
88  for ( ServiceTargetSearcher::Targets::const_iterator i = sts.getTargets().begin(); i != sts.getTargets().end(); ++i )
89  if ( !(*i)->getCarrier() ) {
90  Vehicle* v = dynamic_cast<Vehicle*>(*i);
91  if ( v )
92  externalTargets.push_back( v );
93  }
94 
95  return externalTargets;
96 }
97 
99 {
101  sts.startSearch();
102 
103  internalTargets.clear();
104 
105  for ( ServiceTargetSearcher::Targets::const_iterator i = sts.getTargets().begin(); i != sts.getTargets().end(); ++i )
106  if ( (*i)->getCarrier() == getContainer() ) {
107  Vehicle* v = dynamic_cast<Vehicle*>(*i);
108  if ( v )
109  internalTargets.push_back( v );
110  }
111 
112  return internalTargets;
113 }
114 
115 
117 {
119  sts.startSearch();
120  for ( ServiceTargetSearcher::Targets::const_iterator i = sts.getTargets().begin(); i != sts.getTargets().end(); ++i )
121  if ( target == *i )
122  return true;
123 
124  return false;
125 }
126 
128 {
129  if ( !target )
130  return;
131 
132  if ( validTarget(target) ) {
133  targetNWid = target->networkid;
134  setState( SetUp );
135  }
136 }
137 
138 
139 
141 {
142  if ( getState() != SetUp )
143  return ActionResult(22000);
144 
145  ContainerBase* servicer = getContainer();
146  if ( !avail( servicer ))
147  return ActionResult( 22401 );
148 
149  Vehicle* target = getMap()->getUnit( targetNWid );
150  if ( !target )
151  return ActionResult( 22400 );
152 
153  if ( !validTarget(target))
154  return ActionResult( 22402 );
155 
156 
157  Resources cost;
158  int oldDamage = target->damage;
159  int newDamage = servicer->getMaxRepair ( target, 0, cost );
160 
161  auto_ptr<ChangeContainerProperty> propChange ( new ChangeContainerProperty( target, ChangeContainerProperty::Damage, newDamage ));
162  ActionResult res = propChange->execute( context );
163  if ( res.successful() )
164  propChange.release();
165  else
166  return res;
167 
168  int experience_o = target->getRepairExperienceValue( oldDamage, newDamage, true, Vehicle::experienceResolution );
169  int experience_d = target->getRepairExperienceValue( oldDamage, newDamage, false, Vehicle::experienceResolution );
170 
171  if ( experience_o != target->getExperience_offensive_raw() ) {
172  auto_ptr<ChangeUnitProperty> expChange ( new ChangeUnitProperty( target, ChangeUnitProperty::ExperienceOffensive_Raw, experience_o ));
173  ActionResult res = expChange->execute( context );
174  if ( res.successful() )
175  expChange.release();
176  else
177  return res;
178  }
179 
180  if ( experience_d != target->getExperience_defensive_raw() ) {
181  auto_ptr<ChangeUnitProperty> expChange ( new ChangeUnitProperty( target, ChangeUnitProperty::ExperienceDefensive_Raw, experience_d ));
182  ActionResult res = expChange->execute( context );
183  if ( res.successful() )
184  expChange.release();
185  else
186  return res;
187  }
188 
189 
190  auto_ptr<ConsumeResource> resource ( new ConsumeResource( getContainer(), cost ));
191  res = resource->execute( context );
192  if ( res.successful() )
193  resource.release();
194 
195 
196  return res;
197 }
198 
199 
200 
201 static const int RepairUnitCommandVersion = 1;
202 
204 {
205  ContainerCommand::readData( stream );
206  int version = stream.readInt();
207  if ( version > RepairUnitCommandVersion )
208  throw tinvalidversion ( "RepairUnitCommand", RepairUnitCommandVersion, version );
209  targetNWid = stream.readInt();
210 }
211 
213 {
214  ContainerCommand::writeData( stream );
216  stream.writeInt( targetNWid );
217 }
218 
219 
221 {
222  ASCString c;
223  c.format("repairUnit ( map, %d, %d )", getContainerID(), targetNWid );
224  return c;
225 
226 }
227 
229 {
231 }
232 
234 {
235  ASCString s = "Repair unit " + ASCString::toString( targetNWid );
236 
237  if ( getContainer( false ))
238  s += " with " + getContainer()->getName();
239 
240  return s;
241 }
242 
244 {
245  if ( getMap()->getCurrentPlayer().diplomacy.isAllied( getContainer() ))
246  return ActionResult(0);
247  else
248  return ActionResult(101);
249 }
250 
251 
252 namespace
253 {
254  const bool r1 = registerAction<RepairUnitCommand> ( ActionRegistry::RepairUnitCommand );
255 }
256 
int getExperience_offensive_raw() const
Definition: vehicle.cpp:263
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 service(void) const
int GameActionID
Definition: action.h:35
int getMaxRepair(const ContainerBase *item) const
returns the maximum amount of damage that the given item can be repaired
int damage
Damage. 0 is no damage, when damage reaches 100 the container is destroyed.
ASCString getCommandString() const
virtual ASCString getName() const =0
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 const int checkRepair
Definition: servicing.h:141
UnitWeapon weapons
The weapons.
Definition: vehicletype.h:248
void writeData(tnstream &stream) const
bool hasFunction(ContainerFunctions function) const
ASCString getDescription() const
int getExperience_defensive_raw() const
Definition: vehicle.cpp:268
void setTarget(Vehicle *target)
State getState() const
Definition: command.h:125
vector< Vehicle * > getExternalTargets()
GameMap * getMap()
Definition: action.h:92
SingleWeapon weapon[16]
Definition: vehicletype.h:170
vector< Vehicle * > getInternalTargets()
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
GameActionID getID() const
int getContainerID() const
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
class Vehicle::ReactionFire reactionfire
static const int RepairUnitCommandVersion
ActionResult checkExecutionPrecondition() const
int getRepairExperienceValue(int oldDamage, int newDamage, bool offensive, int rountToResolution)
Definition: vehicle.cpp:209
static bool avail(const ContainerBase *servicer)
void setState(State state)
Definition: command.cpp:44
bool successful() const
static bool availExternally(const Vehicle *servicer)
Vehicle * getUnit(int x, int y, int nwid)
Definition: gamemap.cpp:1215
void writeData(tnstream &stream) const
void readData(tnstream &stream)
ContainerBase * getContainer(bool dontThrow=false)
static const int experienceResolution
Definition: vehicle.h:96
const VehicleType * typ
Definition: vehicle.h:83
bool attacked
did the unit already attack this turn
Definition: vehicle.h:109
ActionResult go(const Context &context)
const ContainerBaseType * baseType
the type descriping all non-instance specific properties of the container
Definition: containerbase.h:80
void readData(tnstream &stream)
The parent class of Vehicle and Building; The name Container originates from Battle Isle...
Definition: containerbase.h:40
static bool availInternally(const ContainerBase *servicer)
const Targets & getTargets() const
Definition: servicing.h:145
Status getStatus() const
for each player that can still be attacked one bit is set
Definition: vehicle.h:155
Resources are basically the currency of ASC.
Definition: typen.h:97
bool validTarget(const Vehicle *target)
int networkid
a unique identification of the unit that is used everywhere in ASC (and not only the network protocol...
Definition: vehicle.h:140