changeunitproperty.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 "changeunitproperty.h"
00023 #include "action-registry.h"
00024 
00025 #include "../vehicle.h"
00026 #include "../gamemap.h"
00027      
00028 ChangeUnitProperty::ChangeUnitProperty( Vehicle* vehicle, Property property, int value, bool valueIsAbsolute )
00029    : UnitAction( vehicle->getMap(), vehicle->networkid ), originalValue(-1), resultingValue(-1)
00030 {
00031    this->property = property;
00032    this->value = value;
00033    this->valueIsAbsolute = valueIsAbsolute;
00034 }
00035       
00036 ASCString ChangeUnitProperty::getPropertyName( Property property )
00037 {
00038    switch ( property ) {
00039       case Experience: return "Experience";
00040       case Movement: return "Movement";
00041       case AttackedFlag: return "AttackedFlag";
00042       case Height: return "Height";
00043       case Direction: return "Direction";
00044       case ReactionFire: return "ReactionFire";
00045       case PowerGeneration: return "PowerGeneration";
00046    };
00047    return "";
00048 }
00049       
00050       
00051 ASCString ChangeUnitProperty::getDescription() const
00052 {
00053    ASCString res = "Change property " + getPropertyName(property);
00054    if ( getUnit(false) ) 
00055       res += " of unit " + getUnit(false)->getName();
00056    
00057    if ( !valueIsAbsolute )
00058       res += " by ";
00059    else 
00060       res += " to ";
00061    res += ASCString::toString(value);
00062    return  res;
00063 }
00064       
00065       
00066 void ChangeUnitProperty::readData ( tnstream& stream ) 
00067 {
00068    UnitAction::readData( stream );
00069    int version = stream.readInt();
00070    if ( version != 1 )
00071       throw tinvalidversion ( "ChangeUnitProperty", 1, version );
00072    
00073    property = (Property) stream.readInt();
00074    value = stream.readInt();
00075    valueIsAbsolute = stream.readInt();
00076    originalValue = stream.readInt();
00077    resultingValue = stream.readInt();
00078    
00079 };
00080       
00081       
00082 void ChangeUnitProperty::writeData ( tnstream& stream )  const
00083 {
00084    UnitAction::writeData( stream );
00085    stream.writeInt( 1 );
00086    stream.writeInt( property );
00087    stream.writeInt( value );
00088    stream.writeInt( valueIsAbsolute );
00089    stream.writeInt( originalValue );
00090    stream.writeInt( resultingValue );
00091 };
00092 
00093 
00094 GameActionID ChangeUnitProperty::getID() const
00095 {
00096    return ActionRegistry::ChangeUnitProperty;
00097 }
00098 
00099 int ChangeUnitProperty::getUnitProperty()
00100 {
00101    switch ( property ) {
00102       case Experience: return getUnit()->experience;
00103       case Movement:   return getUnit()->getMovement( false, false );
00104       case AttackedFlag: return getUnit()->attacked;
00105       case Height: return getUnit()->height;
00106       case Direction: return getUnit()->direction;
00107       case ReactionFire: return getUnit()->reactionfire.status;
00108       case PowerGeneration: return getUnit()->getGeneratorStatus();
00109    };
00110    throw ActionResult(21203, getUnit() );
00111 }
00112 
00113 void ChangeUnitProperty::setUnitProperty( Property property, int value, const Context& context )
00114 {
00115    switch ( property ) {
00116       case Experience: 
00117          getUnit()->experience = value;
00118          break;
00119       case Movement:
00120          getUnit()->setMovement( value, 0 );
00121          break;
00122       case AttackedFlag: 
00123          getUnit()->attacked = (value != 0) ;
00124          break;
00125       case Height:
00126          getUnit()->height = value;
00127          break;
00128       case Direction:
00129          getUnit()->direction = value;
00130          break;
00131       case ReactionFire:
00132          getUnit()->reactionfire.status = (Vehicle::ReactionFire::Status) value;
00133          break;
00134       case PowerGeneration:
00135          getUnit()->setGeneratorStatus( value );
00136          break;
00137       default:
00138          throw ActionResult(21203, getUnit() );
00139    };
00140 }
00141 
00142 
00143 ActionResult ChangeUnitProperty::runAction( const Context& context )
00144 {
00145    originalValue = getUnitProperty();
00146    
00147    if ( !valueIsAbsolute )
00148       setUnitProperty( property, originalValue + value, context );
00149    else
00150       setUnitProperty( property, value, context );
00151    
00152    resultingValue = getUnitProperty();
00153    return ActionResult(0);
00154 }
00155 
00156 
00157 ActionResult ChangeUnitProperty::undoAction( const Context& context )
00158 {
00159    setUnitProperty ( property, originalValue, context );
00160    
00161    return ActionResult(0);
00162 }
00163 
00164 ActionResult ChangeUnitProperty::preCheck()
00165 {
00166    if ( getUnitProperty() != originalValue )
00167       throw ActionResult( 21204, getUnit(), getPropertyName( property ) );
00168    
00169    return ActionResult(0);
00170 }
00171 
00172 ActionResult ChangeUnitProperty::postCheck()
00173 {
00174    if ( getUnitProperty() != resultingValue )
00175       throw ActionResult( 21204, getUnit(), getPropertyName( property ) );
00176    
00177    return ActionResult(0);
00178 }
00179 
00180 
00181 namespace {
00182    const bool r1 = registerAction<ChangeUnitProperty> ( ActionRegistry::ChangeUnitProperty );
00183 }
00184 

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