#include "defines.txt"




#define cur_x v253
#define cur_y v252
#define flagno v251
#define tmp_x v250
#define tmp_y v249
#define varno v248

if (new_room) {
  load.pic(room_no);
  draw.pic(room_no);
  discard.pic(room_no);
  set.key(106,0,left_key);  // 'j'
  set.key(107,0,drop_key);  // 'k'
  set.key(108,0,right_key);  // 'l'
  set.key(105,0,rotate_c_key);  // 'i'
  set.key(111,0,rotate_ac_key);  // 'o'
  set.key(13,0,pause_key);  // enter
  set.key(0,60,new_game_key);  // F2
  set.key(113,0,quit_key);  // 'q'
  load.logics(3); // new piece
  load.logics(4); // erase piece
  load.logics(5); // draw piece
  load.logics(6); // check block move ook
  load.logics(7); // rotate piece
  load.logics(8); // check for filled rows
  load.logics(9); // update score
  load.logics(10); // redraw playing area
  load.logics(11); // clear playing area
  load.logics(12); // new game
  load.view(0);
  load.view(4);
  trace.info(95,13,6);
  set(trace_enabled);
  show.pic();
  display(2,29,"Next piece");
  display(11,31,"Score");
  call(12); // new game
  program.control();
}

if (controller(pause_key)) {
  toggle(game_paused);
  if (game_paused) {
    call(11);  // clear playing area
    display(9,14,"Game paused.");
    display(10,14,"Press ENTER");
    display(11,14,"to continue.");
  }
  else {
    call(10);  // redraw playing area
    call(5);  // draw piece
  }
}

if (controller(new_game_key)) {
  call(11);  // clear playing area
  display(9,14,"Are you sure");
  display(10,14,"you want to ");
  display(11,14,"start a new ");
  display(12,14,"game? (Y/N) ");
WaitForKey1:
  if (!have.key()) {
    goto(WaitForKey1);
  }
  if (key_pressed == 121) {  // Y pressed
    call(12);  // new game
  }
  else {
    call(10);  // redraw playing area
    call(5);  // draw piece
  }
}


if (controller(quit_key)) {
  call(11);  // clear playing area
  display(9,14,"Are you sure");
  display(10,14,"you want to ");
  display(11,14,"quit? (Y/N) ");
WaitForKey2:
  if (!have.key()) {
    goto(WaitForKey2);
  }
  if (key_pressed == 121) { // Y pressed
    quit(1);
  }
  else {
    call(10);  // redraw playing area
    call(5);  // draw piece
  }
}

if (game_over) {
  call(11);  // clear playing area
  display(9,14," GAME OVER ");
  display(11,14," Press Q to ");
  display(12,14,"  quit, or  ");
  display(13,14,"  ENTER to  ");
  display(14,14," play again.");
WaitForKey3:
  if (!have.key()) {
    goto(WaitForKey3);
  }
  if (key_pressed == 113) {  // Q pressed
    quit(1);
  }
  else {
    if (key_pressed == 13) {  // ENTER pressed
      call(12);  // new game
    }
    else {
      key_pressed = 0;
      goto(WaitForKey3);
    }
  }
}

if (!game_paused) {

  if (controller(right_key)) {
    check_x = x;  check_x++;
    call(6);  // check block move ok
    if (block_move_ok) {
      x++;
      call(4);  // erase piece
      call(5);  // draw piece
      old_x = x;
      old_y = y;
    }
  }
  
  if (controller(left_key) && x > 0) {
    check_x = x;  check_x--;
    call(6);  // check block move ok
    if (block_move_ok) {
      x--;
      call(4);  // erase piece
      call(5);  // draw piece
      old_x = x;
      old_y = y;
    }
  }
  
  if (controller(drop_key)) {
    set(drop_pressed);
  }
  
  if (controller(rotate_c_key)) {
    old_x = x;
    old_y = y;
    set(rotate_clockwise);
    call(7);  // rotate piece clockwise
    check_x = x;
    check_y = y;
    call(6);  // check block move ok
    reset(rotate_clockwise);
    call(7);  // rotate piece anti-clockwise
    if (block_move_ok) {
      old_x = x;
      old_y = y;
      call(4);  // erase piece
      set(rotate_clockwise);
      call(7);  // rotate piece clockwise
      call(5);  // draw piece
    }
  }
  
  cycles_till_next_fall--;
  if ((cycles_till_next_fall == 0 || drop_pressed)) {
    cycles_till_next_fall = fall_delay;
  DownOneMore:
    check_x = x;
    check_y = y; check_y++;
    call(6);  // check block move ok
  
    if (!block_move_ok) {
      if (y == 1) {
        set(game_over);
      }
      cur_y = 0;
      cur_x = 0;
    Label2:
      tmp_x = x; tmp_x += cur_x;
      tmp_y = y; tmp_y += cur_y;
      varno = tmp_y;  varno *= 10;  varno += tmp_x;  varno += 9;
      flagno = cur_y; flagno *=4;  flagno += cur_x;  flagno += 100;
      if (issetv(flagno)) {
        *varno = piece_colour;
      }
      cur_x++;
      if (cur_x < 4) {
        goto(Label2);
      }
      else {
        cur_y++;
        if (cur_y < 4) {
          cur_x = 0;
          goto(Label2);
        }
      }
      if ((x != old_x || y != old_y || y == 1)) {
        call(4);  // erase piece
        call(5);  // draw piece
      }
      reset(drop_pressed);
      score_1 = score_1 + 1;
      call(9); // update score 
      call(8);  // check for filled rows
      call(3);  // next piece
    }
    else {
      y++;
      if (drop_pressed) { goto(DownOneMore); }
      else {
        call(4);  // erase piece
        call(5);  // draw piece
        old_x = x;
        old_y = y;
      }
    }
  }
  
  //if ((x != old_x || y != old_y || y == 1)) {
  //  call(4);  // erase piece
  //  call(5);  // draw piece
  //}
  
  old_x = x;
  old_y = y;

}


return();