Skip to content

Commit

Permalink
Merge pull request #120 from pyrrhicPachyderm/energy-hook
Browse files Browse the repository at this point in the history
Add a hook for gaining and paying energy.
  • Loading branch information
iakona authored Oct 6, 2023
2 parents 0735de5 + 26dcbd1 commit b8c39e0
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 33 deletions.
23 changes: 23 additions & 0 deletions CUSTOM.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,26 @@ This section is meant for users who are familiar with both the tts mod and progr
- 4: Slow Phase
- 5: Time Passes
- `turn`: **number** - is the current turn number
- Trigger to modify the cost of played cards
- Create object and tag with "Modify Cost"
- `modifyCost(params)`
- `params`: **table** - contains data about the player and the cards played
- `color`: **string** - is the color of the player whose card cost is being calculated
- `costs`: **table** - maps card GUIDs (as keys) to costs (as values)
- return **table** - the `costs` table, modified appropriately
- e.g. Blitz would reduce the cost of all fast cards by 1, cards could be ignored in cost calculations by deleting them from the table
- Trigger to modify the energy gained with the "Gain" button
- Create object and tag with "Modify Gain"
- `modifyGain(params)`
- `params`: **table** - contains data about the energ gain
- `color`: **string** - is the color of the player who is gaining energy
- `amount`: **number** - baseline amount of energy to gain (before bargain deductions)
- return **number** - modified amount of energy to gain (before bargain deductions)
- Trigger to run when a player successfully gains/pays energy (or successfully undoes these operations)
- Create object and tag with "Gain Pay"
- `onGainPay(params)`
- `params`: **table** - contains data about gain/pay operation
- `color`: **string** - is the color of the player who gained/paid energy
- `isGain`: **bool** - true if the operation is a gain, false if it's a pay
- `isUndo`: **bool** - true if the operation is being undone (right click), false otherwise
- `amount`: **number** - amount of energy gained/paid (before bargain deductions); positive for gain or pay
3 changes: 3 additions & 0 deletions objects/1b39da/object.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"g": 0.7132,
"b": 0.7132
},
"Tags": [
"Modify Cost"
],
"LayoutGroupSortIndex": 0,
"Value": 0,
"Locked": true,
Expand Down
19 changes: 12 additions & 7 deletions objects/1b39da/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ difficulty=0
setupBlightTokens=1
blightTokens=-1

postSetup=true
postSetupComplete=false
hasBroadcast = true

function PostSetup()
Global.setVar("fastDiscount", 1)
postSetupComplete = true
end

function Broadcast(params)
return "Blitz - Remember, Invaders get an additional set of Actions at the end of Setup"
end

function modifyCost(params)
if Global.getVar("gameStarted") and Global.getVar("scenarioCard") ~= nil and Global.getVar("scenarioCard").guid == self.guid then
for guid,cost in pairs(params.costs) do
local card = getObjectFromGUID(guid)
if (card.hasTag("Fast") and not card.hasTag("Temporary Slow")) or card.hasTag("Temporary Fast") then
params.costs[guid] = cost-1
end
end
end
return params.costs
end
3 changes: 3 additions & 0 deletions objects/BnCBag/contained/788333/contained/495c9a/object.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"g": 0.7132,
"b": 0.7132
},
"Tags": [
"Modify Gain"
],
"LayoutGroupSortIndex": 0,
"Value": 0,
"Locked": false,
Expand Down
8 changes: 8 additions & 0 deletions objects/BnCBag/contained/788333/contained/495c9a/script.lua
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
blight=2

