Skip to content

Commit 84cb46b

Browse files
committed
Added thresholdCondition attribute for FillUnitExtension specialization
1 parent 398e721 commit 84cb46b

3 files changed

Lines changed: 85 additions & 20 deletions

File tree

docs/FILLUNIT_EXTENSION.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ Utilize additional features for base game FillUnit specialization, enabling play
3636
<vehicle>
3737
<fillUnitExtension>
3838
<fillUnit fillUnitIndex="2">
39-
<!-- Play alarm sound when fill level is below 10% -->
40-
<fillLevelSound threshold="0.1" thresholdIsGreater="false" template="rollbeltAlarm" linkNode="alarmSoundNode2" />
39+
<!-- Play alarm sound when fill level is equal to 0% -->
40+
<fillLevelSound threshold="0" thresholdCondition="=" template="rollbeltAlarm" linkNode="alarmSoundNode3" />
4141
</fillUnit>
4242
<fillUnit fillUnitIndex="3">
43+
<!-- Play alarm sound when fill level is below 10% -->
44+
<fillLevelSound threshold="0.1" thresholdCondition="<" template="rollbeltAlarm" linkNode="alarmSoundNode2" />
45+
</fillUnit>
46+
<fillUnit fillUnitIndex="4">
4347
<!-- Play alarm sound and activate object changes when fill level is above 85% -->
4448
<fillLevelSound threshold="0.85" template="rollbeltAlarm" linkNode="alarmSoundNode" />
4549
<fillLevelObjectChanges threshold="0.85">
@@ -74,7 +78,7 @@ Trigger object changes based on the fill level percentage.
7478
| Name | Type | Required | Default | Description |
7579
|-----------|-------|----|-----------|------------------------------|
7680
| threshold | float | No | ```0.9```| Defines at which fillUnit fill level percentage the object changes |
77-
| thresholdIsGreater | boolean | No | ```true``` | If true the object changes are activated above threshold, if not then below threshold |
81+
| thresholdCondition | boolean | No | ```>``` | Object changes are activated based on defined condition and threshold value. Possible values: ```<```, ```=```, ```>``` |
7882
| requiresPoweredOn | boolean | No | ```true``` | Require vehicle to be powered on in order for object changes can be active |
7983
| requiresTurnedOn | boolean | No | ```false``` | Require vehicle to be turned on in order for object changes can be active [^1] |
8084

@@ -84,6 +88,7 @@ Trigger object changes based on the fill level percentage.
8488
<vehicle>
8589
<fillUnitExtension>
8690
<fillUnit fillUnitIndex="3">
91+
<!-- Object changes are active when fill level percentage is above 85% -->
8792
<fillLevelObjectChanges threshold="0.85">
8893
<objectChange node="alarmBeacon" visibilityActive="true" visibilityInactive="false" />
8994
</fillLevelObjectChanges>
@@ -105,7 +110,7 @@ Same as a normal vehicle sample entry, but with additional attributes.
105110
| Name | Type | Required | Default | Description |
106111
|-----------|-------|----|-----------|------------------------------|
107112
| threshold | float | No | ```0.9```| Defines at which fillUnit fill level percentage the sound is triggered |
108-
| thresholdIsGreater | boolean | No | ```true``` | If true the sound is triggered above threshold, if not then below threshold |
113+
| thresholdCondition | boolean | No | ```>``` | Sound starts playing based on defined condition and threshold value. Possible values: ```<```, ```=```, ```>``` |
109114
| requiresPoweredOn | boolean | No | ```true``` | Require vehicle to be powered on in order for sound can be playing |
110115
| requiresTurnedOn | boolean | No | ```false``` | Require vehicle to be turned on in order for sound can be playing [^1] |
111116

