LUADOC - Farming Simulator 22

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
80function Farmland:delete()
81end

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 self.npcIndex = g_npcManager:getRandomIndex()
59
60 local npcByIndex = g_npcManager:getNPCByIndex(getXMLInt(xmlFile, key.."#npcIndex"))
61 if npcByIndex ~= nil then
62 self.npcIndex = npcByIndex.index
63 else
64 -- Names are used with custom NPC sets
65 local npcByName = g_npcManager:getNPCByName(getXMLString(xmlFile, key.."#npcName"))
66 if npcByName ~= nil then
67 self.npcIndex = npcByName.index
68 end
69 end
70
71 self.isOwned = false
72 self.showOnFarmlandsScreen = Utils.getNoNil(getXMLBool(xmlFile, key.."#showOnFarmlandsScreen"), true)
73 self.defaultFarmProperty = Utils.getNoNil(getXMLBool(xmlFile, key.."#defaultFarmProperty"), false)
74
75 return true
76end

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
94function Farmland:setArea(areaInHa)
95 self.areaInHa = areaInHa
96 if self.fixedPrice == nil then
97 self:updatePrice()
98 end
99end

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
87function Farmland:setFarmlandIndicatorPosition(xWorldPos, zWorldPos)
88 self.xWorldPos, self.zWorldPos = xWorldPos, zWorldPos
89end