From 198e2dd076beac3b80bf713c7c8142aadbb02423 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Mon, 5 Jan 2015 13:12:38 +0100 Subject: [PATCH] subs: Move shared code from navi.c to subs Rename do_unit_move() to unit_move() to blend into unitsub.c. Give its helpers static linkage. Signed-off-by: Markus Armbruster --- include/map.h | 1 + include/prototypes.h | 2 - include/unit.h | 4 +- src/lib/commands/marc.c | 5 +- src/lib/commands/navi.c | 288 +--------------------------------------- src/lib/subs/maps.c | 59 ++++++++ src/lib/subs/unitsub.c | 229 +++++++++++++++++++++++++++++++- 7 files changed, 289 insertions(+), 299 deletions(-) diff --git a/include/map.h b/include/map.h index b815dc96..a84ef368 100644 --- a/include/map.h +++ b/include/map.h @@ -62,6 +62,7 @@ extern void border(struct range *, char *, char *); /* src/lib/subs/maps.c */ extern int do_map(int bmap, int unit_type, char *arg1, char *arg2); extern int display_region_map(int, int, coord, coord, char *, char *); +extern int nav_map(int, int, int); extern int bmaps_intersect(natid, natid); extern int share_bmap(natid, natid, struct nstr_sect *, char, char *); diff --git a/include/prototypes.h b/include/prototypes.h index 9aa410a0..a9e4556d 100644 --- a/include/prototypes.h +++ b/include/prototypes.h @@ -59,8 +59,6 @@ extern int edit_sect_i(struct sctstr *, char *, int); extern int load_comm_ok(struct sctstr *, natid, i_type, int); extern void gift(natid, natid, void *, char *); extern int display_mark(i_type, int); -extern int nav_map(int, int, int); -extern int do_unit_move(struct emp_qelem *, double *, double *); extern int count_pop(int); extern int line_of_sight(char **rad, int ax, int ay, int bx, int by); extern void plane_sona(struct emp_qelem *, int, int, struct shiplist **); diff --git a/include/unit.h b/include/unit.h index b06c5595..b77d7b51 100644 --- a/include/unit.h +++ b/include/unit.h @@ -56,9 +56,7 @@ extern int unit_nplane(int, int, int *, int *, int *); extern void unit_onresize(int); extern char *unit_nameof(struct empobj *); -extern void unit_list(struct emp_qelem *); -extern char *unit_path(struct empobj *, char *, size_t); -extern void unit_view(struct emp_qelem *); +extern int unit_move(struct emp_qelem *, double *, double *); extern void unit_teleport(struct empobj *, coord, coord); extern int unit_update_cargo(struct empobj *); extern void unit_drop_cargo(struct empobj *, natid); diff --git a/src/lib/commands/marc.c b/src/lib/commands/marc.c index 01118e9a..e2afb38c 100644 --- a/src/lib/commands/marc.c +++ b/src/lib/commands/marc.c @@ -35,8 +35,7 @@ #include #include "commands.h" -#include "file.h" -#include "empobj.h" +#include "unit.h" int march(void) @@ -53,5 +52,5 @@ march(void) pr("No lands\n"); return RET_FAIL; } - return do_unit_move(&land_list, &minmob, &maxmob); + return unit_move(&land_list, &minmob, &maxmob); } diff --git a/src/lib/commands/navi.c b/src/lib/commands/navi.c index d0422fa6..833f7446 100644 --- a/src/lib/commands/navi.c +++ b/src/lib/commands/navi.c @@ -35,15 +35,8 @@ #include #include "commands.h" -#include "map.h" -#include "optlist.h" -#include "path.h" #include "unit.h" -static void pr_leader_change(struct empobj *leader); -static struct empobj *get_leader(struct emp_qelem *list); -static void switch_leader(struct emp_qelem *list, int uid); - int navi(void) { @@ -59,284 +52,5 @@ navi(void) pr("No ships\n"); return RET_FAIL; } - return do_unit_move(&ship_list, &minmob, &maxmob); -} - -int -do_unit_move(struct emp_qelem *ulist, double *minmob, double *maxmob) -{ - char *cp = NULL; - int leader_uid; - struct empobj *leader; - int dir; - int stopping = 0; - int skip = 0; - int moved = 0; - char buf[1024]; - char prompt[128]; - char bmap_flag; - int ac; - int type; - - leader = get_leader(ulist); - leader_uid = leader->uid; - type = leader->ef_type; - pr("%s is %s\n", - type == EF_SHIP ? "Flagship" : "Leader", - unit_nameof(leader)); - - if (player->argp[2]) { - strcpy(buf, player->argp[2]); - cp = unit_path(leader, buf, sizeof(buf)); - } - - while (!QEMPTY(ulist)) { - char dp[80]; - - if (cp == NULL || *cp == '\0' || stopping) { - stopping = 0; - if (type == EF_SHIP) - shp_nav(ulist, minmob, maxmob, player->cnum); - else - lnd_mar(ulist, minmob, maxmob, player->cnum); - if (QEMPTY(ulist)) { - pr("No %s left\n", type == EF_SHIP ? "ships" : "lands"); - return RET_OK; - } - leader = get_leader(ulist); - if (leader->uid != leader_uid) { - leader_uid = leader->uid; - pr_leader_change(leader); - stopping = 1; - continue; - } - if (!skip) - nav_map(leader->x, leader->y, - type == EF_SHIP - ? !(mchr[(int)leader->type].m_flags & M_SUB) : 1); - else - skip = 0; - sprintf(prompt, "<%.1f:%.1f: %s> ", *maxmob, - *minmob, xyas(leader->x, leader->y, player->cnum)); - cp = getstring(prompt, buf); - /* Just in case any of our units were shelled while we were - * at the prompt, we call shp_nav() or lnd_mar() again. - */ - if (type == EF_SHIP) - shp_nav(ulist, minmob, maxmob, player->cnum); - else - lnd_mar(ulist, minmob, maxmob, player->cnum); - if (QEMPTY(ulist)) { - pr("No %s left\n", type == EF_SHIP ? "ships" : "lands"); - return RET_OK; - } - leader = get_leader(ulist); - if (leader->uid != leader_uid) { - leader_uid = leader->uid; - pr_leader_change(leader); - stopping = 1; - continue; - } - if (cp) - cp = unit_path(leader, buf, sizeof(buf)); - } - if (type == EF_SHIP) { - rad_map_set(player->cnum, leader->x, leader->y, leader->effic, - leader->tech, mchr[leader->type].m_vrnge); - } - if (cp == NULL || *cp == '\0') - cp = &dirch[DIR_STOP]; - dir = chkdir(*cp, DIR_STOP, DIR_LAST); - if (dir >= 0) { - if (type == EF_SHIP) - stopping |= shp_nav_one_sector(ulist, dir, player->cnum); - else { - if (!moved && !lnd_abandon_askyn(ulist)) - return RET_FAIL; - stopping |= lnd_mar_one_sector(ulist, dir, player->cnum); - } - moved = 1; - cp++; - continue; - } - ac = parse(cp, player->argbuf, player->argp, NULL, NULL, NULL); - if (ac <= 0) { - player->argp[0] = ""; - cp = NULL; - } else if (ac == 1) { - sprintf(dp, "%d", leader->uid); - player->argp[1] = dp; - cp++; - } else - cp = NULL; - bmap_flag = 0; - switch (*player->argp[0]) { - case 'B': - bmap_flag = 'b'; - /* - * fall through - */ - case 'M': - display_region_map(bmap_flag, type, leader->x, leader->y, - player->argp[1], player->argp[2]); - skip = 1; - continue; - case 'f': - if (ac <= 1) - switch_leader(ulist, -1); - else - switch_leader(ulist, atoi(player->argp[1])); - leader = get_leader(ulist); - if (leader->uid != leader_uid) { - leader_uid = leader->uid; - pr_leader_change(leader); - } - continue; - case 'i': - unit_list(ulist); - continue; - case 'm': - if (type == EF_SHIP) - stopping |= shp_sweep(ulist, 1, 1, player->cnum); - else { - stopping |= lnd_sweep(ulist, 1, 1, player->cnum); - } - continue; - case 'r': - radar(leader->ef_type); - skip = 1; - player->btused++; - continue; - case 'l': - do_look(type); - player->btused++; - continue; - case 's': - if (leader->ef_type != EF_SHIP) - break; - sona(); - player->btused++; - skip = 1; - continue; - case 'd': - if (ac < 3) { - player->argp[2] = ac < 2 ? "1" : player->argp[1]; - sprintf(dp, "%d", leader->uid); - player->argp[1] = dp; - } - if (type == EF_SHIP) - mine(); - else - landmine(); - stopping = 1; - skip = 1; - player->btused++; - continue; - case 'v': - unit_view(ulist); - continue; - } - direrr("`%c' to stop", ", `%c' to view", NULL); - pr(", `i' to list %s, `f' to change %s,\n", - type == EF_SHIP ? "ships" : "units", - type == EF_SHIP ? "flagship" : "leader"); - pr("`r' to radar, %s`l' to look, `M' to map, `B' to bmap,\n", - type == EF_SHIP ? "`s' to sonar, " : ""); - pr("`d' to drop mines, and `m' to minesweep\n"); - stopping = 1; - } - return RET_OK; -} - -int -nav_map(int x, int y, int show_designations) -{ - char *ptr; - struct nstr_sect ns; - struct sctstr sect; - int i; - /* Note this is not re-entrant anyway, so we keep the buffers - around */ - static char *wmapbuf = NULL; - static char **wmap = NULL; - int changed = 0; - - if (!wmapbuf) - wmapbuf = malloc(WORLD_Y * MAPWIDTH(1)); - if (!wmap) { - wmap = malloc(WORLD_Y * sizeof(*wmap)); - if (wmap && wmapbuf) { - for (i = 0; i < WORLD_Y; i++) - wmap[i] = &wmapbuf[MAPWIDTH(1) * i]; - } else if (wmap) { - free(wmap); - wmap = NULL; - } - } - if (!wmapbuf || !wmap) { - pr("Memory error, tell the deity.\n"); - logerror("malloc failed in navi\n"); - return RET_FAIL; - } - snxtsct_dist(&ns, x, y, 1); - blankfill(wmapbuf, &ns.range, 1); - while (nxtsct(&ns, §)) { - ptr = &wmap[ns.dy][ns.dx]; - *ptr = dchr[sect.sct_type].d_mnem; - if (!show_designations && - sect.sct_own != player->cnum && - sect.sct_type != SCT_WATER && - sect.sct_type != SCT_BSPAN && sect.sct_type != SCT_HARBR) - *ptr = '?'; - changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0); - /* - * We do it this way so that 'x' and 'X' - * bdesignations will show up. This can - * be used to mark mined sectors. So, the - * player will see the current des, UNLESS - * they've marked the sector 'x' or 'X', - * in which case they'll see that. - * --ts - */ - *ptr = player->bmap[sect.sct_uid]; - } - if (changed) - writemap(player->cnum); - for (i = 0; i < ns.range.height; i++) - pr("%s\n", wmap[i]); - return RET_OK; -} - -static void -pr_leader_change(struct empobj *leader) -{ - pr("Changing %s to %s\n", - leader->ef_type == EF_SHIP ? "flagship" : "leader", - unit_nameof(leader)); -} - -static struct empobj * -get_leader(struct emp_qelem *list) -{ - return &((struct ulist *)(list->q_back))->unit.gen; -} - -static void -switch_leader(struct emp_qelem *list, int uid) -{ - struct emp_qelem *qp, *save; - struct ulist *ulp; - - if (QEMPTY(list)) - return; - - save = qp = list->q_back; - do { - emp_remque(qp); - emp_insque(qp, list); - qp = list->q_back; - ulp = (struct ulist *)qp; - if (ulp->unit.gen.uid == uid || uid == -1) - break; - } while (list->q_back != save); + return unit_move(&ship_list, &minmob, &maxmob); } diff --git a/src/lib/subs/maps.c b/src/lib/subs/maps.c index 1d1aa035..9fe91bef 100644 --- a/src/lib/subs/maps.c +++ b/src/lib/subs/maps.c @@ -463,6 +463,65 @@ display_region_map(int bmap, int unit_type, coord curx, coord cury, return draw_map(origin, map_flags, &ns); } +int +nav_map(int x, int y, int show_designations) +{ + char *ptr; + struct nstr_sect ns; + struct sctstr sect; + int i; + /* Note this is not re-entrant anyway, so we keep the buffers + around */ + static char *wmapbuf = NULL; + static char **wmap = NULL; + int changed = 0; + + if (!wmapbuf) + wmapbuf = malloc(WORLD_Y * MAPWIDTH(1)); + if (!wmap) { + wmap = malloc(WORLD_Y * sizeof(*wmap)); + if (wmap && wmapbuf) { + for (i = 0; i < WORLD_Y; i++) + wmap[i] = &wmapbuf[MAPWIDTH(1) * i]; + } else if (wmap) { + free(wmap); + wmap = NULL; + } + } + if (!wmapbuf || !wmap) { + pr("Memory error, tell the deity.\n"); + logerror("malloc failed in navi\n"); + return RET_FAIL; + } + snxtsct_dist(&ns, x, y, 1); + blankfill(wmapbuf, &ns.range, 1); + while (nxtsct(&ns, §)) { + ptr = &wmap[ns.dy][ns.dx]; + *ptr = dchr[sect.sct_type].d_mnem; + if (!show_designations && + sect.sct_own != player->cnum && + sect.sct_type != SCT_WATER && + sect.sct_type != SCT_BSPAN && sect.sct_type != SCT_HARBR) + *ptr = '?'; + changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0); + /* + * We do it this way so that 'x' and 'X' + * bdesignations will show up. This can + * be used to mark mined sectors. So, the + * player will see the current des, UNLESS + * they've marked the sector 'x' or 'X', + * in which case they'll see that. + * --ts + */ + *ptr = player->bmap[sect.sct_uid]; + } + if (changed) + writemap(player->cnum); + for (i = 0; i < ns.range.height; i++) + pr("%s\n", wmap[i]); + return RET_OK; +} + int bmaps_intersect(natid a, natid b) { diff --git a/src/lib/subs/unitsub.c b/src/lib/subs/unitsub.c index 440fae7c..4ddff78e 100644 --- a/src/lib/subs/unitsub.c +++ b/src/lib/subs/unitsub.c @@ -34,9 +34,10 @@ #include #include "file.h" +#include "map.h" +#include "optlist.h" #include "path.h" #include "player.h" -#include "optlist.h" #include "prototypes.h" #include "unit.h" @@ -57,7 +58,7 @@ unit_nameof(struct empobj *gp) return "The Beast #666"; } -void +static void unit_list(struct emp_qelem *unit_list) { struct emp_qelem *qp; @@ -116,7 +117,7 @@ unit_list(struct emp_qelem *unit_list) } } -char * +static char * unit_path(struct empobj *unit, char *buf, size_t bufsz) { coord destx; @@ -173,7 +174,7 @@ unit_path(struct empobj *unit, char *buf, size_t bufsz) return buf; } -void +static void unit_view(struct emp_qelem *list) { struct sctstr sect; @@ -199,6 +200,226 @@ unit_view(struct emp_qelem *list) } } +static void +pr_leader_change(struct empobj *leader) +{ + pr("Changing %s to %s\n", + leader->ef_type == EF_SHIP ? "flagship" : "leader", + unit_nameof(leader)); +} + +static struct empobj * +get_leader(struct emp_qelem *list) +{ + return &((struct ulist *)(list->q_back))->unit.gen; +} + +static void +switch_leader(struct emp_qelem *list, int uid) +{ + struct emp_qelem *qp, *save; + struct ulist *ulp; + + if (QEMPTY(list)) + return; + + save = qp = list->q_back; + do { + emp_remque(qp); + emp_insque(qp, list); + qp = list->q_back; + ulp = (struct ulist *)qp; + if (ulp->unit.gen.uid == uid || uid == -1) + break; + } while (list->q_back != save); +} + +int +unit_move(struct emp_qelem *ulist, double *minmob, double *maxmob) +{ + char *cp = NULL; + int leader_uid; + struct empobj *leader; + int dir; + int stopping = 0; + int skip = 0; + int moved = 0; + char buf[1024]; + char prompt[128]; + char bmap_flag; + int ac; + int type; + + leader = get_leader(ulist); + leader_uid = leader->uid; + type = leader->ef_type; + pr("%s is %s\n", + type == EF_SHIP ? "Flagship" : "Leader", + unit_nameof(leader)); + + if (player->argp[2]) { + strcpy(buf, player->argp[2]); + cp = unit_path(leader, buf, sizeof(buf)); + } + + while (!QEMPTY(ulist)) { + char dp[80]; + + if (cp == NULL || *cp == '\0' || stopping) { + stopping = 0; + if (type == EF_SHIP) + shp_nav(ulist, minmob, maxmob, player->cnum); + else + lnd_mar(ulist, minmob, maxmob, player->cnum); + if (QEMPTY(ulist)) { + pr("No %s left\n", type == EF_SHIP ? "ships" : "lands"); + return RET_OK; + } + leader = get_leader(ulist); + if (leader->uid != leader_uid) { + leader_uid = leader->uid; + pr_leader_change(leader); + stopping = 1; + continue; + } + if (!skip) + nav_map(leader->x, leader->y, + type == EF_SHIP + ? !(mchr[(int)leader->type].m_flags & M_SUB) : 1); + else + skip = 0; + sprintf(prompt, "<%.1f:%.1f: %s> ", *maxmob, + *minmob, xyas(leader->x, leader->y, player->cnum)); + cp = getstring(prompt, buf); + /* Just in case any of our units were shelled while we were + * at the prompt, we call shp_nav() or lnd_mar() again. + */ + if (type == EF_SHIP) + shp_nav(ulist, minmob, maxmob, player->cnum); + else + lnd_mar(ulist, minmob, maxmob, player->cnum); + if (QEMPTY(ulist)) { + pr("No %s left\n", type == EF_SHIP ? "ships" : "lands"); + return RET_OK; + } + leader = get_leader(ulist); + if (leader->uid != leader_uid) { + leader_uid = leader->uid; + pr_leader_change(leader); + stopping = 1; + continue; + } + if (cp) + cp = unit_path(leader, buf, sizeof(buf)); + } + if (type == EF_SHIP) { + rad_map_set(player->cnum, leader->x, leader->y, leader->effic, + leader->tech, mchr[leader->type].m_vrnge); + } + if (cp == NULL || *cp == '\0') + cp = &dirch[DIR_STOP]; + dir = chkdir(*cp, DIR_STOP, DIR_LAST); + if (dir >= 0) { + if (type == EF_SHIP) + stopping |= shp_nav_one_sector(ulist, dir, player->cnum); + else { + if (!moved && !lnd_abandon_askyn(ulist)) + return RET_FAIL; + stopping |= lnd_mar_one_sector(ulist, dir, player->cnum); + } + moved = 1; + cp++; + continue; + } + ac = parse(cp, player->argbuf, player->argp, NULL, NULL, NULL); + if (ac <= 0) { + player->argp[0] = ""; + cp = NULL; + } else if (ac == 1) { + sprintf(dp, "%d", leader->uid); + player->argp[1] = dp; + cp++; + } else + cp = NULL; + bmap_flag = 0; + switch (*player->argp[0]) { + case 'B': + bmap_flag = 'b'; + /* + * fall through + */ + case 'M': + display_region_map(bmap_flag, type, leader->x, leader->y, + player->argp[1], player->argp[2]); + skip = 1; + continue; + case 'f': + if (ac <= 1) + switch_leader(ulist, -1); + else + switch_leader(ulist, atoi(player->argp[1])); + leader = get_leader(ulist); + if (leader->uid != leader_uid) { + leader_uid = leader->uid; + pr_leader_change(leader); + } + continue; + case 'i': + unit_list(ulist); + continue; + case 'm': + if (type == EF_SHIP) + stopping |= shp_sweep(ulist, 1, 1, player->cnum); + else { + stopping |= lnd_sweep(ulist, 1, 1, player->cnum); + } + continue; + case 'r': + radar(leader->ef_type); + skip = 1; + player->btused++; + continue; + case 'l': + do_look(type); + player->btused++; + continue; + case 's': + if (leader->ef_type != EF_SHIP) + break; + sona(); + player->btused++; + skip = 1; + continue; + case 'd': + if (ac < 3) { + player->argp[2] = ac < 2 ? "1" : player->argp[1]; + sprintf(dp, "%d", leader->uid); + player->argp[1] = dp; + } + if (type == EF_SHIP) + mine(); + else + landmine(); + stopping = 1; + skip = 1; + player->btused++; + continue; + case 'v': + unit_view(ulist); + continue; + } + direrr("`%c' to stop", ", `%c' to view", NULL); + pr(", `i' to list %s, `f' to change %s,\n", + type == EF_SHIP ? "ships" : "units", + type == EF_SHIP ? "flagship" : "leader"); + pr("`r' to radar, %s`l' to look, `M' to map, `B' to bmap,\n", + type == EF_SHIP ? "`s' to sonar, " : ""); + pr("`d' to drop mines, and `m' to minesweep\n"); + stopping = 1; + } + return RET_OK; +} + /* * Teleport UNIT to X,Y. * If UNIT's mission op-area is centered on it, keep it centered.