]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/land.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / update / land.c
index 6805a32df9456168fd81af7354c7d2e5ef59bef7..64af50b7acef1b8ddba84c553e9e00fd9e9256bc 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-2006, 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, 1996
  */
 
+#include <config.h>
+
 #include "misc.h"
-#include "var.h"
+#include "plague.h"
 #include "sect.h"
 #include "nat.h"
 #include "land.h"
 #include "ship.h"
-#include "var.h"
 #include "news.h"
 #include "file.h"
-#include "product.h"
 #include "optlist.h"
 #include "budg.h"
 #include "player.h"
 
 int mil_dbl_pay;
 
-#ifndef MIN
-#define MIN(x,y)       ((x) > (y) ? (y) : (x))
-#endif
-
-static int landrepair(register struct lndstr *, int *, struct natstr *,
+static int landrepair(struct lndstr *, struct natstr *,
                      int *, int);
-static void upd_land(register struct lndstr *lp, int landno, register int etus,
+static void upd_land(struct lndstr *lp, int etus,
                     struct natstr *np, int *bp, int build);
 
 int
 prod_land(int etus, int natnum, int *bp, int build)
                /* build = 1, maintain = 0 */
 {
-    register struct lndstr *lp;
+    struct lndstr *lp;
     struct sctstr *sp;
     struct natstr *np;
     int n, k = 0;
-    extern long lnd_money[MAXNOC];
     int start_money;
     int lastx = 9999, lasty = 9999;
 
@@ -98,9 +93,9 @@ prod_land(int etus, int natnum, int *bp, int build)
        }
        np = getnatp(lp->lnd_own);
        start_money = np->nat_money;
-       upd_land(lp, n, etus, np, bp, build);
+       upd_land(lp, etus, np, bp, build);
        lnd_money[lp->lnd_own] += np->nat_money - start_money;
-       if ((build && (np->nat_money != start_money)) || (!build))
+       if (!build || np->nat_money != start_money)
            k++;
        if (player->simulation)
            np->nat_money = start_money;
@@ -112,19 +107,15 @@ prod_land(int etus, int natnum, int *bp, int build)
 }
 
 static void
-upd_land(register struct lndstr *lp, int landno, register int etus,
+upd_land(struct lndstr *lp, int etus,
         struct natstr *np, int *bp, int build)
               /* build = 1, maintain = 0 */
 {
-    extern int morale_base;
     struct lchrstr *lcp;
-    int vec[I_MAX + 1];
-    int cvec[I_MAX + 1];
+    int pstage, ptime;
     int n;
-    double techfact(int, double);
     int min = morale_base - (int)np->nat_level[NAT_HLEV];
     int mult;
-    extern double money_land;
     int needed;
     int cost;
     int eff;
@@ -134,12 +125,10 @@ upd_land(register struct lndstr *lp, int landno, register int etus,
            lp->lnd_retreat = min;
 
     lcp = &lchr[(int)lp->lnd_type];
-    getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
     if (build == 1) {
        if (np->nat_priorities[PRI_LBUILD] == 0 || np->nat_money < 0)
            return;
-       if (lp->lnd_effic < LAND_MINEFF ||
-           !(landrepair(lp, vec, np, bp, etus))) {
+       if (lp->lnd_effic < LAND_MINEFF || !landrepair(lp, np, bp, etus)) {
            makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x,
                     lp->lnd_y);
            lp->lnd_own = 0;
@@ -151,10 +140,10 @@ upd_land(register struct lndstr *lp, int landno, register int etus,
            mult = 2;
        if (lcp->l_flags & L_ENGINEER)
            mult *= 3;
-/*             cost = -(mult * etus * dmin(0.0, money_land * LND_COST(lcp->l_cost, lp->lnd_tech - lcp->l_tech)));*/
-       cost = -(mult * etus * dmin(0.0, money_land * lcp->l_cost));
-       if ((np->nat_priorities[PRI_LMAINT] == 0 ||
-            np->nat_money < cost) && !player->simulation) {
+/*             cost = -(mult * etus * MIN(0.0, money_land * LND_COST(lcp->l_cost, lp->lnd_tech - lcp->l_tech)));*/
+       cost = -(mult * etus * MIN(0.0, money_land * lcp->l_cost));
+       if ((np->nat_priorities[PRI_LMAINT] == 0 || np->nat_money < cost)
+           && !player->simulation) {
            if ((eff = lp->lnd_effic - etus / 5) < LAND_MINEFF) {
                wu(0, lp->lnd_own,
                   "%s lost to lack of maintenance\n", prland(lp));
@@ -170,16 +159,13 @@ upd_land(register struct lndstr *lp, int landno, register int etus,
        } else {
            np->nat_money -= cost;
        }
-/* Mil costs are now part of regular mil costs, not maint costs */
-/*             np->nat_money += (int) (money_mil * etus * lnd_getmil(lp));*/
 
        /* Grab more stuff */
        if ((opt_NOFOOD == 0) && !player->simulation)
            resupply_commod(lp, I_FOOD);
 
-       getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
        if (!player->simulation) {
-           if ((n = feed_land(lp, vec, etus, &needed, 1)) > 0) {
+           if ((n = feed_land(lp, etus, &needed, 1)) > 0) {
                wu(0, lp->lnd_own, "%d starved in %s%s\n",
                   n, prland(lp),
                   (lp->lnd_effic < LAND_MINEFF ? ", killing it" : ""));
@@ -190,9 +176,10 @@ upd_land(register struct lndstr *lp, int landno, register int etus,
             * do plague stuff.  plague can't break out on land units,
             * but it can still kill people on them.
             */
-           getvec(VT_COND, cvec, (s_char *)lp, EF_LAND);
-           if (cvec[C_PSTAGE] > 0) {
-               n = plague_people(np, vec, cvec, etus);
+           pstage = lp->lnd_pstage;
+           ptime = lp->lnd_ptime;
+           if (pstage != PLG_HEALTHY) {
+               n = plague_people(np, lp->lnd_item, &pstage, &ptime, etus);
                switch (n) {
                case PLG_DYING:
                    wu(0, lp->lnd_own,
@@ -204,9 +191,9 @@ upd_land(register struct lndstr *lp, int landno, register int etus,
                    break;
                case PLG_INCUBATE:
                    /* Are we still incubating? */
-                   if (n == cvec[C_PSTAGE]) {
+                   if (n == pstage) {
                        /* Yes. Will it turn "infectious" next time? */
-                       if (cvec[C_PTIME] <= etus) {
+                       if (ptime <= etus) {
                            /* Yes.  Report an outbreak. */
                            wu(0, lp->lnd_own,
                               "Outbreak of PLAGUE on %s!\n", prland(lp));
@@ -220,9 +207,9 @@ upd_land(register struct lndstr *lp, int landno, register int etus,
                    break;
                case PLG_EXPOSED:
                    /* Has the plague moved to "incubation" yet? */
-                   if (n != cvec[C_PSTAGE]) {
+                   if (n != pstage) {
                        /* Yes. Will it turn "infectious" next time? */
-                       if (cvec[C_PTIME] <= etus) {
+                       if (ptime <= etus) {
                            /* Yes.  Report an outbreak. */
                            wu(0, lp->lnd_own,
                               "Outbreak of PLAGUE on %s!\n", prland(lp));
@@ -233,21 +220,19 @@ upd_land(register struct lndstr *lp, int landno, register int etus,
                default:
                    break;
                }
-               putvec(VT_COND, cvec, (s_char *)lp, EF_LAND);
+               lp->lnd_pstage = pstage;
+               lp->lnd_ptime = ptime;
            }
-           putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
        }                       /* end !player->simulation */
     }
 }
 
 /*ARGSUSED*/
 static int
-landrepair(register struct lndstr *land, int *vec, struct natstr *np,
+landrepair(struct lndstr *land, struct natstr *np,
           int *bp, int etus)
 {
-    extern int mil_dbl_pay;
-    extern int land_grow_scale;
-    register int delta;
+    int delta;
     struct sctstr *sp;
     struct lchrstr *lp;
     float leftp, buildp;
@@ -256,14 +241,12 @@ landrepair(register struct lndstr *land, int *vec, struct natstr *np,
     int avail;
     int w_p_eff;
     int mult;
-    int svec[I_MAX + 1];
     int mvec[I_MAX + 1];
 
     lp = &lchr[(int)land->lnd_type];
     sp = getsectp(land->lnd_x, land->lnd_y);
     if (sp->sct_off)
        return 1;
-    getvec(VT_ITEM, svec, (s_char *)sp, EF_SECTOR);
     mult = 1;
     if (np->nat_level[NAT_TLEV] < land->lnd_tech * 0.85)
        mult = 2;
@@ -280,12 +263,12 @@ landrepair(register struct lndstr *land, int *vec, struct natstr *np,
     else
        avail = gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
 
-    w_p_eff = 20 + (lp->l_lcm + 2 * lp->l_hcm);
+    w_p_eff = LND_BLD_WORK(lp->l_lcm, lp->l_hcm);
     delta = roundavg((double)avail / w_p_eff);
     if (delta <= 0)
        return 1;
-    if (delta > etus * land_grow_scale)
-       delta = etus * land_grow_scale;
+    if (delta > (int)((float)etus * land_grow_scale))
+       delta = (int)((float)etus * land_grow_scale);
 
     /* delta is the max amount we can grow */
 
@@ -295,7 +278,7 @@ landrepair(register struct lndstr *land, int *vec, struct natstr *np,
 
     leftp = ((float)left / 100.0);
 
-    bzero((s_char *)mvec, sizeof(mvec));
+    memset(mvec, 0, sizeof(mvec));
     mvec[I_LCM] = lcm_needed = ldround((double)(lp->l_lcm * leftp), 1);
     mvec[I_HCM] = hcm_needed = ldround((double)(lp->l_hcm * leftp), 1);
 /*
@@ -324,7 +307,7 @@ landrepair(register struct lndstr *land, int *vec, struct natstr *np,
 
     build = ldround((double)(buildp * 100.0), 1);
 
-    bzero((s_char *)mvec, sizeof(mvec));
+    memset(mvec, 0, sizeof(mvec));
     mvec[I_LCM] = lcm_needed = roundavg((double)(lp->l_lcm * buildp));
     mvec[I_HCM] = hcm_needed = roundavg((double)(lp->l_hcm * buildp));
 /*
@@ -368,72 +351,59 @@ landrepair(register struct lndstr *land, int *vec, struct natstr *np,
  * returns the number who starved, if any.
  */
 int
-feed_land(struct lndstr *lp, register int *vec, int etus, int *needed,
-         int doit)
+feed_land(struct lndstr *lp, int etus, int *needed, int doit)
 {
-    extern double eatrate;
     double food_eaten, ship_eaten;
+    int ifood_eaten;
     double people_left;
-    int can_eat, need, svec[I_MAX + 1];
+    int need;
     int total_people;
     int starved;
-    struct lchrstr *lcp;
     struct shpstr *sp;
 
     if (opt_NOFOOD)
        return 0;               /* no food no work to be done */
 
-    lcp = &lchr[(int)lp->lnd_type];
-
-    food_eaten = (etus * eatrate) * total_mil(lp);
+    total_people = total_mil(lp);
+    food_eaten = etus * eatrate * total_people;
+    ifood_eaten = (int)food_eaten;
+    if (food_eaten - ifood_eaten > 0)
+       ifood_eaten++;
     starved = 0;
     *needed = 0;
+
     /*
      * If we're on a ship, and we don't have enough food,
      * get some food off the carrying ship. (Don't starve
      * the ship, tho...
      */
 /* doit - Only try to take food off the ship during the update */
-    if ((food_eaten > vec[I_FOOD]) && (lp->lnd_ship >= 0) && doit) {
-       need = (int)food_eaten - vec[I_FOOD];
+    if (ifood_eaten > lp->lnd_item[I_FOOD] && lp->lnd_ship >= 0 && doit) {
+       need = ifood_eaten - lp->lnd_item[I_FOOD];
        sp = getshipp(lp->lnd_ship);
-       getvec(VT_ITEM, svec, (s_char *)sp, EF_SHIP);
-       ship_eaten = (etus * eatrate) *
-           (svec[I_CIVIL] + svec[I_MILIT] + svec[I_UW]);
-       if ((svec[I_FOOD] - need) > ship_eaten) {
-           vec[I_FOOD] += need;
-           svec[I_FOOD] -= need;
-       } else if ((svec[I_FOOD] - ship_eaten) > 0) {
-           vec[I_FOOD] += (svec[I_FOOD] - ship_eaten);
-           svec[I_FOOD] -= (svec[I_FOOD] - ship_eaten);
+       ship_eaten = etus * eatrate * (sp->shp_item[I_CIVIL]
+                                      + sp->shp_item[I_MILIT]
+                                      + sp->shp_item[I_UW]);
+       if (sp->shp_item[I_FOOD] - need > ship_eaten) {
+           lp->lnd_item[I_FOOD] += need;
+           sp->shp_item[I_FOOD] -= need;
+       } else if (sp->shp_item[I_FOOD] - ship_eaten > 0) {
+           lp->lnd_item[I_FOOD] += sp->shp_item[I_FOOD] - ship_eaten;
+           sp->shp_item[I_FOOD] -= sp->shp_item[I_FOOD] - ship_eaten;
        }
-       putvec(VT_ITEM, svec, (s_char *)sp, EF_SHIP);
     }
 
-    if (food_eaten > vec[I_FOOD]) {
-       *needed = food_eaten - vec[I_FOOD];
-       if (*needed < (food_eaten - vec[I_FOOD]))
-           (*needed)++;
-       can_eat = (vec[I_FOOD] / (etus * eatrate));
-       total_people = total_mil(lp);
-       /* only want to starve off at most 1/2 the populace. */
-       if (can_eat < (total_people / 2))
-           can_eat = total_people / 2;
-
-       people_left = (vec[I_FOOD] + 0.01) / (food_eaten + 0.01);
+    if (ifood_eaten > lp->lnd_item[I_FOOD]) {
+       *needed = ifood_eaten - lp->lnd_item[I_FOOD];
+       people_left = (lp->lnd_item[I_FOOD] + 0.01) / (food_eaten + 0.01);
        /* only want to starve off at most 1/2 the populace. */
        if (people_left < 0.5)
            people_left = 0.5;
-/*             lp->lnd_effic *= people_left;*/
-       starved = vec[I_MILIT] - (vec[I_MILIT] * people_left);
-/*             if (!player->simulation)
-                       wu(0, lp->lnd_own, "%d mil starved on unit %s.\n",
-                          starved,
-                          prland(lp));*/
-       vec[I_MILIT] -= starved;
-       vec[I_FOOD] = 0;
+       starved = total_people * (1 - people_left);
+       lp->lnd_item[I_MILIT] -= starved;
+       lp->lnd_item[I_FOOD] = 0;
     } else {
-       vec[I_FOOD] -= (int)food_eaten;
+       lp->lnd_item[I_FOOD] -= (int)food_eaten;
     }
     return starved;
 }