fixes, optional beeps, accept arrow keys for control
authorJohn Tsiombikas <nuclear@member.fsf.org>
Mon, 11 Feb 2019 07:38:50 +0000 (09:38 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Mon, 11 Feb 2019 07:38:50 +0000 (09:38 +0200)
src/game.c
src/game.h
src/main.c
src/pieces.h

index 0adc772..1ab330b 100644 (file)
@@ -137,11 +137,14 @@ long update(long msec)
                                fprintf(stderr, "stick at row %d col %d\n", pos[0], pos[1]);
                                stick(cur_piece, next_pos);
                                cur_piece = -1;
                                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;
                        }
                } else {
                        cur_piece = rand() % NUM_PIECES;
+                       prev_rot = cur_rot = 0;
                        fprintf(stderr, "spawn: %d\n", cur_piece);
                        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];
                }
 
                        pos[1] = next_pos[1] = PF_COLS / 2 + piece_spawnpos[cur_piece][1];
                }
 
@@ -158,10 +161,88 @@ long update(long msec)
        return tick_interval - dt;
 }
 
        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)
 {
 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:
        switch(c) {
        case 27:
+               esc = 1;
+               break;
+
+       case C0:
+               esc = 1;
+               csi = C0;
+               break;
+
+       case 'q':
                quit = 1;
                break;
 
                quit = 1;
                break;
 
@@ -196,6 +277,7 @@ void game_input(int c)
                break;
 
        default:
                break;
 
        default:
+               fprintf(stderr, "unhandled input: %x\n", c);
                break;
        }
 }
                break;
        }
 }
@@ -228,6 +310,11 @@ static void stick(int piece, const int *pos)
 
                scr[y * SCR_COLS + x] = piece + FIRST_PIECE_TILE;
        }
 
                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)
 }
 
 static void draw_piece(int piece, const int *pos, int rot, int mode)
@@ -241,6 +328,8 @@ static void draw_piece(int piece, const int *pos, int rot, int mode)
                int y = PF_YOFFS + pos[0] + BLKY(*p);
                p++;
 
                int y = PF_YOFFS + pos[0] + BLKY(*p);
                p++;
 
+               if(y < 0) continue;
+
                ansi_setcursor(y, x * 2);
                wrtile(tile);
        }
                ansi_setcursor(y, x * 2);
                wrtile(tile);
        }
index 14a6620..7687d6f 100644 (file)
@@ -3,6 +3,7 @@
 
 int quit;
 long tick_interval;
 
 int quit;
 long tick_interval;
+int use_bell;
 
 int init_game(void);
 void cleanup_game(void);
 
 int init_game(void);
 void cleanup_game(void);
index dc240fd..461f27c 100644 (file)
@@ -1,4 +1,5 @@
 #include <stdio.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 #include <unistd.h>
 #include <string.h>
 #include <errno.h>
 #include <unistd.h>
@@ -11,6 +12,8 @@
 
 int init(void);
 void cleanup(void);
 
 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 get_msec(void);
 
 static const char *termfile = "/dev/tty";
@@ -23,8 +26,8 @@ int main(int argc, char **argv)
        long msec, next;
        struct timeval tv;
 
        long msec, next;
        struct timeval tv;
 
-       if(argc > 1) {
-               termfile = argv[1];
+       if(parse_args(argc, argv) == -1) {
+               return 1;
        }
 
        if(init() == -1) {
        }
 
        if(init() == -1) {
@@ -94,7 +97,7 @@ int init(void)
        dup(fd);
 
        umask(002);
        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) {
 
 
        if(init_game() == -1) {
@@ -110,6 +113,54 @@ void cleanup(void)
        tcsetattr(0, TCSAFLUSH, &saved_term);
 }
 
        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;
 long get_msec(void)
 {
        struct timeval tv;
index e3a5c54..90be0a0 100644 (file)
@@ -60,7 +60,7 @@ static unsigned char pieces[NUM_PIECES][4][4] = {
 };
 
 static int piece_spawnpos[NUM_PIECES][2] = {
 };
 
 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}
 };
 
 
 };