Skip to main content
Skip to main content

FruitTypeManager

FruitTypeManager

Description

This class handles all fruitTypes and fruitTypeCategories

Parent

AbstractManager

Functions

addFruitTypeCategory

Description

Adds a new fruitType category

Definition

addFruitTypeCategory(string name, boolean isBaseType)

Arguments

stringnamefruit category index name
booleanisBaseTypeif true overwriting of existing category with the same name is prohibited

Return Values

booleancategoryIndexindex of the added category

Code

function FruitTypeManager:addFruitTypeCategory(name, isBaseType)
if not ClassUtil.getIsValidIndexName(name) then
printWarning( "Warning: '" .. tostring(name) .. "' is not a valid name for a fruitTypeCategory.Ignoring fruitTypeCategory!" )
return nil
end

name = string.upper(name)

if isBaseType and self.categories[name] ~ = nil then
printWarning( "Warning:FruitTypeCategory '" .. tostring(name) .. "' already exists.Ignoring fruitTypeCategory!" )
return nil
end

local index = self.categories[name]

if index = = nil then
self.numCategories = self.numCategories + 1
self.categories[name] = self.numCategories
self.indexToCategory[ self.numCategories] = name
self.categoryIndexToFruitTypeIndex[ self.numCategories] = { }
index = self.numCategories
end

return index
end

addFruitTypeConversion

Description

Add fruit type to fill type conversion

Definition

addFruitTypeConversion(integer converter, integer fruitTypeIndex, integer fillTypeIndex, float conversionFactor)

Arguments

integerconverterindex of converter
integerfruitTypeIndexfruit type index
integerfillTypeIndexfill type index
floatconversionFactorfactor of conversion

Code

function FruitTypeManager:addFruitTypeConversion(converter, fruitTypeName, fillTypeName, conversionFactor)
if converter ~ = nil then
if self.fruitTypeConverterLoadData[converter] = = nil then
self.fruitTypeConverterLoadData[converter] = { }
end

table.insert( self.fruitTypeConverterLoadData[converter], { fruitTypeName = fruitTypeName, fillTypeName = fillTypeName, conversionFactor = conversionFactor } )
end
end

addFruitTypeConverter

Description

Adds a new ruit type converter

Definition

addFruitTypeConverter(string name, boolean isBaseType)

Arguments

stringnamename
booleanisBaseTypeif true overwriting existing converter with the same name is prohibited

Return Values

booleanconverterIndexindex of converterIndex

Code

function FruitTypeManager:addFruitTypeConverter(name, isBaseType)
if not ClassUtil.getIsValidIndexName(name) then
printWarning( "Warning: '" .. tostring(name) .. "' is not a valid name for a fruitTypeConverter.Ignoring fruitTypeConverter!" )
return nil
end

name = string.upper(name)

if isBaseType and self.converterNameToIndex[name] ~ = nil then
printWarning( "Warning:FruitTypeConverter '" .. tostring(name) .. "' already exists.Ignoring fruitTypeConverter!" )
return nil
end

local index = self.converterNameToIndex[name]
if index = = nil then
local converter = { }
table.insert( self.fruitTypeConverters, converter)
self.converterNameToIndex[name] = # self.fruitTypeConverters
self.nameToConverter[name] = converter
index = # self.fruitTypeConverters
end

return index
end

addFruitTypeToCategory

Description

Add fruitType to category

Definition

addFruitTypeToCategory(integer fruitTypeIndex, integer categoryIndex)

Arguments

integerfruitTypeIndexindex of fruit type
integercategoryIndexindex of category

Return Values

integersuccesstrue if added else false

Code

function FruitTypeManager:addFruitTypeToCategory(fruitTypeIndex, categoryIndex)
if categoryIndex ~ = nil and fruitTypeIndex ~ = nil then
table.insert( self.categoryIndexToFruitTypeIndex[categoryIndex], fruitTypeIndex)
return true
end
return false
end

addModFoliageType

Description

Add additional foliage types that should be added

Definition

addModFoliageType(string name, string filename)

Arguments

stringnamename of foliage type
stringfilenameabsolute path to the foliage xml

Code

function FruitTypeManager:addModFoliageType(name, filename)
table.insert( self.modFoliageTypesToLoad, { name = name, filename = filename } )
end

getConverterDataByName

Description

Returns converter data by given name

Definition

getConverterDataByName(string converterName)

Arguments

stringconverterNamename of converter

Return Values

stringconverterDataconverter data

Code

