]> git.pond.sub.org Git - empserver/blob - src/lib/commands/load.c
Don't store land unit stats in struct lndstr, part 2
[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
408         /* Plane sanity done */
409         /* Find the right ship */
410         if (load_unload == UNLOAD) {
411             if (pln.pln_ship != sp->shp_uid)
412                 continue;
413         } else if (sp->shp_x != pln.pln_x || sp->shp_y != pln.pln_y)
414             continue;
415
416         /* ship to (plane or missle) sanity */
417         if (!could_be_on_ship(&pln, sp)) {
418             if (plchr[(int)pln.pln_type].pl_flags & P_L) {
419                 strcpy(buf, "planes");
420             } else if (plchr[(int)pln.pln_type].pl_flags & P_K) {
421                 strcpy(buf, "choppers");
422             } else if (plchr[(int)pln.pln_type].pl_flags & P_M) {
423                 strcpy(buf, "missiles");
424             } else if (plchr[(int)pln.pln_type].pl_flags & P_E) {
425                 strcpy(buf, "extra light planes");
426             }                   /* else impossible */
427             if (noisy)
428                 pr("%s cannot carry %s.\n", prship(sp), buf);
429             continue;
430         }
431         /* Fit plane on ship */
432         if (load_unload == LOAD) {
433             if (!put_plane_on_ship(&pln, sp)) {
434                 if (noisy)
435                     pr("Can't put plane %d on this ship!\n", pln.pln_uid);
436                 continue;
437             }
438             sprintf(buf, "loaded on your %s at %s",
439                     prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
440             gift(sp->shp_own, player->cnum, &pln, buf);
441             pln.pln_mission = 0;
442             putplane(pln.pln_uid, &pln);
443         } else {
444             take_plane_off_ship(&pln, sp);
445             sprintf(buf, "unloaded in your %s at %s",
446                     dchr[sectp->sct_type].d_name,
447                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
448             gift(sectp->sct_own, player->cnum, &pln, buf);
449             putplane(pln.pln_uid, &pln);
450         }
451         pr("%s %s %s at %s.\n",
452            prplane(&pln),
453            (load_unload == UNLOAD) ?
454            "unloaded from" : "loaded onto",
455            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
456         loaded = 1;
457     }
458     *nshipsp += loaded;
459     return 0;
460 }
461
462 static int
463 load_land_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
464                int load_unload, int *nshipsp)
465 {
466     struct nstr_item pni, ni;
467     struct lndstr land;
468     struct plnstr plane;
469     int loaded = 0;
470     char *p;
471     char prompt[512];
472     char buf[1024];
473     int load_spy = 0;
474
475     count_units(sp);
476     if (load_unload == LOAD) {
477         if (opt_LANDSPIES) {
478             if ((mchr[(int)sp->shp_type].m_flags & M_SUB) &&
479                 (mchr[(int)sp->shp_type].m_nland == 0)) {
480                 if (sp->shp_nland >= 2) {
481                     pr("Non-land unit carrying subs can only carry up to two spy units.\n");
482                     return 0;
483                 }
484                 /* Eh, let 'em load a spy only */
485                 load_spy = 1;
486             }
487         }
488         if ((!load_spy) &&
489             (sp->shp_nland >= mchr[(int)sp->shp_type].m_nland)) {
490             if (noisy) {
491                 if (mchr[(int)sp->shp_type].m_nland)
492                     pr("%s doesn't have room for any more land units!\n",
493                        prship(sp));
494                 else
495                     pr("%s cannot carry land units!\n", prship(sp));
496             }
497             return 0;
498         }
499     }
500     sprintf(prompt, "Land unit(s) to %s %s? ",
501             load_unload == LOAD ? "load onto" : "unload from", prship(sp));
502     if (!snxtitem(&ni, EF_LAND,
503                   p = getstarg(player->argp[3], prompt, buf)))
504         return RET_SYN;
505
506     if (!still_ok_ship(sectp, sp))
507         return RET_SYN;
508
509     if (noisy && p && *p)
510         noisy = isdigit(*p);
511
512     while (nxtitem(&ni, &land)) {
513         if (land.lnd_own != player->cnum)
514             continue;
515
516         if (load_unload == LOAD) {
517             if (land.lnd_ship > -1) {
518                 if (noisy)
519                     pr("%s is already on ship #%d!\n",
520                        prland(&land), land.lnd_ship);
521                 continue;
522             }
523             if (land.lnd_land > -1) {
524                 if (noisy)
525                     pr("%s is already on land #%d!\n",
526                        prland(&land), land.lnd_land);
527                 continue;
528             }
529             lnd_count_units(&land);
530             if (land.lnd_nland > 0) {
531                 if (noisy)
532                     pr("%s cannot be loaded since it is carrying units\n",
533                        prland(&land));
534                 continue;
535             }
536             if (lchr[(int)land.lnd_type].l_flags & L_HEAVY) {
537                 if (noisy)
538                     pr("%s is too heavy to load.\n", prland(&land));
539                 continue;
540             }
541             if (load_spy && !(lchr[(int)land.lnd_type].l_flags & L_SPY)) {
542                 if (noisy)
543                     pr("Subs can only carry spy units.\n");
544                 continue;
545             }
546         }
547
548         /* Unit sanity done */
549         /* Find the right ship */
550         if (load_unload == UNLOAD) {
551             if (land.lnd_ship != sp->shp_uid)
552                 continue;
553             if (land.lnd_land > -1)
554                 continue;
555         } else if (sp->shp_x != land.lnd_x || sp->shp_y != land.lnd_y)
556             continue;
557
558         if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
559             (!((mchr[(int)sp->shp_type].m_flags & M_SUPPLY) &&
560                (!(mchr[(int)sp->shp_type].m_flags & M_SUB))))) {
561             if (noisy) {
562                 pr("You can only load light units onto ships,\n");
563                 pr("unless the ship is a non-sub supply ship\n");
564                 pr("%s not loaded\n", prland(&land));
565             }
566             continue;
567         }
568         /* Fit unit on ship */
569         if (load_unload == LOAD) {
570             count_units(sp);
571             /* We have to check again, since it may have changed */
572             if (opt_LANDSPIES) {
573                 if ((mchr[(int)sp->shp_type].m_flags & M_SUB) &&
574                     (mchr[(int)sp->shp_type].m_nland == 0)) {
575                     if (sp->shp_nland >= 2) {
576                         pr("Non-land unit carrying subs can only carry up to two spy units.\n");
577                         return 0;
578                     }
579                     /* Eh, let 'em load a spy only */
580                     load_spy = 1;
581                 }
582             }
583             if (!load_spy &&
584                 (sp->shp_nland >= mchr[(int)sp->shp_type].m_nland)) {
585                 if (noisy) {
586                     if (mchr[(int)sp->shp_type].m_nland)
587                         pr("%s doesn't have room for any more land units!\n",
588                            prship(sp));
589                     else
590                         pr("%s cannot carry land units!\n", prship(sp));
591                 }
592                 return 0;
593             }
594             sprintf(buf, "loaded on your %s at %s",
595                     prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
596             gift(sp->shp_own, player->cnum, &land, buf);
597             land.lnd_ship = sp->shp_uid;
598             land.lnd_harden = 0;
599             land.lnd_mission = 0;
600             resupply_all(&land);
601             sp->shp_nland++;
602             putland(land.lnd_uid, &land);
603             if (!has_supply(&land))
604                 pr("WARNING: %s is out of supply!\n", prland(&land));
605             putship(sp->shp_uid, sp);
606             snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
607             while (nxtitem(&pni, &plane)) {
608                 if (plane.pln_flags & PLN_LAUNCHED)
609                     continue;
610                 if (plane.pln_land != land.lnd_uid)
611                     continue;
612                 sprintf(buf, "loaded on %s", prship(sp));
613                 gift(sp->shp_own, player->cnum, &plane, buf);
614                 plane.pln_mission = 0;
615                 putplane(plane.pln_uid, &plane);
616             }
617         } else {
618             sprintf(buf, "unloaded in your %s at %s",
619                     dchr[sectp->sct_type].d_name,
620                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
621
622             /* Spies are unloaded quietly, others aren't */
623             if (!(lchr[(int)land.lnd_type].l_flags & L_SPY))
624                 gift(sectp->sct_own, player->cnum, &land, buf);
625             land.lnd_ship = -1;
626             sp->shp_nland--;
627             putland(land.lnd_uid, &land);
628             putship(sp->shp_uid, sp);
629
630             /* Spies are unloaded quietly, others aren't, and
631                in the off chance they can carry a plane (missile?)
632                they are quietly unloaded too. */
633             if (!(lchr[(int)land.lnd_type].l_flags & L_SPY)) {
634                 snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
635                 while (nxtitem(&pni, &plane)) {
636                     if (plane.pln_flags & PLN_LAUNCHED)
637                         continue;
638                     if (plane.pln_land != land.lnd_uid)
639                         continue;
640                     sprintf(buf, "unloaded at %s",
641                             xyas(plane.pln_x, plane.pln_y,
642                                  sectp->sct_own));
643                     gift(sectp->sct_own, player->cnum, &plane, buf);
644                     plane.pln_mission = 0;
645                     putplane(plane.pln_uid, &plane);
646                 }
647             }
648         }
649         pr("%s %s %s at %s.\n",
650            prland(&land),
651            (load_unload == UNLOAD) ?
652            "unloaded from" : "loaded onto",
653            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
654         loaded = 1;
655     }
656     *nshipsp += loaded;
657     return 0;
658 }
659
660 static int
661 load_comm_ship(struct sctstr *sectp, struct shpstr *sp,
662                struct ichrstr *ich, int load_unload, int *nshipsp)
663 {
664     i_type item = ich->i_uid;
665     struct mchrstr *mcp = &mchr[(int)sp->shp_type];
666     int ship_amt, ship_max, sect_amt, move_amt;
667     int amount;
668     char prompt[512];
669     char *p;
670     char buf[1024];
671
672     sprintf(prompt, "Number of %s to %s %s at %s? ",
673             ich->i_name,
674             (load_unload == UNLOAD) ?
675             "unload from" : "load onto",
676             prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
677     if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
678         return RET_SYN;
679
680     if (!still_ok_ship(sectp, sp))
681         return RET_SYN;
682
683     ship_amt = sp->shp_item[item];
684     ship_max = mcp->m_item[item];
685     sect_amt = sectp->sct_item[item];
686     amount = atoi(p);
687     if (amount < 0)
688         move_amt = -amount - ship_amt;
689     else
690         move_amt = load_unload == LOAD ? amount : -amount;
691     if (move_amt > ship_max - ship_amt)
692         move_amt = ship_max - ship_amt;
693     if (move_amt < -ship_amt)
694         move_amt = -ship_amt;
695     if (move_amt > sect_amt)
696         move_amt = sect_amt;
697     if (move_amt < sect_amt - ITEM_MAX)
698         move_amt = sect_amt - ITEM_MAX;
699     if (!move_amt)
700         return RET_OK;
701     if (sectp->sct_oldown != player->cnum && item == I_CIVIL) {
702         pr("%s civilians refuse to %s at %s!\n",
703            move_amt < 0 ? "Your" : "Foreign",
704            move_amt < 0 ? "disembark" : "board",
705            xyas(sectp->sct_x, sectp->sct_y, player->cnum));
706         return RET_FAIL;
707     }
708
709     if (!want_to_abandon(sectp, item, move_amt, 0))
710         return RET_FAIL;
711     if (!still_ok_ship(sectp, sp))
712         return RET_SYN;
713     sectp->sct_item[item] = sect_amt - move_amt;
714     sp->shp_item[item] = ship_amt + move_amt;
715     if (move_amt >= 0) {
716         pr("%d %s loaded onto %s at %s\n",
717            move_amt, ich->i_name,
718            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
719         if (sp->shp_own != player->cnum) {
720             wu(0, sp->shp_own, "%s loaded %d %s onto %s at %s\n",
721                cname(player->cnum), move_amt, ich->i_name,
722                prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
723         }
724     } else {
725         pr("%d %s unloaded from %s at %s\n",
726            -move_amt, ich->i_name,
727            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
728         if (sectp->sct_own != player->cnum) {
729             wu(0, sectp->sct_own, "%s unloaded %d %s from %s at %s\n",
730                cname(player->cnum), -move_amt, ich->i_name,
731                prship(sp), xyas(sp->shp_x, sp->shp_y, sectp->sct_own));
732         }
733     }
734     ++*nshipsp;
735     return 0;
736 }
737
738 static int
739 load_plane_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
740                 int load_unload, int *nunitsp)
741 {
742     struct nstr_item ni;
743     struct plnstr pln;
744     int loaded = 0;
745     char *p;
746     char prompt[512];
747     char buf[1024];
748     struct lchrstr *lcp = lchr + lp->lnd_type;
749
750     if (!lcp->l_nxlight) {
751         if (noisy)
752             pr("%s cannot carry extra-light planes.\n", prland(lp));
753         return 0;
754     }
755     count_land_planes(lp);
756     if (load_unload == LOAD && lp->lnd_nxlight >= lcp->l_nxlight) {
757         if (noisy)
758             pr("%s doesn't have room for any more extra-light planes\n",
759                prland(lp));
760         return 0;
761     }
762     sprintf(prompt, "Plane(s) to %s %s? ",
763             load_unload == LOAD ? "load onto" : "unload from", prland(lp));
764     if (!snxtitem(&ni, EF_PLANE,
765                   p = getstarg(player->argp[3], prompt, buf)))
766         return RET_SYN;
767
768     if (!still_ok_land(sectp, lp))
769         return RET_SYN;
770
771     if (noisy && p && *p)
772         noisy = isdigit(*p);
773
774     if (sectp->sct_own != player->cnum && load_unload == LOAD) {
775         pr("Sector %s is not yours.\n",
776            xyas(lp->lnd_x, lp->lnd_y, player->cnum));
777         return 0;
778     }
779
780     while (nxtitem(&ni, &pln)) {
781         if (pln.pln_own != player->cnum)
782             continue;
783
784         if (!(plchr[(int)pln.pln_type].pl_flags & P_E)) {
785             if (noisy)
786                 pr("You can only load xlight planes onto units.\n");
787             continue;
788         }
789
790         if (load_unload == LOAD && pln.pln_ship > -1) {
791             if (noisy)
792                 pr("%s is already on ship #%d!\n",
793                    prplane(&pln), pln.pln_ship);
794             continue;
795         }
796         if (load_unload == LOAD && pln.pln_land > -1) {
797             if (noisy)
798                 pr("%s is already on unit #%d!\n",
799                    prplane(&pln), pln.pln_land);
800             continue;
801         }
802         /* Plane sanity done */
803         /* Find the right unit */
804         if (load_unload == UNLOAD) {
805             if (pln.pln_land != lp->lnd_uid)
806                 continue;
807         } else if (lp->lnd_x != pln.pln_x || lp->lnd_y != pln.pln_y)
808             continue;
809
810         /* Fit plane on unit */
811         if (load_unload == LOAD) {
812             if (!put_plane_on_land(&pln, lp)) {
813                 if (noisy)
814                     pr("Can't put plane %d on this unit!\n", pln.pln_uid);
815                 continue;
816             }
817             sprintf(buf, "loaded on %s at %s",
818                     prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
819             gift(lp->lnd_own, player->cnum, &pln, buf);
820             putplane(pln.pln_uid, &pln);
821         } else {
822             take_plane_off_land(&pln, lp);
823             sprintf(buf, "unloaded at your sector at %s",
824                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
825             gift(sectp->sct_own, player->cnum, &pln, buf);
826             putplane(pln.pln_uid, &pln);
827         }
828         pr("%s %s %s at %s.\n",
829            prplane(&pln),
830            (load_unload == UNLOAD) ?
831            "unloaded from" : "loaded onto",
832            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
833         loaded = 1;
834     }
835     *nunitsp += loaded;
836     return 0;
837 }
838
839 static int
840 load_comm_land(struct sctstr *sectp, struct lndstr *lp,
841                struct ichrstr *ich, int load_unload, int *nunitsp)
842 {
843     i_type item = ich->i_uid;
844     struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
845     int land_amt, land_max, sect_amt, move_amt;
846     int amount;
847     char prompt[512];
848     char *p;
849     char buf[1024];
850
851     sprintf(prompt, "Number of %s to %s %s at %s? ",
852             ich->i_name,
853             (load_unload == UNLOAD) ?
854             "unload from" : "load onto",
855             prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
856     if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
857         return RET_SYN;
858
859     if (!still_ok_land(sectp, lp))
860         return RET_SYN;
861
862     land_amt = lp->lnd_item[item];
863     land_max = lcp->l_item[item];
864     sect_amt = sectp->sct_item[item];
865     amount = atoi(p);
866     if (amount < 0)
867         move_amt = -amount - land_amt;
868     else
869         move_amt = load_unload == LOAD ? amount : -amount;
870     if (move_amt > land_max - land_amt)
871         move_amt = land_max - land_amt;
872     if (move_amt < -land_amt)
873         move_amt = -land_amt;
874     if (move_amt > sect_amt)
875         move_amt = sect_amt;
876     if (move_amt < sect_amt - ITEM_MAX)
877         move_amt = sect_amt - ITEM_MAX;
878     if (!move_amt)
879         return RET_OK;
880     if (sectp->sct_own != player->cnum && move_amt > 0) {
881         pr("Sector %s is not yours.\n",
882            xyas(lp->lnd_x, lp->lnd_y, player->cnum));
883         return RET_FAIL;
884     }
885     if (sectp->sct_oldown != player->cnum && item == I_CIVIL) {
886         pr("%s civilians refuse to %s at %s!\n",
887            move_amt < 0 ? "Your" : "Foreign",
888            move_amt < 0 ? "disembark" : "board",
889            xyas(sectp->sct_x, sectp->sct_y, player->cnum));
890         return RET_FAIL;
891     }
892     sectp->sct_item[item] = sect_amt - move_amt;
893     lp->lnd_item[item] = land_amt + move_amt;
894
895     /* Did we put mils onto this unit? If so, reset the fortification */
896     if (item == I_MILIT && move_amt > 0)
897         lp->lnd_harden = 0;
898     if (move_amt >= 0) {
899         pr("%d %s loaded onto %s at %s\n",
900            move_amt, ich->i_name,
901            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
902         if (lp->lnd_own != player->cnum) {
903             wu(0, lp->lnd_own, "%s loaded %d %s onto %s at %s\n",
904                cname(player->cnum), move_amt, ich->i_name,
905                prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
906         }
907     } else {
908         pr("%d %s unloaded from %s at %s\n",
909            -move_amt, ich->i_name,
910            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
911         if (sectp->sct_own != player->cnum) {
912             wu(0, sectp->sct_own, "%s unloaded %d %s from %s at %s\n",
913                cname(player->cnum), -move_amt, ich->i_name,
914                prland(lp), xyas(lp->lnd_x, lp->lnd_y, sectp->sct_own));
915         }
916     }
917     ++*nunitsp;
918     return 0;
919 }
920
921 static int
922 load_land_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
923                int load_unload, int *nunitsp)
924 {
925     struct nstr_item pni, ni;
926     struct lndstr land;
927     struct plnstr plane;
928     int loaded = 0;
929     char *p;
930     char prompt[512];
931     char buf[1024];
932
933     lnd_count_units(lp);
934
935     if (load_unload == LOAD
936         && lp->lnd_nland >= lchr[lp->lnd_type].l_nland) {
937         if (noisy) {
938             if (lp->lnd_nland)
939                 pr("%s doesn't have room for any more land units!\n",
940                    prland(lp));
941             else
942                 pr("%s cannot carry land units!\n", prland(lp));
943         }
944         return 0;
945     }
946     sprintf(prompt, "Land unit(s) to %s %s? ",
947             load_unload == LOAD ? "load onto" : "unload from", prland(lp));
948     if (!snxtitem(&ni, EF_LAND,
949                   p = getstarg(player->argp[3], prompt, buf)))
950         return RET_SYN;
951
952     if (!still_ok_land(sectp, lp))
953         return RET_SYN;
954
955     if (noisy && p && *p)
956         noisy = isdigit(*p);
957
958     while (nxtitem(&ni, &land)) {
959
960         if (land.lnd_own != player->cnum)
961             continue;
962
963         if (load_unload == LOAD) {
964             if (land.lnd_ship > -1) {
965                 if (noisy)
966                     pr("%s is already on ship #%d!\n",
967                        prland(&land), land.lnd_ship);
968                 continue;
969             }
970             if (land.lnd_land > -1) {
971                 if (noisy)
972                     pr("%s is already on land #%d!\n",
973                        prland(&land), land.lnd_land);
974                 continue;
975             }
976             lnd_count_units(&land);
977             if (land.lnd_nland > 0) {
978                 if (noisy)
979                     pr("%s cannot be loaded since it is carrying units\n",
980                        prland(&land));
981                 continue;
982             }
983             if (land.lnd_uid == lp->lnd_uid) {
984                 if (noisy)
985                     pr("%s can't be loaded onto itself!\n", prland(&land));
986                 continue;
987             }
988             if (lchr[(int)land.lnd_type].l_flags & L_HEAVY) {
989                 if (noisy)
990                     pr("%s is too heavy to load.\n", prland(&land));
991                 continue;
992             }
993         }
994
995         /* Unit sanity done */
996         /* Find the right ship */
997         if (load_unload == UNLOAD) {
998             if (land.lnd_land != lp->lnd_uid)
999                 continue;
1000             if (land.lnd_ship > -1)
1001                 continue;
1002         } else if (lp->lnd_x != land.lnd_x || lp->lnd_y != land.lnd_y)
1003             continue;
1004
1005         /* Fit unit on ship */
1006         if (load_unload == LOAD) {
1007             lnd_count_units(lp);
1008             if (lp->lnd_nland >= lchr[lp->lnd_type].l_nland) {
1009                 if (noisy) {
1010                     if (lp->lnd_nland)
1011                         pr("%s doesn't have room for any more land units!\n",
1012                            prland(lp));
1013                     else
1014                         pr("%s cannot carry land units!\n", prland(lp));
1015                 }
1016                 break;
1017             }
1018             sprintf(buf, "loaded on your %s at %s",
1019                     prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
1020             gift(lp->lnd_own, player->cnum, &land, buf);
1021             land.lnd_land = lp->lnd_uid;
1022             land.lnd_harden = 0;
1023             land.lnd_mission = 0;
1024             resupply_all(&land);
1025             lp->lnd_nland++;
1026             putland(land.lnd_uid, &land);
1027             if (!has_supply(&land))
1028                 pr("WARNING: %s is out of supply!\n", prland(&land));
1029             putland(lp->lnd_uid, lp);
1030             snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
1031             while (nxtitem(&pni, &plane)) {
1032                 if (plane.pln_flags & PLN_LAUNCHED)
1033                     continue;
1034                 if (plane.pln_land != land.lnd_uid)
1035                     continue;
1036                 sprintf(buf, "loaded on %s", prland(lp));
1037                 gift(lp->lnd_own, player->cnum, &plane, buf);
1038                 plane.pln_mission = 0;
1039                 putplane(plane.pln_uid, &plane);
1040             }
1041         } else {
1042             sprintf(buf, "unloaded in your %s at %s",
1043                     dchr[sectp->sct_type].d_name,
1044                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
1045             gift(sectp->sct_own, player->cnum, &land, buf);
1046             land.lnd_land = -1;
1047             lp->lnd_nland--;
1048             putland(land.lnd_uid, &land);
1049             putland(lp->lnd_uid, lp);
1050             snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
1051             while (nxtitem(&pni, &plane)) {
1052                 if (plane.pln_flags & PLN_LAUNCHED)
1053                     continue;
1054                 if (plane.pln_land != land.lnd_uid)
1055                     continue;
1056                 sprintf(buf, "unloaded at %s",
1057                         xyas(plane.pln_x, plane.pln_y, sectp->sct_own));
1058                 gift(sectp->sct_own, player->cnum, &plane, buf);
1059                 plane.pln_mission = 0;
1060                 putplane(plane.pln_uid, &plane);
1061             }
1062         }
1063         pr("%s %s %s at %s.\n",
1064            prland(&land),
1065            (load_unload == UNLOAD) ?
1066            "unloaded from" : "loaded onto",
1067            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
1068         loaded = 1;
1069     }
1070     *nunitsp += loaded;
1071     return 0;
1072 }