]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/land.c
Fix trailing whitespace
[empserver] / src / lib / update / land.c
index fd8b869899e5907cf669f044668f366fd193383a..5c0fb5cf0fffbb5c50dcbef293028e1dd26ff804 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
  *
  *  ---
  *
- *  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.
  *
  *  ---
  *
  *  land.c: Do production for land units
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     Thomas Ruschak, 1992
  *     Steve McClure, 1996
+ *     Markus Armbruster, 2006-2008
  */
 
 #include <config.h>
 
-#include "misc.h"
-#include "plague.h"
-#include "sect.h"
-#include "nat.h"
+#include <math.h>
+#include "budg.h"
 #include "land.h"
-#include "ship.h"
+#include "lost.h"
 #include "news.h"
-#include "file.h"
-#include "optlist.h"
-#include "budg.h"
+#include "plague.h"
 #include "player.h"
+#include "ship.h"
 #include "update.h"
-#include "lost.h"
-#include "common.h"
-#include "subs.h"
-#include "common.h"
-#include "gen.h"
 
-int mil_dbl_pay;
-
-static int landrepair(struct lndstr *, struct natstr *,
-                     int *, int);
-static void upd_land(struct lndstr *lp, int etus,
-                    struct natstr *np, int *bp, int build);
+static void landrepair(struct lndstr *, struct natstr *, struct bp *, int);
+static void upd_land(struct lndstr *, int, struct natstr *, struct bp *, int);
+static int feed_land(struct lndstr *, int);
 
 int
