LUADOC - Farming Simulator 19

Bale

Description
Class for bales
Parent
MountableObject
Functions

applyExtraAttributes

Description
Definition
applyExtraAttributes()
Code
327function Bale:applyExtraAttributes(attributes)
328 if self.supportsWrapping then
329 setShaderParameter(self.meshNode, "wrappingState", attributes.wrappingState, 0, 0, 0, false)
330 self.wrappingState = attributes.wrappingState
331 self:setColor(unpack(attributes.wrappingColor))
332 end
333
334 self.baleValueScale = attributes.baleValueScale
335
336 return true;
337end

createNode

Description
Load node from i3d file
Definition
createNode(string i3dFilename)
Arguments
stringi3dFilenamei3d file name
Code
213function Bale:createNode(i3dFilename)
214 self.i3dFilename = i3dFilename;
215 self.customEnvironment, self.baseDirectory = Utils.getModNameAndBaseDirectory(i3dFilename);
216 local baleRoot = g_i3DManager:loadSharedI3DFile(i3dFilename);
217
218 local baleId = getChildAt(baleRoot, 0);
219 link(getRootNode(), baleId);
220 delete(baleRoot);
221
222 self:setNodeId(baleId);
223end

delete

Description
Deleting bale object
Definition
delete()
Code
41function Bale:delete()
42 if self.i3dFilename ~= nil then
43 g_i3DManager:releaseSharedI3DFile(self.i3dFilename, nil, true);
44 end
45 g_currentMission:removeLimitedObject(FSBaseMission.LIMITED_OBJECT_TYPE_BALE, self);
46 unregisterObjectClassName(self);
47 g_currentMission:removeItemToSave(self);
48 Bale:superClass().delete(self);
49end

getCanBeSold

Description
Returns if bale can be sold
Definition
getCanBeSold()
Return Values
booleancanBeSoldbale can be sold
Code
396function Bale:getCanBeSold()
397 return self.canBeSold
398end

getFillLevel

Description
Get fill level of bale
Definition
getFillLevel()
Return Values
integerfillLevelcurrent fill level
Code
375function Bale:getFillLevel()
376 return self.fillLevel;
377end

getFillType

Description
Get fill type of bale
Definition
getFillType()
Return Values
integerfillTypecurrent fill type id
Code
368function Bale:getFillType()
369 return self.fillType;
370end

getValue

Description
Get price value of bale
Definition
getValue()
Code
360function Bale:getValue()
361 local pricePerLiter = g_currentMission.economyManager:getPricePerLiter(self.fillType);
362 return self.fillLevel * pricePerLiter * self.baleValueScale;
363end

load

Description
Load bale
Definition
load(string i3dFilename, float x, float y, float z, float rx, float ry, float rz, integer fillLevel)
Arguments
stringi3dFilenamei3d file name
floatxx world position
floatyz world position
floatzz world position
floatrxrx world rotation
floatryry world rotation
floatrzrz world rotation
integerfillLevelfill level
Code
235function Bale:load(i3dFilename, x,y,z, rx,ry,rz, fillLevel)
236 self.i3dFilename = i3dFilename;
237 self.customEnvironment, self.baseDirectory = Utils.getModNameAndBaseDirectory(i3dFilename);
238 self:createNode(i3dFilename);
239 setTranslation(self.nodeId, x, y, z);
240 setRotation(self.nodeId, rx, ry, rz);
241 if fillLevel ~= nil then
242 self.fillLevel = fillLevel;
243 end
244 g_currentMission:addItemToSave(self);
245end

loadExtraAttributesFromXMLFile

Description
Definition
loadExtraAttributesFromXMLFile()
Code
316function Bale:loadExtraAttributesFromXMLFile(attributes, xmlFile, key, resetVehicles)
317 attributes.wrappingState = Utils.getNoNil(getXMLFloat(xmlFile, key.."#wrappingState"), 0)
318 attributes.wrappingColor = {StringUtil.getVectorFromString(getXMLString(xmlFile, key.."#wrappingColor"))}
319
320 attributes.baleValueScale = Utils.getNoNil(tonumber(getXMLString(xmlFile, key.."#valueScale")), 1)
321
322 return true;
323end

loadFromMemory

Description
Setting node id and i3d file name
Definition
loadFromMemory(integer nodeId, string i3dFilename)
Arguments
integernodeIdnode id
stringi3dFilenamei3d file name
Code
251function Bale:loadFromMemory(nodeId, i3dFilename)
252 self.i3dFilename = i3dFilename;
253 self.customEnvironment, self.baseDirectory = Utils.getModNameAndBaseDirectory(i3dFilename);
254 self:setNodeId(nodeId);
255end

