fprintf(stderr, "stick at row %d col %d\n", pos[0], pos[1]);
stick(cur_piece, next_pos);
cur_piece = -1;
+ return 0;
}
} else {
cur_piece = rand() % NUM_PIECES;
+ prev_rot = cur_rot = 0;
fprintf(stderr, "spawn: %d\n", cur_piece);
- pos[0] = next_pos[0] = piece_spawnpos[cur_piece][0];
+ pos[0] = piece_spawnpos[cur_piece][0];
+ next_pos[0] = pos[0] + 1;
pos[1] = next_pos[1] = PF_COLS / 2 + piece_spawnpos[cur_piece][1];
}
return tick_interval - dt;
}
+
+#define C0 0x9b
+#define SS3 0x8f
+
+static void runesc(int csi, char *buf)
+{
+ if(csi != C0) return;
+
+ if(buf[1] == 0) {
+ switch(buf[0]) {
+ case 'A':
+ game_input('w'); /* up */
+ break;
+ case 'B':
+ game_input('s'); /* down */
+ break;
+ case 'C':
+ game_input('d'); /* right */
+ break;
+ case 'D':
+ game_input('a'); /* left */
+ break;
+ default:
+ break;
+ }
+ }
+}
+
void game_input(int c)
{
+ static int esc, csi;
+ static int esctop;
+ static char escbuf[64];
+
+ if(esc) {
+ esc = 0;
+ if(c == 27) {
+ quit = 1;
+ return;
+ }
+
+ switch(c) {
+ case '[':
+ csi = C0;
+ return;
+ case 'O':
+ csi = SS3;
+ return;
+ default:
+ break;
+ }
+ }
+
+ if(csi) {
+ if(c < 0x20 || c >= 0x80) {
+ csi = 0;
+ esctop = 0;
+ }
+
+ escbuf[esctop++] = c;
+
+ if(c >= 0x40) {
+ int prevcsi = csi;
+ escbuf[esctop] = 0;
+ csi = 0;
+ esctop = 0;
+ runesc(prevcsi, escbuf);
+ }
+ return;
+ }
+
switch(c) {
case 27:
+ esc = 1;
+ break;
+
+ case C0:
+ esc = 1;
+ csi = C0;
+ break;
+
+ case 'q':
quit = 1;
break;
break;
default:
+ fprintf(stderr, "unhandled input: %x\n", c);
break;
}
}
scr[y * SCR_COLS + x] = piece + FIRST_PIECE_TILE;
}
+
+ if(use_bell) {
+ putchar('\a');
+ fflush(stdout);
+ }
}
static void draw_piece(int piece, const int *pos, int rot, int mode)
int y = PF_YOFFS + pos[0] + BLKY(*p);
p++;
+ if(y < 0) continue;
+
ansi_setcursor(y, x * 2);
wrtile(tile);
}
int quit;
long tick_interval;
+int use_bell;
int init_game(void);
void cleanup_game(void);
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
int init(void);
void cleanup(void);
+int parse_args(int argc, char **argv);
+void print_usage(const char *argv0);
long get_msec(void);
static const char *termfile = "/dev/tty";
long msec, next;
struct timeval tv;
- if(argc > 1) {
- termfile = argv[1];
+ if(parse_args(argc, argv) == -1) {
+ return 1;
}
if(init() == -1) {
dup(fd);
umask(002);
- open("ansitris.log", O_WRONLY | O_CREAT, 0664);
+ open("ansitris.log", O_WRONLY | O_CREAT | O_TRUNC, 0664);
if(init_game() == -1) {
tcsetattr(0, TCSAFLUSH, &saved_term);
}
+int parse_args(int argc, char **argv)
+{
+ int i;
+
+ for(i=1; i<argc; i++) {
+ if(argv[i][0] == '-') {
+ if(argv[i][2] == 0) {
+ switch(argv[i][1]) {
+ case 't':
+ termfile = argv[++i];
+ break;
+
+ case 'b':
+ use_bell = 1;
+ break;
+
+ case 'h':
+ print_usage(argv[0]);
+ exit(0);
+
+ default:
+ fprintf(stderr, "invalid option: %s\n", argv[i]);
+ print_usage(argv[0]);
+ return -1;
+ }
+ } else {
+ fprintf(stderr, "invalid option: %s\n", argv[i]);
+ print_usage(argv[0]);
+ return -1;
+ }
+ } else {
+ fprintf(stderr, "unexpected argument: %s\n", argv[i]);
+ print_usage(argv[0]);
+ return -1;
+ }
+ }
+ return 0;
+}
+
+void print_usage(const char *argv0)
+{
+ printf("Usage: %s [options]\n", argv0);
+ printf("Options:\n");
+ printf(" -t <dev>: terminal device (default: /dev/tty)\n");
+ printf(" -b: use bell for sound ques (default: off)\n");
+ printf(" -h: print usage information and exit\n");
+}
+
long get_msec(void)
{
struct timeval tv;
};
static int piece_spawnpos[NUM_PIECES][2] = {
- {-1, -2}, {-1, -3}, {-1, -2}, {-1, -2}, {-1, -2}, {-1, -2}, {-1, -2}
+ {-2, -2}, {-1, -2}, {-1, -2}, {-1, -2}, {-1, -2}, {-1, -2}, {-1, -2}
};