function modifyGain(params)
if Global.getVar("blightedIsland") and Global.getVar("blightedIslandCard").guid == self.guid then
return params.amount + 1
else
return params.amount
end
end
16 changes: 16 additions & 0 deletions savegame.json
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,22 @@
{
"displayed": "Requires Isolate",
"normalized": "requires_isolate"
},
{
"displayed": "Phases",
"normalized": "phases"
},
{
"displayed": "Modify Cost",
"normalized": "modify_cost"
},
{
"displayed": "Modify Gain",
"normalized": "modify_gain"
},
{
"displayed": "Gain Pay",
"normalized": "gain_pay"
}
]
},
Expand Down
1 change: 0 additions & 1 deletion script-state.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"Jagged Earth": true
},
"explorerBag": "613ea4",
"fastDiscount": 0,
"fearPool": 0,
"gamekeys": [],
"gameStarted": false,
Expand Down
59 changes: 34 additions & 25 deletions script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ seaTile = "5f4be2"
selectedColors = {}
selectedBoards = {}
blightCards = {}
fastDiscount = 0
currentPhase = 1
playtestMinorPowers = 0
playtestMajorPowers = 0
Expand Down Expand Up @@ -521,7 +520,6 @@ function onSave()
numPlayers = numPlayers,
numBoards = numBoards,
blightCards = blightCards,
fastDiscount = fastDiscount,
currentPhase = currentPhase,
playtestMinorPowers = playtestMinorPowers,
playtestMajorPowers = playtestMajorPowers,
Expand Down Expand Up @@ -839,7 +837,6 @@ function onLoad(saved_data)
numBoards = loaded_data.numBoards
blightCards = loaded_data.blightCards
showPlayerButtons = loaded_data.showPlayerButtons
fastDiscount = loaded_data.fastDiscount
currentPhase = loaded_data.currentPhase
playtestMinorPowers = loaded_data.playtestMinorPowers
playtestMajorPowers = loaded_data.playtestMajorPowers
Expand Down Expand Up @@ -5630,13 +5627,7 @@ function setupPlayerArea(params)
-- Figure out what color we're supposed to be, or if playerswapping is even allowed.
local timer = params.obj.getVar("timer") -- May be nil
local initialized = params.obj.getVar("initialized")
local color
for k, v in pairs(playerTables) do
if v.guid == params.obj.guid then
color = k
break
end
end
local color = getTableColor(params.obj)
local selected = selectedColors[color]

if not initialized and selected then
Expand Down Expand Up @@ -5717,8 +5708,6 @@ function setupPlayerArea(params)
selected.zone.editButton({index=5, label="", click_function="nullFunc", color="White", height=0, width=0, tooltip=""})
end

local energy = 0

local Elements = {}
Elements.__index = Elements
function Elements:new(init)
Expand Down Expand Up @@ -5752,17 +5741,6 @@ function setupPlayerArea(params)
return table.concat(self, "")
end

local function powerCost(card)
local cost = card.getVar("energy")
-- Skip counting locked card's energy (Aid from Lesser Spirits)
if card.getLock() or cost == nil then
return 0
elseif (card.hasTag("Fast") and not card.hasTag("Temporary Slow")) or card.hasTag("Temporary Fast") then
cost = cost - fastDiscount
end
return cost
end

