X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.db-live.git;a=blobdiff_plain;f=pctb%2Fstructure.c;h=8919e0b07e9f69a909819d381c6faa0537f579e0;hp=d768acd406151a7fccf55a510cbd2d1c9ede4af9;hb=f2c5d45ada202f2ad2640b260cac2fd7bb83eb3e;hpb=09e8b6a8735c8cd1f7f3ffbabc1a05f0a8f8ca75 diff --git a/pctb/structure.c b/pctb/structure.c index d768acd..8919e0b 100644 --- a/pctb/structure.c +++ b/pctb/structure.c @@ -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 + * + * 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 . + * + * 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,8 +251,7 @@ 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); @@ -213,11 +264,15 @@ static void file_read_image_ppm(FILE *f) { 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,10 +280,11 @@ static void file_read_image_ppm(FILE *f) { (rgb_buf[2]); }); - eassert(!ferror(screenshots_file)); - eassert(!feof(screenshots_file)); + sysassert(!ferror(screenshots_file)); + + if (!(npages < MAX_PAGES)) + fatal("Too many images in screenshots file; max is %d.\n", MAX_PAGES); - eassert(npages < MAX_PAGES); page_images[npages++]= im; } @@ -240,9 +296,8 @@ void read_one_screenshot(void) { void read_screenshots(void) { struct stat stab; - int r; - r= fstat(fileno(screenshots_file), &stab); eassert(!r); + sysassert(! fstat(fileno(screenshots_file), &stab) ); for (;;) { if (S_ISREG(stab.st_mode)) { @@ -256,7 +311,7 @@ void read_screenshots(void) { progress("reading screenshot %d...",npages); file_read_image_ppm(screenshots_file); } - eassert(!ferror(screenshots_file)); + sysassert(!ferror(screenshots_file)); progress_log("read %d screenshots.",npages); } @@ -293,10 +348,14 @@ static void ocr_rectangle(Rect r, const OcrCellType ct) { for (x=0; x