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 | Bale= {}; |
4 | Bale_mt = Class(Bale, MountableObject); |
5 | |
6 | InitStaticObjectClass(Bale, "Bale", ObjectIds.OBJECT_BALE); |
7 | |
8 | function Bale:new(isServer, isClient, customMt) |
9 | local mt = customMt; |
10 | if mt == nil then |
11 | mt = Bale_mt; |
12 | end; |
13 | |
14 | local self = MountableObject:new(isServer, isClient, mt); |
15 | |
16 | self.forcedClipDistance = 80; |
17 | registerObjectClassName(self, "Bale"); |
18 | |
19 | self.fillType = Fillable.FILLTYPE_WHEAT_WINDROW; |
20 | self.fillLevel = 0; |
21 | self.wrappingState = 0; |
22 | self.baleValueScale = 1; |
23 | |
24 | g_currentMission:addLimitedObject(self); |
25 | |
26 | return self; |
27 | end; |
28 | |
29 | function Bale:delete() |
30 | if self.i3dFilename ~= nil then |
31 | Utils.releaseSharedI3DFile(self.i3dFilename, nil, true); |
32 | end |
33 | g_currentMission:removeLimitedObject(self); |
34 | unregisterObjectClassName(self); |
35 | g_currentMission:removeItemToSave(self); |
36 | Bale:superClass().delete(self); |
37 | end; |
38 | |
39 | function Bale:readUpdateStream(streamId, timestamp, connection) |
40 | if connection:getIsServer() then |
41 | if self.supportsWrapping then |
42 | self:setWrappingState(streamReadUInt8(streamId)/255); |
43 | end |
44 | end |
45 | Bale:superClass().readUpdateStream(self, streamId, timestamp, connection); |
46 | end; |
47 | |
48 | function Bale:writeUpdateStream(streamId, connection, dirtyMask) |
49 | if not connection:getIsServer() then |
50 | if self.supportsWrapping then |
51 | streamWriteUInt8(streamId, Utils.clamp(self.wrappingState*255, 0, 255)); |
52 | end |
53 | end |
54 | Bale:superClass().writeUpdateStream(self, streamId, connection, dirtyMask); |
55 | end; |
56 | |
57 | function Bale:readStream(streamId, connection) |
58 | local i3dFilename = Utils.convertFromNetworkFilename(streamReadString(streamId)); |
59 | if self.nodeId == 0 then |
60 | self:createNode(i3dFilename); |
61 | end; |
62 | self.fillLevel = streamReadFloat32(streamId); |
63 | Bale:superClass().readStream(self, streamId, connection); |
64 | g_currentMission:addItemToSave(self); |
65 | |
66 | self.baleValueScale = streamReadFloat32(streamId); |
67 | if self.supportsWrapping then |
68 | self:setWrappingState(streamReadUInt8(streamId)/255); |
69 | setShaderParameter(self.baleMesh, "wrappingState", self.wrappingState, 0, 0, 0, false); |
70 | end |
71 | end; |
72 | |
73 | function Bale:writeStream(streamId, connection) |
74 | streamWriteString(streamId, Utils.convertToNetworkFilename(self.i3dFilename)); |
75 | streamWriteFloat32(streamId, self.fillLevel); |
76 | Bale:superClass().writeStream(self, streamId, connection); |
77 | |
78 | streamWriteFloat32(streamId, self.baleValueScale); |
79 | |
80 | if self.supportsWrapping then |
81 | streamWriteUInt8(streamId, Utils.clamp(self.wrappingState*255, 0, 255)); |
82 | end |
83 | end; |
84 | |
85 | function Bale:mount(object, node, x,y,z, rx,ry,rz) |
86 | g_currentMission:removeItemToSave(self); |
87 | Bale:superClass().mount(self, object, node, x,y,z, rx,ry,rz); |
88 | end; |
89 | |
90 | function Bale:unmount() |
91 | if Bale:superClass().unmount(self) then |
92 | g_currentMission:addItemToSave(self); |
93 | return true; |
94 | end |
95 | return false; |
96 | end; |
97 | |
98 | function Bale:setNodeId(nodeId) |
99 | Bale:superClass().setNodeId(self, nodeId); |
100 | |
101 | local isRoundbale = Utils.getNoNil(getUserAttribute(nodeId, "isRoundbale"), false); |
102 | local defaultFillLevel = 2100*2; |
103 | if isRoundbale then |
104 | defaultFillLevel = 2000*2; |
105 | end |
106 | if getUserAttribute(nodeId, "baleValue") ~= nil then |
107 | print("Warning: bale 'baleValue' is not supported anymore. Use 'baleValueScale' instead and adjust the creating vehicles."); |
108 | end |
109 | local meshIndex = Utils.getNoNil(getUserAttribute(nodeId, "baleMeshIndex"), "0"); |
110 | self.baleMesh = Utils.indexToObject(nodeId, meshIndex); |
111 | self.supportsWrapping = Utils.getNoNil(getUserAttribute(nodeId, "supportsWrapping"), false); |
112 | self.fillLevel = defaultFillLevel |
113 | self.baleValueScale = Utils.getNoNil(tonumber(getUserAttribute(nodeId, "baleValueScale")), 1); |
114 | |
115 | self.fillType = Fillable.FILLTYPE_WHEAT_WINDROW; |
116 | local fillTypeStr = getUserAttribute(nodeId, "fillType"); |
117 | if fillTypeStr ~= nil then |
118 | local fillTypeInt = Fillable.fillTypeNameToInt[fillTypeStr]; |
119 | if fillTypeInt ~= nil then |
120 | self.fillType = fillTypeInt; |
121 | end |
122 | elseif Utils.getNoNil(getUserAttribute(nodeId, "isHaybale"), false) then |
123 | self.fillType = Fillable.FILLTYPE_DRYGRASS_WINDROW; |
124 | end |
125 | |
126 | local baleWidth = tonumber(getUserAttribute(nodeId, "baleWidth")); |
127 | local baleHeight = tonumber(getUserAttribute(nodeId, "baleHeight")); |
128 | local baleLength = tonumber(getUserAttribute(nodeId, "baleLength")); |
129 | local baleDiameter = tonumber(getUserAttribute(nodeId, "baleDiameter")); |
130 | if baleDiameter ~= nil and baleWidth ~= nil then |
131 | self.baleDiameter = baleDiameter; |
132 | self.baleWidth = baleWidth; |
133 | elseif baleHeight ~= nil and baleWidth ~= nil and baleLength ~= nil then |
134 | self.baleHeight = baleHeight; |
135 | self.baleWidth = baleWidth; |
136 | self.baleLength = baleLength; |
137 | else |
138 | local isRoundbale = Utils.getNoNil(getUserAttribute(nodeId, "isRoundbale"), false); |
139 | if isRoundbale then |
140 | self.baleDiameter = 1.8; |
141 | self.baleWidth = 1.2; |
142 | else |
143 | self.baleHeight = 0.8; |
144 | self.baleWidth = 1.2; |
145 | self.baleLength = 2.2; |
146 | end |
147 | end |
148 | end; |
149 | |
150 | function Bale:createNode(i3dFilename) |
151 | self.i3dFilename = i3dFilename; |
152 | self.customEnvironment, self.baseDirectory = Utils.getModNameAndBaseDirectory(i3dFilename); |
153 | local baleRoot = Utils.loadSharedI3DFile(i3dFilename); |
154 | |
155 | local baleId = getChildAt(baleRoot, 0); |
156 | link(getRootNode(), baleId); |
157 | delete(baleRoot); |
158 | |
159 | self:setNodeId(baleId); |
160 | end; |
161 | |
162 | function Bale:load(i3dFilename, x,y,z, rx,ry,rz, fillLevel) |
163 | self.i3dFilename = i3dFilename; |
164 | self.customEnvironment, self.baseDirectory = Utils.getModNameAndBaseDirectory(i3dFilename); |
165 | self:createNode(i3dFilename); |
166 | setTranslation(self.nodeId, x, y, z); |
167 | setRotation(self.nodeId, rx, ry, rz); |
168 | if fillLevel ~= nil then |
169 | self.fillLevel = fillLevel; |
170 | end |
171 | g_currentMission:addItemToSave(self); |
172 | end; |
173 | |
174 | function Bale:loadFromMemory(nodeId, i3dFilename) |
175 | self.i3dFilename = i3dFilename; |
176 | self.customEnvironment, self.baseDirectory = Utils.getModNameAndBaseDirectory(i3dFilename); |
177 | self:setNodeId(nodeId); |
178 | end; |
179 | |
180 | function Bale:loadFromAttributesAndNodes(xmlFile, key, resetVehicles) |
181 | |
182 | local x,y,z = Utils.getVectorFromString(getXMLString(xmlFile, key.."#position")); |
183 | local xRot,yRot,zRot = Utils.getVectorFromString(getXMLString(xmlFile, key.."#rotation")); |
184 | if x == nil or y == nil or z == nil or xRot == nil or yRot == nil or zRot == nil then |
185 | return false; |
186 | end; |
187 | local filename = getXMLString(xmlFile, key.."#filename"); |
188 | if filename == nil then |
189 | return false; |
190 | end; |
191 | filename = Utils.convertFromNetworkFilename(filename); |
192 | local rootNode = Utils.loadSharedI3DFile(filename); |
193 | if rootNode == 0 then |
194 | return false; |
195 | end; |
196 | |
197 | local ret = false; |
198 | local node = getChildAt(rootNode, 0); |
199 | if node ~= nil and node ~= 0 then |
200 | setTranslation(node, x,y,z); |
201 | setRotation(node, xRot,yRot,zRot); |
202 | link(getRootNode(), node); |
203 | ret = true; |
204 | end; |
205 | delete(rootNode); |
206 | if not ret then |
207 | return false; |
208 | end; |
209 | |
210 | self:loadFromMemory(node, filename); |
211 | |
212 | |
213 | local fillLevel = getXMLFloat(xmlFile, key.."#fillLevel"); |
214 | if fillLevel ~= nil then |
215 | self.fillLevel = fillLevel; |
216 | end; |
217 | |
218 | if self.supportsWrapping then |
219 | self.wrappingState = Utils.getNoNil(getXMLFloat(xmlFile, key.."#wrappingState"),0); |
220 | setShaderParameter(self.baleMesh, "wrappingState", self.wrappingState, 0, 0, 0, false); |
221 | end; |
222 | |
223 | local baleValueScale = Utils.getNoNil(tonumber(getXMLString(xmlFile, key.."#baleValueScale")), 1); |
224 | if baleValueScale ~= nil then |
225 | self.baleValueScale = baleValueScale; |
226 | end |
227 | |
228 | return true; |
229 | end; |
230 | |
231 | function Bale:getSaveAttributesAndNodes(nodeIdent) |
232 | |
233 | local x,y,z = getTranslation(self.nodeId); |
234 | local xRot,yRot,zRot = getRotation(self.nodeId); |
235 | local wrapping = ""; |
236 | if self.supportsWrapping then |
237 | wrapping = ' wrappingState="'..tostring(self.wrappingState)..'"'; |
238 | end; |
239 | local attributes = 'filename="'.. Utils.encodeToHTML(Utils.convertToNetworkFilename(self.i3dFilename))..'" position="'..x..' '..y..' '..z..'" rotation="'..xRot..' '..yRot..' '..zRot..'" baleValueScale="'..self.baleValueScale..'" fillLevel="'..self.fillLevel..'"'..wrapping; |
240 | local nodes = ""; |
241 | return attributes, nodes; |
242 | end; |
243 | |
244 | function Bale:getValue() |
245 | |
246 | local pricePerLiter = 0; |
247 | local desc = Fillable.fillTypeIndexToDesc[self.fillType]; |
248 | if desc ~= nil then |
249 | pricePerLiter = desc.pricePerLiter; |
250 | end |
251 | return self.fillLevel * pricePerLiter * self.baleValueScale; |
252 | end; |
253 | |
254 | function Bale:getFillType() |
255 | return self.fillType; |
256 | end; |
257 | |
258 | function Bale:getFillLevel() |
259 | return self.fillLevel; |
260 | end; |
261 | |
262 | function Bale:setFillLevel(fillLevel) |
263 | self.fillLevel = fillLevel; |
264 | end; |
265 | |
266 | function Bale:setWrappingState(wrappingState) |
267 | if self.supportsWrapping then |
268 | if self.wrappingState ~= wrappingState then |
269 | self:raiseDirtyFlags(self.physicsObjectDirtyFlag); |
270 | end |
271 | self.wrappingState = wrappingState; |
272 | setShaderParameter(self.baleMesh, "wrappingState", self.wrappingState, 0, 0, 0, false); |
273 | end |
274 | end
|
Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de