85 | function FillTrigger:delete() |
86 | -- remove the gas stations from all vehicles that are triggered by this trigger |
87 | for vehicle,count in pairs(self.vehiclesTriggerCount) do |
88 | if count > 0 then |
89 | if vehicle.removeFillUnitTrigger ~= nil then |
90 | vehicle:removeFillUnitTrigger(self) |
91 | end |
92 | end |
93 | end |
94 | |
95 | if self.mapHotspot ~= nil then |
96 | g_currentMission:removeMapHotspot(self.mapHotspot) |
97 | self.mapHotspot:delete() |
98 | end |
99 | |
100 | g_soundManager:deleteSample(self.sample) |
101 | |
102 | removeTrigger(self.triggerId) |
103 | end |
187 | function FillTrigger:fillTriggerCallback(triggerId, otherId, onEnter, onLeave, onStay) |
188 | if self.isEnabled and (onEnter or onLeave) then |
189 | local vehicle = g_currentMission:getNodeObject(otherId) |
190 | if vehicle ~= nil and vehicle.addFillUnitTrigger ~= nil and vehicle.removeFillUnitTrigger ~= nil and vehicle ~= self and vehicle ~= self.sourceObject then |
191 | local count = Utils.getNoNil(self.vehiclesTriggerCount[vehicle], 0) |
192 | if onEnter then |
193 | local fillType = self:getCurrentFillType() |
194 | local fillUnitIndex = vehicle:getFirstValidFillUnitToFill(fillType) |
195 | if fillUnitIndex ~= nil then |
196 | self.vehiclesTriggerCount[vehicle] = count + 1 |
197 | |
198 | if count == 0 then |
199 | vehicle:addFillUnitTrigger(self, fillType, fillUnitIndex) |
200 | end |
201 | end |
202 | else |
203 | self.vehiclesTriggerCount[vehicle] = count - 1 |
204 | if count <= 1 then |
205 | self.vehiclesTriggerCount[vehicle] = nil |
206 | vehicle:removeFillUnitTrigger(self) |
207 | g_currentMission:showMoneyChange(self.moneyChangeType, nil, false, vehicle:getActiveFarm()) |
208 | end |
209 | end |
210 | end |
211 | end |
212 | end |
118 | function FillTrigger:fillVehicle(vehicle, delta, dt) |
119 | if self.fillLitersPerSecond ~= nil then |
120 | delta = math.max(delta, self.fillLitersPerSecond*0.001*dt) |
121 | end |
122 | |
123 | local farmId = vehicle:getActiveFarm() |
124 | |
125 | if self.sourceObject ~= nil then |
126 | local sourceFuelFillLevel = self.sourceObject:getFillUnitFillLevel(self.fillUnitIndex) |
127 | if sourceFuelFillLevel > 0 and g_currentMission.accessHandler:canFarmAccess(farmId, self.sourceObject) then |
128 | delta = math.min(delta, sourceFuelFillLevel) |
129 | if delta <= 0 then |
130 | return 0 |
131 | end |
132 | else |
133 | return 0 |
134 | end |
135 | end |
136 | |
137 | local fillType = self:getCurrentFillType() |
138 | |
139 | local fillUnitIndex = vehicle:getFirstValidFillUnitToFill(fillType) |
140 | if fillUnitIndex == nil then |
141 | return 0 |
142 | end |
143 | |
144 | delta = vehicle:addFillUnitFillLevel(farmId, fillUnitIndex, delta, fillType, ToolType.TRIGGER, nil) |
145 | |
146 | if delta > 0 then |
147 | if self.sourceObject ~= nil then |
148 | self.sourceObject:addFillUnitFillLevel(farmId, self.fillUnitIndex, -delta, fillType, ToolType.TRIGGER, nil) |
149 | else |
150 | local price = delta * g_currentMission.economyManager:getPricePerLiter(fillType) |
151 | g_farmManager:updateFarmStats(farmId, "expenses", price) |
152 | |
153 | local userId = g_currentMission:getServerUserId() |
154 | local user = g_currentMission.userManager:getUserByConnection(vehicle:getOwner()) |
155 | if user ~= nil then |
156 | userId = user:getId() |
157 | end |
158 | |
159 | g_currentMission:addMoney(-price, farmId, self.moneyChangeType, true) |
160 | end |
161 | end |
162 | |
163 | return delta |
164 | end |
26 | function FillTrigger:new(id, sourceObject, fillUnitIndex, fillLitersPerSecond, defaultFillType, customMt) |
27 | local self = {} |
28 | setmetatable(self, customMt or FillTrigger_mt) |
29 | |
30 | self.customEnvironment = g_currentMission.loadingMapModName |
31 | |
32 | self.triggerId = id |
33 | addTrigger(id, "fillTriggerCallback", self) |
34 | |
35 | -- place sound at the same position as the trigger |
36 | self.soundNode = createTransformGroup("fillTriggerSoundNode") |
37 | link(getParent(id), self.soundNode) |
38 | setTranslation(self.soundNode, getTranslation(id)) |
39 | |
40 | self.sourceObject = sourceObject |
41 | self.vehiclesTriggerCount = {} |
42 | self.fillUnitIndex = fillUnitIndex |
43 | self.fillLitersPerSecond = fillLitersPerSecond |
44 | self.appearsOnPDA = Utils.getNoNil(getUserAttribute(id, "appearsOnPDA"), true) |
45 | self.isEnabled = true |
46 | |
47 | self.fillTypeIndex = FillType.DIESEL |
48 | |
49 | if self.appearsOnPDA and sourceObject == nil then |
50 | local mapPosition = id |
51 | local mapPositionIndex = getUserAttribute(id, "mapPositionIndex") |
52 | if mapPositionIndex ~= nil then |
53 | mapPosition = I3DUtil.indexToObject(id, mapPositionIndex) |
54 | if mapPosition == nil then |
55 | mapPosition = id |
56 | end |
57 | end |
58 | |
59 | local x, _, z = getWorldTranslation(mapPosition) |
60 | |
61 | local fullViewName = Utils.getNoNil(getUserAttribute(id, "stationName"), "map_fuelStation") |
62 | if g_i18n:hasText(fullViewName, self.customEnvironment) then |
63 | fullViewName = g_i18n:getText(fullViewName, self.customEnvironment) |
64 | end |
65 | |
66 | self.mapHotspot = MapHotspot:new("fuelStation", MapHotspot.CATEGORY_DEFAULT) |
67 | self.mapHotspot:setText(fullViewName) |
68 | self.mapHotspot:setWorldPosition(x, z) |
69 | self.mapHotspot:setBorderedImage(nil, getNormalizedUVs(MapHotspot.UV.GAS_STATION)) |
70 | |
71 | g_currentMission:addMapHotspot(self.mapHotspot) |
72 | end |
73 | |
74 | return self |
75 | end |