Advanced Strategic Command
diplomacytest.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * This program is free software; you can redistribute it and/or modify *
4  * it under the terms of the GNU General Public License as published by *
5  * the Free Software Foundation; either version 2 of the License, or *
6  * (at your option) any later version. *
7  * *
8  ***************************************************************************/
9 
10 #include "../gamemap.h"
11 #include "../loaders.h"
12 #include "diplomacytest.h"
13 #include "unittestutil.h"
14 #include "spfst.h"
15 #include "../actions/diplomacycommand.h"
16 
17 
18 void checkSymmetry( const Player& p0, const Player& p1 )
19 {
20  assertOrThrow( p0.diplomacy.getState( p1 ) == p1.diplomacy.getState( p0 ) );
21 }
22 
24 {
25  auto_ptr<GameMap> game ( startMap("unittest-diplomacy.map"));
26 
27  // there is PEACE between the player
28 
29  Player& p0 = game->getPlayer(0);
30  Player& p1 = game->getPlayer(1);
31 
32  assertOrThrow( p0.diplomacy.getState( p1 ) == PEACE );
33  checkSymmetry(p0,p1);
34 
35  assertOrThrow( p0.diplomacy.isAllied( p1 ) == false );
36  assertOrThrow( p0.diplomacy.isHostile( p1 ) == false );
37 
38  assertOrThrow( game->actplayer == 0);
39 
40  assertOrThrow( !fieldvisiblenow( game->getField( MapCoordinate( 0, 18)), p1.getPosition() ));
41 
42 
43  // Player 0 proposes ALLIANCE
44 
45  DiplomacyCommand* dc = new DiplomacyCommand( p0 );
46  dc->newstate( ALLIANCE, p1 );
47  dc->execute( createTestingContext( game.get() ));
48 
49  assertOrThrow( p0.diplomacy.isAllied( p1 ) == false );
50  checkSymmetry(p0,p1);
51 
52  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
53  assertOrThrow( game->actplayer == 1);
54 
55  assertOrThrow( p0.diplomacy.isAllied( p1 ) == false );
56  checkSymmetry(p0,p1);
57 
58  // Player 1 accepts ALLIANCE
59 
60  dc = new DiplomacyCommand( p1 );
61  dc->newstate( ALLIANCE, p0 );
62  dc->execute( createTestingContext( game.get() ));
63  assertOrThrow( p0.diplomacy.isAllied( p1 ) == true );
65 
66  checkSymmetry(p0,p1);
67 
68  // now there is ALLIANCE between the players
69 
70  assertOrThrow( fieldvisiblenow( game->getField( MapCoordinate( 0, 18)), p1.getPosition()));
71 
72 
73  // Player 1 makes a sneak attack
74 
75  dc = new DiplomacyCommand( p1 );
76  dc->sneakAttack( p0 );
77  dc->execute( createTestingContext( game.get() ));
78  checkSymmetry(p0,p1);
79 
80  assertOrThrow( p0.diplomacy.getState( p1 ) == WAR );
81 
82  // NOW there is WAR between the player
83 
84  assertOrThrow( !fieldvisiblenow( game->getField( MapCoordinate( 0, 18)), p1.getPosition()));
85 
86  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
87  assertOrThrow( game->actplayer == 0);
88 
89  checkSymmetry(p0,p1);
90  assertOrThrow( p0.diplomacy.getState( p1 ) == WAR );
91 
92 
93  // Player 0 proposes peace
94 
95  dc = new DiplomacyCommand( p0 );
96  dc->newstate( PEACE_SV, p1 );
97  dc->execute( createTestingContext( game.get() ));
98 
99  checkSymmetry(p0,p1);
100  assertOrThrow( p0.diplomacy.getState( p1 ) == WAR );
101 
102  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
103  assertOrThrow( game->actplayer == 1);
104 
105  // Player 1 only accepts truce
106 
107  dc = new DiplomacyCommand( p1 );
108  dc->newstate( TRUCE, p0 );
109  dc->execute( createTestingContext( game.get() ));
110 
111  checkSymmetry(p0,p1);
112  assertOrThrow( p0.diplomacy.getState( p1 ) == TRUCE );
113 
114  assertOrThrow( p0.diplomacy.getProposal( p1.getPosition(), NULL ) == false);
115  assertOrThrow( p1.diplomacy.getProposal( p0.getPosition(), NULL ) == false);
116 
117  // now there is truce and now queued requests
118 
119 
120  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
121  assertOrThrow( game->actplayer == 0);
122 
123  checkSymmetry(p0,p1);
124  assertOrThrow( p0.diplomacy.getState( p1 ) == TRUCE );
125 
126  // Player 0 proposes peace again
127 
128  dc = new DiplomacyCommand( p0 );
129  dc->newstate( PEACE, p1 );
130  dc->execute( createTestingContext( game.get() ));
131 
132  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
133  assertOrThrow( game->actplayer == 1);
134 
135  checkSymmetry(p0,p1);
136  assertOrThrow( p0.diplomacy.getState( p1 ) == TRUCE );
137 
138  assertOrThrow( p0.diplomacy.getProposal( p1.getPosition(), NULL ) == false );
139  assertOrThrow( p1.diplomacy.getProposal( p0.getPosition(), NULL ) == true );
140 
141 
142  // Player 1 accepts the peace and proposes to do even an alliance
143 
144  dc = new DiplomacyCommand( p1 );
145  dc->newstate( ALLIANCE, p0 );
146  dc->execute( createTestingContext( game.get() ));
147 
148  checkSymmetry(p0,p1);
149  assertOrThrow( p0.diplomacy.getState( p1 ) == PEACE );
150 
151  assertOrThrow( p0.diplomacy.getProposal( p1.getPosition(), NULL ) == true );
152  assertOrThrow( p1.diplomacy.getProposal( p0.getPosition(), NULL ) == false );
153 
154 
155  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
156  assertOrThrow( game->actplayer == 0);
157 
158  checkSymmetry(p0,p1);
159  assertOrThrow( p0.diplomacy.getState( p1 ) == PEACE );
160 
161  assertOrThrow( !fieldvisiblenow( game->getField( MapCoordinate( 0, 18)), p1.getPosition()));
162 
163  dc = new DiplomacyCommand( p0 );
164  dc->newstate( ALLIANCE, p1 );
165  dc->execute( createTestingContext( game.get() ));
166 
167  checkSymmetry(p0,p1);
168  assertOrThrow( p0.diplomacy.getState( p1 ) == ALLIANCE );
169 
170  assertOrThrow( fieldvisiblenow( game->getField( MapCoordinate( 0, 18)), p1.getPosition()));
171 
172 
173  assertOrThrow( p0.diplomacy.getProposal( p1.getPosition(), NULL ) == false );
174  assertOrThrow( p1.diplomacy.getProposal( p0.getPosition(), NULL ) == false );
175 
176 
177  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
178  assertOrThrow( game->actplayer == 1);
179 
180 
181  dc = new DiplomacyCommand( p1 );
182  dc->newstate( TRUCE, p0 );
183  dc->execute( createTestingContext( game.get() ));
184 
185  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
186  assertOrThrow( game->actplayer == 0);
187 
188  dc = new DiplomacyCommand( p0 );
189  dc->newstate( WAR, p1 );
190  dc->execute( createTestingContext( game.get() ));
191 
192 
193  checkSymmetry(p0,p1);
194  assertOrThrow( p0.diplomacy.getState( p1 ) == TRUCE );
195 
196  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
197  assertOrThrow( game->actplayer == 1);
198 
199  checkSymmetry(p0,p1);
200  assertOrThrow( p0.diplomacy.getState( p1 ) == WAR );
201 }
202 
203 
204 
206 {
207  auto_ptr<GameMap> game ( startMap("unittest-diplomacy.map"));
208 
209  // there is PEACE between the player
210 
211  Player& p0 = game->getPlayer(0);
212  Player& p1 = game->getPlayer(1);
213 
214  assertOrThrow( p0.diplomacy.getState( p1 ) == PEACE );
215  checkSymmetry(p0,p1);
216 
217  assertOrThrow( p0.diplomacy.isAllied( p1 ) == false );
218  assertOrThrow( p0.diplomacy.isHostile( p1 ) == false );
219 
220  // player0 declares WAR
221 
222  DiplomacyCommand* dc = new DiplomacyCommand( p0 );
223  dc->newstate( WAR, p1 );
224  dc->execute( createTestingContext( game.get() ));
225 
226  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
227  assertOrThrow( game->actplayer == 1);
228 
229  // .. so on the next turn the WAR will take effect
230 
231  checkSymmetry(p0,p1);
232  assertOrThrow( p0.diplomacy.getState( p1 ) == WAR );
233 
234 }
235 
237 {
238  auto_ptr<GameMap> game ( startMap("unittest-diplomacy.map"));
239 
240  // there is PEACE between the player
241 
242  Player& p0 = game->getPlayer(0);
243  Player& p1 = game->getPlayer(1);
244 
245  assertOrThrow( p0.diplomacy.getState( p1 ) == PEACE );
246  checkSymmetry(p0,p1);
247 
248  assertOrThrow( p0.diplomacy.isAllied( p1 ) == false );
249  assertOrThrow( p0.diplomacy.isHostile( p1 ) == false );
250 
251  // player0 declares WAR
252 
253  DiplomacyCommand* dc = new DiplomacyCommand( p0 );
254  dc->newstate( WAR, p1 );
255  dc->execute( createTestingContext( game.get() ));
256 
257  // ... and presses undo
258 
259  ActionResult res = game->actions.undo( createTestingContext( game.get() ) );
260  assertOrThrow( res.successful() );
261 
262  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
263  assertOrThrow( game->actplayer == 1);
264 
265  checkSymmetry(p0,p1);
266  assertOrThrow( p0.diplomacy.getState( p1 ) == PEACE );
267 
268  // nothing happens
269 
270  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
271 
272  // still nothing
273 
274  assertOrThrow( game->actplayer == 0);
275 
276  checkSymmetry(p0,p1);
277  assertOrThrow( p0.diplomacy.getState( p1 ) == PEACE );
278 
279 }
280 
281 
283 {
284  testDiplomacy1();
285  testDiplomacy2();
286 }
DiplomaticStates getState(PlayerID towardsPlayer) const
Definition: player.cpp:92
map accessing and usage routines used by ASC and the mapeditor
bool isHostile(PlayerID towardsPlayer) const
Definition: player.h:80
Definition: player.h:53
int getPosition() const
Definition: player.h:112
#define assertOrThrow(expr)
Definition: errors.h:69
DiplomaticStateVector diplomacy
Definition: player.h:209
Context createTestingContext(GameMap *gamemap)
void testDiplomacy3()
Definition: player.h:53
void newstate(DiplomaticStates state, Player &towards)
void testDiplomacy()
bool fieldvisiblenow(const MapField *pe, Vehicle *veh, int player)
{@
Definition: spfst.cpp:399
void next_turn(GameMap *gamemap, const NextTurnStrategy &nextTurnStrategy, MapDisplayInterface *display, int playerView)
ActionResult execute(const Context &context)
Definition: action.cpp:41
Coordinate on the twodimensional map.
Definition: typen.h:202
void checkSymmetry(const Player &p0, const Player &p1)
void testDiplomacy1()
GameMap * startMap(const ASCString &filename)
bool successful() const
Definition: player.h:53
bool isAllied(PlayerID towardsPlayer) const
Definition: player.h:82
void sneakAttack(Player &enemy)
the different players in ASC. There may be 8 players (0..7) and neutral units (8) ...
Definition: player.h:99
bool getProposal(int fromPlayer, DiplomaticStates *state)
Definition: player.cpp:127
void testDiplomacy2()
cancels the research on the current technology, losing all research points