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