]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/move.c
Update copyright notice.
[empserver] / src / lib / commands / move.c
index 679bcba49d177e19694b287cf29c60495d09bd44..4fabbe9b76db7be6604fe232feba8aef3a917d3b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2004, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -61,11 +61,9 @@ move(void)
     int left;
     int mcost, dam;
     int infected;
-    int stype;
     int vtype;
     int amt_src;
     int amt_dst;
-    struct dchrstr *dp;
     struct ichrstr *ip;
     int work;
     int loyal;
@@ -94,25 +92,14 @@ move(void)
      * military control necessary to move
      * goodies in occupied territory.
      */
-    if (!istest && sect.sct_oldown != player->cnum && vtype != V_MILIT) {
-       int tot_mil = 0;
-       struct nstr_item ni;
-       struct lndstr land;
-       snxtitem_xy(&ni, EF_LAND, sect.sct_x, sect.sct_y);
-       while (nxtitem(&ni, (s_char *)&land)) {
-           if (land.lnd_own == player->cnum)
-               tot_mil += total_mil(&land);
-       }
-       if ((getvar(V_MILIT, (s_char *)&sect, EF_SECTOR) + tot_mil) * 10
-           < getvar(V_CIVIL, (s_char *)&sect, EF_SECTOR)) {
+    if (!istest && sect.sct_oldown != player->cnum && vtype != I_MILIT) {
+       if (!military_control(&sect)) {
            pr("Military control required to move goods.\n");
            return RET_FAIL;
        }
     }
-    stype = sect.sct_type;
-    dp = &dchr[stype];
-    infected = getvar(V_PSTAGE, (s_char *)&sect, EF_SECTOR) == PLG_INFECT;
-    amt_src = getvar(vtype, (s_char *)&sect, EF_SECTOR);
+    infected = sect.sct_pstage == PLG_INFECT;
+    amt_src = sect.sct_item[vtype];
     if (!istest && amt_src <= 0) {
        pr("No %s in %s\n", ip->i_name,
           xyas(sect.sct_x, sect.sct_y, player->cnum));
@@ -120,7 +107,7 @@ move(void)
     }
     own = sect.sct_own;
     mob = (int)sect.sct_mobil;
-    if (!istest && vtype == V_CIVIL && sect.sct_oldown != own) {
+    if (!istest && vtype == I_CIVIL && sect.sct_oldown != own) {
        pr("You can't move conquered populace!\n");
        return RET_FAIL;
     }
@@ -129,15 +116,13 @@ move(void)
           xyas(sect.sct_x, sect.sct_y, player->cnum));
        return RET_SYN;
     }
-    if (vtype == V_CIVIL) {
-       work = sect.sct_work;
-       if (work != 100)
-           pr("Warning: civil unrest\n");
-       loyal = sect.sct_loyal;
-    } else if (vtype == V_MILIT) {
-       work = 100;
-       loyal = 0;
-    }
+
+    /* only used when moving civs; but prevent spurious compiler warnings */
+    work = sect.sct_work;
+    loyal = sect.sct_loyal;
+    if (vtype == I_CIVIL && work != 100)
+       pr("Warning: civil unrest\n");
+
     if (istest)
        sprintf(prompt, "Number of %s to test move? ", ip->i_name);
     else
@@ -156,7 +141,7 @@ move(void)
        }
     }
 
-    if (!want_to_abandon(&sect, vtype, amount, 0)) {
+    if (!istest && !want_to_abandon(&sect, vtype, amount, 0)) {
        pr("Move cancelled.\n");
        return RET_FAIL;
     }
@@ -166,10 +151,8 @@ move(void)
 
     if (amount <= 0)
        return RET_SYN;
-    packing = ip->i_pkg[dp->d_pkg];
-    if (packing > 1 && sect.sct_effic < 60)
-       packing = 1;
-    weight = (double)amount *ip->i_lbs / packing;
+    packing = sect.sct_effic >= 60 ? dchr[sect.sct_type].d_pkg : IPKG;
+    weight = (double)amount * ip->i_lbs / ip->i_pkg[packing];
     /*
      * First remove commodities from source sector
      */
@@ -179,7 +162,7 @@ move(void)
            pr("Somebody has captured that sector!\n");
            return RET_FAIL;
        }
-       amt_src = getvar(vtype, (s_char *)&start, EF_SECTOR);
+       amt_src = start.sct_item[vtype];
        if (amt_src < amount) {
            pr("Only %d %s left in %s!\n", amt_src,
               ip->i_name, xyas(start.sct_x, start.sct_y, player->cnum));
@@ -188,7 +171,7 @@ move(void)
        } else
            amt_src -= amount;
 
-       putvar(vtype, amt_src, (s_char *)&start, EF_SECTOR);
+       start.sct_item[vtype] = amt_src;
        start.sct_flags |= MOVE_IN_PROGRESS;
        putsect(&start);
     }
@@ -229,7 +212,7 @@ move(void)
        left = mob;
     } else if (!istest) {
        /*
-          * decrement mobility appropriately.
+        * decrement mobility appropriately.
         */
        getsect(x, y, &start);
        mob = start.sct_mobil;
@@ -252,16 +235,17 @@ move(void)
            pr("Somebody has captured that sector!\n");
        getsect(x, y, &sect);
     }