local function calculateTrackElements(spiritBoard)
local elements = Elements:new()
if spiritBoard.script_state ~= "" then
Expand Down Expand Up @@ -5840,7 +5818,7 @@ function setupPlayerArea(params)
local spirit = nil
local aspects = {}
local thresholdCards = {}
energy = 0
local costs = {}
--Go through all items found in the zone
if selected.zone then
for _,entry in ipairs(selected.zone.getObjects()) do
Expand All @@ -5866,7 +5844,10 @@ function setupPlayerArea(params)
elements:add(cardElements)
nonTokenElements:add(cardElements)
end
energy = energy + powerCost(entry)
-- Skip counting locked card's energy (Aid from Lesser Spirits)
if not entry.getLock() then
costs[entry.guid] = entry.getVar("energy")
end
end
end
if not entry.hasTag("Aspect") and entry.getTable("thresholds") ~= nil then
Expand All @@ -5880,6 +5861,11 @@ function setupPlayerArea(params)
end
end
end
costs = modifyCost({color = getTableColor(params.obj), costs = costs})
local energy = 0
for _,cost in pairs(costs) do
energy = energy + cost
end
if spirit ~= nil then
checkThresholds(spirit, aspects, thresholdCards, elements)
end
Expand Down Expand Up @@ -5923,6 +5909,23 @@ function reclaimAll(target_obj, source_color)
end
end
end
function modifyCost(params)
for _,object in pairs(getObjectsWithTag("Modify Cost")) do
params.costs = object.call("modifyCost", params)
end
return params.costs
end
function modifyGain(params)
for _,object in pairs(getObjectsWithTag("Modify Gain")) do
params.amount = object.call("modifyGain", params)
end
return params.amount
end
function onGainPay(params)
for _,object in pairs(getObjectsWithTag("Gain Pay")) do
object.call("onGainPay", params)
end
end
function payDebt(target_obj, source_color, alt_click)
local target_color = nil
for color,data in pairs(selectedColors) do
Expand Down Expand Up @@ -6017,13 +6020,15 @@ function gainEnergy(target_obj, source_color, alt_click)
if not supported then
Player[target_color].broadcast("Spirit does not support automatic energy gain", Color.SoftYellow)
else
energyTotal = modifyGain({color = target_color, amount = energyTotal})
local refunded = updateEnergyCounter(target_color, true, energyTotal, false)
if not refunded then
refunded = refundEnergyTokens(target_color, energyTotal, false)
end
if refunded then
selectedColors[target_color].gained = true
selectedColors[target_color].zone.editButton({index=2, label="Gained", click_function="returnEnergy", color="Green", tooltip="Right click to return energy from presence track"})
onGainPay({color = target_color, isGain = true, isPay = false, amount = energyTotal})
else
Player[source_color].broadcast("Was unable to gain energy", Color.SoftYellow)
end
Expand Down Expand Up @@ -6081,13 +6086,15 @@ function returnEnergy(target_obj, source_color, alt_click)
if not supported then
Player[target_color].broadcast("Spirit does not support automatic energy gain", Color.SoftYellow)
else
energyTotal = modifyGain({color = target_color, amount = energyTotal})
local paid = updateEnergyCounter(target_color, false, energyTotal, false)
if not paid then
paid = payEnergyTokens(target_color, energyTotal, false)
end
if paid then
selectedColors[target_color].gained = false
selectedColors[target_color].zone.editButton({index=2, label="Gain", click_function="gainEnergy", color="Red", tooltip="Left click to gain energy from presence track"})
onGainPay({color = target_color, isGain = true, isPay = true, amount = energyTotal})
else
Player[source_color].broadcast("You don't have enough energy", Color.SoftYellow)
end
Expand Down Expand Up @@ -6125,6 +6132,7 @@ function payEnergy(target_obj, source_color, alt_click)
if paid then
selectedColors[target_color].paid = true
selectedColors[target_color].zone.editButton({index=1, label="Paid", click_function="refundEnergy", color="Green", tooltip="Right click to refund energy for your cards"})
onGainPay({color = target_color, isGain = false, isPay = false, amount = getEnergyLabel(target_color)})
else
Player[source_color].broadcast("You don't have enough energy", Color.SoftYellow)
end
Expand Down Expand Up @@ -6381,6 +6389,7 @@ function refundEnergy(target_obj, source_color, alt_click)
if refunded then
selectedColors[target_color].paid = false
selectedColors[target_color].zone.editButton({index=1, label="Pay", click_function="payEnergy", color="Red", tooltip="Left click to pay energy for your cards"})
onGainPay({color = target_color, isGain = false, isPay = true, amount = getEnergyLabel(target_color)})
else
Player[source_color].broadcast("Was unable to refund energy", Color.SoftYellow)
end
Expand Down

0 comments on commit b8c39e0

Please sign in to comment.