]> git.pond.sub.org Git - empserver/blob - src/lib/commands/load.c
Revert "Don't let trains load trains"
[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 load_plane_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
370                 int loading, int *nshipsp)
371 {
372     struct nstr_item ni;
373     struct plnstr pln;
374     int loaded = 0;
375     char buf[1024];
376     char *p;
377     char prompt[512];
378     struct mchrstr *mcp = mchr + sp->shp_type;
379
380     if (mcp->m_nplanes + mcp->m_nchoppers + mcp->m_nxlight == 0) {
381         if (noisy)
382             pr("%s cannot carry planes\n", prship(sp));
383         return 0;
384     }
385     if (loading &&
386         shp_nplane(sp, NULL, NULL, NULL)
387                 >= mcp->m_nchoppers + mcp->m_nxlight + mcp->m_nplanes) {
388         pr("%s doesn't have room for any more planes\n", prship(sp));
389         return 0;
390     }
391     sprintf(prompt, "Plane(s) to %s %s? ",
392             loading ? "load onto" : "unload from", prship(sp));
393     p = getstarg(player->argp[3], prompt, buf);
394     if (!p)
395         return RET_SYN;
396     if (!snxtitem(&ni, EF_PLANE, p, NULL))
397         return RET_SYN;
398
399     if (!still_ok_ship(sectp, sp))
400         return RET_SYN;
401
402     noisy = ni.sel == NS_LIST;
403
404     while (nxtitem(&ni, &pln)) {
405         if (!player->owner)
406             continue;
407         if (!(plchr[(int)pln.pln_type].pl_flags & P_L)
408             && !(plchr[(int)pln.pln_type].pl_flags & P_E)
409             && !(plchr[(int)pln.pln_type].pl_flags & P_K)
410             && !(plchr[(int)pln.pln_type].pl_flags & P_M)
411             ) {
412             if (noisy)
413                 pr("You can only load light planes, helos, xtra-light, or missiles onto ships.\n");
414             continue;
415         }
416         if (loading && pln.pln_ship > -1) {
417             if (noisy)
418                 pr("%s is already on ship #%d!\n",
419                    prplane(&pln), pln.pln_ship);
420             continue;
421         }
422         if (loading && pln.pln_land > -1) {
423             if (noisy)
424                 pr("%s is already on land unit #%d!\n",
425                    prplane(&pln), pln.pln_land);
426             continue;
427         }
428         if (pln.pln_harden != 0) {
429             if (noisy)
430                 pr("%s has been hardened and can't be loaded\n",
431                    prplane(&pln));
432             continue;
433         }
434
435         if (!loading) {
436             if (pln.pln_ship != sp->shp_uid)
437                 continue;
438         } else if (sp->shp_x != pln.pln_x || sp->shp_y != pln.pln_y)
439             continue;
440
441         if (!could_be_on_ship(&pln, sp)) {
442             if (noisy) {
443                 if (plchr[(int)pln.pln_type].pl_flags & P_K)
444                     p = "choppers";
445                 else if (plchr[(int)pln.pln_type].pl_flags & P_E)
446                     p = "extra light planes";
447                 else if (plchr[(int)pln.pln_type].pl_flags & P_M)
448                     p = "missiles";
449                 else
450                     p = "planes";
451                 pr("%s cannot carry %s.\n", prship(sp), p);
452             }
453             continue;
454         }
455         /* Fit plane on ship */
456         if (loading) {
457             if (!put_plane_on_ship(&pln, sp)) {
458                 pr("Can't put plane %d on this ship!\n", pln.pln_uid);
459                 continue;
460             }
461             sprintf(buf, "loaded on your %s at %s",
462                     prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
463             gift(sp->shp_own, player->cnum, &pln, buf);
464             putplane(pln.pln_uid, &pln);
465         } else {
466             pln.pln_ship = -1;
467             sprintf(buf, "unloaded in your %s at %s",
468                     dchr[sectp->sct_type].d_name,
469                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
470             gift(sectp->sct_own, player->cnum, &pln, buf);
471             putplane(pln.pln_uid, &pln);
472         }
473         pr("%s %s %s at %s.\n",
474            prplane(&pln),
475            loading ? "loaded onto" : "unloaded from",
476            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
477         loaded = 1;
478     }
479     *nshipsp += loaded;
480     return 0;
481 }
482
483 static int
484 load_land_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
485                int loading, int *nshipsp)
486 {
487     struct nstr_item ni;
488     struct lndstr land;
489     int loaded = 0;
490     char *p;
491     char prompt[512];
492     char buf[1024];
493     int load_spy = 0;
494
495     if (!mchr[(int)sp->shp_type].m_nland
496         && !(mchr[sp->shp_type].m_flags & M_SUB)) {
497         if (noisy)
498             pr("%s cannot carry land units!\n", prship(sp));
499         return 0;
500     }
501     if (loading) {
502         if ((mchr[(int)sp->shp_type].m_flags & M_SUB) &&
503             (mchr[(int)sp->shp_type].m_nland == 0)) {
504             if (shp_nland(sp) >= 2) {
505                 pr("Non-land unit carrying subs can only carry up to two spy units.\n");
506                 return 0;
507             }
508             /* Eh, let 'em load a spy only */
509             load_spy = 1;
510         }
511         if (!load_spy && shp_nland(sp) >= mchr[sp->shp_type].m_nland) {
512             pr("%s doesn't have room for any more land units!\n",
513                prship(sp));
514             return 0;
515         }
516     }
517     sprintf(prompt, "Land unit(s) to %s %s? ",
518             loading ? "load onto" : "unload from", prship(sp));
519     p = getstarg(player->argp[3], prompt, buf);
520     if (!p)
521         return RET_SYN;
522     if (!snxtitem(&ni, EF_LAND, p, NULL))
523         return RET_SYN;
524
525     if (!still_ok_ship(sectp, sp))
526         return RET_SYN;
527
528     noisy = ni.sel == NS_LIST;
529
530     while (nxtitem(&ni, &land)) {
531         if (!player->owner)
532             continue;
533
534         if (loading) {
535             if (land.lnd_ship > -1) {
536                 if (noisy)
537                     pr("%s is already on ship #%d!\n",
538                        prland(&land), land.lnd_ship);
539                 continue;
540             }
541             if (land.lnd_land > -1) {
542                 if (noisy)
543                     pr("%s is already on land #%d!\n",
544                        prland(&land), land.lnd_land);
545                 continue;
546             }
547             if (lnd_first_on_land(&land) >= 0) {
548                 if (noisy)
549                     pr("%s cannot be loaded since it is carrying units\n",
550                        prland(&land));
551                 continue;
552             }
553             if (lchr[(int)land.lnd_type].l_flags & L_HEAVY) {
554                 if (noisy)
555                     pr("%s is too heavy to load.\n", prland(&land));
556                 continue;
557             }
558             if (load_spy && !(lchr[(int)land.lnd_type].l_flags & L_SPY)) {
559                 if (noisy)
560                     pr("Subs can only carry spy units.\n");
561                 continue;
562             }
563         }
564
565         /* Unit sanity done */
566         /* Find the right ship */
567         if (!loading) {
568             if (land.lnd_ship != sp->shp_uid)
569                 continue;
570             if (land.lnd_land > -1)
571                 continue;
572         } else if (sp->shp_x != land.lnd_x || sp->shp_y != land.lnd_y)
573             continue;
574
575         if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
576             (!((mchr[(int)sp->shp_type].m_flags & M_SUPPLY) &&
577                (!(mchr[(int)sp->shp_type].m_flags & M_SUB))))) {
578             if (noisy) {
579                 pr("You can only load light units onto ships,\n");
580                 pr("unless the ship is a non-sub supply ship\n");
581                 pr("%s not loaded\n", prland(&land));
582             }
583             continue;
584         }
585         /* Fit unit on ship */
586         if (loading) {
587             if (load_spy) {
588                 if (shp_nland(sp) >= 2) {
589                     pr("Non-land unit carrying subs can only carry up to two spy units.\n");
590                     return 0;
591                 }
592             } else {
593                 if (shp_nland(sp) >= mchr[sp->shp_type].m_nland) {
594                     pr("%s doesn't have room for any more land units!\n",
595                        prship(sp));
596                     return 0;
597                 }
598             }
599             sprintf(buf, "loaded on your %s at %s",
600                     prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
601             gift(sp->shp_own, player->cnum, &land, buf);
602             land.lnd_ship = sp->shp_uid;
603             land.lnd_harden = 0;
604             putland(land.lnd_uid, &land);
605 #if 0
606            /*
607             * FIXME if this supplies from the sector, the putsect in
608             * load() / lload() duplicates those supplies, causing a
609             * seqno mismatch
610             */
611             if (!lnd_supply_all(&land))
612                 pr("WARNING: %s is out of supply!\n", prland(&land));
613 #else
614             if (!lnd_in_supply(&land))
615                 pr("WARNING: %s is out of supply!\n", prland(&land));
616 #endif
617         } else {
618             sprintf(buf, "unloaded in your %s at %s",
619                     dchr[sectp->sct_type].d_name,
620                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
621
622             /* Spies are unloaded quietly, others aren't */
623             if (!(lchr[(int)land.lnd_type].l_flags & L_SPY))
624                 gift(sectp->sct_own, player->cnum, &land, buf);
625             land.lnd_ship = -1;
626             putland(land.lnd_uid, &land);
627         }
628         pr("%s %s %s at %s.\n",
629            prland(&land),
630            loading ? "loaded onto" : "unloaded from",
631            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
632         loaded = 1;
633     }
634     *nshipsp += loaded;
635     return 0;
636 }
637
638 static int
639 load_comm_ship(struct sctstr *sectp, struct shpstr *sp,
640                struct ichrstr *ich, int loading, int *nshipsp)
641 {
642     i_type item = ich->i_uid;
643     struct mchrstr *mcp = &mchr[(int)sp->shp_type];
644     int ship_amt, sect_amt, move_amt;
645     char prompt[512];
646     char *p;
647     char buf[1024];
648
649     sprintf(prompt, "Number of %s to %s %s at %s? ",
650             ich->i_name,
651             loading ? "load onto" : "unload from",
652             prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
653     p = getstarg(player->argp[3], prompt, buf);
654     if (!p || !*p)
655         return RET_SYN;
656
657     if (!still_ok_ship(sectp, sp))
658         return RET_SYN;
659
660     ship_amt = sp->shp_item[item];
661     sect_amt = sectp->sct_item[item];
662     move_amt = move_amount(sect_amt, ship_amt, mcp->m_item[item],
663                            loading, atoi(p));
664     if (!load_comm_ok(sectp, sp->shp_own, item, move_amt))
665         return RET_OK;
666     if (!abandon_askyn(sectp, item, move_amt, NULL))
667         return RET_FAIL;
668     if (!still_ok_ship(sectp, sp))
669         return RET_SYN;
670     sectp->sct_item[item] = sect_amt - move_amt;
671     sp->shp_item[item] = ship_amt + move_amt;
672
673     if (move_amt >= 0) {
674         pr("%d %s loaded onto %s at %s\n",
675            move_amt, ich->i_name,
676            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
677         if (sp->shp_own != player->cnum) {
678             wu(0, sp->shp_own, "%s loaded %d %s onto %s at %s\n",
679                cname(player->cnum), move_amt, ich->i_name,
680                prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
681         }
682     } else {
683         pr("%d %s unloaded from %s at %s\n",
684            -move_amt, ich->i_name,
685            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
686         if (sectp->sct_own != player->cnum) {
687             wu(0, sectp->sct_own, "%s unloaded %d %s from %s at %s\n",
688                cname(player->cnum), -move_amt, ich->i_name,
689                prship(sp), xyas(sp->shp_x, sp->shp_y, sectp->sct_own));
690         }
691     }
692     ++*nshipsp;
693     return 0;
694 }
695
696 static int
697 load_plane_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
698                 int loading, int *nunitsp)
699 {
700     struct nstr_item ni;
701     struct plnstr pln;
702     int loaded = 0;
703     char *p;
704     char prompt[512];
705     char buf[1024];
706     struct lchrstr *lcp = lchr + lp->lnd_type;
707
708     if (!lcp->l_nxlight) {
709         if (noisy)
710             pr("%s cannot carry extra-light planes.\n", prland(lp));
711         return 0;
712     }
713     if (loading && lnd_nxlight(lp) >= lcp->l_nxlight) {
714         pr("%s doesn't have room for any more extra-light planes\n",
715            prland(lp));
716         return 0;
717     }
718     sprintf(prompt, "Plane(s) to %s %s? ",
719             loading ? "load onto" : "unload from", prland(lp));
720     p = getstarg(player->argp[3], prompt, buf);
721     if (!p)
722         return RET_SYN;
723     if (!snxtitem(&ni, EF_PLANE, p, NULL))
724         return RET_SYN;
725
726     if (!still_ok_land(sectp, lp))
727         return RET_SYN;
728
729     noisy = ni.sel == NS_LIST;
730
731     while (nxtitem(&ni, &pln)) {
732         if (!player->owner)
733             continue;
734
735         if (!(plchr[(int)pln.pln_type].pl_flags & P_E)) {
736             if (noisy)
737                 pr("You can only load xlight planes onto units.\n");
738             continue;
739         }
740
741         if (loading && pln.pln_ship > -1) {
742             if (noisy)
743                 pr("%s is already on ship #%d!\n",
744                    prplane(&pln), pln.pln_ship);
745             continue;
746         }
747         if (loading && pln.pln_land > -1) {
748             if (noisy)
749                 pr("%s is already on unit #%d!\n",
750                    prplane(&pln), pln.pln_land);
751             continue;
752         }
753         if (pln.pln_harden != 0) {
754             if (noisy)
755                 pr("%s has been hardened and can't be loaded\n",
756                    prplane(&pln));
757             continue;
758         }
759
760         /* Plane sanity done */
761         /* Find the right unit */
762         if (!loading) {
763             if (pln.pln_land != lp->lnd_uid)
764                 continue;
765         } else if (lp->lnd_x != pln.pln_x || lp->lnd_y != pln.pln_y)
766             continue;
767
768         /* Fit plane on unit */
769         if (loading) {
770             if (!put_plane_on_land(&pln, lp)) {
771                 pr("Can't put plane %d on this unit!\n", pln.pln_uid);
772                 continue;
773             }
774             sprintf(buf, "loaded on %s at %s",
775                     prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
776             gift(lp->lnd_own, player->cnum, &pln, buf);
777             putplane(pln.pln_uid, &pln);
778         } else {
779             pln.pln_land = -1;
780             sprintf(buf, "unloaded at your sector at %s",
781                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
782             gift(sectp->sct_own, player->cnum, &pln, buf);
783             putplane(pln.pln_uid, &pln);
784         }
785         pr("%s %s %s at %s.\n",
786            prplane(&pln),
787            loading ? "loaded onto" : "unloaded from",
788            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
789         loaded = 1;
790     }
791     *nunitsp += loaded;
792     return 0;
793 }
794
795 static int
796 load_comm_land(struct sctstr *sectp, struct lndstr *lp,
797                struct ichrstr *ich, int loading, int *nunitsp)
798 {
799     i_type item = ich->i_uid;
800     struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
801     int land_amt, sect_amt, move_amt;
802     char prompt[512];
803     char *p;
804     char buf[1024];
805
806     sprintf(prompt, "Number of %s to %s %s at %s? ",
807             ich->i_name,
808             loading ? "load onto" : "unload from",
809             prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
810     p = getstarg(player->argp[3], prompt, buf);
811     if (!p || !*p)
812         return RET_SYN;
813
814     if (!still_ok_land(sectp, lp))
815         return RET_SYN;
816
817     land_amt = lp->lnd_item[item];
818     sect_amt = sectp->sct_item[item];
819     move_amt = move_amount(sect_amt, land_amt, lcp->l_item[item],
820                            loading, atoi(p));
821     if (!load_comm_ok(sectp, lp->lnd_own, item, move_amt))
822         return RET_OK;
823     sectp->sct_item[item] = sect_amt - move_amt;
824     lp->lnd_item[item] = land_amt + move_amt;
825
826     /* Did we put mils onto this unit? If so, reset the fortification */
827     if (item == I_MILIT && move_amt > 0)
828         lp->lnd_harden = 0;
829
830     if (move_amt >= 0) {
831         pr("%d %s loaded onto %s at %s\n",
832            move_amt, ich->i_name,
833            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
834         if (lp->lnd_own != player->cnum) {
835             wu(0, lp->lnd_own, "%s loaded %d %s onto %s at %s\n",
836                cname(player->cnum), move_amt, ich->i_name,
837                prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
838         }
839     } else {
840         pr("%d %s unloaded from %s at %s\n",
841            -move_amt, ich->i_name,
842            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
843         if (sectp->sct_own != player->cnum) {
844             wu(0, sectp->sct_own, "%s unloaded %d %s from %s at %s\n",
845                cname(player->cnum), -move_amt, ich->i_name,
846                prland(lp), xyas(lp->lnd_x, lp->lnd_y, sectp->sct_own));
847         }
848     }
849     ++*nunitsp;
850     return 0;
851 }
852
853 static int
854 load_land_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
855                int loading, int *nunitsp)
856 {
857     struct nstr_item ni;
858     struct lndstr land;
859     int loaded = 0;
860     char *p;
861     char prompt[512];
862     char buf[1024];
863
864     if (!lchr[lp->lnd_type].l_nland) {
865         if (noisy)
866             pr("%s cannot carry land units!\n", prland(lp));
867         return 0;
868     }
869     if (loading && lnd_nland(lp) >= lchr[lp->lnd_type].l_nland) {
870         pr("%s doesn't have room for any more land units!\n",
871            prland(lp));
872         return 0;
873     }
874     sprintf(prompt, "Land unit(s) to %s %s? ",
875             loading ? "load onto" : "unload from", prland(lp));
876     p = getstarg(player->argp[3], prompt, buf);
877     if (!p)
878         return RET_SYN;
879     if (!snxtitem(&ni, EF_LAND, p, NULL))
880         return RET_SYN;
881
882     if (!still_ok_land(sectp, lp))
883         return RET_SYN;
884
885     noisy = ni.sel == NS_LIST;
886
887     while (nxtitem(&ni, &land)) {
888         if (!player->owner)
889             continue;
890
891         if (loading) {
892             if (land.lnd_ship > -1) {
893                 if (noisy)
894                     pr("%s is already on ship #%d!\n",
895                        prland(&land), land.lnd_ship);
896                 continue;
897             }
898             if (land.lnd_land > -1) {
899                 if (noisy)
900                     pr("%s is already on land #%d!\n",
901                        prland(&land), land.lnd_land);
902                 continue;
903             }
904             if (lnd_first_on_land(&land) >= 0) {
905                 if (noisy)
906                     pr("%s cannot be loaded since it is carrying units\n",
907                        prland(&land));
908                 continue;
909             }
910             if (land.lnd_uid == lp->lnd_uid) {
911                 if (noisy)
912                     pr("%s can't be loaded onto itself!\n", prland(&land));
913                 continue;
914             }
915             if (lchr[(int)land.lnd_type].l_flags & L_HEAVY) {
916                 if (noisy)
917                     pr("%s is too heavy to load.\n", prland(&land));
918                 continue;
919             }
920         }
921
922         /* Unit sanity done */
923         /* Find the right ship */
924         if (!loading) {
925             if (land.lnd_land != lp->lnd_uid)
926                 continue;
927             if (land.lnd_ship > -1)
928                 continue;
929         } else if (lp->lnd_x != land.lnd_x || lp->lnd_y != land.lnd_y)
930             continue;
931
932         /* Fit unit on ship */
933         if (loading) {
934             if (lnd_nland(lp) >= lchr[lp->lnd_type].l_nland) {
935                 pr("%s doesn't have room for any more land units!\n",
936                    prland(lp));
937                 break;
938             }
939             sprintf(buf, "loaded on your %s at %s",
940                     prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
941             gift(lp->lnd_own, player->cnum, &land, buf);
942             land.lnd_land = lp->lnd_uid;
943             land.lnd_harden = 0;
944             putland(land.lnd_uid, &land);
945 #if 0
946            /* FIXME same issue as in load_land_ship() */
947             if (!lnd_supply_all(&land))
948                 pr("WARNING: %s is out of supply!\n", prland(&land));
949 #else
950             if (!lnd_in_supply(&land))
951                 pr("WARNING: %s is out of supply!\n", prland(&land));
952 #endif
953         } else {
954             sprintf(buf, "unloaded in your %s at %s",
955                     dchr[sectp->sct_type].d_name,
956                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
957             gift(sectp->sct_own, player->cnum, &land, buf);
958             land.lnd_land = -1;
959             putland(land.lnd_uid, &land);
960         }
961         pr("%s %s %s at %s.\n",
962            prland(&land),
963            loading ? "loaded onto" : "unloaded from",
964            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
965         loaded = 1;
966     }
967     *nunitsp += loaded;
968     return 0;
969 }