Skip to content

Commit

Permalink
Imply activated abilities during continuous effects
Browse files Browse the repository at this point in the history
  • Loading branch information
radar committed Nov 8, 2024
1 parent d1c7be2 commit 6bcd090
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 23 deletions.
3 changes: 2 additions & 1 deletion lib/magic/cards/basic_land.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ def self.card_name(name)
end

def self.color(color)
mana_ability = Class.new(Magic::TapManaAbility) do
mana_ability = Class.new(Magic::ManaAbility) do
costs "{T}"
choices color
end

Expand Down
2 changes: 1 addition & 1 deletion lib/magic/cards/crown_of_skemfar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def can_activate_ability?(ability)
end

def activated_abilities
[ReturnFromGraveyard.new(source: self)]
[ReturnFromGraveyard]
end
end
end
Expand Down
24 changes: 8 additions & 16 deletions lib/magic/permanent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class Permanent
:controller,
:card,
:types,
:power,
:toughness,
:keywords,
:delayed_responses,
:attachments,
:protections,
Expand Down Expand Up @@ -68,6 +71,7 @@ def initialize(game:, owner:, card:, token: false, cast: true, kicked: false, co
@tapped = false
@types = card.types
@keyword_grants = card.keyword_grants
@activated_abilities = card.activated_abilities
@counters = Counters::Collection.new([])
@damage = 0
@protections = Protections.new(card.protections.dup)
Expand All @@ -88,30 +92,22 @@ def types=(types)
@types = types
end

def power
@power
end

def power=(power)
@power = power
end

def toughness
@toughness
end

def toughness=(toughness)
@toughness = toughness
end

def keywords
@keywords
end

def keywords=(keywords)
@keywords = keywords
end

def activated_abilities=(abilities)
@activated_abilities = abilities
end

def keyword_grant_modifiers
modifiers.select { |modifier| modifier.is_a?(Permanents::Modifications::KeywordGrant) }
end
Expand All @@ -120,10 +116,6 @@ def inspect
"#<Magic::Permanent name:#{card.name} controller:#{controller.name}>"
end

def activated_abilities
@activated_abilities ||= card.activated_abilities.map { |ability| ability.new(source: self) }
end

def state_triggered_abilities
@state_triggered_abilities ||= card.state_triggered_abilities.map { |ability| ability.new(source: self) }
end
Expand Down
9 changes: 9 additions & 0 deletions lib/magic/permanents/continuous_effects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def apply!
permanent.types = types
game.logger.debug "Types: #{types}"
# Layer 6
permanent.activated_abilities = calculate_activated_abililities
permanent.keywords = calculate_keywords
game.logger.debug "Keywords: #{permanent.keywords}"
# Layer 7
Expand Down Expand Up @@ -100,6 +101,14 @@ def keyword_grant_static_abilities
def static_abilities_for(permanent)
static_abilities.applies_to(permanent)
end

def calculate_activated_abililities
[
*card.activated_abilities,
].map do |ability|
ability.new(source: permanent)
end
end
end
end
end
4 changes: 4 additions & 0 deletions lib/magic/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def static_abilities
[]
end

def activated_abilities
[]
end

def replacement_effects
{}
end
Expand Down
5 changes: 5 additions & 0 deletions lib/magic/zones/graveyard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ def add(card, _index = 0)

super
end

def by_name(name)
super.map { |card| GraveyardCard.new(card: card) }
end

def graveyard?
true
end
Expand Down
9 changes: 4 additions & 5 deletions spec/cards/crown_of_skemfar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,21 @@
end

context "return from graveyard to hand" do
let(:crown_of_skemfar) { Card("Crown Of Skemfar") }

before do
p1.graveyard.add(crown_of_skemfar)
p1.graveyard.add(Card("Crown Of Skemfar"))
end

it "returns to hand" do
ability = crown_of_skemfar.activated_abilities.first
graveyard_card = p1.graveyard.by_name("Crown of Skemfar").first
ability = graveyard_card.activated_abilities.first
p1.add_mana(green: 3)
p1.activate_ability(ability: ability) do
_1.pay_mana(generic: { green: 2 }, green: 1)
end

game.stack.resolve!

expect(p1.hand).to include(crown_of_skemfar)
expect(p1.hand.by_name("Crown of Skemfar").count).to eq(1)
end
end
end

0 comments on commit 6bcd090

Please sign in to comment.