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