Advanced Strategic Command
transfercontrolcommand.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 "transfercontrolcommand.h"
23 
24 #include "../vehicle.h"
25 #include "../mapfield.h"
26 #include "../gamemap.h"
27 #include "../viewcalculation.h"
28 #include "../spfst.h"
29 #include "../mapdisplayinterface.h"
30 #include "action-registry.h"
31 #include "../itemrepository.h"
32 #include "../containercontrols.h"
33 #include "consumeresource.h"
34 #include "servicecommand.h"
35 #include "convertcontainer.h"
36 
38 {
39  if ( !item )
40  return false;
41 
43  return false;
44 
45  Receivers rec = getReceivers( item->getMap(), item->getOwner(), item->getCarrier() );
46 
47  return rec.size() > 0 ;
48 }
49 
50 
51 TransferControlCommand::Receivers TransferControlCommand::getReceivers( GameMap* map, int currentPlayer, bool isInCarrier )
52 {
53  Receivers rec;
54  if ( !map )
55  return rec;
56 
57  for ( int p = 0; p < map->getPlayerCount(); ++p )
58  if ( p != currentPlayer )
59  if ( map->player[p].exist() ) {
60  if ( isInCarrier ) {
61  if ( map->player[p].diplomacy.getState( currentPlayer ) >= ALLIANCE )
62  rec.push_back( &map->player[p] );
63  } else {
64  if ( map->player[p].diplomacy.getState( currentPlayer ) >= PEACE )
65  rec.push_back( &map->player[p] );
66  }
67  }
68 
69  return rec;
70 }
71 
72 
74 {
75  return getReceivers( getContainer()->getMap(), getContainer()->getOwner(), getContainer()->getCarrier() );
76 }
77 
78 
79 TransferControlCommand :: TransferControlCommand ( ContainerBase* container )
80  : ContainerCommand ( container ), receivingPlayer(-1)
81 {
82 
83 }
84 
85 
86 
87 
88 
90 {
91  if ( getState() != SetUp )
92  return ActionResult(22000);
93 
94  if ( !avail( getContainer() ))
95  return ActionResult(22800);
96 
97  bool found = false;
98  Receivers rec = getReceivers();
99  for ( Receivers::const_iterator i = rec.begin(); i != rec.end(); ++i )
100  if ( (*i)->getPosition() == receivingPlayer )
101  found = true;
102 
103  if ( !found )
104  return ActionResult(22801);
105 
106  auto_ptr<ConvertContainer> cc ( new ConvertContainer( getContainer(), receivingPlayer ));
107  ActionResult res = cc->execute(context);
108 
109  if ( res.successful() ) {
110  cc.release();
111  setState( Finished );
112 
113  computeview( getMap(), 0, false, &context );
114 
115  if ( context.display )
116  context.display->repaintDisplay();
117  } else
118  setState( Failed );
119 
120  return res;
121 }
122 
123 
124 
125 static const int TransferControlCommandVersion = 1;
126 
128 {
129  ContainerCommand::readData( stream );
130  int version = stream.readInt();
131  if ( version > TransferControlCommandVersion )
132  throw tinvalidversion ( "TransferControlCommand", TransferControlCommandVersion, version );
133  receivingPlayer = stream.readInt();
134 }
135 
137 {
138  ContainerCommand::writeData( stream );
139  stream.writeInt( TransferControlCommandVersion );
140  stream.writeInt( receivingPlayer );
141 }
142 
144 {
145  if ( receiver ) {
146  receivingPlayer = receiver->getPosition();
147  setState( SetUp );
148  }
149 }
150 
151 
153 {
154  ASCString c;
155  c.format("transferControl ( map, %d, %d )", getContainerID(), receivingPlayer );
156  return c;
157 
158 }
159 
161 {
163 }
164 
166 {
167  ASCString s = "Transfer Control";
168 
169  if ( getContainer(true) ) {
170  s += " of " + getContainer()->getName();
171  }
172 
173  s += "to " + getMap()->getPlayer( receivingPlayer ).getName();
174  return s;
175 }
176 
177 namespace
178 {
179  const bool r1 = registerAction<TransferControlCommand> ( ActionRegistry::TransferControlCommand );
180 }
181 
DiplomaticStates getState(PlayerID towardsPlayer) const
Definition: player.cpp:92
Player & getPlayer(PlayerID p)
Definition: gamemap.h:257
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 computeview(GameMap *gamemap, int player_fieldcount_mask, bool disableShareView, const Context *context)
completely computes the view
int GameActionID
Definition: action.h:35
the command is totally done
Definition: command.h:120
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 getName() const
returns the name of the player
Definition: player.cpp:274
DiplomaticStateVector diplomacy
Definition: player.h:209
int getOwner() const
returns the number of the player this vehicle/building belongs to
void writeData(tnstream &stream) const
State getState() const
Definition: command.h:125
static const int TransferControlCommandVersion
GameMap * getMap()
Definition: action.h:92
The interface for all kinds of IO stream.
ASCString & format(const charT *pFormat,...)
Definition: ascstring.cpp:78
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
int getContainerID() const
void setReceiver(const Player *receiver)
vector< const Player * > Receivers
ASCString getCommandString() const
static bool avail(const ContainerBase *item)
ActionResult go(const Context &context)
void setState(State state)
Definition: command.cpp:44
bool successful() const
MapDisplayInterface * display
Definition: context.h:39
Player player[9]
Definition: gamemap.h:253
void writeData(tnstream &stream) const
int getgameparameter(GameParameter num) const
Definition: gamemap.cpp:1047
Definition: player.h:53
ContainerBase * getCarrier() const
if this is a unit and it is inside a building or transport, returns the transport. NULL otherwise.
ContainerBase * getContainer(bool dontThrow=false)
void readData(tnstream &stream)
GameMap * getMap() const
void readData(tnstream &stream)
The parent class of Vehicle and Building; The name Container originates from Battle Isle...
Definition: containerbase.h:40
the different players in ASC. There may be 8 players (0..7) and neutral units (8) ...
Definition: player.h:99
int getPlayerCount() const
Definition: gamemap.h:255
virtual void repaintDisplay()=0
bool exist() const
does the player exist at all
Definition: player.cpp:313
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182