]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/supply.c
Don't resupply supply unit after use as supply source
[empserver] / src / lib / subs / supply.c
index a417f3a99a4af8e5d4692a81d9f6d380742a7305..ce5bf89465757ea4ae5d4e8c4f2108ef9db4448c 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-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
  *  ---
  *
  *  supply.c: Supply subroutines
- * 
+ *
  *  Known contributors to this file:
- *  
+ *
  */
 
 #include <config.h>
 
-#include "misc.h"
-#include "nat.h"
-#include "ship.h"
-#include "land.h"
-#include "sect.h"
-#include "xy.h"
-#include "nsc.h"
-#include "path.h"
-#include "item.h"
+#include <math.h>
 #include "file.h"
+#include "land.h"
+#include "nat.h"
 #include "optlist.h"
 #include "player.h"
 #include "prototypes.h"
+#include "sect.h"
+#include "ship.h"
 
 static int get_minimum(struct lndstr *, i_type);
 static int s_commod(int, int, int, i_type, int, int);
 
 /*
  * We want to get enough guns to be maxed out, enough shells to
- *     fire once, one update's worth of food, enough fuel for
- *     one update.
+ *     fire once, one update's worth of food.
  *
  * Firts, try to forage in the sector
  * Second look for a warehouse or headquarters to leech
  * Third, look for a ship we own in a harbor
  * Fourth, look for supplies in a supply unit we own
- *             (one good reason to do this last is that the supply
- *              unit will then call resupply, taking more time)
+ *             (one good reason to do this last is that the supply
+ *              unit will then call resupply, taking more time)
  *
  * May want to put code to resupply with SAMs here, later --ts
- *
  */
 
 void
@@ -72,8 +66,6 @@ resupply_all(struct lndstr *lp)
     if (!opt_NOFOOD)
        resupply_commod(lp, I_FOOD);
     resupply_commod(lp, I_SHELL);
-    if (opt_FUEL)
-       resupply_commod(lp, I_PETROL);
 }
 
 /*
@@ -85,40 +77,12 @@ void
 resupply_commod(struct lndstr *lp, i_type type)
 {
     int amt;
-    struct shpstr ship;
 
-    /* Ok, do we now have enough? */
     amt = get_minimum(lp, type) - lp->lnd_item[type];
     if (amt > 0) {
-       lp->lnd_item[type] += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
+       lp->lnd_item[type] += supply_commod(lp->lnd_own,
+                                           lp->lnd_x, lp->lnd_y,
                                            type, amt);
-       amt = get_minimum(lp, type) - lp->lnd_item[type];
-    }
-    /* Now, check again to see if we have enough. */
-    if (amt > 0) {
-       /* Are we on a ship?  if so, try to get it from the ship first. */
-       if (lp->lnd_ship >= 0) {
-           getship(lp->lnd_ship, &ship);
-           /* Now, determine how much we can get */
-           if (amt > ship.shp_item[type])
-               amt = ship.shp_item[type];
-           /* Now, add and subtract */
-           lp->lnd_item[type] += amt;
-           ship.shp_item[type] -= amt;
-           putship(lp->lnd_ship, &ship);
-       }
-    }
-
-    if (opt_FUEL && type == I_PETROL) {
-       int fuel_needed = (lp->lnd_fuelu * (((float)etu_per_update
-                                            * land_mob_scale)) / 10.0);
-
-       while ((lp->lnd_fuel < fuel_needed) && lp->lnd_item[I_PETROL]) {
-           lp->lnd_fuel += 10;
-           if (lp->lnd_fuel > lp->lnd_fuelc)
-               lp->lnd_fuel = lp->lnd_fuelc;
-           lp->lnd_item[I_PETROL]--;
-       }
     }
 }
 
@@ -128,7 +92,7 @@ resupply_commod(struct lndstr *lp, i_type type)
 int
 supply_commod(int own, int x, int y, i_type type, int total_wanted)
 {
-    if (total_wanted < 0)
+    if (total_wanted <= 0)
        return 0;
     return s_commod(own, x, y, type, total_wanted, !player->simulation);
 }
