From 042372d4c2856433f9eba3f80bd0399c0a23e807 Mon Sep 17 00:00:00 2001 From: Ron Koenderink Date: Thu, 16 Dec 2004 11:12:28 +0000 Subject: [PATCH] (have_looked,have_found,set_have_looked,set_have_found,print_found): collapse to shiplist function set (add,free,print,on). Fix bug where ship uid was a unsigned char instead of short. Switch to head pointer instead of head record with a linked list. Add a free(). --- include/plane.h | 15 +++--- include/prototypes.h | 4 +- src/lib/commands/bomb.c | 24 ++------- src/lib/commands/sona.c | 11 ++-- src/lib/subs/aircombat.c | 27 ++-------- src/lib/subs/aswplnsubs.c | 107 +++++++++----------------------------- src/lib/subs/list.c | 4 +- 7 files changed, 48 insertions(+), 144 deletions(-) diff --git a/include/plane.h b/include/plane.h index d9384c322..10966b500 100644 --- a/include/plane.h +++ b/include/plane.h @@ -150,11 +150,9 @@ struct plist { extern struct plchrstr plchr[]; extern int pln_maxno; -struct shiplook { +struct shiplist { short uid; - u_char looked; - u_char found; - struct shiplook *next; + struct shiplist *next; }; #define PLN_ATTDEF(b, t) (b + ((b?1:0) * ((t/20)>10?10:(t/20)))) @@ -188,10 +186,9 @@ extern int all_missiles(struct emp_qelem *); extern int can_fly(int); /* src/lib/subs/aswplnsubs.c */ -extern int have_looked(u_char, struct shiplook *); -extern int have_found(u_char, struct shiplook *); -extern void set_have_looked(u_char, struct shiplook *); -extern void set_have_found(u_char, struct shiplook *); -extern int print_found(struct shiplook *); +extern int on_shiplist(short, struct shiplist *); +extern void add_shiplist(short, struct shiplist **); +extern void free_shiplist(struct shiplist **); +extern void print_shiplist(struct shiplist *); #endif /* _PLANE_H_ */ diff --git a/include/prototypes.h b/include/prototypes.h index 2d93ad01b..781e562ae 100644 --- a/include/prototypes.h +++ b/include/prototypes.h @@ -75,7 +75,7 @@ extern void scuttle_ship(struct shpstr *); extern void resnoise(struct sctstr *sptr, int public_amt, s_char *name, int old, int new); 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 shiplook *); +extern void plane_sona(struct emp_qelem *, int, int, struct shiplist **); extern int tend_nxtitem(struct nstr_item *, void *); extern s_char *prsub(struct shpstr *); extern int check_trade(void); @@ -323,7 +323,7 @@ extern int carriersatxy(coord, coord, int, int, natid); extern int unitsatxy(coord, coord, int, int); extern int planesatxy(coord, coord, int, int, struct emp_qelem *); extern int asw_shipsatxy(coord, coord, int, int, struct plnstr *, - struct shiplook *); + struct shiplist **); extern int num_shipsatxy(coord, coord, int, int); extern int islist(s_char *); /* src/lib/subs/mission.c */ diff --git a/src/lib/commands/bomb.c b/src/lib/commands/bomb.c index 5643bbd0f..58f966b51 100644 --- a/src/lib/commands/bomb.c +++ b/src/lib/commands/bomb.c @@ -496,7 +496,7 @@ ship_bomb(struct emp_qelem *list, struct sctstr *target) int ignore; struct shpstr ship; int nships = 0; - struct shiplook head, *s, *s2; + struct shiplist *head = NULL; s_char buf[1024]; s_char prompt[128]; s_char msg[128]; @@ -506,20 +506,9 @@ ship_bomb(struct emp_qelem *list, struct sctstr *target) int gun; int shell; - memset(&head, 0, sizeof(struct shiplook)); - head.uid = -1; for (qp = list->q_forw; qp != list && !player->aborted; qp = qp->q_forw) { - if (head.uid != -1) { - s = head.next; - while (s != (struct shiplook *)0) { - s2 = s; - s = s->next; - free(s2); - } - } - memset(&head, 0, sizeof(struct shiplook)); - head.uid = -1; + free_shiplist(&head); plp = (struct plist *)qp; if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T))) continue; @@ -545,7 +534,7 @@ ship_bomb(struct emp_qelem *list, struct sctstr *target) } if (*q == '?') { if (plp->pcp->pl_flags & P_A) - print_found(&head); + print_shiplist(head); else shipsatxy(target->sct_x, target->sct_y, 0, M_SUB); continue; @@ -634,12 +623,7 @@ ship_bomb(struct emp_qelem *list, struct sctstr *target) next: ; } - s = head.next; - while (s != (struct shiplook *)0) { - s2 = s; - s = s->next; - free(s2); - } + free_shiplist(&head); } static void diff --git a/src/lib/commands/sona.c b/src/lib/commands/sona.c index a050dade3..39db13f07 100644 --- a/src/lib/commands/sona.c +++ b/src/lib/commands/sona.c @@ -228,7 +228,7 @@ sona(void) void plane_sona(struct emp_qelem *plane_list, int x, int y, - struct shiplook *head) + struct shiplist **head) { struct plnstr *pp; struct plchrstr *pcp; @@ -265,13 +265,8 @@ plane_sona(struct emp_qelem *plane_list, int x, int y, targ = &s; if (targ->shp_own == pp->pln_own || targ->shp_own == 0) continue; -/* - if (have_looked(targ->shp_uid,head)) - continue; -*/ - if (have_found(targ->shp_uid, head)) + if (on_shiplist(targ->shp_uid, *head)) continue; - set_have_looked(targ->shp_uid, head); tmcp = &mchr[(int)targ->shp_type]; if (!(tmcp->m_flags & M_SUB)) continue; @@ -294,7 +289,7 @@ plane_sona(struct emp_qelem *plane_list, int x, int y, } if ((dist > vrange)) continue; - set_have_found(targ->shp_uid, head); + add_shiplist(targ->shp_uid, head); if (!found) { mpr(pp->pln_own, "\nSonar contact in %s\n", xyas(x, y, pp->pln_own)); diff --git a/src/lib/subs/aircombat.c b/src/lib/subs/aircombat.c index 090d8720d..11f46227d 100644 --- a/src/lib/subs/aircombat.c +++ b/src/lib/subs/aircombat.c @@ -95,17 +95,13 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list, struct natstr *over, *mynatp; struct plist *plp; int evaded; - struct shiplook head; - struct shiplook *s, *s2; + struct shiplist *head = NULL; int changed = 0; int intown = 0; /* Last owner to intercept */ /* We want to only intercept once per sector per owner. So, if we overfly a sector, and then overfly some land units or ships, we don't want to potentially intercept 3 times. */ - memset(&head, 0, sizeof(struct shiplook)); - head.uid = -1; - plp = (struct plist *)bomb_list->q_forw; plane_owner = plp->plane.pln_own; @@ -271,14 +267,8 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list, writemap(player->cnum); /* Now, if the bomber and escort lists are empty, we are done */ if (QEMPTY(bomb_list) && QEMPTY(esc_list)) { - if (mission_flags & P_A) { - s = head.next; - while (s != (struct shiplook *)0) { - s2 = s; - s = s->next; - free(s2); - } - } + if (mission_flags & P_A) + free_shiplist(&head); return; } @@ -356,15 +346,8 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list, } } } - - if ((mission_flags & P_A) && (head.uid != -1)) { - s = head.next; - while (s != (struct shiplook *)0) { - s2 = s; - s = s->next; - free(s2); - } - } + if (mission_flags & P_A) + free_shiplist(&head); } static int diff --git a/src/lib/subs/aswplnsubs.c b/src/lib/subs/aswplnsubs.c index 0e75755c3..8f02695aa 100644 --- a/src/lib/subs/aswplnsubs.c +++ b/src/lib/subs/aswplnsubs.c @@ -28,133 +28,80 @@ * aswplnsubs.c: Various subroutines used for ASW planes * * Known contributors to this file: + * Ron Koenderink, 2004 * */ #include "misc.h" -#include "player.h" -#include "xy.h" -#include "ship.h" -#include "nsc.h" -#include "nat.h" -#include "path.h" #include "file.h" #include "plane.h" -#include -#include -#include "prototypes.h" int -have_looked(u_char uid, struct shiplook *head) +on_shiplist(short uid, struct shiplist *head) { - struct shiplook *s; + struct shiplist *s; s = head; - if (s->uid == -1) - return 0; - - while (s != ((struct shiplook *)0)) { - if (s->uid == uid) - return s->looked; - s = s->next; - } - - return 0; -} - -int -have_found(u_char uid, struct shiplook *head) -{ - struct shiplook *s; - - s = head; - if (s->uid == -1) - return 0; - - while (s != ((struct shiplook *)0)) { + while (s != NULL) { if (s->uid == uid) - return s->found; + return 1; s = s->next; } - return 0; } void -set_have_looked(u_char uid, struct shiplook *head) +add_shiplist(short uid, struct shiplist **head) { - struct shiplook *s, *s2; + struct shiplist *s, *s2; - s = head; - if (s->uid == -1) { - s->uid = uid; - s->looked = 1; - s->found = 0; - s->next = (struct shiplook *)0; - } + s = *head; + s2 = NULL; - while (s != ((struct shiplook *)0)) { + while (s != NULL) { if (s->uid == uid) { - s->looked = 1; return; } s2 = s; s = s->next; } - s = (struct shiplook *)malloc(sizeof(struct shiplook)); - memset(s, 0, sizeof(struct shiplook)); - s2->next = s; + s = malloc(sizeof(struct shiplist)); + if (s2 != NULL) + s2->next = s; + else + *head = s; s->uid = uid; - s->looked = 1; - s->next = (struct shiplook *)0; + s->next = NULL; } void -set_have_found(u_char uid, struct shiplook *head) +free_shiplist(struct shiplist **head) { - struct shiplook *s, *s2; + struct shiplist *s, *s2; - s = head; - if (s->uid == -1) { - s->uid = uid; - s->looked = 0; - s->found = 1; - s->next = (struct shiplook *)0; - } + s = *head; - - while (s != ((struct shiplook *)0)) { - if (s->uid == uid) { - s->found = 1; - return; - } + while (s != NULL) { s2 = s; s = s->next; + free(s2); } - - s = (struct shiplook *)malloc(sizeof(struct shiplook)); - memset(s, 0, sizeof(struct shiplook)); - s2->next = s; - s->uid = uid; - s->found = 1; - s->next = (struct shiplook *)0; + *head = NULL; } -int -print_found(struct shiplook *head) +void +print_shiplist(struct shiplist *head) { - struct shiplook *s; + struct shiplist *s; int first; struct mchrstr *mp; struct shpstr ship; s = head; first = 1; - if (s->uid == -1) - return 0; - while (s != ((struct shiplook *)0)) { + while (s != NULL) { getship(s->uid, &ship); mp = &mchr[(int)ship.shp_type]; if (first) { @@ -165,6 +112,4 @@ print_found(struct shiplook *head) cname(ship.shp_own), effadv(ship.shp_effic), prship(&ship)); s = s->next; } - - return 1; } diff --git a/src/lib/subs/list.c b/src/lib/subs/list.c index 68df75f9e..7eb7f1b96 100644 --- a/src/lib/subs/list.c +++ b/src/lib/subs/list.c @@ -209,7 +209,7 @@ planesatxy(coord x, coord y, int wantflags, int nowantflags, int asw_shipsatxy(coord x, coord y, int wantflags, int nowantflags, - struct plnstr *pp, struct shiplook *head) + struct plnstr *pp, struct shiplist **head) { int first; int ships; @@ -239,7 +239,7 @@ asw_shipsatxy(coord x, coord y, int wantflags, int nowantflags, shp_hardtarget(&ship), EF_SHIP)) continue; } - set_have_found(ship.shp_uid, head); + add_shiplist(ship.shp_uid, head); if (first) { pr(" # owner eff type\n"); first = 0; -- 2.43.0