(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().
This commit is contained in:
Ron Koenderink 2004-12-16 11:12:28 +00:00
parent fd700f235b
commit 042372d4c2
7 changed files with 48 additions and 144 deletions

View file

@ -150,11 +150,9 @@ struct plist {
extern struct plchrstr plchr[]; extern struct plchrstr plchr[];
extern int pln_maxno; extern int pln_maxno;
struct shiplook { struct shiplist {
short uid; short uid;
u_char looked; struct shiplist *next;
u_char found;
struct shiplook *next;
}; };
#define PLN_ATTDEF(b, t) (b + ((b?1:0) * ((t/20)>10?10:(t/20)))) #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); extern int can_fly(int);
/* src/lib/subs/aswplnsubs.c */ /* src/lib/subs/aswplnsubs.c */
extern int have_looked(u_char, struct shiplook *); extern int on_shiplist(short, struct shiplist *);
extern int have_found(u_char, struct shiplook *); extern void add_shiplist(short, struct shiplist **);
extern void set_have_looked(u_char, struct shiplook *); extern void free_shiplist(struct shiplist **);
extern void set_have_found(u_char, struct shiplook *); extern void print_shiplist(struct shiplist *);
extern int print_found(struct shiplook *);
#endif /* _PLANE_H_ */ #endif /* _PLANE_H_ */

View file

@ -75,7 +75,7 @@ extern void scuttle_ship(struct shpstr *);
extern void resnoise(struct sctstr *sptr, int public_amt, s_char *name, extern void resnoise(struct sctstr *sptr, int public_amt, s_char *name,
int old, int new); int old, int new);
extern int line_of_sight(char **rad, int ax, int ay, int bx, int by); 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 int tend_nxtitem(struct nstr_item *, void *);
extern s_char *prsub(struct shpstr *); extern s_char *prsub(struct shpstr *);
extern int check_trade(void); 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 unitsatxy(coord, coord, int, int);
extern int planesatxy(coord, coord, int, int, struct emp_qelem *); extern int planesatxy(coord, coord, int, int, struct emp_qelem *);
extern int asw_shipsatxy(coord, coord, int, int, struct plnstr *, extern int asw_shipsatxy(coord, coord, int, int, struct plnstr *,
struct shiplook *); struct shiplist **);
extern int num_shipsatxy(coord, coord, int, int); extern int num_shipsatxy(coord, coord, int, int);
extern int islist(s_char *); extern int islist(s_char *);
/* src/lib/subs/mission.c */ /* src/lib/subs/mission.c */

View file

@ -496,7 +496,7 @@ ship_bomb(struct emp_qelem *list, struct sctstr *target)
int ignore; int ignore;
struct shpstr ship; struct shpstr ship;
int nships = 0; int nships = 0;
struct shiplook head, *s, *s2; struct shiplist *head = NULL;
s_char buf[1024]; s_char buf[1024];
s_char prompt[128]; s_char prompt[128];
s_char msg[128]; s_char msg[128];
@ -506,20 +506,9 @@ ship_bomb(struct emp_qelem *list, struct sctstr *target)
int gun; int gun;
int shell; int shell;
memset(&head, 0, sizeof(struct shiplook));
head.uid = -1;
for (qp = list->q_forw; qp != list && !player->aborted; for (qp = list->q_forw; qp != list && !player->aborted;
qp = qp->q_forw) { qp = qp->q_forw) {
if (head.uid != -1) { free_shiplist(&head);
s = head.next;
while (s != (struct shiplook *)0) {
s2 = s;
s = s->next;
free(s2);
}
}
memset(&head, 0, sizeof(struct shiplook));
head.uid = -1;
plp = (struct plist *)qp; plp = (struct plist *)qp;
if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T))) if ((plp->pcp->pl_flags & P_C) && (!(plp->pcp->pl_flags & P_T)))
continue; continue;
@ -545,7 +534,7 @@ ship_bomb(struct emp_qelem *list, struct sctstr *target)
} }
if (*q == '?') { if (*q == '?') {
if (plp->pcp->pl_flags & P_A) if (plp->pcp->pl_flags & P_A)
print_found(&head); print_shiplist(head);
else else
shipsatxy(target->sct_x, target->sct_y, 0, M_SUB); shipsatxy(target->sct_x, target->sct_y, 0, M_SUB);
continue; continue;
@ -634,12 +623,7 @@ ship_bomb(struct emp_qelem *list, struct sctstr *target)
next: next:
; ;
} }
s = head.next; free_shiplist(&head);
while (s != (struct shiplook *)0) {
s2 = s;
s = s->next;
free(s2);
}
} }
static void static void

