Advertisement
Guest User

Untitled

a guest
Feb 11th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.69 KB | None | 0 0
  1. local assets =
  2. {
  3.     Asset("ANIM", "anim/shop_basic.zip"),
  4.     Asset("ATLAS", "minimap/shopkeeper.xml"),
  5.     Asset("IMAGE", "minimap/shopkeeper.tex"),
  6. }
  7.  
  8. local prefabs =
  9. {
  10.     "tentacle_pillar_arm",
  11.     "armormarble",
  12.     "armor_sanity",
  13.     "armorsnurtleshell",
  14.     "resurrectionstatue",
  15.     "poop",
  16.     "stafflight",
  17.     "firemerm",
  18.     "collapse_small",
  19. }
  20.  
  21. local spawns =
  22. {
  23.     armor_sanity        = 0.5,
  24.     health_plus         = 10,
  25.     health_minus        = 10,
  26.     stafflight          = 15,
  27.     poop                = 50,
  28.     firemerm            = 200,
  29.     trinket             = 75,
  30. }
  31.  
  32. local actions =
  33. {
  34.     trinket              = { amt = 2, },
  35.     poop                 = { amt = 5, },
  36.     firemerm             = { amt = 3, },
  37.     stafflight           = { amt = 1, },
  38. }
  39.  
  40. local MAX_LIGHT_ON_FRAME = 15
  41. local MAX_LIGHT_OFF_FRAME = 30
  42.  
  43. local function OnLightDirty(inst)
  44.     if inst._lighttask == nil then
  45.         inst._lighttask = inst:DoPeriodicTask(FRAMES, OnUpdateLight, nil, 1)
  46.     end
  47.     inst._lightmaxframe = inst._islighton:value() and MAX_LIGHT_ON_FRAME or MAX_LIGHT_OFF_FRAME
  48.     OnUpdateLight(inst, 0)
  49. end
  50.  
  51. local function PlayerSpawnCritter(player, critter, pos)
  52.     TheWorld:PushEvent("ms_sendlightningstrike", pos)
  53.     SpawnPrefab("collapse_small").Transform:SetPosition(pos:Get())
  54.     local spawn = SpawnPrefab(critter)
  55.     if spawn ~= nil then
  56.         spawn.Transform:SetPosition(pos:Get())
  57.         if spawn.components.combat ~= nil then
  58.             spawn.components.combat:SetTarget(player)
  59.         end
  60.     end
  61. end
  62.  
  63. local function SpawnCritter(critter, pos, player)
  64.     player:DoTaskInTime(GetRandomWithVariance(1, 0.8), PlayerSpawnCritter, critter, pos)
  65. end
  66.  
  67. local function SpawnAt(inst, prefab)
  68.     local pos = inst:GetPosition()
  69.     local offset, check_angle, deflected = FindWalkableOffset(pos, math.random() * 2 * PI, 4 , 8, true, false) -- try to avoid walls
  70.     if offset ~= nil then
  71.         return SpawnPrefab(prefab).Transform:SetPosition((pos + offset):Get())
  72.     end
  73. end
  74. ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  75. local function DoRandomThing(inst, pos, count, target)
  76.     count = count or 1
  77.     pos = pos or inst:GetPosition()
  78.  
  79.     for doit = 1, count do
  80.         local item = weighted_random_choice(spawns)
  81.  
  82.         local doaction = actions[item]
  83.  
  84.         local amt = doaction ~= nil and doaction.amt or 1
  85.         local sanity = doaction ~= nil and doaction.sanity or 0
  86.         local health = doaction ~= nil and doaction.health or 0
  87.         local func = doaction ~= nil and doaction.callback or nil
  88.         local radius = doaction ~= nil and doaction.radius or 4
  89.  
  90.         local player = target
  91.  
  92.         if doaction ~= nil and doaction.var ~= nil then
  93.             amt = math.max(0, GetRandomWithVariance(amt, doaction.var))
  94.         end
  95.  
  96.         if amt == 0 and func ~= nil then
  97.             func(inst, item, doaction)
  98.         end
  99.  
  100.         for i = 1, amt do
  101.             local offset, check_angle, deflected = FindWalkableOffset(pos, math.random() * 2 * PI, radius , 8, true, false) -- try to avoid walls
  102.             if offset ~= nil then
  103.                 if func ~= nil then
  104.                     func(inst, item, doaction)
  105.                 elseif item == "trinket" then
  106.                     local prefab = PickRandomTrinket()
  107.                     if prefab ~= nil then
  108.                         SpawnCritter(prefab, pos + offset, player)
  109.                     end
  110.                 else
  111.                     SpawnCritter(item, pos + offset, player)
  112.                 end
  113.             end
  114.         end
  115.     end
  116. end
  117. ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  118. local function OnIsNight(inst, isnight)
  119.     if isnight then
  120.         inst.components.trader:Disable()
  121.         inst.AnimState:PlayAnimation("night_pre")
  122.         inst.AnimState:PushAnimation("idle_night", true)
  123.     else
  124.         inst.components.trader:Enable()
  125.         inst.AnimState:PlayAnimation("night_pst")
  126.         inst.AnimState:PushAnimation("idle", true)
  127.     end
  128. end
  129.  
  130. local function OnTurnOn(inst, isnight)
  131.     if inst.components.talker then
  132.         inst.components.talker:Say(TUNING.CLOSETEXT)
  133.         inst.SoundEmitter:PlaySound("dontstarve/maxwell/talk_LP_world6", "talk")
  134.         inst:DoTaskInTime(2, function(inst)
  135.             inst.SoundEmitter:KillSound("talk")
  136.         end)
  137.     end
  138. end
  139.  
  140. local function OnTurnOff(inst, isnight)
  141.     if inst.components.talker then
  142.         inst.components.talker:Say(TUNING.GONETEXT)
  143.         inst.SoundEmitter:PlaySound("dontstarve/maxwell/talk_LP_world6", "talk")
  144.         inst:DoTaskInTime(2, function(inst)
  145.             inst.SoundEmitter:KillSound("talk")
  146.             end)
  147.         end
  148.     end
  149.  
  150. local function onactivate(inst)
  151.         inst.AnimState:PlayAnimation("purchase")
  152.         inst.AnimState:PushAnimation("idle", true)
  153.                 inst.SoundEmitter:PlaySound("dontstarve/common/teleportato/teleportato_maxwelllaugh")
  154.             if inst.components.talker then
  155.                 inst.components.talker:Say(TUNING.BUYTEXT)
  156.             end
  157. end
  158.  
  159. local function ShouldAcceptItem(inst, item)
  160.     if item.prefab == ("firescale") then
  161.             return true
  162.         else
  163.             return false
  164.     end
  165. end
  166.  
  167. local function OnGetItemFromPlayer(inst, giver, item)
  168. local pos = inst:GetPosition()
  169. if item.prefab == ("firescale") then
  170.     if inst.components.talker then
  171.         inst.components.talker:Say("SUKKKKK")
  172.         inst.SoundEmitter:PlaySound("dontstarve/characters/skincollector/talk_LP", "skincollector")
  173.         inst:DoTaskInTime(2, function(inst)
  174.             inst.SoundEmitter:KillSound("skincollector")
  175.             inst.AnimState:PlayAnimation("spawnshit")
  176.             inst.AnimState:PushAnimation("idle", true)
  177.                 inst:DoTaskInTime(1, function(inst)
  178.                 inst.SoundEmitter:PlaySound("dontstarve/rain/thunder_close")
  179.                 inst:DoTaskInTime(1, DoRandomThing(inst, pos, nil, giver))
  180.             end)   
  181.         end)
  182.     end
  183. end
  184. end
  185.  
  186. local function fn()
  187.     local inst = CreateEntity()
  188.     inst.entity:AddTransform()
  189.  
  190.     inst.entity:AddAnimState()
  191.     inst.entity:AddSoundEmitter()
  192.     inst.entity:AddNetwork()
  193.  
  194.     inst.AnimState:SetBank("shop")
  195.     inst.AnimState:SetBuild("shop_basic")
  196.     inst.AnimState:PlayAnimation("idle", true)  
  197.    
  198.     inst:AddTag("trader")
  199.  
  200.     inst:AddComponent("trader")
  201.     inst.components.trader:SetAcceptTest(ShouldAcceptItem)
  202.     inst.components.trader.onaccept = OnGetItemFromPlayer
  203.    
  204.     inst:AddTag("prototyper")
  205.  
  206.     inst:AddTag("structure")
  207.     MakeObstaclePhysics(inst, .4)
  208.  
  209.     local minimap = inst.entity:AddMiniMapEntity()
  210.     minimap:SetIcon( "shopkeeper.tex" )
  211.    
  212.     inst.entity:SetPristine()
  213.  
  214.     if not TheWorld.ismastersim then
  215.         return inst
  216.     end
  217.  
  218.     inst:AddComponent("inspectable")
  219.    
  220.     inst:AddComponent("talker")
  221.     inst.components.talker.fontsize = 35
  222.     inst.components.talker.font = TALKINGFONT
  223.     inst.components.talker.offset = Vector3(0,-575,0)
  224.    
  225.     inst:AddComponent("prototyper")
  226.     inst.components.prototyper.trees = TUNING.PROTOTYPER_TREES.SHOP_ONE
  227.     inst.components.prototyper.onturnon = OnTurnOn
  228.     inst.components.prototyper.onturnoff = OnTurnOff
  229.     inst.components.prototyper.onactivate = onactivate
  230.  
  231.     inst:AddComponent("wardrobe")
  232.     inst.components.wardrobe:SetCanUseAction(false) --also means NO wardrobe tag!
  233.     inst.components.wardrobe:SetCanBeShared(true)
  234.     inst.components.wardrobe:SetRange(TUNING.RESEARCH_MACHINE_DIST + 1)
  235.        
  236.     inst:AddComponent("hauntable")
  237.     inst.components.hauntable:SetHauntValue(TUNING.HAUNT_TINY)
  238.    
  239.     inst:WatchWorldState("isnight", OnIsNight)
  240.     OnIsNight(inst, TheWorld.state.isnight)
  241.  
  242.     return inst
  243. end
  244.  
  245.  
  246.  
  247. return Prefab( "common/objects/hum_shop", fn, assets, prefabs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement