MAP_SHADERS = {
  { name = 'Default', frag = '/shaders/default.frag' },
  { name = 'Bloom', frag = '/shaders/bloom.frag'},
  { name = 'Sepia', frag ='/shaders/sepia.frag' },
  { name = 'Grayscale', frag ='/shaders/grayscale.frag' },
  { name = 'Pulse', frag = '/shaders/pulse.frag' },
  { name = 'Old Tv', frag = '/shaders/oldtv.frag' },
  { name = 'Fog', frag = '/shaders/fog.frag', tex1 = '/shaders/clouds.png' },
  { name = 'Fog1', frag = '/shaders/fog1.frag', tex1 = '/shaders/clouds1.png' },
  { name = 'Fog2', frag = '/shaders/fog2.frag', tex1 = '/shaders/clouds2.png' },
  { name = 'Party', frag = '/shaders/party.frag' },
  { name = 'Radial Blur', frag ='/shaders/radialblur.frag' },
  { name = 'Zomg', frag ='/shaders/zomg.frag' },
  { name = 'Heat', frag ='/shaders/heat.frag' },
  { name = 'Heat1', frag ='/shaders/heat1.frag' },
  { name = 'Noise', frag ='/shaders/noise.frag' },
}

local areas = {
{from = {x = 1001, y = 1057, z = 13}, to = {x = 1012, y = 1066, z = 13}, name = 'Party'},
{from = {x = 1114, y = 718, z = 4}, to = {x = 1141, y = 750, z = 4}, name = 'Heat'},
{from = {x = 1126, y = 713, z = 5}, to = {x = 1147, y = 735, z = 5}, name = 'Heat'},
{from = {x = 1133, y = 706, z = 6}, to = {x = 1166, y = 741, z = 6}, name = 'Heat'},
{from = {x = 1086, y = 709, z = 6}, to = {x = 1134, y = 748, z = 6}, name = 'Heat1'},
{from = {x = 1191, y = 1039, z = 7}, to = {x = 1244, y = 1079, z = 7}, name = 'Fog1'},
}

function isInRange(position, fromPosition, toPosition)
    return (position.x >= fromPosition.x and position.y >= fromPosition.y and position.z >= fromPosition.z and position.x <= toPosition.x and position.y <= toPosition.y and position.z <= toPosition.z)
end

function init()
   if not g_graphics.canUseShaders() then return end
   for _i,opts in pairs(MAP_SHADERS) do
         local shader = g_shaders.createFragmentShader(opts.name, opts.frag)

         if opts.tex1 then
           shader:addMultiTexture(opts.tex1)
         end
         if opts.tex2 then
           shader:addMultiTexture(opts.tex2)
         end
   end

   connect(LocalPlayer, {
         onPositionChange = updatePosition
   })
 
   local map = modules.game_interface.getMapPanel()
   map:setMapShader(g_shaders.getShader('Default'))
end

function terminate()

end

function updatePosition()
  local player = g_game.getLocalPlayer()
  if not player then return end
  local pos = player:getPosition()
  if not pos then return end
 
  local name = 'Default'
  local map = modules.game_interface.getMapPanel()
 
  if hasSqmUp(pos) then
         map:setMapShader(g_shaders.getShader(name))
         return true
  end  
 
  for _, TABLE in ipairs(areas) do
          if isInRange(pos, TABLE.from, TABLE.to) then
                 name = TABLE.name
          end
  end
  map:setMapShader(g_shaders.getShader(name))
end

function hasSqmUp(posi)
local pos = {x = posi.x, y = posi.y, z = posi.z-1}
local thing = g_map.getTile(pos)
   return thing and true or false
end