Jump to content

Trying to barricade an object below mouse


ethanwdp

Recommended Posts

I'm the furthest I've ever been in a long time to getting a barricade cheat to work, problem is that I can't figure out how exactly the Barricade function for it works.

 

Here's my code so far:

 

CheatCoreCM.DoBarricade = function(keyPressed)
    if keyPressed == 45 then
        local mx = getMouseXScaled();
        local my = getMouseYScaled();
        local player = getPlayer();
        local wz = getPlayer():getZ();
        local wx, wy = ISCoordConversion.ToWorld(mx, my, wz);
        wx = math.floor(wx);
        wy = math.floor(wy);
        local cell = getWorld():getCell();
        local sq = cell:getGridSquare(wx, wy, wz);
        local sqObjs = sq:getObjects();
        local sqSize = sqObjs:size();
        local planks = {}
        for i = sqSize-1, 0, -1 do
            local obj = sqObjs:get(i);
            if i > 0 then -- checks for floor, otherwise it'd leave a gaping hole
                for i = 0, getPlayer():getInventory():getItems():size() - 1 do
                    local itemGet = getPlayer():getInventory():getItems():get(i);
                    --getPlayer():getInventory():AddItem("Base.Plank")
                    if itemGet:getType() == "Plank" then
                        itemGet:setCondition(itemGet:getConditionMax(), false)
                        table.insert(planks, itemGet)
                    end
                end
                sq:getObjects():Barricade(getPlayer(), planks)
            end
        end
    end
end

Events.OnKeyKeepPressed.Add(CheatCoreCM.DoBarricade)

 

According to the games code, Barricade should accept two arguments: the player character, and the item.

 

I didn't know how to specify an exact item, so I took a bit of the build menu code and formatted it to work with my mod.

 

Is there a way to just barricade without items? If not, how do I use Barricade() anyways?

Link to comment
Share on other sites

Edit2: RoboMatt was on the right track, as it turns out. The barricade TA's timer is not hardcoded in, and you can remove it. I was also barking up the wrong tree for objects, I had to first detect the instance of the object, and then store it in a variable. I then call ISTimedActionQueue.add(ISBarricadeAction:new(getPlayer(), variableThatIStoredInstanceIn, 0));

 

Here's the working code:

 

 

CheatCoreCM.DoBarricade = function(keyPressed)
    if CheatCoreCM.IsBarricade == true and keyPressed == 45 then
        local mx = getMouseXScaled();
        local my = getMouseYScaled();
        local player = getPlayer();
        local wz = getPlayer():getZ();
        local wx, wy = ISCoordConversion.ToWorld(mx, my, wz);
        wx = math.floor(wx);
        wy = math.floor(wy);
        local cell = getWorld():getCell();
        local sq = cell:getGridSquare(wx, wy, wz);
        local sqObjs = sq:getObjects();
        local sqSize = sqObjs:size();
        local planks = {}
        local worldobjects = sq:getWorldObjects()
        if not getPlayer():getInventory():contains("Plank") then
            getPlayer():getInventory():AddItem("Base.Plank")
        end
        if not getPlayer():getInventory():contains("Nails") then
            getPlayer():getInventory():AddItem("Base.Nails")
        end
        for i = sqSize-1, 0, -1 do
            local obj = sqObjs:get(i);
            if instanceof(obj, "IsoWindow") or instanceof(obj, "IsoWoodenWall") or instanceof(obj, "IsoDoor") or instanceof(obj, "IsoThumpable") then
                local window = obj
                ISTimedActionQueue.add(ISBarricadeAction:new(getPlayer(), window, 0));
            end
            end
        end
    end
end

 

 

You need a hammer equipped(will work on making that automatic later), but it successfully barricades it.

 

Now that I have a framework for it, I can also make it barricade to a specific level, and unbarricade to a specific level.

 

Edit 3: Fully working function with many safe guards in place to ensure instant barricading without disrupting the users inventory or equipped items:

 

 

