LUADOC - Farming Simulator 17

Printable Version

Bale

Description
Class for bales
Parent
MountableObject
Functions

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
18function Bale:new(isServer, isClient, customMt)
19 local mt = customMt;
20 if mt == nil then
21 mt = Bale_mt;
22 end;
23
24 local self = MountableObject:new(isServer, isClient, mt);
25
26 self.forcedClipDistance = 150;
27 registerObjectClassName(self, "Bale");
28
29 self.fillType = FillUtil.FILLTYPE_STRAW;
30 self.fillLevel = 0;
31 self.wrappingState = 0;
32 self.baleValueScale = 1;
33 self.canBeSold = true
34
35 g_currentMission:addLimitedObject(FSBaseMission.LIMITED_OBJECT_TYPE_BALE, self);
36
37 return self;
38end;

delete

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

readUpdateStream

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

writeUpdateStream

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

readStream

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

writeStream

Description
Called on server side on join
Definition
writeStream(integer streamId, table connection)
Arguments
integerstreamIdstream ID
tableconnectionconnection
Code
104function Bale:writeStream(streamId, connection)
105 streamWriteString(streamId, Utils.convertToNetworkFilename(self.i3dFilename));
106 streamWriteFloat32(streamId, self.fillLevel);
107 Bale:superClass().writeStream(self, streamId, connection);
108
109 streamWriteFloat32(streamId, self.baleValueScale);
110
111 if self.supportsWrapping then
112 streamWriteUInt8(streamId, Utils.clamp(self.wrappingState*255, 0, 255));
113 end
114end;

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
126function Bale:mount(object, node, x,y,z, rx,ry,rz)
127 g_currentMission:removeItemToSave(self);
128 Bale:superClass().mount(self, object, node, x,y,z, rx,ry,rz);
129end;

unmount

Description
Unmount bale
Definition
unmount()
Code
133function Bale:unmount()
134 if Bale:superClass().unmount(self) then
135 g_currentMission:addItemToSave(self);
136 return true;
137 end
138 return false;
139end;

setNodeId

Description
Set node id
Definition
setNodeId(integer nodeId)
Arguments
integernodeIdnode Id
Code
144function Bale:setNodeId(nodeId)
145 Bale:superClass().setNodeId(self, nodeId);
146
147 local isRoundbale = Utils.getNoNil(getUserAttribute(nodeId, "isRoundbale"), false);
148 local defaultFillLevel = 2100*2;
149 if isRoundbale then
150 defaultFillLevel = 2000*2;
151 end
152 if getUserAttribute(nodeId, "baleValue") ~= nil then
153 print("Warning: bale 'baleValue' is not supported anymore. Use 'baleValueScale' instead and adjust the creating vehicles.");
154 end
155 local meshIndex = Utils.getNoNil(getUserAttribute(nodeId, "baleMeshIndex"), "1|0");
156 self.meshNode = Utils.indexToObject(nodeId, meshIndex);
157
158 self.meshNodes = {self.meshNode}
159
160 self.supportsWrapping = Utils.getNoNil(getUserAttribute(nodeId, "supportsWrapping"), false);
161 self.fillLevel = defaultFillLevel
162 self.baleValueScale = Utils.getNoNil(tonumber(getUserAttribute(nodeId, "baleValueScale")), 1);
163
164 self.fillType = FillUtil.FILLTYPE_STRAW;
165 local fillTypeStr = getUserAttribute(nodeId, "fillType");
166 if fillTypeStr ~= nil then
167 local fillTypeInt = FillUtil.fillTypeNameToInt[fillTypeStr];
168 if fillTypeInt ~= nil then
169 self.fillType = fillTypeInt;
170 end
171 elseif Utils.getNoNil(getUserAttribute(nodeId, "isHaybale"), false) then
172 self.fillType = FillUtil.FILLTYPE_DRYGRASS_WINDROW;
173 end
174
175 local baleWidth = tonumber(getUserAttribute(nodeId, "baleWidth"));
176 local baleHeight = tonumber(getUserAttribute(nodeId, "baleHeight"));
177 local baleLength = tonumber(getUserAttribute(nodeId, "baleLength"));
178 local baleDiameter = tonumber(getUserAttribute(nodeId, "baleDiameter"));
179 if baleDiameter ~= nil and baleWidth ~= nil then
180 self.baleDiameter = baleDiameter;
181 self.baleWidth = baleWidth;
182 elseif baleHeight ~= nil and baleWidth ~= nil and baleLength ~= nil then
183 self.baleHeight = baleHeight;
184 self.baleWidth = baleWidth;
185 self.baleLength = baleLength;
186 else
187 local isRoundbale = Utils.getNoNil(getUserAttribute(nodeId, "isRoundbale"), false);
188 if isRoundbale then
189 self.baleDiameter = 1.8;
190 self.baleWidth = 1.2;
191 else
192 self.baleHeight = 0.8;
193 self.baleWidth = 1.2;
194 self.baleLength = 2.4;
195 end
196 end
197end;

createNode

Description
Load node from i3d file
Definition
createNode(string i3dFilename)
Arguments
stringi3dFilenamei3d file name
Code
202function Bale:createNode(i3dFilename)
203 self.i3dFilename = i3dFilename;
204 self.customEnvironment, self.baseDirectory = Utils.getModNameAndBaseDirectory(i3dFilename);
205 local baleRoot = Utils.loadSharedI3DFile(i3dFilename);
206
207 local baleId = getChildAt(baleRoot, 0);
208 link(getRootNode(), baleId);
209 delete(baleRoot);
210
211 self:setNodeId(baleId);
212end;

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
224function Bale:load(i3dFilename, x,y,z, rx,ry,rz, fillLevel)
225 self.i3dFilename = i3dFilename;
226 self.customEnvironment, self.baseDirectory = Utils.getModNameAndBaseDirectory(i3dFilename);
227 self:createNode(i3dFilename);
228 setTranslation(self.nodeId, x, y, z);
229 setRotation(self.nodeId, rx, ry, rz);
230 if fillLevel ~= nil then
231 self.fillLevel = fillLevel;
232 end
233 g_currentMission:addItemToSave(self);
234end;

