Advanced Strategic Command
unitcommand.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.de
3  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 
22 #include "unitcommand.h"
23 
24 #include "../gamemap.h"
25 
27 {
28  return getMap()->getUnit( unitNetworkID );
29 }
30 
32 {
33  return getMap()->getUnit( unitNetworkID );
34 }
35 
37  : Command( vehicle->getMap() ),
38  unitNetworkID ( vehicle->networkid ),
39  startingPosition( vehicle->getPosition() ),
40  unitTypeID( vehicle->typ->id )
41 {
42 };
43 
45  : Command( map ), unitNetworkID ( -1 ), unitTypeID( -1 )
46 {
47 
48 }
49 
50 static const int unitCommandStreamVersion = 2;
51 
53 {
54  Command::readData( stream );
55  int version = stream.readInt();
56  if ( version < 1 || version > unitCommandStreamVersion )
57  throw tinvalidversion ( "UnitCommand", unitCommandStreamVersion, version );
58  unitNetworkID = stream.readInt();
59 
60  if ( version >= 2 ) {
61  unitTypeID = stream.readInt();
62  startingPosition.read( stream );
63  }
64 }
65 
66 
67 void UnitCommand::writeData ( tnstream& stream ) const
68 {
69  Command::writeData( stream );
71  stream.writeInt( unitNetworkID );
72  stream.writeInt( unitTypeID );
73  startingPosition.write( stream );
74 }
75 
76 vector<MapCoordinate> UnitCommand::getCoordinates() const
77 {
78  vector<MapCoordinate> v = Command::getCoordinates();
79  if ( startingPosition.valid() )
80  v.push_back( startingPosition );
81  return v;
82 }
83 
85 {
86  if ( unitTypeID == -1 ) {
87  const Vehicle* veh = getMap()->getUnit( unitNetworkID );
88  if ( veh )
89  return veh->typ->id;
90  }
91 
92  return unitTypeID;
93 };
94 
96 {
97  if ( getMap()->actplayer != getUnit()->getOwner() )
98  return ActionResult(101);
99  else
100  return ActionResult(0);
101 }
void writeData(tnstream &stream) const
Definition: unitcommand.cpp:67
virtual void writeInt(int i)
Writes a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is p...
Definition: basestrm.cpp:363
bool valid() const
Definition: typen.h:221
virtual int readInt(void)
Reads a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is pe...
Definition: basestrm.cpp:284
static const int unitCommandStreamVersion
Definition: unitcommand.cpp:50
void read(tnstream &stream)
Definition: typen.h:213
GameMap * getMap()
Definition: action.h:92
The interface for all kinds of IO stream.
A Command is an action that the player initiates.
Definition: command.h:114
UnitCommand(GameMap *map)
Definition: unitcommand.cpp:44
virtual vector< MapCoordinate > getCoordinates() const
this is for informational purposes, so the player can see where a Command has taken place when review...
Definition: unitcommand.cpp:76
ActionResult checkExecutionPrecondition() const
Definition: unitcommand.cpp:95
void write(tnstream &stream) const
Definition: typen.h:212
virtual void readData(tnstream &stream)
Definition: command.cpp:31
virtual void writeData(tnstream &stream) const
Definition: command.cpp:38
Vehicle * getUnit(int x, int y, int nwid)
Definition: gamemap.cpp:1215
const VehicleType * typ
Definition: vehicle.h:83
int getUnitTypeID() const
Definition: unitcommand.cpp:84
void readData(tnstream &stream)
Definition: unitcommand.cpp:52
virtual vector< MapCoordinate > getCoordinates() const
this is for informational purposes, so the player can see where a Command has taken place when review...
Definition: command.cpp:77
const Vehicle * getUnit() const
Definition: unitcommand.cpp:26
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182