double GatewayPathfindingStateSpaceMP::shortestDistanceToGate(Tile tile, Gateway gateway) { Tile gateStart = gateway.getStart(); Tile gateEnd = gateway.getEnd(); int deltaX = 0; int deltaY = 0; if(gateStart.x == gateEnd.x) { //Vertical gate int higherY, lowerY; if(gateStart.y > gateEnd.y) { higherY = gateStart.y; lowerY = gateEnd.y; } else { higherY = gateEnd.y; lowerY = gateStart.y; } if(tile.y > higherY) { deltaY = tile.y - higherY; } else if(tile.y < lowerY) { deltaY = lowerY - tile.y; } deltaX = tile.x - gateStart.x; } else if(gateStart.y == gateEnd.y) { //Horizontal gate int higherX, lowerX; if(gateStart.x > gateEnd.x) { higherX = gateStart.x; lowerX = gateEnd.x; } else { higherX = gateEnd.x; lowerX = gateStart.x; } if(tile.x > higherX) { deltaX = tile.x - higherX; } else if(tile.x < lowerX) { deltaX = lowerX - tile.x; } deltaY = tile.y - gateStart.y; } else { //Diagonal gate (always 45 degrees) int higherX, lowerX; int higherY, lowerY; bool backslashGate = false; if(gateStart.y > gateEnd.y) { higherY = gateStart.y; lowerY = gateEnd.y; if(gateStart.x > gateEnd.x) { higherX = gateStart.x; lowerX = gateEnd.x; backslashGate = false; } else { higherX = gateEnd.x; lowerX = gateStart.x; backslashGate = true; } } else { higherY = gateEnd.y; lowerY = gateStart.y; if(gateStart.x > gateEnd.x) { higherX = gateStart.x; lowerX = gateEnd.x; backslashGate = true; } else { higherX = gateEnd.x; lowerX = gateStart.x; backslashGate = false; } } if(backslashGate) { if(tile.y > (higherY - (lowerX - tile.x))) { deltaX = tile.x - lowerX; deltaY = tile.y - higherY; } else if(tile.y < (lowerY - (higherX - tile.x))) { deltaX = higherX - tile.x; deltaY = lowerY - tile.y; } else { deltaX = deltaY = ( (tile.x - lowerX) + (tile.x - higherX) + (tile.y - lowerY) + (tile.y - higherY) ) / 4; } } else { if(tile.y > (higherY - (tile.x - higherX))) { deltaX = tile.x - higherX; deltaY = tile.y - higherY; } else if(tile.y < (lowerY - (tile.x - lowerX))) { deltaX = lowerX - tile.x; deltaY = lowerY - tile.y; } else { deltaX = deltaY = ( (tile.x - lowerX) + (tile.x - higherX) + (lowerY - tile.y) + (higherY - tile.y) ) / 4; } } } return octileDistance(deltaX, deltaY); }