--!strict
-- Creates the reusable map-piece pool in ServerStorage once per place.

local ServerStorage = game:GetService("ServerStorage")

local old = ServerStorage:FindFirstChild("GameZonePrefabs")
if old then old:Destroy() end
local pool = Instance.new("Folder")
pool.Name = "GameZonePrefabs"
pool:SetAttribute("Ready", false)
pool.Parent = ServerStorage

local function block(parent: Instance, name: string, size: Vector3, position: Vector3, color: Color3, material: Enum.Material): Part
	local p = Instance.new("Part")
	p.Name, p.Size, p.Position = name, size, position
	p.Anchored, p.CanCollide, p.Color, p.Material = true, true, color, material
	p.Parent = parent
	return p
end

local function connector(parent: Model, name: string, position: Vector3, direction: Vector3)
	local a = Instance.new("Attachment")
	a.Name, a.Position, a.Axis = name, position, direction
	a:SetAttribute("Connector", true)
	a.Parent = parent.PrimaryPart
end

local function prefab(name: string, size: Vector3, build: (Model, Part) -> ())
	local model = Instance.new("Model")
	model.Name = name
	model:SetAttribute("TileSize", size)
	local root = block(model, "Root", Vector3.new(1, 1, 1), Vector3.zero, Color3.new(1, 1, 1), Enum.Material.SmoothPlastic)
	root.Transparency, root.CanCollide = 1, false
	root:SetAttribute("PrefabRoot", true)
	model.PrimaryPart = root
	build(model, root)
	model.Parent = pool
end

local road = Color3.fromRGB(48, 52, 58)
local wall = Color3.fromRGB(178, 184, 220)
local roof = Color3.fromRGB(115, 119, 154)
local accent = Color3.fromRGB(255, 145, 91)
local sidewalk = Color3.fromRGB(226, 202, 190)

local function gridTexture(part: BasePart, tile: number, color: Color3)
	local function face(normal: Vector3)
		local texture = Instance.new("Texture")
		texture.Name = "ConcreteGrid"
		texture.Texture = "rbxasset://textures/Studs.png"
		texture.Face = if normal == Vector3.yAxis then Enum.NormalId.Top
			elseif normal == -Vector3.yAxis then Enum.NormalId.Bottom
			elseif normal == Vector3.xAxis then Enum.NormalId.Right
			elseif normal == -Vector3.xAxis then Enum.NormalId.Left
			elseif normal == Vector3.zAxis then Enum.NormalId.Back else Enum.NormalId.Front
		texture.StudsPerTileU, texture.StudsPerTileV = tile, tile
		texture.Color3 = color
		texture.Transparency = 0.68
		texture.Parent = part
	end
	for _, axis in { Vector3.xAxis, -Vector3.xAxis, Vector3.yAxis, -Vector3.yAxis, Vector3.zAxis, -Vector3.zAxis } do face(axis) end
end

local function inkEdges(parent: Instance, source: BasePart)
	local model = source:FindFirstAncestorOfClass("Model")
	if model then
		model:SetAttribute("PencilOutline", true)
	end
end

local function wallPanel(parent: Instance, name: string, size: Vector3, position: Vector3)
	local panel = block(parent, name, size, position, wall, Enum.Material.Concrete)
	gridTexture(panel, 8, Color3.fromRGB(205, 209, 244))
	inkEdges(parent, panel)
	return panel
end

prefab("RoadStraight", Vector3.new(40, 1, 40), function(m, root)
	local street = block(m, "Road", Vector3.new(40, 0.3, 20), Vector3.new(0, -0.35, 0), road, Enum.Material.Asphalt)
	inkEdges(m, street)
	block(m, "SidewalkA", Vector3.new(40, 0.2, 10), Vector3.new(0, -0.1, -15), sidewalk, Enum.Material.Concrete)
	block(m, "SidewalkB", Vector3.new(40, 0.2, 10), Vector3.new(0, -0.1, 15), sidewalk, Enum.Material.Concrete)
	block(m, "CurbA", Vector3.new(40, 0.2, 0.5), Vector3.new(0, -0.1, -10.25), Color3.fromRGB(173, 169, 184), Enum.Material.Concrete)
	block(m, "CurbB", Vector3.new(40, 0.2, 0.5), Vector3.new(0, -0.1, 10.25), Color3.fromRGB(173, 169, 184), Enum.Material.Concrete)
	for x = -15, 15, 10 do
		block(m, "PencilLaneDash", Vector3.new(4, 0.04, 0.3), Vector3.new(x, -0.18, 0), Color3.fromRGB(191, 190, 184), Enum.Material.SmoothPlastic)
	end
	connector(m, "West", Vector3.new(-20, 0, 0), Vector3.xAxis)
	connector(m, "East", Vector3.new(20, 0, 0), -Vector3.xAxis)
end)

