LUADOC - Farming Simulator 17

Printable Version

WindTurbinePlaceable

Description
Class for placeable wind turbines
Parent
Placeable
Functions

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
18function WindTurbinePlaceable:new(isServer, isClient, customMt)
19 local mt = customMt;
20 if mt == nil then
21 mt = WindTurbinePlaceable_mt;
22 end;
23
24 local self = Placeable:new(isServer, isClient, mt);
25
26 registerObjectClassName(self, "WindTurbinePlaceable");
27
28 self.rotationNode = 0;
29 self.headNode = 0;
30 self.incomePerHour = 0;
31 return self;
32end;

delete

Description
Deleting placeable wind turbine
Definition
delete()
Code
36function WindTurbinePlaceable:delete()
37 unregisterObjectClassName(self);
38 g_currentMission.environment:removeHourChangeListener(self);
39 WindTurbinePlaceable:superClass().delete(self);
40end;

deleteFinal

Description
Deleting placeable wind turbine
Definition
deleteFinal()
Code
44function WindTurbinePlaceable:deleteFinal()
45 WindTurbinePlaceable:superClass().deleteFinal(self);
46end;

readStream

Description
Called on client side on join
Definition
readStream(integer streamId, table connection)
Arguments
integerstreamIdstream ID
tableconnectionconnection
Code
52function WindTurbinePlaceable:readStream(streamId, connection)
53 if connection:getIsServer() then
54 self.headRotation=Utils.readCompressedAngle(streamId);
55 end;
56 WindTurbinePlaceable:superClass().readStream(self, streamId, connection);
57end;

writeStream

Description
Called on server side on join
Definition
writeStream(integer streamId, table connection)
Arguments
integerstreamIdstream ID
tableconnectionconnection
Code
63function WindTurbinePlaceable:writeStream(streamId, connection)
64 if not connection:getIsServer() then
65 Utils.writeCompressedAngle(streamId, self.headRotation);
66 end;
67 WindTurbinePlaceable:superClass().writeStream(self, streamId, connection);
68end;

createNode

Description
Create node
Definition
createNode(string i3dFilename)
Arguments
stringi3dFilenamei3d file name
Return Values
booleansuccesssuccess
Code
82function WindTurbinePlaceable:createNode(i3dFilename)
83 if not WindTurbinePlaceable:superClass().createNode(self, i3dFilename) then
84 return false;
85 end;
86
87 if getNumOfChildren(self.nodeId) < 1 then
88 delete(self.nodeId);
89 self.nodeId = 0;
90 return false;
91 end;
92 self.headNode = getChildAt(self.nodeId, 0);
93 if getNumOfChildren(self.headNode) < 1 then
94 delete(self.nodeId);
95 self.nodeId = 0;
96 return false;
97 end;
98 self.rotationNode = getChildAt(self.headNode, 0);
99
100 return true;
101end;

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
114function WindTurbinePlaceable:load(xmlFilename, x,y,z, rx,ry,rz, initRandom)
115 if not WindTurbinePlaceable:superClass().load(self, xmlFilename, x,y,z, rx,ry,rz, initRandom) then
116 return false;
117 end;
118
119 return true;
120end;

finalizePlacement

Description
Called if placeable is placed
Definition
finalizePlacement()
Code
124function WindTurbinePlaceable:finalizePlacement()
125 WindTurbinePlaceable:superClass().finalizePlacement(self);
126 g_currentMission.environment:addHourChangeListener(self);
127end

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
138function WindTurbinePlaceable:initPose(x,y,z, rx,ry,rz, initRandom)
139 WindTurbinePlaceable:superClass().initPose(self, x,y,z, rx,ry,rz, initRandom);
140
141 if initRandom == nil or initRandom == true then
142 local rotVariation = 0.2;
143 self.headRotation = 0.7 + math.random() * 2*rotVariation - rotVariation;
144 end;
145 rotate(self.rotationNode, 0,0,math.random()*math.pi*2);
146 self:updateHeadRotation();
147end;

updateHeadRotation

Description
Updating head rotation
Definition
updateHeadRotation()
Code
151function WindTurbinePlaceable:updateHeadRotation()
152 local dx,_,dz = worldDirectionToLocal(self.nodeId, math.sin(self.headRotation),0,math.cos(self.headRotation));
153 setDirection(self.headNode, dx,0,dz, 0,1,0);
154end;

loadFromAttributesAndNodes

Description
Loading from attributes and nodes
Definition
loadFromAttributesAndNodes(integer xmlFile, string key, boolean resetVehicles)
Arguments
integerxmlFileid of xml object
stringkeykey
booleanresetVehiclesreset vehicles
Return Values
booleansuccesssuccess
Code
162function WindTurbinePlaceable:loadFromAttributesAndNodes(xmlFile, key, resetVehicles)
163 local headRotation = getXMLFloat(xmlFile, key.."#headRotation");
164 if headRotation == nil then
165 return false;
166 end;
167
168 self.headRotation = headRotation;
169
170 if not WindTurbinePlaceable:superClass().loadFromAttributesAndNodes(self, xmlFile, key, resetVehicles) then
171 return false;
172 end;
173
174 return true;
175end;

getSaveAttributesAndNodes

Description
Get save attributes and nodes
Definition
getSaveAttributesAndNodes(string nodeIdent)
Arguments
stringnodeIdentnode ident
Return Values
stringattributesattributes
stringnodesnodes
Code
182function WindTurbinePlaceable:getSaveAttributesAndNodes(nodeIdent)
183 local attributes, nodes = WindTurbinePlaceable:superClass().getSaveAttributesAndNodes(self, nodeIdent);
184 attributes = attributes .. ' headRotation="'..self.headRotation..'"';
185 return attributes, nodes;
186end;

update

Description
Update
Definition
update(float dt)
Arguments
floatdttime since last call in ms
Code
191function WindTurbinePlaceable:update(dt)
192 if self.rotationNode ~= 0 then
193 rotate(self.rotationNode, 0,0,-0.0025*dt);
194 end;
195end;

hourChanged

Description
Called if hour changed
Definition
hourChanged()
Code
199function WindTurbinePlaceable:hourChanged()
200 if self.isServer then
201 g_currentMission:addSharedMoney(self.incomePerHour, "propertyIncome");
202 g_currentMission:addMoneyChange(self.incomePerHour, EconomyManager.MONEY_TYPE_PROPERTY_INCOME);
203 end
204end