local H=game:GetService("\080\108\097\121\101\114\115")local u=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local z=require((u:WaitForChild("\080\114\111\106\101\099\116")):WaitForChild("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101"))local A=(require((u:WaitForChild("\080\114\111\106\101\099\116")):WaitForChild("\083\101\114\118\101\114\083\099\114\105\112\116\083\101\114\118\105\099\101"))):GetContext(script)local Z=(z:GetResource("\069\120\116\101\114\110\097\108\085\116\105\108\046\078\101\120\117\115\073\110\115\116\097\110\099\101\046\078\101\120\117\115\073\110\115\116\097\110\099\101")):Extend()Z:SetClassName("\072\111\117\115\101\083\101\114\118\105\099\101")Z.LastPlayerCFrames={}A:SetContextResource(Z)local W=z:GetResource("\071\097\109\101\068\097\116\097\046\071\101\110\101\114\097\116\105\111\110\046\077\097\112\067\101\108\108\115")local s=z:GetResource("\071\097\109\101\083\116\097\116\101")local V=z:GetResource("\071\097\109\101\082\101\112\108\105\099\097\116\105\111\110")local U=A:GetResource("\083\101\114\118\105\099\101\046\068\117\110\103\101\111\110\083\101\114\118\105\099\101")local d=Instance.new("\070\111\108\100\101\114")d.Name="\072\111\117\115\101\083\116\097\116\101"d.Parent=s local f=Instance.new("\070\111\108\100\101\114")f.Name="\072\111\117\115\101\082\101\112\108\105\099\097\116\105\111\110"f.Parent=V local v=Instance.new("\082\101\109\111\116\101\069\118\101\110\116")v.Name="\083\116\097\114\116\072\111\117\115\101"v.Parent=f function Z.GetHouseStateObject(z,H,u)return d:FindFirstChild(tostring(H)..("\095"..tostring(u)))end function Z.GetHouseState(z,H,u)local A=z:GetHouseStateObject(H,u)return A and A.Value or"\065\067\084\073\086\069"end function Z.SetHouseState(A,H,u,z)local Z=A:GetHouseStateObject(H,u)if z=="\065\067\084\073\086\069"then if Z then Z:Destroy()end else if not Z then Z=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")Z.Name=tostring(H)..("\095"..tostring(u))Z.Value=z Z.Parent=d else Z.Value=z end end end function Z.StartHouse(A,u,z)if not W[u]or W[u][z]~=7 then return end if A:GetHouseState(u,z)~="\065\067\084\073\086\069"then return end A:SetHouseState(u,z,"\083\084\065\082\084\073\078\071")wait(5)local Z={}for H,A in pairs(H:GetPlayers())do local W=A.Character if W then local H=W:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")local s=W:FindFirstChild("\072\117\109\097\110\111\105\100")if H and(s and(s.Health>0 and(H.Position.X>=u*100-50 and(H.Position.X<=u*100+50 and(H.Position.Z>=z*100-50 and(H.Position.Z<=z*100+50 and H.Position.Y>=0))))))then table.insert(Z,A)end end end A:SetHouseState(u,z,"\073\078\065\067\084\073\086\069")if#Z>0 then U:RunDungeon(u,z,Z)end wait(30)A:SetHouseState(u,z,"\065\067\084\073\086\069")end v.OnServerEvent:Connect(function(H,u,z)local A=H.Character if not A then return end local W=A:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")if not W then return end if not(W.Position.X>=u*100-50 and(W.Position.X<=u*100+50 and(W.Position.Z>=z*100-50 and(W.Position.Z<=z*100+50 and W.Position.Y>=0))))then return end Z:StartHouse(u,z)end)return Z
--[[
TheNexusAvenger

Controls the houses and starting dungeons.
Class is static (should not be created).
--]]

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ReplicatedStorageProject = require(ReplicatedStorage:WaitForChild("Project"):WaitForChild("ReplicatedStorage"))
local ServerScriptServiceProject = require(ReplicatedStorage:WaitForChild("Project"):WaitForChild("ServerScriptService")):GetContext(script)

