var first  = hasMemo("cyan_0") ? getName("cyan_decor") : hasMemo("yellow_0") ? getName("yellow_decor") : getName("magenta_decor");
var second = hasMemo("cyan_1") ? getName("cyan_decor") : hasMemo("yellow_1") ? getName("yellow_decor") : getName("magenta_decor");
var third  = hasMemo("cyan_2") ? getName("cyan_decor") : hasMemo("yellow_2") ? getName("yellow_decor") : getName("magenta_decor");

function unblinkFirst()  { first.ac(1);  }
function unblinkSecond() { second.ac(1); }
function unblinkThird()  { third.ac(1);  }

function blinkFirst()
{
  first.ac(2);
  delay(0.15, unblinkFirst);
}

function blinkSecond()
{
  second.ac(2);
  delay(0.15, unblinkSecond);
}

function blinkThird()
{
  third.ac(2);
  delay(0.15, unblinkThird);
}

var sound = "p4/lamp_0";

function firstBlinker()
{
  blinkFirst();
  delay(0.25, blinkFirst);
  delay(0.75, blinkFirst);
  delay(1.25, blinkFirst);
  delay(3.00, blinkFirst);
}

function secondBlinker()
{
  delay(1.0, blinkSecond);
  delay(2.0, blinkSecond);
  delay(2.25, blinkSecond);
  delay(2.75, blinkSecond);
  delay(3.25, blinkSecond);
}

function thirdBlinker()
{
  delay(0.5, blinkThird);
  delay(1.5, blinkThird);
  delay(1.75, blinkThird);
  delay(2.5, blinkThird);
  delay(3.5, blinkThird);
  delay(3.75, blinkThird);
}

function restartLoop()
{
  if (game.activeMap.name != "yumo_treshold") return;
  firstBlinker();
  secondBlinker();
  thirdBlinker();
  delay(4, restartLoop);
}

function startLoop()
{
  loop('p4/lamp_3', 999, 7, 4);
  restartLoop();
}
function openDoor()
{
  run("insert_ladder");
}

if (hasMemo("lamp_3"))
{
  startLoop();
}
else if (hasMemo("lamp_2"))
{
  setMemo("gaveLamps");
  firstBlinker();
  secondBlinker();
  thirdBlinker();
  sfx("p4/lamp_2");
  setMemo("lamp_3");
  delay(4, startLoop);
  delay(4, openDoor);
}
else if (hasMemo("lamp_1"))
{
  firstBlinker();
  secondBlinker();
  sfx("p4/lamp_1");
}
else
{
  firstBlinker();
  sfx("p4/lamp_0");
}