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_numKeys };
33 static char *keyNames[KT_numKeys+1] = {
34 "Left", "Rotate", "Right", "Drop", "Down", "ToggleSpy", "Pause",
35 "Faster", "Redraw", 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 ExtFunc void MapKeys(char *newKeys)
49 /* XXX assumptions about ASCII encoding here */
50 for (i = k = 0; newKeys[i] && k < KT_numKeys; i++,k++) {
51 if (newKeys[i] == '^' && newKeys[i+1])
52 keyTable[k] = toupper(newKeys[++i]) - ('A' - 1);
54 keyTable[k] = newKeys[i];
56 memset(used, 0, sizeof(used));
57 for (k = 0; k < KT_numKeys; k++) {
58 ch = (unsigned char) keyTable[k];
60 if (iscntrl(ch) && ch < ' ')
61 sprintf(scratch, "Ctrl-%c", ch + ('A' - 1));
63 sprintf(scratch, "\"%c\"", ch);
65 sprintf(scratch, "0x%X", ch);
67 fprintf(stderr, "Duplicate key mappings:\n");
69 fprintf(stderr, " %s mapped to both %s and %s\n",
70 scratch, keyNames[used[ch]-1], keyNames[k]);
78 ExtFunc int StartNewPiece(int scr, Shape *shape)
80 curShape[scr] = shape;
81 curY[scr] = boardVisible[scr] + 4;
82 curX[scr] = boardWidth[scr] / 2;
83 while (!ShapeVisible(shape, scr, curY[scr], curX[scr]))
85 if (!ShapeFits(shape, scr, curY[scr], curX[scr]))
87 PlotShape(shape, scr, curY[scr], curX[scr], 1);
91 ExtFunc void OneGame(int scr, int scr2)
94 int linesCleared, changed = 0;
95 int spied = 0, spying = 0, dropMode = 0;
96 int oldPaused = 0, paused = 0, pausedByMe = 0, pausedByThem = 0;
102 speed = stepDownInterval;
109 UpdateOpponentDisplay();
112 SetITimer(speed, speed);
114 RobotCmd(0, "GameType %s\n", gameNames[game]);
115 RobotCmd(0, "BoardSize 0 %d %d\n",
116 boardVisible[scr], boardWidth[scr]);
118 RobotCmd(0, "BoardSize 1 %d %d\n",
119 boardVisible[scr2], boardWidth[scr2]);
120 RobotCmd(0, "Opponent 1 %s %s\n", opponentName, opponentHost);
121 if (opponentFlags & SCF_usingRobot)
122 RobotCmd(0, "OpponentFlag 1 robot\n");
123 if (opponentFlags & SCF_fairRobot)
124 RobotCmd(0, "OpponentFlag 1 fairRobot\n");
126 RobotCmd(0, "TickLength %.3f\n", speed / 1.0e6);
127 RobotCmd(0, "BeginGame\n");
130 while (StartNewPiece(scr, ChooseOption(stdOptions))) {
131 if (robotEnable && !fairRobot)
132 RobotCmd(1, "NewPiece %d\n", ++pieceCount);
137 shapeNum = ShapeToNetNum(curShape[scr]);
138 data[0] = hton2(shapeNum);
139 SendPacket(NP_newPiece, sizeof(data), data);
142 changed = RefreshBoard(scr) || changed;
144 changed = RefreshBoard(scr2) || changed;
150 switch (WaitMyEvent(&event, EM_any)) {
152 if (!MovePiece(scr, -1, 0))
155 SendPacket(NP_down, 0, NULL);
158 p = strchr(keyTable, tolower(event.u.key));
161 RobotCmd(1, "UserKey %d %s\n",
162 (int)(unsigned char)event.u.key,
163 p ? keyNames[key] : "?");
169 if (paused && (key != KT_pause) && (key != KT_redraw))
173 if (MovePiece(scr, 0, -1) && spied)
174 SendPacket(NP_left, 0, NULL);
177 if (MovePiece(scr, 0, 1) && spied)
178 SendPacket(NP_right, 0, NULL);
181 if (RotatePiece(scr) && spied)
182 SendPacket(NP_rotate, 0, NULL);
185 if (MovePiece(scr, -1, 0) && spied)
186 SendPacket(NP_down, 0, NULL);
189 spying = (!spying) && (scr2 >= 0);
192 if (DropPiece(scr) > 0) {
194 SendPacket(NP_drop, 0, NULL);
195 SetITimer(speed, speed);
197 dropMode = dropModeEnable;
200 pausedByMe = !pausedByMe;
201 if (game == GT_classicTwo) {
204 data[0] = hton2(pausedByMe);
205 SendPacket(NP_pause, sizeof(data), data);
207 paused = pausedByMe || pausedByThem;
209 RobotCmd(1, "Pause %d %d\n", pausedByMe,
211 ShowPause(pausedByMe, pausedByThem);
215 if (game != GT_onePlayer)
218 SetITimer(speed, SetITimer(0, 0));
223 ScheduleFullRedraw();
228 if (dropMode && DropPiece(scr) > 0) {
230 SendPacket(NP_drop, 0, NULL);
231 SetITimer(speed, speed);
238 cmd = event.u.robot.data;
239 if ((p = strchr(cmd, ' ')))
242 p = cmd + strlen(cmd);
243 for (key = 0; keyNames[key]; ++key)
244 if (!strcmp(keyNames[key], cmd) &&
245 (fairRobot || (1 == sscanf(p, "%d", &num) &&
248 if (!strcmp(cmd, "Message")) {
255 switch(event.u.net.type) {
261 memcpy(data, event.u.net.data, sizeof(data[0]));
262 column = Random(0, boardWidth[scr]);
263 data[1] = hton2(column);
264 InsertJunk(scr, ntoh2(data[0]), column);
266 SendPacket(NP_insertJunk, sizeof(data), data);
275 memcpy(data, event.u.net.data, sizeof(data));
276 shapeNum = ntoh2(data[0]);
277 StartNewPiece(scr2, NetNumToShape(shapeNum));
281 MovePiece(scr2, -1, 0);
284 MovePiece(scr2, 0, -1);
287 MovePiece(scr2, 0, 1);
296 ClearFullLines(scr2);
302 memcpy(data, event.u.net.data, sizeof(data));
303 InsertJunk(scr2, ntoh2(data[0]), ntoh2(data[1]));
310 memcpy(data, event.u.net.data, sizeof(data));
311 pausedByThem = ntoh2(data[0]);
312 paused = pausedByMe || pausedByThem;
314 RobotCmd(1, "Pause %d %d\n", pausedByMe,
316 ShowPause(pausedByMe, pausedByThem);
330 if (paused != oldPaused) {
332 pauseTimeLeft = SetITimer(0, 0);
334 SetITimer(speed, pauseTimeLeft);
341 linesCleared = ClearFullLines(scr);
342 if (linesCleared > 0 && spied)
343 SendPacket(NP_clear, 0, NULL);
344 if (game == GT_classicTwo && linesCleared > 1) {
348 junkLines = linesCleared - (linesCleared < 4);
349 data[0] = hton2(junkLines);
350 SendPacket(NP_giveJunk, sizeof(data), data);
357 ExtFunc int main(int argc, char **argv)
359 int initConn = 0, waitConn = 0, ch;
360 char *hostStr = NULL, *portStr = NULL;
362 standoutEnable = colorEnable = 1;
363 stepDownInterval = DEFAULT_INTERVAL;
364 MapKeys(DEFAULT_KEYS);
365 while ((ch = getopt(argc, argv, "hHRs:r:Fk:c:woDSCp:i:")) != -1)
378 stepDownInterval = atof(optarg) * 1e6;
381 initSeed = atoi(optarg);
382 myFlags |= SCF_setSeed;
387 myFlags |= SCF_usingRobot;
391 myFlags |= SCF_fairRobot;
418 if (optind < argc || (initConn && waitConn)) {
422 if (fairRobot && !robotEnable)
423 fatal("You can't use the -F option without the -r option");
426 InitRobot(robotProg);
429 if (initConn || waitConn) {
432 game = GT_classicTwo;
434 InitiateConnection(hostStr, portStr);
436 WaitForConnection(portStr);
441 data[0] = hton4(MAJOR_VERSION);
442 data[1] = hton4(PROTOCOL_VERSION);
443 SendPacket(NP_version, sizeof(data), data);
444 if (WaitMyEvent(&event, EM_net) != E_net)
445 fatal("Network negotiation failed");
446 memcpy(data, event.u.net.data, sizeof(data));
447 major = ntoh4(data[0]);
448 protocolVersion = ntoh4(data[1]);
449 if (event.u.net.type != NP_version || major < MAJOR_VERSION)
450 fatal("Your opponent is using an old, incompatible version\n"
451 "of Netris. They should get the latest version.");
452 if (major > MAJOR_VERSION)
453 fatal("Your opponent is using an newer, incompatible version\n"
454 "of Netris. Get the latest version.");
455 if (protocolVersion > PROTOCOL_VERSION)
456 protocolVersion = PROTOCOL_VERSION;
458 if (protocolVersion < 3 && stepDownInterval != DEFAULT_INTERVAL)
459 fatal("Your opponent's version of Netris predates the -i option.\n"
460 "For fairness, you shouldn't use the -i option either.");
466 if (protocolVersion >= 3)
469 len = sizeof(netint4[2]);
470 if ((myFlags & SCF_setSeed))
476 data[0] = hton4(myFlags);
477 data[1] = hton4(seed);
478 data[2] = hton4(stepDownInterval);
479 SendPacket(NP_startConn, len, data);
480 if (WaitMyEvent(&event, EM_net) != E_net ||
481 event.u.net.type != NP_startConn)
482 fatal("Network negotiation failed");
483 memcpy(data, event.u.net.data, len);
484 opponentFlags = ntoh4(data[0]);
485 seed = ntoh4(data[1]);
487 if ((opponentFlags & SCF_setSeed) != (myFlags & SCF_setSeed))
488 fatal("If one player sets the random number seed, "
490 if ((myFlags & SCF_setSeed) && seed != initSeed)
491 fatal("Both players have set the random number seed, "
492 "and they are unequal.");
493 if (protocolVersion >= 3 && stepDownInterval != ntoh4(data[2]))
494 fatal("Your opponent is using a different step-down "
495 "interval (-i).\nYou must both use the same one.");
503 userName = getenv("LOGNAME");
504 if (!userName || !userName[0])
505 userName = getenv("USER");
506 if (!userName || !userName[0])
507 strcpy(userName, "???");
508 len = strlen(userName)+1;
509 if (len > sizeof(opponentName))
510 len = sizeof(opponentName);
511 SendPacket(NP_userName, len, userName);
512 if (WaitMyEvent(&event, EM_net) != E_net ||
513 event.u.net.type != NP_userName)
514 fatal("Network negotiation failed");
515 strncpy(opponentName, event.u.net.data,
516 sizeof(opponentName)-1);
517 opponentName[sizeof(opponentName)-1] = 0;
518 for (i = 0; opponentName[i]; ++i)
519 if (!isprint(opponentName[i]))
520 opponentName[i] = '?';
521 for (i = 0; opponentHost[i]; ++i)
522 if (!isprint(opponentHost[i]))
523 opponentHost[i] = '?';