Advanced Strategic Command
reactionfireswitchcommand.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 "../mapdisplayinterface.h"
30 #include "action-registry.h"
31 #include "changeunitproperty.h"
32 #include "changeunitmovement.h"
33 
34 
35 bool ReactionFireSwitchCommand :: avail ( const Vehicle* unit, bool newState )
36 {
37  if ( !unit )
38  return false;
39 
40  if ( newState == true ) {
41  if ( unit->getMap()->getField( unit->getPosition() )->getContainer() == unit )
44  if ( unit->weapexist() )
45  for ( int i = 0; i < unit->typ->weapons.count; ++i )
46  if ( unit->typ->weapons.weapon[i].offensive() && unit->typ->weapons.weapon[i].reactionFireShots )
47  return true;
48  } else {
50  return true;
51  }
52 
53  return false;
54 }
55 
56 
57 
58 ReactionFireSwitchCommand :: ReactionFireSwitchCommand ( Vehicle* container )
59  : UnitCommand ( container )
60 {
61 }
62 
64 {
65  newRFstate = enabled;
66  setState( SetUp );
67 }
68 
69 
70 
71 
73 {
74  if ( getState() != SetUp )
75  return ActionResult(22000);
76 
77  Vehicle* unit = getUnit();
78  if ( !avail( unit, newRFstate ))
79  return ActionResult( 22300 );
80 
81 
82  if ( newRFstate == true ) {
83 
84  auto_ptr<ChangeUnitProperty> propChange ( new ChangeUnitProperty( getUnit(), ChangeUnitProperty::ReactionFire, (int) Vehicle::ReactionFire::init2 ));
85  ActionResult res = propChange->execute( context );
86 
87  if ( res.successful() ) {
88  propChange.release();
89  if ( context.display )
90  context.display->repaintDisplay();
91  }
92 
93  return res;
94  } else {
98 
99  auto_ptr<ChangeUnitMovement> propChange ( new ChangeUnitMovement( getUnit(), 0 ));
100  ActionResult res = propChange->execute( context );
101 
102  if ( res.successful() )
103  propChange.release();
104 
105  }
106 
107  auto_ptr<ChangeUnitProperty> propChange ( new ChangeUnitProperty( getUnit(), ChangeUnitProperty::ReactionFire, (int) Vehicle::ReactionFire::off ));
108  ActionResult res = propChange->execute( context );
109 
110  if ( res.successful() ) {
111  propChange.release();
112  if ( context.display )
113  context.display->repaintDisplay();
114  }
115 
116  return res;
117  }
118 }
119 
120 
121 
122 static const int ReactionFireSwitchCommandVersion = 1;
123 
125 {
126  UnitCommand::readData( stream );
127  int version = stream.readInt();
128  if ( version > ReactionFireSwitchCommandVersion )
129  throw tinvalidversion ( "ReactionFireSwitchCommand", ReactionFireSwitchCommandVersion, version );
130  newRFstate = stream.readInt();
131 }
132 
134 {
135  UnitCommand::writeData( stream );
137  stream.writeInt( newRFstate );
138 }
139 
140 
142 {
143  ASCString c;
144  c.format("unitReactionFireEnable ( map, %d, %d )", getUnitID(), newRFstate );
145  return c;
146 
147 }
148 
150 {
152 }
153 
155 {
156  ASCString s = newRFstate ? "Enable " : "Disable ";
157  s += "reaction fire ";
158 
159  if ( getMap()->getUnit( getUnitID() ))
160  s += " for " + getMap()->getUnit( getUnitID() )->getName();
161 
162  return s;
163 }
164 
165 namespace
166 {
167  const bool r1 = registerAction<ReactionFireSwitchCommand> ( ActionRegistry::ReactionFireSwitchCommand );
168 }
169 
bool weapexist() const
Is the unit able to shoot ?
Definition: vehicle.cpp:796
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
static bool avail(const Vehicle *unit, bool newState)
int getUnitID() const
Definition: unitcommand.h:37
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 hasFunction(ContainerFunctions function) const
State getState() const
Definition: command.h:125
MapCoordinate3D getPosition() const
returns the units position
Definition: vehicle.cpp:1552
GameMap * getMap()
Definition: action.h:92
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
class Vehicle::ReactionFire reactionfire
int reactionFireShots
Definition: vehicletype.h:145
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
static const int ReactionFireSwitchCommandVersion
bool successful() const
Vehicle * getUnit(int x, int y, int nwid)
Definition: gamemap.cpp:1215
MapDisplayInterface * display
Definition: context.h:39
void writeData(tnstream &stream) const
const VehicleType * typ
Definition: vehicle.h:83
bool offensive(void) const
void readData(tnstream &stream)
Definition: unitcommand.cpp:52
const ContainerBaseType * baseType
the type descriping all non-instance specific properties of the container
Definition: containerbase.h:80
ActionResult go(const Context &context)
GameMap * getMap() const
Status getStatus() const
for each player that can still be attacked one bit is set
Definition: vehicle.h:155
virtual void repaintDisplay()=0
const Vehicle * getUnit() const
Definition: unitcommand.cpp:26
MapField * getField(int x, int y)
Definition: gamemap.h:465