]> git.pond.sub.org Git - empserver/blob - src/lib/commands/load.c
(gift, load_comm_ship, load_comm_land): Fix dangerous misuse of wu():
[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     s_char *p;
88     s_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     s_char *p;
227     s_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(int givee, int giver, s_char *ptr, int type, s_char *mesg)
323 {
324     s_char *p;
325
326     if (giver == givee)
327         return;
328
329     switch (type) {
330     case EF_SHIP:
331         p = prship((struct shpstr *)ptr);
332         break;
333     case EF_PLANE:
334         p = prplane((struct plnstr *)ptr);
335         break;
336     case EF_LAND:
337         p = prland((struct lndstr *)ptr);
338         break;
339     default:
340         CANT_HAPPEN("bad TYPE");
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     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, &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, &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             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, (s_char *)&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, (s_char *)&plane,
656                      EF_PLANE, buf);
657                 makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
658                          plane.pln_x, plane.pln_y);
659                 plane.pln_own = sp->shp_own;
660                 makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
661                             plane.pln_x, plane.pln_y);
662                 plane.pln_mission = 0;
663                 putplane(plane.pln_uid, &plane);
664             }
665         } else {
666             sprintf(buf, "unloaded in your %s at %s",
667                     dchr[sectp->sct_type].d_name,
668                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
669
670             /* Spies are unloaded quietly, others aren't */
671             if (!(lchr[(int)land.lnd_type].l_flags & L_SPY)) {
672                 gift(sectp->sct_own, player->cnum, (s_char *)&land,
673                      EF_LAND, buf);
674                 makelost(EF_LAND, land.lnd_own, land.lnd_uid,
675                          land.lnd_x, land.lnd_y);
676                 land.lnd_own = sectp->sct_own;
677                 makenotlost(EF_LAND, land.lnd_own, land.lnd_uid,
678                             land.lnd_x, land.lnd_y);
679             }
680
681             land.lnd_ship = -1;
682             sp->shp_nland--;
683             putland(land.lnd_uid, &land);
684             putship(sp->shp_uid, sp);
685
686             /* Spies are unloaded quietly, others aren't, and
687                in the off chance they can carry a plane (missile?)
688                they are quietly unloaded too. */
689             if (!(lchr[(int)land.lnd_type].l_flags & L_SPY)) {
690                 snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
691                 while (nxtitem(&pni, &plane)) {
692                     if (plane.pln_flags & PLN_LAUNCHED)
693                         continue;
694                     if (plane.pln_land != land.lnd_uid)
695                         continue;
696                     sprintf(buf, "unloaded at %s",
697                             xyas(plane.pln_x, plane.pln_y,
698                                  sectp->sct_own));
699                     gift(sectp->sct_own, player->cnum, (s_char *)&plane,
700                          EF_PLANE, buf);
701                     makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
702                              plane.pln_x, plane.pln_y);
703                     plane.pln_own = sectp->sct_own;
704                     makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
705                                 plane.pln_x, plane.pln_y);
706                     plane.pln_mission = 0;
707                     putplane(plane.pln_uid, &plane);
708                 }
709             }
710         }
711         pr("%s %s %s at %s.\n",
712            prland(&land),
713            (load_unload == UNLOAD) ?
714            "unloaded from" : "loaded onto",
715            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
716         loaded = 1;
717     }
718     *nshipsp += loaded;
719     return 0;
720 }
721
722 static int
723 load_comm_ship(struct sctstr *sectp, struct shpstr *sp,
724                struct ichrstr *ich, int load_unload, int *nshipsp)
725 {
726     i_type item = ich->i_uid;
727     struct mchrstr *mcp = &mchr[(int)sp->shp_type];
728     int ship_amt, ship_max, sect_amt, move_amt;
729     int amount;
730     s_char prompt[512];
731     s_char *p;
732     s_char buf[1024];
733
734     sprintf(prompt, "Number of %s to %s %s at %s? ",
735             ich->i_name,
736             (load_unload == UNLOAD) ?
737             "unload from" : "load onto",
738             prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
739     if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
740         return RET_SYN;
741
742     if (!still_ok_ship(sectp, sp))
743         return RET_SYN;
744
745     ship_amt = sp->shp_item[item];
746     ship_max = mcp->m_item[item];
747     sect_amt = sectp->sct_item[item];
748     amount = atoi(p);
749     if (amount < 0)
750         move_amt = -amount - ship_amt;
751     else
752         move_amt = load_unload == LOAD ? amount : -amount;
753     if (move_amt > ship_max - ship_amt)
754         move_amt = ship_max - ship_amt;
755     if (move_amt < -ship_amt)
756         move_amt = -ship_amt;
757     if (move_amt > sect_amt)
758         move_amt = sect_amt;
759     if (move_amt < sect_amt - ITEM_MAX)
760         move_amt = sect_amt - ITEM_MAX;
761     if (!move_amt)
762         return RET_OK;
763     if (sectp->sct_oldown != player->cnum && item == I_CIVIL) {
764         pr("%s civilians refuse to %s at %s!\n",
765            move_amt < 0 ? "Your" : "Foreign",
766            move_amt < 0 ? "disembark" : "board",
767            xyas(sectp->sct_x, sectp->sct_y, player->cnum));
768         return RET_FAIL;
769     }
770
771     if (!want_to_abandon(sectp, item, move_amt, 0))
772         return RET_FAIL;
773     if (!still_ok_ship(sectp, sp))
774         return RET_SYN;
775     sectp->sct_item[item] = sect_amt - move_amt;
776     sp->shp_item[item] = ship_amt + move_amt;
777     if (move_amt >= 0) {
778         pr("%d %s loaded onto %s at %s\n",
779            move_amt, ich->i_name,
780            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
781         if (sp->shp_own != player->cnum) {
782             wu(0, sp->shp_own, "%s loaded %d %s onto %s at %s\n",
783                cname(player->cnum), move_amt, ich->i_name,
784                prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
785         }
786     } else {
787         pr("%d %s unloaded from %s at %s\n",
788            -move_amt, ich->i_name,
789            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
790         if (sectp->sct_own != player->cnum) {
791             wu(0, sectp->sct_own, "%s unloaded %d %s from %s at %s\n",
792                cname(player->cnum), -move_amt, ich->i_name,
793                prship(sp), xyas(sp->shp_x, sp->shp_y, sectp->sct_own));
794         }
795     }
796     ++*nshipsp;
797     return 0;
798 }
799
800 static int
801 load_plane_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
802                 int load_unload, int *nunitsp)
803 {
804     struct nstr_item ni;
805     struct plnstr pln;
806     int loaded = 0;
807     s_char *p;
808     s_char prompt[512];
809     s_char buf[1024];
810
811     if (!lp->lnd_maxlight) {
812         if (noisy)
813             pr("%s cannot carry extra-light planes.\n", prland(lp));
814         return 0;
815     }
816     count_land_planes(lp);
817     if (load_unload == LOAD && lp->lnd_nxlight >= lp->lnd_maxlight) {
818         if (noisy)
819             pr("%s doesn't have room for any more extra-light planes\n",
820                prland(lp));
821         return 0;
822     }
823     sprintf(prompt, "Plane(s) to %s %s? ",
824             load_unload == LOAD ? "load onto" : "unload from", prland(lp));
825     if (!snxtitem(&ni, EF_PLANE,
826                   p = getstarg(player->argp[3], prompt, buf)))
827         return RET_SYN;
828
829     if (!still_ok_land(sectp, lp))
830         return RET_SYN;
831
832     if (p && *p)
833         noisy &= isdigit(*p);
834
835     if (sectp->sct_own != player->cnum && load_unload == LOAD) {
836         pr("Sector %s is not yours.\n",
837            xyas(lp->lnd_x, lp->lnd_y, player->cnum));
838         return 0;
839     }
840
841     while (nxtitem(&ni, &pln)) {
842         if (pln.pln_own != player->cnum)
843             continue;
844
845         if (!(plchr[(int)pln.pln_type].pl_flags & P_E)) {
846             if (noisy)
847                 pr("You can only load xlight planes onto units.\n");
848             continue;
849         }
850
851         if (load_unload == LOAD && pln.pln_ship > -1) {
852             if (noisy)
853                 pr("%s is already on ship #%d!\n",
854                    prplane(&pln), pln.pln_ship);
855             continue;
856         }
857         if (load_unload == LOAD && pln.pln_land > -1) {
858             if (noisy)
859                 pr("%s is already on unit #%d!\n",
860                    prplane(&pln), pln.pln_land);
861             continue;
862         }
863         /* Plane sanity done */
864         /* Find the right unit */
865         if (load_unload == UNLOAD) {
866             if (pln.pln_land != lp->lnd_uid)
867                 continue;
868         } else if (lp->lnd_x != pln.pln_x || lp->lnd_y != pln.pln_y)
869             continue;
870
871         /* Fit plane on unit */
872         if (load_unload == LOAD) {
873             if (!put_plane_on_land(&pln, lp)) {
874                 if (noisy)
875                     pr("Can't put plane %d on this unit!\n", pln.pln_uid);
876                 continue;
877             }
878             sprintf(buf, "loaded on %s at %s",
879                     prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
880             gift(lp->lnd_own, player->cnum, (s_char *)&pln, EF_PLANE, buf);
881             makelost(EF_PLANE, pln.pln_own, pln.pln_uid, pln.pln_x,
882                      pln.pln_y);
883             pln.pln_own = lp->lnd_own;
884             makenotlost(EF_PLANE, pln.pln_own, pln.pln_uid, pln.pln_x,
885                         pln.pln_y);
886             putplane(pln.pln_uid, &pln);
887         } else {
888             if (!take_plane_off_land(&pln, lp)) {
889                 pr("Unable to take plane off unit!\n");
890                 logerror("load: plane %d could not be taken off unit %d\n",
891                          pln.pln_uid, lp->lnd_uid);
892                 continue;
893             }
894             sprintf(buf, "unloaded at your sector at %s",
895                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
896             gift(sectp->sct_own, player->cnum, (s_char *)&pln,
897                  EF_PLANE, buf);
898             makelost(EF_PLANE, pln.pln_own, pln.pln_uid, pln.pln_x,
899                      pln.pln_y);
900             pln.pln_own = sectp->sct_own;
901             makenotlost(EF_PLANE, pln.pln_own, pln.pln_uid, pln.pln_x,
902                         pln.pln_y);
903             putplane(pln.pln_uid, &pln);
904         }
905         pr("%s %s %s at %s.\n",
906            prplane(&pln),
907            (load_unload == UNLOAD) ?
908            "unloaded from" : "loaded onto",
909            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
910         loaded = 1;
911     }
912     *nunitsp += loaded;
913     return 0;
914 }
915
916 static int
917 load_comm_land(struct sctstr *sectp, struct lndstr *lp,
918                struct ichrstr *ich, int load_unload, int *nunitsp)
919 {
920     i_type item = ich->i_uid;
921     struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
922     int land_amt, land_max, sect_amt, move_amt;
923     int amount;
924     s_char prompt[512];
925     s_char *p;
926     s_char buf[1024];
927
928     sprintf(prompt, "Number of %s to %s %s at %s? ",
929             ich->i_name,
930             (load_unload == UNLOAD) ?
931             "unload from" : "load onto",
932             prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
933     if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
934         return RET_SYN;
935
936     if (!still_ok_land(sectp, lp))
937         return RET_SYN;
938
939     land_amt = lp->lnd_item[item];
940     land_max = lcp->l_item[item];
941     sect_amt = sectp->sct_item[item];
942     amount = atoi(p);
943     if (amount < 0)
944         move_amt = -amount - land_amt;
945     else
946         move_amt = load_unload == LOAD ? amount : -amount;
947     if (move_amt > land_max - land_amt)
948         move_amt = land_max - land_amt;
949     if (move_amt < -land_amt)
950         move_amt = -land_amt;
951     if (move_amt > sect_amt)
952         move_amt = sect_amt;
953     if (move_amt < sect_amt - ITEM_MAX)
954         move_amt = sect_amt - ITEM_MAX;
955     if (!move_amt)
956         return RET_OK;
957     if (sectp->sct_own != player->cnum && move_amt > 0) {
958         pr("Sector %s is not yours.\n",
959            xyas(lp->lnd_x, lp->lnd_y, player->cnum));
960         return RET_FAIL;
961     }
962     if (sectp->sct_oldown != player->cnum && item == I_CIVIL) {
963         pr("%s civilians refuse to %s at %s!\n",
964            move_amt < 0 ? "Your" : "Foreign",
965            move_amt < 0 ? "disembark" : "board",
966            xyas(sectp->sct_x, sectp->sct_y, player->cnum));
967         return RET_FAIL;
968     }
969     sectp->sct_item[item] = sect_amt - move_amt;
970     lp->lnd_item[item] = land_amt + move_amt;
971
972     /* Did we put mils onto this unit? If so, reset the fortification */
973     if (item == I_MILIT && move_amt > 0)
974         lp->lnd_harden = 0;
975     if (move_amt >= 0) {
976         pr("%d %s loaded onto %s at %s\n",
977            move_amt, ich->i_name,
978            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
979         if (lp->lnd_own != player->cnum) {
980             wu(0, lp->lnd_own, "%s loaded %d %s onto %s at %s\n",
981                cname(player->cnum), move_amt, ich->i_name,
982                prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
983         }
984     } else {
985         pr("%d %s unloaded from %s at %s\n",
986            -move_amt, ich->i_name,
987            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
988         if (sectp->sct_own != player->cnum) {
989             wu(0, sectp->sct_own, "%s unloaded %d %s from %s at %s\n",
990                cname(player->cnum), -move_amt, ich->i_name,
991                prland(lp), xyas(lp->lnd_x, lp->lnd_y, sectp->sct_own));
992         }
993     }
994     ++*nunitsp;
995     return 0;
996 }
997
998 static int
999 load_land_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
1000                int load_unload, int *nunitsp)
1001 {
1002     struct nstr_item pni, ni;
1003     struct lndstr land;
1004     struct plnstr plane;
1005     int loaded = 0;
1006     s_char *p;
1007     s_char prompt[512];
1008     s_char buf[1024];
1009
1010     lnd_count_units(lp);
1011
1012     if (load_unload == LOAD && lp->lnd_nland >= lp->lnd_maxland) {
1013         if (noisy) {
1014             if (lp->lnd_nland)
1015                 pr("%s doesn't have room for any more land units!\n",
1016                    prland(lp));
1017             else
1018                 pr("%s cannot carry land units!\n", prland(lp));
1019         }
1020         return 0;
1021     }
1022     sprintf(prompt, "Land unit(s) to %s %s? ",
1023             load_unload == LOAD ? "load onto" : "unload from", prland(lp));
1024     if (!snxtitem(&ni, EF_LAND,
1025                   p = getstarg(player->argp[3], prompt, buf)))
1026         return RET_SYN;
1027
1028     if (!still_ok_land(sectp, lp))
1029         return RET_SYN;
1030
1031     if (p && *p)
1032         noisy &= isdigit(*p);
1033
1034     while (nxtitem(&ni, &land)) {
1035
1036         if (land.lnd_own != player->cnum)
1037             continue;
1038
1039         if (load_unload == LOAD) {
1040             if (land.lnd_ship > -1) {
1041                 if (noisy)
1042                     pr("%s is already on ship #%d!\n",
1043                        prland(&land), land.lnd_ship);
1044                 continue;
1045             }
1046             if (land.lnd_land > -1) {
1047                 if (noisy)
1048                     pr("%s is already on land #%d!\n",
1049                        prland(&land), land.lnd_land);
1050                 continue;
1051             }
1052             lnd_count_units(&land);
1053             if (land.lnd_nland > 0) {
1054                 if (noisy)
1055                     pr("%s cannot be loaded since it is carrying units\n",
1056                        prland(&land));
1057                 continue;
1058             }
1059             if (land.lnd_uid == lp->lnd_uid) {
1060                 if (noisy)
1061                     pr("%s can't be loaded onto itself!\n", prland(&land));
1062                 continue;
1063             }
1064             if (lchr[(int)land.lnd_type].l_flags & L_HEAVY) {
1065                 if (noisy)
1066                     pr("%s is too heavy to load.\n", prland(&land));
1067                 continue;
1068             }
1069         }
1070
1071         /* Unit sanity done */
1072         /* Find the right ship */
1073         if (load_unload == UNLOAD) {
1074             if (land.lnd_land != lp->lnd_uid)
1075                 continue;
1076             if (land.lnd_ship > -1)
1077                 continue;
1078         } else if (lp->lnd_x != land.lnd_x || lp->lnd_y != land.lnd_y)
1079             continue;
1080
1081         /* Fit unit on ship */
1082         if (load_unload == LOAD) {
1083             lnd_count_units(lp);
1084             if (lp->lnd_nland >= lp->lnd_maxland) {
1085                 if (noisy) {
1086                     if (lp->lnd_nland)
1087                         pr("%s doesn't have room for any more land units!\n", prland(lp));
1088                     else
1089                         pr("%s cannot carry land units!\n", prland(lp));
1090                 }
1091                 break;
1092             }
1093             sprintf(buf, "loaded on your %s at %s",
1094                     prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
1095             gift(lp->lnd_own, player->cnum, (s_char *)&land, EF_LAND, buf);
1096             makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
1097                      land.lnd_y);
1098             land.lnd_own = lp->lnd_own;
1099             makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
1100                         land.lnd_y);
1101             land.lnd_land = lp->lnd_uid;
1102             land.lnd_harden = 0;
1103             land.lnd_mission = 0;
1104             resupply_all(&land);
1105             lp->lnd_nland++;
1106             putland(land.lnd_uid, &land);
1107             if (!has_supply(&land))
1108                 pr("WARNING: %s is out of supply!\n", prland(&land));
1109             putland(lp->lnd_uid, lp);
1110             snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
1111             while (nxtitem(&pni, &plane)) {
1112                 if (plane.pln_flags & PLN_LAUNCHED)
1113                     continue;
1114                 if (plane.pln_land != land.lnd_uid)
1115                     continue;
1116                 sprintf(buf, "loaded on %s", prland(lp));
1117                 gift(lp->lnd_own, player->cnum, (s_char *)&plane,
1118                      EF_PLANE, buf);
1119                 makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
1120                          plane.pln_x, plane.pln_y);
1121                 plane.pln_own = lp->lnd_own;
1122                 makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
1123                             plane.pln_x, plane.pln_y);
1124                 plane.pln_mission = 0;
1125                 putplane(plane.pln_uid, &plane);
1126             }
1127         } else {
1128             sprintf(buf, "unloaded in your %s at %s",
1129                     dchr[sectp->sct_type].d_name,
1130                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
1131             gift(sectp->sct_own, player->cnum, (s_char *)&land,
1132                  EF_LAND, buf);
1133             makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
1134                      land.lnd_y);
1135             land.lnd_own = sectp->sct_own;
1136             makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
1137                         land.lnd_y);
1138             land.lnd_land = -1;
1139             lp->lnd_nland--;
1140             putland(land.lnd_uid, &land);
1141             putland(lp->lnd_uid, lp);
1142             snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
1143             while (nxtitem(&pni, &plane)) {
1144                 if (plane.pln_flags & PLN_LAUNCHED)
1145                     continue;
1146                 if (plane.pln_land != land.lnd_uid)
1147                     continue;
1148                 sprintf(buf, "unloaded at %s",
1149                         xyas(plane.pln_x, plane.pln_y, sectp->sct_own));
1150                 gift(sectp->sct_own, player->cnum, (s_char *)&plane,
1151                      EF_PLANE, buf);
1152                 makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
1153                          plane.pln_x, plane.pln_y);
1154                 plane.pln_own = sectp->sct_own;
1155                 makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
1156                             plane.pln_x, plane.pln_y);
1157                 plane.pln_mission = 0;
1158                 putplane(plane.pln_uid, &plane);
1159             }
1160         }
1161         pr("%s %s %s at %s.\n",
1162            prland(&land),
1163            (load_unload == UNLOAD) ?
1164            "unloaded from" : "loaded onto",
1165            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
1166         loaded = 1;
1167     }
1168     *nunitsp += loaded;
1169     return 0;
1170 }