]> git.pond.sub.org Git - empserver/blob - src/lib/commands/load.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / commands / load.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 #ifdef Rel4
37 #include <string.h>
38 #endif /* Rel4 */
39 #include "misc.h"
40 #include "player.h"
41 #include "xy.h"
42 #include "file.h"
43 #include "var.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 void gift(int givee, int giver, s_char *ptr, int type, s_char *mesg);
61 static int load_plane_ship(struct sctstr *sectp, struct shpstr *sp,
62                            int noisy, int load_unload, int *nshipsp);
63 static int load_land_ship(struct sctstr *sectp, struct shpstr *sp,
64                           int noisy, int load_unload, int *nshipsp);
65 static int load_comm_ship(struct sctstr *sectp, struct shpstr *sp,
66                           struct ichrstr *ich, int load_unload,
67                           int *nshipsp);
68 static int load_plane_land(struct sctstr *sectp, struct lndstr *lp,
69                            int noisy, int load_unload, int *nunitsp);
70 static int load_land_land(struct sctstr *sectp, struct lndstr *lp,
71                           int noisy, int load_unload, int *nunitsp);
72 static int load_comm_land(struct sctstr *sectp, struct lndstr *lp,
73                           struct ichrstr *ich, int load_unload,
74                           int *nunitsp);
75
76 int
77 load(void)
78 {
79     int noisy;
80     int load_unload;
81     int type;
82     struct nstr_item nbst;
83     struct ichrstr *ich;
84     int nships;
85     struct sctstr sect;
86     struct shpstr ship;
87     int retval;
88     s_char *p;
89     s_char buf[1024];
90
91     if (!(p = getstarg(player->argp[1],
92                        "What commodity (or 'plane' or 'land')? ", buf))
93         || !*p)
94         return RET_SYN;
95
96     if (!strncmp(p, "plane", 5))
97         type = EF_PLANE;
98     else if (!strncmp(p, "land", 4))
99         type = EF_LAND;
100     else if (NULL != (ich = whatitem(p, (s_char *)0)))
101         type = EF_SECTOR;
102     else {
103         pr("Bad commodity.\n");
104         return RET_SYN;
105     }
106
107     if (!(p = getstarg(player->argp[2], "Ship(s): ", buf)) || !*p)
108         return RET_SYN;
109
110     noisy = isdigit(*p);
111
112     if (!snxtitem(&nbst, EF_SHIP, p))
113         return RET_SYN;
114
115     nbst.flags &= ~(EFF_OWNER);
116
117     load_unload = **player->argp == 'l' ? LOAD : UNLOAD;
118
119     nships = 0;
120     while (nxtitem(&nbst, (s_char *)&ship)) {
121         if (!ship.shp_own)
122             continue;
123         if (!player->owner && (load_unload == UNLOAD)) {
124             continue;
125         }
126         if (opt_MARKET) {
127             if (ontradingblock(EF_SHIP, (int *)&ship)) {
128                 pr("You cannot load/unload an item on the trading block!\n");
129                 continue;
130             }
131         }
132
133         if (ship.shp_own != player->cnum) {
134             if (!noisy)
135                 continue;
136             if (getrel(getnatp(player->cnum), ship.shp_own) < FRIENDLY)
137                 continue;
138         }
139         if (!getsect(ship.shp_x, ship.shp_y, &sect))    /* XXX */
140             continue;
141         if ((sect.sct_own != player->cnum) &&
142             (ship.shp_own != player->cnum))
143             continue;
144         if (!player->owner &&
145             sect.sct_type != SCT_HARBR &&
146             (!opt_BIG_CITY || sect.sct_type != SCT_CAPIT))
147             continue;
148         if (!sect.sct_own)
149             continue;
150         if ((sect.sct_own != player->cnum) && (load_unload == LOAD)) {
151             if (noisy)
152                 pr("You don't own %s \n",
153                    xyas(ship.shp_x, ship.shp_y, player->cnum));
154             continue;
155         }
156         if (sect.sct_type != SCT_HARBR &&
157             (!opt_BIG_CITY || sect.sct_type != SCT_CAPIT)) {
158             if (noisy)
159                 pr("Sector %s is not a harbor%s.\n",
160                    xyas(ship.shp_x, ship.shp_y, player->cnum),
161                    opt_BIG_CITY ? " or a city" : "");
162             continue;
163         }
164         if (sect.sct_own != player->cnum && load_unload == UNLOAD
165             && getrel(getnatp(sect.sct_own), player->cnum) < FRIENDLY) {
166             if (noisy)
167                 pr("You can't unload into an unfriendly %s\n",
168                    dchr[sect.sct_type].d_name);
169             continue;
170         }
171         if (sect.sct_effic < 2) {
172             if (noisy)
173                 pr("The %s at %s is not 2%% efficient yet.\n",
174                    dchr[sect.sct_type].d_name,
175                    xyas(ship.shp_x, ship.shp_y, player->cnum));
176             continue;
177         }
178         switch (type) {
179         case EF_PLANE:
180             if (0 !=
181                 (retval =
182                  load_plane_ship(&sect, &ship, noisy, load_unload,
183                                  &nships)))
184                 return retval;
185             break;
186         case EF_LAND:
187             if (0 !=
188                 (retval =
189                  load_land_ship(&sect, &ship, noisy, load_unload,
190                                 &nships)))
191                 return retval;
192             break;
193         case EF_SECTOR:
194             if (0 !=
195                 (retval =
196                  load_comm_ship(&sect, &ship, ich, load_unload, &nships)))
197                 return retval;
198         }
199         /* load/unload plague */
200         if (getvar(V_PSTAGE, (s_char *)&sect, EF_SECTOR) == PLG_INFECT &&
201             getvar(V_PSTAGE, (s_char *)&ship, EF_SHIP) == PLG_HEALTHY)
202             putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)&ship, EF_SHIP);
203         if (getvar(V_PSTAGE, (s_char *)&ship, EF_SHIP) == PLG_INFECT &&
204             getvar(V_PSTAGE, (s_char *)&sect, EF_SECTOR) == PLG_HEALTHY)
205             putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)&sect, EF_SECTOR);
206         putsect(&sect);
207         putship(ship.shp_uid, &ship);
208     }
209     if (!nships)
210         pr("No ships affected\n");
211     else
212         pr("%d ship%s %sloaded\n", nships, splur(nships),
213            load_unload == UNLOAD ? "un" : "");
214     return RET_OK;
215 }
216
217 int
218 lload(void)
219 {
220     int noisy;
221     int load_unload;
222     int type;
223     struct nstr_item nbst;
224     struct ichrstr *ich;
225     int nunits;
226     struct sctstr sect;
227     struct lndstr land;
228     int retval;
229     s_char *p;
230     s_char buf[1024];
231
232     if (!(p = getstarg(player->argp[1],
233                        "What commodity (or 'plane' or 'land')? ", buf))
234         || !*p)
235         return RET_SYN;
236     if (!strncmp(p, "plane", 5))
237         type = EF_PLANE;
238     else if (!strncmp(p, "land", 4))
239         type = EF_LAND;
240     else if (NULL != (ich = whatitem(p, (s_char *)0)))
241         type = EF_SECTOR;
242     else {
243         pr("Bad commodity.\n");
244         return RET_SYN;
245     }
246
247     if (!(p = getstarg(player->argp[2], "Unit(s): ", buf)) || !*p)
248         return RET_SYN;
249
250     noisy = isdigit(*p);
251
252     if (!snxtitem(&nbst, EF_LAND, p))
253         return RET_SYN;
254
255     load_unload = *(*player->argp + 1) == 'l' ? LOAD : UNLOAD;
256
257     nbst.flags &= ~(EFF_OWNER);
258
259     nunits = 0;
260     while (nxtitem(&nbst, (s_char *)&land)) {
261         if (land.lnd_own == 0)
262             continue;
263
264         if (player->cnum != land.lnd_own &&
265             getrel(getnatp(player->cnum), land.lnd_own) != ALLIED)
266             continue;
267
268         if (!getsect(land.lnd_x, land.lnd_y, &sect))    /* XXX */
269             continue;
270
271         if (sect.sct_own != player->cnum && land.lnd_own != player->cnum)
272             continue;
273         if (opt_MARKET) {
274             if (ontradingblock(EF_LAND, (int *)&land)) {
275                 pr("You cannot load/unload an item on the trading block!\n");
276                 continue;
277             }
278         }
279
280         if (sect.sct_own != player->cnum &&
281             getrel(getnatp(sect.sct_own), land.lnd_own) != ALLIED) {
282             pr("Sector %s is not yours.\n",
283                xyas(land.lnd_x, land.lnd_y, player->cnum));
284             continue;
285         }
286         switch (type) {
287         case EF_LAND:
288             if (0 !=
289                 (retval =
290                  load_land_land(&sect, &land, noisy, load_unload,
291                                 &nunits)))
292                 return retval;
293             break;
294         case EF_PLANE:
295             if (0 !=
296                 (retval =
297                  load_plane_land(&sect, &land, noisy, load_unload,
298                                  &nunits)))
299                 return retval;
300             break;
301         case EF_SECTOR:
302             if (0 !=
303                 (retval =
304                  load_comm_land(&sect, &land, ich, load_unload, &nunits)))
305                 return retval;
306         }
307         /* load/unload plague */
308         if (getvar(V_PSTAGE, (s_char *)&sect, EF_SECTOR) == PLG_INFECT &&
309             getvar(V_PSTAGE, (s_char *)&land, EF_LAND) == PLG_HEALTHY)
310             putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)&land, EF_LAND);
311         if (getvar(V_PSTAGE, (s_char *)&land, EF_LAND) == PLG_INFECT &&
312             getvar(V_PSTAGE, (s_char *)&sect, EF_SECTOR) == PLG_HEALTHY)
313             putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)&sect, EF_SECTOR);
314
315         putsect(&sect);
316         putland(land.lnd_uid, &land);
317     }
318     if (nunits == 0)
319         pr("No units affected\n");
320     else
321         pr("%d unit%s %sloaded\n", nunits, splur(nunits),
322            load_unload == UNLOAD ? "un" : "");
323     return RET_OK;
324 }
325
326 void
327 gift(int givee, int giver, s_char *ptr, int type, s_char *mesg)
328 {
329     s_char *p, line[256];
330
331     if (giver == givee)
332         return;
333
334
335     switch (type) {
336     case EF_SHIP:
337         p = prship((struct shpstr *)ptr);
338         break;
339     case EF_PLANE:
340         p = prplane((struct plnstr *)ptr);
341         break;
342     case EF_LAND:
343         p = prland((struct lndstr *)ptr);
344         break;
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
400         (&ni, EF_PLANE, 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
545         (&ni, EF_LAND, 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     register int max_amt;
742     register int move_amt;
743     register int ship_amt;
744     register int sect_amt;
745     int amount;
746     int upto = 0, abs_max;
747     s_char prompt[512];
748     s_char *p;
749     s_char buf[1024];
750
751     sprintf(prompt, "Number of %s to %s %s at %s? ",
752             ich->i_name,
753             (load_unload == UNLOAD) ?
754             "unload from" : "load onto",
755             prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
756     if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
757         return RET_SYN;
758
759     if (!still_ok_ship(sectp, sp))
760         return RET_SYN;
761
762     amount = atoi(p);
763     if (amount < 0) {
764         /* We want to load up to this amount */
765         upto = -(amount);
766         load_unload = LOAD;
767     } else if (!amount)
768         return 0;
769     ship_amt = getvar(item, (s_char *)sp, EF_SHIP);
770     sect_amt = getvar(item, (s_char *)sectp, EF_SECTOR);
771     if (sectp->sct_oldown != player->cnum && item == V_CIVIL) {
772         pr("%s civilians refuse to %s at %s!\n",
773            load_unload == UNLOAD ? "Your" : "Foreign",
774            load_unload == UNLOAD ?
775            "disembark" : "board",
776            xyas(sectp->sct_x, sectp->sct_y, player->cnum));
777         return 0;
778     }
779     if (load_unload == UNLOAD) {
780         abs_max = max_amt = min(9999 - sect_amt, ship_amt);
781     } else {
782         struct mchrstr *vbase;
783         vbase = &mchr[(int)sp->shp_type];
784         abs_max = max_amt = vl_find(item, vbase->m_vtype,
785                                     vbase->m_vamt, (int)vbase->m_nv);
786         max_amt = min(sect_amt, max_amt - ship_amt);
787     }
788     if (max_amt <= 0 && !upto)
789         return 0;
790     if (upto) {
791         move_amt = upto - ship_amt;
792         if (move_amt > sect_amt)
793             move_amt = sect_amt;
794         if (ship_amt + move_amt > abs_max)
795             move_amt = abs_max - ship_amt;
796     } else
797         move_amt = load_unload * min(amount, max_amt);
798     if (!move_amt)
799         return 0;
800
801     if (!want_to_abandon(sectp, item, move_amt, 0))
802         return RET_FAIL;
803     if (!still_ok_ship(sectp, sp))
804         return RET_SYN;
805     putvar(item, sect_amt - move_amt, (s_char *)sectp, EF_SECTOR);
806     putvar(item, ship_amt + move_amt, (s_char *)sp, EF_SHIP);
807     if (load_unload == LOAD) {
808         pr("%d %s loaded onto %s at %s\n",
809            move_amt,
810            ich->i_name,
811            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
812         if (sp->shp_own != player->cnum) {
813             sprintf(buf, "%s loaded %d %s onto %s at %s\n",
814                     cname(player->cnum),
815                     move_amt,
816                     ich->i_name,
817                     prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
818             wu(0, sp->shp_own, buf);
819         }
820     } else {
821         pr("%d %s unloaded from %s at %s\n",
822            -move_amt,
823            ich->i_name,
824            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
825         if (sectp->sct_own != player->cnum) {
826             sprintf(buf, "%s unloaded %d %s from %s at %s\n",
827                     cname(player->cnum),
828                     -move_amt,
829                     ich->i_name,
830                     prship(sp),
831                     xyas(sp->shp_x, sp->shp_y, sectp->sct_own));
832             wu(0, sectp->sct_own, buf);
833         }
834     }
835     ++(*nshipsp);
836     return 0;
837 }
838
839 static int
840 load_plane_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
841                 int load_unload, int *nunitsp)
842 {
843     struct nstr_item ni;
844     struct plnstr pln;
845     int loaded = 0;
846     s_char *p;
847     s_char prompt[512];
848     s_char buf[1024];
849
850     if (!(lchr[(int)lp->lnd_type].l_flags & L_XLIGHT)) {
851         if (noisy)
852             pr("%s cannot carry extra-light planes.\n", prland(lp));
853         return 0;
854     }
855     count_land_planes(lp);
856     if (load_unload == LOAD && lp->lnd_nxlight >= lp->lnd_maxlight) {
857         if (noisy)
858             pr("%s doesn't have room for any more extra-light planes\n",
859                prland(lp));
860         return 0;
861     }
862     sprintf(prompt, "Plane(s) to %s %s? ",
863             load_unload == LOAD ? "load onto" : "unload from", prland(lp));
864     if (!snxtitem
865         (&ni, EF_PLANE, p = getstarg(player->argp[3], prompt, buf)))
866         return RET_SYN;
867
868     if (!still_ok_land(sectp, lp))
869         return RET_SYN;
870
871     if (p && *p)
872         noisy &= isdigit(*p);
873
874     if (sectp->sct_own != player->cnum && load_unload == LOAD) {
875         pr("Sector %s is not yours.\n",
876            xyas(lp->lnd_x, lp->lnd_y, player->cnum));
877         return 0;
878     }
879
880     while (nxtitem(&ni, (s_char *)&pln)) {
881         if (pln.pln_own != player->cnum)
882             continue;
883
884         if (!(plchr[(int)pln.pln_type].pl_flags & P_E)) {
885             if (noisy)
886                 pr("You can only load xlight planes onto units.\n");
887             continue;
888         }
889
890         if (load_unload == LOAD && pln.pln_ship > -1) {
891             if (noisy)
892                 pr("%s is already on ship #%d!\n",
893                    prplane(&pln), pln.pln_ship);
894             continue;
895         }
896         if (load_unload == LOAD && pln.pln_land > -1) {
897             if (noisy)
898                 pr("%s is already on unit #%d!\n",
899                    prplane(&pln), pln.pln_land);
900             continue;
901         }
902         /* Plane sanity done */
903         /* Find the right unit */
904         if (load_unload == UNLOAD) {
905             if (pln.pln_land != lp->lnd_uid)
906                 continue;
907         } else if (lp->lnd_x != pln.pln_x || lp->lnd_y != pln.pln_y)
908             continue;
909
910         /* Fit plane on unit */
911         if (load_unload == LOAD) {
912             if (!put_plane_on_land(&pln, lp)) {
913                 if (noisy)
914                     pr("Can't put plane %d on this unit!\n", pln.pln_uid);
915                 continue;
916             }
917             sprintf(buf, "loaded on %s at %s",
918                     prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
919             gift(lp->lnd_own, player->cnum, (s_char *)&pln, EF_PLANE, buf);
920             makelost(EF_PLANE, pln.pln_own, pln.pln_uid, pln.pln_x,
921                      pln.pln_y);
922             pln.pln_own = lp->lnd_own;
923             makenotlost(EF_PLANE, pln.pln_own, pln.pln_uid, pln.pln_x,
924                         pln.pln_y);
925             putplane(pln.pln_uid, &pln);
926         } else {
927             if (!take_plane_off_land(&pln, lp)) {
928                 pr("Unable to take plane off unit!\n");
929                 logerror("load: plane %d could not be taken off unit %d\n",
930                          pln.pln_uid, lp->lnd_uid);
931                 continue;
932             }
933             sprintf(buf, "unloaded at your sector at %s",
934                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
935             gift(sectp->sct_own, player->cnum, (s_char *)&pln,
936                  EF_PLANE, buf);
937             makelost(EF_PLANE, pln.pln_own, pln.pln_uid, pln.pln_x,
938                      pln.pln_y);
939             pln.pln_own = sectp->sct_own;
940             makenotlost(EF_PLANE, pln.pln_own, pln.pln_uid, pln.pln_x,
941                         pln.pln_y);
942             putplane(pln.pln_uid, &pln);
943         }
944         pr("%s %s %s at %s.\n",
945            prplane(&pln),
946            (load_unload == UNLOAD) ?
947            "unloaded from" : "loaded onto",
948            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
949         loaded = 1;
950     }
951     *nunitsp += loaded;
952     return 0;
953 }
954
955 static int
956 load_comm_land(struct sctstr *sectp, struct lndstr *lp,
957                struct ichrstr *ich, int load_unload, int *nunitsp)
958 {
959     register int item = ich->i_vtype;
960     register int max_amt;
961     register int move_amt;
962     register int land_amt;
963     register int sect_amt;
964     int amount;
965     int upto = 0, abs_max;
966     s_char prompt[512];
967     s_char *p;
968     s_char buf[1024];
969
970     sprintf(prompt, "Number of %s to %s %s at %s? ",
971             ich->i_name,
972             (load_unload == UNLOAD) ?
973             "unload from" : "load onto",
974             prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
975     if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
976         return RET_SYN;
977
978     if (!still_ok_land(sectp, lp))
979         return RET_SYN;
980
981     amount = atoi(p);
982     if (amount < 0) {
983         /* We want to load up to this amount */
984         upto = -(amount);
985         load_unload = LOAD;
986     } else if (!amount)
987         return 0;
988
989     if (sectp->sct_own != player->cnum && load_unload == LOAD) {
990         pr("Sector %s is not yours.\n",
991            xyas(lp->lnd_x, lp->lnd_y, player->cnum));
992         return 0;
993     }
994
995     land_amt = getvar(item, (s_char *)lp, EF_LAND);
996     sect_amt = getvar(item, (s_char *)sectp, EF_SECTOR);
997     if (sectp->sct_oldown != player->cnum && item == V_CIVIL) {
998         pr("%s civilians refuse to %s at %s!\n",
999            load_unload == UNLOAD ? "Your" : "Foreign",
1000            load_unload == UNLOAD ?
1001            "disembark" : "board",
1002            xyas(sectp->sct_x, sectp->sct_y, player->cnum));
1003         return 0;
1004     }
1005     if (load_unload == UNLOAD) {
1006         abs_max = max_amt = min(9999 - sect_amt, land_amt);
1007     } else {
1008         struct lchrstr *vbase;
1009         vbase = &lchr[(int)lp->lnd_type];
1010         abs_max = max_amt = vl_find(item, vbase->l_vtype,
1011                                     vbase->l_vamt, (int)vbase->l_nv);
1012         max_amt = min(sect_amt, max_amt - land_amt);
1013     }
1014     if ((max_amt <= 0) && (upto == 0))
1015         return 0;
1016     if (upto) {
1017         move_amt = upto - land_amt;
1018         if (land_amt + move_amt > abs_max)
1019             move_amt = abs_max - land_amt;
1020         if (move_amt > sect_amt)
1021             move_amt = sect_amt;
1022     } else
1023         move_amt = load_unload * min(amount, max_amt);
1024     if (move_amt == 0)
1025         return 0;
1026     putvar(item, sect_amt - move_amt, (s_char *)sectp, EF_SECTOR);
1027     putvar(item, land_amt + move_amt, (s_char *)lp, EF_LAND);
1028
1029     /* Did we put mils onto this unit? If so, reset the fortification */
1030     if (item == V_MILIT && move_amt > 0)
1031         lp->lnd_harden = 0;
1032     if (load_unload == LOAD) {
1033         pr("%d %s loaded onto %s at %s\n",
1034            move_amt,
1035            ich->i_name,
1036            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
1037         if (lp->lnd_own != player->cnum) {
1038             sprintf(buf, "%s loaded %d %s onto %s at %s\n",
1039                     cname(player->cnum),
1040                     move_amt,
1041                     ich->i_name,
1042                     prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
1043             wu(0, lp->lnd_own, buf);
1044         }
1045     } else {
1046         pr("%d %s unloaded from %s at %s\n",
1047            -move_amt,
1048            ich->i_name,
1049            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
1050         if (sectp->sct_own != player->cnum) {
1051             sprintf(buf, "%s unloaded %d %s from %s at %s\n",
1052                     cname(player->cnum),
1053                     -move_amt,
1054                     ich->i_name,
1055                     prland(lp),
1056                     xyas(lp->lnd_x, lp->lnd_y, sectp->sct_own));
1057             wu(0, sectp->sct_own, buf);
1058         }
1059     }
1060
1061     ++(*nunitsp);
1062     return 0;
1063 }
1064
1065 static int
1066 load_land_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
1067                int load_unload, int *nunitsp)
1068 {
1069     struct nstr_item pni, ni;
1070     struct lndstr land;
1071     struct plnstr plane;
1072     int loaded = 0;
1073     s_char *p;
1074     s_char prompt[512];
1075     s_char buf[1024];
1076
1077     lnd_count_units(lp);
1078
1079     if (load_unload == LOAD && lp->lnd_nland >= lp->lnd_maxland) {
1080         if (noisy) {
1081             if (lp->lnd_nland)
1082                 pr("%s doesn't have room for any more land units!\n",
1083                    prland(lp));
1084             else
1085                 pr("%s cannot carry land units!\n", prland(lp));
1086         }
1087         return 0;
1088     }
1089     sprintf(prompt, "Land unit(s) to %s %s? ",
1090             load_unload == LOAD ? "load onto" : "unload from", prland(lp));
1091     if (!snxtitem
1092         (&ni, EF_LAND, p = getstarg(player->argp[3], prompt, buf)))
1093         return RET_SYN;
1094
1095     if (!still_ok_land(sectp, lp))
1096         return RET_SYN;
1097
1098     if (p && *p)
1099         noisy &= isdigit(*p);
1100
1101     while (nxtitem(&ni, (s_char *)&land)) {
1102
1103         if (land.lnd_own != player->cnum)
1104             continue;
1105
1106         if (load_unload == LOAD) {
1107             if (land.lnd_ship > -1) {
1108                 if (noisy)
1109                     pr("%s is already on ship #%d!\n",
1110                        prland(&land), land.lnd_ship);
1111                 continue;
1112             }
1113             if (land.lnd_land > -1) {
1114                 if (noisy)
1115                     pr("%s is already on land #%d!\n",
1116                        prland(&land), land.lnd_land);
1117                 continue;
1118             }
1119             lnd_count_units(&land);
1120             if (land.lnd_nland > 0) {
1121                 if (noisy)
1122                     pr("%s cannot be loaded since it is carrying units\n",
1123                        prland(&land));
1124                 continue;
1125             }
1126             if (land.lnd_uid == lp->lnd_uid) {
1127                 if (noisy)
1128                     pr("%s can't be loaded onto itself!\n", prland(&land));
1129                 continue;
1130             }
1131             if (lchr[(int)land.lnd_type].l_flags & L_HEAVY) {
1132                 if (noisy)
1133                     pr("%s is too heavy to load.\n", prland(&land));
1134                 continue;
1135             }
1136         }
1137
1138         /* Unit sanity done */
1139         /* Find the right ship */
1140         if (load_unload == UNLOAD) {
1141             if (land.lnd_land != lp->lnd_uid)
1142                 continue;
1143             if (land.lnd_ship > -1)
1144                 continue;
1145         } else if (lp->lnd_x != land.lnd_x || lp->lnd_y != land.lnd_y)
1146             continue;
1147
1148         /* Fit unit on ship */
1149         if (load_unload == LOAD) {
1150             lnd_count_units(lp);
1151             if (lp->lnd_nland >= lp->lnd_maxland) {
1152                 if (noisy) {
1153                     if (lp->lnd_nland)
1154                         pr("%s doesn't have room for any more land units!\n", prland(lp));
1155                     else
1156                         pr("%s cannot carry land units!\n", prland(lp));
1157                 }
1158                 break;
1159             }
1160             sprintf(buf, "loaded on your %s at %s",
1161                     prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
1162             gift(lp->lnd_own, player->cnum, (s_char *)&land, EF_LAND, buf);
1163             makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
1164                      land.lnd_y);
1165             land.lnd_own = lp->lnd_own;
1166             makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
1167                         land.lnd_y);
1168             land.lnd_land = lp->lnd_uid;
1169             land.lnd_harden = 0;
1170             land.lnd_mission = 0;
1171             resupply_all(&land);
1172             lp->lnd_nland++;
1173             putland(land.lnd_uid, &land);
1174             if (!has_supply(&land))
1175                 pr("WARNING: %s is out of supply!\n", prland(&land));
1176             putland(lp->lnd_uid, lp);
1177             snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
1178             while (nxtitem(&pni, (s_char *)&plane)) {
1179                 if (plane.pln_flags & PLN_LAUNCHED)
1180                     continue;
1181                 if (plane.pln_land != land.lnd_uid)
1182                     continue;
1183                 sprintf(buf, "loaded on %s", prland(lp));
1184                 gift(lp->lnd_own, player->cnum, (s_char *)&plane,
1185                      EF_PLANE, buf);
1186                 makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
1187                          plane.pln_x, plane.pln_y);
1188                 plane.pln_own = lp->lnd_own;
1189                 makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
1190                             plane.pln_x, plane.pln_y);
1191                 plane.pln_mission = 0;
1192                 putplane(plane.pln_uid, &plane);
1193             }
1194         } else {
1195             sprintf(buf, "unloaded in your %s at %s",
1196                     dchr[sectp->sct_type].d_name,
1197                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
1198             gift(sectp->sct_own, player->cnum, (s_char *)&land,
1199                  EF_LAND, buf);
1200             makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
1201                      land.lnd_y);
1202             land.lnd_own = sectp->sct_own;
1203             makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
1204                         land.lnd_y);
1205             land.lnd_land = (-1);
1206             lp->lnd_nland--;
1207             putland(land.lnd_uid, &land);
1208             putland(lp->lnd_uid, lp);
1209             snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
1210             while (nxtitem(&pni, (s_char *)&plane)) {
1211                 if (plane.pln_flags & PLN_LAUNCHED)
1212                     continue;
1213                 if (plane.pln_land != land.lnd_uid)
1214                     continue;
1215                 sprintf(buf, "unloaded at %s",
1216                         xyas(plane.pln_x, plane.pln_y, sectp->sct_own));
1217                 gift(sectp->sct_own, player->cnum, (s_char *)&plane,
1218                      EF_PLANE, buf);
1219                 makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
1220                          plane.pln_x, plane.pln_y);
1221                 plane.pln_own = sectp->sct_own;
1222                 makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
1223                             plane.pln_x, plane.pln_y);
1224                 plane.pln_mission = 0;
1225                 putplane(plane.pln_uid, &plane);
1226             }
1227         }
1228         pr("%s %s %s at %s.\n",
1229            prland(&land),
1230            (load_unload == UNLOAD) ?
1231            "unloaded from" : "loaded onto",
1232            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
1233         loaded = 1;
1234     }
1235     *nunitsp += loaded;
1236     return 0;
1237 }