File: destructbuildingcommand.cpp

package info (click to toggle)
asc 2.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 75,080 kB
  • ctags: 24,943
  • sloc: cpp: 155,023; sh: 8,829; ansic: 6,890; makefile: 650; perl: 138
file content (207 lines) | stat: -rw-r--r-- 5,787 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
     This file is part of Advanced Strategic Command; http://www.asc-hq.de
     Copyright (C) 1994-2008  Martin Bickel  and  Marc Schellenberger

     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program; see the file COPYING. If not, write to the
     Free Software Foundation, Inc., 59 Temple Place, Suite 330,
     Boston, MA  02111-1307  USA
*/


#include "destructbuildingcommand.h"

#include "../vehicle.h"
#include "../mapfield.h"
#include "../gamemap.h"
#include "../viewcalculation.h"
#include "../spfst.h"
#include "../mapdisplayinterface.h"
#include "action-registry.h"
#include "../itemrepository.h"
#include "../containercontrols.h"
#include "changeunitproperty.h"
#include "spawnbuilding.h"
#include "changeunitmovement.h"
#include "consumeresource.h"
#include "destructcontainer.h"



bool DestructBuildingCommand :: avail ( const Vehicle* eht )
{
   if ( !eht )
      return false;

   if ( eht->attacked == false && !eht->hasMoved() )
      if ( eht->getOwner() == eht->getMap()->actplayer )
            if ( eht->typ->hasFunction( ContainerBaseType::ConstructBuildings  ) || !eht->typ->buildingsBuildable.empty() )
               if ( eht->getTank().fuel >= destruct_building_fuel_usage * eht->typ->fuelConsumption )
                  return true;

   return false;
}


DestructBuildingCommand :: DestructBuildingCommand ( Vehicle* container )
      : UnitCommand ( container )
{

}

Resources DestructBuildingCommand::getDestructionCost( const Building* bld) const 
{
   Resources r;
   r.material = - bld->typ->productionCost.material * (100 - bld->damage) / destruct_building_material_get / 100;
   r.fuel = destruct_building_fuel_usage * getUnit()->typ->fuelConsumption;
   return r;
}


vector<MapCoordinate> DestructBuildingCommand::getFields()
{
   vector<MapCoordinate> fields;
   Vehicle* veh = getUnit();
   for ( int d = 0; d < 6; ++d ) {
      MapCoordinate pos = getNeighbouringFieldCoordinate( veh->getPosition(), d );
      tfield* fld = getMap()->getField(pos);
      if ( fld )
         if ( fld->building && getheightdelta( log2(veh->height), log2(fld->building->typ->height)) == 0 && !fld->building->typ->buildingNotRemovable ) 
            fields.push_back( pos );
   }

   return fields;
}

bool DestructBuildingCommand :: isFieldUsable( const MapCoordinate& pos )
{
   vector<MapCoordinate> fields = getFields();
   return find( fields.begin(), fields.end(), pos ) != fields.end() ;
}


void DestructBuildingCommand :: setTargetPosition( const MapCoordinate& pos )
{
   this->target = pos;
   tfield* fld = getMap()->getField(target);

   if ( !fld )
      throw ActionResult(21002);

   setState( SetUp );

}



ActionResult DestructBuildingCommand::go ( const Context& context )
{
   if ( getState() != SetUp )
      return ActionResult(22000);
   
   
   Building* building = getMap()->getField( target )->building;
   if ( !building )
      return ActionResult(22502);
   
   const BuildingType* buildingType = building->typ;
   
   Resources cost = getDestructionCost( building );
   
   auto_ptr<DestructContainer> dc ( new DestructContainer( building, true ));
   ActionResult res = dc->execute( context );
   if ( res.successful() )
      dc.release();
   else
      return res;
   
   auto_ptr<ConsumeResource> cr ( new ConsumeResource( getUnit(), cost ));
   res = cr->execute( context );
   if ( res.successful() )
      cr.release();
   else
      return res;
   
   auto_ptr<ChangeUnitMovement> cum ( new ChangeUnitMovement( getUnit(), 0 ));
   res = cum->execute( context );
   if ( res.successful() )
      cum.release();
   else
      return res;

   auto_ptr<ChangeUnitProperty> cup ( new ChangeUnitProperty( getUnit(), ChangeUnitProperty::AttackedFlag, 1 ));
   res = cup->execute( context );
   if ( res.successful() )
      cup.release();
   else
      return res;
   
   evaluateviewcalculation( getMap(), target, buildingType->view, 0, false, &context );

   
   if ( context.display )
      context.display->repaintDisplay();

   return ActionResult(0);
}



static const int DestructBuildingCommandVersion = 1;

void DestructBuildingCommand :: readData ( tnstream& stream )
{
   UnitCommand::readData( stream );
   int version = stream.readInt();
   if ( version > DestructBuildingCommandVersion )
      throw tinvalidversion ( "DestructBuildingCommand", DestructBuildingCommandVersion, version );
   target.read( stream );
}

void DestructBuildingCommand :: writeData ( tnstream& stream ) const
{
   UnitCommand::writeData( stream );
   stream.writeInt( DestructBuildingCommandVersion );
   target.write( stream );
}



ASCString DestructBuildingCommand :: getCommandString() const
{
   ASCString c;
   c.format("unitDestructBuilding ( map, %d, asc.MapCoordinate(%d, %d) )", getUnitID(), target.x, target.y );
   return c;
}

GameActionID DestructBuildingCommand::getID() const
{
   return ActionRegistry::DestructBuildingCommand;
}

ASCString DestructBuildingCommand::getDescription() const
{
   ASCString s = "Destruct building at "+ target.toString() ;


   if ( getUnit() ) {
      s += " with " + getUnit()->getName();
   }
   return s;
}

namespace
{
const bool r1 = registerAction<DestructBuildingCommand> ( ActionRegistry::DestructBuildingCommand );
}