Advanced Strategic Command
changediplomaticstate.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 "changediplomaticstate.h"
23 
24 #include "action-registry.h"
25 #include "../gamemap.h"
26 #include "../itemrepository.h"
27 
28 
29 ChangeDiplomaticState::ChangeDiplomaticState( Player& fromplayer, Player& towardsPlayer, int queue, DiplomaticStates newState )
30  : GameAction( fromplayer.getParentMap() )
31 {
32  this->fromPlayer = fromplayer.getPosition();
33  this->towardsPlayer = towardsPlayer.getPosition();
34  this->queued = queue;
35  this->newState = newState;
36  this->oldState = -1;
37 }
38 
40 {
41  stream.readInt();
42  fromPlayer = stream.readInt();
43  towardsPlayer = stream.readInt();
44  queued = stream.readInt();
45  newState = (DiplomaticStates) stream.readInt();
46  oldState = stream.readInt();
47 }
48 
49 
51 {
52  stream.writeInt( 1 );
53  stream.writeInt( fromPlayer );
54  stream.writeInt( towardsPlayer );
55  stream.writeInt( queued );
56  stream.writeInt( newState );
57  stream.writeInt( oldState );
58 }
59 
61 {
62  DiplomaticStateVector& d = getMap()->getPlayer( fromPlayer ).diplomacy;
63 
64  if ( queued > 0) {
65  DiplomaticStateVector::QueuedStateChanges::iterator i = d.queuedStateChanges.find( towardsPlayer );
66  if ( i != d.queuedStateChanges.end() )
67  oldState = i->second;
68  else
69  oldState = -1;
70 
71  d.queuedStateChanges[towardsPlayer] = newState;
72  } else
73  if ( queued == 0 ) {
74  oldState = d.getState( towardsPlayer );
75  d.setState( towardsPlayer, newState );
76  } else
77  if ( queued == -1 ) {
78  DiplomaticStateVector::QueuedStateChanges::iterator i = d.queuedStateChanges.find( towardsPlayer );
79  if ( i != d.queuedStateChanges.end() ) {
80  oldState = i->second;
81  d.queuedStateChanges.erase( i );
82  } else
83  oldState = -1;
84  }
85 
86  return ActionResult(0);
87 }
88 
89 
91 {
92  DiplomaticStateVector& d = getMap()->getPlayer( fromPlayer ).diplomacy;
93 
94  if ( queued == 1 ) {
95  DiplomaticStateVector::QueuedStateChanges::iterator i = d.queuedStateChanges.find( towardsPlayer );
96  if ( oldState == -1 )
97  d.queuedStateChanges.erase( i );
98  else
99  i->second = (DiplomaticStates) oldState;
100 
101  } else
102  if ( queued == 0 ) {
103  d.setState( towardsPlayer, (DiplomaticStates) oldState );
104  } else
105  if ( queued == -1 ) {
106  if ( oldState < 0 || oldState >= diplomaticStateNum )
107  return ActionResult(23101);
108  d.queuedStateChanges[towardsPlayer] = (DiplomaticStates) oldState;
109  }
110 
111  return ActionResult(0);
112 }
113 
114 
116 {
117  ASCString c;
118  c.format("changediplomaticstate ( %d %d %d %d )", fromPlayer, towardsPlayer, queued, (int)newState );
119  return c;
120 }
121 
123 {
125 }
126 
128 {
129  ASCString s = "Change diplomatic state between " + ASCString::toString(fromPlayer) + " and " + ASCString::toString(towardsPlayer)
130  + " to " + diplomaticStateNames[newState];
131  return s;
132 }
133 
134 
135 namespace {
136  const bool r1 = registerAction<ChangeDiplomaticState> ( ActionRegistry::ChangeDiplomaticState );
137 }
138 
DiplomaticStates getState(PlayerID towardsPlayer) const
Definition: player.cpp:92
void readData(tnstream &stream)
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
DiplomaticStates
Definition: player.h:53
int GameActionID
Definition: action.h:35
const char * diplomaticStateNames[diplomaticStateNum+1]
Definition: player.cpp:36
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
DiplomaticStateVector diplomacy
Definition: player.h:209
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
const int diplomaticStateNum
Definition: player.h:52
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
void setState(PlayerID towardsPlayer, DiplomaticStates s)
Definition: player.cpp:115
ASCString getDescription() const
ActionResult runAction(const Context &context)
void writeData(tnstream &stream) const
ASCString getCommandString() const
the different players in ASC. There may be 8 players (0..7) and neutral units (8) ...
Definition: player.h:99
A GameAction is an modification of something on a map.
Definition: action.h:45
ActionResult undoAction(const Context &context)
GameActionID getID() const