From ca88f666f4ae3742d15d51de065a2e0890376dcc Mon Sep 17 00:00:00 2001 From: Patrick Quinn Date: Mon, 16 Jan 2012 20:40:44 -0500 Subject: [PATCH] Make logic for Highway, Bridge, etc cost reduction more generic See #21 --- cards.coffee | 41 ++++++++++++++++++++++++----------------- gameState.coffee | 20 ++++++++------------ 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/cards.coffee b/cards.coffee index edb14f4..a1347ff 100644 --- a/cards.coffee +++ b/cards.coffee @@ -80,11 +80,10 @@ basicCard = { # coins and the cost in potions. getCost: (state) -> coins = this.costInCoins(state) - coins -= state.bridges - coins -= state.highways - coins -= state.princesses * 2 - if this.isAction - coins -= state.quarries * 2 + + for modifier in state.costModifiers + coins += modifier.modify(this) + if coins < 0 coins = 0 return [coins, this.costInPotions(state)] @@ -562,7 +561,14 @@ makeCard "Philosopher's Stone", treasure, { makeCard 'Quarry', treasure, { cost: 4 coins: 1 - playEffect: (state) -> state.quarries += 1 + playEffect: (state) => + state.costModifiers.push + source: this + modify: (card) -> + if card.isAction + -2 + else + 0 } makeCard 'Royal Seal', treasure, { @@ -859,12 +865,13 @@ makeCard 'Followers', prize, { # Since there is only one Princess card, and Princess's cost # reduction effect has the clause "while this is in play", -# state.princesses will never need to be greater than 1. makeCard 'Princess', prize, { buys: 1 playEffect: (state) -> - state.princesses = 1 + state.costModifiers.push + source: this + modify: (card) -> -2 } makeCard 'Trusty Steed', prize, { @@ -1454,9 +1461,10 @@ makeCard 'Bridge', action, { cost: 4 coins: 1 buys: 1 - playEffect: - (state) -> - state.bridges += 1 + playEffect: (state) -> + state.costModifiers.push + source: this + modify: (card) -> -1 } makeCard 'Cartographer', action, { @@ -1806,11 +1814,9 @@ makeCard "Highway", action, { actions: +1 playEffect: (state) -> - highways = 0 - for card in state.current.inPlay - if card.name is "Highway" - highways++ - state.highways = highways + state.costModifiers.push + source: this + modify: (card) -> -1 } makeCard "Horse Traders", action, { @@ -2090,7 +2096,8 @@ makeCard "Mining Village", c.Village, { makeCard "Mint", action, { cost: 5 buyEffect: (state) -> - state.quarries = 0 + # Remove cost modifiers that were created by treasure (e.g. Quarry) + state.costModifiers = (m for m in state.costModifiers when !m.source.isTreasure) state.potions = 0 inPlay = state.current.inPlay for i in [inPlay.length-1...-1] diff --git a/gameState.coffee b/gameState.coffee index f4950a1..8d7d130 100644 --- a/gameState.coffee +++ b/gameState.coffee @@ -458,10 +458,11 @@ class State @tradeRouteMat = [] @tradeRouteValue = 0 - @bridges = 0 - @highways = 0 - @princesses = 0 - @quarries = 0 + # A list of objects which have a "modify" method that takes a card and returns + # a modification to its cost. Objects must also have a "source" property that + # specifies which card caused the cost modification. + @costModifiers = [] + @copperValue = 1 @phase = 'start' @extraturn = false @@ -925,10 +926,8 @@ class State @current.potions = 0 @current.actionsPlayed = 0 @copperValue = 1 - @bridges = 0 - @highways = 0 - @princesses = 0 - @quarries = 0 + + @costModifiers = [] #Announce extra turn if @extraturn @@ -1247,10 +1246,7 @@ class State newState.nPlayers = @nPlayers newState.tradeRouteMat = @tradeRouteMat.slice(0) newState.tradeRouteValue = @tradeRouteValue - newState.bridges = @bridges - newState.highways = @highways - newState.princesses = @princesses - newState.quarries = @quarries + newState.costModifiers = @costModifiers.concat() newState.copperValue = @copperValue newState.phase = @phase newState.cache = {}