Skip to content

Commit

Permalink
Make logic for Highway, Bridge, etc cost reduction more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
bilts committed Jan 17, 2012
1 parent 1b7f9c6 commit ca88f66
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
41 changes: 24 additions & 17 deletions cards.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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, {
Expand Down Expand Up @@ -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, {
Expand Down Expand Up @@ -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, {
Expand Down Expand Up @@ -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, {
Expand Down Expand Up @@ -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]
Expand Down
20 changes: 8 additions & 12 deletions gameState.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = {}
Expand Down

0 comments on commit ca88f66

Please sign in to comment.