3d08e9c48f6d8333ff1a3c4b0a3b014515e3b7bc
[pkg/netris.git] / debian / patches / 01_multi-games-with-scoring
1 Index: netris-0.52/curses.c
2 ===================================================================
3 --- netris-0.52.orig/curses.c
4 +++ netris-0.52/curses.c
5 @@ -201,6 +201,8 @@ ExtFunc void InitScreen(int scr)
6         for (y = boardVisible[scr] - 1; y >= 0; --y) {
7                 move(boardYPos[scr] - y, boardXPos[scr] - 1);
8                 addch('|');
9 +               for (x = boardWidth[scr] - 1; x >= 0; --x)
10 +                       addstr("  ");
11                 move(boardYPos[scr] - y, boardXPos[scr] + 2 * boardWidth[scr]);
12                 addch('|');
13         }
14 @@ -256,6 +258,23 @@ ExtFunc void PlotUnderline(int scr, int 
15  
16  ExtFunc void ShowDisplayInfo(void)
17  {
18 +       move(statusYPos - 3, statusXPos);
19 +       printw("Won:  %3d", won);
20 +       move(statusYPos - 2, statusXPos);
21 +       printw("Lost: %3d", lost);
22 +
23 +       move(statusYPos - 1, statusXPos);
24 +       switch(gameState) {
25 +       case STATE_WAIT_CONNECTION:
26 +               addstr("Waiting for opponent...      ");
27 +               break;
28 +       case STATE_WAIT_KEYPRESS:
29 +               addstr("Press the key for a new game.");
30 +               break;
31 +       default:
32 +               addstr("                             ");
33 +       }
34 +
35         move(statusYPos - 9, statusXPos);
36         printw("Seed: %d", initSeed);
37         clrtoeol();
38 Index: netris-0.52/netris.h
39 ===================================================================
40 --- netris-0.52.orig/netris.h
41 +++ netris-0.52/netris.h
42 @@ -65,7 +65,7 @@ typedef long netint4;
43  
44  #define DEFAULT_PORT 9284      /* Very arbitrary */
45  
46 -#define DEFAULT_KEYS "jkl mspf^l"
47 +#define DEFAULT_KEYS "jkl mspf^ln"
48  
49  /* Protocol versions */
50  #define MAJOR_VERSION          1       
51 @@ -152,6 +152,13 @@ typedef struct _ShapeOption {
52  typedef int (*ShapeDrawFunc)(int scr, int y, int x,
53                                         BlockType type, void *data);
54  
55 +enum States {
56 +       STATE_STARTING,
57 +       STATE_PLAYING,
58 +       STATE_WAIT_CONNECTION,
59 +       STATE_WAIT_KEYPRESS
60 +};
61 +
62  EXT GameType game;
63  EXT int boardHeight[MAX_SCREENS];
64  EXT int boardVisible[MAX_SCREENS], boardWidth[MAX_SCREENS];
65 @@ -167,6 +174,9 @@ EXT long stepDownInterval, speed;
66  
67  EXT int myFlags, opponentFlags;
68  
69 +EXT int won, lost;
70 +EXT enum States gameState;
71 +
72  EXT char scratch[1024];
73  
74  extern ShapeOption stdOptions[];
75 Index: netris-0.52/util.c
76 ===================================================================
77 --- netris-0.52.orig/util.c
78 +++ netris-0.52/util.c
79 @@ -74,7 +74,7 @@ ExtFunc void Usage(void)
80           "  -p <port>  Set port number (default is %d)\n"
81           "  -k <keys>  Remap keys.  The argument is a prefix of the string\n"
82           "               containing the keys in order: left, rotate, right, drop,\n"
83 -         "               down-faster, toggle-spying, pause, faster, redraw.\n"
84 +         "               down-faster, toggle-spying, pause, faster, redraw, new.\n"
85           "               \"^\" prefixes controls.  (default is \"%s\")\n"
86           "  -i <sec>   Set the step-down interval, in seconds\n"
87           "  -r <robot> Execute <robot> (a command) as a robot controlling\n"
88 Index: netris-0.52/game.c
89 ===================================================================
90 --- netris-0.52.orig/game.c
91 +++ netris-0.52/game.c
92 @@ -28,11 +28,11 @@
93  #include <netinet/in.h>
94  
95  enum { KT_left, KT_rotate, KT_right, KT_drop, KT_down,
96 -       KT_toggleSpy, KT_pause, KT_faster, KT_redraw, KT_numKeys };
97 +       KT_toggleSpy, KT_pause, KT_faster, KT_redraw, KT_new, KT_numKeys };
98  
99  static char *keyNames[KT_numKeys+1] = {
100         "Left", "Rotate", "Right", "Drop", "Down", "ToggleSpy", "Pause",
101 -       "Faster", "Redraw", NULL };
102 +       "Faster", "Redraw", "New", NULL };
103  
104  static char *gameNames[GT_len] = { "OnePlayer", "ClassicTwo" };
105  
106 @@ -40,6 +40,10 @@ static char keyTable[KT_numKeys+1];
107  static int dropModeEnable = 0;
108  static char *robotProg;
109  
110 +static int wonLast = 0;
111 +int lost = 0, won = 0;
112 +enum States gameState = STATE_STARTING;
113 +
114  ExtFunc void MapKeys(char *newKeys)
115  {
116         int i, k, ch;
117 @@ -323,6 +327,7 @@ ExtFunc void OneGame(int scr, int scr2)
118                                         break;
119                                 case E_lostRobot:
120                                 case E_lostConn:
121 +                                       wonLast = 1;
122                                         goto gameOver;
123                                 default:
124                                         break;
125 @@ -350,14 +355,17 @@ ExtFunc void OneGame(int scr, int scr2)
126                         SendPacket(NP_giveJunk, sizeof(data), data);
127                 }
128         }
129 +       wonLast = 0;
130 +
131  gameOver:
132         SetITimer(0, 0);
133  }
134  
135  ExtFunc int main(int argc, char **argv)
136  {
137 -       int initConn = 0, waitConn = 0, ch;
138 +       int initConn = 0, waitConn = 0, ch, done = 0;
139         char *hostStr = NULL, *portStr = NULL;
140 +       MyEvent event;
141  
142         standoutEnable = colorEnable = 1;
143         stepDownInterval = DEFAULT_INTERVAL;
144 @@ -422,112 +430,139 @@ ExtFunc int main(int argc, char **argv)
145         if (fairRobot && !robotEnable)
146                 fatal("You can't use the -F option without the -r option");
147         InitUtil();
148 -       if (robotEnable)
149 -               InitRobot(robotProg);
150 -       InitNet();
151         InitScreens();
152 -       if (initConn || waitConn) {
153 -               MyEvent event;
154 -
155 -               game = GT_classicTwo;
156 -               if (initConn)
157 -                       InitiateConnection(hostStr, portStr);
158 -               else if (waitConn)
159 -                       WaitForConnection(portStr);
160 -               {
161 -                       netint4 data[2];
162 -                       int major;
163 -
164 -                       data[0] = hton4(MAJOR_VERSION);
165 -                       data[1] = hton4(PROTOCOL_VERSION);
166 -                       SendPacket(NP_version, sizeof(data), data);
167 -                       if (WaitMyEvent(&event, EM_net) != E_net)
168 -                               fatal("Network negotiation failed");
169 -                       memcpy(data, event.u.net.data, sizeof(data));
170 -                       major = ntoh4(data[0]);
171 -                       protocolVersion = ntoh4(data[1]);
172 -                       if (event.u.net.type != NP_version || major < MAJOR_VERSION)
173 -                               fatal("Your opponent is using an old, incompatible version\n"
174 -                                       "of Netris.  They should get the latest version.");
175 -                       if (major > MAJOR_VERSION)
176 -                               fatal("Your opponent is using an newer, incompatible version\n"
177 -                                       "of Netris.  Get the latest version.");
178 -                       if (protocolVersion > PROTOCOL_VERSION)
179 -                               protocolVersion = PROTOCOL_VERSION;
180 -               }
181 -               if (protocolVersion < 3 && stepDownInterval != DEFAULT_INTERVAL)
182 -                       fatal("Your opponent's version of Netris predates the -i option.\n"
183 -                                       "For fairness, you shouldn't use the -i option either.");
184 -               {
185 -                       netint4 data[3];
186 -                       int len;
187 -                       int seed;
188 +       while(!done) {
189 +               if (robotEnable)
190 +                       InitRobot(robotProg);
191 +               InitNet();
192 +               if (!initSeed)
193 +                       SRandom(time(0));
194 +               if (initConn || waitConn) {
195 +                       game = GT_classicTwo;
196 +                       if(gameState != STATE_STARTING) {
197 +                               gameState = STATE_WAIT_CONNECTION;
198 +                               ShowDisplayInfo();
199 +                               RefreshScreen();
200 +                       }
201 +                       if (initConn)
202 +                               InitiateConnection(hostStr, portStr);
203 +                       else if (waitConn)
204 +                               WaitForConnection(portStr);
205 +                       gameState = STATE_PLAYING;
206 +                       ShowDisplayInfo();
207 +                       RefreshScreen();
208 +                       {
209 +                               netint4 data[2];
210 +                               int major;
211 +
212 +                               data[0] = hton4(MAJOR_VERSION);
213 +                               data[1] = hton4(PROTOCOL_VERSION);
214 +                               SendPacket(NP_version, sizeof(data), data);
215 +                               if (WaitMyEvent(&event, EM_net) != E_net)
216 +                                       fatal("Network negotiation failed");
217 +                               memcpy(data, event.u.net.data, sizeof(data));
218 +                               major = ntoh4(data[0]);
219 +                               protocolVersion = ntoh4(data[1]);
220 +                               if (event.u.net.type != NP_version || major < MAJOR_VERSION)
221 +                                       fatal("Your opponent is using an old, incompatible version\n"
222 +                                             "of Netris.  They should get the latest version.");
223 +                               if (major > MAJOR_VERSION)
224 +                                       fatal("Your opponent is using an newer, incompatible version\n"
225 +                                             "of Netris.  Get the latest version.");
226 +                               if (protocolVersion > PROTOCOL_VERSION)
227 +                                       protocolVersion = PROTOCOL_VERSION;
228 +                       }
229 +                       if (protocolVersion < 3 && stepDownInterval != DEFAULT_INTERVAL)
230 +                               fatal("Your opponent's version of Netris predates the -i option.\n"
231 +                                     "For fairness, you shouldn't use the -i option either.");
232 +                       {
233 +                               netint4 data[3];
234 +                               int len;
235 +                               int seed;
236  
237 -                       if (protocolVersion >= 3)
238 -                               len = sizeof(data);
239 -                       else
240 -                               len = sizeof(netint4[2]);
241 -                       if ((myFlags & SCF_setSeed))
242 -                               seed = initSeed;
243 -                       else
244 -                               seed = time(0);
245 -                       if (waitConn)
246 -                               SRandom(seed);
247 -                       data[0] = hton4(myFlags);
248 -                       data[1] = hton4(seed);
249 -                       data[2] = hton4(stepDownInterval);
250 -                       SendPacket(NP_startConn, len, data);
251 -                       if (WaitMyEvent(&event, EM_net) != E_net ||
252 -                                       event.u.net.type != NP_startConn)
253 -                               fatal("Network negotiation failed");
254 -                       memcpy(data, event.u.net.data, len);
255 -                       opponentFlags = ntoh4(data[0]);
256 -                       seed = ntoh4(data[1]);
257 -                       if (initConn) {
258 -                               if ((opponentFlags & SCF_setSeed) != (myFlags & SCF_setSeed))
259 -                                       fatal("If one player sets the random number seed, "
260 -                                                       "both must.");
261 -                               if ((myFlags & SCF_setSeed) && seed != initSeed)
262 -                                       fatal("Both players have set the random number seed, "
263 -                                                       "and they are unequal.");
264 -                               if (protocolVersion >= 3 && stepDownInterval != ntoh4(data[2]))
265 -                                       fatal("Your opponent is using a different step-down "
266 -                                               "interval (-i).\nYou must both use the same one.");
267 -                               SRandom(seed);
268 +                               if (protocolVersion >= 3)
269 +                                       len = sizeof(data);
270 +                               else
271 +                                       len = sizeof(netint4[2]);
272 +                               if ((myFlags & SCF_setSeed))
273 +                                       seed = initSeed;
274 +                               else
275 +                                       seed = time(0);
276 +                               if (waitConn)
277 +                                       SRandom(seed);
278 +                               data[0] = hton4(myFlags);
279 +                               data[1] = hton4(seed);
280 +                               data[2] = hton4(stepDownInterval);
281 +                               SendPacket(NP_startConn, len, data);
282 +                               if (WaitMyEvent(&event, EM_net) != E_net ||
283 +                                   event.u.net.type != NP_startConn)
284 +                                       fatal("Network negotiation failed");
285 +                               memcpy(data, event.u.net.data, len);
286 +                               opponentFlags = ntoh4(data[0]);
287 +                               seed = ntoh4(data[1]);
288 +                               if (initConn) {
289 +                                       if ((opponentFlags & SCF_setSeed) != (myFlags & SCF_setSeed))
290 +                                               fatal("If one player sets the random number seed, "
291 +                                                     "both must.");
292 +                                       if ((myFlags & SCF_setSeed) && seed != initSeed)
293 +                                               fatal("Both players have set the random number seed, "
294 +                                                     "and they are unequal.");
295 +                                       if (protocolVersion >= 3 && stepDownInterval != ntoh4(data[2]))
296 +                                               fatal("Your opponent is using a different step-down "
297 +                                                     "interval (-i).\nYou must both use the same one.");
298 +                                       SRandom(seed);
299 +                               }
300 +                       }
301 +                       {
302 +                               char *userName;
303 +                               int len, i;
304 +
305 +                               userName = getenv("LOGNAME");
306 +                               if (!userName || !userName[0])
307 +                                       userName = getenv("USER");
308 +                               if (!userName || !userName[0])
309 +                                       strcpy(userName, "???");
310 +                               len = strlen(userName)+1;
311 +                               if (len > sizeof(opponentName))
312 +                                       len = sizeof(opponentName);
313 +                               SendPacket(NP_userName, len, userName);
314 +                               if (WaitMyEvent(&event, EM_net) != E_net ||
315 +                                   event.u.net.type != NP_userName)
316 +                                       fatal("Network negotiation failed");
317 +                               strncpy(opponentName, event.u.net.data,
318 +                                       sizeof(opponentName)-1);
319 +                               opponentName[sizeof(opponentName)-1] = 0;
320 +                               for (i = 0; opponentName[i]; ++i)
321 +                                       if (!isprint(opponentName[i]))
322 +                                               opponentName[i] = '?';
323 +                               for (i = 0; opponentHost[i]; ++i)
324 +                                       if (!isprint(opponentHost[i]))
325 +                                               opponentHost[i] = '?';
326                         }
327 +                       OneGame(0, 1);
328                 }
329 -               {
330 -                       char *userName;
331 -                       int len, i;
332 -
333 -                       userName = getenv("LOGNAME");
334 -                       if (!userName || !userName[0])
335 -                               userName = getenv("USER");
336 -                       if (!userName || !userName[0])
337 -                               strcpy(userName, "???");
338 -                       len = strlen(userName)+1;
339 -                       if (len > sizeof(opponentName))
340 -                               len = sizeof(opponentName);
341 -                       SendPacket(NP_userName, len, userName);
342 -                       if (WaitMyEvent(&event, EM_net) != E_net ||
343 -                                       event.u.net.type != NP_userName)
344 -                               fatal("Network negotiation failed");
345 -                       strncpy(opponentName, event.u.net.data,
346 -                               sizeof(opponentName)-1);
347 -                       opponentName[sizeof(opponentName)-1] = 0;
348 -                       for (i = 0; opponentName[i]; ++i)
349 -                               if (!isprint(opponentName[i]))
350 -                                       opponentName[i] = '?';
351 -                       for (i = 0; opponentHost[i]; ++i)
352 -                               if (!isprint(opponentHost[i]))
353 -                                       opponentHost[i] = '?';
354 +               else {
355 +                       game = GT_onePlayer;
356 +                       OneGame(0, -1);
357 +               }
358 +               if (wonLast) {
359 +                       won++;
360 +               } else {
361 +                       lost++;
362 +                       WaitMyEvent(&event, EM_net);
363 +               }
364 +               CloseNet();
365 +               if (robotEnable) {
366 +                       CloseRobot();
367 +               } else {
368 +                       gameState = STATE_WAIT_KEYPRESS;
369 +                       ShowDisplayInfo();
370 +                       RefreshScreen();
371 +                       while(getchar() != keyTable[KT_new])
372 +                               ;
373                 }
374 -               OneGame(0, 1);
375 -       }
376 -       else {
377 -               game = GT_onePlayer;
378 -               OneGame(0, -1);
379         }
380 +
381         return 0;
382  }
383  
384 Index: netris-0.52/board.c
385 ===================================================================
386 --- netris-0.52.orig/board.c
387 +++ netris-0.52/board.c
388 @@ -36,6 +36,18 @@ static int oldFalling[MAX_SCREENS][MAX_B
389  
390  ExtFunc void InitBoard(int scr)
391  {
392 +       int s,w,h;
393 +
394 +       for(s = 0 ; s < MAX_SCREENS ; s++)
395 +               for(h = 0 ; h < MAX_BOARD_HEIGHT ; h++)
396 +                       for(w = 0 ; w < MAX_BOARD_WIDTH ; w++) {
397 +                               board[s][h][w] = 0;
398 +                               oldBoard[s][h][w] = 0;
399 +                               changed[s][h] = 0;
400 +                               falling[s][w] = 0;
401 +                               oldFalling[s][w] = 0;
402 +                       }
403 +
404         boardHeight[scr] = MAX_BOARD_HEIGHT;
405         boardVisible[scr] = 20;
406         boardWidth[scr] = 10;