diplomacytest.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                                                         *
00003  *   This program is free software; you can redistribute it and/or modify  *
00004  *   it under the terms of the GNU General Public License as published by  *
00005  *   the Free Software Foundation; either version 2 of the License, or     *
00006  *   (at your option) any later version.                                   *
00007  *                                                                         *
00008  ***************************************************************************/
00009 
00010 #include "../gamemap.h"
00011 #include "../loaders.h"
00012 #include "diplomacytest.h"
00013 #include "unittestutil.h"
00014 #include "spfst.h"
00015 #include "../actions/diplomacycommand.h"
00016 
00017 
00018 void checkSymmetry( const Player& p0, const Player& p1 )
00019 {
00020    assertOrThrow( p0.diplomacy.getState( p1 ) == p1.diplomacy.getState( p0 ) );
00021 }
00022 
00023 void testDiplomacy1() 
00024 {
00025    auto_ptr<GameMap> game ( startMap("unittest-diplomacy.map"));
00026    
00027    // there is PEACE between the player
00028    
00029    Player& p0 = game->getPlayer(0);
00030    Player& p1 = game->getPlayer(1);
00031    
00032    assertOrThrow( p0.diplomacy.getState( p1 ) == PEACE );
00033    checkSymmetry(p0,p1);
00034    
00035    assertOrThrow( p0.diplomacy.isAllied( p1 ) == false );
00036    assertOrThrow( p0.diplomacy.isHostile( p1 ) == false );
00037    
00038    assertOrThrow( game->actplayer == 0);
00039    
00040    assertOrThrow( !fieldvisiblenow( game->getField( MapCoordinate( 0, 18)), p1.getPosition() ));
00041    
00042    
00043    // Player 0 proposes ALLIANCE
00044    
00045    DiplomacyCommand* dc = new DiplomacyCommand( p0 );
00046    dc->newstate( ALLIANCE, p1 );
00047    dc->execute( createTestingContext( game.get() ));
00048    
00049    assertOrThrow( p0.diplomacy.isAllied( p1 ) == false );
00050    checkSymmetry(p0,p1);
00051    
00052    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00053    assertOrThrow( game->actplayer == 1);
00054    
00055    assertOrThrow( p0.diplomacy.isAllied( p1 ) == false );
00056    checkSymmetry(p0,p1);
00057    
00058    // Player 1 accepts ALLIANCE
00059    
00060    dc = new DiplomacyCommand( p1 );
00061    dc->newstate( ALLIANCE, p0 );
00062    dc->execute( createTestingContext( game.get() ));
00063    assertOrThrow( p0.diplomacy.isAllied( p1 ) == true );
00064    assertOrThrow( p0.diplomacy.getState( p1 ) == ALLIANCE );
00065    
00066    checkSymmetry(p0,p1);
00067    
00068    // now there is ALLIANCE between the players
00069    
00070    assertOrThrow( fieldvisiblenow( game->getField( MapCoordinate( 0, 18)), p1.getPosition()));
00071    
00072    
00073    // Player 1 makes a sneak attack 
00074    
00075    dc = new DiplomacyCommand( p1 );
00076    dc->sneakAttack( p0 );
00077    dc->execute( createTestingContext( game.get() ));
00078    checkSymmetry(p0,p1);
00079    
00080    assertOrThrow( p0.diplomacy.getState( p1 ) == WAR );
00081    
00082    // NOW there is WAR between the player
00083    
00084    assertOrThrow( !fieldvisiblenow( game->getField( MapCoordinate( 0, 18)), p1.getPosition()));
00085    
00086    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00087    assertOrThrow( game->actplayer == 0);
00088    
00089    checkSymmetry(p0,p1);
00090    assertOrThrow( p0.diplomacy.getState( p1 ) == WAR );
00091    
00092    
00093    // Player 0 proposes peace 
00094    
00095    dc = new DiplomacyCommand( p0 );
00096    dc->newstate( PEACE_SV, p1 );
00097    dc->execute( createTestingContext( game.get() ));
00098    
00099    checkSymmetry(p0,p1);
00100    assertOrThrow( p0.diplomacy.getState( p1 ) == WAR );
00101    
00102    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00103    assertOrThrow( game->actplayer == 1);
00104    
00105    // Player 1 only accepts truce
00106    
00107    dc = new DiplomacyCommand( p1 );
00108    dc->newstate( TRUCE, p0 );
00109    dc->execute( createTestingContext( game.get() ));
00110    
00111    checkSymmetry(p0,p1);
00112    assertOrThrow( p0.diplomacy.getState( p1 ) == TRUCE );
00113    
00114    assertOrThrow( p0.diplomacy.getProposal( p1.getPosition(), NULL ) == false);
00115    assertOrThrow( p1.diplomacy.getProposal( p0.getPosition(), NULL ) == false);
00116    
00117    // now there is truce and now queued requests
00118    
00119    
00120    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00121    assertOrThrow( game->actplayer == 0);
00122    
00123    checkSymmetry(p0,p1);
00124    assertOrThrow( p0.diplomacy.getState( p1 ) == TRUCE );
00125    
00126    // Player 0 proposes peace again
00127    
00128    dc = new DiplomacyCommand( p0 );
00129    dc->newstate( PEACE, p1 );
00130    dc->execute( createTestingContext( game.get() ));
00131    
00132    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00133    assertOrThrow( game->actplayer == 1);
00134    
00135    checkSymmetry(p0,p1);
00136    assertOrThrow( p0.diplomacy.getState( p1 ) == TRUCE );
00137    
00138    assertOrThrow( p0.diplomacy.getProposal( p1.getPosition(), NULL ) == false );
00139    assertOrThrow( p1.diplomacy.getProposal( p0.getPosition(), NULL ) == true );
00140    
00141    
00142    // Player 1 accepts the peace and proposes to do even an alliance  
00143    
00144    dc = new DiplomacyCommand( p1 );
00145    dc->newstate( ALLIANCE, p0 );
00146    dc->execute( createTestingContext( game.get() ));
00147    
00148    checkSymmetry(p0,p1);
00149    assertOrThrow( p0.diplomacy.getState( p1 ) == PEACE );
00150    
00151    assertOrThrow( p0.diplomacy.getProposal( p1.getPosition(), NULL ) == true );
00152    assertOrThrow( p1.diplomacy.getProposal( p0.getPosition(), NULL ) == false );
00153    
00154    
00155    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00156    assertOrThrow( game->actplayer == 0);
00157    
00158    checkSymmetry(p0,p1);
00159    assertOrThrow( p0.diplomacy.getState( p1 ) == PEACE );
00160    
00161    assertOrThrow( !fieldvisiblenow( game->getField( MapCoordinate( 0, 18)), p1.getPosition()));
00162    
00163    dc = new DiplomacyCommand( p0 );
00164    dc->newstate( ALLIANCE, p1 );
00165    dc->execute( createTestingContext( game.get() ));
00166    
00167    checkSymmetry(p0,p1);
00168    assertOrThrow( p0.diplomacy.getState( p1 ) == ALLIANCE );
00169    
00170    assertOrThrow( fieldvisiblenow( game->getField( MapCoordinate( 0, 18)), p1.getPosition()));
00171    
00172    
00173    assertOrThrow( p0.diplomacy.getProposal( p1.getPosition(), NULL ) == false );
00174    assertOrThrow( p1.diplomacy.getProposal( p0.getPosition(), NULL ) == false );
00175    
00176    
00177    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00178    assertOrThrow( game->actplayer == 1);
00179    
00180    
00181    dc = new DiplomacyCommand( p1 );
00182    dc->newstate( TRUCE, p0 );
00183    dc->execute( createTestingContext( game.get() ));
00184 
00185    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00186    assertOrThrow( game->actplayer == 0);
00187    
00188    dc = new DiplomacyCommand( p0 );
00189    dc->newstate( WAR, p1 );
00190    dc->execute( createTestingContext( game.get() ));
00191       
00192    
00193    checkSymmetry(p0,p1);
00194    assertOrThrow( p0.diplomacy.getState( p1 ) == TRUCE );
00195    
00196    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00197    assertOrThrow( game->actplayer == 1);
00198    
00199    checkSymmetry(p0,p1);
00200    assertOrThrow( p0.diplomacy.getState( p1 ) == WAR );
00201 }
00202 
00203 
00204 
00205 void testDiplomacy2() 
00206 {
00207    auto_ptr<GameMap> game ( startMap("unittest-diplomacy.map"));
00208    
00209    // there is PEACE between the player
00210    
00211    Player& p0 = game->getPlayer(0);
00212    Player& p1 = game->getPlayer(1);
00213    
00214    assertOrThrow( p0.diplomacy.getState( p1 ) == PEACE );
00215    checkSymmetry(p0,p1);
00216    
00217    assertOrThrow( p0.diplomacy.isAllied( p1 ) == false );
00218    assertOrThrow( p0.diplomacy.isHostile( p1 ) == false );
00219    
00220    // player0 declares WAR
00221    
00222    DiplomacyCommand* dc = new DiplomacyCommand( p0 );
00223    dc->newstate( WAR, p1 );
00224    dc->execute( createTestingContext( game.get() ));
00225    
00226    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00227    assertOrThrow( game->actplayer == 1);
00228    
00229    // .. so on the next turn the WAR will take effect
00230    
00231    checkSymmetry(p0,p1);
00232    assertOrThrow( p0.diplomacy.getState( p1 ) == WAR );
00233    
00234 }
00235 
00236 void testDiplomacy3() 
00237 {
00238    auto_ptr<GameMap> game ( startMap("unittest-diplomacy.map"));
00239    
00240    // there is PEACE between the player
00241    
00242    Player& p0 = game->getPlayer(0);
00243    Player& p1 = game->getPlayer(1);
00244    
00245    assertOrThrow( p0.diplomacy.getState( p1 ) == PEACE );
00246    checkSymmetry(p0,p1);
00247    
00248    assertOrThrow( p0.diplomacy.isAllied( p1 ) == false );
00249    assertOrThrow( p0.diplomacy.isHostile( p1 ) == false );
00250    
00251    // player0 declares WAR
00252    
00253    DiplomacyCommand* dc = new DiplomacyCommand( p0 );
00254    dc->newstate( WAR, p1 );
00255    dc->execute( createTestingContext( game.get() ));
00256    
00257    // ... and presses undo
00258    
00259    ActionResult res = game->actions.undo( createTestingContext( game.get() ) );
00260    assertOrThrow( res.successful() );
00261    
00262    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00263    assertOrThrow( game->actplayer == 1);
00264    
00265    checkSymmetry(p0,p1);
00266    assertOrThrow( p0.diplomacy.getState( p1 ) == PEACE );
00267    
00268    // nothing happens
00269    
00270    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00271    
00272    // still nothing
00273    
00274    assertOrThrow( game->actplayer == 0);
00275    
00276    checkSymmetry(p0,p1);
00277    assertOrThrow( p0.diplomacy.getState( p1 ) == PEACE );
00278    
00279 }
00280 
00281 
00282 void testDiplomacy() 
00283 {
00284    testDiplomacy1();
00285    testDiplomacy2();
00286 }

Generated on Mon May 21 01:26:31 2012 for Advanced Strategic Command by  doxygen 1.5.1