]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/ship.c
Update copyright notice
[empserver] / src / lib / update / ship.c
index 6e018ef728571ecc85f965231c973e6cbc69d74e..f0711cd403467f9f3f6a023e241a042b69eeae6b 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2018, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
  *  ---
  *
  *  ship.c: Do production for ships
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     Steve McClure, 1996
  *     Ron Koenderink, 2004
- *     Markus Armbruster, 2006
+ *     Markus Armbruster, 2006-2016
  */
 
 #include <config.h>
 
 #include <math.h>
-#include "misc.h"
-#include "plague.h"
-#include "sect.h"
+#include "chance.h"
+#include "land.h"
+#include "lost.h"
 #include "nat.h"
-#include "ship.h"
 #include "news.h"
-#include "file.h"
-#include "product.h"
-#include "land.h"
-#include "xy.h"
-#include "nsc.h"
 #include "optlist.h"
+#include "plague.h"
 #include "player.h"
+#include "product.h"
+#include "prototypes.h"
+#include "ship.h"
 #include "update.h"
-#include "common.h"
-#include "subs.h"
-#include "gen.h"
-#include "lost.h"
-#include "budg.h"
 
-static void shiprepair(struct shpstr *, struct natstr *, int *, int);
-static void upd_ship(struct shpstr *, int, struct natstr *, int *, int);
+static void upd_ship(struct shpstr *, int, struct bp *, int);
+static void plague_ship(struct shpstr *, int);
+static void shiprepair(struct shpstr *, struct natstr *, struct bp *,
+                      int, struct budget *);
+static void ship_produce(struct shpstr *, int, struct budget *);
 static int feed_ship(struct shpstr *, int);
 
