File: moveunitcommand.cpp

package info (click to toggle)
asc 2.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 75,080 kB
  • ctags: 24,943
  • sloc: cpp: 155,023; sh: 8,829; ansic: 6,890; makefile: 650; perl: 138
file content (364 lines) | stat: -rw-r--r-- 10,956 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
     This file is part of Advanced Strategic Command; http://www.asc-hq.de
     Copyright (C) 1994-2008  Martin Bickel  and  Marc Schellenberger
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
 
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.
 
     You should have received a copy of the GNU General Public License
     along with this program; see the file COPYING. If not, write to the 
     Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
     Boston, MA  02111-1307  USA
*/

#include <iostream>

#include "moveunitcommand.h"

#include "../vehicle.h"
#include "../mapfield.h"
#include "../gamemap.h"
#include "../viewcalculation.h"
#include "../spfst.h"
#include "../mapdisplayinterface.h"
#include "vehicleattack.h"
#include "action-registry.h"
#include "moveunit.h"


bool MoveUnitCommand :: avail ( Vehicle* eht )
{
   
   if ( eht ) 
      return eht->canMove();
   
   return false;
}


bool MoveUnitCommand :: ascendAvail ( Vehicle* veh )
{
   if ( veh )
      if ( veh->getHeightChange( +1 ))
         return true;
   return false;
}

bool MoveUnitCommand :: descendAvail ( Vehicle* veh )
{
   if ( veh )
      if ( veh->getHeightChange( -1 ))
         return true;
   
   return false;
}



MoveUnitCommand :: MoveUnitCommand ( Vehicle* unit )
   : UnitCommand ( unit ), flags(0), verticalDirection(0)
{
   
}


class HeightChangeLimitation: public AStar3D::OperationLimiter {
         bool allow_Height_Change;
      public:
         HeightChangeLimitation ( bool allow_Height_Change_ ) : allow_Height_Change ( allow_Height_Change_ ) {};
         virtual bool allowHeightChange() { return allow_Height_Change; };
         virtual bool allowMovement() { return true; };
         virtual bool allowEnteringContainer() { return true; };
         virtual bool allowLeavingContainer() { return true; };
         virtual bool allowDocking() { return true; };
};

class MovementLimitation: public AStar3D::OperationLimiter {
         bool simpleMode;
         int hcNum;
      public:
         MovementLimitation ( bool simpleMode_ ) : simpleMode ( simpleMode_ ), hcNum(0) {};
         virtual bool allowHeightChange() { ++hcNum; if ( simpleMode) return hcNum <= 1 ; else return true; };
         virtual bool allowMovement() { return !simpleMode; };
         virtual bool allowEnteringContainer() { return true; };
         virtual bool allowLeavingContainer() { return true; };
         virtual bool allowDocking() { return true; };
};

      
class PathFinder : public AStar3D {
   public:
      PathFinder ( Vehicle* veh, int maxDistance ) : AStar3D(veh->getMap(), veh, false, maxDistance ) {};

      /** searches for all fields that are within the range of maxDist and marks them.
         On each field one bit for each level of height will be set.
         The Destructor removes all marks.
      */
      void getMovementFields ( set<MapCoordinate3D>& reachableFields, set<MapCoordinate3D>& reachableFieldsIndirect, int height );
};
      
      
void PathFinder :: getMovementFields ( set<MapCoordinate3D>& reachableFields, set<MapCoordinate3D>& reachableFieldsIndirect, int height )
{
   Path dummy;
   findPath ( dummy, MapCoordinate3D(-1, -1, veh->height) );  //this field does not exist...

   int unitHeight = veh->getPosition().getNumericalHeight();
   if ( !this->actmap->getField ( veh->getPosition())->unitHere ( veh ))
      unitHeight = -1;

   // there are different entries for the same x/y coordinate but different height.
   // Since the UI is only in xy, we need to find the height which is the easiest to reach
   typedef multimap<MapCoordinate,Container::iterator > Fields;
   Fields fields;
   int orgHeight=-1;
   int minMovement = maxint;
   for ( Container::iterator i = visited.begin(); i != visited.end(); ++i ) {
      if ( i->h.x != veh->getPosition().x || i->h.y != veh->getPosition().y || i->h.getNumericalHeight() != unitHeight ) {
         int h = i->h.getNumericalHeight();
         // if ( h == -1 )
         //   h = i->enterHeight;
         if ( h == -1 || height == -1 || h == height ) {
            if ( i->canStop )
               fields.insert(make_pair(MapCoordinate(i->h),  i));
            else
               reachableFieldsIndirect.insert( i->h );
         }
      }
      if ( i->h.getNumericalHeight() >= 0 )
         if ( i->gval < minMovement ) {
            orgHeight = i->h.getNumericalHeight();
            minMovement = int (i->gval);
         }
   }
   for ( Fields::iterator i = fields.begin(); i != fields.end();  ) {
      int height = i->second->h.getNumericalHeight();
      int move = int(i->second->gval);
      Fields::key_type key = i->first;
      ++i;
      while ( i != fields.end() && i->first == key ) {
         if ( i->second->gval  < move || ( i->second->gval == move && abs(i->second->h.getNumericalHeight()-orgHeight) < abs(height-orgHeight) ))
            height = i->second->h.getNumericalHeight();
         ++i;
      }
      MapCoordinate3D f;
      f.setnum( key.x, key.y, height );
      reachableFields.insert ( f );
   }
}
      