function FruitTypeManager:getConverterDataByName(converterName)
return self.nameToConverter[converterName and string.upper(converterName)]
end

getCutHeightByFruitTypeIndex

Description

Definition

getCutHeightByFruitTypeIndex(integer index, boolean? isForageCutter)

Arguments

integerindex
boolean?isForageCutter

Return Values

boolean?cutHeightin m

Code

function FruitTypeManager:getCutHeightByFruitTypeIndex(index, isForageCutter)
--#debug Assert.isInteger(index)
local fruitType = self.indexToFruitType[index]
if isForageCutter then
return(fruitType and(fruitType.forageCutHeight or fruitType.cutHeight)) or 0.15
end

return(fruitType and fruitType.cutHeight) or 0.15
end

getFillTypeByFruitTypeIndex

Description

Definition

getFillTypeByFruitTypeIndex(integer index)

Arguments

integerindex

Return Values

integerfillType

Code

function FruitTypeManager:getFillTypeByFruitTypeIndex(index)
--#debug Assert.isInteger(index)
return self.fruitTypeIndexToFillType[index]
end

getFillTypeIndexByFruitTypeIndex

Description

Definition

getFillTypeIndexByFruitTypeIndex(integer index)

Arguments

integerindex

Return Values

integerfillTypeIndex

Code

function FruitTypeManager:getFillTypeIndexByFruitTypeIndex(index)
--#debug Assert.isInteger(index)
local fillType = self.fruitTypeIndexToFillType[index]
if fillType ~ = nil then
return fillType.index
end
return nil
end

getFillTypeIndicesByFruitTypeCategoryName

Description

Gets a list of fillType indices from string of space separated fruit type category names

Definition

getFillTypeIndicesByFruitTypeCategoryName(string fruitTypeCategoryNames, string? warning)

Arguments

stringfruitTypeCategoryNamesfruit type categories
string?warningwarning if category not found

Return Values

string?fillTypesfill type indices

Code

function FruitTypeManager:getFillTypeIndicesByFruitTypeCategoryName(fruitTypeCategoryNames, warning)
local fillTypes = self:getFillTypesByFruitTypeCategoryNames(fruitTypeCategoryNames, warning)

-- convert to list of indices
for index, fillType in ipairs(fillTypes) do
fillTypes[index] = fillType.index
end

return fillTypes -- indices
end

getFillTypeIndicesByFruitTypeNames

Description

Gets a list of fillType indices from string of space separated fruit type names

Definition

getFillTypeIndicesByFruitTypeNames(string names, string? warning)

Arguments

stringnamesfruit type names
string?warningwarning if fill type not found

Return Values

string?fillTypesfill types

Code

function FruitTypeManager:getFillTypeIndicesByFruitTypeNames(names, warning)
local fillTypes = self:getFillTypesByFruitTypeNames(names, warning)

-- convert to list of indices
for index, fillType in ipairs(fillTypes) do
fillTypes[index] = fillType.index
end

return fillTypes -- indices
end

getFillTypeLiterPerSqm

Description

Get fill type liter per sqm

Definition

getFillTypeLiterPerSqm(integer fillTypeIndex, float defaultValue)

Arguments

integerfillTypeIndexfill type index
floatdefaultValuedefault value if fill type not found

Return Values

floatliterPerSqmliter per sqm

Code

function FruitTypeManager:getFillTypeLiterPerSqm(fillTypeIndex, defaultValue)
--#debug Assert.isInteger(fillTypeIndex)
local fruitType = self.fruitTypes[ self:getFruitTypeIndexByFillTypeIndex(fillTypeIndex)]
if fruitType ~ = nil then
if fruitType.hasWindrow then
return fruitType.windrowLiterPerSqm
else
return fruitType.literPerSqm
end
end
return defaultValue
end

getFillTypeNameByFruitTypeIndex

Description

Definition

getFillTypeNameByFruitTypeIndex(integer index)

Arguments

integerindex

Return Values

integerfillTypeName

Code

function FruitTypeManager:getFillTypeNameByFruitTypeIndex(index)
--#debug Assert.isInteger(index)
local fillTypeIndex = self.fruitTypeIndexToFillType[index]
return fillTypeIndex and fillTypeIndex.name or nil
end

getFillTypesByFruitTypeCategoryNames

Description

Gets a list of fillTypes from string of space separated fruit type category names

Definition

getFillTypesByFruitTypeCategoryNames(string fruitTypeCategoryNames, string? warning)

Arguments

stringfruitTypeCategoryNamesfruit type categories
string?warningwarning if category not found

