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