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