@@ -115,6 +120,7 @@ Same as a normal vehicle sample entry, but with additional attributes.
115120
<vehicle>
116121
<fillUnitExtension>
117122
<fillUnit fillUnitIndex="3">
123+
<!-- Sound starts playing when fill level percentage is above 95% -->
118124
<fillLevelSound threshold="0.95" template="rollbeltAlarm" linkNode="alarmSoundNode" />
119125
</fillUnit>
120126
</fillUnitExtension>

scripts/ProcessorUtils.lua

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
---@enum Condition
2+
Condition = {
3+
LESSER_THAN = 1,
4+
EQUAL_TO = 2,
5+
GREATER_THAN = 3
6+
}
7+
18
---@class ProcessorUtils
29
ProcessorUtils = {}
310

@@ -125,3 +132,39 @@ function ProcessorUtils.loadDischargeObjectChanges(node, xmlFile, key)
125132
ObjectChangeUtil.setObjectChanges(node.stateObjectChanges, false, node.vehicle, node.vehicle.setMovingToolDirty)
126133
end
127134
end
135+
136+
---@param xmlFile XMLFile
137+
---@param key string
138+
---@param defaultValue? Condition
139+
---@return Condition
140+
---@nodiscard
141+
function ProcessorUtils.getXMLCondition(xmlFile, key, defaultValue)
142+
defaultValue = defaultValue or Condition.GREATER_THAN
143+
144+
local value = xmlFile:getValue(key)
145+
146+
if value == '<' then
147+
return Condition.LESSER_THAN
148+
elseif value == '=' then
149+
return Condition.EQUAL_TO
150+
elseif value == '>' then
151+
return Condition.GREATER_THAN
152+
end
153+
154+
return defaultValue
155+
end
156+
157+
---@param lhs number
158+
---@param rhs number
159+
---@param condition Condition
160+
---@return boolean
161+
---@nodiscard
162+
function ProcessorUtils.getIsConditionFulfilled(lhs, rhs, condition)
163+
if condition == Condition.LESSER_THAN then
164+
return lhs < rhs
165+
elseif condition == Condition.EQUAL_TO then
166+
return lhs == rhs
167+
else
168+
return lhs > rhs
169+
end
170+
end

scripts/specializations/FillUnitExtension.lua

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---@class FillUnitEntry
22
---@field fillUnitIndex number
33
---@field soundThreshold number
4-
---@field soundThresholdIsGreater boolean
4+
---@field soundThresholdCondition Condition
55
---@field soundRequiresTurnedOn boolean
66
---@field soundRequiresPoweredOn boolean
77
---@field sample? table
88
---@field fillLevelObjectChangeThreshold number
9-
---@field fillLevelObjectChangeThresholdIsGreater number
9+
---@field fillLevelObjectChangeThresholdCondition Condition
1010
---@field fillLevelObjectChangeRequiresTurnedOn boolean
1111
---@field fillLevelObjectChangeRequiresPoweredOn boolean
1212
---@field fillLevelObjectChanges? table
@@ -34,16 +34,18 @@ function FillUnitExtension.initSpecialization()
3434
schema:register(XMLValueType.INT, basePath .. '#fillUnitIndex')
3535

3636
SoundManager.registerSampleXMLPaths(schema, basePath, "fillLevelSound")
37-
schema:register(XMLValueType.FLOAT, basePath .. '.fillLevelSound#threshold', '', 0.9)
38-
schema:register(XMLValueType.BOOL, basePath .. '.fillLevelSound#thresholdIsGreater', '', true)
39-
schema:register(XMLValueType.BOOL, basePath .. '.fillLevelSound#requiresTurnedOn', '', false)
40-
schema:register(XMLValueType.BOOL, basePath .. '.fillLevelSound#requiresPoweredOn', '', true)
37+
schema:register(XMLValueType.FLOAT, basePath .. '.fillLevelSound#threshold', 'Defines at which fillUnit fill level percentage the sound is triggered', 0.9)
38+
schema:register(XMLValueType.BOOL, basePath .. '.fillLevelSound#thresholdIsGreater', 'Deprecated', true)
39+
schema:register(XMLValueType.STRING, basePath .. '.fillLevelSound#thresholdCondition', 'Sound starts playing based on defined condition and threshold value. Possible values: "<", "=", ">"', '>')
40+
schema:register(XMLValueType.BOOL, basePath .. '.fillLevelSound#requiresTurnedOn', 'Require vehicle to be powered on in order for sound can be playing', false)
41+
schema:register(XMLValueType.BOOL, basePath .. '.fillLevelSound#requiresPoweredOn', 'Require vehicle to be turned on in order for sound can be playing (if vehicle has TurnOn specialization)', true)
4142

