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