-    if (vtype == V_CIVIL && getvar(V_CIVIL, (s_char *)&sect, EF_SECTOR) &&
-       sect.sct_oldown != player->cnum) {
+    if (vtype == I_CIVIL && sect.sct_item[I_CIVIL]
+       && sect.sct_oldown != player->cnum) {
        pr("Your civilians don't want to stay!\n");
        getsect(x, y, &sect);
     }
 
-    amt_dst = getvar(vtype, (s_char *)&sect, EF_SECTOR);
-    if (32767 - amt_dst < amount) {
+    amt_dst = sect.sct_item[vtype];
+    if (amount > ITEM_MAX - amt_dst) {
        pr("Only enough room for %d in %s.  The goods will be returned.\n",
-          32767 - amt_dst, xyas(sect.sct_x, sect.sct_y, player->cnum));
+          ITEM_MAX - amt_dst, xyas(sect.sct_x, sect.sct_y, player->cnum));
+       /* FIXME Not nice.  Move what we can and return the rest.  */
        getsect(x, y, &sect);
     }
 
@@ -269,6 +253,13 @@ move(void)
        pr("%d mob left in %s\n", left,
           xyas(start.sct_x, start.sct_y, player->cnum));
 
+    if (amount <= 0) {
+       getsect(x, y, &start);
+       start.sct_flags &= ~MOVE_IN_PROGRESS;
+       putsect(&start);
+       return RET_OK;
+    }
+
 /* If the sector that things are going to is no longer
    owned by the player, and was the starting sector,
    try to find somewhere to dump the stuff.  If nowhere
@@ -281,8 +272,8 @@ move(void)
            getsect(x + diroff[n][0], y + diroff[n][1], &tsct);
            if (tsct.sct_own != player->cnum)
                continue;
-           amt_dst = getvar(vtype, (s_char *)&tsct, EF_SECTOR);
-           if (32767 - amt_dst < amount)
+           amt_dst = tsct.sct_item[vtype];
+           if (amount > ITEM_MAX - amt_dst)
                continue;
            n = -1;
            break;
@@ -308,40 +299,26 @@ move(void)
        getsect(tsct.sct_x, tsct.sct_y, &sect);
     }
 
-    amt_dst = getvar(vtype, (s_char *)&sect, EF_SECTOR);
-    if (32767 - amt_dst < amount) {
-       amount = 32767 - amt_dst;
+    amt_dst = sect.sct_item[vtype];
+    if (amount > ITEM_MAX - amt_dst) {
+       amount = ITEM_MAX - amt_dst;
        pr("Only room for %d, the rest were lost.\n", amount);
     }
     if (istest)
        return RET_OK;
-    if (putvar(vtype, amount + amt_dst, (s_char *)&sect, EF_SECTOR) < 0) {
-       pr("No more room in %s.  The goods were lost.\n",
-          xyas(sect.sct_x, sect.sct_y, player->cnum));
-       /* charge the player mobility anyway */
-       amount = 0;
-    }
+    sect.sct_item[vtype] = amount + amt_dst;
     /*
      * Now add commodities to destination sector,
      * along with plague that came along for the ride.
      * Takeover unowned sectors if not deity.
      */
-    if (infected && getvar(V_PSTAGE, (s_char *)&sect, EF_SECTOR) == 0)
-       putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)&sect, EF_SECTOR);
-    if (vtype == V_CIVIL) {
-       if (opt_NEW_WORK) {
-           sect.sct_loyal = ((amt_dst * sect.sct_loyal) +
-                             (amount * loyal)) / (amt_dst + amount);
-           sect.sct_work = ((amt_dst * sect.sct_work) +
-                            (amount * work)) / (amt_dst + amount);
-       } else {                /* ! NEW_WORK */
-
-           /* It only takes one bad apple... */
-           if (sect.sct_loyal < loyal)
-               sect.sct_loyal = loyal;
-           if (sect.sct_work > work)
-               sect.sct_work = work;
-       }                       /* end NEW_WORK */
+    if (infected && sect.sct_pstage == PLG_HEALTHY)
+       sect.sct_pstage = PLG_EXPOSED;
+    if (vtype == I_CIVIL) {
+       sect.sct_loyal
+           = (amt_dst * sect.sct_loyal + amount * loyal) / (amt_dst + amount);
+       sect.sct_work
+           = (amt_dst * sect.sct_work + amount * work) / (amt_dst + amount);
     }
     putsect(&sect);
     getsect(x, y, &start);
@@ -395,16 +372,16 @@ would_abandon(struct sctstr *sp, int vtype, int amnt, struct lndstr *lp)
 {
     int mil, civs, loyalcivs;
 
-    if ((vtype != V_CIVIL) && (vtype != V_MILIT))
+    if ((vtype != I_CIVIL) && (vtype != I_MILIT))
        return 0;
 
-    mil = getvar(V_MILIT, (s_char *)sp, EF_SECTOR);
-    civs = getvar(V_CIVIL, (s_char *)sp, EF_SECTOR);
+    mil = sp->sct_item[I_MILIT];
+    civs = sp->sct_item[I_CIVIL];
 
-    if (vtype == V_MILIT)
+    if (vtype == I_MILIT)
        mil -= amnt;
 
-    if (vtype == V_CIVIL)
+    if (vtype == I_CIVIL)
        civs -= amnt;
 
     if (sp->sct_own == sp->sct_oldown)
@@ -413,10 +390,9 @@ would_abandon(struct sctstr *sp, int vtype, int amnt, struct lndstr *lp)
        loyalcivs = 0;
 
     /* If they have a military unit there, they still own it */
-    if (sp->sct_own != 0 && ((loyalcivs == 0) && (mil == 0) &&
-                            (has_units
-                             (sp->sct_x, sp->sct_y, sp->sct_own,
-                              lp) == 0)))
+    if (sp->sct_own != 0
+       && ((loyalcivs == 0) && (mil == 0)
+           && (has_units(sp->sct_x, sp->sct_y, sp->sct_own, lp) == 0)))
        return 1;
 
     return 0;