loadFromXMLFile

Description
Loading from attributes and nodes
Definition
loadFromXMLFile(integer xmlFile, string key, boolean resetVehicles)
Arguments
integerxmlFileid of xml object
stringkeykey
booleanresetVehiclesreset vehicles
Return Values
booleansuccesssuccess
Code
263function Bale:loadFromXMLFile(xmlFile, key, resetVehicles)
264
265 local x,y,z = StringUtil.getVectorFromString(getXMLString(xmlFile, key.."#position"));
266 local xRot,yRot,zRot = StringUtil.getVectorFromString(getXMLString(xmlFile, key.."#rotation"));
267 if x == nil or y == nil or z == nil or xRot == nil or yRot == nil or zRot == nil then
268 return false;
269 end;
270 xRot = math.rad(xRot)
271 yRot = math.rad(yRot)
272 zRot = math.rad(zRot)
273
274 local filename = getXMLString(xmlFile, key.."#filename");
275 if filename == nil then
276 return false;
277 end;
278 filename = NetworkUtil.convertFromNetworkFilename(filename);
279 local rootNode = g_i3DManager:loadSharedI3DFile(filename);
280 if rootNode == 0 then
281 return false;
282 end;
283
284 local ret = false;
285 local node = getChildAt(rootNode, 0);
286 if node ~= nil and node ~= 0 then
287 setTranslation(node, x,y,z);
288 setRotation(node, xRot,yRot,zRot);
289 link(getRootNode(), node);
290 ret = true;
291 end;
292 delete(rootNode);
293 if not ret then
294 return false;
295 end;
296
297 self:setOwnerFarmId(Utils.getNoNil(getXMLInt(xmlFile, key .. "#farmId"), AccessHandler.EVERYONE), true)
298
299 self:loadFromMemory(node, filename);
300
301
302 local fillLevel = getXMLFloat(xmlFile, key.."#fillLevel");
303 if fillLevel ~= nil then
304 self.fillLevel = fillLevel;
305 end;
306
307 local attributes = {}
308 self:loadExtraAttributesFromXMLFile(attributes, xmlFile, key, resetVehicles)
309 self:applyExtraAttributes(attributes)
310
311 return true;
312end

mount

Description
Mount bale to object
Definition
mount(table object, integer node, float x, float y, float z, float rx, float ry, float rz)
Arguments
tableobjecttarget object
integernodetarget node id
floatxx position
floatyz position
floatzz position
floatrxrx rotation
floatryry rotation
floatrzrz rotation
Code
136function Bale:mount(object, node, x,y,z, rx,ry,rz)
137 g_currentMission:removeItemToSave(self);
138 Bale:superClass().mount(self, object, node, x,y,z, rx,ry,rz);
139end

new

Description
Creating bale object
Definition
new(boolean isServer, boolean isClient, table customMt)
Arguments
booleanisServeris server
booleanisClientis client
tablecustomMtcustomMt
Return Values
tableinstanceInstance of object
Code
17function Bale:new(isServer, isClient, customMt)
18 local mt = customMt;
19 if mt == nil then
20 mt = Bale_mt;
21 end;
22
23 local self = MountableObject:new(isServer, isClient, mt);
24
25 self.forcedClipDistance = 150;
26 registerObjectClassName(self, "Bale");
27
28 self.fillType = FillType.STRAW;
29 self.fillLevel = 0;
30 self.wrappingState = 0;
31 self.baleValueScale = 1;
32 self.canBeSold = true
33
34 g_currentMission:addLimitedObject(FSBaseMission.LIMITED_OBJECT_TYPE_BALE, self);
35
36 return self;
37end

readStream

Description
Called on client side on join
Definition
readStream(integer streamId, table connection)
Arguments
integerstreamIdstream ID
tableconnectionconnection
Code
83function Bale:readStream(streamId, connection)
84 local i3dFilename = NetworkUtil.convertFromNetworkFilename(streamReadString(streamId));
85 if self.nodeId == 0 then
86 self:createNode(i3dFilename);
87 end;
88 self.fillLevel = streamReadFloat32(streamId);
89 Bale:superClass().readStream(self, streamId, connection);
90 g_currentMission:addItemToSave(self);
91
92 self.baleValueScale = streamReadFloat32(streamId);
93 if self.supportsWrapping then
94 self:setWrappingState(streamReadUInt8(streamId)/255);
95 setShaderParameter(self.meshNode, "wrappingState", self.wrappingState, 0, 0, 0, false);
96
97 local r = streamReadFloat32(streamId);
98 local g = streamReadFloat32(streamId);
99 local b = streamReadFloat32(streamId);
100 local a = streamReadFloat32(streamId);
101 self:setColor(r, g, b, a);
102 end;
103end

