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