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