local prefix = "otrx"

local otrx_x = CV_RegisterVar({
  name = prefix .. "_x",
    defaultvalue = "0.0",
  possiblevalue = {MIN = 0, MAX = FRACUNIT},
  flags = CV_FLOAT
})

local otrx_y = CV_RegisterVar({
  name = prefix .. "_y",
    defaultvalue = "0.0",
  possiblevalue = {MIN = 0, MAX = FRACUNIT},
  flags = CV_FLOAT
})

rawset(_G, "OR_GetSplitscreenCoords", function(cam)
  local screen_width, screen_height = 320, 200
  local hori_center, vert_center = screen_width/2, screen_height/2
    
  if (splitscreen == 1)
    vert_center, screen_height = $/2, $/2
    if (cam.pnum == 2)
      vert_center = $ * 3
    end
  elseif (splitscreen >= 2)
    vert_center, screen_height = $/2, $/2
    hori_center, screen_width = $/2, $/2
    if (cam.pnum == 2)
      hori_center = $ * 3
    elseif (cam.pnum == 3)
      vert_center = $ * 3
    elseif (cam.pnum == 4)
      hori_center, vert_center = $ * 3, $ * 3
    end
  end
  return screen_width, screen_height, hori_center, vert_center
end)

local OR_GetSplitscreenCoords = OR_GetSplitscreenCoords

rawset(_G, "OR_GetOTricksIconCoords", function(cam)
  local screen_width, screen_height, hori_center, vert_center = OR_GetSplitscreenCoords(cam)
  
  local icon_x = hori_center - 47
  local icon_y = vert_center + (screen_height/2) - 19
    
  if (splitscreen == 1)
    icon_y = vert_center + (screen_height/2) - 14
  elseif (splitscreen >= 2)
    icon_y = vert_center + (screen_height/2) - 8
    if (hori_center < screen_width)
      icon_x = hori_center - (screen_width/2) + 8
    else
      icon_x = hori_center + (screen_width/2) - 35 -- i am ignoring your fucked-up lap counts. if this bothers you, write a fix for me.
    end
  end
  
  -- custom icon position processing
  if (otrx_x.value > 0)
    icon_x = (screen_width * otrx_x.value) / FRACUNIT
    if (hori_center > screen_width)
      icon_x = $ + screen_width
    end
  end
  
  if (otrx_y.value > 0)
    icon_y = (screen_height * otrx_y.value) / FRACUNIT
    if (vert_center > screen_height)
      icon_y = $ + screen_height
    end
  end
  return icon_x, icon_y
end)