]> git.pond.sub.org Git - empserver/blob - src/lib/commands/load.c
Clean up unreadable assignments within if conditionals
[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     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) {
117             if (load_unload == UNLOAD || !noisy)
118                 continue;
119             if (getrel(getnatp(ship.shp_own), player->cnum) < FRIENDLY)
120                 continue;
121         }
122
123         if (!getsect(ship.shp_x, ship.shp_y, &sect))    /* XXX */
124             continue;
125         if (!sect.sct_own)
126             continue;
127         if (!player->owner) {
128             if (ship.shp_own != player->cnum)
129                 continue;
130             if (!sect_has_dock(&sect))
131                 continue;
132             if (load_unload == LOAD) {
133                 if (noisy)
134                     pr("You don't own %s \n",
135                        xyas(sect.sct_x, sect.sct_y, player->cnum));
136                 continue;
137             }
138         }
139         if (!sect_has_dock(&sect)) {
140             if (noisy)
141                 pr("Sector %s is not a harbor or canal.\n",
142                    xyas(sect.sct_x, sect.sct_y, player->cnum));
143             continue;
144         }
145         if (!player->owner && load_unload == UNLOAD
146             && getrel(getnatp(sect.sct_own), player->cnum) < FRIENDLY) {
147             if (noisy)
148                 pr("You can't unload into an unfriendly %s\n",
149                    dchr[sect.sct_type].d_name);
150             continue;
151         }
152         if (sect.sct_effic < 2) {
153             if (noisy)
154                 pr("The %s at %s is not 2%% efficient yet.\n",
155                    dchr[sect.sct_type].d_name,
156                    xyas(sect.sct_x, sect.sct_y, player->cnum));
157             continue;
158         }
159
160         if (opt_MARKET) {
161             if (ontradingblock(EF_SHIP, &ship)) {
162                 pr("You cannot load/unload an item on the trading block!\n");
163                 continue;
164             }
165         }
166
167         switch (type) {
168         case EF_PLANE:
169             retval = load_plane_ship(&sect, &ship, noisy, load_unload,
170                                      &nships);
171             if (retval != 0)
172                 return retval;
173             break;
174         case EF_LAND:
175             retval = load_land_ship(&sect, &ship, noisy, load_unload,
176                                     &nships);
177             if (retval != 0)
178                 return retval;
179             break;
180         case EF_SECTOR:
181             retval = load_comm_ship(&sect, &ship, ich, load_unload,
182                                     &nships);
183             if (retval != 0)
184                 return retval;
185         }
186         /* load/unload plague */
187         if (sect.sct_pstage == PLG_INFECT
188             && ship.shp_pstage == PLG_HEALTHY)
189             ship.shp_pstage = PLG_EXPOSED;
190         if (ship.shp_pstage == PLG_INFECT
191             && sect.sct_pstage == PLG_HEALTHY)
192             sect.sct_pstage = PLG_EXPOSED;
193         putsect(&sect);
194         putship(ship.shp_uid, &ship);
195     }
196     if (!nships)
197         pr("No ships affected\n");
198     else
199         pr("%d ship%s %sloaded\n", nships, splur(nships),
200            load_unload == UNLOAD ? "un" : "");
201     return RET_OK;
202 }
203
204 int
205 lload(void)
206 {
207     int noisy;
208     int load_unload;
209     int type;
210     struct nstr_item nbst;
211     struct ichrstr *ich;
212     int nunits;
213     struct sctstr sect;
214     struct lndstr land;
215     int retval;
216     char *p;
217     char buf[1024];
218
219     if (!(p = getstarg(player->argp[1],
220                        "What commodity (or 'plane' or 'land')? ", buf))
221         || !*p)
222         return RET_SYN;
223     if (!strncmp(p, "plane", 5))
224         type = EF_PLANE;
225     else if (!strncmp(p, "land", 4))
226         type = EF_LAND;
227     else if (NULL != (ich = item_by_name(p)))
228         type = EF_SECTOR;
229     else {
230         pr("Can't load '%s'\n", p);
231         return RET_SYN;
232     }
233
234     if (!(p = getstarg(player->argp[2], "Unit(s): ", buf)) || !*p)
235         return RET_SYN;
236
237     noisy = isdigit(*p);
238
239     if (!snxtitem(&nbst, EF_LAND, p, NULL))
240         return RET_SYN;
241
242     load_unload = player->argp[0][1] == 'l' ? LOAD : UNLOAD;
243
244     nunits = 0;
245     while (nxtitem(&nbst, &land)) {
246         if (land.lnd_own == 0)
247             continue;
248         if (!player->owner) {
249             if (load_unload == UNLOAD || !noisy)
250                 continue;
251             if (getrel(getnatp(land.lnd_own), player->cnum) != ALLIED)
252                 continue;
253         }
254
255         if (!getsect(land.lnd_x, land.lnd_y, &sect))    /* XXX */
256             continue;
257         if (!player->owner) {
258             if (land.lnd_own != player->cnum)
259                 continue;
260             if (load_unload == LOAD) {
261                 if (noisy)
262                     pr("Sector %s is not yours.\n",
263                        xyas(sect.sct_x, sect.sct_y, player->cnum));
264                 continue;
265             }
266             if (getrel(getnatp(sect.sct_own), player->cnum) != ALLIED) {
267                 pr("Sector %s is not yours.\n",
268                    xyas(sect.sct_x, sect.sct_y, player->cnum));
269                 continue;
270             }
271         }
272
273         if (opt_MARKET) {
274             if (ontradingblock(EF_LAND, &land)) {
275                 pr("You cannot load/unload an item on the trading block!\n");
276                 continue;
277             }
278         }
279
280         switch (type) {
281         case EF_LAND:
282             retval = load_land_land(&sect, &land, noisy, load_unload,
283                                     &nunits);
284             if (retval != 0)
285                 return retval;
286             break;
287         case EF_PLANE:
288             retval = load_plane_land(&sect, &land, noisy, load_unload,
289                                      &nunits);
290             if (retval != 0)
291                 return retval;
292             break;
293         case EF_SECTOR:
294             retval = load_comm_land(&sect, &land, ich, load_unload,
295                                     &nunits);
296             if (retval != 0)
297                 return retval;
298         }
299         /* load/unload plague */
300         if (sect.sct_pstage == PLG_INFECT
301             && land.lnd_pstage == PLG_HEALTHY)
302             land.lnd_pstage = PLG_EXPOSED;
303         if (land.lnd_pstage == PLG_INFECT
304             && sect.sct_pstage == PLG_HEALTHY)
305             sect.sct_pstage = PLG_EXPOSED;
306
307         putsect(&sect);
308         putland(land.lnd_uid, &land);
309     }
310     if (nunits == 0)
311         pr("No units affected\n");
312     else
313         pr("%d unit%s %sloaded\n", nunits, splur(nunits),
314            load_unload == UNLOAD ? "un" : "");
315     return RET_OK;
316 }
317
318 static int
319 move_amount(int sect_amt, int unit_amt, int unit_max,
320            int load_unload, int amount)
321 {
322     int move_amt;
323
324     if (amount < 0)
325         move_amt = -amount - unit_amt;
326     else
327         move_amt = load_unload == LOAD ? amount : -amount;
328     if (move_amt > unit_max - unit_amt)
329         move_amt = unit_max - unit_amt;
330     if (move_amt < -unit_amt)
331         move_amt = -unit_amt;
332     if (move_amt > sect_amt)
333         move_amt = sect_amt;
334     if (move_amt < sect_amt - ITEM_MAX)
335         move_amt = sect_amt - ITEM_MAX;
336     return move_amt;
337 }
338
339 static int
340 load_comm_ok(struct sctstr *sectp, natid unit_own,
341              i_type item, int move_amt)
342 {
343     if (!move_amt)
344         return 0;
345     if (move_amt < 0 && !player->god && unit_own != player->cnum)
346         return 0;
347     if (move_amt > 0 && !player->god && sectp->sct_own != player->cnum)
348         return 0;
349     if (sectp->sct_oldown != unit_own && item == I_CIVIL) {
350         pr("%s civilians refuse to %s at %s!\n",
351            (move_amt < 0 ? unit_own : sectp->sct_oldown) == player->cnum
352            ? "Your" : "Foreign",
353            move_amt < 0 ? "disembark" : "board",
354            xyas(sectp->sct_x, sectp->sct_y, player->cnum));
355         return 0;
356     }
357     return 1;
358 }
359
360 void
361 gift(natid givee, natid giver, void *ptr, char *mesg)
362 {
363     if (giver != givee)
364         wu(0, givee, "%s %s %s\n", cname(giver), obj_nameof(ptr), mesg);
365     unit_give_away(ptr, givee, 0);
366 }
367
368 static int
369 still_ok_ship(struct sctstr *sectp, struct shpstr *shipp)
370 {
371     if (!check_sect_ok(sectp))
372         return 0;
373     if (!check_ship_ok(shipp))
374         return 0;
375     return 1;
376 }
377
378 static int
379 still_ok_land(struct sctstr *sectp, struct lndstr *landp)
380 {
381     if (!check_sect_ok(sectp))
382         return 0;
383     if (!check_land_ok(landp))
384         return 0;
385     return 1;
386 }
387
388 static int
389 load_plane_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
390                 int load_unload, int *nshipsp)
391 {
392     struct nstr_item ni;
393     struct plnstr pln;
394     int loaded = 0;
395     char buf[1024];
396     char *p;
397     char prompt[512];
398     struct mchrstr *mcp = mchr + sp->shp_type;
399
400     if (mcp->m_nplanes + mcp->m_nchoppers + mcp->m_nxlight == 0) {
401         if (noisy)
402             pr("%s cannot carry planes\n", prship(sp));
403         return 0;
404     }
405     if (load_unload == LOAD &&
406         shp_nplane(sp, NULL, NULL, NULL)
407                 >= mcp->m_nchoppers + mcp->m_nxlight + mcp->m_nplanes) {
408         if (noisy)
409             pr("%s doesn't have room for any more planes\n", prship(sp));
410         return 0;
411     }
412     sprintf(prompt, "Plane(s) to %s %s? ",
413             load_unload == LOAD ? "load onto" : "unload from", prship(sp));
414     p = getstarg(player->argp[3], prompt, buf);
415     if (!p)
416         return RET_SYN;
417     if (!snxtitem(&ni, EF_PLANE, p, NULL))
418         return RET_SYN;
419
420     if (!still_ok_ship(sectp, sp))
421         return RET_SYN;
422
423     if (noisy && p && *p)
424         noisy = isdigit(*p);
425
426     while (nxtitem(&ni, &pln)) {
427         if (!player->owner)
428             continue;
429         if (!(plchr[(int)pln.pln_type].pl_flags & P_L)
430             && !(plchr[(int)pln.pln_type].pl_flags & P_E)
431             && !(plchr[(int)pln.pln_type].pl_flags & P_K)
432             && !(plchr[(int)pln.pln_type].pl_flags & P_M)
433             ) {
434             if (noisy)
435                 pr("You can only load light planes, helos, xtra-light, or missiles onto ships.\n");
436             continue;
437         }
438         if (load_unload == LOAD && pln.pln_ship > -1) {
439             if (noisy)
440                 pr("%s is already on ship #%d!\n",
441                    prplane(&pln), pln.pln_ship);
442             continue;
443         }
444         if (load_unload == LOAD && pln.pln_land > -1) {
445             if (noisy)
446                 pr("%s is already on land unit #%d!\n",
447                    prplane(&pln), pln.pln_land);
448             continue;
449         }
450         if (pln.pln_harden != 0) {
451             if (noisy)
452                 pr("%s has been hardened and can't be loaded\n",
453                    prplane(&pln));
454             continue;
455         }
456
457         /* Plane sanity done */
458         /* Find the right ship */
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         /* ship to (plane or missle) sanity */
466         if (!could_be_on_ship(&pln, sp, 0, 0, 0, 0)) {
467             if (plchr[(int)pln.pln_type].pl_flags & P_L) {
468                 strcpy(buf, "planes");
469             } else if (plchr[(int)pln.pln_type].pl_flags & P_K) {
470                 strcpy(buf, "choppers");
471             } else if (plchr[(int)pln.pln_type].pl_flags & P_M) {
472                 strcpy(buf, "missiles");
473             } else if (plchr[(int)pln.pln_type].pl_flags & P_E) {
474                 strcpy(buf, "extra light planes");
475             }                   /* else impossible */
476             if (noisy)
477                 pr("%s cannot carry %s.\n", prship(sp), buf);
478             continue;
479         }
480         /* Fit plane on ship */
481         if (load_unload == LOAD) {
482             if (!put_plane_on_ship(&pln, sp)) {
483                 if (noisy)
484                     pr("Can't put plane %d on this ship!\n", pln.pln_uid);
485                 continue;
486             }
487             sprintf(buf, "loaded on your %s at %s",
488                     prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
489             gift(sp->shp_own, player->cnum, &pln, buf);
490             putplane(pln.pln_uid, &pln);
491         } else {
492             pln.pln_ship = -1;
493             sprintf(buf, "unloaded in your %s at %s",
494                     dchr[sectp->sct_type].d_name,
495                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
496             gift(sectp->sct_own, player->cnum, &pln, buf);
497             putplane(pln.pln_uid, &pln);
498         }
499         pr("%s %s %s at %s.\n",
500            prplane(&pln),
501            (load_unload == UNLOAD) ?
502            "unloaded from" : "loaded onto",
503            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
504         loaded = 1;
505     }
506     *nshipsp += loaded;
507     return 0;
508 }
509
510 static int
511 load_land_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
512                int load_unload, int *nshipsp)
513 {
514     struct nstr_item ni;
515     struct lndstr land;
516     int loaded = 0;
517     char *p;
518     char prompt[512];
519     char buf[1024];
520     int load_spy = 0;
521
522     if (load_unload == LOAD) {
523         if (opt_LANDSPIES) {
524             if ((mchr[(int)sp->shp_type].m_flags & M_SUB) &&
525                 (mchr[(int)sp->shp_type].m_nland == 0)) {
526                 if (shp_nland(sp) >= 2) {
527                     pr("Non-land unit carrying subs can only carry up to two spy units.\n");
528                     return 0;
529                 }
530                 /* Eh, let 'em load a spy only */
531                 load_spy = 1;
532             }
533         }
534         if (!load_spy && shp_nland(sp) >= mchr[sp->shp_type].m_nland) {
535             if (noisy) {
536                 if (mchr[(int)sp->shp_type].m_nland)
537                     pr("%s doesn't have room for any more land units!\n",
538                        prship(sp));
539                 else
540                     pr("%s cannot carry land units!\n", prship(sp));
541             }
542             return 0;
543         }
544     }
545     sprintf(prompt, "Land unit(s) to %s %s? ",
546             load_unload == LOAD ? "load onto" : "unload from", prship(sp));
547     p = getstarg(player->argp[3], prompt, buf);
548     if (!p)
549         return RET_SYN;
550     if (!snxtitem(&ni, EF_LAND, p, NULL))
551         return RET_SYN;
552
553     if (!still_ok_ship(sectp, sp))
554         return RET_SYN;
555
556     if (noisy && p && *p)
557         noisy = isdigit(*p);
558
559     while (nxtitem(&ni, &land)) {
560         if (!player->owner)
561             continue;
562
563         if (load_unload == LOAD) {
564             if (land.lnd_ship > -1) {
565                 if (noisy)
566                     pr("%s is already on ship #%d!\n",
567                        prland(&land), land.lnd_ship);
568                 continue;
569             }
570             if (land.lnd_land > -1) {
571                 if (noisy)
572                     pr("%s is already on land #%d!\n",
573                        prland(&land), land.lnd_land);
574                 continue;
575             }
576             if (lnd_first_on_land(&land) >= 0) {
577                 if (noisy)
578                     pr("%s cannot be loaded since it is carrying units\n",
579                        prland(&land));
580                 continue;
581             }
582             if (lchr[(int)land.lnd_type].l_flags & L_HEAVY) {
583                 if (noisy)
584                     pr("%s is too heavy to load.\n", prland(&land));
585                 continue;
586             }
587             if (load_spy && !(lchr[(int)land.lnd_type].l_flags & L_SPY)) {
588                 if (noisy)
589                     pr("Subs can only carry spy units.\n");
590                 continue;
591             }
592         }
593
594         /* Unit sanity done */
595         /* Find the right ship */
596         if (load_unload == UNLOAD) {
597             if (land.lnd_ship != sp->shp_uid)
598                 continue;
599             if (land.lnd_land > -1)
600                 continue;
601         } else if (sp->shp_x != land.lnd_x || sp->shp_y != land.lnd_y)
602             continue;
603
604         if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
605             (!((mchr[(int)sp->shp_type].m_flags & M_SUPPLY) &&
606                (!(mchr[(int)sp->shp_type].m_flags & M_SUB))))) {
607             if (noisy) {
608                 pr("You can only load light units onto ships,\n");
609                 pr("unless the ship is a non-sub supply ship\n");
610                 pr("%s not loaded\n", prland(&land));
611             }
612             continue;
613         }
614         /* Fit unit on ship */
615         if (load_unload == LOAD) {
616             /* We have to check again, since it may have changed */
617             if (opt_LANDSPIES) {
618                 if ((mchr[(int)sp->shp_type].m_flags & M_SUB) &&
619                     (mchr[(int)sp->shp_type].m_nland == 0)) {
620                     if (shp_nland(sp) >= 2) {
621                         pr("Non-land unit carrying subs can only carry up to two spy units.\n");
622                         return 0;
623                     }
624                     /* Eh, let 'em load a spy only */
625                     load_spy = 1;
626                 }
627             }
628             if (!load_spy && shp_nland(sp) >= mchr[sp->shp_type].m_nland) {
629                 if (noisy) {
630                     if (mchr[(int)sp->shp_type].m_nland)
631                         pr("%s doesn't have room for any more land units!\n",
632                            prship(sp));
633                     else
634                         pr("%s cannot carry land units!\n", prship(sp));
635                 }
636                 return 0;
637             }
638             sprintf(buf, "loaded on your %s at %s",
639                     prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
640             gift(sp->shp_own, player->cnum, &land, buf);
641             land.lnd_ship = sp->shp_uid;
642             land.lnd_harden = 0;
643             putland(land.lnd_uid, &land);
644 #if 0
645            /*
646             * FIXME if this supplies from the sector, the putsect in
647             * load() / lload() duplicates those supplies, causing a
648             * seqno mismatch
649             */
650             if (!lnd_supply_all(&land))
651                 pr("WARNING: %s is out of supply!\n", prland(&land));
652 #else
653             if (!lnd_in_supply(&land))
654                 pr("WARNING: %s is out of supply!\n", prland(&land));
655 #endif
656         } else {
657             sprintf(buf, "unloaded in your %s at %s",
658                     dchr[sectp->sct_type].d_name,
659                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
660
661             /* Spies are unloaded quietly, others aren't */
662             if (!(lchr[(int)land.lnd_type].l_flags & L_SPY))
663                 gift(sectp->sct_own, player->cnum, &land, buf);
664             land.lnd_ship = -1;
665             putland(land.lnd_uid, &land);
666         }
667         pr("%s %s %s at %s.\n",
668            prland(&land),
669            (load_unload == UNLOAD) ?
670            "unloaded from" : "loaded onto",
671            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
672         loaded = 1;
673     }
674     *nshipsp += loaded;
675     return 0;
676 }
677
678 static int
679 load_comm_ship(struct sctstr *sectp, struct shpstr *sp,
680                struct ichrstr *ich, int load_unload, int *nshipsp)
681 {
682     i_type item = ich->i_uid;
683     struct mchrstr *mcp = &mchr[(int)sp->shp_type];
684     int ship_amt, sect_amt, move_amt;
685     char prompt[512];
686     char *p;
687     char buf[1024];
688
689     sprintf(prompt, "Number of %s to %s %s at %s? ",
690             ich->i_name,
691             (load_unload == UNLOAD) ?
692             "unload from" : "load onto",
693             prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
694     if (!(p = getstarg(player->argp[3], prompt, buf)) || !*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, 0))
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     if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
856         return RET_SYN;
857
858     if (!still_ok_land(sectp, lp))
859         return RET_SYN;
860
861     land_amt = lp->lnd_item[item];
862     sect_amt = sectp->sct_item[item];
863     move_amt = move_amount(sect_amt, land_amt, lcp->l_item[item],
864                            load_unload, atoi(p));
865     if (!load_comm_ok(sectp, lp->lnd_own, item, move_amt))
866         return RET_OK;
867     sectp->sct_item[item] = sect_amt - move_amt;
868     lp->lnd_item[item] = land_amt + move_amt;
869
870     /* Did we put mils onto this unit? If so, reset the fortification */
871     if (item == I_MILIT && move_amt > 0)
872         lp->lnd_harden = 0;
873
874     if (move_amt >= 0) {
875         pr("%d %s loaded onto %s at %s\n",
876            move_amt, ich->i_name,
877            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
878         if (lp->lnd_own != player->cnum) {
879             wu(0, lp->lnd_own, "%s loaded %d %s onto %s at %s\n",
880                cname(player->cnum), move_amt, ich->i_name,
881                prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
882         }
883     } else {
884         pr("%d %s unloaded from %s at %s\n",
885            -move_amt, ich->i_name,
886            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
887         if (sectp->sct_own != player->cnum) {
888             wu(0, sectp->sct_own, "%s unloaded %d %s from %s at %s\n",
889                cname(player->cnum), -move_amt, ich->i_name,
890                prland(lp), xyas(lp->lnd_x, lp->lnd_y, sectp->sct_own));
891         }
892     }
893     ++*nunitsp;
894     return 0;
895 }
896
897 static int
898 load_land_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
899                int load_unload, int *nunitsp)
900 {
901     struct nstr_item ni;
902     struct lndstr land;
903     int loaded = 0;
904     char *p;
905     char prompt[512];
906     char buf[1024];
907
908     if (load_unload == LOAD
909         && lnd_nland(lp) >= lchr[lp->lnd_type].l_nland) {
910         if (noisy) {
911             if (lchr[lp->lnd_type].l_nland)
912                 pr("%s doesn't have room for any more land units!\n",
913                    prland(lp));
914             else
915                 pr("%s cannot carry land units!\n", prland(lp));
916         }
917         return 0;
918     }
919     sprintf(prompt, "Land unit(s) to %s %s? ",
920             load_unload == LOAD ? "load onto" : "unload from", prland(lp));
921     p = getstarg(player->argp[3], prompt, buf);
922     if (!p)
923         return RET_SYN;
924     if (!snxtitem(&ni, EF_LAND, p, NULL))
925         return RET_SYN;
926
927     if (!still_ok_land(sectp, lp))
928         return RET_SYN;
929
930     if (noisy && p && *p)
931         noisy = isdigit(*p);
932
933     while (nxtitem(&ni, &land)) {
934         if (!player->owner)
935             continue;
936
937         if (load_unload == LOAD) {
938             if (land.lnd_ship > -1) {
939                 if (noisy)
940                     pr("%s is already on ship #%d!\n",
941                        prland(&land), land.lnd_ship);
942                 continue;
943             }
944             if (land.lnd_land > -1) {
945                 if (noisy)
946                     pr("%s is already on land #%d!\n",
947                        prland(&land), land.lnd_land);
948                 continue;
949             }
950             if (lnd_first_on_land(&land) >= 0) {
951                 if (noisy)
952                     pr("%s cannot be loaded since it is carrying units\n",
953                        prland(&land));
954                 continue;
955             }
956             if (land.lnd_uid == lp->lnd_uid) {
957                 if (noisy)
958                     pr("%s can't be loaded onto itself!\n", prland(&land));
959                 continue;
960             }
961             if (lchr[(int)land.lnd_type].l_flags & (L_HEAVY | L_TRAIN)) {
962                 if (noisy)
963                     pr("%s is too heavy to load.\n", prland(&land));
964                 continue;
965             }
966         }
967
968         /* Unit sanity done */
969         /* Find the right ship */
970         if (load_unload == UNLOAD) {
971             if (land.lnd_land != lp->lnd_uid)
972                 continue;
973             if (land.lnd_ship > -1)
974                 continue;
975         } else if (lp->lnd_x != land.lnd_x || lp->lnd_y != land.lnd_y)
976             continue;
977
978         /* Fit unit on ship */
979         if (load_unload == LOAD) {
980             if (lnd_nland(lp) >= lchr[lp->lnd_type].l_nland) {
981                 if (noisy) {
982                     if (lchr[lp->lnd_type].l_nland)
983                         pr("%s doesn't have room for any more land units!\n",
984                            prland(lp));
985                     else
986                         pr("%s cannot carry land units!\n", prland(lp));
987                 }
988                 break;
989             }
990             sprintf(buf, "loaded on your %s at %s",
991                     prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
992             gift(lp->lnd_own, player->cnum, &land, buf);
993             land.lnd_land = lp->lnd_uid;
994             land.lnd_harden = 0;
995             putland(land.lnd_uid, &land);
996 #if 0
997            /* FIXME same issue as in load_land_ship() */
998             if (!lnd_supply_all(&land))
999                 pr("WARNING: %s is out of supply!\n", prland(&land));
1000 #else
1001             if (!lnd_in_supply(&land))
1002                 pr("WARNING: %s is out of supply!\n", prland(&land));
1003 #endif
1004         } else {
1005             sprintf(buf, "unloaded in your %s at %s",
1006                     dchr[sectp->sct_type].d_name,
1007                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
1008             gift(sectp->sct_own, player->cnum, &land, buf);
1009             land.lnd_land = -1;
1010             putland(land.lnd_uid, &land);
1011         }
1012         pr("%s %s %s at %s.\n",
1013            prland(&land),
1014            (load_unload == UNLOAD) ?
1015            "unloaded from" : "loaded onto",
1016            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
1017         loaded = 1;
1018     }
1019     *nunitsp += loaded;
1020     return 0;
1021 }