23 | function GasStation:new(id, trailer, customMt) |
24 | |
25 | local self = {}; |
26 | if customMt ~= nil then |
27 | setmetatable(self, customMt); |
28 | else |
29 | setmetatable(self, GasStation_mt); |
30 | end; |
31 | |
32 | self.triggerId = id; |
33 | addTrigger(id, "triggerCallback", self); |
34 | |
35 | self.appearsOnPDA = Utils.getNoNil(getUserAttribute(id, "appearsOnPDA"), true); |
36 | |
37 | self.trailer = trailer; |
38 | |
39 | self.isEnabled = true; |
40 | |
41 | self.vehiclesTriggerCount = {}; |
42 | |
43 | if self.appearsOnPDA then |
44 | local mapPosition = id; |
45 | local mapPositionIndex = getUserAttribute(id, "mapPositionIndex"); |
46 | if mapPositionIndex ~= nil then |
47 | mapPosition = Utils.indexToObject(id, mapPositionIndex); |
48 | if mapPosition == nil then |
49 | mapPosition = id; |
50 | end; |
51 | end; |
52 | |
53 | local x, _, z = getWorldTranslation(mapPosition); |
54 | |
55 | local hotspotObjectId = 0; |
56 | if self.trailer ~= nil then |
57 | hotspotObjectId = id; |
58 | end |
59 | |
60 | local fullViewName = Utils.getNoNil(getUserAttribute(id, "stationName"), "map_fuelStation") |
61 | if g_i18n:hasText(fullViewName) then |
62 | fullViewName = g_i18n:getText(fullViewName) |
63 | end |
64 | self.mapHotspot = g_currentMission.ingameMap:createMapHotspot("fuelStation", fullViewName, nil, getNormalizedUVs({264, 520, 240, 240}), nil, x, z, nil, nil, false, false, false, hotspotObjectId, nil, MapHotspot.CATEGORY_DEFAULT); |
65 | end |
66 | |
67 | self.moneyChangeId = getMoneyTypeId(); |
68 | |
69 | return self; |
70 | end; |
74 | function GasStation:delete() |
75 | -- remove the gas stations from all vehicles that are triggered by this trigger |
76 | for vehicle,count in pairs(self.vehiclesTriggerCount) do |
77 | if count > 0 then |
78 | if vehicle.removeFuelFillTrigger ~= nil then |
79 | vehicle:removeFuelFillTrigger(self); |
80 | end; |
81 | end; |
82 | end; |
83 | |
84 | if self.mapHotspot ~= nil then |
85 | g_currentMission.ingameMap:deleteMapHotspot(self.mapHotspot); |
86 | end |
87 | |
88 | removeTrigger(self.triggerId); |
89 | end; |
104 | function GasStation:fillFuel(vehicle, delta) |
105 | if self.trailer ~= nil then |
106 | local trailerFuelFillLevel = self.trailer:getFillLevel(FillUtil.FILLTYPE_FUEL); |
107 | if trailerFuelFillLevel > 0 then |
108 | delta = math.min(delta, trailerFuelFillLevel); |
109 | if delta <= 0 then |
110 | return 0; |
111 | end |
112 | else |
113 | return 0; |
114 | end |
115 | end |
116 | |
117 | if vehicle.setFuelFillLevel ~= nil then |
118 | local oldFillLevel = vehicle.fuelFillLevel |
119 | vehicle:setFuelFillLevel(vehicle.fuelFillLevel + delta); |
120 | delta = vehicle.fuelFillLevel - oldFillLevel; |
121 | else |
122 | if not vehicle:allowFillType(FillUtil.FILLTYPE_FUEL, false) then |
123 | return 0; |
124 | end |
125 | local oldFillLevel = vehicle:getFillLevel(FillUtil.FILLTYPE_FUEL); |
126 | vehicle:setFillLevel(oldFillLevel + delta, FillUtil.FILLTYPE_FUEL); |
127 | delta = vehicle:getFillLevel(FillUtil.FILLTYPE_FUEL) - oldFillLevel; |
128 | end |
129 | |
130 | if delta > 0 then |
131 | if self.trailer ~= nil then |
132 | self.trailer:setFillLevel(self.trailer:getFillLevel(FillUtil.FILLTYPE_FUEL) - delta, FillUtil.FILLTYPE_FUEL); |
133 | else |
134 | local price = delta * g_currentMission.economyManager:getPricePerLiter(FillUtil.FILLTYPE_FUEL); |
135 | g_currentMission.missionStats:updateStats("expenses", price); |
136 | |
137 | local userId = g_currentMission:getServerUserId() |
138 | local user = g_currentMission:findUserByConnection(vehicle:getOwner()) |
139 | if user ~= nil then |
140 | userId = user.userId |
141 | end |
142 | g_currentMission:addMoney(-price, userId, "purchaseFuel") |
143 | g_currentMission:addMoneyChange(-price, self.moneyChangeId); |
144 | end |
145 | end |
146 | return delta; |
147 | end |
172 | function GasStation:triggerCallback(triggerId, otherId, onEnter, onLeave, onStay) |
173 | |
174 | if self.isEnabled and (onEnter or onLeave) then |
175 | |
176 | local vehicle = g_currentMission.nodeToVehicle[otherId]; |
177 | if vehicle ~= nil and vehicle.addFuelFillTrigger ~= nil and vehicle.removeFuelFillTrigger ~= nil and vehicle ~= self then |
178 | local count = Utils.getNoNil(self.vehiclesTriggerCount[vehicle], 0); |
179 | |
180 | if onEnter then |
181 | self.vehiclesTriggerCount[vehicle] = count+1; |
182 | if count == 0 then |
183 | vehicle:addFuelFillTrigger(self); |
184 | end |
185 | else -- onLeave |
186 | self.vehiclesTriggerCount[vehicle] = count-1; |
187 | if count == 1 then |
188 | self.vehiclesTriggerCount[vehicle] = nil; |
189 | vehicle:removeFuelFillTrigger(self); |
190 | g_currentMission:showMoneyChange(self.moneyChangeId, g_i18n:getText("finance_purchaseFuel")); |
191 | end |
192 | end; |
193 | end; |
194 | end; |
195 | end; |