Skip to content

Commit

Permalink
Rework choices
Browse files Browse the repository at this point in the history
  • Loading branch information
radar committed Jan 24, 2024
1 parent 0052748 commit cd5c531
Show file tree
Hide file tree
Showing 43 changed files with 145 additions and 160 deletions.
8 changes: 6 additions & 2 deletions lib/magic/activated_ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ def battlefield
game.battlefield
end

def trigger(event, **args)
source.trigger(event, source: self, **args)
def trigger_effect(effect, **args)
source.trigger_effect(effect, source: self, **args)
end

def add_choice(choice, **args)
source.add_choice(choice, **args)
end
end
end
25 changes: 22 additions & 3 deletions lib/magic/card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,33 @@ def can_attack? = !defender?
def can_block? = true
def can_activate_ability?(_) = true

def trigger(effect, source: self, **args)
def add_choice(choice, **args)
case choice
when :discard
game.choices.add(Magic::Choice::Discard.new(player: controller, **args))
else
raise "Unknown choice: #{choice.inspect}"
end
end

def trigger_effect(effect, source: self, **args)
case effect
when :add_counter
game.add_effect(Effects::AddCounter.new(source: source, **args))
when :deal_damage
game.add_effect(Effects::DealDamage.new(source: source, **args))
when :destroy_target
game.add_effect(Effects::DestroyTarget.new(source: source, **args))
when :draw_card
game.add_effect(Effects::DrawCards.new(source: source, **args))
when :discard
game.choices.add(Magic::Choice::Discard.new(player: controller, **args))
when :gain_life
game.add_effect(Effects::GainLife.new(source: source, target: source.controller, **args))
when :lose_life
game.add_effect(Effects::LoseLife.new(source: source, **args))
when :modify_power_toughness
game.add_effect(Effects::ApplyPowerToughnessModification.new(source: source, **args))
when :return_to_owners_hand
game.add_effect(Effects::ReturnToOwnersHand.new(source: source, **args))
else
raise "Unknown trigger: #{effect.inspect}"
end
Expand Down
4 changes: 2 additions & 2 deletions lib/magic/cards/academy_elite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def costs
end

def resolve!
trigger(:draw_card, source: source)
trigger(:discard)
trigger_effect(:draw_card, source: source)
add_choice(:discard)
end
end

Expand Down
10 changes: 3 additions & 7 deletions lib/magic/cards/acidic_slime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ module Cards
toughness 2

enters_the_battlefield do
effect = Effects::DestroyTarget.new(
source: permanent,
)
game.add_effect(effect)
game.choices.add(AcidicSlime::Choice.new(source: self))
end

end

class AcidicSlime < Creature
def target_choices(_)
game.battlefield.cards.by_any_type("Artifact", "Enchantment", "Land")
class Choice < Choice::DestroyTarget
def choices = game.battlefield.cards.by_any_type("Artifact", "Enchantment", "Land")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/magic/cards/animal_sanctuary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def target_choices
end

def resolve!
game.add_effect(Effects::AddCounter.new(source: source, counter_type: Counters::Plus1Plus1, choices: target_choices))
trigger_effect(:add_counter, counter_type: Counters::Plus1Plus1, choices: target_choices)
end
end

Expand Down
10 changes: 5 additions & 5 deletions lib/magic/cards/barrin_tolarian_archmage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ def event_handlers
}
end

class Effect < Effects::ReturnToOwnersHand
end

class ETB < TriggeredAbility::EnterTheBattlefield
def perform
effect = Effect.new(source: self, choices: game.battlefield.by_any_type(T::Creature, T::Planeswalker))
game.add_effect(effect)
permanent.trigger_effect(
:return_to_owners_hand,
source: permanent,
choices: game.battlefield.by_any_type(T::Creature, T::Planeswalker)
)
end
end
end
Expand Down
7 changes: 1 addition & 6 deletions lib/magic/cards/basris_lieutenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ module Cards
class BasrisLieutenant < Creature
class ETB < TriggeredAbility::EnterTheBattlefield
def perform
effect = Effects::AddCounter.new(
source: permanent,
counter_type: Counters::Plus1Plus1,
choices: controller.creatures,
)
game.add_effect(effect)
permanent.trigger_effect(:add_counter, counter_type: Counters::Plus1Plus1, choices: controller.creatures)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/magic/cards/bombard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def target_choices
end