View file

@ -228,7 +228,7 @@ sona(void)
void void
plane_sona(struct emp_qelem *plane_list, int x, int y, plane_sona(struct emp_qelem *plane_list, int x, int y,
struct shiplook *head) struct shiplist **head)
{ {
struct plnstr *pp; struct plnstr *pp;
struct plchrstr *pcp; struct plchrstr *pcp;
@ -265,13 +265,8 @@ plane_sona(struct emp_qelem *plane_list, int x, int y,
targ = &s; targ = &s;
if (targ->shp_own == pp->pln_own || targ->shp_own == 0) if (targ->shp_own == pp->pln_own || targ->shp_own == 0)
continue; continue;
/* if (on_shiplist(targ->shp_uid, *head))
if (have_looked(targ->shp_uid,head))
continue;
*/
if (have_found(targ->shp_uid, head))
continue; continue;
set_have_looked(targ->shp_uid, head);
tmcp = &mchr[(int)targ->shp_type]; tmcp = &mchr[(int)targ->shp_type];
if (!(tmcp->m_flags & M_SUB)) if (!(tmcp->m_flags & M_SUB))
continue; continue;
@ -294,7 +289,7 @@ plane_sona(struct emp_qelem *plane_list, int x, int y,
} }
if ((dist > vrange)) if ((dist > vrange))
continue; continue;
set_have_found(targ->shp_uid, head); add_shiplist(targ->shp_uid, head);
if (!found) { if (!found) {
mpr(pp->pln_own, mpr(pp->pln_own,
"\nSonar contact in %s\n", xyas(x, y, pp->pln_own)); "\nSonar contact in %s\n", xyas(x, y, pp->pln_own));

View file

@ -95,17 +95,13 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
struct natstr *over, *mynatp; struct natstr *over, *mynatp;
struct plist *plp; struct plist *plp;
int evaded; int evaded;
struct shiplook head; struct shiplist *head = NULL;
struct shiplook *s, *s2;
int changed = 0; int changed = 0;
int intown = 0; /* Last owner to intercept */ int intown = 0; /* Last owner to intercept */
/* We want to only intercept once per sector per owner. So, if we overfly /* 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 a sector, and then overfly some land units or ships, we don't want to
potentially intercept 3 times. */ potentially intercept 3 times. */
memset(&head, 0, sizeof(struct shiplook));
head.uid = -1;
plp = (struct plist *)bomb_list->q_forw; plp = (struct plist *)bomb_list->q_forw;
plane_owner = plp->plane.pln_own; 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); writemap(player->cnum);
/* Now, if the bomber and escort lists are empty, we are done */ /* Now, if the bomber and escort lists are empty, we are done */
if (QEMPTY(bomb_list) && QEMPTY(esc_list)) { if (QEMPTY(bomb_list) && QEMPTY(esc_list)) {
if (mission_flags & P_A) { if (mission_flags & P_A)
s = head.next; free_shiplist(&head);
while (s != (struct shiplook *)0) {
s2 = s;
s = s->next;
free(s2);
}
}
return; return;
} }
@ -356,15 +346,8 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
} }
} }
} }
if (mission_flags & P_A)
if ((mission_flags & P_A) && (head.uid != -1)) { free_shiplist(&head);
s = head.next;
while (s != (struct shiplook *)0) {
s2 = s;
s = s->next;
free(s2);
}
}
} }
static int static int

