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

## no_rank, no_suit, all_suits

# Card:get_id()
[[patches]]
[patches.pattern]
target = "card.lua"
pattern = "if self.ability.effect == 'Stone Card' and not self.vampired then"
match_indent = true
position = "at"
payload = '''if SMODS.has_no_rank(self) and not self.vampired then'''

# Card:get_chip_bonus()
[[patches]]
[patches.regex]
target = "card.lua"
pattern = '''
(?<indent>[\t ]*)if self\.ability\.effect == 'Stone Card' then
[\t ]*    return self\.ability\.bonus \+ \(self\.ability\.perma_bonus or 0\)
[\t ]*end'''
position = "at"
payload = '''
if self.ability.effect == 'Stone Card' or self.config.center.replace_base_card then
    return self.ability.bonus + (self.ability.perma_bonus or 0)
end'''
line_prepend = '$indent'

# Card:calculate_joker()
# Raised Fist
[[patches]]
[patches.pattern]
target = "card.lua"
pattern = "if temp_ID >= G.hand.cards[i].base.id and G.hand.cards[i].ability.effect ~= 'Stone Card' then temp_Mult = G.hand.cards[i].base.nominal; temp_ID = G.hand.cards[i].base.id; raised_card = G.hand.cards[i] end"
match_indent = true
position = "at"
payload = """if temp_ID >= G.hand.cards[i].base.id and not SMODS.has_no_rank(G.hand.cards[i]) then 
    temp_Mult = G.hand.cards[i].base.nominal
    temp_ID = G.hand.cards[i].base.id
    raised_card = G.hand.cards[i]
end"""
# Flower Pot, Seeing Double
[[patches]]
[patches.pattern]
target = "card.lua"
pattern = "if context.scoring_hand[i].ability.name ~= 'Wild Card' then"
match_indent = true
position = "at"
payload = '''if not SMODS.has_any_suit(context.scoring_hand[i]) then'''

[[patches]]
[patches.pattern]
target = "card.lua"
pattern = "if context.scoring_hand[i].ability.name == 'Wild Card' then"
match_indent = true
position = "at"
payload = '''if SMODS.has_any_suit(context.scoring_hand[i]) then'''

# Card:get_suit()
[[patches]]
[patches.regex]
target = "card.lua"
pattern = '''(?<indent>[\t ]*)if self\.ability\.effect == 'Stone Card' then'''
line_prepend = '$indent'
position = "at"
payload = '''if SMODS.has_no_suit(self) then'''
[[patches]]
[patches.pattern]
target = "card.lua"
pattern = 'if self.ability.name == "Wild Card" then'
match_indent = true
position = "at"
payload = '''if SMODS.has_any_suit(self) then'''
[[patches]]
[patches.pattern]
target = "card.lua"
pattern = 'if self.ability.name == "Wild Card" and not self.debuff then'
match_indent = true
position = "at"
payload = '''if SMODS.has_any_suit(self) and not self.debuff then'''

# check_for_unlock
[[patches]]
[patches.pattern]
target = "functions/common_events.lua"
pattern = "if v.ability.name ~= 'Stone Card' and v.base.suit == 'Hearts' then"
match_indent = true
position = "at"
payload = "if not SMODS.has_no_suit(v) and v.base.suit == 'Hearts' then"

# reset_idol_card()
[[patches]]
[patches.pattern]
target = "functions/common_events.lua"
pattern = "valid_idol_cards[#valid_idol_cards+1] = v"
match_indent = true
position = "at"
payload = """if not SMODS.has_no_suit(v) and not SMODS.has_no_rank(v) then
    valid_idol_cards[#valid_idol_cards+1] = v
end"""

# reset_mail_rank()
[[patches]]
[patches.pattern]
target = "functions/common_events.lua"
pattern = "valid_mail_cards[#valid_mail_cards+1] = v"
match_indent = true
position = "at"
payload = """if not SMODS.has_no_rank(v) then
    valid_mail_cards[#valid_mail_cards+1] = v
end"""

