[manifest]
version = "1.0.0"
dump_lua = true
priority = -10

### Seal API
# Card:open()
[[patches]]
[patches.regex]
target = 'card.lua'
pattern = '''
(?<indent>[\t ]*)local seal_rate = 10
[\n\t ]*local seal_poll = pseudorandom\(pseudoseed\('stdseal'..G.GAME.round_resets.ante\)\)
[\n\t ]*if seal_poll > 1 - 0.02\*seal_rate then
[\n\t ]*local seal_type = pseudorandom\(pseudoseed\('stdsealtype'..G.GAME.round_resets.ante\)\)
[\n\t ]*if seal_type > 0.75 then card:set_seal\('Red'\)
[\n\t ]*elseif seal_type > 0.5 then card:set_seal\('Blue'\)
[\n\t ]*elseif seal_type > 0.25 then card:set_seal\('Gold'\)
[\n\t ]*else card:set_seal\('Purple'\)
[\n\t ]*end
[\n\t ]*end'''
position = 'at'
line_prepend = '$indent'
payload = '''
card:set_seal(SMODS.poll_seal({mod = 10}), true, true)'''

# get_badge_colour()
[[patches]]
[patches.pattern]
target = 'functions/UI_definitions.lua'
pattern = 'return G.BADGE_COL[key] or {1, 0, 0, 1}'
position = 'before'
match_indent = true
payload = '''
for k, v in pairs(SMODS.Seals) do
    G.BADGE_COL[k:lower()..'_seal'] = v.badge_colour
end'''

# Card:calculate_seal()
[[patches]]
[patches.regex]
target = "card.lua"
pattern = 'function Card:calculate_seal\(context\)\n(?<indent>[\t ]*)if self.debuff then return nil end'
position = 'after'
line_prepend = '$indent'
payload = '''


local obj = G.P_SEALS[self.seal] or {}
if obj.calculate and type(obj.calculate) == 'function' then
	local o = obj:calculate(self, context)
	if o then
        if not o.card then o.card = self end
        return o
    end
end'''

# Card:update()
[[patches]]
[patches.pattern]
target = "card.lua"
pattern = 'if G.STAGE == G.STAGES.RUN then'
position = 'before'
match_indent = true
payload = '''
local obj = G.P_SEALS[self.seal] or {}
if obj.update and type(obj.update) == 'function' then
    obj:update(self, dt)
end'''

# Card:get_p_dollars()
# Also, Gold Seal respects quantum enhancements
# Patch is here to avoid conflict
[[patches]]
[patches.regex]
target = "card.lua"
pattern = '''(?<indent>[\t ]*)if self\.seal == 'Gold' then'''
position = 'at'
line_prepend = '$indent'
payload = '''
local obj = G.P_SEALS[self.seal] or {}
if obj.get_p_dollars and type(obj.get_p_dollars) == 'function' then
    ret = ret + obj:get_p_dollars(self)
elseif self.seal == 'Gold' and not self.ability.extra_enhancement then'''
# note for later: the Card:get_xxx functions sometimes take context and sometimes don't,
# which is annoying for enhancements
# this should probably be changed to be consistent in better calc

# generate_card_ui()
[[patches]]
[patches.pattern]
target = 'functions/common_events.lua'
pattern = "if v == 'gold_seal'*"
match_indent = true
position = 'before'
payload = '''
local seal = G.P_SEALS[v] or G.P_SEALS[SMODS.Seal.badge_to_key[v] or '']
if seal then
    local t = {key = v, set = 'Other', config = {}}
    info_queue[#info_queue + 1] = t
    if seal.loc_vars and type(seal.loc_vars) == 'function' then
        local res = seal:loc_vars(info_queue, card) or {}
        t.vars = res.vars
        t.key = res.key or t.key
        t.set = res.set or t.set
    end
else'''
[[patches]]
[patches.pattern]
target = 'functions/common_events.lua'
pattern = "if v == 'purple_seal'*"
match_indent = true
position = 'after'
payload = 'end'

