src/lib/commands/upgr.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  *  upgr.c: Upgrade tech of ships/planes/units
00029  * 
00030  *  Known contributors to this file:
00031  *     Dave Pare, 1986
00032  *     Steve McClure, 1996-2000
00033  */
00034 
00035 #include <config.h>
00036 
00037 #include "commands.h"
00038 #include "land.h"
00039 #include "plane.h"
00040 #include "ship.h"
00041 
00042 enum {
00043   UPGR_COST = 15,               /* how much avail and money to charge */
00044   UPGR_EFF = 35                 /* efficiency reduction */
00045 };
00046 
00047 static int lupgr(void);
00048 static int pupgr(void);
00049 static int supgr(void);
00050 
00051 int
00052 upgr(void)
00053 {
00054     char *p;
00055     char buf[1024];
00056 
00057     if (!(p = getstarg(player->argp[1], "Ship, land, or plane? ", buf)))
00058         return RET_SYN;
00059     switch (*p) {
00060     case 's':
00061     case 'S':
00062         return supgr();
00063     case 'p':
00064     case 'P':
00065         return pupgr();
00066     case 'l':
00067     case 'L':
00068         return lupgr();
00069     default:
00070         break;
00071     }
00072     pr("Ships, land units or planes only!\n");
00073     return RET_SYN;
00074 }
00075 
00076 static int
00077 lupgr(void)
00078 {
00079     struct sctstr sect;
00080     struct natstr *natp;
00081     struct nstr_item ni;
00082     struct lndstr land;
00083     struct lchrstr *lp;
00084     int n;
00085     int tlev;
00086     int avail, cost;
00087     int rel;
00088     long cash;
00089 
00090     if (!snxtitem(&ni, EF_LAND, player->argp[2]))
00091         return RET_SYN;
00092     natp = getnatp(player->cnum);
00093     cash = natp->nat_money;
00094     tlev = (int)natp->nat_level[NAT_TLEV];
00095     n = 0;
00096     while (nxtitem(&ni, &land)) {
00097         if (land.lnd_own == 0)
00098             continue;
00099         getsect(land.lnd_x, land.lnd_y, &sect);
00100         if (sect.sct_own != player->cnum)
00101             continue;
00102         if (sect.sct_type != SCT_HEADQ || sect.sct_effic < 60)
00103             continue;
00104         rel = getrel(getnatp(land.lnd_own), sect.sct_own);
00105         if (rel < FRIENDLY && sect.sct_own != land.lnd_own) {
00106             pr("You are not on friendly terms with the owner of unit %d!\n",
00107                land.lnd_uid);
00108             continue;
00109         }
00110         n++;
00111         lp = &lchr[(int)land.lnd_type];
00112         avail = (LND_BLD_WORK(lp->l_lcm, lp->l_hcm) * UPGR_COST + 99) / 100;
00113         if (sect.sct_avail < avail) {
00114             pr("Not enough available work in %s to upgrade a %s\n",
00115                xyas(sect.sct_x, sect.sct_y, player->cnum), lp->l_name);
00116             pr(" (%d available work required)\n", avail);
00117             continue;
00118         }
00119         if (land.lnd_effic < 60) {
00120             pr("%s is too damaged to upgrade!\n", prland(&land));
00121             continue;
00122         }
00123         if (land.lnd_tech >= tlev) {
00124             pr("%s tech: %d, yours is only %d\n",
00125                prland(&land), land.lnd_tech, tlev);
00126             continue;
00127         }
00128         cost = lp->l_cost * UPGR_COST / 100;
00129         if (cost + player->dolcost > cash) {
00130             pr("You don't have enough money to upgrade %s!\n",
00131                prland(&land));
00132             continue;
00133         }
00134 
00135         sect.sct_avail -= avail;
00136         land.lnd_effic -= UPGR_EFF;
00137         lnd_set_tech(&land, tlev);
00138         land.lnd_harden = 0;
00139         land.lnd_mission = 0;
00140 
00141         putland(land.lnd_uid, &land);
00142         putsect(&sect);
00143         player->dolcost += cost;
00144         pr("%s upgraded to tech %d, at a cost of %d\n",
00145            prland(&land), land.lnd_tech, cost);
00146         if (land.lnd_own != player->cnum)
00147             wu(0, land.lnd_own,
00148                "%s upgraded by %s to tech %d, at a cost of %d\n",
00149                prland(&land), cname(player->cnum), land.lnd_tech,
00150                cost);
00151     }
00152     if (n == 0) {
00153         pr("No land units\n");
00154         return RET_SYN;
00155     }
00156     return RET_OK;
00157 }
00158 
00159 static int
00160 supgr(void)
00161 {
00162     struct sctstr sect;
00163     struct natstr *natp;
00164     struct nstr_item ni;
00165     struct shpstr ship;
00166     struct mchrstr *mp;
00167     int n;
00168     int tlev;
00169     int avail, cost;
00170     int rel;
00171     long cash;
00172 
00173     if (!snxtitem(&ni, EF_SHIP, player->argp[2]))
00174         return RET_SYN;
00175     natp = getnatp(player->cnum);
00176     cash = natp->nat_money;
00177     tlev = (int)natp->nat_level[NAT_TLEV];
00178     n = 0;
00179     while (nxtitem(&ni, &ship)) {
00180         if (ship.shp_own == 0)
00181             continue;
00182         getsect(ship.shp_x, ship.shp_y, &sect);
00183         if (sect.sct_own != player->cnum)
00184             continue;
00185         if (sect.sct_type != SCT_HARBR || sect.sct_effic < 60)
00186             continue;
00187         rel = getrel(getnatp(ship.shp_own), sect.sct_own);
00188         if (rel < FRIENDLY && sect.sct_own != ship.shp_own) {
00189             pr("You are not on friendly terms with the owner of ship %d!\n",
00190                ship.shp_uid);
00191             continue;
00192         }
00193         n++;
00194         mp = &mchr[(int)ship.shp_type];
00195         avail = (SHP_BLD_WORK(mp->m_lcm, mp->m_hcm) * UPGR_COST + 99) / 100;
00196         if (sect.sct_avail < avail) {
00197             pr("Not enough available work in %s to upgrade a %s\n",
00198                xyas(sect.sct_x, sect.sct_y, player->cnum), mp->m_name);
00199             pr(" (%d available work required)\n", avail);
00200             continue;
00201         }
00202         if (ship.shp_effic < 60) {
00203             pr("%s is too damaged to upgrade!\n", prship(&ship));
00204             continue;
00205         }
00206         if (ship.shp_tech >= tlev) {
00207             pr("%s tech: %d, yours is only %d\n",
00208                prship(&ship), ship.shp_tech, tlev);
00209             continue;
00210         }
00211         cost = mp->m_cost * UPGR_COST / 100;
00212         if (cost + player->dolcost > cash) {
00213             pr("You don't have enough money to upgrade %s!\n",
00214                prship(&ship));
00215             continue;
00216         }
00217 
00218         sect.sct_avail -= avail;
00219         ship.shp_effic -= UPGR_EFF;
00220         shp_set_tech(&ship, tlev);
00221         ship.shp_mission = 0;
00222 
00223         putship(ship.shp_uid, &ship);
00224         putsect(&sect);
00225         player->dolcost += cost;
00226         pr("%s upgraded to tech %d, at a cost of %d\n",
00227            prship(&ship), ship.shp_tech, cost);
00228         if (ship.shp_own != player->cnum)
00229             wu(0, ship.shp_own,
00230                "%s upgraded by %s to tech %d, at a cost of %d\n",
00231                prship(&ship), cname(player->cnum), ship.shp_tech,
00232                cost);
00233     }
00234     if (n == 0) {
00235         pr("No ships\n");
00236         return RET_SYN;
00237     }
00238     return RET_OK;
00239 }
00240 
00241 static int
00242 pupgr(void)
00243 {
00244     struct sctstr sect;
00245     struct natstr *natp;
00246     struct nstr_item ni;
00247     struct plnstr plane;
00248     struct plchrstr *pp;
00249     int n;
00250     int tlev;
00251     int avail, cost;
00252     int rel;
00253     long cash;
00254 
00255     if (!snxtitem(&ni, EF_PLANE, player->argp[2]))
00256         return RET_SYN;
00257     natp = getnatp(player->cnum);
00258     cash = natp->nat_money;
00259     tlev = (int)natp->nat_level[NAT_TLEV];
00260     n = 0;
00261     while (nxtitem(&ni, &plane)) {
00262         if (plane.pln_own == 0)
00263             continue;
00264         getsect(plane.pln_x, plane.pln_y, &sect);
00265         if (sect.sct_own != player->cnum)
00266             continue;
00267         if (sect.sct_type != SCT_AIRPT || sect.sct_effic < 60)
00268             continue;
00269         rel = getrel(getnatp(plane.pln_own), sect.sct_own);
00270         if (rel < FRIENDLY && sect.sct_own != plane.pln_own) {
00271             pr("You are not on friendly terms with the owner of plane %d!\n",
00272                plane.pln_uid);
00273             continue;
00274         }
00275         n++;
00276         pp = &plchr[(int)plane.pln_type];
00277         avail = (PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm) * UPGR_COST + 99) / 100;
00278         if (sect.sct_avail < avail) {
00279             pr("Not enough available work in %s to upgrade a %s\n",
00280                xyas(sect.sct_x, sect.sct_y, player->cnum), pp->pl_name);
00281             pr(" (%d available work required)\n", avail);
00282             continue;
00283         }
00284         if (plane.pln_effic < 60) {
00285             pr("%s is too damaged to upgrade!\n", prplane(&plane));
00286             continue;
00287         }
00288         if (plane.pln_tech >= tlev) {
00289             pr("%s tech: %d, yours is only %d\n",
00290                prplane(&plane), plane.pln_tech, tlev);
00291             continue;
00292         }
00293         cost = pp->pl_cost * UPGR_COST / 100;
00294         if (cost + player->dolcost > cash) {
00295             pr("You don't have enough money to upgrade %s!\n",
00296                prplane(&plane));
00297             continue;
00298         }
00299         if (plane.pln_flags & PLN_LAUNCHED) {
00300             pr("Plane %s is in orbit!\n", prplane(&plane));
00301             continue;
00302         }
00303 
00304         sect.sct_avail -= avail;
00305         plane.pln_effic -= UPGR_EFF;
00306         pln_set_tech(&plane, tlev);
00307         plane.pln_harden = 0;
00308         plane.pln_mission = 0;
00309 
00310         putplane(plane.pln_uid, &plane);
00311         putsect(&sect);
00312         player->dolcost += cost;
00313         pr("%s upgraded to tech %d, at a cost of %d\n",
00314            prplane(&plane), plane.pln_tech, cost);
00315         if (plane.pln_own != player->cnum)
00316             wu(0, plane.pln_own,
00317                "%s upgraded by %s to tech %d, at a cost of %d\n",
00318                prplane(&plane), cname(player->cnum), plane.pln_tech,
00319                cost);
00320     }
00321     if (n == 0) {
00322         pr("No planes.\n");
00323         return RET_SYN;
00324     }
00325     return RET_OK;
00326 }

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