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