Guest User

Untitled

a guest
Nov 22nd, 2026
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.92 KB | None | 0 0
  1. function widget:GetInfo()
  2. return {
  3. name = "Team South",
  4. desc = "Center the battlefield on the line from your start zone to the front",
  5. author = "Zero-K community widget",
  6. layer = 10,
  7. enabled = true,
  8. }
  9. end
  10.  
  11. local getRawBoxes = VFS.Include("LuaUI/Headers/startbox_utilities.lua")
  12.  
  13. -- Camera and startup tuning
  14. local CARDINAL_LOCK = "CamSpringLockCardinalDirections"
  15. local EDGE_SNAP = math.tan(math.rad(10))
  16. local MIN_STRIP_OVERLAP = 0.5
  17. local MIN_STRIP_ASPECT = 2
  18. local PREGAME_CAMERA_DELAY = 0.45
  19. local CAMERA_READY_SCREEN_FRAME = 3
  20.  
  21. local PI = math.pi
  22. local MAP_X, MAP_Z = Game.mapSizeX, Game.mapSizeZ
  23. local MID_X, MID_Z = MAP_X * 0.5, MAP_Z * 0.5
  24.  
  25. local changedCardinalLock = false
  26. local screenFrames = 0
  27. local firstPregameDraw
  28.  
  29. local function isPlayer()
  30. return not Spring.GetSpectatingState() and not Spring.IsReplay()
  31. end
  32.  
  33. -- Start zones and battle direction
  34.  
  35. local function inMap(x, z)
  36. return type(x) == "number" and type(z) == "number"
  37. and x >= 0 and x <= MAP_X and z >= 0 and z <= MAP_Z
  38. and (x ~= 0 or z ~= 0)
  39. end
  40.  
  41. local function startZoneOrigin(ally)
  42. local gaia = Spring.GetGaiaTeamID()
  43. for _, team in ipairs(Spring.GetTeamList(ally) or {}) do
  44. if team ~= gaia then
  45. -- Zero-K stores the average of the assigned zone's recommended startpoints.
  46. local x = Spring.GetTeamRulesParam(team, "allyteam_origin_x")
  47. local z = Spring.GetTeamRulesParam(team, "allyteam_origin_z")
  48. if inMap(x, z) then
  49. return x, z, Spring.GetTeamRulesParam(team, "start_box_id")
  50. end
  51. end
  52. end
  53. end
  54.  
  55. local function engineZoneOrigin(ally)
  56. local x0, z0, x1, z1 = Spring.GetAllyTeamStartBox(ally)
  57. if type(x0) ~= "number" or type(z0) ~= "number"
  58. or type(x1) ~= "number" or type(z1) ~= "number"
  59. or x0 < 0 or z0 < 0 or x1 > MAP_X or z1 > MAP_Z
  60. or x1 <= x0 or z1 <= z0 then return end
  61. -- Recoil returns the whole map when no start box was assigned.
  62. if x0 == 0 and z0 == 0 and x1 == MAP_X and z1 == MAP_Z then return end
  63. return (x0 + x1) * 0.5, (z0 + z1) * 0.5
  64. end
  65.  
  66. local function zoneBounds(zone)
  67. if not zone then return end
  68. local left, top, right, bottom = 1, 1, 0, 0
  69. for _, polygon in ipairs(zone.boxes) do
  70. for _, point in ipairs(polygon) do
  71. if inMap(point[1], point[2]) or (point[1] == 0 and point[2] == 0) then
  72. local x, z = point[1] / MAP_X, point[2] / MAP_Z
  73. left, top = math.min(left, x), math.min(top, z)
  74. right, bottom = math.max(right, x), math.max(bottom, z)
  75. end
  76. end
  77. end
  78. if left < right and top < bottom then return left, top, right, bottom end
  79. end
  80.  
  81. local function oppositeStrips(a0, a1, b0, b1, alongA0, alongA1, alongB0, alongB1)
  82. local spanA, spanB = alongA1 - alongA0, alongB1 - alongB0
  83. local overlap = math.min(alongA1, alongB1) - math.max(alongA0, alongB0)
  84. return ((a1 <= 0.5 and b0 >= 0.5) or (b1 <= 0.5 and a0 >= 0.5))
  85. and overlap >= MIN_STRIP_OVERLAP
  86. and spanA >= MIN_STRIP_ASPECT * (a1 - a0)
  87. and spanB >= MIN_STRIP_ASPECT * (b1 - b0)
  88. end
  89.  
  90. local function getOppositeStripAxis(boxID, opponentBoxID)
  91. if boxID == nil or opponentBoxID == nil or boxID == opponentBoxID then return end
  92. local boxes = getRawBoxes()
  93. local left, top, right, bottom = zoneBounds(boxes[boxID])
  94. local otherLeft, otherTop, otherRight, otherBottom = zoneBounds(boxes[opponentBoxID])
  95. if not left or not otherLeft then return end
  96. -- A recommended point can be off-center even when the zones face each other along an edge.
  97. if oppositeStrips(left, right, otherLeft, otherRight, top, bottom, otherTop, otherBottom) then
  98. return "x", (top + bottom) * 0.5 * MAP_Z
  99. end
  100. if oppositeStrips(top, bottom, otherTop, otherBottom, left, right, otherLeft, otherRight) then
  101. return "z", (left + right) * 0.5 * MAP_X
  102. end
  103. end
  104.  
  105. local function getBattleAxis(origin)
  106. local ally = Spring.GetMyAllyTeamID()
  107. if ally == nil or ally < 0 then return end
  108. local x, z, boxID = origin(ally)
  109. if not x then return end
  110.  
  111. local goalX, goalZ = MID_X, MID_Z
  112. local gaia = Spring.GetGaiaTeamID()
  113. local otherAlly, opponents = nil, 0
  114. for _, id in ipairs(Spring.GetAllyTeamList()) do
  115. if id ~= ally then
  116. for _, team in ipairs(Spring.GetTeamList(id) or {}) do
  117. if team ~= gaia then
  118. otherAlly, opponents = id, opponents + 1
  119. break
  120. end
  121. end
  122. end
  123. end
  124. if opponents == 1 then
  125. local oppositeX, oppositeZ, oppositeBoxID = origin(otherAlly)
  126. if oppositeX then
  127. goalX, goalZ = oppositeX, oppositeZ
  128. local side, middle = getOppositeStripAxis(boxID, oppositeBoxID)
  129. if side == "x" then
  130. z, goalZ = middle, middle
  131. elseif side == "z" then
  132. x, goalX = middle, middle
  133. end
  134. end
  135. end
  136.  
  137. local dx, dz = goalX - x, goalZ - z
  138. -- Stabilize near-cardinal matchups without rounding true diagonal starts.
  139. if math.abs(dx) <= math.abs(dz) * EDGE_SNAP then
  140. dx = 0
  141. elseif math.abs(dz) <= math.abs(dx) * EDGE_SNAP then
  142. dz = 0
  143. end
  144. local gap = math.sqrt(dx * dx + dz * dz)
  145. if gap > 1 then
  146. return {x = x, z = z, dx = dx / gap, dz = dz / gap, gap = gap}
  147. end
  148. end
  149.  
  150. -- Camera framing
  151.  
  152. local function getScreenCenterOnGround()
  153. local width, height = Spring.GetViewGeometry()
  154. local kind, point = Spring.TraceScreenRay(math.floor(width * 0.5), math.floor(height * 0.5), true, false, true)
  155. if kind == "ground" and point and inMap(point[1], point[3]) then
  156. return point[1], point[2], point[3]
  157. end
  158. end
  159.  
  160. local function frameTeamAtBottom(axis)
  161. local state = Spring.GetCameraState()
  162. if not state then return false end
  163.  
  164. local pitch, distance
  165. if state.name == "ta" then
  166. pitch, distance = PI - state.angle, state.height
  167. elseif state.name == "spring" then
  168. pitch, distance = state.rx, state.dist
  169. else
  170. local x, y, z = getScreenCenterOnGround()
  171. if x then
  172. local cx, cy, cz = Spring.GetCameraPosition()
  173. distance = math.sqrt((cx-x)^2 + (cy-y)^2 + (cz-z)^2)
  174. if distance > 1 then
  175. pitch = math.acos(math.max(-1, math.min(1, (y-cy) / distance)))
  176. end
  177. end
  178. end
  179. pitch = math.max(PI * 0.64, math.min(PI * 0.99, pitch or PI * 0.85))
  180. distance = math.max(20, distance or axis.gap * 0.8)
  181.  
  182. -- Aim ahead along the front so the start projects below the screen center.
  183. local fov = state.fov or 45
  184. local screenOffset = 0.52 * math.tan(math.rad(fov) * 0.5)
  185. local advance = distance * screenOffset / (-math.cos(pitch) + screenOffset * math.sin(pitch))
  186. advance = math.min(advance, axis.gap * 0.66)
  187. local x, z = axis.x + axis.dx * advance, axis.z + axis.dz * advance
  188. local y = math.max(0, Spring.GetGroundHeight(x, z))
  189. local heading = math.atan2(axis.dx, -axis.dz)
  190.  
  191. if state.name == "ta" and axis.dx == 0 then
  192. state.flipped = axis.dz > 0 and 1 or -1
  193. state.px, state.py, state.pz = x, y, z
  194. state.angle = PI - pitch
  195. elseif state.name == "free" and WG.COFC_Enabled and WG.COFC_SetCameraState then
  196. local back = distance * math.sin(pitch)
  197. state.px, state.py, state.pz = x - axis.dx * back, y - distance * math.cos(pitch), z - axis.dz * back
  198. state.rx, state.ry, state.rz = pitch, heading, 0
  199. WG.COFC_SetCameraState(state, 0)
  200. return true
  201. else
  202. local spring = Spring.GetCameraNames().spring
  203. if spring == nil then return false end
  204. -- Recoil otherwise remaps the requested yaw before rendering the Spring camera.
  205. if Spring.GetConfigInt(CARDINAL_LOCK, 1) ~= 0 then
  206. Spring.SetConfigInt(CARDINAL_LOCK, 0, true)
  207. changedCardinalLock = true
  208. end
  209. state = {
  210. name = "spring", mode = spring, px = x, py = y, pz = z,
  211. dist = distance, fov = fov, rx = pitch, ry = heading, rz = 0,
  212. }
  213. end
  214.  
  215. return Spring.SetCameraState(state, 0) ~= false
  216. end
  217.  
  218. local function alignCamera(allowFallback)
  219. local axis = getBattleAxis(startZoneOrigin)
  220. if not axis and allowFallback then
  221. axis = getBattleAxis(engineZoneOrigin)
  222. end
  223. return axis and frameTeamAtBottom(axis)
  224. end
  225.  
  226. local function alignInStartPicker()
  227. -- The start selector also centers the old camera from its first Update calls.
  228. firstPregameDraw = firstPregameDraw or Spring.GetTimer()
  229. if Spring.DiffTimers(Spring.GetTimer(), firstPregameDraw) >= PREGAME_CAMERA_DELAY then
  230. return alignCamera(false)
  231. end
  232. end
  233.  
  234. -- Zero-K camera settings
  235.  
  236. options_path = "Settings/Camera"
  237. options_order = {"flipOnStart", "flip"}
  238. options = {
  239. flipOnStart = {
  240. name = "Flip on start",
  241. desc = "Automatically align the camera with your team's start zone at the bottom.",
  242. type = "bool",
  243. value = true,
  244. everyMode = true,
  245. noHotkey = true,
  246. },
  247. flip = {
  248. name = "Flip",
  249. desc = "Realign the camera with your team's start zone at the bottom.",
  250. type = "button",
  251. everyMode = true,
  252. OnChange = function()
  253. if isPlayer() then alignCamera(true) end
  254. end,
  255. },
  256. }
  257.  
  258. -- Engine callbacks
  259.  
  260. function widget:Initialize()
  261. if Spring.GetGameFrame() > 0 then
  262. widgetHandler:RemoveCallIn("DrawScreen")
  263. end
  264. end
  265.  
  266. function widget:DrawScreen()
  267. if not isPlayer() then
  268. widgetHandler:RemoveCallIn("DrawScreen")
  269. return
  270. end
  271.  
  272. local matchStarted = Spring.GetGameFrame() > 0
  273. if not options.flipOnStart.value then
  274. if matchStarted then widgetHandler:RemoveCallIn("DrawScreen") end
  275. return
  276. end
  277.  
  278. -- Zero-K restores the chosen camera mode on its second rendered frame.
  279. screenFrames = screenFrames + 1
  280. if screenFrames < CAMERA_READY_SCREEN_FRAME then return end
  281.  
  282. if matchStarted then
  283. -- A very short start picker may end before we could align there.
  284. alignCamera(true)
  285. elseif not alignInStartPicker() then
  286. return
  287. end
  288.  
  289. -- Stop after the first alignment so the match start cannot recenter the camera.
  290. widgetHandler:RemoveCallIn("DrawScreen")
  291. end
  292.  
  293. function widget:Shutdown()
  294. if changedCardinalLock and Spring.GetConfigInt(CARDINAL_LOCK, 1) == 0 then
  295. Spring.SetConfigInt(CARDINAL_LOCK, 1, true)
  296. end
  297. end
  298.  
Advertisement
Add Comment
Please, Sign In to add comment