20 | function LivestockTrailer:load(savegame) |
21 | |
22 | self.setUnitFillLevel = Utils.overwrittenFunction(self.setUnitFillLevel, LivestockTrailer.setUnitFillLevel); |
23 | self.getCapacity = Utils.overwrittenFunction(self.getCapacity, LivestockTrailer.getCapacity); |
24 | |
25 | self.livestockTrailer = {}; |
26 | self.livestockTrailer.fillUnitIndex = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.livestockTrailer#fillUnitIndex"), 1); |
27 | |
28 | self:setUnitCapacity(self.livestockTrailer.fillUnitIndex, 0); |
29 | self.animalPlaces = {}; |
30 | |
31 | local i=0; |
32 | while true do |
33 | local key = string.format("vehicle.liveStockTrailer.animal(%d)", i); |
34 | if not hasXMLProperty(self.xmlFile, key) then |
35 | break; |
36 | end |
37 | local animalName = getXMLString(self.xmlFile, key .. "#type"); |
38 | local animalDesc = AnimalUtil.animals[animalName]; |
39 | if animalDesc == nil then |
40 | print("Warning: animal type '"..tostring(animalName) .. "' could not be found!"); |
41 | break; |
42 | end |
43 | |
44 | local filename = getXMLString(self.xmlFile, key .. "#filename"); |
45 | if filename == nil then |
46 | if animalName == "pig" then |
47 | filename = "dataS2/character/pig/pigLoaded.i3d"; |
48 | elseif animalName == "sheep" then |
49 | filename = "dataS2/character/sheep/sheepLoaded.i3d"; |
50 | elseif animalName == "cow" then |
51 | filename = "dataS2/character/cow/cowLoaded.i3d"; |
52 | end |
53 | end |
54 | local i3dNode = Utils.loadSharedI3DFile(filename, self.baseDirectory, false, false, false); |
55 | local node; |
56 | if i3dNode == 0 then |
57 | print("Warning: Could not load file '"..tostring(filename).."' for animal type '"..tostring(animalName).."'"); |
58 | else |
59 | node = getChildAt(i3dNode, 0); |
60 | end |
61 | |
62 | local places = {}; |
63 | local parent = Utils.indexToObject(self.components, getXMLString(self.xmlFile, key .. "#index")); |
64 | local numChildren = getNumOfChildren(parent); |
65 | for i=0,numChildren-1 do |
66 | local child = getChildAt(parent, i); |
67 | if node ~= nil then |
68 | local animal = clone(node, false, false, false); |
69 | link(child, animal); |
70 | end |
71 | setVisibility(child, false); |
72 | table.insert(places, {node=child, used=false}); |
73 | end |
74 | self:setUnitCapacity(self.livestockTrailer.fillUnitIndex, math.max(self:getUnitCapacity(self.livestockTrailer.fillUnitIndex), table.getn(places))); |
75 | |
76 | self.animalPlaces[animalDesc.fillType] = places; |
77 | i=i+1; |
78 | end |
79 | |
80 | -- needed to make network sync. functions in Fillable.lua work correctly |
81 | self.synchronizeFullFillLevel = true; |
82 | |
83 | self.animalTrigger = nil |
84 | |
85 | self.LivestockTrailerDirtyFlag = self:getNextDirtyFlag(); |
86 | end |
91 | function LivestockTrailer:postLoad(savegame) |
92 | if savegame ~= nil then |
93 | if hasXMLProperty(savegame.xmlFile, savegame.key.."#fillLevels") and hasXMLProperty(savegame.xmlFile, savegame.key.."#fillTypes") then |
94 | local fillLevels = { Utils.getVectorFromString(Utils.getNoNil(getXMLString(savegame.xmlFile, savegame.key.."#fillLevels"), "")) }; |
95 | local fillTypes = Utils.splitString(" ", Utils.getNoNil(getXMLString(savegame.xmlFile, savegame.key.."#fillTypes"), "")); |
96 | |
97 | local i = self.livestockTrailer.fillUnitIndex; |
98 | if fillTypes[i] ~= nil and fillLevels[i] ~= nil then |
99 | local fillType = FillUtil.fillTypeNameToInt[ fillTypes[i] ]; |
100 | self:setFillLevel(0, fillType, true, nil); -- call function twice to make animal nodes visible |
101 | self:setFillLevel(fillLevels[i], fillType, true, nil); |
102 | end |
103 | end |
104 | end |
105 | |
106 | end |
150 | function LivestockTrailer:setUnitFillLevel(superFunc, fillUnitIndex, fillLevel, fillType, force, fillInfo) |
151 | |
152 | if fillUnitIndex ~= self.livestockTrailer.fillUnitIndex then |
153 | superFunc(self, fillUnitIndex, fillLevel, fillType, force, fillInfo) |
154 | else |
155 | |
156 | -- first, let's empty the trailer completely |
157 | for _,places in pairs(self.animalPlaces) do |
158 | for j=1,table.getn(places) do |
159 | places[j].used = false; |
160 | setVisibility(places[j].node, false); |
161 | end |
162 | end |
163 | |
164 | -- now, let's fill the trailer |
165 | if self.animalPlaces[fillType] ~= nil then |
166 | self:setUnitCapacity(self.livestockTrailer.fillUnitIndex, table.getn(self.animalPlaces[fillType])); |
167 | |
168 | local places = self.animalPlaces[fillType]; |
169 | |
170 | for i=1,fillLevel do |
171 | local index = math.random(1, self:getUnitCapacity(self.livestockTrailer.fillUnitIndex)); |
172 | if not places[index].used then |
173 | places[index].used = true; |
174 | setVisibility(places[index].node, true); |
175 | local animalNode = getChildAt(places[index].node, 0); |
176 | setShaderParameter(animalNode, "RDT", 1, math.random(), 20, 20, false); |
177 | setShaderParameter(animalNode, "mirrorScaleAndOffsetUV", 0.5, 0.5, math.random(0, 1), math.random(0, 1), false); |
178 | setShaderParameter(animalNode, "atlasInvSizeAndOffsetUV", 1, 1, 0, 0, false); |
179 | else |
180 | for j=1,table.getn(places) do |
181 | index = index + 1; |
182 | if index > table.getn(places) then |
183 | index = 1; |
184 | end |
185 | if not places[index].used then |
186 | places[index].used = true; |
187 | setVisibility(places[index].node, true); |
188 | local animalNode = getChildAt(places[index].node, 0); |
189 | setShaderParameter(animalNode, "RDT", 1, math.random(), 20, 20, false); |
190 | setShaderParameter(animalNode, "mirrorScaleAndOffsetUV", 0.5, 0.5, math.random(0, 1), math.random(0, 1), false); |
191 | setShaderParameter(animalNode, "atlasInvSizeAndOffsetUV", 1, 1, 0, 0, false); |
192 | break; |
193 | end |
194 | end |
195 | end |
196 | end |
197 | end |
198 | |
199 | superFunc(self, fillUnitIndex, fillLevel, fillType, force, fillInfo); |
200 | |
201 | end |
202 | end |