Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de
1 | -- Copyright (C) GIANTS Software GmbH, Confidential, All Rights Reserved. |
2 | |
3 | GasStation = {}; |
4 | |
5 | local GasStation_mt = Class(GasStation); |
6 | |
7 | function GasStation:onCreate(id) |
8 | g_currentMission:addNonUpdateable(GasStation:new(id)); |
9 | -- print("created gas station trigger, id: ", id); |
10 | end; |
11 | |
12 | function GasStation:new(id, trailer, customMt) |
13 | |
14 | local self = {}; |
15 | if customMt ~= nil then |
16 | setmetatable(self, customMt); |
17 | else |
18 | setmetatable(self, GasStation_mt); |
19 | end; |
20 | |
21 | self.triggerId = id; |
22 | addTrigger(id, "triggerCallback", self); |
23 | |
24 | self.appearsOnPDA = Utils.getNoNil(getUserAttribute(id, "appearsOnPDA"), true); |
25 | |
26 | self.trailer = trailer; |
27 | |
28 | self.isEnabled = true; |
29 | |
30 | self.vehiclesTriggerCount = {}; |
31 | |
32 | if self.appearsOnPDA then |
33 | local mapPosition = id; |
34 | local mapPositionIndex = getUserAttribute(id, "mapPositionIndex"); |
35 | if mapPositionIndex ~= nil then |
36 | mapPosition = Utils.indexToObject(id, mapPositionIndex); |
37 | if mapPosition == nil then |
38 | mapPosition = id; |
39 | end; |
40 | end; |
41 | |
42 | local x, _, z = getWorldTranslation(mapPosition); |
43 | |
44 | local hotspotObjectId = 0; |
45 | if self.trailer ~= nil then |
46 | hotspotObjectId = id; |
47 | end |
48 | self.mapHotspot = g_currentMission.ingameMap:createMapHotspot("FuelStation", "dataS2/menu/hud/hud_pda_spot_fuelStation.png", x, z, nil, nil, false, false, false, hotspotObjectId, true); |
49 | end |
50 | |
51 | self.moneyChangeId = getMoneyTypeId(); |
52 | |
53 | return self; |
54 | end; |
55 | |
56 | function GasStation:delete() |
57 | -- remove the gas stations from all vehicles that are triggered by this trigger |
58 | for vehicle,count in pairs(self.vehiclesTriggerCount) do |
59 | if count > 0 then |
60 | if vehicle.removeFuelFillTrigger ~= nil then |
61 | vehicle:removeFuelFillTrigger(self); |
62 | end; |
63 | end; |
64 | end; |
65 | |
66 | if self.mapHotspot ~= nil then |
67 | g_currentMission.ingameMap:deleteMapHotspot(self.mapHotspot); |
68 | end |
69 | |
70 | removeTrigger(self.triggerId); |
71 | end; |
72 | |
73 | function GasStation:onVehicleDeleted(vehicle) |
74 | self.vehiclesTriggerCount[vehicle] = nil; |
75 | g_currentMission:showMoneyChange(self.moneyChangeId); |
76 | end |
77 | |
78 | function GasStation:fillFuel(vehicle, delta) |
79 | if self.trailer ~= nil then |
80 | local trailerFuelFillLevel = self.trailer:getFillLevel(Fillable.FILLTYPE_FUEL); |
81 | if trailerFuelFillLevel > 0 then |
82 | delta = math.min(delta, trailerFuelFillLevel); |
83 | if delta <= 0 then |
84 | return 0; |
85 | end |
86 | else |
87 | return 0; |
88 | end |
89 | end |
90 | |
91 | if vehicle.setFuelFillLevel ~= nil then |
92 | local oldFillLevel = vehicle.fuelFillLevel |
93 | vehicle:setFuelFillLevel(vehicle.fuelFillLevel + delta); |
94 | delta = vehicle.fuelFillLevel - oldFillLevel; |
95 | else |
96 | if not vehicle:allowFillType(Fillable.FILLTYPE_FUEL, false) then |
97 | return 0; |
98 | end |
99 | local oldFillLevel = vehicle:getFillLevel(Fillable.FILLTYPE_FUEL); |
100 | vehicle:setFillLevel(oldFillLevel + delta, Fillable.FILLTYPE_FUEL); |
101 | delta = vehicle:getFillLevel(Fillable.FILLTYPE_FUEL) - oldFillLevel; |
102 | end |
103 | |
104 | if delta > 0 then |
105 | if self.trailer ~= nil then |
106 | self.trailer:setFillLevel(self.trailer:getFillLevel(Fillable.FILLTYPE_FUEL) - delta, Fillable.FILLTYPE_FUEL); |
107 | else |
108 | local price = delta * g_fuelPricePerLiter; |
109 | |
110 | g_currentMission.missionStats:updateStats("expenses", price); |
111 | g_currentMission:addSharedMoney(-price, "vehicleRunningCost"); |
112 | g_currentMission:addMoneyChange(-price, self.moneyChangeId); |
113 | end |
114 | end |
115 | return delta; |
116 | end |
117 | |
118 | function GasStation:getIsActivatable(vehicle) |
119 | if self.trailer ~= nil then |
120 | if self.trailer:getFillLevel(Fillable.FILLTYPE_FUEL) <= 0 then |
121 | return false; |
122 | end |
123 | end |
124 | if vehicle.setFuelFillLevel == nil and not vehicle:allowFillType(Fillable.FILLTYPE_FUEL, false) then |
125 | return false; |
126 | end |
127 | return true; |
128 | end |
129 | |
130 | function GasStation:triggerCallback(triggerId, otherId, onEnter, onLeave, onStay) |
131 | |
132 | if self.isEnabled and (onEnter or onLeave) then |
133 | |
134 | local vehicle = g_currentMission.nodeToVehicle[otherId]; |
135 | if vehicle ~= nil and vehicle.addFuelFillTrigger ~= nil and vehicle.removeFuelFillTrigger ~= nil and vehicle ~= self then |
136 | local count = Utils.getNoNil(self.vehiclesTriggerCount[vehicle], 0); |
137 | |
138 | if onEnter then |
139 | self.vehiclesTriggerCount[vehicle] = count+1; |
140 | if count == 0 then |
141 | vehicle:addFuelFillTrigger(self); |
142 | end |
143 | else -- onLeave |
144 | self.vehiclesTriggerCount[vehicle] = count-1; |
145 | if count == 1 then |
146 | self.vehiclesTriggerCount[vehicle] = nil; |
147 | vehicle:removeFuelFillTrigger(self); |
148 | g_currentMission:showMoneyChange(self.moneyChangeId); |
149 | end |
150 | end; |
151 | end; |
152 | end; |
153 | end;
|
Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de