forked from JumpyDuke/UniversalProcessKit_FS15
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUPK_WashTrigger.lua
156 lines (129 loc) · 5.4 KB
/
UPK_WashTrigger.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
-- by mor2000
--------------------
-- EntityTrigger (enables modules if vehicle or walker is present)
local UPK_WashTrigger_mt = ClassUPK(UPK_WashTrigger,UniversalProcessKit)
InitObjectClass(UPK_WashTrigger, "UPK_WashTrigger")
UniversalProcessKit.addModule("washtrigger",UPK_WashTrigger)
function UPK_WashTrigger:new(nodeId, parent)
printFn('UPK_WashTrigger:new(',nodeId,', ',parent,')')
local self = UniversalProcessKit:new(nodeId, parent, UPK_WashTrigger_mt)
registerObjectClassName(self, "UPK_WashTrigger")
self.washPerSecond = getNumberFromUserAttribute(nodeId, "washPerSecond", 0.05)
self.dirtThreshold = getNumberFromUserAttribute(nodeId, "dirtThreshold", 0.05, 0, 1)
self.pricePerSecond = getNumberFromUserAttribute(nodeId, "pricePerSecond", 0)
self.pricePerSecondMultiplier = getVectorFromUserAttribute(nodeId, "pricePerSecondMultiplier", "1 1 1")
-- statName
self.statName=getStringFromUserAttribute(nodeId, "statName")
local validStatName=false
if self.statName~=nil then
for _,v in pairs(FinanceStats.statNames) do
if self.statName==v then
validStatName=true
break
end
end
end
if not validStatName then
self.statName="other"
end
-- if washing
self.enableChildrenIfWashing = getBoolFromUserAttribute(nodeId, "enableChildrenIfWashing", false)
self.disableChildrenIfWashing = getBoolFromUserAttribute(nodeId, "disableChildrenIfWashing", false)
if self.enableChildrenIfWashing then
self.disableChildrenIfWashing = false
end
-- if not washing
self.enableChildrenIfNotWashing = getBoolFromUserAttribute(nodeId, "enableChildrenIfNotWashing", false)
self.disableChildrenIfNotWashing = getBoolFromUserAttribute(nodeId, "disableChildrenIfNotWashing", false)
if self.enableChildrenIfNotWashing then
self.disableChildrenIfNotWashing = false
end
self.allowedVehicles={}
self.allowedVehicles[UniversalProcessKit.VEHICLE_MOTORIZED] = getBoolFromUserAttribute(nodeId, "allowMotorized", true)
self.allowedVehicles[UniversalProcessKit.VEHICLE_COMBINE] = getBoolFromUserAttribute(nodeId, "allowCombine", true)
self.allowedVehicles[UniversalProcessKit.VEHICLE_TIPPER] = getBoolFromUserAttribute(nodeId, "allowTipper", true)
self.allowedVehicles[UniversalProcessKit.VEHICLE_SHOVEL] = getBoolFromUserAttribute(nodeId, "allowShovel", true)
self.allowedVehicles[UniversalProcessKit.VEHICLE_FILLABLE] = getBoolFromUserAttribute(nodeId, "allowShovel", true)
self.allowedVehicles[UniversalProcessKit.VEHICLE_ATTACHMENT] = getBoolFromUserAttribute(nodeId, "allowAttachment", true)
self.allowWalker = getBoolFromUserAttribute(nodeId, "allowWalker", false)
self:addTrigger()
self:printFn('UPK_WashTrigger:new done')
return self
end
function UPK_WashTrigger:postLoad()
self:printFn('UPK_WashTrigger:postLoad()')
UPK_WashTrigger:superClass().postLoad(self)
self:triggerUpdate(false,false)
self:update(30)
end
function UPK_WashTrigger:triggerUpdate(vehicle,isInTrigger)
self:printFn('UPK_WashTrigger:triggerUpdate(',vehicle,', ',isInTrigger,')')
if self.isEnabled and self.washPerSecond~=0 then
for k,v in pairs(self.allowedVehicles) do
if v and UniversalProcessKit.isVehicleType(vehicle, k) then
if isInTrigger then
--self.amountToFillOfVehicle[vehicle]=0
--self:print('UniversalProcessKitListener.addUpdateable('..tostring(self)..')')
UniversalProcessKitListener.addUpdateable(self)
else
--self.amountToFillOfVehicle[vehicle]=nil
end
end
end
end
end
function UPK_WashTrigger:update(dt)
self:printAll('UPK_WashTrigger:update(',dt,')')
if self.entitiesInTrigger==0 then
if self.isServer then
if self.enableChildrenIfNotWashing then
self:enableChildren(true)
elseif self.disableChildrenIfNotWashing then
self:enableChildren(false)
end
end
UniversalProcessKitListener.removeUpdateable(self)
return
end
if self.isEnabled and self.washPerSecond~=0 then
local dirtToRemove = self.washPerSecond/1000 * dt
local dirtRemoved = 0
for _,vehicle in pairs(self.entities) do
if vehicle.dirtAmount~=nil and vehicle.setDirtAmount~=nil then
if self.washPerSecond>0 and vehicle.dirtAmount>self.dirtThreshold then
--self:print('dirt Amount: '..tostring(vehicle.dirtAmount))
local dirtRemovedVehicle = mathmin(vehicle.dirtAmount, dirtToRemove)
dirtRemoved = mathmax(dirtRemoved, dirtRemovedVehicle)
vehicle:setDirtAmount(vehicle.dirtAmount - dirtRemovedVehicle)
elseif self.washPerSecond<0 and vehicle.dirtAmount<self.dirtThreshold then
local dirtAddedVehicle = mathmin(self.dirtThreshold-vehicle.dirtAmount,-dirtToRemove)
dirtRemoved = mathmax(dirtRemoved, dirtAddedVehicle)
vehicle:setDirtAmount(vehicle.dirtAmount + dirtAddedVehicle)
end
end
end
if round(dirtRemoved,8)==0 then
if self.isServer then
if self.enableChildrenIfNotWashing then
self:enableChildren(true)
elseif self.disableChildrenIfNotWashing then
self:enableChildren(false)
end
end
else
if self.isServer then
if self.pricePerSecond>0 then
local difficulty = g_currentMission.missionStats.difficulty
local pricePerSecondAdjustment = self.pricePerSecondMultiplier[difficulty]
local price = (self.pricePerSecond/1000 * dt) * pricePerSecondAdjustment
g_currentMission:addSharedMoney(-price, self.statName)
end
if self.enableChildrenIfWashing then
self:enableChildren(true)
elseif self.disableChildrenIfWashing then
self:enableChildren(false)
end
end
end
end
end