Advertisement
Guest User

Untitled

a guest
Jun 4th, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1.  
  2. function widget:GetInfo()
  3. return {
  4. name = "DGun Stall Assist",
  5. desc = "Waits cons/facs when trying to dgun and stalling",
  6. author = "Niobium",
  7. date = "2 April 2010",
  8. license = "GNU GPL, v2 or later",
  9. layer = 0,
  10. enabled = true -- loaded by default?
  11. }
  12. end
  13.  
  14. ----------------------------------------------------------------
  15. -- Config
  16. ----------------------------------------------------------------
  17. local targetEnergy = 600
  18. local watchForTime = 5
  19.  
  20. ----------------------------------------------------------------
  21. -- Globals
  22. ----------------------------------------------------------------
  23. local watchTime = 0
  24. local waitedUnits = nil -- nil / waitedUnits[1..n] = uID
  25. local shouldWait = {} -- shouldWait[uDefID] = true / nil
  26. local isFactory = {} -- isFactory[uDefID] = true / nil
  27.  
  28. ----------------------------------------------------------------
  29. -- Speedups
  30. ----------------------------------------------------------------
  31. local spGetActiveCommand = Spring.GetActiveCommand
  32. local spGiveOrderToUnitArray = Spring.GiveOrderToUnitArray
  33. local spGetUnitCommands = Spring.GetUnitCommands
  34. local spGetFactoryCommands = Spring.GetFactoryCommands
  35. local spGetMyTeamID = Spring.GetMyTeamID
  36. local spGetTeamResources = Spring.GetTeamResources
  37. local spGetTeamUnits = Spring.GetTeamUnits
  38. local spGetUnitDefID = Spring.GetUnitDefID
  39. local spGetSpectatingState = Spring.GetSpectatingState
  40.  
  41. local CMD_DGUN = CMD.DGUN
  42. local CMD_WAIT = CMD.WAIT
  43.  
  44. ----------------------------------------------------------------
  45. -- Callins
  46. ----------------------------------------------------------------
  47. function widget:Initialize()
  48.  
  49. if spGetSpectatingState() then
  50. widgetHandler:RemoveWidget(self)
  51. return
  52. end
  53.  
  54. for uDefID, uDef in pairs(UnitDefs) do
  55. if (uDef.buildSpeed > 0) and uDef.canAssist and (not uDef.canDGun) then
  56. shouldWait[uDefID] = true
  57. if uDef.isFactory then
  58. isFactory[uDefID] = true
  59. end
  60. end
  61. end
  62. end
  63.  
  64. function widget:Update(dt)
  65.  
  66. local _, activeCmdID = spGetActiveCommand()
  67. if activeCmdID == CMD_DGUN then
  68. watchTime = watchForTime
  69. else
  70. watchTime = watchTime - dt
  71.  
  72. if waitedUnits and (watchTime < 0) then
  73.  
  74. local toUnwait = {}
  75. for i = 1, #waitedUnits do
  76. local uID = waitedUnits[i]
  77. local uDefID = spGetUnitDefID(uID)
  78. local uCmds = isFactory[uDefID] and spGetFactoryCommands(uID, 1) or spGetUnitCommands(uID, 1)
  79. if uCmds and (#uCmds > 0) and (uCmds[1].id == CMD_WAIT) then
  80. toUnwait[#toUnwait + 1] = uID
  81. end
  82. end
  83. spGiveOrderToUnitArray(toUnwait, CMD_WAIT, {}, {})
  84.  
  85. waitedUnits = nil
  86. end
  87. end
  88.  
  89. if (watchTime > 0) and (not waitedUnits) then
  90.  
  91. if spGetSpectatingState() then
  92. widgetHandler:RemoveWidget(self)
  93. return
  94. end
  95.  
  96. local myTeamID = spGetMyTeamID()
  97. local currentEnergy, energyStorage = spGetTeamResources(myTeamID, "energy")
  98. if (currentEnergy < targetEnergy) and (energyStorage >= targetEnergy) then
  99.  
  100. waitedUnits = {}
  101. local myUnits = spGetTeamUnits(myTeamID)
  102. for i = 1, #myUnits do
  103. local uID = myUnits[i]
  104. local uDefID = spGetUnitDefID(uID)
  105. if shouldWait[uDefID] then
  106. local uCmds = isFactory[uDefID] and spGetFactoryCommands(uID, 1) or spGetUnitCommands(uID, 1)
  107. if (#uCmds == 0) or (uCmds[1].id ~= CMD_WAIT) then
  108. waitedUnits[#waitedUnits + 1] = uID
  109. end
  110. end
  111. end
  112. spGiveOrderToUnitArray(waitedUnits, CMD_WAIT, {}, {})
  113. end
  114. end
  115. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement