Fiddle with the coordinate system to see if I can improve user
[sgt/puzzles] / rect.c
CommitLineData
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.
08dd70c3 31 *
32 * - During redraw, do corner analysis centrally in game_redraw()
33 * itself so that we can take it into account when computing the
34 * `visible' array. If we can do this, we can actually _turn on_
35 * the `visible' processing and keep redraws to the minimum
36 * required.
3870c4d8 37 */
38
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <assert.h>
43#include <math.h>
44
45#include "puzzles.h"
46
47const char *const game_name = "Rectangles";
48const int game_can_configure = TRUE;
49
50enum {
51 COL_BACKGROUND,
52 COL_CORRECT,
53 COL_LINE,
54 COL_TEXT,
55 COL_GRID,
08dd70c3 56 COL_DRAG,
3870c4d8 57 NCOLOURS
58};
59
60struct game_params {
61 int w, h;
62};
63
64#define INDEX(state, x, y) (((y) * (state)->w) + (x))
65#define index(state, a, x, y) ((a) [ INDEX(state,x,y) ])
66#define grid(state,x,y) index(state, (state)->grid, x, y)
67#define vedge(state,x,y) index(state, (state)->vedge, x, y)
68#define hedge(state,x,y) index(state, (state)->hedge, x, y)
69
70#define CRANGE(state,x,y,dx,dy) ( (x) >= dx && (x) < (state)->w && \
71 (y) >= dy && (y) < (state)->h )
72#define RANGE(state,x,y) CRANGE(state,x,y,0,0)
73#define HRANGE(state,x,y) CRANGE(state,x,y,0,1)
74#define VRANGE(state,x,y) CRANGE(state,x,y,1,0)
75
76#define TILE_SIZE 24
77#define BORDER 18
78
d4e7900f 79#define CORNER_TOLERANCE 0.15F
80#define CENTRE_TOLERANCE 0.15F
81
ef29354c 82#define FLASH_TIME 0.13F
83
3870c4d8 84#define COORD(x) ( (x) * TILE_SIZE + BORDER )
85#define FROMCOORD(x) ( ((x) - BORDER) / TILE_SIZE )
86
87struct game_state {
88 int w, h;
89 int *grid; /* contains the numbers */
90 unsigned char *vedge; /* (w+1) x h */
91 unsigned char *hedge; /* w x (h+1) */
ef29354c 92 int completed;
3870c4d8 93};
94
95game_params *default_params(void)
96{
97 game_params *ret = snew(game_params);
98
99 ret->w = ret->h = 7;
100
101 return ret;
102}
103
104int game_fetch_preset(int i, char **name, game_params **params)
105{
106 game_params *ret;
107 int w, h;
108 char buf[80];
109
110 switch (i) {
111 case 0: w = 7, h = 7; break;
112 case 1: w = 11, h = 11; break;
113 case 2: w = 15, h = 15; break;
114 case 3: w = 19, h = 19; break;
115 default: return FALSE;
116 }
117
118 sprintf(buf, "%dx%d", w, h);
119 *name = dupstr(buf);
120 *params = ret = snew(game_params);
121 ret->w = w;
122 ret->h = h;
123 return TRUE;
124}
125
126void free_params(game_params *params)
127{
128 sfree(params);
129}
130
131game_params *dup_params(game_params *params)
132{
133 game_params *ret = snew(game_params);
134 *ret = *params; /* structure copy */
135 return ret;
136}
137
138config_item *game_configure(game_params *params)
139{
140 config_item *ret;
141 char buf[80];
142
143 ret = snewn(5, config_item);
144
145 ret[0].name = "Width";
146 ret[0].type = C_STRING;
147 sprintf(buf, "%d", params->w);
148 ret[0].sval = dupstr(buf);
149 ret[0].ival = 0;
150
151 ret[1].name = "Height";
152 ret[1].type = C_STRING;
153 sprintf(buf, "%d", params->h);
154 ret[1].sval = dupstr(buf);
155 ret[1].ival = 0;
156
157 ret[2].name = NULL;
158 ret[2].type = C_END;
159 ret[2].sval = NULL;
160 ret[2].ival = 0;
161
162 return ret;
163}
164
165game_params *custom_params(config_item *cfg)
166{
167 game_params *ret = snew(game_params);
168
169 ret->w = atoi(cfg[0].sval);
170 ret->h = atoi(cfg[1].sval);
171
172 return ret;
173}
174
175char *validate_params(game_params *params)
176{
177 if (params->w <= 0 && params->h <= 0)
178 return "Width and height must both be greater than zero";
d4e7900f 179 if (params->w < 2 && params->h < 2)
180 return "Grid area must be greater than one";
3870c4d8 181 return NULL;
182}
183
184struct rect {
185 int x, y;
186 int w, h;
187};
188
189struct rectlist {
190 struct rect *rects;
191 int n;
192};
193
194static struct rectlist *get_rectlist(game_params *params, int *grid)
195{
196 int rw, rh;
197 int x, y;
198 int maxarea;
199 struct rect *rects = NULL;
200 int nrects = 0, rectsize = 0;
201
202 /*
d4e7900f 203 * Maximum rectangle area is 1/6 of total grid size, unless
204 * this means we can't place any rectangles at all in which
205 * case we set it to 2 at minimum.
3870c4d8 206 */
207 maxarea = params->w * params->h / 6;
d4e7900f 208 if (maxarea < 2)
209 maxarea = 2;
3870c4d8 210
211 for (rw = 1; rw <= params->w; rw++)
212 for (rh = 1; rh <= params->h; rh++) {
213 if (rw * rh > maxarea)
214 continue;
215 if (rw * rh == 1)
216 continue;
217 for (x = 0; x <= params->w - rw; x++)
218 for (y = 0; y <= params->h - rh; y++) {
219 /*
220 * We have a candidate rectangle placement. See
221 * if it's unobstructed.
222 */
223 int xx, yy;
224 int ok;
225
226 ok = TRUE;
227 for (xx = x; xx < x+rw; xx++)
228 for (yy = y; yy < y+rh; yy++)
229 if (index(params, grid, xx, yy) >= 0) {
230 ok = FALSE;
231 goto break1; /* break both loops at once */
232 }
233 break1:
234
235 if (!ok)
236 continue;
237
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
263static void free_rectlist(struct rectlist *list)
264{
265 sfree(list->rects);
266 sfree(list);
267}
268
269static 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
284static 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
323static 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
374char *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
688char *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') {
700 squares += atoi(seed-1);
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
716game_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
759game_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
779void free_game(game_state *state)
780{
781 sfree(state->grid);
782 sfree(state->vedge);
783 sfree(state->hedge);
784 sfree(state);
785}
786
787static 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 880struct 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 904game_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
915void free_ui(game_ui *ui)
916{
08dd70c3 917 sfree(ui);
918}
919
d4e7900f 920void 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
1004static 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
1051game_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
08dd70c3 1148#define CORRECT 256
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
1154struct game_drawstate {
1155 int started;
1156 int w, h;
08dd70c3 1157 unsigned short *visible;
3870c4d8 1158};
1159
1160void 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
1166float *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
1196game_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;
08dd70c3 1204 ds->visible = snewn(ds->w * ds->h, unsigned short);
1205 for (i = 0; i < ds->w * ds->h; i++)
1206 ds->visible[i] = 0xFFFF;
3870c4d8 1207
1208 return ds;
1209}
1210
1211void game_free_drawstate(game_drawstate *ds)
1212{
1213 sfree(ds->visible);
1214 sfree(ds);
1215}
1216
08dd70c3 1217void draw_tile(frontend *fe, game_state *state, int x, int y,
1218 unsigned char *hedge, unsigned char *vedge, int correct)
3870c4d8 1219{
1220 int cx = COORD(x), cy = COORD(y);
1221 char str[80];
1222
1223 draw_rect(fe, cx, cy, TILE_SIZE+1, TILE_SIZE+1, COL_GRID);
1224 draw_rect(fe, cx+1, cy+1, TILE_SIZE-1, TILE_SIZE-1,
1225 correct ? COL_CORRECT : COL_BACKGROUND);
1226
1227 if (grid(state,x,y)) {
1228 sprintf(str, "%d", grid(state,x,y));
1229 draw_text(fe, cx+TILE_SIZE/2, cy+TILE_SIZE/2, FONT_VARIABLE,
105a00d0 1230 TILE_SIZE/2, ALIGN_HCENTRE | ALIGN_VCENTRE, COL_TEXT, str);
3870c4d8 1231 }
1232
1233 /*
1234 * Draw edges.
1235 */
08dd70c3 1236 if (!HRANGE(state,x,y) || index(state,hedge,x,y))
1237 draw_rect(fe, cx, cy, TILE_SIZE+1, 2,
1238 HRANGE(state,x,y) ? COLOUR(index(state,hedge,x,y)) :
1239 COL_LINE);
1240 if (!HRANGE(state,x,y+1) || index(state,hedge,x,y+1))
1241 draw_rect(fe, cx, cy+TILE_SIZE-1, TILE_SIZE+1, 2,
1242 HRANGE(state,x,y+1) ? COLOUR(index(state,hedge,x,y+1)) :
1243 COL_LINE);
1244 if (!VRANGE(state,x,y) || index(state,vedge,x,y))
1245 draw_rect(fe, cx, cy, 2, TILE_SIZE+1,
1246 VRANGE(state,x,y) ? COLOUR(index(state,vedge,x,y)) :
1247 COL_LINE);
1248 if (!VRANGE(state,x+1,y) || index(state,vedge,x+1,y))
1249 draw_rect(fe, cx+TILE_SIZE-1, cy, 2, TILE_SIZE+1,
1250 VRANGE(state,x+1,y) ? COLOUR(index(state,vedge,x+1,y)) :
1251 COL_LINE);
3870c4d8 1252
1253 /*
1254 * Draw corners.
1255 */
08dd70c3 1256 if ((HRANGE(state,x-1,y) && index(state,hedge,x-1,y)) ||
1257 (VRANGE(state,x,y-1) && index(state,vedge,x,y-1)))
1258 draw_rect(fe, cx, cy, 2, 2,
1259 COLOUR(MAX4(index(state,hedge,x-1,y),
1260 index(state,vedge,x,y-1),
1261 index(state,hedge,x,y),
1262 index(state,vedge,x,y))));
1263 if ((HRANGE(state,x+1,y) && index(state,hedge,x+1,y)) ||
1264 (VRANGE(state,x+1,y-1) && index(state,vedge,x+1,y-1)))
1265 draw_rect(fe, cx+TILE_SIZE-1, cy, 2, 2,
1266 COLOUR(MAX4(index(state,hedge,x+1,y),
1267 index(state,vedge,x+1,y-1),
1268 index(state,hedge,x,y),
1269 index(state,vedge,x+1,y))));
1270 if ((HRANGE(state,x-1,y+1) && index(state,hedge,x-1,y+1)) ||
1271 (VRANGE(state,x,y+1) && index(state,vedge,x,y+1)))
1272 draw_rect(fe, cx, cy+TILE_SIZE-1, 2, 2,
1273 COLOUR(MAX4(index(state,hedge,x-1,y+1),
1274 index(state,vedge,x,y+1),
1275 index(state,hedge,x,y+1),
1276 index(state,vedge,x,y))));
1277 if ((HRANGE(state,x+1,y+1) && index(state,hedge,x+1,y+1)) ||
1278 (VRANGE(state,x+1,y+1) && index(state,vedge,x+1,y+1)))
1279 draw_rect(fe, cx+TILE_SIZE-1, cy+TILE_SIZE-1, 2, 2,
1280 COLOUR(MAX4(index(state,hedge,x+1,y+1),
1281 index(state,vedge,x+1,y+1),
1282 index(state,hedge,x,y+1),
1283 index(state,vedge,x+1,y))));
3870c4d8 1284
1285 draw_update(fe, cx, cy, TILE_SIZE+1, TILE_SIZE+1);
1286}
1287
1288void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
74a4e547 1289 game_state *state, game_ui *ui,
1290 float animtime, float flashtime)
3870c4d8 1291{
1292 int x, y;
1293 unsigned char *correct;
08dd70c3 1294 unsigned char *hedge, *vedge;
3870c4d8 1295
1296 correct = get_correct(state);
1297
08dd70c3 1298 if (ui->dragged) {
1299 hedge = snewn(state->w*state->h, unsigned char);
1300 vedge = snewn(state->w*state->h, unsigned char);
1301 memcpy(hedge, state->hedge, state->w*state->h);
1302 memcpy(vedge, state->vedge, state->w*state->h);
1303 ui_draw_rect(state, ui, hedge, vedge, 2);
1304 } else {
1305 hedge = state->hedge;
1306 vedge = state->vedge;
1307 }
1308
3870c4d8 1309 if (!ds->started) {
105a00d0 1310 draw_rect(fe, 0, 0,
1311 state->w * TILE_SIZE + 2*BORDER + 1,
1312 state->h * TILE_SIZE + 2*BORDER + 1, COL_BACKGROUND);
3870c4d8 1313 draw_rect(fe, COORD(0)-1, COORD(0)-1,
1314 ds->w*TILE_SIZE+3, ds->h*TILE_SIZE+3, COL_LINE);
1315 ds->started = TRUE;
863c3945 1316 draw_update(fe, 0, 0,
1317 state->w * TILE_SIZE + 2*BORDER + 1,
1318 state->h * TILE_SIZE + 2*BORDER + 1);
3870c4d8 1319 }
1320
1321 for (x = 0; x < state->w; x++)
1322 for (y = 0; y < state->h; y++) {
08dd70c3 1323 unsigned short c = 0;
1324
1325 if (HRANGE(state,x,y))
1326 c |= index(state,hedge,x,y);
1327 if (HRANGE(state,x+1,y))
1328 c |= index(state,hedge,x+1,y) << 2;
1329 if (VRANGE(state,x,y))
1330 c |= index(state,vedge,x,y) << 4;
1331 if (VRANGE(state,x,y+1))
1332 c |= index(state,vedge,x,y+1) << 6;
ef29354c 1333 if (index(state, correct, x, y) && !flashtime)
3870c4d8 1334 c |= CORRECT;
1335
1336 if (index(ds,ds->visible,x,y) != c) {
08dd70c3 1337 draw_tile(fe, state, x, y, hedge, vedge, c & CORRECT);
1338 /* index(ds,ds->visible,x,y) = c; */
3870c4d8 1339 }
1340 }
1341
08dd70c3 1342 if (hedge != state->hedge) {
1343 sfree(hedge);
1344 sfree(vedge);
1345 }
1346
3870c4d8 1347 sfree(correct);
1348}
1349
1350float game_anim_length(game_state *oldstate, game_state *newstate)
1351{
1352 return 0.0F;
1353}
1354
1355float game_flash_length(game_state *oldstate, game_state *newstate)
1356{
ef29354c 1357 if (!oldstate->completed && newstate->completed)
1358 return FLASH_TIME;
3870c4d8 1359 return 0.0F;
1360}
1361
1362int game_wants_statusbar(void)
1363{
1364 return FALSE;
1365}