-- Bundled by luabundle {"version":"1.6.0"}
local __bundle_require, __bundle_loaded, __bundle_register, __bundle_modules = (function(superRequire)
	local loadingPlaceholder = {[{}] = true}

	local register
	local modules = {}

	local require
	local loaded = {}

	register = function(name, body)
		if not modules[name] then
			modules[name] = body
		end
	end

	require = function(name)
		local loadedModule = loaded[name]

		if loadedModule then
			if loadedModule == loadingPlaceholder then
				return nil
			end
		else
			if not modules[name] then
				if not superRequire then
					local identifier = type(name) == 'string' and '\"' .. name .. '\"' or tostring(name)
					error('Tried to require ' .. identifier .. ', but no such module has been registered')
				else
					return superRequire(name)
				end
			end

			loaded[name] = loadingPlaceholder
			loadedModule = modules[name](require, loaded, register, modules)
			loaded[name] = loadedModule
		end

		return loadedModule
	end

	return require, loaded, register, modules
end)(nil)
__bundle_register("__root", function(require, _LOADED, __bundle_register, __bundle_modules)
require("core/DoomInPlayCounter")
end)
__bundle_register("core/DoomInPlayCounter", function(require, _LOADED, __bundle_register, __bundle_modules)
-- common parameters
local castParameters        = {}
castParameters.direction    = { 0, 1, 0 }
castParameters.type         = 3
castParameters.max_distance = 0

local zone
local doomURL    = "https://i.imgur.com/EoL7yaZ.png"
local IGNORE_TAG = "DoomCounter_ignore"

-- playermats 1 to 4
local originAndSize = {
  { origin = { -55, 1.6, 16.5 }, size = { 12, 1, 25 } },
  { origin = { -55, 1.6, -16.5 }, size = { 12, 1, 25 } },
  { origin = { -25, 1.6, 27 }, size = { 25, 1, 12 } },
  { origin = { -25, 1.6, -27 }, size = { 25, 1, 12 } }
}

-- create button, context menu and start loop
function onLoad()
  self.createButton({
    label = tostring(0),
    click_function = "none",
    function_owner = self,
    position = { 0, 0.06, 0 },
    height = 0,
    width = 0,
    scale = { 1.5, 1.5, 1.5 },
    font_size = 600,
    font_color = { 1, 1, 1, 100 },
    color = { 0, 0, 0, 0 }
  })

  zone = getObjectFromGUID("a2f932")
  loopID = Wait.time(countDoom, 2, -1)
end

-- main function
function countDoom()
  local doom = 0
  for i = 1, 5 do doom = doom + search(i) end
  self.editButton({ index = 0, label = tostring(doom) })
end

-- searches playermats (num = 1-4) or the scripting zone (num = 5)
function search(num)
  local val = 0
  if num == 5 then
    for _, obj in ipairs(zone.getObjects()) do
      val = val + isDoom(obj)
    end
  else
    castParameters.origin = originAndSize[num].origin
    castParameters.size = originAndSize[num].size

    for _, obj in ipairs(Physics.cast(castParameters)) do
      val = val + isDoom(obj.hit_object)
    end
  end
  return val
end

-- checks an object for the doom image and gets quantity (for stacks)
function isDoom(obj)
  if (obj.is_face_down and obj.getCustomObject().image_bottom == doomURL) or
      (obj.name == "Custom_Token" and obj.getCustomObject().image == doomURL) then
    if not obj.hasTag(IGNORE_TAG) then
      return math.abs(obj.getQuantity())
    end
  end
  return 0
end

-- removes doom from playermats / playarea
function removeDoom(options)
  local trashCan = getObjectFromGUID("70b9f6")
  local count = 0
  if options.Playermats then
    for i = 1, 4 do
      castParameters.origin = originAndSize[i].origin
      castParameters.size = originAndSize[i].size

      for _, obj in ipairs(Physics.cast(castParameters)) do
        local obj = obj.hit_object
        local amount = isDoom(obj)
        if amount > 0 then
          trashCan.putObject(obj)
          count = count + amount
        end
      end
    end
    broadcastToAll(count .. " doom removed from Playermats.", "White")
  end

  local count = 0
  if options.Playarea then
    for _, obj in ipairs(zone.getObjects()) do
      local amount = isDoom(obj)
      if amount > 0 then
        trashCan.putObject(obj)
        count = count + amount
      end
    end
    broadcastToAll(count .. " doom removed from Playarea.", "White")
  end
end
end)
return __bundle_require("__root")