Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function widget:GetInfo()
- return {
- name = "Trophy Hunter AI",
- desc = "Any unit set to hold-fire will target the most expensive enemy in range, with specific exceptions",
- author = "AutoWar",
- date = "2014",
- license = "GNU GPL, v2 or later",
- layer = 9999,
- enabled = true
- }
- end
- ----------------------------------------------------
- --Global Localizations of Variables and Functions and Initialization of Global Tables
- --Pseudocode MetalCostByUnit = {manual table}
- local MetalCostOfAllUnits={}
- for name, def in pairs(UnitDefNames) do
- MetalCostOfAllUnits[name] = def.metalCost
- end
- --Pseudocode AttackRangeByUnit = {manual table}
- local AttackRangeOfAllUnits=
- {
- amphaa=800,
- amphassault=600,
- amphbomb=200,
- amphfloater=450,
- amphraider=400,
- amphraider2=300,
- amphraider3=250,
- amphriot=430,
- armaak=1040,
- armamd=4500,
- armanni=1200,
- armartic=460,
- armbanth=1200,
- armbrawl=650,
- armbrtha=6200,
- armcarry=3500,
- armcir=1800,
- armcom1=300,
- armcomdgun=250,
- armcrabe=600,
- armcybr=500,
- armdeva=410,
- armflea=140,
- armham=840,
- armjeth=700,
- armkam=240,
- armmanni=1020,
- armmerl=1500,
- armorco=925,
- armpb=560,
- armpw=185,
- armraven=1450,
- armraz=400,
- armrock=455,
- armsnipe=700,
- armsptk=540,
- armspy=100,
- armstiletto_laser=730,
- armwar=270,
- armzeus=280,
- arm_venom=240,
- assaultcruiser=450,
- attackdrone=400,
- battledrone=450,
- benzcom1=360,
- blackdawn=300,
- bladew=80,
- bomberassault=500,
- bomberdive=600,
- bomberlaser=1000,
- bomberstrike=360,
- capturecar=450,
- carrydrone=360,
- commrecon1=400,
- commsupport1=450,
- comm_sonic=320,
- coracv=220,
- corak=245,
- corbats=1400,
- corbhmth=1850,
- corbtrans=125,
- corclog=50,
- corcom1=290,
- corcrash=880,
- corcrw=450,
- cordoom=650,
- corfast=320,
- corfav=180,
- corflak=1000,
- corgarp=800,
- corgator=270,
- corgol=450,
- corgrav=460,
- corhlt=620,
- corlevlr=290,
- corllt=460,
- cormak=300,
- cormart=1180,
- cormist=600,
- cornukesub=3000,
- corpre=460,
- corpyro=280,
- corpyro2=260,
- corrad=600,
- corraid=320,
- corrazor=1040,
- correap=360,
- corrl=610,
- corsent=900,
- corsh=220,
- corshad=600,
- corsilo=72000,
- corstorm=530,
- corsumo=390,
- corthud=280,
- corvamp=800,
- cremcom1=300,
- dante=460,
- destroyer=420,
- empmissile=3500,
- fighter=700,
- fighterdrone=500,
- firewalker=900,
- funnelweb=220,
- gorg=550,
- gunshipaa=750,
- gunshipsupport=360,
- heavyturret=750,
- hoveraa=800,
- hoverassault=200,
- hoverdepthcharge=270,
- hoverminer=300,
- hoverriot=355,
- hoverscout=220,
- hovershotgun=250,
- hoverskirm=360,
- hoversonic=320,
- iwin=30000,
- jumpblackhole=475,
- jumpblackhole2=300,
- jumpimpulse=500,
- jumpriot=280,
- logkoda=225,
- mahlazer=9000,
- missiletower=850,
- napalmmissile=3500,
- nebula=450,
- nsaclash=450,
- panther=250,
- puppy=170,
- raveparty=7500,
- roost=600,
- scorpion=470,
- screamer=2400,
- seismic=6000,
- shieldarty=940,
- shieldfelon=430,
- shipaa=1040,
- shiparty=1000,
- shipraider=280,
- shipscout=260,
- shipskirm=690,
- shiptorp=340,
- slowmort=600,
- spherepole=100,
- spideraa=1000,
- spideranarchid=270,
- spiderassault=350,
- spiderriot=300,
- sprinkler=900,
- subarty=900,
- subraider=290,
- tacnuke=3500,
- tankriotraider=240,
- tawf114=430,
- tiptest=320,
- trem=1300,
- turrettorp=590,
- vehaa=730,
- vehdisable=520,
- wolverine_mine=115,
- zenith=9000
- }
- local TrophyHunter = {}
- local MyTeamID = Spring.GetMyTeamID()
- local MyUnits = {}
- ----------------------------------------------------
- --Function Definitions
- function GetMyUnits(unitID, unitDefID, unitTeam) --activate when unit completed
- if unitTeam==MyTeamID then
- local unitname = UnitDefs[unitDefID].name
- MyUnits[unitID]=AttackRangeOfAllUnits[unitname] --unitID stored as key, attack range for that unit stored as value
- local unitDefID = Spring.GetUnitDefID(unitID)
- local unitname = UnitDefs[unitDefID].name
- Spring.Echo(unitname, "is added to the list of my units!")
- end
- end
- function GetAndRemoveTrophyHunter() --activate every second
- for MyUnitsIDkey, MyUnitsAttackRangeValue in pairs(MyUnits) do
- local UnitFirestate=Spring.GetUnitStates(MyUnitsIDkey).firestate
- if UnitFirestate==0 then --hold fire = 0
- TrophyHunter[MyUnitsIDkey]=MyUnitsAttackRangeValue
- local unitDefID = Spring.GetUnitDefID(MyUnitsIDkey)
- local unitname = UnitDefs[unitDefID].name
- Spring.Echo(unitname, "is added to the list of trophy hunters!")
- else
- TrophyHunter[MyUnitsIDkey]=nil
- end
- end
- end
- function CheckAttackBiggestTrophy() --activate every second
- for TrophyHunterUnitIDkey,AttackRangeValue in pairs(TrophyHunter) do
- local biggestTrophyID = 1
- local Trophy = {}
- local x,y,z = Spring.GetUnitPosition (TrophyHunterUnitIDkey)
- if (x and y and z) then -- check for valid position
- local UnitsInRangeSphere=Spring.GetUnitsInSphere(x,y,z,AttackRangeValue)
- for key,unitIDvalue in pairs(UnitsInRangeSphere) do --this for loop determines enemy units nearby the current unitID
- local unitDefID = Spring.GetUnitDefID(unitIDvalue)
- local unitname = UnitDefs[unitDefID].name
- local metalCost = MetalCostOfAllUnits[unitname]
- if Spring.IsUnitAllied(unitIDvalue)==false then
- Trophy[metalCost]=unitIDvalue
- end
- end
- for enemyMetalCostkey, unitIDvalue in pairs(Trophy) do --this for loop determines the highest metal cost unitID returned by the previous for loop
- local unitDefID = Spring.GetUnitDefID(unitIDvalue)
- local unitname = UnitDefs[unitDefID].name
- local metalCost = MetalCostOfAllUnits[unitname]
- if metalCost<enemyMetalCostkey then
- metalCost=enemyMetalCostkey
- end
- biggestTrophyID = Trophy[metalCost]
- end
- end
- Spring.GiveOrderToUnit(TrophyHunterUnitIDkey, CMD.INSERT, {-1,CMD.ATTACK,CMD.OPT_SHIFT,biggestTrophy}, {"alt"})
- end
- end
- ----------------------------------------------------
- --Widget Call-Ins
- function widget:UnitFinished(unitID, unitDefID, unitTeam)
- GetMyUnits(unitID, unitDefID, unitTeam)
- end
- function widget:UnitDestroyed(unitID, unitDefID, unitTeam)
- if MyUnits[unitID] then
- MyUnits[unitID]=nil
- end
- end
- function widget:GameFrame(frameNum)
- if frameNum%30==0 then
- GetAndRemoveTrophyHunter()
- CheckAttackBiggestTrophy()
- end
- end
Add Comment
Please, Sign In to add comment