forked from Foo54/FooBar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
184 lines (174 loc) · 7.17 KB
/
utils.lua
File metadata and controls
184 lines (174 loc) · 7.17 KB
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
--Utility function to check things without erroring | copied from cryptid
---@param t table
---@param ... any
---@return table|false
function FooBar.safe_get(t, ...)
local current = t
for _, k in ipairs({ ... }) do
if not current or current[k] == nil then
return false
end
current = current[k]
end
return current
end
--Utility function to check things without erroring | copied from cryptid
---@param t table
---@param ta table
---@return table|false
function FooBar.safe_get_table(t, ta)
local current = t
for _, k in ipairs(ta) do
if not current or current[k] == nil then
return false
end
current = current[k]
end
return current
end
function FooBar.filter_by_pool (cards, pool)
local ret = {}
for key, c in pairs(cards) do
local pools = FooBar.safe_get(c, "config", "center", "pools")
if pools and pools[pool] then
ret[#ret + 1] = c
end
end
return ret
end
function FooBar.average_probability ()
local card = SMODS.find_card("j_foobar_simplex")[1]
return card and not card.debuff
end
-- Utility function for generating take_ownership calculate effects for jokers that do something every n triggers
---@param key string
---@param condition function
---@param num_table table|nil
---@param dem_table table
---@param effect function
---@return function
function FooBar.generate_average_probability_func_nth_trigger (key, condition, num_table, dem_table, effect)
local ref_card = SMODS.Joker:get_obj(key).calculate or function() end
return function(self, card, context)
if condition(self, card, context) and FooBar.average_probability() then
local num, dem = SMODS.get_probability_vars(card, FooBar.safe_get_table(card.ability, num_table) or 1, FooBar.safe_get_table(card.ability, dem_table) or 2, key)
card.ability.foobar_counter = ((card.ability.foobar_counter or -1) + 1) % dem
if card.ability.foobar_counter < num then
return effect(self, card, context)
else
return {}
end
end
return ref_card(self, card, context)
end
end
-- Utility function for generating loc_vars effects for jokers that do something every n triggers
---@param key string
---@param num_table table|nil
---@param dem_table table
---@param loc_vars_key string
---@param loc_vars_table table|nil
---@return function
function FooBar.generate_average_probability_nth_trigger_loc_vars (key, num_table, dem_table, loc_vars_key, loc_vars_table)
local ref_card = SMODS.Joker:get_obj(key).loc_vars or function() end
return function(self, info_queue, card)
if FooBar.average_probability() then
local num, dem = SMODS.get_probability_vars(card, FooBar.safe_get_table(card.ability, num_table) or 1, FooBar.safe_get_table(card.ability, dem_table) or 2, key)
local ret = {num, dem, (card.ability.foobar_counter or 0) + 1}
for i, v in ipairs(loc_vars_table or {}) do
ret[#ret + 1] = FooBar.safe_get_table(card.ability, loc_vars_table)
end
return {key = loc_vars_key, vars = ret}
end
return ref_card(self, info_queue, card)
end
end
-- Utility function for calling take_ownership for jokers that do something every n triggers
---@param key string
---@param condition function
---@param num_table table|nil
---@param dem_table table
---@param effect function
---@param loc_vars_key string
---@param loc_vars_table table|nil
---@return function
function FooBar.take_ownership_nth_trigger (key, condition, num_table, dem_table, effect, loc_vars_key, loc_vars_table)
SMODS.Joker:take_ownership(key, {
calculate = FooBar.generate_average_probability_func_nth_trigger("j_" .. key, condition, num_table, dem_table, effect),
loc_vars = FooBar.generate_average_probability_nth_trigger_loc_vars("j_" .. key, num_table, dem_table, loc_vars_key, loc_vars_table),
}, true)
end
-- Utility function for generating take_ownership calculate effects for jokers have an expected value
---@param key string
---@param condition function
---@param num_table table
---@param dem_table table
---@param fail table
---@param succeed table
---@return function
function FooBar.generate_average_probability_func_expected_value (key, condition, num_table, dem_table, fail, succeed)
local ref_card = SMODS.Joker:get_obj(key).calculate or function() end
return function(self, card, context)
if condition(self, card, context) and FooBar.average_probability() then
local ret = {}
for index, value in ipairs(dem_table) do
local num, dem = SMODS.get_probability_vars(card, FooBar.safe_get_table(card.ability, num_table[index]) or 1, FooBar.safe_get_table(card.ability, dem_table[index]) or 2, key)
num = math.min(dem, num)
local suc, fal = succeed[index].value, fail[index].value
if suc == nil then suc = FooBar.safe_get_table(card.ability, succeed[index].path) end
if fal == nil then fal = FooBar.safe_get_table(card.ability, fail[index].path) end
local v = (num * suc + (dem - num) * fal) / dem
ret[succeed[index].key] = v
end
return ret
end
return ref_card(self, card, context)
end
end
-- Utility function for generating loc_vars effects for jokers that do something every n triggers
---@param key string
---@param num_table table
---@param dem_table table
---@param fail table
---@param succeed table
---@param loc_vars_key string
---@param loc_vars_table table|nil
---@return function
function FooBar.generate_average_probability_expected_value_loc_vars (key, num_table, dem_table, fail, succeed, loc_vars_key, loc_vars_table)
local ref_card = SMODS.Joker:get_obj(key).loc_vars or function() end
return function(self, info_queue, card)
if FooBar.average_probability() then
local ret = {}
for index, value in ipairs(dem_table) do
local num, dem = SMODS.get_probability_vars(card, FooBar.safe_get_table(card.ability, num_table[index]) or 1, FooBar.safe_get_table(card.ability, dem_table[index]) or 2, key)
num = math.min(dem, num)
local suc, fal = succeed[index].value, fail[index].value
if suc == nil then suc = FooBar.safe_get_table(card.ability, succeed[index].path) end
if fal == nil then fal = FooBar.safe_get_table(card.ability, fail[index].path) end
local v = (num * suc + (dem - num) * fal) / dem
ret[#ret + 1] = v
end
for i, v in ipairs(loc_vars_table or {}) do
ret[#ret + 1] = FooBar.safe_get_table(card.ability, loc_vars_table)
end
return {key = loc_vars_key, vars = ret}
end
return ref_card(self, info_queue, card)
end
end
-- Utility function for calling take_ownership for jokers that have an expected value
---@param key string
---@param condition function
---@param num_table table
---@param dem_table table
---@param fail table
---@param succeed table
---@param loc_vars_key string
---@param loc_vars_table table|nil
---@return function
function FooBar.take_ownership_expected_value (key, condition, num_table, dem_table, fail, succeed, loc_vars_key, loc_vars_table)
SMODS.Joker:take_ownership(key, {
calculate = FooBar.generate_average_probability_func_expected_value("j_" .. key, condition, num_table, dem_table, fail, succeed),
loc_vars = FooBar.generate_average_probability_expected_value_loc_vars("j_" .. key, num_table, dem_table, fail, succeed, loc_vars_key, loc_vars_table),
}, true)
end