LUADOC - Farming Simulator 19

Farmland

Description
This class wraps all farmland data
XML Configuration Parameters
farmland#namestring Name of the farmland
farmland#areaInHafloat [optional, default=2.5] Area of farmland in hectares
farmland#pricefloat [optional, default=<calculated by area>] Fixed price of farmland
farmland#priceFactorfloat [optional, default=1.0] Price modification factor used in price calculations
farmland#npcIndexint [optional, default=<random NPC>] Fixed NPC assignment by index
farmland#defaultFarmPropertybool [optional, default=false] If true, this farmland is owned by the player in a new game (on easy difficulty)

Functions

delete

Description
Delete field definition object
Definition
delete()
Code
78function Farmland:delete()
79end

load

Description
Load farmland data from xml
Definition
load(integer xmlFile, string key)
Arguments
integerxmlFilehandle of xml file
stringkeycurrent xml element key
Return Values
booleantrueif loading was successful else false
Code
39function Farmland:load(xmlFile, key)
40 self.id = getXMLInt(xmlFile, key.."#id")
41
42 if self.id == nil or self.id == 0 then
43 print("Error: Invalid farmland id '"..tostring(self.id).."'!")
44 return false
45 end
46
47 self.name = Utils.getNoNil(getXMLString(xmlFile, key.."#name"), "")
48 self.areaInHa = Utils.getNoNil(getXMLFloat(xmlFile, key.."#areaInHa"), 2.5)
49
50 self.fixedPrice = getXMLFloat(xmlFile, key.."#price")
51 if self.fixedPrice == nil then
52 self.priceFactor = Utils.getNoNil(getXMLFloat(xmlFile, key.."#priceScale"), 1)
53 end
54 self.price = self.fixedPrice or 1
55
56 self:updatePrice()
57
58 local npc = g_npcManager:getNPCByIndex(getXMLInt(xmlFile, key.."#npcIndex"))
59 self.npcIndex = g_npcManager:getRandomIndex()
60 if npc ~= nil then
61 self.npcIndex = npc.index
62 end
63
64 -- Names are used with custom NPC sets
65 local npc = g_npcManager:getNPCByName(getXMLString(xmlFile, key.."#npcName"))
66 if npc ~= nil then
67 self.npcIndex = npc.index
68 end
69
70 self.isOwned = false
71 self.defaultFarmProperty = Utils.getNoNil(getXMLBool(xmlFile, key.."#defaultFarmProperty"), false)
72
73 return true
74end

new

Description
Create field definition object
Definition
new()
Return Values
tableinstanceInstance of object
Code
24function Farmland:new(customMt)
25 local self = setmetatable({}, customMt or Farmland_mt)
26
27 self.isOwned = false
28 self.xWorldPos = 0
29 self.zWorldPos = 0
30
31 return self
32end

setArea

Description
Set farmland area
Definition
setArea(float areaInHa)
Arguments
floatareaInHafarmland size in ha
Code
92function Farmland:setArea(areaInHa)
93 self.areaInHa = areaInHa
94 if self.fixedPrice == nil then
95 self:updatePrice()
96 end
97end

setFarmlandIndicatorPosition

Description
Set farmland area indicator world position
Definition
setFarmlandIndicatorPosition(float xWorldPos, float zWorldPos)
Arguments
floatxWorldPosfarmland indicator x world position
floatzWorldPosfarmland size in ha
Code
85function Farmland:setFarmlandIndicatorPosition(xWorldPos, zWorldPos)
86 self.xWorldPos, self.zWorldPos = xWorldPos, zWorldPos
87end