]> git.pond.sub.org Git - empserver/blob - src/lib/commands/load.c
Fix relation checks in load, unload, lload, lunload
[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
252         if (player->cnum != land.lnd_own &&
253             getrel(getnatp(land.lnd_own), player->cnum) != 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), player->cnum) != 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 #if 0
598            /*
599             * FIXME if this supplies from the sector, the putsect in
600             * load() / lload() duplicates those supplies, causing a
601             * seqno mismatch
602             */
603             resupply_all(&land);
604 #endif
605             putland(land.lnd_uid, &land);
606             if (!has_supply(&land))
607                 pr("WARNING: %s is out of supply!\n", prland(&land));
608         } else {
609             sprintf(buf, "unloaded in your %s at %s",
610                     dchr[sectp->sct_type].d_name,
611                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
612
613             /* Spies are unloaded quietly, others aren't */
614             if (!(lchr[(int)land.lnd_type].l_flags & L_SPY))
615                 gift(sectp->sct_own, player->cnum, &land, buf);
616             land.lnd_ship = -1;
617             putland(land.lnd_uid, &land);
618         }
619         pr("%s %s %s at %s.\n",
620            prland(&land),
621            (load_unload == UNLOAD) ?
622            "unloaded from" : "loaded onto",
623            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
624         loaded = 1;
625     }
626     *nshipsp += loaded;
627     return 0;
628 }
629
630 static int
631 load_comm_ship(struct sctstr *sectp, struct shpstr *sp,
632                struct ichrstr *ich, int load_unload, int *nshipsp)
633 {
634     i_type item = ich->i_uid;
635     struct mchrstr *mcp = &mchr[(int)sp->shp_type];
636     int ship_amt, ship_max, sect_amt, move_amt;
637     int amount;
638     char prompt[512];
639     char *p;
640     char buf[1024];
641
642     sprintf(prompt, "Number of %s to %s %s at %s? ",
643             ich->i_name,
644             (load_unload == UNLOAD) ?
645             "unload from" : "load onto",
646             prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
647     if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
648         return RET_SYN;
649
650     if (!still_ok_ship(sectp, sp))
651         return RET_SYN;
652
653     ship_amt = sp->shp_item[item];
654     ship_max = mcp->m_item[item];
655     sect_amt = sectp->sct_item[item];
656     amount = atoi(p);
657     if (amount < 0)
658         move_amt = -amount - ship_amt;
659     else
660         move_amt = load_unload == LOAD ? amount : -amount;
661     if (move_amt > ship_max - ship_amt)
662         move_amt = ship_max - ship_amt;
663     if (move_amt < -ship_amt)
664         move_amt = -ship_amt;
665     if (move_amt > sect_amt)
666         move_amt = sect_amt;
667     if (move_amt < sect_amt - ITEM_MAX)
668         move_amt = sect_amt - ITEM_MAX;
669     if (!move_amt)
670         return RET_OK;
671     if (sectp->sct_oldown != player->cnum && item == I_CIVIL) {
672         pr("%s civilians refuse to %s at %s!\n",
673            move_amt < 0 ? "Your" : "Foreign",
674            move_amt < 0 ? "disembark" : "board",
675            xyas(sectp->sct_x, sectp->sct_y, player->cnum));
676         return RET_FAIL;
677     }
678
679     if (!want_to_abandon(sectp, item, move_amt, 0))
680         return RET_FAIL;
681     if (!still_ok_ship(sectp, sp))
682         return RET_SYN;
683     sectp->sct_item[item] = sect_amt - move_amt;
684     sp->shp_item[item] = ship_amt + move_amt;
685     if (move_amt >= 0) {
686         pr("%d %s loaded onto %s at %s\n",
687            move_amt, ich->i_name,
688            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
689         if (sp->shp_own != player->cnum) {
690             wu(0, sp->shp_own, "%s loaded %d %s onto %s at %s\n",
691                cname(player->cnum), move_amt, ich->i_name,
692                prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
693         }
694     } else {
695         pr("%d %s unloaded from %s at %s\n",
696            -move_amt, ich->i_name,
697            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
698         if (sectp->sct_own != player->cnum) {
699             wu(0, sectp->sct_own, "%s unloaded %d %s from %s at %s\n",
700                cname(player->cnum), -move_amt, ich->i_name,
701                prship(sp), xyas(sp->shp_x, sp->shp_y, sectp->sct_own));
702         }
703     }
704     ++*nshipsp;
705     return 0;
706 }
707
708 static int
709 load_plane_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
710                 int load_unload, int *nunitsp)
711 {
712     struct nstr_item ni;
713     struct plnstr pln;
714     int loaded = 0;
715     char *p;
716     char prompt[512];
717     char buf[1024];
718     struct lchrstr *lcp = lchr + lp->lnd_type;
719
720     if (!lcp->l_nxlight) {
721         if (noisy)
722             pr("%s cannot carry extra-light planes.\n", prland(lp));
723         return 0;
724     }
725     if (load_unload == LOAD && lnd_nxlight(lp) >= lcp->l_nxlight) {
726         if (noisy)
727             pr("%s doesn't have room for any more extra-light planes\n",
728                prland(lp));
729         return 0;
730     }
731     sprintf(prompt, "Plane(s) to %s %s? ",
732             load_unload == LOAD ? "load onto" : "unload from", prland(lp));
733     p = getstarg(player->argp[3], prompt, buf);
734     if (!p)
735         return RET_SYN;
736     if (!snxtitem(&ni, EF_PLANE, p, NULL))
737         return RET_SYN;
738
739     if (!still_ok_land(sectp, lp))
740         return RET_SYN;
741
742     if (noisy && p && *p)
743         noisy = isdigit(*p);
744
745     if (sectp->sct_own != player->cnum && load_unload == LOAD) {
746         pr("Sector %s is not yours.\n",
747            xyas(lp->lnd_x, lp->lnd_y, player->cnum));
748         return 0;
749     }
750
751     while (nxtitem(&ni, &pln)) {
752         if (pln.pln_own != player->cnum)
753             continue;
754
755         if (!(plchr[(int)pln.pln_type].pl_flags & P_E)) {
756             if (noisy)
757                 pr("You can only load xlight planes onto units.\n");
758             continue;
759         }
760
761         if (load_unload == LOAD && pln.pln_ship > -1) {
762             if (noisy)
763                 pr("%s is already on ship #%d!\n",
764                    prplane(&pln), pln.pln_ship);
765             continue;
766         }
767         if (load_unload == LOAD && pln.pln_land > -1) {
768             if (noisy)
769                 pr("%s is already on unit #%d!\n",
770                    prplane(&pln), pln.pln_land);
771             continue;
772         }
773         if (pln.pln_harden != 0) {
774             if (noisy)
775                 pr("%s has been hardened and can't be loaded\n",
776                    prplane(&pln));
777             continue;
778         }
779
780         /* Plane sanity done */
781         /* Find the right unit */
782         if (load_unload == UNLOAD) {
783             if (pln.pln_land != lp->lnd_uid)
784                 continue;
785         } else if (lp->lnd_x != pln.pln_x || lp->lnd_y != pln.pln_y)
786             continue;
787
788         /* Fit plane on unit */
789         if (load_unload == LOAD) {
790             if (!put_plane_on_land(&pln, lp)) {
791                 if (noisy)
792                     pr("Can't put plane %d on this unit!\n", pln.pln_uid);
793                 continue;
794             }
795             sprintf(buf, "loaded on %s at %s",
796                     prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
797             gift(lp->lnd_own, player->cnum, &pln, buf);
798             putplane(pln.pln_uid, &pln);
799         } else {
800             pln.pln_land = -1;
801             sprintf(buf, "unloaded at your sector at %s",
802                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
803             gift(sectp->sct_own, player->cnum, &pln, buf);
804             putplane(pln.pln_uid, &pln);
805         }
806         pr("%s %s %s at %s.\n",
807            prplane(&pln),
808            (load_unload == UNLOAD) ?
809            "unloaded from" : "loaded onto",
810            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
811         loaded = 1;
812     }
813     *nunitsp += loaded;
814     return 0;
815 }
816
817 static int
818 load_comm_land(struct sctstr *sectp, struct lndstr *lp,
819                struct ichrstr *ich, int load_unload, int *nunitsp)
820 {
821     i_type item = ich->i_uid;
822     struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
823     int land_amt, land_max, sect_amt, move_amt;
824     int amount;
825     char prompt[512];
826     char *p;
827     char buf[1024];
828
829     sprintf(prompt, "Number of %s to %s %s at %s? ",
830             ich->i_name,
831             (load_unload == UNLOAD) ?
832             "unload from" : "load onto",
833             prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
834     if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
835         return RET_SYN;
836
837     if (!still_ok_land(sectp, lp))
838         return RET_SYN;
839
840     land_amt = lp->lnd_item[item];
841     land_max = lcp->l_item[item];
842     sect_amt = sectp->sct_item[item];
843     amount = atoi(p);
844     if (amount < 0)
845         move_amt = -amount - land_amt;
846     else
847         move_amt = load_unload == LOAD ? amount : -amount;
848     if (move_amt > land_max - land_amt)
849         move_amt = land_max - land_amt;
850     if (move_amt < -land_amt)
851         move_amt = -land_amt;
852     if (move_amt > sect_amt)
853         move_amt = sect_amt;
854     if (move_amt < sect_amt - ITEM_MAX)
855         move_amt = sect_amt - ITEM_MAX;
856     if (!move_amt)
857         return RET_OK;
858     if (sectp->sct_own != player->cnum && move_amt > 0) {
859         pr("Sector %s is not yours.\n",
860            xyas(lp->lnd_x, lp->lnd_y, player->cnum));
861         return RET_FAIL;
862     }
863     if (sectp->sct_oldown != player->cnum && item == I_CIVIL) {
864         pr("%s civilians refuse to %s at %s!\n",
865            move_amt < 0 ? "Your" : "Foreign",
866            move_amt < 0 ? "disembark" : "board",
867            xyas(sectp->sct_x, sectp->sct_y, player->cnum));
868         return RET_FAIL;
869     }
870     sectp->sct_item[item] = sect_amt - move_amt;
871     lp->lnd_item[item] = land_amt + move_amt;
872
873     /* Did we put mils onto this unit? If so, reset the fortification */
874     if (item == I_MILIT && move_amt > 0)
875         lp->lnd_harden = 0;
876     if (move_amt >= 0) {
877         pr("%d %s loaded onto %s at %s\n",
878            move_amt, ich->i_name,
879            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
880         if (lp->lnd_own != player->cnum) {
881             wu(0, lp->lnd_own, "%s loaded %d %s onto %s at %s\n",
882                cname(player->cnum), move_amt, ich->i_name,
883                prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
884         }
885     } else {
886         pr("%d %s unloaded from %s at %s\n",
887            -move_amt, ich->i_name,
888            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
889         if (sectp->sct_own != player->cnum) {
890             wu(0, sectp->sct_own, "%s unloaded %d %s from %s at %s\n",
891                cname(player->cnum), -move_amt, ich->i_name,
892                prland(lp), xyas(lp->lnd_x, lp->lnd_y, sectp->sct_own));
893         }
894     }
895     ++*nunitsp;
896     return 0;
897 }
898
899 static int
900 load_land_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
901                int load_unload, int *nunitsp)
902 {
903     struct nstr_item ni;
904     struct lndstr land;
905     int loaded = 0;
906     char *p;
907     char prompt[512];
908     char buf[1024];
909
910     if (load_unload == LOAD
911         && lnd_nland(lp) >= lchr[lp->lnd_type].l_nland) {
912         if (noisy) {
913             if (lchr[lp->lnd_type].l_nland)
914                 pr("%s doesn't have room for any more land units!\n",
915                    prland(lp));
916             else
917                 pr("%s cannot carry land units!\n", prland(lp));
918         }
919         return 0;
920     }
921     sprintf(prompt, "Land unit(s) to %s %s? ",
922             load_unload == LOAD ? "load onto" : "unload from", prland(lp));
923     p = getstarg(player->argp[3], prompt, buf);
924     if (!p)
925         return RET_SYN;
926     if (!snxtitem(&ni, EF_LAND, p, NULL))
927         return RET_SYN;
928
929     if (!still_ok_land(sectp, lp))
930         return RET_SYN;
931
932     if (noisy && p && *p)
933         noisy = isdigit(*p);
934
935     while (nxtitem(&ni, &land)) {
936
937         if (land.lnd_own != player->cnum)
938             continue;
939
940         if (load_unload == LOAD) {
941             if (land.lnd_ship > -1) {
942                 if (noisy)
943                     pr("%s is already on ship #%d!\n",
944                        prland(&land), land.lnd_ship);
945                 continue;
946             }
947             if (land.lnd_land > -1) {
948                 if (noisy)
949                     pr("%s is already on land #%d!\n",
950                        prland(&land), land.lnd_land);
951                 continue;
952             }
953             if (lnd_first_on_land(&land) >= 0) {
954                 if (noisy)
955                     pr("%s cannot be loaded since it is carrying units\n",
956                        prland(&land));
957                 continue;
958             }
959             if (land.lnd_uid == lp->lnd_uid) {
960                 if (noisy)
961                     pr("%s can't be loaded onto itself!\n", prland(&land));
962                 continue;
963             }
964             if (lchr[(int)land.lnd_type].l_flags & (L_HEAVY | L_TRAIN)) {
965                 if (noisy)
966                     pr("%s is too heavy to load.\n", prland(&land));
967                 continue;
968             }
969         }
970
971         /* Unit sanity done */
972         /* Find the right ship */
973         if (load_unload == UNLOAD) {
974             if (land.lnd_land != lp->lnd_uid)
975                 continue;
976             if (land.lnd_ship > -1)
977                 continue;
978         } else if (lp->lnd_x != land.lnd_x || lp->lnd_y != land.lnd_y)
979             continue;
980
981         /* Fit unit on ship */
982         if (load_unload == LOAD) {
983             if (lnd_nland(lp) >= lchr[lp->lnd_type].l_nland) {
984                 if (noisy) {
985                     if (lchr[lp->lnd_type].l_nland)
986                         pr("%s doesn't have room for any more land units!\n",
987                            prland(lp));
988                     else
989                         pr("%s cannot carry land units!\n", prland(lp));
990                 }
991                 break;
992             }
993             sprintf(buf, "loaded on your %s at %s",
994                     prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
995             gift(lp->lnd_own, player->cnum, &land, buf);
996             land.lnd_land = lp->lnd_uid;
997             land.lnd_harden = 0;
998 #if 0
999            /* FIXME same issue as in load_land_ship() */
1000             resupply_all(&land);
1001 #endif
1002             putland(land.lnd_uid, &land);
1003             if (!has_supply(&land))
1004                 pr("WARNING: %s is out of supply!\n", prland(&land));
1005         } else {
1006             sprintf(buf, "unloaded in your %s at %s",
1007                     dchr[sectp->sct_type].d_name,
1008                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
1009             gift(sectp->sct_own, player->cnum, &land, buf);
1010             land.lnd_land = -1;
1011             putland(land.lnd_uid, &land);
1012         }
1013         pr("%s %s %s at %s.\n",
1014            prland(&land),
1015            (load_unload == UNLOAD) ?
1016            "unloaded from" : "loaded onto",
1017            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
1018         loaded = 1;
1019     }
1020     *nunitsp += loaded;
1021     return 0;
1022 }