--!strict local Runtime = script.Parent.Parent local Types = require(Runtime.Types) local Common = require(Runtime.Common) local Kinds = require(game.ReplicatedStorage.Hunter.EventKinds) local Strings = require(game.ReplicatedStorage.Hunter.Strings) local CollectionService = game:GetService("CollectionService") local Zones = require(Runtime.Zones) local Effects = require(Runtime.SiegeEffects) local Sites = require(Runtime.Parent.ArenaObjectiveSites) local SiegeConfig = require(game.ReplicatedStorage.CitadelAssaultConfig) local Capture = require(Runtime.Parent.CitadelCaptureService) local Score = require(Runtime.Parent.CitadelScoreService) local SiegeSpawn = require(Runtime.Parent.CitadelSpawnService) type Fighter = Types.Fighter type Side = Types.Side local SPEC: Zones.Spec = {radius = SiegeConfig.CAPTURE_RADIUS, height = SiegeConfig.HEIGHT_TOLERANCE, sightHeight = SiegeConfig.SIGHT_HEIGHT} local function start(ctx: Types.ModeContext): Types.ModeInstance local map, world, geometry, folder, state = assert(ctx.map), ctx.world, ctx.geometry, ctx.folder, ctx.state local zones: {Common.Zone} = {} local visuals: {Effects.Rig} = {} local visibleIndex = 0 local data = Instance.new("Folder") data.Name = "CitadelMatchState" data.Parent = state ctx.janitor.give(data) data:SetAttribute("Radius", SiegeConfig.CAPTURE_RADIUS) data:SetAttribute("BlueBase", map.blue) data:SetAttribute("OrangeBase", map.orange) local sites: {Sites.Site} = {map.ground, map.gallery, map.roof} for _, point in CollectionService:GetTagged("CitadelPoint") do if not point:IsA("BasePart") or not point:IsDescendantOf(world) then continue end local index = point:GetAttribute("Index") if typeof(index) == "number" and index % 1 == 0 and sites[index] then sites[index] = {name = sites[index].name, position = point.Position} end end for i, site in sites do local zone = Common.makeZone(folder, geometry, site, "Checkpoint" .. i, SiegeConfig.CAPTURE_RADIUS, Common.gold) zone.marker.body:SetAttribute("Index", i) zone.marker.body.Parent = nil ctx.janitor.give(zone.marker.body) table.insert(zones, zone) table.insert(visuals, Effects.new(zone.marker)) end data:SetAttribute("Total", #zones) local siegeTimes: Score.Ledger = {Blue = {}, Orange = {}} local siegeSide: Side = if SiegeConfig.FIRST_ATTACKER == "Orange" then "Orange" else "Blue" local siegeIndex, siegeProgress = 1, 0 local siegeCaptureStarted = false local hadSiegeSwap = false local swapAt = SiegeConfig.ATTACK_DURATION local lastStatus: string? = nil local marked: {[Player]: boolean} = {} local function runSiege(members: {Player}, fighters: {Fighter}, now: number, dt: number): boolean local half = SiegeConfig.ATTACK_DURATION local elapsed = now - ctx.liveAt if elapsed >= swapAt and not hadSiegeSwap then ctx.setStage("RoleSwap", ctx.liveAt + swapAt + SiegeConfig.SWAP_FREEZE) hadSiegeSwap = true for i, z in zones do visuals[i].hide() z.marker.body.Parent = nil end visibleIndex, lastStatus = 0, nil siegeSide, siegeIndex, siegeProgress = Common.opposite(siegeSide), 1, 0 siegeCaptureStarted = false local arenaSites = map ctx.events.fire(Kinds.RoleSwap, {side = siegeSide}) local ordinals = {Blue = 0, Orange = 0} for _, p in members do ctx.deploy(p) local side, character = Common.sideOf(p), p.Character if not side or not character or not character.PrimaryPart then continue end local attack = side == siegeSide ordinals[side] += 1 local at = SiegeSpawn.position(world, attack, if attack then arenaSites.blue else arenaSites.orange, ordinals[side]) character:PivotTo(CFrame.new(at + Vector3.yAxis * 4)) end end local swap = elapsed >= swapAt and elapsed < swapAt + SiegeConfig.SWAP_FREEZE local attackStart = if hadSiegeSwap then swapAt + SiegeConfig.SWAP_FREEZE else 0 data:SetAttribute("Phase", if swap then "RoleSwap" elseif hadSiegeSwap then "Attack2" else "Attack1") data:SetAttribute("Attacker", siegeSide) data:SetAttribute("Seconds", math.clamp(math.ceil((if hadSiegeSwap then half else swapAt) - (elapsed - attackStart)), 0, half)) if swap then data:SetAttribute("Progress", 0) data:SetAttribute("Status", "Idle") ctx.status(Strings.siegeSwap) return false end if state:GetAttribute("RoundStage") == "RoleSwap" then ctx.setStage("Live") end if hadSiegeSwap and elapsed >= attackStart + half then return true end local attacker = Strings.team(siegeSide) local zone = zones[siegeIndex] data:SetAttribute("Index", siegeIndex) for player in marked do if not table.find(members, player) then player:SetAttribute("CitadelZone", nil) marked[player] = nil end end for _, player in members do marked[player] = true end if not zone then data:SetAttribute("Point", nil) data:SetAttribute("NextPoint", nil) data:SetAttribute("Status", "Complete") for player in marked do player:SetAttribute("CitadelZone", nil) end ctx.status(Strings.siegeComplete) return false end local visual = visuals[siegeIndex] if visibleIndex ~= siegeIndex then visibleIndex, lastStatus = siegeIndex, nil zone.marker.body.Parent = folder visual.show() ctx.events.fire(Kinds.ZoneStarted, {side = siegeSide, index = siegeIndex, text = zone.site.name, at = zone.site.position}) end local updated: {[Player]: boolean} = {} local attack, defense = false, false local attackers: {Player} = {} for _, fighter in fighters do local reason = Zones.classify(fighter.root.Position, zone.site.position, SPEC, geometry) if reason == "Inside" then ctx.contribute(fighter.player, "holdSeconds", dt) if fighter.side == siegeSide then attack = true table.insert(attackers, fighter.player) else defense = true end end updated[fighter.player] = true fighter.player:SetAttribute("CitadelZone", reason) end for player in marked do if not updated[player] then player:SetAttribute("CitadelZone", nil) end end local progress, phase = Capture.advance(siegeProgress, math.min(dt, math.max(0, elapsed - attackStart)), attack, defense, SiegeConfig.CAPTURE_TIME, SiegeConfig.DECAY_MULTIPLIER) if phase == "Capturing" and not siegeCaptureStarted then ctx.cue(fighters, false, siegeSide) siegeCaptureStarted = true elseif progress == 0 and phase ~= "Capturing" then siegeCaptureStarted = false end siegeProgress = progress data:SetAttribute("Progress", progress) data:SetAttribute("Status", phase) data:SetAttribute("Point", zone.site.position) data:SetAttribute("PointName", zone.site.name) local nextZone = zones[siegeIndex + 1] data:SetAttribute("NextPoint", if nextZone then nextZone.site.position else nil) local percent = math.floor(progress / SiegeConfig.HUD_PROGRESS_STEP + 1e-8) * SiegeConfig.HUD_PROGRESS_STEP * 100 ctx.status(Strings.siegeStatus(attacker, siegeIndex, #zones, percent, phase)) local teamTint = Common.color(siegeSide) local tint = if phase == "Idle" then Common.gold elseif phase == "Contested" then SiegeConfig.CONTESTED_COLOR elseif phase == "Decaying" then teamTint:Lerp(Color3.fromRGB(150, 162, 182), .45) else teamTint Common.paint(zone.marker, tint) if phase == "Contested" and lastStatus ~= phase then ctx.events.fire(Kinds.ZoneContested, {side = siegeSide, index = siegeIndex, at = zone.site.position}) end lastStatus = phase if phase ~= "Captured" or not Score.record(siegeTimes, siegeSide, siegeIndex, elapsed - attackStart) then return false end ctx.scoring.add(siegeSide, SiegeConfig.POINTS_PER_OBJECTIVE, Kinds.ZoneCaptured) for _, player in attackers do ctx.contribute(player, "captures", 1) end ctx.events.fire(Kinds.ZoneCaptured, {side = siegeSide, index = siegeIndex, text = zone.site.name, value = elapsed - attackStart, at = zone.site.position}) ctx.cue(fighters, true, nil) siegeCaptureStarted = false data:SetAttribute(siegeSide .. "Time" .. siegeIndex, elapsed - attackStart) visual.capture() siegeIndex += 1 siegeProgress = 0 if siegeIndex > #zones then ctx.events.fire(Kinds.AllObjectivesTaken, {side = siegeSide}) if SiegeConfig.EARLY_FINISH_WHEN_ALL_TAKEN then if hadSiegeSwap then return true end swapAt = elapsed state:SetAttribute("SiegeEndsAt", ctx.liveAt + swapAt + SiegeConfig.SWAP_FREEZE + half) end end return false end return Common.instance(ctx, { step = function(now, dt, fighters) return runSiege(ctx.members(), fighters, now, dt) end, winner = function() local winner, reason = Score.winner(siegeTimes, SiegeConfig.TIE_EPSILON) state:SetAttribute("CitadelResultReason", reason) return winner, reason end, death = function(p) p:SetAttribute("CitadelZone", nil) end, leaving = function(p) marked[p] = nil; p:SetAttribute("CitadelZone", nil) end, destroy = function() state:SetAttribute("SiegeEndsAt", nil) for p in marked do p:SetAttribute("CitadelZone", nil) end end, }) end return {id = "Siege", usesMap = true, start = start}