-int
-prod_ship(int etus, int natnum, int *bp, int build)
-               /* build = 1, maintain = 0 */
+void prep_ships(int etus, struct bp *bp)
 {
+    int mil, i, n;
+    double mil_pay;
     struct shpstr *sp;
-    struct natstr *np;
-    int n, k = 0;
-    int start_money;
-    int lastx = 9999, lasty = 9999;
 
-    bp_enable_cachepath();
-    for (n = 0; NULL != (sp = getshipp(n)); n++) {
+    for (i = 0; (sp = getshipp(i)); i++) {
        if (sp->shp_own == 0)
            continue;
-       if (sp->shp_own != natnum)
-           continue;
-       if (sp->shp_effic < SHIP_MINEFF) {
+       if (CANT_HAPPEN(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);
-       if (lastx == 9999 || lasty == 9999) {
-           lastx = sp->shp_x;
-           lasty = sp->shp_y;
-       }
-       if (lastx != sp->shp_x || lasty != sp->shp_y) {
-           /* Reset the cache */
-           bp_disable_cachepath();
-           bp_clear_cachepath();
-           bp_enable_cachepath();
+       bp_consider_unit(bp, (struct empobj *)sp);
+       mil = sp->shp_item[I_MILIT];
+       mil_pay = mil * etus * money_mil;
+       nat_budget[sp->shp_own].mil.count += mil;
+       nat_budget[sp->shp_own].mil.money += mil_pay;
+       nat_budget[sp->shp_own].money += mil_pay;
+
+       if (!player->simulation) {
+           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);
+           }
+           plague_ship(sp, etus);
        }
-       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)
-           k++;
-       if (player->simulation)
-           np->nat_money = start_money;
     }
-    bp_disable_cachepath();
-    bp_clear_cachepath();
+}
+
+void
+prod_ship(int etus, struct bp *bp, int build)
+               /* build = 1, maintain = 0 */
+{
+    struct shpstr *sp;
+    int i;
 
-    if (opt_SAIL) {
-       if (build && !player->simulation)       /* make sure to only sail once */
-           sail_ship(natnum);
+    for (i = 0; (sp = getshipp(i)); i++) {
+       if (sp->shp_own == 0)
+           continue;
+       if (bp_skip_unit(bp, (struct empobj *)sp))
+           continue;
+       upd_ship(sp, etus, bp, build);
     }
-    return k;
 }
 
 static void
-upd_ship(struct shpstr *sp, int etus,
-        struct natstr *np, int *bp, int build)
+upd_ship(struct shpstr *sp, int etus, struct bp *bp, int build)
               /* build = 1, maintain = 0 */
 {
-    struct sctstr *sectp;
-    struct mchrstr *mp;
-    int pstage, ptime;
-    int oil_gained;
-    int max_oil;
-    int max_food;
-    struct pchrstr *product;
-    unsigned char *resource;
-    int dep;
-    int n;
-    int mult;
-    int cost;
-    int eff;
+    struct budget *budget = &nat_budget[sp->shp_own];
+    struct mchrstr *mp = &mchr[sp->shp_type];
+    struct natstr *np = getnatp(sp->shp_own);
+    int mult, eff_lost;
+    double cost;
 
-    mp = &mchr[(int)sp->shp_type];
     if (build == 1) {
-       if (!sp->shp_off && np->nat_money >= 0)
-           shiprepair(sp, np, bp, etus);
+       if (!sp->shp_off && budget->money >= 0)
+           shiprepair(sp, np, bp, etus, budget);
+       ship_produce(sp, etus, budget);
        if (!player->simulation)
            sp->shp_off = 0;
     } else {
+       budget->oldowned_civs += sp->shp_item[I_CIVIL];
        mult = 1;
        if (np->nat_level[NAT_TLEV] < sp->shp_tech * 0.85)
            mult = 2;
-       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);
-               sp->shp_own = 0;
-               return;
+       budget->bm[BUDG_SHP_MAINT].count++;
+       cost = mult * etus * -money_ship * mp->m_cost;
+       if (budget->money < cost && !player->simulation) {
+           eff_lost = etus / 5;
+           if (sp->shp_effic - eff_lost < SHIP_MINEFF)
+               eff_lost = sp->shp_effic - SHIP_MINEFF;
+           if (eff_lost > 0) {
+               wu(0, sp->shp_own, "%s lost %d%% to lack of maintenance\n",
+                  prship(sp), eff_lost);
+               sp->shp_effic -= eff_lost;
            }
-           wu(0, sp->shp_own,
-              "%s lost %d%% to lack of maintenance\n",
-              prship(sp), sp->shp_effic - eff);
-           sp->shp_effic = eff;
        } else {
-           np->nat_money -= cost;
+           budget->bm[BUDG_SHP_MAINT].money -= cost;
+           budget->money -= cost;
        }
+    }
+}
 
-       if (!player->simulation) {
-           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 = 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.
-            */
-           pstage = sp->shp_pstage;
-           ptime = sp->shp_ptime;
-           if (pstage != PLG_HEALTHY) {
-               n = plague_people(np, sp->shp_item, &pstage, &ptime, etus);
-               switch (n) {
-               case PLG_DYING:
-                   wu(0, sp->shp_own,
-                      "PLAGUE deaths reported on %s\n", prship(sp));
-                   nreport(sp->shp_own, N_DIE_PLAGUE, 0, 1);
-                   break;
-               case PLG_INFECT:
-                   wu(0, sp->shp_own, "%s battling PLAGUE\n", prship(sp));
-                   break;
-               case PLG_INCUBATE:
-                   /* Are we still incubating? */
-                   if (n == pstage) {
-                       /* Yes. Will it turn "infectious" next time? */
-                       if (ptime <= etus) {
-                           /* Yes.  Report an outbreak. */
-                           wu(0, sp->shp_own,
-                              "Outbreak of PLAGUE on %s!\n", prship(sp));
-                           nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
-                       }
-                   } else {
-                       /* It has already moved on to "infectious" */
-                       wu(0, sp->shp_own,
-                          "%s battling PLAGUE\n", prship(sp));
-                   }
-                   break;
-               case PLG_EXPOSED:
-                   /* Has the plague moved to "incubation" yet? */
-                   if (n != pstage) {
-                       /* Yes. Will it turn "infectious" next time? */
-                       if (ptime <= etus) {
-                           /* Yes.  Report an outbreak. */
-                           wu(0, sp->shp_own,
-                              "Outbreak of PLAGUE on %s!\n", prship(sp));
-                           nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
-                       }
-                   }
-                   break;
-               default:
-                   break;
-               }
-
-               sp->shp_pstage = pstage;
-               sp->shp_ptime = ptime;
-           }
-           pops[sp->shp_own] += sp->shp_item[I_CIVIL];
-       }
+static void
+plague_ship(struct shpstr *sp, int etus)
+{
+    struct natstr *np = getnatp(sp->shp_own);
+    int pstage, ptime;
+    int n;
+
+    /* Plague can't break out on ships, but it can still kill people */
+    pstage = sp->shp_pstage;
+    ptime = sp->shp_ptime;
+    if (pstage != PLG_HEALTHY) {
+       n = plague_people(np, sp->shp_item, &pstage, &ptime, etus);
+       if (n != PLG_HEALTHY)
+           plague_report(sp->shp_own, n, pstage, ptime, etus,
+                         "on", prship(sp));
+       sp->shp_pstage = pstage;
+       sp->shp_ptime = ptime;
     }
 }
 
@@ -275,34 +171,41 @@ upd_ship(struct shpstr *sp, int etus,
  * 8 * 8 * $40 = $2560!
  */
 static void
-shiprepair(struct shpstr *ship, struct natstr *np, int *bp, int etus)
+shiprepair(struct shpstr *ship, struct natstr *np, struct bp *bp, int etus,
+          struct budget *budget)
 {
+    struct mchrstr *mp = &mchr[(int)ship->shp_type];
     int delta;
-    struct sctstr *sp;
-    struct mchrstr *mp;
-    float leftp, buildp;
-    int left, build;
-    int lcm_needed, hcm_needed;
+    struct sctstr *sp, scratch_sect;
+    int build;
     int wf;
     int avail;
-    int w_p_eff;
     int mult;
-    int mvec[I_MAX + 1];
-    int rel;
+    double cost;
+
+    if (ship->shp_effic == 100)
+       return;
 
-    mp = &mchr[(int)ship->shp_type];
     sp = getsectp(ship->shp_x, ship->shp_y);
+    if (sp->sct_off)
+       return;
 
-    if ((sp->sct_own != ship->shp_own) && (sp->sct_own != 0)) {
-       rel = getrel(getnatp(sp->sct_own), ship->shp_own);
+    if (sp->sct_own != 0
+       && relations_with(sp->sct_own, ship->shp_own) < FRIENDLY)
+       return;
 
-       if (rel < FRIENDLY)
-           return;
+    if (player->simulation) {
+       scratch_sect = *sp;
+       bp_to_sect(bp, &scratch_sect);
+       sp = &scratch_sect;
     }
 
-    wf = 0;
+    mult = 1;
+    if (np->nat_level[NAT_TLEV] < ship->shp_tech * 0.85)
+       mult = 2;
+
     /* only military can work on a military boat */
-    if (ship->shp_glim > 0)
+    if (mp->m_glim != 0)
        wf = etus * ship->shp_item[I_MILIT] / 2;
     else
        wf = etus * (ship->shp_item[I_CIVIL] / 2 + ship->shp_item[I_MILIT] / 5);
@@ -310,78 +213,28 @@ shiprepair(struct shpstr *ship, struct natstr *np, int *bp, int etus)
     if (sp->sct_type != SCT_HARBR) {
        wf /= 3;
        avail = wf;
-    } else {
-       if (!player->simulation)
-           avail = wf + sp->sct_avail * 100;
-       else
-           avail = wf + gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
-    }
-
-    w_p_eff = SHP_BLD_WORK(mp->m_lcm, mp->m_hcm);
-
-    if ((sp->sct_off) && (sp->sct_own == ship->shp_own))
-       return;
-
-    mult = 1;
-    if (np->nat_level[NAT_TLEV] < ship->shp_tech * 0.85)
-       mult = 2;
-
-    if (ship->shp_effic == 100) {
-       /* ship is ok; no repairs needed */
-       return;
-    }
+    } else
+       avail = wf + sp->sct_avail * 100;
 
-    left = 100 - ship->shp_effic;
-    delta = roundavg((double)avail / w_p_eff);
+    delta = avail / mp->m_bwork;
     if (delta <= 0)
        return;
     if (delta > (int)((float)etus * ship_grow_scale))
        delta = (int)((float)etus * ship_grow_scale);
-    if (delta > left)
-       delta = left;
-
-    /* delta is the max amount we can grow */
-
-    left = 100 - ship->shp_effic;
-    if (left > delta)
-       left = delta;
-
-    leftp = left / 100.0;
-    memset(mvec, 0, sizeof(mvec));
-    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 (delta > 100 - ship->shp_effic)
+       delta = 100 - ship->shp_effic;
 
-    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);
-
-    build = ldround(buildp * 100.0, 1);
-    memset(mvec, 0, sizeof(mvec));
-    mvec[I_LCM] = roundavg(mp->m_lcm * buildp);
-    mvec[I_HCM] = roundavg(mp->m_hcm * buildp);
-    get_materials(sp, bp, mvec, 1);
+    build = get_materials(sp, mp->m_mat, delta);
 
     if (sp->sct_type != SCT_HARBR)
        build = delta;
-    wf -= build * w_p_eff;
+
+    wf -= build * mp->m_bwork;
     if (wf < 0) {
-       /*
-        * I didn't use roundavg here, because I want to penalize
-        * the player with a large number of ships.
-        */
-       if (!player->simulation)
-           avail = (sp->sct_avail * 100 + wf) / 100;
-       else
-           avail = (gt_bg_nmbr(bp, sp, I_MAX + 1) * 100 + wf) / 100;
+       avail = roundavg((sp->sct_avail * 100 + wf) / 100.0);
        if (avail < 0)
            avail = 0;
-       if (!player->simulation)
-           sp->sct_avail = avail;
-       else
-           pt_bg_nmbr(bp, sp, I_MAX + 1, avail);
+       sp->sct_avail = avail;
     }
     if (sp->sct_type != SCT_HARBR)
        if ((build + ship->shp_effic) > 80) {
@@ -390,11 +243,74 @@ shiprepair(struct shpstr *ship, struct natstr *np, int *bp, int etus)
                build = 0;
        }
 
-    np->nat_money -= mult * mp->m_cost * build / 100.0;
+    bp_set_from_sect(bp, sp);
+    cost = mult * mp->m_cost * build / 100.0;
+    budget->bm[BUDG_SHP_BUILD].count += !!build;
+    budget->bm[BUDG_SHP_BUILD].money -= cost;
+    budget->money -= cost;
     if (!player->simulation)
        ship->shp_effic += (signed char)build;
 }
 
+static void
+ship_produce(struct shpstr *sp, int etus, struct budget *budget)
+{
+    struct mchrstr *mp = &mchr[sp->shp_type];
+    struct sctstr *sectp = getsectp(sp->shp_x, sp->shp_y);
+    int oil_gained;
+    int max_oil;
+    int max_food;
+    struct pchrstr *product;
+    unsigned char *resource;
+    int dep;
+
+    if (player->simulation)
+       return;
+
+    /* produce oil */
+    if (!sp->shp_off && budget->money >= 0
+       && (mp->m_flags & M_OIL) && sectp->sct_type == SCT_WATER) {
+       product = &pchr[dchr[SCT_OIL].d_prd];
+       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(SCT_OIL, 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 (!sp->shp_off && budget->money >= 0
+       && (mp->m_flags & M_FOOD) && sectp->sct_type == SCT_WATER) {
+       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(SCT_AGRI, sp->shp_tech));
+    }
+    max_food = mp->m_item[I_FOOD];
+    if (sp->shp_item[I_FOOD] > max_food)
+       sp->shp_item[I_FOOD] = max_food;
+}
+
 /*
  * returns the number who starved, if any.
  */
@@ -412,16 +328,10 @@ feed_ship(struct shpstr *sp, int etus)
     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);
+       snxtitem_cargo(&ni, EF_LAND, EF_SHIP, sp->shp_uid);
        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;