View file

@ -28,133 +28,80 @@
* aswplnsubs.c: Various subroutines used for ASW planes * aswplnsubs.c: Various subroutines used for ASW planes
* *
* Known contributors to this file: * Known contributors to this file:
* Ron Koenderink, 2004
* *
*/ */
#include "misc.h" #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 "file.h"
#include "plane.h" #include "plane.h"
#include <fcntl.h>
#include <ctype.h>
#include "prototypes.h"
int int
have_looked(u_char uid, struct shiplook *head) on_shiplist(short uid, struct shiplist *head)
{ {
struct shiplook *s; struct shiplist *s;
s = head; s = head;
if (s->uid == -1) while (s != NULL) {
return 0;
while (s != ((struct shiplook *)0)) {
if (s->uid == uid) if (s->uid == uid)
return s->looked; return 1;
s = s->next; 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)) {
if (s->uid == uid)
return s->found;
s = s->next;
}
return 0; return 0;
} }
void 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; s = *head;
if (s->uid == -1) { s2 = NULL;
s->uid = uid;
s->looked = 1;
s->found = 0;
s->next = (struct shiplook *)0;
}
while (s != ((struct shiplook *)0)) { while (s != NULL) {
if (s->uid == uid) { if (s->uid == uid) {
s->looked = 1;
return; return;
} }
s2 = s; s2 = s;
s = s->next; s = s->next;
} }
s = (struct shiplook *)malloc(sizeof(struct shiplook)); s = malloc(sizeof(struct shiplist));
memset(s, 0, sizeof(struct shiplook)); if (s2 != NULL)
s2->next = s; s2->next = s;
else
*head = s;
s->uid = uid; s->uid = uid;
s->looked = 1; s->next = NULL;
s->next = (struct shiplook *)0;
} }
void 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; s = *head;
if (s->uid == -1) {
s->uid = uid;
s->looked = 0;
s->found = 1;
s->next = (struct shiplook *)0;
}
while (s != NULL) {
while (s != ((struct shiplook *)0)) {
if (s->uid == uid) {
s->found = 1;
return;
}
s2 = s; s2 = s;
s = s->next; s = s->next;
free(s2);
} }
*head = NULL;
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;
} }
int void
print_found(struct shiplook *head) print_shiplist(struct shiplist *head)
{ {
struct shiplook *s; struct shiplist *s;
int first; int first;
struct mchrstr *mp; struct mchrstr *mp;
struct shpstr ship; struct shpstr ship;
s = head; s = head;
first = 1; first = 1;
if (s->uid == -1)
return 0;
while (s != ((struct shiplook *)0)) { while (s != NULL) {
getship(s->uid, &ship); getship(s->uid, &ship);
mp = &mchr[(int)ship.shp_type]; mp = &mchr[(int)ship.shp_type];
if (first) { if (first) {
@ -165,6 +112,4 @@ print_found(struct shiplook *head)
cname(ship.shp_own), effadv(ship.shp_effic), prship(&ship)); cname(ship.shp_own), effadv(ship.shp_effic), prship(&ship));
s = s->next; s = s->next;
} }
return 1;
} }

View file

@ -209,7 +209,7 @@ planesatxy(coord x, coord y, int wantflags, int nowantflags,
int int
asw_shipsatxy(coord x, coord y, int wantflags, int nowantflags, 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 first;
int ships; int ships;
@ -239,7 +239,7 @@ asw_shipsatxy(coord x, coord y, int wantflags, int nowantflags,
shp_hardtarget(&ship), EF_SHIP)) shp_hardtarget(&ship), EF_SHIP))
continue; continue;
} }
set_have_found(ship.shp_uid, head); add_shiplist(ship.shp_uid, head);
if (first) { if (first) {
pr(" # owner eff type\n"); pr(" # owner eff type\n");
first = 0; first = 0;