chiark / gitweb /
copyright messages
[ypp-sc-tools.db-live.git] / pctb / structure.c
index f9f1e7f52b1c8cf08328fe8a05a0446f0bc3452f..8919e0b07e9f69a909819d381c6faa0537f579e0 100644 (file)
@@ -1,18 +1,32 @@
 /*
-  */
+ * Parsing of the structure of the YPP client's displayed image
+ */
+/*
+ *  This is part of ypp-sc-tools, a set of third-party tools for assisting
+ *  players of Yohoho Puzzle Pirates.
+ * 
+ *  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
+ * 
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ * 
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ * 
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * 
+ *  Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
+ *  are used without permission.  This program is not endorsed or
+ *  sponsored by Three Rings.
+ */
 
 #include "structure.h"
 
-
-typedef struct {
-  int x, y;
-} Point;
-
-typedef struct { /* both inclusive */
-  Point tl;
-  Point br;
-} Rect;
-
 static CanonImage *cim;
 
 static inline char get(int x, int y) { return cim->d[y * cim->w + x]; }
@@ -63,13 +77,50 @@ const CanonColourInfo canoncolourinfos[]= {
   { 0,0 }
 };
 
+
+static void mustfail1(const char *file, int line, const char *what) {
+  fprintf(stderr,
+         "\n\n"
+         "Unable to figure out contents YPP client display.\n"
+         " Check that your client is logged in has the correct display.\n"
+         " If that isn't the problem, please report this as a fault.\n\n"
+         "Technical details:"
+         " %s:%d: requirement failed: %s\n",
+         file, line, what);
+}
+static void mustfail2(void) NORET;
+static void mustfail2(void) {
+  fprintf(stderr, "\n\nGiving up.\n");
+  exit(8);
+}
+
+#define MUST(x, ifnot) do{                     \
+    if (!(x)) {                                        \
+      mustfail1(__FILE__,__LINE__,#x);         \
+      ifnot;                                   \
+      mustfail2();                             \
+    }                                          \
+  }while(0)
+
+#define MP(v) fprintf(stderr," %s=%d,%d",#v,(v).x,(v).y)
+#define MI(v) fprintf(stderr," %s=%d",   #v,(v))
+#define MC(v) fprintf(stderr," %s='%c'", #v,(v))
+#define MS(v) fprintf(stderr," %s=\"%s\"", #v,(v))
+#define MSB(v) fprintf(stderr," %s", (v))
+#define MR(v) fprintf(stderr," %s=%d,%d..%d,%d",\
+                      #v,(v).tl.x,(v).tl.y,(v).br.x,(v).br.y)
+
+
 static void require_rectangle(int tlx, int tly, int brx, int bry,
                              const char *ok) {
-  int x,y;
-  for (x=tlx; x<=brx; x++)
-    for (y=tly; y<=bry; y++) {
-      int c= get(x,y);
-      assert(strchr(ok,c));
+  Point p;
+  for (p.x=tlx; p.x<=brx; p.x++)
+    for (p.y=tly; p.y<=bry; p.y++) {
+      int c= get_p(p);
+      MUST( strchr(ok,c), ({
+            Rect rm={{tlx,tly},{brx,bry}};
+            MR(rm);MP(p);MS(ok);
+      }));
     }
 }
 static void require_rectangle_r(Rect rr, const char *ok) {
@@ -78,14 +129,13 @@ static void require_rectangle_r(Rect rr, const char *ok) {
 
 static void debug_rect(const char *what, int whati, Rect rr) {
   if (!DEBUGP(rect)) return;
-  int y,r,w;
+  int y,w;
   fprintf(debug, "%s %d: %d,%d..%d,%d:\n", what, whati,
          rr.tl.x,rr.tl.y, rr.br.x,rr.br.y);
   w= rr.br.x - rr.tl.x + 1;
   for (y=rr.tl.y; y<=rr.br.y; y++) {
     fprintf(debug, "%4d%*s|", y, rr.tl.x,"");
-    r= fwrite(cim->d + y*cim->w + rr.tl.x, 1, w, debug);
-    eassert(r==w);
+    fwrite(cim->d + y*cim->w + rr.tl.x, 1, w, debug);
     fputc('|',debug);
     fputc('\n',debug);
   }
@@ -102,8 +152,10 @@ static void debug_rect(const char *what, int whati, Rect rr) {
 #define WALK_UNTIL_MUST(point,coord,increm,last,edge)  \
   do {                                                 \
     WALK_UNTIL(point,coord,increm,last,edge);          \
-    eassert((point).coord != (last)+(increm));         \
-  } while(0)
+    MUST( (point).coord != (last)+(increm),            \
+         MP(point); MI(increm); MI(last); MC(edge);    \
+         );                                            \
+  }while(0)
 
 void find_structure(CanonImage *im) {
   cim= im;
@@ -169,9 +221,9 @@ void find_structure(CanonImage *im) {
   Point across= { mainr.tl.x, commbasey };
   int colno=0;
   for (;;) {
-    eassert(get_p(across) != '+');
+    MUST( get_p(across) != '+', MI(colno);MP(across);MR(mainr);MI(commbasey) );
     WALK_UNTIL(across, x,+1, mainr.br.x, '+');
-    eassert(colno < MAX_COLUMNS);
+    MUST( colno < MAX_COLUMNS, MP(across);MR(mainr);MI(commbasey); );
     int colrx= across.x;
     if (colrx > mainr.br.x) colrx= mainr.br.x;
     if (colno < INTERESTING_COLUMNS)
@@ -186,12 +238,12 @@ void find_structure(CanonImage *im) {
     require_rectangle(across.x,mainr.tl.y, across.x,mainr.br.y, "+");
     across.x++;
   }
-  eassert(colno >= MIN_COLUMNS);
+  MUST( colno >= MIN_COLUMNS, MI(colno);MR(mainr);MP(across); );
 
-#define SET_ONCE(var,val) do{                  \
-    int v= (val);                              \
-    if ((var)==-1) (var)= v;                   \
-    else eassert((var) == v);                  \
+#define SET_ONCE(var,val) do{                                          \
+    int v= (val);                                                      \
+    if ((var)==-1) (var)= v;                                           \
+    else MUST( (var) == v, MSB(#var);MI((var));MI(v);MR(mainr); );     \
   }while(0)
 
   SET_ONCE(columns, colno);
@@ -199,25 +251,28 @@ void find_structure(CanonImage *im) {
 }                  
 
 CanonImage *alloc_canon_image(int w, int h) {
-  CanonImage *im= malloc(sizeof(CanonImage) + w*h);
-  eassert(im);
+  CanonImage *im= mmalloc(sizeof(CanonImage) + w*h);
   im->w= w;
   im->h= h;
   memset(im->d,'?',w*h);
   return im;
 }
 
-CanonImage *file_read_image_ppm(FILE *f) {
+static void file_read_image_ppm(FILE *f) {
   struct pam inpam;
   unsigned char rgb_buf[3];
   CanonImage *im;
 
   pnm_readpaminit(f, &inpam, sizeof(inpam));
-  eassert(inpam.maxval == 255);
-  eassert(inpam.bytes_per_sample == 1);
+  if (!(inpam.maxval == 255 &&
+       inpam.bytes_per_sample == 1 &&
+       inpam.format == RPPM_FORMAT))
+    fatal("PNM screenshot(s) file must be 8bpp 1 byte per sample RGB");
 
   CANONICALISE_IMAGE(im, inpam.width, inpam.height, {
-    int r= fread(&rgb_buf,1,3,f);  eassert(r==3);
+    int r= fread(&rgb_buf,1,3,f);
+    sysassert(!ferror(f));
+    if (r!=3) fatal("PNM screenshot(s) file ends unexpectedly");
 
     rgb=
        ((unsigned long)rgb_buf[0]<<16) |
@@ -225,17 +280,39 @@ CanonImage *file_read_image_ppm(FILE *f) {
                       (rgb_buf[2]);
   });
 
-  return im;
+  sysassert(!ferror(screenshots_file));
+
+  if (!(npages < MAX_PAGES))
+    fatal("Too many images in screenshots file; max is %d.\n", MAX_PAGES);
+
+  page_images[npages++]= im;
 }
 
-void read_screenshots(void) {
-//  int c;
-//  while ((c= fgetc(screenshots_file) != EOF)) {
-//    ungetc(c, screenshots_file);
+void read_one_screenshot(void) {
+  progress("reading screenshot...");
+  file_read_image_ppm(screenshots_file);
+  progress_log("read screenshot.");
+}
 
-//    eassert(npages < MAX_PAGES);
-    page_images[npages++]= file_read_image_ppm(screenshots_file);
-//  }
+void read_screenshots(void) {
+  struct stat stab;
+  
+  sysassert(! fstat(fileno(screenshots_file), &stab) );
+  
+  for (;;) {
+    if (S_ISREG(stab.st_mode)) {
+      long pos= ftell(screenshots_file);
+      if (pos == stab.st_size) break;
+    } else {
+      int c= fgetc(screenshots_file);
+      if (c==EOF) break;
+      ungetc(c, screenshots_file);
+    }
+    progress("reading screenshot %d...",npages);
+    file_read_image_ppm(screenshots_file);
+  }
+  sysassert(!ferror(screenshots_file));
+  progress_log("read %d screenshots.",npages);
 }
 
 static void find_commodity(int offset, Rect *rr) {
@@ -243,13 +320,14 @@ static void find_commodity(int offset, Rect *rr) {
   rr->tl.y= commbasey - offset*comminty;
   rr->br.y= rr->tl.y + comminty-2;
   if (rr->tl.y < mainr.tl.y || rr->br.y > mainr.br.y) { rr->tl.x=-1; return; }
+  
+  rr->tl.x= mainr.tl.x;
+  rr->br.x= mainr.br.x;
+
   if (rr->tl.y > mainr.tl.y)
     require_rectangle(rr->tl.x,rr->tl.y-1, rr->br.x,rr->tl.y-1, "+");
   if (rr->br.y < mainr.tl.y)
     require_rectangle(rr->tl.x,rr->br.y+1, rr->br.x,rr->br.y+1, "+");
-  
-  rr->tl.x= mainr.tl.x;
-  rr->br.x= mainr.br.x;
 }
 
 static void find_table_entry(Rect commod, int colno, Rect *cellr) {
@@ -270,10 +348,14 @@ static void ocr_rectangle(Rect r, const OcrCellType ct) {
   for (x=0; x<w; x++) {
     Pixcol cx, rv;
     for (y=0, cx=0, rv=1; y<text_h; y++, rv<<=1) {
-      switch (get(x+r.tl.x, y+r.tl.y)) {
+      Point here= { x+r.tl.x, y+r.tl.y };
+      int pixel= get_p(here);
+      switch (pixel) {
       case ' ':           break;
       case 'o': cx |= rv; break;
-      default: eassert(!"wrong pixel");
+      default:
+       MUST(!"wrong pixel",
+            MC(pixel);MP(here);MSB(ocr_celltype_name(ct));MR(r); );
       }
     }
     cols[x]= cx;
@@ -312,8 +394,8 @@ void analyse(void) {
        tab= "\t";
       }
       fputs("\n", stdout);
-      eassert(!ferror(stdout));
-      eassert(!fflush(stdout));
+      sysassert(!ferror(stdout));
+      sysassert(!fflush(stdout));
     }
   }
 }