2 * Netris -- A free networked version of T*tris
3 * Copyright (C) 1994,1995,1996 Mark H. Weaver <mhw@netris.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * $Id: game.c,v 1.39 1999/05/16 06:56:27 mhw Exp $
27 #include <sys/types.h>
28 #include <netinet/in.h>
30 enum { KT_left, KT_rotate, KT_right, KT_drop, KT_down,
31 KT_toggleSpy, KT_pause, KT_faster, KT_redraw, KT_new, KT_numKeys };
33 static char *keyNames[KT_numKeys+1] = {
34 "Left", "Rotate", "Right", "Drop", "Down", "ToggleSpy", "Pause",
35 "Faster", "Redraw", "New", NULL };
37 static char *gameNames[GT_len] = { "OnePlayer", "ClassicTwo" };
39 static char keyTable[KT_numKeys+1];
40 static int dropModeEnable = 0;
41 static char *robotProg;
43 static int wonLast = 0;
44 int lost = 0, won = 0;
45 enum States gameState = STATE_STARTING;
47 ExtFunc void MapKeys(char *newKeys)
53 /* XXX assumptions about ASCII encoding here */
54 for (i = k = 0; newKeys[i] && k < KT_numKeys; i++,k++) {
55 if (newKeys[i] == '^' && newKeys[i+1])
56 keyTable[k] = toupper(newKeys[++i]) - ('A' - 1);
58 keyTable[k] = newKeys[i];
60 memset(used, 0, sizeof(used));
61 for (k = 0; k < KT_numKeys; k++) {
62 ch = (unsigned char) keyTable[k];
64 if (iscntrl(ch) && ch < ' ')
65 sprintf(scratch, "Ctrl-%c", ch + ('A' - 1));
67 sprintf(scratch, "\"%c\"", ch);
69 sprintf(scratch, "0x%X", ch);
71 fprintf(stderr, "Duplicate key mappings:\n");
73 fprintf(stderr, " %s mapped to both %s and %s\n",
74 scratch, keyNames[used[ch]-1], keyNames[k]);
82 ExtFunc int StartNewPiece(int scr, Shape *shape)
84 curShape[scr] = shape;
85 curY[scr] = boardVisible[scr] + 4;
86 curX[scr] = boardWidth[scr] / 2;
87 while (!ShapeVisible(shape, scr, curY[scr], curX[scr]))
89 if (!ShapeFits(shape, scr, curY[scr], curX[scr]))
91 PlotShape(shape, scr, curY[scr], curX[scr], 1);
95 ExtFunc void OneGame(int scr, int scr2)
98 int linesCleared, changed = 0;
99 int spied = 0, spying = 0, dropMode = 0;
100 int oldPaused = 0, paused = 0, pausedByMe = 0, pausedByThem = 0;
106 myLinesCleared = enemyLinesCleared = 0;
107 speed = stepDownInterval;
114 UpdateOpponentDisplay();
117 SetITimer(speed, speed);
119 RobotCmd(0, "GameType %s\n", gameNames[game]);
120 RobotCmd(0, "BoardSize 0 %d %d\n",
121 boardVisible[scr], boardWidth[scr]);
123 RobotCmd(0, "BoardSize 1 %d %d\n",
124 boardVisible[scr2], boardWidth[scr2]);
125 RobotCmd(0, "Opponent 1 %s %s\n", opponentName, opponentHost);
126 if (opponentFlags & SCF_usingRobot)
127 RobotCmd(0, "OpponentFlag 1 robot\n");
128 if (opponentFlags & SCF_fairRobot)
129 RobotCmd(0, "OpponentFlag 1 fairRobot\n");
131 RobotCmd(0, "TickLength %.3f\n", speed / 1.0e6);
132 RobotCmd(0, "BeginGame\n");
135 while (StartNewPiece(scr, ChooseOption(stdOptions))) {
136 if (robotEnable && !fairRobot)
137 RobotCmd(1, "NewPiece %d\n", ++pieceCount);
142 shapeNum = ShapeToNetNum(curShape[scr]);
143 data[0] = hton2(shapeNum);
144 SendPacket(NP_newPiece, sizeof(data), data);
147 changed = RefreshBoard(scr) || changed;
149 changed = RefreshBoard(scr2) || changed;
155 switch (WaitMyEvent(&event, EM_any)) {
157 if (!MovePiece(scr, -1, 0))
160 SendPacket(NP_down, 0, NULL);
163 p = strchr(keyTable, tolower(event.u.key));
166 RobotCmd(1, "UserKey %d %s\n",
167 (int)(unsigned char)event.u.key,
168 p ? keyNames[key] : "?");
174 if (paused && (key != KT_pause) && (key != KT_redraw))
178 if (MovePiece(scr, 0, -1) && spied)
179 SendPacket(NP_left, 0, NULL);
182 if (MovePiece(scr, 0, 1) && spied)
183 SendPacket(NP_right, 0, NULL);
186 if (RotatePiece(scr) && spied)
187 SendPacket(NP_rotate, 0, NULL);
190 if (MovePiece(scr, -1, 0) && spied)
191 SendPacket(NP_down, 0, NULL);
194 spying = (!spying) && (scr2 >= 0);
197 if (DropPiece(scr) > 0) {
199 SendPacket(NP_drop, 0, NULL);
200 SetITimer(speed, speed);
202 dropMode = dropModeEnable;
205 pausedByMe = !pausedByMe;
206 if (game == GT_classicTwo) {
209 data[0] = hton2(pausedByMe);
210 SendPacket(NP_pause, sizeof(data), data);
212 paused = pausedByMe || pausedByThem;
214 RobotCmd(1, "Pause %d %d\n", pausedByMe,
216 ShowPause(pausedByMe, pausedByThem);
220 if (game != GT_onePlayer)
223 SetITimer(speed, SetITimer(0, 0));
228 ScheduleFullRedraw();
233 if (dropMode && DropPiece(scr) > 0) {
235 SendPacket(NP_drop, 0, NULL);
236 SetITimer(speed, speed);
243 cmd = event.u.robot.data;
244 if ((p = strchr(cmd, ' ')))
247 p = cmd + strlen(cmd);
248 for (key = 0; keyNames[key]; ++key)
249 if (!strcmp(keyNames[key], cmd) &&
250 (fairRobot || (1 == sscanf(p, "%d", &num) &&
253 if (!strcmp(cmd, "Message")) {
260 switch(event.u.net.type) {
266 memcpy(data, event.u.net.data, sizeof(data[0]));
267 column = Random(0, boardWidth[scr]);
268 data[1] = hton2(column);
269 InsertJunk(scr, ntoh2(data[0]), column);
271 SendPacket(NP_insertJunk, sizeof(data), data);
280 memcpy(data, event.u.net.data, sizeof(data));
281 shapeNum = ntoh2(data[0]);
282 StartNewPiece(scr2, NetNumToShape(shapeNum));
286 MovePiece(scr2, -1, 0);
289 MovePiece(scr2, 0, -1);
292 MovePiece(scr2, 0, 1);
302 int cleared = ClearFullLines(scr2);
304 enemyLinesCleared += cleared;
305 enemyTotalLinesCleared += cleared;
315 memcpy(data, event.u.net.data, sizeof(data));
316 InsertJunk(scr2, ntoh2(data[0]), ntoh2(data[1]));
323 memcpy(data, event.u.net.data, sizeof(data));
324 pausedByThem = ntoh2(data[0]);
325 paused = pausedByMe || pausedByThem;
327 RobotCmd(1, "Pause %d %d\n", pausedByMe,
329 ShowPause(pausedByMe, pausedByThem);
344 if (paused != oldPaused) {
346 pauseTimeLeft = SetITimer(0, 0);
348 SetITimer(speed, pauseTimeLeft);
355 myLinesCleared += linesCleared = ClearFullLines(scr);
356 myTotalLinesCleared += linesCleared;
361 if (linesCleared > 0 && spied)
362 SendPacket(NP_clear, 0, NULL);
363 if (game == GT_classicTwo && linesCleared > 1) {
367 junkLines = linesCleared - (linesCleared < 4);
368 data[0] = hton2(junkLines);
369 SendPacket(NP_giveJunk, sizeof(data), data);
378 ExtFunc int main(int argc, char **argv)
380 int initConn = 0, waitConn = 0, ch, done = 0;
381 char *hostStr = NULL, *portStr = NULL;
384 standoutEnable = colorEnable = 1;
385 stepDownInterval = DEFAULT_INTERVAL;
386 MapKeys(DEFAULT_KEYS);
387 while ((ch = getopt(argc, argv, "hHRs:r:Fk:c:woDSCp:i:")) != -1)
400 stepDownInterval = atof(optarg) * 1e6;
403 initSeed = atoi(optarg);
404 myFlags |= SCF_setSeed;
409 myFlags |= SCF_usingRobot;
413 myFlags |= SCF_fairRobot;
440 if (optind < argc || (initConn && waitConn)) {
444 if (fairRobot && !robotEnable)
445 fatal("You can't use the -F option without the -r option");
450 InitRobot(robotProg);
454 if (initConn || waitConn) {
455 game = GT_classicTwo;
456 if(gameState != STATE_STARTING) {
457 gameState = STATE_WAIT_CONNECTION;
462 InitiateConnection(hostStr, portStr);
464 WaitForConnection(portStr);
465 gameState = STATE_PLAYING;
472 data[0] = hton4(MAJOR_VERSION);
473 data[1] = hton4(PROTOCOL_VERSION);
474 SendPacket(NP_version, sizeof(data), data);
475 if (WaitMyEvent(&event, EM_net) != E_net)
476 fatal("Network negotiation failed");
477 memcpy(data, event.u.net.data, sizeof(data));
478 major = ntoh4(data[0]);
479 protocolVersion = ntoh4(data[1]);
480 if (event.u.net.type != NP_version || major < MAJOR_VERSION)
481 fatal("Your opponent is using an old, incompatible version\n"
482 "of Netris. They should get the latest version.");
483 if (major > MAJOR_VERSION)
484 fatal("Your opponent is using an newer, incompatible version\n"
485 "of Netris. Get the latest version.");
486 if (protocolVersion > PROTOCOL_VERSION)
487 protocolVersion = PROTOCOL_VERSION;
489 if (protocolVersion < 3 && stepDownInterval != DEFAULT_INTERVAL)
490 fatal("Your opponent's version of Netris predates the -i option.\n"
491 "For fairness, you shouldn't use the -i option either.");
497 if (protocolVersion >= 3)
500 len = sizeof(netint4[2]);
501 if ((myFlags & SCF_setSeed))
507 data[0] = hton4(myFlags);
508 data[1] = hton4(seed);
509 data[2] = hton4(stepDownInterval);
510 SendPacket(NP_startConn, len, data);
511 if (WaitMyEvent(&event, EM_net) != E_net ||
512 event.u.net.type != NP_startConn)
513 fatal("Network negotiation failed");
514 memcpy(data, event.u.net.data, len);
515 opponentFlags = ntoh4(data[0]);
516 seed = ntoh4(data[1]);
518 if ((opponentFlags & SCF_setSeed) != (myFlags & SCF_setSeed))
519 fatal("If one player sets the random number seed, "
521 if ((myFlags & SCF_setSeed) && seed != initSeed)
522 fatal("Both players have set the random number seed, "
523 "and they are unequal.");
524 if (protocolVersion >= 3 && stepDownInterval != ntoh4(data[2]))
525 fatal("Your opponent is using a different step-down "
526 "interval (-i).\nYou must both use the same one.");
534 userName = getenv("LOGNAME");
535 if (!userName || !userName[0])
536 userName = getenv("USER");
537 if (!userName || !userName[0])
538 strcpy(userName, "???");
539 len = strlen(userName)+1;
540 if (len > sizeof(opponentName))
541 len = sizeof(opponentName);
542 SendPacket(NP_userName, len, userName);
543 if (WaitMyEvent(&event, EM_net) != E_net ||
544 event.u.net.type != NP_userName)
545 fatal("Network negotiation failed");
546 strncpy(opponentName, event.u.net.data,
547 sizeof(opponentName)-1);
548 opponentName[sizeof(opponentName)-1] = 0;
549 for (i = 0; opponentName[i]; ++i)
550 if (!isprint(opponentName[i]))
551 opponentName[i] = '?';
552 for (i = 0; opponentHost[i]; ++i)
553 if (!isprint(opponentHost[i]))
554 opponentHost[i] = '?';
566 WaitMyEvent(&event, EM_net);
572 gameState = STATE_WAIT_KEYPRESS;
575 while(getchar() != keyTable[KT_new])