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