prefab("RoadIntersection", Vector3.new(40, 1, 40), function(m, root)
	local asphalt = block(m, "AsphaltBed", Vector3.new(39.8, 0.3, 39.8), Vector3.new(0, -0.35, 0), road, Enum.Material.Asphalt)
	inkEdges(m, asphalt)
	for _, x in {-14.5, 14.5} do
		for _, z in {-14.5, 14.5} do
			block(m, "CornerPavement", Vector3.new(11, 0.2, 11), Vector3.new(x, -0.1, z), sidewalk, Enum.Material.Concrete)
		end
	end
	for _, offset in {-14, 14} do
		for stripe = -2, 2 do
			block(m, "CrosswalkStripe", Vector3.new(0.65, 0.04, 1.4), Vector3.new(offset, -0.18, stripe * 2.1), Color3.fromRGB(210, 204, 195), Enum.Material.SmoothPlastic)
		end
	end
end)

prefab("Plaza", Vector3.new(40, 1, 40), function(m, root)
	block(m, "PlazaPaving", Vector3.new(39.8, 0.25, 39.8), Vector3.new(0, -0.15, 0), sidewalk, Enum.Material.Concrete)
	for _, x in {-15, 15} do
		for _, z in {-15, 15} do
			block(m, "LavenderInset", Vector3.new(8, 0.08, 8), Vector3.new(x, 0.02, z), Color3.fromRGB(190, 190, 218), Enum.Material.CeramicTiles)
		end
	end
	local basin = block(m, "FountainBasin", Vector3.new(11, 1.2, 11), Vector3.new(0, 0.55, 0), Color3.fromRGB(164, 177, 202), Enum.Material.Concrete)
	basin.Shape = Enum.PartType.Cylinder
	local water = block(m, "FountainWater", Vector3.new(8.5, 0.35, 8.5), Vector3.new(0, 1.28, 0), Color3.fromRGB(104, 205, 221), Enum.Material.Glass)
	water.Shape, water.Transparency = Enum.PartType.Cylinder, 0.18
	block(m, "FountainStem", Vector3.new(2, 5, 2), Vector3.new(0, 3.4, 0), Color3.fromRGB(190, 190, 218), Enum.Material.Concrete)
	local crown = block(m, "FountainCap", Vector3.new(4, 0.7, 4), Vector3.new(0, 6.1, 0), accent, Enum.Material.Slate)
	crown.Shape = Enum.PartType.Cylinder
	for _, x in {-12, 12} do
		for _, z in {-12, 12} do
			local lamp = block(m, "PlazaLantern", Vector3.new(0.8, 7, 0.8), Vector3.new(x, 3.5, z), Color3.fromRGB(58, 60, 78), Enum.Material.Metal)
			inkEdges(m, lamp)
			block(m, "LanternGlow", Vector3.new(2, 1.5, 2), Vector3.new(x, 7.3, z), Color3.fromRGB(255, 218, 145), Enum.Material.Neon)
		end
	end
end)

prefab("RoadCorner", Vector3.new(40, 1, 40), function(m, root)
	local roadX = block(m, "RoadX", Vector3.new(40, 0.3, 12), Vector3.new(0, -0.35, 0), road, Enum.Material.Asphalt)
	local roadZ = block(m, "RoadZ", Vector3.new(12, 0.3, 40), Vector3.new(0, -0.35, 0), road, Enum.Material.Asphalt)
	inkEdges(m, roadX)
	inkEdges(m, roadZ)
	connector(m, "North", Vector3.new(0, 0, -20), -Vector3.zAxis)
	connector(m, "East", Vector3.new(20, 0, 0), -Vector3.xAxis)
end)

prefab("OpenLot", Vector3.new(40, 1, 40), function(m, root)
	block(m, "Paving", Vector3.new(39.8, 0.25, 39.8), Vector3.new(0, -0.22, 0), Color3.fromRGB(222, 202, 190), Enum.Material.Concrete)
	connector(m, "North", Vector3.new(0, 0, -20), -Vector3.zAxis)
	connector(m, "East", Vector3.new(20, 0, 0), -Vector3.xAxis)
	connector(m, "South", Vector3.new(0, 0, 20), Vector3.zAxis)
	connector(m, "West", Vector3.new(-20, 0, 0), Vector3.xAxis)
end)

