]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/unitsub.c
Update copyright notice
[empserver] / src / lib / subs / unitsub.c
index ebd4d5e1732dc021d282288dfd6c1ec17fc664f9..ea5756821951251644e315f100cada95df8a1380 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
  *  ---
  *
  *  unitsub.c: Common subroutines for multiple type of units
- * 
+ *
  *  Known contributors to this file:
  *     Ron Koenderink, 2007
- *     Markus Armbruster, 2008
+ *     Markus Armbruster, 2009
  */
 
 #include <config.h>
@@ -37,6 +37,7 @@
 #include "empobj.h"
 #include "file.h"
 #include "player.h"
+#include "optlist.h"
 #include "prototypes.h"
 #include "unit.h"
 
@@ -55,7 +56,7 @@ unit_list(struct emp_qelem *unit_list)
        return;
     qp = unit_list->q_back;
     ulp = (struct ulist *)qp;
-    type = ulp->unit.ef_type;
+    type = ulp->unit.gen.ef_type;
     if (CANT_HAPPEN(type != EF_LAND && type != EF_SHIP))
        return;
 
@@ -146,14 +147,14 @@ unit_path(int together, struct empobj *unit, char *buf)
        return NULL;
 
     if (!sarg_xy(buf, &destx, &desty))
-       return 0;
+       return NULL;
     if (!together) {
        pr("Cannot go to a destination sector if not all starting in the same sector\n");
-       return 0;
+       return NULL;
     }
     if (!getsect(destx, desty, &d_sect)) {
        pr("%d,%d is not a sector\n", destx, desty);
-       return 0;
+       return NULL;
     }
     if (unit->ef_type == EF_SHIP) {
        cp = BestShipPath(buf, unit->x, unit->y,
@@ -161,7 +162,7 @@ unit_path(int together, struct empobj *unit, char *buf)
        if (!cp || unit->mobil <= 0) {
            pr("Can't get to '%s' right now.\n",
                xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
-           return 0;
+           return NULL;
        }
     } else {
        getsect(unit->x, unit->y, &sect);
@@ -172,7 +173,7 @@ unit_path(int together, struct empobj *unit, char *buf)
               mtype == MOB_RAIL ? "railway" : "path",
               xyas(unit->x, unit->y, player->cnum),
               xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
-           return 0;
+           return NULL;
        }
        pr("Using path '%s'\n", cp);
     }
@@ -190,10 +191,10 @@ unit_view(struct emp_qelem *list)
     for (qp = list->q_back; qp != list; qp = next) {
        next = qp->q_back;
        ulp = (struct ulist *)qp;
-       if (CANT_HAPPEN(!(ef_flags(ulp->unit.ef_type) & EFF_XY)))
+       if (CANT_HAPPEN(!(ef_flags(ulp->unit.gen.ef_type) & EFF_XY)))
            continue;
        getsect(ulp->unit.gen.x, ulp->unit.gen.y, &sect);
-       if (ulp->unit.ef_type == EF_SHIP) {
+       if (ulp->unit.gen.ef_type == EF_SHIP) {
            if (((struct mchrstr *)ulp->chrp)->m_flags & M_FOOD)
                pr("[fert:%d] ", sect.sct_fertil);
            if (((struct mchrstr *)ulp->chrp)->m_flags & M_OIL)
@@ -299,14 +300,63 @@ unit_give_away(struct empobj *unit, natid recipient, natid giver)
     }
 
     unit->own = recipient;
-    unit->mission = 0;
-    unit->group = 0;
+    unit_wipe_orders(unit);
+    put_empobj(unit->ef_type, unit->uid, unit);
 
     for (type = EF_PLANE; type <= EF_NUKE; type++) {
        snxtitem_cargo(&ni, type, unit->ef_type, unit->uid);
-       while (nxtitem(&ni, &cargo)) {
+       while (nxtitem(&ni, &cargo))
            unit_give_away(&cargo.gen, recipient, giver);
-           put_empobj(type, cargo.gen.uid, &cargo.gen);
+    }
+}
+
+/*
+ * Wipe orders and such from UNIT.
+ */
+void
+unit_wipe_orders(struct empobj *unit)
+{
+    struct shpstr *sp;
+    struct plnstr *pp;
+    struct lndstr *lp;
+    int i;
+
+    unit->group = 0;
+    unit->opx = unit->opy = 0;
+    unit->mission = 0;
+    unit->radius = 0;
+
+    switch (unit->ef_type) {
+    case EF_SHIP:
+       sp = (struct shpstr *)unit;
+       sp->shp_destx[0] = sp->shp_desty[0] = 0;
+       sp->shp_destx[1] = sp->shp_desty[1] = 0;
+       for (i = 0; i < TMAX; ++i) {
+           sp->shp_tstart[i] = I_NONE;
+           sp->shp_tend[i] = I_NONE;
+           sp->shp_lstart[i] = 0;
+           sp->shp_lend[i] = 0;
        }
+       sp->shp_autonav = 0;
+       sp->shp_mobquota = 0;
+       sp->shp_path[0] = 0;
+       sp->shp_follow = sp->shp_uid;
+       sp->shp_rflags = 0;
+       sp->shp_rpath[0] = 0;
+       break;
+    case EF_PLANE:
+       pp = (struct plnstr *)unit;
+       pp->pln_range = pln_range_max(pp);
+       break;
+    case EF_LAND:
+       lp = (struct lndstr *)unit;
+       lp->lnd_retreat = morale_base;
+       lp->lnd_rflags = 0;
+       lp->lnd_rpath[0] = 0;
+       break;
+    case EF_NUKE:
+       break;
+    default:
+       CANT_REACH();
     }
 }