Advanced Strategic Command
containercommand.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 "containercommand.h"
23 
24 #include "../vehicle.h"
25 #include "../gamemap.h"
26 
28  : Command( container->getMap() )
29 {
30  this->containerID = container->getIdentification();
31 }
32 
34  : Command( map )
35 {
36  this->containerID = -1;
37 }
38 
39 
40 
42 {
43  ContainerBase* veh = getMap()->getContainer( containerID );
44  if ( !veh && !dontThrow )
45  throw ActionResult(21001, "ID is " + ASCString::toString(containerID) );
46  else
47  return veh;
48 }
49 
50 const ContainerBase* ContainerCommand::getContainer( bool dontThrow ) const
51 {
52  const ContainerBase* veh = getMap()->getContainer( containerID );
53  if ( !veh && !dontThrow )
54  throw ActionResult(21001, "ID is " + ASCString::toString(containerID) );
55  else
56  return veh;
57 }
58 
60 {
61  Command::readData( stream );
62  int version = stream.readInt();
63  if ( version != 1 )
64  throw tinvalidversion ( "containerID", 1, version );
65 
66  containerID = stream.readInt();
67 };
68 
69 
70 void ContainerCommand::writeData ( tnstream& stream ) const
71 {
72  Command::writeData( stream );
73  stream.writeInt( 1 );
74  stream.writeInt( containerID );
75 };
76 
78 {
79  if ( getMap()->actplayer != getContainer()->getOwner() )
80  return ActionResult(101);
81  else
82  return ActionResult(0);
83 }
84 
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
ActionResult checkExecutionPrecondition() const
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
ContainerBase * getContainer(int nwid)
Definition: gamemap.cpp:1231
virtual int getIdentification() const =0
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
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
virtual void readData(tnstream &stream)
Definition: command.cpp:31
virtual void writeData(tnstream &stream) const
Definition: command.cpp:38
void writeData(tnstream &stream) const
ContainerBase * getContainer(bool dontThrow=false)
void readData(tnstream &stream)
The parent class of Vehicle and Building; The name Container originates from Battle Isle...
Definition: containerbase.h:40
ContainerCommand(ContainerBase *container)
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182