From 29b161f1c0e0128b4377f736c558dcdeca5f7ea7 Mon Sep 17 00:00:00 2001 From: nick black Date: Mon, 10 May 2021 04:37:08 -0400 Subject: [PATCH] [box] place ships underneath grid #1639 --- src/demo/boxdemo.c | 49 ++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/src/demo/boxdemo.c b/src/demo/boxdemo.c index 0022db4fe..dce49d47f 100644 --- a/src/demo/boxdemo.c +++ b/src/demo/boxdemo.c @@ -94,21 +94,28 @@ move_ships(struct notcurses* nc, struct ship* ships, unsigned shipcount){ if(ships[s].n == NULL){ continue; } - int y, x; - ncplane_yx(ships[s].n, &y, &x); - y += ships[s].vely; - x += ships[s].velx; - if(x < 0){ - x = 0; - }else if(x >= ncplane_dim_x(stdn) - SHIPWIDTH){ - x = ncplane_dim_x(stdn) - SHIPWIDTH - 1; + int yoff, xoff, ny, nx; + ncplane_yx(ships[s].n, &yoff, &xoff); + ncplane_dim_yx(ships[s].n, &ny, &nx); + int dimy = ncplane_dim_y(stdn); + int dimx = ncplane_dim_x(stdn); + yoff += ships[s].vely; + xoff += ships[s].velx; + if(xoff <= 0){ + xoff = 0; + ships[s].velx = -ships[s].velx; + }else if(xoff >= dimx - nx){ + xoff = dimx - nx - 1; + ships[s].velx = -ships[s].velx; } - if(y < 0){ - y = 0; - }else if(y >= ncplane_dim_y(stdn) - SHIPHEIGHT){ - y = ncplane_dim_y(stdn) - SHIPHEIGHT - 1; + if(yoff <= 1){ + yoff = 2; + ships[s].vely = -ships[s].vely; + }else if(yoff >= dimy - ny){ + yoff = dimy - ny - 1; + ships[s].vely = -ships[s].vely; } - ncplane_move_yx(ships[s].n, y, x); + ncplane_move_yx(ships[s].n, yoff, xoff); } return 0; } @@ -142,8 +149,12 @@ get_ships(struct notcurses* nc, struct ship* ships, unsigned shipcount){ } } ncplane_move_below(ships[s].n, notcurses_stdplane(nc)); - ships[s].vely = random() % 5 - 2; - ships[s].velx = random() % 5 - 2; + if((ships[s].vely = random() % 6 - 3) == 0){ + ships[s].vely = 3; + } + if((ships[s].velx = random() % 6 - 3) == 0){ + ships[s].velx = 3; + } } ncvisual_destroy(wmv); return 0; @@ -167,10 +178,6 @@ int box_demo(struct notcurses* nc){ const int targx = 7; const int targy = 7; int ytargbase = (ylen - targy) / 2; - nccell c = CELL_CHAR_INITIALIZER(' '); - nccell_set_bg_default(&c); - ncplane_set_base_cell(n, &c); - nccell_release(n, &c); ncplane_set_fg_rgb8(n, 180, 40, 180); ncplane_set_bg_default(n); if(notcurses_canutf8(nc)){ @@ -209,7 +216,7 @@ int box_demo(struct notcurses* nc){ int y = 1, x = 0; ncplane_dim_yx(n, &ylen, &xlen); --ylen; - while(ylen - y >= targy * 2 && xlen - x >= targx * 2){ + while(ylen - y >= targy && xlen - x >= targx){ if(ncplane_cursor_move_yx(n, y, x)){ return -1; } @@ -242,7 +249,7 @@ int box_demo(struct notcurses* nc){ ncplane_dim_yx(n, &ylen, &xlen); --ylen; move_ships(nc, ships, sizeof(ships) / sizeof(*ships)); - while(ylen - y >= targy * 2 && xlen - x >= targx * 2){ + while(ylen - y >= targy && xlen - x >= targx){ if(ncplane_cursor_move_yx(n, y, x)){ return -1; }