CheatCoreCM.DoBarricade = function(keyPressed)
    if CheatCoreCM.IsBarricade == true and keyPressed == 44 then
        local mx = getMouseXScaled();
        local my = getMouseYScaled();
        local player = getPlayer();
        local wz = getPlayer():getZ();
        local wx, wy = ISCoordConversion.ToWorld(mx, my, wz);
        wx = math.floor(wx);
        wy = math.floor(wy);
        local cell = getWorld():getCell();
        local sq = cell:getGridSquare(wx, wy, wz);
        local sqObjs = sq:getObjects();
        local sqSize = sqObjs:size();
        local planks = {}
        local worldobjects = sq:getWorldObjects()
        local previousPlanks = getPlayer():getInventory():getNumberOfItem("Plank") -- I store the count of the players nails and planks for later use
        local previousNails = getPlayer():getInventory():getNumberOfItem("Nails")
        local previousEquippedItem = getPlayer():getSecondaryHandItem()
        local hasAddedHammer = false
        local removalCount = 0
        local objectToBarricade;
        
        if getPlayer():getSecondaryHandItem() ~= "Hammer" and getPlayer():getInventory():contains("Hammer") then -- checks if the player has a hammer equipped
            ISTimedActionQueue.add(ISEquipWeaponAction:new(getPlayer(), getPlayer():getInventory():FindAndReturn("Hammer"), 0, false, false));
        elseif not getPlayer():getInventory():contains("Hammer") then
            hasAddedHammer = true
            getPlayer():getInventory():AddItem("Base.Hammer")
            ISTimedActionQueue.add(ISEquipWeaponAction:new(getPlayer(), getPlayer():getInventory():FindAndReturn("Hammer"), 0, false, false));
        end
        
        local function syncInventory()
            getPlayer():getInventory():RemoveAll("Plank") -- I remove all planks and nails from the inventory, and re-add them later.
            getPlayer():getInventory():RemoveAll("Nails")
            if previousPlanks ~= 0 then
                for i = 1,previousPlanks do
                    getPlayer():getInventory():AddItem("Base.Plank")
                end
            end
            if previousNails ~= 0 then
                for i = 1,previousNails do
                    getPlayer():getInventory():AddItem("Base.Nails")
                    for i = 1,4 do -- for some reason, adding Base.Nails once gives 5 nails. I remove 4 from every iteration to return the exact number of nails the player had in his inventory beforehand.
                        getPlayer():getInventory():RemoveOneOf("Nails")
                    end
                end
            end
            if previousEquippedItem ~= nil then
                ISTimedActionQueue.add(ISEquipWeaponAction:new(getPlayer(), previousEquippedItem, 0, false, false));
                if hasAddedHammer == true then
                    getPlayer():getInventory():RemoveOneOf("Hammer")
                end
            end
        end
        
        for i = sqSize-1, 0, -1 do
            local obj = sqObjs:get(i);
            if instanceof(obj, "IsoWindow") or instanceof(obj, "IsoWoodenWall") or instanceof(obj, "IsoDoor") or instanceof(obj, "IsoThumpable") then
                objectToBarricade = obj -- I store the returned instance into a variable
                if CheatCoreCM.BarricadeDoDelete == true then
                    for i = 1,CheatCoreCM.BarricadeLevel do
                        ISTimedActionQueue.add(ISUnbarricadeAction:new(getPlayer(), objectToBarricade, 0));
                        getPlayer():getInventory():RemoveOneOf("Plank")
                        getPlayer():getInventory():RemoveOneOf("Nails")
                        --[[
                        if i == CheatCoreCM.BarricadeLevel then
                            syncInventory()
                        end
                        --]]
                    end
                elseif CheatCoreCM.BarricadeDoDelete ~= true then
                    for i = 1,CheatCoreCM.BarricadeLevel do
                        getPlayer():getInventory():AddItem("Base.Plank") -- I add the necessary amount of planks and nails required to barricade the object.
                        getPlayer():getInventory():AddItem("Base.Nails")
                        for i = 1,4 do
                            getPlayer():getInventory():RemoveOneOf("Nails")
                        end
                        local requiredPlanks = getPlayer():getInventory():getNumberOfItem("Plank")
                        local requiredNails = getPlayer():getInventory():getNumberOfItem("Nails")
                        ISTimedActionQueue.add(ISBarricadeAction:new(getPlayer(), objectToBarricade, 0));
                        if CheatCoreCM.BarricadeLevel ~= 1 then
                            if i == CheatCoreCM.BarricadeLevel and requiredPlanks == requiredPlanks * CheatCoreCM.BarricadeLevel and requiredNails == requiredNails * CheatCoreCM.BarricadeLevel then
                                syncInventory()
                            end
                        end
                    end
                end
            end
        end
    end
end

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...