]> git.pond.sub.org Git - empserver/blobdiff - src/lib/common/cargo.c
Update copyright notice
[empserver] / src / lib / common / cargo.c
index cf8a6282c09e3224d6ca00fd7c74e6178a6e23c3..5c36377d9ab223c32e7df9f040d5fec8effdd7d4 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
  *  ---
  *
  *  cargo.c: Cargo lists
- * 
+ *
  *  Known contributors to this file:
- *     Markus Armbruster, 2008
+ *     Markus Armbruster, 2009
  */
 
 #include <config.h>
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 #include "file.h"
 #include "unit.h"
 
 struct clink {
-    short next;
-    short head[EF_NUKE - EF_PLANE + 1];
+    int next;
+    int head[EF_NUKE - EF_PLANE + 1];
 };
 
 /*
@@ -69,15 +69,15 @@ struct clink {
  * cargo lists know nothing about that.
  */
 static struct clink *clink[EF_NUKE + 1];
-static short nclink[EF_NUKE + 1];
+static int nclink[EF_NUKE + 1];
 
 /*
  * Return pointer to CL's cargo list head for file type TYPE.
  */
-static short *
+static int *
 clink_headp(struct clink *cl, int type)
 {
-    static short dummy;
+    static int dummy;
 
     if (CANT_HAPPEN(type < EF_PLANE || type > EF_NUKE)) {
        dummy = -1;
@@ -103,7 +103,7 @@ clink_init(struct clink *cl)
  * Check whether *UIDP is a valid uid for file type TYPE.
  */
 static void
-clink_check1(short *uidp, int type)
+clink_check1(int *uidp, int type)
 {
     if (CANT_HAPPEN(*uidp >= nclink[type]))
        *uidp = -1;
@@ -137,13 +137,14 @@ clink_check(int type)
 static void
 clink_add(struct clink *cl, int type, int uid)
 {
-    short *head = clink_headp(cl, type);
+    int *head = clink_headp(cl, type);
 
     if (CANT_HAPPEN(type < 0 || type > EF_NUKE
                    || uid < 0 || uid >= nclink[type]))
        return;
     if (CANT_HAPPEN(*head >= nclink[type]))
        *head = -1;
+    CANT_HAPPEN(clink[type][uid].next >= 0);
     clink[type][uid].next = *head;
     *head = uid;
 }
@@ -155,10 +156,10 @@ clink_add(struct clink *cl, int type, int uid)
 static void
 clink_rem(struct clink *cl, int type, int uid)
 {
-    short *head = clink_headp(cl, type);
+    int *head = clink_headp(cl, type);
     struct clink *linkv;
     int n;
-    short *p;
+    int *p;
 
     if (CANT_HAPPEN(type < 0 || type > EF_NUKE))
        return;
@@ -171,6 +172,7 @@ clink_rem(struct clink *cl, int type, int uid)
     }
 
     *p = linkv[uid].next;
+    linkv[uid].next = -1;
 }
 
 /*
@@ -183,9 +185,9 @@ unit_carrier_change(struct empobj *cargo, int type, int old, int new)
 {
     if (CANT_HAPPEN(type < 0 || type > EF_NUKE))
        return;
-    if (old >= 0)
+    if (old >= 0 && !CANT_HAPPEN(old >= nclink[type]))
        clink_rem(&clink[type][old], cargo->ef_type, cargo->uid);
-    if (new >= 0)
+    if (new >= 0 && !CANT_HAPPEN(new >= nclink[type]))
        clink_add(&clink[type][new], cargo->ef_type, cargo->uid);
 }
 
@@ -233,30 +235,38 @@ unit_cargo_init(void)
     struct lndstr *lp;
     struct nukstr *np;
 
-    for (i = EF_SHIP; i <= EF_NUKE; i++) {
-       nclink[i] = 0;
+    memset(nclink, 0, sizeof(nclink));
+    for (i = EF_SHIP; i <= EF_NUKE; i++)
        unit_onresize(i);
-    }
 
     for (i = 0; (pp = getplanep(i)); i++) {
-       if (!pp->pln_own)
+       if (!pp->pln_own) {
+           if (CANT_HAPPEN(pp->pln_ship >= 0 || pp->pln_land >= 0))
+               pp->pln_ship = pp->pln_land = -1;
            continue;
+       }
        if (CANT_HAPPEN(pp->pln_ship >= 0 && pp->pln_land >= 0))
            pp->pln_land = -1;
        pln_carrier_change(pp, EF_SHIP, -1, pp->pln_ship);
        pln_carrier_change(pp, EF_LAND, -1, pp->pln_land);
     }
     for (i = 0; (lp = getlandp(i)); i++) {
-       if (!lp->lnd_own)
+       if (!lp->lnd_own) {
+           if (CANT_HAPPEN(lp->lnd_ship >= 0 || lp->lnd_land >= 0))
+               lp->lnd_ship = lp->lnd_land = -1;
            continue;
+       }
        if (CANT_HAPPEN(lp->lnd_ship >= 0 && lp->lnd_land >= 0))
            lp->lnd_land = -1;
        lnd_carrier_change(lp, EF_SHIP, -1, lp->lnd_ship);
        lnd_carrier_change(lp, EF_LAND, -1, lp->lnd_land);
     }
     for (i = 0; (np = getnukep(i)); i++) {
-       if (!np->nuk_own)
+       if (!np->nuk_own) {
+           if (CANT_HAPPEN(np->nuk_plane >= 0))
+               np->nuk_plane = -1;
            continue;
+       }
        nuk_carrier_change(np, EF_PLANE, -1, np->nuk_plane);
     }
 }
@@ -266,14 +276,14 @@ unit_cargo_init(void)
  * Return 0 on success, -1 on error.
  * This is the struct empfile onresize callback for units.
  */
-int
+void
 unit_onresize(int type)
 {
     int n, i;
     struct clink *cl;
 
     if (CANT_HAPPEN(type < EF_SHIP || type > EF_NUKE))
-       return -1;
+       return;
 
     n = ef_nelem(type);
     cl = realloc(clink[type], n * sizeof(*clink[type]));
@@ -283,8 +293,8 @@ unit_onresize(int type)
        clink_init(&cl[i]);
     clink[type] = cl;
     nclink[type] = n;
-    clink_check(type);
-    return 0;
+    if (ef_flags(type) & EFF_MEM)
+       clink_check(type);
 }
 
 /*
@@ -296,7 +306,7 @@ unit_onresize(int type)
 int
 unit_cargo_first(int type, int uid, int cargo_type)
 {
-    short *headp;
+    int *headp;
 
     if (CANT_HAPPEN(type < EF_SHIP || type > EF_NUKE))
        return -1;