forked from Dukefarming/FS25-lua-scripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVehicleActionControllerAction.lua
More file actions
274 lines (193 loc) · 6.93 KB
/
VehicleActionControllerAction.lua
File metadata and controls
274 lines (193 loc) · 6.93 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
---Copyright (C) GIANTS Software GmbH, Confidential, All Rights Reserved.
local VehicleActionControllerAction_mt = Class(VehicleActionControllerAction)
---
function VehicleActionControllerAction.new(parent, name, inputAction, priority, customMt)
local self = setmetatable({}, customMt or VehicleActionControllerAction_mt)
self.parent = parent
self.name = name
self.inputAction = inputAction
self.priority = priority
self.lastDirection = -1
self.lastValidDirection = 0
self.isSaved = false
self.resetOnDeactivation = true
self.identifier = ""
self.aiEventListener = {}
return self
end
---
function VehicleActionControllerAction:remove()
self.parent:removeAction(self)
end
---
function VehicleActionControllerAction:updateParent(parent)
if parent ~= self.parent then
self.parent:removeAction(self)
parent:addAction(self)
end
self.parent = parent
end
---
function VehicleActionControllerAction:setCallback(callbackTarget, inputCallback, inputCallbackRev)
self.callbackTarget = callbackTarget
self.inputCallback = inputCallback
self.inputCallbackRev = inputCallbackRev
self.identifier = callbackTarget.configFileName or ""
end
---
function VehicleActionControllerAction:setFinishedFunctions(finishedFunctionTarget, finishedFunc, finishedResult, finishedResultRev, finishedFuncRev)
self.finishedFunctionTarget = finishedFunctionTarget
self.finishedFunc = finishedFunc
self.finishedFuncRev = finishedFuncRev
self.finishedResult = finishedResult
self.finishedResultRev = finishedResultRev
end
---
function VehicleActionControllerAction:setDeactivateFunction(deactivateFunctionTarget, deactivateFunc, inverseDeactivateFunc)
self.deactivateFunctionTarget = deactivateFunctionTarget
self.deactivateFunc = deactivateFunc
self.inverseDeactivateFunc = Utils.getNoNil(inverseDeactivateFunc, false)
end
---
function VehicleActionControllerAction:setIsAvailableFunction(availableFunc)
self.availableFunc = availableFunc
end
---
function VehicleActionControllerAction:setIsAccessibleFunction(accessibleFunc)
self.accessibleFunc = accessibleFunc
end
---
function VehicleActionControllerAction:setActionIcons(iconPos, iconNeg, changeColor)
self.iconPos = iconPos
self.iconNeg = iconNeg
self.iconChangeColor = changeColor
end
---
function VehicleActionControllerAction:setResetOnDeactivation(resetOnDeactivation)
self.resetOnDeactivation = resetOnDeactivation
end
---
function VehicleActionControllerAction:setIsSaved(isSaved)
self.isSaved = isSaved
end
---
function VehicleActionControllerAction:getIsSaved()
return self.isSaved
end
---
function VehicleActionControllerAction:isAvailable()
if self.availableFunc ~= nil then
return self.availableFunc()
end
return true
end
---
function VehicleActionControllerAction:getControlledActionIcons()
return self.iconPos, self.iconNeg, self.iconChangeColor
end
---
function VehicleActionControllerAction:getLastDirection()
return self.lastDirection
end
---
function VehicleActionControllerAction:getDoResetOnDeactivation()
return self.resetOnDeactivation
end
---
function VehicleActionControllerAction:addAIEventListener(sourceVehicle, eventName, direction, forceUntilFinished)
self.sourceVehicle = sourceVehicle
local listener = {}
listener.eventName = eventName
listener.direction = direction
listener.forceUntilFinished = forceUntilFinished
table.insert(self.aiEventListener, listener)
end
---
function VehicleActionControllerAction:registerActionEvents(target, vehicle, actionEvents, isActiveForInput, isActiveForInputIgnoreSelection)
--local _, actionEventId = vehicle:addActionEvent(actionEvents, self.inputAction, target, VehicleActionControllerAction.onActionEvent, false, true, false, true)
--g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_HIGH)
end
---
function VehicleActionControllerAction:actionEvent(actionName, inputValue, actionIndex, isAnalog)
self:doAction()
end
---
function VehicleActionControllerAction:doAction(direction, isAIEvent)
if direction == nil then
direction = -self.lastDirection
end
self.lastDirection = direction
local success = self.inputCallback(self.callbackTarget, direction, isAIEvent)
if success then
self.lastValidDirection = self.lastDirection
end
return success
end
---
function VehicleActionControllerAction:getIsFinished(direction)
if self.finishedFunc ~= nil then
if direction > 0 then
return self.finishedFunc(self.finishedFunctionTarget) == self.finishedResult
else
return self.finishedFunc(self.finishedFunctionTarget) == self.finishedResultRev
end
end
return true
end
---
function VehicleActionControllerAction:getSourceVehicle()
return self.sourceVehicle
end
---
function VehicleActionControllerAction:onAIEvent(eventName)
for _, listener in ipairs(self.aiEventListener) do
if listener.eventName == eventName then
if not self:doAction(listener.direction, true) and listener.forceUntilFinished then
self.forceDirectionUntilFinished = listener.direction
else
if self.forceDirectionUntilFinished ~= nil then
if listener.direction ~= self.forceDirectionUntilFinished then
self.forceDirectionUntilFinished = nil
end
end
self.parent:stopActionSequence()
end
end
end
end
---
function VehicleActionControllerAction:update(dt)
if self.deactivateFunc ~= nil then
if self.lastDirection == 1 then
if self.deactivateFunc(self.deactivateFunctionTarget) == not self.inverseDeactivateFunc then
if self.parent.currentSequenceIndex == nil and self.forceDirectionUntilFinished == nil then
self.parent:startActionSequence()
end
end
end
end
end
---
function VehicleActionControllerAction:updateForAI(dt)
if self.forceDirectionUntilFinished ~= nil then
if self:doAction(self.forceDirectionUntilFinished) then
self.forceDirectionUntilFinished = nil
self.parent:stopActionSequence()
end
end
end
---
function VehicleActionControllerAction:getDebugText()
local finishedResult = "?"
if self.finishedFunc ~= nil then
finishedResult = self.finishedFunc(self.finishedFunctionTarget)
if type(finishedResult) == "number" then
finishedResult = string.format("%.1f", finishedResult)
end
end
local vehicleName = "Unknown Vehicle"
if self.callbackTarget ~= nil then
vehicleName = self.callbackTarget:getName()
end
return string.format("Prio '%d' - Vehicle '%s' - Action '%s' (%s/%s)", self.priority, vehicleName, self.name, finishedResult, self.lastDirection == 1 and self.finishedResult or self.finishedResultRev)
end