00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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 }