DesignationDragger.DragSquares issue [SOLVED]

Started by pawnstorm, May 17, 2014, 06:03:18 PM

Previous topic - Next topic

pawnstorm

I tried to use Find.MainTabsRoot.tabArchitect.selectedPanel.dragger.DragSquares in a custom placement restricter (after checking dragger.dragging ofcourse) and it just crashes to desktop whenever I try to do anything with those squares.

My theory is it doesn't like it when startDragSquare equals Gen.MouseWorldSquare(). Unfortunately, startDragSquare is private so there's no way for me to handle that condition.

Edit: Oh wait, actually I think it crashes to desktop because DragSquares calls CanDesignateAt, which in turn calls my placement restricter, which calls DragSquares again, resulting in a never ending recursion... ok, so I guess this isn't a bug, but it would still be nice to be able to use the dragger in placement restricters. Hmm, maybe I can come up with a solution if I watch Inception again :P.

Edit2: I came up with a solution btw, without even having to sit through Inception again :). There's probably some room for optimization, but this works:


    public class PlacementRestricter_NoWaffle : PlacementRestricter
    {
        private IntVec3 firstDragSquare;
        private IntVec3 lastDragSquare;
        private bool dragging = false;

        public override AcceptanceReport CanPlaceWithRestriction(EntityDef checkingDef, IntVec3 loc, IntRot rot)
        {
            DesignationDragger dragger = Find.MainTabsRoot.tabArchitect.selectedPanel.dragger;

            if (!dragger.dragging)
            {
                if (dragging)
                {
                    dragging = false;
                    firstDragSquare = IntVec3.Invalid;
                    lastDragSquare = IntVec3.Invalid;
                }
            }
            else if (!dragging)
            {
                dragging = true;
                firstDragSquare = loc;
            }
            else if (firstDragSquare == Gen.MouseWorldSquare())
            {
                lastDragSquare = IntVec3.Invalid;
            }

            if (SquareHas(new IntVec3(loc.x - 1, loc.y, loc.z), (ThingDef)checkingDef)
                && SquareHas(new IntVec3(loc.x - 1, loc.y, loc.z + 1), (ThingDef)checkingDef)
                && SquareHas(new IntVec3(loc.x, loc.y, loc.z + 1), (ThingDef)checkingDef))
                return false;

            if (SquareHas(new IntVec3(loc.x, loc.y, loc.z + 1), (ThingDef)checkingDef)
                && SquareHas(new IntVec3(loc.x + 1, loc.y, loc.z + 1), (ThingDef)checkingDef)
                && SquareHas(new IntVec3(loc.x + 1, loc.y, loc.z), (ThingDef)checkingDef))
                return false;

            if (SquareHas(new IntVec3(loc.x + 1, loc.y, loc.z), (ThingDef)checkingDef)
                && SquareHas(new IntVec3(loc.x + 1, loc.y, loc.z - 1), (ThingDef)checkingDef)
                && SquareHas(new IntVec3(loc.x, loc.y, loc.z - 1), (ThingDef)checkingDef))
                return false;

            if (SquareHas(new IntVec3(loc.x, loc.y, loc.z - 1), (ThingDef)checkingDef)
                && SquareHas(new IntVec3(loc.x - 1, loc.y, loc.z - 1), (ThingDef)checkingDef)
                && SquareHas(new IntVec3(loc.x - 1, loc.y, loc.z), (ThingDef)checkingDef))
                return false;

            if (dragging)
                lastDragSquare = loc;

            return true;
        }

        private bool SquareHas(IntVec3 sq, ThingDef def)
        {
            if (!sq.InBounds())
                return false;
            if (Find.Grids.ThingAt(sq, def) != null || Find.Grids.ThingAt(sq, def.blueprintDef) != null)
                return true;
            if (dragging && lastDragSquare == sq)
                return true;
            return false;
        }
    }


Edit3: For anybody wondering what it does, it prevents making sandbag waffles.

Edit4: Just for completion, I've updated it with the same code as I used in Vanilla+ v1.2.3, in case anybody finds it useful
Edit5: oh, I overlooked something... fixed

StorymasterQ

I like how this game can result in quotes that would be quite unnerving when said in public, out of context. - Myself

The dubious quotes list is now public. See it here