]> git.pond.sub.org Git - empserver/blob - src/lib/commands/work.c
neweff prod work: Use update code instead of duplicating it
[empserver] / src / lib / commands / work.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, 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-2016
31  */
32
33 #include <config.h>
34
35 #include "chance.h"
36 #include "commands.h"
37 #include "land.h"
38 #include "optlist.h"
39
40 int
41 work(void)
42 {
43     int nunits;
44     struct nstr_item ni;
45     struct sctstr sect;
46     struct lndstr land;
47     int work_amt, eff_amt, w, sect_avail;
48     char *p;
49     char buf[1024];
50     double cost;
51     struct natstr *natp = getnatp(player->cnum);
52
53     if (!snxtitem(&ni, EF_LAND, player->argp[1], NULL))
54         return RET_SYN;
55     p = getstarg(player->argp[2], "Amount: ", buf);
56     if (!p || !*p)
57         return RET_SYN;
58     work_amt = atoi(p);
59     if ((work_amt < 0) || (work_amt > land_mob_max)) {
60         pr("Mobility used must be from 0 to %d\n", land_mob_max);
61         return RET_FAIL;
62     }
63     nunits = 0;
64     while (nxtitem(&ni, &land)) {
65         if (!player->owner || land.lnd_own == 0)
66             continue;
67         if (!(lchr[(int)land.lnd_type].l_flags & L_ENGINEER))
68             continue;
69         if (land.lnd_mobil <= 0) {
70             pr("%s has no mobility!\n", prland(&land));
71             continue;
72         }
73         getsect(land.lnd_x, land.lnd_y, &sect);
74         if (sect.sct_effic >= 100 && sect.sct_type == sect.sct_newtype) {
75             pr("Nothing to do for %s in %s\n",
76                prland(&land), xyas(sect.sct_x, sect.sct_y, player->cnum));
77             continue;
78         }
79         eff_amt = MIN(land.lnd_mobil, work_amt);
80         w = (eff_amt * land.lnd_effic) / 600;
81         if (w < 1) {
82             pr("%s doesn't work enough to change efficiency (try increasing amount)\n",
83                prland(&land));
84             continue;
85         }
86         sect_avail = sect.sct_avail;
87         sect.sct_avail = w;
88         cost = buildeff(&sect);
89         w = sect.sct_avail - sect_avail;
90         sect.sct_avail = sect_avail;
91         if (w == 0) {
92             pr("%s can't change efficiency in %s\n",
93                prland(&land), xyas(land.lnd_x, land.lnd_y, player->cnum));
94             continue;
95         }
96         if (player->dolcost + cost > natp->nat_money) {
97             pr("You can't afford to work that much in %s!\n",
98                xyas(land.lnd_x, land.lnd_y, player->cnum));
99             break;
100         }
101         player->dolcost += cost;
102         land.lnd_mission = 0;
103         land.lnd_mobil -= roundavg(w * 600.0 / land.lnd_effic);
104         nunits++;
105         pr("%s %s efficiency at %s to %d\n",
106            prland(&land),
107            sect.sct_type == sect.sct_newtype ? "raised" : "lowered",
108            xyas(land.lnd_x, land.lnd_y, player->cnum),
109            sect.sct_effic);
110         putland(land.lnd_uid, &land);
111         putsect(&sect);
112     }
113     if (nunits == 0) {
114         if (player->argp[1])
115             pr("%s: No unit(s)\n", player->argp[1]);
116         else
117             pr("%s: No unit(s)\n", "");
118         return RET_FAIL;
119     } else
120         pr("%d unit%s\n", nunits, splur(nunits));
121     return RET_OK;
122 }