Created
March 28, 2022 17:48
-
-
Save Beherith/c048b1a9312513ff4cd7ab331c59c40c to your computer and use it in GitHub Desktop.
Mash tab to cycle camera target between your commander and your first factory. Unbinds useless overview mode.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function widget:GetInfo() | |
return { | |
name = "Tab Comm Base", | |
desc = "Binds tab to swap between commander and base", | |
author = "Beherith", | |
date = "20220328", | |
license = "GNU GPL, v2 or later", | |
layer = 0, | |
enabled = true | |
} | |
end | |
local communitID = nil | |
local commdeathpos = nil | |
local basepos = nil | |
local speed = 0.25 | |
local armcomUnitDefID = UnitDefNames.armcom.id | |
local corcomUnitDefID = UnitDefNames.corcom.id | |
local spec, fullview = Spring.GetSpectatingState() | |
local myTeamID = Spring.GetMyTeamID() | |
if spec then return end | |
local function dist3d(a,b) | |
return math.sqrt( (a[1]-b[1])*(a[1]-b[1]) + (a[2]-b[2])*(a[2]-b[2]) + (a[3]-b[3])*(a[3]-b[3]) ) | |
end | |
local function tab_comm_base() | |
local camX, camY, camZ = Spring.GetCameraPosition() | |
local campos = {camX, camY, camZ} | |
local distancetocomm = nil | |
local distancetobase = nil | |
if communitID then | |
local dx, dy, dz = Spring.GetUnitPosition(communitID) | |
distancetocomm = dist3d(campos, {dx,dy,dz}) | |
elseif commdeathpos then | |
distancetocomm = dist3d(campos, commdeathpos) | |
end | |
if basepos then | |
distancetobase = dist3d(campos, basepos) | |
end | |
if (distancetocomm and distancetobase and (distancetocomm > distancetobase)) | |
or (distancetocomm and (distancetobase == nil)) then | |
if communitID then | |
local dx, dy, dz = Spring.GetUnitPosition(communitID) | |
Spring.SetCameraTarget(dx, dy, dz, speed) | |
elseif commdeathpos then | |
Spring.SetCameraTarget(commdeathpos[1], commdeathpos[2], commdeathpos[3], speed) | |
end | |
elseif distancetobase then | |
Spring.SetCameraTarget(basepos[1], basepos[2], basepos[3], speed) | |
end | |
end | |
function widget:Initialize() | |
Spring.SendCommands({"unbind Any+tab toggleoverview" }) | |
widgetHandler:AddAction("tab_comm_base", tab_comm_base, nil, 'p') | |
Spring.SendCommands({"bind Any+tab tab_comm_base" }) | |
for _, unitID in pairs(Spring.GetTeamUnits(myTeamID)) do | |
local unitDefID = Spring.GetUnitDefID(unitID) | |
if unitDefID and (unitDefID == armcomUnitDefID or unitDefID == corcomUnitDefID) then | |
communitID = unitID | |
end | |
end | |
end | |
function widget:Shutdown() | |
Spring.SendCommands({"unbind Any+tab tab_comm_base" }) | |
widgetHandler:RemoveAction("tab_comm_base") | |
Spring.SendCommands({"bind Any+tab toggleoverview" }) | |
end | |
function widget:UnitDestroyed(unitID) | |
if unitID == communitID then | |
local dx, dy, dz = Spring.GetUnitPosition(unitID) | |
communitID = nil | |
commdeathpos = {dx, dy, dz} | |
end | |
end | |
function widget:UnitCreated(unitID, unitDefID, unitTeam, builderID) | |
if communitID == nil then | |
if unitTeam == myTeamID and (unitDefID == armcomUnitDefID or unitDefID == corcomUnitDefID) then | |
communitID = unitID | |
commdeathpos = nil | |
end | |
end | |
if basepos == nil then | |
if UnitDefs[unitDefID].isFactory then | |
local dx, dy, dz = Spring.GetUnitPosition(unitID) | |
basepos = {dx, dy, dz} | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment