]> git.pond.sub.org Git - empserver/blob - src/lib/commands/tend.c
Remove a bunch of redundant casts.
[empserver] / src / lib / commands / tend.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  *  tend.c: Transfer goodies from one ship to another.
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Thomas Ruschak, 1992
33  *     Steve McClure, 2000
34  */
35
36 #include <string.h>
37 #include "misc.h"
38 #include "player.h"
39 #include "plague.h"
40 #include "xy.h"
41 #include "file.h"
42 #include "ship.h"
43 #include "item.h"
44 #include "nsc.h"
45 #include "nat.h"
46 #include "land.h"
47 #include "plane.h"
48 #include "genitem.h"
49 #include "commands.h"
50
51 static void expose_ship(struct shpstr *s1, struct shpstr *s2);
52 static int tend_land(struct shpstr *tenderp, s_char *units);
53
54 int
55 tend(void)
56 {
57     struct nstr_item targets;
58     struct nstr_item tenders;
59     struct shpstr tender;
60     struct shpstr target;
61     struct ichrstr *ip;
62     struct mchrstr *vbase;
63     int amt;
64     int retval;
65     int ontender;
66     int ontarget;
67     int maxtender;
68     int maxtarget;
69     int transfer;
70     int total;
71     int type;
72     s_char *p;
73     s_char prompt[512];
74     s_char buf[1024];
75
76     if (!(p = getstarg(player->argp[1],
77                        "Tend what commodity (or 'land')? ", buf)) || !*p)
78         return RET_SYN;
79
80     if (!strncmp(p, "land", 4))
81         type = EF_LAND;
82     else if (NULL != (ip = item_by_name(p)))
83         type = EF_SECTOR;
84     else {
85         pr("Can't tend '%s'\n", p);
86         return RET_SYN;
87     }
88
89     if (!snxtitem(&tenders, EF_SHIP,
90                   getstarg(player->argp[2], "Tender(s)? ", buf)))
91         return RET_SYN;
92
93     while (nxtitem(&tenders, &tender)) {
94         if (!player->owner)
95             continue;
96         if (type == EF_LAND) {
97             sprintf(prompt, "Land unit(s) to tend from %s? ",
98                     prship(&tender));
99             if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
100                 continue;
101             if (!check_ship_ok(&tender))
102                 return RET_SYN;
103             if (0 != (retval = tend_land(&tender, p)))
104                 return retval;
105             continue;
106         }
107         sprintf(prompt, "Number of %s to tend from %s? ",
108                 ip->i_name, prship(&tender));
109         if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
110             continue;
111         if (!check_ship_ok(&tender))
112             return RET_SYN;
113         if (!(amt = atoi(p))) {
114             pr("Amount must be non-zero!\n");
115             return RET_SYN;
116         }
117         ontender = tender.shp_item[ip->i_vtype];
118         if (ontender == 0 && amt > 0) {
119             pr("No %s on %s\n", ip->i_name, prship(&tender));
120             return RET_FAIL;
121         }
122         vbase = &mchr[(int)tender.shp_type];
123         maxtender = vbase->m_item[ip->i_vtype];
124         if (maxtender == 0) {
125             pr("A %s cannot hold any %s\n",
126                mchr[(int)tender.shp_type].m_name, ip->i_name);
127             break;
128         }
129         if (!snxtitem(&targets, EF_SHIP,
130                       getstarg(player->argp[4], "Ships to be tended? ",
131                                buf)))
132             break;
133         if (!check_ship_ok(&tender))
134             return RET_SYN;
135         total = 0;
136         while (nxtitem(&targets, &target)) {
137             if (!player->owner &&
138                 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
139                 continue;
140             if (target.shp_uid == tender.shp_uid)
141                 continue;
142             if (tender.shp_x != target.shp_x ||
143                 tender.shp_y != target.shp_y)
144                 continue;
145             ontarget = target.shp_item[ip->i_vtype];
146             if (ontarget == 0 && amt < 0) {
147                 pr("No %s on %s\n", ip->i_name, prship(&target));
148                 continue;
149             }
150             vbase = &mchr[(int)target.shp_type];
151             maxtarget = vbase->m_item[ip->i_vtype];
152             if (amt < 0) {
153                 if (!player->owner)
154                     amt = 0;
155
156                 /* take from target and give to tender */
157                 transfer = min(ontarget, -amt);
158                 transfer = min(maxtender - ontender, transfer);
159                 if (transfer == 0)
160                     continue;
161                 target.shp_item[ip->i_vtype] = ontarget - transfer;
162                 ontender += transfer;
163                 total += transfer;
164             } else {
165                 /* give to target from tender */
166                 transfer = min(ontender, amt);
167                 transfer = min(transfer, maxtarget - ontarget);
168                 if (transfer == 0)
169                     continue;
170                 target.shp_item[ip->i_vtype] = ontarget + transfer;
171                 ontender -= transfer;
172                 total += transfer;
173             }
174             expose_ship(&tender, &target);
175             putship(target.shp_uid, &target);
176             if (amt > 0 && ontender == 0) {
177                 pr("%s out of %s\n", prship(&tender), ip->i_name);
178                 break;
179             }
180         }
181         pr("%d total %s transferred %s %s\n",
182            total, ip->i_name, (amt > 0) ? "off of" : "to",
183            prship(&tender));
184         tender.shp_item[ip->i_vtype] = ontender;
185         tender.shp_mission = 0;
186         putship(tender.shp_uid, &tender);
187     }
188     return RET_OK;
189 }
190
191 static void
192 expose_ship(struct shpstr *s1, struct shpstr *s2)
193 {
194     if (s1->shp_pstage == PLG_INFECT && s2->shp_pstage == PLG_HEALTHY)
195         s2->shp_pstage = PLG_EXPOSED;
196     if (s2->shp_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
197         s1->shp_pstage = PLG_EXPOSED;
198 }
199
200 static int
201 tend_land(struct shpstr *tenderp, s_char *units)
202 {
203     struct nstr_item lni;
204     struct nstr_item targets;
205     struct shpstr target;
206     struct lndstr land;
207     struct plnstr plane;
208     struct nstr_item pni;
209     s_char buf[1024];
210
211     if (!snxtitem(&lni, EF_LAND, units))
212         return RET_SYN;
213
214     while (nxtitem(&lni, &land)) {
215         if (!player->owner)
216             continue;
217         if (land.lnd_ship != tenderp->shp_uid) {
218             pr("%s is not on %s!\n", prland(&land), prship(tenderp));
219             continue;
220         }
221         if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
222             pr("%s does not have \"assault\" capability and can't be tended\n", prland(&land));
223             continue;
224         }
225         if (!snxtitem(&targets, EF_SHIP,
226                       getstarg(player->argp[4], "Ship to be tended? ",
227                                buf)))
228             break;
229         if (!check_land_ok(&land))
230             return RET_SYN;
231         while (nxtitem(&targets, &target)) {
232             if (!player->owner &&
233                 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
234                 continue;
235             if (target.shp_uid == tenderp->shp_uid)
236                 continue;
237             if (tenderp->shp_x != target.shp_x ||
238                 tenderp->shp_y != target.shp_y)
239                 continue;
240
241             /* Fit unit on ship */
242             count_units(&target);
243             getship(target.shp_uid, &target);
244
245             if (target.shp_nland >= mchr[(int)target.shp_type].m_nland) {
246                 if (mchr[(int)target.shp_type].m_nland)
247                     pr("%s doesn't have room for any more land units!\n",
248                        prship(&target));
249                 else
250                     pr("%s doesn't carry land units!\n", prship(&target));
251                 continue;
252             }
253             pr("%s transferred from %s to %s\n",
254                prland(&land), prship(tenderp), prship(&target));
255             sprintf(buf, "loaded on your %s at %s",
256                     prship(&target), xyas(target.shp_x, target.shp_y,
257                                           target.shp_own));
258             gift(target.shp_own, player->cnum, (s_char *)&land,
259                  EF_LAND, buf);
260             makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
261                      land.lnd_y);
262             land.lnd_own = target.shp_own;
263             makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
264                         land.lnd_y);
265             land.lnd_ship = target.shp_uid;
266             land.lnd_harden = 0;
267             land.lnd_mission = 0;
268             target.shp_nland++;
269             putland(land.lnd_uid, &land);
270             expose_ship(tenderp, &target);
271             putship(target.shp_uid, &target);
272             count_units(tenderp);
273             putship(tenderp->shp_uid, tenderp);
274             snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
275             while (nxtitem(&pni, &plane)) {
276                 if (plane.pln_flags & PLN_LAUNCHED)
277                     continue;
278                 if (plane.pln_land != land.lnd_uid)
279                     continue;
280                 sprintf(buf, "loaded on %s", prship(&target));
281                 gift(target.shp_own, player->cnum, (s_char *)&plane,
282                      EF_PLANE, buf);
283                 makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
284                          plane.pln_x, plane.pln_y);
285                 plane.pln_own = target.shp_own;
286                 makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
287                             plane.pln_x, plane.pln_y);
288                 plane.pln_mission = 0;
289                 putplane(plane.pln_uid, &plane);
290             }
291         }
292     }
293     return 0;
294 }