]> git.pond.sub.org Git - empserver/blob - src/lib/commands/load.c
load: Factor out plane_loadable(), land_loadable()
[empserver] / src / lib / commands / load.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2018, 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     return 1;
384 }
385
386 static int
387 land_loadable(struct lndstr *lp, int noisy)
388 {
389     if (lp->lnd_ship >= 0) {
390         if (noisy)
391             pr("%s is already on ship #%d!\n",
392                prland(lp), lp->lnd_ship);
393         return 0;
394     }
395     if (lp->lnd_land >= 0) {
396         if (noisy)
397             pr("%s is already on land #%d!\n",
398                prland(lp), lp->lnd_land);
399         return 0;
400     }
401     if (lnd_first_on_land(lp) >= 0) {
402         /* Outlawed to prevent arbitrarily deep recursion */
403         if (noisy)
404             pr("%s cannot be loaded since it is carrying units\n",
405                prland(lp));
406         return 0;
407     }
408     if (lchr[lp->lnd_type].l_flags & L_HEAVY) {
409         if (noisy)
410             pr("%s is too heavy to load.\n", prland(lp));
411         return 0;
412     }
413     return 1;
414 }
415
416 static int
417 load_plane_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
418                 int loading, int *nshipsp)
419 {
420     struct nstr_item ni;
421     struct plnstr pln;
422     int loaded = 0;
423     char buf[1024];
424     char *p;
425     char prompt[512];
426     struct mchrstr *mcp = mchr + sp->shp_type;
427
428     if (mcp->m_nplanes + mcp->m_nchoppers + mcp->m_nxlight == 0) {
429         if (noisy)
430             pr("%s cannot carry planes\n", prship(sp));
431         return 0;
432     }
433     if (loading &&
434         shp_nplane(sp, NULL, NULL, NULL)
435                 >= mcp->m_nchoppers + mcp->m_nxlight + mcp->m_nplanes) {
436         pr("%s doesn't have room for any more planes\n", prship(sp));
437         return 0;
438     }
439     sprintf(prompt, "Plane(s) to %s %s? ",
440             loading ? "load onto" : "unload from", prship(sp));
441     p = getstarg(player->argp[3], prompt, buf);
442     if (!p)
443         return RET_SYN;
444     if (!snxtitem(&ni, EF_PLANE, p, NULL))
445         return RET_SYN;
446
447     if (!still_ok_ship(sectp, sp))
448         return RET_SYN;
449
450     noisy = ni.sel == NS_LIST;
451
452     while (nxtitem(&ni, &pln)) {
453         if (!player->owner)
454             continue;
455         if (!(plchr[(int)pln.pln_type].pl_flags & P_L)
456             && !(plchr[(int)pln.pln_type].pl_flags & P_E)
457             && !(plchr[(int)pln.pln_type].pl_flags & P_K)
458             && !(plchr[(int)pln.pln_type].pl_flags & P_M)
459             ) {
460             if (noisy)
461                 pr("You can only load light planes, helos, xtra-light, or missiles onto ships.\n");
462             continue;
463         }
464         if (loading && !plane_loadable(&pln, noisy))
465             continue;
466         if (pln.pln_harden != 0) {
467             if (noisy)
468                 pr("%s has been hardened and can't be loaded\n",
469                    prplane(&pln));
470             continue;
471         }
472
473         if (!loading) {
474             if (pln.pln_ship != sp->shp_uid)
475                 continue;
476         } else if (sp->shp_x != pln.pln_x || sp->shp_y != pln.pln_y)
477             continue;
478
479         if (!could_be_on_ship(&pln, sp)) {
480             if (noisy) {
481                 if (plchr[(int)pln.pln_type].pl_flags & P_K)
482                     p = "choppers";
483                 else if (plchr[(int)pln.pln_type].pl_flags & P_E)
484                     p = "extra light planes";
485                 else if (plchr[(int)pln.pln_type].pl_flags & P_M)
486                     p = "missiles";
487                 else
488                     p = "planes";
489                 pr("%s cannot carry %s.\n", prship(sp), p);
490             }
491             continue;
492         }
493         /* Fit plane on ship */
494         if (loading) {
495             if (!put_plane_on_ship(&pln, sp)) {
496                 pr("Can't put plane %d on this ship!\n", pln.pln_uid);
497                 continue;
498             }
499             sprintf(buf, "loaded on your %s at %s",
500                     prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
501             gift(sp->shp_own, player->cnum, &pln, buf);
502             putplane(pln.pln_uid, &pln);
503         } else {
504             pln.pln_ship = -1;
505             sprintf(buf, "unloaded in your %s at %s",
506                     dchr[sectp->sct_type].d_name,
507                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
508             gift(sectp->sct_own, player->cnum, &pln, buf);
509             putplane(pln.pln_uid, &pln);
510         }
511         pr("%s %s %s at %s.\n",
512            prplane(&pln),
513            loading ? "loaded onto" : "unloaded from",
514            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
515         loaded = 1;
516     }
517     *nshipsp += loaded;
518     return 0;
519 }
520
521 static int
522 load_land_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
523                int loading, int *nshipsp)
524 {
525     struct nstr_item ni;
526     struct lndstr land;
527     int loaded = 0;
528     char *p;
529     char prompt[512];
530     char buf[1024];
531     int load_spy = 0;
532
533     if (!mchr[(int)sp->shp_type].m_nland
534         && !(mchr[sp->shp_type].m_flags & M_SUB)) {
535         if (noisy)
536             pr("%s cannot carry land units!\n", prship(sp));
537         return 0;
538     }
539     if (loading) {
540         if ((mchr[(int)sp->shp_type].m_flags & M_SUB) &&
541             (mchr[(int)sp->shp_type].m_nland == 0)) {
542             if (shp_nland(sp) >= 2) {
543                 pr("Non-land unit carrying subs can only carry up to two spy units.\n");
544                 return 0;
545             }
546             /* Eh, let 'em load a spy only */
547             load_spy = 1;
548         }
549         if (!load_spy && shp_nland(sp) >= mchr[sp->shp_type].m_nland) {
550             pr("%s doesn't have room for any more land units!\n",
551                prship(sp));
552             return 0;
553         }
554     }
555     sprintf(prompt, "Land unit(s) to %s %s? ",
556             loading ? "load onto" : "unload from", prship(sp));
557     p = getstarg(player->argp[3], prompt, buf);
558     if (!p)
559         return RET_SYN;
560     if (!snxtitem(&ni, EF_LAND, p, NULL))
561         return RET_SYN;
562
563     if (!still_ok_ship(sectp, sp))
564         return RET_SYN;
565
566     noisy = ni.sel == NS_LIST;
567
568     while (nxtitem(&ni, &land)) {
569         if (!player->owner)
570             continue;
571
572         if (loading) {
573             if (!land_loadable(&land, noisy))
574                 continue;
575             if (load_spy && !(lchr[(int)land.lnd_type].l_flags & L_SPY)) {
576                 if (noisy)
577                     pr("Subs can only carry spy units.\n");
578                 continue;
579             }
580         }
581
582         /* Unit sanity done */
583         /* Find the right ship */
584         if (!loading) {
585             if (land.lnd_ship != sp->shp_uid)
586                 continue;
587             if (land.lnd_land > -1)
588                 continue;
589         } else if (sp->shp_x != land.lnd_x || sp->shp_y != land.lnd_y)
590             continue;
591
592         if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
593             (!((mchr[(int)sp->shp_type].m_flags & M_SUPPLY) &&
594                (!(mchr[(int)sp->shp_type].m_flags & M_SUB))))) {
595             if (noisy) {
596                 pr("You can only load light units onto ships,\n");
597                 pr("unless the ship is a non-sub supply ship\n");
598                 pr("%s not loaded\n", prland(&land));
599             }
600             continue;
601         }
602         /* Fit unit on ship */
603         if (loading) {
604             if (load_spy) {
605                 if (shp_nland(sp) >= 2) {
606                     pr("Non-land unit carrying subs can only carry up to two spy units.\n");
607                     return 0;
608                 }
609             } else {
610                 if (shp_nland(sp) >= mchr[sp->shp_type].m_nland) {
611                     pr("%s doesn't have room for any more land units!\n",
612                        prship(sp));
613                     return 0;
614                 }
615             }
616             sprintf(buf, "loaded on your %s at %s",
617                     prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
618             gift(sp->shp_own, player->cnum, &land, buf);
619             land.lnd_ship = sp->shp_uid;
620             land.lnd_harden = 0;
621             putland(land.lnd_uid, &land);
622 #if 0
623            /*
624             * FIXME if this supplies from the sector, the putsect in
625             * load() / lload() duplicates those supplies, causing a
626             * seqno mismatch
627             */
628             if (!lnd_supply_all(&land))
629                 pr("WARNING: %s is out of supply!\n", prland(&land));
630 #else
631             if (!lnd_in_supply(&land))
632                 pr("WARNING: %s is out of supply!\n", prland(&land));
633 #endif
634         } else {
635             sprintf(buf, "unloaded in your %s at %s",
636                     dchr[sectp->sct_type].d_name,
637                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
638
639             /* Spies are unloaded quietly, others aren't */
640             if (!(lchr[(int)land.lnd_type].l_flags & L_SPY))
641                 gift(sectp->sct_own, player->cnum, &land, buf);
642             land.lnd_ship = -1;
643             putland(land.lnd_uid, &land);
644         }
645         pr("%s %s %s at %s.\n",
646            prland(&land),
647            loading ? "loaded onto" : "unloaded from",
648            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
649         loaded = 1;
650     }
651     *nshipsp += loaded;
652     return 0;
653 }
654
655 static int
656 load_comm_ship(struct sctstr *sectp, struct shpstr *sp,
657                struct ichrstr *ich, int loading, int *nshipsp)
658 {
659     i_type item = ich->i_uid;
660     struct mchrstr *mcp = &mchr[(int)sp->shp_type];
661     int ship_amt, sect_amt, move_amt;
662     char prompt[512];
663     char *p;
664     char buf[1024];
665
666     sprintf(prompt, "Number of %s to %s %s at %s? ",
667             ich->i_name,
668             loading ? "load onto" : "unload from",
669             prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
670     p = getstarg(player->argp[3], prompt, buf);
671     if (!p || !*p)
672         return RET_SYN;
673
674     if (!still_ok_ship(sectp, sp))
675         return RET_SYN;
676
677     ship_amt = sp->shp_item[item];
678     sect_amt = sectp->sct_item[item];
679     move_amt = move_amount(sect_amt, ship_amt, mcp->m_item[item],
680                            loading, atoi(p));
681     if (!load_comm_ok(sectp, sp->shp_own, item, move_amt))
682         return RET_OK;
683     if (!abandon_askyn(sectp, item, move_amt, NULL))
684         return RET_FAIL;
685     if (!still_ok_ship(sectp, sp))
686         return RET_SYN;
687     sectp->sct_item[item] = sect_amt - move_amt;
688     sp->shp_item[item] = ship_amt + move_amt;
689
690     if (move_amt >= 0) {
691         pr("%d %s loaded onto %s at %s\n",
692            move_amt, ich->i_name,
693            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
694         if (sp->shp_own != player->cnum) {
695             wu(0, sp->shp_own, "%s loaded %d %s onto %s at %s\n",
696                cname(player->cnum), move_amt, ich->i_name,
697                prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
698         }
699     } else {
700         pr("%d %s unloaded from %s at %s\n",
701            -move_amt, ich->i_name,
702            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
703         if (sectp->sct_own != player->cnum) {
704             wu(0, sectp->sct_own, "%s unloaded %d %s from %s at %s\n",
705                cname(player->cnum), -move_amt, ich->i_name,
706                prship(sp), xyas(sp->shp_x, sp->shp_y, sectp->sct_own));
707         }
708     }
709     ++*nshipsp;
710     return 0;
711 }
712
713 static int
714 load_plane_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
715                 int loading, int *nunitsp)
716 {
717     struct nstr_item ni;
718     struct plnstr pln;
719     int loaded = 0;
720     char *p;
721     char prompt[512];
722     char buf[1024];
723     struct lchrstr *lcp = lchr + lp->lnd_type;
724
725     if (!lcp->l_nxlight) {
726         if (noisy)
727             pr("%s cannot carry extra-light planes.\n", prland(lp));
728         return 0;
729     }
730     if (loading && lnd_nxlight(lp) >= lcp->l_nxlight) {
731         pr("%s doesn't have room for any more extra-light planes\n",
732            prland(lp));
733         return 0;
734     }
735     sprintf(prompt, "Plane(s) to %s %s? ",
736             loading ? "load onto" : "unload from", prland(lp));
737     p = getstarg(player->argp[3], prompt, buf);
738     if (!p)
739         return RET_SYN;
740     if (!snxtitem(&ni, EF_PLANE, p, NULL))
741         return RET_SYN;
742
743     if (!still_ok_land(sectp, lp))
744         return RET_SYN;
745
746     noisy = ni.sel == NS_LIST;
747
748     while (nxtitem(&ni, &pln)) {
749         if (!player->owner)
750             continue;
751
752         if (!(plchr[(int)pln.pln_type].pl_flags & P_E)) {
753             if (noisy)
754                 pr("You can only load xlight planes onto units.\n");
755             continue;
756         }
757         if (loading && !plane_loadable(&pln, noisy))
758             continue;
759         if (pln.pln_harden != 0) {
760             if (noisy)
761                 pr("%s has been hardened and can't be loaded\n",
762                    prplane(&pln));
763             continue;
764         }
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 }