From be547a2dba10292fb3191bc5fec42212d2391660 Mon Sep 17 00:00:00 2001 From: rodri Date: Wed, 30 Aug 2023 22:07:50 +0000 Subject: forbid placing ships after being done with the layout. show the current ship and its length during placement. also made changes to some of the info banners. --- bts.c | 72 ++++++++++++++++++++++++++++++++++++++++-------------------------- btsd.c | 4 ++++ fns.h | 1 + util.c | 18 ++++++++++++++++- 4 files changed, 66 insertions(+), 29 deletions(-) diff --git a/bts.c b/bts.c index bedb343..14b5c2c 100644 --- a/bts.c +++ b/bts.c @@ -80,6 +80,7 @@ Point2 lastshot; struct { int state; + int layoutdone; } game; struct { @@ -158,6 +159,23 @@ csetcursor(Mousectl *mc, Cursor *c) oc = c; } +void +resetgame(void) +{ + int i; + + memset(localboard.map, Twater, MAPW*MAPH); + memset(alienboard.map, Twater, MAPW*MAPH); + for(i = 0; i < nelem(armada); i++){ + armada[i].bbox = ZR; + memset(armada[i].hit, 0, armada[i].ncells*sizeof(int)); + armada[i].sunk = 0; + } + curship = nil; + game.state = Waiting0; + game.layoutdone = 0; +} + Image * gettileimage(int type) { @@ -224,8 +242,9 @@ drawboard(Image *dst, Board *b) void drawinfo(Image *dst) { + static Image *c; Point p; - char *s; + char *s, aux[32]; s = nil; switch(game.state){ @@ -236,8 +255,22 @@ drawinfo(Image *dst) } if(s == nil) return; - p = Pt(SCRW/2 - stringwidth(font, s)/2, SCRH-Boardmargin); + p = Pt(SCRW/2 - stringwidth(font, s)/2, 0); string(dst, p, display->white, ZP, font, s); + + if(game.state == Outlaying){ + if(c == nil) + c = eallocimage(display, Rect(0,0,1,1), screen->chan, 1, DYellow); + if(curship != nil){ + snprint(aux, sizeof aux, "%s (%d)", shipname(curship-armada), curship->ncells); + p = Pt(SCRW/2 - stringwidth(font, aux)/2, SCRH-Boardmargin); + string(dst, p, c, ZP, font, aux); + }else{ + s = "done with the layout?"; + p = Pt(SCRW/2 - stringwidth(font, s)/2, SCRH-Boardmargin); + string(dst, p, c, ZP, font, s); + } + } } void @@ -252,7 +285,7 @@ redraw(void) drawinfo(screenb); if(conclusion.s != nil) - string(screenb, Pt(SCRW/2 - stringwidth(font, conclusion.s)/2, 0), conclusion.c, ZP, font, conclusion.s); + string(screenb, Pt(SCRW/2 - stringwidth(font, conclusion.s)/2, font->height+5), conclusion.c, ZP, font, conclusion.s); draw(screen, screen->r, screenb, nil, ZP); @@ -260,8 +293,9 @@ redraw(void) unlockdisplay(display); if(conclusion.s != nil){ - sleep(5000); + resetgame(); conclusion.s = nil; + sleep(5000); redraw(); } } @@ -374,21 +408,6 @@ initarmada(void) } } -void -resetgame(void) -{ - int i; - - memset(localboard.map, Twater, MAPW*MAPH); - memset(alienboard.map, Twater, MAPW*MAPH); - for(i = 0; i < nelem(armada); i++){ - armada[i].bbox = ZR; - memset(armada[i].hit, 0, armada[i].ncells*sizeof(int)); - armada[i].sunk = 0; - } - curship = nil; -} - int confirmdone(Mousectl *mc) { @@ -527,10 +546,11 @@ rmb(Mousectl *mc) mc->xy = addpt(mc->xy, screen->r.min); switch(menuhit(3, mc, &menu, _screen)){ case PLACESHIP: - curship = &armada[0]; + if(!game.layoutdone) + curship = &armada[0]; break; case DONE: - if(curship != nil) + if(curship != nil || game.layoutdone) break; if(!confirmdone(mc)) @@ -545,6 +565,7 @@ rmb(Mousectl *mc) cell2coords(armada[i].p), armada[i].orient == OH? 'h': 'v'); } chanprint(egress, "layout %s\n", buf); + game.layoutdone++; break; } send(drawchan, nil); @@ -672,15 +693,10 @@ processcmd(char *cmd) if(debug) fprint(2, "rcvd '%s'\n", cmd); - if(strcmp(cmd, "win") == 0){ + if(strcmp(cmd, "win") == 0) celebrate(); - resetgame(); - game.state = Waiting0; - }else if(strcmp(cmd, "lose") == 0){ + else if(strcmp(cmd, "lose") == 0) keelhaul(); - resetgame(); - game.state = Waiting0; - } switch(game.state){ case Waiting0: diff --git a/btsd.c b/btsd.c index 92346b2..6e4ac5e 100644 --- a/btsd.c +++ b/btsd.c @@ -252,6 +252,10 @@ matchmaker(void *) threadsetname("matchmaker"); for(;;){ + /* + * TODO make fairer matches + * locking playerq while checking nplayers and popping the couple. + */ if(playerq.nplayers < 2){ sleep(100); continue; diff --git a/fns.h b/fns.h index cea6e73..d013a60 100644 --- a/fns.h +++ b/fns.h @@ -18,3 +18,4 @@ void settiles(Map*, Point2, int, int, int); void fprintmap(int, Map*); int countshipcells(Map*); int shiplen(int); +char *shipname(int); diff --git a/util.c b/util.c index 826a508..f1daf2a 100644 --- a/util.c +++ b/util.c @@ -16,6 +16,13 @@ static int shiplentab[] = { [Ssubmarine] 3, [Sdestroyer] 2, }; +static char *shipnametab[] = { + [Scarrier] "carrier", + [Sbattleship] "battleship", + [Scruiser] "cruiser", + [Ssubmarine] "submarine", + [Sdestroyer] "destroyer", +}; char * @@ -109,6 +116,15 @@ countshipcells(Map *m) int shiplen(int stype) { - assert(stype >= 0 && stype < NSHIPS); + if(stype < 0 || stype >= NSHIPS) + return -1; return shiplentab[stype]; } + +char * +shipname(int stype) +{ + if(stype < 0 || stype >= NSHIPS) + return nil; + return shipnametab[stype]; +} -- cgit v1.2.3