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

### Blind API

## Set debuffed_by_blind, use it for Matador behavior
## Blind:debuff_card()
[[patches]]
[patches.pattern]
target = 'blind.lua'
pattern = 'card:set_debuff(true)'
position = 'after'
payload = "if card.debuff then card.debuffed_by_blind = true end"
match_indent = true
[[patches]]
[patches.regex]
target = 'blind.lua'
pattern = 'card:set_debuff\(true\); return end'
position = 'at'
payload = """
card:set_debuff(true); if card.debuff then card.debuffed_by_blind = true end; return end"""

## Card:set_debuff()
[[patches]]
[patches.regex]
target = 'card.lua'
pattern = '''
self\.debuff = should_debuff
(?<indent>[\t ]*)end
'''
position = 'after'
payload = """if not self.debuff then self.debuffed_by_blind = false end

"""
line_prepend = '$indent'

## Blind functions

# Blind:set_blind()
[[patches]]
[patches.pattern]
target = 'blind.lua'
pattern = "G.GAME.last_blind = G.GAME.last_blind or {}"
position = 'before'
match_indent = true
payload = '''
local obj = self.config.blind
self.children.animatedSprite.atlas = G.ANIMATION_ATLAS[obj.atlas] or G.ANIMATION_ATLAS['blind_chips']'''

[[patches]]
[patches.pattern]
target = 'blind.lua'
pattern = "if self.name == 'The Eye' and not reset then"
position = 'at'
payload = '''
if blind then
    self.in_blind = true
end
local obj = self.config.blind
if not reset and obj.set_blind and type(obj.set_blind) == 'function' then
    obj:set_blind()
elseif self.name == 'The Eye' and not reset then'''
match_indent = true

# Blind:disable()
[[patches]]
[patches.pattern]
target = 'blind.lua'
pattern = "if self.name == 'The Water' then"
position = 'before'
match_indent = true
payload = '''
local obj = self.config.blind
if obj.disable and type(obj.disable) == 'function' then
    obj:disable()
end'''

# Blind:defeat()
[[patches]]
[patches.pattern]
target = 'blind.lua'
pattern = "if self.name == 'The Manacle' and not self.disabled then"
position = 'before'
match_indent = true
payload = '''
local obj = self.config.blind
if obj.defeat and type(obj.defeat) == 'function' then
    obj:defeat()
end'''

# Blind:debuff_card()
[[patches]]
[patches.pattern]
target = 'blind.lua'
pattern = "if self.debuff and not self.disabled and card.area ~= G.jokers then"
position = 'before'
match_indent = true
payload = '''
local obj = self.config.blind
if not self.disabled and obj.recalc_debuff and type(obj.recalc_debuff) == 'function' then
    if obj:recalc_debuff(card, from_blind) then
        card:set_debuff(true)
        if card.debuff then card.debuffed_by_blind = true end
    else
        card:set_debuff(false)
    end
    return
elseif not self.disabled and obj.debuff_card and type(obj.debuff_card) == 'function' then
    sendWarnMessage(("Blind object %s has debuff_card function, recalc_debuff is preferred"):format(obj.key), obj.set)
    if obj:debuff_card(card, from_blind) then
        card:set_debuff(true)
        if card.debuff then card.debuffed_by_blind = true end
    else
        card:set_debuff(false)
    end
    return
end'''

# Blind:stay_flipped()

[[patches]]
[patches.pattern]
target = 'blind.lua'
pattern = "function Blind:stay_flipped(area, card)"
position = 'at'
match_indent = true
payload = '''function Blind:stay_flipped(area, card, from_area)'''

[[patches]]
[patches.pattern]
target = 'blind.lua'
pattern = "if area == G.hand then"
position = 'before'
match_indent = true
payload = '''
local obj = self.config.blind
if obj.stay_flipped and type(obj.stay_flipped) == 'function' then
    return obj:stay_flipped(area, card, from_area)
end'''


[[patches]]
[patches.pattern]
target = 'cardarea.lua'
pattern = "local stay_flipped = G.GAME and G.GAME.blind and G.GAME.blind:stay_flipped(self, card)"
position = 'at'
match_indent = true
payload = '''local stay_flipped = G.GAME and G.GAME.blind and G.GAME.blind:stay_flipped(self, card, area)'''

