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