4243
ObjectChangeUtil.registerObjectChangeXMLPaths(schema, basePath .. ".fillLevelObjectChanges")
4344
schema:register(XMLValueType.FLOAT, basePath .. '.fillLevelObjectChanges#threshold', 'Defines at which fillUnit fill level percentage the object changes', 0.9)
44-
schema:register(XMLValueType.BOOL, basePath .. '.fillLevelObjectChanges#thresholdIsGreater', '', true)
45-
schema:register(XMLValueType.BOOL, basePath .. '.fillLevelObjectChanges#requiresTurnedOn', '', false)
46-
schema:register(XMLValueType.BOOL, basePath .. '.fillLevelObjectChanges#requiresPoweredOn', '', true)
45+
schema:register(XMLValueType.BOOL, basePath .. '.fillLevelObjectChanges#thresholdIsGreater', 'Deprecated', true)
46+
schema:register(XMLValueType.STRING, basePath .. '.fillLevelObjectChanges#thresholdCondition', 'Object changes are activated based on defined condition and threshold value. Possible values: "<", "=", ">"', '>')
47+
schema:register(XMLValueType.BOOL, basePath .. '.fillLevelObjectChanges#requiresTurnedOn', 'Require vehicle to be powered on in order for object changes can be active', false)
48+
schema:register(XMLValueType.BOOL, basePath .. '.fillLevelObjectChanges#requiresPoweredOn', 'Require vehicle to be turned on in order for object changes can be active (if vehicle has TurnOn specialization)', true)
4749

4850
schema:setXMLSpecializationType()
4951
end
@@ -77,8 +79,15 @@ function FillUnitExtension:onLoad()
7779
if self.isClient then
7880
entry.soundRequiresPoweredOn = xmlFile:getValue(key .. '.fillLevelSound#requiresPoweredOn', true)
7981
entry.soundRequiresTurnedOn = xmlFile:getValue(key .. '.fillLevelSound#requiresTurnedOn', false)
80-
entry.soundThreshold = xmlFile:getValue(key .. '.fillLevelSound#threshold', 0.9)
81-
entry.soundThresholdIsGreater = xmlFile:getValue(key .. '.fillLevelSound#thresholdIsGreater', true)
82+
entry.soundThreshold = MathUtil.round(xmlFile:getValue(key .. '.fillLevelSound#threshold', 0.9), 3)
83+
entry.soundThresholdCondition = Condition.GREATER_THAN
84+
85+
if xmlFile:hasProperty(key .. '.fillLevelSound#thresholdCondition') then
86+
entry.soundThresholdCondition = ProcessorUtils.getXMLCondition(xmlFile, key .. '.fillLevelSound#thresholdCondition', entry.soundThresholdCondition)
87+
elseif xmlFile:getValue(key .. '.fillLevelSound#thresholdIsGreater') == false then
88+
entry.soundThresholdCondition = Condition.LESSER_THAN
89+
end
90+
8291
entry.sample = g_soundManager:loadSampleFromXML(xmlFile, key, "fillLevelSound", self.baseDirectory, self.components, 0, AudioGroup.VEHICLE, self.i3dMappings, self)
8392
end
8493