[[patches]]
[patches.regex]
target = 'functions/common_events.lua'
position = 'at'
pattern = '''\{key = (?<badge>'.*?_seal'), set = 'Other'\}'''
payload = '''G.P_SEALS[$badge] or G.P_SEALS[SMODS.Seal.badge_to_key[$badge] or '']'''

# Card:update_alert()
[[patches]]
[patches.pattern]
target = 'card.lua'
pattern = "function Card:update_alert()"
match_indent = true
position = 'after'
payload = '''
    if self.ability.set == 'Default' and self.config.center and self.config.center.key == 'c_base' and self.seal then
        if G.P_SEALS[self.seal].alerted and self.children.alert then
            self.children.alert:remove()
            self.children.alert = nil
        elseif not G.P_SEALS[self.seal].alerted and not self.children.alert and G.P_SEALS[self.seal].discovered then
            self.children.alert = UIBox{
                definition = create_UIBox_card_alert(), 
                config = {align="tli",
                        offset = {x = 0.1, y = 0.1},
                        parent = self}
            }
        end
    end'''

# Card:hover()
[[patches]]
[patches.pattern]
target = 'card.lua'
pattern = "G:save_progress()"
match_indent = false
position = "after"
payload = '''
        elseif self.children.alert and self.seal and not G.P_SEALS[self.seal].alerted then
            G.P_SEALS[self.seal].alerted = true
            G:save_progress()'''

# Game:init_item_prototypes()
[[patches]]
[patches.regex]
target = 'game.lua'
pattern = '''(?<indent>[\t ]*)Gold =[ {A-z=1-4,"}\n]*},[\n\t ]*}'''
position = 'at'
line_prepend = '$indent'
payload = '''
Red = {order = 1,  discovered = false, set = "Seal"},
Blue = {order = 2,  discovered = false, set = "Seal"},
Gold = {order = 3,  discovered = false, set = "Seal"},
Purple = {order = 4,  discovered = false, set = "Seal"},
}
'''

# Card:set_seal()
[[patches]]
[patches.pattern]
target = 'card.lua'
pattern = '''G.CONTROLLER.locks.seal = true'''
position = 'after'
match_indent = true
payload = '''local sound = G.P_SEALS[_seal].sound or {sound = 'gold_seal', per = 1.2, vol = 0.4}'''
[[patches]]
[patches.pattern]
target = 'card.lua'
pattern = '''play_sound('gold_seal', 1.2, 0.4)'''
position = 'at'
match_indent = true
payload = '''
self.ability.delay_seal = false
play_sound(sound.sound, sound.per, sound.vol)'''
## Populate Seal Ability Table
[[patches]]
[patches.pattern]
target = 'card.lua'
pattern = '''self.seal = _seal'''
position = 'after'
match_indent = true
payload = '''
self.ability.seal = {}
for k, v in pairs(G.P_SEALS[_seal].config or {}) do
    if type(v) == 'table' then
        self.ability.seal[k] = copy_table(v)
    else
        self.ability.seal[k] = v
    end
end

self.ability.delay_seal = not silent
'''

# card_limit support
[[patches]]
[patches.pattern]
target = 'card.lua'
match_indent = true
position = 'before'
pattern = '''
end
    if self.ability.name == 'Gold Card' and self.seal == 'Gold' and self.playing_card then 
'''
payload = '''
    self.ability.card_limit = self.ability.card_limit + (self.ability.seal.card_limit or 0)
    self.ability.extra_slots_used = self.ability.extra_slots_used + (self.ability.seal.extra_slots_used or 0)
    if self.area then self.area:handle_card_limit(self.ability.seal.card_limit, self.ability.seal.extra_slots_used) end
'''


[[patches]]
[patches.pattern]
target = 'functions/common_events.lua'
pattern = '''new_card:set_seal(other.seal, true)'''
position = 'after'
match_indent = true
payload = '''
if other.seal then
    for k, v in pairs(other.ability.seal or {}) do
        if type(v) == 'table' then
            new_card.ability.seal[k] = copy_table(v)
        else
            new_card.ability.seal[k] = v
        end
    end
end
''' 
