--Generate a basic map


local map = {}

map.sizeX = 100
map.sizeY = 100
map.fatgridSize = 10
map.grid = {}
map.fatgrid = {}
map.impassible = 1000000



local cardinals = { Vector3.new(-1,0,0),Vector3.new(1,0,0),Vector3.new(0,-1,0),Vector3.new(0,1,0) }
local corners = { 
	{ dir = Vector3.new(-1,-1,0), a = Vector3.new(-1, 0,0), b = Vector3.new(0,-1,0) },
	{ dir = Vector3.new( 1,-1,0), a = Vector3.new( 1, 0,0), b = Vector3.new(0,-1,0) },
	{ dir = Vector3.new( 1, 1,0), a = Vector3.new( 1, 0,0), b = Vector3.new(0, 1,0) },
	{ dir = Vector3.new(-1, 1,0), a = Vector3.new(-1, 0,0), b = Vector3.new(0, 1,0) },
}

function map:GetPoint(vec2 : Vector3) 
	return map.grid[vec2]
end

function map:SetPoint(vec2 : Vector3, val : number, cost : number) 
	
	local getPoint = map.grid[vec2]
	if (getPoint == nil) then
		getPoint = { tile = 0, cost = 0, connections = {}, x = vec2.X, y = vec2.Y }
		map.grid[vec2] = getPoint
	end
	getPoint.tile = val
	getPoint.cost = cost
end

function map:CreateConnections(vec2)
	local getPoint = map.grid[vec2]
	if (getPoint == nil) then
		return
	end
	
	getPoint.connections = {}
	for key,vec in pairs(cardinals) do
		local neighbour = self:GetPoint(vec2 + vec)
		if (neighbour) then
			table.insert(getPoint.connections, { n = neighbour, cost = neighbour.cost + 10 })
		end
	end
	for key,record in pairs(corners) do
		local neighbour = self:GetPoint(vec2 + record.dir)
		
		local a = self:GetPoint(vec2 + record.a)
		local b = self:GetPoint(vec2 + record.b)
		if (a and a.cost > 0) then 
			continue
		end
		if (b and b.cost > 0) then 
			continue
		end
		
		if (neighbour) then
			table.insert(getPoint.connections, { n = neighbour, cost = neighbour.cost + 14 })
		end
	end
end


function map:GenerateDummy()
	
	math.randomseed(1)
		

	--empty air
	for y=0,map.sizeY-1 do
		for x=0,map.sizeX-1 do
			self:SetPoint(Vector3.new(x,y,0),0 , 0)
		end
	end
	
	--borders	
	for y=0,map.sizeY-1 do
		self:SetPoint(Vector3.new(0,y,0),1, map.impassible)
		self:SetPoint(Vector3.new(map.sizeX-1,y,0),1, map.impassible)
	end

	for x=0,map.sizeX-1 do
		self:SetPoint(Vector3.new(x,0,0),1 ,map.impassible)
		self:SetPoint(Vector3.new(x,map.sizeY-1,0),1, map.impassible)
	end

	
	for y=0,map.sizeY-1 do
		for x=0,map.sizeX-1 do
			if (math.random()<0.15) then
				self:SetPoint(Vector3.new(x,y,0), 1 , map.impassible)
			end
		end
	end
	
	--Create connections
	for y=0,map.sizeY-1 do
		for x=0,map.sizeX-1 do
			self:CreateConnections(Vector3.new(x,y,0))
		end
	end
end

function map:CreateVisuals()
	for vec2,value in pairs(self.grid) do
		
		if (value.tile > 0) then
			local part = Instance.new("Part")
			part.Size = Vector3.new(6,3,6)
			part.Position = Vector3.new(3 + vec2.x * 6 , 1.5, 3 + vec2.y * 6)
			part.Anchored = true
			part.Parent = game.Workspace
			part.Transparency = 0
		end
	end
	
end


function map:MakePath(path, color)
	for _,value in pairs(path) do
		local part = Instance.new("Part")
		part.Size = Vector3.new(3,3,3)
		part.Position = Vector3.new(3 + value.x * 6 , 3, 3 + value.y * 6)
		part.Anchored = true
		part.Color = color
		part.Parent = game.Workspace
	end

	
end

function debugBall(node,color)
	local part = Instance.new("Part")
	part.Size = Vector3.new(3,3,3)
	part.Position = Vector3.new(3 + node.x * 6 , 3, 3 + node.y * 6)
	part.Anchored = true
	part.Color = color
	part.Parent = game.Workspace
	
end