# reset_castle_card()
[[patches]]
[patches.pattern]
target = "functions/common_events.lua"
pattern = "valid_castle_cards[#valid_castle_cards+1] = v"
match_indent = true
position = "at"
payload = """if not SMODS.has_no_suit(v) then
    valid_castle_cards[#valid_castle_cards+1] = v
end"""

# G.FUNCS.evaluate_play()
[[patches]]
[patches.pattern]
target = "functions/state_events.lua"
pattern = "if G.play.cards[i].ability.effect == 'Stone Card' then"
match_indent = true
position = "at"
payload = '''if SMODS.always_scores(G.play.cards[i]) then'''
[[patches]]
[patches.pattern]
target = "functions/state_events.lua"
pattern = "if scoring_hand[i].ability.effect ~= 'Stone Card' then"
match_indent = true
position = "at"
payload = '''if not SMODS.has_no_rank(scoring_hand[i]) then'''
[[patches]]
[patches.pattern]
target = "functions/state_events.lua"
pattern = "G.GAME.cards_played[scoring_hand[i].base.value].suits[scoring_hand[i].base.suit] = true"
match_indent = true
position = "at"
payload = """if not SMODS.has_no_suit(scoring_hand[i]) then
    G.GAME.cards_played[scoring_hand[i].base.value].suits[scoring_hand[i].base.suit] = true
end"""


## replace_base_card
# Determines whether to draw the base card's front or not
# Card:draw()
[[patches]]
[patches.pattern]
target = "card.lua"
pattern = "if self.children.front and self.ability.effect ~= 'Stone Card' then"
match_indent = true
position = "at"
payload = "if self.children.front and self.ability.effect ~= 'Stone Card' and not self.config.center.replace_base_card then"

# Card:generate_UIBox_ability_table()
# replaces two consecutive lines
[[patches]]
[patches.pattern]
target = "functions/common_events.lua"
pattern = "if (_c.name == 'Stone Card') then full_UI_table.name = true end"
match_indent = true
position = "at"
payload = "if _c.name == 'Stone Card' or _c.replace_base_card then full_UI_table.name = true"
[[patches]]
[patches.pattern]
target = "functions/common_events.lua"
pattern = "if (specific_vars.playing_card and (_c.name ~= 'Stone Card')) then"
match_indent = true
position = "at"
payload = "elseif specific_vars.playing_card then"

# eval_card()
[[patches]]
[patches.pattern]
target = "functions/common_events.lua"
pattern = "function eval_card(card, context)"
match_indent = true
position = "after"
payload = """
    local enhancement_calculated = false
    local center = card.config.center"""
[[patches]]
[patches.pattern]
target = "functions/common_events.lua"
pattern = "local jokers = card:calculate_joker(context)"
match_indent = true
position = "before"
payload = """
if card.ability.set == 'Enhanced' and center.calculate and type(center.calculate) == 'function' then
    center:calculate(card, context, ret)
    enhancement_calculated = true
end"""
[[patches]]
[patches.pattern]
target = "functions/common_events.lua"
pattern = "local seals = card:calculate_seal(context)"
match_indent = true
position = "before"
payload = """
if card.ability.set == 'Enhanced' and center.calculate and type(center.calculate) == 'function' then 
    center:calculate(card, context, ret)
    enhancement_calculated = true
end"""
[[patches]]
[patches.pattern]
target = "functions/common_events.lua"
pattern = "if context.cardarea == G.jokers or context.card == G.consumeables then"
match_indent = true
position = "before"
payload = """
if not enhancement_calculated and card.ability.set == 'Enhanced' and center.calculate and type(center.calculate) == 'function' then 
    center:calculate(card, context, ret)
    enhancement_calculated = true
end
local seals = not card.ability.extra_enhancement and card:calculate_seal(context)
if seals then
    ret.seals = seals
end"""


## Add additional eval_card() calls

# Game:update_draw_to_hand()

