00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef unitctrlH
00023 #define unitctrlH
00024
00025 #include <vector>
00026 #include <map>
00027
00028 #include "typen.h"
00029 #include "mapalgorithms.h"
00030 #include "basestrm.h"
00031 #include "spfst.h"
00032 #include "attack.h"
00033 #include "astar2.h"
00034 #include "actions/servicing.h"
00035
00036
00042 #ifdef karteneditor
00043 #error The mapeditor does not need any pathfinding
00044 #endif
00045
00046
00047 extern SigC::Signal0<void> fieldCrossed;
00048
00049
00050 template<class T>
00051 class FieldList {
00052 GameMap* localmap;
00053 int fieldnum;
00054 std::vector<int> xpos;
00055 std::vector<int> ypos;
00056 std::vector<T> data;
00057 public:
00058 FieldList ( void );
00059 int getFieldNum ( void ) const;
00060 tfield* getField ( int num ) const;
00061 T& getData ( int num );
00062 T& getData ( int x, int y );
00063 void getFieldCoordinates ( int num, int* x, int* y ) const;
00064 MapCoordinate getFieldCoordinates ( int num ) const;
00065 void addField ( int x, int y, const T& _data );
00066 void addField ( const MapCoordinate& mc, const T& _data );
00067 void addField ( int x, int y );
00068 void addField ( const MapCoordinate& mc );
00069 void setMap ( GameMap* map );
00070 GameMap* getMap ( void );
00071 bool isMember ( int x, int y );
00072 bool isMember ( const MapCoordinate& mc );
00073 };
00074
00075
00076 typedef FieldList<int> IntFieldList;
00077 typedef FieldList<AttackWeap> AttackFieldList;
00078
00079
00080 typedef class PendingVehicleActions* PPendingVehicleActions;
00081
00082
00083 enum VehicleActionType { vat_nothing, vat_move, vat_ascent, vat_descent, vat_attack, vat_service, vat_newservice };
00084
00085 class VehicleAction {
00086 protected:
00087 PPendingVehicleActions pva;
00088 VehicleActionType actionType;
00089 public:
00090 virtual int getStatus( void ) = 0;
00091 virtual int available ( Vehicle* veh ) const = 0;
00092 virtual int execute ( Vehicle* veh, int x, int y, int step, int param1, int param2 ) = 0;
00093 virtual void registerPVA ( VehicleActionType _actionType, PPendingVehicleActions _pva );
00094 VehicleAction ( VehicleActionType _actionType, PPendingVehicleActions _pva );
00095 virtual ~VehicleAction ( );
00096 };
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 typedef int trichtungen[sidenum];
00118
00119 class MapDisplayInterface;
00120
00121 class BaseVehicleMovement : public VehicleAction {
00122 protected:
00123 MapDisplayInterface* mapDisplay;
00124 int status;
00125 public:
00126 BaseVehicleMovement ( VehicleActionType _actionType, PPendingVehicleActions _pva, MapDisplayInterface* md ) : VehicleAction ( _actionType, _pva ),mapDisplay(md), status(0), attackedByReactionFire(false) {};
00127 BaseVehicleMovement ( MapDisplayInterface* md ) : VehicleAction ( vat_move, NULL ),mapDisplay(md), status(0), attackedByReactionFire(false) {};
00128 AStar3D::Path path;
00129 int execute ( Vehicle* veh, int x, int y, int step, int height, int noInterrupt );
00130 bool attackedByReactionFire;
00131 Vehicle* getVehicle ( void ) { return vehicle; };
00132 void registerMapDisplay ( MapDisplayInterface* _mapDisplay ) { mapDisplay = _mapDisplay; };
00133 virtual int getStatus ( void ) { return status; };
00134 int available ( Vehicle* veh ) const;
00135
00136
00137 protected:
00138 Vehicle* vehicle;
00139
00140 int moveunitxy ( AStar3D::Path& pathToMove, int noInterrupt = -1 );
00141
00142 class PathFinder : public AStar3D {
00143 public:
00144 PathFinder ( GameMap* actmap, Vehicle* veh, int maxDistance ) : AStar3D(actmap, veh, false, maxDistance ) {};
00145
00150 void getMovementFields ( IntFieldList& reachableFields, IntFieldList& reachableFieldsIndirect, int height );
00151 };
00152 };
00153
00154
00155 class VehicleMovement : public BaseVehicleMovement {
00156 public:
00157 IntFieldList reachableFields;
00158 IntFieldList reachableFieldsIndirect;
00159 int available ( Vehicle* veh ) const { return status==0 && avail(veh); };
00160 static bool avail ( Vehicle* veh );
00161
00162 enum { NoInterrupt = 1, DisableHeightChange = 2 };
00163 int execute ( Vehicle* veh, int x, int y, int step, int height, int capabilities );
00164
00165 virtual void registerPVA ( VehicleActionType _actionType, PPendingVehicleActions _pva );
00166 VehicleMovement ( MapDisplayInterface* md, PPendingVehicleActions _pva = NULL );
00167 ~VehicleMovement ( );
00168 };
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 class ChangeVehicleHeight : public BaseVehicleMovement {
00194 int newheight;
00195 int dir;
00196 public:
00197 IntFieldList reachableFields;
00198 int execute ( Vehicle* veh, int x, int y, int step, int noInterrupt, int disableMovement );
00199 ChangeVehicleHeight ( MapDisplayInterface* md, PPendingVehicleActions _pva , VehicleActionType vat, int dir_ );
00200 };
00201
00202 class IncreaseVehicleHeight : public ChangeVehicleHeight {
00203 public:
00204 IncreaseVehicleHeight ( MapDisplayInterface* md, PPendingVehicleActions _pva = NULL );
00205 static bool avail ( Vehicle* veh );
00206 int available ( Vehicle* veh ) const;
00207 ~IncreaseVehicleHeight();
00208 };
00209
00210 class DecreaseVehicleHeight : public ChangeVehicleHeight {
00211 public:
00212 DecreaseVehicleHeight ( MapDisplayInterface* md, PPendingVehicleActions _pva = NULL );
00213 static bool avail ( Vehicle* veh );
00214 int available ( Vehicle* veh ) const;
00215 ~DecreaseVehicleHeight();
00216 };
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 class VehicleAttack : public VehicleAction {
00239 Vehicle* vehicle;
00240 int status;
00241 int kamikaze;
00242 class tsearchattackablevehicles : public SearchFields {
00243 VehicleAttack* va;
00244 public:
00245 int anzahlgegner;
00246 const Vehicle* angreifer;
00247 int kamikaze;
00248 void init ( const Vehicle* eht, int _kamikaze, VehicleAttack* _va );
00249 virtual void testfield ( const MapCoordinate& mc );
00250 int run ( void );
00251 tsearchattackablevehicles ( GameMap* _gamemap ) : SearchFields ( _gamemap ) {};
00252 } search;
00253
00254 protected:
00255 MapDisplayInterface* mapDisplay;
00256 public:
00257 AttackFieldList attackableVehicles;
00258 AttackFieldList attackableBuildings;
00259 AttackFieldList attackableObjects;
00260
00261 int getStatus( void ) { return status; };
00262 Vehicle* getAttacker() { return vehicle; };
00263 virtual int available ( Vehicle* veh ) const { return status==0 && avail(veh); };
00264 static bool avail( Vehicle* veh );
00265 virtual int execute ( Vehicle* veh, int x, int y, int step, int _kamikaze, int weapnum );
00266 virtual void registerPVA ( VehicleActionType _actionType, PPendingVehicleActions _pva );
00267 VehicleAttack ( MapDisplayInterface* md, PPendingVehicleActions _pva = NULL );
00268 virtual ~VehicleAttack ( );
00269 };
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 class VehicleService : public VehicleAction {
00289 Vehicle* vehicle;
00290 Building* building;
00291 int status;
00292
00293 public:
00294 class FieldSearch : public SearchFields {
00295 VehicleService& vs;
00296 Vehicle* veh;
00297 Building* bld;
00298 Resources buildingResources;
00299 Resources resourcesCapacity;
00300 public:
00301 struct {
00302 bool distance;
00303 bool height;
00304 } bypassChecks;
00305 virtual void testfield ( const MapCoordinate& mc );
00306 void checkVehicle2Vehicle ( Vehicle* veh, int xp, int yp );
00307 void checkBuilding2Vehicle ( Vehicle* veh );
00308 bool initrefuelling( int xp1, int yp1 );
00309 void init ( Vehicle* _veh, Building* _bld );
00310 void run ( );
00311 FieldSearch ( VehicleService& _vs, GameMap* _gamemap ) : SearchFields ( _gamemap ), vs ( _vs ) { bypassChecks.distance = false; bypassChecks.height = false; };
00312 } fieldSearch;
00313
00314
00315 protected:
00316 MapDisplayInterface* mapDisplay;
00317 public:
00318 Vehicle* getVehicle ( void ) { return vehicle; };
00319 Building* getBuilding ( void ) { return building; };
00320
00321 enum Service { srv_repair, srv_resource, srv_ammo };
00322 class Target {
00323 public:
00324 Vehicle* dest;
00325
00326 struct Service {
00327 VehicleService::Service type;
00328 int sourcePos;
00329 int targetPos;
00330 int curAmount;
00331 int maxAmount;
00332 int minAmount;
00333 int maxPercentage;
00334 int orgSourceAmount;
00335 };
00336 vector<Service> service;
00337 };
00338 typedef map<int,Target> TargetContainer;
00339 TargetContainer dest;
00340
00341 int getStatus( void ) { return status; };
00342 virtual int available ( Vehicle* veh ) const;
00343 static int avail ( const Vehicle* veh );
00344 static int getServices ( Vehicle* veh );
00345 int execute ( Vehicle* veh, int targetNWID, int dummy, int step, int pos, int amount );
00346 int fillEverything ( int targetNWID, bool repairsToo );
00347 virtual void registerPVA ( VehicleActionType _actionType, PPendingVehicleActions _pva );
00348 VehicleService ( MapDisplayInterface* md, PPendingVehicleActions _pva = NULL );
00349 virtual ~VehicleService ( );
00350
00352 int guimode;
00353 };
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378 #if 1
00379
00380 class NewVehicleService : public VehicleAction {
00381 public:
00382 typedef ServiceTargetSearcher::Targets Targets;
00383 ServiceTargetSearcher::Targets targets;
00384 private:
00385 ContainerBase* container;
00386 int status;
00387
00388
00389 protected:
00390 MapDisplayInterface* mapDisplay;
00391 public:
00392 ContainerBase* getContainer() { return container; };
00393
00394 int getStatus( void ) { return status; };
00395 virtual int available ( ContainerBase* veh ) const;
00396 virtual int available ( Vehicle* veh ) const;
00397 static int avail ( ContainerBase* veh );
00398 int executeContainer ( ContainerBase* veh, int x, int y, int step, int pos, int amount );
00399 int execute ( Vehicle* veh, int x, int y, int step, int pos, int amount );
00400
00401 bool targetAvail( const ContainerBase* target );
00402
00403 int fillEverything ( ContainerBase* target, bool repairsToo = false );
00404 virtual void registerPVA ( VehicleActionType _actionType, PPendingVehicleActions _pva );
00405 NewVehicleService ( MapDisplayInterface* md, PPendingVehicleActions _pva = NULL );
00406 virtual ~NewVehicleService ( );
00407
00409 int guimode;
00410 };
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432 #endif
00433
00434
00435
00436 class PendingVehicleActions {
00437 public:
00438 int actionType;
00439 PendingVehicleActions ( void );
00440 VehicleAction* action;
00441
00442 VehicleMovement* move;
00443 IncreaseVehicleHeight* ascent;
00444 DecreaseVehicleHeight* descent;
00445 VehicleAttack* attack;
00446 VehicleService* service;
00447 NewVehicleService* newservice;
00448 ~PendingVehicleActions ( );
00449 void reset();
00450 };
00451
00453 extern PendingVehicleActions pendingVehicleActions;
00454
00455
00456
00457
00458
00459
00460 template<class T> FieldList<T> :: FieldList ( void )
00461 {
00462 fieldnum = 0;
00463 localmap = NULL;
00464 }
00465
00466 template<class T> int FieldList<T> :: getFieldNum ( void ) const
00467 {
00468 return fieldnum;
00469 }
00470
00471 template<class T> tfield* FieldList<T> :: getField ( int num ) const
00472 {
00473 if ( num < fieldnum && num >= 0 )
00474 return getfield ( xpos[num], ypos[num] );
00475 else
00476 return NULL;
00477 }
00478
00479
00480 template<class T> T& FieldList<T> :: getData ( int num )
00481 {
00482 if ( num < fieldnum && num >= 0 )
00483 return data[num] ;
00484
00485 throw OutOfRange();
00486 }
00487
00488 template<class T> T& FieldList<T> :: getData ( int x, int y )
00489 {
00490 for ( int i = 0; i < fieldnum; i++ )
00491 if ( xpos[i] == x && ypos[i] == y )
00492 return data[i];
00493
00494 throw OutOfRange();
00495 }
00496
00497
00498 template<class T> void FieldList<T> :: getFieldCoordinates ( int num, int* x, int* y ) const
00499 {
00500 if ( num < fieldnum && num >= 0 ) {
00501 *x = xpos[num];
00502 *y = ypos[num];
00503 } else {
00504 *x = -1;
00505 *y = -1;
00506 }
00507 }
00508
00509 template<class T> MapCoordinate FieldList<T> :: getFieldCoordinates ( int num ) const
00510 {
00511 if ( num < fieldnum && num >= 0 ) {
00512 return MapCoordinate( xpos[num], ypos[num] );
00513 } else {
00514 return MapCoordinate( -1, -1 );
00515 }
00516 }
00517
00518
00519 template<class T> void FieldList<T> :: addField ( const MapCoordinate& mc, const T& _data )
00520 {
00521 addField ( mc.x, mc.y, _data );
00522 }
00523
00524 template<class T> void FieldList<T> :: addField ( int x, int y, const T& _data )
00525 {
00526 int found = 0;
00527 for( int i = 0; i < fieldnum; i++ )
00528 if ( xpos[i] == x && ypos[i] == y )
00529 found = 1;
00530 if ( !found ) {
00531 xpos.push_back ( x );
00532 ypos.push_back ( y );
00533 data.push_back( _data );
00534
00535 fieldnum++;
00536 }
00537 }
00538
00539
00540
00541 template<class T> void FieldList<T> :: addField ( const MapCoordinate& mc )
00542 {
00543 addField ( mc.x, mc.y );
00544 }
00545
00546 template<class T> void FieldList<T> :: addField ( int x, int y )
00547 {
00548 int found = 0;
00549 for( int i = 0; i < fieldnum; i++ )
00550 if ( xpos[i] == x && ypos[i] == y )
00551 found = 1;
00552 if ( !found ) {
00553 xpos.push_back ( x );
00554 ypos.push_back ( y );
00555 T t;
00556 data.push_back ( t );
00557 fieldnum++;
00558 }
00559 }
00560
00561
00562 template<class T> void FieldList<T> :: setMap ( GameMap* map )
00563 {
00564 localmap = map;
00565 }
00566
00567 template<class T> GameMap* FieldList<T> :: getMap ( void )
00568 {
00569 return localmap;
00570 }
00571
00572 template<class T> bool FieldList<T> :: isMember ( int x, int y )
00573 {
00574 for ( int i = 0; i < fieldnum; i++ )
00575 if ( xpos[i] == x && ypos[i] == y )
00576 return true;
00577 return false;
00578 }
00579
00580 template<class T> bool FieldList<T> :: isMember ( const MapCoordinate& mc )
00581 {
00582 for ( int i = 0; i < fieldnum; i++ )
00583 if ( xpos[i] == mc.x && ypos[i] == mc.y )
00584 return true;
00585 return false;
00586 }
00587
00588
00589 #endif
00590