vehicleattack.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 "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    if ( !getUnit()->typ->hasFunction( ContainerBaseType::MoveAfterAttack )) {
00111       GameAction* a = new ChangeUnitMovement( getMap(), getUnit()->networkid, 0 );
00112       a->execute( context );
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      // computeview( getMap(), 0, false, &context );
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 }

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