LUADOC - Farming Simulator 19

WindTurbinePlaceable

Description
Class for placeable wind turbines
Parent
Placeable
Functions

delete

Description
Deleting placeable wind turbine
Definition
delete()
Code
30function WindTurbinePlaceable:delete()
31 unregisterObjectClassName(self)
32 g_currentMission.environment:removeHourChangeListener(self)
33 WindTurbinePlaceable:superClass().delete(self)
34end

finalizePlacement

Description
Called if placeable is placed
Definition
finalizePlacement()
Code
91function WindTurbinePlaceable:finalizePlacement()
92 WindTurbinePlaceable:superClass().finalizePlacement(self)
93 g_currentMission.environment:addHourChangeListener(self)
94end

hourChanged

Description
Called if hour changed
Definition
hourChanged()
Code
167function WindTurbinePlaceable:hourChanged()
168 if self.isServer then
169 g_currentMission:addMoney(self.incomePerHour, self:getOwnerFarmId(), MoneyType.PROPERTY_INCOME, true)
170 end
171end

initPose

Description
Initialize pose
Definition
initPose(float x, float y, float z, float rx, float ry, float rz, boolean initRandom)
Arguments
floatxx world position
floatyy world position
floatzz world position
floatrxrx world rotation
floatryry world rotation
floatrzrz world rotation
booleaninitRandominitialize random
Code
105function WindTurbinePlaceable:initPose(x,y,z, rx,ry,rz, initRandom)
106 WindTurbinePlaceable:superClass().initPose(self, x,y,z, rx,ry,rz, initRandom)
107
108 if self.headNode ~= nil and self.headNode ~= 0 then
109 if initRandom == nil or initRandom == true then
110 local rotVariation = 0.2
111 self.headRotation = 0.7 + math.random() * 2*rotVariation - rotVariation
112 end
113
114 rotate(self.rotationNode, 0,0,math.random()*math.pi*2)
115
116 self:updateHeadRotation()
117 end
118end

load

Description
Load wind turbine
Definition
load(string xmlFilename, float x, float y, float z, float rx, float ry, float rz, boolean initRandom)
Arguments
stringxmlFilenamexml file name
floatxx world position
floatyz world position
floatzz world position
floatrxrx world rotation
floatryry world rotation
floatrzrz world rotation
booleaninitRandominitialize random
Return Values
booleansuccesssuccess
Code
71function WindTurbinePlaceable:load(xmlFilename, x,y,z, rx,ry,rz, initRandom)
72 if not WindTurbinePlaceable:superClass().load(self, xmlFilename, x,y,z, rx,ry,rz, initRandom) then
73 return false
74 end
75
76 local xmlFile = loadXMLFile("TempXML", xmlFilename)
77
78 self.headNode = I3DUtil.indexToObject(self.nodeId, getXMLString(xmlFile, "placeable.windTurbine#headNode"))
79 self.rotationNode = I3DUtil.indexToObject(self.nodeId, getXMLString(xmlFile, "placeable.windTurbine#rotationNode"))
80
81 -- Now that the nodes are loaded, pose again
82 self:initPose(x,y,z, rx,ry,rz, initRandom)
83
84 delete(xmlFile)
85
86 return true
87end

loadFromXMLFile

Description
Loading from attributes and nodes
Definition
loadFromXMLFile(integer xmlFile, string key, boolean resetVehicles)
Arguments
integerxmlFileid of xml object
stringkeykey
booleanresetVehiclesreset vehicles
Return Values
booleansuccesssuccess
Code
133function WindTurbinePlaceable:loadFromXMLFile(xmlFile, key, resetVehicles)
134 local headRotation = getXMLFloat(xmlFile, key.."#headRotation")
135 if headRotation == nil then
136 return false
137 end
138
139 self.headRotation = headRotation
140
141 if not WindTurbinePlaceable:superClass().loadFromXMLFile(self, xmlFile, key, resetVehicles) then
142 return false
143 end
144
145 return true
146end

new

Description
Creating placeable wind turbine
Definition
new(boolean isServer, boolean isClient, table customMt)
Arguments
booleanisServeris server
booleanisClientis client
tablecustomMtcustom metatable
Return Values
tableinstanceInstance of object
Code
16function WindTurbinePlaceable:new(isServer, isClient, customMt)
17 local self = Placeable:new(isServer, isClient, customMt or WindTurbinePlaceable_mt)
18
19 registerObjectClassName(self, "WindTurbinePlaceable")
20
21 self.rotationNode = 0
22 self.headNode = 0
23 self.incomePerHour = 0
24
25 return self
26end

readStream

Description
Called on client side on join
Definition
readStream(integer streamId, table connection)
Arguments
integerstreamIdstream ID
tableconnectionconnection
Code
40function WindTurbinePlaceable:readStream(streamId, connection)
41 if connection:getIsServer() then
42 self.headRotation = NetworkUtil.readCompressedAngle(streamId)
43 end
44
45 WindTurbinePlaceable:superClass().readStream(self, streamId, connection)
46end

update

Description
Update
Definition
update(float dt)
Arguments
floatdttime since last call in ms
Code
157function WindTurbinePlaceable:update(dt)
158 if self.rotationNode ~= 0 then
159 rotate(self.rotationNode, 0,0,-0.0025*dt)
160
161 self:raiseActive()
162 end
163end

updateHeadRotation

Description
Updating head rotation
Definition
updateHeadRotation()
Code
122function WindTurbinePlaceable:updateHeadRotation()
123 local dx,_,dz = worldDirectionToLocal(self.nodeId, math.sin(self.headRotation),0,math.cos(self.headRotation))
124 setDirection(self.headNode, dx,0,dz, 0,1,0)
125end

writeStream

Description
Called on server side on join
Definition
writeStream(integer streamId, table connection)
Arguments
integerstreamIdstream ID
tableconnectionconnection
Code
52function WindTurbinePlaceable:writeStream(streamId, connection)
53 if not connection:getIsServer() then
54 NetworkUtil.writeCompressedAngle(streamId, self.headRotation)
55 end
56
57 WindTurbinePlaceable:superClass().writeStream(self, streamId, connection)
58end