Advanced Strategic Command
unittestutil.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 
11 #include "../actions/moveunitcommand.h"
12 #include "../actions/attackcommand.h"
13 #include "../loaders.h"
14 #include "../viewcalculation.h"
15 #include "../turncontrol.h"
16 
17 #include "unittestutil.h"
18 
19 
20 
21 void move( Vehicle* veh, const MapCoordinate& dest )
22 {
23  auto_ptr<MoveUnitCommand> muc ( new MoveUnitCommand( veh ));
24  muc->setDestination( dest );
25  ActionResult res = muc->execute( createTestingContext( veh->getMap() ));
26  if ( res.successful() )
27  muc.release();
28  else
29  throw ActionResult(res);
30 }
31 
32 void move( Vehicle* veh, const MapCoordinate3D& dest )
33 {
34  auto_ptr<MoveUnitCommand> muc ( new MoveUnitCommand( veh ));
35  muc->setDestination( dest );
36  ActionResult res = muc->execute( createTestingContext( veh->getMap() ));
37  if ( res.successful() )
38  muc.release();
39  else
40  throw ActionResult(res);
41 }
42 
43 void attack( Vehicle* veh, const MapCoordinate& target )
44 {
45  auto_ptr<AttackCommand> muc ( new AttackCommand( veh ));
46  muc->setTarget( target );
47  ActionResult res = muc->execute( createTestingContext( veh->getMap() ));
48  if ( res.successful() )
49  muc.release();
50  else
51  throw ActionResult(res);
52 }
53 
54 
56 {
58  if ( !game )
59  throw TestFailure("could not load map " + filename);;
60 
61  game->levelfinished = false;
62 
63  if ( game->replayinfo ) {
64  delete game->replayinfo;
65  game->replayinfo = NULL;
66  }
67 
68  computeview( game );
69 
70  if ( game && game->actplayer == -1 )
71  next_turn(game, NextTurnStrategy_Abort(), NULL, -1);
72 
73  return game;
74 
75 }
76 
78 {
79  Context context;
80 
81  context.gamemap = gamemap;
82  context.actingPlayer = &gamemap->getPlayer( gamemap->actplayer );
83  context.parentAction = NULL;
84  context.display = NULL;
85  context.viewingPlayer = gamemap->getPlayerView();
86  context.actionContainer = &gamemap->actions;
87  return context;
88 }
89 
90 void testCargoMovement( Vehicle* veh, int movement )
91 {
92  for ( Vehicle::Cargo::const_iterator i = veh->getCargo().begin(); i != veh->getCargo().end(); ++i )
93  if ( *i ) {
94  Vehicle* cargo1 = *i;
95  assertOrThrow( cargo1->getMovement() == movement );
96 
97  testCargoMovement( cargo1, movement );
98  }
99 }
100 
101 void testCargoMovementMax( Vehicle* veh, int movement )
102 {
103  for ( Vehicle::Cargo::const_iterator i = veh->getCargo().begin(); i != veh->getCargo().end(); ++i )
104  if ( *i ) {
105  Vehicle* cargo1 = *i;
106  assertOrThrow( cargo1->getMovement() <= movement );
107 
108  testCargoMovementMax( cargo1, movement );
109  }
110 }
111 
113 {
114  for ( Vehicle::Cargo::const_iterator i = carrier->getCargo().begin(); i != carrier->getCargo().end(); ++i )
115  if ( *i )
116  return *i;
117  return NULL;
118 }
Player * actingPlayer
Definition: context.h:37
ASCString filename
The name of the file from which the item was loaded.
Definition: typen.h:290
Player & getPlayer(PlayerID p)
Definition: gamemap.h:257
void testCargoMovement(Vehicle *veh, int movement)
int computeview(GameMap *gamemap, int player_fieldcount_mask, bool disableShareView, const Context *context)
completely computes the view
void move(Vehicle *veh, const MapCoordinate &dest)
Uint8 levelfinished
Definition: gamemap.h:321
#define assertOrThrow(expr)
Definition: errors.h:69
Vehicle * getFirstCargo(ContainerBase *carrier)
Context createTestingContext(GameMap *gamemap)
Loki::Functor< GameMap *, LOKI_TYPELIST_1(const ASCString &) > MapLoadingFunction
Definition: loaders.h:57
static GameMap * loadmap(const ASCString &name)
Definition: loaders.cpp:1039
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
void testCargoMovementMax(Vehicle *veh, int movement)
int getPlayerView() const
the player which is currently viewing the map.
Definition: gamemap.cpp:1007
void next_turn(GameMap *gamemap, const NextTurnStrategy &nextTurnStrategy, MapDisplayInterface *display, int playerView)
GameMap * mapLoadingExceptionChecker(const ASCString &filename, MapLoadingFunction loader)
Definition: loaders.cpp:1539
Coordinate on the twodimensional map.
Definition: typen.h:202
GameAction * parentAction
Definition: context.h:38
GameMap * startMap(const ASCString &filename)
int viewingPlayer
Definition: context.h:40
signed char actplayer
the player who is currently making his moves (may be human or AI)
Definition: gamemap.h:232
bool successful() const
MapDisplayInterface * display
Definition: context.h:39
GameMap * gamemap
Definition: context.h:36
ActionContainer * actionContainer
Definition: context.h:41
void attack(Vehicle *veh, const MapCoordinate &target)
ActionContainer actions
Records all action that have been done for undo/redo purposes.
Definition: gamemap.h:390
Coordinate on the map including height.
Definition: typen.h:238
GameMap * getMap() const
The parent class of Vehicle and Building; The name Container originates from Battle Isle...
Definition: containerbase.h:40
const Cargo & getCargo() const
int getMovement(bool checkFuel=true, bool checkRF=true) const
returns the movement points the unit has left for this turn. CheckFuel should almost always be true...
Definition: vehicle.cpp:558
ReplayInfo * replayinfo
Definition: gamemap.h:396
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182