Skip to content

Commit

Permalink
more work on automating rolls correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
bmos committed Jun 27, 2021
1 parent 540fb26 commit eed2141
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 48 deletions.
Binary file added FG-PFRPG-Malady-Tracker.ext
Binary file not shown.
7 changes: 2 additions & 5 deletions campaign/record_disease.xml
Expand Up @@ -244,11 +244,8 @@
<gmvisibleonly />
<script>
function onButtonPress()
if TimeManager then
local nRound = DB.getValue("combattracker.round", 0)
local nInit = DB.getValue(CombatManager.getActiveCT(), "initresult", 0)
if nInit > 0 then nInitMin = 0.1 - (0.001 * (nInit)) end
local nDateinMinutes = TimeManager.getCurrentDateinMinutes() + nInitMin
if TimeManager_Disabled then
local nDateinMinutes = TimeManager.getCurrentDateinMinutes()
DB.setValue(window.getDatabaseNode(), 'starttime', 'number', nDateinMinutes)
DB.setValue(window.getDatabaseNode(), 'starttimestring', 'string', tostring(nDateinMinutes))
DB.setValue(window.getDatabaseNode(), 'savecount', 'number', 0)
Expand Down
10 changes: 3 additions & 7 deletions campaign/scripts/adddisease.lua
Expand Up @@ -3,7 +3,7 @@
--

--- Allow dragging and dropping madnesses between players
local function addDisease(nodeChar, sClass, sRecord, nodeTargetList)
function addDisease(nodeChar, sClass, sRecord, nodeTargetList)
if not nodeChar or not sClass or not sRecord or not nodeTargetList then
return false;
end
Expand All @@ -30,12 +30,8 @@ local function addDisease(nodeChar, sClass, sRecord, nodeTargetList)
ActionDiseaseTimeRoll.performRoll(draginfo, nodeEntry, rActor, DB.getValue(nodeSource, 'duration_dice'), DB.getValue(nodeSource, 'duration_interval'), 'Duration')
end
end
if TimeManager and DB.getValue(nodeEntry, 'freq_interval') and tonumber(DB.getValue(nodeEntry, 'freq_unit', 0.1)) then
local nRound = DB.getValue("combattracker.round", 0)
local nInit = DB.getValue(CombatManager.getActiveCT(), "initresult", 0)
local nInitMin = 0
if nInit > 0 then nInitMin = 0.1 - (0.001 * (nInit)) end
local nDateinMinutes = TimeManager.getCurrentDateinMinutes() + nInitMin
if TimeManager_Disabled and DB.getValue(nodeEntry, 'freq_interval') and tonumber(DB.getValue(nodeEntry, 'freq_unit', 0.1)) then
local nDateinMinutes = TimeManager.getCurrentDateinMinutes()
--Debug.chat('addDisease', nDateinMinutes)
DB.setValue(nodeEntry, 'starttime', 'number', nDateinMinutes)
DB.setValue(nodeEntry, 'starttimestring', 'string', tostring(nDateinMinutes))
Expand Down
12 changes: 6 additions & 6 deletions campaign/scripts/diseasedesc_stats.lua
Expand Up @@ -7,7 +7,7 @@ function onInit()
end

-- This function takes the Save DC related information and combines them into a single string that can be displayed once the window is locked.
local function generateOnsetString()
function generateOnsetString()
local sOnset = ''

if onset_unit.getValue() ~= '' then sOnset = onset_interval.getValue() end
Expand All @@ -17,7 +17,7 @@ local function generateOnsetString()
end

-- This function takes the Save DC related information and combines them into a single string that can be displayed once the window is locked.
local function generateDurationString()
function generateDurationString()
local sDur = ''

if duration_unit.getValue() ~= '' then sDur = duration_interval.getValue() end
Expand All @@ -34,7 +34,7 @@ function round(nNum, nDecimalPlaces)
end

-- This function takes the Save DC related information and combines them into a single string that can be displayed once the window is locked.
local function generateFrequencyString()
function generateFrequencyString()
local sFreq = ''

if freq_unit.getValue() ~= '' then sFreq = round(freq_interval.getValue(), 1) end
Expand Down Expand Up @@ -67,7 +67,7 @@ function generateSaveString()
end

-- This function sets the visibility and editability of various fields on the malady sheet when it is unlocked.
local function ifLocked(sType)
function ifLocked(sType)
local sSubtype = ''
if subtype.getValue() and subtype.getValue() ~= '' then
sSubtype = ' (' .. subtype.getValue() .. ')'
Expand Down Expand Up @@ -166,7 +166,7 @@ local function ifLocked(sType)
end

