local trigger = 150 -- Commander health under which plan B is activated. function widget:GetInfo() return { name = "Plan B", desc = "Gives all your units except the Commander to an ally when the last commander's health drops below "..trigger..". Bind a key to /luaui kamikaze to manually activate.", author = "quantum", date = "Jan 8, 2007", license = "GNU GPL, v2 or later", layer = 0, enabled = true -- loaded by default? } end local commIDs = {} function widget:Initialize() for _,uid in ipairs(Spring.GetTeamUnits(Spring.GetMyTeamID())) do local udid = Spring.GetUnitDefID(uid) local ud = udid and UnitDefs[udid] or nil if (ud and ud.isCommander) then commIDs[uid] = Spring.GetUnitHealth(uid) print("Added commander: "..uid.." "..commIDs[uid]) end end end commTable = {} function Say(msg) Spring.SendCommands({'say ' .. msg}) end function echo(msg) Spring.SendCommands({"echo " .. msg}) end function widget:Update(t) commTable = {} for i, uid in ipairs(Spring.GetTeamUnits(Spring.GetMyTeamID())) do if UnitDefs[Spring.GetUnitDefID(uid)].isCommander then table.insert(commTable, uid) end end n = table.getn(commTable) -- echo("commTable: "..n) for uid, v in pairs(commIDs) do -- echo("Comm health: "..Spring.GetUnitHealth(uid)) if Spring.GetUnitHealth(uid) ~= nil and Spring.GetUnitHealth(uid) < trigger and n == 1 and (Game.commEnds) then Kamikaze() end end end function widget:TextCommand(command) if command ~= 'kamikaze' then return false else Kamikaze() end return true end function Kamikaze() teamList = Spring.GetTeamList(Spring.GetMyAllyTeamID()) for i, v in ipairs(teamList) do if v ~= Spring.GetMyTeamID() then ally = v end end if ally == nil then echo("With me ends my alliance.") return true end Say("Executing Plan B. Transferring units to "..Spring.GetPlayerInfo(ally)..".") teamUnits = Spring.GetTeamUnits(Spring.GetMyTeamID()) for i, v in ipairs(teamUnits) do if UnitDefs[Spring.GetUnitDefID(v)].isCommander == false then Spring.SelectUnitsByValues({i, v}) Spring.ShareResources(ally, "units") end end echo("Units given to player "..ally) widgetHandler:RemoveWidget() end function widget:UnitCreated(unitID, unitDefID) if (Spring.GetUnitTeam(unitID) ~= Spring.GetMyTeamID()) then return end local ud = UnitDefs[unitDefID] if (ud and ud.isCommander) then commIDs[unitID] = Spring.GetUnitHealth(unitID) --print("Added commander: "..unitID.." "..commIDs[unitID]) end end function widget:UnitDestroyed(unitID, unitDefID) table.remove(commTable, unitID) commIDs[unitID] = nil end function widget:UnitGiven(unitID2, unitDefID2, unitTeam2) if (ud and ud.isCommander) then commIDs[unitID2] = -1 --print("Added commander: "..unitID.." "..commIDs[unitID]) end end function widget:UnitTaken(unitID, unitDefID, unitTeam) widget:UnitDestroyed(unitID, unitDefID) end function widget:UnitGiven(unitID, unitDefID, unitTeam) widget:UnitCreated(unitID, unitDefID) end -- function widget:UnitChangedTeam(unitID, unitDefID, oldTeam, newTeam) -- if (oldTeam == newTeam) then -- return -- end -- local myTeam = Spring.GetMyTeamID() -- if (oldTeam == myTeam) then -- commIDs[unitID] = nil -- elseif (newTeam == myTeam) then -- local ud = UnitDefs[unitDefID] -- if (ud and ud.isCommander) then -- commIDs[unitID] = -1 -- print("Added commander: "..unitID.." "..commIDs[unitID]) -- end -- end -- end