Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpringBoard-ZK replacement candidate #423

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
37 changes: 37 additions & 0 deletions scen_edit/command/export_metal_spot_config_command.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
ExportMetalSpotConfigCommand = Command:extends{}
ExportMetalSpotConfigCommand.className = "ExportMetalSpotConfigCommand"

function ExportMetalSpotConfigCommand:init(path)
self.path = path
if Path.GetExt(self.path) ~= ".lua" then
self.path = self.path .. ".lua"
end
end

function ExportMetalSpotConfigCommand:GetMetalSpots()
local tbl = {}
local spots = SB.model.mexManager:getAllMexes()
for mexID, params in pairs(spots) do
local x, z, metal = params.x, params.z, params.metal
table.insert(tbl, {x = x, z = z, metal = metal})
if params.xmirror and params.zmirror then
x, z = Game.mapSizeX-x, Game.mapSizeZ-z
table.insert(tbl, {x = x, z = z, metal = metal})
elseif params.xmirror then
x = Game.mapSizeX-x
table.insert(tbl, {x = x, z = z, metal = metal})
elseif params.zmirror then
z = Game.mapSizeZ-z
table.insert(tbl, {x = x, z = z, metal = metal})
end
end
return tbl
end

function ExportMetalSpotConfigCommand:execute()
local file = assert(io.open(self.path, "w"))
file:write("local allspots = ")
file:write(table.show(self:GetMetalSpots()):sub(#"return "))
file:write("\nreturn {spots = allspots}")
file:close()
end
51 changes: 51 additions & 0 deletions scen_edit/command/export_startbox_config_command.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
ExportStartBoxConfigCommand = Command:extends{}
ExportStartBoxConfigCommand.className = "ExportStartBoxConfigCommand"

function ExportStartBoxConfigCommand:init(path)
self.path = path
if Path.GetExt(self.path) ~= ".lua" then
self.path = self.path .. ".lua"
end
end

function ExportStartBoxConfigCommand:GetStartBoxes()
local teamList = {}
-- teamList.boxes, teamList.x, teamList.z, teamList.name, teamList.short = {}, {}, {}, {}, {}
local boxes = SB.model.startboxManager:getAllStartBoxes()
for boxID, _ in pairs(boxes) do
local teamID = SB.model.startboxManager:getTeam(boxID)
local team = SB.model.teamManager:getTeam(teamID)
local maybePos = SB.model.startboxManager:getBoxPos(boxID)
if team.startPos then
maybePos.x, maybePos.z = team.startPos.x, team.startPos.z
end
if not team.short then
team.short = team.name
end
if not teamList[teamID] then
teamList[teamID] = {name = team.name, short = team.short, x = maybePos.x, z = maybePos.z, boxes = {}}
end
table.insert(teamList[teamID].boxes, boxID)
end
return teamList
end

function ExportStartBoxConfigCommand:execute()
local file = assert(io.open(self.path, "w"))
local teamList = self:GetStartBoxes()
file:write("local allboxes = {\n")
for ID, team in pairs(teamList) do
file:write("\t["..ID.."] = {\n\t\tstartpoints = {\n\t\t\t {")
file:write(teamList[ID].x..","..teamList[ID].z.."}\n\t\t},\n\t\t")
file:write("boxes = {\n\t\t\t")
for _, boxID in pairs(teamList[ID].boxes) do
local tbl = SB.model.startboxManager:getBox(boxID)
file:write(table.show(tbl):sub(#"return ")..",")
end
file:write("\n\t\t},\n\t\tnameLong = \""..teamList[ID].name.."\",")
file:write("\n\t\tnameShort = \""..teamList[ID].short.."\",\n\t},\n")
end
file:write("\n}\n\n")
file:write("return allboxes")
file:close()
end
57 changes: 57 additions & 0 deletions scen_edit/command/load_zkconfig_command.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
LoadZKMapConfigCommand = Command:extends{}
LoadZKMapConfigCommand.className = "LoadZKMapConfigCommand"

function LoadZKMapConfigCommand:init(ZKmapconfig)
-- Since the introduction of the data packing/unpacking, is much more
-- efficient passing tables than strings
if ZKmapconfig then
self.mapConfig = loadstring(ZKmapconfig)()
else
self.mapConfig = {}
local boxID = 1
local mapsideBoxes = "mapconfig/map_startboxes.lua"
local mapsideMexes = "mapconfig/map_metal_layout.lua"
if VFS.FileExists (mapsideBoxes) then
self.mapConfig.teamList, self.mapConfig.boxes = {}, {}
local boxConfig = VFS.Include(mapsideBoxes)
for ID, boxdata in pairs(boxConfig) do
local color = { r=math.random(), g=math.random(), b=math.random(), a=1}
self.mapConfig.teamList[ID] = {id = ID, allyTeam = ID, color = color, name = boxdata.nameLong, short = (boxdata.nameShort or boxdata.nameLong), x = boxdata.startpoints[1][1], z = boxdata.startpoints[1][2], boxes = {}}
for _, box in pairs(boxdata.boxes) do
table.insert(self.mapConfig.teamList[ID].boxes, boxID)
table.insert(self.mapConfig.boxes, box)
boxID = boxID + 1
end
end
end
if VFS.FileExists (mapsideMexes) then
self.mapConfig.mexes = VFS.Include(mapsideMexes).spots
end
end
end

function LoadZKMapConfigCommand:execute()
--assert(self.mapConfig)
local mexes = self.mapConfig.mexes or {}
local boxes = self.mapConfig.boxes or {}
local teamList = self.mapConfig.teamList or {}
SB.delay(function()
SB.delay(function()
for mexID, mex in pairs(mexes) do
SB.model.mexManager:addMex(mex)
end
for teamID, team in pairs(teamList) do
for _, boxID in pairs(team.boxes) do
local newBoxID = SB.model.startboxManager:addBox(boxes[boxID])
SB.model.startboxManager:setTeam(newBoxID, teamID)
end
if not SB.model.teamManager:getTeam(teamID) then
local cmd = AddTeamCommand(team.name, team.color, team.allyTeam, Spring.GetSideData(team.allyTeam))
SB.commandManager:execute(cmd)
end
local cmd = UpdateTeamCommand(team)
SB.commandManager:execute(cmd)
end
end)
end)
end
5 changes: 5 additions & 0 deletions scen_edit/command/project/load_project_command_widget.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ function LoadProjectCommandWidget:_LoadUnsynced()
table.insert(cmds, LoadGUIStateCommand(VFS.LoadFile(file, VFS.RAW)))
end

local file = Path.Join(SB.project.path, Project.ZKCONFIG_FILE)
if VFS.FileExists(file, VFS.RAW) then
table.insert(cmds, LoadZKMapConfigCommand(VFS.LoadFile(file, VFS.RAW)))
end

local cmd = CompoundCommand(cmds)
SB.commandManager:execute(cmd, true)
end
24 changes: 23 additions & 1 deletion scen_edit/command/save_command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ local function GUIStateSave(path)
table.save(guiState, path)
end

local function ZKconfigSave(path)
local tbl = {}
local teamList = {}
local boxes = SB.model.startboxManager:getAllStartBoxes()
for boxID, _ in pairs(boxes) do
local teamID = SB.model.startboxManager:getTeam(boxID)
local team = SB.model.teamManager:getTeam(teamID)
if not teamList[teamID] then
teamList[teamID] = {name = team.name, short = team.short, x = team.x or nil, z = team.z or nil, boxes = {}}
end
table.insert(teamList[teamID].boxes, boxID)
end
local mexes = SB.model.mexManager:getAllMexes()
tbl.mexes, tbl.boxes, tbl.teamList = mexes, boxes, teamList
table.save(tbl, path)
end

function SaveCommand:execute()
local projectDir = self.path

Expand Down Expand Up @@ -111,7 +128,12 @@ function SaveCommand:execute()
end, function(elapsed)
Log.Notice(("[%.4fs] Saved GUI state"):format(elapsed))
end)


Time.MeasureTime(function()
ZKconfigSave(Path.Join(projectDir, Project.ZKCONFIG_FILE))
end, function(elapsed)
Log.Notice(("[%.4fs] Saved GUI state"):format(elapsed))
end)
-- Hide the console (FIXME: game agnostic way)
-- Spring.SendCommands("console 0")

Expand Down
109 changes: 109 additions & 0 deletions scen_edit/model/mex_manager.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
MexManager = Observable:extends{}

function MexManager:init()
self:super('init')
self.mexIDCount = 0
self.mexes = {}
end

function MexManager:addMex(mex, mexID)
if mexID == nil then
mexID = self.mexIDCount + 1
end
self.mexIDCount = mexID
if mex.xmirror == nil then
mex.xmirror, mex.zmirror = false, false
end
self.mexes[mexID] = mex
self:callListeners("onMexAdded", mexID, mex)
return mexID
end

function MexManager:getMexIDCount(mexID)
if mexID == nil then
mexID = self.mexIDCount + 1
end
self.mexIDCount = mexID
return self.mexIDCount
end

function MexManager:getAllMexes()
local metalspots = {}
for mexID, mex in pairs(self.mexes) do
metalspots[mexID] = mex
end
return metalspots
end

function MexManager:getAllMexIDs()
local metalspots = {}
for mexID, mex in pairs(self.mexes) do
table.insert(metalspots, mexID)
end
return metalspots
end

function MexManager:removeMex(mexID)
if self.mexes[mexID] ~= nil then
local mex = self.mexes[mexID]
self.mexes[mexID] = nil
self:callListeners("onMexRemoved", mexID, mex)
end
end

function MexManager:setMex(mexID, partialObject)
assert(self.mexes[mexID])
local obj = partialObject
local partialmex = {}
for key, _ in pairs(self.mexes[mexID]) do
if obj[key] ~= nil then
partialmex[key] = self.mexes[mexID][key]
self.mexes[mexID][key] = obj[key]
end
end
self:callListeners("onMexChange", mexID, partialmex)
end

function MexManager:getMex(mexID)
return self.mexes[mexID]
end
-- Utility functions
local function DistSq(x1, z1, x2, z2)
return (x1 - x2)*(x1 - x2) + (z1 - z2)*(z1 - z2)
end

function MexManager:getMexIn(x, z)
local x2, z2 = x, z
local selected, dragDiffX, dragDiffZ
for mexID, mex in pairs(self.mexes) do
local xpos, zpos = mex.x, mex.z
if DistSq(xpos, zpos, x2, z2) < 1600 then
selected = mexID
dragDiffX = mex.x - x2
dragDiffZ = mex.z - z2
end
end
return selected, dragDiffX, dragDiffZ
end

function MexManager:getPos(mexID)
assert(self.mexes[mexID])
local y = Spring.GetGroundHeight(self.mexes[mexID].x, self.mexes[mexID].z)
return {x = self.mexes[mexID].x,y = y, z = self.mexes[mexID].z}
end
------------------------------------------------
-- Listener definition
------------------------------------------------
MexManagerListener = LCS.class.abstract{}

function MexManagerListener:onMexAdded(mexID, mex)
end

function MexManagerListener:onMexRemoved(mexID, mex)
end

function MexManagerListener:onMexChange(mexID, partmex, partobj)
end
------------------------------------------------
-- End listener definition
------------------------------------------------
2 changes: 2 additions & 0 deletions scen_edit/model/model.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ SB.IncludeDir(Path.Join(SB.DIRS.SRC, 'model/object'))

function Model:init()
self.areaManager = AreaManager()
self.mexManager = MexManager()
self.variableManager = VariableManager()
self.triggerManager = TriggerManager()
self.teamManager = TeamManager()
self.startboxManager = StartBoxManager()
self.scenarioInfo = ScenarioInfo()
self.terrainManager = TerrainManager()
if Script.GetName() == "LuaUI" then
Expand Down
1 change: 1 addition & 0 deletions scen_edit/model/project.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Project.METAL_FILE = Path.Join(Project.FOLDER_PREFIX, "metal.data")
Project.SCRIPT_FILE = Path.Join(Project.FOLDER_PREFIX, "script.txt")

Project.GUI_FILE = Path.Join(Project.FOLDER_PREFIX, "gui.lua")
Project.ZKCONFIG_FILE = Path.Join(Project.FOLDER_PREFIX, "zkconfig.lua")
Project.SCREENSHOT_FILE = Path.Join(Project.FOLDER_PREFIX, "screenshot.jpg")
Project.TEXTURES_FOLDER = Path.Join(Project.FOLDER_PREFIX, "textures/")

Expand Down
Loading