LUA command hooks

LUA command hooks

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

Moderator: Moderators

Post Reply
trepan
Former Engine Dev
Posts: 1200
Joined: 17 Nov 2005, 00:52

LUA command hooks

Post by trepan »

A couple of quick features using the LUA interface:

Code: Select all

function CommandNotify(id, params, options)
  -- GUARD + CTRL = copy command queue
  if ((id == CMD_GUARD) and options.ctrl) then
    targetID = params[1];
    queue = Spring.GetCommandQueue(targetID);
    if (queue ~= nil) then  --  might not be an ally
      Spring.GiveOrder( CMD_STOP, {}, {} )
      for k,v in ipairs(queue) do  --  in order
        if (not v.options.internal) then
          opts = {}
          if (v.options.alt)   then table.insert(opts, "alt")   end
          if (v.options.ctrl)  then table.insert(opts, "ctrl")  end
          if (v.options.shift) then table.insert(opts, "shift") end
          if (v.options.right) then table.insert(opts, "right") end
          Spring.GiveOrder( v.id, v.params, opts )
        end
      end
      return false  --  do not add the original GUARD command
    end   
  end
  return true
end

Code: Select all

function SelectedString()
  local s = ""
  uidTable = Spring.GetSelectedUnits("raw")
  uidTable.n = nil  --  or use ipairs
  for k,v in pairs(uidTable) do
    s = s .. ' +' .. v
  end
  return s
end


function UnitCreated(unitID, unitDefID)
  ud = UnitDefs[unitDefID]
  if ((ud ~= nil) and (Spring.GetUnitTeam(unitID) == MyTeamID)) then
    if (ud.builder and not ud.canMove) then
      -- set immobile builders (nanotowers) to the ROAM movestate,
      -- and give them a PATROL order (does not matter where, afaict)
      selstr = SelectedString()   
      Spring.SendCommands({ "selectunits clear +" .. unitID })
      x, y, z = Spring.GetUnitPosition(unitID)
      Spring.GiveOrder( CMD_MOVE_STATE, { 2 }, { } )
      Spring.GiveOrder( CMD_PATROL, { x + 25, y, z - 25 }, { } )
      Spring.SendCommands({ "selectunits clear" .. selstr })
    elseif (ud.isFactory) then
      -- give factories a self GUARD pattern for their progeny
      selstr = SelectedString()
      Spring.SendCommands({ "selectunits clear +" .. unitID })
      x, y, z = Spring.GetUnitPosition(unitID)
      Spring.GiveOrder( CMD_MOVE, { x + 100, y, z + 100 }, { "shift" } )
      Spring.GiveOrder( CMD_MOVE, { x + 100, y, z }, { "shift" } )
      Spring.GiveOrder( CMD_GUARD, { unitID }, { "shift" } )
      Spring.SendCommands({ "selectunits clear" .. selstr })
    end
  end
end
trepan
Former Engine Dev
Posts: 1200
Joined: 17 Nov 2005, 00:52

Post by trepan »

Update on the second part:

Code: Select all

function UnitCreated(unitID, unitDefID)
  ud = UnitDefs[unitDefID]
  if ((ud ~= nil) and (Spring.GetUnitTeam(unitID) == MyTeamID)) then
    if (ud.isFactory) then
      -- give factories a self GUARD pattern for their progeny
      selstr = SelectedString()
      Spring.SendCommands({ "selectunits clear +" .. unitID })
      x, y, z = Spring.GetUnitPosition(unitID)
      Spring.GiveOrder( CMD_MOVE, { x + 100, y, z + 100 }, { "shift" } )
      Spring.GiveOrder( CMD_MOVE, { x + 100, y, z }, { "shift" } )
      Spring.GiveOrder( CMD_GUARD, { unitID }, { "shift" } )
      Spring.SendCommands({ "selectunits clear" .. selstr })
    elseif (ud.builder and not ud.canMove) then
      -- set immobile builders (nanotowers) to the ROAM movestate,
      -- and give them a PATROL order (does not matter where, afaict)
      selstr = SelectedString()
      Spring.SendCommands({ "selectunits clear +" .. unitID })
      x, y, z = Spring.GetUnitPosition(unitID)
      Spring.GiveOrder( CMD_MOVE_STATE, { 2 }, { } )
      Spring.GiveOrder( CMD_PATROL, { x + 25, y, z - 25 }, { } )
      Spring.SendCommands({ "selectunits clear" .. selstr })
    end
    
    if (ud.canStockpile) then
      -- give stockpilers 5 units to build    
      selstr = SelectedString()
      Spring.SendCommands({ "selectunits clear +" .. unitID })
      Spring.GiveOrder( CMD_STOCKPILE, { }, { "shift" } )     
      Spring.SendCommands({ "selectunits clear" .. selstr })
    end
  end  
