Advanced Strategic Command
consumeresource.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 "consumeresource.h"
23 #include "action-registry.h"
24 
25 #include "../vehicle.h"
26 #include "../gamemap.h"
27 
28 ConsumeResource::ConsumeResource( ContainerBase* container, const Resources& toGet )
29  : ContainerAction( container )
30 {
31  this->toGet = toGet;
32 }
33 
34 
36 {
37  ASCString res = "Consume " + toGet.toString() + " resources ";
38  const ContainerBase* c = getContainer();
39  if ( c )
40  res += " of " + c->getName();
41  return res;
42 }
43 
44 
45 const int consumeResourceVersion = 1;
46 
47 void ConsumeResource::readData ( tnstream& stream )
48 {
49  ContainerAction::readData( stream );
50 
51  int version = stream.readInt();
52  if ( version < 1 || version > consumeResourceVersion)
53  throw tinvalidversion ( "ConsumeResource", consumeResourceVersion, version );
54 
55  toGet.read( stream );
56  got.read( stream );
57 };
58 
59 
60 void ConsumeResource::writeData ( tnstream& stream ) const
61 {
63  stream.writeInt( consumeResourceVersion );
64  toGet.write( stream );
65  got.write( stream );
66 };
67 
68 
70 {
72 }
73 
75 {
76  got = getContainer()->getResource( toGet, false, 1, context.actingPlayer->getPosition() );
77  return ActionResult(0);
78 }
79 
80 
82 {
83  getContainer()->putResource( got, false, 1, context.actingPlayer->getPosition() );
84  return ActionResult(0);
85 }
86 
88 {
89  return ActionResult(0);
90 }
91 
92 
93 namespace {
94  const bool r1 = registerAction<ConsumeResource> ( ActionRegistry::ConsumeResource );
95 }
void writeData(tnstream &stream) const
Player * actingPlayer
Definition: context.h:37
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 putResource(int amount, int resourcetype, bool queryonly, int scope=1, int player=-1)=0
scope: 0 = local 1 = resource network 2 = global in all buildings 3 = map wide pool( used only intern...
int GameActionID
Definition: action.h:35
virtual void writeData(tnstream &stream) const
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
int getPosition() const
Definition: player.h:112
ASCString toString() const
Definition: typen.cpp:268
virtual ActionResult undoAction(const Context &context)
void read(tnstream &stream)
Definition: typen.cpp:195
The interface for all kinds of IO stream.
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
virtual ActionResult runAction(const Context &context)
virtual GameActionID getID() const
virtual int getResource(int amount, int resourcetype, bool queryonly, int scope=1, int player=-1)=0
virtual ActionResult postCheck()
postCheck is called after an action has been redone and should check that the state of the map is exa...
void write(tnstream &stream) const
Definition: typen.cpp:201
ASCString getDescription() const
ContainerBase * getContainer(bool dontThrow=false)
The parent class of Vehicle and Building; The name Container originates from Battle Isle...
Definition: containerbase.h:40
const int consumeResourceVersion
Resources are basically the currency of ASC.
Definition: typen.h:97
virtual void readData(tnstream &stream)
void readData(tnstream &stream)