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

Function Script_Unit_CalculateRoute

src/script/unit.c:1296–1353  ·  view source on GitHub ↗

* Calculate the route to a tile. * * Stack: 1 - An encoded tile to calculate the route to. * * @param script The script engine to operate on. * @return 0 if we arrived on location, 1 otherwise. */

Source from the content-addressed store, hash-verified

1294 * @return 0 if we arrived on location, 1 otherwise.
1295 */
1296uint16 Script_Unit_CalculateRoute(ScriptEngine *script)
1297{
1298 Unit *u;
1299 uint16 encoded;
1300 uint16 packedSrc;
1301 uint16 packedDst;
1302
1303 u = g_scriptCurrentUnit;
1304 encoded = STACK_PEEK(1);
1305
1306 if (u->currentDestination.x != 0 || u->currentDestination.y != 0 || !Tools_Index_IsValid(encoded)) return 1;
1307
1308 packedSrc = Tile_PackTile(u->o.position);
1309 packedDst = Tools_Index_GetPackedTile(encoded);
1310
1311 if (packedDst == packedSrc) {
1312 u->route[0] = 0xFF;
1313 u->targetMove = 0;
1314 return 0;
1315 }
1316
1317 if (u->route[0] == 0xFF) {
1318 Pathfinder_Data res;
1319 uint8 buffer[42];
1320
1321 res = Script_Unit_Pathfinder(packedSrc, packedDst, buffer, 40);
1322
1323 memcpy(u->route, res.buffer, min(res.routeSize, 14));
1324
1325 if (u->route[0] == 0xFF) {
1326 u->targetMove = 0;
1327 if (u->o.type == UNIT_SANDWORM) {
1328 script->delay = 720;
1329 }
1330 }
1331 } else {
1332 uint16 distance;
1333
1334 distance = Tile_GetDistancePacked(packedDst, packedSrc);
1335 if (distance < 14) u->route[distance] = 0xFF;
1336 }
1337
1338 if (u->route[0] == 0xFF) return 1;
1339
1340 if (u->orientation[0].current != (int8)(u->route[0] * 32)) {
1341 Unit_SetOrientation(u, (int8)(u->route[0] * 32), false, 0);
1342 return 1;
1343 }
1344
1345 if (!Unit_StartMovement(u)) {
1346 u->route[0] = 0xFF;
1347 return 0;
1348 }
1349
1350 memmove(&u->route[0], &u->route[1], 13);
1351 u->route[13] = 0xFF;
1352 return 1;
1353}

Callers

nothing calls this directly

Calls 6

Tools_Index_IsValidFunction · 0.85
Script_Unit_PathfinderFunction · 0.85
Tile_GetDistancePackedFunction · 0.85
Unit_SetOrientationFunction · 0.85
Unit_StartMovementFunction · 0.85

Tested by

no test coverage detected