LUADOC - Farming Simulator 17

Printable Version

HandTool

Description
Class for handtools
Functions

registerHandTool

Description
Register handtool type
Definition
registerHandTool(string typeName, table classObject)
Arguments
stringtypeNamename of new type
tableclassObjectclass object

new

Description
Creating handtool object
Definition
new(boolean isServer, boolean isClient, table customMt)
Arguments
booleanisServeris server
booleanisClientis client
tablecustomMtcustom metatable
Return Values
tableinstanceInstance of object
Code
30function HandTool:new(isServer, isClient, customMt)
31 local mt = customMt;
32 if mt == nil then
33 mt = HandTool_mt;
34 end;
35
36 local self = Object:new(isServer, isClient, mt);
37 self.static = true;
38 self.owner = nil;
39 self.price = 0;
40 self.age = 0;
41 return self;
42end;

load

Description
Load chainsaw from xml file
Definition
load(string xmlFilename, table player)
Arguments
stringxmlFilenamexml file name
tableplayerplayer
Return Values
booleansuccesssuccess
Code
49function HandTool:load(xmlFilename, player)
50 self.configFileName = xmlFilename;
51
52 self.customEnvironment, self.baseDirectory = Utils.getModNameAndBaseDirectory(xmlFilename);
53
54 local xmlFile = loadXMLFile("TempXML", xmlFilename);
55 if xmlFile == 0 then
56 return false;
57 end;
58 local i3dFilename = getXMLString(xmlFile, "handTool.filename");
59 self.position = Utils.getVectorNFromString(Utils.getNoNil(getXMLString(xmlFile, "handTool.position#value"), "0 0 0"), 3);
60 self.rotation = Utils.getRadiansFromString(Utils.getNoNil(getXMLString(xmlFile, "handTool.rotation#value"), "0 0 0"), 3);
61
62 if i3dFilename == nil then
63 delete(xmlFile);
64 return false;
65 end;
66 self.i3dFilename = Utils.getFilename(i3dFilename, self.baseDirectory);
67
68 local node = Utils.loadSharedI3DFile(self.i3dFilename);
69 self.rootNode = getChildAt(node, 0);
70 self.player = player;
71
72 local storeItem = StoreItemsUtil.storeItemsByXMLFilename[self.configFileName:lower()]
73 if self.price == 0 or self.price == nil then
74 self.price = StoreItemsUtil.getDefaultPrice(storeItem);
75 end
76
77 if g_currentMission ~= nil and storeItem.canBeSold then
78 g_currentMission.environment:addDayChangeListener(self)
79 end
80
81 self.targets = {};
82 IKUtil.loadIKChainTargets(xmlFile, "handTool.targets", self.rootNode, self.targets);
83
84 link(player.toolsRootNode, self.rootNode);
85 setTranslation(self.rootNode, self.position[1], self.position[2], self.position[3]);
86 setRotation(self.rootNode, self.rotation[1], self.rotation[2], self.rotation[3]);
87
88 delete(node);
89 delete(xmlFile);
90 setVisibility(self.rootNode, false);
91
92 if player.isOwner then
93 g_currentMission:addHandTool(self);
94 end;
95
96 return true;
97end;

readStream

Description
Called on client side on join
Definition
readStream(integer streamId, table connection, table player)
Arguments
integerstreamIdstream ID
tableconnectionconnection
tableplayerplayer
Code
107function HandTool:readStream(streamId, connection, player)
108 local filename = Utils.convertFromNetworkFilename(streamReadString(streamId));
109 self:load(filename, player);
110 self.age = streamReadUInt16(streamId);
111 self.price = streamReadUInt16(streamId);
112end;

writeStream

Description
Called on server side on join
Definition
writeStream(integer streamId, table connection)
Arguments
integerstreamIdstream ID
tableconnectionconnection
Code
118function HandTool:writeStream(streamId, connection)
119 streamWriteString(streamId, Utils.convertToNetworkFilename(self.configFileName));
120 streamWriteUInt16(streamId, self.age);
121 streamWriteUInt16(streamId, self.price);
122end;

delete

Description
Deleting handtool
Definition
delete()
Code
126function HandTool:delete()
127 if g_currentMission ~= nil then
128 g_currentMission.environment:removeDayChangeListener(self)
129 end
130 if self.player.isOwner then
131 g_currentMission:removeHandTool(self);
132 end;
133 if self.rootNode ~= nil and self.rootNode ~= 0 then
134 Utils.releaseSharedI3DFile(self.i3dFilename, nil, true);
135 delete(self.rootNode);
136 end;
137end;

onActivate

Description
On activate
Definition
onActivate(boolean allowInput)
Arguments
booleanallowInputallow input
Code
151function HandTool:onActivate(allowInput)
152 setVisibility(self.rootNode, true);
153end;

onDeactivate

Description
On deactivate
Definition
onDeactivate(boolean allowInput)
Arguments
booleanallowInputallow input
Code
158function HandTool:onDeactivate(allowInput)
159 setVisibility(self.rootNode, false);
160end;

getDailyUpKeep

Description
Get daily up keep
Definition
getDailyUpKeep()
Return Values
floatdailyUpKeepdaily up keep
Code
175function HandTool:getDailyUpKeep()
176 local storeItem = StoreItemsUtil.storeItemsByXMLFilename[self.configFileName:lower()]
177 local multiplier = 1
178 if storeItem.lifetime ~= nil and storeItem.lifetime ~= 0 then
179 local ageMultiplier = math.min(self.age/storeItem.lifetime, 1)
180 multiplier = EconomyManager.MAX_DAILYUPKEEP_MULTIPLIER * ageMultiplier
181 end
182 return StoreItemsUtil.getDailyUpkeep(storeItem, nil) * multiplier
183end

getSellPrice

Description
Get sell price
Definition
getSellPrice()
Return Values
floatsellPricesell price
Code
188function HandTool:getSellPrice()
189 local priceMultiplier = 0.5
190 local maxVehicleAge = StoreItemsUtil.storeItemsByXMLFilename[self.configFileName:lower()].lifetime;
191
192 if maxVehicleAge ~= nil and maxVehicleAge ~= 0 then
193 priceMultiplier = priceMultiplier * math.exp(-3.5 * math.min(self.age/maxVehicleAge, 1))
194 end
195
196 return math.floor(self.price * math.max(priceMultiplier, 0.05));
197end

dayChanged

Description
Called if day changed
Definition
dayChanged()
Code
201function HandTool:dayChanged()
202 self.age = self.age + 1;
203end