]> git.pond.sub.org Git - empserver/blob - src/lib/commands/load.c
Make load, unload, lload and lunload usable for deities
[empserver] / src / lib / commands / load.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  *  load.c: load/unload goods from a sector onto a ship or land unit
29  *
30  *  Known contributors to this file:
31  *     David Sharnoff, 1987
32  *     Ken Stevens, 1995 (rewritten)
33  *     Steve McClure, 1998-2000
34  *     Markus Armbruster, 2004-2008
35  */
36
37 #include <config.h>
38
39 #include <ctype.h>
40 #include "commands.h"
41 #include "empobj.h"
42 #include "item.h"
43 #include "land.h"
44 #include "optlist.h"
45 #include "plague.h"
46 #include "plane.h"
47 #include "ship.h"
48 #include "unit.h"
49
50 /*
51  * The values 1 and -1 are important below, don't change them.
52  */
53 #define LOAD    1
54 #define UNLOAD  -1
55
56 static int load_plane_ship(struct sctstr *sectp, struct shpstr *sp,
57                            int noisy, int load_unload, int *nshipsp);
58 static int load_land_ship(struct sctstr *sectp, struct shpstr *sp,
59                           int noisy, int load_unload, int *nshipsp);
60 static int load_comm_ship(struct sctstr *sectp, struct shpstr *sp,
61                           struct ichrstr *ich, int load_unload,
62                           int *nshipsp);
63 static int load_plane_land(struct sctstr *sectp, struct lndstr *lp,
64                            int noisy, int load_unload, int *nunitsp);
65 static int load_land_land(struct sctstr *sectp, struct lndstr *lp,
66                           int noisy, int load_unload, int *nunitsp);
67 static int load_comm_land(struct sctstr *sectp, struct lndstr *lp,
68                           struct ichrstr *ich, int load_unload,
69                           int *nunitsp);
70
71 int
72 load(void)
73 {
74     int noisy;
75     int load_unload;
76     int type;
77     struct nstr_item nbst;
78     struct ichrstr *ich;
79     int nships;
80     struct sctstr sect;
81     struct shpstr ship;
82     int retval;
83     char *p;
84     char buf[1024];
85
86     if (!(p = getstarg(player->argp[1],
87                        "What commodity (or 'plane' or 'land')? ", buf))
88         || !*p)
89         return RET_SYN;
90
91     if (!strncmp(p, "plane", 5))
92         type = EF_PLANE;
93     else if (!strncmp(p, "land", 4))
94         type = EF_LAND;
95     else if (NULL != (ich = item_by_name(p)))
96         type = EF_SECTOR;
97     else {
98         pr("Can't load '%s'\n", p);
99         return RET_SYN;
100     }
101
102     if (!(p = getstarg(player->argp[2], "Ship(s): ", buf)) || !*p)
103         return RET_SYN;
104
105     noisy = isdigit(*p);
106
107     if (!snxtitem(&nbst, EF_SHIP, p, NULL))
108         return RET_SYN;
109
110     load_unload = **player->argp == 'l' ? LOAD : UNLOAD;
111
112     nships = 0;
113     while (nxtitem(&nbst, &ship)) {
114         if (!ship.shp_own)
115             continue;
116         if (!player->owner && (load_unload == UNLOAD)) {
117             continue;
118         }
119         if (!player->owner) {
120             if (!noisy)
121                 continue;
122             if (getrel(getnatp(ship.shp_own), player->cnum) < FRIENDLY)
123                 continue;
124         }
125         if (!getsect(ship.shp_x, ship.shp_y, &sect))    /* XXX */
126             continue;
127         if (!player->owner && ship.shp_own != player->cnum)
128             continue;
129         if (!player->owner && !sect_has_dock(&sect))
130             continue;
131         if (!sect.sct_own)
132             continue;
133         if (!player->owner && load_unload == LOAD) {
134             if (noisy)
135                 pr("You don't own %s \n",
136                    xyas(ship.shp_x, ship.shp_y, player->cnum));
137             continue;
138         }
139         if (!sect_has_dock(&sect)) {
140             if (noisy)
141                 pr("Sector %s is not a harbor or canal.\n",
142                    xyas(ship.shp_x, ship.shp_y, player->cnum));
143             continue;
144         }
145         if (!player->owner && load_unload == UNLOAD
146             && getrel(getnatp(sect.sct_own), player->cnum) < FRIENDLY) {
147             if (noisy)
148                 pr("You can't unload into an unfriendly %s\n",
149                    dchr[sect.sct_type].d_name);
150             continue;
151         }
152         if (sect.sct_effic < 2) {
153             if (noisy)
154                 pr("The %s at %s is not 2%% efficient yet.\n",
155                    dchr[sect.sct_type].d_name,
156                    xyas(ship.shp_x, ship.shp_y, player->cnum));
157             continue;
158         }
159
160         if (opt_MARKET) {
161             if (ontradingblock(EF_SHIP, &ship)) {
162                 pr("You cannot load/unload an item on the trading block!\n");
163                 continue;
164             }
165         }
166
167         switch (type) {
168         case EF_PLANE:
169             if (0 !=
170                 (retval =
171                  load_plane_ship(&sect, &ship, noisy, load_unload,
172                                  &nships)))
173                 return retval;
174             break;
175         case EF_LAND:
176             if (0 !=
177                 (retval =
178                  load_land_ship(&sect, &ship, noisy, load_unload,
179                                 &nships)))
180                 return retval;
181             break;
182         case EF_SECTOR:
183             if (0 !=
184                 (retval =
185                  load_comm_ship(&sect, &ship, ich, load_unload, &nships)))
186                 return retval;
187         }
188         /* load/unload plague */
189         if (sect.sct_pstage == PLG_INFECT
190             && ship.shp_pstage == PLG_HEALTHY)
191             ship.shp_pstage = PLG_EXPOSED;
192         if (ship.shp_pstage == PLG_INFECT
193             && sect.sct_pstage == PLG_HEALTHY)
194             sect.sct_pstage = PLG_EXPOSED;
195         putsect(&sect);
196         putship(ship.shp_uid, &ship);
197     }
198     if (!nships)
199         pr("No ships affected\n");
200     else
201         pr("%d ship%s %sloaded\n", nships, splur(nships),
202            load_unload == UNLOAD ? "un" : "");
203     return RET_OK;
204 }
205
206 int
207 lload(void)
208 {
209     int noisy;
210     int load_unload;
211     int type;
212     struct nstr_item nbst;
213     struct ichrstr *ich;
214     int nunits;
215     struct sctstr sect;
216     struct lndstr land;
217     int retval;
218     char *p;
219     char buf[1024];
220
221     if (!(p = getstarg(player->argp[1],
222                        "What commodity (or 'plane' or 'land')? ", buf))
223         || !*p)
224         return RET_SYN;
225     if (!strncmp(p, "plane", 5))
226         type = EF_PLANE;
227     else if (!strncmp(p, "land", 4))
228         type = EF_LAND;
229     else if (NULL != (ich = item_by_name(p)))
230         type = EF_SECTOR;
231     else {
232         pr("Can't load '%s'\n", p);
233         return RET_SYN;
234     }
235
236     if (!(p = getstarg(player->argp[2], "Unit(s): ", buf)) || !*p)
237         return RET_SYN;
238
239     noisy = isdigit(*p);
240
241     if (!snxtitem(&nbst, EF_LAND, p, NULL))
242         return RET_SYN;
243
244     load_unload = player->argp[0][1] == 'l' ? LOAD : UNLOAD;
245
246     nunits = 0;
247     while (nxtitem(&nbst, &land)) {
248         if (land.lnd_own == 0)
249             continue;
250         if (!player->owner && load_unload == UNLOAD)
251             continue;
252         if (!player->owner) {
253             if (!noisy)
254                 continue;
255             if (getrel(getnatp(land.lnd_own), player->cnum) != ALLIED)
256                 continue;
257         }
258
259         if (!getsect(land.lnd_x, land.lnd_y, &sect))    /* XXX */
260             continue;
261         if (!player->owner && land.lnd_own != player->cnum)
262             continue;
263         if (!player->owner && load_unload == LOAD) {
264             if (noisy)
265                 pr("Sector %s is not yours.\n",
266                    xyas(sect.sct_x, sect.sct_y, player->cnum));
267             continue;
268         }
269         if (!player->owner &&
270             getrel(getnatp(sect.sct_own), player->cnum) != ALLIED) {
271             pr("Sector %s is not yours.\n",
272                xyas(land.lnd_x, land.lnd_y, player->cnum));
273             continue;
274         }
275
276         if (opt_MARKET) {
277             if (ontradingblock(EF_LAND, &land)) {
278                 pr("You cannot load/unload an item on the trading block!\n");
279                 continue;
280             }
281         }
282
283         switch (type) {
284         case EF_LAND:
285             if (0 !=
286                 (retval =
287                  load_land_land(&sect, &land, noisy, load_unload,
288                                 &nunits)))
289                 return retval;
290             break;
291         case EF_PLANE:
292             if (0 !=
293                 (retval =
294                  load_plane_land(&sect, &land, noisy, load_unload,
295                                  &nunits)))
296                 return retval;
297             break;
298         case EF_SECTOR:
299             if (0 !=
300                 (retval =
301                  load_comm_land(&sect, &land, ich, load_unload, &nunits)))
302                 return retval;
303         }
304         /* load/unload plague */
305         if (sect.sct_pstage == PLG_INFECT
306             && land.lnd_pstage == PLG_HEALTHY)
307             land.lnd_pstage = PLG_EXPOSED;
308         if (land.lnd_pstage == PLG_INFECT
309             && sect.sct_pstage == PLG_HEALTHY)
310             sect.sct_pstage = PLG_EXPOSED;
311
312         putsect(&sect);
313         putland(land.lnd_uid, &land);
314     }
315     if (nunits == 0)
316         pr("No units affected\n");
317     else
318         pr("%d unit%s %sloaded\n", nunits, splur(nunits),
319            load_unload == UNLOAD ? "un" : "");
320     return RET_OK;
321 }
322
323 static int
324 move_amount(int sect_amt, int unit_amt, int unit_max,
325            int load_unload, int amount)
326 {
327     int move_amt;
328
329     if (amount < 0)
330         move_amt = -amount - unit_amt;
331     else
332         move_amt = load_unload == LOAD ? amount : -amount;
333     if (move_amt > unit_max - unit_amt)
334         move_amt = unit_max - unit_amt;
335     if (move_amt < -unit_amt)
336         move_amt = -unit_amt;
337     if (move_amt > sect_amt)
338         move_amt = sect_amt;
339     if (move_amt < sect_amt - ITEM_MAX)
340         move_amt = sect_amt - ITEM_MAX;
341     return move_amt;
342 }
343
344 static int
345 load_comm_ok(struct sctstr *sectp, natid unit_own,
346              i_type item, int move_amt)
347 {
348     if (!move_amt)
349         return 0;
350     if (move_amt < 0 && !player->god && unit_own != player->cnum)
351         return 0;
352     if (move_amt > 0 && !player->god && sectp->sct_own != player->cnum)
353         return 0;
354     if (sectp->sct_oldown != unit_own && item == I_CIVIL) {
355         pr("%s civilians refuse to %s at %s!\n",
356            (move_amt < 0 ? unit_own : sectp->sct_oldown) == player->cnum
357            ? "Your" : "Foreign",
358            move_amt < 0 ? "disembark" : "board",
359            xyas(sectp->sct_x, sectp->sct_y, player->cnum));
360         return 0;
361     }
362     return 1;
363 }
364
365 void
366 gift(natid givee, natid giver, void *ptr, char *mesg)
367 {
368     if (giver != givee)
369         wu(0, givee, "%s %s %s\n", cname(giver), obj_nameof(ptr), mesg);
370     unit_give_away(ptr, givee, 0);
371 }
372
373 static int
374 still_ok_ship(struct sctstr *sectp, struct shpstr *shipp)
375 {
376     if (!check_sect_ok(sectp))
377         return 0;
378     if (!check_ship_ok(shipp))
379         return 0;
380     return 1;
381 }
382
383 static int
384 still_ok_land(struct sctstr *sectp, struct lndstr *landp)
385 {
386     if (!check_sect_ok(sectp))
387         return 0;
388     if (!check_land_ok(landp))
389         return 0;
390     return 1;
391 }
392
393 static int
394 load_plane_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
395                 int load_unload, int *nshipsp)
396 {
397     struct nstr_item ni;
398     struct plnstr pln;
399     int loaded = 0;
400     char buf[1024];
401     char *p;
402     char prompt[512];
403     struct mchrstr *mcp = mchr + sp->shp_type;
404
405     if (mcp->m_nplanes + mcp->m_nchoppers + mcp->m_nxlight == 0) {
406         if (noisy)
407             pr("%s cannot carry planes\n", prship(sp));
408         return 0;
409     }
410     if (load_unload == LOAD &&
411         shp_nplane(sp, NULL, NULL, NULL)
412                 >= mcp->m_nchoppers + mcp->m_nxlight + mcp->m_nplanes) {
413         if (noisy)
414             pr("%s doesn't have room for any more planes\n", prship(sp));
415         return 0;
416     }
417     sprintf(prompt, "Plane(s) to %s %s? ",
418             load_unload == LOAD ? "load onto" : "unload from", prship(sp));
419     p = getstarg(player->argp[3], prompt, buf);
420     if (!p)
421         return RET_SYN;
422     if (!snxtitem(&ni, EF_PLANE, p, NULL))
423         return RET_SYN;
424
425     if (!still_ok_ship(sectp, sp))
426         return RET_SYN;
427
428     if (noisy && p && *p)
429         noisy = isdigit(*p);
430
431     while (nxtitem(&ni, &pln)) {
432         if (!player->owner)
433             continue;
434         if (!(plchr[(int)pln.pln_type].pl_flags & P_L)
435             && !(plchr[(int)pln.pln_type].pl_flags & P_E)
436             && !(plchr[(int)pln.pln_type].pl_flags & P_K)
437             && !(plchr[(int)pln.pln_type].pl_flags & P_M)
438             ) {
439             if (noisy)
440                 pr("You can only load light planes, helos, xtra-light, or missiles onto ships.\n");
441             continue;
442         }
443         if (load_unload == LOAD && pln.pln_ship > -1) {
444             if (noisy)
445                 pr("%s is already on ship #%d!\n",
446                    prplane(&pln), pln.pln_ship);
447             continue;
448         }
449         if (load_unload == LOAD && pln.pln_land > -1) {
450             if (noisy)
451                 pr("%s is already on land unit #%d!\n",
452                    prplane(&pln), pln.pln_land);
453             continue;
454         }
455         if (pln.pln_harden != 0) {
456             if (noisy)
457                 pr("%s has been hardened and can't be loaded\n",
458                    prplane(&pln));
459             continue;
460         }
461
462         /* Plane sanity done */
463         /* Find the right ship */
464         if (load_unload == UNLOAD) {
465             if (pln.pln_ship != sp->shp_uid)
466                 continue;
467         } else if (sp->shp_x != pln.pln_x || sp->shp_y != pln.pln_y)
468             continue;
469
470         /* ship to (plane or missle) sanity */
471         if (!could_be_on_ship(&pln, sp, 0, 0, 0, 0)) {
472             if (plchr[(int)pln.pln_type].pl_flags & P_L) {
473                 strcpy(buf, "planes");
474             } else if (plchr[(int)pln.pln_type].pl_flags & P_K) {
475                 strcpy(buf, "choppers");
476             } else if (plchr[(int)pln.pln_type].pl_flags & P_M) {
477                 strcpy(buf, "missiles");
478             } else if (plchr[(int)pln.pln_type].pl_flags & P_E) {
479                 strcpy(buf, "extra light planes");
480             }                   /* else impossible */
481             if (noisy)
482                 pr("%s cannot carry %s.\n", prship(sp), buf);
483             continue;
484         }
485         /* Fit plane on ship */
486         if (load_unload == LOAD) {
487             if (!put_plane_on_ship(&pln, sp)) {
488                 if (noisy)
489                     pr("Can't put plane %d on this ship!\n", pln.pln_uid);
490                 continue;
491             }
492             sprintf(buf, "loaded on your %s at %s",
493                     prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
494             gift(sp->shp_own, player->cnum, &pln, buf);
495             putplane(pln.pln_uid, &pln);
496         } else {
497             pln.pln_ship = -1;
498             sprintf(buf, "unloaded in your %s at %s",
499                     dchr[sectp->sct_type].d_name,
500                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
501             gift(sectp->sct_own, player->cnum, &pln, buf);
502             putplane(pln.pln_uid, &pln);
503         }
504         pr("%s %s %s at %s.\n",
505            prplane(&pln),
506            (load_unload == UNLOAD) ?
507            "unloaded from" : "loaded onto",
508            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
509         loaded = 1;
510     }
511     *nshipsp += loaded;
512     return 0;
513 }
514
515 static int
516 load_land_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
517                int load_unload, int *nshipsp)
518 {
519     struct nstr_item ni;
520     struct lndstr land;
521     int loaded = 0;
522     char *p;
523     char prompt[512];
524     char buf[1024];
525     int load_spy = 0;
526
527     if (load_unload == LOAD) {
528         if (opt_LANDSPIES) {
529             if ((mchr[(int)sp->shp_type].m_flags & M_SUB) &&
530                 (mchr[(int)sp->shp_type].m_nland == 0)) {
531                 if (shp_nland(sp) >= 2) {
532                     pr("Non-land unit carrying subs can only carry up to two spy units.\n");
533                     return 0;
534                 }
535                 /* Eh, let 'em load a spy only */
536                 load_spy = 1;
537             }
538         }
539         if (!load_spy && shp_nland(sp) >= mchr[sp->shp_type].m_nland) {
540             if (noisy) {
541                 if (mchr[(int)sp->shp_type].m_nland)
542                     pr("%s doesn't have room for any more land units!\n",
543                        prship(sp));
544                 else
545                     pr("%s cannot carry land units!\n", prship(sp));
546             }
547             return 0;
548         }
549     }
550     sprintf(prompt, "Land unit(s) to %s %s? ",
551             load_unload == LOAD ? "load onto" : "unload from", prship(sp));
552     p = getstarg(player->argp[3], prompt, buf);
553     if (!p)
554         return RET_SYN;
555     if (!snxtitem(&ni, EF_LAND, p, NULL))
556         return RET_SYN;
557
558     if (!still_ok_ship(sectp, sp))
559         return RET_SYN;
560
561     if (noisy && p && *p)
562         noisy = isdigit(*p);
563
564     while (nxtitem(&ni, &land)) {
565         if (!player->owner)
566             continue;
567
568         if (load_unload == LOAD) {
569             if (land.lnd_ship > -1) {
570                 if (noisy)
571                     pr("%s is already on ship #%d!\n",
572                        prland(&land), land.lnd_ship);
573                 continue;
574             }
575             if (land.lnd_land > -1) {
576                 if (noisy)
577                     pr("%s is already on land #%d!\n",
578                        prland(&land), land.lnd_land);
579                 continue;
580             }
581             if (lnd_first_on_land(&land) >= 0) {
582                 if (noisy)
583                     pr("%s cannot be loaded since it is carrying units\n",
584                        prland(&land));
585                 continue;
586             }
587             if (lchr[(int)land.lnd_type].l_flags & L_HEAVY) {
588                 if (noisy)
589                     pr("%s is too heavy to load.\n", prland(&land));
590                 continue;
591             }
592             if (load_spy && !(lchr[(int)land.lnd_type].l_flags & L_SPY)) {
593                 if (noisy)
594                     pr("Subs can only carry spy units.\n");
595                 continue;
596             }
597         }
598
599         /* Unit sanity done */
600         /* Find the right ship */
601         if (load_unload == UNLOAD) {
602             if (land.lnd_ship != sp->shp_uid)
603                 continue;
604             if (land.lnd_land > -1)
605                 continue;
606         } else if (sp->shp_x != land.lnd_x || sp->shp_y != land.lnd_y)
607             continue;
608
609         if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
610             (!((mchr[(int)sp->shp_type].m_flags & M_SUPPLY) &&
611                (!(mchr[(int)sp->shp_type].m_flags & M_SUB))))) {
612             if (noisy) {
613                 pr("You can only load light units onto ships,\n");
614                 pr("unless the ship is a non-sub supply ship\n");
615                 pr("%s not loaded\n", prland(&land));
616             }
617             continue;
618         }
619         /* Fit unit on ship */
620         if (load_unload == LOAD) {
621             /* We have to check again, since it may have changed */
622             if (opt_LANDSPIES) {
623                 if ((mchr[(int)sp->shp_type].m_flags & M_SUB) &&
624                     (mchr[(int)sp->shp_type].m_nland == 0)) {
625                     if (shp_nland(sp) >= 2) {
626                         pr("Non-land unit carrying subs can only carry up to two spy units.\n");
627                         return 0;
628                     }
629                     /* Eh, let 'em load a spy only */
630                     load_spy = 1;
631                 }
632             }
633             if (!load_spy && shp_nland(sp) >= mchr[sp->shp_type].m_nland) {
634                 if (noisy) {
635                     if (mchr[(int)sp->shp_type].m_nland)
636                         pr("%s doesn't have room for any more land units!\n",
637                            prship(sp));
638                     else
639                         pr("%s cannot carry land units!\n", prship(sp));
640                 }
641                 return 0;
642             }
643             sprintf(buf, "loaded on your %s at %s",
644                     prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
645             gift(sp->shp_own, player->cnum, &land, buf);
646             land.lnd_ship = sp->shp_uid;
647             land.lnd_harden = 0;
648 #if 0
649            /*
650             * FIXME if this supplies from the sector, the putsect in
651             * load() / lload() duplicates those supplies, causing a
652             * seqno mismatch
653             */
654             resupply_all(&land);
655 #endif
656             putland(land.lnd_uid, &land);
657             if (!has_supply(&land))
658                 pr("WARNING: %s is out of supply!\n", prland(&land));
659         } else {
660             sprintf(buf, "unloaded in your %s at %s",
661                     dchr[sectp->sct_type].d_name,
662                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
663
664             /* Spies are unloaded quietly, others aren't */
665             if (!(lchr[(int)land.lnd_type].l_flags & L_SPY))
666                 gift(sectp->sct_own, player->cnum, &land, buf);
667             land.lnd_ship = -1;
668             putland(land.lnd_uid, &land);
669         }
670         pr("%s %s %s at %s.\n",
671            prland(&land),
672            (load_unload == UNLOAD) ?
673            "unloaded from" : "loaded onto",
674            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
675         loaded = 1;
676     }
677     *nshipsp += loaded;
678     return 0;
679 }
680
681 static int
682 load_comm_ship(struct sctstr *sectp, struct shpstr *sp,
683                struct ichrstr *ich, int load_unload, int *nshipsp)
684 {
685     i_type item = ich->i_uid;
686     struct mchrstr *mcp = &mchr[(int)sp->shp_type];
687     int ship_amt, sect_amt, move_amt;
688     char prompt[512];
689     char *p;
690     char buf[1024];
691
692     sprintf(prompt, "Number of %s to %s %s at %s? ",
693             ich->i_name,
694             (load_unload == UNLOAD) ?
695             "unload from" : "load onto",
696             prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
697     if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
698         return RET_SYN;
699
700     if (!still_ok_ship(sectp, sp))
701         return RET_SYN;
702
703     ship_amt = sp->shp_item[item];
704     sect_amt = sectp->sct_item[item];
705     move_amt = move_amount(sect_amt, ship_amt, mcp->m_item[item],
706                            load_unload, atoi(p));
707     if (!load_comm_ok(sectp, sp->shp_own, item, move_amt))
708         return RET_OK;
709     if (!want_to_abandon(sectp, item, move_amt, 0))
710         return RET_FAIL;
711     if (!still_ok_ship(sectp, sp))
712         return RET_SYN;
713     sectp->sct_item[item] = sect_amt - move_amt;
714     sp->shp_item[item] = ship_amt + move_amt;
715
716     if (move_amt >= 0) {
717         pr("%d %s loaded onto %s at %s\n",
718            move_amt, ich->i_name,
719            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
720         if (sp->shp_own != player->cnum) {
721             wu(0, sp->shp_own, "%s loaded %d %s onto %s at %s\n",
722                cname(player->cnum), move_amt, ich->i_name,
723                prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
724         }
725     } else {
726         pr("%d %s unloaded from %s at %s\n",
727            -move_amt, ich->i_name,
728            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
729         if (sectp->sct_own != player->cnum) {
730             wu(0, sectp->sct_own, "%s unloaded %d %s from %s at %s\n",
731                cname(player->cnum), -move_amt, ich->i_name,
732                prship(sp), xyas(sp->shp_x, sp->shp_y, sectp->sct_own));
733         }
734     }
735     ++*nshipsp;
736     return 0;
737 }
738
739 static int
740 load_plane_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
741                 int load_unload, int *nunitsp)
742 {
743     struct nstr_item ni;
744     struct plnstr pln;
745     int loaded = 0;
746     char *p;
747     char prompt[512];
748     char buf[1024];
749     struct lchrstr *lcp = lchr + lp->lnd_type;
750
751     if (!lcp->l_nxlight) {
752         if (noisy)
753             pr("%s cannot carry extra-light planes.\n", prland(lp));
754         return 0;
755     }
756     if (load_unload == LOAD && lnd_nxlight(lp) >= lcp->l_nxlight) {
757         if (noisy)
758             pr("%s doesn't have room for any more extra-light planes\n",
759                prland(lp));
760         return 0;
761     }
762     sprintf(prompt, "Plane(s) to %s %s? ",
763             load_unload == LOAD ? "load onto" : "unload from", prland(lp));
764     p = getstarg(player->argp[3], prompt, buf);
765     if (!p)
766         return RET_SYN;
767     if (!snxtitem(&ni, EF_PLANE, p, NULL))
768         return RET_SYN;
769
770     if (!still_ok_land(sectp, lp))
771         return RET_SYN;
772
773     if (noisy && p && *p)
774         noisy = isdigit(*p);
775
776     while (nxtitem(&ni, &pln)) {
777         if (!player->owner)
778             continue;
779
780         if (!(plchr[(int)pln.pln_type].pl_flags & P_E)) {
781             if (noisy)
782                 pr("You can only load xlight planes onto units.\n");
783             continue;
784         }
785
786         if (load_unload == LOAD && pln.pln_ship > -1) {
787             if (noisy)
788                 pr("%s is already on ship #%d!\n",
789                    prplane(&pln), pln.pln_ship);
790             continue;
791         }
792         if (load_unload == LOAD && pln.pln_land > -1) {
793             if (noisy)
794                 pr("%s is already on unit #%d!\n",
795                    prplane(&pln), pln.pln_land);
796             continue;
797         }
798         if (pln.pln_harden != 0) {
799             if (noisy)
800                 pr("%s has been hardened and can't be loaded\n",
801                    prplane(&pln));
802             continue;
803         }
804
805         /* Plane sanity done */
806         /* Find the right unit */
807         if (load_unload == UNLOAD) {
808             if (pln.pln_land != lp->lnd_uid)
809                 continue;
810         } else if (lp->lnd_x != pln.pln_x || lp->lnd_y != pln.pln_y)
811             continue;
812
813         /* Fit plane on unit */
814         if (load_unload == LOAD) {
815             if (!put_plane_on_land(&pln, lp)) {
816                 if (noisy)
817                     pr("Can't put plane %d on this unit!\n", pln.pln_uid);
818                 continue;
819             }
820             sprintf(buf, "loaded on %s at %s",
821                     prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
822             gift(lp->lnd_own, player->cnum, &pln, buf);
823             putplane(pln.pln_uid, &pln);
824         } else {
825             pln.pln_land = -1;
826             sprintf(buf, "unloaded at your sector at %s",
827                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
828             gift(sectp->sct_own, player->cnum, &pln, buf);
829             putplane(pln.pln_uid, &pln);
830         }
831         pr("%s %s %s at %s.\n",
832            prplane(&pln),
833            (load_unload == UNLOAD) ?
834            "unloaded from" : "loaded onto",
835            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
836         loaded = 1;
837     }
838     *nunitsp += loaded;
839     return 0;
840 }
841
842 static int
843 load_comm_land(struct sctstr *sectp, struct lndstr *lp,
844                struct ichrstr *ich, int load_unload, int *nunitsp)
845 {
846     i_type item = ich->i_uid;
847     struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
848     int land_amt, sect_amt, move_amt;
849     char prompt[512];
850     char *p;
851     char buf[1024];
852
853     sprintf(prompt, "Number of %s to %s %s at %s? ",
854             ich->i_name,
855             (load_unload == UNLOAD) ?
856             "unload from" : "load onto",
857             prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
858     if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
859         return RET_SYN;
860
861     if (!still_ok_land(sectp, lp))
862         return RET_SYN;
863
864     land_amt = lp->lnd_item[item];
865     sect_amt = sectp->sct_item[item];
866     move_amt = move_amount(sect_amt, land_amt, lcp->l_item[item],
867                            load_unload, atoi(p));
868     if (!load_comm_ok(sectp, lp->lnd_own, item, move_amt))
869         return RET_OK;
870     sectp->sct_item[item] = sect_amt - move_amt;
871     lp->lnd_item[item] = land_amt + move_amt;
872
873     /* Did we put mils onto this unit? If so, reset the fortification */
874     if (item == I_MILIT && move_amt > 0)
875         lp->lnd_harden = 0;
876
877     if (move_amt >= 0) {
878         pr("%d %s loaded onto %s at %s\n",
879            move_amt, ich->i_name,
880            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
881         if (lp->lnd_own != player->cnum) {
882             wu(0, lp->lnd_own, "%s loaded %d %s onto %s at %s\n",
883                cname(player->cnum), move_amt, ich->i_name,
884                prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
885         }
886     } else {
887         pr("%d %s unloaded from %s at %s\n",
888            -move_amt, ich->i_name,
889            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
890         if (sectp->sct_own != player->cnum) {
891             wu(0, sectp->sct_own, "%s unloaded %d %s from %s at %s\n",
892                cname(player->cnum), -move_amt, ich->i_name,
893                prland(lp), xyas(lp->lnd_x, lp->lnd_y, sectp->sct_own));
894         }
895     }
896     ++*nunitsp;
897     return 0;
898 }
899
900 static int
901 load_land_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
902                int load_unload, int *nunitsp)
903 {
904     struct nstr_item ni;
905     struct lndstr land;
906     int loaded = 0;
907     char *p;
908     char prompt[512];
909     char buf[1024];
910
911     if (load_unload == LOAD
912         && lnd_nland(lp) >= lchr[lp->lnd_type].l_nland) {
913         if (noisy) {
914             if (lchr[lp->lnd_type].l_nland)
915                 pr("%s doesn't have room for any more land units!\n",
916                    prland(lp));
917             else
918                 pr("%s cannot carry land units!\n", prland(lp));
919         }
920         return 0;
921     }
922     sprintf(prompt, "Land unit(s) to %s %s? ",
923             load_unload == LOAD ? "load onto" : "unload from", prland(lp));
924     p = getstarg(player->argp[3], prompt, buf);
925     if (!p)
926         return RET_SYN;
927     if (!snxtitem(&ni, EF_LAND, p, NULL))
928         return RET_SYN;
929
930     if (!still_ok_land(sectp, lp))
931         return RET_SYN;
932
933     if (noisy && p && *p)
934         noisy = isdigit(*p);
935
936     while (nxtitem(&ni, &land)) {
937         if (!player->owner)
938             continue;
939
940         if (load_unload == LOAD) {
941             if (land.lnd_ship > -1) {
942                 if (noisy)
943                     pr("%s is already on ship #%d!\n",
944                        prland(&land), land.lnd_ship);
945                 continue;
946             }
947             if (land.lnd_land > -1) {
948                 if (noisy)
949                     pr("%s is already on land #%d!\n",
950                        prland(&land), land.lnd_land);
951                 continue;
952             }
953             if (lnd_first_on_land(&land) >= 0) {
954                 if (noisy)
955                     pr("%s cannot be loaded since it is carrying units\n",
956                        prland(&land));
957                 continue;
958             }
959             if (land.lnd_uid == lp->lnd_uid) {
960                 if (noisy)
961                     pr("%s can't be loaded onto itself!\n", prland(&land));
962                 continue;
963             }
964             if (lchr[(int)land.lnd_type].l_flags & (L_HEAVY | L_TRAIN)) {
965                 if (noisy)
966                     pr("%s is too heavy to load.\n", prland(&land));
967                 continue;
968             }
969         }
970
971         /* Unit sanity done */
972         /* Find the right ship */
973         if (load_unload == UNLOAD) {
974             if (land.lnd_land != lp->lnd_uid)
975                 continue;
976             if (land.lnd_ship > -1)
977                 continue;
978         } else if (lp->lnd_x != land.lnd_x || lp->lnd_y != land.lnd_y)
979             continue;
980
981         /* Fit unit on ship */
982         if (load_unload == LOAD) {
983             if (lnd_nland(lp) >= lchr[lp->lnd_type].l_nland) {
984                 if (noisy) {
985                     if (lchr[lp->lnd_type].l_nland)
986                         pr("%s doesn't have room for any more land units!\n",
987                            prland(lp));
988                     else
989                         pr("%s cannot carry land units!\n", prland(lp));
990                 }
991                 break;
992             }
993             sprintf(buf, "loaded on your %s at %s",
994                     prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
995             gift(lp->lnd_own, player->cnum, &land, buf);
996             land.lnd_land = lp->lnd_uid;
997             land.lnd_harden = 0;
998 #if 0
999            /* FIXME same issue as in load_land_ship() */
1000             resupply_all(&land);
1001 #endif
1002             putland(land.lnd_uid, &land);
1003             if (!has_supply(&land))
1004                 pr("WARNING: %s is out of supply!\n", prland(&land));
1005         } else {
1006             sprintf(buf, "unloaded in your %s at %s",
1007                     dchr[sectp->sct_type].d_name,
1008                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
1009             gift(sectp->sct_own, player->cnum, &land, buf);
1010             land.lnd_land = -1;
1011             putland(land.lnd_uid, &land);
1012         }
1013         pr("%s %s %s at %s.\n",
1014            prland(&land),
1015            (load_unload == UNLOAD) ?
1016            "unloaded from" : "loaded onto",
1017            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
1018         loaded = 1;
1019     }
1020     *nunitsp += loaded;
1021     return 0;
1022 }