Sprache Deutsch Language English

Script Dokumentation LS 2015 - FillablePallet (Patch 1.3)

Script Dokumentation Übersicht

scripts/objects/FillablePallet.lua

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
3FillablePallet = {};
4FillablePallet_mt = Class(FillablePallet, MountableObject);
5
6InitStaticObjectClass(FillablePallet, "FillablePallet", ObjectIds.OBJECT_FILLABLE_PALLET);
7
8function FillablePallet:new(isServer, isClient, customMt)
9 local mt = customMt;
10 if mt == nil then
11 mt = FillablePallet_mt;
12 end;
13
14 local self = MountableObject:new(isServer, isClient, mt);
15
16 self.forcedClipDistance = 80;
17 registerObjectClassName(self, "FillablePallet");
18
19 self.fillType = Fillable.FILLTYPE_WHEAT_WINDROW;
20 self.fillLevel = 0;
21 self.capacity = 2000;
22 self.fillablePalletValueScale = 0;
23 self.isReferenced = false;
24 self.synchronizeFillLevel = true;
25
26 self.fillablePalletDirtyFlag = self:getNextDirtyFlag();
27
28 g_currentMission:addLimitedObject(self);
29
30 return self;
31end;
32
33function FillablePallet:delete()
34 if self.fillTrigger ~= nil then
35 self.fillTrigger:delete();
36 self.fillTrigger = nil;
37 end;
38 if self.i3dFilename ~= nil then
39 Utils.releaseSharedI3DFile(self.i3dFilename, nil, true);
40 end
41 g_currentMission:removeLimitedObject(self);
42 unregisterObjectClassName(self);
43 g_currentMission:removeItemToSave(self);
44 FillablePallet:superClass().delete(self);
45end;
46
47function FillablePallet:getAllowsAutoDelete()
48 return not self.isReferenced and FillablePallet:superClass().getAllowsAutoDelete(self);
49end
50
51function FillablePallet:readStream(streamId, connection)
52 local i3dFilename = Utils.convertFromNetworkFilename(streamReadString(streamId));
53 if self.nodeId == 0 then
54 self:createNode(i3dFilename);
55 end;
56 local fillLevel = nil;
57 if self.synchronizeFillLevel then
58 fillLevel = streamReadUIntN(streamId, 15)/32767*self.capacity;
59 end;
60
61 FillablePallet:superClass().readStream(self, streamId, connection);
62 g_currentMission:addItemToSave(self);
63
64 if self.synchronizeFillLevel then
65 self:setFillLevel(fillLevel, false);
66 end;
67end;
68
69function FillablePallet:writeStream(streamId, connection)
70 streamWriteString(streamId, Utils.convertToNetworkFilename(self.i3dFilename));
71
72 if self.synchronizeFillLevel then
73 local percent = 0;
74 if self.capacity ~= 0 then
75 percent = Utils.clamp(self.fillLevel / self.capacity, 0, 1);
76 end
77 streamWriteUIntN(streamId, math.floor(percent*32767), 15);
78 end;
79 FillablePallet:superClass().writeStream(self, streamId, connection);
80end;
81
82function FillablePallet:readUpdateStream(streamId, timestamp, connection)
83 if connection:getIsServer() then
84 if self.synchronizeFillLevel then
85 if streamReadBool(streamId) then
86 local fillLevel = streamReadUIntN(streamId, 15)/32767*self.capacity;
87 self:setFillLevel(fillLevel, false);
88 end
89 end;
90 end
91 FillablePallet:superClass().readUpdateStream(self, streamId, timestamp, connection);
92end;
93
94function FillablePallet:writeUpdateStream(streamId, connection, dirtyMask)
95 if not connection:getIsServer() then
96 if self.synchronizeFillLevel then
97 if streamWriteBool(streamId, bitAND(dirtyMask, self.fillablePalletDirtyFlag) ~= 0) then
98 local percent = 0;
99 if self.capacity ~= 0 then
100 percent = Utils.clamp(self.fillLevel / self.capacity, 0, 1);
101 end
102 streamWriteUIntN(streamId, math.floor(percent*32767), 15);
103 end
104 end;
105 end
106 FillablePallet:superClass().writeUpdateStream(self, streamId, connection, dirtyMask);
107end;
108
109function FillablePallet:mount(object, node, x,y,z, rx,ry,rz)
110 g_currentMission:removeItemToSave(self);
111 FillablePallet:superClass().mount(self, object, node, x,y,z, rx,ry,rz);
112end;
113
114function FillablePallet:unmount()
115 if FillablePallet:superClass().unmount(self) then
116 g_currentMission:addItemToSave(self);
117 return true;
118 end
119 return false;
120end;
121
122function FillablePallet:setNodeId(nodeId)
123 FillablePallet:superClass().setNodeId(self, nodeId);
124
125 self.capacity = Utils.getNoNil(tonumber(getUserAttribute(nodeId, "capacity")), self.capacity);
126 self.fillablePalletValueScale = Utils.getNoNil(tonumber(getUserAttribute(nodeId, "fillablePalletValueScale")), 1);
127
128 self.fillType = Fillable.FILLTYPE_WOOL;
129 local fillTypeStr = getUserAttribute(nodeId, "fillType");
130 if fillTypeStr ~= nil then
131 local fillTypeInt = Fillable.fillTypeNameToInt[fillTypeStr];
132 if fillTypeInt ~= nil then
133 self.fillType = fillTypeInt;
134 end
135 end
136
137 self.visibilityNodes = Utils.indexToObject(nodeId, getUserAttribute(nodeId, "visibilityNodesIndex"));
138 self.visibilityNodesAdditive = Utils.getNoNil(getUserAttribute(nodeId, "visibilityNodesAdditive"), false);
139
140 self.iconNode = Utils.indexToObject(nodeId, getUserAttribute(nodeId, "iconNode"));
141 self.iconColor = Utils.getVectorNFromString(Utils.getNoNil(getUserAttribute(nodeId, "iconColor"), "0 0 0 1"), 4);
142
143 self.movingPlane = Utils.indexToObject(nodeId, getUserAttribute(nodeId, "movingPlane"));
144 self.movingPlaneMinY = Utils.getNoNil(tonumber(getUserAttribute(nodeId, "movingPlaneMinY")), 0);
145 self.movingPlaneMaxY = Utils.getNoNil(tonumber(getUserAttribute(nodeId, "movingPlaneMaxY")), 1);
146
147 self.fillLevel = -1;
148 self:setFillLevel(Utils.getNoNil(tonumber(getUserAttribute(nodeId, "startFillLevel")), 0), false);
149
150 self.fillTriggerNode = Utils.indexToObject(nodeId, getUserAttribute(nodeId, "fillTrigger"));
151 if self.fillTriggerNode ~= nil then
152 local fillClass = getUserAttribute(nodeId, "fillTriggerClass");
153 local fillTriggerClass = nil;
154 if fillClass ~= nil then
155 fillTriggerClass = getClassObject(fillClass);
156 end;
157
158 if fillTriggerClass ~= nil then
159 self.fillTrigger = fillTriggerClass:new();
160 if not self.fillTrigger:load(self.fillTriggerNode, self.fillType, self) then
161 self.fillTrigger:delete();
162 self.fillTrigger = nil;
163 end
164 else
165 print("Warning: fillTriggerClass '"..tostring(fillClass).."' not found!");
166 end;
167 end;
168end;
169
170function FillablePallet:createNode(i3dFilename)
171 self.i3dFilename = i3dFilename;
172 self.customEnvironment, self.baseDirectory = Utils.getModNameAndBaseDirectory(i3dFilename);
173 local fillablePalletRoot = Utils.loadSharedI3DFile(i3dFilename);
174
175 local FillablePalletId = getChildAt(fillablePalletRoot, 0);
176 link(getRootNode(), FillablePalletId);
177 delete(fillablePalletRoot);
178
179 self:setNodeId(FillablePalletId);
180end;
181
182function FillablePallet:load(i3dFilename, x,y,z, rx,ry,rz)
183 self.i3dFilename = i3dFilename;
184 self.customEnvironment, self.baseDirectory = Utils.getModNameAndBaseDirectory(i3dFilename);
185 self:createNode(i3dFilename);
186 setTranslation(self.nodeId, x, y, z);
187 setRotation(self.nodeId, rx, ry, rz);
188 g_currentMission:addItemToSave(self);
189
190 return true;
191end;
192
193function FillablePallet:loadFromMemory(nodeId, i3dFilename)
194 self.i3dFilename = i3dFilename;
195 self.customEnvironment, self.baseDirectory = Utils.getModNameAndBaseDirectory(i3dFilename);
196 self:setNodeId(nodeId);
197end;
198
199function FillablePallet:loadFromAttributesAndNodes(xmlFile, key, resetVehicles)
200
201 local x,y,z = Utils.getVectorFromString(getXMLString(xmlFile, key.."#position"));
202 local xRot,yRot,zRot = Utils.getVectorFromString(getXMLString(xmlFile, key.."#rotation"));
203 if x == nil or y == nil or z == nil or xRot == nil or yRot == nil or zRot == nil then
204 return false;
205 end;
206 local filename = getXMLString(xmlFile, key.."#filename");
207 if filename == nil then
208 return false;
209 end;
210 filename = Utils.convertFromNetworkFilename(filename);
211 local rootNode = Utils.loadSharedI3DFile(filename);
212 if rootNode == 0 then
213 return false;
214 end;
215
216 local ret = false;
217 local node = getChildAt(rootNode, 0);
218 if node ~= nil and node ~= 0 then
219 setTranslation(node, x,y,z);
220 setRotation(node, xRot,yRot,zRot);
221 link(getRootNode(), node);
222 ret = true;
223 end;
224 delete(rootNode);
225 if not ret then
226 return false;
227 end;
228
229 self:loadFromMemory(node, filename);
230
231 local fillLevel = getXMLFloat(xmlFile, key.."#fillLevel");
232 if fillLevel ~= nil then
233 self.fillLevel = -1;
234 self:setFillLevel(fillLevel, false);
235 end
236 return true;
237end;
238
239function FillablePallet:getSaveAttributesAndNodes(nodeIdent)
240
241 local x,y,z = getTranslation(self.nodeId);
242 local xRot,yRot,zRot = getRotation(self.nodeId);
243 local attributes = 'filename="'.. Utils.encodeToHTML(Utils.convertToNetworkFilename(self.i3dFilename))..'" position="'..x..' '..y..' '..z..'" rotation="'..xRot..' '..yRot..' '..zRot..'" fillLevel="'..self.fillLevel..'"';
244 local nodes = "";
245 return attributes, nodes;
246end;
247
248function FillablePallet:getValue()
249
250 local pricePerLiter = 0;
251 local desc = Fillable.fillTypeIndexToDesc[self.fillType];
252 if desc ~= nil then
253 pricePerLiter = desc.pricePerLiter;
254 end
255 return self.fillLevel * pricePerLiter * self.fillablePalletValueScale;
256end;
257
258function FillablePallet:getFillType()
259 return self.fillType;
260end;
261
262function FillablePallet:setFillLevel(fillLevel, setDirty)
263 fillLevel = Utils.clamp(fillLevel, 0, self.capacity);
264 if self.fillLevel ~= fillLevel then
265 self.fillLevel = fillLevel;
266 if self.isServer and (setDirty == nil or setDirty) then
267 self:raiseDirtyFlags(self.fillablePalletDirtyFlag);
268 end
269 if self.visibilityNodes ~= nil then
270 local numVisibilityNodes = getNumOfChildren(self.visibilityNodes);
271 local numVisible = math.ceil(numVisibilityNodes*self.fillLevel/self.capacity);
272 if self.visibilityNodesAdditive then
273 for i=1,numVisibilityNodes do
274 setVisibility(getChildAt(self.visibilityNodes, i-1), i <= numVisible);
275 end
276 else
277 for i=1,numVisibilityNodes do
278 setVisibility(getChildAt(self.visibilityNodes, i-1), i == numVisible);
279 end
280 end
281 end
282 if self.movingPlane ~= nil then
283 local x,y,z = getTranslation(self.movingPlane);
284 y = Utils.lerp(self.movingPlaneMinY, self.movingPlaneMaxY, self:getFillLevel()/self:getCapacity());
285 setTranslation(self.movingPlane, x,y,z);
286 end;
287
288 if self.fillLevel > 0 and self.iconNode ~= nil then
289 local desc = Fillable.fillTypeIndexToDesc[self.fillType];
290 if desc ~= nil then
291 if desc.iconMaterial ~= nil then
292 setMaterial(self.iconNode, desc.iconMaterial, 0);
293 setShaderParameter(self.iconNode, "iconColor", self.iconColor[1], self.iconColor[2], self.iconColor[3], self.iconColor[4], false);
294 end;
295 setVisibility(self.iconNode, desc.iconMaterial ~= nil);
296 end;
297 end;
298 end
299end;
300
301function FillablePallet:getFillLevel()
302 return self.fillLevel;
303end;
304
305function FillablePallet:getCapacity()
306 return self.capacity;
307end;
Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de
Script Dokumentation Übersicht