unitcommand.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 "unitcommand.h"
00023 
00024 #include "../gamemap.h"
00025 
00026 const Vehicle* UnitCommand::getUnit() const
00027 {
00028    return getMap()->getUnit( unitNetworkID );
00029 }
00030 
00031 Vehicle* UnitCommand::getUnit() 
00032 {
00033    return getMap()->getUnit( unitNetworkID );
00034 }
00035 
00036 UnitCommand::UnitCommand( Vehicle* vehicle )
00037    : Command( vehicle->getMap() ), 
00038      unitNetworkID ( vehicle->networkid ), 
00039      startingPosition( vehicle->getPosition() ),
00040      unitTypeID( vehicle->typ->id )
00041 {
00042 };
00043 
00044 UnitCommand::UnitCommand( GameMap* map )
00045    : Command( map ), unitNetworkID ( -1 ), unitTypeID( -1 )
00046 {
00047      
00048 }
00049 
00050 static const int unitCommandStreamVersion = 2;
00051 
00052 void UnitCommand::readData ( tnstream& stream )
00053 {
00054    Command::readData( stream );
00055    int version = stream.readInt();
00056    if ( version < 1 || version > unitCommandStreamVersion ) 
00057       throw tinvalidversion ( "UnitCommand", unitCommandStreamVersion, version );
00058    unitNetworkID = stream.readInt();   
00059    
00060    if ( version >= 2 ) {
00061       unitTypeID  = stream.readInt();
00062       startingPosition.read( stream );
00063    }
00064 }
00065 
00066 
00067 void UnitCommand::writeData ( tnstream& stream ) const
00068 {
00069    Command::writeData( stream );
00070    stream.writeInt( unitCommandStreamVersion );
00071    stream.writeInt( unitNetworkID );
00072    stream.writeInt( unitTypeID );
00073    startingPosition.write( stream );
00074 }
00075 
00076 vector<MapCoordinate> UnitCommand::getCoordinates() const
00077 {
00078    vector<MapCoordinate> v = Command::getCoordinates();
00079    if ( startingPosition.valid() )
00080       v.push_back( startingPosition );
00081    return v;
00082 }
00083 
00084 int UnitCommand::getUnitTypeID() const 
00085 { 
00086    if ( unitTypeID == -1 ) {
00087       const Vehicle* veh = getMap()->getUnit( unitNetworkID );
00088       if ( veh )
00089          return veh->typ->id;
00090    } 
00091    
00092    return unitTypeID; 
00093 }; 
00094 
00095 ActionResult UnitCommand::checkExecutionPrecondition() const
00096 {
00097    if ( getMap()->actplayer != getUnit()->getOwner() )
00098       return ActionResult(101);
00099    else
00100       return ActionResult(0);
00101 }

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