Imported Debian patch 0.52-8
[pkg/netris.git] / debian / patches / 01_multi-games-with-scoring
1 Author: Tomas Berndtsson <tomas@nocrew.org>     vim:ft=diff:
2 Description: Add multi game support with scoring
3
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);
11                 addch('|');
12 +               for (x = boardWidth[scr] - 1; x >= 0; --x)
13 +                       addstr("  ");
14                 move(boardYPos[scr] - y, boardXPos[scr] + 2 * boardWidth[scr]);
15                 addch('|');
16         }
17 @@ -256,6 +258,23 @@ ExtFunc void PlotUnderline(int scr, int
18  
19  ExtFunc void ShowDisplayInfo(void)
20  {
21 +       move(statusYPos - 3, statusXPos);
22 +       printw("Won:  %3d", won);
23 +       move(statusYPos - 2, statusXPos);
24 +       printw("Lost: %3d", lost);
25 +
26 +       move(statusYPos - 1, statusXPos);
27 +       switch(gameState) {
28 +       case STATE_WAIT_CONNECTION:
29 +               addstr("Waiting for opponent...      ");
30 +               break;
31 +       case STATE_WAIT_KEYPRESS:
32 +               addstr("Press the key for a new game.");
33 +               break;
34 +       default:
35 +               addstr("                             ");
36 +       }
37 +
38         move(statusYPos - 9, statusXPos);
39         printw("Seed: %d", initSeed);
40         clrtoeol();
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;
46  
47  #define DEFAULT_PORT 9284      /* Very arbitrary */
48  
49 -#define DEFAULT_KEYS "jkl mspf^l"
50 +#define DEFAULT_KEYS "jkl mspf^ln"
51  
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);
57  
58 +enum States {
59 +       STATE_STARTING,
60 +       STATE_PLAYING,
61 +       STATE_WAIT_CONNECTION,
62 +       STATE_WAIT_KEYPRESS
63 +};
64 +
65  EXT GameType game;
66  EXT int boardHeight[MAX_SCREENS];
67  EXT int boardVisible[MAX_SCREENS], boardWidth[MAX_SCREENS];
68 @@ -167,6 +174,9 @@ EXT long stepDownInterval, speed;
69  
70  EXT int myFlags, opponentFlags;
71  
72 +EXT int won, lost;
73 +EXT enum States gameState;
74 +
75  EXT char scratch[1024];
76  
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
95 @@ -28,11 +28,11 @@
96  #include <netinet/in.h>
97  
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 };
101  
102  static char *keyNames[KT_numKeys+1] = {
103         "Left", "Rotate", "Right", "Drop", "Down", "ToggleSpy", "Pause",
104 -       "Faster", "Redraw", NULL };
105 +       "Faster", "Redraw", "New", NULL };
106  
107  static char *gameNames[GT_len] = { "OnePlayer", "ClassicTwo" };
108  
109 @@ -40,6 +40,10 @@ static char keyTable[KT_numKeys+1];
110  static int dropModeEnable = 0;
111  static char *robotProg;
112  
113 +static int wonLast = 0;
114 +int lost = 0, won = 0;
115 +enum States gameState = STATE_STARTING;
116 +
117  ExtFunc void MapKeys(char *newKeys)
118  {
119         int i, k, ch;
120 @@ -323,6 +327,7 @@ ExtFunc void OneGame(int scr, int scr2)
121                                         break;
122                                 case E_lostRobot:
123                                 case E_lostConn:
124 +                                       wonLast = 1;
125                                         goto gameOver;
126                                 default:
127                                         break;
128 @@ -350,14 +355,17 @@ ExtFunc void OneGame(int scr, int scr2)
129                         SendPacket(NP_giveJunk, sizeof(data), data);
130                 }
131         }
132 +       wonLast = 0;
133 +
134  gameOver:
135         SetITimer(0, 0);
136  }
137  
138  ExtFunc int main(int argc, char **argv)
139  {
140 -       int initConn = 0, waitConn = 0, ch;
141 +       int initConn = 0, waitConn = 0, ch, done = 0;
142         char *hostStr = NULL, *portStr = NULL;
143 +       MyEvent event;
144  
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");
150         InitUtil();
151 -       if (robotEnable)
152 -               InitRobot(robotProg);
153 -       InitNet();
154         InitScreens();
155 -       if (initConn || waitConn) {
156 -               MyEvent event;
157 -
158 -               game = GT_classicTwo;
159 -               if (initConn)
160 -                       InitiateConnection(hostStr, portStr);
161 -               else if (waitConn)
162 -                       WaitForConnection(portStr);
163 -               {
164 -                       netint4 data[2];
165 -                       int major;
166 -
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;
183 -               }
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.");
187 -               {
188 -                       netint4 data[3];
189 -                       int len;
190 -                       int seed;
191 +       while(!done) {
192 +               if (robotEnable)
193 +                       InitRobot(robotProg);
194 +               InitNet();
195 +               if (!initSeed)
196 +                       SRandom(time(0));
197 +               if (initConn || waitConn) {
198 +                       game = GT_classicTwo;
199 +                       if(gameState != STATE_STARTING) {
200 +                               gameState = STATE_WAIT_CONNECTION;
201 +                               ShowDisplayInfo();
202 +                               RefreshScreen();
203 +                       }
204 +                       if (initConn)
205 +                               InitiateConnection(hostStr, portStr);
206 +                       else if (waitConn)
207 +                               WaitForConnection(portStr);
208 +                       gameState = STATE_PLAYING;
209 +                       ShowDisplayInfo();
210 +                       RefreshScreen();
211 +                       {
212 +                               netint4 data[2];
213 +                               int major;
214 +
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;
231 +                       }
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.");
235 +                       {
236 +                               netint4 data[3];
237 +                               int len;
238 +                               int seed;
239  
240 -                       if (protocolVersion >= 3)
241 -                               len = sizeof(data);
242 -                       else
243 -                               len = sizeof(netint4[2]);
244 -                       if ((myFlags & SCF_setSeed))
245 -                               seed = initSeed;
246 -                       else
247 -                               seed = time(0);
248 -                       if (waitConn)
249 -                               SRandom(seed);
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]);
260 -                       if (initConn) {
261 -                               if ((opponentFlags & SCF_setSeed) != (myFlags & SCF_setSeed))
262 -                                       fatal("If one player sets the random number seed, "
263 -                                                       "both must.");
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.");
270 -                               SRandom(seed);
271 +                               if (protocolVersion >= 3)
272 +                                       len = sizeof(data);
273 +                               else
274 +                                       len = sizeof(netint4[2]);
275 +                               if ((myFlags & SCF_setSeed))
276 +                                       seed = initSeed;
277 +                               else
278 +                                       seed = time(0);
279 +                               if (waitConn)
280 +                                       SRandom(seed);
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]);
291 +                               if (initConn) {
292 +                                       if ((opponentFlags & SCF_setSeed) != (myFlags & SCF_setSeed))
293 +                                               fatal("If one player sets the random number seed, "
294 +                                                     "both must.");
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.");
301 +                                       SRandom(seed);
302 +                               }
303 +                       }
304 +                       {
305 +                               char *userName;
306 +                               int len, i;
307 +
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] = '?';
329                         }
330 +                       OneGame(0, 1);
331                 }
332 -               {
333 -                       char *userName;
334 -                       int len, i;
335 -
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] = '?';
357 +               else {
358 +                       game = GT_onePlayer;
359 +                       OneGame(0, -1);
360 +               }
361 +               if (wonLast) {
362 +                       won++;
363 +               } else {
364 +                       lost++;
365 +                       WaitMyEvent(&event, EM_net);
366 +               }
367 +               CloseNet();
368 +               if (robotEnable) {
369 +                       CloseRobot();
370 +               } else {
371 +                       gameState = STATE_WAIT_KEYPRESS;
372 +                       ShowDisplayInfo();
373 +                       RefreshScreen();
374 +                       while(getchar() != keyTable[KT_new])
375 +                               ;
376                 }
377 -               OneGame(0, 1);
378 -       }
379 -       else {
380 -               game = GT_onePlayer;
381 -               OneGame(0, -1);
382         }
383 +
384         return 0;
385  }
386  
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
392  
393  ExtFunc void InitBoard(int scr)
394  {
395 +       int s,w,h;
396 +
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;
402 +                               changed[s][h] = 0;
403 +                               falling[s][w] = 0;
404 +                               oldFalling[s][w] = 0;
405 +                       }
406 +
407         boardHeight[scr] = MAX_BOARD_HEIGHT;
408         boardVisible[scr] = 20;
409         boardWidth[scr] = 10;