readUpdateStream

Description
Called on client side on update
Definition
readUpdateStream(integer streamId, integer timestamp, table connection)
Arguments
integerstreamIdstream ID
integertimestamptimestamp
tableconnectionconnection
Code
56function Bale:readUpdateStream(streamId, timestamp, connection)
57 if connection:getIsServer() then
58 if self.supportsWrapping then
59 self:setWrappingState(streamReadUInt8(streamId)/255);
60 end
61 end
62 Bale:superClass().readUpdateStream(self, streamId, timestamp, connection);
63end

saveToXMLFile

Description
Definition
saveToXMLFile()
Code
341function Bale:saveToXMLFile(xmlFile, key)
342 local x,y,z = getTranslation(self.nodeId)
343 local xRot,yRot,zRot = getRotation(self.nodeId)
344
345 setXMLString(xmlFile, key.."#filename", HTMLUtil.encodeToHTML(NetworkUtil.convertToNetworkFilename(self.i3dFilename)))
346 setXMLString(xmlFile, key.."#position", string.format("%.4f %.4f %.4f", x, y, z))
347 setXMLString(xmlFile, key.."#rotation", string.format("%.4f %.4f %.4f", math.deg(xRot), math.deg(yRot), math.deg(zRot)))
348 setXMLFloat(xmlFile, key.."#valueScale", self.baleValueScale)
349 setXMLFloat(xmlFile, key.."#fillLevel", self.fillLevel)
350 setXMLInt(xmlFile, key.."#farmId", self:getOwnerFarmId())
351
352 if self.supportsWrapping then
353 setXMLFloat(xmlFile, key.."#wrappingState", self.wrappingState)
354 setXMLString(xmlFile, key.."#wrappingColor", string.format("%f %f %f %f", self.wrappingColor[1], self.wrappingColor[2], self.wrappingColor[3], self.wrappingColor[4]))
355 end
356end

setCanBeSold

Description
Set if bale can be sold
Definition
setCanBeSold(boolean canBeSold)
Arguments
booleancanBeSoldbale can be sold
Code
389function Bale:setCanBeSold(canBeSold)
390 self.canBeSold = canBeSold
391end

setColor

Description
Set color of bale
Definition
setColor(float r, float g, float b, float a)
Arguments
floatrred channel value
floatggreen channel value
floatbblue channel value
floataalpha channel value
Code
419function Bale:setColor(r, g, b, a)
420 r, g, b, a = Utils.getNoNil(r, 1), Utils.getNoNil(g, 1), Utils.getNoNil(b, 1), Utils.getNoNil(a, 1);
421 self.wrappingColor = {r, g, b, a};
422
423 if getHasShaderParameter(self.meshNode, "colorScale") then
424 setShaderParameter(self.meshNode, "colorScale", r, g, b, a, false);
425 end;
426end

setFillLevel

Description
Set fill level of bale
Definition
setFillLevel(integer fillLevel)
Arguments
integerfillLevelfill level
Code
382function Bale:setFillLevel(fillLevel)
383 self.fillLevel = fillLevel;
384end

setNodeId

