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