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