prefab("Courtyard", Vector3.new(40, 1, 40), function(m, root)
	block(m, "Ground", Vector3.new(34, 0.5, 34), Vector3.new(0, 0.25, 0), Color3.fromRGB(216, 199, 183), Enum.Material.Concrete)
	for _, p in {{-14, -14}, {14, -14}, {-14, 14}, {14, 14}} do
		local pillar = wallPanel(m, "Pillar", Vector3.new(3, 8, 3), Vector3.new(p[1], 4, p[2]))
		gridTexture(pillar, 5, Color3.fromRGB(205, 209, 244))
	end
	connector(m, "South", Vector3.new(0, 0, 20), Vector3.zAxis)
	connector(m, "West", Vector3.new(-20, 0, 0), Vector3.xAxis)
end)

local function house(name: string, style: number)
	prefab(name, Vector3.new(40, 1, 40), function(m, root)
		local wallColor = if style == 1 then Color3.fromRGB(220, 207, 195) elseif style == 2 then Color3.fromRGB(199, 207, 229) else Color3.fromRGB(225, 190, 167)
		wallPanel(m, "RearFacadeLeft", Vector3.new(10, 16, 2), Vector3.new(-10, 8, -14)).Color = wallColor
		wallPanel(m, "RearFacadeRight", Vector3.new(10, 16, 2), Vector3.new(10, 8, -14)).Color = wallColor
		local left = wallPanel(m, "LeftFacade", Vector3.new(2, 16, 28), Vector3.new(-14, 8, 0))
		left.Color = wallColor
		local right = wallPanel(m, "RightFacade", Vector3.new(2, 16, 28), Vector3.new(14, 8, 0))
		right.Color = wallColor
		wallPanel(m, "FrontLeft", Vector3.new(10, 16, 2), Vector3.new(-10, 8, 14)).Color = wallColor
		wallPanel(m, "FrontRight", Vector3.new(10, 16, 2), Vector3.new(10, 8, 14)).Color = wallColor
		block(m, "InteriorFloor", Vector3.new(25, 0.3, 25), Vector3.new(0, -0.08, 0), Color3.fromRGB(218, 197, 183), Enum.Material.Concrete)
		local roofPart = block(m, "OverhangingRoof", Vector3.new(36, 1.2, 34), Vector3.new(0, 16.8, 0), roof, Enum.Material.Slate)
		inkEdges(m, roofPart)
		for _, panelData in {
			{"UpperRear", Vector3.new(22, 7, 1.2), Vector3.new(0, 20.9, -11.4)},
			{"UpperLeft", Vector3.new(1.2, 7, 18), Vector3.new(-10.4, 20.9, -2)},
			{"UpperRight", Vector3.new(1.2, 7, 18), Vector3.new(10.4, 20.9, -2)},
			{"UpperFrontLeft", Vector3.new(7, 7, 1.2), Vector3.new(-7.5, 20.9, 7.4)},
			{"UpperFrontRight", Vector3.new(7, 7, 1.2), Vector3.new(7.5, 20.9, 7.4)},
		} do
			local upperWall = wallPanel(m, panelData[1], panelData[2], panelData[3])
			upperWall.Color = wallColor
		end
		local topRoof = block(m, "UpperRoof", Vector3.new(26, 0.8, 24), Vector3.new(0, 24.8, -2), accent, Enum.Material.Slate)
		inkEdges(m, topRoof)
		for _, x in {-8, 8} do
			local window = block(m, "Window", Vector3.new(5, 4, 0.18), Vector3.new(x, 9, 14.96), Color3.fromRGB(151, 205, 220), Enum.Material.Glass)
			window.Transparency = 0.25
			inkEdges(m, window)
			block(m, "WindowSill", Vector3.new(6, 0.35, 0.5), Vector3.new(x, 6.8, 14.8), accent, Enum.Material.Wood)
		end
		local door = block(m, "Door", Vector3.new(5, 10, 0.3), Vector3.new(0, 5, 14.95), Color3.fromRGB(127, 78, 63), Enum.Material.Wood)
		door.CanCollide = false
		inkEdges(m, door)
		local porch = block(m, "Porch", Vector3.new(12, 0.6, 5), Vector3.new(0, 0.2, 17), Color3.fromRGB(221, 195, 178), Enum.Material.Concrete)
		inkEdges(m, porch)
		-- Exterior steps lead to the broad first roof; a second stair continues to the rooftop.
		for step = 0, 14 do
			block(m, "RoofAccessStep", Vector3.new(5.2, 1.1, 2.25), Vector3.new(17.2, 0.55 + step * 1.1, 14 - step * 2.1), accent, Enum.Material.Metal)
		end
		for step = 0, 6 do
			block(m, "UpperRoofStep", Vector3.new(2.2, 1.1, 2.25), Vector3.new(12.5, 17.95 + step * 1.1, 7 - step * 2.2), roof, Enum.Material.Metal)
		end
		connector(m, "North", Vector3.new(0, 0, -20), -Vector3.zAxis)
		connector(m, "South", Vector3.new(0, 0, 20), Vector3.zAxis)
	end)