-prod_land(int etus, int natnum, int *bp, int build)
+prod_land(int etus, int natnum, struct bp *bp, int build)
                /* build = 1, maintain = 0 */
 {
     struct lndstr *lp;
@@ -77,6 +67,12 @@ prod_land(int etus, int natnum, int *bp, int build)
            continue;
        if (lp->lnd_own != natnum)
            continue;
+       if (lp->lnd_effic < LAND_MINEFF) {
+           makelost(EF_LAND, lp->lnd_own, lp->lnd_uid,
+                    lp->lnd_x, lp->lnd_y);
+           lp->lnd_own = 0;
+           continue;
+       }
 
        sp = getsectp(lp->lnd_x, lp->lnd_y);
        if (sp->sct_type == SCT_SANCT)
@@ -108,7 +104,7 @@ prod_land(int etus, int natnum, int *bp, int build)
 
 static void
 upd_land(struct lndstr *lp, int etus,
-        struct natstr *np, int *bp, int build)
+        struct natstr *np, struct bp *bp, int build)
               /* build = 1, maintain = 0 */
 {
     struct lchrstr *lcp;
@@ -116,7 +112,6 @@ upd_land(struct lndstr *lp, int etus,
     int n;
     int min = morale_base - (int)np->nat_level[NAT_HLEV];
     int mult;
-    int needed;
     int cost;
     int eff;
 
@@ -126,29 +121,23 @@ upd_land(struct lndstr *lp, int etus,
 
     lcp = &lchr[(int)lp->lnd_type];
     if (build == 1) {
-       if (np->nat_priorities[PRI_LBUILD] == 0 || np->nat_money < 0)
-           return;
-       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;
-           return;
-       }
+       if (!lp->lnd_off && np->nat_money >= 0)
+           landrepair(lp, np, bp, etus);
+       if (!player->simulation)
+           lp->lnd_off = 0;
     } else {
        mult = 1;
        if (np->nat_level[NAT_TLEV] < lp->lnd_tech * 0.85)
            mult = 2;
        if (lcp->l_flags & L_ENGINEER)
            mult *= 3;
-/*             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 (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));
-               makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x,
-                        lp->lnd_y);
+               makelost(EF_LAND, lp->lnd_own, lp->lnd_uid,
+                        lp->lnd_x, lp->lnd_y);
                lp->lnd_own = 0;
                return;
            }
@@ -160,12 +149,9 @@ upd_land(struct lndstr *lp, int etus,
            np->nat_money -= cost;
        }
 
-       /* Grab more stuff */
-       if ((opt_NOFOOD == 0) && !player->simulation)
-           resupply_commod(lp, I_FOOD);
-
        if (!player->simulation) {
-           if ((n = feed_land(lp, etus, &needed, 1)) > 0) {
+           /* feed */
+           if ((n = feed_land(lp, etus)) > 0) {
                wu(0, lp->lnd_own, "%d starved in %s%s\n",
                   n, prland(lp),
                   (lp->lnd_effic < LAND_MINEFF ? ", killing it" : ""));
@@ -227,17 +213,13 @@ upd_land(struct lndstr *lp, int etus,
     }
 }
 
-/*ARGSUSED*/
-static int
-landrepair(struct lndstr *land, struct natstr *np,
-          int *bp, int etus)
+static void
+landrepair(struct lndstr *land, struct natstr *np, struct bp *bp, int etus)
 {
     int delta;
     struct sctstr *sp;
     struct lchrstr *lp;
-    float leftp, buildp;
-    int left, build;
-    int mil_needed, lcm_needed, hcm_needed, gun_needed, shell_needed;
+    int build;
     int avail;
     int w_p_eff;
     int mult;
@@ -246,164 +228,73 @@ landrepair(struct lndstr *land, struct natstr *np,
     lp = &lchr[(int)land->lnd_type];
     sp = getsectp(land->lnd_x, land->lnd_y);
     if (sp->sct_off)
-       return 1;
+       return;
     mult = 1;
     if (np->nat_level[NAT_TLEV] < land->lnd_tech * 0.85)
        mult = 2;
 
     if (land->lnd_effic == 100) {
        /* land is ok; no repairs needed */
-       return 1;
+       return;
     }
-    if (sp->sct_own != land->lnd_own)
-       return 1;
+    if ((sp->sct_own != land->lnd_own) &&
+       (getrel(getnatp(sp->sct_own), land->lnd_own) != ALLIED))
+       return;
 
     if (!player->simulation)
        avail = sp->sct_avail * 100;
     else
-       avail = gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
+       avail = bp_get_avail(bp, sp) * 100;
 
     w_p_eff = LND_BLD_WORK(lp->l_lcm, lp->l_hcm);
     delta = roundavg((double)avail / w_p_eff);
     if (delta <= 0)
-       return 1;
+       return;
     if (delta > (int)((float)etus * land_grow_scale))
        delta = (int)((float)etus * land_grow_scale);
-
-    /* delta is the max amount we can grow */
-
-    left = 100 - land->lnd_effic;
-    if (left > delta)
-       left = delta;
-
-    leftp = ((float)left / 100.0);
-
-    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);
-/*
-        mvec[I_GUN] = gun_needed = ldround((double)(lp->l_gun * leftp),1);
-        mvec[I_MILIT] = mil_needed = ldround((double)(lp->l_mil * leftp),1);
-        mvec[I_SHELL] = shell_needed = ldround((double)(lp->l_shell *leftp),1);
- */
-    mvec[I_GUN] = gun_needed = 0;
-    mvec[I_MILIT] = mil_needed = 0;
-    mvec[I_SHELL] = shell_needed = 0;
-
-    get_materials(sp, bp, mvec, 0);
-
-    if (mvec[I_MILIT] >= mil_needed)
-       buildp = leftp;
-    else
-       buildp = ((float)mvec[I_MILIT] / (float)lp->l_mil);
-    if (mvec[I_LCM] < lcm_needed)
-       buildp = MIN(buildp, ((float)mvec[I_LCM] / (float)lp->l_lcm));
-    if (mvec[I_HCM] < hcm_needed)
-       buildp = MIN(buildp, ((float)mvec[I_HCM] / (float)lp->l_hcm));
-    if (mvec[I_GUN] < gun_needed)
-       buildp = MIN(buildp, ((float)mvec[I_GUN] / (float)lp->l_gun));
-    if (mvec[I_SHELL] < shell_needed)
-       buildp = MIN(buildp, ((float)mvec[I_SHELL] / (float)lp->l_shell));
-
-    build = ldround((double)(buildp * 100.0), 1);
+    if (delta > 100 - land->lnd_effic)
+       delta = 100 - land->lnd_effic;
 
     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));
-/*
-        mvec[I_GUN] = gun_needed = roundavg((double)(lp->l_gun * buildp));
-        mvec[I_MILIT] = mil_needed = roundavg((double)(lp->l_mil * buildp));
-        mvec[I_SHELL] = shell_needed = roundavg((double)(lp->l_shell *buildp));
- */
-    mvec[I_GUN] = gun_needed = 0;
-    mvec[I_MILIT] = mil_needed = 0;
-    mvec[I_SHELL] = shell_needed = 0;
-    mil_dbl_pay += mil_needed;
-
-    get_materials(sp, bp, mvec, 1);
+    mvec[I_LCM] = lp->l_lcm;
+    mvec[I_HCM] = lp->l_hcm;
+    build = get_materials(sp, bp, mvec, delta);
 
     if ((sp->sct_type != SCT_HEADQ) && (sp->sct_type != SCT_FORTR))
        build /= 3;
 
     avail -= build * w_p_eff;
-    if (!player->simulation) {
+    if (avail < 0)
+       avail = 0;
+    if (!player->simulation)
        sp->sct_avail = avail / 100;
-       if (sp->sct_avail < 0)
-           sp->sct_avail = 0;
-    } else {
-       pt_bg_nmbr(bp, sp, I_MAX + 1, avail / 100);
-       if (gt_bg_nmbr(bp, sp, I_MAX + 1) < 0)
-           pt_bg_nmbr(bp, sp, I_MAX + 1, 0);
-    }
+    else
+       bp_put_avail(bp, sp, avail / 100);
 
     if (build < 0)
        logerror("land unit %d building %d ! \n", land->lnd_uid, build);
     np->nat_money -= mult * lp->l_cost * build / 100.0;
     if (!player->simulation) {
-       land->lnd_effic += (s_char)build;
-
-       putsect(sp);
+       land->lnd_effic += (signed char)build;
     }
-    return 1;
 }
 
 /*
  * returns the number who starved, if any.
  */
-int
-feed_land(struct lndstr *lp, int etus, int *needed, int doit)
+static int
+feed_land(struct lndstr *lp, int etus)
 {
-    double food_eaten, ship_eaten;
-    int ifood_eaten;
-    double people_left;
-    int need;
-    int total_people;
-    int starved;
-    struct shpstr *sp;
+    int needed;
 
     if (opt_NOFOOD)
-       return 0;               /* no food no work to be done */
+       return 0;
 
-    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;
+    needed = (int)ceil(food_needed(lp->lnd_item, etus));
 
-    /*
-     * 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 (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);
-       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;
-       }
-    }
+    /* scrounge */
+    if (needed > lp->lnd_item[I_FOOD])
+       resupply_commod(lp, I_FOOD);
 
-    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;
-       starved = total_people * (1 - people_left);
-       lp->lnd_item[I_MILIT] -= starved;
-       lp->lnd_item[I_FOOD] = 0;
-    } else {
-       lp->lnd_item[I_FOOD] -= (int)food_eaten;
-    }
-    return starved;
+    return feed_people(lp->lnd_item, etus);
 }