src/lib/update/nav_util.c

Go to the documentation of this file.
00001 /*
00002  *  Empire - A multi-player, client/server Internet based war game.
00003  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
00004  *                           Ken Stevens, Steve McClure
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  ---
00021  *
00022  *  See files README, COPYING and CREDITS in the root of the source
00023  *  tree for related information and legal notices.  It is expected
00024  *  that future projects/authors will amend these files as needed.
00025  *
00026  *  ---
00027  *
00028  *  nav_util.c: Utilities for autonav and sail
00029  * 
00030  *  Known contributors to this file:
00031  *     
00032  */
00033 
00034 #include <config.h>
00035 
00036 #include "item.h"
00037 #include "land.h"
00038 #include "nsc.h"
00039 #include "path.h"
00040 #include "plague.h"
00041 #include "plane.h"
00042 #include "ship.h"
00043 #include "update.h"
00044 
00045 /* load a specific ship given its 
00046  * location and what field to modify.
00047  * new autonav code
00048  * Chad Zabel 6/1/94 
00049  */
00050 int
00051 load_it(struct shpstr *sp, struct sctstr *psect, int i)
00052 {
00053     int shipown, amount, ship_amt, sect_amt;
00054     int abs_max, max_amt, transfer;
00055     i_type comm;
00056     struct mchrstr *vship;
00057 
00058     amount = sp->shp_lend[i];
00059     shipown = sp->shp_own;
00060     comm = sp->shp_tend[i];
00061     if (CANT_HAPPEN(comm <= I_NONE || comm > I_MAX))
00062         return 0;
00063 
00064     ship_amt = sp->shp_item[comm];
00065     sect_amt = psect->sct_item[comm];
00066 
00067     /* check for disloyal civilians */
00068     if (psect->sct_oldown != shipown && comm == I_CIVIL) {
00069         wu(0, shipown,
00070            "Ship #%d - unable to load disloyal civilians at %s.",
00071            sp->shp_uid, xyas(psect->sct_x, psect->sct_y, psect->sct_own));
00072         return 0;
00073     }
00074     if (comm == I_CIVIL || comm == I_MILIT)
00075         sect_amt--;             /* leave 1 civ or mil to hold the sector. */
00076     vship = &mchr[(int)sp->shp_type];
00077     abs_max = max_amt = vship->m_item[comm];
00078 
00079     if (!abs_max)
00080         return 0;               /* can't load the ship, skip to the end. */
00081 
00082     max_amt = MIN(sect_amt, max_amt - ship_amt);
00083     if (max_amt <= 0 && (ship_amt != abs_max)) {
00084         sp->shp_autonav |= AN_LOADING;
00085         return 0;
00086     }
00087 
00088 
00089     transfer = amount - ship_amt;
00090     if (transfer > sect_amt) {  /* not enough in the   */
00091         transfer = sect_amt;    /* sector to fill the  */
00092         sp->shp_autonav |= AN_LOADING;  /* ship, set load flag */
00093     }
00094     if (ship_amt + transfer > abs_max)  /* Do not load more    */
00095         transfer = abs_max - ship_amt;  /* then the max alowed */
00096     /* on the ship.        */
00097 
00098     if (transfer <= 0)
00099         return 0;               /* nothing to move */
00100 
00101 
00102     sp->shp_item[comm] = ship_amt + transfer;
00103     if (comm == I_CIVIL || comm == I_MILIT)
00104         sect_amt++;             /*adjustment */
00105     psect->sct_item[comm] = sect_amt - transfer;
00106 
00107     /* deal with the plague */
00108     if (psect->sct_pstage == PLG_INFECT && sp->shp_pstage == PLG_HEALTHY)
00109         sp->shp_pstage = PLG_EXPOSED;
00110     if (sp->shp_pstage == PLG_INFECT && psect->sct_pstage == PLG_HEALTHY)
00111         psect->sct_pstage = PLG_EXPOSED;
00112 
00113     return 1;                   /* we did someloading return 1 to keep */
00114     /* our loop happy in nav_ship()        */
00115 
00116 }
00117 
00118 /* unload_it 
00119  * A guess alot of this looks like load_it but because of its location
00120  * in the autonav code I had to split the 2 procedures up.
00121  * unload_it dumps all the goods from the ship to the harbor.
00122  * ONLY goods in the trade fields will be unloaded.
00123  * new autonav code
00124  * Chad Zabel 6/1/94  
00125  */
00126 void
00127 unload_it(struct shpstr *sp)
00128 {
00129     struct sctstr *sectp;
00130     int i;
00131     int landowner;
00132     int shipown;
00133     i_type comm;
00134     int sect_amt;
00135     int ship_amt;
00136     int max_amt;
00137 
00138     sectp = getsectp(sp->shp_x, sp->shp_y);
00139 
00140     landowner = sectp->sct_own;
00141     shipown = sp->shp_own;
00142 
00143     for (i = 0; i < TMAX; ++i) {
00144         if (sp->shp_tend[i] == I_NONE || sp->shp_lend[i] == 0)
00145             continue;
00146         if (landowner == 0)
00147             continue;
00148         if (sectp->sct_type != SCT_HARBR)
00149             continue;
00150 
00151         comm = sp->shp_tend[i];
00152         if (CANT_HAPPEN(comm <= I_NONE || comm > I_MAX))
00153             continue;
00154         ship_amt = sp->shp_item[comm];
00155         sect_amt = sectp->sct_item[comm];
00156 
00157         /* check for disloyal civilians */
00158         if (sectp->sct_oldown != shipown && comm == I_CIVIL) {
00159             wu(0, sp->shp_own,
00160                "Ship #%d - unable to unload civilians into a disloyal sector at %s.",
00161                sp->shp_uid, xyas(sectp->sct_x, sectp->sct_y,
00162                                  sectp->sct_own));
00163             continue;
00164         }
00165         if (comm == I_CIVIL)
00166             ship_amt--;         /* This leaves 1 civs on board the ship */
00167 
00168         max_amt = MIN(ship_amt, ITEM_MAX - sect_amt);
00169         if (max_amt <= 0)
00170             continue;
00171 
00172         sp->shp_item[comm] = ship_amt - max_amt;
00173         sectp->sct_item[comm] = sect_amt + max_amt;
00174 
00175         if (sectp->sct_pstage == PLG_INFECT && sp->shp_pstage == PLG_HEALTHY)
00176             sp->shp_pstage = PLG_EXPOSED;
00177         if (sp->shp_pstage == PLG_INFECT && sectp->sct_pstage == PLG_HEALTHY)
00178             sectp->sct_pstage = PLG_EXPOSED;
00179     }
00180 }
00181 
00182 /* auto_fuel_ship 
00183  * Assume a check for fuel=0 has already been made and passed.  
00184  * Try to fill a ship using petro. and then oil.            
00185  * new autonav code.
00186  * This should be merged with the fuel command someday. 
00187  * Chad Zabel 6/1/94
00188  */
00189 
00190 void
00191 auto_fuel_ship(struct shpstr *sp)
00192 {
00193     double d;
00194     int totalfuel = 0;
00195     int need;
00196     int maxfuel;
00197     int newfuel = 0;
00198     int add_fuel = 0;
00199 
00200     if (opt_FUEL == 0)
00201         return;
00202     getship(sp->shp_uid, sp);   /* refresh */
00203     /* fill with petrol */
00204     maxfuel = mchr[(int)sp->shp_type].m_fuelc;
00205     d = maxfuel / 5.0;
00206     if (d - (int)d > 0.0)
00207         d++;
00208     need = (int)d;
00209 
00210     newfuel = supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
00211                             I_PETROL, need);
00212     add_fuel += newfuel * 5;
00213     if (add_fuel > maxfuel)
00214         add_fuel = maxfuel;
00215     sp->shp_fuel += add_fuel;
00216     totalfuel += add_fuel;
00217 
00218     if (totalfuel == maxfuel) {
00219         putship(sp->shp_uid, sp);
00220         return;                 /* the ship is full */
00221     }
00222     add_fuel = 0;
00223     /* fill with oil */
00224     d = (maxfuel - totalfuel) / 50.0;
00225     if (d - (int)d > 0.0)
00226         d++;
00227     need = (int)d;
00228 
00229     newfuel = supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
00230                             I_OIL, need);
00231     add_fuel = newfuel * 50;
00232     if (add_fuel > maxfuel)
00233         add_fuel = maxfuel;
00234     sp->shp_fuel += add_fuel;
00235     putship(sp->shp_uid, sp);
00236 }

Generated on Fri Mar 28 11:01:16 2008 for empserver by  doxygen 1.5.2