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