diff options
author | rodri <rgl@antares-labs.eu> | 2023-08-30 22:07:50 +0000 |
---|---|---|
committer | rodri <rgl@antares-labs.eu> | 2023-08-30 22:07:50 +0000 |
commit | be547a2dba10292fb3191bc5fec42212d2391660 (patch) | |
tree | 4f8771a774d0a947f39aea1c9e92e4c768f87bb9 | |
parent | 3d93e42d0e854a3bb7c3a380a0635cf927d59ab7 (diff) | |
download | battleship-be547a2dba10292fb3191bc5fec42212d2391660.tar.gz battleship-be547a2dba10292fb3191bc5fec42212d2391660.tar.bz2 battleship-be547a2dba10292fb3191bc5fec42212d2391660.zip |
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.
-rw-r--r-- | bts.c | 72 | ||||
-rw-r--r-- | btsd.c | 4 | ||||
-rw-r--r-- | fns.h | 1 | ||||
-rw-r--r-- | util.c | 18 |
4 files changed, 66 insertions, 29 deletions
@@ -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: @@ -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; @@ -18,3 +18,4 @@ void settiles(Map*, Point2, int, int, int); void fprintmap(int, Map*); int countshipcells(Map*); int shiplen(int); +char *shipname(int); @@ -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]; +} |