// ****************************************************************************
//
// Logic 3: Generate next piece
//
// ****************************************************************************

// piece types:
// xx   x     x    x   xx    xx   x
// xx   x     x    xx   xx  xx    x
//      xx   xx    x              x
//                                x
// 1    2    3    4    5    6     7


#include "defines.txt"
#define cur_x v251
#define cur_y v252
#define flagno v253
#define tmp_x v254
#define tmp_y v255

random(1,7,piece_type);
random(1,6,piece_colour);
piece_rotation = 0;

if (piece_type == 1) { random(0,0,piece_rotation); }
if (piece_type == 2) { random(0,3,piece_rotation); }
if (piece_type == 3) { random(0,3,piece_rotation); }
if (piece_type == 4) { random(0,3,piece_rotation); }
if (piece_type == 5) { random(0,1,piece_rotation); }
if (piece_type == 6) { random(0,1,piece_rotation); }
if (piece_type == 7) { random(0,1,piece_rotation); }

set(rotate_clockwise);
call(7);  // rotate piece



cur_x = 0;
cur_y = 0;
Next:
flagno = cur_y;  flagno *= 4;  flagno += cur_x;  flagno += 100;
// flagno = cur_y*4 + cur*x + 100
if (issetv(flagno)) {
  v249 = piece_colour;  // cel number
}
else {
  v249 = 0;
}
v248 = 0;  // view & loop number
v250 = 4;  // priority & margin
tmp_x = cur_x;  tmp_x *= 5;  tmp_x += 125;
tmp_y = cur_y;  tmp_y *= 8;  tmp_y += 31;
add.to.pic.v(v248,v248,v249,tmp_x,tmp_y,v250,v250);
cur_x++;
if (cur_x < 4) {
  goto(Next);
}
else {
  cur_y++;
  cur_x = 0;
  if (cur_y < 4) {
    goto(Next);
  }
}

v253 = piece_type;
v254 = piece_colour;
v255 = piece_rotation;
piece_type = next_piece;
piece_colour = next_colour;
piece_rotation = next_rotation;
next_piece = v253;
next_colour = v254;
next_rotation = v255;

set(rotate_clockwise);
call(7);  // rotate piece
reset(rotate_clockwise);
call(7);  // rotate piece





x = 14;
y = 1;
old_x = x;
old_y = y;

call(5);  // draw piece

cycles_till_next_fall = 4;


return();