View topic - Save/load Widget (cheat + give)


All times are UTC + 1 hour


Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: 26 Oct 2009, 23:29 

Joined: 02 Oct 2007, 03:00
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:

--"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:
File comment: v1
unit_save.lua [2.95 KiB]
Downloaded 130 times


Last edited by Posts on 27 Oct 2009, 00:30, edited 1 time in total.
Top
 Offline Profile  
 
PostPosted: 27 Oct 2009, 00:10 
Cursed Zero-K Developer
User avatar

Joined: 07 Nov 2007, 21:48
Location: Horse
Very interesting idea making it a widget. I'll try it out.
Top
 Offline Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Site layout created by Roflcopter et al.