confirmed quadra-compliant behaviour
[netris.git] / board.c
1 /*
2  * Netris -- A free networked version of T*tris
3  * Copyright (C) 1994-1996,1999  Mark H. Weaver <mhw@netris.org>
4  * 
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.
9  * 
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.
14  * 
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.
18  */
19
20 #include "netris.h"
21 #include <stdlib.h>
22
23 #include "board.h"
24
25 static const char shapes[7][4][4][4] = {
26         /*
27          * 4 rotations of 4x4 pixels per shape
28          * high nibble signifies joinage (left, right, top, bottom, from MSB)
29          * low nibble identifies block type (typically 2..8)
30          */
31         
32         // J
33         { { {   0,    0,    0, 0000},
34             {0x47, 0xC7, 0x97, 0000},
35             {   0,    0, 0x27, 0000},
36             {0000, 0000, 0000, 0000} },
37           { {   0, 0x17,    0, 0000},
38             {   0, 0x37,    0, 0000},
39             {0x47, 0xA7,    0, 0000},
40             {0000, 0000, 0000, 0000} },
41           { {0x17,    0,    0, 0000},
42             {0x67, 0xC7, 0x87, 0000},
43             {   0,    0,    0, 0000},
44             {0000, 0000, 0000, 0000} },
45           { {   0, 0x57, 0x87, 0000},
46             {   0, 0x37,    0, 0000},
47             {   0, 0x27,    0, 0000},
48             {0000, 0000, 0000, 0000} } },
49         
50         // L
51         { { {   0,    0,    0, 0000},
52             {0x53, 0xC3, 0x83, 0000},
53             {0x23,    0,    0, 0000},
54             {0000, 0000, 0000, 0000} },
55           { {0x43, 0x93,    0, 0000},
56             {   0, 0x33,    0, 0000},
57             {   0, 0x23,    0, 0000},
58             {0000, 0000, 0000, 0000} },
59           { {   0,    0, 0x13, 0000},
60             {0x43, 0xC3, 0xA3, 0000},
61             {   0,    0,    0, 0000},
62             {0000, 0000, 0000, 0000} },
63           { {   0, 0x13,    0, 0000},
64             {   0, 0x33,    0, 0000},
65             {   0, 0x63, 0x83, 0000},
66             {0000, 0000, 0000, 0000} } },
67         
68         // T
69         { { {   0,    0,    0, 0000},
70             {0x48, 0xD8, 0x88, 0000},
71             {   0, 0x28,    0, 0000},
72             {0000, 0000, 0000, 0000} },
73           { {   0, 0x18,    0, 0000},
74             {0x48, 0xB8,    0, 0000},
75             {   0, 0x28,    0, 0000},
76             {0000, 0000, 0000, 0000} },
77           { {   0, 0x18,    0, 0000},
78             {0x48, 0xE8, 0x88, 0000},
79             {   0,    0,    0, 0000},
80             {0000, 0000, 0000, 0000} },
81           { {   0, 0x18,    0, 0000},
82             {   0, 0x78, 0x88, 0000},
83             {   0, 0x28,    0, 0000},
84             {0000, 0000, 0000, 0000} } },
85         
86         // S
87         { { {   0,    0,    0, 0000},
88             {   0, 0x52, 0x82, 0000},
89             {0x42, 0xA2,    0, 0000},
90             {0000, 0000, 0000, 0000} },
91           { {0x12,    0,    0, 0000},
92             {0x62, 0x92,    0, 0000},
93             {   0, 0x22,    0, 0000},
94             {0000, 0000, 0000, 0000} },
95           { {   0,    0,    0, 0000},
96             {   0, 0x52, 0x82, 0000},
97             {0x42, 0xA2,    0, 0000},
98             {0000, 0000, 0000, 0000} },
99           { {0x12,    0,    0, 0000},
100             {0x62, 0x92,    0, 0000},
101             {   0, 0x22,    0, 0000},
102             {0000, 0000, 0000, 0000} } },
103         
104         // Z
105         { { {   0,    0,    0, 0000},
106             {0x46, 0x96,    0, 0000},
107             {   0, 0x66, 0x86, 0000},
108             {0000, 0000, 0000, 0000} },
109           { {   0, 0x16,    0, 0000},
110             {0x56, 0xA6,    0, 0000},
111             {0x26,    0,    0, 0000},
112             {0000, 0000, 0000, 0000} },
113           { {   0,    0,    0, 0000},
114             {0x46, 0x96,    0, 0000},
115             {   0, 0x66, 0x86, 0000},
116             {0000, 0000, 0000, 0000} },
117           { {   0, 0x16,    0, 0000},
118             {0x56, 0xA6,    0, 0000},
119             {0x26,    0,    0, 0000},
120             {0000, 0000, 0000, 0000} } },
121         
122         // I
123         { { {   0,    0,    0,    0},
124             {0x44, 0xC4, 0xC4, 0x84},
125             {   0,    0,    0,    0},
126             {   0,    0,    0,    0} },
127           { {   0, 0x14,    0,    0},
128             {   0, 0x34,    0,    0},
129             {   0, 0x34,    0,    0},
130             {   0, 0x24,    0,    0} },
131           { {   0,    0,    0,    0},
132             {0x44, 0xC4, 0xC4, 0x84},
133             {   0,    0,    0,    0},
134             {   0,    0,    0,    0} },
135           { {   0, 0x14,    0,    0},
136             {   0, 0x34,    0,    0},
137             {   0, 0x34,    0,    0},
138             {   0, 0x24,    0,    0} } },
139         
140         // O
141         { { {   0,    0,    0, 0000},
142             {   0, 0x55, 0x95, 0000},
143             {   0, 0x65, 0xA5, 0000},
144             {0000, 0000, 0000, 0000} },
145           { {   0,    0,    0, 0000},
146             {   0, 0x55, 0x95, 0000},
147             {   0, 0x65, 0xA5, 0000},
148             {0000, 0000, 0000, 0000} },
149           { {   0,    0,    0, 0000},
150             {   0, 0x55, 0x95, 0000},
151             {   0, 0x65, 0xA5, 0000},
152             {0000, 0000, 0000, 0000} },
153           { {   0,    0,    0, 0000},
154             {   0, 0x55, 0x95, 0000},
155             {   0, 0x65, 0xA5, 0000},
156             {0000, 0000, 0000, 0000} } }
157 };
158
159 int ShapeIterate(char s, int scr, int y, int x, ShapeDrawFunc func)
160 { //Draw a certain shape using <ShapeDrawFunc>
161         int i, j, result;
162         char type, rotation;
163
164         type = s / 4;
165         rotation = s & 3;
166         for (i = 0; i < 4; i++)
167                 for (j = 0; j < 4; j++)
168                         if (shapes[type][rotation][i][j])
169                                 if (result = func(scr, y-i, x+j, shapes[type][rotation][i][j]))
170                                         return result;
171         return 0;
172 }
173
174
175 float stdOptions[7] = {1, 1, 1, 1, 1, 1, 1}; //stdOptions
176
177 char ChooseOption(float options[7])
178 { //Return a random piece with given piece weight
179         int i;
180         float total = 0, val;
181
182         for (i = 0; i < 7; i++) total += options[i];
183         val = Random(0, 32767) / 32768.0 * total;
184         for (i = 0; i < 7; i++) if ((val -= options[i]) < 0)
185                 return i << 2;
186         return 0;
187 }
188
189
190 static unsigned char board[MAX_SCREENS][MAX_BOARD_HEIGHT][MAX_BOARD_WIDTH];
191 static unsigned char oldBoard[MAX_SCREENS][MAX_BOARD_HEIGHT][MAX_BOARD_WIDTH];
192 static unsigned int changed[MAX_SCREENS][MAX_BOARD_HEIGHT];
193 static int shadowy;
194
195 void ClearField(int scr)
196 { //Empty the whole field (all blocks BT_none)
197         int y, x;
198
199         for (y = Players[scr].boardHeight - 1; y >= 0; --y)
200                 for (x = 0; x < Players[scr].boardWidth; ++x) {
201                         oldBoard[scr][y][x] = board[scr][y][x] = BT_none;
202                 }
203 }
204
205 unsigned char GetBlock(int scr, int y, int x)
206 { //Returns the block on field at position (x,y)
207         if (y < 0 || x < 0 || x >= Players[scr].boardWidth)
208                 return BT_wall;
209         else if (y >= Players[scr].boardHeight)
210                 return BT_none;
211         else
212                 return board[scr][y][x];
213 }
214
215 static void SetBlock(int scr, int y, int x, unsigned char type)
216 {
217         if (y >= 0 && y < Players[scr].boardHeight
218          && x >= 0 && x < Players[scr].boardWidth) {
219                 board[scr][y][x] = type;
220                 changed[scr][y] |= 1 << x;
221         }
222 }
223
224 int RefreshBoard(int scr)
225 { //draw changes to screen
226         int y, x, any = 0;
227         unsigned int c;
228
229         for (y = Players[scr].boardVisible - 1; y >= 0; y--)
230                 if ((c = changed[scr][y])) { // line changed
231                         for (x = 0; c; (c >>= 1), x++)
232                                 if (c & 1 && board[scr][y][x] != oldBoard[scr][y][x]) {
233                                         PlotBlock(scr, y, x, board[scr][y][x]);
234                                         oldBoard[scr][y][x] = board[scr][y][x];
235                                 }
236                         changed[scr][y] = 0; // reset
237                         any = 1;
238                 } //changed row
239         return any;
240 }
241
242 int GlanceFunc(int scr, int y, int x, unsigned char type)
243 {
244         PlotBlockXY(y, x, type);
245         return 0;
246 }
247
248 int ShadowFunc(int scr, int y, int x, unsigned char type)
249 { //draw shadow
250         SetBlock(scr, y, x, BT_shadow);
251         return 0;
252 }
253
254 int PlotFunc(int scr, int y, int x, unsigned char type)
255 {
256         SetBlock(scr, y, x, type);
257         return 0;
258 }
259 void PlotShape(char shape, int scr, int y, int x, int shadow)
260 { //put shape on field
261         if (shadow) {
262                 for (shadowy = y - 1; shadowy >= 0; shadowy--)
263                         if (!ShapeFits(shape, scr, shadowy, x))
264                                 break;
265                 ShapeIterate(shape, scr, shadowy + 1, x, ShadowFunc);
266         } //draw shadow
267         ShapeIterate(shape, scr, y, x, PlotFunc);
268 }
269
270 int EraseFunc(int scr, int y, int x, unsigned char type)
271 {
272         SetBlock(scr, y, x, BT_none);
273         return 0;
274 }
275 void EraseShape(char shape, int scr, int y, int x, int shadow)
276 { //remove block from field
277         ShapeIterate(shape, scr, y, x, EraseFunc);
278         if (shadow && scr == me) // draw shadow
279                 ShapeIterate(shape, scr, shadowy + 1, x, EraseFunc);
280 }
281
282 int CollisionFunc(int scr, int y, int x, unsigned char type)
283 {
284         return GetBlock(scr, y, x) > BT_none;
285 }
286 int ShapeFits(char shape, int scr, int y, int x)
287 { //check if there's nothing in the way
288         return !ShapeIterate(shape, scr, y, x, CollisionFunc);
289 }
290
291 int VisibleFunc(int scr, int y, int x, unsigned char type)
292 {
293         return (y >= 0 && y < Players[scr].boardVisible &&
294                         x >= 0 && x < Players[scr].boardWidth);
295 }
296 int ShapeVisible(char shape, int scr, int y, int x)
297 {
298         return ShapeIterate(shape, scr, y, x, VisibleFunc);
299 }
300
301 int MovePiece(int scr, int deltaY, int deltaX)
302 {
303         int result;
304
305         EraseShape(Players[scr].curShape, scr,
306                 Players[scr].curY, Players[scr].curX, 1);
307         result = ShapeFits(Players[scr].curShape, scr, Players[scr].curY + deltaY,
308                            Players[scr].curX + deltaX);
309         if (result) {
310                 Players[scr].curY += deltaY;
311                 Players[scr].curX += deltaX;
312         }
313         PlotShape(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX,
314                 scr == me);
315         return result;
316 }
317
318 int RotatePiece(int scr, int dir)
319 {
320         char newshape;
321         int result;
322
323         EraseShape(Players[scr].curShape, scr, Players[scr].curY,
324                 Players[scr].curX, 1);
325         /* (inc|dec)rement only 3 least significant bits which indicate rotation */
326         newshape = (Players[scr].curShape & 252) + (((Players[scr].curShape & 3) + dir) & 3);
327         result = ShapeFits(newshape, scr, Players[scr].curY, Players[scr].curX);
328         if (!result) {
329                 // move if it doesn't fit anymore
330                 short int slideX;
331                 for (slideX = 0; slideX < 2; slideX = -slideX) {
332                         // slide left and right
333                         if (slideX >= 0) slideX++; // slide more
334                         if (result = ShapeFits(newshape, scr, Players[scr].curY,
335                                 Players[scr].curX+slideX)) break;
336                 }
337                 if (result) Players[scr].curX += slideX;
338         }
339         if (result) Players[scr].curShape = newshape;
340         PlotShape(Players[scr].curShape, scr,
341                 Players[scr].curY, Players[scr].curX, scr == me);
342         return result;
343 }
344
345 int DropPiece(int scr)
346 {
347         int count = 0;
348
349         EraseShape(Players[scr].curShape, scr,
350                 Players[scr].curY, Players[scr].curX, 1);
351         while (ShapeFits(Players[scr].curShape, scr,
352                Players[scr].curY - 1, Players[scr].curX)) {
353                 Players[scr].curY--;
354                 count++;
355         }
356         PlotShape(Players[scr].curShape, scr,
357                 Players[scr].curY, Players[scr].curX, 0);
358         return count;
359 }
360
361 int BlockFree(int scr, int x, int y, unsigned char z)
362 { //Check if blocks are empty below block (x,y) and sticking to (x,y) mask <z>
363         unsigned char curblock;
364
365         if (y == 0) return 0; // at bottom
366         curblock = GetBlock(scr, y, x) & z;
367         if (curblock & 0x10 && !BlockFree(scr, x, y-1, z & 0xD0)) return 0;
368         if (curblock & 0x20 && !BlockFree(scr, x, y+1, z & 0xE0)) return 0;
369         if (curblock & 0x40 && !BlockFree(scr, x+1, y, z & 0x70)) return 0;
370         if (curblock & 0x80 && !BlockFree(scr, x-1, y, z & 0xB0)) return 0;
371         if ((z = GetBlock(scr, y-1, x)) & 0x20) return 1; // stuck to block below
372         if (z > BT_none) return 0; // some other piece below
373         return 1; // nothing below
374 }
375
376 int BlockFall(int scr, int x, int y, unsigned char z)
377 { //Drop down block (x,y) and those sticking to it mask <z>
378         if (GetBlock(scr, y, x) & z & 0x10) BlockFall(scr, x, y-1, z & 0xD0);
379         if (GetBlock(scr, y, x) & z & 0x20) BlockFall(scr, x, y+1, z & 0xE0);
380         if (GetBlock(scr, y, x) & z & 0x40) BlockFall(scr, x+1, y, z & 0x70);
381         if (GetBlock(scr, y, x) & z & 0x80) BlockFall(scr, x-1, y, z & 0xB0);
382         SetBlock(scr, y-1, x, GetBlock(scr, y, x));
383         SetBlock(scr, y, x, BT_none);
384 }
385
386 int CheckFall(int scr)
387 { //Drop any free blocks on field
388         int xloop, x, x2, y, fallen = 0;
389         unsigned char z;
390
391         if (!Game.gravity) return 0;
392         for (y = Players[scr].boardHeight - 1; y > 0; y--)
393                 for (x = 0; x < Players[scr].boardWidth; x++) {
394                         if ((z = GetBlock(scr, y, x)) > BT_none && (z & 0xA0) == 0) {
395                                 // block present which doesn't stick left/up => topleft block
396                                 if (BlockFree(scr, x, y, 0xF0)) {
397                                         BlockFall(scr, x, y, 0xF0); // move blocks down
398                                         fallen++;
399                                 }
400                         } //block present
401                 }
402         return fallen;
403 }
404
405 int LineIsFull(int scr, int y)
406 { //return 0 if any blocks present on line <y>
407         int x;
408
409         for (x = 0; x < Players[scr].boardWidth; x++)
410                 if (GetBlock(scr, y, x) <= BT_none)
411                         return 0;
412         return 1;
413 }
414
415 void CopyLine(int scr, int from, int to)
416 { //move blocks on line <from> to line <to>
417         int x;
418
419         if (from != to)
420                 for (x = 0; x < Players[scr].boardWidth; ++x)
421                         SetBlock(scr, to, x, GetBlock(scr, from, x));
422 }
423
424 int ClearFullLines(int scr)
425 { //remove full lines, return lines cleared
426         int from, to, x, linescleared = 0;
427
428         do {
429         from = to = 0;
430         while (to < Players[scr].boardHeight) {
431                 while (LineIsFull(scr, from)) {
432                         from++; // skip
433                         for (x = 0; x<Players[scr].boardWidth; x++) {
434                                 // don't stick blocks to line which we'll remove
435                                 SetBlock(scr, from, x, GetBlock(scr, from, x) & 0xEF);
436                                 if (from > 1)
437                                         SetBlock(scr, from-2, x, GetBlock(scr, from-2, x) & 0xDF);
438                         }
439                 }
440                 CopyLine(scr, from++, to++);
441         }
442         linescleared += from - to;
443         } while (CheckFall(scr));
444         return linescleared;
445 }
446
447 void InsertJunk(int scr, int color, int count, int column)
448 { //add <count> junklines with hole at <column> to <scr> by team <color>
449         int y, x;
450
451         EraseShape(Players[scr].curShape, scr,
452                 Players[scr].curY, Players[scr].curX, 1);
453         for (y = Players[scr].boardHeight - count - 1; y >= 0; --y)
454                 CopyLine(scr, y, y + count);
455         for (y = 0; y < count; ++y)
456                 for (x = 0; x < Players[scr].boardWidth; ++x)
457                         SetBlock(scr, y, x, x == column ? BT_none : color + 1
458                                 + 0x40 * (x != column-1 && x < Players[scr].boardWidth-1)
459                                 + 0x80 * (x != column+1 && x > 0));
460         Players[scr].curY += count; // move piece up..
461         for (y = 0; y < count; ++y)
462                 if (ShapeFits(Players[scr].curShape, scr, Players[scr].curY - 1, Players[scr].curX))
463                         Players[scr].curY--; // ...and down again as far as possible
464                 else break;
465         PlotShape(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX,
466                 scr == me);
467 }
468