inflictdamage.cpp

Go to the documentation of this file.
00001 /*
00002      This file is part of Advanced Strategic Command; http://www.asc-hq.de
00003      Copyright (C) 1994-2010  Martin Bickel  and  Marc Schellenberger
00004  
00005      This program is free software; you can redistribute it and/or modify
00006      it under the terms of the GNU General Public License as published by
00007      the Free Software Foundation; either version 2 of the License, or
00008      (at your option) any later version.
00009  
00010      This program is distributed in the hope that it will be useful,
00011      but WITHOUT ANY WARRANTY; without even the implied warranty of
00012      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013      GNU General Public License for more details.
00014  
00015      You should have received a copy of the GNU General Public License
00016      along with this program; see the file COPYING. If not, write to the 
00017      Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
00018      Boston, MA  02111-1307  USA
00019 */
00020 
00021 
00022 #include "inflictdamage.h"
00023 #include "action-registry.h"
00024 
00025 #include "../containerbase.h"
00026 #include "../gamemap.h"
00027 
00028 #include "destructcontainer.h"
00029      
00030 InflictDamage::InflictDamage( ContainerBase* container, int damage )
00031    : ContainerAction( container )
00032 {
00033    this->damage = damage;
00034    this->originalDamage = -1;
00035    this->resultingDamage = -1;
00036 }
00037       
00038       
00039 ASCString InflictDamage::getDescription() const
00040 {
00041    ASCString res = "Inflict " + ASCString::toString(damage) + " damage";
00042    return  res;
00043 }
00044       
00045       
00046 void InflictDamage::readData ( tnstream& stream ) 
00047 {
00048    int version = stream.readInt();
00049    if ( version != 1 )
00050       throw tinvalidversion ( "InflictUnitDamage", 1, version );
00051    
00052    ContainerAction::readData( stream );
00053    
00054    damage = stream.readInt();
00055    originalDamage = stream.readInt();
00056    resultingDamage = stream.readInt();
00057 };
00058       
00059       
00060 void InflictDamage::writeData ( tnstream& stream ) const
00061 {
00062    stream.writeInt( 1 );
00063    
00064    ContainerAction::writeData( stream );
00065    
00066    stream.writeInt( damage );
00067    stream.writeInt( originalDamage );
00068    stream.writeInt( resultingDamage );
00069 };
00070 
00071 
00072 GameActionID InflictDamage::getID() const
00073 {
00074    return ActionRegistry::InflictDamage;
00075 }
00076 
00077 ActionResult InflictDamage::runAction( const Context& context )
00078 {
00079    ContainerBase* c = getContainer();
00080    
00081    originalDamage = c->damage;
00082    c->damage += damage;
00083    if ( c->damage >= 100 ) 
00084       c->damage = 100;
00085    
00086    resultingDamage = c->damage;
00087    if ( c->damage >= 100 ) {
00088       GameAction* a = new DestructContainer( c );
00089       ActionResult r = a->execute( context );
00090       if ( !r.successful() )
00091          return r;
00092    }
00093    return ActionResult(0);
00094 }
00095 
00096 
00097 ActionResult InflictDamage::undoAction( const Context& context )
00098 {
00099    ContainerBase* c = getContainer();
00100    
00101    if ( c->damage < damage )
00102       return ActionResult( 21201, c);
00103    
00104    c->damage -= damage;
00105    return ActionResult(0);
00106 }
00107 
00108 ActionResult InflictDamage::postCheck()
00109 {
00110    ContainerBase* c = getContainer();
00111    
00112    if ( c->damage != resultingDamage )
00113       return ActionResult( 21201, c );  
00114    
00115    return ActionResult(0);
00116 }
00117 
00118 
00119 
00120 namespace {
00121    const bool r1 = registerAction<InflictDamage> ( ActionRegistry::InflictDamage );
00122 }

Generated on Mon May 21 01:26:34 2012 for Advanced Strategic Command by  doxygen 1.5.1