-- 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/PlayArea")
end)
__bundle_register("core/PlayArea", function(require, _LOADED, __bundle_register, __bundle_modules)
---------------------------------------------------------
-- general setup
---------------------------------------------------------

-- set true to enable debug logging
DEBUG = false

-- we use this to turn off collision handling until onLoad() is complete
COLLISION_ENABLED = false

local INVESTIGATOR_COUNTER_GUID = "f182ee"
local clueData = {}
spawnedLocationGUIDs = {}

---------------------------------------------------------
-- general code
---------------------------------------------------------

function onSave() return JSON.encode(spawnedLocationGUIDs) end

function onLoad(save_state)
  -- records locations we have spawned clues for
  spawnedLocationGUIDs = JSON.decode(save_state) or {}

  local TOKEN_DATA = Global.getTable('TOKEN_DATA')
  clueData = {
    thickness    = 0.1,
    stackable    = true,
    type         = 2,
    image        = TOKEN_DATA.clue.image,
    image_bottom = TOKEN_DATA.doom.image
  }

  local dataHelper = getObjectFromGUID('708279')
  LOCATIONS = dataHelper.getTable('LOCATIONS_DATA')

  self.interactable = DEBUG
  Wait.time(function() COLLISION_ENABLED = true end, 1)
end

function log(message)
  if DEBUG then print(message) end
end

---------------------------------------------------------
-- clue spawning
---------------------------------------------------------

-- try the compound key then the name alone as default
function getLocation(object)
  return LOCATIONS[object.getName() .. '_' .. object.getGUID()] or LOCATIONS[object.getName()]
end

-- Return the number of clues to spawn on this location
function getClueCount(object, isFaceDown, playerCount)
  local details = getLocation(object)
  if details == nil then
    error('attempted to get clue for unexpected object: ' .. object.getName())
  end

  log(object.getName() .. ' : ' .. details['type'] .. ' : ' .. details['value'] .. ' : ' .. details['clueSide'])
  if ((isFaceDown and details['clueSide'] == 'back') or (not isFaceDown and details['clueSide'] == 'front')) then
    if details['type'] == 'fixed' then
      return details['value']
    elseif details['type'] == 'perPlayer' then
      return details['value'] * playerCount
    end
    error('unexpected location type: ' .. details['type'])
  end
  return 0
end

function spawnCluesAtLocation(clueCount, object)
  if spawnedLocationGUIDs[object.getGUID()] ~= nil then
    error('tried to spawn clue for already spawned location:' .. object.getName())
  end

  log('spawning clues for ' .. object.getName() .. '_' .. object.getGUID())
  log('player count is ' .. getInvestigatorCount() .. ', clue count is ' .. clueCount)

  -- mark this location as spawned, can't happen again
  spawnedLocationGUIDs[object.getGUID()] = true

  -- spawn clues (starting top right, moving to the next column after 4 clues)
  local pos = object.getPosition()
  for i = 1, clueCount do
    local column = math.floor((i - 1) / 4)
    local row = (i - 1) % 4
    spawnClue({ pos.x - 1 + 0.55 * row, pos.y, pos.z - 1.4 - 0.55 * column })
  end
end

function spawnClue(position)
  local token = spawnObject({
    position = position,
    rotation = { 3.88, -90, 0.24 },
    type     = 'Custom_Tile'
  })

  token.setCustomObject(clueData)
  token.scale { 0.25, 1, 0.25 }
  token.use_snap_points = false
end

function updateLocations(args)
  custom_data_helper_guid = args[1]
  local custom_data_helper = getObjectFromGUID(args[1])

  for k, v in pairs(custom_data_helper.getTable("LOCATIONS_DATA")) do
    LOCATIONS[k] = v
  end
end

function onCollisionEnter(collision_info)
  if not COLLISION_ENABLED then return end

  -- check if we should spawn clues here and do so according to playercount
  local object = collision_info.collision_object
  if getLocation(object) ~= nil and spawnedLocationGUIDs[object.getGUID()] == nil then
    local clueCount = getClueCount(object, object.is_face_down, getInvestigatorCount())
    if clueCount > 0 then spawnCluesAtLocation(clueCount, object) end
  end
end

function getInvestigatorCount()
  local investigatorCounter = getObjectFromGUID('f182ee')
  return investigatorCounter.getVar("val")
end
end)
return __bundle_require("__root")
