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