ActionResult MoveUnitCommand::searchFields(int height, int capabilities)
{
   Vehicle* veh = getUnit();
   if ( !veh ) 
      return ActionResult(101);

   int h;
   if ( getMap()->getField(veh->getPosition())->unitHere(veh) )
      h = log2(veh->height); // != height-change: true
   else 
      h = -1; // height-change = false
      
   if ( height == -2  )
      h = -1;

   if ( (capabilities | flags) & LimitVerticalDirection ) {
      if ( getVerticalDirection() == 0 ) {
         HeightChangeLimitation hcl ( !(capabilities & DisableHeightChange) );
         PathFinder pf ( veh, veh->getMovement() );
         pf.registerOperationLimiter( &hcl );
         pf.getMovementFields ( reachableFields, reachableFieldsIndirect, h );
      } else {
         const Vehicletype::HeightChangeMethod* hcm = veh->getHeightChange( getVerticalDirection() );
         if ( !hcm )
            fatalError ( "Inconsistent call to changeheight ");
   
         h = veh->getPosition().getNumericalHeight() + hcm->heightDelta;
         
         MovementLimitation ml ( capabilities & ShortestHeightChange );
         PathFinder pf ( veh, veh->getMovement() );
         pf.registerOperationLimiter( &ml );
         pf.getMovementFields ( reachableFields, reachableFieldsIndirect, h );
      }
   } else {
      PathFinder pf ( veh, veh->getMovement() );
      pf.getMovementFields ( reachableFields, reachableFieldsIndirect, -1 );
   }

   if ( reachableFields.size() == 0 ) 
      return ActionResult(107);
   
   setState( Evaluated );
   
   return ActionResult(0);
}



void MoveUnitCommand :: setDestination( const MapCoordinate3D& destination )
{
   this->destination = destination;
   setState( SetUp );
}

void MoveUnitCommand :: setDestination( const MapCoordinate& destination )
{
   if ( getState() != Evaluated )
      searchFields();
   
   for ( set<MapCoordinate3D>::iterator i = reachableFields.begin(); i != reachableFields.end(); ++i )
      if ( destination.x == i->x && destination.y == i->y ) {
         this->destination = *i;
         break;
      }
   setState( SetUp );
}

bool MoveUnitCommand::isFieldReachable( const MapCoordinate& pos, bool direct )
{
   if ( direct ) {
      for ( set<MapCoordinate3D>::iterator i = reachableFields.begin(); i != reachableFields.end(); ++i )
         if ( i->x == pos.x && i->y == pos.y )
            return true;
   } else {
      for ( set<MapCoordinate3D>::iterator i = reachableFieldsIndirect.begin(); i != reachableFieldsIndirect.end(); ++i )
         if ( i->x == pos.x && i->y == pos.y )
            return true;
   }
   return false;  
}

bool MoveUnitCommand::isFieldReachable3D( const MapCoordinate3D& pos, bool direct )
{
   AStar3D ast ( getMap(), getUnit(), false, getUnit()->getMovement() );
   AStar3D::Path localPath;
   ast.findPath ( localPath, pos );
   return !localPath.empty();
}


void MoveUnitCommand::calcPath()
{
   path.clear();
   AStar3D ast ( getMap(), getUnit(), false, getUnit()->getMovement() );
   ast.findPath ( path, destination );
}

const AStar3D::Path& MoveUnitCommand::getPath()
{
   return path;  
}


ActionResult MoveUnitCommand::go ( const Context& context )
{
   if ( getState() != SetUp )
      return ActionResult(22000);
   
   /*
   searchFields();
   
   if ( reachableFields.find( destination ) == reachableFields.end() ) 
      return ActionResult(105);
*/
   if ( !getUnit() )
      return ActionResult(21001);
   
   calcPath();
   
   if ( path.empty() || path.rbegin()->x != destination.x || path.rbegin()->y != destination.y  ) 
      return ActionResult( 105 );
   
   int nwid = getUnit()->networkid;

   if ( context.display )
      context.display->startAction();
   
   ActionResult res = (new MoveUnit(getUnit(), path, flags&NoInterrupt))->execute(context);
   
   if ( context.display )
      context.display->stopAction();


   int destDamage;
   if ( getMap()->getUnit( nwid ))
      destDamage = getUnit()->damage;
   else
      destDamage = 100;

   if ( res.successful() )
      setState( Completed );
   else
      setState( Failed );
   
   return res;

}

void MoveUnitCommand :: setVerticalDirection( int dir )
{
   verticalDirection = dir;
   flags |= LimitVerticalDirection;  
}


static const int moveCommandVersion = 1;

void MoveUnitCommand :: readData ( tnstream& stream )
{
   UnitCommand::readData( stream );
   int version = stream.readInt();
   if ( version > moveCommandVersion )
      throw tinvalidversion ( "MoveCommand", moveCommandVersion, version );
   destination.read( stream );
   
   flags = stream.readInt( );
   verticalDirection = stream.readInt();
}

void MoveUnitCommand :: writeData ( tnstream& stream ) const
{
   UnitCommand::writeData( stream );
   stream.writeInt( moveCommandVersion );
   destination.write( stream );
   stream.writeInt( flags );
   stream.writeInt( verticalDirection );
}

ASCString MoveUnitCommand :: getCommandString() const {
   ASCString c;
   c.format("unitMovement ( map, %d, asc.MapCoordinate( %d, %d), %d )", getUnitID(), destination.x, destination.y, destination.getNumericalHeight() );
   return c;
}

GameActionID MoveUnitCommand::getID() const 
{
   return ActionRegistry::MoveUnitCommand;   
}

ASCString MoveUnitCommand::getDescription() const
{
   ASCString s = "Move unit"; 
   if ( getUnit() ) {
      s += " " + getUnit()->getName();
   }
   s += " to " + destination.toString();
   return s;
}


namespace {
   const bool r1 = registerAction<MoveUnitCommand> ( ActionRegistry::MoveUnitCommand );
}