Advanced Strategic Command
convertcontainer.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 "convertcontainer.h"
23 #include "destructcontainer.h"
24 #include "action-registry.h"
25 
26 #include "../vehicle.h"
27 
28 ConvertContainer::ConvertContainer( ContainerBase* container, int newPlayer )
29  : ContainerAction( container ), newOwner(newPlayer), previousOwner(-1)
30 {
31 }
32 
34 {
35  ASCString res = "Change owner of " + getContainer()->getName();
36  res += " to " + ASCString::toString(newOwner);
37  return res;
38 }
39 
40 
42 {
43  ContainerAction::readData( stream );
44  int version = stream.readInt();
45  if ( version != 1 )
46  throw tinvalidversion ( "ConvertContainer", 1, version );
47 
48  newOwner = stream.readInt();
49  previousOwner = stream.readInt();
50 };
51 
52 
53 void ConvertContainer::writeData ( tnstream& stream ) const
54 {
56  stream.writeInt( 1 );
57  stream.writeInt( newOwner );
58  stream.writeInt( previousOwner );
59 };
60 
61 
63 {
65 }
66 
67 
69 {
71  previousOwner = c->getOwner();
72 
74  (new DestructContainer(c))->execute(context);
75  return ActionResult(0);
76  }
77 
78  c->registerForNewOwner( newOwner );
79 
80  for ( ContainerBase::Cargo::iterator i = c->cargo.begin(); i != c->cargo.end(); ++i )
81  if ( *i )
82  (new ConvertContainer( *i, newOwner ))->execute(context);
83 
84 
85  c->conquered();
87 
88  return ActionResult(0);
89 }
90 
91 
93 {
94  getContainer()->registerForNewOwner( previousOwner );
95  return ActionResult(0);
96 }
97 
99 {
100  if ( getContainer()->getOwner() != previousOwner )
101  throw ActionResult( 21204, getContainer(), "owner" );
102 
103  return ActionResult(0);
104 }
105 
107 {
108  if ( getContainer()->getOwner() != newOwner )
109  throw ActionResult( 21204, getContainer(), "owner" );
110 
111  return ActionResult(0);
112 }
113 
114 
115 namespace {
116  const bool r1 = registerAction<ConvertContainer> ( ActionRegistry::ConvertContainer );
117 }
void writeData(tnstream &stream) const
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
int GameActionID
Definition: action.h:35
virtual ASCString getName() const =0
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
bool hasFunction(ContainerFunctions function) const
int getOwner() const
returns the number of the player this vehicle/building belongs to
static sigc::signal< void, ContainerBase * > anyContainerConquered
The interface for all kinds of IO stream.
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
virtual ActionResult postCheck()
postCheck is called after an action has been redone and should check that the state of the map is exa...
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
virtual void readData(tnstream &stream)
ActionResult execute(const Context &context)
Definition: action.cpp:41
virtual ActionResult undoAction(const Context &context)
virtual GameActionID getID() const
virtual ActionResult runAction(const Context &context)
ContainerBase * getContainer(bool dontThrow=false)
const ContainerBaseType * baseType
the type descriping all non-instance specific properties of the container
Definition: containerbase.h:80
sigc::signal< void > conquered
ASCString getDescription() const
The parent class of Vehicle and Building; The name Container originates from Battle Isle...
Definition: containerbase.h:40
virtual void writeData(tnstream &stream) const
virtual void registerForNewOwner(int player)=0
this is a low level functions that changes the registration in the map. It's called by convert(int...
void readData(tnstream &stream)
virtual ActionResult preCheck()
preCheck is called before an action is redone and should check the preconditions for the action...