Advanced Strategic Command
powergenerationswitchcommand.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 
33 
34 bool PowerGenerationSwitchCommand :: avail ( const Vehicle* unit, bool newState )
35 {
36  if ( !unit )
37  return false;
38 
39  if ( unit->getOwner() == unit->getMap()->actplayer &&
41  if ( unit->getGeneratorStatus() != newState )
42  return true;
43 
44  return false;
45 }
46 
47 
48 
49 PowerGenerationSwitchCommand :: PowerGenerationSwitchCommand ( Vehicle* container )
50  : UnitCommand ( container )
51 {
52 }
53 
55 {
56  newState = enabled;
57  setState( SetUp );
58 }
59 
60 
61 
62 
64 {
65  if ( getState() != SetUp )
66  return ActionResult(22000);
67 
68  Vehicle* unit = getUnit();
69  if ( !avail( unit, newState ))
70  return ActionResult( 22300 );
71 
72  auto_ptr<ChangeUnitProperty> propChange ( new ChangeUnitProperty( getUnit(), ChangeUnitProperty::PowerGeneration, newState ));
73  ActionResult res = propChange->execute( context );
74 
75  if ( res.successful() )
76  propChange.release();
77 
78  return res;
79 }
80 
81 
82 
84 
86 {
87  UnitCommand::readData( stream );
88  int version = stream.readInt();
90  throw tinvalidversion ( "PowerGenerationSwitchCommand", PowerGenerationSwitchCommandVersion, version );
91  newState = stream.readInt();
92 }
93 
95 {
96  UnitCommand::writeData( stream );
98  stream.writeInt( newState );
99 }
100 
101 
103 {
104  ASCString c;
105  c.format("unitPowerGenerationEnable ( map, %d, %d )", getUnitID(), newState );
106  return c;
107 
108 }
109 
111 {
113 }
114 
116 {
117  ASCString s = newState ? "Enable " : "Disable ";
118  s += "power generation ";
119 
120  if ( getMap()->getUnit( getUnitID() ))
121  s += " for " + getMap()->getUnit( getUnitID() )->getName();
122 
123  return s;
124 }
125 
126 namespace
127 {
128  const bool r1 = registerAction<PowerGenerationSwitchCommand> ( ActionRegistry::PowerGenerationSwitchCommand );
129 }
130 
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
int getUnitID() const
Definition: unitcommand.h:37
bool getGeneratorStatus() const
Definition: vehicle.h:323
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
bool hasFunction(ContainerFunctions function) const
int getOwner() const
returns the number of the player this vehicle/building belongs to
void writeData(tnstream &stream) const
State getState() const
Definition: command.h:125
GameMap * getMap()
Definition: action.h:92
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
static const int PowerGenerationSwitchCommandVersion
ASCString getName() const
returns the units name or, if it does not exist, the unit type's name or description ...
Definition: vehicle.cpp:1569
ActionResult go(const Context &context)
void setState(State state)
Definition: command.cpp:44
signed char actplayer
the player who is currently making his moves (may be human or AI)
Definition: gamemap.h:232
bool successful() const
Vehicle * getUnit(int x, int y, int nwid)
Definition: gamemap.cpp:1215
const VehicleType * typ
Definition: vehicle.h:83
void readData(tnstream &stream)
Definition: unitcommand.cpp:52
GameMap * getMap() const
static bool avail(const Vehicle *unit, bool newState)
const Vehicle * getUnit() const
Definition: unitcommand.cpp:26