remove usage of "_G"

This commit is contained in:
Chr1Z93 2022-11-24 17:32:29 +01:00
parent 3cf51936f2
commit 5bda3f431b

View File

@ -2,71 +2,55 @@
-- made by: Chr1Z
-- description: for easier managing of the chaos bag (adding / removing tokens)
information = {
version = "1.4",
last_updated = "12.11.2022"
version = "1.5",
last_updated = "24.11.2022"
}
local TOKEN_URL = {
ElderSign = "https://i.imgur.com/nEmqjmj.png",
plusOne = "https://i.imgur.com/uIx8jbY.png",
Zero = "https://i.imgur.com/btEtVfd.png",
minusOne = "https://i.imgur.com/w3XbrCC.png",
minusTwo = "https://i.imgur.com/bfTg2hb.png",
minusThree = "https://i.imgur.com/yfs8gHq.png",
minusFour = "https://i.imgur.com/qrgGQRD.png",
minusFive = "https://i.imgur.com/3Ym1IeG.png",
minusSix = "https://i.imgur.com/c9qdSzS.png",
minusSeven = "https://i.imgur.com/4WRD42n.png",
minusEight = "https://i.imgur.com/9t3rPTQ.png",
Skull = "https://i.imgur.com/stbBxtx.png",
Cultist = "https://i.imgur.com/VzhJJaH.png",
Tablet = "https://i.imgur.com/1plY463.png",
ElderThing = "https://i.imgur.com/ttnspKt.png",
AutoFail = "https://i.imgur.com/lns4fhz.png",
Frost = "http://cloud-3.steamusercontent.com/ugc/1858293462583104677/195F93C063A8881B805CE2FD4767A9718B27B6AE/"
ElderSign = "https://i.imgur.com/nEmqjmj.png",
plusOne = "https://i.imgur.com/uIx8jbY.png",
Zero = "https://i.imgur.com/btEtVfd.png",
minusOne = "https://i.imgur.com/w3XbrCC.png",
minusTwo = "https://i.imgur.com/bfTg2hb.png",
minusThree = "https://i.imgur.com/yfs8gHq.png",
minusFour = "https://i.imgur.com/qrgGQRD.png",
minusFive = "https://i.imgur.com/3Ym1IeG.png",
minusSix = "https://i.imgur.com/c9qdSzS.png",
minusSeven = "https://i.imgur.com/4WRD42n.png",
minusEight = "https://i.imgur.com/9t3rPTQ.png",
Skull = "https://i.imgur.com/stbBxtx.png",
Cultist = "https://i.imgur.com/VzhJJaH.png",
Tablet = "https://i.imgur.com/1plY463.png",
ElderThing = "https://i.imgur.com/ttnspKt.png",
AutoFail = "https://i.imgur.com/lns4fhz.png",
Frost = "http://cloud-3.steamusercontent.com/ugc/1858293462583104677/195F93C063A8881B805CE2FD4767A9718B27B6AE/"
}
local TOKEN_NAMES = {
-- first row
"plusOne", "Zero", "minusOne", "minusTwo", "minusThree", "minusFour",
-- second row
"minusFive", "minusSix", "minusSeven", "minusEight", "Frost",
-- third row
"ElderSign", "Skull", "Cultist", "Tablet", "ElderThing", "AutoFail"
-- first row
"plusOne", "Zero", "minusOne", "minusTwo", "minusThree", "minusFour",
-- second row
"minusFive", "minusSix", "minusSeven", "minusEight", "Frost",
-- third row
"ElderSign", "Skull", "Cultist", "Tablet", "ElderThing", "AutoFail"
}
local BUTTON_TOOLTIP = {
-- first row
"+1", "0", "-1", "-2", "-3", "-4",
-- second row
"-5", "-6", "-7", "-8", "Frost",
-- third row
"Elder Sign", "Skull", "Cultist", "Tablet", "Elder Thing", "Auto-fail"
-- first row
"+1", "0", "-1", "-2", "-3", "-4",
-- second row
"-5", "-6", "-7", "-8", "Frost",
-- third row
"Elder Sign", "Skull", "Cultist", "Tablet", "Elder Thing", "Auto-fail"
}
local tokenarranger = getObjectFromGUID("022907")
local z = { -0.778, 0, 0.75 }
local BUTTON_POSITION = {
-- first row
{ -1.90, 0, z[1] },
{ -1.14, 0, z[1] },
{ -0.38, 0, z[1] },
{ 0.38, 0, z[1] },
{ 1.14, 0, z[1] },
{ 1.90, 0, z[1] },
-- second row
{ -1.90, 0, z[2] },
{ -1.14, 0, z[2] },
{ -0.38, 0, z[2] },
{ 0.38, 0, z[2] },
{ 1.90, 0, z[2] },
-- third row
{ -1.90, 0, z[3] },
{ -1.14, 0, z[3] },
{ -0.38, 0, z[3] },
{ 0.38, 0, z[3] },
{ 1.14, 0, z[3] },
{ 1.90, 0, z[3] },
-- first row
-1.90, -1.14, -0.38, 0.38, 1.14, 1.90,
-- second row
-1.90, -1.14, -0.38, 0.38, 1.90,
-- third row
-1.90, -1.14, -0.38, 0.38, 1.14, 1.90
}
-- common button parameters
@ -76,125 +60,133 @@ buttonParameters.color = { 0, 0, 0, 0 }
buttonParameters.width = 300
buttonParameters.height = 300
local UPDATING = false
local tokenarranger
local name
local tokens = {}
function onLoad()
-- create buttons for tokens
for i = 1, #BUTTON_POSITION do
buttonParameters.position = BUTTON_POSITION[i]
buttonParameters.click_function = attachIndex("button_click", i)
buttonParameters.tooltip = BUTTON_TOOLTIP[i]
self.createButton(buttonParameters)
-- create buttons for tokens
for i = 1, #BUTTON_POSITION do
local funcName = "buttonClick" .. i
self.setVar(funcName, function(_, _, isRightClick) buttonClick(_, _, isRightClick, i) end)
buttonParameters.click_function = funcName
buttonParameters.tooltip = BUTTON_TOOLTIP[i]
buttonParameters.position = { x = BUTTON_POSITION[i], y = 0, z = 0 }
if i < 7 then
buttonParameters.position.z = -0.778
elseif i > 12 then
buttonParameters.position.z = 0.75
end
self.addContextMenuItem("More Information", function()
printToAll("------------------------------", "White")
printToAll("Chaos Bag Manager v" .. information["version"] .. " by Chr1Z", "Orange")
printToAll("last updated: " .. information["last_updated"], "White")
end)
self.createButton(buttonParameters)
end
self.addContextMenuItem("More Information", function()
printToAll("------------------------------", "White")
printToAll("Chaos Bag Manager v" .. information["version"] .. " by Chr1Z", "Orange")
printToAll("last updated: " .. information["last_updated"], "White")
end)
tokenarranger = getObjectFromGUID("022907")
end
-- get chaos bag from scripting zone and description
function getChaosBag()
local chaosbag = nil
local chaosbag_zone = getObjectFromGUID("83ef06")
local chaosbag = nil
local chaosbag_zone = getObjectFromGUID("83ef06")
-- error handling: scripting zone not found
if chaosbag_zone == nil then
printToAll("Zone for chaos bag detection couldn't be found.", "Red")
return nil
end
-- error handling: scripting zone not found
if chaosbag_zone == nil then
printToAll("Zone for chaos bag detection couldn't be found.", "Red")
return nil
end
for _, v in ipairs(chaosbag_zone.getObjects()) do
if v.getDescription() == "Chaos Bag" then
chaosbag = getObjectFromGUID(v.getGUID())
break
end
for _, v in ipairs(chaosbag_zone.getObjects()) do
if v.getDescription() == "Chaos Bag" then
chaosbag = getObjectFromGUID(v.getGUID())
break
end
end
-- error handling: chaos bag not found
if chaosbag == nil then
printToAll("Chaos bag couldn't be found.", "Red")
end
return chaosbag
end
-- helper function to carry index
function attachIndex(click_function, index)
local fn_name = click_function .. index
_G[fn_name] = function(obj, player_color, alt_click)
_G[click_function](obj, player_color, alt_click, index)
end
return fn_name
-- error handling: chaos bag not found
if chaosbag == nil then
printToAll("Chaos bag couldn't be found.", "Red")
end
return chaosbag
end
-- click function for buttons
function button_click(obj, player_color, alt_click, index)
chaosbag = getChaosBag()
function buttonClick(_, _, isRightClick, index)
chaosbag = getChaosBag()
-- error handling: chaos bag not found
if chaosbag == nil then return end
-- error handling: chaos bag not found
if chaosbag == nil then return end
name = BUTTON_TOOLTIP[index]
tokens = {}
for _, v in ipairs(chaosbag.getObjects()) do
if v.name == name then table.insert(tokens, v.guid) end
name = BUTTON_TOOLTIP[index]
tokens = {}
for _, v in ipairs(chaosbag.getObjects()) do
if v.name == name then table.insert(tokens, v.guid) end
end
token = TOKEN_NAMES[index]
if isRightClick then
-- error handling: no matching token found
if #tokens == 0 then
printToAll("No " .. name .. " tokens in the chaos bag.", "Yellow")
return
end
token = TOKEN_NAMES[index]
if alt_click then
-- error handling: no matching token found
if #tokens == 0 then
printToAll("No " .. name .. " tokens in the chaos bag.", "Yellow")
return
end
-- remove token
chaosbag.takeObject({
guid = tokens[1],
position = self.getPosition(),
smooth = false,
callback_function = remove_callback
})
else
-- spawn token (only 8 frost tokens allowed)
if token == "Frost" and #tokens == 8 then
printToAll("The maximum of 8 Frost tokens is already in the bag.", "Yellow")
return
end
local obj = spawnObject({
type = 'Custom_Tile',
position = chaosbag.getPosition() + Vector(0, 1, 0),
rotation = { x = 0, y = 260, z = 0 },
callback_function = spawn_callback
})
obj.setCustomObject({
type = 2,
image = TOKEN_URL[token],
thickness = 0.1
})
-- remove token
chaosbag.takeObject({
guid = tokens[1],
position = self.getPosition(),
smooth = false,
callback_function = removeCallback
})
else
-- spawn token (only 8 frost tokens allowed)
if token == "Frost" and #tokens == 8 then
printToAll("The maximum of 8 Frost tokens is already in the bag.", "Yellow")
return
end
updateTokenArranger()
local obj = spawnObject({
type = 'Custom_Tile',
position = chaosbag.getPosition() + Vector(0, 1, 0),
rotation = { x = 0, y = 260, z = 0 },
callback_function = spawnCallback
})
obj.setCustomObject({
type = 2,
image = TOKEN_URL[token],
thickness = 0.1
})
end
updateTokenArranger()
end
function remove_callback(obj)
printToAll("Removing " .. name .. " token (in bag: " .. #tokens - 1 .. ")", "White")
obj.destruct()
function removeCallback(obj)
printToAll("Removing " .. name .. " token (in bag: " .. #tokens - 1 .. ")", "White")
obj.destruct()
end
function spawn_callback(obj)
obj.scale { 0.81, 1, 0.81 }
obj.setName(name)
printToAll("Adding " .. name .. " token (in bag: " .. #tokens + 1 .. ")", "White")
function spawnCallback(obj)
obj.scale { 0.81, 1, 0.81 }
obj.setName(name)
printToAll("Adding " .. name .. " token (in bag: " .. #tokens + 1 .. ")", "White")
end
UPDATING = false
function updateTokenArranger()
if tokenarranger and not UPDATING then
UPDATING = true
Wait.time(function()
UPDATING = false
tokenarranger.call("layout")
end, 1.5)
end
if tokenarranger and not UPDATING then
UPDATING = true
Wait.time(function()
UPDATING = false
tokenarranger.call("layout")
end, 1.5)
end
end