General robustness patch from James Harvey:
authorsimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Thu, 30 Jun 2005 09:07:00 +0000 (09:07 +0000)
committersimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Thu, 30 Jun 2005 09:07:00 +0000 (09:07 +0000)
 - 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!

git-svn-id: svn://svn.tartarus.org/sgt/puzzles@6041 cda61777-01e9-0310-a592-d414129be87e

fifteen.c
flip.c
midend.c
mines.c
pattern.c
rect.c
samegame.c
solo.c
twiddle.c

index 5da2548..bd791ea 100644 (file)
--- a/fifteen.c
+++ b/fifteen.c
@@ -566,19 +566,19 @@ static game_state *execute_move(game_state *from, char *move)
 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;
diff --git a/flip.c b/flip.c
index 4f78ec7..9ad3eb2 100644 (file)
--- a/flip.c
+++ b/flip.c
@@ -957,19 +957,19 @@ static game_state *execute_move(game_state *from, char *move)
 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;
index 15d11bb..8928fde 100644 (file)
--- a/midend.c
+++ b/midend.c
@@ -149,6 +149,7 @@ void midend_free(midend_data *me)
     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 @@ char *midend_deserialise(midend_data *me,
                 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) {
@@ -1516,6 +1517,12 @@ char *midend_deserialise(midend_data *me,
 
     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! */
 
     cleanup:
diff --git a/mines.c b/mines.c
index d2ec395..6cfb634 100644 (file)
--- a/mines.c
+++ b/mines.c
@@ -2607,19 +2607,19 @@ static game_state *execute_move(game_state *from, char *move)
 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;
index 19d16ff..b7c5334 100644 (file)
--- a/pattern.c
+++ b/pattern.c
@@ -30,7 +30,7 @@ enum {
         ( ((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 @@ static char *new_game_desc(game_params *params, random_state *rs,
     assert(desc[desclen-1] == '/');
     desc[desclen-1] = '\0';
     sfree(rowdata);
+    sfree(grid);
     return desc;
 }
 
@@ -858,8 +859,8 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
         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 game_state *execute_move(game_state *from, char *move)
 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);
diff --git a/rect.c b/rect.c
index 5852f59..f092a2d 100644 (file)
--- a/rect.c
+++ b/rect.c
@@ -2498,7 +2498,7 @@ static game_state *execute_move(game_state *from, char *move)
 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 @@ static void game_size(game_params *params, game_drawstate *ds,
      * 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;
index 18110f5..ed516e2 100644 (file)
@@ -689,7 +689,7 @@ static game_state *execute_move(game_state *from, char *move)
 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 @@ static void game_size(game_params *params, game_drawstate *ds, int *x, int *y,
      * 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;
diff --git a/solo.c b/solo.c
index bbe0c31..b9510e2 100644 (file)
--- a/solo.c
+++ b/solo.c
@@ -2024,7 +2024,7 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
             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 @@ static game_state *execute_move(game_state *from, char *move)
  */
 
 #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);
index 7e2b816..fc12411 100644 (file)
--- a/twiddle.c
+++ b/twiddle.c
@@ -747,19 +747,19 @@ static game_state *execute_move(game_state *from, char *move)
 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;