end
house("HouseWarm", 1)
house("HouseBlue", 2)
house("HouseTerracotta", 3)

prefab("Skybridge", Vector3.new(40, 1, 40), function(m, root)
	local deck = block(m, "BridgeDeck", Vector3.new(43, 0.8, 8), Vector3.new(0, 17.7, 0), Color3.fromRGB(178, 184, 220), Enum.Material.Concrete)
	inkEdges(m, deck)
	for _, z in {-4, 4} do
		block(m, "BridgeRail", Vector3.new(43, 2.2, 0.45), Vector3.new(0, 19.2, z), Color3.fromRGB(93, 95, 126), Enum.Material.Metal)
	end
	for _, x in {-15, 15} do
		block(m, "BridgeInkMark", Vector3.new(0.45, 0.04, 5.5), Vector3.new(x, 18.13, 0), accent, Enum.Material.SmoothPlastic)
	end
end)

prefab("Tree", Vector3.new(40, 1, 40), function(m, root)
	local foliageRng = Random.new(DateTime.now().UnixTimestampMillis % 2147483647)
	local trunk = block(m, "Trunk", Vector3.new(2.5, 10, 2.5), Vector3.new(0, 5, 0), Color3.fromRGB(120, 83, 65), Enum.Material.Wood)
	inkEdges(m, trunk)
	for _, crown in {{0, 12, 0, 13}, {-4, 10, 2, 9}, {4, 11, -2, 10}, {1, 16, 1, 8}} do
		local foliage = Instance.new("Part")
		foliage.Name = "PaperLeafCanopy"
		foliage.Shape = Enum.PartType.Ball
		foliage.Size = Vector3.new(crown[4], crown[4] * 0.82, crown[4])
		foliage.Position = Vector3.new(crown[1], crown[2], crown[3])
		foliage.Anchored = true
		foliage.CanCollide = true
		foliage.Color = Color3.fromRGB(foliageRng:NextInteger(123, 158), 157, 137)
		foliage.Material = Enum.Material.SmoothPlastic
		foliage.Parent = m
		inkEdges(m, foliage)
	end
	block(m, "Planter", Vector3.new(10, 1, 10), Vector3.new(0, 0.1, 0), Color3.fromRGB(186, 170, 160), Enum.Material.Concrete)
end)

prefab("StreetLamp", Vector3.new(40, 1, 40), function(m, root)
	local post = block(m, "LampPost", Vector3.new(0.7, 12, 0.7), Vector3.new(0, 6, 0), Color3.fromRGB(58, 60, 78), Enum.Material.Metal)
	inkEdges(m, post)
	local arm = block(m, "LampArm", Vector3.new(4, 0.6, 0.6), Vector3.new(1.6, 11.7, 0), Color3.fromRGB(58, 60, 78), Enum.Material.Metal)
	inkEdges(m, arm)
	local lamp = block(m, "WarmLamp", Vector3.new(2, 0.5, 1.5), Vector3.new(2.8, 11.2, 0), Color3.fromRGB(255, 218, 145), Enum.Material.Neon)
	inkEdges(m, lamp)
end)

prefab("RoofRamp", Vector3.new(40, 1, 40), function(m, root)
	local slope = Instance.new("WedgePart")
	slope.Name = "RoofAccessRamp"
	slope.Size = Vector3.new(12, 18, 34)
	slope.CFrame = CFrame.new(0, 9, 0) * CFrame.Angles(0, math.rad(90), 0)
	slope.Anchored = true
	slope.Color = accent
	slope.Material = Enum.Material.Metal
	slope.Parent = m
	block(m, "LowerLanding", Vector3.new(14, 0.6, 8), Vector3.new(0, 0.3, 16), Color3.fromRGB(221, 195, 178), Enum.Material.Concrete)
	block(m, "UpperLanding", Vector3.new(14, 0.6, 8), Vector3.new(0, 9.3, -16), roof, Enum.Material.Slate)
	connector(m, "Lower", Vector3.new(0, 0, 20), Vector3.zAxis)
	connector(m, "Upper", Vector3.new(0, 9, -20), -Vector3.zAxis)
end)

