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 { "level", 1, 0, 'l' },
35 { "team", 1, 0, 't' },
37 { "robot", 1, 0, 'r' },
38 { "fair-robot", 0, 0, 'F' },
39 { "dropmode", 2, 0, 'd' },
40 { "instadrop", 2, 0, 'D' },
41 { "color", 2, 0, 'C' },
42 { "slowterm", 2, 0, 'S' },
43 { "keys", 1, 0, 'k' },
44 { "rules", 0, 0, 'R' },
45 { "info", 0, 0, 'H' },
46 { "help", 0, 0, 'h' },
50 enum { KT_left, KT_right, KT_rotright, KT_rotleft, KT_drop, KT_down,
51 KT_faster, KT_pause, KT_redraw, KT_quit, KT_numKeys };
53 static char *keyNames[KT_numKeys+1] = {
54 "Left", "Right", "RotRight", "RotLeft", "Drop", "Down",
55 "Faster", "Pause", "Redraw", "Quit", NULL };
57 static char *gameNames[GT_len] = { "OnePlayer", "ClassicTwo" };
59 static char keyTable[KT_numKeys+1];
62 static int paused = 0;
65 ExtFunc void MapKeys(char *newKeys)
71 /* XXX assumptions about ASCII encoding here */
72 for (i = k = 0; newKeys[i] && k < KT_numKeys; i++,k++) {
73 if (newKeys[i] == '^' && newKeys[i+1])
74 keyTable[k] = toupper(newKeys[++i]) - ('A' - 1);
76 keyTable[k] = newKeys[i];
78 memset(used, 0, sizeof(used));
79 for (k = 0; k < KT_numKeys; k++) {
80 ch = (unsigned char) keyTable[k];
82 if (iscntrl(ch) && ch < ' ')
83 sprintf(scratch, "Ctrl-%c", ch + ('A' - 1));
85 sprintf(scratch, "\"%c\"", ch);
87 sprintf(scratch, "0x%X", ch);
89 fprintf(stderr, "Duplicate key mappings:\n");
91 fprintf(stderr, " %s mapped to both %s and %s\n",
92 scratch, keyNames[used[ch]-1], keyNames[k]);
100 ExtFunc void WriteConf(void)
104 file_out = fopen(CONFIG_FILE, "w");
105 if (file_out == NULL)
106 die("Error writing config file");
108 fprintf(file_out, "### NETRIS %s Config file ###\n\n", version_string);
111 fprintf(stderr, "Wrote new game configuration to %s\n", CONFIG_FILE);
114 ExtFunc void HandleOption(char tag, char *value)
118 if (value && !strcasecmp(value, "0")) Game.ascii = 0;
128 case 'i': //speed (of level 1)
129 Game.initspeed = atof(value) * 1e6;
132 if ((Players[0].score.level = atof(value)) < 1)
133 Players[0].score.level = 1;
134 if (Players[0].score.level > 15)
135 Players[0].score.level = 15;
138 Players[0].team = atoi(value);
144 Players[i / 10].spy = i % 10;
149 Players[0].flags |= SCF_usingRobot;
152 case 'F': //fair robot
154 Players[0].flags |= SCF_fairRobot;
156 case 'd': //drop mode
157 if (value && !strcasecmp(value, "0")) Players[0].dropmode &= 254;
158 else Players[0].dropmode |= 1;
160 case 'D': //instadrop
161 if (value && !strcasecmp(value, "0")) Players[0].dropmode &= 253;
162 else Players[0].dropmode |= 2;
165 if (value && !strcasecmp(value, "1")) Game.color = 1;
169 if (value && !strcasecmp(value, "1")) Game.standout = 1;
170 else Game.standout = 0;
173 MapKeys(value); break;
185 ExtFunc void ReadConf(char *filename)
191 char tag[81], value[81];
193 file_in = fopen(filename, "r");
195 while (fgets(buf, 512, file_in) != NULL) {
196 if ((ch = strchr(buf, '#')))
197 *ch = '\0'; // truncate string from # char
198 for (i = strlen(buf)-1; i >= 0; i--)
199 if (buf[i] == ' ' || buf[i] == '\n' || buf[i] == '\t' || buf[i] == 13)
203 sscanf(buf, "%80[^= \t] = %80[^\n]", tag, value);
204 for (i = 0; options[i].name; i++){
205 if (!strcasecmp(options[i].name, tag)) {
206 HandleOption(options[i].val, value);
214 fprintf(stderr, "Unable to open config file %s.\n", filename);
219 ExtFunc int StartNewPiece(int scr, Shape *shape)
221 if (Players[scr].nextShape) {
222 Players[scr].curShape = Players[scr].nextShape;
223 Players[scr].nextShape = shape;
226 Players[scr].curShape = shape;
227 Players[scr].curY = Players[scr].boardVisible + 4;
228 Players[scr].curX = Players[scr].boardWidth / 2;
229 while (!ShapeVisible(Players[scr].curShape, scr,
230 Players[scr].curY, Players[scr].curX))
232 if (!ShapeFits(Players[scr].curShape, scr,
233 Players[scr].curY, Players[scr].curX))
235 PlotShape(Players[scr].curShape, scr,
236 Players[scr].curY, Players[scr].curX, 1);
240 ExtFunc void checkPaused(void)
241 { //check whether anyone paused the game
245 for (i = 1; i <= totalPlayers; i++)
246 if (Players[i].alive)
247 paused |= Players[i].flags & SCF_paused;
250 ExtFunc void OneGame(int scr)
253 int linesCleared, changed = 0;
254 int spied = 0, dropMode = 0;
261 int linevalues[4] = { 40, 100, 400, 1200 }; //= 50*lines! - 10*(lines==1)
264 Game.speed = Game.initspeed;
265 for (i = 1; i < Players[scr].score.level; i++)
266 Game.speed /= SPEEDINC;
267 if (Game.speed < SPEEDMINIMUM)
268 Game.speed = SPEEDMINIMUM;
271 for (i = 1; i <= totalPlayers; i++)
272 if (Players[i].spy) DrawField(i);
273 if (totalPlayers > 1) {
276 SetITimer(Game.speed, Game.speed);
278 RobotCmd(0, "GameType %s\n", gameNames[game]);
279 RobotCmd(0, "BoardSize 0 %d %d\n",
280 Players[scr].boardVisible, Players[scr].boardWidth);
281 for (i = 1; i <= totalPlayers; i++) {
282 RobotCmd(0, "BoardSize %d %d %d\n",
283 i, Players[i].boardVisible, Players[i].boardWidth);
284 RobotCmd(0, "Opponent %d %s %s\n",
285 i, Players[i].name, Players[i].host);
286 if (Players[i].flags & SCF_usingRobot)
287 RobotCmd(0, "OpponentFlag %d robot\n", i);
288 if (Players[i].flags & SCF_fairRobot)
289 RobotCmd(0, "OpponentFlag %d fairRobot\n", i);
291 RobotCmd(0, "TickLength %.3f\n", Game.speed / 1.0e6);
292 RobotCmd(0, "BeginGame\n");
295 Players[scr].nextShape = ChooseOption(stdOptions);
297 if (Players[scr].alive) {
298 if (!StartNewPiece(scr, ChooseOption(stdOptions))) {
300 Players[scr].alive = 0;
301 FieldMessage(scr, "G A M E O V E R");
303 if (game == GT_classicTwo)
304 SendPacket(scr, NP_argghhh,
305 sizeof(Players[0].alive), &Players[scr].alive);
308 ShowScore(scr, Players[scr].score);
309 if (robotEnable && !fairRobot)
310 RobotCmd(1, "NewPiece %d\n", ++pieceCount);
315 shapeNum = ShapeToNetNum(Players[scr].curShape);
316 data[0] = hton2(shapeNum);
317 SendPacket(scr, NP_newPiece, sizeof(data), data);
322 for (i = 1; i <= totalPlayers; i++) if (Players[i].spy)
323 changed |= RefreshBoard(i);
325 if (!paused) ShowTime();
330 for (i = 1; i <= totalPlayers; i++)
331 if (Players[i].alive) playercount++;
332 if (playercount < game + 1) goto gameOver;
334 switch (WaitMyEvent(&event, EM_any)) {
336 if (Players[scr].alive)
337 if (!MovePiece(scr, -1, 0))
340 SendPacket(scr, NP_down, 0, NULL);
343 p = strchr(keyTable, tolower(event.u.key));
346 RobotCmd(1, "UserKey %d %s\n",
347 (int)(unsigned char)event.u.key,
348 p ? keyNames[key] : "?");
350 } //let robot handle keypress
353 if (!Players[scr].alive && key != KT_quit) break;
354 if (paused && key < KT_pause) break;
357 if (MovePiece(scr, 0, -1) && spied)
358 SendPacket(scr, NP_left, 0, NULL);
361 if (MovePiece(scr, 0, 1) && spied)
362 SendPacket(scr, NP_right, 0, NULL);
365 if (RotatePiece(scr, 0) && spied)
366 SendPacket(scr, NP_rotleft, 0, NULL);
369 if (RotatePiece(scr, 1) && spied)
370 SendPacket(scr, NP_rotright, 0, NULL);
373 SetITimer(Game.speed, Game.speed);
374 if (MovePiece(scr, -1, 0)) {
375 if (spied) SendPacket(scr, NP_down, 0, NULL);
380 SetITimer(Game.speed, Game.speed);
381 if (DropPiece(scr)) {
382 if (spied) SendPacket(scr, NP_drop, 0, NULL);
383 if (Players[scr].dropmode == 2) goto nextPiece;
386 dropMode = Players[scr].dropmode;
389 Players[scr].flags ^= SCF_paused;
391 if (game == GT_classicTwo)
392 SendPacket(scr, NP_pause, 0, NULL);
393 if (robotEnable) RobotCmd(1, "Pause %d\n", paused);
398 if (game != GT_onePlayer)
400 if ((Game.speed /= SPEEDINC) < SPEEDMINIMUM)
401 Game.speed = SPEEDMINIMUM;
402 SetITimer(Game.speed, SetITimer(0, 0));
403 Players[scr].score.level++;
404 ShowScore(scr, Players[scr].score);
408 ScheduleFullRedraw();
413 FieldMessage(me, "G A M E O V E R");
417 if (dropMode && DropPiece(scr) > 0) {
418 SetITimer(Game.speed, Game.speed);
420 SendPacket(scr, NP_drop, 0, NULL);
427 cmd = event.u.robot.data;
428 if ((p = strchr(cmd, ' ')))
431 p = cmd + strlen(cmd);
432 for (key = 0; keyNames[key]; ++key)
433 if (!strcmp(keyNames[key], cmd) &&
434 (fairRobot || (1 == sscanf(p, "%d", &num) &&
437 if (!strcmp(cmd, "Message")) {
444 switch(event.u.net.type) {
450 if (!Players[scr].alive) break;
451 memcpy(data, event.u.net.data, sizeof(data[0]));
452 column = Random(0, Players[scr].boardWidth);
453 data[1] = hton2(column);
454 InsertJunk(scr, ntoh2(data[0]), column);
456 SendPacket(scr, NP_insertJunk, sizeof(data), data);
457 // Message("Opponent added %d lines");
459 } //receive junklines
465 FreezePiece(event.u.net.uid);
466 memcpy(data, event.u.net.data, sizeof(data));
467 shapeNum = ntoh2(data[0]);
468 StartNewPiece(event.u.net.uid, NetNumToShape(shapeNum));
472 MovePiece(event.u.net.uid, -1, 0);
475 MovePiece(event.u.net.uid, 0, -1);
478 MovePiece(event.u.net.uid, 0, 1);
481 RotatePiece(event.u.net.uid, 0);
484 RotatePiece(event.u.net.uid, 1);
487 DropPiece(event.u.net.uid);
490 ClearFullLines(event.u.net.uid);
496 memcpy(data, event.u.net.data, sizeof(data));
497 InsertJunk(event.u.net.uid, ntoh2(data[0]), ntoh2(data[1]));
499 } //player added junklines
502 Players[event.u.net.uid].flags ^= SCF_paused;
504 if (robotEnable) RobotCmd(1, "Pause %d\n", paused);
505 ShowPause(event.u.net.uid);
511 memcpy(&Players[event.u.net.uid].alive,
512 event.u.net.data, sizeof(Players[0].alive));
514 FieldMessage(event.u.net.uid, "G A M E O V E R");
528 if (paused != oldPaused) {
531 pauseTimeLeft = SetITimer(0, 0);
534 SetITimer(Game.speed, pauseTimeLeft);
543 Players[scr].score.drops++;
544 Players[scr].score.score++;
545 if ((linesCleared = ClearFullLines(scr)) > 0) {
546 if (game == GT_onePlayer)
547 if ((Players[scr].score.lines / 10) <
548 ((Players[scr].score.lines + linesCleared) / 10)) {
549 if ((Game.speed /= SPEEDINC) < SPEEDMINIMUM)
550 Game.speed = SPEEDMINIMUM;
551 SetITimer(Game.speed, SetITimer(0, 0));
552 Players[scr].score.level++;
554 Players[scr].score.score += linevalues[linesCleared];
555 Players[scr].score.lines += linesCleared;
556 Players[scr].score.adds += linesCleared - (linesCleared < 4);
558 SendPacket(scr, NP_clear, 0, NULL);
560 if (game == GT_classicTwo && linesCleared > 1) {
564 junkLines = linesCleared - (linesCleared < 4);
565 data[0] = hton2(junkLines);
566 SendPacket(scr, NP_giveJunk, sizeof(data), data);
573 ExtFunc int main(int argc, char **argv)
578 Game.standout = Game.color = 1;
579 Game.initspeed = DEFAULT_INTERVAL;
580 MapKeys(DEFAULT_KEYS);
585 for (i = 0; i <= MAX_SCREENS; i++)
586 Players[i].score.level = Players[i].spy = 1;
587 if (!(userName = getenv("LOGNAME")) || !userName[0])
588 if (!(userName = getenv("USER")) || !userName[0])
589 userName = "Anonymous";
590 strncpy(Players[0].name, userName, 16); //sizeof(Player.name)
591 Players[0].name[16] = 0;
592 Players[0].boardWidth = 10;
593 Players[0].boardHeight = MAX_BOARD_HEIGHT;
594 Players[0].boardVisible = 20;
596 Players[0].alive = 1;
599 // if (getopt(argc, argv, "f:") == 'f')
602 ReadConf(CONFIG_FILE);
603 while ((ch = getopt_long(argc, argv,
604 "hHRr:Fk:c:odDSCap:i:l:t:", options, NULL)) != -1)
605 HandleOption(ch, optarg);
610 if (fairRobot && !robotEnable)
611 fatal("You can't use the -F option without the -r option");
617 game = GT_classicTwo;
618 Players[0].flags |= SCF_paused;
620 InitiateConnection(hostStr, port);
628 memcpy(&Players[me], &Players[0], sizeof(Player));