]> git.pond.sub.org Git - empserver/blob - src/lib/commands/load.c
(load_comm_ship, load_comm_land): Partial rewrite for clarity. Fix
[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 #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 = whatitem(p, (s_char *)0)))
98         type = EF_SECTOR;
99     else {
100         pr("Bad commodity.\n");
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 = whatitem(p, (s_char *)0)))
238         type = EF_SECTOR;
239     else {
240         pr("Bad commodity.\n");
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     }
343
344     sprintf(line, "%s %s %s\n", cname(giver), p, mesg);
345     wu(0, givee, line);
346 }
347
348 static int
349 still_ok_ship(struct sctstr *sectp, struct shpstr *shipp)
350 {
351     if (!check_sect_ok(sectp))
352         return 0;
353     if (!check_ship_ok(shipp))
354         return 0;
355     return 1;
356 }
357
358 static int
359 still_ok_land(struct sctstr *sectp, struct lndstr *landp)
360 {
361     if (!check_sect_ok(sectp))
362         return 0;
363     if (!check_land_ok(landp))
364         return 0;
365     return 1;
366 }
367
368 static int
369 load_plane_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
370                 int load_unload, int *nshipsp)
371 {
372     struct nstr_item ni;
373     struct plnstr pln;
374     int loaded = 0;
375     s_char buf[1024];
376     s_char *p;
377     s_char prompt[512];
378     struct mchrstr *mcp = mchr + sp->shp_type;
379
380     if (!(mcp->m_flags & (M_CHOPPER | M_XLIGHT | M_FLY | M_MSL))) {
381         if (noisy)
382             pr("%s cannot carry planes\n", prship(sp));
383         return 0;
384     }
385     count_planes(sp);
386     if (load_unload == LOAD &&
387         sp->shp_nchoppers >= mcp->m_nchoppers &&
388         sp->shp_nxlight >= mcp->m_nxlight &&
389         sp->shp_nplane >= mcp->m_nplanes) {
390         if (noisy)
391             pr("%s doesn't have room for any more planes\n", prship(sp));
392         return 0;
393     }
394     sprintf(prompt, "Plane(s) to %s %s? ",
395             load_unload == LOAD ? "load onto" : "unload from", prship(sp));
396     if (!snxtitem(&ni, EF_PLANE,
397                   p = getstarg(player->argp[3], prompt, buf)))
398         return RET_SYN;
399
400     if (!still_ok_ship(sectp, sp))
401         return RET_SYN;
402
403     if (p && *p)
404         noisy &= isdigit(*p);
405
406     while (nxtitem(&ni, (s_char *)&pln)) {
407         if (pln.pln_own != player->cnum)
408             continue;
409         if (!(plchr[(int)pln.pln_type].pl_flags & P_L)
410             && !(plchr[(int)pln.pln_type].pl_flags & P_E)
411             && !(plchr[(int)pln.pln_type].pl_flags & P_K)
412             && !(plchr[(int)pln.pln_type].pl_flags & P_M)
413             ) {
414             if (noisy)
415                 pr("You can only load light planes, helos, xtra-light, or missiles onto ships.\n");
416             continue;
417         }
418         if (load_unload == LOAD && pln.pln_ship > -1) {
419             if (noisy)
420                 pr("%s is already on ship #%d!\n",
421                    prplane(&pln), pln.pln_ship);
422             continue;
423         }
424         if (load_unload == LOAD && pln.pln_land > -1) {
425             if (noisy)
426                 pr("%s is already on land unit #%d!\n",
427                    prplane(&pln), pln.pln_land);
428             continue;
429         }
430
431         /* Plane sanity done */
432         /* Find the right ship */
433         if (load_unload == UNLOAD) {
434             if (pln.pln_ship != sp->shp_uid)
435                 continue;
436         } else if (sp->shp_x != pln.pln_x || sp->shp_y != pln.pln_y)
437             continue;
438
439         /* ship to (plane or missle) sanity */
440         if (!can_be_on_ship(pln.pln_uid, sp->shp_uid)) {
441             if (plchr[(int)pln.pln_type].pl_flags & P_L) {
442                 strcpy(buf, "planes");
443             } else if (plchr[(int)pln.pln_type].pl_flags & P_K) {
444                 strcpy(buf, "choppers");
445             } else if (plchr[(int)pln.pln_type].pl_flags & P_M) {
446                 strcpy(buf, "missiles");
447             } else if (plchr[(int)pln.pln_type].pl_flags & P_E) {
448                 strcpy(buf, "extra light planes");
449             }                   /* else impossible */
450             if (noisy)
451                 pr("%s cannot carry %s.\n", prship(sp), buf);
452             continue;
453         }
454         /* Fit plane on ship */
455         if (load_unload == LOAD) {
456             if (!put_plane_on_ship(&pln, sp)) {
457                 if (noisy)
458                     pr("Can't put plane %d on this ship!\n", pln.pln_uid);
459                 continue;
460             }
461             sprintf(buf, "loaded on your %s at %s",
462                     prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
463             gift(sp->shp_own, player->cnum, (s_char *)&pln, EF_PLANE, buf);
464             makelost(EF_PLANE, pln.pln_own, pln.pln_uid, pln.pln_x,
465                      pln.pln_y);
466             pln.pln_own = sp->shp_own;
467             makenotlost(EF_PLANE, pln.pln_own, pln.pln_uid, pln.pln_x,
468                         pln.pln_y);
469             pln.pln_mission = 0;
470             putplane(pln.pln_uid, &pln);
471         } else {
472             if (!take_plane_off_ship(&pln, sp)) {
473                 pr("Unable to take plane off ship!\n");
474                 logerror("load: plane %d could not be taken off ship %d\n",
475                          pln.pln_uid, sp->shp_uid);
476                 continue;
477             }
478             sprintf(buf, "unloaded in your %s at %s",
479                     dchr[sectp->sct_type].d_name,
480                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
481             gift(sectp->sct_own, player->cnum, (s_char *)&pln,
482                  EF_PLANE, buf);
483             makelost(EF_PLANE, pln.pln_own, pln.pln_uid, pln.pln_x,
484                      pln.pln_y);
485             pln.pln_own = sectp->sct_own;
486             makenotlost(EF_PLANE, pln.pln_own, pln.pln_uid, pln.pln_x,
487                         pln.pln_y);
488             putplane(pln.pln_uid, &pln);
489         }
490         pr("%s %s %s at %s.\n",
491            prplane(&pln),
492            (load_unload == UNLOAD) ?
493            "unloaded from" : "loaded onto",
494            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
495         loaded = 1;
496     }
497     *nshipsp += loaded;
498     return 0;
499 }
500
501 static int
502 load_land_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
503                int load_unload, int *nshipsp)
504 {
505     struct nstr_item pni, ni;
506     struct lndstr land;
507     struct plnstr plane;
508     int loaded = 0;
509     s_char *p;
510     s_char prompt[512];
511     s_char buf[1024];
512     int load_spy = 0;
513
514     count_units(sp);
515     if (load_unload == LOAD) {
516         if (opt_LANDSPIES) {
517             if ((mchr[(int)sp->shp_type].m_flags & M_SUB) &&
518                 (mchr[(int)sp->shp_type].m_nland == 0)) {
519                 if (sp->shp_nland >= 2) {
520                     pr("Non-land unit carrying subs can only carry up to two spy units.\n");
521                     return 0;
522                 }
523                 /* Eh, let 'em load a spy only */
524                 load_spy = 1;
525             }
526         }
527         if ((!load_spy) &&
528             (sp->shp_nland >= mchr[(int)sp->shp_type].m_nland)) {
529             if (noisy) {
530                 if (mchr[(int)sp->shp_type].m_nland)
531                     pr("%s doesn't have room for any more land units!\n",
532                        prship(sp));
533                 else
534                     pr("%s cannot carry land units!\n", prship(sp));
535             }
536             return 0;
537         }
538     }
539     sprintf(prompt, "Land unit(s) to %s %s? ",
540             load_unload == LOAD ? "load onto" : "unload from", prship(sp));
541     if (!snxtitem(&ni, EF_LAND,
542                   p = getstarg(player->argp[3], prompt, buf)))
543         return RET_SYN;
544
545     if (!still_ok_ship(sectp, sp))
546         return RET_SYN;
547
548     if (p && *p)
549         noisy &= isdigit(*p);
550
551     while (nxtitem(&ni, (s_char *)&land)) {
552         if (land.lnd_own != player->cnum)
553             continue;
554
555         if (load_unload == LOAD) {
556             if (land.lnd_ship > -1) {
557                 if (noisy)
558                     pr("%s is already on ship #%d!\n",
559                        prland(&land), land.lnd_ship);
560                 continue;
561             }
562             if (land.lnd_land > -1) {
563                 if (noisy)
564                     pr("%s is already on land #%d!\n",
565                        prland(&land), land.lnd_land);
566                 continue;
567             }
568             lnd_count_units(&land);
569             if (land.lnd_nland > 0) {
570                 if (noisy)
571                     pr("%s cannot be loaded since it is carrying units\n",
572                        prland(&land));
573                 continue;
574             }
575             if (lchr[(int)land.lnd_type].l_flags & L_HEAVY) {
576                 if (noisy)
577                     pr("%s is too heavy to load.\n", prland(&land));
578                 continue;
579             }
580             if (load_spy && !(lchr[(int)land.lnd_type].l_flags & L_SPY)) {
581                 if (noisy)
582                     pr("Subs can only carry spy units.\n");
583                 continue;
584             }
585         }
586
587         /* Unit sanity done */
588         /* Find the right ship */
589         if (load_unload == UNLOAD) {
590             if (land.lnd_ship != sp->shp_uid)
591                 continue;
592             if (land.lnd_land > -1)
593                 continue;
594         } else if (sp->shp_x != land.lnd_x || sp->shp_y != land.lnd_y)
595             continue;
596
597         if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
598             (!((mchr[(int)sp->shp_type].m_flags & M_SUPPLY) &&
599                (!(mchr[(int)sp->shp_type].m_flags & M_SUB))))) {
600             if (noisy) {
601                 pr("You can only load light units onto ships,\n");
602                 pr("unless the ship is a non-sub supply ship\n");
603                 pr("%s not loaded\n", prland(&land));
604             }
605             continue;
606         }
607         /* Fit unit on ship */
608         if (load_unload == LOAD) {
609             count_units(sp);
610             /* We have to check again, since it may have changed */
611             if (opt_LANDSPIES) {
612                 if ((mchr[(int)sp->shp_type].m_flags & M_SUB) &&
613                     (mchr[(int)sp->shp_type].m_nland == 0)) {
614                     if (sp->shp_nland >= 2) {
615                         pr("Non-land unit carrying subs can only carry up to two spy units.\n");
616                         return 0;
617                     }
618                     /* Eh, let 'em load a spy only */
619                     load_spy = 1;
620                 }
621             }
622             if ((!load_spy) &&
623                 (sp->shp_nland >= mchr[(int)sp->shp_type].m_nland)) {
624                 if (noisy) {
625                     if (mchr[(int)sp->shp_type].m_nland)
626                         pr("%s doesn't have room for any more land units!\n", prship(sp));
627                     else
628                         pr("%s cannot carry land units!\n", prship(sp));
629                 }
630                 return 0;
631             }
632 #if 0
633             if (sp->shp_nland >= mchr[(int)sp->shp_type].m_nland) {
634                 if (noisy)
635                     if (mchr[(int)sp->shp_type].m_nland)
636                         pr("%s doesn't have room for any more land units!\n", prship(sp));
637                     else
638                         pr("%s cannot carry land units!\n", prship(sp));
639                 break;
640             }
641 #endif
642             sprintf(buf, "loaded on your %s at %s",
643                     prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
644             gift(sp->shp_own, player->cnum, (s_char *)&land, EF_LAND, buf);
645             makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
646                      land.lnd_y);
647             land.lnd_own = sp->shp_own;
648             makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
649                         land.lnd_y);
650             land.lnd_ship = sp->shp_uid;
651             land.lnd_harden = 0;
652             land.lnd_mission = 0;
653             resupply_all(&land);
654             sp->shp_nland++;
655             putland(land.lnd_uid, &land);
656             if (!has_supply(&land))
657                 pr("WARNING: %s is out of supply!\n", prland(&land));
658             putship(sp->shp_uid, sp);
659             snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
660             while (nxtitem(&pni, (s_char *)&plane)) {
661                 if (plane.pln_flags & PLN_LAUNCHED)
662                     continue;
663                 if (plane.pln_land != land.lnd_uid)
664                     continue;
665                 sprintf(buf, "loaded on %s", prship(sp));
666                 gift(sp->shp_own, player->cnum, (s_char *)&plane,
667                      EF_PLANE, buf);
668                 makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
669                          plane.pln_x, plane.pln_y);
670                 plane.pln_own = sp->shp_own;
671                 makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
672                             plane.pln_x, plane.pln_y);
673                 plane.pln_mission = 0;
674                 putplane(plane.pln_uid, &plane);
675             }
676         } else {
677             sprintf(buf, "unloaded in your %s at %s",
678                     dchr[sectp->sct_type].d_name,
679                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
680
681             /* Spies are unloaded quietly, others aren't */
682             if (!(lchr[(int)land.lnd_type].l_flags & L_SPY)) {
683                 gift(sectp->sct_own, player->cnum, (s_char *)&land,
684                      EF_LAND, buf);
685                 makelost(EF_LAND, land.lnd_own, land.lnd_uid,
686                          land.lnd_x, land.lnd_y);
687                 land.lnd_own = sectp->sct_own;
688                 makenotlost(EF_LAND, land.lnd_own, land.lnd_uid,
689                             land.lnd_x, land.lnd_y);
690             }
691
692             land.lnd_ship = (-1);
693             sp->shp_nland--;
694             putland(land.lnd_uid, &land);
695             putship(sp->shp_uid, sp);
696
697             /* Spies are unloaded quietly, others aren't, and
698                in the off chance they can carry a plane (missile?)
699                they are quietly unloaded too. */
700             if (!(lchr[(int)land.lnd_type].l_flags & L_SPY)) {
701                 snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
702                 while (nxtitem(&pni, (s_char *)&plane)) {
703                     if (plane.pln_flags & PLN_LAUNCHED)
704                         continue;
705                     if (plane.pln_land != land.lnd_uid)
706                         continue;
707                     sprintf(buf, "unloaded at %s",
708                             xyas(plane.pln_x, plane.pln_y,
709                                  sectp->sct_own));
710                     gift(sectp->sct_own, player->cnum, (s_char *)&plane,
711                          EF_PLANE, buf);
712                     makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
713                              plane.pln_x, plane.pln_y);
714                     plane.pln_own = sectp->sct_own;
715                     makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
716                                 plane.pln_x, plane.pln_y);
717                     plane.pln_mission = 0;
718                     putplane(plane.pln_uid, &plane);
719                 }
720             }
721         }
722         pr("%s %s %s at %s.\n",
723            prland(&land),
724            (load_unload == UNLOAD) ?
725            "unloaded from" : "loaded onto",
726            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
727         loaded = 1;
728     }
729     *nshipsp += loaded;
730     return 0;
731 }
732
733 static int
734 load_comm_ship(struct sctstr *sectp, struct shpstr *sp,
735                struct ichrstr *ich, int load_unload, int *nshipsp)
736 {
737     register int item = ich->i_vtype;
738     struct mchrstr *mcp = &mchr[(int)sp->shp_type];
739     int ship_amt, ship_max, sect_amt, move_amt;
740     int amount;
741     s_char prompt[512];
742     s_char *p;
743     s_char buf[1024];
744
745     sprintf(prompt, "Number of %s to %s %s at %s? ",
746             ich->i_name,
747             (load_unload == UNLOAD) ?
748             "unload from" : "load onto",
749             prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
750     if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
751         return RET_SYN;
752
753     if (!still_ok_ship(sectp, sp))
754         return RET_SYN;
755
756     ship_amt = sp->shp_item[item];
757     ship_max = vl_find(item, mcp->m_vtype, mcp->m_vamt, mcp->m_nv);
758     sect_amt = sectp->sct_item[item];
759     amount = atoi(p);
760     if (amount < 0)
761         move_amt = -amount - ship_amt;
762     else
763         move_amt = load_unload == LOAD ? amount : -amount;
764     if (move_amt > ship_max - ship_amt)
765         move_amt = ship_max - ship_amt;
766     if (move_amt < -ship_amt)
767         move_amt = -ship_amt;
768     if (move_amt > sect_amt)
769         move_amt = sect_amt;
770     if (move_amt < sect_amt - 9999)
771         move_amt = sect_amt - 9999;
772     if (!move_amt)
773         return RET_OK;
774     if (sectp->sct_oldown != player->cnum && item == V_CIVIL) {
775         pr("%s civilians refuse to %s at %s!\n",
776            move_amt < 0 ? "Your" : "Foreign",
777            move_amt < 0 ? "disembark" : "board",
778            xyas(sectp->sct_x, sectp->sct_y, player->cnum));
779         return RET_FAIL;
780     }
781
782     if (!want_to_abandon(sectp, item, move_amt, 0))
783         return RET_FAIL;
784     if (!still_ok_ship(sectp, sp))
785         return RET_SYN;
786     sectp->sct_item[item] = sect_amt - move_amt;
787     sp->shp_item[item] = ship_amt + move_amt;
788     if (move_amt >= 0) {
789         pr("%d %s loaded onto %s at %s\n",
790            move_amt,
791            ich->i_name,
792            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
793         if (sp->shp_own != player->cnum) {
794             sprintf(buf, "%s loaded %d %s onto %s at %s\n",
795                     cname(player->cnum),
796                     move_amt,
797                     ich->i_name,
798                     prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
799             wu(0, sp->shp_own, buf);
800         }
801     } else {
802         pr("%d %s unloaded from %s at %s\n",
803            -move_amt,
804            ich->i_name,
805            prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
806         if (sectp->sct_own != player->cnum) {
807             sprintf(buf, "%s unloaded %d %s from %s at %s\n",
808                     cname(player->cnum),
809                     -move_amt,
810                     ich->i_name,
811                     prship(sp),
812                     xyas(sp->shp_x, sp->shp_y, sectp->sct_own));
813             wu(0, sectp->sct_own, buf);
814         }
815     }
816     ++(*nshipsp);
817     return 0;
818 }
819
820 static int
821 load_plane_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
822                 int load_unload, int *nunitsp)
823 {
824     struct nstr_item ni;
825     struct plnstr pln;
826     int loaded = 0;
827     s_char *p;
828     s_char prompt[512];
829     s_char buf[1024];
830
831     if (!(lchr[(int)lp->lnd_type].l_flags & L_XLIGHT)) {
832         if (noisy)
833             pr("%s cannot carry extra-light planes.\n", prland(lp));
834         return 0;
835     }
836     count_land_planes(lp);
837     if (load_unload == LOAD && lp->lnd_nxlight >= lp->lnd_maxlight) {
838         if (noisy)
839             pr("%s doesn't have room for any more extra-light planes\n",
840                prland(lp));
841         return 0;
842     }
843     sprintf(prompt, "Plane(s) to %s %s? ",
844             load_unload == LOAD ? "load onto" : "unload from", prland(lp));
845     if (!snxtitem(&ni, EF_PLANE,
846                   p = getstarg(player->argp[3], prompt, buf)))
847         return RET_SYN;
848
849     if (!still_ok_land(sectp, lp))
850         return RET_SYN;
851
852     if (p && *p)
853         noisy &= isdigit(*p);
854
855     if (sectp->sct_own != player->cnum && load_unload == LOAD) {
856         pr("Sector %s is not yours.\n",
857            xyas(lp->lnd_x, lp->lnd_y, player->cnum));
858         return 0;
859     }
860
861     while (nxtitem(&ni, (s_char *)&pln)) {
862         if (pln.pln_own != player->cnum)
863             continue;
864
865         if (!(plchr[(int)pln.pln_type].pl_flags & P_E)) {
866             if (noisy)
867                 pr("You can only load xlight planes onto units.\n");
868             continue;
869         }
870
871         if (load_unload == LOAD && pln.pln_ship > -1) {
872             if (noisy)
873                 pr("%s is already on ship #%d!\n",
874                    prplane(&pln), pln.pln_ship);
875             continue;
876         }
877         if (load_unload == LOAD && pln.pln_land > -1) {
878             if (noisy)
879                 pr("%s is already on unit #%d!\n",
880                    prplane(&pln), pln.pln_land);
881             continue;
882         }
883         /* Plane sanity done */
884         /* Find the right unit */
885         if (load_unload == UNLOAD) {
886             if (pln.pln_land != lp->lnd_uid)
887                 continue;
888         } else if (lp->lnd_x != pln.pln_x || lp->lnd_y != pln.pln_y)
889             continue;
890
891         /* Fit plane on unit */
892         if (load_unload == LOAD) {
893             if (!put_plane_on_land(&pln, lp)) {
894                 if (noisy)
895                     pr("Can't put plane %d on this unit!\n", pln.pln_uid);
896                 continue;
897             }
898             sprintf(buf, "loaded on %s at %s",
899                     prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
900             gift(lp->lnd_own, player->cnum, (s_char *)&pln, EF_PLANE, buf);
901             makelost(EF_PLANE, pln.pln_own, pln.pln_uid, pln.pln_x,
902                      pln.pln_y);
903             pln.pln_own = lp->lnd_own;
904             makenotlost(EF_PLANE, pln.pln_own, pln.pln_uid, pln.pln_x,
905                         pln.pln_y);
906             putplane(pln.pln_uid, &pln);
907         } else {
908             if (!take_plane_off_land(&pln, lp)) {
909                 pr("Unable to take plane off unit!\n");
910                 logerror("load: plane %d could not be taken off unit %d\n",
911                          pln.pln_uid, lp->lnd_uid);
912                 continue;
913             }
914             sprintf(buf, "unloaded at your sector at %s",
915                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
916             gift(sectp->sct_own, player->cnum, (s_char *)&pln,
917                  EF_PLANE, buf);
918             makelost(EF_PLANE, pln.pln_own, pln.pln_uid, pln.pln_x,
919                      pln.pln_y);
920             pln.pln_own = sectp->sct_own;
921             makenotlost(EF_PLANE, pln.pln_own, pln.pln_uid, pln.pln_x,
922                         pln.pln_y);
923             putplane(pln.pln_uid, &pln);
924         }
925         pr("%s %s %s at %s.\n",
926            prplane(&pln),
927            (load_unload == UNLOAD) ?
928            "unloaded from" : "loaded onto",
929            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
930         loaded = 1;
931     }
932     *nunitsp += loaded;
933     return 0;
934 }
935
936 static int
937 load_comm_land(struct sctstr *sectp, struct lndstr *lp,
938                struct ichrstr *ich, int load_unload, int *nunitsp)
939 {
940     register int item = ich->i_vtype;
941     struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
942     int land_amt, land_max, sect_amt, move_amt;
943     int amount;
944     s_char prompt[512];
945     s_char *p;
946     s_char buf[1024];
947
948     sprintf(prompt, "Number of %s to %s %s at %s? ",
949             ich->i_name,
950             (load_unload == UNLOAD) ?
951             "unload from" : "load onto",
952             prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
953     if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
954         return RET_SYN;
955
956     if (!still_ok_land(sectp, lp))
957         return RET_SYN;
958
959     land_amt = lp->lnd_item[item];
960     land_max = vl_find(item, lcp->l_vtype, lcp->l_vamt, lcp->l_nv);
961     sect_amt = sectp->sct_item[item];
962     amount = atoi(p);
963     if (amount < 0)
964         move_amt = -amount - land_amt;
965     else
966         move_amt = load_unload == LOAD ? amount : -amount;
967     if (move_amt > land_max - land_amt)
968         move_amt = land_max - land_amt;
969     if (move_amt < -land_amt)
970         move_amt = -land_amt;
971     if (move_amt > sect_amt)
972         move_amt = sect_amt;
973     if (move_amt < sect_amt - 9999)
974         move_amt = sect_amt - 9999;
975     if (!move_amt)
976         return RET_OK;
977     if (sectp->sct_own != player->cnum && move_amt > 0) {
978         pr("Sector %s is not yours.\n",
979            xyas(lp->lnd_x, lp->lnd_y, player->cnum));
980         return RET_FAIL;
981     }
982     if (sectp->sct_oldown != player->cnum && item == V_CIVIL) {
983         pr("%s civilians refuse to %s at %s!\n",
984            move_amt < 0 ? "Your" : "Foreign",
985            move_amt < 0 ? "disembark" : "board",
986            xyas(sectp->sct_x, sectp->sct_y, player->cnum));
987         return RET_FAIL;
988     }
989     sectp->sct_item[item] = sect_amt - move_amt;
990     lp->lnd_item[item] = land_amt + move_amt;
991
992     /* Did we put mils onto this unit? If so, reset the fortification */
993     if (item == V_MILIT && move_amt > 0)
994         lp->lnd_harden = 0;
995     if (move_amt >= 0) {
996         pr("%d %s loaded onto %s at %s\n",
997            move_amt,
998            ich->i_name,
999            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
1000         if (lp->lnd_own != player->cnum) {
1001             sprintf(buf, "%s loaded %d %s onto %s at %s\n",
1002                     cname(player->cnum),
1003                     move_amt,
1004                     ich->i_name,
1005                     prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
1006             wu(0, lp->lnd_own, buf);
1007         }
1008     } else {
1009         pr("%d %s unloaded from %s at %s\n",
1010            -move_amt,
1011            ich->i_name,
1012            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
1013         if (sectp->sct_own != player->cnum) {
1014             sprintf(buf, "%s unloaded %d %s from %s at %s\n",
1015                     cname(player->cnum),
1016                     -move_amt,
1017                     ich->i_name,
1018                     prland(lp),
1019                     xyas(lp->lnd_x, lp->lnd_y, sectp->sct_own));
1020             wu(0, sectp->sct_own, buf);
1021         }
1022     }
1023     ++(*nunitsp);
1024     return 0;
1025 }
1026
1027 static int
1028 load_land_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
1029                int load_unload, int *nunitsp)
1030 {
1031     struct nstr_item pni, ni;
1032     struct lndstr land;
1033     struct plnstr plane;
1034     int loaded = 0;
1035     s_char *p;
1036     s_char prompt[512];
1037     s_char buf[1024];
1038
1039     lnd_count_units(lp);
1040
1041     if (load_unload == LOAD && lp->lnd_nland >= lp->lnd_maxland) {
1042         if (noisy) {
1043             if (lp->lnd_nland)
1044                 pr("%s doesn't have room for any more land units!\n",
1045                    prland(lp));
1046             else
1047                 pr("%s cannot carry land units!\n", prland(lp));
1048         }
1049         return 0;
1050     }
1051     sprintf(prompt, "Land unit(s) to %s %s? ",
1052             load_unload == LOAD ? "load onto" : "unload from", prland(lp));
1053     if (!snxtitem(&ni, EF_LAND,
1054                   p = getstarg(player->argp[3], prompt, buf)))
1055         return RET_SYN;
1056
1057     if (!still_ok_land(sectp, lp))
1058         return RET_SYN;
1059
1060     if (p && *p)
1061         noisy &= isdigit(*p);
1062
1063     while (nxtitem(&ni, (s_char *)&land)) {
1064
1065         if (land.lnd_own != player->cnum)
1066             continue;
1067
1068         if (load_unload == LOAD) {
1069             if (land.lnd_ship > -1) {
1070                 if (noisy)
1071                     pr("%s is already on ship #%d!\n",
1072                        prland(&land), land.lnd_ship);
1073                 continue;
1074             }
1075             if (land.lnd_land > -1) {
1076                 if (noisy)
1077                     pr("%s is already on land #%d!\n",
1078                        prland(&land), land.lnd_land);
1079                 continue;
1080             }
1081             lnd_count_units(&land);
1082             if (land.lnd_nland > 0) {
1083                 if (noisy)
1084                     pr("%s cannot be loaded since it is carrying units\n",
1085                        prland(&land));
1086                 continue;
1087             }
1088             if (land.lnd_uid == lp->lnd_uid) {
1089                 if (noisy)
1090                     pr("%s can't be loaded onto itself!\n", prland(&land));
1091                 continue;
1092             }
1093             if (lchr[(int)land.lnd_type].l_flags & L_HEAVY) {
1094                 if (noisy)
1095                     pr("%s is too heavy to load.\n", prland(&land));
1096                 continue;
1097             }
1098         }
1099
1100         /* Unit sanity done */
1101         /* Find the right ship */
1102         if (load_unload == UNLOAD) {
1103             if (land.lnd_land != lp->lnd_uid)
1104                 continue;
1105             if (land.lnd_ship > -1)
1106                 continue;
1107         } else if (lp->lnd_x != land.lnd_x || lp->lnd_y != land.lnd_y)
1108             continue;
1109
1110         /* Fit unit on ship */
1111         if (load_unload == LOAD) {
1112             lnd_count_units(lp);
1113             if (lp->lnd_nland >= lp->lnd_maxland) {
1114                 if (noisy) {
1115                     if (lp->lnd_nland)
1116                         pr("%s doesn't have room for any more land units!\n", prland(lp));
1117                     else
1118                         pr("%s cannot carry land units!\n", prland(lp));
1119                 }
1120                 break;
1121             }
1122             sprintf(buf, "loaded on your %s at %s",
1123                     prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
1124             gift(lp->lnd_own, player->cnum, (s_char *)&land, EF_LAND, buf);
1125             makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
1126                      land.lnd_y);
1127             land.lnd_own = lp->lnd_own;
1128             makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
1129                         land.lnd_y);
1130             land.lnd_land = lp->lnd_uid;
1131             land.lnd_harden = 0;
1132             land.lnd_mission = 0;
1133             resupply_all(&land);
1134             lp->lnd_nland++;
1135             putland(land.lnd_uid, &land);
1136             if (!has_supply(&land))
1137                 pr("WARNING: %s is out of supply!\n", prland(&land));
1138             putland(lp->lnd_uid, lp);
1139             snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
1140             while (nxtitem(&pni, (s_char *)&plane)) {
1141                 if (plane.pln_flags & PLN_LAUNCHED)
1142                     continue;
1143                 if (plane.pln_land != land.lnd_uid)
1144                     continue;
1145                 sprintf(buf, "loaded on %s", prland(lp));
1146                 gift(lp->lnd_own, player->cnum, (s_char *)&plane,
1147                      EF_PLANE, buf);
1148                 makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
1149                          plane.pln_x, plane.pln_y);
1150                 plane.pln_own = lp->lnd_own;
1151                 makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
1152                             plane.pln_x, plane.pln_y);
1153                 plane.pln_mission = 0;
1154                 putplane(plane.pln_uid, &plane);
1155             }
1156         } else {
1157             sprintf(buf, "unloaded in your %s at %s",
1158                     dchr[sectp->sct_type].d_name,
1159                     xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
1160             gift(sectp->sct_own, player->cnum, (s_char *)&land,
1161                  EF_LAND, buf);
1162             makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
1163                      land.lnd_y);
1164             land.lnd_own = sectp->sct_own;
1165             makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
1166                         land.lnd_y);
1167             land.lnd_land = (-1);
1168             lp->lnd_nland--;
1169             putland(land.lnd_uid, &land);
1170             putland(lp->lnd_uid, lp);
1171             snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
1172             while (nxtitem(&pni, (s_char *)&plane)) {
1173                 if (plane.pln_flags & PLN_LAUNCHED)
1174                     continue;
1175                 if (plane.pln_land != land.lnd_uid)
1176                     continue;
1177                 sprintf(buf, "unloaded at %s",
1178                         xyas(plane.pln_x, plane.pln_y, sectp->sct_own));
1179                 gift(sectp->sct_own, player->cnum, (s_char *)&plane,
1180                      EF_PLANE, buf);
1181                 makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
1182                          plane.pln_x, plane.pln_y);
1183                 plane.pln_own = sectp->sct_own;
1184                 makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
1185                             plane.pln_x, plane.pln_y);
1186                 plane.pln_mission = 0;
1187                 putplane(plane.pln_uid, &plane);
1188             }
1189         }
1190         pr("%s %s %s at %s.\n",
1191            prland(&land),
1192            (load_unload == UNLOAD) ?
1193            "unloaded from" : "loaded onto",
1194            prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
1195         loaded = 1;
1196     }
1197     *nunitsp += loaded;
1198     return 0;
1199 }