end
User avatar
Gabba
Posts: 319
Joined: 08 Sep 2004, 22:59

Re: LUA command hooks

Post by Gabba »

trepan wrote:A couple of quick features using the LUA interface:

Code: Select all

function CommandNotify(id, params, options)
  -- GUARD + CTRL = copy command queue
  if ((id == CMD_GUARD) and options.ctrl) then
    targetID = params[1];
    queue = Spring.GetCommandQueue(targetID);
    if (queue ~= nil) then  --  might not be an ally
      Spring.GiveOrder( CMD_STOP, {}, {} )
      for k,v in ipairs(queue) do  --  in order
        if (not v.options.internal) then
          opts = {}
          if (v.options.alt)   then table.insert(opts, "alt")   end
          if (v.options.ctrl)  then table.insert(opts, "ctrl")  end
          if (v.options.shift) then table.insert(opts, "shift") end
          if (v.options.right) then table.insert(opts, "right") end
          Spring.GiveOrder( v.id, v.params, opts )
        end
      end
      return false  --  do not add the original GUARD command
    end   
  end
  return true
end
Very interesting this one :) . I've been waiting for this command for a while. Will you make a bit more advertising for it than just posting the code? I bet most people just turned away from the thread when they saw there were no pictures!
I suppose this will be in the next version of Spring?
trepan
Former Engine Dev
Posts: 1200
Joined: 17 Nov 2005, 00:52

Post by trepan »

Na, I'll leave the advertising of specific functions to the trickle down effect
(ie: relying on folks like you, who are observant enough to pick out the relevant
details from the code). I do not have a complete "gui.lua" written, so I am only
advertising the LUA functionality, but not the particulars.

The code to support this feature is in SVN, so it can be used in the next
version of spring. You will also require a "gui.lua" with the related support code.
trepan
Former Engine Dev
Posts: 1200
Joined: 17 Nov 2005, 00:52

Post by trepan »

Might as well set matching states during a copy:

Code: Select all

function b2n(value)
  -- convert a boolean to a number
  if (value) then return 1 else return 0 end
end
   
function CommandNotify(id, params, options)
  -- GUARD + CTRL = copy command queue
  if ((id == CMD_GUARD) and options.ctrl) then
    local targetID = params[1];
    local queue = Spring.GetCommandQueue(targetID);
    if (queue ~= nil) then  --  might not be an ally
      -- copy the command queue
      Spring.GiveOrder( CMD_STOP, {}, {} )
      for k,v in ipairs(queue) do  --  in order
        if (not v.options.internal) then
          opts = {}
          if (v.options.alt)   then table.insert(opts, "alt")   end
          if (v.options.ctrl)  then table.insert(opts, "ctrl")  end
          if (v.options.shift) then table.insert(opts, "shift") end
          if (v.options.right) then table.insert(opts, "right") end
          Spring.GiveOrder( v.id, v.params, opts )
        end
      end  
      -- set matching states
      uStates = Spring.GetUnitStates(targetID)
      if (uStates ~= nil) then
        Spring.GiveOrder(CMD_CLOAK,  {b2n(uStates.cloak)} ,    {})
        Spring.GiveOrder(CMD_ONOFF,  {b2n(uStates.active)},    {})
        Spring.GiveOrder(CMD_REPEAT, {b2n(uStates.repeating)}, {})
        Spring.GiveOrder(CMD_FIRE_STATE, {uStates.firestate},  {})
        Spring.GiveOrder(CMD_MOVE_STATE, {uStates.movestate},  {})
      end
      return false  --  do not add the original GUARD command
    end
  end  
  return true
end
User avatar
PauloMorfeo
Posts: 2004
Joined: 15 Dec 2004, 20:53

Post by PauloMorfeo »

trepan wrote:Na, I'll leave the advertising of specific functions to the trickle down effect
(ie: relying on folks like you, who are observant enough to pick out the relevant
details from the code). ...
But you could (should?) at least add to the anouncements a short description of what that is suposed to do.

After all, you are not just expecting "folks" "who are observant enough to pick out the relevant details from the code" from the svn updates, are you? You are posting it here so everyone can see.

As i said before, a feature in Spring that no one knows about or how to use, is, basicaly, a feature that doesn't exists.
Post Reply

Return to “Lua Scripts”