Return Values

string?fillTypesfill types

Code

function FruitTypeManager:getFillTypesByFruitTypeCategoryNames(fruitTypeCategoryNames, warning)
local fillTypes = { }
local alreadyAdded = { }
local categories = string.split(fruitTypeCategoryNames, " " )
for _, categoryName in pairs(categories) do
categoryName = string.upper(categoryName)
local category = self.categories[categoryName]
if category ~ = nil then
for _, fruitTypeIndex in ipairs( self.categoryIndexToFruitTypeIndex[category]) do
local fillType = self:getFillTypeByFruitTypeIndex(fruitTypeIndex)
if fillType ~ = nil then
if alreadyAdded[fillType] = = nil then
table.insert(fillTypes, fillType)
alreadyAdded[fillType] = true
end
end
end
else
if warning ~ = nil then
printWarning( string.format(warning, categoryName))
end
end
end
return fillTypes
end

getFillTypesByFruitTypeNames

Description

Gets a list of fillType from string with fruit type names

Definition

getFillTypesByFruitTypeNames(string names, string? warning)

Arguments

stringnamesfruit type names
string?warningwarning if fill type not found

Return Values

string?fillTypesfill types

Code

function FruitTypeManager:getFillTypesByFruitTypeNames(names, warning)
local fillTypes = { }
local alreadyAdded = { }
local fruitTypeNames = string.split(names, " " )
for _, name in pairs(fruitTypeNames) do
local fillType = nil
local fruitType = self:getFruitTypeByName(name)
if fruitType ~ = nil then
fillType = self:getFillTypeByFruitTypeIndex(fruitType.index)
end
if fillType ~ = nil then
if alreadyAdded[fillType] = = nil then
table.insert(fillTypes, fillType)
alreadyAdded[fillType] = true
end
else
if warning ~ = nil then
printWarning( string.format(warning, name))
end
end
end

return fillTypes
end

getFruitTypeByFillTypeIndex

Description

Definition

getFruitTypeByFillTypeIndex(integer index)

Arguments

integerindex

Return Values

integerfruitType

Code

function FruitTypeManager:getFruitTypeByFillTypeIndex(index)
--#debug Assert.isInteger(index)
return self.fruitTypes[ self.fillTypeIndexToFruitTypeIndex[index]]
end

getFruitTypeByIndex

Description

Gets a fruitType by index

Definition

getFruitTypeByIndex(integer index)

Arguments

integerindexthe fruit index

Return Values

integerfruitthe fruit object

Code

function FruitTypeManager:getFruitTypeByIndex(index)
return self.indexToFruitType[index]
end

getFruitTypeByName

Description

Gets a fruitType by index name

Definition

getFruitTypeByName(string name)

Arguments

stringnamethe fruit index name

Return Values

stringfruitTypethe fruit object

Code

function FruitTypeManager:getFruitTypeByName(name)
if name = = nil then
return nil
end

return self.nameToFruitType[ string.upper(name)]
end

getFruitTypeIndexByFillTypeIndex

Description

Definition

getFruitTypeIndexByFillTypeIndex(integer index)

Arguments

integerindex

Return Values

integerfruitTypeIndex

Code

function FruitTypeManager:getFruitTypeIndexByFillTypeIndex(index)
--#debug Assert.isInteger(index)
return self.fillTypeIndexToFruitTypeIndex[index]
end

getFruitTypeIndicesByCategoryNames

Description

Gets a list of fruitTypesIndices of the given category names

Definition

getFruitTypeIndicesByCategoryNames(string names, string warning)

Arguments

stringnamesstring of space separated fruitType category index names
stringwarninga warning text shown if a category is not found

Return Values

stringfruitTypesIndiceslist of fruitTypeIndices

Code

function FruitTypeManager:getFruitTypeIndicesByCategoryNames(names, warning)
local fruitTypes = self:getFruitTypesByCategoryNames(names, warning)

-- convert to list of indices
for index, fruitType in ipairs(fruitTypes) do
fruitTypes[index] = fruitType.index
end

return fruitTypes -- indices
end

getFruitTypeIndicesByNames

Description

Gets list of fruitType indices from string with space separated fruit type names

Definition

getFruitTypeIndicesByNames(string names, string? warning)

Arguments

stringnamesstring of space separated fruit type names
string?warningwarning if fruit type not found

Return Values

string?fruitTypeIndicesarray of fruit type indices

Code

function FruitTypeManager:getFruitTypeIndicesByNames(names, warning)
local fruitTypes = self:getFruitTypesByNames(names, warning)