[[patches]]
[patches.pattern]
target = 'functions/common_events.lua'
pattern = "local stay_flipped = G.GAME and G.GAME.blind and G.GAME.blind:stay_flipped(to, card)"
position = 'at'
match_indent = true
payload = '''local stay_flipped = G.GAME and G.GAME.blind and G.GAME.blind:stay_flipped(to, card, from)'''

# Blind:drawn_to_hand()
[[patches]]
[patches.regex]
target = 'blind.lua'
pattern = "(?<indent>[\t ]*)if self.name == 'Cerulean Bell' then\n"
position = 'before'
line_prepend = '$indent'
payload = '''
local obj = self.config.blind
if obj.drawn_to_hand and type(obj.drawn_to_hand) == 'function' then
    obj:drawn_to_hand()
end
'''

# Blind:debuff_hand()
[[patches]]
[patches.pattern]
target = 'blind.lua'
pattern = "if self.debuff then"
position = 'before'
match_indent = true
payload = '''
local obj = self.config.blind
if obj.debuff_hand and type(obj.debuff_hand) == 'function' then
    return obj:debuff_hand(cards, hand, handname, check)
end'''

# Blind:modify_hand()
[[patches]]
[patches.pattern]
target = 'blind.lua'
pattern = "if self.disabled then return mult, hand_chips, false end"
position = 'after'
match_indent = true
payload = '''
local obj = self.config.blind
if obj.modify_hand and type(obj.modify_hand) == 'function' then
    return obj:modify_hand(cards, poker_hands, text, mult, hand_chips)
end'''

[[patches]]
[patches.pattern]
target = 'blind.lua'
pattern = "function Blind:modify_hand(cards, poker_hands, text, mult, hand_chips)"
position = 'at'
match_indent = true
payload = '''function Blind:modify_hand(cards, poker_hands, text, mult, hand_chips, scoring_hand)'''

[[patches]]
[patches.pattern]
target = 'functions/state_events.lua'
pattern = "mult, hand_chips, modded = G.GAME.blind:modify_hand(G.play.cards, poker_hands, text, mult, hand_chips)"
position = 'at'
match_indent = true
payload = '''mult, hand_chips, modded = G.GAME.blind:modify_hand(G.play.cards, poker_hands, text, mult, hand_chips, scoring_hand)'''

# Blind:press_play()
[[patches]]
[patches.pattern]
target = 'blind.lua'
pattern = 'if self.name == "The Hook" then'
position = 'before'
match_indent = true
payload = '''
local obj = self.config.blind
if obj.press_play and type(obj.press_play) == 'function' then
    return obj:press_play()
end'''

# Blind:get_loc_debuff_text()
[[patches]]
[patches.pattern]
target = 'blind.lua'
pattern = 'function Blind:get_loc_debuff_text()'
position = 'after'
match_indent = true
payload = '''
    local obj = self.config.blind
    if obj.get_loc_debuff_text and type(obj.get_loc_debuff_text) == 'function' then
        return obj:get_loc_debuff_text()
    end'''

# Blind:set_text()
[[patches]]
[patches.pattern]
target = 'blind.lua'
pattern = "local loc_target = localize{type = 'raw_descriptions', key = self.config.blind.key, set = 'Blind', vars = loc_vars or self.config.blind.vars}"
position = 'at'
match_indent = true
payload = '''
local target = {type = 'raw_descriptions', key = self.config.blind.key, set = 'Blind', vars = loc_vars or self.config.blind.vars}
local obj = self.config.blind
if obj.loc_vars and type(obj.loc_vars) == 'function' then
    local res = obj:loc_vars() or {}
    target.vars = res.vars or target.vars
    target.key = res.key or target.key
end
local loc_target = localize(target)'''

# Blind:load()
[[patches]]
[patches.pattern]
target = 'blind.lua'
pattern = 'if G.P_BLINDS[blindTable.config_blind] then'
position = 'after'
match_indent = true
payload = '''
if self.config.blind.atlas then
    self.children.animatedSprite.atlas = G.ANIMATION_ATLAS[self.config.blind.atlas]
end'''


# create_UIBox_blind_choice()
# create_UIBox_round_scores_row()
[[patches]]
[patches.regex]
target = 'functions/UI_definitions.lua'
pattern = "(?<indent>[\t ]*)blind_choice.animation = AnimatedSprite\\(0,0, 1.4, 1.4, (?<atlas>G.ANIMATION_ATLAS\\['blind_chips'\\]),  blind_choice.config.pos\\)"
position = 'at'
root_capture = 'atlas'
payload = "G.ANIMATION_ATLAS[blind_choice.config.atlas] or G.ANIMATION_ATLAS['blind_chips']"