[[patches]]
[patches.pattern]
target = "game.lua"
pattern = "G.GAME.current_round.discards_used == 0 and G.GAME.facing_blind then"
match_indent = true
position = "after"
payload = """
for i = 1, #G.hand.cards do
    eval_card(G.hand.cards[i], {first_hand_drawn = true})
end"""

# G.FUNCS.discard_cards_from_highlighted()

[[patches]]
[patches.pattern]
target = "functions/state_events.lua"
pattern = "inc_career_stat('c_cards_discarded', highlighted_count)"
match_indent = true
position = "after"
payload = """
for i = 1, #G.hand.cards do
    eval_card(G.hand.cards[i], {pre_discard = true, full_hand = G.hand.highlighted, hook = hook})
end"""
[[patches]]
[patches.regex]
target = "functions/state_events.lua"
pattern = '''
(?<indent>[\t ]*)G\.hand\.highlighted\[i\]:calculate_seal\(\{discard = true\}\)
[\t ]*local removed = false'''
line_prepend = '$indent'
position = "at"
payload = """
local removed = false
local eval = nil
eval = eval_card(G.hand.highlighted[i], {discard = true, full_hand = G.hand.highlighted})
if eval and eval.remove then
    removed = true
    card_eval_status_text(G.hand.highlighted[i], 'jokers', nil, 1, nil, eval)
end"""

## Allow cards to function as multiple enhancements (e.g. from jokers)

# Calculate extra enhancements when played
[[patches]]
[patches.pattern]
target = "functions/state_events.lua"
pattern = "local effects = {eval_card(scoring_hand[i], {cardarea = G.play, full_hand = G.play.cards, scoring_hand = scoring_hand, poker_hand = text})}"
position = "after"
payload = '''
local post_effect = {seals = effects[1].seals, edition = effects[1].edition}
effects[1].seals = nil
effects[1].edition = nil
local extra_enhancements = SMODS.get_enhancements(scoring_hand[i], true)
local old_ability = copy_table(scoring_hand[i].ability)
local old_center = scoring_hand[i].config.center
local old_center_key = scoring_hand[i].config.center_key
for k, _ in pairs(extra_enhancements) do
    if G.P_CENTERS[k] then
        scoring_hand[i]:set_ability(G.P_CENTERS[k])
        scoring_hand[i].ability.extra_enhancement = k
        effects[#effects+1] = eval_card(scoring_hand[i], {cardarea = G.play, full_hand = G.play.cards, scoring_hand = scoring_hand, poker_hand = text, extra_enhancement = true})
    end
end
effects[#effects+1] = post_effect
scoring_hand[i].ability = old_ability
scoring_hand[i].config.center = old_center
scoring_hand[i].config.center_key = old_center_key
scoring_hand[i]:set_sprites(old_center)
'''
match_indent = true

# Calculate extra enhancements when held in hand
[[patches]]
[patches.pattern]
target = "functions/state_events.lua"
pattern = "local effects = {eval_card(G.hand.cards[i], {cardarea = G.hand, full_hand = G.play.cards, scoring_hand = scoring_hand, scoring_name = text, poker_hands = poker_hands})}"
position = "after"
payload = '''
local extra_enhancements = SMODS.get_enhancements(G.hand.cards[i], true)
local old_ability = copy_table(G.hand.cards[i].ability)
local old_center = G.hand.cards[i].config.center
local old_center_key = G.hand.cards[i].config.center_key
for k, _ in pairs(extra_enhancements) do
    if G.P_CENTERS[k] then
        G.hand.cards[i]:set_ability(G.P_CENTERS[k])
        G.hand.cards[i].ability.extra_enhancement = k
        effects[#effects+1] = eval_card(G.hand.cards[i], {cardarea = G.hand, full_hand = G.play.cards, scoring_hand = scoring_hand, scoring_name = text, poker_hands = poker_hands, extra_enhancement = true})
    end
end
G.hand.cards[i].ability = old_ability
G.hand.cards[i].config.center = old_center
G.hand.cards[i].config.center_key = old_center_key
G.hand.cards[i]:set_sprites(old_center)
'''
match_indent = true

