]> git.pond.sub.org Git - empserver/blob - src/lib/commands/tend.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / commands / tend.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  *  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 #ifdef Rel4
37 #include <string.h>
38 #endif /* Rel4 */
39 #include "misc.h"
40 #include "player.h"
41 #include "var.h"
42 #include "xy.h"
43 #include "file.h"
44 #include "ship.h"
45 #include "item.h"
46 #include "nsc.h"
47 #include "nat.h"
48 #include "land.h"
49 #include "plane.h"
50 #include "nuke.h"
51 #include "genitem.h"
52 #include "commands.h"
53
54 static void expose_ship(struct shpstr *s1, struct shpstr *s2);
55 static int tend_land(struct shpstr *tenderp, s_char *units);
56
57 int
58 tend(void)
59 {
60     struct nstr_item targets;
61     struct nstr_item tenders;
62     struct shpstr tender;
63     struct shpstr target;
64     struct ichrstr *ip;
65     struct mchrstr *vbase;
66     int amt;
67     int retval;
68     int ontender;
69     int ontarget;
70     int maxtender;
71     int maxtarget;
72     int transfer;
73     int total;
74     int type;
75     s_char *p;
76     s_char prompt[512];
77     s_char buf[1024];
78
79     if (!(p = getstarg(player->argp[1],
80                        "Tend what commodity (or 'land')? ", buf)) || !*p)
81         return RET_SYN;
82
83     if (!strncmp(p, "land", 4))
84         type = EF_LAND;
85     else if (NULL != (ip = whatitem(p, (s_char *)0)))
86         type = EF_SECTOR;
87     else {
88         pr("Bad commodity.\n");
89         return RET_SYN;
90     }
91
92     if (!snxtitem
93         (&tenders, EF_SHIP, getstarg(player->argp[2], "Tender(s)? ", buf)))
94         return RET_SYN;
95
96     while (nxtitem(&tenders, (s_char *)&tender)) {
97         if (!player->owner)
98             continue;
99         if (type == EF_LAND) {
100             sprintf(prompt, "Land unit(s) to tend from %s? ",
101                     prship(&tender));
102             if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
103                 continue;
104             if (!check_ship_ok(&tender))
105                 return RET_SYN;
106             if (0 != (retval = tend_land(&tender, p)))
107                 return retval;
108             continue;
109         }
110         sprintf(prompt, "Number of %s to tend from %s? ",
111                 ip->i_name, prship(&tender));
112         if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
113             continue;
114         if (!check_ship_ok(&tender))
115             return RET_SYN;
116         if (!(amt = atoi(p))) {
117             pr("Amount must be non-zero!\n");
118             return RET_SYN;
119         }
120         ontender = getvar(ip->i_vtype, (s_char *)&tender, EF_SHIP);
121         if (ontender == 0 && amt > 0) {
122             pr("No %s on %s\n", ip->i_name, prship(&tender));
123             return RET_FAIL;
124         }
125         vbase = &mchr[(int)tender.shp_type];
126         maxtender = vl_find(ip->i_vtype, vbase->m_vtype,
127                             vbase->m_vamt, (int)vbase->m_nv);
128         if (maxtender == 0) {
129             pr("A %s cannot hold any %s\n",
130                mchr[(int)tender.shp_type].m_name, ip->i_name);
131             break;
132         }
133         if (!snxtitem(&targets, EF_SHIP,
134                       getstarg(player->argp[4], "Ships to be tended? ",
135                                buf)))
136             break;
137         if (!check_ship_ok(&tender))
138             return RET_SYN;
139         total = 0;
140         while (tend_nxtitem(&targets, (s_char *)&target)) {
141             if (!player->owner &&
142                 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
143                 continue;
144             if (target.shp_uid == tender.shp_uid)
145                 continue;
146             if (tender.shp_x != target.shp_x ||
147                 tender.shp_y != target.shp_y)
148                 continue;
149             ontarget = getvar(ip->i_vtype, (s_char *)&target, EF_SHIP);
150             if (ontarget == 0 && amt < 0) {
151                 pr("No %s on %s\n", ip->i_name, prship(&target));
152                 continue;
153             }
154             vbase = &mchr[(int)target.shp_type];
155             maxtarget = vl_find(ip->i_vtype, vbase->m_vtype,
156                                 vbase->m_vamt, (int)vbase->m_nv);
157             if (amt < 0) {
158                 if (!player->owner)
159                     amt = 0;
160
161                 /* take from target and give to tender */
162                 transfer = min(ontarget, -amt);
163                 transfer = min(maxtender - ontender, transfer);
164                 if (transfer == 0)
165                     continue;
166                 putvar(ip->i_vtype, ontarget - transfer,
167                        (s_char *)&target, EF_SHIP);
168                 ontender += transfer;
169                 total += transfer;
170             } else {
171                 /* give to target from tender */
172                 transfer = min(ontender, amt);
173                 transfer = min(transfer, maxtarget - ontarget);
174                 if (transfer == 0)
175                     continue;
176                 putvar(ip->i_vtype, ontarget + transfer,
177                        (s_char *)&target, EF_SHIP);
178                 ontender -= transfer;
179                 total += transfer;
180             }
181             expose_ship(&tender, &target);
182             putship(target.shp_uid, &target);
183             if (amt > 0 && ontender == 0) {
184                 pr("%s out of %s\n", prship(&tender), ip->i_name);
185                 break;
186             }
187         }
188         pr("%d total %s transferred %s %s\n",
189            total, ip->i_name, (amt > 0) ? "off of" : "to",
190            prship(&tender));
191         putvar(ip->i_vtype, ontender, (s_char *)&tender, EF_SHIP);
192         tender.shp_mission = 0;
193         putship(tender.shp_uid, &tender);
194     }
195     return RET_OK;
196 }
197
198 static void
199 expose_ship(struct shpstr *s1, struct shpstr *s2)
200 {
201     if (getvar(V_PSTAGE, (s_char *)s1, EF_SHIP) == PLG_INFECT &&
202         getvar(V_PSTAGE, (s_char *)s2, EF_SHIP) == PLG_HEALTHY)
203         putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)s2, EF_SHIP);
204     if (getvar(V_PSTAGE, (s_char *)s2, EF_SHIP) == PLG_INFECT &&
205         getvar(V_PSTAGE, (s_char *)s1, EF_SHIP) == PLG_HEALTHY)
206         putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)s1, EF_SHIP);
207 }
208
209 /*
210  * tend_nxtitem.c
211  *
212  * get next item from list. Stolen from nxtitem to make 1 itsy-bitsy change
213  *
214  * Dave Pare, 1989
215  */
216
217 int
218 tend_nxtitem(struct nstr_item *np, caddr_t ptr)
219 {
220     struct genitem *gp;
221     int selected;
222
223     if (np->sel == NS_UNDEF)
224         return 0;
225     gp = (struct genitem *)ptr;
226     do {
227         if (np->sel == NS_LIST) {
228             np->index++;
229             if (np->index >= np->size)
230                 return 0;
231             np->cur = np->list[np->index];
232         } else {
233             np->cur++;
234         }
235         if (!np->read(np->type, np->cur, ptr)) {
236             /* if read fails, fatal */
237             return 0;
238         }
239         selected = 1;
240         switch (np->sel) {
241         case NS_LIST:
242             /* The change is to take the player->owner check out here */
243             break;
244         case NS_ALL:
245             /* XXX maybe combine NS_LIST and NS_ALL later */
246             break;
247         case NS_DIST:
248             if (!xyinrange(gp->x, gp->y, &np->range)) {
249                 selected = 0;
250                 break;
251             }
252             np->curdist = mapdist((int)gp->x, (int)gp->y,
253                                   (int)np->cx, (int)np->cy);
254             if (np->curdist > np->dist)
255                 selected = 0;
256             break;
257         case NS_AREA:
258             if (!xyinrange(gp->x, gp->y, &np->range))
259                 selected = 0;
260             if (gp->x == np->range.hx || gp->y == np->range.hy)
261                 selected = 0;
262             break;
263         case NS_XY:
264             if (gp->x != np->cx || gp->y != np->cy)
265                 selected = 0;
266             break;
267         case NS_GROUP:
268             if (np->group != gp->group)
269                 selected = 0;
270             break;
271         default:
272             logerror("nxtitem: bad selector %d\n", np->sel);
273             return 0;
274         }
275         if (selected && np->ncond) {
276             /* nstr_exec is expensive, so we do it last */
277             if (!nstr_exec(np->cond, np->ncond, ptr, np->type))
278                 selected = 0;
279         }
280     } while (!selected);
281     return 1;
282 }
283
284 static int
285 tend_land(struct shpstr *tenderp, s_char *units)
286 {
287     struct nstr_item lni;
288     struct nstr_item targets;
289     struct shpstr target;
290     struct lndstr land;
291     struct plnstr plane;
292     struct nstr_item pni;
293     s_char buf[1024];
294
295     if (!snxtitem(&lni, EF_LAND, units))
296         return RET_SYN;
297
298     while (nxtitem(&lni, (s_char *)&land)) {
299         if (!player->owner)
300             continue;
301         if (land.lnd_ship != tenderp->shp_uid) {
302             pr("%s is not on %s!\n", prland(&land), prship(tenderp));
303             continue;
304         }
305         if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
306             pr("%s does not have \"assault\" capability and can't be tended\n", prland(&land));
307             continue;
308         }
309         if (!snxtitem(&targets, EF_SHIP,
310                       getstarg(player->argp[4], "Ship to be tended? ",
311                                buf)))
312             break;
313         if (!check_land_ok(&land))
314             return RET_SYN;
315         while (tend_nxtitem(&targets, (s_char *)&target)) {
316             if (!player->owner &&
317                 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
318                 continue;
319             if (target.shp_uid == tenderp->shp_uid)
320                 continue;
321             if (tenderp->shp_x != target.shp_x ||
322                 tenderp->shp_y != target.shp_y)
323                 continue;
324
325             /* Fit unit on ship */
326             count_units(&target);
327             getship(target.shp_uid, &target);
328
329             if (target.shp_nland >= mchr[(int)target.shp_type].m_nland) {
330                 if (mchr[(int)target.shp_type].m_nland)
331                     pr("%s doesn't have room for any more land units!\n",
332                        prship(&target));
333                 else
334                     pr("%s doesn't carry land units!\n", prship(&target));
335                 continue;
336             }
337             pr("%s transferred from %s to %s\n",
338                prland(&land), prship(tenderp), prship(&target));
339             sprintf(buf, "loaded on your %s at %s",
340                     prship(&target), xyas(target.shp_x, target.shp_y,
341                                           target.shp_own));
342             gift(target.shp_own, player->cnum, (s_char *)&land,
343                  EF_LAND, buf);
344             makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
345                      land.lnd_y);
346             land.lnd_own = target.shp_own;
347             makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
348                         land.lnd_y);
349             land.lnd_ship = target.shp_uid;
350             land.lnd_harden = 0;
351             land.lnd_mission = 0;
352             target.shp_nland++;
353             putland(land.lnd_uid, &land);
354             expose_ship(tenderp, &target);
355             putship(target.shp_uid, &target);
356             count_units(tenderp);
357             putship(tenderp->shp_uid, tenderp);
358             snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
359             while (nxtitem(&pni, (s_char *)&plane)) {
360                 if (plane.pln_flags & PLN_LAUNCHED)
361                     continue;
362                 if (plane.pln_land != land.lnd_uid)
363                     continue;
364                 sprintf(buf, "loaded on %s", prship(&target));
365                 gift(target.shp_own, player->cnum, (s_char *)&plane,
366                      EF_PLANE, buf);
367                 makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
368                          plane.pln_x, plane.pln_y);
369                 plane.pln_own = target.shp_own;
370                 makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
371                             plane.pln_x, plane.pln_y);
372                 plane.pln_mission = 0;
373                 putplane(plane.pln_uid, &plane);
374             }
375         }
376     }
377     return 0;
378 }