Advertisement
Guest User

Follow_And_Shove

a guest
Sep 29th, 2015
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.40 KB | None | 0 0
  1. function widget:GetInfo()
  2.   return {
  3.     name      = "Follow And Shove (v1.1)",
  4.     desc      = "giving a guard order while holding the alt key causes the selected units to constantly path to the selected unit",
  5.     author    = "AutoWar",
  6.     date      = "2015",
  7.     license   = "GNU GPL, v2 or later",
  8.     layer     = 9999,
  9.     enabled   = true
  10.   }
  11. end
  12.  
  13. -----------------------------------------------------------------
  14.  
  15. local spGetUnitNearestEnemy = Spring.GetUnitNearestEnemy
  16. local spGetAllUnits         = Spring.GetAllUnits    --( ) -> nil | unitTable = { [1] = number unitID, ... }
  17. local spMarkerAddPoint      = Spring.MarkerAddPoint
  18. local spEcho                = Spring.Echo
  19. local spInsertUnitCmdDesc   = Spring.InsertUnitCmdDesc
  20. local spGetUnitAllyTeam     = Spring.GetUnitAllyTeam
  21. local spValidUnitID         = Spring.ValidUnitID
  22. local spGetUnitPosition     = Spring.GetUnitPosition
  23. local spGetUnitDefID        = Spring.GetUnitDefID
  24. local spGiveOrderToUnit     = Spring.GiveOrderToUnit
  25. local spGetUnitStates       = Spring.GetUnitStates
  26. local spGiveOrderToUnitArray= Spring.GiveOrderToUnitArray
  27. local spGetUnitIsDead       = Spring.GetUnitIsDead
  28. local spValidUnitID         = Spring.ValidUnitID
  29. local spGetTeamUnits        = Spring.GetTeamUnits  --( number teamID ) -> nil | unitTable = { [1] = number unitID, etc... }
  30. local spGetMyTeamID         = Spring.GetMyTeamID
  31. local spGetMyAllyTeamID     = Spring.GetMyAllyTeamID
  32. local spIsUnitIcon          = Spring.IsUnitIcon
  33. local myAllyID = Spring.GetMyAllyTeamID()
  34. local myTeamID = Spring.GetMyTeamID()
  35. local CMD_ATTACK = CMD.ATTACK
  36. local CMD_MOVE = CMD.MOVE
  37. local CMD_UNIT_SET_TARGET = 34923
  38. local spGetAllyTeamList         = Spring.GetAllyTeamList    --( ) -> { [1] = number allyTeamID, etc... }
  39. local spGetTeamList             = Spring.GetTeamList        --( [number allyTeamID = -1] ) -> nil | { [1] = number teamID, etc... }
  40. local spGetUnitTeam             = Spring.GetUnitTeam        --( number unitID ) -> nil | number teamID
  41. local CMD_LOAD_UNITS            = CMD.LOAD_UNITS            -- expect 1 parameter in return (unitid) or 4 parameters in return (mappos+radius)
  42. local spGetUnitTransporter      = Spring.GetUnitTransporter --( number unitID ) -> nil | number unitID
  43. local spGetUnitIsTransporting   = Spring.GetUnitIsTransporting --( number unitID ) -> nil | { [1] = number unitID, etc... }
  44. local spGetUnitHealth           = Spring.GetUnitHealth      --( number unitID ) -> nil | number health, number maxHealth, number paralyzeDamage, number captureProgress, number buildProgress
  45. local spGetUnitIsStunned        = Spring.GetUnitIsStunned   --( number unitID ) -> nil | boolean stunned_or_inbuild, boolean stunned, boolean inbuild
  46. local spGetUnitRulesParam       = Spring.GetUnitRulesParam  --( number unitID, number index | string ruleName ) -> nil | number value | string value
  47. local CMD_OPT_SHIFT             = CMD.OPT_SHIFT             --If the unit is working down a shift queue, add it seemless into it
  48. local CMD_INSERT                = CMD.INSERT
  49. local spGetCommandQueue         = Spring.GetCommandQueue    --( number unitID, number count = 0 ) -> nil | number commandQueueSize,  ( number unitID [, number count = -1] ) -> nil | table commandQueueTable = {[1] = {("id"=number),("params"={ [1]=number, ...}),("options"={"coded"=number,"alt"=boolean,"ctrl"=boolean,"shift"=boolean,"right"=boolean,"internal"=boolean,"meta"=boolean}}), ...}
  50. local spGetSelectedUnits        = Spring.GetSelectedUnits   --( ) -> { [1] = unitID, ... }
  51. local spGetGroundHeight         = Spring.GetGroundHeight    --(x,z)
  52. local CMD_CAPTURE               = CMD.CAPTURE               --expect 1 parameter in return (unitid) or 4 parameters in return (mappos+radius)
  53. local spGetUnitRulesParam       = Spring.GetUnitRulesParam  --( number unitID, number index | string ruleName ) -> nil | number value | string value
  54. local CMD_GUARD                 = CMD.GUARD                 --expect 1 parameters in return (unitid)
  55.  
  56. -----------------------------------------------------------------
  57.  
  58.  
  59.  
  60. -----------------------------------------------------------------
  61.  
  62. local function Distance(x1,z1,x2,z2)
  63.     return math.sqrt((x1-x2)^2 + (z1-z2)^2)
  64. end
  65.  
  66. local function GetUnitName(unitID)
  67.     if spValidUnitID(unitID)==true and spGetUnitDefID(unitID) then
  68.         return UnitDefs[spGetUnitDefID(unitID)].name
  69.     end
  70. end
  71.  
  72. -----------------------------------------------------------------
  73.  
  74. local ActivelyFollowingUnitsTable = {}
  75.  
  76. function widget:UnitCommand(unitID, unitDefID, unitTeam, cmdId, cmdOpts, cmdParams,cmdTag)
  77. --spEcho(cmdId,cmdOpts,cmdParams[1])
  78.     if cmdId==CMD_GUARD and cmdOpts==CMD.OPT_ALT then
  79.         --spEcho(cmdId,cmdOpts)
  80.         local targetID = cmdParams[1] --the target unit to follow
  81.         local followerID = unitID --the unit following the target
  82.         ActivelyFollowingUnitsTable[followerID]=targetID
  83.     end
  84.     if cmdId==0 and cmdParams[1]==nil then --give a stop order
  85.         ActivelyFollowingUnitsTable[unitID]=nil --remove selected followers from widget's control
  86.     end
  87. end
  88.  
  89.  
  90. function widget:GameFrame(frameNum)
  91.     if frameNum%15==0 then
  92.         for followerID,targetID in pairs(ActivelyFollowingUnitsTable) do
  93.         --spEcho(followerID, targetID, spValidUnitID(followerID), spValidUnitID(targetID))
  94.             if spValidUnitID(followerID)==true and spValidUnitID(targetID)==true then
  95.             local x,y,z = spGetUnitPosition(targetID)
  96.                 --spEcho(followerID, targetID)
  97.                 spGiveOrderToUnit(followerID, CMD_MOVE, {x,y,z}, {"alt"})
  98.             end
  99.         end
  100.     end
  101. end
  102.  
  103.  
  104.  
  105.  
  106. --[[
  107.  
  108. Follow And Shove Command
  109.  
  110. hotkey plus guard command will set the guarded unit as the mappos data source for move orders every second.
  111.  
  112. uses altkey+gaurd
  113.  
  114. v1.1 changelog
  115. update speed increased to twice per second
  116.  
  117. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement