local function j(j)local Z=random:NextInteger(1,100)local h if Z>92 then h=torch:Clone()elseif Z>80 then h=gravityFuel:Clone()elseif Z>10 then h=repairKit:Clone()elseif Z>5 then h=smallCargo:Clone()elseif Z>3 then h=largeCargo:Clone()else h=broom:Clone()end h.Position=j h.Parent=workspace.Tools h:SetNetworkOwner(nil)end return function()local Z=RoomManager.getRooms()for Z,h in pairs(Z)do local n=h:getRegions()local W=TOTAL_TOOLS_PER_ROOM/#n for Z,h in pairs(n)do for Z=1,W,1 do local n=getRandomClearLocationInRegionAsync(h,AREA_CLEAR_SIZE)j(n)end end end end
--[[
	Randomly picks a tool type depending on weight, and spawns it in specified position.
]]
local function createRandomToolAtPosition(position)
	local randomPick = random:NextInteger(1, 100)

	local tool
	if randomPick > 92 then
		tool = torch:Clone()
	elseif randomPick > 80 then
		tool = gravityFuel:Clone()
	elseif randomPick > 10 then
		tool = repairKit:Clone()
	elseif randomPick > 5 then
		tool = smallCargo:Clone()
	elseif randomPick > 3 then
		tool = largeCargo:Clone()
	else
		tool = broom:Clone()
	end

	tool.Position = position

	tool.Parent = workspace.Tools

	tool:SetNetworkOwner(nil)
end

return function()
	local rooms = RoomManager.getRooms()

	for _, room in pairs(rooms) do
		local regions = room:getRegions()
		local toolsToSpawnPerRegion = TOTAL_TOOLS_PER_ROOM / #regions

		for _, region in pairs(regions) do
			for i = 1, toolsToSpawnPerRegion do
				local position = getRandomClearLocationInRegionAsync(region, AREA_CLEAR_SIZE)

				createRandomToolAtPosition(position)
			end
		end
	end
end
