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