]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/load.c
Fix automatic supply of defending and reacting units
[empserver] / src / lib / commands / load.c
index 746a4bb03990f134c36e939859ab503b181cd18b..b809a08b59f65baec1184798d6b9ccc882291267 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-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -113,45 +113,36 @@ load(void)
     while (nxtitem(&nbst, &ship)) {
        if (!ship.shp_own)
            continue;
-       if (!player->owner && (load_unload == UNLOAD)) {
-           continue;
-       }
-       if (opt_MARKET) {
-           if (ontradingblock(EF_SHIP, &ship)) {
-               pr("You cannot load/unload an item on the trading block!\n");
-               continue;
-           }
-       }
-
-       if (ship.shp_own != player->cnum) {
-           if (!noisy)
+       if (!player->owner) {
+           if (load_unload == UNLOAD || !noisy)
                continue;
            if (getrel(getnatp(ship.shp_own), player->cnum) < FRIENDLY)
                continue;
        }
+
        if (!getsect(ship.shp_x, ship.shp_y, &sect))    /* XXX */
            continue;
-       if ((sect.sct_own != player->cnum) &&
-           (ship.shp_own != player->cnum))
-           continue;
-       if (!player->owner &&
-           !sect_has_dock(&sect))
-           continue;
        if (!sect.sct_own)
            continue;
-       if ((sect.sct_own != player->cnum) && (load_unload == LOAD)) {
-           if (noisy)
-               pr("You don't own %s \n",
-                  xyas(ship.shp_x, ship.shp_y, player->cnum));
-           continue;
+       if (!player->owner) {
+           if (ship.shp_own != player->cnum)
+               continue;
+           if (!sect_has_dock(&sect))
+               continue;
+           if (load_unload == LOAD) {
+               if (noisy)
+                   pr("You don't own %s \n",
+                      xyas(sect.sct_x, sect.sct_y, player->cnum));
+               continue;
+           }
        }
        if (!sect_has_dock(&sect)) {
            if (noisy)
                pr("Sector %s is not a harbor or canal.\n",
-                  xyas(ship.shp_x, ship.shp_y, player->cnum));
+                  xyas(sect.sct_x, sect.sct_y, player->cnum));
            continue;
        }
-       if (sect.sct_own != player->cnum && load_unload == UNLOAD
+       if (!player->owner && load_unload == UNLOAD
            && getrel(getnatp(sect.sct_own), player->cnum) < FRIENDLY) {
            if (noisy)
                pr("You can't unload into an unfriendly %s\n",
@@ -162,9 +153,17 @@ load(void)
            if (noisy)
                pr("The %s at %s is not 2%% efficient yet.\n",
                   dchr[sect.sct_type].d_name,
-                  xyas(ship.shp_x, ship.shp_y, player->cnum));
+                  xyas(sect.sct_x, sect.sct_y, player->cnum));
            continue;
        }
+
+       if (opt_MARKET) {
+           if (ontradingblock(EF_SHIP, &ship)) {
+               pr("You cannot load/unload an item on the trading block!\n");
+               continue;
+           }
+       }
+
        switch (type) {
        case EF_PLANE:
            if (0 !=
@@ -248,10 +247,8 @@ lload(void)
     while (nxtitem(&nbst, &land)) {
        if (land.lnd_own == 0)
            continue;
-       if (!player->owner && load_unload == UNLOAD)
-           continue;
-       if (player->cnum != land.lnd_own) {
-           if (!noisy)
+       if (!player->owner) {
+           if (load_unload == UNLOAD || !noisy)
                continue;
            if (getrel(getnatp(land.lnd_own), player->cnum) != ALLIED)
                continue;
@@ -259,9 +256,22 @@ lload(void)
 
        if (!getsect(land.lnd_x, land.lnd_y, &sect))    /* XXX */
            continue;
+       if (!player->owner) {
+           if (land.lnd_own != player->cnum)
+               continue;
+           if (load_unload == LOAD) {
+               if (noisy)
+                   pr("Sector %s is not yours.\n",
+                      xyas(sect.sct_x, sect.sct_y, player->cnum));
+               continue;
+           }
+           if (getrel(getnatp(sect.sct_own), player->cnum) != ALLIED) {
+               pr("Sector %s is not yours.\n",
+                  xyas(sect.sct_x, sect.sct_y, player->cnum));
+               continue;
+           }
+       }
 
-       if (sect.sct_own != player->cnum && land.lnd_own != player->cnum)
-           continue;
        if (opt_MARKET) {
            if (ontradingblock(EF_LAND, &land)) {
                pr("You cannot load/unload an item on the trading block!\n");
@@ -269,18 +279,6 @@ lload(void)
            }
        }
 
-       if (sect.sct_own != player->cnum && load_unload == LOAD) {
-           if (noisy)
-               pr("Sector %s is not yours.\n",
-                  xyas(sect.sct_x, sect.sct_y, player->cnum));
-           continue;
-       }
-       if (sect.sct_own != player->cnum &&
-           getrel(getnatp(sect.sct_own), player->cnum) != ALLIED) {
-           pr("Sector %s is not yours.\n",
-              xyas(land.lnd_x, land.lnd_y, player->cnum));
-           continue;
-       }
        switch (type) {
        case EF_LAND:
            if (0 !=
@@ -321,12 +319,37 @@ lload(void)
     return RET_OK;
 }
 
+static int
+move_amount(int sect_amt, int unit_amt, int unit_max,
+           int load_unload, int amount)
+{
+    int move_amt;
+
+    if (amount < 0)
+       move_amt = -amount - unit_amt;
+    else
+       move_amt = load_unload == LOAD ? amount : -amount;
+    if (move_amt > unit_max - unit_amt)
+       move_amt = unit_max - unit_amt;
+    if (move_amt < -unit_amt)
+       move_amt = -unit_amt;
+    if (move_amt > sect_amt)
+       move_amt = sect_amt;
+    if (move_amt < sect_amt - ITEM_MAX)
+       move_amt = sect_amt - ITEM_MAX;
+    return move_amt;
+}
+
 static int
 load_comm_ok(struct sctstr *sectp, natid unit_own,
             i_type item, int move_amt)
 {
     if (!move_amt)
        return 0;
+    if (move_amt < 0 && !player->god && unit_own != player->cnum)
+       return 0;
+    if (move_amt > 0 && !player->god && sectp->sct_own != player->cnum)
+       return 0;
     if (sectp->sct_oldown != unit_own && item == I_CIVIL) {
        pr("%s civilians refuse to %s at %s!\n",
           (move_amt < 0 ? unit_own : sectp->sct_oldown) == player->cnum
@@ -405,7 +428,7 @@ load_plane_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
        noisy = isdigit(*p);
 
     while (nxtitem(&ni, &pln)) {
-       if (pln.pln_own != player->cnum)
+       if (!player->owner)
            continue;
        if (!(plchr[(int)pln.pln_type].pl_flags & P_L)
            && !(plchr[(int)pln.pln_type].pl_flags & P_E)
@@ -538,7 +561,7 @@ load_land_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
        noisy = isdigit(*p);
 
     while (nxtitem(&ni, &land)) {
-       if (land.lnd_own != player->cnum)
+       if (!player->owner)
            continue;
 
        if (load_unload == LOAD) {
@@ -630,7 +653,7 @@ load_land_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
            resupply_all(&land);
 #endif
            putland(land.lnd_uid, &land);
-           if (!has_supply(&land))
+           if (!lnd_in_supply(&land))
                pr("WARNING: %s is out of supply!\n", prland(&land));
        } else {
            sprintf(buf, "unloaded in your %s at %s",
@@ -660,8 +683,7 @@ load_comm_ship(struct sctstr *sectp, struct shpstr *sp,
 {
     i_type item = ich->i_uid;
     struct mchrstr *mcp = &mchr[(int)sp->shp_type];
-    int ship_amt, ship_max, sect_amt, move_amt;
-    int amount;
+    int ship_amt, sect_amt, move_amt;
     char prompt[512];
     char *p;
     char buf[1024];
@@ -678,21 +700,9 @@ load_comm_ship(struct sctstr *sectp, struct shpstr *sp,
        return RET_SYN;
 
     ship_amt = sp->shp_item[item];
-    ship_max = mcp->m_item[item];
     sect_amt = sectp->sct_item[item];
-    amount = atoi(p);
-    if (amount < 0)
-       move_amt = -amount - ship_amt;
-    else
-       move_amt = load_unload == LOAD ? amount : -amount;
-    if (move_amt > ship_max - ship_amt)
-       move_amt = ship_max - ship_amt;
-    if (move_amt < -ship_amt)
-       move_amt = -ship_amt;
-    if (move_amt > sect_amt)
-       move_amt = sect_amt;
-    if (move_amt < sect_amt - ITEM_MAX)
-       move_amt = sect_amt - ITEM_MAX;
+    move_amt = move_amount(sect_amt, ship_amt, mcp->m_item[item],
+                          load_unload, atoi(p));
     if (!load_comm_ok(sectp, sp->shp_own, item, move_amt))
        return RET_OK;
     if (!want_to_abandon(sectp, item, move_amt, 0))
@@ -701,6 +711,7 @@ load_comm_ship(struct sctstr *sectp, struct shpstr *sp,
        return RET_SYN;
     sectp->sct_item[item] = sect_amt - move_amt;
     sp->shp_item[item] = ship_amt + move_amt;
+
     if (move_amt >= 0) {
        pr("%d %s loaded onto %s at %s\n",
           move_amt, ich->i_name,
@@ -762,7 +773,7 @@ load_plane_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
        noisy = isdigit(*p);
 
     while (nxtitem(&ni, &pln)) {
-       if (pln.pln_own != player->cnum)
+       if (!player->owner)
            continue;
 
        if (!(plchr[(int)pln.pln_type].pl_flags & P_E)) {
@@ -833,8 +844,7 @@ load_comm_land(struct sctstr *sectp, struct lndstr *lp,
 {
     i_type item = ich->i_uid;
     struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
-    int land_amt, land_max, sect_amt, move_amt;
-    int amount;
+    int land_amt, sect_amt, move_amt;
     char prompt[512];
     char *p;
     char buf[1024];
@@ -851,34 +861,18 @@ load_comm_land(struct sctstr *sectp, struct lndstr *lp,
        return RET_SYN;
 
     land_amt = lp->lnd_item[item];
-    land_max = lcp->l_item[item];
     sect_amt = sectp->sct_item[item];
-    amount = atoi(p);
-    if (amount < 0)
-       move_amt = -amount - land_amt;
-    else
-       move_amt = load_unload == LOAD ? amount : -amount;
-    if (move_amt > land_max - land_amt)
-       move_amt = land_max - land_amt;
-    if (move_amt < -land_amt)
-       move_amt = -land_amt;
-    if (move_amt > sect_amt)
-       move_amt = sect_amt;
-    if (move_amt < sect_amt - ITEM_MAX)
-       move_amt = sect_amt - ITEM_MAX;
+    move_amt = move_amount(sect_amt, land_amt, lcp->l_item[item],
+                          load_unload, atoi(p));
     if (!load_comm_ok(sectp, lp->lnd_own, item, move_amt))
        return RET_OK;
-    if (sectp->sct_own != player->cnum && move_amt > 0) {
-       pr("Sector %s is not yours.\n",
-          xyas(lp->lnd_x, lp->lnd_y, player->cnum));
-       return RET_FAIL;
-    }
     sectp->sct_item[item] = sect_amt - move_amt;
     lp->lnd_item[item] = land_amt + move_amt;
 
     /* Did we put mils onto this unit? If so, reset the fortification */
     if (item == I_MILIT && move_amt > 0)
        lp->lnd_harden = 0;
+
     if (move_amt >= 0) {
        pr("%d %s loaded onto %s at %s\n",
           move_amt, ich->i_name,
@@ -939,8 +933,7 @@ load_land_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
        noisy = isdigit(*p);
 
     while (nxtitem(&ni, &land)) {
-
-       if (land.lnd_own != player->cnum)
+       if (!player->owner)
            continue;
 
        if (load_unload == LOAD) {
@@ -1006,7 +999,7 @@ load_land_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
            resupply_all(&land);
 #endif
            putland(land.lnd_uid, &land);
-           if (!has_supply(&land))
+           if (!lnd_in_supply(&land))
                pr("WARNING: %s is out of supply!\n", prland(&land));
        } else {
            sprintf(buf, "unloaded in your %s at %s",