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