-
Notifications
You must be signed in to change notification settings - Fork 0
/
dice-rolling-tool.lua
217 lines (200 loc) · 5.36 KB
/
dice-rolling-tool.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
local _dice = {
diceSets = {
[4] = 0,
[6] = 0,
[8] = 0,
[10] = 0,
[12] = 0,
[20] = 0
},
modifier = 0
}
local registeredDice = {}
function onload()
self.createButton(
{
click_function = "calculate",
function_owner = self,
label = "Calculate",
position = {0, 0.25, -1.18},
scale = {0.5, 0.5, 0.5},
width = 3075,
height = 600,
font_size = 400
}
)
self.createButton(
{
click_function = "setDiceVal",
function_owner = self,
label = "Dice",
position = {-1, 0.25, -1.75},
scale = {0.5, 0.5, 0.5},
width = 1175,
height = 450,
font_size = 150,
tooltip = "Dice",
alignment = 3
}
)
self.createButton(
{
click_function = "reset",
function_owner = self,
label = "X",
position = {0, 0.27, -1.75},
scale = {0.5, 0.5, 0.5},
width = 575,
height = 450,
font_size = 250,
color = {0.098, 0.098, 0.098, 1},
font_color = {1, 1, 1, 1},
tooltip = "Reset",
alignment = 3
}
)
self.createInput(
{
input_function = "setModVal",
function_owner = self,
label = "Mod",
position = {1, 0.25, -1.75},
scale = {0.5, 0.5, 0.5},
width = 1175,
height = 450,
font_size = 400,
tooltip = "Mod",
alignment = 3
}
)
end
function onCollisionEnter(info)
if info then
local obj = info.collision_object
if obj then
if obj.type == "Dice" and not registered(obj.guid) then
local sides = #obj.getRotationValues()
_dice.diceSets[sides] = _dice.diceSets[sides] + 1
table.insert(registeredDice, obj.guid)
updateDiceVal()
end
end
end
end
function registered(guid)
for i, v in ipairs(registeredDice) do
if v == guid then
return true
end
end
return false
end
function setDiceVal(obj, player_color)
updateDiceVal()
end
function updateDiceVal()
local str = ""
for k, v in pairs(_dice.diceSets) do
if v > 0 then
if string.len(str) > 0 then
str = str .. "+" .. v .. "d" .. k
else
str = v .. "d" .. k
end
end
end
self.editButton({index = 1, label = str})
end
function setModVal()
_dice.modifier = tonumber(self.getInputs()[1].value)
end
function reset()
_dice.diceSets = {
[4] = 0,
[6] = 0,
[8] = 0,
[10] = 0,
[12] = 0,
[20] = 0
}
_dice.modifier = 0
self.editButton({index = 1, label = ""})
self.editInput({index = 0, value = ""})
self.editButton({index = 0, label = "Calculate"})
self.setDescription("")
registeredDice = {}
end
function calculate(obj, player_clicker_color, alt_click)
local green = "2ECC40"
local red = "AAAAAAaa"
local rolledString = self.getButtons()[2].label
self.setDescription("")
local log = ""
local result = 0
for k, v in pairs(_dice.diceSets) do
if v > 0 then
local subset = calcSet({sides = k, dice = v})
log = log .. "Rolling " .. v .. "d" .. k .. ": (" .. subset.result .. ")\n "
for i = 1, #subset.rolls do
log =
log ..
string.format("([%s]%s[-] [%s]%s[-])", green, subset.rolls[i].roll, red, subset.rolls[i].scrap)
end
log = log .. "\n"
result = result + subset.result
end
end
log =
log ..
string.format("\n[%s][b]Total: %s + %s = %s[/b][-]", green, result, _dice.modifier, result + _dice.modifier)
local inCombat = Global.call("isInCombat")
log(inCombat)
log(type(inCombat))
if inCombat then
-- monitoring purposes
printToColor(
string.format("Rolling %s: %s+%s", rolledString, result, _dice.modifier),
"Black",
{0.666, 0.666, 0.666, 1}
)
end
result = result + _dice.modifier
self.setDescription(log)
self.editButton({index = 0, label = result})
end
function calcSet(set)
local result = 0
local rolls = {}
for i = 1, set.dice do
math.randomseed(math.random(250, 12456))
local roll1 = math.random(1, set.sides)
math.randomseed(math.random(roll1 * math.random(750, 98723)))
local roll2 = math.random(1, set.sides)
local roll = math.max(roll1, roll2)
table.insert(rolls, {roll = roll, scrap = math.min(roll1, roll2)})
result = result + roll
end
return {result = result, rolls = rolls}
end
function split(str, pat)
local t = {}
local fpat = "(.-)" .. pat
local last_end = 1
local s,
e,
cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t, cap)
end
last_end = e + 1
s,
e,
cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end