64 | function AnimalManager:loadAnimals(xmlHandle, baseDirectory) |
65 | if xmlHandle == 0 then |
66 | return false |
67 | end |
68 | self.animals = {} |
69 | self.typeToAnimal = {} |
70 | local i = 0 |
71 | while true do |
72 | local animalKey = string.format("animals.animal(%d)", i) |
73 | if not hasXMLProperty(xmlHandle, animalKey) then |
74 | break |
75 | end |
76 | local animal = {} |
77 | animal.type = getXMLString(xmlHandle, animalKey.. "#type") |
78 | animal.class = getXMLString(xmlHandle, animalKey.. "#class") |
79 | |
80 | if not ClassUtil.getIsValidClassName(animal.class) then |
81 | g_logManager:error("Invalid animal class name '%s'!", tostring(animal.class)) |
82 | return false |
83 | end |
84 | if ClassUtil.getClassObject(animal.class) == nil then |
85 | g_logManager:error("Animal class '%s' not defined !", tostring(animal.class)) |
86 | return false |
87 | end |
88 | |
89 | local hasName = Utils.getNoNil(getXMLBool(xmlHandle, animalKey.."#hasName"), false) |
90 | animal.stats = {} |
91 | animal.stats.breeding = Utils.getNoNil(getXMLString(xmlHandle, animalKey.. "#statsBreeding"), "") |
92 | animal.subTypes = {} |
93 | if animal.type ~= nil then |
94 | animal.type = animal.type:upper() |
95 | -- Specific animal subtype info |
96 | local j = 0 |
97 | while true do |
98 | local subTypeKey = string.format("%s.subType(%d)", animalKey, j) |
99 | if not hasXMLProperty(xmlHandle, subTypeKey) then |
100 | break |
101 | end |
102 | local animalSubType = {} |
103 | local fillTypeName = getXMLString(xmlHandle, subTypeKey.."#fillTypeName") |
104 | local fillType = g_fillTypeManager:getFillTypeByName(fillTypeName) |
105 | if fillType ~= nil then |
106 | animalSubType.storeInfo = {} |
107 | animalSubType.output = {} |
108 | animalSubType.input = {} |
109 | animalSubType.breeding = {} |
110 | animalSubType.texture = {} |
111 | animalSubType.dirt = {} |
112 | animalSubType.dummy = {} |
113 | -- General info |
114 | self.numAnimals = self.numAnimals + 1 |
115 | animalSubType.index = self.numAnimals |
116 | animalSubType.type = animal.type |
117 | animalSubType.hasName = hasName |
118 | animalSubType.subTypeId = j + 1 -- in lua context we use a 1-based array. Only when we call engine do we translate to 0-based array |
119 | animalSubType.fillType = fillType.index |
120 | animalSubType.fillTypeDesc = fillType |
121 | animalSubType.subTypeName = g_i18n:convertText(Utils.getNoNil(getXMLString(xmlHandle, subTypeKey.."#name"), "")) |
122 | -- Store |
123 | animalSubType.storeInfo.shopItemName = g_i18n:convertText(Utils.getNoNil(getXMLString(xmlHandle, subTypeKey..".store#itemName"), "")) |
124 | animalSubType.storeInfo.canBeBought = Utils.getNoNil(getXMLBool(xmlHandle, subTypeKey..".store#canBeBought"), false) |
125 | animalSubType.storeInfo.imageFilename = Utils.getNoNil(getXMLString(xmlHandle, subTypeKey..".store#image"), "") |
126 | animalSubType.storeInfo.imageFilename = Utils.getFilename(animalSubType.storeInfo.imageFilename, baseDirectory) |
127 | animalSubType.storeInfo.buyPrice = Utils.getNoNil(getXMLFloat(xmlHandle, subTypeKey..".store#buyPrice"), 0.0) |
128 | animalSubType.storeInfo.sellPrice = Utils.getNoNil(getXMLFloat(xmlHandle, subTypeKey..".store#sellPrice"), 0.0) |
129 | animalSubType.storeInfo.transportPrice = Utils.getNoNil(getXMLFloat(xmlHandle, subTypeKey..".store#transportPrice"), 200.0) |
130 | -- Output |
131 | animalSubType.output.milkPerDay = Utils.getNoNil(getXMLFloat(xmlHandle, subTypeKey..".output#milkPerDay"), 0) |
132 | animalSubType.output.manurePerDay = Utils.getNoNil(getXMLFloat(xmlHandle, subTypeKey..".output#manurePerDay"), 0) |
133 | animalSubType.output.liquidManurePerDay = Utils.getNoNil(getXMLFloat(xmlHandle, subTypeKey..".output#liquidManurePerDay"), 0) |
134 | animalSubType.output.palletsPerDay = Utils.getNoNil(getXMLFloat(xmlHandle, subTypeKey..".output#palletsPerDay"), 0) |
135 | animalSubType.output.foodSpillagePerDay = Utils.getNoNil(getXMLFloat(xmlHandle, subTypeKey..".output#foodSpillagePerDay"), 0) |
136 | -- Input |
137 | animalSubType.input.strawPerDay = Utils.getNoNil(getXMLFloat(xmlHandle, subTypeKey..".input#strawPerDay"), 0) |
138 | animalSubType.input.waterPerDay = Utils.getNoNil(getXMLFloat(xmlHandle, subTypeKey..".input#waterPerDay"), 0) |
139 | animalSubType.input.foodPerDay = Utils.getNoNil(getXMLFloat(xmlHandle, subTypeKey..".input#foodPerDay"), 0) |
140 | -- texture |
141 | animalSubType.texture.tileUIndex = getXMLInt(xmlHandle, subTypeKey..".texture#tileUIndex") |
142 | animalSubType.texture.tileVIndex = getXMLInt(xmlHandle, subTypeKey..".texture#tileVIndex") |
143 | -- dirt |
144 | animalSubType.dirt.cleanDuration = (getXMLFloat(xmlHandle, subTypeKey..".dirt#cleanDuration") or 0.5) * 1000 |
145 | -- dummy |
146 | animalSubType.dummy.filename = Utils.getFilename(Utils.getNoNil(getXMLString(xmlHandle, subTypeKey..".dummy#filename"), baseDirectory)) |
147 | animalSubType.dummy.meshNodeStr = getXMLString(xmlHandle, subTypeKey..".dummy#meshNode") or "0" |
148 | animalSubType.dummy.hairNodeStr = getXMLString(xmlHandle, subTypeKey..".dummy#hairNode") |
149 | |
150 | -- Breeding |
151 | animalSubType.breeding.birthRatePerDay = Utils.getNoNil(getXMLFloat(xmlHandle, subTypeKey..".breeding#birthRatePerDay"), 0) |
152 | -- Riding |
153 | animalSubType.rideableFileName = Utils.getFilename(Utils.getNoNil(getXMLString(xmlHandle, subTypeKey..".rideable#filename"), "")) |
154 | -- Search tables |
155 | self.indexToAnimal[animalSubType.index] = animalSubType |
156 | self.fillTypeToAnimal[animalSubType.fillType] = animalSubType |
157 | |
158 | table.insert(animal.subTypes, animalSubType) |
159 | else |
160 | g_logManager:warning("FillType '%s' for animal '%s' not defined. Ignoring animal!", tostring(fillTypeName), animal.subTypeName) |
161 | end |
162 | j = j + 1 |
163 | end |
164 | |
165 | if #animal.subTypes > 0 then |
166 | self.typeToAnimal[animal.type] = animal |
167 | table.insert(self.animals, animal) |
168 | else |
169 | g_logManager:error("No sub types defined for animal '%s'. Ignoring animal!", animal.type) |
170 | end |
171 | else |
172 | g_logManager:warning("Animal '%d' has no type. Ignoring animal!", i) |
173 | end |
174 | i = i + 1 |
175 | end |
176 | end |