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