local HouseService = ReplicatedStorageProject:GetResource("ExternalUtil.NexusInstance.NexusInstance"):Extend()
HouseService:SetClassName("HouseService")
HouseService.LastPlayerCFrames = {}
ServerScriptServiceProject:SetContextResource(HouseService)

local MapCellData = ReplicatedStorageProject:GetResource("GameData.Generation.MapCells")
local GameState = ReplicatedStorageProject:GetResource("GameState")
local GameReplication = ReplicatedStorageProject:GetResource("GameReplication")
local DungeonService = ServerScriptServiceProject:GetResource("Service.DungeonService")



--Set up the replication.
local HouseState = Instance.new("Folder")
HouseState.Name = "HouseState"
HouseState.Parent = GameState

local HouseReplication = Instance.new("Folder")
HouseReplication.Name = "HouseReplication"
HouseReplication.Parent = GameReplication

local StartHouse = Instance.new("RemoteEvent")
StartHouse.Name = "StartHouse"
StartHouse.Parent = HouseReplication



--[[
Gets the state object for a house.
--]]
function HouseService:GetHouseStateObject(X,Y)
    return HouseState:FindFirstChild(tostring(X).."_"..tostring(Y))
end

--[[
Gets the state for a house.
--]]
function HouseService:GetHouseState(X,Y)
    local StateObject = self:GetHouseStateObject(X,Y)
    return StateObject and StateObject.Value or "ACTIVE"
end

--[[
Sets the state for a house.
--]]
function HouseService:SetHouseState(X,Y,State)
    local StateObject = self:GetHouseStateObject(X,Y)
    if State == "ACTIVE" then
        --Destroy the state object if the house meant to be active (no object implies active)
        if StateObject then
            StateObject:Destroy()
        end
    else
        --Create the object if it doesn't exist, or set the state object.
        if not StateObject then
            StateObject = Instance.new("StringValue")
            StateObject.Name = tostring(X).."_"..tostring(Y)
            StateObject.Value = State
            StateObject.Parent = HouseState
        else
            StateObject.Value = State
        end
    end
end

--[[
Starts the house.
--]]
function HouseService:StartHouse(X,Y)
    --Return if the house isn't a house.
    if not MapCellData[X] or MapCellData[X][Y] ~= 7 then
        return
    end

    --Return if the house isn't acitve.
    if self:GetHouseState(X,Y) ~= "ACTIVE" then
        return
    end

    --Prepare the house.
    self:SetHouseState(X,Y,"STARTING")
    wait(5)

    --Determine the players in the cell.
    local PlayersInCell = {}
    for _,Player in pairs(Players:GetPlayers()) do
        local Character = Player.Character
        if Character then
            local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
            local Humanoid = Character:FindFirstChild("Humanoid")
            if HumanoidRootPart and Humanoid and Humanoid.Health > 0 and HumanoidRootPart.Position.X >= (X * 100) - 50 and HumanoidRootPart.Position.X <= (X * 100) + 50 and HumanoidRootPart.Position.Z >= (Y * 100) - 50 and HumanoidRootPart.Position.Z <= (Y * 100) + 50 and HumanoidRootPart.Position.Y >= 0 then
                table.insert(PlayersInCell,Player)
            end
        end
    end

    --Start the house.
    self:SetHouseState(X,Y,"INACTIVE")
    if #PlayersInCell > 0 then
        DungeonService:RunDungeon(X,Y,PlayersInCell)
    end
    wait(30)

    --Reset the house.
    self:SetHouseState(X,Y,"ACTIVE")
end



--Connect replication.
StartHouse.OnServerEvent:Connect(function(Player,X,Y)
    --Return if the player is not in the cell.
    local Character = Player.Character
    if not Character then return end
    local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
    if not HumanoidRootPart then return end
    if not (HumanoidRootPart.Position.X >= (X * 100) - 50 and HumanoidRootPart.Position.X <= (X * 100) + 50 and HumanoidRootPart.Position.Z >= (Y * 100) - 50 and HumanoidRootPart.Position.Z <= (Y * 100) + 50 and HumanoidRootPart.Position.Y >= 0) then return end
    
    --Start the house.
    HouseService:StartHouse(X,Y)
end)



return HouseService