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