////////////////////////////////////////////////////////////
//                                                        //
// Radicalicious' auto-decorate technology of the future. //
//                                                        //
// This script is prepared for OLDC 2021 use only. It is  //
// not complete and therefore non-reusable. Check my MB   //
// submissions if you'd like a full version, if I ever    //
// get around to finishing it, that is..                  //
//                                                        //
// Credits to amperbee and Golden for helping with the    //
// table nonsense for getting vertex positions.           //
//                                                        //
////////////////////////////////////////////////////////////

local function autodec(sector, mobjtype, ceiling, grid, spread, rarity)
	local sectorVertexes = {}
	local minX = 65535*FRACUNIT
	local minY = 65535*FRACUNIT
	local maxX = -65535*FRACUNIT
	local maxY = -65535*FRACUNIT
	
	for i = 0,#sector.lines-1
		table.insert(sectorVertexes, sector.lines[i].v1)
		table.insert(sectorVertexes, sector.lines[i].v2)
	end
			
	for i = 1,#sectorVertexes
		local vrtx = sectorVertexes[i]
		if minX > vrtx.x
			minX = vrtx.x
		end
		if minY > vrtx.y
			minY = vrtx.y
		end
		if maxX < vrtx.x
			maxX = vrtx.x
		end
		if maxY < vrtx.y
			maxY = vrtx.y
		end
	end
			
	local objSpawnX = minX
	local objSpawnY = minY
			
	while (objSpawnY < maxY)
		while (objSpawnX < maxX)
			local deco = P_SpawnMobj(objSpawnX, objSpawnY, 0, mobjtype)
			deco.scale = P_RandomRange(FRACUNIT-(FRACUNIT/3), FRACUNIT+(FRACUNIT/3))
			local offsetX = P_RandomRange(-spread, spread)
			local offsetY = P_RandomRange(-spread, spread)
			P_TeleportMove(deco, (deco.x + offsetX*FRACUNIT), (deco.y + offsetY*FRACUNIT), deco.subsector.sector.floorheight)
			P_TeleportMove(deco, deco.x, deco.y, deco.floorz) // slope hack
			if (ceiling == true)
				deco.flags2 = $ | MF2_OBJECTFLIP
				deco.eflags = $ | MFE_VERTICALFLIP
				P_TeleportMove(deco, deco.x, deco.y, deco.subsector.sector.ceilingheight-deco.height)
				P_TeleportMove(deco, deco.x, deco.y, deco.ceilingz-deco.height) // slope hack the second
			end
			if ((deco.subsector.sector != sector) or (P_RandomKey(rarity+1) > 0))
				P_RemoveMobj(deco)
			end
					
			objSpawnX = $ + grid*FRACUNIT
		end
		objSpawnX = minX
		objSpawnY = $ + grid*FRACUNIT
	end
end

addHook("MapLoad", function(mapnum)
	if not (mapheaderinfo[gamemap].autodec)
		return
	end

	for sector in sectors.iterate
		if sector.floorpic == "CAVEWL7"
			autodec(sector, MT_STALAGMITE2, false, 320, 32, 4)
			autodec(sector, MT_STALAGMITE3, false, 512, 16, 4)
		elseif sector.floorpic == "CAVEWLB"
			autodec(sector, MT_STALAGMITE4, false, 320, 32, 4)
			autodec(sector, MT_STALAGMITE5, false, 512, 16, 4)
		elseif sector.floorpic == "MMBB"
			autodec(sector, MT_STALAGMITE4, false, 96, 32, 4)
			autodec(sector, MT_STALAGMITE5, false, 192, 16, 4)
		elseif sector.floorpic == "MMBA"
			autodec(sector, MT_STALAGMITE2, false, 96, 32, 4)
			autodec(sector, MT_STALAGMITE3, false, 192, 16, 4)
		elseif sector.floorpic == "MMB9"
			autodec(sector, MT_STALAGMITE0, false, 96, 32, 4)
			autodec(sector, MT_STALAGMITE1, false, 192, 16, 4)
		elseif sector.floorpic == "RCZWLL4"
			autodec(sector, MT_HHZSTALAGMITE_TALL, false, 96, 32, 16)
			autodec(sector, MT_HHZSTALAGMITE_SHORT, false, 192, 16, 16)
		elseif sector.floorpic == "ERZROCK2"
			autodec(sector, MT_HHZSTALAGMITE_TALL, false, 96, 32, 16)
			autodec(sector, MT_HHZSTALAGMITE_SHORT, false, 192, 16, 16)
		end
		
		if sector.ceilingpic == "CAVEWL7"
			autodec(sector, MT_STALAGMITE2, true, 320, 32, 3)
			autodec(sector, MT_STALAGMITE3, true, 512, 16, 3)
		elseif sector.ceilingpic == "CAVEWLB"
			autodec(sector, MT_STALAGMITE4, true, 320, 32, 3)
			autodec(sector, MT_STALAGMITE5, true, 512, 16, 3)
		elseif sector.ceilingpic == "RVZWALL6"
			autodec(sector, MT_STALAGMITE4, true, 96, 32, 3)
			autodec(sector, MT_STALAGMITE5, true, 192, 16, 3)
		elseif sector.ceilingpic == "RCZWLL4"
			autodec(sector, MT_HHZSTALAGMITE_TALL, true, 96, 32, 16)
			autodec(sector, MT_HHZSTALAGMITE_SHORT, true, 192, 16, 16)
		elseif sector.ceilingpic == "ERZROCK2"
			autodec(sector, MT_HHZSTALAGMITE_TALL, true, 96, 32, 16)
			autodec(sector, MT_HHZSTALAGMITE_SHORT, true, 192, 16, 16)
		end
	end
end)