Module:BiomesData

From Against the Storm Official Wiki

Documentation for this module may be created at Module:BiomesData/doc

---@class BiomesData
local BiomesData = {}



--region Dependencies

local JsonUtils = require("Module:JsonUtils")

--endregion



--region Private constants

local DATA_FILE = "Module:BiomesData/Biomes.json"

local SCHEMA = {
    ID = "id",
    ICON = "iconName",
    DLC_REQUIRED = "dlc",
    NAME = "displayName",
    DESCRIPTION = "description",

    SOIL_GRADE = "soilGrade",
    TOWN_TYPE = "townTypeName",
    WANTED_GOODS_IDS_ARRAY = "wantedGoods",

    TREES_IDS_ARRAY = "trees",
    DEPOSITS_IDS_ARRAY = "deposits",
    ORE_IDS_ARRAY = "ore",

    EFFECTS_IDS_ARRAY = "effects",
}

--endregion



--region Localization string constants

--endregion




--region Private member variables

local biomesData

--endregion



--region Private methods

 local function loadData()

    if not biomesData then

        local newData = JsonUtils.convertJSONToLuaTable(DATA_FILE)

        --Promote the ID to be the key of the main data table.
        biomesData = {}
        for _, biome in ipairs(newData) do
            biomesData[biome[SCHEMA.ID]] = biome
        end
    end
end

local function getBiomeFieldByID(biomeID, fieldName)

    loadData()
    local biome = biomesData[biomeID]
    if not biome then
        return nil
    else
        return biome[fieldName]
    end
end

--endregion




--region Public methods

---@public
---Gets an array of all IDs of biomes in the data.
---
---@return table an array of IDs
function BiomesData.getAllBiomeIDs()

    loadData()

    local biomeIDs = {}
    for id, _ in pairs(biomesData) do
        table.insert(biomeIDs, id)
    end
    return biomeIDs
end



---@public
---Gets the icon filename for the specified biome.
---
---@param biomeID string
---@return string the icon filename
function BiomesData.getIcon(biomeID)
    return getBiomeFieldByID(biomeID, SCHEMA.ICON)
end



---@public
-----Gets the name of the specified biome.
-----
-----@param biomeID string
-----@return string the display name
function BiomesData.getName(biomeID)
    return getBiomeFieldByID(biomeID, SCHEMA.NAME)
end



---@public
---Gets the description of the specified biome.
---
---@param biomeID string
---@return string description
function BiomesData.getDescription(biomeID)
    return getBiomeFieldByID(biomeID, SCHEMA.DESCRIPTION)
end



---@public
---Checks whether the specified deposit is in the specified biome.
---
---@param depositID string ID of a deposit
---@param biomeID string
---@return boolean true if the deposit is in the biome
function BiomesData.isDepositInBiome(depositID, biomeID)

    local depositsArray = getBiomeFieldByID(biomeID, SCHEMA.DEPOSITS_IDS_ARRAY)
    for _, id in ipairs(depositsArray) do
        if id == depositID then
            return true
        end
    end
    return false
end



---@public
---Checks whether the specified tree is in the specified biome.
---
---@param treeID string ID of a tree
---@param biomeID string
---@return boolean true if the tree is in the biome
function BiomesData.isTreeInBiome(treeID, biomeID)
    local treesArray = getBiomeFieldByID(biomeID, SCHEMA.TREES_IDS_ARRAY)
    for _, id in ipairs(treesArray) do
        if id == treeID then
            return true
        end
    end
    return false
end



---@public
---Checks whether the specified ore is in the specified biome.
---
---@param oreID string ID of a tree
---@param biomeID string
---@return boolean true if the ore is in the biome
function BiomesData.isOreInBiome(oreID, biomeID)
    local oresArray = getBiomeFieldByID(biomeID, SCHEMA.ORE_IDS_ARRAY)
    for _, id in ipairs(oresArray) do
        if id == oreID then
            return true
        end
    end
    return false
end



---@public
---Shortcut method if you don't know the type of node, but need to check all three (deposits, trees, ores) to see if the ID is in the specified biome.
---
---@param nodeID string ID of a node of some type
---@param biomeID string
---@return boolean true if the specified node of some type is in the biome
function BiomesData.isNodeOfAnyTypeInBiome(nodeID, biomeID)

    local found = BiomesData.isDepositInBiome(nodeID, biomeID)
    if found then
        return true
    end
    found = BiomesData.isTreeInBiome(nodeID, biomeID)
    if found then
        return true
    end
    found = BiomesData.isOreInBiome(nodeID, biomeID)
    if found then
        return true
    end
    return false
end

--endregion

return BiomesData