-- convert to list of indices
for index, fruitType in ipairs(fruitTypes) do
fruitTypes[index] = fruitType.index
end

return fruitTypes -- indices
end

getFruitTypeNameByIndex

Description

Definition

getFruitTypeNameByIndex(integer index)

Arguments

integerindex

Return Values

integerfruitTypeName

Code

function FruitTypeManager:getFruitTypeNameByIndex(index)
local fruitType = self.indexToFruitType[index]
if fruitType ~ = nil then
return fruitType.name
end

if index = = FruitType.UNKNOWN then
return "UNKNOWN"
end

return nil
end

getFruitTypes

Description

Gets a list of fruitTypes

Definition

getFruitTypes()

Return Values

integerfruitTypesa list of fruitTypes

Code

function FruitTypeManager:getFruitTypes()
return self.fruitTypes
end

getFruitTypesByCategoryNames

Description

Gets an array of fruitTypes (tables/objects) of the given category names

Definition

getFruitTypesByCategoryNames(string names, string warning)

Arguments

stringnamesstring of space separated fruitType category index names
stringwarninga warning text shown if a category is not found

Return Values

stringfruitTypesarray of fruitTypes (tables/objects)

Code

function FruitTypeManager:getFruitTypesByCategoryNames(names, warning)
local fruitTypes = { }
local alreadyAdded = { }
local categories = string.split(names, " " )
for _, categoryName in pairs(categories) do
categoryName = string.upper(categoryName)
local categoryIndex = self.categories[categoryName]
local categoryFruitTypeIndices = self.categoryIndexToFruitTypeIndex[categoryIndex]
if categoryFruitTypeIndices ~ = nil then
for _, fruitTypeIndex in ipairs(categoryFruitTypeIndices) do
local fruitType = self.indexToFruitType[fruitTypeIndex]
if alreadyAdded[fruitType] = = nil then
table.insert(fruitTypes, fruitType)
alreadyAdded[fruitType] = true
end
end
else
if warning ~ = nil then
printWarning( string.format(warning, categoryName))
end
end
end
return fruitTypes
end

getFruitTypesByNames

Description

Gets list of fruitTypes (tables/objects) from string with fruit type names

Definition

getFruitTypesByNames(string names, string? warning)

Arguments

stringnamesstring of space separated fruit type names
string?warningwarning if fruit type not found

Return Values

string?fruitTypesarray of fruit types (tables/objects)

Code

function FruitTypeManager:getFruitTypesByNames(names, warning)
local fruitTypes = { }
local alreadyAdded = { }
local fruitTypeNames = string.split(names, " " )
for _, name in pairs(fruitTypeNames) do
name = string.upper(name)
local fruitType = self.nameToFruitType[name]
if fruitType ~ = nil then
if alreadyAdded[fruitType] = = nil then
table.insert(fruitTypes, fruitType)
alreadyAdded[fruitType] = true
end
else
if warning ~ = nil then
printWarning( string.format(warning, name))
end
end
end

return fruitTypes
end

getWindrowFillTypeIndexByFruitTypeIndex

Description

Definition

getWindrowFillTypeIndexByFruitTypeIndex(integer index)

Arguments

integerindex

Return Values

integerwindrowFillTypeIndex

Code

function FruitTypeManager:getWindrowFillTypeIndexByFruitTypeIndex(index)
--#debug Assert.isInteger(index)
return self.fruitTypeIndexToWindrowFillTypeIndex[index]
end

initDataStructures

Description

Initialize data structures

Definition

initDataStructures()

Code

function FruitTypeManager:initDataStructures()
self.fruitTypes = { }
self.indexToFruitType = { }
self.nameToIndex = { }
self.nameToFruitType = { }
self.fruitTypeIndexToFillType = { }
self.fillTypeIndexToFruitTypeIndex = { }
self.densityTypeIndexToFruitType = { }
self.filenameToFruitType = { }

self.fruitTypeConverters = { }
self.fruitTypeConverterLoadData = { }
self.converterNameToIndex = { }
self.nameToConverter = { }

self.windrowFillTypes = { }
self.fruitTypeIndexToWindrowFillTypeIndex = { }
self.windrowCutFillTypeIndexToFruitTypeIndex = { }

self.modFoliageTypesToLoad = { }

self.numCategories = 0
self.categories = { }
self.indexToCategory = { }
self.categoryIndexToFruitTypeIndex = { }

