src/lib/commands/improve.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  *  improve.c: Improve the infrastructure of a sector
00029  * 
00030  *  Known contributors to this file:
00031  *     Steve McClure, 1996-2000
00032  */
00033 
00034 #include <config.h>
00035 
00036 #include "commands.h"
00037 #include "optlist.h"
00038 #include "path.h"
00039 
00040 int
00041 improve(void)
00042 {
00043     struct sctstr sect;
00044     int nsect;
00045     struct nstr_sect nstr;
00046     char *p;
00047     char buf[1024];
00048     char prompt[128];
00049     int type;
00050     int value;
00051     int ovalue;
00052     int maxup;
00053     struct natstr *natp;
00054     int lneeded;
00055     int hneeded;
00056     int mneeded;
00057     int dneeded;
00058     int wanted;
00059 
00060     p = getstarg(player->argp[1],
00061                  "Improve what ('road', 'rail' or 'defense')? ",
00062                  buf);
00063     if (!p || !*p)
00064         return RET_SYN;
00065     if (!strncmp(p, "ro", 2))
00066         type = INT_ROAD;
00067     else if (!strncmp(p, "ra", 2))
00068         type = INT_RAIL;
00069     else if (!strncmp(p, "de", 2)) {
00070         type = INT_DEF;
00071     } else
00072         return RET_SYN;
00073 
00074     if (!intrchr[type].in_enable) {
00075         pr("%s improvement is disabled.\n", intrchr[type].in_name);
00076         return RET_FAIL;
00077     }
00078 
00079     if (!snxtsct(&nstr, player->argp[2]))
00080         return RET_SYN;
00081     prdate();
00082     nsect = 0;
00083     while (nxtsct(&nstr, &sect)) {
00084         if (!player->owner)
00085             continue;
00086         if (type == INT_ROAD)
00087             value = sect.sct_road;
00088         else if (type == INT_RAIL)
00089             value = sect.sct_rail;
00090         else /* type == INT_DEF */
00091             value = sect.sct_defense;
00092         sprintf(prompt, "Sector %s has a %s of %d%%.  Improve how much? ",
00093                 xyas(sect.sct_x, sect.sct_y, player->cnum),
00094                 intrchr[type].in_name, value);
00095         if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
00096             continue;
00097         if (!check_sect_ok(&sect))
00098             continue;
00099         maxup = 100 - value;
00100         wanted = atoi(p);
00101         if (wanted < 0)
00102             continue;
00103         if (wanted < maxup)
00104             maxup = wanted;
00105         if (!maxup)
00106             continue;
00107         lneeded = intrchr[type].in_lcms * maxup;
00108         if (sect.sct_item[I_LCM] < lneeded) {
00109             lneeded = sect.sct_item[I_LCM];
00110             maxup = lneeded / intrchr[type].in_lcms;
00111             if (maxup <= 0) {
00112                 pr("Not enough lcms in %s\n",
00113                    xyas(sect.sct_x, sect.sct_y, player->cnum));
00114                 continue;
00115             }
00116         }
00117         hneeded = intrchr[type].in_hcms * maxup;
00118         if (sect.sct_item[I_HCM] < hneeded) {
00119             hneeded = sect.sct_item[I_HCM];
00120             maxup = hneeded / intrchr[type].in_hcms;
00121             if (maxup <= 0) {
00122                 pr("Not enough hcms in %s\n",
00123                    xyas(sect.sct_x, sect.sct_y, player->cnum));
00124                 continue;
00125             }
00126         }
00127         mneeded = intrchr[type].in_mcost * maxup;
00128         if ((sect.sct_mobil - 1) < mneeded) {
00129             mneeded = sect.sct_mobil - 1;
00130             if (mneeded < 0)
00131                 mneeded = 0;
00132             maxup = mneeded / intrchr[type].in_mcost;
00133             if (maxup <= 0) {
00134                 pr("Not enough mobility in %s\n",
00135                    xyas(sect.sct_x, sect.sct_y, player->cnum));
00136                 continue;
00137             }
00138         }
00139         dneeded = intrchr[type].in_dcost * maxup;
00140         natp = getnatp(player->cnum);
00141         if ((natp->nat_money - 1) < dneeded) {
00142 /* Nasty - leave 'em with a buck. :) */
00143             dneeded = natp->nat_money - 1;
00144             if (dneeded < 0)
00145                 dneeded = 0;
00146             maxup = dneeded / intrchr[type].in_dcost;
00147             if (maxup <= 0) {
00148                 pr("Not enough money left to improve %s\n",
00149                    xyas(sect.sct_x, sect.sct_y, player->cnum));
00150                 continue;
00151             }
00152         }
00153         if (maxup <= 0)
00154             continue;
00155         lneeded = intrchr[type].in_lcms * maxup;
00156         hneeded = intrchr[type].in_hcms * maxup;
00157         mneeded = intrchr[type].in_mcost * maxup;
00158         dneeded = intrchr[type].in_dcost * maxup;
00159         player->dolcost += dneeded;
00160         sect.sct_item[I_LCM] -= lneeded;
00161         sect.sct_item[I_HCM] -= hneeded;
00162         sect.sct_mobil -= mneeded;
00163         ovalue = value;
00164         value += maxup;
00165         if (CANT_HAPPEN(value > 100))
00166             value = 100;
00167         pr("Sector %s %s increased from %d%% to %d%%\n",
00168            xyas(sect.sct_x, sect.sct_y, player->cnum),
00169            intrchr[type].in_name, ovalue, value);
00170         if (type == INT_ROAD)
00171             sect.sct_road = value;
00172         else if (type == INT_RAIL)
00173             sect.sct_rail = value;
00174         else if (type == INT_DEF)
00175             sect.sct_defense = value;
00176         putsect(&sect);
00177         nsect++;
00178     }
00179     if (nsect == 0) {
00180         if (player->argp[2])
00181             pr("%s: No sector(s)\n", player->argp[1]);
00182         else
00183             pr("%s: No sector(s)\n", "");
00184         return RET_FAIL;
00185     } else
00186         pr("%d sector%s\n", nsect, splur(nsect));
00187     return 0;
00188 }

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