-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathVehicleStatus.lua
429 lines (366 loc) · 15.4 KB
/
VehicleStatus.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
-- VehicleStatus.lua for FS19
-- Author: sperrgebiet
-- Please see https://github.com/sperrgebiet/FS19_VehicleExplorer for additional information, credits, issues and everything else
VehicleStatus = {};
VehicleStatus.ModName = g_currentModName;
VehicleStatus.ModDirectory = g_currentModDirectory;
VehicleStatus.Version = "0.9.4.6";
VehicleStatus.debug = fileExists(VehicleStatus.ModDirectory ..'debug');
print(string.format('VehicleStatus v%s - DebugMode %s)', VehicleStatus.Version, tostring(VehicleStatus.debug)));
function VehicleStatus.prerequisitesPresent(specializations)
return true;
end
function VehicleStatus.registerEventListeners(vehicleType)
local functionNames = { "onLoad", "onPostLoad", "saveToXMLFile" };
for _, functionName in ipairs(functionNames) do
SpecializationUtil.registerEventListener(vehicleType, functionName, VehicleStatus);
end
end
function VehicleStatus:onLoad(savegame)
end;
function VehicleStatus:onPostLoad(savegame)
if VehicleSort.config[14][2] and savegame ~= nil then
if self.spec_motorized ~= nil then
local motorTurnedOn = Utils.getNoNil(getXMLBool(savegame.xmlFile, savegame.key .. ".vehicleStatus#isMotorStarted"), false);
VehicleSort:dp(string.format('motorTurnedOn: {%s} for {%s} | savegame.key: {%s}', tostring(motorTurnedOn), self.configFileName, savegame.key .. ".vehicleStatus#isMotorStarted"), 'VehicleStatus:onPostLoad');
if motorTurnedOn then
self:startMotor();
end
end
if self.spec_turnOnVehicle ~= nil then
local isTurnedOn = Utils.getNoNil(getXMLBool(savegame.xmlFile, savegame.key .. ".vehicleStatus#isTurnedOn"), false);
VehicleSort:dp(string.format('isTurnedOn: {%s} for {%s} | savegame.key: {%s}', tostring(isTurnedOn), self.configFileName, savegame.key .. ".vehicleStatus#isTurnedOn"), 'VehicleStatus:onPostLoad');
if isTurnedOn then
self:setIsTurnedOn(isTurnedOn);
end
end
if self.spec_lights ~= nil and self.spec_enterable ~= nil then
local lightsMask = Utils.getNoNil(getXMLInt(savegame.xmlFile, savegame.key .. ".vehicleStatus#lightsMask"), 0);
VehicleSort:dp(string.format('lightsMask: {%s} for {%s} | savegame.key: {%s}', tostring(lightsMask), self.configFileName, savegame.key .. ".vehicleStatus#lightsMask"), 'VehicleStatus:onPostLoad');
if lightsMask > 0 then
self:setLightsTypesMask(lightsMask, true);
end
local beaconsOn = Utils.getNoNil(getXMLBool(savegame.xmlFile, savegame.key .. ".vehicleStatus#beaconsOn"), false);
VehicleSort:dp(string.format('beaconsOn: {%s} for {%s} | savegame.key: {%s}', tostring(beaconsOn), self.configFileName, savegame.key .. ".vehicleStatus#beaconsOn"), 'VehicleStatus:onPostLoad');
if beaconsOn then
self:setBeaconLightsVisibility(beaconsOn, true);
end
local turnLightsState = Utils.getNoNil(getXMLInt(savegame.xmlFile, savegame.key .. ".vehicleStatus#turnLightsState"), 0);
VehicleSort:dp(string.format('turnLightsState: {%s} for {%s} | savegame.key: {%s}', tostring(turnLightsState), self.configFileName, savegame.key .. ".vehicleStatus#turnLightsState"), 'VehicleStatus:onPostLoad');
if turnLightsState > 0 then
self:setTurnLightState(turnLightsState, true);
end
local brakeLightsOn = Utils.getNoNil(getXMLBool(savegame.xmlFile, savegame.key .. ".vehicleStatus#brakeLightsOn"), false);
VehicleSort:dp(string.format('brakeLightsOn: {%s} for {%s} | savegame.key: {%s}', tostring(brakeLightsOn), self.configFileName, savegame.key .. ".vehicleStatus#brakeLightsOn"), 'VehicleStatus:onPostLoad');
if brakeLightsOn then
self:setBrakeLightsVisibility(brakeLightsOn, true);
end
end
-- Handling trains differently
if self.typeName == 'locomotive' then
if VehicleSort.loadTrainStatus[self.id] == nil then
VehicleSort.loadTrainStatus[self.id] = {};
VehicleSort.loadTrainStatus.entries = VehicleSort.loadTrainStatus.entries + 1;
end
VehicleSort.loadTrainStatus[self.id]['motorTurnedOn'] = Utils.getNoNil(getXMLBool(savegame.xmlFile, savegame.key .. ".vehicleStatus#isMotorStarted"), false);
VehicleSort:dp(string.format('Added train motor to loadTrainStatus. id {%d}', self.id));
end
end
end
function VehicleStatus:saveToXMLFile(xmlFile, key)
if VehicleSort.config[14][2] then
if VehicleStatus:getIsMotorStarted(self) then
setXMLBool(xmlFile, key .. '#isMotorStarted', VehicleStatus:getIsMotorStarted(self));
end
if VehicleStatus:getIsTurnedOn(self) then
setXMLBool(xmlFile, key .. '#isTurnedOn', VehicleStatus:getIsTurnedOn(self));
end
if VehicleStatus:getIsLightTurnedOn(self) then
setXMLInt(xmlFile, key .. '#lightsMask', self:getLightsTypesMask());
end
if VehicleStatus:getBeaconLightsVisibility(self) then
setXMLBool(xmlFile, key .. '#beaconsOn', VehicleStatus:getBeaconLightsVisibility(self));
end
if VehicleStatus:getTurnLightState(self) > 0 then
setXMLInt(xmlFile, key .. '#turnLightsState', VehicleStatus:getTurnLightState(self));
end
if VehicleStatus:getIsBrakeLightsOn(self) then
setXMLBool(xmlFile, key .. '#brakeLightsOn', VehicleStatus:getIsBrakeLightsOn(self));
end
end
end
function VehicleStatus:getIsMotorStarted(vehObj)
if vehObj.spec_motorized ~= nil and vehObj.getIsMotorStarted ~= nil then
return vehObj:getIsMotorStarted();
end
end
function VehicleStatus:getIsTurnedOn(vehObj)
if vehObj.spec_turnOnVehicle ~= nil and vehObj.getIsTurnedOn ~= nil then
return vehObj:getIsTurnedOn()
end
end
function VehicleStatus:getIsLightTurnedOn(vehObj)
if vehObj.spec_lights ~= nil and vehObj.getLightsTypesMask ~= nil then
if vehObj:getLightsTypesMask() > 0 then
return true;
else
return false;
end
end
end
function VehicleStatus:getBeaconLightsVisibility(vehObj)
if vehObj.spec_lights ~= nil and vehObj.getBeaconLightsVisibility ~= nil then
return vehObj:getBeaconLightsVisibility()
end
end
function VehicleStatus:getTurnLightState(vehObj)
if vehObj.spec_lights ~= nil and vehObj.getTurnLightState ~= nil then
return vehObj:getTurnLightState()
else
return 0;
end
end
function VehicleStatus:getIsBrakeLightsOn(vehObj)
if vehObj.spec_lights ~= nil and vehObj.spec_lights.brakeLightsVisibility ~= nil then
return vehObj.spec_lights.brakeLightsVisibility;
end
end
function VehicleStatus:getSpeedStr(vehObj)
if vehObj.getLastSpeed ~= nil then
local unit = nil
local speed = nil
if g_i18n.useMiles then
speed = math.floor(vehObj:getLastSpeed() * 0.621371)
unit = g_i18n.texts.unit_mph
else
speed = math.floor(vehObj:getLastSpeed())
unit = g_i18n.texts.unit_kmh
end
return tostring(speed) .. " " .. unit;
end
end
function VehicleStatus:RepairVehicleWithImplements(realId)
veh = g_currentMission.vehicles[realId];
VehicleSort:dp(string.format('realId {%s} for configFileName {%s}', realId, veh.configFileName), 'VehicleStatus:RepairVehicleWithImplements');
if veh ~= nil then
if veh.repairVehicle ~= nil then
veh:repairVehicle(true);
VehicleSort:dp(string.format('Repaired vehicle realId {%s} - configFileName {%s}', tostring(realId), veh.configFileName), 'VehicleStatus:RepairVehicleWithImplements');
local implements = VehicleSort:getVehImplements(realId);
if implements ~= nil then
for i = 1, #implements do
local imp = implements[i];
if imp ~= nil and imp.object ~= nil and imp.object.repairVehicle ~= nil then
imp.object:repairVehicle(true);
VehicleSort:dp(string.format('Repaired implement configFileName {%s}', tostring(imp.object.configFileName)), 'VehicleStatus:RepairVehicleWithImplements');
end
end
end
end
end
end
function VehicleStatus:CleanVehicleWithImplements(realId)
veh = g_currentMission.vehicles[realId];
VehicleSort:dp(string.format('realId {%s} for configFileName {%s}', realId, veh.configFileName), 'VehicleStatus:CleanVehicleWithImplements');
if veh ~= nil then
if veh.spec_washable ~= nil then
VehicleStatus:setDirtOnObject(veh, 0)
VehicleSort:dp(string.format('Cleaned vehicle realId {%s} - configFileName {%s}', tostring(realId), veh.configFileName), 'VehicleStatus:CleanVehicleWithImplements');
local implements = VehicleSort:getVehImplements(realId);
if implements ~= nil then
for i = 1, #implements do
local imp = implements[i];
if imp ~= nil and imp.object ~= nil and imp.object.spec_washable ~= nil then
VehicleStatus:setDirtOnObject(imp.object, 0)
VehicleSort:dp(string.format('Cleaned implement configFileName {%s}', tostring(imp.object.configFileName)), 'VehicleStatus:CleanVehicleWithImplements');
end
end
end
end
end
end
function VehicleStatus:RepaintVehicleWithImplements(realId)
veh = g_currentMission.vehicles[realId];
VehicleSort:dp(string.format('realId {%s} for configFileName {%s}', realId, veh.configFileName), 'VehicleStatus:RepaintVehicleWithImplements');
if veh ~= nil then
if veh.repaintVehicle then
veh:repaintVehicle();
VehicleSort:dp(string.format('Repainted vehicle realId {%s} - configFileName {%s}', tostring(realId), veh.configFileName), 'VehicleStatus:RepaintVehicleWithImplements');
local implements = VehicleSort:getVehImplements(realId);
if implements ~= nil then
for i = 1, #implements do
local imp = implements[i];
if imp ~= nil and imp.object ~= nil and imp.object.repaintVehicle then
imp.object:repaintVehicle();
VehicleSort:dp(string.format('Repainted implement configFileName {%s}', tostring(imp.object.configFileName)), 'VehicleStatus:RepaintVehicleWithImplements');
end
end
end
end
end
end
function VehicleStatus:getVehImplementsWear(realId)
local texts = {};
local line = "";
local implements = VehicleSort:getVehImplements(realId);
if implements ~= nil then
for i = 1, #implements do
local imp = implements[i];
if (imp ~= nil and imp.object ~= nil and imp.object.getWearTotalAmount ~= nil) then
line = string.gsub(VehicleSort:getAttachmentName(imp.object), "%s$", "") .. " | " .. g_i18n.modEnvironments[VehicleSort.ModName].texts.wear .. ": " .. VehicleSort:calcPercentage(imp.object:getWearTotalAmount(), 1) .. " %";
table.insert(texts, line);
end
end
return texts;
else
return nil;
end
end
function VehicleStatus:getDirtPercForObject(obj)
if obj ~= nil then
if obj.spec_washable ~= nil then
local nodeCount = 0;
local dirtAmount = 0;
for _, node in pairs(obj.spec_washable.washableNodes) do
dirtAmount = dirtAmount + node.dirtAmount;
nodeCount = nodeCount + 1;
end
-- Total dirt should be combined dirt / nodecount
return VehicleSort:calcPercentage(dirtAmount / nodeCount, 1);
else
return nil;
end
else
return nil;
end
end
function VehicleStatus:getVehImplementsDirt(realId)
local texts = {};
local line = "";
local implements = VehicleSort:getVehImplements(realId);
if implements ~= nil then
for i = 1, #implements do
local imp = implements[i];
if (imp ~= nil and imp.object ~= nil and VehicleStatus:getDirtPercForObject(imp.object) ~= nil) then
line = string.gsub(VehicleSort:getAttachmentName(imp.object), "%s$", "") .. " | " .. g_i18n.texts.setting_dirt .. ": " .. VehicleStatus:getDirtPercForObject(imp.object) .. " %";
table.insert(texts, line);
end
end
return texts;
else
return nil;
end
end
function VehicleStatus:setDirtOnObject(obj, dirt)
if obj.spec_washable ~= nil then
for _, node in pairs(obj.spec_washable.washableNodes) do
Washable:setNodeDirtAmount(node, dirt, force);
end
return true;
else
return nil;
end
end
function VehicleStatus:getDieselLevel(realId)
local veh = g_currentMission.vehicles[realId];
if veh.getConsumerFillUnitIndex ~= nil and veh.getFillUnitFillLevel ~= nil then
local fuelFillType = veh:getConsumerFillUnitIndex(FillType.DIESEL);
local level = veh:getFillUnitFillLevel(fuelFillType);
local capacity = veh:getFillUnitCapacity(fuelFillType);
--VehicleSort:dp(string.format('level {%s} - capacity {%s}', level, capacity), 'getDieselLevel');
if level ~= nil and capacity ~= nil then
return math.floor(level), math.floor(capacity);
else
return nil;
end
else
return nil;
end
end
function VehicleStatus:getDefLevel(realId)
local veh = g_currentMission.vehicles[realId];
if veh.getConsumerFillUnitIndex ~= nil and veh.getFillUnitFillLevel ~= nil then
local fuelFillType = veh:getConsumerFillUnitIndex(FillType.DEF);
local level = veh:getFillUnitFillLevel(fuelFillType);
local capacity = veh:getFillUnitCapacity(fuelFillType);
--VehicleSort:dp(string.format('level {%s} - capacity {%s}', level, capacity), 'getDefLevel');
if level ~= nil and capacity ~= nil then
return math.floor(level), math.floor(capacity);
else
return nil;
end
else
return nil;
end
end
function VehicleStatus:getImplementStatus(realId)
local impStatus = {};
local implements = VehicleSort:getVehImplements(realId);
if implements ~= nil then
for i = 1, #implements do
local imp = implements[i];
if imp ~= nil and imp.object ~= nil then
if imp.object.typeName ~= 'attachableFrontloader' then
local isTurnedOn = nil;
local isLowered = nil;
if imp.object.getIsTurnedOn then
isTurnedOn = imp.object:getIsTurnedOn();
end
if imp.object.getIsLowered then
isLowered = imp.object:getIsLowered();
end
local entry = {jointDescIndex = imp.object.jointDescIndex, isTurnedOn = isTurnedOn, isLowered = isLowered, name = VehicleSort:getAttachmentName(imp.object)};
table.insert(impStatus, entry);
end
end
end
end
if #impStatus > 0 then
return impStatus;
end
end
function VehicleStatus:getFieldNumber(realId)
local veh = g_currentMission.vehicles[realId];
if veh.getIsOnField and veh:getIsOnField() then
--Took the majority of the getFieldNumber code from VehicleInspector by HappyLooser. Kudos to him/her
local veh_pos_x, veh_pos_y, veh_pos_z = getWorldTranslation(veh.components[1].node)
for fieldNum,fieldDef in ipairs(g_fieldManager.fields) do
for a=1, #fieldDef.getFieldStatusPartitions do
local b = fieldDef.getFieldStatusPartitions[a];
local x, z, wX, wZ, hX, hZ = b.x0, b.z0, b.widthX, b.widthZ, b.heightX, b.heightZ;
local distanceMax = math.max(wX, wZ, hX, hZ);
local distance = MathUtil.vector2Length(veh_pos_x - x, veh_pos_z - z);
if distance <= distanceMax then
--print("distance...".. tostring(distance).. " - maxDistance...".. tostring(distanceMax))
return fieldDef.fieldId;
end;
end;
end;
else
return false;
end
end
function VehicleStatus:getOperatingHours(obj)
if obj.getOperatingTime then
local milliSeconds = obj:getOperatingTime()
local hours = string.format("%.1f h", (milliSeconds / 3600000));
return hours;
end
end
function VehicleStatus:getVehImplementsOperatingHours(realId)
local texts = {};
local line = "";
local implements = VehicleSort:getVehImplements(realId);
if implements ~= nil then
for i = 1, #implements do
local imp = implements[i];
if (imp ~= nil and imp.object ~= nil and imp.object.getOperatingTime ~= nil) then
line = string.gsub(VehicleSort:getAttachmentName(imp.object), "%s$", "") .. " | " .. g_i18n.modEnvironments[VehicleSort.ModName].texts.operationHours .. ": " .. VehicleStatus:getOperatingHours(imp.object);
table.insert(texts, line);
end
end
return texts;
else
return nil;
end
end