]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/tend.c
Update copyright notice.
[empserver] / src / lib / commands / tend.c
index c26387e3049f0548158efd8365f1e01ff4cdfec7..a1535a103bb1e0d6d5f539f22e6ce0929035148b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2004, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -19,9 +19,9 @@
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
  *     Steve McClure, 2000
  */
 
-#include <string.h>
-#include "misc.h"
-#include "player.h"
-#include "plague.h"
-#include "xy.h"
-#include "file.h"
-#include "ship.h"
+#include <config.h>
+
+#include "commands.h"
 #include "item.h"
-#include "nsc.h"
-#include "nat.h"
 #include "land.h"
+#include "plague.h"
 #include "plane.h"
-#include "genitem.h"
-#include "commands.h"
+#include "ship.h"
 
 static void expose_ship(struct shpstr *s1, struct shpstr *s2);
-static int tend_land(struct shpstr *tenderp, s_char *units);
+static int tend_land(struct shpstr *tenderp, char *units);
 
 int
 tend(void)
@@ -69,9 +63,9 @@ tend(void)
     int transfer;
     int total;
     int type;
-    s_char *p;
-    s_char prompt[512];
-    s_char buf[1024];
+    char *p;
+    char prompt[512];
+    char buf[1024];
 
     if (!(p = getstarg(player->argp[1],
                       "Tend what commodity (or 'land')? ", buf)) || !*p)
@@ -90,7 +84,7 @@ tend(void)
                  getstarg(player->argp[2], "Tender(s)? ", buf)))
        return RET_SYN;
 
