LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

PlaceableDoghouse

Description
Specialization for placeables
Functions

canBuy

Description
Definition
canBuy()
Code
329function PlaceableDoghouse:canBuy(superFunc)
330 local canBuy, warning = superFunc(self)
331 if not canBuy then
332 return false, warning
333 end
334
335 if self:isDoghouseRegistered() then
336 return false, g_i18n:getText("warning_onlyOneOfThisItemAllowedPerFarm")
337 end
338
339 return true, nil
340end

dogInteractionTriggerCallback

Description
Definition
dogInteractionTriggerCallback()
Code
216function PlaceableDoghouse:dogInteractionTriggerCallback(triggerId, otherId, onEnter, onLeave, onStay)
217 local spec = self.spec_doghouse
218 if spec.dog ~= nil then
219 if g_currentMission.player ~= nil and otherId == g_currentMission.player.rootNode and g_currentMission.player.farmId == self:getOwnerFarmId() then
220 if onEnter then
221 g_currentMission.activatableObjectsSystem:addActivatable(spec.activatable)
222 elseif onLeave then
223 g_currentMission.activatableObjectsSystem:removeActivatable(spec.activatable)
224 end
225 end
226 end
227end

drawDogName

Description
Definition
drawDogName()
Code
231function PlaceableDoghouse:drawDogName()
232 local spec = self.spec_doghouse
233 if spec.dog ~= nil and spec.namePlateNode ~= nil then
234 -- @Todo: adust text width
235 setTextColor(0.843, 0.745, 0.705, 1.0)
236 setTextAlignment(RenderText.ALIGN_CENTER)
237 local x,y,z = getWorldTranslation(spec.namePlateNode)
238 local rx,ry,rz = getWorldRotation(spec.namePlateNode)
239 renderText3D(x,y,z, rx,ry,rz, 0.04, spec.dog.name)
240 setTextAlignment(RenderText.ALIGN_LEFT)
241 end
242end

getCanBePlacedAt

Description
Definition
getCanBePlacedAt()
Code
319function PlaceableDoghouse:getCanBePlacedAt(superFunc, x, y, z, farmId)
320 if self:isDoghouseRegistered() then
321 return false, g_i18n:getText("warning_onlyOneOfThisItemAllowedPerFarm")
322 end
323
324 return superFunc(self, x, y, z, farmId)
325end

getDog

Description
Definition
getDog()
Code
307function PlaceableDoghouse:getDog()
308 return self.spec_doghouse.dog
309end

getNeedHourChanged

Description
Definition
getNeedHourChanged()
Code
344function PlaceableDoghouse:getNeedHourChanged(superFunc)
345 return true
346end

getSpawnNode

Description
Definition
getSpawnNode()
Code
313function PlaceableDoghouse:getSpawnNode()
314 return self.spec_doghouse.spawnNode
315end

isDoghouseRegistered

Description
Returns true if a doghouse is registered
Definition
isDoghouseRegistered()
Return Values
bool
Code
247function PlaceableDoghouse:isDoghouseRegistered()
248 local dogHouse = g_currentMission:getDoghouse(self:getOwnerFarmId())
249 return dogHouse ~= nil
250end

loadFromXMLFile

Description
Definition
loadFromXMLFile()
Code
198function PlaceableDoghouse:loadFromXMLFile(xmlFile, key)
199 local spec = self.spec_doghouse
200 if spec.dog ~= nil then
201 spec.dog:loadFromXMLFile(xmlFile, key)
202 end
203end

onDelete

Description
Definition
onDelete()
Code
132function PlaceableDoghouse:onDelete()
133 local spec = self.spec_doghouse
134
135 g_currentMission.activatableObjectsSystem:removeActivatable(spec.activatable)
136
137 self:unregisterDoghouseToMission()
138
139 if self.isServer then
140 if spec.dogBall ~= nil and not spec.dogBall.isDeleted then -- Note: the dogBall might have been deleted by the server/client delete loop already
141 spec.dogBall:delete()
142 spec.dogBall = nil
143 end
144 if spec.dog ~= nil and not spec.dog.isDeleted then -- Note: the dogBall might have been deleted by the server/client delete loop already
145 spec.dog:delete()
146 spec.dog = nil
147 end
148 end
149
150 if spec.triggerNode ~= nil then
151 removeTrigger(spec.triggerNode)
152 end
153end

onFinalizePlacement

Description
Definition
onFinalizePlacement()
Code
157function PlaceableDoghouse:onFinalizePlacement()
158 local spec = self.spec_doghouse
159
160 if self.isServer and spec.dog ~= nil then
161 spec.dog:finalizePlacement()
162 end
163
164 self:registerDoghouseToMission()
165end

onHourChanged

Description
Definition
onHourChanged()
Code
350function PlaceableDoghouse:onHourChanged()
351 self:setFoodBowlState(false, true)
352end

onLoad

