]> git.pond.sub.org Git - empserver/blob - src/lib/commands/work.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / commands / work.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 "misc.h"
37 #include "player.h"
38 #include "xy.h"
39 #include "sect.h"
40 #include "land.h"
41 #include "nat.h"
42 #include "nsc.h"
43 #include "file.h"
44 #include "optlist.h"
45 #include "commands.h"
46
47 static int buildeff(struct sctstr *, int, double *);
48
49 int
50 work(void)
51 {
52     int nunits;
53     struct nstr_item ni;
54     struct sctstr sect;
55     struct lndstr land;
56     int work_amt, eff_amt, w;
57     s_char *p;
58     coord donex = 0, doney = 1;
59     s_char buf[1024];
60
61     if (!snxtitem(&ni, EF_LAND, player->argp[1]))
62         return RET_SYN;
63     p = getstarg(player->argp[2], "Amount: ", buf);
64     if (p == 0 || *p == 0)
65         return RET_SYN;
66     work_amt = atoi(p);
67     if ((work_amt < 0) || (work_amt > land_mob_max)) {
68         pr("Mobility used must be from 0 to %d\n", land_mob_max);
69         return RET_FAIL;
70     }
71     nunits = 0;
72     while (nxtitem(&ni, &land)) {
73         if (!player->owner || land.lnd_own == 0)
74             continue;
75         if (!(lchr[(int)land.lnd_type].l_flags & L_ENGINEER))
76             continue;
77         if (land.lnd_mobil <= 0) {
78             pr("%s has no mobility!\n", prland(&land));
79             continue;
80         }
81         getsect(land.lnd_x, land.lnd_y, &sect);
82         if (sect.sct_effic >= 100 && sect.sct_type == sect.sct_newtype) {
83             if (sect.sct_x != donex || sect.sct_y != doney)
84                 pr("%s is %d%% efficient\n",
85                    xyas(sect.sct_x, sect.sct_y, player->cnum),
86                    sect.sct_effic);
87             /* not perfect, but it'll do -KHS */
88             donex = sect.sct_x;
89             doney = sect.sct_y;
90             continue;
91         }
92         eff_amt = MIN(land.lnd_mobil, work_amt);
93         w = ldround(((double)eff_amt * land.lnd_effic / 600.0), 1);
94         if (w < 1) {
95             pr("%s doesn't work enough to change efficiency (try increasing amount)\n", prland(&land));
96             continue;
97         }
98         nunits++;
99         eff_amt = ((6 * buildeff(&sect, w, &player->dolcost)) /
100                    (land.lnd_effic / 100.0));
101         land.lnd_mission = 0;
102         land.lnd_mobil -= eff_amt;
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            (int)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     work_cost = 0;
130     if (sp->sct_type != sp->sct_newtype) {
131         /*
132          * Tear down existing sector.
133          * Easier to destroy than to build.
134          */
135         work_cost = (sp->sct_effic + 3) / 4;
136         if (work_cost > work)
137             work_cost = work;
138         n = sp->sct_effic - work_cost * 4;
139         if (n <= 0) {
140             n = 0;
141             sp->sct_type = sp->sct_newtype;
142         }
143         sp->sct_effic = n;
144         if (!opt_DEFENSE_INFRA)
145             sp->sct_defense = sp->sct_effic;
146         work -= work_cost;
147         *money += work_cost;
148         effdone += work_cost;
149     }
150     if (sp->sct_type == sp->sct_newtype) {
151         work_cost = 100 - sp->sct_effic;
152         if (work_cost > work)
153             work_cost = work;
154
155         if (dchr[sp->sct_type].d_lcms > 0) {
156             lcms = sp->sct_item[I_LCM];
157             lcms /= dchr[sp->sct_type].d_lcms;
158             if (work_cost > lcms)
159                 work_cost = lcms;
160         }
161         if (dchr[sp->sct_type].d_hcms > 0) {
162             hcms = sp->sct_item[I_HCM];
163             hcms /= dchr[sp->sct_type].d_hcms;
164             if (work_cost > hcms)
165                 work_cost = hcms;
166         }
167
168         sp->sct_effic += work_cost;
169         *money += work_cost * dchr[sp->sct_type].d_build;
170
171         if ((dchr[sp->sct_type].d_lcms > 0) ||
172             (dchr[sp->sct_type].d_hcms > 0)) {
173             sp->sct_item[I_LCM] -= work_cost * dchr[sp->sct_type].d_lcms;
174             sp->sct_item[I_HCM] -= work_cost * dchr[sp->sct_type].d_hcms;
175         }
176         effdone += work_cost;
177     }
178     return effdone;
179 }