3870c4d8 |
1 | /* |
2 | * rect.c: Puzzle from nikoli.co.jp. You have a square grid with |
3 | * numbers in some squares; you must divide the square grid up into |
4 | * variously sized rectangles, such that every rectangle contains |
5 | * exactly one numbered square and the area of each rectangle is |
6 | * equal to the number contained in it. |
7 | */ |
8 | |
9 | /* |
10 | * TODO: |
11 | * |
12 | * - Improve on singleton removal by making an aesthetic choice |
13 | * about which of the options to take. |
14 | * |
15 | * - When doing the 3x3 trick in singleton removal, limit the size |
16 | * of the generated rectangles in accordance with the max |
17 | * rectangle size. |
18 | * |
19 | * - It might be interesting to deliberately try to place |
20 | * numbers so as to reduce alternative solution patterns. I |
21 | * doubt we can do a perfect job of this, but we can make a |
22 | * start by, for example, noticing pairs of 2-rects |
23 | * alongside one another and _not_ putting their numbers at |
24 | * opposite ends. |
25 | * |
26 | * - If we start by sorting the rectlist in descending order |
27 | * of area, we might be able to bias our random number |
28 | * selection to produce a few large rectangles more often |
29 | * than oodles of small ones? Unsure, but might be worth a |
30 | * try. |
31 | */ |
32 | |
33 | #include <stdio.h> |
34 | #include <stdlib.h> |
35 | #include <string.h> |
36 | #include <assert.h> |
b0e26073 |
37 | #include <ctype.h> |
3870c4d8 |
38 | #include <math.h> |
39 | |
40 | #include "puzzles.h" |
41 | |
42 | const char *const game_name = "Rectangles"; |
e91825f8 |
43 | const char *const game_winhelp_topic = "games.rectangles"; |
3870c4d8 |
44 | const int game_can_configure = TRUE; |
45 | |
46 | enum { |
47 | COL_BACKGROUND, |
48 | COL_CORRECT, |
49 | COL_LINE, |
50 | COL_TEXT, |
51 | COL_GRID, |
08dd70c3 |
52 | COL_DRAG, |
3870c4d8 |
53 | NCOLOURS |
54 | }; |
55 | |
56 | struct game_params { |
57 | int w, h; |
58 | }; |
59 | |
60 | #define INDEX(state, x, y) (((y) * (state)->w) + (x)) |
61 | #define index(state, a, x, y) ((a) [ INDEX(state,x,y) ]) |
62 | #define grid(state,x,y) index(state, (state)->grid, x, y) |
63 | #define vedge(state,x,y) index(state, (state)->vedge, x, y) |
64 | #define hedge(state,x,y) index(state, (state)->hedge, x, y) |
65 | |
66 | #define CRANGE(state,x,y,dx,dy) ( (x) >= dx && (x) < (state)->w && \ |
67 | (y) >= dy && (y) < (state)->h ) |
68 | #define RANGE(state,x,y) CRANGE(state,x,y,0,0) |
69 | #define HRANGE(state,x,y) CRANGE(state,x,y,0,1) |
70 | #define VRANGE(state,x,y) CRANGE(state,x,y,1,0) |
71 | |
72 | #define TILE_SIZE 24 |
73 | #define BORDER 18 |
74 | |
d4e7900f |
75 | #define CORNER_TOLERANCE 0.15F |
76 | #define CENTRE_TOLERANCE 0.15F |
77 | |
ef29354c |
78 | #define FLASH_TIME 0.13F |
79 | |
3870c4d8 |
80 | #define COORD(x) ( (x) * TILE_SIZE + BORDER ) |
81 | #define FROMCOORD(x) ( ((x) - BORDER) / TILE_SIZE ) |
82 | |
83 | struct game_state { |
84 | int w, h; |
85 | int *grid; /* contains the numbers */ |
86 | unsigned char *vedge; /* (w+1) x h */ |
87 | unsigned char *hedge; /* w x (h+1) */ |
ef29354c |
88 | int completed; |
3870c4d8 |
89 | }; |
90 | |
91 | game_params *default_params(void) |
92 | { |
93 | game_params *ret = snew(game_params); |
94 | |
95 | ret->w = ret->h = 7; |
96 | |
97 | return ret; |
98 | } |
99 | |
100 | int game_fetch_preset(int i, char **name, game_params **params) |
101 | { |
102 | game_params *ret; |
103 | int w, h; |
104 | char buf[80]; |
105 | |
106 | switch (i) { |
107 | case 0: w = 7, h = 7; break; |
108 | case 1: w = 11, h = 11; break; |
109 | case 2: w = 15, h = 15; break; |
110 | case 3: w = 19, h = 19; break; |
111 | default: return FALSE; |
112 | } |
113 | |
114 | sprintf(buf, "%dx%d", w, h); |
115 | *name = dupstr(buf); |
116 | *params = ret = snew(game_params); |
117 | ret->w = w; |
118 | ret->h = h; |
119 | return TRUE; |
120 | } |
121 | |
122 | void free_params(game_params *params) |
123 | { |
124 | sfree(params); |
125 | } |
126 | |
127 | game_params *dup_params(game_params *params) |
128 | { |
129 | game_params *ret = snew(game_params); |
130 | *ret = *params; /* structure copy */ |
131 | return ret; |
132 | } |
133 | |
b0e26073 |
134 | game_params *decode_params(char const *string) |
135 | { |
136 | game_params *ret = default_params(); |
137 | |
138 | ret->w = ret->h = atoi(string); |
139 | while (*string && isdigit(*string)) string++; |
140 | if (*string == 'x') { |
141 | string++; |
142 | ret->h = atoi(string); |
143 | } |
144 | |
145 | return ret; |
146 | } |
147 | |
148 | char *encode_params(game_params *params) |
149 | { |
150 | char data[256]; |
151 | |
152 | sprintf(data, "%dx%d", params->w, params->h); |
153 | |
154 | return dupstr(data); |
155 | } |
156 | |
3870c4d8 |
157 | config_item *game_configure(game_params *params) |
158 | { |
159 | config_item *ret; |
160 | char buf[80]; |
161 | |
162 | ret = snewn(5, config_item); |
163 | |
164 | ret[0].name = "Width"; |
165 | ret[0].type = C_STRING; |
166 | sprintf(buf, "%d", params->w); |
167 | ret[0].sval = dupstr(buf); |
168 | ret[0].ival = 0; |
169 | |
170 | ret[1].name = "Height"; |
171 | ret[1].type = C_STRING; |
172 | sprintf(buf, "%d", params->h); |
173 | ret[1].sval = dupstr(buf); |
174 | ret[1].ival = 0; |
175 | |
176 | ret[2].name = NULL; |
177 | ret[2].type = C_END; |
178 | ret[2].sval = NULL; |
179 | ret[2].ival = 0; |
180 | |
181 | return ret; |
182 | } |
183 | |
184 | game_params *custom_params(config_item *cfg) |
185 | { |
186 | game_params *ret = snew(game_params); |
187 | |
188 | ret->w = atoi(cfg[0].sval); |
189 | ret->h = atoi(cfg[1].sval); |
190 | |
191 | return ret; |
192 | } |
193 | |
194 | char *validate_params(game_params *params) |
195 | { |
196 | if (params->w <= 0 && params->h <= 0) |
197 | return "Width and height must both be greater than zero"; |
d4e7900f |
198 | if (params->w < 2 && params->h < 2) |
199 | return "Grid area must be greater than one"; |
3870c4d8 |
200 | return NULL; |
201 | } |
202 | |
203 | struct rect { |
204 | int x, y; |
205 | int w, h; |
206 | }; |
207 | |
208 | struct rectlist { |
209 | struct rect *rects; |
210 | int n; |
211 | }; |
212 | |
213 | static struct rectlist *get_rectlist(game_params *params, int *grid) |
214 | { |
215 | int rw, rh; |
216 | int x, y; |
217 | int maxarea; |
218 | struct rect *rects = NULL; |
219 | int nrects = 0, rectsize = 0; |
220 | |
221 | /* |
d4e7900f |
222 | * Maximum rectangle area is 1/6 of total grid size, unless |
223 | * this means we can't place any rectangles at all in which |
224 | * case we set it to 2 at minimum. |
3870c4d8 |
225 | */ |
226 | maxarea = params->w * params->h / 6; |
d4e7900f |
227 | if (maxarea < 2) |
228 | maxarea = 2; |
3870c4d8 |
229 | |
230 | for (rw = 1; rw <= params->w; rw++) |
231 | for (rh = 1; rh <= params->h; rh++) { |
232 | if (rw * rh > maxarea) |
233 | continue; |
234 | if (rw * rh == 1) |
235 | continue; |
236 | for (x = 0; x <= params->w - rw; x++) |
237 | for (y = 0; y <= params->h - rh; y++) { |
3870c4d8 |
238 | if (nrects >= rectsize) { |
239 | rectsize = nrects + 256; |
240 | rects = sresize(rects, rectsize, struct rect); |
241 | } |
242 | |
243 | rects[nrects].x = x; |
244 | rects[nrects].y = y; |
245 | rects[nrects].w = rw; |
246 | rects[nrects].h = rh; |
247 | nrects++; |
248 | } |
249 | } |
250 | |
251 | if (nrects > 0) { |
252 | struct rectlist *ret; |
253 | ret = snew(struct rectlist); |
254 | ret->rects = rects; |
255 | ret->n = nrects; |
256 | return ret; |
257 | } else { |
258 | assert(rects == NULL); /* hence no need to free */ |
259 | return NULL; |
260 | } |
261 | } |
262 | |
263 | static void free_rectlist(struct rectlist *list) |
264 | { |
265 | sfree(list->rects); |
266 | sfree(list); |
267 | } |
268 | |
269 | static void place_rect(game_params *params, int *grid, struct rect r) |
270 | { |
271 | int idx = INDEX(params, r.x, r.y); |
272 | int x, y; |
273 | |
274 | for (x = r.x; x < r.x+r.w; x++) |
275 | for (y = r.y; y < r.y+r.h; y++) { |
276 | index(params, grid, x, y) = idx; |
277 | } |
278 | #ifdef GENERATION_DIAGNOSTICS |
279 | printf(" placing rectangle at (%d,%d) size %d x %d\n", |
280 | r.x, r.y, r.w, r.h); |
281 | #endif |
282 | } |
283 | |
284 | static struct rect find_rect(game_params *params, int *grid, int x, int y) |
285 | { |
286 | int idx, w, h; |
287 | struct rect r; |
288 | |
289 | /* |
290 | * Find the top left of the rectangle. |
291 | */ |
292 | idx = index(params, grid, x, y); |
293 | |
294 | if (idx < 0) { |
295 | r.x = x; |
296 | r.y = y; |
297 | r.w = r.h = 1; |
298 | return r; /* 1x1 singleton here */ |
299 | } |
300 | |
301 | y = idx / params->w; |
302 | x = idx % params->w; |
303 | |
304 | /* |
305 | * Find the width and height of the rectangle. |
306 | */ |
307 | for (w = 1; |
308 | (x+w < params->w && index(params,grid,x+w,y)==idx); |
309 | w++); |
310 | for (h = 1; |
311 | (y+h < params->h && index(params,grid,x,y+h)==idx); |
312 | h++); |
313 | |
314 | r.x = x; |
315 | r.y = y; |
316 | r.w = w; |
317 | r.h = h; |
318 | |
319 | return r; |
320 | } |
321 | |
322 | #ifdef GENERATION_DIAGNOSTICS |
323 | static void display_grid(game_params *params, int *grid, int *numbers) |
324 | { |
325 | unsigned char *egrid = snewn((params->w*2+3) * (params->h*2+3), |
326 | unsigned char); |
327 | memset(egrid, 0, (params->w*2+3) * (params->h*2+3)); |
328 | int x, y; |
329 | int r = (params->w*2+3); |
330 | |
331 | for (x = 0; x < params->w; x++) |
332 | for (y = 0; y < params->h; y++) { |
333 | int i = index(params, grid, x, y); |
334 | if (x == 0 || index(params, grid, x-1, y) != i) |
335 | egrid[(2*y+2) * r + (2*x+1)] = 1; |
336 | if (x == params->w-1 || index(params, grid, x+1, y) != i) |
337 | egrid[(2*y+2) * r + (2*x+3)] = 1; |
338 | if (y == 0 || index(params, grid, x, y-1) != i) |
339 | egrid[(2*y+1) * r + (2*x+2)] = 1; |
340 | if (y == params->h-1 || index(params, grid, x, y+1) != i) |
341 | egrid[(2*y+3) * r + (2*x+2)] = 1; |
342 | } |
343 | |
344 | for (y = 1; y < 2*params->h+2; y++) { |
345 | for (x = 1; x < 2*params->w+2; x++) { |
346 | if (!((y|x)&1)) { |
347 | int k = index(params, numbers, x/2-1, y/2-1); |
348 | if (k) printf("%2d", k); else printf(" "); |
349 | } else if (!((y&x)&1)) { |
350 | int v = egrid[y*r+x]; |
351 | if ((y&1) && v) v = '-'; |
352 | if ((x&1) && v) v = '|'; |
353 | if (!v) v = ' '; |
354 | putchar(v); |
355 | if (!(x&1)) putchar(v); |
356 | } else { |
357 | int c, d = 0; |
358 | if (egrid[y*r+(x+1)]) d |= 1; |
359 | if (egrid[(y-1)*r+x]) d |= 2; |
360 | if (egrid[y*r+(x-1)]) d |= 4; |
361 | if (egrid[(y+1)*r+x]) d |= 8; |
362 | c = " ??+?-++?+|+++++"[d]; |
363 | putchar(c); |
364 | if (!(x&1)) putchar(c); |
365 | } |
366 | } |
367 | putchar('\n'); |
368 | } |
369 | |
370 | sfree(egrid); |
371 | } |
372 | #endif |
373 | |
374 | char *new_game_seed(game_params *params, random_state *rs) |
375 | { |
376 | int *grid, *numbers; |
377 | struct rectlist *list; |
378 | int x, y, run, i; |
379 | char *seed, *p; |
380 | |
381 | grid = snewn(params->w * params->h, int); |
382 | numbers = snewn(params->w * params->h, int); |
383 | |
384 | for (y = 0; y < params->h; y++) |
385 | for (x = 0; x < params->w; x++) { |
386 | index(params, grid, x, y) = -1; |
387 | index(params, numbers, x, y) = 0; |
388 | } |
389 | |
390 | list = get_rectlist(params, grid); |
391 | assert(list != NULL); |
392 | |
393 | /* |
394 | * Place rectangles until we can't any more. |
395 | */ |
396 | while (list->n > 0) { |
397 | int i, m; |
398 | struct rect r; |
399 | |
400 | /* |
401 | * Pick a random rectangle. |
402 | */ |
403 | i = random_upto(rs, list->n); |
404 | r = list->rects[i]; |
405 | |
406 | /* |
407 | * Place it. |
408 | */ |
409 | place_rect(params, grid, r); |
410 | |
411 | /* |
412 | * Winnow the list by removing any rectangles which |
413 | * overlap this one. |
414 | */ |
415 | m = 0; |
416 | for (i = 0; i < list->n; i++) { |
417 | struct rect s = list->rects[i]; |
418 | if (s.x+s.w <= r.x || r.x+r.w <= s.x || |
419 | s.y+s.h <= r.y || r.y+r.h <= s.y) |
420 | list->rects[m++] = s; |
421 | } |
422 | list->n = m; |
423 | } |
424 | |
425 | free_rectlist(list); |
426 | |
427 | /* |
428 | * Deal with singleton spaces remaining in the grid, one by |
429 | * one. |
430 | * |
431 | * We do this by making a local change to the layout. There are |
432 | * several possibilities: |
433 | * |
434 | * +-----+-----+ Here, we can remove the singleton by |
435 | * | | | extending the 1x2 rectangle below it |
436 | * +--+--+-----+ into a 1x3. |
437 | * | | | | |
438 | * | +--+ | |
439 | * | | | | |
440 | * | | | | |
441 | * | | | | |
442 | * +--+--+-----+ |
443 | * |
444 | * +--+--+--+ Here, that trick doesn't work: there's no |
445 | * | | | 1 x n rectangle with the singleton at one |
446 | * | | | end. Instead, we extend a 1 x n rectangle |
447 | * | | | _out_ from the singleton, shaving a layer |
448 | * +--+--+ | off the end of another rectangle. So if we |
449 | * | | | | extended up, we'd make our singleton part |
450 | * | +--+--+ of a 1x3 and generate a 1x2 where the 2x2 |
451 | * | | | used to be; or we could extend right into |
452 | * +--+-----+ a 2x1, turning the 1x3 into a 1x2. |
453 | * |
454 | * +-----+--+ Here, we can't even do _that_, since any |
455 | * | | | direction we choose to extend the singleton |
456 | * +--+--+ | will produce a new singleton as a result of |
457 | * | | | | truncating one of the size-2 rectangles. |
458 | * | +--+--+ Fortunately, this case can _only_ occur when |
459 | * | | | a singleton is surrounded by four size-2s |
460 | * +--+-----+ in this fashion; so instead we can simply |
461 | * replace the whole section with a single 3x3. |
462 | */ |
463 | for (x = 0; x < params->w; x++) { |
464 | for (y = 0; y < params->h; y++) { |
465 | if (index(params, grid, x, y) < 0) { |
466 | int dirs[4], ndirs; |
467 | |
468 | #ifdef GENERATION_DIAGNOSTICS |
469 | display_grid(params, grid, numbers); |
470 | printf("singleton at %d,%d\n", x, y); |
471 | #endif |
472 | |
473 | /* |
474 | * Check in which directions we can feasibly extend |
475 | * the singleton. We can extend in a particular |
476 | * direction iff either: |
477 | * |
478 | * - the rectangle on that side of the singleton |
479 | * is not 2x1, and we are at one end of the edge |
480 | * of it we are touching |
481 | * |
482 | * - it is 2x1 but we are on its short side. |
483 | * |
484 | * FIXME: we could plausibly choose between these |
485 | * based on the sizes of the rectangles they would |
486 | * create? |
487 | */ |
488 | ndirs = 0; |
489 | if (x < params->w-1) { |
490 | struct rect r = find_rect(params, grid, x+1, y); |
491 | if ((r.w * r.h > 2 && (r.y==y || r.y+r.h-1==y)) || r.h==1) |
492 | dirs[ndirs++] = 1; /* right */ |
493 | } |
494 | if (y > 0) { |
495 | struct rect r = find_rect(params, grid, x, y-1); |
496 | if ((r.w * r.h > 2 && (r.x==x || r.x+r.w-1==x)) || r.w==1) |
497 | dirs[ndirs++] = 2; /* up */ |
498 | } |
499 | if (x > 0) { |
500 | struct rect r = find_rect(params, grid, x-1, y); |
501 | if ((r.w * r.h > 2 && (r.y==y || r.y+r.h-1==y)) || r.h==1) |
502 | dirs[ndirs++] = 4; /* left */ |
503 | } |
504 | if (y < params->h-1) { |
505 | struct rect r = find_rect(params, grid, x, y+1); |
506 | if ((r.w * r.h > 2 && (r.x==x || r.x+r.w-1==x)) || r.w==1) |
507 | dirs[ndirs++] = 8; /* down */ |
508 | } |
509 | |
510 | if (ndirs > 0) { |
511 | int which, dir; |
512 | struct rect r1, r2; |
513 | |
514 | which = random_upto(rs, ndirs); |
515 | dir = dirs[which]; |
516 | |
517 | switch (dir) { |
518 | case 1: /* right */ |
519 | assert(x < params->w+1); |
520 | #ifdef GENERATION_DIAGNOSTICS |
521 | printf("extending right\n"); |
522 | #endif |
523 | r1 = find_rect(params, grid, x+1, y); |
524 | r2.x = x; |
525 | r2.y = y; |
526 | r2.w = 1 + r1.w; |
527 | r2.h = 1; |
528 | if (r1.y == y) |
529 | r1.y++; |
530 | r1.h--; |
531 | break; |
532 | case 2: /* up */ |
533 | assert(y > 0); |
534 | #ifdef GENERATION_DIAGNOSTICS |
535 | printf("extending up\n"); |
536 | #endif |
537 | r1 = find_rect(params, grid, x, y-1); |
538 | r2.x = x; |
539 | r2.y = r1.y; |
540 | r2.w = 1; |
541 | r2.h = 1 + r1.h; |
542 | if (r1.x == x) |
543 | r1.x++; |
544 | r1.w--; |
545 | break; |
546 | case 4: /* left */ |
547 | assert(x > 0); |
548 | #ifdef GENERATION_DIAGNOSTICS |
549 | printf("extending left\n"); |
550 | #endif |
551 | r1 = find_rect(params, grid, x-1, y); |
552 | r2.x = r1.x; |
553 | r2.y = y; |
554 | r2.w = 1 + r1.w; |
555 | r2.h = 1; |
556 | if (r1.y == y) |
557 | r1.y++; |
558 | r1.h--; |
559 | break; |
560 | case 8: /* down */ |
561 | assert(y < params->h+1); |
562 | #ifdef GENERATION_DIAGNOSTICS |
563 | printf("extending down\n"); |
564 | #endif |
565 | r1 = find_rect(params, grid, x, y+1); |
566 | r2.x = x; |
567 | r2.y = y; |
568 | r2.w = 1; |
569 | r2.h = 1 + r1.h; |
570 | if (r1.x == x) |
571 | r1.x++; |
572 | r1.w--; |
573 | break; |
574 | } |
575 | if (r1.h > 0 && r1.w > 0) |
576 | place_rect(params, grid, r1); |
577 | place_rect(params, grid, r2); |
578 | } else { |
579 | #ifndef NDEBUG |
580 | /* |
581 | * Sanity-check that there really is a 3x3 |
582 | * rectangle surrounding this singleton and it |
583 | * contains absolutely everything we could |
584 | * possibly need. |
585 | */ |
586 | { |
587 | int xx, yy; |
588 | assert(x > 0 && x < params->w-1); |
589 | assert(y > 0 && y < params->h-1); |
590 | |
591 | for (xx = x-1; xx <= x+1; xx++) |
592 | for (yy = y-1; yy <= y+1; yy++) { |
593 | struct rect r = find_rect(params,grid,xx,yy); |
594 | assert(r.x >= x-1); |
595 | assert(r.y >= y-1); |
596 | assert(r.x+r.w-1 <= x+1); |
597 | assert(r.y+r.h-1 <= y+1); |
598 | } |
599 | } |
600 | #endif |
601 | |
602 | #ifdef GENERATION_DIAGNOSTICS |
603 | printf("need the 3x3 trick\n"); |
604 | #endif |
605 | |
606 | /* |
607 | * FIXME: If the maximum rectangle area for |
608 | * this grid is less than 9, we ought to |
609 | * subdivide the 3x3 in some fashion. There are |
610 | * five other possibilities: |
611 | * |
612 | * - a 6 and a 3 |
613 | * - a 4, a 3 and a 2 |
614 | * - three 3s |
615 | * - a 3 and three 2s (two different arrangements). |
616 | */ |
617 | |
618 | { |
619 | struct rect r; |
620 | r.x = x-1; |
621 | r.y = y-1; |
622 | r.w = r.h = 3; |
623 | place_rect(params, grid, r); |
624 | } |
625 | } |
626 | } |
627 | } |
628 | } |
629 | |
630 | /* |
631 | * Place numbers. |
632 | */ |
633 | for (x = 0; x < params->w; x++) { |
634 | for (y = 0; y < params->h; y++) { |
635 | int idx = INDEX(params, x, y); |
636 | if (index(params, grid, x, y) == idx) { |
637 | struct rect r = find_rect(params, grid, x, y); |
638 | int n, xx, yy; |
639 | |
640 | /* |
641 | * Decide where to put the number. |
642 | */ |
643 | n = random_upto(rs, r.w*r.h); |
644 | yy = n / r.w; |
645 | xx = n % r.w; |
646 | index(params,numbers,x+xx,y+yy) = r.w*r.h; |
647 | } |
648 | } |
649 | } |
650 | |
651 | #ifdef GENERATION_DIAGNOSTICS |
652 | display_grid(params, grid, numbers); |
653 | #endif |
654 | |
655 | seed = snewn(11 * params->w * params->h, char); |
656 | p = seed; |
657 | run = 0; |
658 | for (i = 0; i <= params->w * params->h; i++) { |
659 | int n = (i < params->w * params->h ? numbers[i] : -1); |
660 | |
661 | if (!n) |
662 | run++; |
663 | else { |
664 | if (run) { |
665 | while (run > 0) { |
666 | int c = 'a' - 1 + run; |
667 | if (run > 26) |
668 | c = 'z'; |
669 | *p++ = c; |
670 | run -= c - ('a' - 1); |
671 | } |
672 | } else { |
673 | *p++ = '_'; |
674 | } |
675 | if (n > 0) |
676 | p += sprintf(p, "%d", n); |
677 | run = 0; |
678 | } |
679 | } |
680 | *p = '\0'; |
681 | |
682 | sfree(grid); |
683 | sfree(numbers); |
684 | |
685 | return seed; |
686 | } |
687 | |
688 | char *validate_seed(game_params *params, char *seed) |
689 | { |
690 | int area = params->w * params->h; |
691 | int squares = 0; |
692 | |
693 | while (*seed) { |
694 | int n = *seed++; |
695 | if (n >= 'a' && n <= 'z') { |
696 | squares += n - 'a' + 1; |
697 | } else if (n == '_') { |
698 | /* do nothing */; |
699 | } else if (n > '0' && n <= '9') { |
9bb5bf60 |
700 | squares++; |
3870c4d8 |
701 | while (*seed >= '0' && *seed <= '9') |
702 | seed++; |
703 | } else |
704 | return "Invalid character in game specification"; |
705 | } |
706 | |
707 | if (squares < area) |
708 | return "Not enough data to fill grid"; |
709 | |
710 | if (squares > area) |
711 | return "Too much data to fit in grid"; |
712 | |
713 | return NULL; |
714 | } |
715 | |
716 | game_state *new_game(game_params *params, char *seed) |
717 | { |
718 | game_state *state = snew(game_state); |
719 | int x, y, i, area; |
720 | |
721 | state->w = params->w; |
722 | state->h = params->h; |
723 | |
724 | area = state->w * state->h; |
725 | |
726 | state->grid = snewn(area, int); |
727 | state->vedge = snewn(area, unsigned char); |
728 | state->hedge = snewn(area, unsigned char); |
ef29354c |
729 | state->completed = FALSE; |
3870c4d8 |
730 | |
731 | i = 0; |
732 | while (*seed) { |
733 | int n = *seed++; |
734 | if (n >= 'a' && n <= 'z') { |
735 | int run = n - 'a' + 1; |
736 | assert(i + run <= area); |
737 | while (run-- > 0) |
738 | state->grid[i++] = 0; |
739 | } else if (n == '_') { |
740 | /* do nothing */; |
741 | } else if (n > '0' && n <= '9') { |
742 | assert(i < area); |
743 | state->grid[i++] = atoi(seed-1); |
744 | while (*seed >= '0' && *seed <= '9') |
745 | seed++; |
746 | } else { |
747 | assert(!"We can't get here"); |
748 | } |
749 | } |
750 | assert(i == area); |
751 | |
752 | for (y = 0; y < state->h; y++) |
753 | for (x = 0; x < state->w; x++) |
754 | vedge(state,x,y) = hedge(state,x,y) = 0; |
755 | |
756 | return state; |
757 | } |
758 | |
759 | game_state *dup_game(game_state *state) |
760 | { |
761 | game_state *ret = snew(game_state); |
762 | |
763 | ret->w = state->w; |
764 | ret->h = state->h; |
765 | |
766 | ret->vedge = snewn(state->w * state->h, unsigned char); |
767 | ret->hedge = snewn(state->w * state->h, unsigned char); |
768 | ret->grid = snewn(state->w * state->h, int); |
769 | |
ef29354c |
770 | ret->completed = state->completed; |
771 | |
3870c4d8 |
772 | memcpy(ret->grid, state->grid, state->w * state->h * sizeof(int)); |
773 | memcpy(ret->vedge, state->vedge, state->w*state->h*sizeof(unsigned char)); |
774 | memcpy(ret->hedge, state->hedge, state->w*state->h*sizeof(unsigned char)); |
775 | |
776 | return ret; |
777 | } |
778 | |
779 | void free_game(game_state *state) |
780 | { |
781 | sfree(state->grid); |
782 | sfree(state->vedge); |
783 | sfree(state->hedge); |
784 | sfree(state); |
785 | } |
786 | |
787 | static unsigned char *get_correct(game_state *state) |
788 | { |
789 | unsigned char *ret; |
790 | int x, y; |
791 | |
792 | ret = snewn(state->w * state->h, unsigned char); |
793 | memset(ret, 0xFF, state->w * state->h); |
794 | |
795 | for (x = 0; x < state->w; x++) |
796 | for (y = 0; y < state->h; y++) |
797 | if (index(state,ret,x,y) == 0xFF) { |
798 | int rw, rh; |
799 | int xx, yy; |
800 | int num, area, valid; |
801 | |
802 | /* |
803 | * Find a rectangle starting at this point. |
804 | */ |
805 | rw = 1; |
806 | while (x+rw < state->w && !vedge(state,x+rw,y)) |
807 | rw++; |
808 | rh = 1; |
809 | while (y+rh < state->h && !hedge(state,x,y+rh)) |
810 | rh++; |
811 | |
812 | /* |
813 | * We know what the dimensions of the rectangle |
814 | * should be if it's there at all. Find out if we |
815 | * really have a valid rectangle. |
816 | */ |
817 | valid = TRUE; |
818 | /* Check the horizontal edges. */ |
819 | for (xx = x; xx < x+rw; xx++) { |
820 | for (yy = y; yy <= y+rh; yy++) { |
821 | int e = !HRANGE(state,xx,yy) || hedge(state,xx,yy); |
822 | int ec = (yy == y || yy == y+rh); |
823 | if (e != ec) |
824 | valid = FALSE; |
825 | } |
826 | } |
827 | /* Check the vertical edges. */ |
828 | for (yy = y; yy < y+rh; yy++) { |
829 | for (xx = x; xx <= x+rw; xx++) { |
830 | int e = !VRANGE(state,xx,yy) || vedge(state,xx,yy); |
831 | int ec = (xx == x || xx == x+rw); |
832 | if (e != ec) |
833 | valid = FALSE; |
834 | } |
835 | } |
836 | |
837 | /* |
838 | * If this is not a valid rectangle with no other |
839 | * edges inside it, we just mark this square as not |
840 | * complete and proceed to the next square. |
841 | */ |
842 | if (!valid) { |
843 | index(state, ret, x, y) = 0; |
844 | continue; |
845 | } |
846 | |
847 | /* |
848 | * We have a rectangle. Now see what its area is, |
849 | * and how many numbers are in it. |
850 | */ |
851 | num = 0; |
852 | area = 0; |
853 | for (xx = x; xx < x+rw; xx++) { |
854 | for (yy = y; yy < y+rh; yy++) { |
855 | area++; |
856 | if (grid(state,xx,yy)) { |
857 | if (num > 0) |
858 | valid = FALSE; /* two numbers */ |
859 | num = grid(state,xx,yy); |
860 | } |
861 | } |
862 | } |
863 | if (num != area) |
864 | valid = FALSE; |
865 | |
866 | /* |
867 | * Now fill in the whole rectangle based on the |
868 | * value of `valid'. |
869 | */ |
870 | for (xx = x; xx < x+rw; xx++) { |
871 | for (yy = y; yy < y+rh; yy++) { |
872 | index(state, ret, xx, yy) = valid; |
873 | } |
874 | } |
875 | } |
876 | |
877 | return ret; |
878 | } |
879 | |
08dd70c3 |
880 | struct game_ui { |
881 | /* |
882 | * These coordinates are 2 times the obvious grid coordinates. |
883 | * Hence, the top left of the grid is (0,0), the grid point to |
884 | * the right of that is (2,0), the one _below that_ is (2,2) |
885 | * and so on. This is so that we can specify a drag start point |
886 | * on an edge (one odd coordinate) or in the middle of a square |
887 | * (two odd coordinates) rather than always at a corner. |
888 | * |
889 | * -1,-1 means no drag is in progress. |
890 | */ |
891 | int drag_start_x; |
892 | int drag_start_y; |
893 | int drag_end_x; |
894 | int drag_end_y; |
895 | /* |
896 | * This flag is set as soon as a dragging action moves the |
897 | * mouse pointer away from its starting point, so that even if |
898 | * the pointer _returns_ to its starting point the action is |
899 | * treated as a small drag rather than a click. |
900 | */ |
901 | int dragged; |
902 | }; |
903 | |
74a4e547 |
904 | game_ui *new_ui(game_state *state) |
905 | { |
08dd70c3 |
906 | game_ui *ui = snew(game_ui); |
907 | ui->drag_start_x = -1; |
908 | ui->drag_start_y = -1; |
909 | ui->drag_end_x = -1; |
910 | ui->drag_end_y = -1; |
911 | ui->dragged = FALSE; |
912 | return ui; |
74a4e547 |
913 | } |
914 | |
915 | void free_ui(game_ui *ui) |
916 | { |
08dd70c3 |
917 | sfree(ui); |
918 | } |
919 | |
d4e7900f |
920 | void coord_round(float x, float y, int *xr, int *yr) |
08dd70c3 |
921 | { |
d4e7900f |
922 | float xs, ys, xv, yv, dx, dy, dist; |
08dd70c3 |
923 | |
924 | /* |
d4e7900f |
925 | * Find the nearest square-centre. |
08dd70c3 |
926 | */ |
d4e7900f |
927 | xs = (float)floor(x) + 0.5F; |
928 | ys = (float)floor(y) + 0.5F; |
08dd70c3 |
929 | |
930 | /* |
d4e7900f |
931 | * And find the nearest grid vertex. |
08dd70c3 |
932 | */ |
d4e7900f |
933 | xv = (float)floor(x + 0.5F); |
934 | yv = (float)floor(y + 0.5F); |
08dd70c3 |
935 | |
936 | /* |
d4e7900f |
937 | * We allocate clicks in parts of the grid square to either |
938 | * corners, edges or square centres, as follows: |
939 | * |
940 | * +--+--------+--+ |
941 | * | | | | |
942 | * +--+ +--+ |
943 | * | `. ,' | |
944 | * | +--+ | |
945 | * | | | | |
946 | * | +--+ | |
947 | * | ,' `. | |
948 | * +--+ +--+ |
949 | * | | | | |
950 | * +--+--------+--+ |
951 | * |
952 | * (Not to scale!) |
953 | * |
954 | * In other words: we measure the square distance (i.e. |
955 | * max(dx,dy)) from the click to the nearest corner, and if |
956 | * it's within CORNER_TOLERANCE then we return a corner click. |
957 | * We measure the square distance from the click to the nearest |
958 | * centre, and if that's within CENTRE_TOLERANCE we return a |
959 | * centre click. Failing that, we find which of the two edge |
960 | * centres is nearer to the click and return that edge. |
08dd70c3 |
961 | */ |
d4e7900f |
962 | |
963 | /* |
964 | * Check for corner click. |
965 | */ |
966 | dx = (float)fabs(x - xv); |
967 | dy = (float)fabs(y - yv); |
968 | dist = (dx > dy ? dx : dy); |
969 | if (dist < CORNER_TOLERANCE) { |
970 | *xr = 2 * (int)xv; |
971 | *yr = 2 * (int)yv; |
972 | } else { |
973 | /* |
974 | * Check for centre click. |
975 | */ |
976 | dx = (float)fabs(x - xs); |
977 | dy = (float)fabs(y - ys); |
978 | dist = (dx > dy ? dx : dy); |
979 | if (dist < CENTRE_TOLERANCE) { |
980 | *xr = 1 + 2 * (int)xs; |
981 | *yr = 1 + 2 * (int)ys; |
982 | } else { |
983 | /* |
984 | * Failing both of those, see which edge we're closer to. |
985 | * Conveniently, this is simply done by testing the relative |
986 | * magnitude of dx and dy (which are currently distances from |
987 | * the square centre). |
988 | */ |
989 | if (dx > dy) { |
990 | /* Vertical edge: x-coord of corner, |
991 | * y-coord of square centre. */ |
992 | *xr = 2 * (int)xv; |
993 | *yr = 1 + 2 * (int)ys; |
994 | } else { |
995 | /* Horizontal edge: x-coord of square centre, |
996 | * y-coord of corner. */ |
997 | *xr = 1 + 2 * (int)xs; |
998 | *yr = 2 * (int)yv; |
999 | } |
1000 | } |
1001 | } |
08dd70c3 |
1002 | } |
1003 | |
1004 | static void ui_draw_rect(game_state *state, game_ui *ui, |
1005 | unsigned char *hedge, unsigned char *vedge, int c) |
1006 | { |
1007 | int x1, x2, y1, y2, x, y, t; |
1008 | |
1009 | x1 = ui->drag_start_x; |
1010 | x2 = ui->drag_end_x; |
1011 | if (x2 < x1) { t = x1; x1 = x2; x2 = t; } |
1012 | |
1013 | y1 = ui->drag_start_y; |
1014 | y2 = ui->drag_end_y; |
1015 | if (y2 < y1) { t = y1; y1 = y2; y2 = t; } |
1016 | |
1017 | x1 = x1 / 2; /* rounds down */ |
1018 | x2 = (x2+1) / 2; /* rounds up */ |
1019 | y1 = y1 / 2; /* rounds down */ |
1020 | y2 = (y2+1) / 2; /* rounds up */ |
1021 | |
1022 | /* |
1023 | * Draw horizontal edges of rectangles. |
1024 | */ |
1025 | for (x = x1; x < x2; x++) |
1026 | for (y = y1; y <= y2; y++) |
1027 | if (HRANGE(state,x,y)) { |
1028 | int val = index(state,hedge,x,y); |
1029 | if (y == y1 || y == y2) |
1030 | val = c; |
1031 | else if (c == 1) |
1032 | val = 0; |
1033 | index(state,hedge,x,y) = val; |
1034 | } |
1035 | |
1036 | /* |
1037 | * Draw vertical edges of rectangles. |
1038 | */ |
1039 | for (y = y1; y < y2; y++) |
1040 | for (x = x1; x <= x2; x++) |
1041 | if (VRANGE(state,x,y)) { |
1042 | int val = index(state,vedge,x,y); |
1043 | if (x == x1 || x == x2) |
1044 | val = c; |
1045 | else if (c == 1) |
1046 | val = 0; |
1047 | index(state,vedge,x,y) = val; |
1048 | } |
74a4e547 |
1049 | } |
1050 | |
1051 | game_state *make_move(game_state *from, game_ui *ui, int x, int y, int button) |
3870c4d8 |
1052 | { |
08dd70c3 |
1053 | int xc, yc; |
1054 | int startdrag = FALSE, enddrag = FALSE, active = FALSE; |
3870c4d8 |
1055 | game_state *ret; |
1056 | |
08dd70c3 |
1057 | if (button == LEFT_BUTTON) { |
1058 | startdrag = TRUE; |
1059 | } else if (button == LEFT_RELEASE) { |
1060 | enddrag = TRUE; |
1061 | } else if (button != LEFT_DRAG) { |
1062 | return NULL; |
1063 | } |
1064 | |
d4e7900f |
1065 | coord_round(FROMCOORD((float)x), FROMCOORD((float)y), &xc, &yc); |
08dd70c3 |
1066 | |
1067 | if (startdrag) { |
1068 | ui->drag_start_x = xc; |
1069 | ui->drag_start_y = yc; |
1070 | ui->drag_end_x = xc; |
1071 | ui->drag_end_y = yc; |
1072 | ui->dragged = FALSE; |
1073 | active = TRUE; |
1074 | } |
3870c4d8 |
1075 | |
08dd70c3 |
1076 | if (xc != ui->drag_end_x || yc != ui->drag_end_y) { |
1077 | ui->drag_end_x = xc; |
1078 | ui->drag_end_y = yc; |
1079 | ui->dragged = TRUE; |
1080 | active = TRUE; |
1081 | } |
3870c4d8 |
1082 | |
934797c7 |
1083 | ret = NULL; |
1084 | |
1085 | if (enddrag) { |
1086 | if (xc >= 0 && xc <= 2*from->w && |
1087 | yc >= 0 && yc <= 2*from->h) { |
1088 | ret = dup_game(from); |
1089 | |
1090 | if (ui->dragged) { |
1091 | ui_draw_rect(ret, ui, ret->hedge, ret->vedge, 1); |
1092 | } else { |
1093 | if ((xc & 1) && !(yc & 1) && HRANGE(from,xc/2,yc/2)) { |
1094 | hedge(ret,xc/2,yc/2) = !hedge(ret,xc/2,yc/2); |
1095 | } |
1096 | if ((yc & 1) && !(xc & 1) && VRANGE(from,xc/2,yc/2)) { |
1097 | vedge(ret,xc/2,yc/2) = !vedge(ret,xc/2,yc/2); |
1098 | } |
1099 | } |
3870c4d8 |
1100 | |
934797c7 |
1101 | if (!memcmp(ret->hedge, from->hedge, from->w*from->h) && |
1102 | !memcmp(ret->vedge, from->vedge, from->w*from->h)) { |
1103 | free_game(ret); |
1104 | ret = NULL; |
1105 | } |
ef29354c |
1106 | |
1107 | /* |
1108 | * We've made a real change to the grid. Check to see |
1109 | * if the game has been completed. |
1110 | */ |
d4e7900f |
1111 | if (ret && !ret->completed) { |
ef29354c |
1112 | int x, y, ok; |
1113 | unsigned char *correct = get_correct(ret); |
1114 | |
1115 | ok = TRUE; |
1116 | for (x = 0; x < ret->w; x++) |
1117 | for (y = 0; y < ret->h; y++) |
1118 | if (!index(ret, correct, x, y)) |
1119 | ok = FALSE; |
1120 | |
1121 | sfree(correct); |
1122 | |
1123 | if (ok) |
1124 | ret->completed = TRUE; |
1125 | } |
934797c7 |
1126 | } |
1127 | |
1128 | ui->drag_start_x = -1; |
1129 | ui->drag_start_y = -1; |
1130 | ui->drag_end_x = -1; |
1131 | ui->drag_end_y = -1; |
1132 | ui->dragged = FALSE; |
1133 | active = TRUE; |
3870c4d8 |
1134 | } |
1135 | |
934797c7 |
1136 | if (ret) |
1137 | return ret; /* a move has been made */ |
1138 | else if (active) |
08dd70c3 |
1139 | return from; /* UI activity has occurred */ |
934797c7 |
1140 | else |
1141 | return NULL; |
3870c4d8 |
1142 | } |
1143 | |
1144 | /* ---------------------------------------------------------------------- |
1145 | * Drawing routines. |
1146 | */ |
1147 | |
ec9a0f09 |
1148 | #define CORRECT 65536 |
08dd70c3 |
1149 | |
1150 | #define COLOUR(k) ( (k)==1 ? COL_LINE : COL_DRAG ) |
1151 | #define MAX(x,y) ( (x)>(y) ? (x) : (y) ) |
1152 | #define MAX4(x,y,z,w) ( MAX(MAX(x,y),MAX(z,w)) ) |
3870c4d8 |
1153 | |
1154 | struct game_drawstate { |
1155 | int started; |
1156 | int w, h; |
ec9a0f09 |
1157 | unsigned int *visible; |
3870c4d8 |
1158 | }; |
1159 | |
1160 | void game_size(game_params *params, int *x, int *y) |
1161 | { |
1162 | *x = params->w * TILE_SIZE + 2*BORDER + 1; |
1163 | *y = params->h * TILE_SIZE + 2*BORDER + 1; |
1164 | } |
1165 | |
1166 | float *game_colours(frontend *fe, game_state *state, int *ncolours) |
1167 | { |
1168 | float *ret = snewn(3 * NCOLOURS, float); |
1169 | |
1170 | frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]); |
1171 | |
1172 | ret[COL_GRID * 3 + 0] = 0.5F * ret[COL_BACKGROUND * 3 + 0]; |
1173 | ret[COL_GRID * 3 + 1] = 0.5F * ret[COL_BACKGROUND * 3 + 1]; |
1174 | ret[COL_GRID * 3 + 2] = 0.5F * ret[COL_BACKGROUND * 3 + 2]; |
1175 | |
08dd70c3 |
1176 | ret[COL_DRAG * 3 + 0] = 1.0F; |
1177 | ret[COL_DRAG * 3 + 1] = 0.0F; |
1178 | ret[COL_DRAG * 3 + 2] = 0.0F; |
1179 | |
3870c4d8 |
1180 | ret[COL_CORRECT * 3 + 0] = 0.75F * ret[COL_BACKGROUND * 3 + 0]; |
1181 | ret[COL_CORRECT * 3 + 1] = 0.75F * ret[COL_BACKGROUND * 3 + 1]; |
1182 | ret[COL_CORRECT * 3 + 2] = 0.75F * ret[COL_BACKGROUND * 3 + 2]; |
1183 | |
1184 | ret[COL_LINE * 3 + 0] = 0.0F; |
1185 | ret[COL_LINE * 3 + 1] = 0.0F; |
1186 | ret[COL_LINE * 3 + 2] = 0.0F; |
1187 | |
1188 | ret[COL_TEXT * 3 + 0] = 0.0F; |
1189 | ret[COL_TEXT * 3 + 1] = 0.0F; |
1190 | ret[COL_TEXT * 3 + 2] = 0.0F; |
1191 | |
1192 | *ncolours = NCOLOURS; |
1193 | return ret; |
1194 | } |
1195 | |
1196 | game_drawstate *game_new_drawstate(game_state *state) |
1197 | { |
1198 | struct game_drawstate *ds = snew(struct game_drawstate); |
08dd70c3 |
1199 | int i; |
3870c4d8 |
1200 | |
1201 | ds->started = FALSE; |
1202 | ds->w = state->w; |
1203 | ds->h = state->h; |
ec9a0f09 |
1204 | ds->visible = snewn(ds->w * ds->h, unsigned int); |
08dd70c3 |
1205 | for (i = 0; i < ds->w * ds->h; i++) |
1206 | ds->visible[i] = 0xFFFF; |
3870c4d8 |
1207 | |
1208 | return ds; |
1209 | } |
1210 | |
1211 | void game_free_drawstate(game_drawstate *ds) |
1212 | { |
1213 | sfree(ds->visible); |
1214 | sfree(ds); |
1215 | } |
1216 | |
08dd70c3 |
1217 | void draw_tile(frontend *fe, game_state *state, int x, int y, |
ec9a0f09 |
1218 | unsigned char *hedge, unsigned char *vedge, |
1219 | unsigned char *corners, int correct) |
3870c4d8 |
1220 | { |
1221 | int cx = COORD(x), cy = COORD(y); |
1222 | char str[80]; |
1223 | |
1224 | draw_rect(fe, cx, cy, TILE_SIZE+1, TILE_SIZE+1, COL_GRID); |
1225 | draw_rect(fe, cx+1, cy+1, TILE_SIZE-1, TILE_SIZE-1, |
1226 | correct ? COL_CORRECT : COL_BACKGROUND); |
1227 | |
1228 | if (grid(state,x,y)) { |
1229 | sprintf(str, "%d", grid(state,x,y)); |
1230 | draw_text(fe, cx+TILE_SIZE/2, cy+TILE_SIZE/2, FONT_VARIABLE, |
105a00d0 |
1231 | TILE_SIZE/2, ALIGN_HCENTRE | ALIGN_VCENTRE, COL_TEXT, str); |
3870c4d8 |
1232 | } |
1233 | |
1234 | /* |
1235 | * Draw edges. |
1236 | */ |
08dd70c3 |
1237 | if (!HRANGE(state,x,y) || index(state,hedge,x,y)) |
1238 | draw_rect(fe, cx, cy, TILE_SIZE+1, 2, |
1239 | HRANGE(state,x,y) ? COLOUR(index(state,hedge,x,y)) : |
1240 | COL_LINE); |
1241 | if (!HRANGE(state,x,y+1) || index(state,hedge,x,y+1)) |
1242 | draw_rect(fe, cx, cy+TILE_SIZE-1, TILE_SIZE+1, 2, |
1243 | HRANGE(state,x,y+1) ? COLOUR(index(state,hedge,x,y+1)) : |
1244 | COL_LINE); |
1245 | if (!VRANGE(state,x,y) || index(state,vedge,x,y)) |
1246 | draw_rect(fe, cx, cy, 2, TILE_SIZE+1, |
1247 | VRANGE(state,x,y) ? COLOUR(index(state,vedge,x,y)) : |
1248 | COL_LINE); |
1249 | if (!VRANGE(state,x+1,y) || index(state,vedge,x+1,y)) |
1250 | draw_rect(fe, cx+TILE_SIZE-1, cy, 2, TILE_SIZE+1, |
1251 | VRANGE(state,x+1,y) ? COLOUR(index(state,vedge,x+1,y)) : |
1252 | COL_LINE); |
3870c4d8 |
1253 | |
1254 | /* |
1255 | * Draw corners. |
1256 | */ |
ec9a0f09 |
1257 | if (index(state,corners,x,y)) |
08dd70c3 |
1258 | draw_rect(fe, cx, cy, 2, 2, |
ec9a0f09 |
1259 | COLOUR(index(state,corners,x,y))); |
1260 | if (x+1 < state->w && index(state,corners,x+1,y)) |
08dd70c3 |
1261 | draw_rect(fe, cx+TILE_SIZE-1, cy, 2, 2, |
ec9a0f09 |
1262 | COLOUR(index(state,corners,x+1,y))); |
1263 | if (y+1 < state->h && index(state,corners,x,y+1)) |
08dd70c3 |
1264 | draw_rect(fe, cx, cy+TILE_SIZE-1, 2, 2, |
ec9a0f09 |
1265 | COLOUR(index(state,corners,x,y+1))); |
1266 | if (x+1 < state->w && y+1 < state->h && index(state,corners,x+1,y+1)) |
08dd70c3 |
1267 | draw_rect(fe, cx+TILE_SIZE-1, cy+TILE_SIZE-1, 2, 2, |
ec9a0f09 |
1268 | COLOUR(index(state,corners,x+1,y+1))); |
3870c4d8 |
1269 | |
1270 | draw_update(fe, cx, cy, TILE_SIZE+1, TILE_SIZE+1); |
1271 | } |
1272 | |
1273 | void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, |
74a4e547 |
1274 | game_state *state, game_ui *ui, |
1275 | float animtime, float flashtime) |
3870c4d8 |
1276 | { |
1277 | int x, y; |
1278 | unsigned char *correct; |
ec9a0f09 |
1279 | unsigned char *hedge, *vedge, *corners; |
3870c4d8 |
1280 | |
1281 | correct = get_correct(state); |
1282 | |
08dd70c3 |
1283 | if (ui->dragged) { |
1284 | hedge = snewn(state->w*state->h, unsigned char); |
1285 | vedge = snewn(state->w*state->h, unsigned char); |
1286 | memcpy(hedge, state->hedge, state->w*state->h); |
1287 | memcpy(vedge, state->vedge, state->w*state->h); |
1288 | ui_draw_rect(state, ui, hedge, vedge, 2); |
1289 | } else { |
1290 | hedge = state->hedge; |
1291 | vedge = state->vedge; |
1292 | } |
1293 | |
ec9a0f09 |
1294 | corners = snewn(state->w * state->h, unsigned char); |
1295 | memset(corners, 0, state->w * state->h); |
1296 | for (x = 0; x < state->w; x++) |
1297 | for (y = 0; y < state->h; y++) { |
1298 | if (x > 0) { |
1299 | int e = index(state, vedge, x, y); |
1300 | if (index(state,corners,x,y) < e) |
1301 | index(state,corners,x,y) = e; |
1302 | if (y+1 < state->h && |
1303 | index(state,corners,x,y+1) < e) |
1304 | index(state,corners,x,y+1) = e; |
1305 | } |
1306 | if (y > 0) { |
1307 | int e = index(state, hedge, x, y); |
1308 | if (index(state,corners,x,y) < e) |
1309 | index(state,corners,x,y) = e; |
1310 | if (x+1 < state->w && |
1311 | index(state,corners,x+1,y) < e) |
1312 | index(state,corners,x+1,y) = e; |
1313 | } |
1314 | } |
1315 | |
3870c4d8 |
1316 | if (!ds->started) { |
105a00d0 |
1317 | draw_rect(fe, 0, 0, |
1318 | state->w * TILE_SIZE + 2*BORDER + 1, |
1319 | state->h * TILE_SIZE + 2*BORDER + 1, COL_BACKGROUND); |
3870c4d8 |
1320 | draw_rect(fe, COORD(0)-1, COORD(0)-1, |
1321 | ds->w*TILE_SIZE+3, ds->h*TILE_SIZE+3, COL_LINE); |
1322 | ds->started = TRUE; |
863c3945 |
1323 | draw_update(fe, 0, 0, |
1324 | state->w * TILE_SIZE + 2*BORDER + 1, |
1325 | state->h * TILE_SIZE + 2*BORDER + 1); |
3870c4d8 |
1326 | } |
1327 | |
1328 | for (x = 0; x < state->w; x++) |
1329 | for (y = 0; y < state->h; y++) { |
ec9a0f09 |
1330 | unsigned int c = 0; |
08dd70c3 |
1331 | |
1332 | if (HRANGE(state,x,y)) |
1333 | c |= index(state,hedge,x,y); |
eddb22e8 |
1334 | if (HRANGE(state,x,y+1)) |
1335 | c |= index(state,hedge,x,y+1) << 2; |
08dd70c3 |
1336 | if (VRANGE(state,x,y)) |
1337 | c |= index(state,vedge,x,y) << 4; |
eddb22e8 |
1338 | if (VRANGE(state,x+1,y)) |
1339 | c |= index(state,vedge,x+1,y) << 6; |
ec9a0f09 |
1340 | c |= index(state,corners,x,y) << 8; |
1341 | if (x+1 < state->w) |
1342 | c |= index(state,corners,x+1,y) << 10; |
1343 | if (y+1 < state->h) |
1344 | c |= index(state,corners,x,y+1) << 12; |
1345 | if (x+1 < state->w && y+1 < state->h) |
1346 | c |= index(state,corners,x+1,y+1) << 14; |
ef29354c |
1347 | if (index(state, correct, x, y) && !flashtime) |
3870c4d8 |
1348 | c |= CORRECT; |
1349 | |
1350 | if (index(ds,ds->visible,x,y) != c) { |
ec9a0f09 |
1351 | draw_tile(fe, state, x, y, hedge, vedge, corners, c & CORRECT); |
1352 | index(ds,ds->visible,x,y) = c; |
3870c4d8 |
1353 | } |
1354 | } |
1355 | |
08dd70c3 |
1356 | if (hedge != state->hedge) { |
1357 | sfree(hedge); |
1358 | sfree(vedge); |
1359 | } |
1360 | |
11c44cf5 |
1361 | sfree(corners); |
3870c4d8 |
1362 | sfree(correct); |
1363 | } |
1364 | |
1365 | float game_anim_length(game_state *oldstate, game_state *newstate) |
1366 | { |
1367 | return 0.0F; |
1368 | } |
1369 | |
1370 | float game_flash_length(game_state *oldstate, game_state *newstate) |
1371 | { |
ef29354c |
1372 | if (!oldstate->completed && newstate->completed) |
1373 | return FLASH_TIME; |
3870c4d8 |
1374 | return 0.0F; |
1375 | } |
1376 | |
1377 | int game_wants_statusbar(void) |
1378 | { |
1379 | return FALSE; |
1380 | } |