LUADOC - Farming Simulator 17

Printable Version

Washable

Description
This is the specialization for all washable vehicles
Functions

preLoad

Description
Called before loading
Definition
preLoad(table savegame)
Arguments
tablesavegamesavegame
Code
21function Washable:preLoad(savegame)
22 self.setDirtAmount = Washable.setDirtAmount;
23 self.getDirtAmount = Washable.getDirtAmount;
24 self.getDirtMultiplier = Washable.getDirtMultiplier;
25 self.addWashingTrigger = Washable.addWashingTrigger;
26 self.removeWashingTrigger = Washable.removeWashingTrigger;
27 self.addWashableNode = Washable.addWashableNode;
28 self.removeWashableNode = Washable.removeWashableNode;
29end

load

Description
Called on loading
Definition
load(table savegame)
Arguments
tablesavegamesavegame
Code
34function Washable:load(savegame)
35
36 self.sentDirtAmount = 0;
37 self.washableDirtyFlag = self:getNextDirtyFlag();
38 self.dirtAmount = 0;
39
40 if hasXMLProperty(self.xmlFile, "vehicle.washable#dirtDuration") then
41 self.washableNodes = {};
42 self.dirtDuration = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.washable#dirtDuration"), 1) * 60 * 1000;
43 if self.dirtDuration ~= 0 then
44 self.dirtDuration = 1 / self.dirtDuration; -- dirtDuration == 0 disables dirt effect
45 end
46 self.washDuration = math.max(Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.washable#washDuration"), 1) * 60 * 1000, 0.00001); -- washDuration == 0 washes vehicle immediately
47 self.workMultiplier = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.washable#workMultiplier"), 1);
48 self.fieldMultiplier = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.washable#fieldMultiplier"), 2);
49 end
50
51 self.washingTriggers = {};
52end

postLoad

Description
Called after loading
Definition
postLoad(table savegame)
Arguments
tablesavegamesavegame
Code
57function Washable:postLoad(savegame)
58 -- getting als washable nodes in postLoad to make sure also linked nodes are washable
59 if self.washableNodes ~= nil then
60 for _, comp in pairs(self.components) do
61 self:addWashableNode(comp.node);
62 end
63
64 if savegame ~= nil and Washable.getIntervalMultiplier() ~= 0 then
65 self:setDirtAmount(Utils.getNoNil(getXMLFloat(savegame.xmlFile, savegame.key.."#dirtAmount"), 0), true);
66 else
67 self:setDirtAmount(0, true);
68 end
69 end
70end

readStream

Description
Called on client side on join
Definition
readStream(integer streamId, integer connection)
Arguments
integerstreamIdstreamId
integerconnectionconnection
Code
76function Washable:readStream(streamId, connection)
77 if self.washableNodes ~= nil then
78 local dirtAmount = streamReadInt8(streamId) / 100;
79 self.sentDirtAmount = dirtAmount;
80 self:setDirtAmount(dirtAmount, true);
81 end
82end

writeStream

Description
Called on server side on join
Definition
writeStream(integer streamId, integer connection)
Arguments
integerstreamIdstreamId
integerconnectionconnection
Code
88function Washable:writeStream(streamId, connection)
89 if self.washableNodes ~= nil then
90 streamWriteInt8(streamId, math.floor(self:getDirtAmount()*100));
91 end
92end

readUpdateStream

Description
Called on on update
Definition
readUpdateStream(integer streamId, integer timestamp, table connection)
Arguments
integerstreamIdstream ID
integertimestamptimestamp
tableconnectionconnection
Code
99function Washable:readUpdateStream(streamId, timestamp, connection)
100 if connection:getIsServer() and self.washableNodes ~= nil then
101 local dirty = streamReadBool(streamId);
102 if dirty then
103 local dirtAmount = streamReadInt8(streamId) / 100;
104 self:setDirtAmount(dirtAmount, true);
105 end
106 end
107end

writeUpdateStream

Description
Called on on update
Definition
writeUpdateStream(integer streamId, table connection, integer dirtyMask)
Arguments
integerstreamIdstream ID
tableconnectionconnection
integerdirtyMaskdirty mask
Code
114function Washable:writeUpdateStream(streamId, connection, dirtyMask)
115 if not connection:getIsServer() and self.washableNodes ~= nil then
116 if streamWriteBool(streamId, bitAND(dirtyMask, self.washableDirtyFlag) ~= 0) then
117 streamWriteInt8(streamId, math.floor(self.sentDirtAmount*100));
118 end
119 end
120end

delete

Description
Called on deleting
Definition
delete()
Code
124function Washable:delete()
125 for _, trigger in pairs(self.washingTriggers) do
126 trigger:onVehicleDeleted(self);
127 end
128end

updateTick

Description
Called on update tick
Definition
updateTick(float dt)
Arguments
floatdttime since last call in ms
Code
142function Washable:updateTick(dt)
143 if self.washableNodes ~= nil then
144 if self.isServer then
145 if g_currentMission.environment.lastRainScale > 0.1 and g_currentMission.environment.timeSinceLastRain < 30 then
146 local amount = self:getDirtAmount();
147 if amount > 0.5 then
148 amount = self:getDirtAmount() - (dt/self.washDuration);
149 end
150 self:setDirtAmount(amount);
151 else
152 if self:getIsActive() or self.isActive then
153 self:setDirtAmount(self:getDirtAmount() + (dt * self.dirtDuration)*self:getDirtMultiplier()*Washable.getIntervalMultiplier());
154 end
155 end
156 end
157 end
158end

setDirtAmount

Description
Set dirt amount
Definition
setDirtAmount(float dirtAmount, boolean force)
Arguments
floatdirtAmountnew dirt amount [0..1]
booleanforceforce action
Code
167function Washable:setDirtAmount(dirtAmount, force)
168 if self.washableNodes ~= nil then
169 self.dirtAmount = Utils.clamp(Utils.getNoNil(dirtAmount, self.dirtAmount), 0, 1);
170
171 -- only update shader if dirt change sum is about 1%
172 if math.abs(self.sentDirtAmount - self.dirtAmount) > 0.01 or force then
173 for _, node in pairs(self.washableNodes) do
174 local x,_,z,w = getShaderParameter(node, "RDT");
175 setShaderParameter(node, "RDT", x, self.dirtAmount, z, w, false);
176 end
177
178 if self.isServer then
179 -- use min threshold (0.01) for sync limitation
180 self:raiseDirtyFlags(self.washableDirtyFlag);
181 self.sentDirtAmount = self.dirtAmount;
182 end
183 end
184 end
185end

addWashableNode

Description
Add node to washable nodes
Definition
addWashableNode(integer node)
Arguments
integernodeid of node to add
Code
190function Washable:addWashableNode(node)
191 if self.washableNodes ~= nil and node ~= nil then
192 Utils.getNodesByShaderParam(node, "RDT", self.washableNodes);
193 end
194end

removeWashableNode

Description
Remove node from washable nodes
Definition
removeWashableNode(integer node)
Arguments
integernodeid of node to remove
Code
199function Washable:removeWashableNode(node)
200 if self.washableNodes ~= nil and node ~= nil then
201 self.washableNodes[node] = nil;
202 end
203end

getDirtAmount

Description
Returns current dirt amount
Definition
getDirtAmount()
Return Values
floatdirtAmountdirt amount
Code
208function Washable:getDirtAmount(superFunc)
209 return self.dirtAmount;
210end

getDirtMultiplier

Description
Returns current dirt multiplier
Definition
getDirtMultiplier()
Return Values
floatdirtMultipliercurrent dirt multiplier
Code
215function Washable:getDirtMultiplier(superFunc)
216 local multiplier = 1;
217 if self:getLastSpeed() < 1 then
218 multiplier = 0;
219 end
220
221 if self:getIsOnField() then
222 multiplier = multiplier * self.fieldMultiplier;
223 end
224
225 if superFunc ~= nil then
226 multiplier = multiplier + superFunc(self);
227 end
228 return multiplier;
229end

getSaveAttributesAndNodes

Description
Returns attributes and nodes to save
Definition
getSaveAttributesAndNodes(table nodeIdent)
Arguments
tablenodeIdentnode ident
Return Values
stringattributesattributes
stringnodesnodes
Code
236function Washable:getSaveAttributesAndNodes(nodeIdent)
237 local attributes = 'dirtAmount="'.. tostring(self.dirtAmount) .. '"';
238 return attributes, nil;
239end

addWashingTrigger

Description
Add trigger to washing triggers
Definition
addWashingTrigger(table trigger)
Arguments
tabletriggertrigger to add
Code
244function Washable:addWashingTrigger(trigger)
245 table.insert(self.washingTriggers, trigger);
246end

removeWashingTrigger

Description
Remove trigger from washing triggers
Definition
removeWashingTrigger(table trigger)
Arguments
tabletriggertrigger to remove
Code
251function Washable:removeWashingTrigger(trigger)
252 for i=1, table.getn(self.washingTriggers) do
253 if self.washingTriggers[i] == trigger then
254 table.remove(self.washingTriggers, i);
255 break;
256 end
257 end
258end

getIntervalMultiplier

Description
Returns dirt interval multiplier set by user
Definition
getIntervalMultiplier()
Return Values
floatmultipliermultiplier
Code
263function Washable.getIntervalMultiplier()
264 if g_currentMission.missionInfo.dirtInterval == 1 then
265 return 0;
266 elseif g_currentMission.missionInfo.dirtInterval == 2 then
267 return 0.25;
268 elseif g_currentMission.missionInfo.dirtInterval == 3 then
269 return 0.5;
270 elseif g_currentMission.missionInfo.dirtInterval == 4 then
271 return 1;
272 end
273end