@@ -88,8 +97,15 @@ function FillUnitExtension:onLoad()
8897
if #entry.fillLevelObjectChanges == 0 then
8998
entry.fillLevelObjectChanges = nil
9099
else
91-
entry.fillLevelObjectChangeThreshold = xmlFile:getValue(key .. '.fillLevelObjectChanges#threshold', 0.9)
92-
entry.fillLevelObjectChangeThresholdIsGreater = xmlFile:getValue(key .. '.fillLevelObjectChanges#thresholdIsGreater', true)
100+
entry.fillLevelObjectChangeThreshold = MathUtil.round(xmlFile:getValue(key .. '.fillLevelObjectChanges#threshold', 0.9), 3)
101+
entry.fillLevelObjectChangeThresholdCondition = Condition.GREATER_THAN
102+
103+
if xmlFile:hasProperty(key .. '.fillLevelObjectChanges#thresholdCondition') then
104+
entry.fillLevelObjectChangeThresholdCondition = ProcessorUtils.getXMLCondition(xmlFile, key .. '.fillLevelObjectChanges#thresholdCondition', entry.fillLevelObjectChangeThresholdCondition)
105+
elseif xmlFile:getValue(key .. '.fillLevelObjectChanges#thresholdIsGreater') == false then
106+
entry.fillLevelObjectChangeThresholdCondition = Condition.LESSER_THAN
107+
end
108+
93109
entry.fillLevelObjectChangeRequiresPoweredOn = xmlFile:getValue(key .. '.fillLevelObjectChanges#requiresPoweredOn', true)
94110
entry.fillLevelObjectChangeRequiresTurnedOn = xmlFile:getValue(key .. '.fillLevelObjectChanges#requiresTurnedOn', false)
95111
ObjectChangeUtil.setObjectChanges(entry.fillLevelObjectChanges, false, self, self.setMovingToolDirty)
@@ -113,7 +129,7 @@ function FillUnitExtension:onUpdateTick(dt)
113129
local spec = self[FillUnitExtension.SPEC_NAME]
114130

115131
for _, entry in ipairs(spec.fillUnitEntries) do
116-
local fillLevelPct = self:getFillUnitFillLevelPercentage(entry.fillUnitIndex) or 0
132+
local fillLevelPct = MathUtil.round(self:getFillUnitFillLevelPercentage(entry.fillUnitIndex) or 0, 3)
117133
local isPowered = self:getIsPowered()
118134
---@type boolean?
119135
local isTurnedOn
@@ -123,7 +139,7 @@ function FillUnitExtension:onUpdateTick(dt)
123139
end
124140

125141
if entry.fillLevelObjectChanges ~= nil then
126-
local isActive = (entry.fillLevelObjectChangeThresholdIsGreater and fillLevelPct > entry.fillLevelObjectChangeThreshold) or (not entry.fillLevelObjectChangeThresholdIsGreater and fillLevelPct < entry.fillLevelObjectChangeThreshold)
142+
local isActive = ProcessorUtils.getIsConditionFulfilled(fillLevelPct, entry.fillLevelObjectChangeThreshold, entry.fillLevelObjectChangeThresholdCondition)
127143

128144
if entry.fillLevelObjectChangeRequiresTurnedOn and isTurnedOn == false then
129145
isActive = false
@@ -135,7 +151,7 @@ function FillUnitExtension:onUpdateTick(dt)
135151
end
136152

137153
if self.isClient and entry.sample ~= nil then
138-
local playSample = (entry.soundThresholdIsGreater and fillLevelPct > entry.soundThreshold) or (not entry.soundThresholdIsGreater and fillLevelPct < entry.soundThreshold)
154+
local playSample = ProcessorUtils.getIsConditionFulfilled(fillLevelPct, entry.soundThreshold, entry.soundThresholdCondition)
139155
local isPlaying = g_soundManager:getIsSamplePlaying(entry.sample)
140156

141157
if entry.soundRequiresTurnedOn and isTurnedOn == false then

0 commit comments

Comments
 (0)