1 Author: Tomas Berndtsson <tomas@nocrew.org> vim:ft=diff:
2 Description: Add multi game support with scoring
4 Index: netris-0.52/curses.c
5 ===================================================================
6 --- netris-0.52.orig/curses.c
7 +++ netris-0.52/curses.c
8 @@ -201,6 +201,8 @@ ExtFunc void InitScreen(int scr)
9 for (y = boardVisible[scr] - 1; y >= 0; --y) {
10 move(boardYPos[scr] - y, boardXPos[scr] - 1);
12 + for (x = boardWidth[scr] - 1; x >= 0; --x)
14 move(boardYPos[scr] - y, boardXPos[scr] + 2 * boardWidth[scr]);
17 @@ -256,6 +258,23 @@ ExtFunc void PlotUnderline(int scr, int
19 ExtFunc void ShowDisplayInfo(void)
21 + move(statusYPos - 3, statusXPos);
22 + printw("Won: %3d", won);
23 + move(statusYPos - 2, statusXPos);
24 + printw("Lost: %3d", lost);
26 + move(statusYPos - 1, statusXPos);
28 + case STATE_WAIT_CONNECTION:
29 + addstr("Waiting for opponent... ");
31 + case STATE_WAIT_KEYPRESS:
32 + addstr("Press the key for a new game.");
38 move(statusYPos - 9, statusXPos);
39 printw("Seed: %d", initSeed);
41 Index: netris-0.52/netris.h
42 ===================================================================
43 --- netris-0.52.orig/netris.h
44 +++ netris-0.52/netris.h
45 @@ -65,7 +65,7 @@ typedef long netint4;
47 #define DEFAULT_PORT 9284 /* Very arbitrary */
49 -#define DEFAULT_KEYS "jkl mspf^l"
50 +#define DEFAULT_KEYS "jkl mspf^ln"
52 /* Protocol versions */
53 #define MAJOR_VERSION 1
54 @@ -152,6 +152,13 @@ typedef struct _ShapeOption {
55 typedef int (*ShapeDrawFunc)(int scr, int y, int x,
56 BlockType type, void *data);
61 + STATE_WAIT_CONNECTION,
66 EXT int boardHeight[MAX_SCREENS];
67 EXT int boardVisible[MAX_SCREENS], boardWidth[MAX_SCREENS];
68 @@ -167,6 +174,9 @@ EXT long stepDownInterval, speed;
70 EXT int myFlags, opponentFlags;
73 +EXT enum States gameState;
75 EXT char scratch[1024];
77 extern ShapeOption stdOptions[];
78 Index: netris-0.52/util.c
79 ===================================================================
80 --- netris-0.52.orig/util.c
81 +++ netris-0.52/util.c
82 @@ -74,7 +74,7 @@ ExtFunc void Usage(void)
83 " -p <port> Set port number (default is %d)\n"
84 " -k <keys> Remap keys. The argument is a prefix of the string\n"
85 " containing the keys in order: left, rotate, right, drop,\n"
86 - " down-faster, toggle-spying, pause, faster, redraw.\n"
87 + " down-faster, toggle-spying, pause, faster, redraw, new.\n"
88 " \"^\" prefixes controls. (default is \"%s\")\n"
89 " -i <sec> Set the step-down interval, in seconds\n"
90 " -r <robot> Execute <robot> (a command) as a robot controlling\n"
91 Index: netris-0.52/game.c
92 ===================================================================
93 --- netris-0.52.orig/game.c
94 +++ netris-0.52/game.c
96 #include <netinet/in.h>
98 enum { KT_left, KT_rotate, KT_right, KT_drop, KT_down,
99 - KT_toggleSpy, KT_pause, KT_faster, KT_redraw, KT_numKeys };
100 + KT_toggleSpy, KT_pause, KT_faster, KT_redraw, KT_new, KT_numKeys };
102 static char *keyNames[KT_numKeys+1] = {
103 "Left", "Rotate", "Right", "Drop", "Down", "ToggleSpy", "Pause",
104 - "Faster", "Redraw", NULL };
105 + "Faster", "Redraw", "New", NULL };
107 static char *gameNames[GT_len] = { "OnePlayer", "ClassicTwo" };
109 @@ -40,6 +40,10 @@ static char keyTable[KT_numKeys+1];
110 static int dropModeEnable = 0;
111 static char *robotProg;
113 +static int wonLast = 0;
114 +int lost = 0, won = 0;
115 +enum States gameState = STATE_STARTING;
117 ExtFunc void MapKeys(char *newKeys)
120 @@ -323,6 +327,7 @@ ExtFunc void OneGame(int scr, int scr2)
128 @@ -350,14 +355,17 @@ ExtFunc void OneGame(int scr, int scr2)
129 SendPacket(NP_giveJunk, sizeof(data), data);
138 ExtFunc int main(int argc, char **argv)
140 - int initConn = 0, waitConn = 0, ch;
141 + int initConn = 0, waitConn = 0, ch, done = 0;
142 char *hostStr = NULL, *portStr = NULL;
145 standoutEnable = colorEnable = 1;
146 stepDownInterval = DEFAULT_INTERVAL;
147 @@ -422,112 +430,139 @@ ExtFunc int main(int argc, char **argv)
148 if (fairRobot && !robotEnable)
149 fatal("You can't use the -F option without the -r option");
152 - InitRobot(robotProg);
155 - if (initConn || waitConn) {
158 - game = GT_classicTwo;
160 - InitiateConnection(hostStr, portStr);
162 - WaitForConnection(portStr);
167 - data[0] = hton4(MAJOR_VERSION);
168 - data[1] = hton4(PROTOCOL_VERSION);
169 - SendPacket(NP_version, sizeof(data), data);
170 - if (WaitMyEvent(&event, EM_net) != E_net)
171 - fatal("Network negotiation failed");
172 - memcpy(data, event.u.net.data, sizeof(data));
173 - major = ntoh4(data[0]);
174 - protocolVersion = ntoh4(data[1]);
175 - if (event.u.net.type != NP_version || major < MAJOR_VERSION)
176 - fatal("Your opponent is using an old, incompatible version\n"
177 - "of Netris. They should get the latest version.");
178 - if (major > MAJOR_VERSION)
179 - fatal("Your opponent is using an newer, incompatible version\n"
180 - "of Netris. Get the latest version.");
181 - if (protocolVersion > PROTOCOL_VERSION)
182 - protocolVersion = PROTOCOL_VERSION;
184 - if (protocolVersion < 3 && stepDownInterval != DEFAULT_INTERVAL)
185 - fatal("Your opponent's version of Netris predates the -i option.\n"
186 - "For fairness, you shouldn't use the -i option either.");
193 + InitRobot(robotProg);
197 + if (initConn || waitConn) {
198 + game = GT_classicTwo;
199 + if(gameState != STATE_STARTING) {
200 + gameState = STATE_WAIT_CONNECTION;
205 + InitiateConnection(hostStr, portStr);
207 + WaitForConnection(portStr);
208 + gameState = STATE_PLAYING;
215 + data[0] = hton4(MAJOR_VERSION);
216 + data[1] = hton4(PROTOCOL_VERSION);
217 + SendPacket(NP_version, sizeof(data), data);
218 + if (WaitMyEvent(&event, EM_net) != E_net)
219 + fatal("Network negotiation failed");
220 + memcpy(data, event.u.net.data, sizeof(data));
221 + major = ntoh4(data[0]);
222 + protocolVersion = ntoh4(data[1]);
223 + if (event.u.net.type != NP_version || major < MAJOR_VERSION)
224 + fatal("Your opponent is using an old, incompatible version\n"
225 + "of Netris. They should get the latest version.");
226 + if (major > MAJOR_VERSION)
227 + fatal("Your opponent is using an newer, incompatible version\n"
228 + "of Netris. Get the latest version.");
229 + if (protocolVersion > PROTOCOL_VERSION)
230 + protocolVersion = PROTOCOL_VERSION;
232 + if (protocolVersion < 3 && stepDownInterval != DEFAULT_INTERVAL)
233 + fatal("Your opponent's version of Netris predates the -i option.\n"
234 + "For fairness, you shouldn't use the -i option either.");
240 - if (protocolVersion >= 3)
241 - len = sizeof(data);
243 - len = sizeof(netint4[2]);
244 - if ((myFlags & SCF_setSeed))
250 - data[0] = hton4(myFlags);
251 - data[1] = hton4(seed);
252 - data[2] = hton4(stepDownInterval);
253 - SendPacket(NP_startConn, len, data);
254 - if (WaitMyEvent(&event, EM_net) != E_net ||
255 - event.u.net.type != NP_startConn)
256 - fatal("Network negotiation failed");
257 - memcpy(data, event.u.net.data, len);
258 - opponentFlags = ntoh4(data[0]);
259 - seed = ntoh4(data[1]);
261 - if ((opponentFlags & SCF_setSeed) != (myFlags & SCF_setSeed))
262 - fatal("If one player sets the random number seed, "
264 - if ((myFlags & SCF_setSeed) && seed != initSeed)
265 - fatal("Both players have set the random number seed, "
266 - "and they are unequal.");
267 - if (protocolVersion >= 3 && stepDownInterval != ntoh4(data[2]))
268 - fatal("Your opponent is using a different step-down "
269 - "interval (-i).\nYou must both use the same one.");
271 + if (protocolVersion >= 3)
272 + len = sizeof(data);
274 + len = sizeof(netint4[2]);
275 + if ((myFlags & SCF_setSeed))
281 + data[0] = hton4(myFlags);
282 + data[1] = hton4(seed);
283 + data[2] = hton4(stepDownInterval);
284 + SendPacket(NP_startConn, len, data);
285 + if (WaitMyEvent(&event, EM_net) != E_net ||
286 + event.u.net.type != NP_startConn)
287 + fatal("Network negotiation failed");
288 + memcpy(data, event.u.net.data, len);
289 + opponentFlags = ntoh4(data[0]);
290 + seed = ntoh4(data[1]);
292 + if ((opponentFlags & SCF_setSeed) != (myFlags & SCF_setSeed))
293 + fatal("If one player sets the random number seed, "
295 + if ((myFlags & SCF_setSeed) && seed != initSeed)
296 + fatal("Both players have set the random number seed, "
297 + "and they are unequal.");
298 + if (protocolVersion >= 3 && stepDownInterval != ntoh4(data[2]))
299 + fatal("Your opponent is using a different step-down "
300 + "interval (-i).\nYou must both use the same one.");
308 + userName = getenv("LOGNAME");
309 + if (!userName || !userName[0])
310 + userName = getenv("USER");
311 + if (!userName || !userName[0])
312 + strcpy(userName, "???");
313 + len = strlen(userName)+1;
314 + if (len > sizeof(opponentName))
315 + len = sizeof(opponentName);
316 + SendPacket(NP_userName, len, userName);
317 + if (WaitMyEvent(&event, EM_net) != E_net ||
318 + event.u.net.type != NP_userName)
319 + fatal("Network negotiation failed");
320 + strncpy(opponentName, event.u.net.data,
321 + sizeof(opponentName)-1);
322 + opponentName[sizeof(opponentName)-1] = 0;
323 + for (i = 0; opponentName[i]; ++i)
324 + if (!isprint(opponentName[i]))
325 + opponentName[i] = '?';
326 + for (i = 0; opponentHost[i]; ++i)
327 + if (!isprint(opponentHost[i]))
328 + opponentHost[i] = '?';
336 - userName = getenv("LOGNAME");
337 - if (!userName || !userName[0])
338 - userName = getenv("USER");
339 - if (!userName || !userName[0])
340 - strcpy(userName, "???");
341 - len = strlen(userName)+1;
342 - if (len > sizeof(opponentName))
343 - len = sizeof(opponentName);
344 - SendPacket(NP_userName, len, userName);
345 - if (WaitMyEvent(&event, EM_net) != E_net ||
346 - event.u.net.type != NP_userName)
347 - fatal("Network negotiation failed");
348 - strncpy(opponentName, event.u.net.data,
349 - sizeof(opponentName)-1);
350 - opponentName[sizeof(opponentName)-1] = 0;
351 - for (i = 0; opponentName[i]; ++i)
352 - if (!isprint(opponentName[i]))
353 - opponentName[i] = '?';
354 - for (i = 0; opponentHost[i]; ++i)
355 - if (!isprint(opponentHost[i]))
356 - opponentHost[i] = '?';
358 + game = GT_onePlayer;
365 + WaitMyEvent(&event, EM_net);
371 + gameState = STATE_WAIT_KEYPRESS;
374 + while(getchar() != keyTable[KT_new])
380 - game = GT_onePlayer;
387 Index: netris-0.52/board.c
388 ===================================================================
389 --- netris-0.52.orig/board.c
390 +++ netris-0.52/board.c
391 @@ -36,6 +36,18 @@ static int oldFalling[MAX_SCREENS][MAX_B
393 ExtFunc void InitBoard(int scr)
397 + for(s = 0 ; s < MAX_SCREENS ; s++)
398 + for(h = 0 ; h < MAX_BOARD_HEIGHT ; h++)
399 + for(w = 0 ; w < MAX_BOARD_WIDTH ; w++) {
400 + board[s][h][w] = 0;
401 + oldBoard[s][h][w] = 0;
404 + oldFalling[s][w] = 0;
407 boardHeight[scr] = MAX_BOARD_HEIGHT;
408 boardVisible[scr] = 20;
409 boardWidth[scr] = 10;