movementtest.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 <iostream>
00011 
00012 #include "../actions/moveunitcommand.h"
00013 #include "../loaders.h"
00014 #include "../itemrepository.h"
00015 #include "unittestutil.h"
00016 #include "../tasks/taskcontainer.h"
00017 
00018 void testMovement1() 
00019 {
00020    auto_ptr<GameMap> game ( startMap("unittest-movement.map"));
00021    
00022    Vehicle* veh = game->getField(0,0)->vehicle;
00023    assertOrThrow( veh->getMovement() == 100 );
00024    
00025    move( veh, MapCoordinate(5,10));
00026    
00027    assertOrThrow( veh->getMovement() == 0 );
00028    testCargoMovement( veh, 50 );
00029       
00030    ActionResult res = game->actions.undo( createTestingContext( game.get() ) );  
00031    assertOrThrow( res.successful() );
00032       
00033    assertOrThrow( veh->getMovement() == 100 );
00034    testCargoMovement( veh, 100 );
00035    
00036    move(veh, MapCoordinate(0,1));   
00037    move(veh, MapCoordinate(1,2));   
00038    move(veh, MapCoordinate(1,3));   
00039    move(veh, MapCoordinate(2,4));   
00040    move(veh, MapCoordinate(2,5));   
00041    move(veh, MapCoordinate(3,6));   
00042    move(veh, MapCoordinate(3,7));   
00043    move(veh, MapCoordinate(4,8));   
00044    move(veh, MapCoordinate(4,9));   
00045    move(veh, MapCoordinate(5,10));   
00046    
00047    assertOrThrow( veh->getMovement() == 0 );
00048    testCargoMovement( veh, 50 );
00049    
00050 }
00051 
00052 void testMovementRF() 
00053 {
00054    auto_ptr<GameMap> game ( startMap("unittest-reactionfire.map"));
00055    
00056    Vehicle* veh = game->getField(4,2)->vehicle;
00057    assertOrThrow( veh != NULL );
00058    
00059    move( veh, MapCoordinate(4,3));
00060    
00061    assertOrThrow( veh->damage == 0 );
00062    
00063    move( veh, MapCoordinate(5,4));
00064    
00065    assertOrThrow( veh->damage > 50 );
00066 }
00067 
00068 void testMovementTracks() 
00069 {
00070    auto_ptr<GameMap> game ( startMap("unittest-objectgeneration.map"));
00071    
00072    Vehicle* veh = game->getField(4,9)->vehicle;
00073    assertOrThrow( veh != NULL );
00074    
00075    move( veh, MapCoordinate(6,12));
00076    
00077    ObjectType* track = objectTypeRepository.getObject_byID( 7 );
00078    
00079    assertOrThrow( game->getField(4,9)->checkForObject( track) != NULL );
00080    assertOrThrow( game->getField(5,10)->checkForObject( track) == NULL );
00081    
00082    ActionResult res = game->actions.undo( createTestingContext( game.get() ) );  
00083    
00084    assertOrThrow( res.successful() );
00085    
00086    assertOrThrow( game->getField(4,9)->checkForObject( track) == NULL );
00087    assertOrThrow( game->getField(5,10)->checkForObject( track) == NULL );
00088   
00089 }
00090 
00091 void testHeightChangeAI()
00092 {
00093    auto_ptr<GameMap> game ( startMap("unittest-heightchange.map"));
00094    Vehicle* veh = game->getField(4,17)->vehicle;
00095    assertOrThrow( veh != NULL );
00096    
00097    auto_ptr<MoveUnitCommand> muc ( new MoveUnitCommand( veh ));
00098    
00099    // this is the AI way of doing things, selecting a 3D coordinate as destination
00100    
00101    MapCoordinate3D dest( 6,10,1<<5 );
00102    assertOrThrow( muc->isFieldReachable3D( dest, true ));
00103    
00104    muc->setDestination( dest );
00105    ActionResult res = muc->execute( createTestingContext( veh->getMap() ));
00106    if ( res.successful() )
00107       muc.release();
00108    else
00109       throw ActionResult(res);
00110    
00111    assertOrThrow( veh->height == 32 );
00112 }
00113 
00114 void testHeightChangeGUI()
00115 {
00116    auto_ptr<GameMap> game ( startMap("unittest-heightchange.map"));
00117    Vehicle* veh = game->getField(4,17)->vehicle;
00118    assertOrThrow( veh != NULL );
00119    
00120    auto_ptr<MoveUnitCommand> muc ( new MoveUnitCommand( veh ));
00121    
00122    // this is the GUI way of doing things, selecting a 2D coordinate as destination
00123    
00124    MapCoordinate dest( 6,10 );
00125    muc->searchFields();
00126    assertOrThrow( muc->isFieldReachable( dest, true ));
00127    
00128    muc->setDestination( dest );
00129    ActionResult res = muc->execute( createTestingContext( veh->getMap() ));
00130    if ( res.successful() )
00131       muc.release();
00132    else
00133       throw ActionResult(res);
00134    
00135    assertOrThrow( veh->height == 16 ); // because low level flight can be reached with less movement consumption
00136    
00137 }
00138 
00139 
00140 void testMovementFieldsReachable()
00141 {
00142    auto_ptr<GameMap> game ( startMap("unittest-moveback.map"));
00143    Vehicle* veh = game->getField(15,31)->vehicle;
00144    assertOrThrow( veh != NULL );
00145    
00146    auto_ptr<MoveUnitCommand> muc ( new MoveUnitCommand( veh ));
00147    
00148    muc->searchFields();
00149    const set<MapCoordinate3D>& fields = muc->getReachableFields();
00150    
00151    for ( set<MapCoordinate3D>::const_iterator i = fields.begin(); i != fields.end(); ++i ) {
00152       auto_ptr<MoveUnitCommand> m2 ( new MoveUnitCommand( veh ));
00153       m2->setDestination( *i );
00154       ActionResult res = m2->execute( createTestingContext( veh->getMap() ));
00155       assertOrThrow( res.successful() );
00156       m2.release();
00157       
00158       res = game->actions.undo( createTestingContext( game.get() ));
00159       assertOrThrow( res.successful() );
00160    }
00161 }
00162 
00163 
00164 static void runTasks( GameMap* gamemap )
00165 {
00166    if ( gamemap->tasks ) {
00167       TaskContainer* tc = dynamic_cast<TaskContainer*>( gamemap->tasks );
00168       if ( tc ) {
00169          while ( tc->pendingCommands.begin() != tc->pendingCommands.end() ) {
00170             Command* c = tc->pendingCommands.front();
00171             TaskInterface* ti = dynamic_cast<TaskInterface*>( c );
00172             
00173             if ( ti->operatable() ) {
00174                ActionResult res = c->execute( createTestingContext( gamemap ));
00175                if ( !res.successful() )
00176                   throw res;
00177             }
00178             
00179             tc->pendingCommands.erase( tc->pendingCommands.begin() );
00180          }
00181       }
00182    }
00183 }
00184 
00185 
00186 static void moveUnitTest( GameMap* game, Vehicle* veh, const MapCoordinate& destination, int turns )
00187 {
00188    assertOrThrow( veh != NULL );
00189    assertOrThrow( game != NULL );
00190    auto_ptr<MoveUnitCommand> muc ( new MoveUnitCommand( veh ));
00191    muc->searchFields();
00192    muc->setDestination( destination );
00193    ActionResult res = muc->execute( createTestingContext( veh->getMap() ));
00194    if ( res.successful() )
00195       muc.release();
00196    else
00197       throw ActionResult(res);
00198    
00199    for ( int i = 0; i < turns; ++i ) {
00200       next_turn( game, NextTurnStrategy_Abort(), NULL, -1 );
00201       runTasks( game );
00202    }
00203    
00204    assertOrThrow( destination == veh->getPosition() );
00205    
00206    TaskContainer* tc = dynamic_cast<TaskContainer*>( game->tasks );
00207    assertOrThrow( tc != NULL );
00208    
00209    assertOrThrow( tc->pendingCommands.begin() == tc->pendingCommands.end() );
00210 
00211 }
00212 
00213 void testLongDistMovement()
00214 {
00215    auto_ptr<GameMap> game ( startMap("unittest-moveland.map"));
00216    Vehicle* veh;
00217    MapCoordinate airport(0,7);
00218    MapCoordinate carrier(0,0);
00219    
00220    veh = game->getField(9,18)->vehicle;
00221    moveUnitTest(  game.get(), veh, airport, 1 );
00222    
00223    veh = game->getField(9,19)->vehicle;
00224    moveUnitTest(  game.get(), veh, carrier, 1 );
00225    
00226    veh = game->getField(9,17)->vehicle;
00227    moveUnitTest(  game.get(), veh, airport, 3 );
00228 }
00229 
00230 
00231 void testMapResizeWithMovement()
00232 {
00233    auto_ptr<GameMap> game ( startMap("unittest-moveland.map"));
00234    Vehicle* veh = game->getField(9,16)->vehicle;
00235   
00236    assertOrThrow( veh != NULL );
00237    
00238    auto_ptr<MoveUnitCommand> muc ( new MoveUnitCommand( veh ));
00239    muc->searchFields();
00240    muc->setDestination( MapCoordinate(3,2) );
00241    ActionResult res = muc->execute( createTestingContext( veh->getMap() ));
00242    if ( res.successful() )
00243       muc.release();
00244    else
00245       throw ActionResult(res);
00246    
00247    game->resize(10,10,10,10);
00248    
00249    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00250    runTasks( game.get() );
00251    
00252    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00253    runTasks( game.get() );
00254    
00255    assertOrThrow( MapCoordinate( 13,12 ) == veh->getPosition() );
00256   
00257 }
00258 
00259 
00260 
00261 void testMovement() 
00262 {
00263    testMapResizeWithMovement();
00264    testLongDistMovement();
00265    testMovementFieldsReachable();
00266    testMovement1();
00267    testMovementRF();
00268    testMovementTracks();
00269    testHeightChangeAI();
00270    testHeightChangeGUI();
00271 }

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