LJLim

Trophy Hunter widget modified

Dec 13th, 2014
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.03 KB | None | 0 0
  1. function widget:GetInfo()
  2.   return {
  3.     name      = "Trophy Hunter AI",
  4.     desc      = "Any unit set to hold-fire will target the most expensive enemy in range, with specific exceptions",
  5.     author    = "AutoWar",
  6.     date      = "2014",
  7.     license   = "GNU GPL, v2 or later",
  8.     layer     = 9999,
  9.     enabled   = true
  10.   }
  11. end
  12.  
  13. ----------------------------------------------------
  14. --Global Localizations of Variables and Functions and Initialization of Global Tables
  15.  
  16. --Pseudocode MetalCostByUnit = {manual table}
  17. local MetalCostOfAllUnits={}
  18. for name, def in pairs(UnitDefNames) do
  19.     MetalCostOfAllUnits[name] = def.metalCost
  20. end
  21.  
  22. --Pseudocode AttackRangeByUnit = {manual table}
  23. local AttackRangeOfAllUnits=
  24. {
  25. amphaa=800,
  26. amphassault=600,
  27. amphbomb=200,
  28. amphfloater=450,
  29. amphraider=400,
  30. amphraider2=300,
  31. amphraider3=250,
  32. amphriot=430,
  33. armaak=1040,
  34. armamd=4500,
  35. armanni=1200,
  36. armartic=460,
  37. armbanth=1200,
  38. armbrawl=650,
  39. armbrtha=6200,
  40. armcarry=3500,
  41. armcir=1800,
  42. armcom1=300,
  43. armcomdgun=250,
  44. armcrabe=600,
  45. armcybr=500,
  46. armdeva=410,
  47. armflea=140,
  48. armham=840,
  49. armjeth=700,
  50. armkam=240,
  51. armmanni=1020,
  52. armmerl=1500,
  53. armorco=925,
  54. armpb=560,
  55. armpw=185,
  56. armraven=1450,
  57. armraz=400,
  58. armrock=455,
  59. armsnipe=700,
  60. armsptk=540,
  61. armspy=100,
  62. armstiletto_laser=730,
  63. armwar=270,
  64. armzeus=280,
  65. arm_venom=240,
  66. assaultcruiser=450,
  67. attackdrone=400,
  68. battledrone=450,
  69. benzcom1=360,
  70. blackdawn=300,
  71. bladew=80,
  72. bomberassault=500,
  73. bomberdive=600,
  74. bomberlaser=1000,
  75. bomberstrike=360,
  76. capturecar=450,
  77. carrydrone=360,
  78. commrecon1=400,
  79. commsupport1=450,
  80. comm_sonic=320,
  81. coracv=220,
  82. corak=245,
  83. corbats=1400,
  84. corbhmth=1850,
  85. corbtrans=125,
  86. corclog=50,
  87. corcom1=290,
  88. corcrash=880,
  89. corcrw=450,
  90. cordoom=650,
  91. corfast=320,
  92. corfav=180,
  93. corflak=1000,
  94. corgarp=800,
  95. corgator=270,
  96. corgol=450,
  97. corgrav=460,
  98. corhlt=620,
  99. corlevlr=290,
  100. corllt=460,
  101. cormak=300,
  102. cormart=1180,
  103. cormist=600,
  104. cornukesub=3000,
  105. corpre=460,
  106. corpyro=280,
  107. corpyro2=260,
  108. corrad=600,
  109. corraid=320,
  110. corrazor=1040,
  111. correap=360,
  112. corrl=610,
  113. corsent=900,
  114. corsh=220,
  115. corshad=600,
  116. corsilo=72000,
  117. corstorm=530,
  118. corsumo=390,
  119. corthud=280,
  120. corvamp=800,
  121. cremcom1=300,
  122. dante=460,
  123. destroyer=420,
  124. empmissile=3500,
  125. fighter=700,
  126. fighterdrone=500,
  127. firewalker=900,
  128. funnelweb=220,
  129. gorg=550,
  130. gunshipaa=750,
  131. gunshipsupport=360,
  132. heavyturret=750,
  133. hoveraa=800,
  134. hoverassault=200,
  135. hoverdepthcharge=270,
  136. hoverminer=300,
  137. hoverriot=355,
  138. hoverscout=220,
  139. hovershotgun=250,
  140. hoverskirm=360,
  141. hoversonic=320,
  142. iwin=30000,
  143. jumpblackhole=475,
  144. jumpblackhole2=300,
  145. jumpimpulse=500,
  146. jumpriot=280,
  147. logkoda=225,
  148. mahlazer=9000,
  149. missiletower=850,
  150. napalmmissile=3500,
  151. nebula=450,
  152. nsaclash=450,
  153. panther=250,
  154. puppy=170,
  155. raveparty=7500,
  156. roost=600,
  157. scorpion=470,
  158. screamer=2400,
  159. seismic=6000,
  160. shieldarty=940,
  161. shieldfelon=430,
  162. shipaa=1040,
  163. shiparty=1000,
  164. shipraider=280,
  165. shipscout=260,
  166. shipskirm=690,
  167. shiptorp=340,
  168. slowmort=600,
  169. spherepole=100,
  170. spideraa=1000,
  171. spideranarchid=270,
  172. spiderassault=350,
  173. spiderriot=300,
  174. sprinkler=900,
  175. subarty=900,
  176. subraider=290,
  177. tacnuke=3500,
  178. tankriotraider=240,
  179. tawf114=430,
  180. tiptest=320,
  181. trem=1300,
  182. turrettorp=590,
  183. vehaa=730,
  184. vehdisable=520,
  185. wolverine_mine=115,
  186. zenith=9000
  187. }
  188.  
  189.  
  190. local TrophyHunter = {}
  191. local MyTeamID = Spring.GetMyTeamID()
  192. local MyUnits = {}
  193.  
  194. ----------------------------------------------------
  195. --Function Definitions
  196.  
  197. function GetMyUnits(unitID, unitDefID, unitTeam) --activate when unit completed
  198.     if unitTeam==MyTeamID then
  199.     local unitname = UnitDefs[unitDefID].name
  200.         MyUnits[unitID]=AttackRangeOfAllUnits[unitname] --unitID stored as key, attack range for that unit stored as value
  201.         local unitDefID = Spring.GetUnitDefID(unitID)
  202.         local unitname = UnitDefs[unitDefID].name
  203.         Spring.Echo(unitname, "is added to the list of my units!")
  204.     end
  205. end
  206.  
  207.  
  208. function GetAndRemoveTrophyHunter() --activate every second
  209.     for MyUnitsIDkey, MyUnitsAttackRangeValue in pairs(MyUnits) do
  210.     local UnitFirestate=Spring.GetUnitStates(MyUnitsIDkey).firestate
  211.         if UnitFirestate==0 then  --hold fire = 0
  212.             TrophyHunter[MyUnitsIDkey]=MyUnitsAttackRangeValue
  213.             local unitDefID = Spring.GetUnitDefID(MyUnitsIDkey)
  214.             local unitname = UnitDefs[unitDefID].name
  215.             Spring.Echo(unitname, "is added to the list of trophy hunters!")
  216.         else
  217.             TrophyHunter[MyUnitsIDkey]=nil
  218.         end
  219.     end
  220. end
  221.  
  222.  
  223. function CheckAttackBiggestTrophy() --activate every second
  224.     for TrophyHunterUnitIDkey,AttackRangeValue in pairs(TrophyHunter) do
  225.         local biggestTrophyID = 1
  226.         local Trophy = {}
  227.         local x,y,z = Spring.GetUnitPosition (TrophyHunterUnitIDkey)
  228.         if (x and y and z) then -- check for valid position
  229.             local UnitsInRangeSphere=Spring.GetUnitsInSphere(x,y,z,AttackRangeValue)
  230.             for key,unitIDvalue in pairs(UnitsInRangeSphere) do --this for loop determines enemy units nearby the current unitID
  231.                 local unitDefID = Spring.GetUnitDefID(unitIDvalue)
  232.                 local unitname = UnitDefs[unitDefID].name
  233.                 local metalCost = MetalCostOfAllUnits[unitname]
  234.                 if Spring.IsUnitAllied(unitIDvalue)==false then
  235.                     Trophy[metalCost]=unitIDvalue
  236.                 end
  237.             end
  238.             for enemyMetalCostkey, unitIDvalue in pairs(Trophy) do --this for loop determines the highest metal cost unitID returned by the previous for loop
  239.                 local unitDefID = Spring.GetUnitDefID(unitIDvalue)
  240.                 local unitname = UnitDefs[unitDefID].name
  241.                 local metalCost = MetalCostOfAllUnits[unitname]
  242.                 if metalCost<enemyMetalCostkey then
  243.                     metalCost=enemyMetalCostkey
  244.                 end
  245.                 biggestTrophyID = Trophy[metalCost]
  246.             end
  247.         end
  248.         Spring.GiveOrderToUnit(TrophyHunterUnitIDkey, CMD.INSERT,  {-1,CMD.ATTACK,CMD.OPT_SHIFT,biggestTrophy}, {"alt"})
  249.     end
  250. end
  251.  
  252. ----------------------------------------------------
  253. --Widget Call-Ins
  254.  
  255. function widget:UnitFinished(unitID, unitDefID, unitTeam)
  256.     GetMyUnits(unitID, unitDefID, unitTeam)
  257. end
  258.  
  259. function widget:UnitDestroyed(unitID, unitDefID, unitTeam)
  260.     if MyUnits[unitID] then
  261.         MyUnits[unitID]=nil
  262.     end
  263. end
  264.  
  265. function widget:GameFrame(frameNum)
  266.     if frameNum%30==0 then
  267.         GetAndRemoveTrophyHunter()
  268.         CheckAttackBiggestTrophy()
  269.     end
  270. end
Add Comment
Please, Sign In to add comment