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