prefab("Barricade", Vector3.new(40, 1, 40), function(m, root)
	local body = block(m, "ConcreteBlock", Vector3.new(13, 5, 4), Vector3.new(0, 2.5, 0), Color3.fromRGB(221, 139, 104), Enum.Material.Concrete)
	gridTexture(body, 6, Color3.fromRGB(255, 204, 157))
	inkEdges(m, body)
	for x = -4, 4, 4 do
		block(m, "PaintStripe", Vector3.new(1.2, 5.1, 4.1), Vector3.new(x, 2.5, 0), Color3.fromRGB(244, 211, 172), Enum.Material.SmoothPlastic)
	end
	connector(m, "West", Vector3.new(-20, 0, 0), Vector3.xAxis)
	connector(m, "East", Vector3.new(20, 0, 0), -Vector3.xAxis)
end)

prefab("Building", Vector3.new(40, 1, 40), function(m, root)
	wallPanel(m, "BuildingLeft", Vector3.new(14, 14, 28), Vector3.new(-13, 7, 0))
	wallPanel(m, "BuildingRight", Vector3.new(14, 14, 28), Vector3.new(13, 7, 0))
	wallPanel(m, "RearWall", Vector3.new(28, 14, 2), Vector3.new(0, 7, -14))
	wallPanel(m, "FrontLeft", Vector3.new(9, 14, 2), Vector3.new(-9.5, 7, 14))
	wallPanel(m, "FrontRight", Vector3.new(9, 14, 2), Vector3.new(9.5, 7, 14))
	block(m, "Roof", Vector3.new(34, 1, 30), Vector3.new(0, 14.5, 0), roof, Enum.Material.Metal)
	block(m, "PassageFloor", Vector3.new(10, 0.5, 28), Vector3.new(0, 0.25, 0), Color3.fromRGB(221, 195, 178), Enum.Material.Concrete)
	block(m, "OrangeCover", Vector3.new(9, 12, 7), Vector3.new(0, 6, 3), accent, Enum.Material.SmoothPlastic)
	connector(m, "North", Vector3.new(0, 0, -20), -Vector3.zAxis)
	connector(m, "South", Vector3.new(0, 0, 20), Vector3.zAxis)
end)

prefab("Alley", Vector3.new(40, 1, 40), function(m, root)
	block(m, "AlleyFloor", Vector3.new(14, 0.4, 40), Vector3.new(0, 0, 0), Color3.fromRGB(225, 199, 181), Enum.Material.Concrete)
	wallPanel(m, "WestWall", Vector3.new(2, 16, 40), Vector3.new(-18, 8, 0))
	wallPanel(m, "EastWall", Vector3.new(2, 16, 40), Vector3.new(18, 8, 0))
	local orangeBlock = block(m, "OrangeBlock", Vector3.new(7, 10, 7), Vector3.new(-5, 5, -5), accent, Enum.Material.SmoothPlastic)
	gridTexture(orangeBlock, 6, Color3.fromRGB(255, 204, 157))
	inkEdges(m, orangeBlock)
	block(m, "LowDetour", Vector3.new(4, 3, 10), Vector3.new(5, 1.5, 8), roof, Enum.Material.Metal)
	connector(m, "North", Vector3.new(0, 0, -20), -Vector3.zAxis)
	connector(m, "South", Vector3.new(0, 0, 20), Vector3.zAxis)
end)

prefab("Ramp", Vector3.new(40, 1, 40), function(m, root)
	local ramp = Instance.new("WedgePart")
	ramp.Name, ramp.Size, ramp.Position = "Ramp", Vector3.new(12, 10, 30), Vector3.new(0, 5, 0)
	ramp.Anchored, ramp.Color, ramp.Material, ramp.Parent = true, accent, Enum.Material.Metal, m
	block(m, "Landing", Vector3.new(18, 1, 10), Vector3.new(0, 10, -13), roof, Enum.Material.Metal)
	connector(m, "South", Vector3.new(0, 0, 20), Vector3.zAxis)
	connector(m, "North", Vector3.new(0, 10, -20), -Vector3.zAxis)
end)

pool:SetAttribute("PrefabCount", #pool:GetChildren())

pool:SetAttribute("Ready", true)

