Cloning a Unit's Order Queue

Cloning a Unit's Order Queue

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

Moderator: Moderators

Post Reply
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Cloning a Unit's Order Queue

Post by Argh »

Um... I'm having trouble with something that's so simple, it's annoying. I just want to exactly copy the orders of one unit to another, sans the "tag".

So, I've written the code like this, where u == the unitID of the unit I want to copy from, and k == the unitID of the unit I want to copy to:

local cmds = Spring.GetUnitCommands(u)
Spring.GiveOrderArrayToUnitMap(k,cmds)

... isn't it supposed to be that simple? Or am I missing something here? Sorry in advance, I can't seem to find an example that actually works when I use it.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: Cloning a Unit's Order Queue

Post by FLOZi »

For a start unitmap takes a table as the first arg, tried

local cmds = Spring.GetUnitCommands(u)
Spring.GiveOrderArrayToUnitMap({k},cmds)

?

That might be complete rubbish, its 4.45am :oops:
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Cloning a Unit's Order Queue

Post by Argh »

Meh, that didn't work, either... hmm...
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: Cloning a Unit's Order Queue

Post by FLOZi »

Code: Select all

  --copy command queue
  local cmds = Spring.GetUnitCommands(unitID)
  for i = 2, cmds.n do  -- skip the first command (CMD_MORPH)
    local cmd = cmds[i]
    Spring.GiveOrderToUnit(newUnit, cmd.id, cmd.params, cmd.options.coded)
  end
Is how it's done in the morph gadget
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Cloning a Unit's Order Queue

Post by Argh »

Yeah, I tried that earlier, it didn't work, either. Something must be screwy about how I'm going about this, lemme take another look.
User avatar
TheFatController
Balanced Annihilation Developer
Posts: 1177
Joined: 10 Dec 2006, 18:46

Re: Cloning a Unit's Order Queue

Post by TheFatController »

Here's mine from a Gadget i haven't released, should work out of context (target should be a unit with no orders)

Code: Select all

local function copyCommandQueue(targetID, unitID)

  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
  
end
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Cloning a Unit's Order Queue

Post by Argh »

I got it working, finally. I've found that Units don't get their CommandQueue before StartMoving() runs, though. Might be a useful factoid for people out there.
User avatar
Pendrokar
Posts: 658
Joined: 30 May 2007, 10:45

Re: Cloning a Unit's Order Queue

Post by Pendrokar »

I have a problem with this too to get the widget I last mentioned.

Code: Select all

function widget:CommandNotify(commandID, params, options)
 Spring.GiveOrderToUnit(unitID, CMD.FIGHT, params, options.coded)
return true
Off course there is much more to the code but I took out the only thing that isn't working. In one way it works. The unit gets the order but it just ignores "alt", "ctrl", "shift", "right" options.
Post Reply

Return to “Lua Scripts”