Save/load Widget (cheat + give)

Save/load Widget (cheat + give)

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
Posts
Posts: 63
Joined: 02 Oct 2007, 04:00

Save/load Widget (cheat + give)

Post by Posts »

Summary: loads units with cheat give command

have not tested in multiplayer, in theory it should work

-----------

Save (Shift+F8)
Best when used as a spectator, anything covered by the fog of war will not be saved.
Old save will be destroyed and replaced by new save.
Data will be saved to file "xsavegame" (you don't need to know this, you are forced to use this file name)

-----------

Load (Ctrl+F8)
Cheat mode must be enabled, the "give" command is required.
The only action the the load command takes is "give".

--------

Saved Information:
/give <unitName> [team] @x,y,z
yeah, that is it.
newbie tip: "team" is not actually the team number (sort of)

---------

Credits:

--"zwzsg", a few lines of code from him, he has an advanced save/load system that uses start scripts and saves more details
--Kernel_Panic_3.6\LuaRules\Gadgets\spawn_units.lua
--Kernel_Panic_3.6\LuaUI\Widgets\dump_units.lua

--split function from http://lua-users.org/wiki/SplitJoin , PhilippeLhoste

------------------

Code: Select all


--"zwzsg", a few lines of code from him, he has an advanced save/load system that uses start scripts and saves more details
--Kernel_Panic_3.6\LuaRules\Gadgets\spawn_units.lua
--Kernel_Panic_3.6\LuaUI\Widgets\dump_units.lua

--split function from http://lua-users.org/wiki/SplitJoin , PhilippeLhoste

function widget:GetInfo()
  return {
    name      = "Save/Load cheat-give",
    desc      = "Crude save/load widget, save(Shift+F8)-as-spectator, load(Ctrl+F8)-give-cheat",
    author    = "posts",
    date      = "Oct 26, 2009",
    license   = "GNU GPL, v2 or later",
    layer     = 0,
    enabled   = true  --  loaded by default?
  }
end

local SendCommands		= Spring.SendCommands
local spGetModKeyState	= Spring.GetModKeyState

function widget:GameStart()
  SendCommands("echo loaded test")
end

-- START COPY from: http://lua-users.org/wiki/SplitJoin
function Split(str, delim, maxNb)
    -- Eliminate bad cases...
    if string.find(str, delim) == nil then
        return { str }
    end
    if maxNb == nil or maxNb < 1 then
        maxNb = 0    -- No limit
    end
    local result = {}
    local pat = "(.-)" .. delim .. "()"
    local nb = 0
    local lastPos
    for part, pos in string.gmatch(str, pat) do
        nb = nb + 1
        result[nb] = part
        lastPos = pos
        if nb == maxNb then break end
    end
    -- Handle the last field
    if nb ~= maxNb then
        result[nb + 1] = string.sub(str, lastPos)
    end
    return result
end
-- END COPY from: http://lua-users.org/wiki/SplitJoin

local function xSave()

	local _, specFullView = Spring.GetSpectatingState()
	if (not specFullView) then
		Spring.Echo("Partial Save, you must be spectator to save everything")
	end

	local savefile = io.open("xsavegame","wb")
	for _,u in ipairs(Spring.GetAllUnits()) do
		local name = UnitDefs[Spring.GetUnitDefID(u)].name
		local team = Spring.GetUnitTeam(u)
		local x,y,z = Spring.GetUnitPosition(u)
		savefile:write("give "..name.." "..team.." "..x.." "..y.." "..z.."\r\n")
	end
	savefile:flush()
	savefile:close()
	Spring.Echo("Game Saved")
end

local function xLoad()

	if (not Spring.IsCheatingEnabled()) then
		Spring.Echo("Cheat mode must be enabled to load the saved game")
		return
	end

	local savefile,err = io.open("xsavegame")
	if savefile == nil then
		return
	end
	
	for line in savefile:lines("xsavegame") do
		local args = Split(line," ")
		local cmd = args[1]
		if cmd == "give" then
			local name = args[2]
			local team = args[3]
			local x = args[4]
			local y = args[5]
			local z = args[6]
			SendCommands("give 1 "..name.." "..team.." @"..x..","..y..","..z)
		end
	end
	
	savefile:close()
	Spring.Echo("Save game loaded")
end

function widget:KeyPress(key, mods, isRepeat)
	local F8 = 289
	local alt,ctrl,meta,shift = spGetModKeyState()
	
	if (ctrl and key == F8) then
		--SendCommands("echo load")
		xLoad()
	end
	if (shift and key == F8) then
		--SendCommands("echo save")
		xSave()
	end
	--if (alt and key == F8) then
	--	SendCommands("echo auto")
	--end
end
Attachments
unit_save.lua
v1
(2.95 KiB) Downloaded 133 times
Last edited by Posts on 27 Oct 2009, 00:30, edited 1 time in total.
User avatar
CarRepairer
Cursed Zero-K Developer
Posts: 3359
Joined: 07 Nov 2007, 21:48

Re: Save/load Widget (cheat + give)

Post by CarRepairer »

Very interesting idea making it a widget. I'll try it out.
Post Reply

Return to “Lua Scripts”