local I=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local x=game:GetService("\072\116\116\112\083\101\114\118\105\099\101")local u=require((I: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 U=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\079\098\106\101\099\116")local k=U:Extend()k:SetClassName("\066\111\111\108\071\114\105\100")function k.__new(U,I,x,u)U:InitializeSuper()U.GridSizeX=I U.GridSizeY=x U.Grid={}U.DefaultValue=u or false for I=1,I,1 do U.Grid[I]={}for x=1,x,1 do U.Grid[I][x]=u or false end end end function k.GetValue(u,I,x)if I<1 or I>u.GridSizeX or x<1 or x>u.GridSizeY then return u.DefaultValue end return u.Grid[I][x]end function k.SetValue(U,I,x,u)if I<1 or I>U.GridSizeX or x<1 or x>U.GridSizeY then return end U.Grid[I][x]=u end function k.LoadFromString(u,I)local U=x:JSONDecode(I)for I=1,u.GridSizeX,1 do local x=U[I]if x then local U=x[1]local k=2 local R=x[2]for p=1,u.GridSizeY,1 do u.Grid[I][p]=U R=R-1 if R==0 then U=not U k=k+1 R=x[k]or 0 end end end end end function k.Serialize(I)local u={}for I,x in pairs(I.Grid)do local U={x[1],0}u[I]=U local k=x[1]local R=2 for I,x in pairs(x)do if x==k then U[R]=U[R]+1 else R=R+1 k=x U[R]=1 end end end return x:JSONEncode(u)end return k
--[[
TheNexusAvenger

Data structure for a grid of bools.
--]]

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

local ReplicatedStorageProject = require(ReplicatedStorage:WaitForChild("Project"):WaitForChild("ReplicatedStorage"))

local NexusObject = ReplicatedStorageProject:GetResource("ExternalUtil.NexusInstance.NexusObject")

local BoolGrid = NexusObject:Extend()
BoolGrid:SetClassName("BoolGrid")



--[[
Creates a bool grid.
--]]
function BoolGrid:__new(GridSizeX,GridSizeY,DefaultValue)
    self:InitializeSuper()

    --Store the state.
    self.GridSizeX = GridSizeX
    self.GridSizeY = GridSizeY
    self.Grid = {}
    self.DefaultValue = DefaultValue or false

    --Initialize the grid.
    for X = 1,GridSizeX do
        self.Grid[X] = {}
        for Y = 1,GridSizeY do
            self.Grid[X][Y] = DefaultValue or false
        end
    end
end

--[[
Returns the value for a given grid position.
--]]
function BoolGrid:GetValue(X,Y)
    --Return the default if the value is out of range.
    if X < 1 or X > self.GridSizeX or Y < 1 or Y > self.GridSizeY then
        return self.DefaultValue
    end

    --Return the value in the grid.
    return self.Grid[X][Y]
end

--[[
Sets the value for a given grid position.
--]]
function BoolGrid:SetValue(X,Y,Value)
    --Return if the value is out of range.
    if X < 1 or X > self.GridSizeX or Y < 1 or Y > self.GridSizeY then
        return
    end

    --Set the value.
    self.Grid[X][Y] = Value
end

--[[
Initializes the grid from a string.
--]]
function BoolGrid:LoadFromString(DataString)
    --Parse the string.
    local Data = HttpService:JSONDecode(DataString)

    --Set the data from the string.
    for X = 1,self.GridSizeX do
        local Row = Data[X]
        if Row then
            local CurrentValue = Row[1]
            local CurrentNumber = 2
            local Remaining = Row[2]
            for Y = 1,self.GridSizeY do
                --Set the value.
                self.Grid[X][Y] = CurrentValue
                Remaining = Remaining - 1

                --Set the next value if there is it should be changed.
                if Remaining == 0 then
                    CurrentValue = not CurrentValue
                    CurrentNumber = CurrentNumber + 1
                    Remaining = Row[CurrentNumber] or 0
                end
            end
        end
    end
end

--[[
Serializes the grid to a string.
--]]
function BoolGrid:Serialize()
    --Serailize the rows.
    local Data = {}
    for X,Row in pairs(self.Grid) do
        local RowData = {Row[1],0}
        Data[X] = RowData
        local CurrentValue = Row[1]
        local CurrentId = 2
        for Y,Value in pairs(Row) do
            if Value == CurrentValue then
                RowData[CurrentId] = RowData[CurrentId] + 1
            else
                CurrentId = CurrentId + 1
                CurrentValue = Value
                RowData[CurrentId] = 1
            end
        end
    end

    --Return the data as a string.
    return HttpService:JSONEncode(Data)
end



return BoolGrid