-- This function sets the visibility and editability of various fields on the malady sheet when it is locked.
local function ifUnlocked(sType)
function ifUnlocked(sType)
save_string.setVisible(false)
savetype.setVisible(true)
savedc_label.setVisible(true)
Expand Down Expand Up @@ -219,7 +219,7 @@ local function ifUnlocked(sType)
end

button_settime.setVisible(false)
if TimeManager and getDatabaseNode().getParent().getName() ~= 'disease' and getDatabaseNode().getChild('...').getName() ~= 'reference' then
if TimeManager_Disabled and getDatabaseNode().getParent().getName() ~= 'disease' and getDatabaseNode().getChild('...').getName() ~= 'reference' then
button_settime.setVisible(true)
end

Expand Down
45 changes: 17 additions & 28 deletions scripts/disease_tracker.lua
Expand Up @@ -3,10 +3,7 @@
--

function onInit()
if LongTermEffects then
CombatManager.setCustomTurnEnd(parseDiseases)

DB.addHandler('calendar.dateinminutesstring', 'onUpdate', parseDiseases)
if TimeManager_Disabled and LongTermEffects then
DB.addHandler('calendar.dateinminutes', 'onUpdate', parseDiseases)
end

Expand All @@ -30,7 +27,7 @@ function round(number, decimals)
return number / n
end

local function calculateFrequency(nodeDisease, sDiseaseName)
function calculateFrequency(nodeDisease, sDiseaseName)
local nFreqUnit = tonumber(DB.getValue(nodeDisease, 'freq_unit'))
local nFreqVal = DB.getValue(nodeDisease, 'freq_interval')
local nFreq
Expand All @@ -43,21 +40,21 @@ local function calculateFrequency(nodeDisease, sDiseaseName)
return nFreq
end

local function calculateDuration(nodeDisease, sDiseaseName, nOnset)
function calculateDuration(nodeDisease, sDiseaseName, nOnset)
local nDurUnit = tonumber(DB.getValue(nodeDisease, 'duration_unit'))
local nDurVal = DB.getValue(nodeDisease, 'duration_interval')
local nDuration
-- Debug.console(sDiseaseName, 'nDurUnit: ' .. nDurUnit, 'nDurVal: ' .. nDurVal)
if nDurUnit and nDurVal then
nDuration = nDurUnit * nDurVal + nOnset
nDuration = nDurUnit * nDurVal
nDurationWithOnset = nDurUnit * nDurVal + nOnset
end
-- Debug.console(sDiseaseName, 'nDurationWithOnset: ' .. nDurationWithOnset, 'nDuration: ' .. nDuration)

return nDurationWithOnset, nDuration, nDurUnit
end

local function calculateOnset(nodeDisease, sDiseaseName)
function calculateOnset(nodeDisease, sDiseaseName)
local nOnsUnit = tonumber(DB.getValue(nodeDisease, 'onset_unit'))
local nOnsVal = DB.getValue(nodeDisease, 'onset_interval')
local nOnset
Expand All @@ -71,41 +68,34 @@ local function calculateOnset(nodeDisease, sDiseaseName)
end

-- This function iterates through each disease and poison of the character
function parseDiseases(nodeActor)
local nDateinMinutes, nInit = TimeManager.getCurrentDateinMinutes(), 0
if nodeActor then
local rActor = ActorManager.resolveActor(nodeActor)
nInit = DB.getValue(ActorManager.getCTNode(rActor), "initresult", 0)
if nInit > 0 then nInit = 0.1 - (0.001 * (nInit)) end
end
local nDateWithRounds = nDateinMinutes + nInit
function parseDiseases()
local nDateWithRounds = TimeManager.getCurrentDateinMinutes()
for _,nodeCT in pairs(DB.getChildren('combattracker.list')) do
local rActor = ActorManager.resolveActor(nodeCT)
local nodeActor = ActorManager.getCreatureNode(rActor)
for _,nodeDisease in pairs(DB.getChildren(nodeActor, 'diseases')) do
local nDateOfContr = DB.getValue(nodeDisease, 'starttimestring') or DB.getValue(nodeDisease, 'starttime')
if nDateOfContr and DB.getValue(nodeDisease, 'savetype') and not string.find(DB.getValue(nodeDisease, 'name', ''), '%[') then
local nTimeElapsed = nDateWithRounds - nDateOfContr
local nTimeSinceContraction = nDateWithRounds - nDateOfContr

