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