Advanced Strategic Command
destructbuildingcommand.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 "../itemrepository.h"
32 #include "../containercontrols.h"
33 #include "changeunitproperty.h"
34 #include "spawnbuilding.h"
35 #include "changeunitmovement.h"
36 #include "consumeresource.h"
37 #include "destructcontainer.h"
38 
39 
40 
42 {
43  if ( !eht )
44  return false;
45 
46  if ( eht->attacked == false && !eht->hasMoved() )
47  if ( eht->getOwner() == eht->getMap()->actplayer )
50  return true;
51 
52  return false;
53 }
54 
55 
56 DestructBuildingCommand :: DestructBuildingCommand ( Vehicle* container )
57  : UnitCommand ( container )
58 {
59 
60 }
61 
62 Resources DestructBuildingCommand::getDestructionCost( const Building* bld) const
63 {
64  Resources r;
65  r.material = - bld->typ->productionCost.material * (100 - bld->damage) / destruct_building_material_get / 100;
67  return r;
68 }
69 
70 
71 vector<MapCoordinate> DestructBuildingCommand::getFields()
72 {
73  vector<MapCoordinate> fields;
74  Vehicle* veh = getUnit();
75  for ( int d = 0; d < 6; ++d ) {
77  MapField* fld = getMap()->getField(pos);
78  if ( fld )
79  if ( fld->building && getheightdelta( getFirstBit(veh->height), getFirstBit(fld->building->typ->height)) == 0 && !fld->building->typ->buildingNotRemovable )
80  fields.push_back( pos );
81  }
82 
83  return fields;
84 }
85 
87 {
88  vector<MapCoordinate> fields = getFields();
89  return find( fields.begin(), fields.end(), pos ) != fields.end() ;
90 }
91 
92 
94 {
95  this->target = pos;
96  MapField* fld = getMap()->getField(target);
97 
98  if ( !fld )
99  throw ActionResult(21002);
100 
101  setState( SetUp );
102 
103 }
104 
105 
106 
108 {
109  if ( getState() != SetUp )
110  return ActionResult(22000);
111 
112 
113  Building* building = getMap()->getField( target )->building;
114  if ( !building )
115  return ActionResult(22502);
116 
117  const BuildingType* buildingType = building->typ;
118 
119  Resources cost = getDestructionCost( building );
120 
121  auto_ptr<DestructContainer> dc ( new DestructContainer( building, true ));
122  ActionResult res = dc->execute( context );
123  if ( res.successful() )
124  dc.release();
125  else
126  return res;
127 
128  auto_ptr<ConsumeResource> cr ( new ConsumeResource( getUnit(), cost ));
129  res = cr->execute( context );
130  if ( res.successful() )
131  cr.release();
132  else
133  return res;
134 
135  auto_ptr<ChangeUnitMovement> cum ( new ChangeUnitMovement( getUnit(), 0 ));
136  res = cum->execute( context );
137  if ( res.successful() )
138  cum.release();
139  else
140  return res;
141 
142  auto_ptr<ChangeUnitProperty> cup ( new ChangeUnitProperty( getUnit(), ChangeUnitProperty::AttackedFlag, 1 ));
143  res = cup->execute( context );
144  if ( res.successful() )
145  cup.release();
146  else
147  return res;
148 
149  evaluateviewcalculation( getMap(), target, buildingType->view, 0, false, &context );
150 
151 
152  if ( context.display )
153  context.display->repaintDisplay();
154 
155  return ActionResult(0);
156 }
157 
158 
159 
160 static const int DestructBuildingCommandVersion = 1;
161 
163 {
164  UnitCommand::readData( stream );
165  int version = stream.readInt();
166  if ( version > DestructBuildingCommandVersion )
167  throw tinvalidversion ( "DestructBuildingCommand", DestructBuildingCommandVersion, version );
168  target.read( stream );
169 }
170 
172 {
173  UnitCommand::writeData( stream );
175  target.write( stream );
176 }
177 
178 
179 
181 {
182  ASCString c;
183  c.format("unitDestructBuilding ( map, %d, asc.MapCoordinate(%d, %d) )", getUnitID(), target.x, target.y );
184  return c;
185 }
186 
188 {
190 }
191 
193 {
194  ASCString s = "Destruct building at "+ target.toString() ;
195 
196 
197  if ( getUnit() ) {
198  s += " with " + getUnit()->getName();
199  }
200  return s;
201 }
202 
203 namespace
204 {
205 const bool r1 = registerAction<DestructBuildingCommand> ( ActionRegistry::DestructBuildingCommand );
206 }
207 
int fuel
Definition: typen.h:101
void setTargetPosition(const MapCoordinate &pos)
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
const BuildingType * typ
Definition: buildings.h:48
int GameActionID
Definition: action.h:35
int damage
Damage. 0 is no damage, when damage reaches 100 the container is destroyed.
int getUnitID() const
Definition: unitcommand.h:37
int getheightdelta(const ContainerBase *c1, const ContainerBase *c2)
calculate the height difference between two levels of height.
Definition: spfst.cpp:393
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
int getFirstBit(int zahl)
Count the number of zero bits on the LSB side of "zahl".
Definition: misc.cpp:45
bool hasFunction(ContainerFunctions function) const
vector< IntRange > buildingsBuildable
the ids of buildings this unit can construct
Definition: vehicletype.h:227
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
void read(tnstream &stream)
Definition: typen.h:213
MapCoordinate3D getPosition() const
returns the units position
Definition: vehicle.cpp:1552
GameMap * getMap()
Definition: action.h:92
ASCString toString(bool coordinates=false) const
Definition: typen.cpp:304
The interface for all kinds of IO stream.
Resources getTank() const
returns the resources that the unit is carrying
Definition: vehicle.cpp:378
a single field of the map
Definition: mapfield.h:26
ASCString & format(const charT *pFormat,...)
Definition: ascstring.cpp:78
bool isFieldUsable(const MapCoordinate &pos)
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
vector< MapCoordinate > getFields()
int view
the visibility range
int height
the current level of height ( BITMAPPED ! )
Definition: vehicle.h:118
The class describing properties that are common to all buildings of a certain kind.
Definition: buildingtype.h:35
void readData(tnstream &stream)
void write(tnstream &stream) const
Definition: typen.h:212
static const int DestructBuildingCommandVersion
Coordinate on the twodimensional map.
Definition: typen.h:202
ASCString getName() const
returns the units name or, if it does not exist, the unit type's name or description ...
Definition: vehicle.cpp:1569
int evaluateviewcalculation(GameMap *gamemap, int player_fieldcount_mask, bool disableShareView, const Context *context)
evaluates the view on the whole map.
const int destruct_building_material_get
when pulling down a building, this fraction of the buildings construction cost is reclaimed ...
Definition: typen.h:532
int material
Definition: typen.h:100
void setState(State state)
Definition: command.cpp:44
#define destruct_building_fuel_usage
Definition: typen.h:533
signed char actplayer
the player who is currently making his moves (may be human or AI)
Definition: gamemap.h:232
bool successful() const
MapDisplayInterface * display
Definition: context.h:39
static bool avail(const Vehicle *eht)
bool hasMoved(void) const
did the unit move this turn
Definition: vehicle.cpp:552
int fuelConsumption
the fuel consumption to move a single field
Definition: vehicletype.h:200
ActionResult go(const Context &context)
const VehicleType * typ
Definition: vehicle.h:83
bool attacked
did the unit already attack this turn
Definition: vehicle.h:109
Building * building
Definition: mapfield.h:102
void readData(tnstream &stream)
Definition: unitcommand.cpp:52
GameMap * getMap() const
An actual building on the map, which references a BuildingType Buildings have an owner,.
Definition: buildings.h:38
Resources are basically the currency of ASC.
Definition: typen.h:97
virtual void repaintDisplay()=0
const Vehicle * getUnit() const
Definition: unitcommand.cpp:26
MapCoordinate3D getNeighbouringFieldCoordinate(const MapCoordinate3D &pos, int direc)
returns the coordinate of the field that is adjecent to the given field in the direction of direc ...
MapField * getField(int x, int y)
Definition: gamemap.h:465