Advertisement
Guest User

Better Vacuum Chest.

a guest
Mar 13th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.58 KB | None | 0 0
  1. require "prefabutil"
  2.  
  3. local assets =
  4. {
  5.     Asset("ANIM", "anim/VacuumChest.zip"),
  6.     Asset("ANIM", "anim/firefighter_placement.zip"),
  7.     Asset("ATLAS", "images/inventoryimages/vacuum_chest.xml"),
  8.     Asset("IMAGE", "images/inventoryimages/vacuum_chest.tex"),
  9. }
  10.  
  11. local prefabs =
  12. {
  13.     "collapse_small",
  14. }
  15.  
  16. local function onopen(inst)
  17.     inst.AnimState:PlayAnimation("open")
  18.     inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_open")    
  19. end
  20.  
  21. local function onclose(inst)
  22.     inst.AnimState:PlayAnimation("close")
  23.     inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_close")       
  24. end
  25.  
  26. local function onhammered(inst, worker)
  27.     inst.components.lootdropper:DropLoot()
  28.     inst.components.container:DropEverything()
  29.     SpawnPrefab("collapse_small").Transform:SetPosition(inst.Transform:GetWorldPosition())
  30.     inst.SoundEmitter:PlaySound("dontstarve/common/destroy_wood")
  31.    
  32.     inst:Remove()
  33. end
  34.  
  35. local function onhit(inst, worker)
  36.     inst.AnimState:PlayAnimation("hit")
  37.     inst.components.container:DropEverything()
  38.     inst.AnimState:PushAnimation("closed", false)
  39.     inst.components.container:Close()
  40. end
  41.  
  42.  
  43. local function onbuilt(inst)
  44.     inst.AnimState:PlayAnimation("place")
  45.     inst.AnimState:PushAnimation("closed", false)
  46. end
  47.  
  48. --------------------------------------------------------------------------
  49. --9.67741935483871 is the default scale of the placer outline thing since its sets the scale to 1.55 and 15 / 1.55 = 9.67741935483871
  50. local PLACER_SCALE = TUNING.VACUUM_RANGE / (15 / 1.55)
  51.  
  52. local function OnEnableHelper(inst, enabled)
  53.     if enabled then
  54.         if inst.helper == nil then
  55.             inst.helper = CreateEntity()
  56.  
  57.             --[[Non-networked entity]]
  58.             inst.helper.entity:SetCanSleep(false)
  59.             inst.helper.persists = false
  60.  
  61.             inst.helper.entity:AddTransform()
  62.             inst.helper.entity:AddAnimState()
  63.  
  64.             inst.helper:AddTag("CLASSIFIED")
  65.             inst.helper:AddTag("NOCLICK")
  66.             inst.helper:AddTag("placer")
  67.  
  68.             inst.helper.Transform:SetScale(PLACER_SCALE, PLACER_SCALE, PLACER_SCALE)
  69.  
  70.             inst.helper.AnimState:SetBank("firefighter_placement")
  71.             inst.helper.AnimState:SetBuild("firefighter_placement")
  72.             inst.helper.AnimState:PlayAnimation("idle")
  73.             inst.helper.AnimState:SetLightOverride(1)
  74.             inst.helper.AnimState:SetOrientation(ANIM_ORIENTATION.OnGround)
  75.             inst.helper.AnimState:SetLayer(LAYER_BACKGROUND)
  76.             inst.helper.AnimState:SetSortOrder(1)
  77.             inst.helper.AnimState:SetAddColour(0, .2, .5, 0)
  78.  
  79.             inst.helper.entity:SetParent(inst.entity)
  80.         end
  81.     elseif inst.helper ~= nil then
  82.         inst.helper:Remove()
  83.         inst.helper = nil
  84.     end
  85. end
  86.  
  87. --------------------------------------------------------------------------
  88.  
  89. local function fn(Sim)
  90.     local inst = CreateEntity()
  91.  
  92.     inst.entity:AddTransform()
  93.     inst.entity:AddAnimState()
  94.     inst.entity:AddSoundEmitter()
  95.     inst.entity:AddMiniMapEntity()
  96.     inst.entity:AddNetwork()
  97.  
  98.     inst.MiniMapEntity:SetIcon( "vacuum_chest.tex" )
  99.  
  100.     inst.AnimState:SetBank("chest")
  101.     inst.AnimState:SetBuild("VacuumChest")
  102.     inst.AnimState:PlayAnimation("closed")
  103.    
  104.     inst:AddTag("structure")
  105.     inst:AddTag("chest")
  106.  
  107.     --Dedicated server does not need deployhelper
  108.     if not TheNet:IsDedicated() then
  109.         inst:AddComponent("deployhelper")
  110.         inst.components.deployhelper.onenablehelper = OnEnableHelper
  111.     end
  112.    
  113.     inst.entity:SetPristine()
  114.  
  115.     if not TheWorld.ismastersim then
  116.         inst.OnEntityReplicated = function(inst)
  117.             inst.replica.container:WidgetSetup("chester")
  118.         end
  119.         return inst
  120.     end
  121.  
  122.     inst:AddComponent("inspectable")
  123.  
  124.     inst:AddComponent("container")
  125.     inst.components.container:WidgetSetup("chester")
  126.     inst.components.container.onopenfn = onopen
  127.     inst.components.container.onclosefn = onclose
  128.  
  129.     inst:AddComponent("lootdropper")
  130.     inst:AddComponent("workable")
  131.     inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
  132.     inst.components.workable:SetWorkLeft(2)
  133.     inst.components.workable:SetOnFinishCallback(onhammered)
  134.     inst.components.workable:SetOnWorkCallback(onhit)
  135.        
  136.     inst:ListenForEvent( "onbuilt", onbuilt)
  137.     MakeSnowCovered(inst, .01) 
  138.  
  139.     local function suckit(item)
  140.         if inst.AnimState:IsCurrentAnimation("closed") or inst.AnimState:IsCurrentAnimation("close") then
  141.             inst.AnimState:PlayAnimation("hit")
  142.             inst.AnimState:PushAnimation("closed", false)
  143.         end
  144.         inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_open")
  145.         inst.components.container:GiveItem(item)
  146.     end
  147.  
  148.  
  149.     local function vacuum(inst)
  150.         local x,y,z = inst.Transform:GetWorldPosition()
  151.         local SEARCH_RADIUS = TUNING.VACUUM_RANGE
  152.  
  153.         local ents = TheSim:FindEntities(x, y, z, SEARCH_RADIUS, nil, {"minesprung", "mineactive", "FX", "NOCLICK", "DECOR", "INLIMBO"})
  154.  
  155.         local function IsItem(ent)
  156.             return ent.components.inventoryitem and ent.components.inventoryitem.canbepickedup and ent.components.inventoryitem.cangoincontainer and ent or nil
  157.         end
  158.        
  159.         local count = 0
  160.         for i, v in ipairs(ents) do
  161.             --only pickup a couple items at a time
  162.             count = count + 1
  163.             if count > 5 then return end
  164.             local Item = IsItem(v)
  165.             if Item then
  166.                 if not inst.components.container:IsFull() then
  167.                     suckit(Item)
  168.                     return
  169.                 elseif Item.components.stackable then
  170.                     local stack = inst.components.container:FindItem(function(i) return (i.prefab == Item.prefab and not i.components.stackable:IsFull()) end)
  171.                
  172.                     if stack then
  173.                         suckit(Item)
  174.                         return
  175.                     end
  176.                 end
  177.             end
  178.         end
  179.     end
  180.  
  181.     inst:DoPeriodicTask(0.5, vacuum)
  182.  
  183.  
  184.     return inst
  185. end
  186.  
  187. local function placer_postinit_fn(inst)
  188.     --Show the vacuum chest placer on top of the vacuum chest range ground placer
  189.  
  190.     local placer2 = CreateEntity()
  191.  
  192.     --[[Non-networked entity]]
  193.     placer2.entity:SetCanSleep(false)
  194.     placer2.persists = false
  195.  
  196.     placer2.entity:AddTransform()
  197.     placer2.entity:AddAnimState()
  198.  
  199.     placer2:AddTag("CLASSIFIED")
  200.     placer2:AddTag("NOCLICK")
  201.     placer2:AddTag("placer")
  202.  
  203.     local s = 1 / PLACER_SCALE
  204.     placer2.Transform:SetScale(s, s, s)
  205.  
  206.     placer2.AnimState:SetBank("chest")
  207.     placer2.AnimState:SetBuild("VacuumChest")
  208.     placer2.AnimState:PlayAnimation("closed")
  209.     placer2.AnimState:SetLightOverride(1)
  210.  
  211.     placer2.entity:SetParent(inst.entity)
  212.  
  213.     inst.components.placer:LinkEntity(placer2)
  214. end
  215.  
  216. return Prefab( "common/vacuum_chest", fn, assets),
  217.         MakePlacer("common/vacuum_chest_placer", "firefighter_placement", "firefighter_placement", "idle", true, nil, nil, PLACER_SCALE, nil, nil, placer_postinit_fn)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement