Page 1 of 1

Saving and restoring command queue

Posted: 12 Jan 2009, 10:45
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?

Re: Saving and restoring command queue

Posted: 12 Jan 2009, 11:01
by jK
I didn't wrote the wiki for nothing ...

Re: Saving and restoring command queue

Posted: 12 Jan 2009, 11:03
by Tronic
Which one? I've been using http://spring.clan-sy.com/wiki/Lua_Scripting but couldn't get good tips there.

Re: Saving and restoring command queue

Posted: 12 Jan 2009, 17:38
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

Re: Saving and restoring command queue

Posted: 13 Jan 2009, 10:41
by Forboding Angel
edit: fuckit, waste of time

Re: Saving and restoring command queue

Posted: 13 Jan 2009, 11:37
by Tronic
Now, do I need to manually recreate the arrays and if so, is there code to do that already?

Re: Saving and restoring command queue

Posted: 13 Jan 2009, 12:10
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