1 /*************************************************************************** 2 * Mechanized Assault and Exploration Reloaded Projectfile * 3 * * 4 * This program is free software; you can redistribute it and/or modify * 5 * it under the terms of the GNU General Public License as published by * 6 * the Free Software Foundation; either version 2 of the License, or * 7 * (at your option) any later version. * 8 * * 9 * This program is distributed in the hope that it will be useful, * 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 * GNU General Public License for more details. * 13 * * 14 * You should have received a copy of the GNU General Public License * 15 * along with this program; if not, write to the * 16 * Free Software Foundation, Inc., * 17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 18 ***************************************************************************/ 19 20 #ifndef game_logic_movejobsH 21 #define game_logic_movejobsH 22 23 #include <SDL.h> 24 #include <vector> 25 26 #include "utility/signal/signal.h" 27 #include "utility/position.h" 28 29 class cClient; 30 class cMap; 31 class cNetMessage; 32 class cServer; 33 class cUnit; 34 class cVehicle; 35 36 /* Size of a memory block while pathfinding */ 37 #define MEM_BLOCK_SIZE 10 38 39 enum MJOB_TYPES 40 { 41 MJOB_OK, 42 MJOB_STOP, 43 MJOB_FINISHED, 44 MJOB_BLOCKED 45 }; 46 47 // structures for the calculation of the path 48 struct sWaypoint 49 { 50 sWaypoint* next; 51 cPosition position; 52 int Costs; 53 }; 54 55 /* node structure for pathfinding */ 56 struct sPathNode 57 { 58 /* x and y coords */ 59 cPosition position; 60 /* the difrent cost types */ 61 int costF, costG, costH; 62 /* previous and next node of this one in the hole path */ 63 sPathNode* prev, *next; 64 65 /* the costs to enter on this field */ 66 int fieldCosts; 67 }; 68 69 enum ePathDestinationTypes 70 { 71 PATH_DEST_TYPE_POS, 72 PATH_DEST_TYPE_LOAD, 73 PATH_DEST_TYPE_ATTACK 74 }; 75 76 class cPathDestHandler 77 { 78 ePathDestinationTypes type; 79 80 const cVehicle* srcVehicle; 81 82 const cUnit* destUnit; 83 cPosition destination; 84 public: 85 cPathDestHandler (ePathDestinationTypes type_, const cPosition& destination, const cVehicle* srcVehicle_, const cUnit* destUnit_); 86 87 bool hasReachedDestination (const cPosition& position) const; 88 int heuristicCost (const cPosition& source) const; 89 }; 90 91 class cPathCalculator 92 { 93 void init (const cPosition& source, const cMap& Map, const cVehicle& Vehicle, const std::vector<cVehicle*>* group); 94 95 public: 96 cPathCalculator (const cPosition& source, const cPosition& destination, const cMap& Map, const cVehicle& Vehicle, const std::vector<cVehicle*>* group = nullptr); 97 cPathCalculator (const cPosition& source, const cUnit& destUnit, const cMap& Map, const cVehicle& Vehicle, bool load); 98 cPathCalculator (const cPosition& source, const cMap& Map, const cVehicle& Vehicle, const cPosition& attack); 99 ~cPathCalculator(); 100 101 /** 102 * calculates the best path in costs and length 103 *@author alzi alias DoctorDeath 104 */ 105 sWaypoint* calcPath(); 106 /** 107 * calculates the costs for moving from the source- to the destinationfield 108 *@author alzi alias DoctorDeath 109 */ 110 int calcNextCost (const cPosition& source, const cPosition& destination) const; 111 112 /* the map on which the path will be calculated */ 113 const cMap* Map; 114 /* the moving vehicle */ 115 const cVehicle* Vehicle; 116 /* if more then one vehicle is moving in a group this is the list of all moving vehicles */ 117 const std::vector<cVehicle*>* group; 118 /* source and destination coords */ 119 cPosition source; 120 bool bPlane, bShip; 121 cPathDestHandler* destHandler; 122 123 124 private: 125 /* the waypoints of the found path*/ 126 sWaypoint* Waypoints; 127 /* memoryblocks for the nodes */ 128 sPathNode** MemBlocks; 129 /* number of blocks */ 130 int blocknum; 131 /* restsize of the last block */ 132 int blocksize; 133 134 /* heaplist where all nodes are sorted by there costF value */ 135 std::vector<sPathNode*> nodesHeap; 136 /* open nodes map */ 137 std::vector<sPathNode*> openList; 138 /* closed nodes map */ 139 std::vector<sPathNode*> closedList; 140 /* number of nodes saved on the heaplist; equal to number of nodes in the openlist */ 141 int heapCount; 142 /** 143 * expands the nodes around the overgiven one 144 *@author alzi alias DoctorDeath 145 */ 146 void expandNodes (sPathNode* Node); 147 /** 148 * returns a pointer to allocated memory and allocets a new block in memory if necessary 149 *@author alzi alias DoctorDeath 150 */ 151 sPathNode* allocNode(); 152 /** 153 * inserts a node into the heaplist and sets it to the right position by its costF value. 154 *@author alzi alias DoctorDeath 155 */ 156 void insertToHeap (sPathNode* Node, bool exists); 157 /** 158 * deletes the first node in the heaplist and resorts the rest. 159 *@author alzi alias DoctorDeath 160 */ 161 void deleteFirstFromHeap(); 162 }; 163 164 165 enum eEndMoveActionType 166 { 167 EMAT_LOAD, 168 EMAT_GET_IN, 169 EMAT_ATTACK 170 }; 171 172 class cEndMoveAction 173 { 174 public: 175 cVehicle* vehicle_; 176 eEndMoveActionType type_; 177 int destID_; //we store the ID and not a pointer to vehicle/building, 178 //so we don't have to invalidate the pointer, when the dest unit gets destroyed 179 180 private: 181 void executeLoadAction (cServer& server); 182 void executeGetInAction (cServer& server); 183 void executeAttackAction (cServer& server); 184 185 public: 186 cEndMoveAction (cVehicle* vehicle, int destID, eEndMoveActionType type); 187 188 void execute (cServer& server); 189 }; 190 191 class cServerMoveJob 192 { 193 cServer* server; 194 public: 195 cServerMoveJob (cServer& server, const cPosition& source, const cPosition& destination, cVehicle* Vehicle); 196 ~cServerMoveJob(); 197 198 cMap* Map; 199 cVehicle* Vehicle; 200 201 cPosition source; 202 cPosition destination; 203 bool bFinished; 204 bool bEndForNow; 205 int iNextDir; 206 int iSavedSpeed; 207 bool bPlane; 208 cEndMoveAction* endAction; 209 210 sWaypoint* Waypoints; 211 212 static cServerMoveJob* generateFromMessage (cServer& server, cNetMessage& message); 213 214 bool calcPath(); 215 void release(); 216 bool checkMove(); 217 void moveVehicle(); 218 void doEndMoveVehicle(); 219 void calcNextDir(); 220 void stop(); 221 void resume(); 222 void addEndAction (int destID, eEndMoveActionType type); 223 }; 224 225 class cClientMoveJob 226 { 227 cClient* client; 228 229 void init (const cPosition& source, cVehicle* Vehicle); 230 public: 231 static sWaypoint* calcPath (const cMap& map, const cPosition& source, const cPosition& destination, const cVehicle& vehicle, const std::vector<cVehicle*>* group = nullptr); 232 233 cClientMoveJob (cClient& client_, const cPosition& source, const cPosition& destination, cVehicle* Vehicle); 234 ~cClientMoveJob(); 235 cMap* Map; 236 cVehicle* Vehicle; 237 cEndMoveAction* endMoveAction; 238 239 cPosition source; 240 cPosition destination; 241 bool bFinished; 242 bool bEndForNow; 243 bool bSuspended; 244 int iNextDir; 245 int iSavedSpeed; 246 bool bPlane; 247 248 sWaypoint* Waypoints; 249 250 bool generateFromMessage (cNetMessage& message); 251 252 void release(); 253 void handleNextMove (int iType, int iSavedSpeed); 254 void moveVehicle(); 255 void doEndMoveVehicle(); 256 void calcNextDir(); 257 void drawArrow (SDL_Rect Dest, SDL_Rect* LastDest, bool bSpezial) const; 258 259 // TODO: check when this signal get triggered 260 mutable cSignal<void (const cVehicle&)> activated; 261 mutable cSignal<void (const cVehicle&)> stopped; 262 mutable cSignal<void (const cVehicle&)> moved; 263 mutable cSignal<void (const cVehicle&)> blocked; 264 }; 265 266 #endif // game_logic_movejobsH 267