loadFromMemory

Description
Setting node id and i3d file name
Definition
loadFromMemory(integer nodeId, string i3dFilename)
Arguments
integernodeIdnode id
stringi3dFilenamei3d file name
Code
240function Bale:loadFromMemory(nodeId, i3dFilename)
241 self.i3dFilename = i3dFilename;
242 self.customEnvironment, self.baseDirectory = Utils.getModNameAndBaseDirectory(i3dFilename);
243 self:setNodeId(nodeId);
244end;

loadFromAttributesAndNodes

Description
Loading from attributes and nodes
Definition
loadFromAttributesAndNodes(integer xmlFile, string key, boolean resetVehicles)
Arguments
integerxmlFileid of xml object
stringkeykey
booleanresetVehiclesreset vehicles
Return Values
booleansuccesssuccess
Code
252function Bale:loadFromAttributesAndNodes(xmlFile, key, resetVehicles)
253
254 local x,y,z = Utils.getVectorFromString(getXMLString(xmlFile, key.."#position"));
255 local xRot,yRot,zRot = Utils.getVectorFromString(getXMLString(xmlFile, key.."#rotation"));
256 if x == nil or y == nil or z == nil or xRot == nil or yRot == nil or zRot == nil then
257 return false;
258 end;
259 local filename = getXMLString(xmlFile, key.."#filename");
260 if filename == nil then
261 return false;
262 end;
263 filename = Utils.convertFromNetworkFilename(filename);
264 local rootNode = Utils.loadSharedI3DFile(filename);
265 if rootNode == 0 then
266 return false;
267 end;
268
269 local ret = false;
270 local node = getChildAt(rootNode, 0);
271 if node ~= nil and node ~= 0 then
272 setTranslation(node, x,y,z);
273 setRotation(node, xRot,yRot,zRot);
274 link(getRootNode(), node);
275 ret = true;
276 end;
277 delete(rootNode);
278 if not ret then
279 return false;
280 end;
281
282 self:loadFromMemory(node, filename);
283
284
285 local fillLevel = getXMLFloat(xmlFile, key.."#fillLevel");
286 if fillLevel ~= nil then
287 self.fillLevel = fillLevel;
288 end;
289
290 if self.supportsWrapping then
291 self.wrappingState = Utils.getNoNil(getXMLFloat(xmlFile, key.."#wrappingState"),0);
292 setShaderParameter(self.meshNode, "wrappingState", self.wrappingState, 0, 0, 0, false);
293 end;
294
295 local baleValueScale = Utils.getNoNil(tonumber(getXMLString(xmlFile, key.."#baleValueScale")), 1);
296 if baleValueScale ~= nil then
297 self.baleValueScale = baleValueScale;
298 end
299
300 return true;
301end;

getSaveAttributesAndNodes

Description
Get save attributes and nodes
Definition
getSaveAttributesAndNodes(string nodeIdent)
Arguments
stringnodeIdentnode ident
Return Values
stringattributesattributes
stringnodesnodes
Code
308function Bale:getSaveAttributesAndNodes(nodeIdent)
309
310 local x,y,z = getTranslation(self.nodeId);
311 local xRot,yRot,zRot = getRotation(self.nodeId);
312 local wrapping = "";
313 if self.supportsWrapping then
314 wrapping = ' wrappingState="'..tostring(self.wrappingState)..'"';
315 end;
316 local attributes = 'filename="'.. Utils.encodeToHTML(Utils.convertToNetworkFilename(self.i3dFilename))..'" position="'..x..' '..y..' '..z..'" rotation="'..xRot..' '..yRot..' '..zRot..'" baleValueScale="'..self.baleValueScale..'" fillLevel="'..self.fillLevel..'"'..wrapping;
317 local nodes = "";
318 return attributes, nodes;
319end;

getValue

Description
Get price value of bale
Definition
getValue()
Code
323function Bale:getValue()
324 local pricePerLiter = g_currentMission.economyManager:getPricePerLiter(self.fillType);
325 return self.fillLevel * pricePerLiter * self.baleValueScale;
326end;

getFillType

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

getFillLevel

Description
Get fill level of bale
Definition
getFillLevel()
Return Values
integerfillLevelcurrent fill level
Code
338function Bale:getFillLevel()
339 return self.fillLevel;
340end;

setFillLevel

Description
Set fill level of bale
Definition
setFillLevel(integer fillLevel)
Arguments
integerfillLevelfill level
Code
345function Bale:setFillLevel(fillLevel)
346 self.fillLevel = fillLevel;
347end;

setWrappingState

Description
Set wrapping state of bale
Definition
setWrappingState(boolean wrappingState)
Arguments
booleanwrappingStatenew wrapping state
Code
360function Bale:setWrappingState(wrappingState)
361 if self.supportsWrapping then
362 if self.wrappingState ~= wrappingState then
363 self:raiseDirtyFlags(self.physicsObjectDirtyFlag);
364 end
365 self.wrappingState = wrappingState;
366 setShaderParameter(self.meshNode, "wrappingState", self.wrappingState, 0, 0, 0, false);
367 end
368end

getMeshNodes

Description
Set wrapping state of bale
Definition
getMeshNodes()
Return Values
tablemeshNodesmesh nodes
Code
373function Bale:getMeshNodes()
374 return self.meshNodes
375end;