function Utils::getValidOffsetTile(tile, offsetX, offsetY) {
local oldX = AIMap.GetTileX(tile);
local oldY = AIMap.GetTileY(tile);
local newX = oldX;
local newY = oldY;
if (offsetX > 0) {
for (local x = offsetX; x > 0; x--) {
if (AIMap.IsValidTile(AIMap.GetTileIndex(newX + 1, newY))) {
newX = newX + 1;
}
}
} else {
for (local x = offsetX; x < 0; x++) {
if (AIMap.IsValidTile(AIMap.GetTileIndex(newX - 1, newY))) {
newX = newX - 1;
}
}
}
if (offsetY > 0) {
for (local y = offsetY; y > 0; y--) {
if (AIMap.IsValidTile(AIMap.GetTileIndex(newX, newY + 1))) {
newY = newY + 1;
}
}
} else {
for (local y = offsetY; y < 0; y++) {
if (AIMap.IsValidTile(AIMap.GetTileIndex(newX, newY - 1))) {
newY = newY - 1;
}
}
}
return AIMap.GetTileIndex(newX, newY);
}