]> git.pond.sub.org Git - empserver/blob - src/lib/commands/tend.c
Fail commands properly when they get aborted
[empserver] / src / lib / commands / tend.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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  *  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 <config.h>
37
38 #include "commands.h"
39 #include "item.h"
40 #include "land.h"
41 #include "plague.h"
42 #include "plane.h"
43 #include "ship.h"
44
45 static void expose_ship(struct shpstr *s1, struct shpstr *s2);
46 static int tend_land(struct shpstr *tenderp, char *units);
47
48 int
49 tend(void)
50 {
51     struct nstr_item targets;
52     struct nstr_item tenders;
53     struct shpstr tender;
54     struct shpstr target;
55     struct ichrstr *ip;
56     struct mchrstr *vbase;
57     int amt;
58     int retval;
59     int ontender;
60     int ontarget;
61     int maxtender;
62     int maxtarget;
63     int transfer;
64     int total;
65     int type;
66     char *p;
67     char prompt[512];
68     char buf[1024];
69
70     if (!(p = getstarg(player->argp[1],
71                        "Tend what commodity (or 'land')? ", buf)) || !*p)
72         return RET_SYN;
73
74     if (!strncmp(p, "land", 4))
75         type = EF_LAND;
76     else if (NULL != (ip = item_by_name(p)))
77         type = EF_SECTOR;
78     else {
79         pr("Can't tend '%s'\n", p);
80         return RET_SYN;
81     }
82
83     if (!snxtitem(&tenders, EF_SHIP,
84                   getstarg(player->argp[2], "Tender(s)? ", buf)))
85         return RET_SYN;
86
87     while (nxtitem(&tenders, &tender)) {
88         if (!player->owner)
89             continue;
90         if (type == EF_LAND) {
91             sprintf(prompt, "Land unit(s) to tend from %s? ",
92                     prship(&tender));
93             p = getstarg(player->argp[3], prompt, buf);
94             if (!p)
95                 return RET_SYN;
96             if (!*p)
97                 continue;
98             if (!check_ship_ok(&tender))
99                 return RET_SYN;
100             if (0 != (retval = tend_land(&tender, p)))
101                 return retval;
102             continue;
103         }
104         sprintf(prompt, "Number of %s to tend from %s? ",
105                 ip->i_name, prship(&tender));
106         p = getstarg(player->argp[3], prompt, buf);
107         if (!p)
108             return RET_SYN;
109         if (!*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_uid];
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_uid];
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             return RET_SYN;
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_uid];
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_uid];
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_uid] = 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_uid] = ontarget + transfer;
171                 ontender -= transfer;
172                 total += transfer;
173                 if (transfer && target.shp_own != player->cnum) {
174                     wu(0, target.shp_own, "%s tended %d %s to %s\n",
175                        cname(player->cnum), total, ip->i_name,
176                        prship(&target));
177                 }
178             }
179             expose_ship(&tender, &target);
180             putship(target.shp_uid, &target);
181             if (amt > 0 && ontender == 0) {
182                 pr("%s out of %s\n", prship(&tender), ip->i_name);
183                 break;
184             }
185         }
186         pr("%d total %s transferred %s %s\n",
187            total, ip->i_name, (amt > 0) ? "off of" : "to",
188            prship(&tender));
189         tender.shp_item[ip->i_uid] = ontender;
190         tender.shp_mission = 0;
191         putship(tender.shp_uid, &tender);
192     }
193     return RET_OK;
194 }
195
196 static void
197 expose_ship(struct shpstr *s1, struct shpstr *s2)
198 {
199     if (s1->shp_pstage == PLG_INFECT && s2->shp_pstage == PLG_HEALTHY)
200         s2->shp_pstage = PLG_EXPOSED;
201     if (s2->shp_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
202         s1->shp_pstage = PLG_EXPOSED;
203 }
204
205 static int
206 tend_land(struct shpstr *tenderp, char *units)
207 {
208     struct nstr_item lni;
209     struct nstr_item targets;
210     struct shpstr target;
211     struct lndstr land;
212     struct plnstr plane;
213     struct nstr_item pni;
214     char buf[1024];
215
216     if (!snxtitem(&lni, EF_LAND, units))
217         return RET_SYN;
218
219     while (nxtitem(&lni, &land)) {
220         if (!player->owner)
221             continue;
222         if (land.lnd_ship != tenderp->shp_uid) {
223             pr("%s is not on %s!\n", prland(&land), prship(tenderp));
224             continue;
225         }
226         if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
227             pr("%s does not have \"assault\" capability and can't be tended\n",
228                prland(&land));
229             continue;
230         }
231         if (!snxtitem(&targets, EF_SHIP,
232                       getstarg(player->argp[4], "Ship to be tended? ",
233                                buf)))
234             return RET_SYN;
235         if (!check_land_ok(&land))
236             return RET_SYN;
237         while (nxtitem(&targets, &target)) {
238             if (!player->owner &&
239                 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
240                 continue;
241             if (target.shp_uid == tenderp->shp_uid)
242                 continue;
243             if (tenderp->shp_x != target.shp_x ||
244                 tenderp->shp_y != target.shp_y)
245                 continue;
246
247             /* Fit unit on ship */
248             count_units(&target);
249             getship(target.shp_uid, &target);
250
251             if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
252                 (!((mchr[(int)target.shp_type].m_flags & M_SUPPLY) &&
253                    (!(mchr[(int)target.shp_type].m_flags & M_SUB))))) {
254                 pr("You can only load light units onto ships,\n"
255                    "unless the ship is a non-sub supply ship\n"
256                    "%s not tended\n", prland(&land));
257                 continue;
258             }
259
260             if ((mchr[(int)target.shp_type].m_flags & M_SUB) &&
261                 (lchr[(int)land.lnd_type].l_flags & L_SPY) &&
262                 !mchr[(int)target.shp_type].m_nland) {
263                 if (target.shp_nland > 1) {
264                     pr("%s doesn't have room for more than two spy units!\n",
265                        prship(&target));
266                     continue;
267                 }
268             } else if (target.shp_nland >= mchr[(int)target.shp_type].m_nland) {
269                 if (mchr[(int)target.shp_type].m_nland)
270                     pr("%s doesn't have room for any more land units!\n",
271                        prship(&target));
272                 else
273                     pr("%s doesn't carry land units!\n", prship(&target));
274                 continue;
275             }
276             pr("%s transferred from %s to %s\n",
277                prland(&land), prship(tenderp), prship(&target));
278             sprintf(buf, "loaded on your %s at %s",
279                     prship(&target), xyas(target.shp_x, target.shp_y,
280                                           target.shp_own));
281             gift(target.shp_own, player->cnum, &land, buf);
282             land.lnd_ship = target.shp_uid;
283             land.lnd_harden = 0;
284             land.lnd_mission = 0;
285             target.shp_nland++;
286             putland(land.lnd_uid, &land);
287             expose_ship(tenderp, &target);
288             putship(target.shp_uid, &target);
289             count_units(tenderp);
290             putship(tenderp->shp_uid, tenderp);
291             snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
292             while (nxtitem(&pni, &plane)) {
293                 if (plane.pln_flags & PLN_LAUNCHED)
294                     continue;
295                 if (plane.pln_land != land.lnd_uid)
296                     continue;
297                 sprintf(buf, "loaded on %s", prship(&target));
298                 gift(target.shp_own, player->cnum, &plane, buf);
299                 plane.pln_mission = 0;
300                 putplane(plane.pln_uid, &plane);
301             }
302         }
303     }
304     return 0;
305 }