src/lib/commands/work.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  *  work.c: Implementation of the work command
00029  * 
00030  *  Known contributors to this file:
00031  *   
00032  */
00033 
00034 #include <config.h>
00035 
00036 #include "commands.h"
00037 #include "land.h"
00038 #include "optlist.h"
00039 
00040 static int buildeff(struct sctstr *, int, double *);
00041 
00042 int
00043 work(void)
00044 {
00045     int nunits;
00046     struct nstr_item ni;
00047     struct sctstr sect;
00048     struct lndstr land;
00049     int work_amt, eff_amt, w;
00050     char *p;
00051     coord donex = 0, doney = 1;
00052     char buf[1024];
00053 
00054     if (!snxtitem(&ni, EF_LAND, player->argp[1]))
00055         return RET_SYN;
00056     p = getstarg(player->argp[2], "Amount: ", buf);
00057     if (p == 0 || *p == 0)
00058         return RET_SYN;
00059     work_amt = atoi(p);
00060     if ((work_amt < 0) || (work_amt > land_mob_max)) {
00061         pr("Mobility used must be from 0 to %d\n", land_mob_max);
00062         return RET_FAIL;
00063     }
00064     nunits = 0;
00065     while (nxtitem(&ni, &land)) {
00066         if (!player->owner || land.lnd_own == 0)
00067             continue;
00068         if (!(lchr[(int)land.lnd_type].l_flags & L_ENGINEER))
00069             continue;
00070         if (land.lnd_mobil <= 0) {
00071             pr("%s has no mobility!\n", prland(&land));
00072             continue;
00073         }
00074         getsect(land.lnd_x, land.lnd_y, &sect);
00075         if (sect.sct_effic >= 100 && sect.sct_type == sect.sct_newtype) {
00076             if (sect.sct_x != donex || sect.sct_y != doney)
00077                 pr("%s is %d%% efficient\n",
00078                    xyas(sect.sct_x, sect.sct_y, player->cnum),
00079                    sect.sct_effic);
00080             /* not perfect, but it'll do -KHS */
00081             donex = sect.sct_x;
00082             doney = sect.sct_y;
00083             continue;
00084         }
00085         eff_amt = MIN(land.lnd_mobil, work_amt);
00086         w = ldround(((double)eff_amt * land.lnd_effic / 600.0), 1);
00087         if (w < 1) {
00088             pr("%s doesn't work enough to change efficiency (try increasing amount)\n",
00089                prland(&land));
00090             continue;
00091         }
00092         nunits++;
00093         eff_amt = ((6 * buildeff(&sect, w, &player->dolcost)) /
00094                    (land.lnd_effic / 100.0));
00095         land.lnd_mission = 0;
00096         land.lnd_mobil -= eff_amt;
00097         pr("%s %s efficiency at %s to %d\n",
00098            prland(&land),
00099            sect.sct_type == sect.sct_newtype ? "raised" : "lowered",
00100            xyas(land.lnd_x, land.lnd_y, player->cnum),
00101            (int)sect.sct_effic);
00102         putland(land.lnd_uid, &land);
00103         putsect(&sect);
00104     }
00105     if (nunits == 0) {
00106         if (player->argp[1])
00107             pr("%s: No unit(s)\n", player->argp[1]);
00108         else
00109             pr("%s: No unit(s)\n", "");
00110         return RET_FAIL;
00111     } else
00112         pr("%d unit%s\n", nunits, splur(nunits));
00113     return RET_OK;
00114 }
00115 
00116 static int
00117 buildeff(struct sctstr *sp, int work, double *money)
00118 {
00119     int work_cost;
00120     int n, hcms, lcms;
00121     int effdone = 0;
00122 
00123     work_cost = 0;
00124     if (sp->sct_type != sp->sct_newtype) {
00125         /*
00126          * Tear down existing sector.
00127          * Easier to destroy than to build.
00128          */
00129         work_cost = (sp->sct_effic + 3) / 4;
00130         if (work_cost > work)
00131             work_cost = work;
00132         n = sp->sct_effic - work_cost * 4;
00133         if (n <= 0) {
00134             n = 0;
00135             sp->sct_type = sp->sct_newtype;
00136         }
00137         sp->sct_effic = n;
00138         work -= work_cost;
00139         *money += work_cost;
00140         effdone += work_cost;
00141     }
00142     if (sp->sct_type == sp->sct_newtype) {
00143         work_cost = 100 - sp->sct_effic;
00144         if (work_cost > work)
00145             work_cost = work;
00146 
00147         if (dchr[sp->sct_type].d_lcms > 0) {
00148             lcms = sp->sct_item[I_LCM];
00149             lcms /= dchr[sp->sct_type].d_lcms;
00150             if (work_cost > lcms)
00151                 work_cost = lcms;
00152         }
00153         if (dchr[sp->sct_type].d_hcms > 0) {
00154             hcms = sp->sct_item[I_HCM];
00155             hcms /= dchr[sp->sct_type].d_hcms;
00156             if (work_cost > hcms)
00157                 work_cost = hcms;
00158         }
00159 
00160         sp->sct_effic += work_cost;
00161         *money += work_cost * dchr[sp->sct_type].d_build;
00162 
00163         if ((dchr[sp->sct_type].d_lcms > 0) ||
00164             (dchr[sp->sct_type].d_hcms > 0)) {
00165             sp->sct_item[I_LCM] -= work_cost * dchr[sp->sct_type].d_lcms;
00166             sp->sct_item[I_HCM] -= work_cost * dchr[sp->sct_type].d_hcms;
00167         }
00168         effdone += work_cost;
00169     }
00170     return effdone;
00171 }

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