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 static struct option options[] = {
31 { "ascii", 2, 0, 'a' },
32 { "connect", 1, 0, 'c' },
33 { "port", 1, 0, 'p' },
34 { "speed", 1, 0, 'i' },
35 { "level", 1, 0, 'l' },
37 { "seed", 1, 0, 's' },
38 { "robot", 1, 0, 'r' },
39 { "fair-robot", 0, 0, 'F' },
40 { "dropmode", 2, 0, 'd' },
41 { "instadrop", 2, 0, 'D' },
42 { "color", 2, 0, 'C' },
43 { "slowterm", 2, 0, 'S' },
44 { "keys", 1, 0, 'k' },
45 { "rules", 0, 0, 'R' },
46 { "info", 0, 0, 'H' },
47 { "help", 0, 0, 'h' },
51 enum { KT_left, KT_right, KT_rotright, KT_rotleft, KT_drop, KT_down,
52 KT_toggleSpy, KT_pause, KT_faster, KT_redraw, KT_numKeys };
54 static char *keyNames[KT_numKeys+1] = {
55 "Left", "Right", "RotRight", "RotLeft", "Drop", "Down",
56 "ToggleSpy", "Pause", "Faster", "Redraw", NULL };
58 static char *gameNames[GT_len] = { "OnePlayer", "ClassicTwo" };
60 static char keyTable[KT_numKeys+1];
63 ExtFunc void MapKeys(char *newKeys)
69 /* XXX assumptions about ASCII encoding here */
70 for (i = k = 0; newKeys[i] && k < KT_numKeys; i++,k++) {
71 if (newKeys[i] == '^' && newKeys[i+1])
72 keyTable[k] = toupper(newKeys[++i]) - ('A' - 1);
74 keyTable[k] = newKeys[i];
76 memset(used, 0, sizeof(used));
77 for (k = 0; k < KT_numKeys; k++) {
78 ch = (unsigned char) keyTable[k];
80 if (iscntrl(ch) && ch < ' ')
81 sprintf(scratch, "Ctrl-%c", ch + ('A' - 1));
83 sprintf(scratch, "\"%c\"", ch);
85 sprintf(scratch, "0x%X", ch);
87 fprintf(stderr, "Duplicate key mappings:\n");
89 fprintf(stderr, " %s mapped to both %s and %s\n",
90 scratch, keyNames[used[ch]-1], keyNames[k]);
98 ExtFunc void WriteConf(void)
102 file_out = fopen(CONFIG_FILE, "w");
103 if (file_out == NULL)
104 die("Error writing config file");
106 fprintf(file_out, "### NETRIS %s Config file ###\n\n", version_string);
109 fprintf(stderr, "Wrote new game configuration to %s\n", CONFIG_FILE);
112 ExtFunc void HandleOption(char tag, char *value)
116 if (value && !strcasecmp(value, "0")) Game.ascii = 0;
124 portStr = value; break;
125 case 'i': //speed (of level 1)
126 Game.initspeed = atof(value) * 1e6;
129 if ((Players[0].score.level = atof(value)) < 1)
130 Players[0].score.level = 1;
131 if (Players[0].score.level > 15)
132 Players[0].score.level = 15;
138 Players[i / 10].spy = i % 10;
142 Game.seed = atoi(value);
146 Players[0].flags |= SCF_usingRobot;
149 case 'F': //fair robot
151 Players[0].flags |= SCF_fairRobot;
153 case 'd': //drop mode
154 if (value && !strcasecmp(value, "0")) Players[0].dropmode &= 254;
155 else Players[0].dropmode |= 1;
157 case 'D': //instadrop
158 if (value && !strcasecmp(value, "0")) Players[0].dropmode &= 253;
159 else Players[0].dropmode |= 2;
162 if (value && !strcasecmp(value, "1")) Game.color = 1;
166 if (value && !strcasecmp(value, "1")) Game.standout = 1;
167 else Game.standout = 0;
170 MapKeys(value); break;
182 ExtFunc void ReadConf(char *filename)
188 char tag[81], value[81];
190 file_in = fopen(filename, "r");
192 while (fgets(buf, 512, file_in) != NULL) {
193 if ((ch = strchr(buf, '#')))
194 *ch = '\0'; // truncate string from # char
195 for (i = strlen(buf)-1; i >= 0; i--)
196 if (buf[i] == ' ' || buf[i] == '\n' || buf[i] == '\t' || buf[i] == 13)
200 sscanf(buf, "%80[^= \t] = %80[^\n]", tag, value);
201 for (i = 0; options[i].name; i++){
202 if (!strcasecmp(options[i].name, tag)) {
203 HandleOption(options[i].val, value);
211 fprintf(stderr, "Unable to open config file %s.\n", filename);
216 ExtFunc int StartNewPiece(int scr, Shape *shape)
218 if (Players[scr].nextShape) {
219 Players[scr].curShape = Players[scr].nextShape;
220 Players[scr].nextShape = shape;
223 Players[scr].curShape = shape;
224 Players[scr].curY = Players[scr].boardVisible + 4;
225 Players[scr].curX = Players[scr].boardWidth / 2;
226 while (!ShapeVisible(Players[scr].curShape, scr,
227 Players[scr].curY, Players[scr].curX))
229 if (!ShapeFits(Players[scr].curShape, scr,
230 Players[scr].curY, Players[scr].curX))
232 PlotShape(Players[scr].curShape, scr,
233 Players[scr].curY, Players[scr].curX, 1);
237 ExtFunc void OneGame(int scr)
240 int linesCleared, changed = 0;
241 int spied = 0, dropMode = 0;
242 int oldPaused = 0, paused = 0, pausedByMe = 0, pausedByThem = 0;
247 int linevalues[4] = { 40, 100, 400, 1200 }; //= 50*lines! - 10*(lines==1)
250 Game.speed = Game.initspeed;
251 for (i = 1; i < Players[scr].score.level; i++)
252 Game.speed /= SPEEDINC;
253 if (Game.speed < SPEEDMINIMUM)
254 Game.speed = SPEEDMINIMUM;
257 for (i = 1; i <= totalPlayers + 1; i++)
258 if (Players[i].spy) DrawField(i);
259 if (totalPlayers > 0) {
263 SetITimer(Game.speed, Game.speed);
265 RobotCmd(0, "GameType %s\n", gameNames[game]);
266 RobotCmd(0, "BoardSize 0 %d %d\n",
267 Players[scr].boardVisible, Players[scr].boardWidth);
268 for (i = 1; i <= totalPlayers + 1; i++) {
269 RobotCmd(0, "BoardSize %d %d %d\n",
270 i, Players[i].boardVisible, Players[i].boardWidth);
271 RobotCmd(0, "Opponent %d %s %s\n",
272 i, Players[i].name, Players[i].host);
273 if (Players[i].flags & SCF_usingRobot)
274 RobotCmd(0, "OpponentFlag %d robot\n", i);
275 if (Players[i].flags & SCF_fairRobot)
276 RobotCmd(0, "OpponentFlag %d fairRobot\n", i);
278 RobotCmd(0, "TickLength %.3f\n", Game.speed / 1.0e6);
279 RobotCmd(0, "BeginGame\n");
282 Players[scr].nextShape = ChooseOption(stdOptions);
283 while (StartNewPiece(scr, ChooseOption(stdOptions))) {
284 ShowScore(scr, Players[scr].score);
285 if (robotEnable && !fairRobot)
286 RobotCmd(1, "NewPiece %d\n", ++pieceCount);
291 shapeNum = ShapeToNetNum(Players[scr].curShape);
292 data[0] = hton2(shapeNum);
293 SendPacket(scr, NP_newPiece, sizeof(data), data);
296 changed = RefreshBoard(scr) || changed;
297 for (i = 1; i <= totalPlayers+1; i++) if (Players[i].spy && i != me)
298 changed = RefreshBoard(i) || changed;
300 if (!paused) ShowTime();
305 switch (WaitMyEvent(&event, EM_any)) {
307 if (!MovePiece(scr, -1, 0))
310 SendPacket(scr, NP_down, 0, NULL);
313 p = strchr(keyTable, tolower(event.u.key));
316 RobotCmd(1, "UserKey %d %s\n",
317 (int)(unsigned char)event.u.key,
318 p ? keyNames[key] : "?");
324 if (paused && (key != KT_pause) && (key != KT_redraw))
328 if (MovePiece(scr, 0, -1) && spied)
329 SendPacket(scr, NP_left, 0, NULL);
332 if (MovePiece(scr, 0, 1) && spied)
333 SendPacket(scr, NP_right, 0, NULL);
336 if (RotatePiece(scr, 0) && spied)
337 SendPacket(scr, NP_rotleft, 0, NULL);
340 if (RotatePiece(scr, 1) && spied)
341 SendPacket(scr, NP_rotright, 0, NULL);
344 SetITimer(Game.speed, Game.speed);
345 if (MovePiece(scr, -1, 0)) {
346 if (spied) SendPacket(scr, NP_down, 0, NULL);
351 SetITimer(Game.speed, Game.speed);
352 if (DropPiece(scr)) {
353 if (spied) SendPacket(scr, NP_drop, 0, NULL);
354 if (Players[scr].dropmode == 2) goto nextPiece;
357 dropMode = Players[scr].dropmode;
360 pausedByMe = !pausedByMe;
361 if (game == GT_classicTwo) {
364 data[0] = hton2(pausedByMe);
365 SendPacket(scr, NP_pause, sizeof(data), data);
367 paused = pausedByMe || pausedByThem;
369 RobotCmd(1, "Pause %d %d\n", pausedByMe,
371 ShowPause(pausedByMe, pausedByThem);
375 if (game != GT_onePlayer)
377 if ((Game.speed /= SPEEDINC) < SPEEDMINIMUM)
378 Game.speed = SPEEDMINIMUM;
379 SetITimer(Game.speed, SetITimer(0, 0));
380 Players[scr].score.level++;
381 ShowScore(scr, Players[scr].score);
385 ScheduleFullRedraw();
390 if (dropMode && DropPiece(scr) > 0) {
391 SetITimer(Game.speed, Game.speed);
393 SendPacket(scr, NP_drop, 0, NULL);
400 cmd = event.u.robot.data;
401 if ((p = strchr(cmd, ' ')))
404 p = cmd + strlen(cmd);
405 for (key = 0; keyNames[key]; ++key)
406 if (!strcmp(keyNames[key], cmd) &&
407 (fairRobot || (1 == sscanf(p, "%d", &num) &&
410 if (!strcmp(cmd, "Message")) {
417 switch(event.u.net.type) {
423 memcpy(data, event.u.net.data, sizeof(data[0]));
424 column = Random(0, Players[scr].boardWidth);
425 data[1] = hton2(column);
426 InsertJunk(scr, ntoh2(data[0]), column);
428 SendPacket(scr, NP_insertJunk, sizeof(data), data);
429 // Message("Opponent added %d lines");
437 FreezePiece(event.u.net.uid);
438 memcpy(data, event.u.net.data, sizeof(data));
439 shapeNum = ntoh2(data[0]);
440 StartNewPiece(event.u.net.uid, NetNumToShape(shapeNum));
444 MovePiece(event.u.net.uid, -1, 0);
447 MovePiece(event.u.net.uid, 0, -1);
450 MovePiece(event.u.net.uid, 0, 1);
453 RotatePiece(event.u.net.uid, 0);
456 RotatePiece(event.u.net.uid, 1);
459 DropPiece(event.u.net.uid);
462 ClearFullLines(event.u.net.uid);
468 memcpy(data, event.u.net.data, sizeof(data));
469 InsertJunk(event.u.net.uid, ntoh2(data[0]), ntoh2(data[1]));
476 memcpy(data, event.u.net.data, sizeof(data));
477 pausedByThem = ntoh2(data[0]);
478 paused = pausedByMe || pausedByThem;
480 RobotCmd(1, "Pause %d %d\n", pausedByMe,
482 ShowPause(pausedByMe, pausedByThem);
496 if (paused != oldPaused) {
499 pauseTimeLeft = SetITimer(0, 0);
502 SetITimer(Game.speed, pauseTimeLeft);
511 Players[scr].score.drops++;
512 Players[scr].score.score++;
513 if ((linesCleared = ClearFullLines(scr)) > 0) {
514 if (game == GT_onePlayer)
515 if ((Players[scr].score.lines / 10) <
516 ((Players[scr].score.lines + linesCleared) / 10)) {
517 if ((Game.speed /= SPEEDINC) < SPEEDMINIMUM)
518 Game.speed = SPEEDMINIMUM;
519 SetITimer(Game.speed, SetITimer(0, 0));
520 Players[scr].score.level++;
522 Players[scr].score.score += linevalues[linesCleared];
523 Players[scr].score.lines += linesCleared;
524 Players[scr].score.adds += linesCleared - (linesCleared < 4);
526 SendPacket(scr, NP_clear, 0, NULL);
528 if (game == GT_classicTwo && linesCleared > 1) {
532 junkLines = linesCleared - (linesCleared < 4);
533 data[0] = hton2(junkLines);
534 SendPacket(scr, NP_giveJunk, sizeof(data), data);
541 ExtFunc int main(int argc, char **argv)
545 Game.standout = Game.color = 1;
546 Game.initspeed = DEFAULT_INTERVAL;
547 MapKeys(DEFAULT_KEYS);
552 for (i = 0; i <= MAX_SCREENS; i++)
553 Players[i].score.level = Players[i].spy = 1;
554 if (!(userName = getenv("LOGNAME")) || !userName[0])
555 if (!(userName = getenv("USER")) || !userName[0])
556 userName = "Anonymous";
557 strncpy(Players[0].name, userName, 16); //sizeof(Player.name)
558 Players[0].name[16] = 0;
559 Players[0].boardWidth = 10;
560 Players[0].boardHeight = MAX_BOARD_HEIGHT;
561 Players[0].boardVisible = 20;
563 strcpy(Players[0].host, "localhost");
566 // if (getopt(argc, argv, "f:") == 'f')
569 ReadConf(CONFIG_FILE);
570 while ((ch = getopt_long(argc, argv,
571 "hHRs:r:Fk:c:odDSCap:i:l:", options, NULL)) != -1)
572 HandleOption(ch, optarg);
577 if (fairRobot && !robotEnable)
578 fatal("You can't use the -F option without the -r option");
584 game = GT_classicTwo;
585 InitiateConnection(hostStr, portStr);
593 memcpy(&Players[me], &Players[0], sizeof(Player));