src/lib/update/plane.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  *  plane.c: Do production for planes
00029  * 
00030  *  Known contributors to this file:
00031  *     Dave Pare, 1986
00032  *     Steve McClure, 1998
00033  *     Markus Armbruster, 2006-2007
00034  */
00035 
00036 #include <config.h>
00037 
00038 #include "budg.h"
00039 #include "lost.h"
00040 #include "plane.h"
00041 #include "player.h"
00042 #include "ship.h"
00043 #include "update.h"
00044 
00045 static void planerepair(struct plnstr *, struct natstr *, struct bp *, int);
00046 static void upd_plane(struct plnstr *, int, struct natstr *, struct bp *, int);
00047 
00048 int
00049 prod_plane(int etus, int natnum, struct bp *bp, int buildem)
00050                  /* Build = 1, maintain =0 */
00051 {
00052     struct plnstr *pp;
00053     struct natstr *np;
00054     int n, k = 0;
00055     int start_money;
00056 
00057     for (n = 0; NULL != (pp = getplanep(n)); n++) {
00058         if (pp->pln_own == 0)
00059             continue;
00060         if (pp->pln_own != natnum)
00061             continue;
00062         if (pp->pln_effic < PLANE_MINEFF) {
00063             makelost(EF_PLANE, pp->pln_own, pp->pln_uid,
00064                      pp->pln_x, pp->pln_y);
00065             pp->pln_own = 0;
00066             continue;
00067         }
00068 
00069         if (pp->pln_flags & PLN_LAUNCHED) {
00070             if (!player->simulation && buildem == 0
00071                 && !(pp->pln_flags & PLN_SYNCHRONOUS))
00072                 move_sat(pp);
00073             continue;
00074         }
00075 
00076         np = getnatp(pp->pln_own);
00077         start_money = np->nat_money;
00078         upd_plane(pp, etus, np, bp, buildem);
00079         air_money[pp->pln_own] += np->nat_money - start_money;
00080         if (buildem == 0 || np->nat_money != start_money)
00081             k++;
00082         if (player->simulation)
00083             np->nat_money = start_money;
00084     }
00085 
00086     return k;
00087 }
00088 
00089 static void
00090 upd_plane(struct plnstr *pp, int etus,
00091           struct natstr *np, struct bp *bp, int build)
00092 {
00093     struct plchrstr *pcp = &plchr[(int)pp->pln_type];
00094     int mult, cost, eff;
00095 
00096     if (build == 1) {
00097         if (!pp->pln_off && np->nat_money >= 0)
00098             planerepair(pp, np, bp, etus);
00099         if (!player->simulation)
00100             pp->pln_off = 0;
00101     } else {
00102         mult = 1;
00103         if (np->nat_level[NAT_TLEV] < pp->pln_tech * 0.85)
00104             mult = 2;
00105         cost = -(mult * etus * MIN(0.0, pcp->pl_cost * money_plane));
00106         if (np->nat_money < cost && !player->simulation) {
00107             if ((eff = pp->pln_effic - etus / 5) < PLANE_MINEFF) {
00108                 wu(0, pp->pln_own,
00109                    "%s lost to lack of maintenance\n", prplane(pp));
00110                 makelost(EF_PLANE, pp->pln_own, pp->pln_uid,
00111                          pp->pln_x, pp->pln_y);
00112                 pp->pln_own = 0;
00113                 return;
00114             }
00115             wu(0, pp->pln_own,
00116                "%s lost %d%% to lack of maintenance\n",
00117                prplane(pp), pp->pln_effic - eff);
00118             pp->pln_effic = eff;
00119         } else {
00120             np->nat_money -= cost;
00121         }
00122         /* flight pay is 5x the pay received by other military */
00123         np->nat_money += etus * pcp->pl_crew * money_mil * 5;
00124     }
00125 }
00126 
00127 static void
00128 planerepair(struct plnstr *pp, struct natstr *np, struct bp *bp, int etus)
00129 {
00130     int build;
00131     int mvec[I_MAX + 1];
00132     struct shpstr *carrier;
00133     struct plchrstr *pcp = &plchr[(int)pp->pln_type];
00134     struct sctstr *sp = getsectp(pp->pln_x, pp->pln_y);
00135     int delta;
00136     int mult;
00137     int avail;
00138     int w_p_eff;
00139     int used;
00140 
00141     carrier = NULL;
00142     if (pp->pln_ship >= 0) {
00143         if (pp->pln_effic >= 80)
00144             return;
00145         carrier = getshipp(pp->pln_ship);
00146         if (CANT_HAPPEN(!carrier))
00147             return;
00148         if (carrier->shp_off)
00149             return;
00150         if ((carrier->shp_own != pp->pln_own) &&
00151             (getrel(getnatp(carrier->shp_own), pp->pln_own) != ALLIED))
00152             return;
00153     } else {
00154         if ((sp->sct_own != pp->pln_own) &&
00155             (getrel(getnatp(sp->sct_own), pp->pln_own) != ALLIED))
00156             return;
00157     }
00158 
00159     if (sp->sct_off)
00160         return;
00161     mult = 1;
00162     if (np->nat_level[NAT_TLEV] < pp->pln_tech * 0.85)
00163         mult = 2;
00164 
00165     if (pp->pln_effic == 100)
00166         return;
00167 
00168     if (!player->simulation)
00169         avail = sp->sct_avail * 100;
00170     else
00171         avail = bp_get_avail(bp, sp) * 100;
00172     if (carrier)
00173         avail += etus * carrier->shp_item[I_MILIT] / 2;
00174 
00175     w_p_eff = PLN_BLD_WORK(pcp->pl_lcm, pcp->pl_hcm);
00176     delta = roundavg((double)avail / w_p_eff);
00177     if (delta <= 0)
00178         return;
00179     if (delta > (int)((float)etus * plane_grow_scale))
00180         delta = (int)((float)etus * plane_grow_scale);
00181     if (delta > 100 - pp->pln_effic)
00182         delta = 100 - pp->pln_effic;
00183 
00184     memset(mvec, 0, sizeof(mvec));
00185     mvec[I_MILIT] =  pcp->pl_crew;
00186     mvec[I_LCM] =  pcp->pl_lcm;
00187     mvec[I_HCM] = pcp->pl_hcm;
00188     build = get_materials(sp, bp, mvec, delta);
00189 
00190     if (carrier)
00191         build = delta;
00192 
00193     used = build * w_p_eff;
00194     /*
00195      * I didn't use roundavg here, because I want to
00196      * penalize the player with a large number of planes.
00197      */
00198     if (!player->simulation)
00199         avail = (sp->sct_avail * 100 - used) / 100;
00200     else
00201         avail = (bp_get_avail(bp, sp) * 100 - used) / 100;
00202 
00203     if (avail < 0)
00204         avail = 0;
00205     if (!player->simulation)
00206         sp->sct_avail = avail;
00207     else
00208         bp_put_avail(bp, sp, avail);
00209 
00210     if (sp->sct_type != SCT_AIRPT)
00211         build /= 3;
00212     if (carrier) {
00213         if ((pp->pln_effic + build) > 80)
00214             build = 80 - pp->pln_effic;
00215     }
00216 
00217     np->nat_money -= mult * build * pcp->pl_cost / 100.0;
00218 
00219     if (!player->simulation)
00220         pp->pln_effic += (signed char)build;
00221 }

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