@@ -136,10 +100,10 @@ supply_commod(int own, int x, int y, i_type type, int total_wanted)
 /*
  * Just return the number you COULD get, without doing it
  */
-int
+static int
 try_supply_commod(int own, int x, int y, i_type type, int total_wanted)
 {
-    if (total_wanted < 0)
+    if (total_wanted <= 0)
        return 0;
 
     return s_commod(own, x, y, type, total_wanted, 0);
@@ -165,7 +129,7 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
     int packing;
     struct dchrstr *dp;
     struct ichrstr *ip;
-    s_char buf[1024];
+    char buf[1024];
 
     /* try to get it from sector we're in */
     getsect(x, y, &dest);
@@ -199,14 +163,11 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
            continue;
        if (sect.sct_effic < 60)
            continue;
-       if (BestLandPath(buf, &dest, &sect, &move_cost, MOB_ROAD) ==
-           (s_char *)0)
+       if (!BestLandPath(buf, &dest, &sect, &move_cost, MOB_MOVE))
            continue;
-       if (!opt_NOFOOD && type == I_FOOD) {
-           minimum = 2 + ((etu_per_update * eatrate)
-                          * (sect.sct_item[I_CIVIL] + sect.sct_item[I_MILIT]
-                             + sect.sct_item[I_UW]));
-       }
+       if (!opt_NOFOOD && type == I_FOOD)
+           minimum = 1 + (int)ceil(food_needed(sect.sct_item,
+                                               etu_per_update));
        if (sect.sct_item[type] <= minimum) {
            /* Don't bother... */
            continue;
@@ -216,10 +177,10 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
        packing = ip->i_pkg[dp->d_pkg];
        if (packing > 1 && sect.sct_effic < 60)
            packing = 1;
-       weight = ((double)ip->i_lbs / (double)packing);
+       weight = (double)ip->i_lbs / packing;
        mobcost = move_cost * weight;
        if (mobcost > 0)
-           can_move = ((double)sect.sct_mobil / mobcost);
+           can_move = (double)sect.sct_mobil / mobcost;
        else
            can_move = sect.sct_item[type] - minimum;
        if (can_move > sect.sct_item[type] - minimum)
@@ -236,7 +197,7 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
                n = 0;
            if (n > sect.sct_mobil)
                n = sect.sct_mobil;
-           sect.sct_mobil -= (u_char)n;
+           sect.sct_mobil -= n;
 
            if (actually_doit)
                putsect(&sect);
@@ -254,7 +215,7 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
                n = 0;
            if (n > sect.sct_mobil)
                n = sect.sct_mobil;
-           sect.sct_mobil -= (u_char)n;
+           sect.sct_mobil -= n;
 
            if (actually_doit)
                putsect(&sect);
@@ -275,13 +236,11 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
            continue;
        if (sect.sct_effic < 2)
            continue;
-       if (BestLandPath(buf, &dest, &sect, &move_cost, MOB_ROAD) ==
-           (s_char *)0)
+       if (!BestLandPath(buf, &dest, &sect, &move_cost, MOB_MOVE))
            continue;
        if (!opt_NOFOOD && type == I_FOOD)
-           minimum = 2 + ((etu_per_update * eatrate)
-                          * (ship.shp_item[I_CIVIL] + ship.shp_item[I_MILIT]
-                             + ship.shp_item[I_UW]));
+           minimum = 1 + (int)ceil(food_needed(ship.shp_item,
+                                               etu_per_update));
        if (ship.shp_item[type] <= minimum) {
            /* Don't bother... */
            continue;
@@ -291,10 +250,10 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
        packing = ip->i_pkg[dp->d_pkg];
        if (packing > 1 && sect.sct_effic < 60)
            packing = 1;
-       weight = ((double)ip->i_lbs / (double)packing);
+       weight = (double)ip->i_lbs / packing;
        mobcost = move_cost * weight;
        if (mobcost > 0)
-           can_move = ((double)sect.sct_mobil / mobcost);
+           can_move = (double)sect.sct_mobil / mobcost;
        else
            can_move = ship.shp_item[type] - minimum;
        if (can_move > ship.shp_item[type] - minimum)
@@ -308,7 +267,7 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
                n = 0;
            if (n > sect.sct_mobil)
                n = sect.sct_mobil;
-           sect.sct_mobil -= (u_char)n;
+           sect.sct_mobil -= n;
            if (actually_doit) {
                putship(ship.shp_uid, &ship);
                putsect(&sect);
@@ -325,7 +284,7 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
                n = 0;
            if (n > sect.sct_mobil)
                n = sect.sct_mobil;
-           sect.sct_mobil -= (u_char)n;
+           sect.sct_mobil -= n;
 
            if (actually_doit) {
                putship(ship.shp_uid, &ship);
@@ -351,8 +310,7 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
            continue;
 
        getsect(land.lnd_x, land.lnd_y, &sect);
-       if (BestLandPath(buf, &dest, &sect, &move_cost, MOB_ROAD) ==
-           (s_char *)0)
+       if (!BestLandPath(buf, &dest, &sect, &move_cost, MOB_MOVE))
            continue;
 
        if ((land.lnd_ship >= 0) && (sect.sct_type != SCT_HARBR))
@@ -371,6 +329,7 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
            save = land;
            land.lnd_item[type] = 0;
            putland(land.lnd_uid, &land);
+           save.lnd_seqno = land.lnd_seqno;
 
            land.lnd_item[type] =
                save.lnd_item[type] + s_commod(own, land.lnd_x, land.lnd_y,
@@ -383,10 +342,10 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
 
        min = get_minimum(&land, type);
        ip = &ichr[type];
-       weight = ((double)ip->i_lbs);
+       weight = ip->i_lbs;
        mobcost = move_cost * weight;
        if (mobcost > 0)
-           can_move = ((double)land.lnd_mobil / mobcost);
+           can_move = (double)land.lnd_mobil / mobcost;
        else
            can_move = land.lnd_item[type] - min;
        if (can_move > land.lnd_item[type] - min)
@@ -394,10 +353,6 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
 
        if (can_move >= wanted) {
            land.lnd_item[type] -= wanted;
-
-           /* resupply the supply unit */
-           resupply_commod(&land, type);
-
            land.lnd_mobil -= roundavg(wanted * weight * move_cost);
 
            if (actually_doit)
@@ -423,8 +378,7 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
 
 /*
  * We want to get enough shells to fire once,
- * one update's worth of food, enough fuel for
- * one update.
+ * one update's worth of food.
  */
 
 static int
@@ -440,33 +394,10 @@ get_minimum(struct lndstr *lp, i_type type)
     case I_FOOD:
        if (opt_NOFOOD)
            return 0;           /* no food reqd, get out */
-       want = (((double)etu_per_update * eatrate) *
-               (double)total_mil(lp)) + 1;
+       want = (int)ceil(food_needed(lp->lnd_item, etu_per_update));
        break;
     case I_SHELL:
-       want = lp->lnd_ammo;
-       break;
-
-       /*
-        * return the amount of pet we'd need to get to 
-        * enough fuel for 1 update
-        *
-        */
-    case I_PETROL:
-       if (opt_FUEL == 0)
-           return 0;
-       want = (lp->lnd_fuelu * (((float)etu_per_update *
-                                 land_mob_scale)) / 10.0);
-       want -= lp->lnd_fuel;
-       if (want > 0) {
-           double d;
-           d = (double)want / 10.0;
-           want = (int)d;
-           if (want == 0)
-               want++;
-       }
-
-       max = want;
+       want = lcp->l_ammo;
        break;
     default:
        return 0;
@@ -483,7 +414,6 @@ has_supply(struct lndstr *lp)
 {
     int shells_needed, shells, keepshells;
     int food, food_needed, keepfood;
-    int fuel_needed, fuel, petrol_needed, petrol, keeppetrol;
 
     if (!opt_NOFOOD) {
        food_needed = get_minimum(lp, I_FOOD);
@@ -501,7 +431,7 @@ has_supply(struct lndstr *lp)
 
     }
 
-    shells_needed = lp->lnd_ammo;
+    shells_needed = lchr[lp->lnd_type].l_ammo;
     shells = keepshells = lp->lnd_item[I_SHELL];
     if (shells < shells_needed) {
        lp->lnd_item[I_SHELL] = 0;
@@ -515,103 +445,5 @@ has_supply(struct lndstr *lp)
     if (shells < shells_needed)
        return 0;
 
-    if (opt_FUEL) {
-       fuel_needed = lp->lnd_fuelu;
-       fuel = lp->lnd_fuel;
-       if (fuel < fuel_needed) {
-           petrol_needed =
-               ldround(((double)(fuel_needed - fuel) / 10.0), 1);
-           petrol = keeppetrol = lp->lnd_item[I_PETROL];
-           if (petrol < petrol_needed) {
-               lp->lnd_item[I_PETROL] = 0;
-               putland(lp->lnd_uid, lp);
-               petrol += try_supply_commod(lp->lnd_own,
-                                           lp->lnd_x, lp->lnd_y,
-                                           I_PETROL,
-                                           (petrol_needed - petrol));
-               lp->lnd_item[I_PETROL] = keeppetrol;
-               putland(lp->lnd_uid, lp);
-           }
-           fuel += petrol * 10;
-       }
-
-       if (fuel < fuel_needed)
-           return 0;
-    }
-    /* end opt_FUEL */
-    return 1;
-}
-
-int
-use_supply(struct lndstr *lp)
-{
-    int shells_needed, shells, food, food_needed;
-    int fuel_needed, fuel, petrol_needed, petrol;
-
-    shells_needed = lp->lnd_ammo;
-    shells = lp->lnd_item[I_SHELL];
-    if (shells < shells_needed) {
-       lp->lnd_item[I_SHELL] = 0;
-       putland(lp->lnd_uid, lp);
-       shells += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, I_SHELL,
-                               (shells_needed - shells));
-       lp->lnd_item[I_SHELL] = shells;
-    }
-
-    lp->lnd_item[I_SHELL] = MAX(lp->lnd_item[I_SHELL] - shells_needed, 0);
-
-    if (lp->lnd_frg)           /* artillery */
-       goto done;
-
-    food_needed = get_minimum(lp, I_FOOD);
-    food = lp->lnd_item[I_SHELL];
-
-    if (food < food_needed) {
-       lp->lnd_item[I_FOOD] = 0;
-       putland(lp->lnd_uid, lp);
-       food += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y, I_FOOD,
-                             (food_needed - food));
-       lp->lnd_item[I_FOOD] = food;
-    }
-
-    lp->lnd_item[I_FOOD] = MAX(lp->lnd_item[I_FOOD] - food_needed, 0);
-
-    if (opt_FUEL) {
-       fuel_needed = lp->lnd_fuelu;
-       fuel = lp->lnd_fuel;
-
-       petrol = petrol_needed = 0;
-
-       if (fuel < fuel_needed) {
-           petrol_needed =
-               ldround(((double)(fuel_needed - fuel) / 10.0), 1);
-           petrol = lp->lnd_item[I_PETROL];
-       }
-
-       if (petrol < petrol_needed) {
-           lp->lnd_item[I_PETROL] = 0;
-           putland(lp->lnd_uid, lp);
-           petrol += supply_commod(lp->lnd_own,
-                                   lp->lnd_x, lp->lnd_y,
-                                   I_PETROL, (petrol_needed - petrol));
-           lp->lnd_item[I_PETROL] = petrol;
-       }
-
-       if (petrol_needed) {
-           if (petrol >= petrol_needed) {
-               lp->lnd_item[I_PETROL]
-                   = MAX(lp->lnd_item[I_PETROL] - petrol_needed, 0);
-               lp->lnd_fuel += petrol_needed * 10;
-           } else {
-               lp->lnd_fuel += lp->lnd_item[I_PETROL] * 10;
-               lp->lnd_item[I_PETROL] = 0;
-           }
-       }
-
-       lp->lnd_fuel = MAX(lp->lnd_fuel - fuel_needed, 0);
-    }
-    /* end opt_FUEL */
-  done:
-    putland(lp->lnd_uid, lp);
     return 1;
 }