Script v1_7_1_0
- AI
- Animals
- Collections
- Contracts
- Debug
- Economy
- Elements
- EnvironmentalScore
- Errors
- Events
- GUI
- Handtools
- Hud
- I3d
- Input
- Jobs
- Maps
- Materials
- Misc
- Objects
- Parameters
- Placeables
- Placement
- Player
- Shop
- Sounds
- Specialization
- Specializations
- StateMachine
- Statistics
- Tasks
- Triggers
- Utils
- Vehicles
Engine v1_7_1_0
- AI
- Animation
- Camera
- Entity
- Fillplanes
- general
- General
- I3D
- Input
- Lighting
- Math
- Network
- Node
- NoteNode
- Overlays
- Particle System
- Physics
- Rendering
- Scenegraph
- Shape
- Sound
- Spline
- String
- Terrain Detail
- Text Rendering
- Tire Track
- VoiceChat
- XML
Foundation Reference
Dog
ParentObjectFunctions
- delete
- fetchItem
- finalizePlacement
- followEntity
- getUpdatePriority
- goToSpawn
- hourChanged
- idleStay
- idleWander
- isAbandoned
- load
- loadFromXMLFile
- new
- onFoodBowlFilled
- onGhostAdd
- onGhostRemove
- onIndoorStateChanged
- onPlayerLeave
- pet
- playerFarmChanged
- playerInteractionTriggerCallback
- readStream
- readUpdateStream
- registerSavegameXMLPaths
- resetSteeringParms
- saveToXMLFile
- setName
- setVisibility
- teleportToSpawn
- testScope
- update
- updateTick
- writeStream
- writeUpdateStream
delete
DescriptionDeleteDefinition
delete()Code
78 | function Dog:delete() |
79 | self.isDeleted = true -- mark as deleted so we can track it in Doghouse |
80 | if self.dogInstance ~= nil then |
81 | --#debug log("delete(self.dogInstance)") |
82 | delete(self.dogInstance) |
83 | end |
84 | |
85 | if self.isServer then |
86 | g_messageCenter:unsubscribeAll(self) |
87 | end |
88 | |
89 | unregisterObjectClassName(self) |
90 | g_soundManager:removeIndoorStateChangedListener(self) |
91 | |
92 | Dog:superClass().delete(self) |
93 | end |
fetchItem
DescriptionActivate fetch ball behaviorDefinition
fetchItem(object object)Arguments
object | object | of type DogBall |
342 | function Dog:fetchItem(player, ball) |
343 | if not self.isServer then |
344 | g_client:getServerConnection():sendEvent(DogFetchItemEvent.new(self, player, ball)) |
345 | else |
346 | local x, y, z = getWorldTranslation(ball.nodeId) |
347 | ball.throwPos = {x, y, z} |
348 | setCompanionBehaviorFetch(self.dogInstance, self.animalId, ball.nodeId, player.rootNode) |
349 | self.entityThrower = player.rootNode |
350 | end |
351 | end |
finalizePlacement
DescriptionDefinitionfinalizePlacement()Code
473 | function Dog:finalizePlacement() |
474 | self:setVisibility(true) |
475 | |
476 | if self.isServer then |
477 | g_messageCenter:subscribe(MessageType.HOUR_CHANGED, self.hourChanged, self) |
478 | g_messageCenter:subscribe(MessageType.PLAYER_FARM_CHANGED, Dog.playerFarmChanged, self) |
479 | end |
480 | end |
followEntity
DescriptionActivate follow playerDefinition
followEntity(integer scenegraph)Arguments
integer | scenegraph | node of the ball |
305 | function Dog:followEntity(player) |
306 | -- Note: we also set entityFollow on the client side so that we can update the gui (it won't be 100% accurate if an other player interacts, but that should be good enough) |
307 | self.entityFollow = player.rootNode |
308 | self.entityThrower = nil |
309 | if not self.isServer then |
310 | g_client:getServerConnection():sendEvent(DogFollowEvent.new(self, player)) |
311 | else |
312 | setCompanionBehaviorFollowEntity(self.dogInstance, self.animalId, self.entityFollow) |
313 | self.isStaying = false |
314 | end |
315 | end |
getUpdatePriority
DescriptionGet update priorityDefinition
getUpdatePriority(float skipCount, float x, float y, float z, float coeff, table connection)Arguments
float | skipCount | skip count |
float | x | x position |
float | y | y position |
float | z | z position |
float | coeff | coeff |
table | connection | connection |
float | priority | priority |
235 | function Dog:getUpdatePriority(skipCount, x, y, z, coeff, connection, isGuiVisible) |
236 | local distance, clipDistance = getCompanionClosestDistance(self.dogInstance, x, y, z) |
237 | local clipDist = math.min(clipDistance * coeff, self.forcedClipDistance) |
238 | local result = (1.0 - distance / clipDist) * 0.8 + 0.5 * skipCount * 0.2 |
239 | |
240 | return result |
241 | end |
goToSpawn
DescriptionActivate fetch ball behaviorDefinition
goToSpawn(integer scenegraph)Arguments
integer | scenegraph | node of the ball |
320 | function Dog:goToSpawn() |
321 | self.entityFollow = nil |
322 | self.entityThrower = nil |
323 | |
324 | if not self.isServer then |
325 | g_client:getServerConnection():sendEvent(DogFollowEvent.new(self, nil)) |
326 | else |
327 | setCompanionBehaviorGotoEntity(self.dogInstance, self.animalId, self.spawner:getSpawnNode()) |
328 | end |
329 | end |
hourChanged
DescriptionCalled by the environment when an hour has changed.Definition
hourChanged()Code
257 | function Dog:hourChanged() |
258 | if not self.isServer then |
259 | return |
260 | end |
261 | |
262 | if self.dogInstance ~= nil then |
263 | setCompanionDaytime(self.dogInstance, g_currentMission.environment.dayTime) |
264 | end |
265 | end |
idleStay
DescriptionActivate dog staying behavior (used when the currently followed player is switching vehicles or leaving the game)Definition
idleStay()Code
369 | function Dog:idleStay() |
370 | -- This is only supposed to be called on the server |
371 | self.entityFollow = nil |
372 | self.entityThrower = nil |
373 | setCompanionBehaviorDefault(self.dogInstance, self.animalId) |
374 | self.isStaying = true |
375 | end |
idleWander
DescriptionDefinitionidleWander()Code
379 | function Dog:idleWander() |
380 | -- This is only supposed to be called on the server |
381 | --setCompanionBehaviorIdleWander(self.dogInstance, self.animalId) |
382 | end |
isAbandoned
DescriptionDefinitionisAbandoned()Code
386 | function Dog:isAbandoned(dt) |
387 | local isEntityInRange = false |
388 | for _, player in pairs(g_currentMission.players) do |
389 | if player.isControlled then |
390 | local entityX, entityY, entityZ = getWorldTranslation(player.rootNode) |
391 | local distance, _ = getCompanionClosestDistance(self.dogInstance, entityX, entityY, entityZ) |
392 | if distance < self.abandonRange then |
393 | isEntityInRange = true |
394 | break |
395 | end |
396 | end |
397 | end |
398 | |
399 | if not isEntityInRange then |
400 | for _, enterable in pairs(g_currentMission.enterables) do |
401 | if enterable.spec_enterable ~= nil and enterable.spec_enterable.isControlled then |
402 | local entityX, entityY, entityZ = getWorldTranslation(enterable.rootNode) |
403 | local distance, _ = getCompanionClosestDistance(self.dogInstance, entityX, entityY, entityZ) |
404 | if distance < self.abandonRange then |
405 | isEntityInRange = true |
406 | break |
407 | end |
408 | end |
409 | end |
410 | end |
411 | |
412 | if isEntityInRange then |
413 | self.abandonTimer = self.abandonTimerDuration |
414 | else |
415 | self.abandonTimer = self.abandonTimer - dt |
416 | if self.abandonTimer <= 0 then |
417 | return true |
418 | end |
419 | end |
420 | |
421 | return false |
422 | end |
load
DescriptionDefinitionload()Code
47 | function Dog:load(spawner, xmlFilename, spawnX, spawnY, spawnZ) |
48 | self.spawner = spawner |
49 | self.animalId = 0 |
50 | self.spawnX = spawnX |
51 | self.spawnY = spawnY |
52 | self.spawnZ = spawnZ |
53 | self.xmlFilename = xmlFilename |
54 | self.name = g_currentMission.animalNameSystem:getRandomName() |
55 | |
56 | self.dogInstance = createAnimalCompanionManager("dog", self.xmlFilename, "dog", self.spawnX, self.spawnY, self.spawnZ, g_currentMission.terrainRootNode, self.isServer, self.isClient, 1, AudioGroup.ENVIRONMENT) |
57 | if self.dogInstance == 0 then |
58 | return false |
59 | end |
60 | |
61 | setCompanionTrigger(self.dogInstance, self.animalId, "playerInteractionTriggerCallback", self) |
62 | |
63 | local groundMask = CollisionFlag.TERRAIN + CollisionFlag.STATIC_WORLD + CollisionFlag.STATIC_OBJECT |
64 | local obstacleMask = CollisionFlag.STATIC_OBJECT + |
65 | CollisionFlag.DYNAMIC_OBJECT + |
66 | CollisionFlag.VEHICLE + |
67 | CollisionFlag.TRIGGER_VEHICLE |
68 | setCompanionCollisionMask(self.dogInstance, groundMask, obstacleMask, CollisionFlag.WATER) |
69 | |
70 | g_soundManager:addIndoorStateChangedListener(self) |
71 | setCompanionUseOutdoorAudioSetup(self.dogInstance, not g_soundManager:getIsIndoor()) |
72 | |
73 | return true |
74 | end |
loadFromXMLFile
DescriptionLoading from attributes and nodesDefinition
loadFromXMLFile(integer xmlFile, string key, boolean resetVehicles)Arguments
integer | xmlFile | id of xml object |
string | key | key |
boolean | resetVehicles | reset vehicles |
boolean | success | success |
101 | function Dog:loadFromXMLFile(xmlFile, key, resetVehicles) |
102 | self:setName(xmlFile:getValue(key.."#name", "")) |
103 | |
104 | return true |
105 | end |
new
DescriptionCreatingDefinition
new()Return Values
table | instance | instance of object |
17 | function Dog.new(isServer, isClient, customMt) |
18 | local self = Object.new(isServer, isClient, customMt or Dog_mt) |
19 | |
20 | self.dogInstance = nil |
21 | self.animalId = nil |
22 | self.spawner = nil |
23 | self.xmlFilename = nil |
24 | self.entityFollow = nil |
25 | self.entityThrower = nil |
26 | self.playersInRange = {} |
27 | self.isStaying = false |
28 | self.abandonTimer = 0.0 |
29 | self.abandonTimerDuration = 6000 |
30 | self.abandonRange = 100 |
31 | self.name = "" |
32 | |
33 | self.spawnX = 0 |
34 | self.spawnY = 0 |
35 | self.spawnZ = 0 |
36 | |
37 | self.forcedClipDistance = 80 |
38 | |
39 | registerObjectClassName(self, "Dog") |
40 | |
41 | self.dirtyFlag = self:getNextDirtyFlag() |
42 | return self |
43 | end |
onFoodBowlFilled
DescriptionDefinitiononFoodBowlFilled()Code
333 | function Dog:onFoodBowlFilled(foodBowlNode) |
334 | self.entityFollow = nil |
335 | self.entityThrower = nil |
336 | setCompanionBehaviorFeed(self.dogInstance, self.animalId, foodBowlNode) |
337 | end |
onGhostAdd
DescriptionOn ghost addDefinition
onGhostAdd()Code
251 | function Dog:onGhostAdd() |
252 | self:setVisibility(true) |
253 | end |
onGhostRemove
DescriptionOn ghost removeDefinition
onGhostRemove()Code
245 | function Dog:onGhostRemove() |
246 | self:setVisibility(false) |
247 | end |
onIndoorStateChanged
DescriptionDefinitiononIndoorStateChanged()Code
484 | function Dog:onIndoorStateChanged(isIndoor) |
485 | setCompanionUseOutdoorAudioSetup(self.dogInstance, not g_soundManager:getIsIndoor()) |
486 | end |
onPlayerLeave
DescriptionTells the dog to stay at the current place when we leave the currently followed playerDefinition
onPlayerLeave()Code
459 | function Dog:onPlayerLeave(player) |
460 | -- return dog to it's house |
461 | if self.isServer then |
462 | if self.entityFollow == player.rootNode or self.entityThrower == player.rootNode then |
463 | self:idleStay() |
464 | end |
465 | if self.playersInRange[player.rootNode] ~= nil then |
466 | self.playersInRange[player.rootNode] = nil |
467 | end |
468 | end |
469 | end |
pet
DescriptionActivate dog petting response behaviorDefinition
pet()Code
355 | function Dog:pet() |
356 | if not self.isServer then |
357 | g_client:getServerConnection():sendEvent(DogPetEvent.new(self)) |
358 | else |
359 | setCompanionBehaviorPet(self.dogInstance, self.animalId) |
360 | |
361 | local stats = g_farmManager:getFarmById(self:getOwnerFarmId()).stats |
362 | local total = stats:updateStats("petDogCount", 1) |
363 | g_achievementManager:tryUnlock("PetDog", total) |
364 | end |
365 | end |
playerFarmChanged
DescriptionDefinitionplayerFarmChanged()Code
449 | function Dog:playerFarmChanged(player) |
450 | if self.isServer then |
451 | if self.entityFollow == player.rootNode or self.entityThrower == player.rootNode then |
452 | self:idleStay() |
453 | end |
454 | end |
455 | end |
playerInteractionTriggerCallback
DescriptionCallback when dog interaction trigger is activatedDefinition
playerInteractionTriggerCallback(integer triggerId, integer otherId, boolean onEnter, boolean onLeave, boolean onStay)Arguments
integer | triggerId | id of trigger |
integer | otherId | id of actor |
boolean | onEnter | on enter |
boolean | onLeave | on leave |
boolean | onStay | on stay |
289 | function Dog:playerInteractionTriggerCallback(triggerId, otherId, onEnter, onLeave, onStay) |
290 | local player = g_currentMission.players[otherId] |
291 | if player ~= nil then |
292 | if onEnter then |
293 | if g_currentMission.accessHandler:canFarmAccess(player.farmId, self, false) then |
294 | self.playersInRange[otherId] = true |
295 | end |
296 | elseif onLeave then |
297 | self.playersInRange[otherId] = nil |
298 | end |
299 | end |
300 | end |
readStream
DescriptionCalled on client side on joinDefinition
readStream(integer streamId, table connection)Arguments
integer | streamId | stream ID |
table | connection | connection |
120 | function Dog:readStream(streamId, connection) |
121 | if connection:getIsServer() then |
122 | local spawner = NetworkUtil.readNodeObject(streamId) |
123 | -- Note: the spawner can be when the doghouse not synced yet. The spawner will be set later on |
124 | local xmlFilename = NetworkUtil.convertFromNetworkFilename(streamReadString(streamId)) |
125 | |
126 | local spawnX = streamReadFloat32(streamId) |
127 | local spawnY = streamReadFloat32(streamId) |
128 | local spawnZ = streamReadFloat32(streamId) |
129 | local name = streamReadString(streamId) |
130 | |
131 | local isNew = self.xmlFilename == nil |
132 | if isNew then |
133 | self:load(spawner, xmlFilename, spawnX, spawnY, spawnZ) |
134 | if spawner ~= nil then |
135 | spawner.dog = self |
136 | end |
137 | end |
138 | |
139 | self:setName(name) |
140 | end |
141 | Dog:superClass().readStream(self, streamId, connection) |
142 | end |
readUpdateStream
DescriptionRead update network streamDefinition
readUpdateStream(integer streamId, integer timestamp, table connection)Arguments
integer | streamId | network stream identification |
integer | timestamp | |
table | connection | connection information |
176 | function Dog:readUpdateStream(streamId, timestamp, connection) |
177 | if connection:getIsServer() then |
178 | readAnimalCompanionManagerFromStream(self.dogInstance, streamId, g_clientInterpDelay, g_packetPhysicsNetworkTime, g_client.tickDuration) |
179 | end |
180 | end |
registerSavegameXMLPaths
DescriptionDefinitionregisterSavegameXMLPaths()Code
490 | function Dog.registerSavegameXMLPaths(schema, basePath) |
491 | schema:register(XMLValueType.STRING, basePath .. "#name", "Name of dog") |
492 | end |
resetSteeringParms
DescriptionDefinitionresetSteeringParms()Code
426 | function Dog:resetSteeringParms() |
427 | -- This is only supposed to be called on the server |
428 | |
429 | end |
saveToXMLFile
DescriptionGet save attributes and nodesDefinition
saveToXMLFile(string nodeIdent)Arguments
string | nodeIdent | node ident |
string | attributes | attributes |
string | nodes | nodes |
112 | function Dog:saveToXMLFile(xmlFile, key, usedModNames) |
113 | xmlFile:setValue(key.."#name", HTMLUtil.encodeToHTML(self.name)) |
114 | end |
setName
DescriptionDefinitionsetName()Code
269 | function Dog:setName(name) |
270 | self.name = name or "" |
271 | end |
setVisibility
DescriptionDefinitionsetVisibility()Code
275 | function Dog:setVisibility(state) |
276 | if self.dogInstance ~= nil then |
277 | setCompanionsVisibility(self.dogInstance, state) |
278 | setCompanionsPhysicsUpdate(self.dogInstance, state) |
279 | end |
280 | end |
teleportToSpawn
DescriptionDefinitionteleportToSpawn()Code
433 | function Dog:teleportToSpawn() |
434 | if self.isServer then |
435 | setCompanionPosition(self.dogInstance, self.animalId, self.spawnX, self.spawnY, self.spawnZ) |
436 | self:idleWander() |
437 | self:resetSteeringParms() |
438 | self.isStaying = false |
439 | self.entityFollow = nil |
440 | self.entityThrower = nil |
441 | |
442 | -- TODO sync notification |
443 | g_currentMission:addIngameNotification(FSBaseMission.INGAME_NOTIFICATION_OK, g_i18n:getText("ingameNotification_dogInDogHouse")) |
444 | end |
445 | end |
testScope
DescriptionTest scopeDefinition
testScope(float x, float y, float z, float coeff)Arguments
float | x | x position |
float | y | y position |
float | z | z position |
float | coeff | coeff |
boolean | inScope | in scope |
219 | function Dog:testScope(x,y,z, coeff, isGuiVisible) |
220 | local distance, clipDistance = getCompanionClosestDistance(self.dogInstance, x, y, z) |
221 | local clipDist = math.min(clipDistance * coeff, self.forcedClipDistance) |
222 | |
223 | return distance < clipDist |
224 | end |
update
DescriptionUpdateDefinition
update(float dt)Arguments
float | dt | time since last call in ms |
185 | function Dog:update(dt) |
186 | -- companionDebugDraw(self.dogInstance, self.animalId, true, true, true) |
187 | |
188 | if self.isServer then |
189 | if self.isStaying and self:isAbandoned(dt) then |
190 | self:teleportToSpawn() |
191 | end |
192 | |
193 | -- The dog is always active |
194 | -- The only time when it could be disabled is when it is staying. But we don't care to optimize for that case |
195 | self:raiseActive() |
196 | end |
197 | Dog:superClass().update(self, dt) |
198 | end |
updateTick
DescriptionUpdate network tickDefinition
updateTick(float dt)Arguments
float | dt | time since last call in ms |
203 | function Dog:updateTick(dt) |
204 | if self.isServer and self.dogInstance ~= nil then |
205 | if getAnimalCompanionNeedNetworkUpdate(self.dogInstance) then |
206 | self:raiseDirtyFlags(self.dirtyFlag) |
207 | end |
208 | end |
209 | Dog:superClass().updateTick(self, dt) |
210 | end |
writeStream
DescriptionCalled on server side on joinDefinition
writeStream(integer streamId, table connection)Arguments
integer | streamId | stream ID |
table | connection | connection |
148 | function Dog:writeStream(streamId, connection) |
149 | if not connection:getIsServer() then |
150 | NetworkUtil.writeNodeObject(streamId, self.spawner) |
151 | streamWriteString(streamId, NetworkUtil.convertToNetworkFilename(self.xmlFilename)) |
152 | streamWriteFloat32(streamId, self.spawnX) |
153 | streamWriteFloat32(streamId, self.spawnY) |
154 | streamWriteFloat32(streamId, self.spawnZ) |
155 | streamWriteString(streamId, self.name) |
156 | end |
157 | Dog:superClass().writeStream(self, streamId, connection) |
158 | end |
writeUpdateStream
DescriptionWrite update network streamDefinition
writeUpdateStream(integer streamId, table connection, integer dirtyMask)Arguments
integer | streamId | network stream identification |
table | connection | connection information |
integer | dirtyMask |
165 | function Dog:writeUpdateStream(streamId, connection, dirtyMask) |
166 | if not connection:getIsServer() then |
167 | writeAnimalCompanionManagerToStream(self.dogInstance, streamId) |
168 | end |
169 | end |