shithub: puzzles

Download patch

ref: 118abb4fc9365807ad4174588eeb9aefda550415
parent: 101324af67c5e7819849daeafb337dd4c1f45828
author: Simon Tatham <[email protected]>
date: Thu Jun 30 05:07:00 EDT 2005

General robustness patch from James Harvey:
 - most game_size() functions now work in doubles internally and
   round to nearest, meaning that they have less tendency to try to
   alter a size they returned happily from a previous call
 - couple of fiddly fixes (memory leaks, precautionary casts in
   printf argument lists)
 - midend_deserialise() now constructs an appropriate drawstate,
   which I can't think how I overlooked myself since I _thought_ I
   went through the entire midend structure field by field!

[originally from svn r6041]

--- a/fifteen.c
+++ b/fifteen.c
@@ -566,19 +566,19 @@
 static void game_size(game_params *params, game_drawstate *ds,
                       int *x, int *y, int expand)
 {
-    int tsx, tsy, ts;
+    double tsx, tsy, ts;
     /*
      * Each window dimension equals the tile size times one more
      * than the grid dimension (the border is half the width of the
      * tiles).
      */
-    tsx = *x / (params->w + 1);
-    tsy = *y / (params->h + 1);
+    tsx = (double)*x / ((double)params->w + 1.0);
+    tsy = (double)*y / ((double)params->h + 1.0);
     ts = min(tsx, tsy);
     if (expand)
-        ds->tilesize = ts;
+        ds->tilesize = (int)(ts + 0.5);
     else
-        ds->tilesize = min(ts, PREFERRED_TILE_SIZE);
+        ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
 
     *x = TILE_SIZE * params->w + 2 * BORDER;
     *y = TILE_SIZE * params->h + 2 * BORDER;
--- a/flip.c
+++ b/flip.c
@@ -957,19 +957,19 @@
 static void game_size(game_params *params, game_drawstate *ds,
                       int *x, int *y, int expand)
 {
-    int tsx, tsy, ts;
+    double tsx, tsy, ts;
     /*
      * Each window dimension equals the tile size times one more
      * than the grid dimension (the border is half the width of the
      * tiles).
      */
-    tsx = *x / (params->w + 1);
-    tsy = *y / (params->h + 1);
+    tsx = (double)*x / ((double)params->w + 1.0);
+    tsy = (double)*y / ((double)params->h + 1);
     ts = min(tsx, tsy);
     if (expand)
-        ds->tilesize = ts;
+        ds->tilesize = (int)(ts + 0.5);
     else
-        ds->tilesize = min(ts, PREFERRED_TILE_SIZE);
+        ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
 
     *x = TILE_SIZE * params->w + 2 * BORDER;
     *y = TILE_SIZE * params->h + 2 * BORDER;
--- a/midend.c
+++ b/midend.c
@@ -149,6 +149,7 @@
     random_free(me->random);
     sfree(me->states);
     sfree(me->desc);
+    sfree(me->privdesc);
     sfree(me->seedstr);
     sfree(me->aux_info);
     me->ourgame->free_params(me->params);
@@ -1351,7 +1352,7 @@
                 uistr = val;
                 val = NULL;
             } else if (!strcmp(key, "TIME")) {
-                elapsed = strtod(val, NULL);
+                elapsed = atof(val);
             } else if (!strcmp(key, "NSTATES")) {
                 nstates = atoi(val);
                 if (nstates <= 0) {
@@ -1515,6 +1516,12 @@
     me->pressed_mouse_button = 0;
 
     midend_set_timer(me);
+
+    if (me->drawstate)
+        me->ourgame->free_drawstate(me->drawstate);
+    me->drawstate =
+        me->ourgame->new_drawstate(me->states[me->statepos-1].state);
+    midend_size_new_drawstate(me);
 
     ret = NULL;                        /* success! */
 
--- a/mines.c
+++ b/mines.c
@@ -2607,19 +2607,19 @@
 static void game_size(game_params *params, game_drawstate *ds,
                       int *x, int *y, int expand)
 {
-    int tsx, tsy, ts;
+    double tsx, tsy, ts;
     /*
      * Each window dimension equals the tile size times 3 more than
      * the grid dimension (the border is 3/2 the width of the
      * tiles).
      */
-    tsx = *x / (params->w + 3);
-    tsy = *y / (params->h + 3);
+    tsx = (double)*x / ((double)params->w + 3.0);
+    tsy = (double)*y / ((double)params->h + 3.0);
     ts = min(tsx, tsy);
     if (expand)
-        ds->tilesize = ts;
+        ds->tilesize = (int)(ts + 0.5);
     else
-        ds->tilesize = min(ts, PREFERRED_TILE_SIZE);
+        ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
 
     *x = BORDER * 2 + TILE_SIZE * params->w;
     *y = BORDER * 2 + TILE_SIZE * params->h;
--- a/pattern.c
+++ b/pattern.c
@@ -30,7 +30,7 @@
         ( ((x) - (BORDER + GUTTER + TILE_SIZE * TLBORDER(d))) / TILE_SIZE )
 
 #define SIZE(d) (2*BORDER + GUTTER + TILE_SIZE * (TLBORDER(d) + (d)))
-#define GETTILESIZE(d, w) (w / (2 + TLBORDER(d) + (d)))
+#define GETTILESIZE(d, w) ((double)w / (2.0 + (double)TLBORDER(d) + (double)(d)))
 
 #define TOCOORD(d, x) (BORDER + GUTTER + TILE_SIZE * (TLBORDER(d) + (x)))
 
@@ -549,6 +549,7 @@
     assert(desc[desclen-1] == '/');
     desc[desclen-1] = '\0';
     sfree(rowdata);
+    sfree(grid);
     return desc;
 }
 
@@ -858,8 +859,8 @@
         if (move_needed) {
 	    char buf[80];
 	    sprintf(buf, "%c%d,%d,%d,%d",
-		    (ui->state == GRID_FULL ? 'F' :
-		     ui->state == GRID_EMPTY ? 'E' : 'U'),
+		    (char)(ui->state == GRID_FULL ? 'F' :
+		           ui->state == GRID_EMPTY ? 'E' : 'U'),
 		    x1, y1, x2-x1+1, y2-y1+1);
 	    return dupstr(buf);
         } else
@@ -947,13 +948,13 @@
 static void game_size(game_params *params, game_drawstate *ds,
                       int *x, int *y, int expand)
 {
-    int ts;
+    double ts;
 
     ts = min(GETTILESIZE(params->w, *x), GETTILESIZE(params->h, *y));
     if (expand)
-        ds->tilesize = ts;
+        ds->tilesize = (int)(ts + 0.5);
     else
-        ds->tilesize = min(ts, PREFERRED_TILE_SIZE);
+        ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
 
     *x = SIZE(params->w);
     *y = SIZE(params->h);
--- a/rect.c
+++ b/rect.c
@@ -2498,7 +2498,7 @@
 static void game_size(game_params *params, game_drawstate *ds,
                       int *x, int *y, int expand)
 {
-    int tsx, tsy, ts;
+    double tsx, tsy, ts;
     /*
      * Each window dimension equals the tile size times 1.5 more
      * than the grid dimension (the border is 3/4 the width of the
@@ -2507,13 +2507,13 @@
      * We must cast to unsigned before multiplying by two, because
      * *x might be INT_MAX.
      */
-    tsx = 2 * (unsigned)*x / (2 * params->w + 3);
-    tsy = 2 * (unsigned)*y / (2 * params->h + 3);
+    tsx = 2.0 * (double)*x / (2.0 * (double)params->w + 3.0);
+    tsy = 2.0 * (double)*y / (2.0 * (double)params->h + 3.0);
     ts = min(tsx, tsy);
     if (expand)
-        ds->tilesize = ts;
+        ds->tilesize = (int)(ts + 0.5);
     else
-        ds->tilesize = min(ts, PREFERRED_TILE_SIZE);
+        ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
 
     *x = params->w * TILE_SIZE + 2*BORDER + 1;
     *y = params->h * TILE_SIZE + 2*BORDER + 1;
--- a/samegame.c
+++ b/samegame.c
@@ -689,7 +689,7 @@
 static void game_size(game_params *params, game_drawstate *ds, int *x, int *y,
                       int expand)
 {
-    int tsx, tsy, ts;
+    double tsx, tsy, ts;
 
     /*
      * We could choose the tile gap dynamically as well if we
@@ -708,14 +708,14 @@
      * We must cast to unsigned before adding to *x and *y, since
      * they might be INT_MAX!
      */
-    tsx = (unsigned)(*x + ds->tilegap) / (params->w + 1);
-    tsy = (unsigned)(*y + ds->tilegap) / (params->h + 1);
+    tsx = ((double)*x + (double)ds->tilegap) / ((double)params->w + 1.0);
+    tsy = ((double)*y + (double)ds->tilegap) / ((double)params->h + 1.0);
 
     ts = min(tsx, tsy);
     if (expand)
-        ds->tileinner = ts - ds->tilegap;
+        ds->tileinner = (int)(ts+0.5) - ds->tilegap;
     else
-        ds->tileinner = min(ts, PREFERRED_TILE_SIZE) - ds->tilegap;
+        ds->tileinner = min((int)ts, PREFERRED_TILE_SIZE) - ds->tilegap;
 
     *x = TILE_SIZE * params->w + 2 * BORDER - TILE_GAP;
     *y = TILE_SIZE * params->h + 2 * BORDER - TILE_GAP;
--- a/solo.c
+++ b/solo.c
@@ -2024,7 +2024,7 @@
             return NULL;
 
 	sprintf(buf, "%c%d,%d,%d",
-		ui->hpencil && n > 0 ? 'P' : 'R', ui->hx, ui->hy, n);
+		(char)(ui->hpencil && n > 0 ? 'P' : 'R'), ui->hx, ui->hy, n);
 
 	ui->hx = ui->hy = -1;
 
@@ -2090,19 +2090,19 @@
  */
 
 #define SIZE(cr) ((cr) * TILE_SIZE + 2*BORDER + 1)
-#define GETTILESIZE(cr, w) ( (w-1) / (cr+1) )
+#define GETTILESIZE(cr, w) ( (double)(w-1) / (double)(cr+1) )
 
 static void game_size(game_params *params, game_drawstate *ds,
                       int *x, int *y, int expand)
 {
     int c = params->c, r = params->r, cr = c*r;
-    int ts;
+    double ts;
 
     ts = min(GETTILESIZE(cr, *x), GETTILESIZE(cr, *y));
     if (expand)
-        ds->tilesize = ts;
+        ds->tilesize = (int)(ts+0.5);
     else
-        ds->tilesize = min(ts, PREFERRED_TILE_SIZE);
+        ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
 
     *x = SIZE(cr);
     *y = SIZE(cr);
--- a/twiddle.c
+++ b/twiddle.c
@@ -747,19 +747,19 @@
 static void game_size(game_params *params, game_drawstate *ds,
                       int *x, int *y, int expand)
 {
-    int tsx, tsy, ts;
+    double tsx, tsy, ts;
     /*
      * Each window dimension equals the tile size times one more
      * than the grid dimension (the border is half the width of the
      * tiles).
      */
-    tsx = *x / (params->w + 1);
-    tsy = *y / (params->h + 1);
+    tsx = (double)*x / ((double)params->w + 1.0);
+    tsy = (double)*y / ((double)params->h + 1.0);
     ts = min(tsx, tsy);
     if (expand)
-        ds->tilesize = ts;
+        ds->tilesize = (int)(ts + 0.5);
     else
-        ds->tilesize = min(ts, PREFERRED_TILE_SIZE);
+        ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
 
     *x = TILE_SIZE * params->w + 2 * BORDER;
     *y = TILE_SIZE * params->h + 2 * BORDER;