]> git.pond.sub.org Git - empserver/commitdiff
(have_looked,have_found,set_have_looked,set_have_found,print_found):
authorRon Koenderink <rkoenderink@yahoo.ca>
Thu, 16 Dec 2004 11:12:28 +0000 (11:12 +0000)
committerRon Koenderink <rkoenderink@yahoo.ca>
Thu, 16 Dec 2004 11:12:28 +0000 (11:12 +0000)
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
include/prototypes.h
src/lib/commands/bomb.c
src/lib/commands/sona.c
src/lib/subs/aircombat.c
src/lib/subs/aswplnsubs.c
src/lib/subs/list.c

index d9384c32231fa210be04d4bd325ef994f3554686..10966b5004a83df31b25222dbeb6be4e77ba446c 100644 (file)
@@ -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_ */
index 2d93ad01be4b8e9d9240f717617792eee8423fe7..781e562aeb71de292d002f3963f7cfe3906c920c 100644 (file)
@@ -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 */
index 5643bbd0f7d34c611afe041911af212db06395a1..58f966b51a378dbec19f602957d6c5603e57f694 100644 (file)
@@ -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
index a050dade360b1f72700e147900d69b9052a90bc4..39db13f07af0ff72a1664d9de8cb9c652173eeb2 100644 (file)
@@ -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));
index 090d8720d1405f47266c6ff472edd6b680df9096..11f46227da9e9c9fea67713fa5fdf8fd06ac0621 100644 (file)
@@ -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
index 0e75755c3663ee4d1fd22df807eb3c7ac88c07ba..8f02695aa97a834cd3c158374b1587bac8e7d659 100644 (file)
  *  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 <fcntl.h>
-#include <ctype.h>
-#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;
 }
index 68df75f9ecbaddbb0702f496770c54dcf7b28ce4..7eb7f1b963d4d5bd1a40f034579f1ac53f95c132 100644 (file)
@@ -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;