netris version 0.4
[netris.git] / game.c
diff --git a/game.c b/game.c
index 82c03bfcd56d2e6b95ec9bdc387b83aa47c823f5..26ab6a8f72496f0e20f8a2fff416bc2a44c84da3 100644 (file)
--- a/game.c
+++ b/game.c
@@ -1,6 +1,6 @@
 /*
- * Netris -- A free networked version of Tetris
- * Copyright (C) 1994,1995  Mark Weaver <Mark_Weaver@brown.edu>
+ * Netris -- A free networked version of T*tris
+ * Copyright (C) 1994,1995,1996  Mark H. Weaver <mhw@netris.org>
  * 
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -16,7 +16,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  *
- * $Id: game.c,v 1.37 1995/07/11 08:53:23 mhw Exp $
+ * $Id: game.c,v 1.38 1996/02/09 08:22:11 mhw Exp $
  */
 
 #define NOEXT
 #include <sys/types.h>
 #include <netinet/in.h>
 
-enum { KT_left, KT_rotate, KT_right, KT_drop,
-       KT_down, KT_toggleSpy, KT_pause, KT_faster, KT_numKeys };
+enum { KT_left, KT_rotate, KT_right, KT_drop, KT_down,
+       KT_toggleSpy, KT_pause, KT_faster, KT_redraw, KT_numKeys };
 
 static char *keyNames[KT_numKeys+1] = {
        "Left", "Rotate", "Right", "Drop", "Down", "ToggleSpy", "Pause",
-       "Faster", NULL };
+       "Faster", "Redraw", NULL };
 
 static char *gameNames[GT_len] = { "OnePlayer", "ClassicTwo" };
 
-static char keyTable[KT_numKeys+1] = "jkl mspf";
+static char keyTable[KT_numKeys+1];
 static int dropModeEnable = 0;
 static char *robotProg;
 
+ExtFunc void MapKeys(char *newKeys)
+{
+       int i, k, ch;
+       char used[256];
+       int errs = 0;
+
+       /* XXX assumptions about ASCII encoding here */
+       for (i = k = 0; newKeys[i] && k < KT_numKeys; i++,k++) {
+               if (newKeys[i] == '^' && newKeys[i+1])
+                       keyTable[k] = toupper(newKeys[++i]) - ('A' - 1);
+               else
+                       keyTable[k] = newKeys[i];
+       }
+       memset(used, 0, sizeof(used));
+       for (k = 0; k < KT_numKeys; k++) {
+               ch = (unsigned char) keyTable[k];
+               if (used[ch]) {
+                       if (iscntrl(ch) && ch < ' ')
+                               sprintf(scratch, "Ctrl-%c", ch + ('A' - 1));
+                       else if (isprint(ch))
+                               sprintf(scratch, "\"%c\"", ch);
+                       else
+                               sprintf(scratch, "0x%X", ch);
+                       if (!errs)
+                               fprintf(stderr, "Duplicate key mappings:\n");
+                       errs++;
+                       fprintf(stderr, "  %s mapped to both %s and %s\n",
+                                       scratch, keyNames[used[ch]-1], keyNames[k]);
+               }
+               used[ch] = k + 1;
+       }
+       if (errs)
+               exit(1);
+}
+
 ExtFunc int StartNewPiece(int scr, Shape *shape)
 {
        curShape[scr] = shape;
@@ -131,7 +166,7 @@ ExtFunc void OneGame(int scr, int scr2)
                                        if (!p)
                                                break;
                                keyEvent:
-                                       if (paused && (key != KT_pause))
+                                       if (paused && (key != KT_pause) && (key != KT_redraw))
                                                break;
                                        switch(key) {
                                                case KT_left:
@@ -184,6 +219,11 @@ ExtFunc void OneGame(int scr, int scr2)
                                                        ShowDisplayInfo();
                                                        changed = 1;
                                                        break;
+                                               case KT_redraw:
+                                                       ScheduleFullRedraw();
+                                                       if (paused)
+                                                               RefreshScreen();
+                                                       break;
                                        }
                                        if (dropMode && DropPiece(scr) > 0) {
                                                if (spied)
@@ -321,6 +361,7 @@ ExtFunc int main(int argc, char **argv)
 
        standoutEnable = 1;
        stepDownInterval = DEFAULT_INTERVAL;
+       MapKeys(DEFAULT_KEYS);
        while ((ch = getopt(argc, argv, "hHRs:r:Fk:c:woDSp:i:")) != -1)
                switch (ch) {
                        case 'c':
@@ -355,20 +396,15 @@ ExtFunc int main(int argc, char **argv)
                        case 'S':
                                standoutEnable = 0;
                                break;
+                       case 'k':
+                               MapKeys(optarg);
+                               break;
                        case 'H':
                                DistInfo();
                                exit(0);
                        case 'R':
                                Rules();
                                exit(0);
-                       case 'k':
-                       {
-                               int i;
-
-                               for (i=0;optarg[i] && i < KT_numKeys;i++)
-                                       keyTable[i] = optarg[i];
-                               break;
-                       }
                        case 'h':
                                Usage();
                                exit(0);
@@ -492,3 +528,7 @@ ExtFunc int main(int argc, char **argv)
        return 0;
 }
 
+/*
+ * vi: ts=4 ai
+ * vim: noai si
+ */