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