local H=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local u=require((H: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 z=(require((H: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 A=u:GetResource("\083\116\097\116\101\046\066\111\111\108\071\114\105\100")local Z=z:GetResource("\083\101\114\118\105\099\101\046\065\099\104\105\101\118\101\109\101\110\116\083\101\114\118\105\099\101")local W=z:GetResource("\083\101\114\118\105\099\101\046\080\108\097\121\101\114\068\097\116\097\083\101\114\118\105\099\101")local s=z:GetResource("\083\101\114\118\105\099\101\046\073\110\118\101\110\116\111\114\121\083\101\114\118\105\099\101")local V=(u: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()V:SetClassName("\077\097\112\068\105\115\099\111\118\101\114\121\083\101\114\118\105\099\101")V.DiscoveredCellsBoolGrids={}z:SetContextResource(V)function V.LoadPlayer(u,H)local z=W:GetPlayerData(H)local V=s:GetInventory(H)local U=A.new(200,200)u.DiscoveredCellsBoolGrids[H]=U u.DiscoveredCellsBoolGrids[H]:LoadFromString(z:GetValue("\068\105\115\099\111\118\101\114\101\100\077\097\112\067\101\108\108\115"));(coroutine.wrap(function()while H.Parent do local u=H.Character if u then local A=u:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")if A then local u,s=math.floor(A.Position.X/100+.5),math.floor(A.Position.Z/100+.5)local d=2+#V:GetItemSlots("\086\105\115\105\111\110\082\097\100\105\117\115\073\110\099\114\101\097\115\101")local f=false for u=u-d,u+d,1 do for z=s-d,s+d,1 do if not U:GetValue(u,z)then U:SetValue(u,z,true)f=true if u<10 and z>183 then Z:AwardAchievement(H,"\066\101\121\111\110\100\084\104\101\082\111\099\107\115")end end end end if f then z:SetValue("\068\105\115\099\111\118\101\114\101\100\077\097\112\067\101\108\108\115",U:Serialize())W:SavesPlayerData(H)end end end wait()end end))()end function V.ClearPlayer(u,H)u.DiscoveredCellsBoolGrids[H]=nil end return V
--[[
TheNexusAvenger

Stores and replicates player data.
Class is static (should not be created).
--]]

local ReplicatedStorage = game:GetService("ReplicatedStorage")

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

local BoolGrid = ReplicatedStorageProject:GetResource("State.BoolGrid")
local AchievementService = ServerScriptServiceProject:GetResource("Service.AchievementService")
local PlayerDataService = ServerScriptServiceProject:GetResource("Service.PlayerDataService")
local InventoryService = ServerScriptServiceProject:GetResource("Service.InventoryService")

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



--[[
Initializes the data for a player.
--]]
function MapDiscoveryService:LoadPlayer(Player)
    --Set up the grid.
    local PlayerData = PlayerDataService:GetPlayerData(Player)
    local Inventory = InventoryService:GetInventory(Player)
    local PlayerBoolGrid = BoolGrid.new(200,200)
    self.DiscoveredCellsBoolGrids[Player] = PlayerBoolGrid
    self.DiscoveredCellsBoolGrids[Player]:LoadFromString(PlayerData:GetValue("DiscoveredMapCells"))

    --Set up discovering the map.
    coroutine.wrap(function()
        while Player.Parent do
            --Get the center.
            local Character = Player.Character
            if Character then
                local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
                if HumanoidRootPart then
                    local CenterX,CenterY = math.floor((HumanoidRootPart.Position.X/100) + 0.5),math.floor((HumanoidRootPart.Position.Z/100) + 0.5)
                    local Range = 2 + #Inventory:GetItemSlots("VisionRadiusIncrease")
                    
                    --Discover the sections and mark if new sections were discovered.
                    local SectionsWereDiscovered = false
                    for X = CenterX - Range,CenterX + Range do
                        for Y = CenterY - Range,CenterY + Range do
                            if not PlayerBoolGrid:GetValue(X,Y) then
                                PlayerBoolGrid:SetValue(X,Y,true)
                                SectionsWereDiscovered = true

                                --Award the beyond the rocks achievement.
                                if X < 10 and Y > 183 then
                                    AchievementService:AwardAchievement(Player,"BeyondTheRocks")
                                end
                            end
                        end
                    end

                    --Set the value if it changed.
                    if SectionsWereDiscovered then
                        PlayerData:SetValue("DiscoveredMapCells",PlayerBoolGrid:Serialize())
                        PlayerDataService:SavesPlayerData(Player)
                    end
                end
            end
            wait()
        end
    end)()
end

--[[
Clears a player.
--]]
function MapDiscoveryService:ClearPlayer(Player)
    --Clear the resources.
    self.DiscoveredCellsBoolGrids[Player] = nil
end



return MapDiscoveryService