65 lines
2.5 KiB
Luau
65 lines
2.5 KiB
Luau
--!strict
|
|
-- ModuleScript in ServerScriptService. Spawn markers belong to the player's arena.
|
|
local Players = game:GetService("Players")
|
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
|
local Modes = require(ReplicatedStorage:WaitForChild("ArenaModes") :: ModuleScript)
|
|
local RoundConfig = require(ReplicatedStorage:WaitForChild("Hunter"):WaitForChild("RoundConfig") :: ModuleScript)
|
|
local random = Random.new()
|
|
local ArenaSpawns = {}
|
|
|
|
function ArenaSpawns.choose(player: Player): SpawnLocation
|
|
local arena = Modes.geometry(Modes.of(player))
|
|
local folder = arena:WaitForChild("PlayerSpawns")
|
|
local side = player:GetAttribute("ArenaTeam")
|
|
local choices: {SpawnLocation} = {}
|
|
for _, marker in folder:GetChildren() do
|
|
local group = marker:GetAttribute("ArenaTeam")
|
|
if marker:IsA("SpawnLocation") and marker.Enabled and (group == nil or group == side) then
|
|
table.insert(choices, marker)
|
|
end
|
|
end
|
|
assert(#choices > 0, arena.Name .. " has no enabled spawns for this team")
|
|
local best = choices[random:NextInteger(1, #choices)]
|
|
local bestDistance = -1
|
|
local bestSafe: SpawnLocation? = nil
|
|
local bestSafeScore = -1
|
|
for _, marker in choices do
|
|
local nearest = 1000
|
|
local enemyDistance = math.huge
|
|
for _, other in Players:GetPlayers() do
|
|
local character = other.Character
|
|
local root = character and character:FindFirstChild("HumanoidRootPart")
|
|
local humanoid = character and character:FindFirstChildOfClass("Humanoid")
|
|
if other ~= player and other:GetAttribute("InArena") == true and Modes.same(player, other)
|
|
and root and root:IsA("BasePart") and humanoid and humanoid.Health > 0 then
|
|
local distance = (root.Position - marker.Position).Magnitude
|
|
nearest = math.min(nearest, distance)
|
|
local otherSide = other:GetAttribute("ArenaTeam")
|
|
if otherSide ~= nil and otherSide ~= side and other:GetAttribute("ArenaWaiting") ~= true then
|
|
enemyDistance = math.min(enemyDistance, distance)
|
|
end
|
|
end
|
|
end
|
|
local score = nearest + random:NextNumber(0, 24)
|
|
if enemyDistance >= RoundConfig.safeSpawnDistance and score > bestSafeScore then
|
|
bestSafe, bestSafeScore = marker, score
|
|
end
|
|
if score > bestDistance then
|
|
bestDistance = score
|
|
best = marker
|
|
end
|
|
end
|
|
return bestSafe or best
|
|
end
|
|
|
|
function ArenaSpawns.place(character: Model, marker: SpawnLocation)
|
|
character:PivotTo(marker.CFrame + Vector3.new(0, 4, 0))
|
|
local root = character:FindFirstChild("HumanoidRootPart")
|
|
if root and root:IsA("BasePart") then
|
|
root.AssemblyLinearVelocity = Vector3.zero
|
|
root.AssemblyAngularVelocity = Vector3.zero
|
|
end
|
|
end
|
|
|
|
return ArenaSpawns
|