def resolve!(target:)
game.add_effect(Effects::DealDamage.new(source: self, targets: [target], damage: 4))
trigger_effect(:deal_damage, targets: [target], damage: 4)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/magic/cards/cloudkin_seer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Cards
class CloudkinSeer < Creature
class ETB < TriggeredAbility::EnterTheBattlefield
def perform
permanent.trigger(:draw_card)
permanent.trigger_effect(:draw_card)
end
end

Expand Down
3 changes: 1 addition & 2 deletions lib/magic/cards/daybreak_charger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ module Cards
class DaybreakCharger < Creature
class ETB < TriggeredAbility::EnterTheBattlefield
def perform
effect = Effects::ApplyPowerToughnessModification.new(source: self, power: 2, choices: battlefield.creatures)
game.add_effect(effect)
permanent.trigger_effect(:modify_power_toughness, power: 2, choices: battlefield.creatures)
end
end

Expand Down
9 changes: 1 addition & 8 deletions lib/magic/cards/defiant_strike.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@ def single_target?

def resolve!(target:)
if target.zone == battlefield
game.add_effect(
Effects::ApplyPowerToughnessModification.new(
source: self,
choices: game.battlefield.creatures,
power: 1,
targets: [target]
)
)
target.modify_power_toughness!(source: self, power: 1, toughness: 0)
controller.draw!
end

Expand Down
4 changes: 1 addition & 3 deletions lib/magic/cards/eliminate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ def target_choices
end

def resolve!(target:)
game.add_effect(
Effects::DestroyTarget.new(choices: target_choices, source: self, targets: [target])
)
trigger_effect(:destroy_target, targets: [target])
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/magic/cards/epicure_of_blood.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ def event_handlers
Events::LifeGain => -> (receiver, event) do
return unless event.player == receiver.controller

effect = Effects::LoseLife.new(
trigger_effect(
:lose_life,
source: receiver,
targets: game.opponents(receiver.controller),
life: 1
life: 1,
targets: game.opponents(receiver.controller)
)
game.add_effect(effect)
end
}
end
Expand Down
8 changes: 1 addition & 7 deletions lib/magic/cards/essence_warden.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ def event_handlers
Events::EnteredTheBattlefield => -> (receiver, event) do
return if event.permanent == receiver

game.add_effect(
Effects::GainLife.new(
source: receiver,
target: receiver.controller,
life: 1
)
)
trigger_effect(:gain_life, source: receiver, life: 1)
end
}
end
Expand Down
20 changes: 13 additions & 7 deletions lib/magic/cards/feat_of_resistance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ def target_choices(controller)
controller.creatures
end

class Choice < Magic::Choice::Color
attr_reader :target
def initialize(source:, target:)
super(source: source)
@target = target
end

def resolve!(color:)
target.gains_protection_from_color(color, until_eot: true)
end
end

def resolve!(target:)
target.add_counter(Counters::Plus1Plus1)
game.choices.add(
Magic::Choice::Color.new(
callback: -> (choice) {
target.gains_protection_from_color(choice, until_eot: true)
}
)
)
game.choices.add(Choice.new(source: self, target: target))
end
end
end
Expand Down
11 changes: 3 additions & 8 deletions lib/magic/cards/gale_swooper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,15 @@ module Cards
end

class GaleSwooper < Creature
class Effect < Effects::SingleTargetAndResolve
def resolve(target)
class Choice < Magic::Choice
def resolve!(target:)
target.grant_keyword(Keywords::FLYING, until_eot: true)
end
end

