public ushort DoAutoRoute(Compact cpt)
{
var cptScreen = (byte)cpt.Core.screen;
var cptWidth = (byte)SkyCompact.GetMegaSet(cpt).gridWidth;
InitWalkGrid(cptScreen, cptWidth);
byte startX, startY, destX, destY;
short initStaX, initStaY, initDestX, initDestY;
ClipCoordX(cpt.Core.xcood, out startX, out initStaX);
ClipCoordY(cpt.Core.ycood, out startY, out initStaY);
ClipCoordX(cpt.Core.arTargetX, out destX, out initDestX);
ClipCoordY(cpt.Core.arTargetY, out destY, out initDestY);
var raw = _skyCompact.FetchCptRaw(cpt.Core.animScratchId);
Array.Clear(raw, 0, 64);
var routeDest = new UShortAccess(raw, 0);
if ((startX == destX) && (startY == destY))
return 2;
var routeGrid = new UShortAccess(_routeGrid, 0);
if (routeGrid[(destY + 1) * RouteGridWidth + destX + 1] != 0)
{
//if ((cpt == &Sky::SkyCompact::foster) && (cptScreen == 12) && (destX == 2) && (destY == 14)) {
if (_skyCompact.CptIsId(cpt, (ushort)CptIds.Foster) && (cptScreen == 12) && (destX == 2) &&
(destY == 14))
{
/* workaround for Scriptbug #1043047
In screen 12 (the pipe factory) Joey can block Foster's target
coordinates (2/14). This is normally not too tragic, but in the
scene when foster gets thrown out by Lamb (first time you enter
the pipe factory), the game would enter an infinite loop. */
routeGrid[(destY + 1) * RouteGridWidth + destX + 1] = 0;
// hide this part joey from the grid
}
else
return 1; // AR destination is an unaccessible block
}
if (!CalcWalkGrid(startX, startY, destX, destY))
return 1; // can't find route to block
var routeData = MakeRouteData(destX, destY);
// the route is done.
// if there was an initial x movement (due to clipping) tag it onto the start
routeData = CheckInitMove(routeData, initStaX);
byte cnt = 0;
do
{
routeDest[cnt] = routeData[cnt];
routeDest[cnt + 1] = routeData[cnt + 1];
cnt += 2;
} while (routeData[cnt - 2] != 0);
return 0;
}