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 speed = stepDownInterval;
113 UpdateOpponentDisplay();
116 SetITimer(speed, speed);
118 RobotCmd(0, "GameType %s\n", gameNames[game]);
119 RobotCmd(0, "BoardSize 0 %d %d\n",
120 boardVisible[scr], boardWidth[scr]);
122 RobotCmd(0, "BoardSize 1 %d %d\n",
123 boardVisible[scr2], boardWidth[scr2]);
124 RobotCmd(0, "Opponent 1 %s %s\n", opponentName, opponentHost);
125 if (opponentFlags & SCF_usingRobot)
126 RobotCmd(0, "OpponentFlag 1 robot\n");
127 if (opponentFlags & SCF_fairRobot)
128 RobotCmd(0, "OpponentFlag 1 fairRobot\n");
130 RobotCmd(0, "TickLength %.3f\n", speed / 1.0e6);
131 RobotCmd(0, "BeginGame\n");
134 while (StartNewPiece(scr, ChooseOption(stdOptions))) {
135 if (robotEnable && !fairRobot)
136 RobotCmd(1, "NewPiece %d\n", ++pieceCount);
141 shapeNum = ShapeToNetNum(curShape[scr]);
142 data[0] = hton2(shapeNum);
143 SendPacket(NP_newPiece, sizeof(data), data);
146 changed = RefreshBoard(scr) || changed;
148 changed = RefreshBoard(scr2) || changed;
154 switch (WaitMyEvent(&event, EM_any)) {
156 if (!MovePiece(scr, -1, 0))
159 SendPacket(NP_down, 0, NULL);
162 p = strchr(keyTable, tolower(event.u.key));
165 RobotCmd(1, "UserKey %d %s\n",
166 (int)(unsigned char)event.u.key,
167 p ? keyNames[key] : "?");
173 if (paused && (key != KT_pause) && (key != KT_redraw))
177 if (MovePiece(scr, 0, -1) && spied)
178 SendPacket(NP_left, 0, NULL);
181 if (MovePiece(scr, 0, 1) && spied)
182 SendPacket(NP_right, 0, NULL);
185 if (RotatePiece(scr) && spied)
186 SendPacket(NP_rotate, 0, NULL);
189 if (MovePiece(scr, -1, 0) && spied)
190 SendPacket(NP_down, 0, NULL);
193 spying = (!spying) && (scr2 >= 0);
196 if (DropPiece(scr) > 0) {
198 SendPacket(NP_drop, 0, NULL);
199 SetITimer(speed, speed);
201 dropMode = dropModeEnable;
204 pausedByMe = !pausedByMe;
205 if (game == GT_classicTwo) {
208 data[0] = hton2(pausedByMe);
209 SendPacket(NP_pause, sizeof(data), data);
211 paused = pausedByMe || pausedByThem;
213 RobotCmd(1, "Pause %d %d\n", pausedByMe,
215 ShowPause(pausedByMe, pausedByThem);
219 if (game != GT_onePlayer)
222 SetITimer(speed, SetITimer(0, 0));
227 ScheduleFullRedraw();
232 if (dropMode && DropPiece(scr) > 0) {
234 SendPacket(NP_drop, 0, NULL);
235 SetITimer(speed, speed);
242 cmd = event.u.robot.data;
243 if ((p = strchr(cmd, ' ')))
246 p = cmd + strlen(cmd);
247 for (key = 0; keyNames[key]; ++key)
248 if (!strcmp(keyNames[key], cmd) &&
249 (fairRobot || (1 == sscanf(p, "%d", &num) &&
252 if (!strcmp(cmd, "Message")) {
259 switch(event.u.net.type) {
265 memcpy(data, event.u.net.data, sizeof(data[0]));
266 column = Random(0, boardWidth[scr]);
267 data[1] = hton2(column);
268 InsertJunk(scr, ntoh2(data[0]), column);
270 SendPacket(NP_insertJunk, sizeof(data), data);
279 memcpy(data, event.u.net.data, sizeof(data));
280 shapeNum = ntoh2(data[0]);
281 StartNewPiece(scr2, NetNumToShape(shapeNum));
285 MovePiece(scr2, -1, 0);
288 MovePiece(scr2, 0, -1);
291 MovePiece(scr2, 0, 1);
300 ClearFullLines(scr2);
306 memcpy(data, event.u.net.data, sizeof(data));
307 InsertJunk(scr2, ntoh2(data[0]), ntoh2(data[1]));
314 memcpy(data, event.u.net.data, sizeof(data));
315 pausedByThem = ntoh2(data[0]);
316 paused = pausedByMe || pausedByThem;
318 RobotCmd(1, "Pause %d %d\n", pausedByMe,
320 ShowPause(pausedByMe, pausedByThem);
335 if (paused != oldPaused) {
337 pauseTimeLeft = SetITimer(0, 0);
339 SetITimer(speed, pauseTimeLeft);
346 linesCleared = ClearFullLines(scr);
347 if (linesCleared > 0 && spied)
348 SendPacket(NP_clear, 0, NULL);
349 if (game == GT_classicTwo && linesCleared > 1) {
353 junkLines = linesCleared - (linesCleared < 4);
354 data[0] = hton2(junkLines);
355 SendPacket(NP_giveJunk, sizeof(data), data);
364 ExtFunc int main(int argc, char **argv)
366 int initConn = 0, waitConn = 0, ch, done = 0;
367 char *hostStr = NULL, *portStr = NULL;
370 standoutEnable = colorEnable = 1;
371 stepDownInterval = DEFAULT_INTERVAL;
372 MapKeys(DEFAULT_KEYS);
373 while ((ch = getopt(argc, argv, "hHRs:r:Fk:c:woDSCp:i:")) != -1)
386 stepDownInterval = atof(optarg) * 1e6;
389 initSeed = atoi(optarg);
390 myFlags |= SCF_setSeed;
395 myFlags |= SCF_usingRobot;
399 myFlags |= SCF_fairRobot;
426 if (optind < argc || (initConn && waitConn)) {
430 if (fairRobot && !robotEnable)
431 fatal("You can't use the -F option without the -r option");
436 InitRobot(robotProg);
439 if (initConn || waitConn) {
440 game = GT_classicTwo;
441 if(gameState != STATE_STARTING) {
442 gameState = STATE_WAIT_CONNECTION;
447 InitiateConnection(hostStr, portStr);
449 WaitForConnection(portStr);
450 gameState = STATE_PLAYING;
457 data[0] = hton4(MAJOR_VERSION);
458 data[1] = hton4(PROTOCOL_VERSION);
459 SendPacket(NP_version, sizeof(data), data);
460 if (WaitMyEvent(&event, EM_net) != E_net)
461 fatal("Network negotiation failed");
462 memcpy(data, event.u.net.data, sizeof(data));
463 major = ntoh4(data[0]);
464 protocolVersion = ntoh4(data[1]);
465 if (event.u.net.type != NP_version || major < MAJOR_VERSION)
466 fatal("Your opponent is using an old, incompatible version\n"
467 "of Netris. They should get the latest version.");
468 if (major > MAJOR_VERSION)
469 fatal("Your opponent is using an newer, incompatible version\n"
470 "of Netris. Get the latest version.");
471 if (protocolVersion > PROTOCOL_VERSION)
472 protocolVersion = PROTOCOL_VERSION;
474 if (protocolVersion < 3 && stepDownInterval != DEFAULT_INTERVAL)
475 fatal("Your opponent's version of Netris predates the -i option.\n"
476 "For fairness, you shouldn't use the -i option either.");
482 if (protocolVersion >= 3)
485 len = sizeof(netint4[2]);
486 if ((myFlags & SCF_setSeed))
492 data[0] = hton4(myFlags);
493 data[1] = hton4(seed);
494 data[2] = hton4(stepDownInterval);
495 SendPacket(NP_startConn, len, data);
496 if (WaitMyEvent(&event, EM_net) != E_net ||
497 event.u.net.type != NP_startConn)
498 fatal("Network negotiation failed");
499 memcpy(data, event.u.net.data, len);
500 opponentFlags = ntoh4(data[0]);
501 seed = ntoh4(data[1]);
503 if ((opponentFlags & SCF_setSeed) != (myFlags & SCF_setSeed))
504 fatal("If one player sets the random number seed, "
506 if ((myFlags & SCF_setSeed) && seed != initSeed)
507 fatal("Both players have set the random number seed, "
508 "and they are unequal.");
509 if (protocolVersion >= 3 && stepDownInterval != ntoh4(data[2]))
510 fatal("Your opponent is using a different step-down "
511 "interval (-i).\nYou must both use the same one.");
519 userName = getenv("LOGNAME");
520 if (!userName || !userName[0])
521 userName = getenv("USER");
522 if (!userName || !userName[0])
523 strcpy(userName, "???");
524 len = strlen(userName)+1;
525 if (len > sizeof(opponentName))
526 len = sizeof(opponentName);
527 SendPacket(NP_userName, len, userName);
528 if (WaitMyEvent(&event, EM_net) != E_net ||
529 event.u.net.type != NP_userName)
530 fatal("Network negotiation failed");
531 strncpy(opponentName, event.u.net.data,
532 sizeof(opponentName)-1);
533 opponentName[sizeof(opponentName)-1] = 0;
534 for (i = 0; opponentName[i]; ++i)
535 if (!isprint(opponentName[i]))
536 opponentName[i] = '?';
537 for (i = 0; opponentHost[i]; ++i)
538 if (!isprint(opponentHost[i]))
539 opponentHost[i] = '?';
551 WaitMyEvent(&event, EM_net);
557 gameState = STATE_WAIT_KEYPRESS;
560 while(getchar() != keyTable[KT_new])