-    while (nxtitem(&tenders, (s_char *)&tender)) {
+    while (nxtitem(&tenders, &tender)) {
        if (!player->owner)
            continue;
        if (type == EF_LAND) {
@@ -114,13 +108,13 @@ tend(void)
            pr("Amount must be non-zero!\n");
            return RET_SYN;
        }
-       ontender = tender.shp_item[ip->i_vtype];
+       ontender = tender.shp_item[ip->i_uid];
        if (ontender == 0 && amt > 0) {
            pr("No %s on %s\n", ip->i_name, prship(&tender));
            return RET_FAIL;
        }
        vbase = &mchr[(int)tender.shp_type];
-       maxtender = vbase->m_item[ip->i_vtype];
+       maxtender = vbase->m_item[ip->i_uid];
        if (maxtender == 0) {
            pr("A %s cannot hold any %s\n",
               mchr[(int)tender.shp_type].m_name, ip->i_name);
@@ -133,7 +127,7 @@ tend(void)
        if (!check_ship_ok(&tender))
            return RET_SYN;
        total = 0;
-       while (tend_nxtitem(&targets, (s_char *)&target)) {
+       while (nxtitem(&targets, &target)) {
            if (!player->owner &&
                (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
                continue;
@@ -142,34 +136,39 @@ tend(void)
            if (tender.shp_x != target.shp_x ||
                tender.shp_y != target.shp_y)
                continue;
-           ontarget = target.shp_item[ip->i_vtype];
+           ontarget = target.shp_item[ip->i_uid];
            if (ontarget == 0 && amt < 0) {
                pr("No %s on %s\n", ip->i_name, prship(&target));
                continue;
            }
            vbase = &mchr[(int)target.shp_type];
-           maxtarget = vbase->m_item[ip->i_vtype];
+           maxtarget = vbase->m_item[ip->i_uid];
            if (amt < 0) {
                if (!player->owner)
                    amt = 0;
 
                /* take from target and give to tender */
-               transfer = min(ontarget, -amt);
-               transfer = min(maxtender - ontender, transfer);
+               transfer = MIN(ontarget, -amt);
+               transfer = MIN(maxtender - ontender, transfer);
                if (transfer == 0)
                    continue;
-               target.shp_item[ip->i_vtype] = ontarget - transfer;
+               target.shp_item[ip->i_uid] = ontarget - transfer;
                ontender += transfer;
                total += transfer;
            } else {
                /* give to target from tender */
-               transfer = min(ontender, amt);
-               transfer = min(transfer, maxtarget - ontarget);
+               transfer = MIN(ontender, amt);
+               transfer = MIN(transfer, maxtarget - ontarget);
                if (transfer == 0)
                    continue;
-               target.shp_item[ip->i_vtype] = ontarget + transfer;
+               target.shp_item[ip->i_uid] = ontarget + transfer;
                ontender -= transfer;
                total += transfer;
+               if (transfer && target.shp_own != player->cnum) {
+                   wu(0, target.shp_own, "%s tended %d %s to %s\n",
+                      cname(player->cnum), total, ip->i_name,
+                      prship(&target));
+               }
            }
            expose_ship(&tender, &target);
            putship(target.shp_uid, &target);
@@ -181,7 +180,7 @@ tend(void)
        pr("%d total %s transferred %s %s\n",
           total, ip->i_name, (amt > 0) ? "off of" : "to",
           prship(&tender));
-       tender.shp_item[ip->i_vtype] = ontender;
+       tender.shp_item[ip->i_uid] = ontender;
        tender.shp_mission = 0;
        putship(tender.shp_uid, &tender);
     }
@@ -197,83 +196,8 @@ expose_ship(struct shpstr *s1, struct shpstr *s2)
        s1->shp_pstage = PLG_EXPOSED;
 }
 
-/*
- * tend_nxtitem.c
- *
- * get next item from list. Stolen from nxtitem to make 1 itsy-bitsy change
- *
- * Dave Pare, 1989
- */
-
-int
-tend_nxtitem(struct nstr_item *np, void *ptr)
-{
-    struct genitem *gp;
-    int selected;
-
-    if (np->sel == NS_UNDEF)
-       return 0;
-    gp = (struct genitem *)ptr;
-    do {
-       if (np->sel == NS_LIST) {
-           np->index++;
-           if (np->index >= np->size)
-               return 0;
-           np->cur = np->list[np->index];
-       } else {
-           np->cur++;
-       }
-       if (!np->read(np->type, np->cur, ptr)) {
-           /* if read fails, fatal */
-           return 0;
-       }
-       selected = 1;
-       switch (np->sel) {
-       case NS_LIST:
-           /* The change is to take the player->owner check out here */
-           break;
-       case NS_ALL:
-           /* XXX maybe combine NS_LIST and NS_ALL later */
-           break;
-       case NS_DIST:
-           if (!xyinrange(gp->x, gp->y, &np->range)) {
-               selected = 0;
-               break;
-           }
-           np->curdist = mapdist((int)gp->x, (int)gp->y,
-                                 (int)np->cx, (int)np->cy);
-           if (np->curdist > np->dist)
-               selected = 0;
-           break;
-       case NS_AREA:
-           if (!xyinrange(gp->x, gp->y, &np->range))
-               selected = 0;
-           if (gp->x == np->range.hx || gp->y == np->range.hy)
-               selected = 0;
-           break;
-       case NS_XY:
-           if (gp->x != np->cx || gp->y != np->cy)
-               selected = 0;
-           break;
-       case NS_GROUP:
-           if (np->group != gp->group)
-               selected = 0;
-           break;
-       default:
-           CANT_HAPPEN("bad np->sel");
-           return 0;
-       }
-       if (selected && np->ncond) {
-           /* nstr_exec is expensive, so we do it last */
-           if (!nstr_exec(np->cond, np->ncond, ptr))
-               selected = 0;
-       }
-    } while (!selected);
-    return 1;
-}
-
 static int
-tend_land(struct shpstr *tenderp, s_char *units)
+tend_land(struct shpstr *tenderp, char *units)
 {
     struct nstr_item lni;
     struct nstr_item targets;
@@ -281,12 +205,12 @@ tend_land(struct shpstr *tenderp, s_char *units)
     struct lndstr land;
     struct plnstr plane;
     struct nstr_item pni;
-    s_char buf[1024];
+    char buf[1024];
 
     if (!snxtitem(&lni, EF_LAND, units))
        return RET_SYN;
 
-    while (nxtitem(&lni, (s_char *)&land)) {
+    while (nxtitem(&lni, &land)) {
        if (!player->owner)
            continue;
        if (land.lnd_ship != tenderp->shp_uid) {
@@ -294,7 +218,8 @@ tend_land(struct shpstr *tenderp, s_char *units)
            continue;
        }
        if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
-           pr("%s does not have \"assault\" capability and can't be tended\n", prland(&land));
+           pr("%s does not have \"assault\" capability and can't be tended\n",
+              prland(&land));
            continue;
        }
        if (!snxtitem(&targets, EF_SHIP,
@@ -303,7 +228,7 @@ tend_land(struct shpstr *tenderp, s_char *units)
            break;
        if (!check_land_ok(&land))
            return RET_SYN;
-       while (tend_nxtitem(&targets, (s_char *)&target)) {
+       while (nxtitem(&targets, &target)) {
            if (!player->owner &&
                (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
                continue;
@@ -317,7 +242,24 @@ tend_land(struct shpstr *tenderp, s_char *units)
            count_units(&target);
            getship(target.shp_uid, &target);
 
-           if (target.shp_nland >= mchr[(int)target.shp_type].m_nland) {
+           if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
+               (!((mchr[(int)target.shp_type].m_flags & M_SUPPLY) &&
+                  (!(mchr[(int)target.shp_type].m_flags & M_SUB))))) {
+               pr("You can only load light units onto ships,\n"
+                  "unless the ship is a non-sub supply ship\n"
+                  "%s not tended\n", prland(&land));
+               continue;
+           }
+
+           if ((mchr[(int)target.shp_type].m_flags & M_SUB) &&
+               (lchr[(int)land.lnd_type].l_flags & L_SPY) &&
+               !mchr[(int)target.shp_type].m_nland) {
+               if (target.shp_nland > 1) {
+                   pr("%s doesn't have room for more than two spy units!\n",
+                      prship(&target));
+                   continue;
+               }
+           } else if (target.shp_nland >= mchr[(int)target.shp_type].m_nland) {
                if (mchr[(int)target.shp_type].m_nland)
                    pr("%s doesn't have room for any more land units!\n",
                       prship(&target));
@@ -330,13 +272,7 @@ tend_land(struct shpstr *tenderp, s_char *units)
            sprintf(buf, "loaded on your %s at %s",
                    prship(&target), xyas(target.shp_x, target.shp_y,
                                          target.shp_own));
-           gift(target.shp_own, player->cnum, (s_char *)&land,
-                EF_LAND, buf);
-           makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
-                    land.lnd_y);
-           land.lnd_own = target.shp_own;
-           makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
-                       land.lnd_y);
+           gift(target.shp_own, player->cnum, &land, buf);
            land.lnd_ship = target.shp_uid;
            land.lnd_harden = 0;
            land.lnd_mission = 0;
@@ -347,19 +283,13 @@ tend_land(struct shpstr *tenderp, s_char *units)
            count_units(tenderp);
            putship(tenderp->shp_uid, tenderp);
            snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
-           while (nxtitem(&pni, (s_char *)&plane)) {
+           while (nxtitem(&pni, &plane)) {
                if (plane.pln_flags & PLN_LAUNCHED)
                    continue;
                if (plane.pln_land != land.lnd_uid)
                    continue;
                sprintf(buf, "loaded on %s", prship(&target));
-               gift(target.shp_own, player->cnum, (s_char *)&plane,
-                    EF_PLANE, buf);
-               makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
-                        plane.pln_x, plane.pln_y);
-               plane.pln_own = target.shp_own;
-               makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
-                           plane.pln_x, plane.pln_y);
+               gift(target.shp_own, player->cnum, &plane, buf);
                plane.pln_mission = 0;
                putplane(plane.pln_uid, &plane);
            }