]> git.pond.sub.org Git - empserver/blob - src/lib/commands/work.c
Improve work()'s message when sector doesn't need construction
[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
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 == 0 || *p == 0)
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 = ldround(((double)eff_amt * land.lnd_effic / 600.0), 1);
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         nunits++;
87         eff_amt = ((6 * buildeff(&sect, w, &player->dolcost)) /
88                    (land.lnd_effic / 100.0));
89         land.lnd_mission = 0;
90         land.lnd_mobil -= eff_amt;
91         pr("%s %s efficiency at %s to %d\n",
92            prland(&land),
93            sect.sct_type == sect.sct_newtype ? "raised" : "lowered",
94            xyas(land.lnd_x, land.lnd_y, player->cnum),
95            (int)sect.sct_effic);
96         putland(land.lnd_uid, &land);
97         putsect(&sect);
98     }
99     if (nunits == 0) {
100         if (player->argp[1])
101             pr("%s: No unit(s)\n", player->argp[1]);
102         else
103             pr("%s: No unit(s)\n", "");
104         return RET_FAIL;
105     } else
106         pr("%d unit%s\n", nunits, splur(nunits));
107     return RET_OK;
108 }
109
110 static int
111 buildeff(struct sctstr *sp, int work, double *money)
112 {
113     int work_cost;
114     int n, hcms, lcms;
115     int effdone = 0;
116
117     work_cost = 0;
118     if (sp->sct_type != sp->sct_newtype) {
119         /*
120          * Tear down existing sector.
121          * Easier to destroy than to build.
122          */
123         work_cost = (sp->sct_effic + 3) / 4;
124         if (work_cost > work)
125             work_cost = work;
126         n = sp->sct_effic - work_cost * 4;
127         if (n <= 0) {
128             n = 0;
129             sp->sct_type = sp->sct_newtype;
130         }
131         sp->sct_effic = n;
132         work -= work_cost;
133         *money += work_cost;
134         effdone += work_cost;
135     }
136     if (sp->sct_type == sp->sct_newtype) {
137         work_cost = 100 - sp->sct_effic;
138         if (work_cost > work)
139             work_cost = work;
140
141         if (dchr[sp->sct_type].d_lcms > 0) {
142             lcms = sp->sct_item[I_LCM];
143             lcms /= dchr[sp->sct_type].d_lcms;
144             if (work_cost > lcms)
145                 work_cost = lcms;
146         }
147         if (dchr[sp->sct_type].d_hcms > 0) {
148             hcms = sp->sct_item[I_HCM];
149             hcms /= dchr[sp->sct_type].d_hcms;
150             if (work_cost > hcms)
151                 work_cost = hcms;
152         }
153
154         sp->sct_effic += work_cost;
155         *money += work_cost * dchr[sp->sct_type].d_build;
156
157         if ((dchr[sp->sct_type].d_lcms > 0) ||
158             (dchr[sp->sct_type].d_hcms > 0)) {
159             sp->sct_item[I_LCM] -= work_cost * dchr[sp->sct_type].d_lcms;
160             sp->sct_item[I_HCM] -= work_cost * dchr[sp->sct_type].d_hcms;
161         }
162         effdone += work_cost;
163     }
164     return effdone;
165 }