LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

Wipers

Description
Specialization for windscreen wipers managing animations and turn on/off during rain
Functions

consoleSetWiperState

Description
Definition
consoleSetWiperState()
Code
198function Wipers:consoleSetWiperState(state)
199 local usage = "Usage: gsWiperStateSet <state> (-1 = use state from weather; 0..n = force specific wiper state)"
200 if state == nil then
201 return "Error: No arguments given! " .. usage
202 end
203
204 state = tonumber(state)
205 if state == nil then
206 return "Error: Argument is not a number! " .. usage
207 end
208 Wipers.forcedState = MathUtil.clamp(state, -1, 999)
209
210 return(Wipers.forcedState == -1 and " Reset global wiper state, now using weather state") or string.format("Set global wiper states to %d.", Wipers.forcedState)
211end

getIsActiveForWipers

Description
Definition
getIsActiveForWipers()
Code
192function Wipers:getIsActiveForWipers()
193 return true
194end

initSpecialization

Description
Definition
initSpecialization()
Code
22function Wipers.initSpecialization()
23 local schema = Vehicle.xmlSchema
24 schema:setXMLSpecializationType("Wipers")
25
26 schema:register(XMLValueType.STRING, "vehicle.wipers.wiper(?)#animName", "Animation name")
27 schema:register(XMLValueType.FLOAT, "vehicle.wipers.wiper(?).state(?)#animSpeed", "Animation speed")
28 schema:register(XMLValueType.FLOAT, "vehicle.wipers.wiper(?).state(?)#animPause", "Animation pause time (sec.)")
29
30 schema:setXMLSpecializationType()
31end

loadWiperFromXML

Description
Definition
loadWiperFromXML()
Code
138function Wipers:loadWiperFromXML(xmlFile, key, wiper)
139 local animName = xmlFile:getValue(key .. "#animName")
140 if animName ~= nil then
141 if self:getAnimationExists(animName) then
142 wiper.animName = animName
143 wiper.animDuration = self:getAnimationDuration(animName)
144 wiper.states = {}
145 local j = 0
146 while true do
147 local stateKey = string.format("%s.state(%d)", key, j)
148 if not xmlFile:hasProperty(stateKey) then
149 break
150 end
151
152 local state = {}
153 state.animSpeed = xmlFile:getValue(stateKey .. "#animSpeed")
154 state.animPause = xmlFile:getValue(stateKey .. "#animPause")
155 if state.animSpeed ~= nil and state.animPause ~= nil then
156 state.animPause = state.animPause * 1000
157 table.insert(wiper.states, state)
158 end
159
160 j = j + 1
161 end
162 else
163 Logging.xmlWarning(self.xmlFile, "Animation '%s' not defined for wiper '%s'!", animName, key)
164 return false
165 end
166 else
167 Logging.xmlWarning(self.xmlFile, "Missing animation for wiper '%s'!", key)
168 return false
169 end
170
171 local numStates = #wiper.states
172 if numStates > 0 then
173 local stepSize = 1.0 / numStates
174 local curMax = stepSize
175
176 for _,state in ipairs(wiper.states) do
177 state.maxRainValue = curMax
178 curMax = curMax + stepSize
179 end
180
181 wiper.nextStartTime = nil
182 else
183 Logging.xmlWarning(self.xmlFile, "No states defined for wiper '%s'!", key)
184 return false
185 end
186
187 return true
188end

onFinishAnimation

Description
Definition
onFinishAnimation()
Code
126function Wipers:onFinishAnimation(name)
127 local spec = self.spec_wipers
128 for _, wiper in pairs(spec.wipers) do
129 if wiper.animName == name and self:getAnimationTime(wiper.animName) == 1 then
130 local lastValidState = wiper.states[wiper.lastValidState]
131 self:playAnimation(wiper.animName, -lastValidState.animSpeed, 1, true)
132 end
133 end
134end

onLoad

Description
Definition
onLoad()
Code
50function Wipers:onLoad(savegame)
51 local spec = self.spec_wipers
52
53 spec.wipers = {}
54
55 local i = 0
56 while true do
57 local key = string.format("vehicle.wipers.wiper(%d)", i)
58 if not self.xmlFile:hasProperty(key) then
59 break
60 end
61
62 local wiper = {}
63 if self:loadWiperFromXML(self.xmlFile, key, wiper) then
64 wiper.lastValidState = 1
65 table.insert(spec.wipers, wiper)
66 end
67
68 i = i + 1
69 end
70
71 spec.hasWipers = #spec.wipers > 0
72 spec.lastRainScale = 0.0
73
74 if not spec.hasWipers then
75 SpecializationUtil.removeEventListener(self, "onUpdateTick", Wipers)
76 end
77end

onUpdateTick

Description
Definition
onUpdateTick()
Code
81function Wipers:onUpdateTick(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
82 local spec = self.spec_wipers
83
84 if self:getIsControlled() then
85 spec.lastRainScale = g_currentMission.environment.weather:getRainFallScale()
86
87 for _, wiper in pairs(spec.wipers) do
88 local stateIdToUse = 0
89 if self:getIsActiveForWipers() then
90 if spec.lastRainScale > 0.01 then -- small etha for the long tail
91 for stateIndex,state in ipairs(wiper.states) do
92 if spec.lastRainScale <= state.maxRainValue then
93 stateIdToUse = stateIndex
94 break
95 end
96 end
97 end
98 end
99
100 if Wipers.forcedState ~= -1 then
101 stateIdToUse = MathUtil.clamp(Wipers.forcedState, 0, #wiper.states)
102 end
103
104 if stateIdToUse > 0 then
105 local currentState = wiper.states[stateIdToUse]
106
107 if wiper.nextStartTime == nil or wiper.nextStartTime < g_currentMission.time then
108 if not self:getIsAnimationPlaying(wiper.animName) then
109 self:playAnimation(wiper.animName, currentState.animSpeed, 0, true)
110 wiper.nextStartTime = nil
111 end
112 end
113
114 if wiper.nextStartTime == nil then
115 wiper.nextStartTime = g_currentMission.time + (wiper.animDuration / currentState.animSpeed) * 2 + currentState.animPause
116 end
117
118 wiper.lastValidState = stateIdToUse
119 end
120 end
121 end
122end

prerequisitesPresent

Description
Definition
prerequisitesPresent()
Code
16function Wipers.prerequisitesPresent(specializations)
17 return SpecializationUtil.hasSpecialization(Enterable, specializations) and SpecializationUtil.hasSpecialization(AnimatedVehicle, specializations)
18end

registerEventListeners

Description
Definition
registerEventListeners()
Code
42function Wipers.registerEventListeners(vehicleType)
43 SpecializationUtil.registerEventListener(vehicleType, "onLoad", Wipers)
44 SpecializationUtil.registerEventListener(vehicleType, "onUpdateTick", Wipers)
45 SpecializationUtil.registerEventListener(vehicleType, "onFinishAnimation", Wipers)
46end

registerFunctions

Description
Definition
registerFunctions()
Code
35function Wipers.registerFunctions(vehicleType)
36 SpecializationUtil.registerFunction(vehicleType, "loadWiperFromXML", Wipers.loadWiperFromXML)
37 SpecializationUtil.registerFunction(vehicleType, "getIsActiveForWipers", Wipers.getIsActiveForWipers)
38end