00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "../actions/moveunitcommand.h"
00012 #include "../actions/attackcommand.h"
00013 #include "../loaders.h"
00014 #include "../viewcalculation.h"
00015 #include "../turncontrol.h"
00016
00017 #include "unittestutil.h"
00018
00019
00020
00021 void move( Vehicle* veh, const MapCoordinate& dest )
00022 {
00023 auto_ptr<MoveUnitCommand> muc ( new MoveUnitCommand( veh ));
00024 muc->setDestination( dest );
00025 ActionResult res = muc->execute( createTestingContext( veh->getMap() ));
00026 if ( res.successful() )
00027 muc.release();
00028 else
00029 throw ActionResult(res);
00030 }
00031
00032 void attack( Vehicle* veh, const MapCoordinate& target )
00033 {
00034 auto_ptr<AttackCommand> muc ( new AttackCommand( veh ));
00035 muc->setTarget( target );
00036 ActionResult res = muc->execute( createTestingContext( veh->getMap() ));
00037 if ( res.successful() )
00038 muc.release();
00039 else
00040 throw ActionResult(res);
00041 }
00042
00043
00044 GameMap* startMap( const ASCString& filename )
00045 {
00046 GameMap* game = mapLoadingExceptionChecker( filename, MapLoadingFunction( tmaploaders::loadmap ));
00047 if ( !game )
00048 throw TestFailure("could not load map " + filename);;
00049
00050 game->levelfinished = false;
00051
00052 if ( game->replayinfo ) {
00053 delete game->replayinfo;
00054 game->replayinfo = NULL;
00055 }
00056
00057 computeview( game );
00058
00059 if ( game && game->actplayer == -1 )
00060 next_turn(game, NextTurnStrategy_Abort(), NULL, -1);
00061
00062 return game;
00063
00064 }
00065
00066 Context createTestingContext( GameMap* gamemap )
00067 {
00068 Context context;
00069
00070 context.gamemap = gamemap;
00071 context.actingPlayer = &gamemap->getPlayer( gamemap->actplayer );
00072 context.parentAction = NULL;
00073 context.display = NULL;
00074 context.viewingPlayer = gamemap->getPlayerView();
00075 context.actionContainer = &gamemap->actions;
00076 return context;
00077 }
00078
00079 void testCargoMovement( Vehicle* veh, int movement )
00080 {
00081 for ( Vehicle::Cargo::const_iterator i = veh->getCargo().begin(); i != veh->getCargo().end(); ++i )
00082 if ( *i ) {
00083 Vehicle* cargo1 = *i;
00084 assertOrThrow( cargo1->getMovement() == movement );
00085
00086 testCargoMovement( cargo1, movement );
00087 }
00088 }
00089
00090 void testCargoMovementMax( Vehicle* veh, int movement )
00091 {
00092 for ( Vehicle::Cargo::const_iterator i = veh->getCargo().begin(); i != veh->getCargo().end(); ++i )
00093 if ( *i ) {
00094 Vehicle* cargo1 = *i;
00095 assertOrThrow( cargo1->getMovement() <= movement );
00096
00097 testCargoMovementMax( cargo1, movement );
00098 }
00099 }
00100
00101 Vehicle* getFirstCargo( ContainerBase* carrier )
00102 {
00103 for ( Vehicle::Cargo::const_iterator i = carrier->getCargo().begin(); i != carrier->getCargo().end(); ++i )
00104 if ( *i )
00105 return *i;
00106 return NULL;
00107 }