]> git.pond.sub.org Git - empserver/blob - src/lib/commands/work.c
Import of Empire 4.2.12
[empserver] / src / lib / commands / work.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 "var.h"
37 #include "xy.h"
38 #include "sect.h"
39 #include "land.h"
40 #include "nat.h"
41 #include "nsc.h"
42 #include "deity.h"
43 #include "file.h"
44 #include "optlist.h"
45 #include "commands.h"
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         extern  int land_mob_max;
57         coord   donex = 0, doney = 1;
58         s_char  buf[1024];
59
60         if (!snxtitem(&ni, EF_LAND, player->argp[1]))
61                 return RET_SYN;
62         p = getstarg(player->argp[2], "Amount: ", buf);
63         if (p == 0 || *p == 0)
64                 return RET_SYN;
65         work_amt = atoi(p);
66         if ((work_amt<0) || (work_amt > land_mob_max)){
67                 pr("Mobility used must be from 0 to %d\n",land_mob_max);
68                 return RET_FAIL;
69         }
70         nunits = 0;
71         while (nxtitem(&ni, (s_char *)&land)) {
72                 if (!player->owner || land.lnd_own == 0)
73                         continue;
74                 if (land.lnd_type < 0 || land.lnd_type > lnd_maxno) {
75                         pr("bad unit type %d (#%d)\n",
76                                 land.lnd_type, ni.cur);
77                         continue;
78                 }
79                 if (!(lchr[(int)land.lnd_type].l_flags & L_ENGINEER))
80                         continue;
81                 if (land.lnd_mobil <= 0){
82                         pr("%s has no mobility!\n",
83                            prland(&land));
84                         continue;
85                 }
86                 getsect(land.lnd_x,land.lnd_y,&sect);
87                 if (sect.sct_effic >= 100
88                     && sect.sct_type == sect.sct_newtype) {
89                         if (sect.sct_x != donex || sect.sct_y != doney)
90                                 pr("%s is %d%% efficient\n",
91                                    xyas(sect.sct_x, sect.sct_y, player->cnum),
92                                    sect.sct_effic);
93                         /* not perfect, but it'll do -KHS */
94                         donex = sect.sct_x;
95                         doney = sect.sct_y;
96                         continue;
97                 }
98                 eff_amt= min(land.lnd_mobil,work_amt);
99                 w = ldround(((double)eff_amt * land.lnd_effic/600.0),1);
100                 if (w < 1) {
101                         pr("%s doesn't work enough to change efficiency (try increasing amount)\n",
102                            prland(&land));
103                         continue;
104                 }
105                 nunits++;
106                 eff_amt = ((6 * buildeff(&sect, w, &player->dolcost)) /
107                            (land.lnd_effic / 100.0));
108                 land.lnd_mission = 0;
109                 land.lnd_mobil -= eff_amt;
110                 pr("%s %s efficiency at %s to %d\n",
111                    prland(&land),
112                    sect.sct_type == sect.sct_newtype ? "raised" : "lowered",
113                    xyas(land.lnd_x,land.lnd_y,player->cnum),
114                    (int)sect.sct_effic);
115                 putland(land.lnd_uid,&land);
116                 putsect(&sect);
117         }
118         if (nunits == 0) {
119                 if (player->argp[1])
120                         pr("%s: No unit(s)\n", player->argp[1]);
121                 else
122                         pr("%s: No unit(s)\n", "");
123                 return RET_FAIL;
124         }else
125                 pr("%d unit%s\n", nunits, splur(nunits));
126         return RET_OK;
127 }
128
129 int
130 buildeff(struct sctstr *sp, int work, double *money)
131 {
132         int     vec[I_MAX+1];
133         register int work_cost;
134         int     n, hcms, lcms;
135         int     effdone = 0;
136
137         getvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
138
139         work_cost = 0;
140         if (sp->sct_type != sp->sct_newtype) {
141                 /*
142                  * Tear down existing sector.
143                  * Easier to destroy than to build.
144                  */
145                 work_cost = (sp->sct_effic + 3) / 4;
146                 if (work_cost > work)
147                         work_cost = work;
148                 n = sp->sct_effic - work_cost * 4;
149                 if (n <= 0) {
150                         n = 0;
151                         sp->sct_type = sp->sct_newtype;
152                 }
153                 sp->sct_effic = n;
154                 if (!opt_DEFENSE_INFRA)
155                   sp->sct_defense = sp->sct_effic;
156                 work -= work_cost;
157                 *money += work_cost;
158                 effdone += work_cost;
159         }
160         if (sp->sct_type == sp->sct_newtype) {
161                 work_cost = 100 - sp->sct_effic;
162                 if (work_cost > work)
163                         work_cost = work;
164
165                 if (dchr[sp->sct_type].d_lcms>0){
166                         lcms = vec[I_LCM];
167                         lcms /= dchr[sp->sct_type].d_lcms;
168                         if (work_cost > lcms)
169                                 work_cost = lcms;
170                 }
171                 if (dchr[sp->sct_type].d_hcms>0){
172                         hcms = vec[I_HCM];
173                         hcms /= dchr[sp->sct_type].d_hcms;
174                         if (work_cost > hcms)
175                                 work_cost = hcms;
176                 }
177
178                 sp->sct_effic += work_cost;
179                 *money += work_cost*dchr[sp->sct_type].d_build;
180
181                 if ((dchr[sp->sct_type].d_lcms>0) || 
182                         (dchr[sp->sct_type].d_hcms>0)){
183                         vec[I_LCM] -= work_cost * dchr[sp->sct_type].d_lcms;
184                         vec[I_HCM] -= work_cost * dchr[sp->sct_type].d_hcms;
185                 }
186                 effdone += work_cost;
187         }
188         putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
189         return effdone;
190 }