Advertisement
MarrKoo

Crowbar 2 UnBarricade

May 14th, 2020
1,645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.83 KB | None | 0 0
  1. ----------------------------- FIX WORLD MENU -------------------------------
  2.  
  3. require "ISUI/ISWorldObjectContextMenu"
  4.  
  5. local function predicateNotBroken(item)
  6.     return not item:isBroken()
  7. end
  8.  
  9. local old_getNumPlanks = nil
  10. local is_active_inject = nil -- true(active) / false(done) / nil(disabled)
  11. local temp_worldobjects = nil
  12. local temp_player = nil
  13. local temp_test = nil
  14. local temp_context = nil
  15. local function fn_getNumPlanks(self)
  16.     local num = old_getNumPlanks(self)
  17.     if is_active_inject == nil or temp_test then
  18.         --print('INJECTED - idle')
  19.         return num
  20.     end
  21.     if is_active_inject == false then
  22.         --print('INJECTED - already done')
  23.         return 0
  24.     end
  25.     --print('INJECTED - active')
  26.     local playerObj = getSpecificPlayer(temp_player)
  27.     local playerInv = playerObj:getInventory()
  28.     local hasHammer = playerInv:containsTypeEvalRecurse("Hammer", predicateNotBroken) or playerInv:containsTypeEvalRecurse("HammerStone", predicateNotBroken)
  29.     local hasCrowbar = playerInv:containsTypeEvalRecurse("Crowbar", predicateNotBroken)
  30.  
  31.     if thump ~= nil and not invincibleWindow and thump:getCanBarricade() then --print('thump')
  32.         local ignoreObject = false;
  33.         for _,v in pairs(temp_worldobjects) do
  34.             if instanceof(v,"IsoWindow") and thump~=v then
  35.                 ignoreObject = true;
  36.                 break
  37.             end
  38.         end
  39.         if not ignoreObject then
  40.             local barricade = thump:getBarricadeForCharacter(playerObj)
  41.             if (barricade and num > 0) and (hasHammer or hasCrowbar) then
  42.                 temp_context:addOption(getText("ContextMenu_Unbarricade"), temp_worldobjects, ISWorldObjectContextMenu.onUnbarricade, thump, temp_player);
  43.             end
  44.         end
  45.     end
  46.    
  47.     if window ~= nil and not invincibleWindow then --print('window')
  48.         local barricade = window:getBarricadeForCharacter(playerObj)
  49.         if (barricade and num > 0) and (hasHammer or hasCrowbar) then
  50.             temp_context:addOption(getText("ContextMenu_Unbarricade"), temp_worldobjects, ISWorldObjectContextMenu.onUnbarricade, window, temp_player);
  51.         end
  52.     end
  53.    
  54.     if door ~= nil then --print('door')
  55.         local barricade = door:getBarricadeForCharacter(playerObj)
  56.         if (barricade and num > 0) and (hasHammer or hasCrowbar) then
  57.             temp_context:addOption(getText("ContextMenu_Unbarricade"), temp_worldobjects, ISWorldObjectContextMenu.onUnbarricade, door, temp_player);
  58.         end
  59.     end
  60.    
  61.     is_active_inject = false --done
  62.     return 0
  63. end
  64.  
  65. local old_createMenu = ISWorldObjectContextMenu.createMenu
  66. ISWorldObjectContextMenu.createMenu = function(player, worldobjects, x, y, test)
  67.     --print('WORLD MENU')
  68.     is_active_inject = true
  69.     temp_worldobjects = worldobjects
  70.     temp_player = player
  71.     temp_test = test
  72.     --local hasCrowbar = playerInv:containsTypeEvalRecurse("Crowbar", predicateNotBroken)
  73.     --if thump ~= nil and not invincibleWindow then
  74.         --if thump:getCanBarricade() then
  75.         --local barricade = thump:getBarricadeForCharacter(playerObj)
  76.     local context = old_createMenu(player, worldobjects, x, y, test)
  77.     is_active_inject = nil
  78.     return context
  79. end
  80.  
  81.  
  82. --Create permanent inject
  83. local is_planks_injected = nil
  84. local function fnPlanksInject(player, context) --, worldobjects, test)
  85.     local obj = window or thump or door
  86.     if not (obj and obj.getBarricadeForCharacter) then
  87.         return
  88.     end
  89.     temp_context = context
  90.     local playerObj = getSpecificPlayer(player)
  91.     local barricade = obj:getBarricadeForCharacter(playerObj)
  92.     if not barricade or is_planks_injected then
  93.         return
  94.     end
  95.     is_planks_injected = true
  96.     local m = getmetatable(barricade)
  97.     old_getNumPlanks = m.__index.getNumPlanks
  98.     m.__index.getNumPlanks = fn_getNumPlanks
  99. end
  100. Events.OnPreFillWorldObjectContextMenu.Add(fnPlanksInject)
  101.  
  102.  
  103. --Overwrite, sadly
  104. ISWorldObjectContextMenu.onUnbarricade = function(worldobjects, window, player)
  105.     local playerObj = getSpecificPlayer(player)
  106.     local playerInv = playerObj:getInventory()
  107.     local tool = playerObj:getPrimaryHandItem()
  108.     if luautils.walkAdjWindowOrDoor(playerObj, window:getSquare(), window) then
  109.         local tool_type = tool and tool:getType()
  110.         local is_hammer = tool_type == "Hammer" or tool_type == "HammerStone"
  111.         local is_equipped_crowbar = tool_type == "Crowbar"
  112.         if not is_equipped_crowbar and playerInv:contains("Crowbar") then
  113.             local crowbar = playerInv:FindAndReturn("Crowbar")
  114.             if crowbar:getCondition() > 0 then
  115.                 ISWorldObjectContextMenu.equip(playerObj, tool, "Crowbar", true);
  116.                 is_equipped_crowbar = true
  117.             end
  118.         end
  119.         if not is_equipped_crowbar and not is_hammer then
  120.             if playerInv:containsTypeEvalRecurse("Crowbar", predicateNotBroken) then
  121.                 ISWorldObjectContextMenu.equip(playerObj, tool, "Crowbar", true);
  122.                 is_equipped_crowbar = true
  123.             elseif playerInv:containsTypeEvalRecurse("Hammer", predicateNotBroken) then
  124.                 ISWorldObjectContextMenu.equip(playerObj, tool, "Hammer", true);
  125.             elseif playerInv:containsTypeEvalRecurse("HammerStone", predicateNotBroken) then
  126.                 ISWorldObjectContextMenu.equip(playerObj, tool, "HammerStone", true);
  127.             end
  128.         end
  129.         local work_time = is_equipped_crowbar and 50
  130.             or (600 - playerObj:getPerkLevel(Perks.Woodwork) * 30)
  131.         ISTimedActionQueue.add(ISUnbarricadeAction:new(playerObj, window, work_time ));
  132.     end
  133. end
  134.  
  135.  
  136.  
  137. -------------------------- FIX ACTION -------------------------
  138.  
  139. require "TimedActions/ISUnbarricadeAction"
  140.  
  141. --Overwrite, sadly
  142. function ISUnbarricadeAction:isValid()
  143.     if not instanceof(self.item, "BarricadeAble") or not self.item:getBarricadeForCharacter(self.character) then
  144.         return false
  145.     end
  146.     local barricade = self.item:getBarricadeForCharacter(self.character)
  147.     if barricade:isMetal() or barricade:isMetalBar() then
  148.         if not self.character:hasEquipped("BlowTorch") then
  149.             return false
  150.         end
  151.     else
  152.         if barricade:getNumPlanks() == 0 then
  153.             return false
  154.         end
  155.         if not self.character:hasEquipped("Hammer") and not self.character:hasEquipped("Crowbar") and not self.character:hasEquipped("HammerStone") then
  156.             return false
  157.         end
  158.     end
  159.     return true;
  160. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement