-
Notifications
You must be signed in to change notification settings - Fork 2
/
disease_tracker.lua
145 lines (130 loc) · 5.95 KB
/
disease_tracker.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
--
-- Please see the LICENSE.md file included with this distribution for attribution and copyright information.
--
function onInit()
if TimeManager_Disabled and LongTermEffects then
DB.addHandler('calendar.dateinminutes', 'onUpdate', parseDiseases)
end
LibraryData.setRecordTypeInfo('disease', {
bExport = true,
aDataMap = { 'disease', 'reference.diseases' },
sRecordDisplayClass = 'referencedisease',
aGMListButtons = { 'button_feat_type' };
aPlayerListButtons = { 'button_feat_type' };
aCustomFilters = {
['Type'] = { sField = 'type' },
}
})
end
--- This function rounds to the specified number of decimals
function round(number, decimals)
local n = 10^(decimals or 0)
number = number * n
if number >= 0 then number = math.floor(number + 0.5) else number = math.ceil(number - 0.5) end
return number / n
end
function calculateFrequency(nodeDisease, sDiseaseName)
local nFreqUnit = tonumber(DB.getValue(nodeDisease, 'freq_unit'))
local nFreqVal = DB.getValue(nodeDisease, 'freq_interval')
local nFreq
-- Debug.console(sDiseaseName, 'nFreqUnit: ' .. nFreqUnit, 'nFreqVal: ' .. nFreqVal)
if nFreqUnit and nFreqVal then
nFreq = nFreqUnit * nFreqVal
end
-- Debug.console(sDiseaseName, 'Frequency: ' .. nFreq or 'nil')
return nFreq
end
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
nDurationWithOnset = nDurUnit * nDurVal + nOnset
end
-- Debug.console(sDiseaseName, 'nDurationWithOnset: ' .. nDurationWithOnset, 'nDuration: ' .. nDuration)
return nDurationWithOnset, nDuration, nDurUnit
end
function calculateOnset(nodeDisease, sDiseaseName)
local nOnsUnit = tonumber(DB.getValue(nodeDisease, 'onset_unit'))
local nOnsVal = DB.getValue(nodeDisease, 'onset_interval')
local nOnset
-- Debug.console(sDiseaseName, 'nOnsUnit: ' .. nOnsUnit, 'nOnsVal: ' .. nOnsVal)
if nOnsUnit and nOnsVal then
nOnset = nOnsUnit * nOnsVal
end
-- Debug.console(sDiseaseName, 'nOnset: ' .. nOnset)
return nOnset
end
-- This function iterates through each disease and poison of the character
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 nTimeSinceContraction = nDateWithRounds - nDateOfContr
local nOnset = calculateOnset(nodeDisease, sDiseaseName) or 0
nDiseaseElapsed = round(nTimeSinceContraction - nOnset, 1)
local sDiseaseName = DB.getValue(nodeDisease, 'name', 'their disease')
Debug.console(sDiseaseName, 'nDiseaseElapsed: ' .. nDiseaseElapsed)
-- if the disease has a starting time, the current time is known, and any onset has elapsed
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
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, 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
local nRollCount = 0
if not bIsAutoRoll and nTargetRollCount > 0 then
ChatManager.Message(
string.format(
Interface.getString('disease_agencynotifer'),
DB.getValue(nodeActor, 'name', 'A character'),
nTargetRollCount,
'a ' .. DB.getValue(nodeDisease, 'type', 'malady')),
false, rActor)
else
-- rolls saving throws until the desired total is achieved
repeat
ActionDiseaseSave.performRoll(nil, rActor, nodeDisease)
nRollCount = nRollCount + 1
until nRollCount >= nTargetRollCount
end
-- 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
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)
if not string.find(DB.getValue(nodeDisease, 'name', ''), '%[EXPIRED%]') then
DB.setValue(nodeDisease, 'name', 'string', '[EXPIRED] ' .. sDiseaseName)
end
ChatManager.Message(
string.format(
Interface.getString('disease_expiration'),
DB.getValue(nodeActor, 'name', 'A character'),
sDiseaseName),
false, rActor)
end
-- saves the new total for use next time
DB.setValue(nodeDisease, 'savecount', 'number', nPrevRollCount + nRollCount)
end
end
end
end
end
end
end