Advanced Strategic Command
strategy.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  strategy.cpp - description
3  -------------------
4  begin : Fri Mar 30 2001
5  copyright : (C) 2001 by Martin Bickel
6  email : bickel@asc-hq.org
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 
19 #include <iostream>
20 #include "ai_common.h"
21 
22 #include "../actions/moveunitcommand.h"
23 
24 
26 
27 AI::AiResult AI::strategy( void )
28 {
29  AiResult result;
30 
31  /* to prevent that many units try to rush for the same spot, we are keeping track at how many units
32  are going to which destination */
33  map<MapCoordinate,int> destinationCounter;
34 
35  int stratloop = 0;
36  AiResult localResult;
37  do {
38  localResult.unitsMoved = 0;
39  localResult.unitsWaiting = 0;
40  stratloop++;
41 
42  int counter = 0;
43 
44  vector<int> units;
45  for ( Player::VehicleList::iterator vi = getPlayer().vehicleList.begin(); vi != getPlayer().vehicleList.end(); ++vi )
46  units.push_back( (*vi)->networkid );
47 
48  for ( vector<int>::iterator vi = units.begin(); vi != units.end(); ++vi ) {
49  Vehicle* veh = getMap()->getUnit(*vi);
50  if ( veh ) {
51  ++counter;
52 
53  if ( unitsWorkedInTactics.find( veh) != unitsWorkedInTactics.end() )
54  continue;
55 
56  if ( veh->aiparam[ getPlayerNum() ]->getJob() == AiParameter::job_fight ) {
57  if ( veh->weapexist() && veh->aiparam[ getPlayerNum() ]->getTask() != AiParameter::tsk_tactics
59  /*
60  int orgmovement = veh->getMovement();
61  int orgxpos = veh->xpos ;
62  int orgypos = veh->ypos ;
63  */
64 
65  if ( veh->networkid == aiDebugInterruptHelper ) {
66  cout << "debug point hit with unit " << aiDebugInterruptHelper << " \n";
67  }
68 
69  if ( MoveUnitCommand::avail ( veh )) {
70  MapCoordinate3D dest;
71 
72  AI::Section* sec = sections.getBest ( 0, veh, &dest, true, false, &destinationCounter );
73  if ( sec ) {
74  if( stratloop < 3 )
75  destinationCounter[dest]+=1;
76 
77  int nwid = veh->networkid;
78  int movement = veh->getMovement();
79  moveUnit ( veh, dest, false, false );
80 
81  if ( getMap()->getUnit(nwid)) {
82  AiParameter& aip = *veh->aiparam[getPlayerNum()];
83 
84  aip.dest = dest;
85  if ( getMap()->getUnit(nwid)->getMovement() < movement )
86  localResult.unitsMoved++;
87  } else
88  localResult.unitsMoved++;
89  }
90  }
91  }
92  } else {
93  int nwid = veh->networkid;
94  if ( runUnitTask ( veh ) )
95  if ( getMap()->getUnit( nwid ) ) { // the unit still lives
96  if ( veh->aiparam[getPlayerNum()]->resetAfterJobCompletion )
97  veh->aiparam[getPlayerNum()]->reset( veh );
98  }
99  }
100 
101  displaymessage2("strategy loop %d ; moved unit %d / %d ... ", stratloop, counter, localResult.unitsMoved );
102 
103  checkKeys();
104  }
105  }
106  result += localResult;
107  } while ( localResult.unitsMoved );
108 
109  displaymessage2("strategy completed ... ");
110 
111  return result;
112 }
bool weapexist() const
Is the unit able to shoot ?
Definition: vehicle.cpp:796
GameMap * getMap(void)
returns the map this AI runson
Definition: ai.h:486
AiParameter * aiparam[8]
Definition: vehicle.h:182
int getPlayerNum(void)
returns the number of the player which is controlled by this ai
Definition: ai.h:490
VehicleList vehicleList
a list of all units
Definition: player.h:135
friend class Section
Definition: ai.h:448
Player & getPlayer(void)
Definition: ai.h:492
static bool avail(Vehicle *eht)
Vehicle * getUnit(int x, int y, int nwid)
Definition: gamemap.cpp:1215
All parameters the AI stores persistently about a unit.
void displaymessage2(const char *formatstring,...)
displays a message in the status line of ASC
Coordinate on the map including height.
Definition: typen.h:238
MapCoordinate3D dest
void reset(Vehicle *_unit)
Definition: gamemap.cpp:2141
int aiDebugInterruptHelper
Definition: strategy.cpp:25
int networkid
a unique identification of the unit that is used everywhere in ASC (and not only the network protocol...
Definition: vehicle.h:140
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