Advertisement
Kijan

SurvBag

Aug 28th, 2017
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.96 KB | None | 0 0
  1. -- Created By: Kijan
  2. -- inspired by Memory Bags Created By: MrStump
  3.  
  4.  
  5. function onload(saved_data)
  6.   if saved_data ~= "" then
  7.     local loaded_data = JSON.decode(saved_data)
  8.     --Set up information off of loaded_data
  9.     memoryList = loaded_data.ml
  10.     createCardButtons()
  11.   else
  12.     --Set up information for if there is no saved saved data
  13.     memoryList = {}
  14.     memoryList.lists = {sfa = {}, fa = {}, dis = {}, abi = {}, inj = {}, stats = {}}
  15.   end
  16.   createButtons()
  17.  
  18. end
  19.  
  20. function updateSave()
  21.   local data_to_save = {["ml"] = memoryList}
  22.   saved_data = JSON.encode(data_to_save)
  23.   self.script_state = saved_data
  24. end
  25.  
  26.  
  27. function createCardButtons()
  28.   self.createButton({
  29.     label = self.getName(), click_function = "dud", function_owner = self,
  30.     position = {0, 0.3, 2.5}, rotation = {0, 180, 0}, height = 0, width = 0,
  31.     font_size = 600, color = {0, 0, 0}, font_color = {1, 1, 1}
  32.   })
  33.   local height = 0
  34.   local width = 0
  35.   local font_size = 300
  36.   local x, y, zl = 5, 0.3, 1
  37.   local dz = 0.7
  38.   local zr = zl
  39.   local buttonformat = {
  40.     click_function = "dud", function_owner = self,
  41.     rotation = {0, 180, 0}, height = height, width = width,
  42.     font_size = font_size, color = {0, 0, 0, 0}, font_color = {1, 1, 1, 255}
  43.   }
  44.  
  45.   if #memoryList.lists.sfa + #memoryList.lists.fa > 0 then
  46.     buttonformat.font_color = {1, 1, 1, 255}
  47.     buttonformat.label = "[u]Fighting Arts[/u]"
  48.     buttonformat.position = {x, y, zl}
  49.     self.createButton(buttonformat)
  50.     zl = zl - dz
  51.  
  52.     buttonformat.font_color = {1, 0.3, 0, 255}
  53.     for _, v in pairs(memoryList.lists.sfa) do
  54.       buttonformat.label = v
  55.       buttonformat.position = {x, y, zl}
  56.       self.createButton(buttonformat)
  57.       zl = zl - dz
  58.     end
  59.     buttonformat.font_color = {0.8, 0.8, 0, 255}
  60.     for _, v in pairs(memoryList.lists.fa) do
  61.       buttonformat.label = v
  62.       buttonformat.position = {x, y, zl}
  63.       self.createButton(buttonformat)
  64.       zl = zl - dz
  65.     end
  66.     zl = zl - dz / 2
  67.   end
  68.   if #memoryList.lists.abi > 0 then
  69.     buttonformat.font_color = {1, 1, 1, 255}
  70.     buttonformat.label = "[u]Abilities[/u]"
  71.     buttonformat.position = {x, y, zl}
  72.     self.createButton(buttonformat)
  73.     zl = zl - dz
  74.  
  75.     buttonformat.font_color = {0, 1, 0, 255}
  76.     for _, v in pairs(memoryList.lists.abi) do
  77.       buttonformat.label = v
  78.       buttonformat.position = {x, y, zl}
  79.       self.createButton(buttonformat)
  80.       zl = zl - dz
  81.     end
  82.   end
  83.  
  84.   if #memoryList.lists.dis > 0 then
  85.     buttonformat.font_color = {1, 1, 1, 255}
  86.     buttonformat.label = "[u]Disorders[/u]"
  87.     buttonformat.position = {0 - x, y, zr}
  88.     self.createButton(buttonformat)
  89.     zr = zr - dz
  90.     buttonformat.font_color = {0, 0.5, 1, 255}
  91.     for _, v in pairs(memoryList.lists.dis) do
  92.       buttonformat.label = v
  93.       buttonformat.position = {0 - x, y, zr}
  94.       self.createButton(buttonformat)
  95.       zr = zr - dz
  96.     end
  97.     zr = zr - dz / 2
  98.   end
  99.   if #memoryList.lists.inj > 0 then
  100.     buttonformat.font_color = {1, 1, 1, 255}
  101.     buttonformat.label = "[u]Injuries[/u]"
  102.     buttonformat.position = {0 - x, y, zr}
  103.     self.createButton(buttonformat)
  104.     zr = zr - dz
  105.  
  106.     buttonformat.font_color = {1, 0, 0, 255}
  107.     for _, v in pairs(memoryList.lists.inj) do
  108.       buttonformat.label = v
  109.       buttonformat.position = {0 - x, y, zr}
  110.       self.createButton(buttonformat)
  111.       zr = zr - dz
  112.     end
  113.   end
  114.  
  115.   if memoryList.lists.stats == nil then memoryList.lists.stats = {} end
  116.   zr = -3
  117.   buttonformat.font_color = {0.6, 0.6, 0.6, 255}
  118.   for i, v in pairs(memoryList.lists.stats) do
  119.     buttonformat.label = i .. ": " .. v
  120.     buttonformat.position = {0, y, zr}
  121.     self.createButton(buttonformat)
  122.     zr = zr - dz
  123.   end
  124. end
  125.  
  126.  
  127. --Make setup button
  128. function createButtons()
  129.   dx = 1.5
  130.   height = 500
  131.   width = 500
  132.   font_size = 300
  133.   self.createButton({
  134.     label = "", click_function = "buttonClick_red", function_owner = self,
  135.     position = {dx, 0.3, - 2}, rotation = {0, 180, 0}, height = height, width = width,
  136.     font_size = font_size, color = {1, 0, 0}, font_color = {0, 0, 0}
  137.   })
  138.   self.createButton({
  139.     label = "", click_function = "buttonClick_yellow", function_owner = self,
  140.     position = {dx - 1, 0.3, - 2}, rotation = {0, 180, 0}, height = height, width = width,
  141.     font_size = font_size, color = {1, 1, 0}, font_color = {0, 0, 0}
  142.   })
  143.   self.createButton({
  144.     label = "", click_function = "buttonClick_green", function_owner = self,
  145.     position = {dx - 2, 0.3, - 2}, rotation = {0, 180, 0}, height = height, width = width,
  146.     font_size = font_size, color = {0, 1, 0}, font_color = {0, 0, 0}
  147.   })
  148.   self.createButton({
  149.     label = "", click_function = "buttonClick_blue", function_owner = self,
  150.     position = {dx - 3, 0.3, - 2}, rotation = {0, 180, 0}, height = height, width = width,
  151.     font_size = font_size, color = {0, 0, 1}, font_color = {0, 0, 0}
  152.   })
  153.  
  154. end
  155.  
  156. function recallZone(player)
  157.   -- playerZoneGUID, gridGUID
  158.   objects = getObjectFromGUID(player[1]).getObjects()
  159.   grid = getObjectFromGUID(player[2])
  160.  
  161.  
  162.   for _, obj in pairs(objects) do
  163.     if obj.tag == 'Board' then
  164.       if obj.getDescription() == "SurvivorSheet" then
  165.         self.setName(obj.getName())
  166.         self.setDescription(getStats(obj.getTable("buttons")))
  167.         --obj.setUnlock()
  168.         addToList(obj, grid)
  169.       end
  170.     elseif obj.tag == 'Card' and not obj.getDescription():find("set") then
  171.       if player[3] then
  172.         if string.find(obj.getDescription(), 'irreplaceable') then
  173.           broadcastToAll("[b]" .. obj.getName() .. "[/b] was Irreplaceable and is lost forever.", {1, 1, 1})
  174.           grid.call("deleteCard", {obj.getGUID()})
  175.         else
  176.           addToList(obj, grid)
  177.         end
  178.       else
  179.         addToList(obj, grid)
  180.       end
  181.     end
  182.   end
  183.  
  184.   if player[3] then
  185.     broadcastToAll("[b]" .. self.getName() .. "[/b] is dead.", {1, 1, 1})
  186.   else
  187.     broadcastToAll("[b]" .. self.getName() .. "[/b] is resting in the Settlement", {1, 1, 1})
  188.   end
  189.  
  190.   buttonClick_recall()
  191.   updateSave()
  192. end
  193.  
  194. function addToList(obj, grid)
  195.   local desc = obj.getDescription()
  196.   local cursed = false
  197.  
  198.   if string.find(desc, 'cursed') then
  199.     cursed = true
  200.   end
  201.   if not string.find(desc, 'gear') or cursed then
  202.     local pos, rot = obj.getPosition(), obj.getRotation()
  203.     gridPos = grid.getPosition()
  204.     memoryList[obj.getGUID()] = {
  205.       pos = {x = round(gridPos.x - pos.x, 4), y = round(gridPos.y - pos.y, 4), z = round(gridPos.z - pos.z, 4)},
  206.       rot = {x = round(rot.x, 4), y = round(rot.y, 4), z = round(rot.z, 4)},
  207.       lock = obj.getLock(), cursed = cursed
  208.     }
  209.     if string.find(desc, 'secret fighting art') then
  210.       memoryList.lists.sfa[#memoryList.lists.sfa + 1] = obj.getName()
  211.     elseif string.find(desc, 'fighting art') then
  212.       memoryList.lists.fa[#memoryList.lists.fa + 1] = obj.getName()
  213.     elseif string.find(desc, 'disorder') then
  214.       memoryList.lists.dis[#memoryList.lists.dis + 1] = obj.getName()
  215.     elseif string.find(desc, 'injury') then
  216.       memoryList.lists.inj[#memoryList.lists.inj + 1] = obj.getName()
  217.     elseif string.find(desc, 'ability') then
  218.       memoryList.lists.abi[#memoryList.lists.abi + 1] = obj.getName()
  219.     end
  220.   end
  221. end
  222.  
  223. function getStats(objbtns)
  224.   local stats = ""
  225.   local xp = 0
  226.   local crg = 0
  227.   local und = 0
  228.   local prof = 0
  229.   for i, v in pairs(objbtns) do
  230.     if i == "movement" then
  231.       memoryList.lists.stats['MOV'] = objbtns[tostring(i)].value
  232.     elseif i == "accuracy" then
  233.       memoryList.lists.stats['ACC'] = objbtns[tostring(i)].value
  234.     elseif i == "strength" then
  235.       memoryList.lists.stats['STR'] = objbtns[tostring(i)].value
  236.     elseif i == "evasion" then
  237.       memoryList.lists.stats['EVA'] = objbtns[tostring(i)].value
  238.     elseif i == "luck" then
  239.       memoryList.lists.stats['LCK'] = objbtns[tostring(i)].value
  240.     elseif i == "speed" then
  241.       memoryList.lists.stats['SPD'] = objbtns[tostring(i)].value
  242.     elseif i == "survival" then
  243.       memoryList.lists.stats['SRV'] = objbtns[tostring(i)].value
  244.     elseif i == "insanity" then
  245.       memoryList.lists.stats['INS'] = objbtns[tostring(i)].value
  246.     end
  247.  
  248.     xp = xp + getByPrefix("xp_", i, objbtns)
  249.     crg = crg + getByPrefix("courage_", i, objbtns)
  250.     und = und + getByPrefix("understanding_", i, objbtns)
  251.     prof = prof + getByPrefix("proficiency_", i, objbtns)
  252.  
  253.   end
  254.  
  255.   memoryList.lists.stats['  XP'] = tostring(xp)
  256.   memoryList.lists.stats['CRG'] = tostring(crg)
  257.   memoryList.lists.stats['UND'] = tostring(und)
  258.   memoryList.lists.stats['PRF'] = tostring(prof)
  259.   return stats
  260. end
  261.  
  262. function getByPrefix(s, i, objbtns)
  263.   prefix = string.sub(i, 1, s:len())
  264.   if prefix == s then
  265.     if objbtns[tostring(i)].label ~= '' then
  266.       return 1
  267.     end
  268.   end
  269.   return 0
  270. end
  271.  
  272. function buttonClick_red()
  273.   local count = 0
  274.   for guid, entry in pairs(memoryList) do
  275.     count = count + 1
  276.   end
  277.   if count < 2 then
  278.     recallZone({Global.getTable("zones").zoneP1.guid, Global.getTable("grids").red.guid})
  279.   else
  280.     buttonClick_place(getObjectFromGUID(Global.getTable('grids').red.guid))
  281.   end
  282. end
  283. function buttonClick_yellow()
  284.   local count = 0
  285.   for guid, entry in pairs(memoryList) do
  286.     count = count + 1
  287.   end
  288.   if count < 2 then
  289.     recallZone({Global.getTable("zones").zoneP2.guid, Global.getTable("grids").yellow.guid})
  290.   else
  291.     buttonClick_place(getObjectFromGUID(Global.getTable('grids').yellow.guid))
  292.   end
  293. end
  294. function buttonClick_green()
  295.   local count = 0
  296.   for guid, entry in pairs(memoryList) do
  297.     count = count + 1
  298.   end
  299.   if count < 2 then
  300.     recallZone({Global.getTable("zones").zoneP3.guid, Global.getTable("grids").green.guid})
  301.   else
  302.     buttonClick_place(getObjectFromGUID(Global.getTable('grids').green.guid))
  303.   end
  304. end
  305. function buttonClick_blue()
  306.   local count = 0
  307.   for guid, entry in pairs(memoryList) do
  308.     count = count + 1
  309.   end
  310.   if count < 2 then
  311.     recallZone({Global.getTable("zones").zoneP4.guid, Global.getTable("grids").blue.guid})
  312.   else
  313.     buttonClick_place(getObjectFromGUID(Global.getTable('grids').blue.guid))
  314.   end
  315. end
  316.  
  317.  
  318. --Sends objects from bag/table to their saved position/rotation
  319. function buttonClick_place(grid)
  320.   if grid.getVar("FullCharSheetGUID") == '' or grid.getVar("FullCharSheetGUID") == nil then
  321.  
  322.     gridPos = grid.getPosition()
  323.     local bagObjList = self.getObjects()
  324.     for guid, entry in pairs(memoryList) do
  325.       if guid ~= "lists" then
  326.         local obj = getObjectFromGUID(guid)
  327.         --If obj is inside of the bag
  328.         for _, bagObj in ipairs(bagObjList) do
  329.           if bagObj.guid == guid then
  330.             if entry.cursed then
  331.               local item = self.takeObject({
  332.                 guid = guid, position = {gridPos.x - 3, gridPos.y + 2, gridPos.z}, rotation = entry.rot, smooth = false,
  333.               })
  334.               item.setLock(entry.lock)
  335.             else
  336.               local item = self.takeObject({
  337.                 guid = guid, position = {gridPos.x - entry.pos.x, gridPos.y - entry.pos.y + 0.5, gridPos.z - entry.pos.z}, rotation = entry.rot, smooth = false,
  338.               })
  339.               item.setLock(entry.lock)
  340.             end
  341.  
  342.             break
  343.           end
  344.         end
  345.       end
  346.     end
  347.  
  348.     broadcastToAll("[b]" .. self.getName() .. "[/b] embarks on the Hunt!", {1, 1, 1})
  349.     memoryList = {}
  350.     updateSave()
  351.     objects = self.getObjects()
  352.     if #objects > 0 then
  353.       broadcastToAll("There are still cards in the bag.", {1, 1, 1})
  354.     else
  355.       self.destruct()
  356.     end
  357.   else
  358.     broadcastToAll("This Grid already has a Survivor!", {1, 1, 1})
  359.   end
  360. end
  361.  
  362. --Recalls objects to bag from table
  363. function buttonClick_recall()
  364.   for guid, entry in pairs(memoryList) do
  365.     local obj = getObjectFromGUID(guid)
  366.     if obj ~= nil then self.putObject(obj) end
  367.   end
  368.  
  369.   createCardButtons()
  370.  
  371. end
  372. --Utility functions
  373. function dud()
  374. end
  375.  
  376. --Round number (num) to the Nth decimal (dec)
  377. function round(num, dec)
  378.   local mult = 10^(dec or 0)
  379.   return math.floor(num * mult + 0.5) / mult
  380. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement