]> git.pond.sub.org Git - empserver/blob - src/lib/commands/work.c
Update copyright notice.
[empserver] / src / lib / commands / work.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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     coord donex = 0, doney = 1;
52     char buf[1024];
53
54     if (!snxtitem(&ni, EF_LAND, player->argp[1]))
55         return RET_SYN;
56     p = getstarg(player->argp[2], "Amount: ", buf);
57     if (p == 0 || *p == 0)
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             if (sect.sct_x != donex || sect.sct_y != doney)
77                 pr("%s is %d%% efficient\n",
78                    xyas(sect.sct_x, sect.sct_y, player->cnum),
79                    sect.sct_effic);
80             /* not perfect, but it'll do -KHS */
81             donex = sect.sct_x;
82             doney = sect.sct_y;
83             continue;
84         }
85         eff_amt = MIN(land.lnd_mobil, work_amt);
86         w = ldround(((double)eff_amt * land.lnd_effic / 600.0), 1);
87         if (w < 1) {
88             pr("%s doesn't work enough to change efficiency (try increasing amount)\n",
89                prland(&land));
90             continue;
91         }
92         nunits++;
93         eff_amt = ((6 * buildeff(&sect, w, &player->dolcost)) /
94                    (land.lnd_effic / 100.0));
95         land.lnd_mission = 0;
96         land.lnd_mobil -= eff_amt;
97         pr("%s %s efficiency at %s to %d\n",
98            prland(&land),
99            sect.sct_type == sect.sct_newtype ? "raised" : "lowered",
100            xyas(land.lnd_x, land.lnd_y, player->cnum),
101            (int)sect.sct_effic);
102         putland(land.lnd_uid, &land);
103         putsect(&sect);
104     }
105     if (nunits == 0) {
106         if (player->argp[1])
107             pr("%s: No unit(s)\n", player->argp[1]);
108         else
109             pr("%s: No unit(s)\n", "");
110         return RET_FAIL;
111     } else
112         pr("%d unit%s\n", nunits, splur(nunits));
113     return RET_OK;
114 }
115
116 static int
117 buildeff(struct sctstr *sp, int work, double *money)
118 {
119     int work_cost;
120     int n, hcms, lcms;
121     int effdone = 0;
122
123     work_cost = 0;
124     if (sp->sct_type != sp->sct_newtype) {
125         /*
126          * Tear down existing sector.
127          * Easier to destroy than to build.
128          */
129         work_cost = (sp->sct_effic + 3) / 4;
130         if (work_cost > work)
131             work_cost = work;
132         n = sp->sct_effic - work_cost * 4;
133         if (n <= 0) {
134             n = 0;
135             sp->sct_type = sp->sct_newtype;
136         }
137         sp->sct_effic = n;
138         work -= work_cost;
139         *money += work_cost;
140         effdone += work_cost;
141     }
142     if (sp->sct_type == sp->sct_newtype) {
143         work_cost = 100 - sp->sct_effic;
144         if (work_cost > work)
145             work_cost = work;
146
147         if (dchr[sp->sct_type].d_lcms > 0) {
148             lcms = sp->sct_item[I_LCM];
149             lcms /= dchr[sp->sct_type].d_lcms;
150             if (work_cost > lcms)
151                 work_cost = lcms;
152         }
153         if (dchr[sp->sct_type].d_hcms > 0) {
154             hcms = sp->sct_item[I_HCM];
155             hcms /= dchr[sp->sct_type].d_hcms;
156             if (work_cost > hcms)
157                 work_cost = hcms;
158         }
159
160         sp->sct_effic += work_cost;
161         *money += work_cost * dchr[sp->sct_type].d_build;
162
163         if ((dchr[sp->sct_type].d_lcms > 0) ||
164             (dchr[sp->sct_type].d_hcms > 0)) {
165             sp->sct_item[I_LCM] -= work_cost * dchr[sp->sct_type].d_lcms;
166             sp->sct_item[I_HCM] -= work_cost * dchr[sp->sct_type].d_hcms;
167         }
168         effdone += work_cost;
169     }
170     return effdone;
171 }