Description
Called on loading
Definition
onLoad(table savegame)
Arguments
tablesavegamesavegame
Code
84function PlaceableDoghouse:onLoad(savegame)
85 local spec = self.spec_doghouse
86 local xmlFile = self.xmlFile
87
88 spec.spawnNode = xmlFile:getValue("placeable.dogHouse.dog#node", nil, self.components, self.i3dMappings)
89 if self.isServer then
90 local posX, posY, posZ = getWorldTranslation(spec.spawnNode)
91 local xmlFilename = Utils.getFilename(xmlFile:getValue("placeable.dogHouse.dog#xmlFilename"), self.baseDirectory)
92 spec.dog = Dog.new(self.isServer, self.isClient)
93 spec.dog:setOwnerFarmId(self:getOwnerFarmId(), true)
94 if spec.dog:load(self, xmlFilename, posX, posY, posZ) then
95 spec.dog:register()
96 else
97 Logging.xmlWarning(xmlFile, "Could not load dog!")
98 end
99 end
100
101 spec.namePlateNode = xmlFile:getValue("placeable.dogHouse.nameplate#node", nil, self.components, self.i3dMappings)
102
103 spec.ballSpawnNode = xmlFile:getValue("placeable.dogHouse.ball#node", nil, self.components, self.i3dMappings)
104 if self.isServer then
105 local dogBallFilename = Utils.getFilename(xmlFile:getValue("placeable.dogHouse.ball#filename"), self.baseDirectory)
106 local x, y, z = getWorldTranslation(spec.ballSpawnNode)
107 local rx, ry, rz = getWorldRotation(spec.ballSpawnNode)
108 spec.dogBall = DogBall.new(self.isServer, self.isClient)
109 spec.dogBall:setOwnerFarmId(self:getOwnerFarmId(), true)
110 spec.dogBall:load(dogBallFilename, x, y, z, rx, ry, rz, self)
111 spec.dogBall:register()
112 end
113
114 -- player interaction trigger
115 spec.triggerNode = xmlFile:getValue("placeable.dogHouse.playerInteractionTrigger#node", nil, self.components, self.i3dMappings)
116 if spec.triggerNode ~= nil then
117 addTrigger(spec.triggerNode, "dogInteractionTriggerCallback", self)
118 end
119
120 spec.foodNode = xmlFile:getValue("placeable.dogHouse.bowl#foodNode", nil, self.components, self.i3dMappings)
121 if spec.foodNode == nil then
122 Logging.xmlWarning(xmlFile, "Missing bowl food node in 'placeable.dogHouse.bowl#foodNode'!")
123 else
124 setVisibility(spec.foodNode, false)
125 end
126
127 spec.activatable = PlaceableDoghouseActivatable.new(self)
128end

onReadStream

Description
Definition
onReadStream()
Code
169function PlaceableDoghouse:onReadStream(streamId, connection)
170 if connection:getIsServer() then
171 local spec = self.spec_doghouse
172 spec.dog = NetworkUtil.readNodeObject(streamId)
173 if spec.dog ~= nil then
174 spec.dog.spawner = self
175 end
176 if spec.foodNode ~= nil then
177 setVisibility(spec.foodNode, streamReadBool(streamId))
178 end
179 end
180end

onWriteStream

Description
Called on server side on join
Definition
onWriteStream(integer streamId, table connection)
Arguments
integerstreamIdstream ID
tableconnectionconnection
Code
186function PlaceableDoghouse:onWriteStream(streamId, connection)
187 if not connection:getIsServer() then
188 local spec = self.spec_doghouse
189 NetworkUtil.writeNodeObject(streamId, spec.dog)
190 if spec.foodNode ~= nil then
191 streamWriteBool(streamId, getVisibility(spec.foodNode))
192 end
193 end
194end

prerequisitesPresent

Description
Checks if all prerequisite specializations are loaded
Definition
prerequisitesPresent(table specializations)
Arguments
tablespecializationsspecializations
Return Values
booleanhasPrerequisitetrue if all prerequisite specializations are loaded
Code
21function PlaceableDoghouse.prerequisitesPresent(specializations)
22 return true
23end

registerDoghouseToMission

Description
Registers the doghouse to the mission game.
Definition
registerDoghouseToMission()
Return Values
booltrueif registration went well
Code
255function PlaceableDoghouse:registerDoghouseToMission()
256 if not self:isDoghouseRegistered() then
257 g_currentMission.doghouses[self] = self
258 return true
259 end
260
261 return false
262end

registerEventListeners

Description
Definition
registerEventListeners()
Code
50function PlaceableDoghouse.registerEventListeners(placeableType)
51 SpecializationUtil.registerEventListener(placeableType, "onLoad", PlaceableDoghouse)
52 SpecializationUtil.registerEventListener(placeableType, "onDelete", PlaceableDoghouse)
53 SpecializationUtil.registerEventListener(placeableType, "onFinalizePlacement", PlaceableDoghouse)
54 SpecializationUtil.registerEventListener(placeableType, "onWriteStream", PlaceableDoghouse)
55 SpecializationUtil.registerEventListener(placeableType, "onReadStream", PlaceableDoghouse)
56 SpecializationUtil.registerEventListener(placeableType, "onHourChanged", PlaceableDoghouse)
57end

registerFunctions

