Bug #3847

AreRailTileConnect

Added by krinn over 7 years ago.

Status:NewStart date:2012-03-18
Priority:LowDue date:
Assignee:-% Done:

0%

Category:-
Target version:-

Description

This is the raw code, so some functions are missing:
cBuilder.GetDirection: return a direction comparing A with B tile, i'm sure you have one already in the lib

function cBuilder::GetRailBitMask(rails)
// Return a nibble bitmask with each NE,SW,NW,SE direction set to 1
{
local NE = 1; // we will use them as bitmask
local    SW = 2;
local    NW = 4;
local    SE = 8;
local trackMap=AIList();
trackMap.AddItem(AIRail.RAILTRACK_NE_SW,    NE + SW);    // AIRail.RAILTRACK_NE_SW
trackMap.AddItem(AIRail.RAILTRACK_NW_SE,    NW + SE);    // AIRail.RAILTRACK_NW_SE
trackMap.AddItem(AIRail.RAILTRACK_NW_NE,    NW + NE);    // AIRail.RAILTRACK_NW_NE
trackMap.AddItem(AIRail.RAILTRACK_SW_SE,    SW + SE);    // AIRail.RAILTRACK_SW_SE
trackMap.AddItem(AIRail.RAILTRACK_NW_SW,    NW + SW);    // AIRail.RAILTRACK_NW_SW
trackMap.AddItem(AIRail.RAILTRACK_NE_SE,    NE + SE);    // AIRail.RAILTRACK_NE_SE
if (rails==255)    return 0; // invalid rail
local railmask=0;
foreach (tracks, value in trackMap)
    {
    if ((rails & tracks)==tracks)    { railmask=railmask | value; }
    if (railmask==(NE+SW+NW+SE))    return railmask; // no need to test more tracks
    }
return railmask;
}

function cBuilder::AreRailTilesConnected(tilefrom, tileto)
// Look at tilefront and build rails to connect that tile to its neightbourg tiles that are us with rails
// tilefrom, tileto : tiles to check
// return true if you can walk from tilefrom to tileto
{
local atemp=AICompany.ResolveCompanyID(AICompany.COMPANY_SELF);
if (AITile.GetOwner(tilefrom) != atemp)    return false; // not own by us
if (AITile.GetOwner(tileto) != atemp)        return false; // not own by us
atemp=AIRail.GetRailType(tilefrom);
if (AIRail.GetRailType(tileto) != atemp)    return false; // not same railtype
local NE = 1; // we will use them as bitmask
local    SW = 2;
local    NW = 4;
local    SE = 8;
local direction=cBuilder.GetDirection(tilefrom, tileto);
local tilefrom_mask=cBuilder.GetRailBitMask(AIRail.GetRailTracks(tilefrom));
local tileto_mask=cBuilder.GetRailBitMask(AIRail.GetRailTracks(tileto));
local tilefrom_need, tileto_need=0;
switch (direction)
    {
    case    0: // SE-NW, it's easy, if we want go SE->N
        tilefrom_need=NW;
        tileto_need=SE;
        break;
    case    1: // NW-SE
        tilefrom_need=SE;
        tileto_need=NW;
        break;
    case    2: // SW-NE
        tilefrom_need=NE;
        tileto_need=SW;
        break;
    case    3: // NE-SW
        tilefrom_need=SW;
        tileto_need=NE;
        break;
    }
if (AIRail.IsRailDepotTile(tileto) && AIRail.GetRailDepotFrontTile(tileto)==tilefrom)    tileto_mask=tileto_need;
if (AIRail.IsRailDepotTile(tilefrom) && AIRail.GetRailDepotFrontTile(tilefrom)==tileto)    tilefrom_mask=tilefrom_need;
// if we have a depot, make it act like it is a classic rail if its entry match where we going or come from
if (AIBridge.IsBridgeTile(tileto) || AITunnel.IsTunnelTile(tileto))
    {
    local endat=null;
    endat=AIBridge.IsBridgeTile(tileto) ? AIBridge.GetOtherBridgeEnd(tileto) : AITunnel.GetOtherTunnelEnd(tileto);
    local jumpdir=cBuilder.GetDirection(tileto, endat);
    if (jumpdir == direction) // if the bridge/tunnel goes the same direction, then consider it a plain rail
        {
        tileto_mask=tileto_need;
        }
     }
if (AIBridge.IsBridgeTile(tilefrom) || AITunnel.IsTunnelTile(tilefrom))
    {
    local endat=null;
    endat=AIBridge.IsBridgeTile(tilefrom) ? AIBridge.GetOtherBridgeEnd(tilefrom) : AITunnel.GetOtherTunnelEnd(tilefrom);
    local jumpdir=cBuilder.GetDirection(endat, tilefrom); // reverse direction to find the proper one
    if (jumpdir == direction) // if the bridge/tunnel goes the same direction, then consider it a plain rail
        {
        tilefrom_mask=tilefrom_need;
        }
     }
if ( (tilefrom_mask & tilefrom_need)==tilefrom_need && (tileto_mask & tileto_need)==tileto_need)    return true;
return false;
}

Also available in: Atom PDF