-
Notifications
You must be signed in to change notification settings - Fork 7
/
grid.c
326 lines (266 loc) · 7.76 KB
/
grid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* IceBreaker
* Copyright (c) 2000-2020 Matthew Miller <[email protected]>
*
* <http://www.mattdm.org/icebreaker/>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <SDL.h>
#include <string.h>
#include <stdlib.h>
#include "icebreaker.h"
#include "laundry.h"
#include "grid.h"
#include "penguin.h"
#include "globals.h"
#include "themes.h"
SDL_Surface* gridsave;
char grid[WIDTH][HEIGHT];
static char maskgrid[WIDTH][HEIGHT];
// kludge-o-rama
static long rcount;
#define MAXRCOUNT 80000
int initgrid()
{
// fix -- add error checking
gridsave = SDL_CreateRGBSurface(SDL_SWSURFACE,WIDTH,HEIGHT,screen->format->BitsPerPixel,0,0,0,0);
return 0;
}
void quitgrid()
{
SDL_FreeSurface(gridsave);
}
void drawgridblocks()
{
int r,g,b;
SDL_Rect tmprect;
SDL_FillRect(gridsave,NULL,color.background);
for (tmprect.x=BORDERLEFT;tmprect.x<BORDERRIGHT;tmprect.x+=BLOCKWIDTH)
for (tmprect.y=BORDERTOP;tmprect.y<BORDERBOTTOM;tmprect.y+=BLOCKHEIGHT)
{
r = (random() % (color.boardfillmaxr-color.boardfillminr+1))+color.boardfillminr;
if (color.boardfillminr == color.boardfillming && color.boardfillmaxr == color.boardfillmaxg)
g=r;
else
g = (random() % (color.boardfillmaxg-color.boardfillming+1))+color.boardfillming;
if (color.boardfillminr == color.boardfillminb && color.boardfillmaxr == color.boardfillmaxb)
b=r;
else if (color.boardfillming == color.boardfillminb && color.boardfillmaxg == color.boardfillmaxb)
b=g;
else
b = (random() % (color.boardfillmaxb-color.boardfillminb+1))+color.boardfillminb;
//if (grid[tmprect.x][tmprect.y]==' ' || grid[tmprect.x][tmprect.y]=='w')
if (grid[tmprect.x][tmprect.y]!='.' && grid[tmprect.x][tmprect.y]!='-' && grid[tmprect.x][tmprect.y]!='|')
{
tmprect.w=BLOCKWIDTH; tmprect.h=BLOCKHEIGHT;
SDL_FillRect(gridsave,&tmprect,color.gridline);
if (color.gridhighlight==color.gridline)
{
tmprect.w=BLOCKWIDTH-1; tmprect.h=BLOCKHEIGHT-1; // this makes the gridline show up
SDL_FillRect(gridsave,&tmprect,SDL_MapRGB(screen->format, r, g, b));
}
else
{
tmprect.w=BLOCKWIDTH-1; tmprect.h=BLOCKHEIGHT-1;
SDL_FillRect(gridsave,&tmprect,color.gridhighlight);
tmprect.w=BLOCKWIDTH-2; tmprect.h=BLOCKHEIGHT-2;
// FIX! this is kludgy! changing the index variable on the fly bad bad bad. (even if I do put it back!)
tmprect.x++; tmprect.y++;
SDL_FillRect(gridsave,&tmprect,SDL_MapRGB(screen->format, r, g, b));
tmprect.x--; tmprect.y--;
}
}
}
tmprect.x=BORDERLEFT; tmprect.y=BORDERTOP;
tmprect.w=PLAYWIDTH; tmprect.h=PLAYHEIGHT;
SDL_BlitSurface(gridsave, &tmprect, screen, &tmprect);
}
void markgrid(int x, int y, int w, int h, char fillchar)
{
int i;
// Optimizing this routine much further seems about impossible to me
// but if you know how to do it, let me know. This is by far the
// most frequently-called function in the whole game. Short of
// rethinking the collision detection -- which could stand a rethink
// anyway -- there's not much that can be done.
for (i=x;i<x+w;i++)
memset(&grid[i][y],fillchar,h);
/*
int i, j;
for (j=y;j<y+h;j++)
for (i=x;i<x+w;i++)
grid[i][j]=fillchar;
*/
}
long countcleared()
{
int i, j;
long c;
c=0;
for (i=BORDERLEFT;i<BORDERRIGHT;i++)
for (j=BORDERTOP;j<BORDERBOTTOM;j++)
if (grid[i][j] == ' ' || grid[i][j] == '*')
c++;
//return(100-(c*100/(PLAYWIDTH*PLAYHEIGHT)));
return(c);
}
#ifdef DEBUG
void printboard()
{
int i, j;
for (j=BLOCKWIDTH/2;j<HEIGHT;j+=BLOCKHEIGHT)
{
for (i=BLOCKWIDTH/2;i<WIDTH;i+=BLOCKWIDTH)
{
printf("%c ",grid[i][j]);
}
printf("\n");
}
}
#endif
#ifdef DEBUG
void printwholegrid()
{
int i, j;
printf ("grid:\n");
for (j=0;j<HEIGHT;j++)
{
for (i=0;i<WIDTH;i++)
{
printf("%c ",grid[i][j]);
}
printf("\n");
}
}
#endif
#ifdef DEBUG
void printwholemaskgrid()
{
int i, j;
printf ("maskgrid:\n");
for (j=0;j<HEIGHT;j++)
{
for (i=0;i<WIDTH;i++)
{
printf("%c ",maskgrid[i][j]);
}
printf("\n");
}
}
#endif
void checkempty(int x, int y)
{
//int i,j;
// for debugging...
SDL_Rect tmprect;
// if square isn't empty, just return....
if (grid[x][y]!=' ') { return; }
// it'd be nice to find a way to keep this longer...
memcpy(maskgrid,grid,WIDTH*HEIGHT);
// penguinsearch at that spot...
rcount=0;
if (!penguinsearch(x,y)) // area is clear!
{
//printwholemaskgrid();
//floodfill(x,y);
// this makes sure x and y are the top left corners of blocks.
// since the area is empty of penguins, it should be completely
// safe to use this isntead of floodfill here. really. :)
squarefill( (((x-BORDERLEFT)/BLOCKWIDTH ) * BLOCKWIDTH ) +BORDERLEFT, (((y-BORDERTOP)/BLOCKHEIGHT) * BLOCKHEIGHT) +BORDERTOP);
tmprect.w=BLOCKWIDTH; tmprect.h=BLOCKHEIGHT;
for (tmprect.x=BORDERLEFT;tmprect.x<BORDERRIGHT;tmprect.x+=BLOCKWIDTH)
for (tmprect.y=BORDERTOP;tmprect.y<BORDERBOTTOM;tmprect.y+=BLOCKHEIGHT)
if (grid[tmprect.x][tmprect.y]=='.') // clear it!)
{
SDL_FillRect(screen,&tmprect,color.background);
soil(tmprect);
}
//printwholegrid();
}
/* printf("Search took %ld recursions.\n",rcount); */
/*
for (j=0;j<HEIGHT;j+=BLOCKHEIGHT)
{
for (i=0;i<WIDTH;i+=BLOCKWIDTH)
{
printf("%c ",maskgrid[i][j]);
}
printf("\n");
}
printf("\n");
*/
}
int penguinsearch(int i, int j)
{
int searchval=0;
rcount++;
// kludge! FIX! BAD!
if (rcount>MAXRCOUNT) // bail
{
fprintf(stderr,"Damn. Ran out of recursions.\n");
return(2);
}
// shouldn't need to check bounds because we're only painting in the
// middle. and we call this function so much that the time saved
// is worth it
//if (i<0 || j<0 || i>=WIDTH || j>=HEIGHT)
//{
// fprintf(stderr,"That shouldn't have happened (penguinsearch)! (%d,%d)\n",i,j);
// exit(1);
//}
if (maskgrid[i][j]==' '
|| maskgrid[i][j]=='1' || maskgrid[i][j]=='2' || maskgrid[i][j]=='w') // Ah ha! The nefarious "instant melting ice" bug solved! NOTE: if more lines are added to the game, add them here too!
{
maskgrid[i][j]=',';
// hmmm. the "ice-shelf-collapse" bug *isn't* fixed. :(
searchval=penguinsearch(i+BLOCKWIDTH, j);
if (!searchval) searchval=penguinsearch(i-BLOCKWIDTH, j);
if (!searchval) searchval=penguinsearch(i, j-BLOCKHEIGHT);
if (!searchval) searchval=penguinsearch(i, j+BLOCKHEIGHT);
}
else if (maskgrid[i][j]=='*') // found a penguin!
{
searchval=1;
}
return(searchval);
}
void floodfill(int x, int y)
{
// shouldn't need to check bounds because we're only painting in the
// middle. ie (x<0 || y<0 || x>WIDTH || y>HEIGHT) is always false.
if (grid[x][y]==' ' || grid[x][y]=='1' || grid[x][y]=='2' || grid[x][y]=='w')
{
grid[x][y]='.';
floodfill(x+1, y);
floodfill(x-1, y);
floodfill(x, y+1);
floodfill(x, y-1);
}
}
void squarefill(int x, int y)
{
// x and y must be the top left corner of a square, or else this
// will look silly. and there's no bounds checking!
if (grid[x][y]==' ' || grid[x][y]=='1' || grid[x][y]=='2' || grid[x][y]=='w')
{
markgrid(x,y,BLOCKWIDTH,BLOCKHEIGHT,'.');
squarefill(x+BLOCKWIDTH, y);
squarefill(x-BLOCKWIDTH, y);
squarefill(x, y+BLOCKHEIGHT);
squarefill(x, y-BLOCKHEIGHT);
}
}