Advanced Strategic Command
destructunitcommand.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 "destructunitcommand.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 "../itemrepository.h"
32 #include "../containercontrols.h"
33 #include "consumeresource.h"
34 #include "servicecommand.h"
35 #include "destructcontainer.h"
36 #include "../soundList.h"
37 #include "viewregistration.h"
38 
39 
41 {
42  if ( !unit )
43  return false;
44 
45  if ( unit->getMap()->getField( unit->getPosition() )->getContainer() == unit )
47 
48  return false;
49 }
50 
51 
52 
53 DestructUnitCommand :: DestructUnitCommand ( ContainerBase* container )
54  : ContainerCommand ( container )
55 {
56  if ( avail( container ))
57  setState( SetUp );
58 }
59 
60 
61 
62 
63 
65 {
66  if ( getState() != SetUp )
67  return ActionResult(22000);
68 
69  ContainerBase* container = getContainer();
70  if ( !avail( container ))
71  return ActionResult( 22101 );
72 
73  if ( context.display ) {
74  Vehicle* v = dynamic_cast<Vehicle*>(container);
75  if ( v )
77  else
79  }
80 
81  MapCoordinate pos = container->getPosition();
82  int viewdist = container->baseType->view;
83 
84 
85  auto_ptr<DestructContainer> destructor ( new DestructContainer( container ));
86  ActionResult res = destructor->execute( context );
87  if ( res.successful() )
88  destructor.release();
89 
90  evaluateviewcalculation( getMap(), pos, viewdist, 0, false, &context );
91 
92  if ( context.display )
93  context.display->repaintDisplay();
94 
95  return res;
96 }
97 
98 
99 
100 static const int DestructUnitCommandVersion = 1;
101 
103 {
104  ContainerCommand::readData( stream );
105  int version = stream.readInt();
106  if ( version > DestructUnitCommandVersion )
107  throw tinvalidversion ( "DestructUnitCommand", DestructUnitCommandVersion, version );
108 }
109 
111 {
112  ContainerCommand::writeData( stream );
114 }
115 
116 
118 {
119  ASCString c;
120  c.format("selfDestruct ( map, %d )", getContainerID() );
121  return c;
122 }
123 
125 {
127 }
128 
130 {
131  ASCString s = "Destruct ";
132 
133  if ( getContainer(true) ) {
134  s += " by " + getContainer()->getName();
135  } else
137 
138  return s;
139 }
140 
141 namespace
142 {
143  const bool r1 = registerAction<DestructUnitCommand> ( ActionRegistry::DestructUnitCommand );
144 }
145 
ASCString getDescription() const
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
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
bool hasFunction(ContainerFunctions function) const
static SoundList & getInstance()
Definition: soundList.cpp:60
static bool avail(const ContainerBase *unit)
State getState() const
Definition: command.h:125
static const int DestructUnitCommandVersion
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
int view
the visibility range
GameActionID getID() const
int getContainerID() const
ActionResult go(const Context &context)
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
Coordinate on the twodimensional map.
Definition: typen.h:202
int evaluateviewcalculation(GameMap *gamemap, int player_fieldcount_mask, bool disableShareView, const Context *context)
evaluates the view on the whole map.
void writeData(tnstream &stream) const
ASCString killSoundLabel
this label can select a special sound to be played when this unit is killed
Definition: vehicletype.h:277
void setState(State state)
Definition: command.cpp:44
bool successful() const
MapDisplayInterface * display
Definition: context.h:39
Sound * playSound(Sample snd, int subType=0, bool looping=false, const ASCString &label="")
Definition: soundList.cpp:265
void writeData(tnstream &stream) const
ContainerBase * getContainer(bool dontThrow=false)
const VehicleType * typ
Definition: vehicle.h:83
const ContainerBaseType * baseType
the type descriping all non-instance specific properties of the container
Definition: containerbase.h:80
GameMap * getMap() const
void readData(tnstream &stream)
The parent class of Vehicle and Building; The name Container originates from Battle Isle...
Definition: containerbase.h:40
void readData(tnstream &stream)
virtual void repaintDisplay()=0
ASCString getCommandString() const
MapField * getField(int x, int y)
Definition: gamemap.h:465
virtual MapCoordinate3D getPosition() const =0