Advanced Strategic Command
repairbuildingcommand.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 "repairbuildingcommand.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 "consumeresource.h"
34 
35 
36 
37 bool RepairBuildingCommand :: avail ( const Building* building )
38 {
39  if ( !building )
40  return false;
41 
42  if ( !building->damage )
43  return false;
44  else
45  return building->repairableDamage() > 0;
46 
47 }
48 
49 
50 RepairBuildingCommand :: RepairBuildingCommand ( Building* building )
51  : ContainerCommand ( building )
52 {
53  if ( avail( building ))
54  setState( SetUp );
55 }
56 
58 {
59  RepairData data;
60  data.newDamage = getContainer()->getMaxRepair ( getContainer(), 0, data.cost );
61  data.damageDelta = getContainer()->damage - data.newDamage;
62  return data;
63 }
64 
65 
66 
67 
69 {
70  if ( getState() != SetUp )
71  return ActionResult(22000);
72 
73  ContainerBase* servicer = getContainer();
74  Building* building = dynamic_cast<Building*>(servicer);
75  if ( !building )
76  return ActionResult( 22700 );
77 
78  if ( !avail( building ))
79  return ActionResult( 22700 );
80 
81 
82  RepairData data = getCost();
83 
84  auto_ptr<ChangeContainerProperty> propChange ( new ChangeContainerProperty( building, ChangeContainerProperty::Damage, data.newDamage ));
85  ActionResult res = propChange->execute( context );
86  if ( res.successful() )
87  propChange.release();
88  else
89  return res;
90 
91  auto_ptr<ChangeContainerProperty> propChange2 ( new ChangeContainerProperty( building, ChangeContainerProperty::RepairedThisTurn, data.damageDelta, false ));
92  res = propChange2->execute( context );
93  if ( res.successful() )
94  propChange2.release();
95  else
96  return res;
97 
98 
99  auto_ptr<ConsumeResource> resource ( new ConsumeResource( getContainer(), data.cost ));
100  res = resource->execute( context );
101  if ( res.successful() )
102  resource.release();
103 
104  return res;
105 }
106 
107 
108 
109 static const int RepairBuildingCommandVersion = 1;
110 
112 {
113  ContainerCommand::readData( stream );
114  int version = stream.readInt();
115  if ( version > RepairBuildingCommandVersion )
116  throw tinvalidversion ( "RepairBuildingCommand", RepairBuildingCommandVersion, version );
117 }
118 
120 {
121  ContainerCommand::writeData( stream );
123 }
124 
125 
127 {
128  ASCString c;
129  c.format("repairBuilding ( map, %d )", getContainerID() );
130  return c;
131 
132 }
133 
135 {
137 }
138 
140 {
141  ASCString s = "Repair building ";
142  if ( getContainer(true))
143  s += getContainer( true )->getName() + " at ";
145 
146  return s;
147 }
148 
149 namespace
150 {
151  const bool r1 = registerAction<RepairBuildingCommand> ( ActionRegistry::RepairBuildingCommand );
152 }
153 
static bool avail(const Building *building)
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 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 getDescription() const
void writeData(tnstream &stream) 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
GameActionID getID() const
State getState() const
Definition: command.h:125
The interface for all kinds of IO stream.
ASCString & format(const charT *pFormat,...)
Definition: ascstring.cpp:78
static const int RepairBuildingCommandVersion
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
int getContainerID() const
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
void readData(tnstream &stream)
virtual int repairableDamage() const
returns the amount of damage that can still be repaired this turn
Definition: buildings.cpp:78
void setState(State state)
Definition: command.cpp:44
bool successful() const
void writeData(tnstream &stream) const
ContainerBase * getContainer(bool dontThrow=false)
ActionResult go(const Context &context)
ASCString getCommandString() const
void readData(tnstream &stream)
The parent class of Vehicle and Building; The name Container originates from Battle Isle...
Definition: containerbase.h:40
An actual building on the map, which references a BuildingType Buildings have an owner,.
Definition: buildings.h:38