]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/unitsub.c
Update copyright notice
[empserver] / src / lib / subs / unitsub.c
index 4d2d3d99bc6230b3b12f33aa01093fec833e2813..a3e537bde4c0323f7608b520da6baeda31a9435b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                Ken Stevens, Steve McClure, Markus Armbruster
  *
  *  Empire is free software: you can redistribute it and/or modify
  *
  *  Known contributors to this file:
  *     Ron Koenderink, 2007
- *     Markus Armbruster, 2009
+ *     Markus Armbruster, 2009-2013
  */
 
 #include <config.h>
 
-#include "empobj.h"
 #include "file.h"
 #include "path.h"
 #include "player.h"
 #include "prototypes.h"
 #include "unit.h"
 
+char *
+unit_nameof(struct empobj *gp)
+{
+    switch (gp->ef_type) {
+    case EF_SHIP:
+       return prship((struct shpstr *)gp);
+    case EF_PLANE:
+       return prplane((struct plnstr *)gp);
+    case EF_LAND:
+       return prland((struct lndstr *)gp);
+    case EF_NUKE:
+       return prnuke((struct nukstr *)gp);
+    }
+    CANT_REACH();
+    return "The Beast #666";
+}
+
 void
 unit_list(struct emp_qelem *unit_list)
 {
@@ -61,7 +77,7 @@ unit_list(struct emp_qelem *unit_list)
        return;
 
     if (type == EF_LAND)
-       pr("lnd#     land type       x,y    a  eff  sh gun xl  mu tech retr\n");
+       pr("lnd#     land type       x,y    a  eff mil  sh gun xl ln  mu tech retr\n");
     else
        pr("shp#     ship type       x,y   fl  eff mil  sh gun pn he xl ln mob tech\n");
 
@@ -79,9 +95,10 @@ unit_list(struct emp_qelem *unit_list)
        pr("%1.1s", &unit->group);
        pr("%4d%%", unit->effic);
        if (type == EF_LAND) {
+           pr("%4d", lnd->lnd_item[I_MILIT]);
            pr("%4d", lnd->lnd_item[I_SHELL]);
            pr("%4d", lnd->lnd_item[I_GUN]);
-           pr("%3d", lnd_nxlight(lnd));
+           pr("%3d%3d", lnd_nxlight(lnd), lnd_nland(lnd));
        } else {
            pr("%4d", shp->shp_item[I_MILIT]);
            pr("%4d", shp->shp_item[I_SHELL]);
@@ -115,7 +132,7 @@ unit_put(struct emp_qelem *list, natid actor)
                        && unit->ef_type != EF_SHIP))
            continue;
        if (actor) {
-           mpr(actor, "%s stopped at %s\n", obj_nameof(unit),
+           mpr(actor, "%s stopped at %s\n", unit_nameof(unit),
                xyas(unit->x, unit->y, actor));
            if (unit->ef_type == EF_LAND) {
                if (ulp->mobil < -127)
@@ -134,52 +151,35 @@ unit_put(struct emp_qelem *list, natid actor)
 }
 
 char *
-unit_path(int together, struct empobj *unit, char *buf)
+unit_path(int together, struct empobj *unit, char *buf, size_t bufsz)
 {
     coord destx;
     coord desty;
-    struct sctstr d_sect, sect;
+    struct sctstr sect;
     size_t len;
-    char *cp;
+    double c;
     int mtype;
 
     if (CANT_HAPPEN(unit->ef_type != EF_LAND && unit->ef_type != EF_SHIP))
        return NULL;
 
     if (!sarg_xy(buf, &destx, &desty))
-       return NULL;
+       return buf;
     if (!together) {
        pr("Cannot go to a destination sector if not all starting in the same sector\n");
        return NULL;
     }
-    if (!getsect(destx, desty, &d_sect)) {
-       pr("%d,%d is not a sector\n", destx, desty);
-       return NULL;
-    }
     if (unit->ef_type == EF_SHIP) {
-       if (path_find(unit->x, unit->y, d_sect.sct_x, d_sect.sct_y,
-                     player->cnum, MOB_SAIL) < 0)
-           cp = NULL;
-       else {
-           len = path_find_route(buf, 100, unit->x, unit->y,
-                                 d_sect.sct_x, d_sect.sct_y);
-           if (len >= 100)
-               cp = NULL;
-           else {
-               if (len == 0)
-                   strcpy(buf, "h");
-               cp = buf;
-           }
-       }
-       if (!cp || unit->mobil <= 0) {
+       c = path_find(unit->x, unit->y, destx, desty,
+                     player->cnum, MOB_SAIL);
+       if (c < 0 || unit->mobil <= 0) {
            pr("Can't get to '%s' right now.\n",
-              xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
+              xyas(destx, desty, player->cnum));
            return NULL;
        }
     } else {
        getsect(unit->x, unit->y, &sect);
        mtype = lnd_mobtype((struct lndstr *)unit);
-       buf[0] = 0;
        /*
         * Note: passing sect.sct_own for actor is funny, but works:
         * its only effect is to confine the search to that nation's
@@ -187,30 +187,29 @@ unit_path(int together, struct empobj *unit, char *buf)
         * different for marching in allied land, and passing it would
         * break path finding there.
         */
-       if (path_find(sect.sct_x, sect.sct_y, d_sect.sct_x, d_sect.sct_y,
-                     sect.sct_own, mtype) < 0)
-           cp = NULL;
-       else {
-           len = path_find_route(buf, 1024,
-                                 sect.sct_x, sect.sct_y,
-                                 d_sect.sct_x, d_sect.sct_y);
-           if (len + 1 >= 1024)
-               cp = NULL;
-           else {
-               strcpy(buf + len, "h");
-               cp = buf;
-           }
-       }
-       if (!cp) {
+       c = path_find(unit->x, unit->y, destx, desty, sect.sct_own, mtype);
+       if (c < 0) {
            pr("No owned %s from %s to %s!\n",
               mtype == MOB_RAIL ? "railway" : "path",
               xyas(unit->x, unit->y, player->cnum),
-              xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
+              xyas(destx, desty, player->cnum));
            return NULL;
        }
-       pr("Using path '%s'\n", cp);
     }
-    return cp;
+    len = path_find_route(buf, bufsz, unit->x, unit->y, destx, desty);
+    if (len == 0 || unit->ef_type == EF_LAND) {
+       if (len + 1 < bufsz)
+           strcpy(buf + len, "h");
+       len++;
+    }
+    if (len >= bufsz) {
+       pr("Can't handle path to %s, it's too long, sorry\n",
+          xyas(destx, desty, player->cnum));
+       return NULL;
+    }
+    if (unit->ef_type == EF_LAND)
+       pr("Using path '%s'\n", buf);
+    return buf;
 }
 
 void
@@ -233,44 +232,57 @@ unit_view(struct emp_qelem *list)
            if (((struct mchrstr *)ulp->chrp)->m_flags & M_OIL)
                pr("[oil:%d] ", sect.sct_oil);
        }
-       pr("%s @ %s %d%% %s\n", obj_nameof(&ulp->unit.gen),
+       pr("%s @ %s %d%% %s\n", unit_nameof(&ulp->unit.gen),
           xyas(ulp->unit.gen.x, ulp->unit.gen.y, player->cnum),
           sect.sct_effic, dchr[sect.sct_type].d_name);
     }
 }
 
+/*
+ * Teleport UNIT to X,Y.
+ * If UNIT's mission op-area is centered on it, keep it centered.
+ */
+void
+unit_teleport(struct empobj *unit, coord x, coord y)
+{
+    if (unit->opx == unit->x && unit->opy == unit->y) {
+       unit->opx = x;
+       unit->opy = y;
+    }
+    unit->x = x;
+    unit->y = y;
+}
+
 /*
  * Update cargo of CARRIER for movement or destruction.
  * If the carrier is destroyed, destroy its cargo (planes, land units,
  * nukes).
  * Else update their location to the carrier's.  Any op sectors equal
  * to location get updated, too.
+ * Return number of units updated.
  */
-void
+int
 unit_update_cargo(struct empobj *carrier)
 {
     int cargo_type;
     struct nstr_item ni;
     union empobj_storage obj;
+    int n = 0;
 
     for (cargo_type = EF_PLANE; cargo_type <= EF_NUKE; cargo_type++) {
        snxtitem_cargo(&ni, cargo_type, carrier->ef_type, carrier->uid);
        while (nxtitem(&ni, &obj)) {
-           if (!carrier->own) {
-               mpr(obj.gen.own, "%s lost!\n", obj_nameof(&obj.gen));
+           if (carrier->own)
+               unit_teleport(&obj.gen, carrier->x, carrier->y);
+           else {
+               mpr(obj.gen.own, "%s lost!\n", unit_nameof(&obj.gen));
                obj.gen.effic = 0;
-           } else {
-               /* mission op-area centered on the obj travels with it */
-               if (obj.gen.opx == obj.gen.x && obj.gen.opy == obj.gen.y) {
-                   obj.gen.opx = carrier->x;
-                   obj.gen.opy = carrier->y;
-               }
-               obj.gen.x = carrier->x;
-               obj.gen.y = carrier->y;
            }
            put_empobj(cargo_type, obj.gen.uid, &obj);
+           n++;
        }
     }
+    return n;
 }
 
 /*
@@ -299,7 +311,7 @@ unit_drop_cargo(struct empobj *unit, natid newown)
                break;
            }
            mpr(cargo.gen.own, "%s transferred off %s %d to %s\n",
-               obj_nameof(&cargo.gen),
+               unit_nameof(&cargo.gen),
                ef_nameof(unit->ef_type), unit->uid,
                xyas(cargo.gen.x, cargo.gen.y, cargo.gen.own));
            if (newown)
@@ -327,9 +339,9 @@ unit_give_away(struct empobj *unit, natid recipient, natid giver)
 
     if (giver) {
        mpr(unit->own, "%s given to %s\n",
-           obj_nameof(unit), cname(recipient));
+           unit_nameof(unit), cname(recipient));
        mpr(recipient, "%s given to you by %s\n",
-           obj_nameof(unit), cname(giver));
+           unit_nameof(unit), cname(giver));
     }
 
     unit->own = recipient;