Advertisement
Guest User

Untitled

a guest
Jul 16th, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.63 KB | None | 0 0
  1. -- This seems to still work without this line, the needed functions are probably already in the global space.
  2. require("map/treasurehunt")
  3.  
  4. local assets =
  5. {
  6.     Asset("ANIM", "anim/x_marks_spot.zip"),
  7. }
  8.  
  9. local prefabs =
  10. {
  11.     "messagebottle",
  12.     "collapse_small",
  13. }
  14.  
  15. local function linkBottles(inst, count)
  16.     print(string.format("Place %d bottles\n", count))
  17.     for i = 1, count, 1 do
  18.         local bottle = SpawnPrefab("messagebottle")
  19.         bottle.treasure = inst
  20.         bottle.PlaceBottle(bottle)
  21.     end
  22. end
  23.  
  24. local function linkTreasure(inst)
  25.     local x, y, z = inst.Transform:GetLocalPosition()
  26.     --find a treasure to link to this treasure, don't link twice
  27.     local treasures = TheSim:FindEntities(x, y, z, 10000, {"buriedtreasure"}, {"linkingtreasure"})
  28.     if treasures and #treasures > 0 then
  29.         local t = treasures[math.random(1, #treasures)]
  30.         if t then
  31.             print("Linking treasures")
  32.             t.treasurenext = inst
  33.             inst.treasureprev = t
  34.             t:AddTag("linkingtreasure")
  35.             inst:AddTag("linktreasure")
  36.         end
  37.     end
  38. end
  39.  
  40. local function onfinishcallback(inst, worker)
  41.  
  42.     inst.MiniMapEntity:SetEnabled(false)
  43.     inst:RemoveComponent("workable")
  44.     inst.components.hole.canbury = true
  45.  
  46.     if worker then
  47.         if inst.treasurenext and inst.treasurenext ~= nil then
  48.             --How much longer?
  49.             if inst.treasureprev and inst.treasureprev ~= nil and inst.treasureprev.treasureprev and inst.treasureprev.treasureprev ~= nil then
  50.                 worker.components.talker:Say(GetString(worker.prefab, "ANNOUNCE_MORETREASURE"))
  51.             end
  52.         end
  53.  
  54.         -- figure out which side to drop the loot
  55.         local pt = Vector3(inst.Transform:GetWorldPosition())
  56.         local hispos = Vector3(worker.Transform:GetWorldPosition())
  57.  
  58.         local he_right = ((hispos - pt):Dot(TheCamera:GetRightVec()) > 0)
  59.        
  60.         if he_right then
  61.             inst.components.lootdropper:DropLoot(pt - (TheCamera:GetRightVec()*(math.random()+1)))
  62.             inst.components.lootdropper:DropLoot(pt - (TheCamera:GetRightVec()*(math.random()+1)))
  63.         else
  64.             inst.components.lootdropper:DropLoot(pt + (TheCamera:GetRightVec()*(math.random()+1)))
  65.             inst.components.lootdropper:DropLoot(pt + (TheCamera:GetRightVec()*(math.random()+1)))
  66.         end
  67.        
  68.         inst.SoundEmitter:PlaySound("dontstarve_DLC002/common/loot_reveal")
  69.         SpawnTreasureChest(inst.loot, inst.components.lootdropper, Point(inst.Transform:GetWorldPosition()), inst.treasurenext)
  70.  
  71.         inst:Remove()
  72.     end
  73. end
  74.  
  75. local function onsave(inst, data)
  76.     if not inst.components.workable then
  77.         data.dug = true
  78.     end
  79.  
  80.     if inst.treasureprev and inst.treasureprev ~= nil then
  81.         data.treasureprev = inst.treasureprev.GUID
  82.     end
  83.     if inst.treasurenext and inst.treasurenext ~= nil then
  84.         data.treasurenext = inst.treasurenext.GUID
  85.     end
  86.     if inst.loot then
  87.         data.loot = inst.loot
  88.     end
  89.     if inst.revealed then
  90.         data.revealed = inst.revealed
  91.     end
  92. end
  93.  
  94. local function onload(inst, data)
  95.  
  96.     if data and data.dug or not inst.components.workable then
  97.         inst:RemoveComponent("workable")
  98.         inst.components.hole.canbury = true
  99.         inst:RemoveTag("NOCLICK")
  100.     end
  101.  
  102.     if data and data.loot and data.loot ~= nil then
  103.         inst.loot = data.loot
  104.     end
  105.  
  106.     if data and data.revealed and data.revealed == true then
  107.         print("Reveal treasure")
  108.         inst:Reveal(inst)
  109.     end
  110. end
  111.  
  112. local function loadpostpass(inst, ents, data)
  113.  
  114.     if data then
  115.         if data.loot and data.loot ~= nil then
  116.             if type(data.loot) == "table" then
  117.                 inst.loot = data.loot[math.random(1, #data.loot)]
  118.             else
  119.                 inst.loot = data.loot
  120.             end
  121.         end
  122.         if data.treasureprev and data.treasureprev ~= nil then
  123.             local ent = ents[data.treasureprev]
  124.             if ent then
  125.                 inst.treasureprev = ent.entity
  126.                 inst:AddTag("linktreasure")
  127.             end
  128.         end
  129.         if data.treasurenext and data.treasurenext ~= nil then
  130.             local ent = ents[data.treasurenext]
  131.             if ent then
  132.                 inst.treasurenext = ent.entity
  133.                 inst:AddTag("linkingtreasure")
  134.             end
  135.         end
  136.         if data.bottles then
  137.             linkBottles(inst, data.bottles)
  138.         end
  139.         if data.name then
  140.             inst.debugname = data.name
  141.         end
  142.     end
  143. end
  144.  
  145. local function fn(Sim)
  146.     local inst = CreateEntity()
  147.     local trans = inst.entity:AddTransform()
  148.     local anim = inst.entity:AddAnimState()
  149.     local minimap = inst.entity:AddMiniMapEntity()
  150.  
  151.     inst.entity:AddSoundEmitter()
  152.  
  153.     inst:AddTag("buriedtreasure")
  154.     inst:AddTag("NOCLICK")
  155.     inst.entity:Hide()
  156.  
  157.     minimap:SetIcon( "xspot.png" )
  158.     minimap:SetEnabled(false)
  159.  
  160.     anim:SetBank("x_marks_spot")
  161.     anim:SetBuild("x_marks_spot")
  162.     anim:PlayAnimation("anim")
  163.  
  164.     inst:AddComponent("inspectable")
  165.     inst.components.inspectable.getstatus = function(inst)
  166.         if not inst.components.workable then
  167.             return "DUG"
  168.         end
  169.     end
  170.    
  171.     inst:AddComponent("workable")
  172.     inst.components.workable:SetWorkAction(ACTIONS.DIG)
  173.     inst.components.workable:SetWorkLeft(1)
  174.     inst:AddComponent("lootdropper")
  175.     inst.components.lootdropper:SetLoot({"boneshard"})
  176.        
  177.     inst.components.workable:SetOnFinishCallback(onfinishcallback)
  178.  
  179.     inst:AddComponent("hole")
  180.  
  181.     inst.loot = ""
  182.     inst.revealed = false
  183.  
  184.     inst.Reveal = function(inst)
  185.         print("Treasure revealed")
  186.         inst.revealed = true
  187.         inst.entity:Show()
  188.         inst.MiniMapEntity:SetEnabled(true)
  189.         inst:RemoveTag("NOCLICK")
  190.     end
  191.  
  192.     inst.RevealFog = function(inst)
  193.         print("Tresure fog revealed")
  194.         local x, y, z = inst.Transform:GetLocalPosition()
  195.         local minimap = GetWorld().minimap.MiniMap
  196.         local map = GetWorld().Map
  197.         local cx, cy, cz = map:GetTileCenterPoint(x, 0, z)
  198.         minimap:ShowArea(cx, cy, cz, 30)
  199.         map:VisitTile(map:GetTileCoordsAtPoint(cx, cy, cz))
  200.     end
  201.  
  202.     inst.IsRevealed = function(inst)
  203.         return inst.revealed
  204.     end
  205.  
  206.     inst.FocusMinimap = function(inst, bottle)
  207.         local px, py, pz = GetPlayer().Transform:GetWorldPosition()
  208.         local x, y, z = inst.Transform:GetLocalPosition()
  209.         local minimap = GetWorld().minimap.MiniMap
  210.         print("Find treasure on minimap (" .. x .. ", "  .. z .. ")")
  211.         GetPlayer().HUD.controls:ToggleMap()
  212.         minimap:Focus(x - px, z - pz, -minimap:GetZoom()) --Zoom in all the way    
  213.     end
  214.  
  215.     inst.OnSave = onsave
  216.     inst.OnLoad = onload
  217.     inst.OnLoadPostPass = loadpostpass
  218.  
  219.     if math.random() < 0.2 then
  220.         --linkTreasure(inst)
  221.     end
  222.  
  223.     if not inst.treasurenext or inst.treasurenext == nil then
  224.         --Spawn linked message bottles
  225.         --linkBottles(inst, math.random(1, 3))
  226.     end
  227.  
  228.     inst.SetRandomTreasure = function(inst)
  229.         inst:Reveal()
  230.  
  231.         local treasures = GetTreasureLootDefinitionTable()
  232.         local treasure = GetRandomKey(treasures)
  233.         inst.loot = treasure
  234.     end
  235.  
  236.     return inst
  237. end
  238.  
  239. return Prefab( "shipwrecked/objects/buriedtreasure", fn, assets, prefabs )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement