--!strict local ReplicatedStorage = game:GetService("ReplicatedStorage") local Modes = require(ReplicatedStorage:WaitForChild("ArenaModes") :: ModuleScript) local Sites = require(script.Parent:WaitForChild("ArenaObjectiveSites") :: ModuleScript) local HotPoint = require(script.Parent:WaitForChild("HotPoint") :: ModuleScript) local SiegeConfig = require(ReplicatedStorage:WaitForChild("CitadelAssaultConfig") :: ModuleScript) local Capture = require(script.Parent:WaitForChild("CitadelCaptureService") :: ModuleScript) local Score = require(script.Parent:WaitForChild("CitadelScoreService") :: ModuleScript) local SiegeSpawn = require(script.Parent:WaitForChild("CitadelSpawnService") :: ModuleScript) local CollectionService = game:GetService("CollectionService") type Side = "Blue" | "Orange" type Fighter = HotPoint.Fighter type Marker = HotPoint.Marker type Zone = {site: Sites.Site, marker: Marker, owner: Side?, claiming: Side?, progress: number} type Flag = {side: Side, marker: Marker, carrier: Player?, droppedUntil: number} type Drop = {marker: Marker, amount: number, expires: number} type Config = {arena: string, rule: string, state: Instance, now: number, duration: number, members: () -> {Player}, bench: (Player) -> (), deploy: (Player) -> (), balanceWaiting: () -> ()} export type Match = { step: (number) -> boolean, kill: (Player, Player, Vector3, number) -> (), hit: ((Player, number, boolean) -> ())?, death: (Player, Vector3?, number) -> (), leaving: (Player, Vector3?, number) -> (), winner: () -> string, destroy: () -> (), } local active: {[string]: Match} = {} local Objectives = {} local sides: {Side} = {"Blue", "Orange"} local blue = Color3.fromRGB(67, 216, 255) local orange = Color3.fromRGB(255, 150, 68) local gold = Color3.fromRGB(255, 220, 96) local neutral = Color3.fromRGB(188, 150, 255) local rng = Random.new() local function opposite(side: Side): Side return if side == "Blue" then "Orange" else "Blue" end local function color(side: Side?): Color3 return if side == "Blue" then blue elseif side == "Orange" then orange else neutral end local function sideOf(player: Player): Side? local side = player:GetAttribute("ArenaTeam") return if side == "Blue" then "Blue" elseif side == "Orange" then "Orange" else nil end local function boundsAt(position: Vector3, target: Vector3, radius: number, height: number): boolean local delta = position - target return math.abs(delta.Y) < height and delta.X * delta.X + delta.Z * delta.Z <= radius * radius end local function label(adornee: BasePart, name: string, tint: Color3, offset: number, waypoint: boolean?): TextLabel local gui = Instance.new("BillboardGui") gui.Name = "ObjectiveLabel" gui.Size = UDim2.fromOffset(220, 26) gui.StudsOffsetWorldSpace = Vector3.yAxis * offset gui.MaxDistance = if waypoint then 710 else 170 gui.AlwaysOnTop = waypoint == true gui.LightInfluence = 0 gui.Parent = adornee local text = Instance.new("TextLabel") text.Size = UDim2.fromScale(1, 1) text.BackgroundTransparency = 1 text.Font = Enum.Font.GothamBlack text.TextSize = 15 text.Text, text.TextColor3 = name, tint text.TextStrokeColor3, text.TextStrokeTransparency = Color3.fromRGB(9, 13, 25), .14 text.Parent = gui return text end local function marker(parent: Instance, id: string, name: string, at: Vector3, tint: Color3, diameter: number, tall: boolean?): Marker local part = Instance.new("Part") part.Name = id part.Shape = if tall then Enum.PartType.Ball else Enum.PartType.Cylinder part.Size = if tall then Vector3.new(diameter, diameter, diameter) else Vector3.new(.16, diameter, diameter) part.CFrame = CFrame.new(at + Vector3.yAxis * (if tall then 2.4 else .12)) * CFrame.Angles(0, 0, math.pi / 2) part.Anchored, part.CanCollide, part.CanQuery, part.CanTouch, part.CastShadow = true, false, false, false, false part.Material, part.Color = Enum.Material.Neon, tint part.Transparency = if tall then .04 else .76 local aura = Instance.new("PointLight") aura.Range, aura.Brightness, aura.Color = if tall then 13 else 9, .7, tint aura.Parent = part part.Parent = parent return {body = part, label = label(part, name, tint, if tall then 2.1 else 4, id ~= "Tokens"), origin = at} end local function paint(mark: Marker, tint: Color3) if mark.body.Color == tint then return end mark.body.Color = tint mark.label.TextColor3 = tint local aura = mark.body:FindFirstChildOfClass("PointLight") if aura then aura.Color = tint end end local function visibleAt(fighter: Fighter, at: Vector3, geometry: RaycastParams): boolean return workspace:Raycast(fighter.root.Position, at - fighter.root.Position, geometry) == nil end local function presence(fighters: {Fighter}, zone: Zone, geometry: RaycastParams): (boolean, boolean) local b, o = false, false -- Separate vertically stacked objectives by avatar foot height. for _, fighter in fighters do if boundsAt(fighter.root.Position, zone.site.position, 11, 8) and visibleAt(fighter, zone.site.position + Vector3.yAxis * 2.7, geometry) then if fighter.side == "Blue" then b = true else o = true end end end return b, o end function Objectives.current(arena: string): Match? return active[arena] end function Objectives.start(config: Config): Match local rule, state, arena = config.rule, config.state, config.arena local world = Modes.geometry(arena) local geometry = Modes.collision(arena) local map = if rule == "TeamDeathmatch" or rule == "HuntLeader" then nil else Sites.resolve(arena) local hotPath = if rule == "HotPoint" and map then Sites.hotPath(arena, map, HotPoint.Radius) else nil local folder = Instance.new("Folder") folder.Name = "MatchObjectives" folder.Parent = world local blueScore, orangeScore = 0, 0 local function add(side: Side, points: number) if side == "Blue" then blueScore += points else orangeScore += points end state:SetAttribute(side .. "Score", if side == "Blue" then blueScore else orangeScore) end local function status(text: string) if state:GetAttribute("ObjectiveStatus") ~= text then state:SetAttribute("ObjectiveStatus", text) end end state:SetAttribute("BlueScore", 0) state:SetAttribute("OrangeScore", 0) state:SetAttribute("ObjectiveStatus", "") local function collect(members: {Player}): {Fighter} local found: {Fighter} = {} for _, p in members do local side = sideOf(p) local c = p.Character local h = c and c:FindFirstChildOfClass("Humanoid") local r = c and c:FindFirstChild("HumanoidRootPart") if side and h and h.Health > 0 and r and r:IsA("BasePart") and p:GetAttribute("InArena") == true then table.insert(found, {player = p, side = side, root = r}) end end return found end local function makeZone(site: Sites.Site, name: string): Zone return {site = site, marker = marker(folder, name, site.name, site.position, neutral, 22), owner = nil, claiming = nil, progress = 0} end local zones: {Zone} = {} local hotPoint = if hotPath then HotPoint.start({ state = state, geometry = geometry, sites = hotPath, startedAt = config.now, marker = function(site: Sites.Site): Marker return marker(folder, "HotPoint", "ГОРЯЧАЯ ТОЧКА", site.position, neutral, HotPoint.Radius * 2) end, score = add, status = status, }) else nil local flags: {[Side]: Flag} = {} local tokens: {Drop} = {} local carried: {[Player]: number} = {} local leaders: {[Side]: Player} = {} local previousLeader: {[Side]: Player} = {} local leaderLabels: {[Player]: BillboardGui} = {} local siegeTimes: Score.Ledger = {Blue = {}, Orange = {}} local siegeSide: Side = if SiegeConfig.FIRST_ATTACKER == "Orange" then "Orange" else "Blue" local siegeIndex, siegeProgress = 1, 0 local hadSiegeSwap = false local siegeState: Folder? = nil local lifeActive, miniNumber = false, 0 local lifeEnds, nextMini = math.huge, config.now + 2 local lastStep, nextScore = config.now, config.now + 1 local match: Match if map then if rule == "Domination" then for i, site in {map.west, map.gallery, map.east} do local key = string.char(64 + i) local zone = makeZone(site, key) zone.marker.label.Text = key .. " · " .. site.name table.insert(zones, zone) end elseif rule == "CaptureFlag" or rule == "TrophyHunt" then for _, side in sides do local at = if side == "Blue" then map.blue else map.orange local name = if side == "Blue" then "БАЗА СИНИХ" else "БАЗА ОРАНЖЕВЫХ" marker(folder, side .. "Base", name, at, color(side), 15) if rule == "CaptureFlag" then local flag = marker(folder, side .. "Flag", "ФЛАГ", at, color(side), 2.6, true) flag.body.Transparency = 1 flag.body.CFrame = CFrame.new(flag.body.Position) local function attach(name: string, size: Vector3, offset: Vector3, tint: Color3) local part = Instance.new("Part") part.Name, part.Size, part.Color = name, size, tint part.CFrame = flag.body.CFrame + offset part.Material = Enum.Material.Neon part.Massless, part.CanCollide, part.CanTouch, part.CanQuery = true, false, false, false part.Parent = flag.body local weld = Instance.new("WeldConstraint") weld.Part0, weld.Part1, weld.Parent = flag.body, part, part end attach("Pole", Vector3.new(.17, 4.5, .17), Vector3.new(0, 0, 0), Color3.fromRGB(203, 221, 238)) attach("Banner", Vector3.new(2.65, 1.48, .15), Vector3.new(1.35, 1.45, 0), color(side)) flags[side] = {side = side, marker = flag, carrier = nil, droppedUntil = 0} end end elseif rule == "Siege" then local data = Instance.new("Folder") data.Name = "CitadelMatchState" data.Parent = state siegeState = 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 = makeZone(site, "Checkpoint" .. i) zone.marker.body:SetAttribute("Index", i) zone.marker.body.Parent = nil table.insert(zones, zone) end zones[1].marker.body.Parent = folder end end local function putFlag(flag: Flag, at: Vector3) flag.marker.body.CFrame = CFrame.new(at + Vector3.yAxis * 2.4) end local function sendHome(flag: Flag) if flag.carrier then flag.carrier:SetAttribute("ArenaFlag", nil) end flag.carrier, flag.droppedUntil = nil, 0 putFlag(flag, flag.marker.origin) end local function floorAt(at: Vector3): Vector3 local hit = workspace:Raycast(at + Vector3.yAxis * .3, -Vector3.yAxis * 50, geometry) return if hit then hit.Position else (if map then map.ground.position else at) end local function tokenDrop(at: Vector3, amount: number, now: number) local floor = floorAt(at) for _, existing in tokens do if boundsAt(existing.marker.origin, floor, 3.5, 3) then existing.amount += amount existing.expires = now + 42 existing.marker.label.Text = "ЖЕТОНЫ ×" .. existing.amount return end end if #tokens >= 60 then local old = table.remove(tokens, 1) old.marker.body:Destroy() end table.insert(tokens, {marker = marker(folder, "Tokens", "ЖЕТОНЫ ×" .. amount, floor, gold, 1.55, true), amount = amount, expires = now + 42}) end local function dropCarried(player: Player, at: Vector3?, now: number) for _, flag in flags do if flag.carrier ~= player then continue end if at then flag.carrier, flag.droppedUntil = nil, now + 20 putFlag(flag, floorAt(at)) else sendHome(flag) end player:SetAttribute("ArenaFlag", nil) end local amount = carried[player] if amount then carried[player] = nil player:SetAttribute("ArenaTokens", nil) if at then tokenDrop(at, amount, now) end end end local function clearLeader(side: Side) local p = leaders[side] if not p then return end previousLeader[side] = p leaders[side] = nil p:SetAttribute("ArenaLeader", nil) local badge = leaderLabels[p] if badge then badge:Destroy() end leaderLabels[p] = nil end local function updateLeaders(fighters: {Fighter}) for _, side in sides do local roster: {Fighter} = {} local present = false for _, f in fighters do if f.side ~= side then continue end table.insert(roster, f) if f.player == leaders[side] then present = true end end if present then continue end clearLeader(side) if #roster == 0 then continue end local pick = rng:NextInteger(1, #roster) if #roster > 1 and roster[pick].player == previousLeader[side] then pick = pick % #roster + 1 end local p = roster[pick].player local head = p.Character and p.Character:FindFirstChild("Head") if not head or not head:IsA("BasePart") then continue end leaders[side] = p p:SetAttribute("ArenaLeader", true) leaderLabels[p] = label(head, "ЛИДЕР", gold, 2.7).Parent :: BillboardGui end local a, b = leaders.Blue, leaders.Orange status("Лидеры · " .. (if a then a.DisplayName else "—") .. " / " .. (if b then b.DisplayName else "—")) end local function runFlags(fighters: {Fighter}, now: number) for _, side in sides do local flag = flags[side] local carrier = flag.carrier if carrier and carrier.Character and carrier.Character.PrimaryPart then flag.marker.body.CFrame = CFrame.new(carrier.Character.PrimaryPart.Position + Vector3.yAxis * 3.1) elseif carrier or (flag.droppedUntil > 0 and now >= flag.droppedUntil) then sendHome(flag) end if flag.carrier then continue end for _, f in fighters do if not boundsAt(f.root.Position, flag.marker.body.Position, 5.5, 6.5) or not visibleAt(f, flag.marker.body.Position, geometry) then continue end if f.side == side then if flag.droppedUntil > 0 then sendHome(flag) end else flag.carrier = f.player flag.droppedUntil = 0 f.player:SetAttribute("ArenaFlag", side) break end end end for _, fighter in fighters do local own, enemy = flags[fighter.side], flags[opposite(fighter.side)] if enemy.carrier == fighter.player and not own.carrier and own.droppedUntil == 0 and boundsAt(fighter.root.Position, own.marker.origin, 7, 7) then add(fighter.side, 1) sendHome(enemy) end end local function stateOf(flag: Flag): string return if flag.carrier then "украден" elseif flag.droppedUntil > 0 then "на земле" else "на базе" end status("Флаг синих: " .. stateOf(flags.Blue) .. " · Флаг оранжевых: " .. stateOf(flags.Orange)) end local function runTrophies(fighters: {Fighter}, now: number) if not map then return end for i = #tokens, 1, -1 do local token = tokens[i] local collector: Fighter? = nil if now < token.expires then for _, f in fighters do if boundsAt(f.root.Position, token.marker.origin, 5, 6) and visibleAt(f, token.marker.body.Position, geometry) then collector = f; break end end if not collector then continue end local p = collector.player carried[p] = (carried[p] or 0) + token.amount p:SetAttribute("ArenaTokens", carried[p]) end token.marker.body:Destroy() table.remove(tokens, i) end for _, f in fighters do local amount = carried[f.player] if amount and boundsAt(f.root.Position, if f.side == "Blue" then map.blue else map.orange, 8, 7) then add(f.side, amount) carried[f.player] = nil f.player:SetAttribute("ArenaTokens", nil) end end status("Сдавайте собранные жетоны на своей базе") end local function runSiege(members: {Player}, fighters: {Fighter}, now: number, dt: number): boolean local data = assert(siegeState) local half = SiegeConfig.ATTACK_DURATION local elapsed = now - config.now if elapsed >= half and not hadSiegeSwap then hadSiegeSwap = true for _, z in zones do z.marker.body.Parent = nil end siegeSide, siegeIndex, siegeProgress = opposite(siegeSide), 1, 0 local arenaSites = assert(map) local ordinals = {Blue = 0, Orange = 0} for _, p in members do config.deploy(p) local side, character = 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") status("СМЕНА РОЛЕЙ") return false end local attacker = if siegeSide == "Blue" then "Синие" else "Оранжевые" local zone = zones[siegeIndex] data:SetAttribute("Index", siegeIndex) if not zone then data:SetAttribute("Point", nil) data:SetAttribute("NextPoint", nil) status("Все рубежи взяты!") return true end zone.marker.body.Parent = folder local attack, defense = false, false for _, fighter in fighters do local delta = fighter.root.Position - zone.site.position local reason = "Outside" if delta.X * delta.X + delta.Z * delta.Z <= SiegeConfig.CAPTURE_RADIUS ^ 2 then if math.abs(delta.Y) > SiegeConfig.HEIGHT_TOLERANCE then reason = "Height" elseif not visibleAt(fighter, zone.site.position + Vector3.yAxis * SiegeConfig.SIGHT_HEIGHT, geometry) then reason = "Blocked" else reason = "Inside" if fighter.side == siegeSide then attack = true else defense = true end 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) 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 local reason = if phase == "Contested" then "Зона оспаривается!" elseif phase == "Decaying" then "Захват откатывается" elseif phase == "Capturing" then "Захват идёт" else "Двигайтесь к синему кругу" status(string.format("%s · Рубеж %d/%d · %d%% · %s", attacker, siegeIndex, #zones, percent, reason)) paint(zone.marker, if phase == "Contested" then orange else blue) if phase ~= "Captured" or not Score.record(siegeTimes, siegeSide, siegeIndex, elapsed - attackStart) then return false end add(siegeSide, SiegeConfig.POINTS_PER_OBJECTIVE) data:SetAttribute(siegeSide .. "Time" .. siegeIndex, elapsed - attackStart) zone.marker.body.Parent = nil siegeIndex += 1 siegeProgress = 0 return siegeIndex > #zones end local function runOneLife(members: {Player}, fighters: {Fighter}, now: number): boolean if lifeActive then local b, o = 0, 0 for _, f in fighters do if f.side == "Blue" then b += 1 else o += 1 end end if (b > 0 and o > 0) and now < lifeEnds then status(string.format("Мини-раунд %d · Живы %d / %d · %d с", miniNumber, b, o, math.ceil(lifeEnds - now))) return false end if b ~= o then add(if b > o then "Blue" else "Orange", 1) end if blueScore >= 3 or orangeScore >= 3 then return true end for _, p in members do config.bench(p) end lifeActive, nextMini = false, now + 4 end if now >= nextMini then config.balanceWaiting() end local ready: {Player} = {} local b, o = false, false for _, p in members do local c = p.Character local h = c and c:FindFirstChildOfClass("Humanoid") if not c or not c.PrimaryPart or not h or h.Health <= 0 then continue end table.insert(ready, p) if sideOf(p) == "Blue" then b = true else o = true end end if b and o and now >= nextMini then for _, p in ready do config.deploy(p) end miniNumber += 1 lifeActive, lifeEnds = true, now + 45 status("Мини-раунд " .. miniNumber) elseif not b or not o then status("Ожидаем игроков с обеих сторон") else status("Следующий мини-раунд через " .. math.ceil(nextMini - now) .. " с") end return false end local function step(now: number): boolean if rule == "TeamDeathmatch" then return false end local elapsed = math.max(0, now - lastStep) local dt = math.min(elapsed, .5) lastStep = now local members = config.members() local fighters = collect(members) if rule == "OneLife" then return runOneLife(members, fighters, now) end if rule == "HuntLeader" then updateLeaders(fighters); return false end if rule == "CaptureFlag" then runFlags(fighters, now); return false end if rule == "TrophyHunt" then runTrophies(fighters, now); return false end if rule == "Siege" then return runSiege(members, fighters, now, dt) end if hotPoint then hotPoint.step(fighters, now, elapsed); return false end local scoreNow = now >= nextScore if scoreNow then nextScore = now + 1 end local ownedBlue, ownedOrange = 0, 0 for _, zone in zones do local b, o = presence(fighters, zone, geometry) local side: Side? = if b == o then nil elseif b then "Blue" else "Orange" if side and side ~= zone.owner then if zone.claiming ~= side then zone.claiming, zone.progress = side, 0 end zone.progress += dt if zone.progress >= 3 then zone.owner, zone.progress = side, 0 end else zone.claiming, zone.progress = nil, 0 end paint(zone.marker, color(zone.owner)) if zone.owner == "Blue" then ownedBlue += 1 elseif zone.owner == "Orange" then ownedOrange += 1 end end if scoreNow then if ownedBlue > 0 then add("Blue", ownedBlue) end if ownedOrange > 0 then add("Orange", ownedOrange) end end status(string.format("Точки под контролем · %d / %d", ownedBlue, ownedOrange)) return false end match = { step = step, hit = if hotPoint then hotPoint.hit else nil, kill = function(player, victim, at, now) local side = sideOf(player) if not side then return end if rule == "TeamDeathmatch" then add(side, 1) elseif rule == "HuntLeader" then local target = opposite(side) local bonus = leaders[target] == victim add(side, if bonus then 3 else 1) if bonus then clearLeader(target) end elseif rule == "TrophyHunt" then tokenDrop(at, 1, now) end end, death = function(player, at, now) if hotPoint then hotPoint.death(player) end dropCarried(player, at, now) if rule == "OneLife" then config.bench(player) end end, leaving = function(player, at, now) if hotPoint then hotPoint.death(player) end dropCarried(player, at, now) local side = sideOf(player) if side and leaders[side] == player then clearLeader(side) end end, winner = function() if rule == "Siege" then local winner, reason = Score.winner(siegeTimes, SiegeConfig.TIE_EPSILON) state:SetAttribute("CitadelResultReason", reason) return winner end if blueScore ~= orangeScore then return if blueScore > orangeScore then "Blue" else "Orange" end return "Draw" end, destroy = function() if siegeState then siegeState:Destroy() for _, p in config.members() do p:SetAttribute("CitadelZone", nil) end end if active[arena] == match then active[arena] = nil end if hotPoint then hotPoint.destroy() end for _, side in sides do clearLeader(side) end for p in carried do p:SetAttribute("ArenaTokens", nil) end for _, flag in flags do if flag.carrier then flag.carrier:SetAttribute("ArenaFlag", nil) end end for _, z in zones do z.marker.body:Destroy() end folder:Destroy() status("") end, } active[arena] = match return match end return Objectives