Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function widget:GetInfo()
- return {
- name = "Team South",
- desc = "Center the battlefield on the line from your start zone to the front",
- author = "Zero-K community widget",
- layer = 10,
- enabled = true,
- }
- end
- local getRawBoxes = VFS.Include("LuaUI/Headers/startbox_utilities.lua")
- -- Camera and startup tuning
- local CARDINAL_LOCK = "CamSpringLockCardinalDirections"
- local EDGE_SNAP = math.tan(math.rad(10))
- local MIN_STRIP_OVERLAP = 0.5
- local MIN_STRIP_ASPECT = 2
- local PREGAME_CAMERA_DELAY = 0.45
- local CAMERA_READY_SCREEN_FRAME = 3
- local PI = math.pi
- local MAP_X, MAP_Z = Game.mapSizeX, Game.mapSizeZ
- local MID_X, MID_Z = MAP_X * 0.5, MAP_Z * 0.5
- local changedCardinalLock = false
- local screenFrames = 0
- local firstPregameDraw
- local function isPlayer()
- return not Spring.GetSpectatingState() and not Spring.IsReplay()
- end
- -- Start zones and battle direction
- local function inMap(x, z)
- return type(x) == "number" and type(z) == "number"
- and x >= 0 and x <= MAP_X and z >= 0 and z <= MAP_Z
- and (x ~= 0 or z ~= 0)
- end
- local function startZoneOrigin(ally)
- local gaia = Spring.GetGaiaTeamID()
- for _, team in ipairs(Spring.GetTeamList(ally) or {}) do
- if team ~= gaia then
- -- Zero-K stores the average of the assigned zone's recommended startpoints.
- local x = Spring.GetTeamRulesParam(team, "allyteam_origin_x")
- local z = Spring.GetTeamRulesParam(team, "allyteam_origin_z")
- if inMap(x, z) then
- return x, z, Spring.GetTeamRulesParam(team, "start_box_id")
- end
- end
- end
- end
- local function engineZoneOrigin(ally)
- local x0, z0, x1, z1 = Spring.GetAllyTeamStartBox(ally)
- if type(x0) ~= "number" or type(z0) ~= "number"
- or type(x1) ~= "number" or type(z1) ~= "number"
- or x0 < 0 or z0 < 0 or x1 > MAP_X or z1 > MAP_Z
- or x1 <= x0 or z1 <= z0 then return end
- -- Recoil returns the whole map when no start box was assigned.
- if x0 == 0 and z0 == 0 and x1 == MAP_X and z1 == MAP_Z then return end
- return (x0 + x1) * 0.5, (z0 + z1) * 0.5
- end
- local function zoneBounds(zone)
- if not zone then return end
- local left, top, right, bottom = 1, 1, 0, 0
- for _, polygon in ipairs(zone.boxes) do
- for _, point in ipairs(polygon) do
- if inMap(point[1], point[2]) or (point[1] == 0 and point[2] == 0) then
- local x, z = point[1] / MAP_X, point[2] / MAP_Z
- left, top = math.min(left, x), math.min(top, z)
- right, bottom = math.max(right, x), math.max(bottom, z)
- end
- end
- end
- if left < right and top < bottom then return left, top, right, bottom end
- end
- local function oppositeStrips(a0, a1, b0, b1, alongA0, alongA1, alongB0, alongB1)
- local spanA, spanB = alongA1 - alongA0, alongB1 - alongB0
- local overlap = math.min(alongA1, alongB1) - math.max(alongA0, alongB0)
- return ((a1 <= 0.5 and b0 >= 0.5) or (b1 <= 0.5 and a0 >= 0.5))
- and overlap >= MIN_STRIP_OVERLAP
- and spanA >= MIN_STRIP_ASPECT * (a1 - a0)
- and spanB >= MIN_STRIP_ASPECT * (b1 - b0)
- end
- local function getOppositeStripAxis(boxID, opponentBoxID)
- if boxID == nil or opponentBoxID == nil or boxID == opponentBoxID then return end
- local boxes = getRawBoxes()
- local left, top, right, bottom = zoneBounds(boxes[boxID])
- local otherLeft, otherTop, otherRight, otherBottom = zoneBounds(boxes[opponentBoxID])
- if not left or not otherLeft then return end
- -- A recommended point can be off-center even when the zones face each other along an edge.
- if oppositeStrips(left, right, otherLeft, otherRight, top, bottom, otherTop, otherBottom) then
- return "x", (top + bottom) * 0.5 * MAP_Z
- end
- if oppositeStrips(top, bottom, otherTop, otherBottom, left, right, otherLeft, otherRight) then
- return "z", (left + right) * 0.5 * MAP_X
- end
- end
- local function getBattleAxis(origin)
- local ally = Spring.GetMyAllyTeamID()
- if ally == nil or ally < 0 then return end
- local x, z, boxID = origin(ally)
- if not x then return end
- local goalX, goalZ = MID_X, MID_Z
- local gaia = Spring.GetGaiaTeamID()
- local otherAlly, opponents = nil, 0
- for _, id in ipairs(Spring.GetAllyTeamList()) do
- if id ~= ally then
- for _, team in ipairs(Spring.GetTeamList(id) or {}) do
- if team ~= gaia then
- otherAlly, opponents = id, opponents + 1
- break
- end
- end
- end
- end
- if opponents == 1 then
- local oppositeX, oppositeZ, oppositeBoxID = origin(otherAlly)
- if oppositeX then
- goalX, goalZ = oppositeX, oppositeZ
- local side, middle = getOppositeStripAxis(boxID, oppositeBoxID)
- if side == "x" then
- z, goalZ = middle, middle
- elseif side == "z" then
- x, goalX = middle, middle
- end
- end
- end
- local dx, dz = goalX - x, goalZ - z
- -- Stabilize near-cardinal matchups without rounding true diagonal starts.
- if math.abs(dx) <= math.abs(dz) * EDGE_SNAP then
- dx = 0
- elseif math.abs(dz) <= math.abs(dx) * EDGE_SNAP then
- dz = 0
- end
- local gap = math.sqrt(dx * dx + dz * dz)
- if gap > 1 then
- return {x = x, z = z, dx = dx / gap, dz = dz / gap, gap = gap}
- end
- end
- -- Camera framing
- local function getScreenCenterOnGround()
- local width, height = Spring.GetViewGeometry()
- local kind, point = Spring.TraceScreenRay(math.floor(width * 0.5), math.floor(height * 0.5), true, false, true)
- if kind == "ground" and point and inMap(point[1], point[3]) then
- return point[1], point[2], point[3]
- end
- end
- local function frameTeamAtBottom(axis)
- local state = Spring.GetCameraState()
- if not state then return false end
- local pitch, distance
- if state.name == "ta" then
- pitch, distance = PI - state.angle, state.height
- elseif state.name == "spring" then
- pitch, distance = state.rx, state.dist
- else
- local x, y, z = getScreenCenterOnGround()
- if x then
- local cx, cy, cz = Spring.GetCameraPosition()
- distance = math.sqrt((cx-x)^2 + (cy-y)^2 + (cz-z)^2)
- if distance > 1 then
- pitch = math.acos(math.max(-1, math.min(1, (y-cy) / distance)))
- end
- end
- end
- pitch = math.max(PI * 0.64, math.min(PI * 0.99, pitch or PI * 0.85))
- distance = math.max(20, distance or axis.gap * 0.8)
- -- Aim ahead along the front so the start projects below the screen center.
- local fov = state.fov or 45
- local screenOffset = 0.52 * math.tan(math.rad(fov) * 0.5)
- local advance = distance * screenOffset / (-math.cos(pitch) + screenOffset * math.sin(pitch))
- advance = math.min(advance, axis.gap * 0.66)
- local x, z = axis.x + axis.dx * advance, axis.z + axis.dz * advance
- local y = math.max(0, Spring.GetGroundHeight(x, z))
- local heading = math.atan2(axis.dx, -axis.dz)
- if state.name == "ta" and axis.dx == 0 then
- state.flipped = axis.dz > 0 and 1 or -1
- state.px, state.py, state.pz = x, y, z
- state.angle = PI - pitch
- elseif state.name == "free" and WG.COFC_Enabled and WG.COFC_SetCameraState then
- local back = distance * math.sin(pitch)
- state.px, state.py, state.pz = x - axis.dx * back, y - distance * math.cos(pitch), z - axis.dz * back
- state.rx, state.ry, state.rz = pitch, heading, 0
- WG.COFC_SetCameraState(state, 0)
- return true
- else
- local spring = Spring.GetCameraNames().spring
- if spring == nil then return false end
- -- Recoil otherwise remaps the requested yaw before rendering the Spring camera.
- if Spring.GetConfigInt(CARDINAL_LOCK, 1) ~= 0 then
- Spring.SetConfigInt(CARDINAL_LOCK, 0, true)
- changedCardinalLock = true
- end
- state = {
- name = "spring", mode = spring, px = x, py = y, pz = z,
- dist = distance, fov = fov, rx = pitch, ry = heading, rz = 0,
- }
- end
- return Spring.SetCameraState(state, 0) ~= false
- end
- local function alignCamera(allowFallback)
- local axis = getBattleAxis(startZoneOrigin)
- if not axis and allowFallback then
- axis = getBattleAxis(engineZoneOrigin)
- end
- return axis and frameTeamAtBottom(axis)
- end
- local function alignInStartPicker()
- -- The start selector also centers the old camera from its first Update calls.
- firstPregameDraw = firstPregameDraw or Spring.GetTimer()
- if Spring.DiffTimers(Spring.GetTimer(), firstPregameDraw) >= PREGAME_CAMERA_DELAY then
- return alignCamera(false)
- end
- end
- -- Zero-K camera settings
- options_path = "Settings/Camera"
- options_order = {"flipOnStart", "flip"}
- options = {
- flipOnStart = {
- name = "Flip on start",
- desc = "Automatically align the camera with your team's start zone at the bottom.",
- type = "bool",
- value = true,
- everyMode = true,
- noHotkey = true,
- },
- flip = {
- name = "Flip",
- desc = "Realign the camera with your team's start zone at the bottom.",
- type = "button",
- everyMode = true,
- OnChange = function()
- if isPlayer() then alignCamera(true) end
- end,
- },
- }
- -- Engine callbacks
- function widget:Initialize()
- if Spring.GetGameFrame() > 0 then
- widgetHandler:RemoveCallIn("DrawScreen")
- end
- end
- function widget:DrawScreen()
- if not isPlayer() then
- widgetHandler:RemoveCallIn("DrawScreen")
- return
- end
- local matchStarted = Spring.GetGameFrame() > 0
- if not options.flipOnStart.value then
- if matchStarted then widgetHandler:RemoveCallIn("DrawScreen") end
- return
- end
- -- Zero-K restores the chosen camera mode on its second rendered frame.
- screenFrames = screenFrames + 1
- if screenFrames < CAMERA_READY_SCREEN_FRAME then return end
- if matchStarted then
- -- A very short start picker may end before we could align there.
- alignCamera(true)
- elseif not alignInStartPicker() then
- return
- end
- -- Stop after the first alignment so the match start cannot recenter the camera.
- widgetHandler:RemoveCallIn("DrawScreen")
- end
- function widget:Shutdown()
- if changedCardinalLock and Spring.GetConfigInt(CARDINAL_LOCK, 1) == 0 then
- Spring.SetConfigInt(CARDINAL_LOCK, 1, true)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment