]> git.pond.sub.org Git - empserver/blob - src/lib/commands/load.c
load tend: Avoid "may be used uninitialized" warnings
[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-2021
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 c_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     ich = item_by_name(p);
83     if (!strncmp(p, "plane", 5))
84         type = EF_PLANE;
85     else if (!strncmp(p, "land", 4))
86         type = EF_LAND;
87     else if (ich)
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 c_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     ich = item_by_name(p);
212     if (!strncmp(p, "plane", 5))
213         type = EF_PLANE;
214     else if (!strncmp(p, "land", 4))
215         type = EF_LAND;
216     else if (ich)
217         type = EF_SECTOR;
218     else {
219         pr("Can't %sload '%s'\n", loading ? "" : "un", p);
220         return RET_SYN;
221     }
222
223     p = getstarg(player->argp[2], "Unit(s): ", buf);
224     if (!p || !*p)
225         return RET_SYN;
226
227     if (!snxtitem(&nbst, EF_LAND, p, NULL))
228         return RET_SYN;
229
230     noisy = nbst.sel == NS_LIST;
231
232     nunits = 0;
233     while (nxtitem(&nbst, &land)) {
234         if (land.lnd_own == 0)
235             continue;
236         if (!player->owner) {
237             if (!loading || !noisy)
238                 continue;
239             if (relations_with(land.lnd_own, player->cnum) != ALLIED)
240                 continue;
241         }
242
243         if (!getsect(land.lnd_x, land.lnd_y, &sect))    /* XXX */
244             continue;
245         if (!player->owner) {
246             if (land.lnd_own != player->cnum)
247                 continue;
248             if (loading) {
249                 if (noisy)
250                     pr("Sector %s is not yours.\n",
251                        xyas(sect.sct_x, sect.sct_y, player->cnum));
252                 continue;
253             }
254             if (relations_with(sect.sct_own, player->cnum) != ALLIED) {
255                 if (noisy)
256                     pr("Sector %s is not yours.\n",
257                        xyas(sect.sct_x, sect.sct_y, player->cnum));
258                 continue;
259             }
260         }
261
262         if (opt_MARKET) {
263             if (ontradingblock(EF_LAND, &land)) {
264                 if (noisy)
265                     pr("%s is on the trading block\n", prland(&land));
266                 continue;
267             }
268         }
269
270         switch (type) {
271         case EF_LAND:
272             retval = load_land_land(&sect, &land, noisy, loading, &nunits);
273             if (retval != 0)
274                 return retval;
275             break;
276         case EF_PLANE:
277             retval = load_plane_land(&sect, &land, noisy, loading, &nunits);
278             if (retval != 0)
279                 return retval;
280             break;
281         case EF_SECTOR:
282             retval = load_comm_land(&sect, &land, ich, loading, &nunits);
283             if (retval != 0)
284                 return retval;
285         }
286         /* load/unload plague */
287         if (sect.sct_pstage == PLG_INFECT
288             && land.lnd_pstage == PLG_HEALTHY)
289             land.lnd_pstage = PLG_EXPOSED;
290         if (land.lnd_pstage == PLG_INFECT
291             && sect.sct_pstage == PLG_HEALTHY)
292             sect.sct_pstage = PLG_EXPOSED;
293
294         putsect(&sect);
295         putland(land.lnd_uid, &land);
296     }
297     if (nunits == 0)
298         pr("No units affected\n");
299     else
300         pr("%d unit%s %sloaded\n", nunits, splur(nunits),
301            loading ? "" : "un");
302     return RET_OK;
303 }
304
305 static int
306 move_amount(int sect_amt, int unit_amt, int unit_max,
307            int loading, int amount)
308 {
309     int move_amt;
310
311     if (amount < 0)
312         move_amt = -amount - unit_amt;
313     else
314         move_amt = loading ? amount : -amount;
315     move_amt = LIMIT_TO(move_amt, -unit_amt, unit_max - unit_amt);
316     move_amt = LIMIT_TO(move_amt, sect_amt - ITEM_MAX, sect_amt);
317     return move_amt;
318 }
319
320 int
321 load_comm_ok(struct sctstr *sectp, natid unit_own,
322              i_type item, int move_amt)
323 {
324     if (!move_amt)
325         return 0;
326     if (move_amt < 0 && !player->god && unit_own != player->cnum)
327         return 0;
328     if (move_amt > 0 && !player->god && sectp->sct_own != player->cnum)
329         return 0;
330     if (sectp->sct_oldown != unit_own && item == I_CIVIL) {
331         pr("%s civilians refuse to %s at %s!\n",
332            (move_amt < 0 ? unit_own : sectp->sct_oldown) == player->cnum
333            ? "Your" : "Foreign",
334            move_amt < 0 ? "disembark" : "board",
335            xyas(sectp->sct_x, sectp->sct_y, player->cnum));
336         return 0;
337     }
338     return 1;
339 }
340
341 void
342 gift(natid givee, natid giver, void *ptr, char *mesg)
343 {
344     if (giver != givee)
345         wu(0, givee, "%s %s %s\n", cname(giver), unit_nameof(ptr), mesg);
346     unit_give_away(ptr, givee, 0);
347 }
348
349 static int
350 still_ok_ship(struct sctstr *sectp, struct shpstr *shipp)
351 {
352     if (!check_sect_ok(sectp))
353         return 0;
354     if (!check_ship_ok(shipp))
355         return 0;
356     return 1;
357 }
358
359 static int
360 still_ok_land(struct sctstr *sectp, struct lndstr *landp)
361 {
362     if (!check_sect_ok(sectp))
363         return 0;
364     if (!check_land_ok(landp))
365         return 0;
366     return 1;
367 }
368
369 static int
370 plane_loadable(struct plnstr *pp, int noisy)
371 {
372     if (pp->pln_ship >= 0) {
373         if (noisy)
374             pr("%s is already on ship #%d!\n",
375                prplane(pp), pp->pln_ship);
376         return 0;
377     }
378     if (pp->pln_land >= 0) {
379         if (noisy)
380             pr("%s is already on land unit #%d!\n",
381                prplane(pp), pp->pln_land);
382         return 0;
383     }
384     if (pp->pln_harden) {
385         if (noisy)
386             pr("%s has been hardened and can't be loaded\n",
387                prplane(pp));
388         return 0;
389     }
390     if (pln_is_in_orbit(pp)) {
391         if (noisy)
392             pr("%s is in space\n", prplane(pp));
393         return 0;
394     }
395     return 1;
396 }
397
398 static int
399 land_loadable(struct lndstr *lp, int noisy)
400 {
401     if (lp->lnd_ship >= 0) {
402         if (noisy)
403             pr("%s is already on ship #%d!\n",
404                prland(lp), lp->lnd_ship);
405         return 0;
406     }
407     if (lp->lnd_land >= 0) {
408         if (noisy)
409             pr("%s is already on land #%d!\n",
410                prland(lp), lp->lnd_land);
411         return 0;
412     }
413     if (lnd_first_on_land(lp) >= 0) {
414         /* Outlawed to prevent arbitrarily deep recursion */
415         if (noisy)
416             pr("%s cannot be loaded since it is carrying units\n",
417                prland(lp));
418         return 0;
419     }
420     if (lchr[lp->lnd_type].l_flags & L_HEAVY) {
421         if (noisy)
422             pr("%s is too heavy to load.\n", prland(lp));
423         return 0;
424     }
425     return 1;
426 }
427
428 static int
429 load_plane_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
430                 int loading, int *nshipsp)
431 {
432     struct nstr_item ni;
433     struct plnstr pln;
434     int loaded = 0;
435     char buf[1024];
436     char *p;
437     char prompt[512];
438     struct mchrstr *mcp = mchr + sp->shp_type;
439
440     if (mcp->m_nplanes + mcp->m_nchoppers + mcp->m_nxlight == 0) {
441         if (noisy)
442             pr("%s cannot carry planes\n", prship(sp));
443         return 0;
444     }
445     if (loading &&
446         shp_nplane(sp, NULL, NULL, NULL)
447                 >= mcp->m_nchoppers + mcp->m_nxlight + mcp->m_nplanes) {
448         pr("%s doesn't have room for any more planes\n", prship(sp));
449         return 0;
450     }
451     sprintf(prompt, "Plane(s) to %s %s? ",
452             loading ? "load onto" : "unload from", prship(sp));
453     p = getstarg(player->argp[3], prompt, buf);
454     if (!p)
455         return RET_SYN;
456     if (!snxtitem(&ni, EF_PLANE, p, NULL))
457         return RET_SYN;
458
459     if (!still_ok_ship(sectp, sp))
460         return RET_SYN;
461
462     noisy = ni.sel == NS_LIST;
463
464     while (nxtitem(&ni, &pln)) {
465         if (!player->owner)
466             continue;
467         if (!(plchr[(int)pln.pln_type].pl_flags & P_L)
468             && !(plchr[(int)pln.pln_type].pl_flags & P_E)
469             && !(plchr[(int)pln.pln_type].pl_flags & P_K)
470             && !(plchr[(int)pln.pln_type].pl_flags & P_M)
471             ) {
472             if (noisy)
473                 pr("You can only load light planes, helos, xtra-light, or missiles onto ships.\n");
474             continue;
475         }
476         if (loading && !plane_loadable(&pln, noisy))
477             continue;
478
479         if (!loading) {
480             if (pln.pln_ship != sp->shp_uid)
481                 continue;
482         } else if (sp->shp_x != pln.pln_x || sp->shp_y != pln.pln_y)
483             continue;
484
485         if (!could_be_on_ship(&pln, sp)) {
486             if (noisy) {
487                 if (plchr[(int)pln.pln_type].pl_flags & P_K)
488                     p = "choppers";
489                 else if (plchr[(int)pln.pln_type].pl_flags & P_E)
490                     p = "extra light planes";
491                 else if (plchr[(int)pln.pln_type].pl_flags & P_M)
492                     p = "missiles";
493                 else
494                     p = "planes";
495                 pr("%s cannot carry %s.\n", prship(sp), p);
496             }
497             continue;
498         }
499         /* Fit plane on ship */
500         if (loading) {
501             if (!put_plane_on_ship(&pln, sp)) {
502                 pr("Can't put plane %d on this ship!\n", pln.pln_uid);
503                 continue;
504             }
505             sprintf(buf, "loaded on your %s at %s",
506                     prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
507             gift(sp->shp_own, player->cnum, &pln, buf);
508             putplane(pln.pln_uid, &pln);
509         } else {
510             pln.pln_ship = -1;
511             sprintf(buf, "unloaded in your %s at %s",
512                     dchr[sectp->sct_type].d_name,
513                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
514             gift(sectp->sct_own, player->cnum, &pln, buf);
515             putplane(pln.pln_uid, &pln);
516         }
517         pr("%s %s %s at %s.\n",
518            prplane(&pln),
519            loading ? "loaded onto" : "unloaded from",
520            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
521         loaded = 1;
522     }
523     *nshipsp += loaded;
524     return 0;
525 }
526
527 static int
528 load_land_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
529                int loading, int *nshipsp)
530 {
531     struct nstr_item ni;
532     struct lndstr land;
533     int loaded = 0;
534     char *p;
535     char prompt[512];
536     char buf[1024];
537     int load_spy = 0;
538
539     if (!mchr[(int)sp->shp_type].m_nland
540         && !(mchr[sp->shp_type].m_flags & M_SUB)) {
541         if (noisy)
542             pr("%s cannot carry land units!\n", prship(sp));
543         return 0;
544     }
545     if (loading) {
546         if ((mchr[(int)sp->shp_type].m_flags & M_SUB) &&
547             (mchr[(int)sp->shp_type].m_nland == 0)) {
548             if (shp_nland(sp) >= 2) {
549                 pr("Non-land unit carrying subs can only carry up to two spy units.\n");
550                 return 0;
551             }
552             /* Eh, let 'em load a spy only */
553             load_spy = 1;
554         }
555         if (!load_spy && shp_nland(sp) >= mchr[sp->shp_type].m_nland) {
556             pr("%s doesn't have room for any more land units!\n",
557                prship(sp));
558             return 0;
559         }
560     }
561     sprintf(prompt, "Land unit(s) to %s %s? ",
562             loading ? "load onto" : "unload from", prship(sp));
563     p = getstarg(player->argp[3], prompt, buf);
564     if (!p)
565         return RET_SYN;
566     if (!snxtitem(&ni, EF_LAND, p, NULL))
567         return RET_SYN;
568
569     if (!still_ok_ship(sectp, sp))
570         return RET_SYN;
571
572     noisy = ni.sel == NS_LIST;
573
574     while (nxtitem(&ni, &land)) {
575         if (!player->owner)
576             continue;
577
578         if (loading) {
579             if (!land_loadable(&land, noisy))
580                 continue;
581             if (load_spy && !(lchr[(int)land.lnd_type].l_flags & L_SPY)) {
582                 if (noisy)
583                     pr("Subs can only carry spy units.\n");
584                 continue;
585             }
586         }
587
588         /* Unit sanity done */
589         /* Find the right ship */
590         if (!loading) {
591             if (land.lnd_ship != sp->shp_uid)
592                 continue;
593             if (land.lnd_land > -1)
594                 continue;
595         } else if (sp->shp_x != land.lnd_x || sp->shp_y != land.lnd_y)
596             continue;
597
598         if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
599             (!((mchr[(int)sp->shp_type].m_flags & M_SUPPLY) &&
600                (!(mchr[(int)sp->shp_type].m_flags & M_SUB))))) {
601             if (noisy) {
602                 pr("You can only load light units onto ships,\n");
603                 pr("unless the ship is a non-sub supply ship\n");
604                 pr("%s not loaded\n", prland(&land));
605             }
606             continue;
607         }
608         /* Fit unit on ship */
609         if (loading) {
610             if (load_spy) {
611                 if (shp_nland(sp) >= 2) {
612                     pr("Non-land unit carrying subs can only carry up to two spy units.\n");
613                     return 0;
614                 }
615             } else {
616                 if (shp_nland(sp) >= mchr[sp->shp_type].m_nland) {
617                     pr("%s doesn't have room for any more land units!\n",
618                        prship(sp));
619                     return 0;
620                 }
621             }
622             sprintf(buf, "loaded on your %s at %s",
623                     prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
624             gift(sp->shp_own, player->cnum, &land, buf);
625             land.lnd_ship = sp->shp_uid;
626             land.lnd_harden = 0;
627             putland(land.lnd_uid, &land);
628 #if 0
629            /*
630             * FIXME if this supplies from the sector, the putsect in
631             * load() / lload() duplicates those supplies, causing a
632             * seqno mismatch
633             */
634             if (!lnd_supply_all(&land))
635                 pr("WARNING: %s is out of supply!\n", prland(&land));
636 #else
637             if (!lnd_in_supply(&land))
638                 pr("WARNING: %s is out of supply!\n", prland(&land));
639 #endif
640         } else {
641             sprintf(buf, "unloaded in your %s at %s",
642                     dchr[sectp->sct_type].d_name,
643                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
644
645             /* Spies are unloaded quietly, others aren't */
646             if (!(lchr[(int)land.lnd_type].l_flags & L_SPY))
647                 gift(sectp->sct_own, player->cnum, &land, buf);
648             land.lnd_ship = -1;
649             putland(land.lnd_uid, &land);
650         }
651         pr("%s %s %s at %s.\n",
652            prland(&land),
653            loading ? "loaded onto" : "unloaded from",
654            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
655         loaded = 1;
656     }
657     *nshipsp += loaded;
658     return 0;
659 }
660
661 static int
662 load_comm_ship(struct sctstr *sectp, struct shpstr *sp,
663                struct ichrstr *ich, int loading, int *nshipsp)
664 {
665     i_type item = ich->i_uid;
666     struct mchrstr *mcp = &mchr[(int)sp->shp_type];
667     int ship_amt, sect_amt, move_amt;
668     char prompt[512];
669     char *p;
670     char buf[1024];
671
672     sprintf(prompt, "Number of %s to %s %s at %s? ",
673             ich->i_name,
674             loading ? "load onto" : "unload from",
675             prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
676     p = getstarg(player->argp[3], prompt, buf);
677     if (!p || !*p)
678         return RET_SYN;
679
680     if (!still_ok_ship(sectp, sp))
681         return RET_SYN;
682
683     ship_amt = sp->shp_item[item];
684     sect_amt = sectp->sct_item[item];
685     move_amt = move_amount(sect_amt, ship_amt, mcp->m_item[item],
686                            loading, atoi(p));
687     if (!load_comm_ok(sectp, sp->shp_own, item, move_amt))
688         return RET_OK;
689     if (!abandon_askyn(sectp, item, move_amt, NULL))
690         return RET_FAIL;
691     if (!still_ok_ship(sectp, sp))
692         return RET_SYN;
693     sectp->sct_item[item] = sect_amt - move_amt;
694     sp->shp_item[item] = ship_amt + move_amt;
695
696     if (move_amt >= 0) {
697         pr("%d %s loaded onto %s at %s\n",
698            move_amt, ich->i_name,
699            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
700         if (sp->shp_own != player->cnum) {
701             wu(0, sp->shp_own, "%s loaded %d %s onto %s at %s\n",
702                cname(player->cnum), move_amt, ich->i_name,
703                prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
704         }
705     } else {
706         pr("%d %s unloaded from %s at %s\n",
707            -move_amt, ich->i_name,
708            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
709         if (sectp->sct_own != player->cnum) {
710             wu(0, sectp->sct_own, "%s unloaded %d %s from %s at %s\n",
711                cname(player->cnum), -move_amt, ich->i_name,
712                prship(sp), xyas(sp->shp_x, sp->shp_y, sectp->sct_own));
713         }
714     }
715     ++*nshipsp;
716     return 0;
717 }
718
719 static int
720 load_plane_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
721                 int loading, int *nunitsp)
722 {
723     struct nstr_item ni;
724     struct plnstr pln;
725     int loaded = 0;
726     char *p;
727     char prompt[512];
728     char buf[1024];
729     struct lchrstr *lcp = lchr + lp->lnd_type;
730
731     if (!lcp->l_nxlight) {
732         if (noisy)
733             pr("%s cannot carry extra-light planes.\n", prland(lp));
734         return 0;
735     }
736     if (loading && lnd_nxlight(lp) >= lcp->l_nxlight) {
737         pr("%s doesn't have room for any more extra-light planes\n",
738            prland(lp));
739         return 0;
740     }
741     sprintf(prompt, "Plane(s) to %s %s? ",
742             loading ? "load onto" : "unload from", prland(lp));
743     p = getstarg(player->argp[3], prompt, buf);
744     if (!p)
745         return RET_SYN;
746     if (!snxtitem(&ni, EF_PLANE, p, NULL))
747         return RET_SYN;
748
749     if (!still_ok_land(sectp, lp))
750         return RET_SYN;
751
752     noisy = ni.sel == NS_LIST;
753
754     while (nxtitem(&ni, &pln)) {
755         if (!player->owner)
756             continue;
757
758         if (!(plchr[(int)pln.pln_type].pl_flags & P_E)) {
759             if (noisy)
760                 pr("You can only load xlight planes onto units.\n");
761             continue;
762         }
763         if (loading && !plane_loadable(&pln, noisy))
764             continue;
765
766         /* Plane sanity done */
767         /* Find the right unit */
768         if (!loading) {
769             if (pln.pln_land != lp->lnd_uid)
770                 continue;
771         } else if (lp->lnd_x != pln.pln_x || lp->lnd_y != pln.pln_y)
772             continue;
773
774         /* Fit plane on unit */
775         if (loading) {
776             if (!put_plane_on_land(&pln, lp)) {
777                 pr("Can't put plane %d on this unit!\n", pln.pln_uid);
778                 continue;
779             }
780             sprintf(buf, "loaded on %s at %s",
781                     prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
782             gift(lp->lnd_own, player->cnum, &pln, buf);
783             putplane(pln.pln_uid, &pln);
784         } else {
785             pln.pln_land = -1;
786             sprintf(buf, "unloaded at your sector at %s",
787                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
788             gift(sectp->sct_own, player->cnum, &pln, buf);
789             putplane(pln.pln_uid, &pln);
790         }
791         pr("%s %s %s at %s.\n",
792            prplane(&pln),
793            loading ? "loaded onto" : "unloaded from",
794            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
795         loaded = 1;
796     }
797     *nunitsp += loaded;
798     return 0;
799 }
800
801 static int
802 load_comm_land(struct sctstr *sectp, struct lndstr *lp,
803                struct ichrstr *ich, int loading, int *nunitsp)
804 {
805     i_type item = ich->i_uid;
806     struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
807     int land_amt, sect_amt, move_amt;
808     char prompt[512];
809     char *p;
810     char buf[1024];
811
812     sprintf(prompt, "Number of %s to %s %s at %s? ",
813             ich->i_name,
814             loading ? "load onto" : "unload from",
815             prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
816     p = getstarg(player->argp[3], prompt, buf);
817     if (!p || !*p)
818         return RET_SYN;
819
820     if (!still_ok_land(sectp, lp))
821         return RET_SYN;
822
823     land_amt = lp->lnd_item[item];
824     sect_amt = sectp->sct_item[item];
825     move_amt = move_amount(sect_amt, land_amt, lcp->l_item[item],
826                            loading, atoi(p));
827     if (!load_comm_ok(sectp, lp->lnd_own, item, move_amt))
828         return RET_OK;
829     sectp->sct_item[item] = sect_amt - move_amt;
830     lp->lnd_item[item] = land_amt + move_amt;
831
832     /* Did we put mils onto this unit? If so, reset the fortification */
833     if (item == I_MILIT && move_amt > 0)
834         lp->lnd_harden = 0;
835
836     if (move_amt >= 0) {
837         pr("%d %s loaded onto %s at %s\n",
838            move_amt, ich->i_name,
839            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
840         if (lp->lnd_own != player->cnum) {
841             wu(0, lp->lnd_own, "%s loaded %d %s onto %s at %s\n",
842                cname(player->cnum), move_amt, ich->i_name,
843                prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
844         }
845     } else {
846         pr("%d %s unloaded from %s at %s\n",
847            -move_amt, ich->i_name,
848            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
849         if (sectp->sct_own != player->cnum) {
850             wu(0, sectp->sct_own, "%s unloaded %d %s from %s at %s\n",
851                cname(player->cnum), -move_amt, ich->i_name,
852                prland(lp), xyas(lp->lnd_x, lp->lnd_y, sectp->sct_own));
853         }
854     }
855     ++*nunitsp;
856     return 0;
857 }
858
859 static int
860 load_land_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
861                int loading, int *nunitsp)
862 {
863     struct nstr_item ni;
864     struct lndstr land;
865     int loaded = 0;
866     char *p;
867     char prompt[512];
868     char buf[1024];
869
870     if (!lchr[lp->lnd_type].l_nland) {
871         if (noisy)
872             pr("%s cannot carry land units!\n", prland(lp));
873         return 0;
874     }
875     if (loading && lnd_nland(lp) >= lchr[lp->lnd_type].l_nland) {
876         pr("%s doesn't have room for any more land units!\n",
877            prland(lp));
878         return 0;
879     }
880     sprintf(prompt, "Land unit(s) to %s %s? ",
881             loading ? "load onto" : "unload from", prland(lp));
882     p = getstarg(player->argp[3], prompt, buf);
883     if (!p)
884         return RET_SYN;
885     if (!snxtitem(&ni, EF_LAND, p, NULL))
886         return RET_SYN;
887
888     if (!still_ok_land(sectp, lp))
889         return RET_SYN;
890
891     noisy = ni.sel == NS_LIST;
892
893     while (nxtitem(&ni, &land)) {
894         if (!player->owner)
895             continue;
896
897         if (loading) {
898             if (land.lnd_uid == lp->lnd_uid) {
899                 if (noisy)
900                     pr("%s can't be loaded onto itself!\n", prland(&land));
901                 continue;
902             }
903             if (!land_loadable(&land, noisy))
904                 continue;
905         }
906
907         /* Unit sanity done */
908         /* Find the right ship */
909         if (!loading) {
910             if (land.lnd_land != lp->lnd_uid)
911                 continue;
912             if (land.lnd_ship > -1)
913                 continue;
914         } else if (lp->lnd_x != land.lnd_x || lp->lnd_y != land.lnd_y)
915             continue;
916
917         /* Fit unit on ship */
918         if (loading) {
919             if (lnd_nland(lp) >= lchr[lp->lnd_type].l_nland) {
920                 pr("%s doesn't have room for any more land units!\n",
921                    prland(lp));
922                 break;
923             }
924             sprintf(buf, "loaded on your %s at %s",
925                     prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
926             gift(lp->lnd_own, player->cnum, &land, buf);
927             land.lnd_land = lp->lnd_uid;
928             land.lnd_harden = 0;
929             putland(land.lnd_uid, &land);
930 #if 0
931            /* FIXME same issue as in load_land_ship() */
932             if (!lnd_supply_all(&land))
933                 pr("WARNING: %s is out of supply!\n", prland(&land));
934 #else
935             if (!lnd_in_supply(&land))
936                 pr("WARNING: %s is out of supply!\n", prland(&land));
937 #endif
938         } else {
939             sprintf(buf, "unloaded in your %s at %s",
940                     dchr[sectp->sct_type].d_name,
941                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
942             gift(sectp->sct_own, player->cnum, &land, buf);
943             land.lnd_land = -1;
944             putland(land.lnd_uid, &land);
945         }
946         pr("%s %s %s at %s.\n",
947            prland(&land),
948            loading ? "loaded onto" : "unloaded from",
949            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
950         loaded = 1;
951     }
952     *nunitsp += loaded;
953     return 0;
954 }