unitfieldregistration.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 "unitfieldregistration.h"
00023 #include "action-registry.h"
00024 
00025 #include "../vehicle.h"
00026 #include "../gamemap.h"
00027 #include "../viewcalculation.h"
00028      
00029 UnitFieldRegistration::UnitFieldRegistration( Vehicle* vehicle, const MapCoordinate3D& pos, Operation operation, ContainerBase* carrier )
00030    : UnitAction( vehicle->getMap(), vehicle->networkid)
00031 {
00032    this->operation= operation;
00033    this->position = pos;
00034    if ( carrier )
00035       carrierID = carrier->getIdentification();
00036    else
00037       carrierID = 0; 
00038 }
00039       
00040       
00041 ASCString UnitFieldRegistration::getOpName() const 
00042 {
00043    switch ( operation ) {
00044       case RegisterOnField: return "RegisterOnField";
00045       case UnregisterOnField: return "UnregisterOnField";
00046       case AddView: return "AddView";
00047       case RemoveView: return "RemoveView"; 
00048       case Position: return "Position";
00049       case Position3D: return "Position3D";
00050       case RegisterInCarrier: return "CarrierRegistration";
00051       case UnRegisterFromCarrier: return "CarrierUnregistration";
00052    }
00053    return "";
00054 }
00055       
00056 ASCString UnitFieldRegistration::getDescription() const
00057 {
00058    ASCString res = getOpName();
00059    if ( getUnit(false) ) 
00060       res += " " + getUnit(false)->getName();
00061 
00062    if ( position.valid() )
00063       res += " " + position.toString();
00064    
00065    return  res;
00066 }
00067       
00068 static const int unitFieldRegistrationVersion = 2;
00069       
00070 void UnitFieldRegistration::readData ( tnstream& stream ) 
00071 {
00072    UnitAction::readData( stream );
00073    int version = stream.readInt();
00074    if ( version < 1 || version > unitFieldRegistrationVersion )
00075       throw tinvalidversion ( "UnitFieldRegistration", unitFieldRegistrationVersion, version );
00076    
00077    operation = (Operation) stream.readInt();
00078    position.read( stream );
00079    previousPosition.read( stream );
00080    stream.readInt();
00081    stream.readInt();
00082    
00083    if ( version >= 2 )
00084       carrierID = stream.readInt();
00085    else
00086       carrierID = 0;
00087 };
00088       
00089       
00090 void UnitFieldRegistration::writeData ( tnstream& stream ) const
00091 {
00092    UnitAction::writeData( stream );
00093    stream.writeInt( unitFieldRegistrationVersion );
00094    stream.writeInt( (int) operation );
00095    position.write( stream );
00096    previousPosition.write( stream );
00097    stream.writeInt( 0 ); // dummy
00098    stream.writeInt( 0 ); // dummy
00099    stream.writeInt( carrierID );
00100 };
00101 
00102 
00103 GameActionID UnitFieldRegistration::getID() const
00104 {
00105    return ActionRegistry::UnitFieldRegistration;
00106 }
00107 
00108 ActionResult UnitFieldRegistration::runAction( const Context& context )
00109 {
00110    Vehicle* veh = getUnit();
00111    
00112    MapField* fld = getMap()->getField( position );
00113    
00114    switch ( operation ) {
00115       case RegisterOnField: 
00116          if ( fld->getContainer() )
00117             fld->getContainer()->addToCargo( veh );
00118          else 
00119             fld->vehicle = veh;
00120          break;
00121          
00122       case RegisterInCarrier:
00123       {
00124          ContainerBase* carrier = getMap()->getContainer( carrierID );
00125          if ( !carrier )
00126             throw ActionResult( 21303 );
00127          carrier->addToCargo( veh );
00128          break;
00129       }
00130       
00131       case UnRegisterFromCarrier:
00132       {
00133          ContainerBase* carrier = getMap()->getContainer( carrierID );
00134          if ( !carrier )
00135             throw ActionResult( 21303 );
00136          if ( !carrier->removeUnitFromCargo( veh ))
00137             throw ActionResult( 21302, veh );
00138          break;
00139       }
00140          
00141       case UnregisterOnField:
00142          if ( fld->getContainer() && fld->getContainer() != veh ) {
00143             if ( !fld->getContainer()->removeUnitFromCargo( veh ))
00144                throw ActionResult( 21302, veh );
00145          } else {
00146             if ( fld->vehicle != veh )
00147                throw ActionResult( 21301, veh );
00148             fld->vehicle = NULL;
00149          }
00150          break;
00151             
00152       case AddView:
00153          veh->addview();
00154          break;
00155          
00156       case RemoveView:
00157          veh->removeview();
00158          break;
00159       
00160       case Position3D:
00161          previousPosition = veh->getPosition();
00162          veh->xpos = position.x;
00163          veh->ypos = position.y;
00164          veh->height = position.getBitmappedHeight();
00165          break;
00166          
00167       case Position:
00168          previousPosition = veh->getPosition();
00169          veh->xpos = position.x;
00170          veh->ypos = position.y;
00171          break;
00172    }
00173    
00174    if ( operation == Position3D || operation == Position ) {
00175       for ( Vehicle::Cargo::const_iterator i = veh->getCargo().begin(); i != veh->getCargo().end(); ++i )
00176          if ( *i ) {
00177             auto_ptr<UnitFieldRegistration> ufr4 ( new UnitFieldRegistration( *i, position, Position ));
00178             ActionResult res = ufr4->execute( context );
00179             if ( !res.successful() )
00180                return res;
00181             else
00182                ufr4.release();
00183          }
00184    }
00185    
00186    return ActionResult(0);
00187 }
00188 
00189 
00190 ActionResult UnitFieldRegistration::undoAction( const Context& context )
00191 {
00192    Vehicle* veh = getUnit();
00193    
00194    MapField* fld = getMap()->getField( position );
00195    
00196    switch ( operation ) {
00197       case UnregisterOnField: 
00198          if ( fld->getContainer() )
00199             fld->getContainer()->addToCargo( veh );
00200          else 
00201             fld->vehicle = veh;
00202          break;
00203          
00204       case RegisterInCarrier:
00205       {
00206          ContainerBase* carrier = getMap()->getContainer( carrierID );
00207          if ( !carrier )
00208             throw ActionResult( 21303 );
00209          
00210          if ( !carrier->removeUnitFromCargo( veh ))
00211             throw ActionResult( 21302, veh );
00212          
00213          break;
00214       }
00215       
00216       case UnRegisterFromCarrier:
00217       {
00218          ContainerBase* carrier = getMap()->getContainer( carrierID );
00219          if ( !carrier )
00220             throw ActionResult( 21303 );
00221          
00222          carrier->addToCargo( veh );
00223          break;
00224       }
00225          
00226       case RegisterOnField:
00227          if ( fld->getContainer() && fld->getContainer() != veh ) {
00228             if ( !fld->getContainer()->removeUnitFromCargo( veh ))
00229                throw ActionResult( 21302, veh );
00230          } else {
00231             if ( fld->vehicle != veh )
00232                throw ActionResult( 21301, veh );
00233             fld->vehicle = NULL;
00234          }
00235          break;
00236             
00237       case RemoveView:
00238          veh->addview();
00239          break;
00240          
00241       case AddView:
00242          veh->removeview();
00243          break;
00244          
00245       case Position3D:
00246          veh->xpos = previousPosition.x;
00247          veh->ypos = previousPosition.y;
00248          veh->height = previousPosition.getBitmappedHeight();
00249          break;
00250          
00251       case Position:
00252          veh->xpos = previousPosition.x;
00253          veh->ypos = previousPosition.y;
00254          break;
00255          
00256    }
00257    
00258    return ActionResult(0);
00259 }
00260 
00261 ActionResult UnitFieldRegistration::preCheck()
00262 {
00263    if ( operation == Position || operation == Position3D ) {
00264       if ( getUnit()->xpos != previousPosition.x || getUnit()->ypos != previousPosition.y )
00265          return ActionResult( 21206, getUnit() ); 
00266    }
00267    return ActionResult(0);
00268 }
00269 
00270 ActionResult UnitFieldRegistration::postCheck()
00271 {
00272    return ActionResult(0);
00273 }
00274 
00275 
00276 
00277 namespace {
00278    const bool r1 = registerAction<UnitFieldRegistration> ( ActionRegistry::UnitFieldRegistration );
00279 }
00280 

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