I got mine from Mr Dizekat himself, probably about a year ago, can't remember if I've updated it since, but it still works fine for me. Here's the full thing:
Code: Select all
function widget:GetInfo()
return {
name = "Bomber control",
desc = "Makes bombers execute queue properly",
author = "dizekat",
date = "2010-02-04",
license = "GPL v2 or later",
layer = 5,
enabled = true
}
end
options={
hoverbomb = {
name = 'Hover the bomber above target',
desc = '',
type = 'bool',
value = true
},
release_all = {
name = 'Wait for all bombs',
desc = '',
type = 'bool',
value = false
}
}
local bombers={armpnix=true, armcybr=true, corgripn=true, armthund=true, armsb=true, corhurc=true, corsb=true, corshad=true, cortitan=true}
local bomber_uds={}
local GetUnitWeaponState = Spring.GetUnitWeaponState
local GetUnitDefID = Spring.GetUnitDefID
local GiveOrderToUnit = Spring.GiveOrderToUnit
local GetGameFrame = Spring.GetGameFrame
local GetMyTeamID = Spring.GetMyTeamID
local GetUnitTeam = Spring.GetUnitTeam
local GetGameFrame = Spring.GetGameFrame
local GetCommandQueue = Spring.GetCommandQueue
local GetUnitStates = Spring.GetUnitStates
local my_bombers={}
local function AddUnit(unit_id, unit_udid_)
local unit_udid=unit_udid_
if GetUnitTeam(unit_id)==GetMyTeamID() then --my unit
if unit_udid==nil then
unit_udid=GetUnitDefID(unit_id)
end
local ud=UnitDefs[unit_udid]
if ud and bomber_uds[unit_udid] then
if ud.primaryWeapon then
local _,reloaded_,reloadFrame = GetUnitWeaponState(unit_id,ud.primaryWeapon-1)
my_bombers[unit_id]={reloaded=reloaded_, reload_frame=0}
--Spring.Echo("bomber added")
end
end
end
end
function widget:UnitCreated(unit_id, unit_udid, unit_tid)
if unit_tid==Spring.GetMyTeamID() then
AddUnit(unit_id,unit_udid)
end
end
local function RemoveUnit(unit_id)
my_bombers[unit_id]=nil
end
function widget:UnitDestroyed(unit_id, unit_udid, unit_tid)
RemoveUnit(unit_id)
end
function widget:UnitGiven(unit_id, unit_udid, old_team, new_team)
RemoveUnit(unit_id)
if new_team==GetMyTeamID() then
AddUnit(unit_id,unit_udid)
end
end
local function UpdateUnitsList()
local my_team=GetMyTeamID()
my_bombers={}
for _,unit_id in ipairs(Spring.GetTeamUnits(my_team)) do
AddUnit(unit_id,nil)
end
end
function widget:Initialize()
for i,ud in pairs(UnitDefs) do
if bombers[ud.name] then
bomber_uds[i]=ud
--bomber_uds[i]=true
ud.reloadTime = 0
ud.primaryWeapon = 0
ud.shieldPower = 0
for i=1,ud.weapons.n do
local WeaponDefID = ud.weapons[i].weaponDef;
local WeaponDef = WeaponDefs[ WeaponDefID ];
if (WeaponDef.reload>ud.reloadTime) then
ud.reloadTime = WeaponDef.reload;
ud.primaryWeapon = i;
end
end
end
end
UpdateUnitsList()
end
local current_team=1234567
function widget:CommandNotify(id, params, options)
local selected_units = Spring.GetSelectedUnits()
for i,unit_id in ipairs(selected_units) do
local bomber_data=my_bombers[unit_id]
if bomber_data then
bomber_data.on_reloaded=nil
if bomber_data.must_clear_wait then
bomber_data.must_clear_wait=nil
GiveOrderToUnit(unit_id, CMD.WAIT,{},{})
end
end
end
end
function widget:Update(dt)
local hoverbomb=options.hoverbomb.value
local gameFrame = GetGameFrame()
if ((gameFrame%15)<1) then
local my_team=GetMyTeamID()
if my_team~=current_team then
current_team=my_team
UpdateUnitsList()
end
end
if ((gameFrame%3)<1) then
for bomber_id,bomber_data in pairs(my_bombers) do
local udid = GetUnitDefID(bomber_id)
local ud = UnitDefs[udid or -1]
if ud and ud.primaryWeapon then
local _,reloaded,reload_frame,salvo_left = GetUnitWeaponState(bomber_id, ud.primaryWeapon-1)
local vx,vy,vz=Spring.GetUnitVelocity(bomber_id)
local speed_sq=vx*vx+vz*vz
--Spring.Echo(salvo_left)
bomber_data.did_shot=bomber_data.did_shot or (bomber_data.reloaded and not reloaded) or (bomber_data.reload_frame~=reload_frame)
bomber_data.reloaded=reloaded
bomber_data.reload_frame=reload_frame
if reloaded and bomber_data.on_reloaded then
GiveOrderToUnit(bomber_id, bomber_data.on_reloaded.id, bomber_data.on_reloaded.params, {})
end
if bomber_data.did_shot and (salvo_left==0 or (not options.release_all.value) or (speed_sq<9)) then
bomber_data.did_shot=false
local commands=GetCommandQueue(bomber_id)
local set_wait=false
if commands and commands[1] and commands[1].id==CMD.ATTACK then
local got_next_orders=false
for i=2,#commands do
if commands[i].id ~= CMD.SET_WANTED_MAX_SPEED then
got_next_orders=true
break
end
end
--Spring.Echo(CMD[commands[2].id])
local states=GetUnitStates(bomber_id)
if got_next_orders then
bomber_data.on_reloaded=nil
GiveOrderToUnit(bomber_id, CMD.REMOVE,{commands[1].tag},{})
if states and (states['repeat']) then
GiveOrderToUnit(bomber_id, commands[1].id,commands[1].params,{'shift'})
end
elseif hoverbomb and (not (states and (states['repeat']))) then
bomber_data.on_reloaded=commands[1]
set_wait=true
bomber_data.must_clear_wait=true
end
--- two waits in sequence:
--- workaround for mantis 1823
if not set_wait then
GiveOrderToUnit(bomber_id, CMD.WAIT,{},{})
end
GiveOrderToUnit(bomber_id, CMD.WAIT,{},{})
end
end
end
end
end
end