Automatically rotate hoppers (code included)

Started by WorldOfIllusion, April 28, 2014, 09:13:49 PM

Previous topic - Next topic

WorldOfIllusion

While working on a mod I created a small bit of code that rotates a thing so that it faces the correct way when connecting to another specific structure. This could very easily be added to the vanilla hoppers, so that they rotate to face an adjacent Nutrientpaste dispenser when placed. Just add a call to the following method to the post make method of the hopper:
        public void rotateToInterface()
        {
            foreach (IntVec3 adj in GenAdj.AdjacentSquaresCardinalInBounds(Position))
            {
                foreach (Thing thing in Find.Grids.ThingsAt(adj))
                {
                    Building temp = thing as Building;
                    if (temp != null)
                    {
                        if (temp.canConnect) // change this to be the correct 'rotation' restriction (eg, is nutrient past dispenser)
                        {
                            this.rotation = getConnectingSide(this.Position, temp.Position);
                        }
                    }
                }
            }
        }

        public IntRot getConnectingSide(IntVec3 child, IntVec3 parent)
        {
            // x = left/right map
            // y = up/down layer
            // z = up/down map
            if (parent.x > child.x) return IntRot.east;
            if (parent.x < child.x) return IntRot.west;
            if (parent.z > child.z) return IntRot.north;
            return IntRot.south;
        }

Also, if any modders want to use this, feel free (just send me a PM when you release the mod so I can have a look).

*edit* can probably even remove the foreach thing bit and change it to just check the building grid for the building at each position.
Artistically challenged modder seeking artistically talented texturer's help. Please, please, PM me :)

mrofa

Cool that you share this.
I just got one question thrugh, what will happen if i put two dispensers and a hopper between them?
All i do is clutter all around.

WorldOfIllusion

it'll rotate to one of them (whichever direction I check for first). That said, it'll still work for both dispensers (just can't be rotate to face both of them  :) ).
Artistically challenged modder seeking artistically talented texturer's help. Please, please, PM me :)

Celthric Aysen

Quote from: WorldOfIllusion on April 30, 2014, 12:12:47 AM
it'll rotate to one of them (whichever direction I check for first). That said, it'll still work for both dispensers (just can't be rotate to face both of them  :) ).
well since your now part of the Industrial RIM mod team,
just wondering will you be adding this to Industrial RIM?
My DeviantART|E-Vehicles|Flebe's  Channel
"♫"Every people that i see i will never understand"♪"

WorldOfIllusion

Quote from: Blackjack1000K on April 30, 2014, 12:18:07 AM
Quote from: WorldOfIllusion on April 30, 2014, 12:12:47 AM
it'll rotate to one of them (whichever direction I check for first). That said, it'll still work for both dispensers (just can't be rotate to face both of them  :) ).
well since your now part of the Industrial RIM mod team,
just wondering will you be adding this to Industrial RIM?

Sure.

Also, a quick update. I didn't test the above code and it does have a slight issue - if you want the properly working code check the attached file on this post or just check the mini-mod for this here.

[attachment deleted by admin: too old]
Artistically challenged modder seeking artistically talented texturer's help. Please, please, PM me :)