FruitType = self.nameToIndex
FruitType.UNKNOWN = 0
FruitTypeCategory = self.categories
FruitTypeConverter = self.converterNameToIndex

self.defaultDataPlaneId = nil
end

isFillTypeWindrow

Description

Definition

isFillTypeWindrow(integer index)

Arguments

integerindex

Return Values

integerisFillTypeWindrow

Code

function FruitTypeManager:isFillTypeWindrow(index)
if index ~ = nil then
--#debug Assert.isInteger(index)
return self.windrowFillTypes[index] = = true
end
return false
end

loadDefaultTypes

Description

Definition

loadDefaultTypes()

Code

function FruitTypeManager:loadDefaultTypes()
local xmlFile = loadXMLFile( "fuitTypes" , "data/maps/maps_fruitTypes.xml" )
self:loadFruitTypes(xmlFile, nil , "" , true )
delete(xmlFile)
end

loadFruitTypes

Description

Loads fruitTypes

Definition

loadFruitTypes(entityId xmlFileHandle, table missionInfo, string? baseDirectory, boolean? isBaseType)

Arguments

entityIdxmlFileHandle
tablemissionInfo
string?baseDirectory
boolean?isBaseType

Return Values

boolean?successsuccess

Code

function FruitTypeManager:loadFruitTypes(xmlFileHandle, missionInfo, baseDirectory, isBaseType)
local xmlFile = XMLFile.wrap(xmlFileHandle)
local rootName = xmlFile:getRootName()

local maxNumFruitTypes = 2 ^ FruitTypeManager.SEND_NUM_BITS - 1

for _, key in xmlFile:iterator(rootName .. ".fruitTypes.fruitType" ) do
if # self.fruitTypes > = maxNumFruitTypes then
Logging.xmlError(xmlFile, "FruitTypeManager.loadFruitTypes:too many fruit types.Only %d fruit types are supported" , maxNumFruitTypes)
break
end

local filename = xmlFile:getString(key .. "#filename" )
if filename ~ = nil then
filename = Utils.getFilename(filename, baseDirectory)

local fruitTypeDesc = FruitTypeDesc.new()
if fruitTypeDesc:loadFromFoliageXMLFile(filename) then
if self.modFruitTypes ~ = nil then
for k, modFruitTypeDesc in ipairs( self.modFruitTypes) do
if modFruitTypeDesc.name = = fruitTypeDesc.name then
-- delete default fruitType
fruitTypeDesc:delete()

-- use mod fruitType
fruitTypeDesc = modFruitTypeDesc

table.remove( self.modFruitTypes, k)
break
end
end
end

self:addFruitType(fruitTypeDesc)
end
else
Logging.xmlWarning(xmlFile, "FruitTypeManager.loadFruitTypes:Missing fruit type filename for '%s'" , key)
end
end

if self.modFruitTypes ~ = nil then
for _, modFruitTypeDesc in ipairs( self.modFruitTypes) do
self:addFruitType(modFruitTypeDesc)
end
end
self.modFruitTypes = nil

self:loadCategoriesFromXML(xmlFile, rootName, isBaseType)
self:loadConvertersFromXML(xmlFile, rootName, isBaseType)

xmlFile:delete()
end

loadMapData

Description

Load data on map load

Definition

loadMapData(entityId xmlFileHandle, table missionInfo, string? baseDirectory)

Arguments

entityIdxmlFileHandle
tablemissionInfo
string?baseDirectory

Return Values

string?trueif loading was successful else false

Code

function FruitTypeManager:loadMapData(xmlFileHandle, missionInfo, baseDirectory)
FruitTypeManager:superClass().loadMapData( self )

self.modFruitTypes = { }
XMLUtil.loadDataFromMapXML(xmlFileHandle, "fruitTypes" , baseDirectory, self , self.loadMapFruitTypes, missionInfo, baseDirectory)

self:loadDefaultTypes()

XMLUtil.loadDataFromMapXML(xmlFileHandle, "fruitTypes" , baseDirectory, self , self.loadMapCategoriesAndConverters, missionInfo, baseDirectory)

-- initialize the fruit type converters after all fruit types have been loaded
self:initializeFruitTypeConverters()

return true
end

new

Description

Creating manager

Definition

new(table? customMt)

Arguments

table?customMt

Return Values

table?self

Code

function FruitTypeManager.new(customMt)
local self = AbstractManager.new(customMt or FruitTypeManager _mt)

addConsoleCommand( "gsFruitTypesExportStats" , "Exports the fruit type stats into a text file" , "consoleCommandExportStats" , self )

return self
end