Selectively Passible for Some Pawns

Started by ancusohm, June 28, 2014, 08:46:12 PM

Previous topic - Next topic

ancusohm

I've borrowed a bit of code from the door.cs file that allows me to create new structures that block certain pawns (like all humaniod pawns for example).  Pawns that are not blocked move through it freely.

However, I've run into a problem where pawns that are blocked keep trying to path through the area that is blocking them, even though there is another route to the same location that is not blocked.

Is there some trick to get the pather for the pawn to recognize that the route is blocked? 

Do I need to alter the pather itself?  If so, how would I do that?  (I can't seem to find source code for the pathfinder.)

Any help would be greatly appreciated.  Thank you.

mrofa

Check fire class, thers a function there, "find path around me", it will recalculate path for that door
All i do is clutter all around.

ancusohm

Thanks for your help.

By "find path around me", did you mean the function RecalcPathsOnAndAroundMe() ?

I've tried playing around with that function, and I can't seem to get it to actually do anything.

Here is the definition of the function that I'm using in the thingclass
    private void RecalcPathsOnAndAroundMe()
    {
            Find.PathGrid.RecalculatePerceivedPathCostAt(Position);

    }

Here is where I actually call that function
    public override void SpawnSetup()
    {
        base.SpawnSetup();
        RecalcPathsOnAndAroundMe();
    }

Am I making some obvious mistake? 

mrofa

Yehh you either need to put that into ticker or recall it when door state change.
Since in spawn this will be only used once when they are build so each door state change will affect that.
Also just copy entire function RecalcPathsOnAndAroundMe
from fire to you code.

You can also try with your code just change Position  to base.Position, im not 100% sure how recalcpath work so i would still advise to copy entire function to your code :D
All i do is clutter all around.

Haplo

The normal function does nothing for you, sorry.
It only recalculates the info it already knows..

You have to recalculate the path costs for the cell it is on by yourself when ever you open or closes it.
I'll add it to my LockedDoors-Code in the next release.

If you wanna use it for your mod, you have to do the position calculations by yourself.
You can use this as a starting point for your code:
(This is the code from my locked doors in Miscellaneous after the changes. The SwicthLockState() is called whenever you press the button.)

        /// <summary>
        /// Switches the locked state
        /// </summary>
        /// <returns></returns>
        private void SwitchLockState()
        {
            locked = !locked;

            if (locked)
            {
                // Set path costs to too high
                Find.PathGrid.pathGrid[LocToIndex(Position)] = 10001;
            }
            else
            {
                // Recalc normal path cost
                Find.PathGrid.RecalculatePerceivedPathCostAt(Position);
            }
        }
        /// <summary>
        /// Needed function for calc path grid.
        /// Extracted from PathGrid.ResetPathGrid()
        /// and PathGrid.LocToIndex(...)
        /// </summary>
        /// <param name="loc"></param>
        /// <returns></returns>
        private int LocToIndex(IntVec3 loc)
        {
            // From .ResetPathGrid()
            int mapSizePowTwo = Find.Map.info.PowerOfTwoOverMapSize;
            ushort gridSizeX = (ushort)mapSizePowTwo;
            ushort gridSizeZ = (ushort)mapSizePowTwo;
            ushort gridSizeXMinus1 = (ushort)(gridSizeX - 1);
            ushort gridSizeZLog2 = (ushort)Math.Log((double)gridSizeZ, 2);

            // From .LocToIndex(...)
            return (loc.z << (gridSizeZLog2 & 31)) + loc.x;
        }

ancusohm

Thanks for posting that, Haplo.

But, looking at the code, it seems like that would set a ridiculously high pathcost for /all/ pawns.  I'm looking to make something impassible for only /some/ pawns.... If that's possible.

Tynan

Unfortunately if you're doing a variable path cost for some pawns only it has to be handled in the PathFinder, and there's no easy way to do that. The only case that does this currently is doors.
Tynan Sylvester - @TynanSylvester - Tynan's Blog