# Calculate extra enhancements when held in hand at end of round
[[patches]]
[patches.pattern]
target = "functions/state_events.lua"
pattern = "local effects = {G.hand.cards[i]:get_end_of_round_effect()}"
position = "after"
payload = '''
local extra_enhancements = SMODS.get_enhancements(G.hand.cards[i], true)
local old_ability = copy_table(G.hand.cards[i].ability)
local old_center = G.hand.cards[i].config.center
local old_center_key = G.hand.cards[i].config.center_key
for k, _ in pairs(extra_enhancements) do
    if G.P_CENTERS[k] then
        G.hand.cards[i]:set_ability(G.P_CENTERS[k])
        G.hand.cards[i].ability.extra_enhancement = k
        effects[#effects+1] = G.hand.cards[i]:get_end_of_round_effect()
    end
end
G.hand.cards[i].ability = old_ability
G.hand.cards[i].config.center = old_center
G.hand.cards[i].config.center_key = old_center_key
G.hand.cards[i]:set_sprites(old_center)
'''
match_indent = true

# Prevent blue seal effect on extra enhancements at end of round
[[patches]]
[patches.pattern]
target = "card"
pattern = "if self.seal == 'Blue' and #G.consumeables.cards + G.GAME.consumeable_buffer < G.consumeables.config.card_limit then"
position = "before"
payload = '''
if self.extra_enhancement then return ret end
'''
match_indent = true

# Use the has enhancement function for enhancement gates
[[patches]]
[patches.pattern]
target = "functions/common_events.lua"
pattern = "if vv.config.center.key == v.enhancement_gate then"
position = "at"
payload = "if SMODS.has_enhancement(vv, v.enhancement_gate) then"
match_indent = true

# Glass Card shattering
[[patches]]
[patches.pattern]
target = "card.lua"
pattern = "if card.ability.name == 'Glass Card' then"
position = "at"
payload = "if SMODS.has_enhancement(card, 'm_glass') then"
match_indent = true

[[patches]]
[patches.pattern]
target = "functions/state_events.lua"
pattern = "if G.hand.highlighted[i].ability.name == 'Glass Card' then"
position = "at"
payload = "if SMODS.has_enhancement(G.hand.highlighted[i], 'm_glass') then"
match_indent = true

[[patches]]
[patches.pattern]
target = "functions/state_events.lua"
pattern = "if scoring_hand[i].ability.name == 'Glass Card' and not scoring_hand[i].debuff and pseudorandom('glass') < G.GAME.probabilities.normal/scoring_hand[i].ability.extra then"
position = "at"
payload = "if SMODS.has_enhancement(scoring_hand[i], 'm_glass') and not scoring_hand[i].debuff and pseudorandom('glass') < G.GAME.probabilities.normal/(scoring_hand[i].ability.name == 'Glass Card' and scoring_hand[i].ability.extra or G.P_CENTERS.m_glass.config.extra) then"
match_indent = true

[[patches]]
[patches.pattern]
target = "functions/state_events.lua"
pattern = "if scoring_hand[i].ability.name == 'Glass Card' then"
position = "at"
payload = "if SMODS.has_enhancement(scoring_hand[i], 'm_glass') then"
match_indent = true

[[patches]]
[patches.pattern]
target = "functions/state_events.lua"
pattern = "if cards_destroyed[i].ability.name == 'Glass Card' then"
position = "at"
payload = "if SMODS.has_enhancement(cards_destroyed[i], 'm_glass') then"
match_indent = true

# Prevent blue seals from applying on quantum enhancement calc
[[patches]]
[patches.pattern]
target = "card.lua"
pattern = "if self.seal == 'Blue' and #G.consumeables.cards + G.GAME.consumeable_buffer < G.consumeables.config.card_limit then"
position = "at"
payload = "if self.seal == 'Blue' and #G.consumeables.cards + G.GAME.consumeable_buffer < G.consumeables.config.card_limit and not self.ability.extra_enhancement then"
match_indent = true
