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