]> git.pond.sub.org Git - empserver/blob - src/lib/commands/work.c
Fix work command not to spend more mobility than requested
[empserver] / src / lib / commands / work.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  work.c: Implementation of the work command
29  * 
30  *  Known contributors to this file:
31  *   
32  */
33
34 #include <config.h>
35
36 #include "commands.h"
37 #include "land.h"
38 #include "optlist.h"
39
40 static int buildeff(struct sctstr *, int, double *);
41
42 int
43 work(void)
44 {
45     int nunits;
46     struct nstr_item ni;
47     struct sctstr sect;
48     struct lndstr land;
49     int work_amt, eff_amt, w;
50     char *p;
51     char buf[1024];
52     double cost;
53     struct natstr *natp = getnatp(player->cnum);
54
55     if (!snxtitem(&ni, EF_LAND, player->argp[1], NULL))
56         return RET_SYN;
57     p = getstarg(player->argp[2], "Amount: ", buf);
58     if (p == 0 || *p == 0)
59         return RET_SYN;
60     work_amt = atoi(p);
61     if ((work_amt < 0) || (work_amt > land_mob_max)) {
62         pr("Mobility used must be from 0 to %d\n", land_mob_max);
63         return RET_FAIL;
64     }
65     nunits = 0;
66     while (nxtitem(&ni, &land)) {
67         if (!player->owner || land.lnd_own == 0)
68             continue;
69         if (!(lchr[(int)land.lnd_type].l_flags & L_ENGINEER))
70             continue;
71         if (land.lnd_mobil <= 0) {
72             pr("%s has no mobility!\n", prland(&land));
73             continue;
74         }
75         getsect(land.lnd_x, land.lnd_y, &sect);
76         if (sect.sct_effic >= 100 && sect.sct_type == sect.sct_newtype) {
77             pr("Nothing to do for %s in %s\n",
78                prland(&land), xyas(sect.sct_x, sect.sct_y, player->cnum));
79             continue;
80         }
81         eff_amt = MIN(land.lnd_mobil, work_amt);
82         w = (eff_amt * land.lnd_effic) / 600;
83         if (w < 1) {
84             pr("%s doesn't work enough to change efficiency (try increasing amount)\n",
85                prland(&land));
86             continue;
87         }
88         cost = 0.0;
89         w = buildeff(&sect, w, &cost);
90         if (player->dolcost + cost > natp->nat_money) {
91             pr("You can't afford to work that much in %s!\n",
92                xyas(land.lnd_x, land.lnd_y, player->cnum));
93             break;
94         }
95         player->dolcost += cost;
96         eff_amt = ((6 * w) / (land.lnd_effic / 100.0));
97         land.lnd_mission = 0;
98         land.lnd_mobil -= eff_amt;
99         nunits++;
100         pr("%s %s efficiency at %s to %d\n",
101            prland(&land),
102            sect.sct_type == sect.sct_newtype ? "raised" : "lowered",
103            xyas(land.lnd_x, land.lnd_y, player->cnum),
104            (int)sect.sct_effic);
105         putland(land.lnd_uid, &land);
106         putsect(&sect);
107     }
108     if (nunits == 0) {
109         if (player->argp[1])
110             pr("%s: No unit(s)\n", player->argp[1]);
111         else
112             pr("%s: No unit(s)\n", "");
113         return RET_FAIL;
114     } else
115         pr("%d unit%s\n", nunits, splur(nunits));
116     return RET_OK;
117 }
118
119 static int
120 buildeff(struct sctstr *sp, int work, double *money)
121 {
122     int work_cost;
123     int n, hcms, lcms;
124     int effdone = 0;
125
126     work_cost = 0;
127     if (sp->sct_type != sp->sct_newtype) {
128         /*
129          * Tear down existing sector.
130          * Easier to destroy than to build.
131          */
132         work_cost = (sp->sct_effic + 3) / 4;
133         if (work_cost > work)
134             work_cost = work;
135         n = sp->sct_effic - work_cost * 4;
136         if (n <= 0) {
137             n = 0;
138             sp->sct_type = sp->sct_newtype;
139         }
140         sp->sct_effic = n;
141         work -= work_cost;
142         *money += work_cost;
143         effdone += work_cost;
144     }
145     if (sp->sct_type == sp->sct_newtype) {
146         work_cost = 100 - sp->sct_effic;
147         if (work_cost > work)
148             work_cost = work;
149
150         if (dchr[sp->sct_type].d_lcms > 0) {
151             lcms = sp->sct_item[I_LCM];
152             lcms /= dchr[sp->sct_type].d_lcms;
153             if (work_cost > lcms)
154                 work_cost = lcms;
155         }
156         if (dchr[sp->sct_type].d_hcms > 0) {
157             hcms = sp->sct_item[I_HCM];
158             hcms /= dchr[sp->sct_type].d_hcms;
159             if (work_cost > hcms)
160                 work_cost = hcms;
161         }
162
163         sp->sct_effic += work_cost;
164         *money += work_cost * dchr[sp->sct_type].d_build;
165
166         if ((dchr[sp->sct_type].d_lcms > 0) ||
167             (dchr[sp->sct_type].d_hcms > 0)) {
168             sp->sct_item[I_LCM] -= work_cost * dchr[sp->sct_type].d_lcms;
169             sp->sct_item[I_HCM] -= work_cost * dchr[sp->sct_type].d_hcms;
170         }
171         effdone += work_cost;
172     }
173     return effdone;
174 }