]> git.pond.sub.org Git - empserver/blob - src/lib/commands/load.c
Rename obj_nameof() to unit_nameof() and move to unitsub.c
[empserver] / src / lib / commands / load.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  load.c: load/unload goods from a sector onto a ship or land unit
28  *
29  *  Known contributors to this file:
30  *     David Sharnoff, 1987
31  *     Ken Stevens, 1995 (rewritten)
32  *     Steve McClure, 1998-2000
33  *     Markus Armbruster, 2004-2011
34  */
35
36 #include <config.h>
37
38 #include <ctype.h>
39 #include "commands.h"
40 #include "item.h"
41 #include "land.h"
42 #include "optlist.h"
43 #include "plague.h"
44 #include "plane.h"
45 #include "ship.h"
46 #include "unit.h"
47
48 /*
49  * The values 1 and -1 are important below, don't change them.
50  */
51 #define LOAD    1
52 #define UNLOAD  -1
53
54 static int load_plane_ship(struct sctstr *sectp, struct shpstr *sp,
55                            int noisy, int load_unload, int *nshipsp);
56 static int load_land_ship(struct sctstr *sectp, struct shpstr *sp,
57                           int noisy, int load_unload, int *nshipsp);
58 static int load_comm_ship(struct sctstr *sectp, struct shpstr *sp,
59                           struct ichrstr *ich, int load_unload,
60                           int *nshipsp);
61 static int load_plane_land(struct sctstr *sectp, struct lndstr *lp,
62                            int noisy, int load_unload, int *nunitsp);
63 static int load_land_land(struct sctstr *sectp, struct lndstr *lp,
64                           int noisy, int load_unload, int *nunitsp);
65 static int load_comm_land(struct sctstr *sectp, struct lndstr *lp,
66                           struct ichrstr *ich, int load_unload,
67                           int *nunitsp);
68
69 int
70 load(void)
71 {
72     int noisy;
73     int load_unload;
74     int type;
75     struct nstr_item nbst;
76     struct ichrstr *ich;
77     int nships;
78     struct sctstr sect;
79     struct shpstr ship;
80     int retval;
81     char *p;
82     char buf[1024];
83
84     p = getstarg(player->argp[1],
85                  "What commodity (or 'plane' or 'land')? ", buf);
86     if (!p || !*p)
87         return RET_SYN;
88
89     if (!strncmp(p, "plane", 5))
90         type = EF_PLANE;
91     else if (!strncmp(p, "land", 4))
92         type = EF_LAND;
93     else if (NULL != (ich = item_by_name(p)))
94         type = EF_SECTOR;
95     else {
96         pr("Can't load '%s'\n", p);
97         return RET_SYN;
98     }
99
100     p = getstarg(player->argp[2], "Ship(s): ", buf);
101     if (!p || !*p)
102         return RET_SYN;
103
104     noisy = isdigit(*p);
105
106     if (!snxtitem(&nbst, EF_SHIP, p, NULL))
107         return RET_SYN;
108
109     load_unload = **player->argp == 'l' ? LOAD : UNLOAD;
110
111     nships = 0;
112     while (nxtitem(&nbst, &ship)) {
113         if (!ship.shp_own)
114             continue;
115         if (!player->owner) {
116             if (load_unload == UNLOAD || !noisy)
117                 continue;
118             if (relations_with(ship.shp_own, player->cnum) < FRIENDLY)
119                 continue;
120         }
121
122         if (!getsect(ship.shp_x, ship.shp_y, &sect))    /* XXX */
123             continue;
124         if (!sect.sct_own)
125             continue;
126         if (!player->owner) {
127             if (ship.shp_own != player->cnum)
128                 continue;
129             if (!sect_has_dock(&sect))
130                 continue;
131             if (load_unload == LOAD) {
132                 if (noisy)
133                     pr("You don't own %s \n",
134                        xyas(sect.sct_x, sect.sct_y, player->cnum));
135                 continue;
136             }
137         }
138         if (!sect_has_dock(&sect)) {
139             if (noisy)
140                 pr("Sector %s is not a harbor or canal.\n",
141                    xyas(sect.sct_x, sect.sct_y, player->cnum));
142             continue;
143         }
144         if (load_unload == UNLOAD
145             && !player->owner
146             && relations_with(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     p = getstarg(player->argp[1],
220                  "What commodity (or 'plane' or 'land')? ", buf);
221     if (!p || !*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     p = getstarg(player->argp[2], "Unit(s): ", buf);
235     if (!p || !*p)
236         return RET_SYN;
237
238     noisy = isdigit(*p);
239
240     if (!snxtitem(&nbst, EF_LAND, p, NULL))
241         return RET_SYN;
242
243     load_unload = player->argp[0][1] == 'l' ? LOAD : UNLOAD;
244
245     nunits = 0;
246     while (nxtitem(&nbst, &land)) {
247         if (land.lnd_own == 0)
248             continue;
249         if (!player->owner) {
250             if (load_unload == UNLOAD || !noisy)
251                 continue;
252             if (relations_with(land.lnd_own, player->cnum) != ALLIED)
253                 continue;
254         }
255
256         if (!getsect(land.lnd_x, land.lnd_y, &sect))    /* XXX */
257             continue;
258         if (!player->owner) {
259             if (land.lnd_own != player->cnum)
260                 continue;
261             if (load_unload == LOAD) {
262                 if (noisy)
263                     pr("Sector %s is not yours.\n",
264                        xyas(sect.sct_x, sect.sct_y, player->cnum));
265                 continue;
266             }
267             if (relations_with(sect.sct_own, player->cnum) != ALLIED) {
268                 pr("Sector %s is not yours.\n",
269                    xyas(sect.sct_x, sect.sct_y, player->cnum));
270                 continue;
271             }
272         }
273
274         if (opt_MARKET) {
275             if (ontradingblock(EF_LAND, &land)) {
276                 pr("You cannot load/unload an item on the trading block!\n");
277                 continue;
278             }
279         }
280
281         switch (type) {
282         case EF_LAND:
283             retval = load_land_land(&sect, &land, noisy, load_unload,
284                                     &nunits);
285             if (retval != 0)
286                 return retval;
287             break;
288         case EF_PLANE:
289             retval = load_plane_land(&sect, &land, noisy, load_unload,
290                                      &nunits);
291             if (retval != 0)
292                 return retval;
293             break;
294         case EF_SECTOR:
295             retval = load_comm_land(&sect, &land, ich, load_unload,
296                                     &nunits);
297             if (retval != 0)
298                 return retval;
299         }
300         /* load/unload plague */
301         if (sect.sct_pstage == PLG_INFECT
302             && land.lnd_pstage == PLG_HEALTHY)
303             land.lnd_pstage = PLG_EXPOSED;
304         if (land.lnd_pstage == PLG_INFECT
305             && sect.sct_pstage == PLG_HEALTHY)
306             sect.sct_pstage = PLG_EXPOSED;
307
308         putsect(&sect);
309         putland(land.lnd_uid, &land);
310     }
311     if (nunits == 0)
312         pr("No units affected\n");
313     else
314         pr("%d unit%s %sloaded\n", nunits, splur(nunits),
315            load_unload == UNLOAD ? "un" : "");
316     return RET_OK;
317 }
318
319 static int
320 move_amount(int sect_amt, int unit_amt, int unit_max,
321            int load_unload, int amount)
322 {
323     int move_amt;
324
325     if (amount < 0)
326         move_amt = -amount - unit_amt;
327     else
328         move_amt = load_unload == LOAD ? amount : -amount;
329     if (move_amt > unit_max - unit_amt)
330         move_amt = unit_max - unit_amt;
331     if (move_amt < -unit_amt)
332         move_amt = -unit_amt;
333     if (move_amt > sect_amt)
334         move_amt = sect_amt;
335     if (move_amt < sect_amt - ITEM_MAX)
336         move_amt = sect_amt - ITEM_MAX;
337     return move_amt;
338 }
339
340 static int
341 load_comm_ok(struct sctstr *sectp, natid unit_own,
342              i_type item, int move_amt)
343 {
344     if (!move_amt)
345         return 0;
346     if (move_amt < 0 && !player->god && unit_own != player->cnum)
347         return 0;
348     if (move_amt > 0 && !player->god && sectp->sct_own != player->cnum)
349         return 0;
350     if (sectp->sct_oldown != unit_own && item == I_CIVIL) {
351         pr("%s civilians refuse to %s at %s!\n",
352            (move_amt < 0 ? unit_own : sectp->sct_oldown) == player->cnum
353            ? "Your" : "Foreign",
354            move_amt < 0 ? "disembark" : "board",
355            xyas(sectp->sct_x, sectp->sct_y, player->cnum));
356         return 0;
357     }
358     return 1;
359 }
360
361 void
362 gift(natid givee, natid giver, void *ptr, char *mesg)
363 {
364     if (giver != givee)
365         wu(0, givee, "%s %s %s\n", cname(giver), unit_nameof(ptr), mesg);
366     unit_give_away(ptr, givee, 0);
367 }
368
369 static int
370 still_ok_ship(struct sctstr *sectp, struct shpstr *shipp)
371 {
372     if (!check_sect_ok(sectp))
373         return 0;
374     if (!check_ship_ok(shipp))
375         return 0;
376     return 1;
377 }
378
379 static int
380 still_ok_land(struct sctstr *sectp, struct lndstr *landp)
381 {
382     if (!check_sect_ok(sectp))
383         return 0;
384     if (!check_land_ok(landp))
385         return 0;
386     return 1;
387 }
388
389 static int
390 load_plane_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
391                 int load_unload, int *nshipsp)
392 {
393     struct nstr_item ni;
394     struct plnstr pln;
395     int loaded = 0;
396     char buf[1024];
397     char *p;
398     char prompt[512];
399     struct mchrstr *mcp = mchr + sp->shp_type;
400
401     if (mcp->m_nplanes + mcp->m_nchoppers + mcp->m_nxlight == 0) {
402         if (noisy)
403             pr("%s cannot carry planes\n", prship(sp));
404         return 0;
405     }
406     if (load_unload == LOAD &&
407         shp_nplane(sp, NULL, NULL, NULL)
408                 >= mcp->m_nchoppers + mcp->m_nxlight + mcp->m_nplanes) {
409         if (noisy)
410             pr("%s doesn't have room for any more planes\n", prship(sp));
411         return 0;
412     }
413     sprintf(prompt, "Plane(s) to %s %s? ",
414             load_unload == LOAD ? "load onto" : "unload from", prship(sp));
415     p = getstarg(player->argp[3], prompt, buf);
416     if (!p)
417         return RET_SYN;
418     if (!snxtitem(&ni, EF_PLANE, p, NULL))
419         return RET_SYN;
420
421     if (!still_ok_ship(sectp, sp))
422         return RET_SYN;
423
424     if (noisy && p && *p)
425         noisy = isdigit(*p);
426
427     while (nxtitem(&ni, &pln)) {
428         if (!player->owner)
429             continue;
430         if (!(plchr[(int)pln.pln_type].pl_flags & P_L)
431             && !(plchr[(int)pln.pln_type].pl_flags & P_E)
432             && !(plchr[(int)pln.pln_type].pl_flags & P_K)
433             && !(plchr[(int)pln.pln_type].pl_flags & P_M)
434             ) {
435             if (noisy)
436                 pr("You can only load light planes, helos, xtra-light, or missiles onto ships.\n");
437             continue;
438         }
439         if (load_unload == LOAD && pln.pln_ship > -1) {
440             if (noisy)
441                 pr("%s is already on ship #%d!\n",
442                    prplane(&pln), pln.pln_ship);
443             continue;
444         }
445         if (load_unload == LOAD && pln.pln_land > -1) {
446             if (noisy)
447                 pr("%s is already on land unit #%d!\n",
448                    prplane(&pln), pln.pln_land);
449             continue;
450         }
451         if (pln.pln_harden != 0) {
452             if (noisy)
453                 pr("%s has been hardened and can't be loaded\n",
454                    prplane(&pln));
455             continue;
456         }
457
458         if (load_unload == UNLOAD) {
459             if (pln.pln_ship != sp->shp_uid)
460                 continue;
461         } else if (sp->shp_x != pln.pln_x || sp->shp_y != pln.pln_y)
462             continue;
463
464         if (!could_be_on_ship(&pln, sp, 0, 0, 0, 0)) {
465             if (noisy) {
466                 if (plchr[(int)pln.pln_type].pl_flags & P_K)
467                     p = "choppers";
468                 else if (plchr[(int)pln.pln_type].pl_flags & P_E)
469                     p = "extra light planes";
470                 else if (plchr[(int)pln.pln_type].pl_flags & P_M)
471                     p = "missiles";
472                 else
473                     p = "planes";
474                 pr("%s cannot carry %s.\n", prship(sp), p);
475             }
476             continue;
477         }
478         /* Fit plane on ship */
479         if (load_unload == LOAD) {
480             if (!put_plane_on_ship(&pln, sp)) {
481                 if (noisy)
482                     pr("Can't put plane %d on this ship!\n", pln.pln_uid);
483                 continue;
484             }
485             sprintf(buf, "loaded on your %s at %s",
486                     prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
487             gift(sp->shp_own, player->cnum, &pln, buf);
488             putplane(pln.pln_uid, &pln);
489         } else {
490             pln.pln_ship = -1;
491             sprintf(buf, "unloaded in your %s at %s",
492                     dchr[sectp->sct_type].d_name,
493                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
494             gift(sectp->sct_own, player->cnum, &pln, buf);
495             putplane(pln.pln_uid, &pln);
496         }
497         pr("%s %s %s at %s.\n",
498            prplane(&pln),
499            (load_unload == UNLOAD) ?
500            "unloaded from" : "loaded onto",
501            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
502         loaded = 1;
503     }
504     *nshipsp += loaded;
505     return 0;
506 }
507
508 static int
509 load_land_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
510                int load_unload, int *nshipsp)
511 {
512     struct nstr_item ni;
513     struct lndstr land;
514     int loaded = 0;
515     char *p;
516     char prompt[512];
517     char buf[1024];
518     int load_spy = 0;
519
520     if (load_unload == LOAD) {
521         if (opt_LANDSPIES) {
522             if ((mchr[(int)sp->shp_type].m_flags & M_SUB) &&
523                 (mchr[(int)sp->shp_type].m_nland == 0)) {
524                 if (shp_nland(sp) >= 2) {
525                     pr("Non-land unit carrying subs can only carry up to two spy units.\n");
526                     return 0;
527                 }
528                 /* Eh, let 'em load a spy only */
529                 load_spy = 1;
530             }
531         }
532         if (!load_spy && shp_nland(sp) >= mchr[sp->shp_type].m_nland) {
533             if (noisy) {
534                 if (mchr[(int)sp->shp_type].m_nland)
535                     pr("%s doesn't have room for any more land units!\n",
536                        prship(sp));
537                 else
538                     pr("%s cannot carry land units!\n", prship(sp));
539             }
540             return 0;
541         }
542     }
543     sprintf(prompt, "Land unit(s) to %s %s? ",
544             load_unload == LOAD ? "load onto" : "unload from", prship(sp));
545     p = getstarg(player->argp[3], prompt, buf);
546     if (!p)
547         return RET_SYN;
548     if (!snxtitem(&ni, EF_LAND, p, NULL))
549         return RET_SYN;
550
551     if (!still_ok_ship(sectp, sp))
552         return RET_SYN;
553
554     if (noisy && p && *p)
555         noisy = isdigit(*p);
556
557     while (nxtitem(&ni, &land)) {
558         if (!player->owner)
559             continue;
560
561         if (load_unload == LOAD) {
562             if (land.lnd_ship > -1) {
563                 if (noisy)
564                     pr("%s is already on ship #%d!\n",
565                        prland(&land), land.lnd_ship);
566                 continue;
567             }
568             if (land.lnd_land > -1) {
569                 if (noisy)
570                     pr("%s is already on land #%d!\n",
571                        prland(&land), land.lnd_land);
572                 continue;
573             }
574             if (lnd_first_on_land(&land) >= 0) {
575                 if (noisy)
576                     pr("%s cannot be loaded since it is carrying units\n",
577                        prland(&land));
578                 continue;
579             }
580             if (lchr[(int)land.lnd_type].l_flags & L_HEAVY) {
581                 if (noisy)
582                     pr("%s is too heavy to load.\n", prland(&land));
583                 continue;
584             }
585             if (load_spy && !(lchr[(int)land.lnd_type].l_flags & L_SPY)) {
586                 if (noisy)
587                     pr("Subs can only carry spy units.\n");
588                 continue;
589             }
590         }
591
592         /* Unit sanity done */
593         /* Find the right ship */
594         if (load_unload == UNLOAD) {
595             if (land.lnd_ship != sp->shp_uid)
596                 continue;
597             if (land.lnd_land > -1)
598                 continue;
599         } else if (sp->shp_x != land.lnd_x || sp->shp_y != land.lnd_y)
600             continue;
601
602         if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
603             (!((mchr[(int)sp->shp_type].m_flags & M_SUPPLY) &&
604                (!(mchr[(int)sp->shp_type].m_flags & M_SUB))))) {
605             if (noisy) {
606                 pr("You can only load light units onto ships,\n");
607                 pr("unless the ship is a non-sub supply ship\n");
608                 pr("%s not loaded\n", prland(&land));
609             }
610             continue;
611         }
612         /* Fit unit on ship */
613         if (load_unload == LOAD) {
614             /* We have to check again, since it may have changed */
615             if (opt_LANDSPIES) {
616                 if ((mchr[(int)sp->shp_type].m_flags & M_SUB) &&
617                     (mchr[(int)sp->shp_type].m_nland == 0)) {
618                     if (shp_nland(sp) >= 2) {
619                         pr("Non-land unit carrying subs can only carry up to two spy units.\n");
620                         return 0;
621                     }
622                     /* Eh, let 'em load a spy only */
623                     load_spy = 1;
624                 }
625             }
626             if (!load_spy && shp_nland(sp) >= mchr[sp->shp_type].m_nland) {
627                 if (noisy) {
628                     if (mchr[(int)sp->shp_type].m_nland)
629                         pr("%s doesn't have room for any more land units!\n",
630                            prship(sp));
631                     else
632                         pr("%s cannot carry land units!\n", prship(sp));
633                 }
634                 return 0;
635             }
636             sprintf(buf, "loaded on your %s at %s",
637                     prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
638             gift(sp->shp_own, player->cnum, &land, buf);
639             land.lnd_ship = sp->shp_uid;
640             land.lnd_harden = 0;
641             putland(land.lnd_uid, &land);
642 #if 0
643            /*
644             * FIXME if this supplies from the sector, the putsect in
645             * load() / lload() duplicates those supplies, causing a
646             * seqno mismatch
647             */
648             if (!lnd_supply_all(&land))
649                 pr("WARNING: %s is out of supply!\n", prland(&land));
650 #else
651             if (!lnd_in_supply(&land))
652                 pr("WARNING: %s is out of supply!\n", prland(&land));
653 #endif
654         } else {
655             sprintf(buf, "unloaded in your %s at %s",
656                     dchr[sectp->sct_type].d_name,
657                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
658
659             /* Spies are unloaded quietly, others aren't */
660             if (!(lchr[(int)land.lnd_type].l_flags & L_SPY))
661                 gift(sectp->sct_own, player->cnum, &land, buf);
662             land.lnd_ship = -1;
663             putland(land.lnd_uid, &land);
664         }
665         pr("%s %s %s at %s.\n",
666            prland(&land),
667            (load_unload == UNLOAD) ?
668            "unloaded from" : "loaded onto",
669            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
670         loaded = 1;
671     }
672     *nshipsp += loaded;
673     return 0;
674 }
675
676 static int
677 load_comm_ship(struct sctstr *sectp, struct shpstr *sp,
678                struct ichrstr *ich, int load_unload, int *nshipsp)
679 {
680     i_type item = ich->i_uid;
681     struct mchrstr *mcp = &mchr[(int)sp->shp_type];
682     int ship_amt, sect_amt, move_amt;
683     char prompt[512];
684     char *p;
685     char buf[1024];
686
687     sprintf(prompt, "Number of %s to %s %s at %s? ",
688             ich->i_name,
689             (load_unload == UNLOAD) ?
690             "unload from" : "load onto",
691             prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
692     p = getstarg(player->argp[3], prompt, buf);
693     if (!p || !*p)
694         return RET_SYN;
695
696     if (!still_ok_ship(sectp, sp))
697         return RET_SYN;
698
699     ship_amt = sp->shp_item[item];
700     sect_amt = sectp->sct_item[item];
701     move_amt = move_amount(sect_amt, ship_amt, mcp->m_item[item],
702                            load_unload, atoi(p));
703     if (!load_comm_ok(sectp, sp->shp_own, item, move_amt))
704         return RET_OK;
705     if (!want_to_abandon(sectp, item, move_amt, NULL))
706         return RET_FAIL;
707     if (!still_ok_ship(sectp, sp))
708         return RET_SYN;
709     sectp->sct_item[item] = sect_amt - move_amt;
710     sp->shp_item[item] = ship_amt + move_amt;
711
712     if (move_amt >= 0) {
713         pr("%d %s loaded onto %s at %s\n",
714            move_amt, ich->i_name,
715            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
716         if (sp->shp_own != player->cnum) {
717             wu(0, sp->shp_own, "%s loaded %d %s onto %s at %s\n",
718                cname(player->cnum), move_amt, ich->i_name,
719                prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
720         }
721     } else {
722         pr("%d %s unloaded from %s at %s\n",
723            -move_amt, ich->i_name,
724            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
725         if (sectp->sct_own != player->cnum) {
726             wu(0, sectp->sct_own, "%s unloaded %d %s from %s at %s\n",
727                cname(player->cnum), -move_amt, ich->i_name,
728                prship(sp), xyas(sp->shp_x, sp->shp_y, sectp->sct_own));
729         }
730     }
731     ++*nshipsp;
732     return 0;
733 }
734
735 static int
736 load_plane_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
737                 int load_unload, int *nunitsp)
738 {
739     struct nstr_item ni;
740     struct plnstr pln;
741     int loaded = 0;
742     char *p;
743     char prompt[512];
744     char buf[1024];
745     struct lchrstr *lcp = lchr + lp->lnd_type;
746
747     if (!lcp->l_nxlight) {
748         if (noisy)
749             pr("%s cannot carry extra-light planes.\n", prland(lp));
750         return 0;
751     }
752     if (load_unload == LOAD && lnd_nxlight(lp) >= lcp->l_nxlight) {
753         if (noisy)
754             pr("%s doesn't have room for any more extra-light planes\n",
755                prland(lp));
756         return 0;
757     }
758     sprintf(prompt, "Plane(s) to %s %s? ",
759             load_unload == LOAD ? "load onto" : "unload from", prland(lp));
760     p = getstarg(player->argp[3], prompt, buf);
761     if (!p)
762         return RET_SYN;
763     if (!snxtitem(&ni, EF_PLANE, p, NULL))
764         return RET_SYN;
765
766     if (!still_ok_land(sectp, lp))
767         return RET_SYN;
768
769     if (noisy && p && *p)
770         noisy = isdigit(*p);
771
772     while (nxtitem(&ni, &pln)) {
773         if (!player->owner)
774             continue;
775
776         if (!(plchr[(int)pln.pln_type].pl_flags & P_E)) {
777             if (noisy)
778                 pr("You can only load xlight planes onto units.\n");
779             continue;
780         }
781
782         if (load_unload == LOAD && pln.pln_ship > -1) {
783             if (noisy)
784                 pr("%s is already on ship #%d!\n",
785                    prplane(&pln), pln.pln_ship);
786             continue;
787         }
788         if (load_unload == LOAD && pln.pln_land > -1) {
789             if (noisy)
790                 pr("%s is already on unit #%d!\n",
791                    prplane(&pln), pln.pln_land);
792             continue;
793         }
794         if (pln.pln_harden != 0) {
795             if (noisy)
796                 pr("%s has been hardened and can't be loaded\n",
797                    prplane(&pln));
798             continue;
799         }
800
801         /* Plane sanity done */
802         /* Find the right unit */
803         if (load_unload == UNLOAD) {
804             if (pln.pln_land != lp->lnd_uid)
805                 continue;
806         } else if (lp->lnd_x != pln.pln_x || lp->lnd_y != pln.pln_y)
807             continue;
808
809         /* Fit plane on unit */
810         if (load_unload == LOAD) {
811             if (!put_plane_on_land(&pln, lp)) {
812                 if (noisy)
813                     pr("Can't put plane %d on this unit!\n", pln.pln_uid);
814                 continue;
815             }
816             sprintf(buf, "loaded on %s at %s",
817                     prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
818             gift(lp->lnd_own, player->cnum, &pln, buf);
819             putplane(pln.pln_uid, &pln);
820         } else {
821             pln.pln_land = -1;
822             sprintf(buf, "unloaded at your sector at %s",
823                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
824             gift(sectp->sct_own, player->cnum, &pln, buf);
825             putplane(pln.pln_uid, &pln);
826         }
827         pr("%s %s %s at %s.\n",
828            prplane(&pln),
829            (load_unload == UNLOAD) ?
830            "unloaded from" : "loaded onto",
831            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
832         loaded = 1;
833     }
834     *nunitsp += loaded;
835     return 0;
836 }
837
838 static int
839 load_comm_land(struct sctstr *sectp, struct lndstr *lp,
840                struct ichrstr *ich, int load_unload, int *nunitsp)
841 {
842     i_type item = ich->i_uid;
843     struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
844     int land_amt, sect_amt, move_amt;
845     char prompt[512];
846     char *p;
847     char buf[1024];
848
849     sprintf(prompt, "Number of %s to %s %s at %s? ",
850             ich->i_name,
851             (load_unload == UNLOAD) ?
852             "unload from" : "load onto",
853             prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
854     p = getstarg(player->argp[3], prompt, buf);
855     if (!p || !*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 }