Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
scfmod committed Dec 31, 2021
0 parents commit e739888
Show file tree
Hide file tree
Showing 47 changed files with 3,583 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.idea
.settings/
.vscode/*
.DS_Store
216 changes: 216 additions & 0 deletions classes/MachineConfiguration.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
local modFolder = g_currentModDirectory

---@class MachineConfiguration
---@field object table
---@field name string
---@field filePath string
---@field terraformNodes table<number, number>
---@field collisionNodes table<number, number>
---@field paintNodes table<number, number>
---@field i3dNode number
---@field rootNode number
---@field availableModes table<number, number>
---@field availableModeByIndex table<number, boolean>
---@field availableModeByName table<string, boolean>
MachineConfiguration = {}
local MachineConfiguration_mt = Class(MachineConfiguration)

function MachineConfiguration.new(object, name, filePath, mt)
---@type MachineConfiguration
local self = setmetatable( {}, mt or MachineConfiguration_mt)

self.object = object
self.name = name
self.filePath = filePath

self.terraformNodes = {}
self.collisionNodes = {}
self.paintNodes = {}

self.availableModes = {}
self.availableModeByIndex = {}
self.availableModeByName = {}

return self
end

function MachineConfiguration.getXMLFilePath(name, typeName)
if not typeName then
Logging.warning('MachineConfiguration.getXMLFilePath: typeName is nil')
return
end

local fileName = typeName .. '_' .. name .. '.xml'
-- Logging.info('Looking for ' .. fileName)
local filePath = g_modSettingsDirectory .. 'TerraFarm/configurations/' .. fileName

if not fileExists(filePath) then
filePath = modFolder .. 'configurations/' .. fileName
if not fileExists(filePath) then
return
end
end

return filePath
end

function MachineConfiguration:save()
-- TODO: later.. LOW PRIO
end

function MachineConfiguration:load()
local xmlFile = loadXMLFile(self.name .. '_config', self.filePath)
if xmlFile == nil or xmlFile == 0 then
Logging.warning('MachineConfiguration: Failed to read configuration file - ' .. self.filePath)
return false
end

if not self:loadRootNode(xmlFile) then
return false
end

self:loadTerraformNodes(xmlFile)
self:loadCollisionNodes(xmlFile)
self:loadPaintNodes(xmlFile)
self:loadSettings(xmlFile)

delete(xmlFile)

return true
end

function MachineConfiguration:loadRootNode(xmlFile)
local nodePath = getXMLString(xmlFile, 'configuration.i3d#rootNodePath')
self.i3dNode = I3DUtil.indexToObject(self.object.components,nodePath, self.object.i3dMappings)

if not self.i3dNode then
Logging.error('MachineConfiguration: Failed to find i3d from configuration - ' .. tostring(nodePath))
Logging.info(self.filePath)
return
end

self.rootNode = createTransformGroup('terraformRootNode')
link(self.i3dNode, self.rootNode)

setTranslation(self.rootNode,
Utils.getNoNil(getXMLFloat(xmlFile, 'configuration.i3d#x'), 0),
Utils.getNoNil(getXMLFloat(xmlFile, 'configuration.i3d#y'), 0),
Utils.getNoNil(getXMLFloat(xmlFile, 'configuration.i3d#z'), 0)
)

return true
end

function MachineConfiguration:loadTerraformNodes(xmlFile)
local parentNode = createTransformGroup('terraformNodes')
link(self.rootNode, parentNode)

setTranslation(parentNode,
Utils.getNoNil(getXMLFloat(xmlFile, 'configuration.i3d.terraformNodes#x'), 0),
Utils.getNoNil(getXMLFloat(xmlFile, 'configuration.i3d.terraformNodes#y'), 0),
Utils.getNoNil(getXMLFloat(xmlFile, 'configuration.i3d.terraformNodes#z'), 0)
)

local i = 0
while true do
local key = string.format('configuration.i3d.terraformNodes.node(%d)', i)
if not hasXMLProperty(xmlFile, key) then
break
end
local node = createTransformGroup('terraformNode' .. i)
link(parentNode, node)
setTranslation(node,
Utils.getNoNil(getXMLFloat(xmlFile, key .. '#x'), 0),
Utils.getNoNil(getXMLFloat(xmlFile, key .. '#y'), 0),
Utils.getNoNil(getXMLFloat(xmlFile, key .. '#z'), 0)
)
table.insert(self.terraformNodes, node)
i = i + 1
end
end

function MachineConfiguration:loadCollisionNodes(xmlFile)
local parentNode = createTransformGroup('collisionNodes')
link(self.rootNode, parentNode)

setTranslation(parentNode,
Utils.getNoNil(getXMLFloat(xmlFile, 'configuration.i3d.collisionNodes#x'), 0),
Utils.getNoNil(getXMLFloat(xmlFile, 'configuration.i3d.collisionNodes#y'), 0),
Utils.getNoNil(getXMLFloat(xmlFile, 'configuration.i3d.collisionNodes#z'), 0)
)

local i = 0
while true do
local key = string.format('configuration.i3d.collisionNodes.node(%d)', i)
if not hasXMLProperty(xmlFile, key) then
break
end
local node = createTransformGroup('collisionNode' .. i)
link(parentNode, node)
setTranslation(node,
Utils.getNoNil(getXMLFloat(xmlFile, key .. '#x'), 0),
Utils.getNoNil(getXMLFloat(xmlFile, key .. '#y'), 0),
Utils.getNoNil(getXMLFloat(xmlFile, key .. '#z'), 0)
)
table.insert(self.collisionNodes, node)
i = i + 1
end
end

function MachineConfiguration:loadPaintNodes(xmlFile)
local parentNode = createTransformGroup('paintNodes')
link(self.rootNode, parentNode)

setTranslation(parentNode,
Utils.getNoNil(getXMLFloat(xmlFile, 'configuration.i3d.paintNodes#x'), 0),
Utils.getNoNil(getXMLFloat(xmlFile, 'configuration.i3d.paintNodes#y'), 0),
Utils.getNoNil(getXMLFloat(xmlFile, 'configuration.i3d.paintNodes#z'), 0)
)

local i = 0
while true do
local key = string.format('configuration.i3d.paintNodes.node(%d)', i)
if not hasXMLProperty(xmlFile, key) then
break
end
local node = createTransformGroup('paintNode' .. i)
link(parentNode, node)
setTranslation(node,
Utils.getNoNil(getXMLFloat(xmlFile, key .. '#x'), 0),
Utils.getNoNil(getXMLFloat(xmlFile, key .. '#y'), 0),
Utils.getNoNil(getXMLFloat(xmlFile, key .. '#z'), 0)
)
table.insert(self.paintNodes, node)
i = i + 1
end
end

function MachineConfiguration:loadSettings(xmlFile)
self.terraformStrength = getXMLFloat(xmlFile, 'configuration.setttings.terraformStrength')
self.terraformRadius = getXMLFloat(xmlFile, 'configuration.setttings.terraformRadius')
self.terraformPaintRadius = getXMLFloat(xmlFile, 'configuration.setttings.terraformPaintRadius')
self.terraformSmoothStrength = getXMLFloat(xmlFile, 'configuration.setttings.terraformSmoothStrength')
self.terraformSmoothRadius = getXMLFloat(xmlFile, 'configuration.setttings.terraformSmoothRadius')
self.terraformFlattenStrength = getXMLFloat(xmlFile, 'configuration.setttings.terraformFlattenStrength')
self.terraformFlattenRadius = getXMLFloat(xmlFile, 'configuration.setttings.terraformFlattenRadius')

local i = 0
while true do
local key = string.format('configuration.settings.availableModes.mode(%d)', i)
if not hasXMLProperty(xmlFile, key) then
break
end
local modeName = getXMLString(xmlFile, key)
if modeName then
local mode = g_terraFarm.NAME_TO_MODE[modeName]
if mode then
table.insert(self.availableModes, mode)
self.availableModeByIndex[mode] = true
self.availableModeByName[modeName] = true
else
Logging.warning('MachineConfiguration.loadSettings: unknown mode - ' .. tostring(mode))
end
end
i = i + 1
end
end
49 changes: 49 additions & 0 deletions classes/MachineTypes.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---@class MachineTypes
---@field _typeCount number
---@field typeToClass table<number, table>
---@field classToType table<table, number>
---@field nameToClass table<string, table>
---@field typeToString table<number, string>
---@field stringToType table<string, number>
---@field stringToClass table<string, table>
MachineTypes = {}
local MachineTypes_mt = Class(MachineTypes)

---@return MachineTypes
function MachineTypes.new()
---@type MachineTypes
local self = setmetatable({}, MachineTypes_mt)

self._typeCount = 0

self.typeToClass = {}
self.classToType = {}
self.nameToClass = {}
self.typeToString = {}
self.stringToType = {}
self.stringToClass = {}

return self
end

function MachineTypes:registerType(class, name)
if self.classToType[class] ~= nil then
Logging.error('MachineTypes.registertype: Duplicate type ' .. name)
end

self._typeCount = self._typeCount + 1
local index = self._typeCount

self.typeToClass[index] = class
self.classToType[class] = index
self.nameToClass[name] = class
self.typeToString[index] = name
self.stringToType[name] = index
self.stringToClass[name] = class

class.machineType = index
class.machineTypeName = name
end

---@diagnostic disable-next-line: lowercase-global
g_machineTypes = MachineTypes.new()
Loading

0 comments on commit e739888

Please sign in to comment.