# create_UIBox_your_collection_blinds()
[[patches]]
[patches.regex]
target = 'functions/UI_definitions.lua'
pattern = "(?<indent>[\t ]*)local temp_blind = AnimatedSprite\\(0,0,1.3,1.3, G.ANIMATION_ATLAS\\['blind_chips'\\], discovered and v.pos or G.b_undiscovered.pos\\)"
position = 'at'
payload = '''

local s = 1.3
if math.ceil(#blind_tab/6) > 6 then
    s = s * 6/math.ceil(#blind_tab/6)
end
local temp_blind = AnimatedSprite(0,0,s,s, G.ANIMATION_ATLAS[discovered and v.atlas or 'blind_chips'], discovered and v.pos or G.b_undiscovered.pos)'''
line_prepend = '$indent'

[[patches]]
[patches.pattern]
target = 'functions/UI_definitions.lua'
pattern = 'blind_matrix[math.ceil((k-1)/5+0.001)][1+((k-1)%5)] = {n=G.UIT.C, config={align = "cm", padding = 0.1}, nodes={'
match_indent = true
position = 'at'
payload = '''
local blinds_per_row = math.ceil(#blind_tab / 6)
local row = math.ceil((k - 1) / blinds_per_row + 0.001)
table.insert(blind_matrix[row], {
    n = G.UIT.C,
    config = { align = "cm", padding = 0.1 },
    nodes = {
        ((k - blinds_per_row) % (2 * blinds_per_row) == 1) and { n = G.UIT.B, config = { h = 0.2, w = 0.5 } } or nil,
        { n = G.UIT.O, config = { object = temp_blind, focus_with_object = true } },
        ((k - blinds_per_row) % (2 * blinds_per_row) == 0) and { n = G.UIT.B, config = { h = 0.2, w = 0.5 } } or nil,
    }
})'''

[[patches]]
[patches.regex]
target = 'functions/UI_definitions.lua'
pattern = '[\t ]*\(k==6 or k ==16 or k == 26\) and \{n=G.UIT.B, config=\{h=0.2,w=0.5\}\} or nil,\n[\t ]*\{n=G.UIT.O, config=\{object = temp_blind, focus_with_object = true\}\},\n[\t ]*\(k==5 or k ==15 or k == 25\) and \{n=G.UIT.B, config=\{h=0.2,w=0.5\}\} or nil,\n[\t ]*\}\}'
position = 'at'
payload = ''

[[patches]]
[patches.pattern]
target = 'functions/UI_definitions.lua'
pattern = 'table.sort(blind_tab, function (a, b) return a.order < b.order end)'
match_indent = true
position = 'at'
payload = '''
table.sort(blind_tab, function(a, b) return a.order + (a.boss and a.boss.showdown and 1000 or 0) < b.order + (b.boss and b.boss.showdown and 1000 or 0) end)'''

# add_round_eval_row()
[[patches]]
[patches.pattern]
target = 'functions/common_events.lua'
pattern = "local blind_sprite = AnimatedSprite(0, 0, 1.2,1.2, G.ANIMATION_ATLAS['blind_chips'], copy_table(G.GAME.blind.pos))"
match_indent = true
position = 'at'
payload = '''
local obj = G.GAME.blind.config.blind
local blind_sprite = AnimatedSprite(0, 0, 1.2, 1.2, G.ANIMATION_ATLAS[obj.atlas] or G.ANIMATION_ATLAS['blind_chips'], copy_table(G.GAME.blind.pos))'''

# display blind descriptions in info_queue
[[patches]]
[patches.pattern]
target = "functions/common_events.lua"
pattern = "elseif _c.set == 'Tarot' then"
position = "before"
match_indent = true
payload = '''elseif _c.set == 'Blind' then
    local coll_loc_vars = (_c.collection_loc_vars and type(_c.collection_loc_vars) == 'function' and _c:collection_loc_vars()) or {}
    loc_vars = coll_loc_vars.vars or _c.vars
    localize{type = 'descriptions', key = coll_loc_vars.key or _c.key, set = _c.set, nodes = desc_nodes, vars = loc_vars}
'''

