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