]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/ship.c
Remove budget priorities:
[empserver] / src / lib / update / ship.c
index d4c7628a12961431044cf1c3db43e82d83909861..8bfe670f878d2c2b32575ce939bd3e1006aba611 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.
  *
  *  ---
  *
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     Steve McClure, 1996
+ *     Ron Koenderink, 2004
+ *     Markus Armbruster, 2006
  */
 
+#include <config.h>
+
+#include <math.h>
 #include "misc.h"
-#include "var.h"
+#include "plague.h"
 #include "sect.h"
 #include "nat.h"
 #include "ship.h"
-#include "var.h"
 #include "news.h"
 #include "file.h"
 #include "product.h"
 #include "lost.h"
 #include "budg.h"
 
-#ifndef MIN
-#define MIN(x,y)        ((x) > (y) ? (y) : (x))
-#endif
-
-static int shiprepair(register struct shpstr *, int *, struct natstr *,
-                     int *, int);
-static void upd_ship(register struct shpstr *, register int,
-                    struct natstr *, int *, int);
+static void shiprepair(struct shpstr *, struct natstr *, int *, int);
+static void upd_ship(struct shpstr *, int, struct natstr *, int *, int);
+static int feed_ship(struct shpstr *, int);
 
 int
 prod_ship(int etus, int natnum, int *bp, int build)
                /* build = 1, maintain = 0 */
 {
-    register struct shpstr *sp;
+    struct shpstr *sp;
     struct natstr *np;
     int n, k = 0;
     int start_money;
@@ -78,8 +77,14 @@ prod_ship(int etus, int natnum, int *bp, int build)
            continue;
        if (sp->shp_own != natnum)
            continue;
+       if (sp->shp_effic < SHIP_MINEFF) {
+           makelost(EF_SHIP, sp->shp_own, sp->shp_uid,
+                    sp->shp_x, sp->shp_y);
+           sp->shp_own = 0;
+           continue;
+       }
+
        np = getnatp(sp->shp_own);
-       start_money = np->nat_money;
        if (lastx == 9999 || lasty == 9999) {
            lastx = sp->shp_x;
            lasty = sp->shp_y;
@@ -90,11 +95,12 @@ prod_ship(int etus, int natnum, int *bp, int build)
            bp_clear_cachepath();
            bp_enable_cachepath();
        }
+       start_money = np->nat_money;
        upd_ship(sp, etus, np, bp, build);
        if (build && !player->simulation)       /* make sure to only autonav once */
            nav_ship(sp);       /* autonav the ship */
        sea_money[sp->shp_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;
@@ -110,49 +116,39 @@ prod_ship(int etus, int natnum, int *bp, int build)
 }
 
 static void
-upd_ship(register struct shpstr *sp, register int etus,
+upd_ship(struct shpstr *sp, int etus,
         struct natstr *np, int *bp, int build)
               /* build = 1, maintain = 0 */
 {
     struct sctstr *sectp;
     struct mchrstr *mp;
-    int vec[I_MAX + 1];
-    u_short pstage, ptime;
+    int pstage, ptime;
     int oil_gained;
     int max_oil;
     int max_food;
     struct pchrstr *product;
-    s_char *resource;
+    unsigned char *resource;
+    int dep;
     int n;
     int mult;
-    int needed;
     int cost;
     int eff;
 
     mp = &mchr[(int)sp->shp_type];
-    getvec(VT_ITEM, vec, (s_char *)sp, EF_SHIP);
     if (build == 1) {
-       if (np->nat_priorities[PRI_SBUILD] == 0 || np->nat_money < 0)
-           return;
-       if (sp->shp_effic < SHIP_MINEFF ||
-           !shiprepair(sp, vec, np, bp, etus)) {
-           makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x,
-                    sp->shp_y);
-           sp->shp_own = 0;
-           return;
-       }
+       if (np->nat_money >= 0)
+           shiprepair(sp, np, bp, etus);
     } else {
        mult = 1;
        if (np->nat_level[NAT_TLEV] < sp->shp_tech * 0.85)
            mult = 2;
-       cost = -(mult * etus * dmin(0.0, money_ship * mp->m_cost));
-       if ((np->nat_priorities[PRI_SMAINT] == 0 ||
-            np->nat_money < cost) && !player->simulation) {
+       cost = -(mult * etus * MIN(0.0, money_ship * mp->m_cost));
+       if (np->nat_money < cost && !player->simulation) {
            if ((eff = sp->shp_effic - etus / 5) < SHIP_MINEFF) {
                wu(0, sp->shp_own,
                   "%s lost to lack of maintenance\n", prship(sp));
-               makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x,
-                        sp->shp_y);
+               makelost(EF_SHIP, sp->shp_own, sp->shp_uid,
+                        sp->shp_x, sp->shp_y);
                sp->shp_own = 0;
                return;
            }
@@ -164,42 +160,56 @@ upd_ship(register struct shpstr *sp, register int etus,
            np->nat_money -= cost;
        }
 
-       sectp = getsectp(sp->shp_x, sp->shp_y);
-       if (((mp->m_flags & M_OIL) && (sectp->sct_type == SCT_WATER))
-           && !player->simulation) {
-           /*
-            * take care of oil production
-            */
-           oil_gained = roundavg((vec[I_CIVIL] * etus / 10000.0)
-                                 * sectp->sct_oil);
-           vec[I_OIL] += oil_gained;
-           max_oil = vl_find(V_OIL, mp->m_vtype, mp->m_vamt, mp->m_nv);
-           if (vec[I_OIL] > max_oil)
-               vec[I_OIL] = max_oil;
-           product = &pchr[P_OIL];
-           if (product->p_nrdep != 0 && oil_gained > 0) {
-               resource = ((s_char *)sectp) + product->p_nrndx;
-               *resource -= roundavg(oil_gained *
-                                     product->p_nrdep / 100.0);
-           }
-       }
-       if (((mp->m_flags & M_FOOD) && (sectp->sct_type == SCT_WATER))
-           && !player->simulation) {
-           sectp = getsectp(sp->shp_x, sp->shp_y);
-           vec[I_FOOD] += ((vec[I_CIVIL] * etus) / 1000.0)
-               * sectp->sct_fertil;
-       }
-/* Military costs are now part of regular military costs, not ship costs */
-/*             np->nat_money += (int) (etus * vec[I_MILIT] * money_mil);*/
        if (!player->simulation) {
-           if ((n = feed_ship(sp, vec, etus, &needed, 1)) > 0) {
+           sectp = getsectp(sp->shp_x, sp->shp_y);
+
+           /* produce oil */
+           if ((mp->m_flags & M_OIL) && sectp->sct_type == SCT_WATER) {
+               product = &pchr[P_OIL];
+               oil_gained = roundavg(total_work(100, etus,
+                                                sp->shp_item[I_CIVIL],
+                                                sp->shp_item[I_MILIT],
+                                                sp->shp_item[I_UW],
+                                                ITEM_MAX)
+                                     * sp->shp_effic / 100.0
+                                     * sectp->sct_oil / 100.0
+                                     * prod_eff(product, sp->shp_tech));
+               max_oil = mp->m_item[I_OIL];
+               if (sp->shp_item[I_OIL] + oil_gained > max_oil)
+                   oil_gained = max_oil - sp->shp_item[I_OIL];
+               if (product->p_nrdep != 0 && oil_gained > 0) {
+                   resource = (unsigned char *)sectp + product->p_nrndx;
+                   if (*resource * 100 < product->p_nrdep * oil_gained)
+                       oil_gained = *resource * 100 / product->p_nrdep;
+                   dep = roundavg(oil_gained * product->p_nrdep / 100.0);
+                   if (CANT_HAPPEN(dep > *resource))
+                       dep = *resource;
+                   *resource -= dep;
+               }
+               sp->shp_item[I_OIL] += oil_gained;
+           }
+           /* produce fish */
+           if ((mp->m_flags & M_FOOD) && sectp->sct_type == SCT_WATER) {
+               product = &pchr[P_FOOD];
+               sp->shp_item[I_FOOD]
+                   += roundavg(total_work(100, etus,
+                                          sp->shp_item[I_CIVIL],
+                                          sp->shp_item[I_MILIT],
+                                          sp->shp_item[I_UW],
+                                          ITEM_MAX)
+                               * sp->shp_effic / 100.0
+                               * sectp->sct_fertil / 100.0
+                               * prod_eff(product, sp->shp_tech));
+           }
+           /* feed */
+           if ((n = feed_ship(sp, etus)) > 0) {
                wu(0, sp->shp_own, "%d starved on %s\n", n, prship(sp));
                if (n > 10)
                    nreport(sp->shp_own, N_DIE_FAMINE, 0, 1);
            }
-           max_food = vl_find(V_FOOD, mp->m_vtype, mp->m_vamt, mp->m_nv);
-           if (vec[I_FOOD] > max_food)
-               vec[I_FOOD] = max_food;
+           max_food = mp->m_item[I_FOOD];
+           if (sp->shp_item[I_FOOD] > max_food)
+               sp->shp_item[I_FOOD] = max_food;
            /*
             * do plague stuff.  plague can't break out on ships,
             * but it can still kill people.
@@ -207,7 +217,7 @@ upd_ship(register struct shpstr *sp, register int etus,
            pstage = sp->shp_pstage;
            ptime = sp->shp_ptime;
            if (pstage != PLG_HEALTHY) {
-               n = plague_people(np, vec, &pstage, &ptime, etus);
+               n = plague_people(np, sp->shp_item, &pstage, &ptime, etus);
                switch (n) {
                case PLG_DYING:
                    wu(0, sp->shp_own,
@@ -252,8 +262,7 @@ upd_ship(register struct shpstr *sp, register int etus,
                sp->shp_pstage = pstage;
                sp->shp_ptime = ptime;
            }
-           putvec(VT_ITEM, vec, (s_char *)sp, EF_SHIP);
-           pops[sp->shp_own] += vec[I_CIVIL];
+           pops[sp->shp_own] += sp->shp_item[I_CIVIL];
        }
     }
 }
@@ -263,11 +272,10 @@ upd_ship(register struct shpstr *sp, register int etus,
  * battleships +8 % eff each etu.  This will cost around
  * 8 * 8 * $40 = $2560!
  */
-static int
-shiprepair(register struct shpstr *ship, int *vec, struct natstr *np,
-          int *bp, int etus)
+static void
+shiprepair(struct shpstr *ship, struct natstr *np, int *bp, int etus)
 {
-    register int delta;
+    int delta;
     struct sctstr *sp;
     struct mchrstr *mp;
     float leftp, buildp;
@@ -287,15 +295,15 @@ shiprepair(register struct shpstr *ship, int *vec, struct natstr *np,
        rel = getrel(getnatp(sp->sct_own), ship->shp_own);
 
        if (rel < FRIENDLY)
-           return 1;
+           return;
     }
 
     wf = 0;
     /* only military can work on a military boat */
     if (ship->shp_glim > 0)
-       wf = etus * vec[I_MILIT] / 2;
+       wf = etus * ship->shp_item[I_MILIT] / 2;
     else
-       wf = etus * (vec[I_CIVIL] / 2 + vec[I_MILIT] / 5);
+       wf = etus * (ship->shp_item[I_CIVIL] / 2 + ship->shp_item[I_MILIT] / 5);
 
     if (sp->sct_type != SCT_HARBR) {
        wf /= 3;
@@ -307,45 +315,10 @@ shiprepair(register struct shpstr *ship, int *vec, struct natstr *np,
            avail = wf + gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
     }
 
-    w_p_eff = 20 + (mp->m_lcm + 2 * mp->m_hcm);
-
-    if (sp->sct_type != SCT_HARBR) {
-       int abs_max, amt;
-
-       if (ship->shp_glim > 0) {
-           abs_max = vl_find(V_MILIT, mp->m_vtype,
-                             mp->m_vamt, (int)mp->m_nv);
-           amt = vec[I_MILIT];
-       } else {
-           abs_max = vl_find(V_CIVIL, mp->m_vtype,
-                             mp->m_vamt, (int)mp->m_nv);
-           amt = vec[I_CIVIL];
-           if (abs_max == 0) {
-               abs_max = vl_find(V_MILIT, mp->m_vtype, mp->m_vamt,
-                                 (int)mp->m_nv);
-               amt = vec[I_MILIT];
-           }
-       }
-
-       if (abs_max == 0) {
-           logerror("Abs max of 0 for ship %d\n", ship->shp_uid);
-           abs_max = 1;
-       }
-       avail -= (etus * (100 - ((amt * 100) / abs_max))) / 7;
-       /* think of it as entropy in action */
-    }
-
-    if (avail <= 0) {
-       if (!player->simulation) {
-           if (opt_SHIP_DECAY) {
-               ship->shp_effic += avail / w_p_eff;
-           }
-           return 1;
-       }
-    }
+    w_p_eff = SHP_BLD_WORK(mp->m_lcm, mp->m_hcm);
 
     if ((sp->sct_off) && (sp->sct_own == ship->shp_own))
-       return 1;
+       return;
 
     mult = 1;
     if (np->nat_level[NAT_TLEV] < ship->shp_tech * 0.85)
@@ -353,15 +326,15 @@ shiprepair(register struct shpstr *ship, int *vec, struct natstr *np,
 
     if (ship->shp_effic == 100) {
        /* ship is ok; no repairs needed */
-       return 1;
+       return;
     }
 
     left = 100 - ship->shp_effic;
     delta = roundavg((double)avail / w_p_eff);
     if (delta <= 0)
-       return 1;
-    if (delta > etus * ship_grow_scale)
-       delta = etus * ship_grow_scale;
+       return;
+    if (delta > (int)((float)etus * ship_grow_scale))
+       delta = (int)((float)etus * ship_grow_scale);
     if (delta > left)
        delta = left;
 
@@ -371,25 +344,22 @@ shiprepair(register struct shpstr *ship, int *vec, struct natstr *np,
     if (left > delta)
        left = delta;
 
-    leftp = ((float)left / 100.0);
+    leftp = left / 100.0;
     memset(mvec, 0, sizeof(mvec));
-    mvec[I_LCM] = lcm_needed = ldround((double)(mp->m_lcm * leftp), 1);
-    mvec[I_HCM] = hcm_needed = ldround((double)(mp->m_hcm * leftp), 1);
-
+    mvec[I_LCM] = lcm_needed = ldround(mp->m_lcm * leftp, 1);
+    mvec[I_HCM] = hcm_needed = ldround(mp->m_hcm * leftp, 1);
     get_materials(sp, bp, mvec, 0);
 
-    if (mvec[I_LCM] >= lcm_needed)
-       buildp = leftp;
-    else
-       buildp = ((float)mvec[I_LCM] / (float)mp->m_lcm);
+    buildp = leftp;
+    if (mvec[I_LCM] < lcm_needed)
+       buildp = MIN(buildp, (float)mvec[I_LCM] / (float)mp->m_lcm);
     if (mvec[I_HCM] < hcm_needed)
-       buildp = MIN(buildp, ((float)mvec[I_HCM] / (float)mp->m_hcm));
+       buildp = MIN(buildp, (float)mvec[I_HCM] / (float)mp->m_hcm);
 
-    build = ldround((double)(buildp * 100.0), 1);
+    build = ldround(buildp * 100.0, 1);
     memset(mvec, 0, sizeof(mvec));
-    mvec[I_LCM] = lcm_needed = roundavg((double)(mp->m_lcm * buildp));
-    mvec[I_HCM] = hcm_needed = roundavg((double)(mp->m_hcm * buildp));
-
+    mvec[I_LCM] = roundavg(mp->m_lcm * buildp);
+    mvec[I_HCM] = roundavg(mp->m_hcm * buildp);
     get_materials(sp, bp, mvec, 1);
 
     if (sp->sct_type != SCT_HARBR)
@@ -420,108 +390,44 @@ shiprepair(register struct shpstr *ship, int *vec, struct natstr *np,
 
     np->nat_money -= mult * mp->m_cost * build / 100.0;
     if (!player->simulation)
-       ship->shp_effic += (s_char)build;
-    return 1;
+       ship->shp_effic += (signed char)build;
 }
 
 /*
  * returns the number who starved, if any.
  */
-int
-feed_ship(struct shpstr *sp, register int *vec, int etus, int *needed,
-         int doit)
+static int
+feed_ship(struct shpstr *sp, int etus)
 {
-    double food_eaten, land_eaten;
-    double people_left;
-    int ifood_eaten;
-    int can_eat, need;
-    int total_people;
-    int to_starve;
-    int starved, lvec[I_MAX + 1];
+    int needed, take;
+    double give;
     struct nstr_item ni;
     struct lndstr *lp;
 
     if (opt_NOFOOD)
-       return 0;               /* no food no work to do */
-
-    food_eaten =
-       (etus * eatrate) * (vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW]);
-    ifood_eaten = (int)food_eaten;
-    if ((food_eaten - ifood_eaten) > 0)
-       ifood_eaten++;
-    starved = 0;
-    *needed = 0;
-    if (!player->simulation && food_eaten > vec[I_FOOD])
-       vec[I_FOOD] += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
-                                    I_FOOD, (ifood_eaten - vec[I_FOOD]));
-
-    if (food_eaten > vec[I_FOOD]) {
-/* doit - only steal food from land units during the update */
-       if (sp->shp_nland > 0 && doit) {
-           snxtitem_all(&ni, EF_LAND);
-           while ((lp = (struct lndstr *)nxtitemp(&ni, 0)) &&
-                  (food_eaten > vec[I_FOOD])) {
-               if (lp->lnd_ship != sp->shp_uid)
-                   continue;
-               need = ifood_eaten - vec[I_FOOD];
-               getvec(VT_ITEM, lvec, (s_char *)lp, EF_LAND);
-               land_eaten = (etus * eatrate) * (double)lnd_getmil(lp);
-               if (lvec[I_FOOD] - need > land_eaten) {
-                   vec[I_FOOD] += need;
-                   lvec[I_FOOD] -= need;
-               } else if ((lvec[I_FOOD] - land_eaten) > 0) {
-                   vec[I_FOOD] += (lvec[I_FOOD] - land_eaten);
-                   lvec[I_FOOD] -= (lvec[I_FOOD] - land_eaten);
-               }
-               putvec(VT_ITEM, lvec, (s_char *)lp, EF_LAND);
-           }
+       return 0;
+
+    needed = (int)ceil(food_needed(sp->shp_item, etus));
+
+    /* scrounge */
+    if (needed > sp->shp_item[I_FOOD])
+       sp->shp_item[I_FOOD] += supply_commod(sp->shp_own,
+                                       sp->shp_x, sp->shp_y, I_FOOD,
+                                       needed - sp->shp_item[I_FOOD]);
+    if (needed > sp->shp_item[I_FOOD]) {
+       /* take from embarked land units, but don't starve them */
+       snxtitem_all(&ni, EF_LAND);
+       while ((lp = nxtitemp(&ni)) && needed > sp->shp_item[I_FOOD]) {
+           if (lp->lnd_ship != sp->shp_uid)
+               continue;
+           give = lp->lnd_item[I_FOOD] - food_needed(lp->lnd_item, etus);
+           if (give < 1.0)
+               continue;
+           take = MIN((int)give, needed - sp->shp_item[I_FOOD]);
+           sp->shp_item[I_FOOD] += take;
+           lp->lnd_item[I_FOOD] -= take;
        }
     }
 
-    if (food_eaten > vec[I_FOOD]) {
-       *needed = food_eaten - vec[I_FOOD];
-       if (*needed < (food_eaten - vec[I_FOOD]))
-           (*needed)++;
-       if (opt_NEW_STARVE) {
-           can_eat = (vec[I_FOOD] / (etus * eatrate));
-           total_people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
-
-           /* only want to starve off at most 1/2 the populace. */
-           if (can_eat < (total_people / 2))
-               can_eat = total_people / 2;
-
-           to_starve = total_people - can_eat;
-           while (to_starve && vec[I_UW]) {
-               to_starve--;
-               starved++;
-               vec[I_UW]--;
-           }
-           while (to_starve && vec[I_CIVIL]) {
-               to_starve--;
-               starved++;
-               vec[I_CIVIL]--;
-           }
-           while (to_starve && vec[I_MILIT]) {
-               to_starve--;
-               starved++;
-               vec[I_MILIT]--;
-           }
-
-           vec[I_FOOD] = 0;
-       } else {                /* ! opt_NEW_STARVE */
-           people_left = (vec[I_FOOD] + 0.01) / (food_eaten + 0.01);
-           starved = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
-           /* only want to starve off at most 1/2 the populace. */
-           if (people_left < 0.5)
-               people_left = 0.5;
-           vec[I_CIVIL] = (int)(vec[I_CIVIL] * people_left);
-           vec[I_MILIT] = (int)(vec[I_MILIT] * people_left);
-           vec[I_UW] = (int)(vec[I_UW] * people_left);
-           starved -= vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
-           vec[I_FOOD] = 0;
-       }
-    } else {
-       vec[I_FOOD] -= (int)food_eaten;
-    }
-    return starved;
+    return feed_people(sp->shp_item, etus);
 }