# create_UIBox_blind_choice()
[[patches]]
[patches.pattern]
target = 'functions/UI_definitions.lua'
pattern = "local loc_target = localize{type = 'raw_descriptions', key = blind_choice.config.key, set = 'Blind', vars = {localize(G.GAME.current_round.most_played_poker_hand, 'poker_hands')}}"
match_indent = true
position = 'at'
payload = '''
local target = {type = 'raw_descriptions', key = blind_choice.config.key, set = 'Blind', vars = {}}
if blind_choice.config.name == 'The Ox' then
       target.vars = {localize(G.GAME.current_round.most_played_poker_hand, 'poker_hands')}
end
local obj = blind_choice.config
if obj.loc_vars and _G['type'](obj.loc_vars) == 'function' then
    local res = obj:loc_vars() or {}
    target.vars = res.vars or target.vars
    target.key = res.key or target.key
end
local loc_target = localize(target)'''

# create_UIBox_blind_popup()
[[patches]]
[patches.pattern]
target = 'functions/UI_definitions.lua'
pattern = '''local loc_target = localize{type = 'raw_descriptions', key = blind.key, set = 'Blind', vars = vars or blind.vars}'''
match_indent = true
position = 'at'
payload = '''
local target = {type = 'raw_descriptions', key = blind.key, set = 'Blind', vars = vars or blind.vars}
if blind.collection_loc_vars and type(blind.collection_loc_vars) == 'function' then
    local res = blind:collection_loc_vars() or {}
    target.vars = res.vars or target.vars
    target.key = res.key or target.key
end
local loc_target = localize(target)'''

# get_new_boss()
[[patches]]
[patches.pattern]
target = 'functions/common_events.lua'
pattern = 'elseif not v.boss.showdown*'
match_indent = true
position = 'before'
payload = '''
elseif v.in_pool and type(v.in_pool) == 'function' then
    local res, options = v:in_pool()
    if
        (
            ((G.GAME.round_resets.ante)%G.GAME.win_ante == 0 and G.GAME.round_resets.ante >= 2) ==
            (v.boss.showdown or false)
        ) or
        (options or {}).ignore_showdown_check
    then
        eligible_bosses[k] = res and true or nil
    end'''

# G.UIDEF.challenge_description_tab
[[patches]]
[patches.pattern]
target = 'functions/UI_definitions.lua'
pattern = "local temp_blind = AnimatedSprite(0,0,1,1, G.ANIMATION_ATLAS['blind_chips'], v.pos)"
position = 'at'
match_indent = true
payload = "local temp_blind = AnimatedSprite(0,0,1,1, G.ANIMATION_ATLAS[v.atlas or ''] or G.ANIMATION_ATLAS['blind_chips'], v.pos)"

## elseif-ify some if chains
[[patches]]
[patches.regex]
target = 'blind.lua'
pattern = "end\n(?<indent>[\t ]*)if self.name == ['\"](?<ability>.*?)['\"]"
position = 'at'
payload = "elseif self.name == '$ability'"

# revert the change for The Pillar
[[patches]]
[patches.regex]
target = 'blind.lua'
pattern = "(?<indent>[\t ]*)elseif self.name == 'The Pillar' and card.ability.played_this_ante then"
position = 'at'
line_prepend = '$indent'
payload = '''end
if self.name == 'The Pillar' and card.ability.played_this_ante then'''

# revert the change for The Eye in debuff_hand
[[patches]]
[patches.regex]
target = 'blind.lua'
pattern = "(?<indent>[\t ]*)elseif self.name == 'The Eye' then"
position = 'at'
line_prepend = '$indent'
payload = '''end
if self.name == 'The Eye' then'''

# revert the change for The Arm in debuff_hand
[[patches]]
[patches.regex]
target = 'blind.lua'
pattern = "(?<indent>[\t ]*)elseif self.name == 'The Arm' then"
position = 'at'
line_prepend = '$indent'
payload = '''end
if self.name == 'The Arm' then'''

# revert the change for Crimson Heart in debuff_card
[[patches]]
[patches.regex]
target = 'blind.lua'
pattern = '''(?<indent>[\t ]*)elseif self\.name == 'Crimson Heart' and not self\.disabled'''
position = 'at'
line_prepend = '$indent'
payload = '''end
if self.name == 'Crimson Heart' and not self.disabled'''