--!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 HotPoint = require(Runtime.Parent.HotPoint) 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 = HotPoint.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 data = Instance.new("Folder") data.Name = "CitadelMatchState" data.Parent = state ctx.janitor.give(data) 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) zone.marker.body:SetAttribute("Index", i) zone.marker.body.Parent = nil ctx.janitor.give(zone.marker.body) table.insert(zones, zone) end zones[1].marker.body.Parent = folder 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 function runSiege(members: {Player}, fighters: {Fighter}, now: number, dt: number): boolean local half = SiegeConfig.ATTACK_DURATION local elapsed = now - ctx.liveAt if elapsed >= half and not hadSiegeSwap then ctx.setStage("RoleSwap", ctx.liveAt + half + SiegeConfig.SWAP_FREEZE) hadSiegeSwap = true for _, z in zones do z.marker.body.Parent = nil end 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 >= half and elapsed < half + SiegeConfig.SWAP_FREEZE local attackStart = if hadSiegeSwap then half + 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(half - (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 local attacker = Strings.team(siegeSide) local zone = zones[siegeIndex] data:SetAttribute("Index", siegeIndex) if not zone then data:SetAttribute("Point", nil) data:SetAttribute("NextPoint", nil) ctx.status(Strings.siegeComplete) return true end zone.marker.body.Parent = folder 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 fighter.player:SetAttribute("CitadelZone", reason) 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)) Common.paint(zone.marker, if phase == "Contested" then Common.orange else Common.blue) 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}) ctx.cue(fighters, true, nil) siegeCaptureStarted = false data:SetAttribute(siegeSide .. "Time" .. siegeIndex, elapsed - attackStart) zone.marker.body.Parent = nil siegeIndex += 1 siegeProgress = 0 if siegeIndex > #zones then ctx.events.fire(Kinds.AllObjectivesTaken, {side = siegeSide}) return true 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, destroy = function() for _, p in ctx.members() do p:SetAttribute("CitadelZone", nil) end end, }) end return {id = "Siege", usesMap = true, start = start}