
local function buttonhook(tile, buttonid, statevar)
  hook(tile).onStand = function (x, y, l, player)
    if world.state[buttonid] then return end
    world.state[statevar] = not world.state[statevar]
    world.state[buttonid] = player.id
  end
end
local function buttoncheck(tile, buttonid, statevar)
  local b = map:find(tile)[1]
  local s = world.state[buttonid]
  if s and not world.players[s].actor:intersectsField(b.x, b.y) then
    world.state[buttonid] = nil
    world.state[statevar] = not world.state[statevar]
    world:updateState()
  end
end

buttonhook('floor_buttons/2', 'level5_blue_standing', 'button_blue')

hook'stairs_cave/4'.onActivate = function (l, x, y, player)
  player:warp()
end

hook'stairs_cave/3'.onActivate = hook'stairs_cave/4'.onActivate

hook'specials/7'.onIntersect = function (l, x, y, player)
  player:setMessage([[Náhle cítiš, že Prastaré Zlo
už je nablízku. Ale iba spolu
sa mu môžete postaviť.]])
end

function map:updateState()
  map:setField(world.state.level5_blue_standing and 'floor_buttons/7' or 'floor_buttons/2', 2, map:find('floor_buttons/2')[1])
  map:setField(world.state.mam7 and 'doors/4' or 'doors/0', map:find('doors/4')[1])
  map:setField(world.state.mam7 and 'doors/0' or 'doors/4', map:find('doors/0')[1])
end

map:updateState()

local function spawnuj_pavucika(x,y)
  map:addEnemy(x, y, { size = 0.8, pursuitSpeed = 4, startPursuitDistance = 50, stopPursuitDistance = 50, health = 1, waitTimeMin = 100, waitTimeMax = 100, speed = 0.1 }, { base = 'spider_eyes/', frames = 2, delay = 3 })
end

local oldupdate = map.update
function map:update()
  oldupdate(self)
  buttoncheck('floor_buttons/2', 'level4_blue_standing', 'button_blue')
  local tile7 = map.data.tiles[map:parseTile'specials/7']
  local mam7 = 0
  for i,v in ipairs(world.players) do if v.map == map then
    local havemsg = false
    for _,pos in ipairs(v.actor:intersectedFields()) do
      if map.cache[2][pos[2]][pos[1]] == tile7 then havemsg = true; break end end
    if havemsg then mam7 = mam7 + 1 end
    if not havemsg then v:setMessage(nil) end
  end end
  if mam7 == 2 then
    world.state.mam7 = true
    world:updateState()
  end
  local banan
  for i,v in ipairs(map) do if v.isEnemy and v.textureInfo.base == 'banana/' then banan = v end end
  if not banan and not world.state.deadbanan then world.frozen = 100 end
  if not banan then
    world.state.deadbanan = true
    world.players[1]:setMessage[[Hurá!!!

Úspešne ste porazili Prastaré Zlo a
navždy potlačili hrozbu, ktorú
predstavovalo. Vzácne banánové mlieko
z obrieho banánu bude poskytovať
celému šíremu kraju výživu ešte mnoho
a mnoho rokov.

Archeologické nálezy z ruiny Yarivan
a konštruktérske technológie
použité jej staviteľmi sa ukážu ako
nevyčísliteľne cenné pri pochopení
prastarých civilizácii.

A vy budete známi ako odvážni
hrdinovia po celom kraji!]]
    world.players[2]:setMessage[[GRATULUJEME!

Túto hru pre vás pripravili:

Posledná Konštitúcia

Tomáš Belan - Architekt
Števo Benčík - Remeselník
Viktor Seč - Tovariš]]
    world.players[1].actor.ghost = true
    world.players[2].actor.ghost = true
  end
  if not banan then return end
  if banan and banan.pursuing and math.random(50) == 1 then
    local x = math.floor(banan.ix) + math.random(15) - 8
    local y = math.floor(banan.iy) + math.random(15) - 8
    if map.cache[1][y] and map.cache[1][y][x] and banan:canAccessField(x, y) then
      spawnuj_pavucika(x, y)
    end
  end
end

local d = map:find('doors/0')[1]; map.animTimes[d.i][d.y][d.x] = -100
map.doorcolor[d.x] = { 1, 1, 1 }

local d = map:find('doors/4')[1]; map.animTimes[d.i][d.y][d.x] = -100
map.doorcolor[d.x] = { 1, 1, 1 }

local function spawnuj_banan(x,y)
 map:addEnemy(x, y, { size = 0.8, pursuitSpeed = 3.5, startPursuitDistance = 7, stopPursuitDistance = 50, health = 80, waitTimeMin = 10, waitTimeMax = 15, speed = 0.5 }, { base = 'banana/', frames = 5, delay = 2 })
end

  spawnuj_banan(49, 53)
