MCPcopy Create free account
hub / github.com/OpenDUNE/OpenDUNE / Script_Unit_Pathfinder

Function Script_Unit_Pathfinder

src/script/unit.c:1183–1286  ·  view source on GitHub ↗

* Try to find a path between two points. * * @param packedSrc The start point. * @param packedDst The end point. * @param buffer The buffer to store the route in. * @param bufferSize The size of the buffer. * @return A struct with information about the found route. */

Source from the content-addressed store, hash-verified

1181 * @return A struct with information about the found route.
1182 */
1183static Pathfinder_Data Script_Unit_Pathfinder(uint16 packedSrc, uint16 packedDst, void *buffer, int16 bufferSize)
1184{
1185 uint16 packedCur;
1186 Pathfinder_Data res;
1187
1188 res.packed = packedSrc;
1189 res.score = 0;
1190 res.routeSize = 0;
1191 res.buffer = buffer;
1192
1193 res.buffer[0] = 0xFF;
1194
1195 bufferSize--;
1196
1197 packedCur = packedSrc;
1198 while (res.routeSize < bufferSize) {
1199 uint8 direction;
1200 uint16 packedNext;
1201 int16 score;
1202
1203 if (packedCur == packedDst) break;
1204
1205 /* Try going directly to the destination tile */
1206 direction = Tile_GetDirectionPacked(packedCur, packedDst) / 32;
1207 packedNext = packedCur + s_mapDirection[direction];
1208
1209 /* Check for valid movement towards the tile */
1210 score = Script_Unit_Pathfind_GetScore(packedNext, direction);
1211 if (score <= 255) {
1212 res.buffer[res.routeSize++] = direction;
1213 res.score += score;
1214 } else {
1215 uint8 dir;
1216 bool foundCounterclockwise = false;
1217 bool foundClockwise = false;
1218 int16 routeSize;
1219 Pathfinder_Data routes[2];
1220 uint8 routesBuffer[2][102];
1221 Pathfinder_Data *bestRoute;
1222
1223 while (true) {
1224 if (packedNext == packedDst) break;
1225
1226 /* Find the first valid tile on the (direct) route. */
1227 dir = Tile_GetDirectionPacked(packedNext, packedDst) / 32;
1228 packedNext += s_mapDirection[dir];
1229 if (Script_Unit_Pathfind_GetScore(packedNext, dir) > 255) continue;
1230
1231 /* Try to find a connection between our last valid tile and the new valid tile */
1232 routes[1].packed = packedCur;
1233 routes[1].score = 0;
1234 routes[1].routeSize = 0;
1235 routes[1].buffer = routesBuffer[0];
1236 foundCounterclockwise = Script_Unit_Pathfinder_Connect(packedNext, &routes[1], -1, direction);
1237
1238 routes[0].packed = packedCur;
1239 routes[0].score = 0;
1240 routes[0].routeSize = 0;

Callers 1

Tested by

no test coverage detected