Saving and restoring command queue

Saving and restoring command queue

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

Moderator: Moderators

Post Reply
Tronic
Posts: 75
Joined: 30 Nov 2006, 03:21

Saving and restoring command queue

Post by Tronic »

I'm using

orders = Spring.GetCommandQueue(unitID) -- save
Spring.GiveOrderArrayToUnitArray({unitID}, orders) -- restore

But this gives error "bad command ID". Apparently they use different array formats.

How should this be done?
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: Saving and restoring command queue

Post by jK »

I didn't wrote the wiki for nothing ...
Tronic
Posts: 75
Joined: 30 Nov 2006, 03:21

Re: Saving and restoring command queue

Post by Tronic »

Which one? I've been using http://spring.clan-sy.com/wiki/Lua_Scripting but couldn't get good tips there.
User avatar
CarRepairer
Cursed Zero-K Developer
Posts: 3359
Joined: 07 Nov 2007, 21:48

Re: Saving and restoring command queue

Post by CarRepairer »

Actually I think this has something to do with the big red font in this page: http://spring.clan-sy.com/wiki/Lua_CMDs
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: Saving and restoring command queue

Post by Forboding Angel »

edit: fuckit, waste of time
Tronic
Posts: 75
Joined: 30 Nov 2006, 03:21

Re: Saving and restoring command queue

Post by Tronic »

Now, do I need to manually recreate the arrays and if so, is there code to do that already?
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Saving and restoring command queue

Post by Argh »

Now, do I need to manually recreate the arrays and if so, is there code to do that already?
Yes, and yes.

For standard Units, where you just want to mirror command queues from A to B, here's some code.

Wrap it in a function where params a,b are your target(s) unitIDs and the unitID you're copying from, respectively:

Code: Select all

  local states = Spring.GetUnitStates(targetID)

  if (states ~= nil) then
    Spring.GiveOrderToUnit(unitID, CMD.FIRE_STATE, { states.firestate }, 0)
    Spring.GiveOrderToUnit(unitID, CMD.MOVE_STATE, { states.movestate }, 0)
    Spring.GiveOrderToUnit(unitID, CMD.REPEAT,     { states['repeat']  and 1 or 0 }, 0)
    Spring.GiveOrderToUnit(unitID, CMD.ONOFF,      { states.active     and 1 or 0 }, 0)
    Spring.GiveOrderToUnit(unitID, CMD.CLOAK,      { states.cloak      and 1 or 0 }, 0)
    Spring.GiveOrderToUnit(unitID, CMD.TRAJECTORY, { states.trajectory and 1 or 0 }, 0)
  end
 
  local queue = Spring.GetCommandQueue(targetID);
 
  if (queue ~= nil) then

    for k,v in ipairs(queue) do  --  in order
      local opts = v.options
      if (not opts.internal) then
        local newopts = {}
        if (opts.alt)   then table.insert(newopts, "alt")   end
        if (opts.ctrl)  then table.insert(newopts, "ctrl")  end
        if (opts.right) then table.insert(newopts, "right") end
        table.insert(newopts, "shift")
        Spring.GiveOrderToUnit(unitID, v.id, v.params, opts.coded)
      end
    end
  end

  local buildqueue = Spring.GetRealBuildQueue(targetID)
  if (buildqueue ~= nil) then
    for udid,buildPair in ipairs(buildqueue) do
      local udid, count = next(buildPair, nil)
      Spring.AddBuildOrders(unitID, udid, count)
    end
  end
For Factories, it's a little different:

Code: Select all

	local states = Spring.GetUnitStates(targetID)

	if (states ~= nil) then
		Spring.GiveOrderToUnit(unitID, CMD.FIRE_STATE, { states.firestate }, 0)
		Spring.GiveOrderToUnit(unitID, CMD.MOVE_STATE, { states.movestate }, 0)
		Spring.GiveOrderToUnit(unitID, CMD.REPEAT,     { states['repeat']  and 1 or 0 }, 0)
		Spring.GiveOrderToUnit(unitID, CMD.ONOFF,      { states.active     and 1 or 0 }, 0)
		Spring.GiveOrderToUnit(unitID, CMD.CLOAK,      { states.cloak      and 1 or 0 }, 0)
		Spring.GiveOrderToUnit(unitID, CMD.TRAJECTORY, { states.trajectory and 1 or 0 }, 0)
	end
 
	local queue = Spring.GetCommandQueue(targetID);
 
	if (queue ~= nil) then

	for k,v in ipairs(queue) do  --  in order
		local opts = v.options
		if (not opts.internal) then
			local newopts = {}
			if (opts.alt)   then table.insert(newopts, "alt")   end
				if (opts.ctrl)  then table.insert(newopts, "ctrl")  end
					if (opts.right) then table.insert(newopts, "right") end
						table.insert(newopts, "shift")
						Spring.GiveOrderToUnit(unitID, v.id, v.params, opts.coded)
					end
				end
			end

		local buildqueue = Spring.GetRealBuildQueue(targetID)
		if (buildqueue ~= nil) then
			--Spring.Echo("found build orders")
			for udid,buildPair in ipairs(buildqueue) do
				local udid, count = next(buildPair, buildPair[1])
				--Spring.Echo(udid,count)
				for x = 1,count,1 do
					Spring.GiveOrderToUnit(unitID, -udid, {1}, {""})
				end
			end
		end
Post Reply

Return to “Lua Scripts”