]> git.pond.sub.org Git - empserver/blob - src/lib/commands/work.c
(carg, lcarg, fort, land, ldump, lsta, orde, payo, retr, lretr)
[empserver] / src / lib / commands / work.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 "misc.h"
35 #include "player.h"
36 #include "xy.h"
37 #include "sect.h"
38 #include "land.h"
39 #include "nat.h"
40 #include "nsc.h"
41 #include "file.h"
42 #include "optlist.h"
43 #include "commands.h"
44
45 static int buildeff(struct sctstr *, int, double *);
46
47 int
48 work(void)
49 {
50     int nunits;
51     struct nstr_item ni;
52     struct sctstr sect;
53     struct lndstr land;
54     int work_amt, eff_amt, w;
55     s_char *p;
56     coord donex = 0, doney = 1;
57     s_char buf[1024];
58
59     if (!snxtitem(&ni, EF_LAND, player->argp[1]))
60         return RET_SYN;
61     p = getstarg(player->argp[2], "Amount: ", buf);
62     if (p == 0 || *p == 0)
63         return RET_SYN;
64     work_amt = atoi(p);
65     if ((work_amt < 0) || (work_amt > land_mob_max)) {
66         pr("Mobility used must be from 0 to %d\n", land_mob_max);
67         return RET_FAIL;
68     }
69     nunits = 0;
70     while (nxtitem(&ni, &land)) {
71         if (!player->owner || land.lnd_own == 0)
72             continue;
73         if (!(lchr[(int)land.lnd_type].l_flags & L_ENGINEER))
74             continue;
75         if (land.lnd_mobil <= 0) {
76             pr("%s has no mobility!\n", prland(&land));
77             continue;
78         }
79         getsect(land.lnd_x, land.lnd_y, &sect);
80         if (sect.sct_effic >= 100 && sect.sct_type == sect.sct_newtype) {
81             if (sect.sct_x != donex || sect.sct_y != doney)
82                 pr("%s is %d%% efficient\n",
83                    xyas(sect.sct_x, sect.sct_y, player->cnum),
84                    sect.sct_effic);
85             /* not perfect, but it'll do -KHS */
86             donex = sect.sct_x;
87             doney = sect.sct_y;
88             continue;
89         }
90         eff_amt = min(land.lnd_mobil, work_amt);
91         w = ldround(((double)eff_amt * land.lnd_effic / 600.0), 1);
92         if (w < 1) {
93             pr("%s doesn't work enough to change efficiency (try increasing amount)\n", prland(&land));
94             continue;
95         }
96         nunits++;
97         eff_amt = ((6 * buildeff(&sect, w, &player->dolcost)) /
98                    (land.lnd_effic / 100.0));
99         land.lnd_mission = 0;
100         land.lnd_mobil -= eff_amt;
101         pr("%s %s efficiency at %s to %d\n",
102            prland(&land),
103            sect.sct_type == sect.sct_newtype ? "raised" : "lowered",
104            xyas(land.lnd_x, land.lnd_y, player->cnum),
105            (int)sect.sct_effic);
106         putland(land.lnd_uid, &land);
107         putsect(&sect);
108     }
109     if (nunits == 0) {
110         if (player->argp[1])
111             pr("%s: No unit(s)\n", player->argp[1]);
112         else
113             pr("%s: No unit(s)\n", "");
114         return RET_FAIL;
115     } else
116         pr("%d unit%s\n", nunits, splur(nunits));
117     return RET_OK;
118 }
119
120 static int
121 buildeff(struct sctstr *sp, int work, double *money)
122 {
123     int work_cost;
124     int n, hcms, lcms;
125     int effdone = 0;
126
127     work_cost = 0;
128     if (sp->sct_type != sp->sct_newtype) {
129         /*
130          * Tear down existing sector.
131          * Easier to destroy than to build.
132          */
133         work_cost = (sp->sct_effic + 3) / 4;
134         if (work_cost > work)
135             work_cost = work;
136         n = sp->sct_effic - work_cost * 4;
137         if (n <= 0) {
138             n = 0;
139             sp->sct_type = sp->sct_newtype;
140         }
141         sp->sct_effic = n;
142         if (!opt_DEFENSE_INFRA)
143             sp->sct_defense = sp->sct_effic;
144         work -= work_cost;
145         *money += work_cost;
146         effdone += work_cost;
147     }
148     if (sp->sct_type == sp->sct_newtype) {
149         work_cost = 100 - sp->sct_effic;
150         if (work_cost > work)
151             work_cost = work;
152
153         if (dchr[sp->sct_type].d_lcms > 0) {
154             lcms = sp->sct_item[I_LCM];
155             lcms /= dchr[sp->sct_type].d_lcms;
156             if (work_cost > lcms)
157                 work_cost = lcms;
158         }
159         if (dchr[sp->sct_type].d_hcms > 0) {
160             hcms = sp->sct_item[I_HCM];
161             hcms /= dchr[sp->sct_type].d_hcms;
162             if (work_cost > hcms)
163                 work_cost = hcms;
164         }
165
166         sp->sct_effic += work_cost;
167         *money += work_cost * dchr[sp->sct_type].d_build;
168
169         if ((dchr[sp->sct_type].d_lcms > 0) ||
170             (dchr[sp->sct_type].d_hcms > 0)) {
171             sp->sct_item[I_LCM] -= work_cost * dchr[sp->sct_type].d_lcms;
172             sp->sct_item[I_HCM] -= work_cost * dchr[sp->sct_type].d_hcms;
173         }
174         effdone += work_cost;
175     }
176     return effdone;
177 }