]> git.pond.sub.org Git - empserver/blob - src/lib/commands/tend.c
Update copyright notice.
[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, (s_char *)&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 (tend_nxtitem(&targets, (s_char *)&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 /*
201  * tend_nxtitem.c
202  *
203  * get next item from list. Stolen from nxtitem to make 1 itsy-bitsy change
204  *
205  * Dave Pare, 1989
206  */
207
208 int
209 tend_nxtitem(struct nstr_item *np, void *ptr)
210 {
211     struct genitem *gp;
212     int selected;
213
214     if (np->sel == NS_UNDEF)
215         return 0;
216     gp = (struct genitem *)ptr;
217     do {
218         if (np->sel == NS_LIST) {
219             np->index++;
220             if (np->index >= np->size)
221                 return 0;
222             np->cur = np->list[np->index];
223         } else {
224             np->cur++;
225         }
226         if (!np->read(np->type, np->cur, ptr)) {
227             /* if read fails, fatal */
228             return 0;
229         }
230         selected = 1;
231         switch (np->sel) {
232         case NS_LIST:
233             /* The change is to take the player->owner check out here */
234             break;
235         case NS_ALL:
236             /* XXX maybe combine NS_LIST and NS_ALL later */
237             break;
238         case NS_DIST:
239             if (!xyinrange(gp->x, gp->y, &np->range)) {
240                 selected = 0;
241                 break;
242             }
243             np->curdist = mapdist((int)gp->x, (int)gp->y,
244                                   (int)np->cx, (int)np->cy);
245             if (np->curdist > np->dist)
246                 selected = 0;
247             break;
248         case NS_AREA:
249             if (!xyinrange(gp->x, gp->y, &np->range))
250                 selected = 0;
251             if (gp->x == np->range.hx || gp->y == np->range.hy)
252                 selected = 0;
253             break;
254         case NS_XY:
255             if (gp->x != np->cx || gp->y != np->cy)
256                 selected = 0;
257             break;
258         case NS_GROUP:
259             if (np->group != gp->group)
260                 selected = 0;
261             break;
262         default:
263             CANT_HAPPEN("bad np->sel");
264             return 0;
265         }
266         if (selected && np->ncond) {
267             /* nstr_exec is expensive, so we do it last */
268             if (!nstr_exec(np->cond, np->ncond, ptr))
269                 selected = 0;
270         }
271     } while (!selected);
272     return 1;
273 }
274
275 static int
276 tend_land(struct shpstr *tenderp, s_char *units)
277 {
278     struct nstr_item lni;
279     struct nstr_item targets;
280     struct shpstr target;
281     struct lndstr land;
282     struct plnstr plane;
283     struct nstr_item pni;
284     s_char buf[1024];
285
286     if (!snxtitem(&lni, EF_LAND, units))
287         return RET_SYN;
288
289     while (nxtitem(&lni, (s_char *)&land)) {
290         if (!player->owner)
291             continue;
292         if (land.lnd_ship != tenderp->shp_uid) {
293             pr("%s is not on %s!\n", prland(&land), prship(tenderp));
294             continue;
295         }
296         if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
297             pr("%s does not have \"assault\" capability and can't be tended\n", prland(&land));
298             continue;
299         }
300         if (!snxtitem(&targets, EF_SHIP,
301                       getstarg(player->argp[4], "Ship to be tended? ",
302                                buf)))
303             break;
304         if (!check_land_ok(&land))
305             return RET_SYN;
306         while (tend_nxtitem(&targets, (s_char *)&target)) {
307             if (!player->owner &&
308                 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
309                 continue;
310             if (target.shp_uid == tenderp->shp_uid)
311                 continue;
312             if (tenderp->shp_x != target.shp_x ||
313                 tenderp->shp_y != target.shp_y)
314                 continue;
315
316             /* Fit unit on ship */
317             count_units(&target);
318             getship(target.shp_uid, &target);
319
320             if (target.shp_nland >= mchr[(int)target.shp_type].m_nland) {
321                 if (mchr[(int)target.shp_type].m_nland)
322                     pr("%s doesn't have room for any more land units!\n",
323                        prship(&target));
324                 else
325                     pr("%s doesn't carry land units!\n", prship(&target));
326                 continue;
327             }
328             pr("%s transferred from %s to %s\n",
329                prland(&land), prship(tenderp), prship(&target));
330             sprintf(buf, "loaded on your %s at %s",
331                     prship(&target), xyas(target.shp_x, target.shp_y,
332                                           target.shp_own));
333             gift(target.shp_own, player->cnum, (s_char *)&land,
334                  EF_LAND, buf);
335             makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
336                      land.lnd_y);
337             land.lnd_own = target.shp_own;
338             makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
339                         land.lnd_y);
340             land.lnd_ship = target.shp_uid;
341             land.lnd_harden = 0;
342             land.lnd_mission = 0;
343             target.shp_nland++;
344             putland(land.lnd_uid, &land);
345             expose_ship(tenderp, &target);
346             putship(target.shp_uid, &target);
347             count_units(tenderp);
348             putship(tenderp->shp_uid, tenderp);
349             snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
350             while (nxtitem(&pni, (s_char *)&plane)) {
351                 if (plane.pln_flags & PLN_LAUNCHED)
352                     continue;
353                 if (plane.pln_land != land.lnd_uid)
354                     continue;
355                 sprintf(buf, "loaded on %s", prship(&target));
356                 gift(target.shp_own, player->cnum, (s_char *)&plane,
357                      EF_PLANE, buf);
358                 makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
359                          plane.pln_x, plane.pln_y);
360                 plane.pln_own = target.shp_own;
361                 makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
362                             plane.pln_x, plane.pln_y);
363                 plane.pln_mission = 0;
364                 putplane(plane.pln_uid, &plane);
365             }
366         }
367     }
368     return 0;
369 }