Guest User

Untitled

a guest
Nov 7th, 2016
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.08 KB | None | 0 0
  1. function widget:GetInfo()
  2.   return {
  3.     name        = "Fight Command Order",
  4.     desc        = "Gives select units a fight command after they have been built (because Spring doesn't call unitIdle properly)",
  5.     author      = "Forby",
  6.     date        = "Jan 8, 2007",
  7.     license     = "GNU GPL, v2 or later",
  8.     layer       = 0,
  9.     enabled     = true  --  loaded by default?
  10.   }
  11. end
  12.  
  13. --------------------------------------------------------------------------------
  14. --------------------------------------------------------------------------------
  15.  
  16. local CMD_FIGHT             = CMD.FIGHT
  17. local spGetMyTeamID         = Spring.GetMyTeamID
  18. local spGetTeamUnits        = Spring.GetTeamUnits
  19. local spGetUnitDefID        = Spring.GetUnitDefID
  20. local spGetUnitPosition     = Spring.GetUnitPosition
  21. local spGiveOrderToUnit     = Spring.GiveOrderToUnit
  22. local spGetSpectatingState  = Spring.GetSpectatingState
  23.  
  24. local hmsx = Game.mapSizeX/2
  25. local hmsz = Game.mapSizeZ/2
  26.  
  27.  
  28. --------------------------------------------------------------------------------
  29. --------------------------------------------------------------------------------
  30.  
  31.  
  32. local function IsTargetUnit(ud)
  33.     return ud and ud.unitName == "eorb"
  34. end
  35.  
  36.  
  37. local function SetupUnit(unitID)
  38.     local x, y, z = spGetUnitPosition(unitID)
  39.     if x and y and z then
  40.         if (x > hmsx) then -- avoid to issue commands outside map
  41.           x = x - 50
  42.         else
  43.           x = x + 50
  44.         end
  45.         if (z > hmsz) then
  46.           z = z - 50
  47.         else
  48.           z = z + 50
  49.         end
  50.         -- meta enables reclaim enemy units, alt autoresurrect ( if available )
  51.         spGiveOrderToUnit(unitID, CMD_FIGHT, { x, y, z }, {"meta"})
  52.     end
  53. end
  54.  
  55. function widget:PlayerChanged()
  56.     if spGetSpectatingState() then
  57.         widgetHandler:RemoveWidget()
  58.     end
  59. end
  60.  
  61. function widget:Initialize()
  62.     if spGetSpectatingState() then
  63.         widgetHandler:RemoveWidget()
  64.     end
  65. end
  66.  
  67.  
  68. function widget:UnitCreated(unitID, unitDefID, unitTeam)
  69.     if unitTeam ~= spGetMyTeamID() then
  70.         return
  71.     end
  72.     if IsTargetUnit(UnitDefs[unitDefID]) then
  73.         SetupUnit(unitID)
  74.     end
  75. end
  76.  
  77.  
  78. function widget:UnitGiven(unitID, unitDefID, unitTeam)
  79.     widget:UnitCreated(unitID, unitDefID, unitTeam)
  80. end
Advertisement
Add Comment
Please, Sign In to add comment