local nOnset = calculateOnset(nodeDisease, sDiseaseName) or 0
nTimeElapsed = nTimeElapsed - nOnset
nDiseaseElapsed = round(nTimeSinceContraction - nOnset, 1)

local sDiseaseName = DB.getValue(nodeDisease, 'name', 'their disease')
-- Debug.console(sDiseaseName, 'nTimeElapsed: ' .. nTimeElapsed, 'nDateOfContr: ' .. nDateOfContr, 'nOnset: ' .. nOnset)
Debug.console(sDiseaseName, 'nDiseaseElapsed: ' .. nDiseaseElapsed)
-- if the disease has a starting time, the current time is known, and any onset has elapsed
if nTimeElapsed > 0 then
if nDiseaseElapsed > 0 then
local nFreq = calculateFrequency(nodeDisease, sDiseaseName)
if nFreq then
local nPrevRollCount = DB.getValue(nodeDisease, 'savecount', 0) -- how many saves have been successful
nTimeElapsed = round(nTimeElapsed / nFreq, 3)
local nTotalRolls = math.floor(nTimeElapsed)
-- Debug.console(sDiseaseName, nTimeElapsed .. ': ' .. nTimeElapsed .. ' / ' .. nFreq)
local nTotalRolls = round(nDiseaseElapsed / nFreq, 0)
-- Debug.console(sDiseaseName, 'nTotalRolls: ' .. nTotalRolls, 'nPrevRollCount: ' .. nPrevRollCount)
local nTargetRollCount = nTotalRolls - nPrevRollCount
if nTargetRollCount > 0 then
local nDurationWithOnset, nDuration, nDurUnit = calculateDuration(nodeDisease, sDiseaseName, nOnset)
if nDurationWithOnset then nTargetRollCount = math.min(nTargetRollCount, nDurationWithOnset / nFreq) end
if nDurUnit == 0.1 then nTargetRollCount = nTargetRollCount - 1 end -- fix rounds-based maladies rolling once too many times
-- Debug.console(sDiseaseName, 'nPrevRollCount: ' .. nPrevRollCount, 'nTargetRollCount: ' .. nTargetRollCount)
if nDurationWithOnset then nTargetRollCount = math.min(nTargetRollCount, nDuration / nFreq); end
-- Debug.console(sDiseaseName, 'nDurationWithOnset: ' .. nDurationWithOnset, 'nDiseaseElapsed: ' .. nDiseaseElapsed)
-- Debug.console(sDiseaseName, 'nTargetRollCount: ' .. nTargetRollCount)

-- if character auto-roll is disabled, request roll via a chat message.
local bIsAutoRoll = DB.getValue(nodeActor, 'diseaserollactive', 1) == 1
Expand All @@ -129,8 +119,7 @@ function parseDiseases(nodeActor)
-- if the disease has a duration and the duration has now expired,
-- announce in chat, delete the save-counting + time records,
-- and add [EXPIRED] to the disease name
-- Debug.console(sDiseaseName, 'nDuration: ' .. nDuration, 'nDurationWithOnset: ' .. nDurationWithOnset, 'nTimeElapsed: ' .. nTimeElapsed)
if nDuration and nDuration > 0 and nDurationWithOnset and nTimeElapsed >= nDurationWithOnset then
if (nDuration and nDuration > 0) and (nDiseaseElapsed >= nDuration) then
DB.setValue(nodeDisease, 'starttime', 'number', nil)
DB.setValue(nodeDisease, 'starttimestring', 'string', nil)
DB.setValue(nodeDisease, 'savecount', 'number', nil)
Expand Down
2 changes: 1 addition & 1 deletion scripts/manager_action_diseasesave.lua
Expand Up @@ -89,7 +89,7 @@ function notifyApplySave(rSource, rRoll)
end
end

local function getRoll(rActor, nodeDisease)
function getRoll(rActor, nodeDisease)
local rRoll = {}
rRoll.sType = 'disease'
rRoll.aDice = { 'd20' }
Expand Down
2 changes: 1 addition & 1 deletion scripts/manager_action_diseasetimeroll.lua
Expand Up @@ -7,7 +7,7 @@ function onInit()
ActionsManager.registerResultHandler("diseasetimeroll", onRoll);
end

local function getRoll(rActor, tDice, nFixedInt, sField)
function getRoll(rActor, tDice, nFixedInt, sField)
local rRoll = {};

rRoll.sType = "diseasetimeroll";
Expand Down

0 comments on commit eed2141

Please sign in to comment.