Advanced Strategic Command
unitaction.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 "unitaction.h"
23 
24 #include "../vehicle.h"
25 #include "../gamemap.h"
26 
28  : GameAction( unit->getMap() )
29 {
30  vehicleID = unit->networkid;
31 }
32 
33 
35  : GameAction( gamemap )
36 {
37  vehicleID = -1;
38 }
39 
40 UnitAction::UnitAction( GameMap* gamemap, int vehicleID )
41  : GameAction( gamemap )
42 {
43  this->vehicleID = vehicleID;
44 }
45 
46 
47 Vehicle* UnitAction::getUnit( bool dontThrow )
48 {
49  Vehicle* veh = getMap()->getUnit( vehicleID );
50  if ( !veh && !dontThrow )
51  throw ActionResult(21001, "ID is " + ASCString::toString(vehicleID) );
52  else
53  return veh;
54 }
55 
56 const Vehicle* UnitAction::getUnit( bool dontThrow ) const
57 {
58  const Vehicle* veh = getMap()->getUnit( vehicleID );
59  if ( !veh && !dontThrow )
60  throw ActionResult(21001, "ID is " + ASCString::toString(vehicleID) );
61  else
62  return veh;
63 }
64 
65 void UnitAction::readData ( tnstream& stream )
66 {
67  int version = stream.readInt();
68  if ( version != 1 )
69  throw tinvalidversion ( "UnitAction", 1, version );
70 
71  vehicleID = stream.readInt();
72 };
73 
74 
75 void UnitAction::writeData ( tnstream& stream ) const
76 {
77  stream.writeInt( 1 );
78  stream.writeInt( vehicleID );
79 };
80 
81 
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
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
void writeData(tnstream &stream) const
Definition: unitaction.cpp:75
GameMap * getMap()
Definition: action.h:92
The interface for all kinds of IO stream.
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
Vehicle * getUnit(bool dontThrow=false)
Definition: unitaction.cpp:47
Vehicle * getUnit(int x, int y, int nwid)
Definition: gamemap.cpp:1215
A GameAction is an modification of something on a map.
Definition: action.h:45
UnitAction(GameMap *gamemap, int vehicleID)
Definition: unitaction.cpp:40
int networkid
a unique identification of the unit that is used everywhere in ASC (and not only the network protocol...
Definition: vehicle.h:140
void readData(tnstream &stream)
Definition: unitaction.cpp:65
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182