changecontainerproperty.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 "changecontainerproperty.h"
00023 #include "action-registry.h"
00024 
00025 ChangeContainerProperty::ChangeContainerProperty( ContainerBase* container, Property property, int value, bool valueIsAbsolute )
00026    : ContainerAction( container ), originalValue(-1), resultingValue(-1)
00027 {
00028    this->property = property;
00029    this->value = value;
00030    this->valueIsAbsolute = valueIsAbsolute;
00031 }
00032       
00033 ASCString ChangeContainerProperty::getPropertyName( Property property )
00034 {
00035    switch ( property ) {
00036       case RepairedThisTurn: return "RepairedThisTurn";
00037       case Damage: return "Damage";
00038    };
00039    return "";
00040 }
00041       
00042       
00043 ASCString ChangeContainerProperty::getDescription() const
00044 {
00045    ASCString res = "Change property " + getPropertyName(property);
00046    if ( getContainer(false) ) 
00047       res += " of container " + getContainer(false)->getName();
00048    
00049    if ( !valueIsAbsolute )
00050       res += " by ";
00051    else 
00052       res += " to ";
00053    res += ASCString::toString(value);
00054    return  res;
00055 }
00056       
00057       
00058 void ChangeContainerProperty::readData ( tnstream& stream ) 
00059 {
00060    ContainerAction::readData( stream );
00061    int version = stream.readInt();
00062    if ( version != 1 )
00063       throw tinvalidversion ( "ChangeContainerProperty", 1, version );
00064    
00065    property = (Property) stream.readInt();
00066    value = stream.readInt();
00067    valueIsAbsolute = stream.readInt();
00068    originalValue = stream.readInt();
00069    resultingValue = stream.readInt();
00070    
00071 };
00072       
00073       
00074 void ChangeContainerProperty::writeData ( tnstream& stream ) const
00075 {
00076    ContainerAction::writeData( stream );
00077    stream.writeInt( 1 );
00078    stream.writeInt( property );
00079    stream.writeInt( value );
00080    stream.writeInt( valueIsAbsolute );
00081    stream.writeInt( originalValue );
00082    stream.writeInt( resultingValue );
00083 };
00084 
00085 
00086 GameActionID ChangeContainerProperty::getID() const
00087 {
00088    return ActionRegistry::ChangeContainerProperty;
00089 }
00090 
00091 int ChangeContainerProperty::getUnitProperty()
00092 {
00093    
00094    switch ( property ) {
00095       case Damage: return getContainer()->damage;
00096       case RepairedThisTurn: return getContainer()->repairedThisTurn;
00097       default:
00098          throw ActionResult(21203, getContainer() );
00099    };
00100 }
00101 
00102 void ChangeContainerProperty::setUnitProperty( int value, const Context& context )
00103 {
00104    switch ( property ) {
00105       case Damage:
00106          getContainer()->damage  = value;
00107          break;
00108       case RepairedThisTurn:
00109          getContainer()->repairedThisTurn = value;
00110          break;
00111       default:
00112          throw ActionResult(21203, getContainer() );
00113    };
00114 }
00115 
00116 
00117 ActionResult ChangeContainerProperty::runAction( const Context& context )
00118 {
00119    originalValue = getUnitProperty();
00120    
00121    if ( !valueIsAbsolute )
00122       setUnitProperty( originalValue + value, context );
00123    else
00124       setUnitProperty( value, context );
00125    
00126    resultingValue = getUnitProperty();
00127    return ActionResult(0);
00128 }
00129 
00130 
00131 ActionResult ChangeContainerProperty::undoAction( const Context& context )
00132 {
00133    setUnitProperty ( originalValue, context );
00134    return ActionResult(0);
00135 }
00136 
00137 ActionResult ChangeContainerProperty::preCheck()
00138 {
00139    if ( getUnitProperty() != originalValue )
00140       throw ActionResult( 21204, getContainer(), getPropertyName( property ) );
00141    
00142    return ActionResult(0);
00143 }
00144 
00145 ActionResult ChangeContainerProperty::postCheck()
00146 {
00147    if ( getUnitProperty() != resultingValue )
00148       throw ActionResult( 21204, getContainer(), getPropertyName( property ) );
00149    
00150    return ActionResult(0);
00151 }
00152 
00153 
00154 namespace {
00155    const bool r1 = registerAction<ChangeContainerProperty> ( ActionRegistry::ChangeContainerProperty );
00156 }

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