00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "changeobjectproperty.h"
00023 #include "removeobject.h"
00024 #include "action-registry.h"
00025
00026 #include "../gamemap.h"
00027
00028 ChangeObjectProperty::ChangeObjectProperty( GameMap* map, MapCoordinate& pos, Object* object, Property property, int value, bool valueIsAbsolute )
00029 : GameAction( map ), originalValue(-1), resultingValue(-1)
00030 {
00031 this->position = pos;
00032 this->value = value;
00033 this->property = property;
00034 this->objectID = object->typ->id;
00035 this->valueIsAbsolute = valueIsAbsolute;
00036 }
00037
00038 ASCString ChangeObjectProperty::getPropertyName( Property property )
00039 {
00040 switch ( property ) {
00041 case Damage: return "Damage";
00042 };
00043 return "";
00044 }
00045
00046
00047 ASCString ChangeObjectProperty::getDescription() const
00048 {
00049 ASCString res = "Change property " + getPropertyName(property) + " of object with id " + ASCString::toString(objectID)
00050 + " on field " + position.toString(false);
00051
00052 if ( !valueIsAbsolute )
00053 res += " by ";
00054 else
00055 res += " to ";
00056 res += ASCString::toString(value);
00057 return res;
00058 }
00059
00060
00061 void ChangeObjectProperty::readData ( tnstream& stream )
00062 {
00063 int version = stream.readInt();
00064 if ( version != 1 )
00065 throw tinvalidversion ( "ChangeObjectProperty", 1, version );
00066
00067 position.read( stream );
00068 objectID = stream.readInt();
00069 property = (Property) stream.readInt();
00070 value = stream.readInt();
00071 valueIsAbsolute = stream.readInt();
00072 originalValue = stream.readInt();
00073 resultingValue = stream.readInt();
00074
00075 };
00076
00077
00078 void ChangeObjectProperty::writeData ( tnstream& stream ) const
00079 {
00080 stream.writeInt( 1 );
00081 position.write( stream );
00082 stream.writeInt( objectID );
00083 stream.writeInt( property );
00084 stream.writeInt( value );
00085 stream.writeInt( valueIsAbsolute );
00086 stream.writeInt( originalValue );
00087 stream.writeInt( resultingValue );
00088 };
00089
00090
00091 GameActionID ChangeObjectProperty::getID() const
00092 {
00093 return ActionRegistry::ChangeObjectProperty;
00094 }
00095
00096 Object* ChangeObjectProperty::getObject()
00097 {
00098 MapField* fld = getMap()->getField( position );
00099 if ( !fld )
00100 throw ActionResult(21002);
00101
00102 ObjectType* ot = getMap()->getobjecttype_byid( objectID );
00103 if ( !ot )
00104 throw ActionResult( 21501 );
00105
00106 Object* o = fld->checkForObject( ot );
00107 if ( !o )
00108 throw ActionResult ( 21502 );
00109
00110 return o;
00111 }
00112
00113
00114 int ChangeObjectProperty::getProperty()
00115 {
00116 switch ( property ) {
00117 case Damage: return getObject()->damage;
00118 };
00119 throw ActionResult(21503 );
00120 }
00121
00122 ActionResult ChangeObjectProperty::setProperty( Property property, int value, const Context& context )
00123 {
00124 switch ( property ) {
00125 case Damage:
00126 getObject()->damage = value;
00127 break;
00128 default:
00129 throw ActionResult(21503 );
00130 };
00131 return ActionResult(0);
00132 }
00133
00134
00135 ActionResult ChangeObjectProperty::runAction( const Context& context )
00136 {
00137 originalValue = getProperty();
00138
00139 ActionResult res(0);
00140 if ( !valueIsAbsolute )
00141 res = setProperty( property, originalValue + value, context );
00142 else
00143 res = setProperty( property, value, context );
00144
00145 resultingValue = getProperty();
00146 return res;
00147 }
00148
00149
00150 ActionResult ChangeObjectProperty::undoAction( const Context& context )
00151 {
00152 return setProperty ( property, originalValue, context );
00153 }
00154
00155 ActionResult ChangeObjectProperty::preCheck()
00156 {
00157 if ( getProperty() != originalValue )
00158 throw ActionResult( 21204, getPropertyName( property ) );
00159
00160 return ActionResult(0);
00161 }
00162
00163 ActionResult ChangeObjectProperty::postCheck()
00164 {
00165 if ( getProperty() != resultingValue )
00166 throw ActionResult( 21204, getPropertyName( property ) );
00167
00168 return ActionResult(0);
00169 }
00170
00171
00172 namespace {
00173 const bool r1 = registerAction<ChangeObjectProperty> ( ActionRegistry::ChangeObjectProperty );
00174 }
00175