Description
Set node id
Definition
setNodeId(integer nodeId)
Arguments
integernodeIdnode Id
Code
154function Bale:setNodeId(nodeId)
155 Bale:superClass().setNodeId(self, nodeId);
156
157 local isRoundbale = Utils.getNoNil(getUserAttribute(nodeId, "isRoundbale"), false);
158 local defaultFillLevel = 4000;
159 if getUserAttribute(nodeId, "baleValue") ~= nil then
160 print("Warning: bale 'baleValue' is not supported anymore. Use 'baleValueScale' instead and adjust the creating vehicles.");
161 end
162 local meshIndex = Utils.getNoNil(getUserAttribute(nodeId, "baleMeshIndex"), "1|0");
163 self.meshNode = I3DUtil.indexToObject(nodeId, meshIndex);
164
165 self.meshNodes = {self.meshNode}
166
167 self.supportsWrapping = Utils.getNoNil(getUserAttribute(nodeId, "supportsWrapping"), false);
168 if self.supportsWrapping then
169 self.wrappingColor = {1,1,1,1};
170 end
171
172 self.fillLevel = defaultFillLevel
173 self.baleValueScale = Utils.getNoNil(tonumber(getUserAttribute(nodeId, "baleValueScale")), 1);
174
175 self.fillType = FillType.STRAW;
176 local fillTypeStr = getUserAttribute(nodeId, "fillType");
177 if fillTypeStr ~= nil then
178 local fillTypeIndex = g_fillTypeManager:getFillTypeIndexByName(fillTypeStr)
179 if fillTypeIndex ~= nil then
180 self.fillType = fillTypeIndex;
181 end
182 elseif Utils.getNoNil(getUserAttribute(nodeId, "isHaybale"), false) then
183 self.fillType = FillType.DRYGRASS_WINDROW;
184 end
185
186 local baleWidth = tonumber(getUserAttribute(nodeId, "baleWidth"));
187 local baleHeight = tonumber(getUserAttribute(nodeId, "baleHeight"));
188 local baleLength = tonumber(getUserAttribute(nodeId, "baleLength"));
189 local baleDiameter = tonumber(getUserAttribute(nodeId, "baleDiameter"));
190 if baleDiameter ~= nil and baleWidth ~= nil then
191 self.baleDiameter = baleDiameter;
192 self.baleWidth = baleWidth;
193 elseif baleHeight ~= nil and baleWidth ~= nil and baleLength ~= nil then
194 self.baleHeight = baleHeight;
195 self.baleWidth = baleWidth;
196 self.baleLength = baleLength;
197 else
198 local isRoundbale = Utils.getNoNil(getUserAttribute(nodeId, "isRoundbale"), false);
199 if isRoundbale then
200 self.baleDiameter = 1.8;
201 self.baleWidth = 1.2;
202 else
203 self.baleHeight = 0.8;
204 self.baleWidth = 1.2;
205 self.baleLength = 2.4;
206 end
207 end
208end

setWrappingState

Description
Set wrapping state of bale
Definition
setWrappingState(boolean wrappingState)
Arguments
booleanwrappingStatenew wrapping state
Code
403function Bale:setWrappingState(wrappingState)
404 if self.supportsWrapping then
405 if self.wrappingState ~= wrappingState then
406 self:raiseDirtyFlags(self.physicsObjectDirtyFlag);
407 end
408 self.wrappingState = wrappingState;
409 setShaderParameter(self.meshNode, "wrappingState", self.wrappingState, 0, 0, 0, false);
410 end
411end

unmount

Description
Unmount bale
Definition
unmount()
Code
143function Bale:unmount()
144 if Bale:superClass().unmount(self) then
145 g_currentMission:addItemToSave(self);
146 return true;
147 end
148 return false;
149end

writeStream

Description
Called on server side on join
Definition
writeStream(integer streamId, table connection)
Arguments
integerstreamIdstream ID
tableconnectionconnection
Code
109function Bale:writeStream(streamId, connection)
110 streamWriteString(streamId, NetworkUtil.convertToNetworkFilename(self.i3dFilename));
111 streamWriteFloat32(streamId, self.fillLevel);
112 Bale:superClass().writeStream(self, streamId, connection);
113
114 streamWriteFloat32(streamId, self.baleValueScale);
115
116 if self.supportsWrapping then
117 streamWriteUInt8(streamId, MathUtil.clamp(self.wrappingState*255, 0, 255));
118
119 streamWriteFloat32(streamId, self.wrappingColor[1]);
120 streamWriteFloat32(streamId, self.wrappingColor[2]);
121 streamWriteFloat32(streamId, self.wrappingColor[3]);
122 streamWriteFloat32(streamId, self.wrappingColor[4]);
123 end
124end

writeUpdateStream

Description
Called on server side on update
Definition
writeUpdateStream(integer streamId, table connection, integer dirtyMask)
Arguments
integerstreamIdstream ID
tableconnectionconnection
integerdirtyMaskdirty mask
Code
70function Bale:writeUpdateStream(streamId, connection, dirtyMask)
71 if not connection:getIsServer() then
72 if self.supportsWrapping then
73 streamWriteUInt8(streamId, MathUtil.clamp(self.wrappingState*255, 0, 255));
74 end
75 end
76 Bale:superClass().writeUpdateStream(self, streamId, connection, dirtyMask);
77end