class ETB < TriggeredAbility::EnterTheBattlefield
def perform
game.add_effect(
Effect.new(
source: permanent,
choices: battlefield.creatures,
)
)
game.choices.add(Choice.new(source: permanent))
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/magic/cards/ghostly_pilferer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Cards
end

class GhostlyPilferer < Creature
class Choice < Magic::Choice::Effect
class Choice < Magic::Choice
def costs
@costs ||= [Costs::Mana.new(generic: 2)]
end
Expand Down
8 changes: 1 addition & 7 deletions lib/magic/cards/grasp_of_darkness.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ def single_target?
end

def resolve!(target:)
effect = Effects::ApplyPowerToughnessModification.new(
source: self,
power: -4,
toughness: -4,
targets: target
)
game.add_effect(effect)
target.modify_power_toughness!(source: self, power: -4, toughness: -4)

super
end
Expand Down
15 changes: 2 additions & 13 deletions lib/magic/cards/lathril_blade_of_the_elves.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,8 @@ class ActivatedAbility < Magic::ActivatedAbility
def costs = [Costs::Tap.new(source), Costs::MultiTap.new(-> (c) { c.type?("Elf") }, 10)]

def resolve!
effect = Effects::LoseLife.new(
source: source,
targets: game.opponents(source.controller),
life: 10
)
game.add_effect(effect)

effect = Effects::GainLife.new(
source: source,
target: source.controller,
life: 10
)
game.add_effect(effect)
source.trigger_effect(:lose_life, source: source, life: 10, targets: game.opponents(source.controller))
source.trigger_effect(:gain_life, source: source, life: 10, target: source.controller)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/magic/cards/legions_judgement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def single_target?
end

def resolve!(target:)
game.add_effect(Effects::DestroyTarget.new(source: self, targets: [target], choices: target_choices))
trigger_effect(:destroy_target, targets: [target], choices: target_choices)

super
end
Expand Down
6 changes: 1 addition & 5 deletions lib/magic/cards/lightning_bolt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ def target_choices
game.any_target
end

def single_target?
true
end

def resolve!(target:)
game.add_effect(Effects::DealDamage.new(source: self, targets: [target], damage: 3))
trigger_effect(:deal_damage, targets: [target], choices: target_choices, damage: 3)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/magic/cards/lofty_denial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Cards
end

class LoftyDenial < Instant
class Choice < Magic::Choice::Effect
class Choice < Magic::Choice
attr_reader :target
def initialize(source:, target:)
super(source:)
Expand Down
2 changes: 1 addition & 1 deletion lib/magic/cards/miscast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Cards
end

class Miscast < Instant
class Choice < Magic::Choice::Effect
class Choice < Magic::Choice
attr_reader :source, :target
def initialize(source:, target:)
super(source:)
Expand Down
2 changes: 1 addition & 1 deletion lib/magic/cards/niambi_esteemed_speaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Cards
end

class NiambiEsteemedSpeaker < Creature
class Choice < Magic::Choice::Effect
class Choice < Magic::Choice
attr_reader :source

def initialize(source:, choices:)
Expand Down
2 changes: 1 addition & 1 deletion lib/magic/cards/opt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Opt < Instant
def resolve!
game.add_effect(
Effects::Scry.new(source: self, amount: 1, then_do: -> do
trigger(:draw_card)
trigger_effect(:draw_card)
end)
)

Expand Down
4 changes: 2 additions & 2 deletions lib/magic/cards/peer_into_the_abyss.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def target_choices
end

def resolve!(target:)
trigger(:draw_card, player: target, number_to_draw: (target.library.count / 2.0).ceil)
trigger(:lose_life, targets: [target], life: (target.life / 2.0).ceil)
trigger_effect(:draw_card, player: target, number_to_draw: (target.library.count / 2.0).ceil)
trigger_effect(:lose_life, targets: [target], life: (target.life / 2.0).ceil)
end
end
end
Expand Down
Loading

0 comments on commit cd5c531

Please sign in to comment.