Advanced Strategic Command
inflictdamage.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 "inflictdamage.h"
23 #include "action-registry.h"
24 
25 #include "../containerbase.h"
26 #include "../gamemap.h"
27 
28 #include "destructcontainer.h"
29 
30 InflictDamage::InflictDamage( ContainerBase* container, int damage )
31  : ContainerAction( container )
32 {
33  this->damage = damage;
34  this->originalDamage = -1;
35  this->resultingDamage = -1;
36 }
37 
38 
40 {
41  ASCString res = "Inflict " + ASCString::toString(damage) + " damage";
42  return res;
43 }
44 
45 
47 {
48  int version = stream.readInt();
49  if ( version != 1 )
50  throw tinvalidversion ( "InflictUnitDamage", 1, version );
51 
52  ContainerAction::readData( stream );
53 
54  damage = stream.readInt();
55  originalDamage = stream.readInt();
56  resultingDamage = stream.readInt();
57 };
58 
59 
60 void InflictDamage::writeData ( tnstream& stream ) const
61 {
62  stream.writeInt( 1 );
63 
65 
66  stream.writeInt( damage );
67  stream.writeInt( originalDamage );
68  stream.writeInt( resultingDamage );
69 };
70 
71 
73 {
75 }
76 
78 {
80 
81  originalDamage = c->damage;
82  c->damage += damage;
83  if ( c->damage >= 100 )
84  c->damage = 100;
85 
86  resultingDamage = c->damage;
87  if ( c->damage >= 100 ) {
88  GameAction* a = new DestructContainer( c );
89  ActionResult r = a->execute( context );
90  if ( !r.successful() )
91  return r;
92  }
93  return ActionResult(0);
94 }
95 
96 
98 {
100 
101  if ( c->damage < damage )
102  return ActionResult( 21201, c);
103 
104  c->damage -= damage;
105  return ActionResult(0);
106 }
107 
109 {
111 
112  if ( c->damage != resultingDamage )
113  return ActionResult( 21201, c );
114 
115  return ActionResult(0);
116 }
117 
118 
119 
120 namespace {
121  const bool r1 = registerAction<InflictDamage> ( ActionRegistry::InflictDamage );
122 }
void writeData(tnstream &stream) 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
int damage
Damage. 0 is no damage, when damage reaches 100 the container is destroyed.
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
The interface for all kinds of IO stream.
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
ASCString getDescription() const
ActionResult execute(const Context &context)
Definition: action.cpp:41
virtual ActionResult undoAction(const Context &context)
virtual GameActionID getID() const
bool successful() const
ContainerBase * getContainer(bool dontThrow=false)
The parent class of Vehicle and Building; The name Container originates from Battle Isle...
Definition: containerbase.h:40
virtual ActionResult postCheck()
postCheck is called after an action has been redone and should check that the state of the map is exa...
A GameAction is an modification of something on a map.
Definition: action.h:45
virtual void readData(tnstream &stream)
void readData(tnstream &stream)
virtual ActionResult runAction(const Context &context)
virtual void writeData(tnstream &stream) const