00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "vehicleattack.h"
00023 #include "action-registry.h"
00024
00025 #include "../vehicle.h"
00026 #include "../gamemap.h"
00027 #include "../attack.h"
00028 #include "../spfst.h"
00029 #include "../viewcalculation.h"
00030
00031 #include "changeunitmovement.h"
00032 #include "inflictdamage.h"
00033
00034 #include "../mapdisplayinterface.h"
00035
00036
00037 VehicleAttackAction::VehicleAttackAction( GameMap* gamemap, int vehicleID, const MapCoordinate& target, int weapon )
00038 : UnitAction( gamemap, vehicleID )
00039 {
00040 this->target = target;
00041 this->weapon = weapon;
00042 }
00043
00044
00045 ASCString VehicleAttackAction::getDescription() const
00046 {
00047 ASCString res = "Attack at " + target.toString();
00048 if ( getUnit(false) )
00049 res += " with unit " + getUnit(false)->getName();
00050 return res;
00051 }
00052
00053
00054 void VehicleAttackAction::readData ( tnstream& stream )
00055 {
00056 UnitAction::readData( stream );
00057 int version = stream.readInt();
00058 if ( version != 1 )
00059 throw tinvalidversion ( "VehicleAttack", 1, version );
00060
00061 weapon = stream.readInt();
00062 target.read( stream );
00063 };
00064
00065
00066 void VehicleAttackAction::writeData ( tnstream& stream ) const
00067 {
00068 UnitAction::writeData( stream );
00069 stream.writeInt( 1 );
00070 stream.writeInt( weapon );
00071 target.write( stream );
00072 };
00073
00074
00075 GameActionID VehicleAttackAction::getID() const
00076 {
00077 return ActionRegistry::VehicleAttack;
00078 }
00079
00080 ActionResult VehicleAttackAction::runAction( const Context& context )
00081 {
00082 auto_ptr<tfight> battle;
00083
00084 MapField* fld = getMap()->getField( target );
00085
00086 Vehicle* attacker = getUnit();
00087 Vehicle* attackee = fld->vehicle;
00088
00089 if ( fld->vehicle ) {
00090 battle.reset( new tunitattacksunit ( attacker, attackee, true, weapon ) );
00091 } else {
00092 if ( fld->building )
00093 battle.reset( new tunitattacksbuilding ( getUnit(), target.x, target.y , weapon ) );
00094 else
00095 battle.reset( new tunitattacksobject ( getUnit(), target.x, target.y, weapon ) );
00096 }
00097
00098 bool shown;
00099 if ( context.display && fieldvisiblenow ( getMap()->getField (target), context.viewingPlayer ) ) {
00100 context.display->displayActionCursor ( getUnit()->getPosition(), target );
00101 context.display->showBattle( *battle );
00102 context.display->removeActionCursor ( );
00103 shown = true;
00104 } else {
00105 battle->calc();
00106 shown = false;
00107 }
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 bool somethingDestroyed = (battle->dv.damage >= 100 || battle->av.damage >= 100);
00118
00119 RecalculateAreaView rav ( getMap(), target, maxViewRange / maxmalq + 1, &context );
00120 if ( somethingDestroyed )
00121 rav.removeView();
00122
00123 battle->setresult ( context );
00124
00125 if ( somethingDestroyed )
00126
00127 rav.addView();
00128
00129
00130 if ( context.display && shown )
00131 context.display->displayMap();
00132
00133 return ActionResult(0);
00134 }
00135
00136
00137 ActionResult VehicleAttackAction::undoAction( const Context& context )
00138 {
00139 evaluateviewcalculation( getMap() );
00140 if ( context.display )
00141 context.display->displayMap();
00142 return ActionResult(0);
00143 }
00144
00145
00146
00147 namespace {
00148 const bool r1 = registerAction<VehicleAttackAction> ( ActionRegistry::VehicleAttack );
00149 }