Description
Definition
registerFunctions()
Code
27function PlaceableDoghouse.registerFunctions(placeableType)
28 SpecializationUtil.registerFunction(placeableType, "dogInteractionTriggerCallback", PlaceableDoghouse.dogInteractionTriggerCallback)
29 SpecializationUtil.registerFunction(placeableType, "drawDogName", PlaceableDoghouse.drawDogName)
30 SpecializationUtil.registerFunction(placeableType, "isDoghouseRegistered", PlaceableDoghouse.isDoghouseRegistered)
31 SpecializationUtil.registerFunction(placeableType, "registerDoghouseToMission", PlaceableDoghouse.registerDoghouseToMission)
32 SpecializationUtil.registerFunction(placeableType, "unregisterDoghouseToMission", PlaceableDoghouse.unregisterDoghouseToMission)
33 SpecializationUtil.registerFunction(placeableType, "setFoodBowlState", PlaceableDoghouse.setFoodBowlState)
34 SpecializationUtil.registerFunction(placeableType, "getDog", PlaceableDoghouse.getDog)
35 SpecializationUtil.registerFunction(placeableType, "getSpawnNode", PlaceableDoghouse.getSpawnNode)
36end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
41function PlaceableDoghouse.registerOverwrittenFunctions(placeableType)
42 SpecializationUtil.registerOverwrittenFunction(placeableType, "setOwnerFarmId", PlaceableDoghouse.setOwnerFarmId)
43 SpecializationUtil.registerOverwrittenFunction(placeableType, "canBuy", PlaceableDoghouse.canBuy)
44 SpecializationUtil.registerOverwrittenFunction(placeableType, "getCanBePlacedAt", PlaceableDoghouse.getCanBePlacedAt)
45 SpecializationUtil.registerOverwrittenFunction(placeableType, "getNeedHourChanged", PlaceableDoghouse.getNeedHourChanged)
46end

registerSavegameXMLPaths

Description
Definition
registerSavegameXMLPaths()
Code
75function PlaceableDoghouse.registerSavegameXMLPaths(schema, basePath)
76 schema:setXMLSpecializationType("Doghouse")
77 Dog.registerSavegameXMLPaths(schema, basePath)
78 schema:setXMLSpecializationType()
79end

registerXMLPaths

Description
Definition
registerXMLPaths()
Code
61function PlaceableDoghouse.registerXMLPaths(schema, basePath)
62 schema:setXMLSpecializationType("Doghouse")
63 schema:register(XMLValueType.NODE_INDEX, basePath .. ".dogHouse.dog#node", "Dog link node")
64 schema:register(XMLValueType.STRING, basePath .. ".dogHouse.dog#xmlFilename", "Dog xml filename")
65 schema:register(XMLValueType.NODE_INDEX, basePath .. ".dogHouse.nameplate#node", "Name plate node")
66 schema:register(XMLValueType.NODE_INDEX, basePath .. ".dogHouse.ball#node", "Ball node")
67 schema:register(XMLValueType.STRING, basePath .. ".dogHouse.ball#filename", "Ball 3d file")
68 schema:register(XMLValueType.NODE_INDEX, basePath .. ".dogHouse.playerInteractionTrigger#node", "Interaction trigger node")
69 schema:register(XMLValueType.NODE_INDEX, basePath .. ".dogHouse.bowl#foodNode", "Food node in bowl")
70 schema:setXMLSpecializationType()
71end

saveToXMLFile

Description
Definition
saveToXMLFile()
Code
207function PlaceableDoghouse:saveToXMLFile(xmlFile, key, usedModNames)
208 local spec = self.spec_doghouse
209 if spec.dog ~= nil then
210 spec.dog:saveToXMLFile(xmlFile, key, usedModNames)
211 end
212end

setFoodBowlState

Description
Definition
setFoodBowlState()
Code
292function PlaceableDoghouse:setFoodBowlState(isFilled, noEventSend)
293 local spec = self.spec_doghouse
294 if spec.foodNode ~= nil then
295 PlaceableDoghouseFoodBowlStateEvent.sendEvent(self, isFilled, noEventSend)
296
297 setVisibility(spec.foodNode, isFilled)
298
299 if isFilled and spec.dog ~= nil then
300 spec.dog:onFoodBowlFilled(spec.foodNode)
301 end
302 end
303end

setOwnerFarmId

Description
Definition
setOwnerFarmId()
Code
275function PlaceableDoghouse:setOwnerFarmId(superFunc, farmId, noEventSend)
276 superFunc(self, farmId, noEventSend)
277
278 if self.isServer then
279 local spec = self.spec_doghouse
280 -- Note: we need to send events for the dog and the dogBall if we need to send one for the doghouse
281 if spec.dog ~= nil then
282 spec.dog:setOwnerFarmId(farmId, noEventSend)
283 end
284 if spec.dogBall ~= nil then
285 spec.dogBall:setOwnerFarmId(farmId, noEventSend)
286 end
287 end
288end

unregisterDoghouseToMission

Description
Registers the doghouse to the mission game.
Definition
unregisterDoghouseToMission()
Return Values
booltrueif registration went well
Code
267function PlaceableDoghouse:unregisterDoghouseToMission()
268 g_currentMission.doghouses[self] = nil
269
270 return true
271end