]> git.pond.sub.org Git - empserver/blob - src/lib/subs/plnsub.c
5e7e8023b1e272a33b1d881e87998557a47372e5
[empserver] / src / lib / subs / plnsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, 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  *  plnsub.c: Plane subroutine stuff
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Ken Stevens, 1995
33  *     Steve McClure, 1998-2000
34  *     Markus Armbruster, 2004-2009
35  */
36
37 #include <config.h>
38
39 #include "file.h"
40 #include "item.h"
41 #include "land.h"
42 #include "lost.h"
43 #include "map.h"
44 #include "misc.h"
45 #include "nat.h"
46 #include "nsc.h"
47 #include "nuke.h"
48 #include "optlist.h"
49 #include "path.h"
50 #include "plane.h"
51 #include "player.h"
52 #include "prototypes.h"
53 #include "sect.h"
54 #include "ship.h"
55 #include "xy.h"
56
57 static int fit_plane_on_ship(struct plnstr *, struct shpstr *);
58
59 /*
60  * Get planes and escorts argument.
61  * Read planes into *NI_BOMB, and (optional) escorts into *NI_ESC.
62  * If INPUT_BOMB is not empty, use it, else prompt for more input.
63  * Same for INPUT_ESC.
64  * If we got a plane argument, initialize *NI_BOMB and *NI_ESC, and
65  * return 0.
66  * Else return -1 (*NI_BOMB and *NI_ESC may be modified).
67  */
68 int
69 get_planes(struct nstr_item *ni_bomb, struct nstr_item *ni_esc,
70            char *input_bomb, char *input_esc)
71 {
72     if (!snxtitem(ni_bomb, EF_PLANE, input_bomb, NULL))
73         return -1;
74     if (!snxtitem(ni_esc, EF_PLANE, input_esc, "escort(s)? ")) {
75         if (player->aborted)
76             return -1;
77         pr("No escorts...\n");
78     }
79     return 0;
80 }
81
82 /*
83  * Get assembly point argument.
84  * If INPUT is not empty, use it, else prompt for more input using PROMPT.
85  * If this yields a valid assembly point, read it into *AP_SECT and
86  * return AP_SECT.
87  * Else complain and return NULL.
88  * *AP_SECT and BUF[1024] may be modified in either case.
89  */
90 struct sctstr *
91 get_assembly_point(char *input, struct sctstr *ap_sect, char *buf)
92 {
93     char *p;
94     coord x, y;
95     struct nstr_item ni;
96     struct shpstr ship;
97
98     p = getstarg(input, "assembly point? ", buf);
99     if (!p || *p == 0)
100         return NULL;
101     if (!sarg_xy(p, &x, &y) || !getsect(x, y, ap_sect))
102         return NULL;
103
104     /* over own or allied sector is fine */
105     if (ap_sect->sct_own == player->cnum
106         || getrel(getnatp(ap_sect->sct_own), player->cnum) == ALLIED)
107         return ap_sect;
108
109     /* over own or allied ship is fine */
110     snxtitem_xy(&ni, EF_SHIP, x, y);
111     while (nxtitem(&ni, &ship)) {
112         if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
113             continue;
114         if (ship.shp_own == player->cnum
115             || getrel(getnatp(ship.shp_own), player->cnum) == ALLIED)
116             return ap_sect;
117     }
118
119     pr("Assembly point not owned by you or an ally!\n");
120     return NULL;
121 }
122
123 int
124 pln_onewaymission(struct sctstr *target, int *shipno, int *flagp)
125 {
126     int nships;
127     int cno;
128     int flags, fl;
129     struct shpstr ship;
130     char buf[1024];
131     char *p;
132
133     flags = *flagp;
134
135     /* offer carriers */
136     nships = carriersatxy(target->sct_x, target->sct_y, player->cnum);
137     if (nships) {
138         for (;;) {
139             p = getstring("Carrier #? ", buf);
140             if (!p)
141                 return -1;
142             if (!*p)
143                 break;
144             cno = atoi(p);
145             if (cno < 0
146                 || !getship(cno, &ship)
147                 || (!player->owner
148                     && (getrel(getnatp(ship.shp_own), player->cnum)
149                         != ALLIED))) {
150                 pr("Not yours\n");
151                 continue;
152             }
153             if (ship.shp_x != target->sct_x || ship.shp_y != target->sct_y) {
154                 pr("Ship #%d not in %s\n", cno,
155                    xyas(target->sct_x, target->sct_y, player->cnum));
156                 continue;
157             }
158             fl = carrier_planes(&ship, 0);
159             if (fl == 0) {
160                 pr("Can't land on %s.\n", prship(&ship));
161                 continue;
162             }
163             /* clear to land on ship#CNO */
164             pr("landing on carrier %d\n", cno);
165             flags |= fl;
166             *shipno = cno;
167             *flagp = flags;
168             return 0;
169         }
170     }
171
172     /* try to land at sector */
173     if (target->sct_own != player->cnum
174         && getrel(getnatp(target->sct_own), player->cnum) != ALLIED) {
175         pr("Nowhere to land at sector %s!\n",
176            xyas(target->sct_x, target->sct_y, player->cnum));
177         return -1;
178     }
179     if (target->sct_type == SCT_MOUNT) {
180         pr("Nowhere to land at sector %s!\n",
181            xyas(target->sct_x, target->sct_y, player->cnum));
182         return -1;
183     }
184     if (target->sct_type != SCT_AIRPT || target->sct_effic < 60)
185         flags |= P_V;
186
187     /* clear to land at sector */
188     *shipno = -1;
189     *flagp = flags;
190     return 0;
191 }
192
193 int
194 pln_oneway_to_carrier_ok(struct emp_qelem *bomb_list,
195                          struct emp_qelem *esc_list, int cno)
196 {
197     struct emp_qelem *list, *qp;
198     struct plist *plp;
199     struct shpstr ship;
200
201     if (cno < 0 || !getship(cno, &ship))
202         return 0;
203
204     /* for both lists */
205     for (list = bomb_list;
206          list;
207          list = list == bomb_list ? esc_list : NULL) {
208         for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
209             plp = (struct plist *)qp;
210             if (plp->plane.pln_ship == ship.shp_uid)
211                 continue;
212             if (!fit_plane_on_ship(&plp->plane, &ship))
213                 return 0;
214         }
215     }
216     return 1;
217 }
218
219 void
220 pln_newlanding(struct emp_qelem *list, coord tx, coord ty, int cno)
221 {
222     struct emp_qelem *qp;
223     struct plist *plp;
224     struct shpstr ship;
225     struct sctstr sect;
226
227     if (cno >= 0)
228         getship(cno, &ship);
229     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
230         plp = (struct plist *)qp;
231         if (cno >= 0) {
232             if (!could_be_on_ship(&plp->plane, &ship, 0, 0, 0, 0))
233                 pr("\t%s cannot land on ship #%d! %s aborts!\n",
234                    prplane(&plp->plane), cno, prplane(&plp->plane));
235             else if (!put_plane_on_ship(&plp->plane, &ship))
236                 pr("\tNo room on ship #%d! %s aborts!\n",
237                    cno, prplane(&plp->plane));
238             else {
239                 if (plp->plane.pln_own != ship.shp_own) {
240                     wu(0, ship.shp_own, "%s %s lands on your %s\n",
241                        cname(player->cnum), prplane(&plp->plane),
242                        prship(&ship));
243                 }
244             }
245         } else {
246             plp->plane.pln_x = tx;
247             plp->plane.pln_y = ty;
248             getsect(tx, ty, &sect);
249             if (plp->plane.pln_own != sect.sct_own) {
250                 wu(0, sect.sct_own,
251                    "%s %s lands at your sector %s\n",
252                    cname(player->cnum),
253                    prplane(&plp->plane), xyas(tx, ty, sect.sct_own));
254             }
255             plp->plane.pln_ship = cno;
256         }
257     }
258 }
259
260 void
261 pln_dropoff(struct emp_qelem *list, struct ichrstr *ip, coord tx, coord ty,
262             int cno)
263 {
264     struct emp_qelem *qp;
265     struct plist *plp;
266     int amt;
267     struct sctstr sect;
268     struct shpstr ship;
269     int there;
270     int max;
271
272     if (!ip)
273         return;
274     amt = 0;
275     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
276         plp = (struct plist *)qp;
277         amt += plp->load;
278     }
279     if (cno < 0) {
280         getsect(tx, ty, &sect);
281         if (!sect.sct_own) {
282             if (sect.sct_type == SCT_WATER)
283                 pr("Your %s sink like a rock!\n", ip->i_name);
284             else
285                 pr("Your %s vanish without a trace.\n", ip->i_name);
286             return;
287         }
288         if (sect.sct_own != player->cnum
289             && getrel(getnatp(sect.sct_own), player->cnum) != ALLIED) {
290             pr("You don't own %s!  Cargo jettisoned...\n",
291                xyas(tx, ty, player->cnum));
292             return;
293         }
294         if (ip->i_uid == I_CIVIL && sect.sct_own != sect.sct_oldown) {
295             pr("%s is occupied.  Your civilians suffer from identity crisis and die.\n",
296                xyas(tx, ty, player->cnum));
297             return;
298         }
299         there = sect.sct_item[ip->i_uid];
300         max = ITEM_MAX;
301     } else {
302         getship(cno, &ship);
303         there = ship.shp_item[ip->i_uid];
304         max = mchr[ship.shp_type].m_item[ip->i_uid];
305     }
306     there += amt;
307     if (there > max) {
308         pr("%d excess %s discarded\n", there - max, ip->i_name);
309         amt -= there - max;
310         there = max;
311     }
312     pr("%d %s landed safely", amt, ip->i_name);
313     if (cno < 0) {
314         sect.sct_item[ip->i_uid] = there;
315         if (sect.sct_own != player->cnum)
316             wu(0, sect.sct_own, "%s planes drop %d %s in %s\n",
317                cname(player->cnum), amt, ip->i_name,
318                xyas(tx, ty, sect.sct_own));
319         pr(" at %s\n", xyas(tx, ty, player->cnum));
320         putsect(&sect);
321     } else {
322         ship.shp_item[ip->i_uid] = there;
323         if (ship.shp_own != player->cnum)
324             wu(0, ship.shp_own, "%s planes land %d %s on carrier %d\n",
325                cname(player->cnum), amt, ip->i_name, ship.shp_uid);
326         pr(" on carrier #%d\n", ship.shp_uid);
327         putship(ship.shp_uid, &ship);
328     }
329 }
330
331 void
332 pln_mine(struct emp_qelem *list, coord tx, coord ty)
333 {
334     struct emp_qelem *qp;
335     struct plist *plp;
336     int amt;
337     struct sctstr sect;
338
339     amt = 0;
340     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
341         plp = (struct plist *)qp;
342         amt += plp->load;
343
344     }
345     if (amt > 0) {
346         getsect(tx, ty, &sect);
347         if (sect.sct_type != SCT_WATER) {
348             pr("Your seamines have no effect here.\n");
349             return;
350         }
351         sect.sct_mines = MIN(sect.sct_mines + amt, MINES_MAX);
352         pr("%d mines laid in %s.\n", amt, xyas(tx, ty, player->cnum));
353         if (map_set(player->cnum, tx, ty, 'X', 0))
354             writemap(player->cnum);
355         putsect(&sect);
356     }
357 }
358
359 /*
360  * Has PP's type capabilities satisfying WANTFLAGS and NOWANTFLAGS?
361  * A plane type is capable unless
362  * - it lacks all of the P_B, P_T in WANTFLAGS, or
363  * - it lacks all of the P_F, P_ESC in WANTFLAGS, or
364  * - it lacks all of the P_E, P_L, P_K in WANTFLAGS, or
365  * - it lacks any of the other capabilities in WANTFLAGS, or
366  * - it has any of the capabilities in NOWANTFLAGS.
367  */
368 int
369 pln_capable(struct plnstr *pp, int wantflags, int nowantflags)
370 {
371     int flags = plchr[(int)pp->pln_type].pl_flags;
372
373     if (wantflags & (P_B | P_T)) {
374         if ((flags & wantflags & (P_B | P_T)) == 0)
375             return 0;
376         wantflags &= ~(P_B | P_T);
377     }
378
379     if (wantflags & (P_F | P_ESC)) {
380         if ((flags & wantflags & (P_F | P_ESC)) == 0)
381             return 0;
382         wantflags &= ~(P_F | P_ESC);
383     }
384
385     if (wantflags & (P_E | P_L | P_K)) {
386         if ((flags & wantflags & (P_E | P_L | P_K)) == 0)
387             return 0;
388         wantflags &= ~(P_E | P_L | P_K);
389     }
390
391     if ((flags & wantflags) != wantflags)
392         return 0;
393
394     if (flags & nowantflags)
395         return 0;
396
397     return 1;
398 }
399
400 /*
401  * Return union of capabilities of planes in LIST.
402  */
403 int
404 pln_caps(struct emp_qelem *list)
405 {
406     struct emp_qelem *qp;
407     struct plist *plp;
408     int fl;
409
410     fl = 0;
411     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
412         plp = (struct plist *)qp;
413         fl |= plp->pcp->pl_flags;
414     }
415
416     return fl;
417 }
418
419 /*
420  * Find plane types that can operate from carrier SP.
421  * If MSL find missile types, else non-missile types.
422  * Return a combination of P_L, P_K, P_E.
423  * It's zero if SP can't support air operations due to its type or
424  * state (low efficiency).
425  */
426 int
427 carrier_planes(struct shpstr *sp, int msl)
428 {
429     struct mchrstr *mcp = mchr + sp->shp_type;
430     int res;
431
432     if (sp->shp_effic < SHP_AIROPS_EFF)
433         return 0;
434
435     res = 0;
436     if (mcp->m_flags & M_FLY)
437         res |= P_L;
438     if ((mcp->m_flags & M_MSL) && msl)
439         res |= P_L;
440     if (mcp->m_nchoppers && !msl)
441         res |= P_K;
442     if (mcp->m_nxlight)
443         res |= P_E;
444     return res;
445 }
446
447 int
448 pln_airbase_ok(struct plnstr *pp, int oneway, int noisy)
449 {
450     struct shpstr ship;
451     struct lndstr land;
452     struct sctstr sect;
453     struct plchrstr *pcp = plchr + pp->pln_type;
454
455     if (CANT_HAPPEN(noisy && pp->pln_own != player->cnum))
456         noisy = 0;
457
458     if (pp->pln_ship >= 0) {
459         /* ship: needs to be own or allied, efficient */
460         if (!getship(pp->pln_ship, &ship)) {
461             CANT_REACH();
462             return 0;
463         }
464         if (ship.shp_own != pp->pln_own
465             && getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED) {
466             if (noisy)
467                 pr("(note) An ally does not own the ship %s is on\n",
468                    prplane(pp));
469             return 0;
470         }
471         if (!(carrier_planes(&ship, pcp->pl_flags & P_M) & pcp->pl_flags))
472             return 0;
473
474     } else if (pp->pln_land >= 0) {
475         /* land: needs to be own or allied, efficient, not embarked */
476         if (!getland(pp->pln_land, &land)) {
477             CANT_REACH();
478             return 0;
479         }
480         if (land.lnd_own != pp->pln_own
481             && getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED) {
482             if (noisy)
483                 pr("(note) An ally does not own the unit %s is on\n",
484                    prplane(pp));
485             return 0;
486         }
487         if (land.lnd_effic < LND_AIROPS_EFF || !(pcp->pl_flags & P_E))
488             return 0;
489         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
490             return 0;
491
492     } else {
493         /* sector: needs to be own or allied, efficient airfield */
494         if (!getsect(pp->pln_x, pp->pln_y, &sect)) {
495             CANT_REACH();
496             return 0;
497         }
498
499         if (sect.sct_own != pp->pln_own
500             && getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED) {
501             if (noisy)
502                 pr("(note) An ally does not own the sector %s is in\n",
503                    prplane(pp));
504             return 0;
505         }
506         /* need airfield unless VTOL */
507         if ((pcp->pl_flags & P_V) == 0) {
508             if (sect.sct_type != SCT_AIRPT) {
509                 if (noisy)
510                     pr("%s not at airport\n", prplane(pp));
511                 return 0;
512             }
513             if (sect.sct_effic < 40) {
514                 if (noisy)
515                     pr("%s is not 40%% efficient, %s can't take off from there.\n",
516                        xyas(sect.sct_x, sect.sct_y, player->cnum),
517                        prplane(pp));
518                 return 0;
519             }
520             if (!oneway && sect.sct_effic < 60) {
521                 if (noisy)
522                     pr("%s is not 60%% efficient, %s can't land there.\n",
523                        xyas(sect.sct_x, sect.sct_y, player->cnum),
524                        prplane(pp));
525                 return 0;
526             }
527         }
528     }
529
530     return 1;
531 }
532
533 void
534 pln_sel(struct nstr_item *ni, struct emp_qelem *list, struct sctstr *ap,
535         int ap_to_target, int rangemult, int wantflags, int nowantflags)
536 {
537     struct plnstr plane;
538     int range;
539     struct plchrstr *pcp;
540     struct plist *plp;
541
542     emp_initque(list);
543     while (nxtitem(ni, &plane)) {
544         /*
545          * It would be nice to let deities fly foreign planes, but
546          * much of the code assumes that only the plane's owner can
547          * fly it.
548          */
549         if (plane.pln_own != player->cnum)
550             continue;
551         if (plane.pln_mobil <= 0)
552             continue;
553         if (plane.pln_effic < 40) {
554             pr("%s not efficient enough (must be 40%%)\n",
555                prplane(&plane));
556             continue;
557         }
558         if (!pln_capable(&plane, wantflags, nowantflags))
559             continue;
560         if (opt_MARKET) {
561             if (ontradingblock(EF_PLANE, &plane)) {
562                 pr("plane #%d inelligible - it's for sale.\n",
563                    plane.pln_uid);
564                 continue;
565             }
566         }
567
568         range = mapdist(plane.pln_x, plane.pln_y, ap->sct_x, ap->sct_y);
569         if (range > 4) {
570             pr("%s too far from assembly point\n", prplane(&plane));
571             continue;
572         }
573         range += ap_to_target;
574         range *= rangemult;
575         pcp = &plchr[(int)plane.pln_type];
576         if (plane.pln_range < range) {
577             pr("%s out of range (%d:%d)\n",
578                prplane(&plane), plane.pln_range, range);
579             continue;
580         }
581         if (!pln_airbase_ok(&plane, rangemult != 2, 1))
582             continue;
583         pr("%s standing by\n", prplane(&plane));
584         plane.pln_mission = 0;
585         putplane(plane.pln_uid, &plane);
586         plp = malloc(sizeof(struct plist));
587         plp->load = 0;
588         plp->pcp = pcp;
589         plp->plane = plane;
590         emp_insque(&plp->queue, list);
591     }
592 }
593
594 void
595 pln_arm(struct emp_qelem *list, int dist, char mission, struct ichrstr *ip)
596 {
597     struct emp_qelem *qp;
598     struct emp_qelem *next;
599     struct plist *plp;
600     struct plnstr *pp;
601
602     for (qp = list->q_forw; qp != list; qp = next) {
603         next = qp->q_forw;
604         plp = (struct plist *)qp;
605         pp = &plp->plane;
606         getplane(pp->pln_uid, pp);
607         if ((pp->pln_flags & PLN_LAUNCHED)
608             || pln_equip(plp, ip, mission) < 0) {
609             emp_remque(qp);
610             free(qp);
611             continue;
612         }
613         pp->pln_flags |= PLN_LAUNCHED;
614         pp->pln_mobil -= pln_mobcost(dist, pp, mission);
615         putplane(pp->pln_uid, pp);
616         pr("%s equipped\n", prplane(pp));
617     }
618 }
619
620 int
621 pln_equip(struct plist *plp, struct ichrstr *ip, char mission)
622 {
623     struct plchrstr *pcp;
624     struct plnstr *pp;
625     int load, needed;
626     struct lndstr land;
627     struct shpstr ship;
628     struct sctstr sect;
629     i_type itype;
630     short *item;
631     int own;
632     int abandon_needed;
633
634     pp = &plp->plane;
635     pcp = plp->pcp;
636     if (pp->pln_ship >= 0) {
637         getship(pp->pln_ship, &ship);
638         item = ship.shp_item;
639         own = ship.shp_own;
640     } else if (pp->pln_land >= 0) {
641         getland(pp->pln_land, &land);
642         item = land.lnd_item;
643         own = land.lnd_own;
644     } else {
645         getsect(pp->pln_x, pp->pln_y, &sect);
646         item = sect.sct_item;
647         own = sect.sct_oldown;
648     }
649     if (pcp->pl_fuel > item[I_PETROL]) {
650         pr("%s not enough petrol there!\n", prplane(pp));
651         return -1;
652     }
653     item[I_PETROL] -= pcp->pl_fuel;
654     load = pln_load(pp);
655     itype = I_NONE;
656     switch (mission) {
657     case 's':           /* strategic bomb */
658     case 'p':           /* pinpoint bomb */
659         itype = I_SHELL;
660         break;
661     case 't':           /* transport */
662         if (!(pcp->pl_flags & P_C) || !ip)
663             break;
664         itype = ip->i_uid;
665         load *= 2;
666         break;
667     case 'm':           /* mine */
668         if ((pcp->pl_flags & P_MINE) == 0)
669             break;
670         itype = I_SHELL;
671         load *= 2;
672         break;
673     case 'd':           /* drop */
674         if (!(pcp->pl_flags & P_C) || CANT_HAPPEN(!ip))
675             break;
676         itype = ip->i_uid;
677         if (pcp->pl_flags & P_V)
678             load *= 2;
679         break;
680     case 'a':           /* paradrop */
681         if (!(pcp->pl_flags & P_P))
682             break;
683         itype = I_MILIT;
684         if (pcp->pl_flags & P_V)
685             load *= 2;
686         break;
687     case 'r':           /* reconnaissance */
688     case 'e':           /* escort */
689         load = 0;
690         break;
691     case 'i':           /* missile interception */
692         if (CANT_HAPPEN(!(pcp->pl_flags & P_M)
693                         || !(pcp->pl_flags & (P_N | P_O))))
694             break;
695         if (load)
696             itype = I_SHELL;
697         break;
698     default:
699         CANT_REACH();
700         load = 0;
701     }
702
703     if (itype != I_NONE) {
704         needed = load / ichr[itype].i_lbs;
705         if (needed <= 0) {
706             pr("%s can't contribute to mission\n", prplane(pp));
707             return -1;
708         }
709         if (nuk_on_plane(pp) >= 0) {
710             if (mission == 's' || mission == 't')
711                 needed = 0;
712             else {
713                 pr("%s can't fly this mission"
714                    " while it is carrying a nuclear weapon",
715                    prplane(pp));
716                 return -1;
717             }
718         }
719         if (itype == I_CIVIL && pp->pln_own != own) {
720             pr("You don't control those civilians!\n");
721             return -1;
722         }
723 #if 0
724         /* Supply is broken somewhere, so don't use it for now */
725         if (itype == I_SHELL && item[itype] < needed)
726             item[itype] += supply_commod(plp->plane.pln_own,
727                                          plp->plane.pln_x,
728                                          plp->plane.pln_y,
729                                          I_SHELL, needed);
730 #endif
731         abandon_needed = !!would_abandon(&sect, itype, needed, NULL);
732         if (item[itype] < needed + abandon_needed) {
733             pr("Not enough %s for %s\n", ichr[itype].i_name, prplane(pp));
734             return -1;
735         }
736         item[itype] -= needed;
737         plp->load = needed;
738     }
739
740     if (pp->pln_ship >= 0) {
741         if (pp->pln_own != ship.shp_own) {
742             wu(0, ship.shp_own,
743                "%s %s prepares for takeoff from ship %s\n",
744                cname(pp->pln_own), prplane(pp), prship(&ship));
745         }
746         putship(ship.shp_uid, &ship);
747     } else if (pp->pln_land >= 0) {
748         if (pp->pln_own != land.lnd_own) {
749             wu(0, land.lnd_own,
750                "%s %s prepares for takeoff from unit %s\n",
751                cname(pp->pln_own), prplane(pp), prland(&land));
752         }
753         putland(land.lnd_uid, &land);
754     } else {
755         if (pp->pln_own != sect.sct_own) {
756             wu(0, sect.sct_own, "%s %s prepares for takeoff from %s\n",
757                cname(pp->pln_own), prplane(pp),
758                xyas(sect.sct_x, sect.sct_y, sect.sct_own));
759         }
760         putsect(&sect);
761     }
762     return 0;
763 }
764
765 void
766 pln_put(struct emp_qelem *list)
767 {
768     struct emp_qelem *qp, *next;
769
770     for (qp = list->q_forw; qp != list; qp = next) {
771         next = qp->q_forw;
772         pln_put1((struct plist *)qp);
773     }
774 }
775
776 void
777 pln_put1(struct plist *plp)
778 {
779     struct plnstr *pp;
780     struct shpstr ship;
781     struct sctstr sect;
782
783     pp = &plp->plane;
784
785     if (CANT_HAPPEN((pp->pln_flags & PLN_LAUNCHED)
786                     && (plchr[pp->pln_type].pl_flags & P_M)
787                     && pp->pln_effic >= PLANE_MINEFF))
788         pp->pln_effic = 0;   /* bug: missile launched but not used up */
789
790     if (!(pp->pln_flags & PLN_LAUNCHED))
791         ;                       /* never took off */
792     else if (pp->pln_effic < PLANE_MINEFF) {
793         ;                       /* destroyed */
794     } else if (pp->pln_ship >= 0) {
795         /* It is landing on a carrier */
796         getship(pp->pln_ship, &ship);
797         /* We should do more, like make sure it's really
798            a carrier, etc. but for now just make sure it's
799            not sunk. */
800         if (ship.shp_effic < SHIP_MINEFF) {
801             mpr(pp->pln_own,
802                 "Ship #%d has been sunk, plane #%d has nowhere to land, and\n"
803                 "splashes into the sea.\n",
804                 pp->pln_ship, pp->pln_uid);
805             pp->pln_effic = 0;
806         }
807     } else {
808         /* Presume we are landing back in a sector. */
809         getsect(pp->pln_x, pp->pln_y, &sect);
810         if (sect.sct_type == SCT_WATER || sect.sct_type == SCT_WASTE) {
811             mpr(pp->pln_own,
812                 "Nowhere to land at %s, plane #%d crashes and burns...\n",
813                 xyas(pp->pln_x, pp->pln_y, pp->pln_own), pp->pln_uid);
814             pp->pln_effic = 0;
815         }
816     }
817     pp->pln_flags &= ~PLN_LAUNCHED;
818     putplane(pp->pln_uid, pp);
819     emp_remque(&plp->queue);
820     free(plp);
821 }
822
823 /*
824  * Can PP be loaded on a ship of SP's type?
825  * Assume that it already carries N planes, of which NCH are choppers,
826  * NXL xlight, NMSL light missiles, and the rest are light fixed-wing
827  * planes.
828  */
829 int
830 could_be_on_ship(struct plnstr *pp, struct shpstr *sp,
831                  int n, int nch, int nxl, int nmsl)
832 {
833     struct plchrstr *pcp = &plchr[pp->pln_type];
834     struct mchrstr *mcp = &mchr[sp->shp_type];
835     int nfw;
836
837     if (pcp->pl_flags & P_K)
838         nch++;
839     else if (pcp->pl_flags & P_E)
840         nxl++;
841     else if (!(pcp->pl_flags & P_L))
842         return 0;
843     else if (pcp->pl_flags & P_M)
844         nmsl++;
845     n++;
846     nfw = n - nch - nxl - nmsl;
847
848     if (nch > mcp->m_nchoppers) /* overflow into fixed-wing slots */
849         nfw += nch - mcp->m_nchoppers;
850     if (nxl > mcp->m_nxlight)   /* overflow into missile slots */
851         nmsl += nxl - mcp->m_nxlight;
852     if (nmsl && !(mcp->m_flags & (M_MSL | M_FLY)))
853         return 0;               /* missile slots wanted */
854     if (nfw && !(mcp->m_flags & M_FLY))
855         return 0;               /* fixed-wing slots wanted */
856     return nfw + nmsl <= mcp->m_nplanes;
857 }
858
859 /*
860  * Fit a plane of PP's type on ship SP.
861  * Updating the plane accordingly is the caller's job.
862  * Return whether it fits.
863  */
864 static int
865 fit_plane_on_ship(struct plnstr *pp, struct shpstr *sp)
866 {
867     int n, nch, nxl, nmsl;
868
869     n = shp_nplane(sp, &nch, &nxl, &nmsl);
870     return could_be_on_ship(pp, sp, n, nch, nxl, nmsl);
871 }
872
873 int
874 put_plane_on_ship(struct plnstr *plane, struct shpstr *ship)
875 {
876     if (plane->pln_ship == ship->shp_uid)
877         return 1;               /* Already on ship */
878
879     if (!fit_plane_on_ship(plane, ship))
880         return 0;
881
882     plane->pln_x = ship->shp_x;
883     plane->pln_y = ship->shp_y;
884     plane->pln_ship = ship->shp_uid;
885     putplane(plane->pln_uid, plane);
886     return 1;
887 }
888
889 /*
890  * Fit a plane of PP's type on land unit LP.
891  * Updating the plane accordingly is the caller's job.
892  * Return whether it fits.
893  */
894 static int
895 fit_plane_on_land(struct plnstr *pp, struct lndstr *lp)
896 {
897     struct plchrstr *pcp = plchr + pp->pln_type;
898     struct lchrstr *lcp = lchr + lp->lnd_type;
899
900     return (pcp->pl_flags & P_E) && lnd_nxlight(lp) < lcp->l_nxlight;
901 }
902
903 int
904 put_plane_on_land(struct plnstr *plane, struct lndstr *land)
905 {
906     if (plane->pln_land == land->lnd_uid)
907         return 1;               /* Already on unit */
908
909     if (!fit_plane_on_land(plane, land))
910         return 0;
911
912     plane->pln_x = land->lnd_x;
913     plane->pln_y = land->lnd_y;
914     plane->pln_land = land->lnd_uid;
915     putplane(plane->pln_uid, plane);
916     return 1;
917 }
918
919 void
920 plane_sweep(struct emp_qelem *plane_list, coord x, coord y)
921 {
922     struct plnstr *pp;
923     struct plchrstr *pcp;
924     struct emp_qelem *qp;
925     struct emp_qelem *next;
926     struct plist *ip;
927     struct sctstr sect;
928     int mines_there;
929     int found = 0;
930
931     getsect(x, y, &sect);
932     mines_there = sect.sct_mines;
933
934     if (mines_there == 0)
935         return;
936
937     if (sect.sct_type != SCT_WATER)
938         return;
939
940     for (qp = plane_list->q_forw; ((qp != plane_list) && (mines_there));
941          qp = next) {
942         next = qp->q_forw;
943         ip = (struct plist *)qp;
944         pp = &ip->plane;
945         pcp = ip->pcp;
946         if (!(pcp->pl_flags & P_SWEEP)) /* if it isn't an sweep plane */
947             continue;
948
949         if (chance((100.0 - pln_acc(pp)) / 100.0)) {
950             pr("Sweep! in %s\n",
951                xyas(sect.sct_x, sect.sct_y, player->cnum));
952             mines_there--;
953             found = 1;
954         }
955     }
956
957     if (found && map_set(player->cnum, sect.sct_x, sect.sct_y, 'X', 0))
958         writemap(player->cnum);
959     sect.sct_mines = mines_there;
960     putsect(&sect);
961 }
962
963 int
964 pln_hitchance(struct plnstr *pp, int hardtarget, int type)
965 {
966     struct plchrstr *pcp = plchr + pp->pln_type;
967     double tfact = (double)(pp->pln_tech - pcp->pl_tech) /
968         (pp->pln_tech - pcp->pl_tech / 2);
969     int acc = pln_acc(pp);
970     int hitchance;
971
972     if (type == EF_SHIP) {
973         if (pcp->pl_flags & P_A)
974             acc -= 20;
975         if (!(pcp->pl_flags & P_T))
976             acc += 35;
977     }
978     hitchance = (int)(pp->pln_effic * (1.0 - 0.1 * tfact) *
979                       (1.0 - acc / 100.0)) - hardtarget;
980
981     /* smooth out the bottom of the graph with asymtote at 5 -KHS */
982     if (hitchance < 20)
983         hitchance = 5 + ldround(300.0 / (40.0 - hitchance), 1);
984     if (hitchance > 100)
985         hitchance = 100;
986     return hitchance;
987 }
988
989 int
990 pln_damage(struct plnstr *pp, char type, int noisy)
991 {
992     struct plchrstr *pcp = plchr + pp->pln_type;
993     int load, i, hitroll, aim, len;
994     int dam = 0;
995     int effective = 1;
996     int pinbomber = 0;
997     char buf[80];
998
999     if (CANT_HAPPEN(nuk_on_plane(pp) >= 0))
1000         return 0;
1001
1002     load = pln_load(pp);
1003     if (!load)                 /* e.g. ab, blowing up on launch pad */
1004         return 0;
1005
1006     i = roll(load) + 1;
1007     if (i > load)
1008         i = load;
1009
1010     if (pcp->pl_flags & P_M) {
1011         if (pcp->pl_flags & P_MAR)
1012             pinbomber = 1;
1013     } else if (pcp->pl_flags & P_T)
1014         pinbomber = 1;
1015
1016     aim = pln_acc(pp);
1017     if (type == 's') {
1018         effective = !pinbomber;
1019         aim = 30 + (pinbomber ? aim : 100 - aim);
1020     } else {
1021         effective = pinbomber;
1022         aim = 100 - aim;
1023     }
1024
1025     len = 0;
1026     while (i--) {
1027         dam += roll(6);
1028         hitroll = roll(100);
1029         if (hitroll >= 90) {
1030             dam += 8;
1031             if (noisy)
1032                 len += sprintf(buf + len, "BLAM");
1033         } else if (hitroll < aim) {
1034             dam += 5;
1035             if (noisy)
1036                 len += sprintf(buf + len, "Blam");
1037         } else {
1038             dam += 1;
1039             if (noisy)
1040                 len += sprintf(buf + len, "blam");
1041         }
1042         if (noisy) {
1043             if (len > 75) {
1044                 mpr(pp->pln_own, "%s\n", buf);
1045                 len = 0;
1046             }
1047             if (i)
1048                 len += sprintf(buf + len, "-");
1049         }
1050     }
1051     if (noisy && len)
1052         mpr(pp->pln_own, "%s\n", buf);
1053     if (effective)
1054         dam *= 2;
1055     return dam;
1056 }
1057
1058 int
1059 pln_identchance(struct plnstr *pp, int hardtarget, int type)
1060 {
1061     double misschance =
1062         (100.0 - pln_hitchance(pp, hardtarget, type)) / 100.0;
1063     return (int)(100 - 100 * misschance * misschance);
1064 }
1065
1066 int
1067 pln_mobcost(int dist, struct plnstr *pp, char mission)
1068 {
1069     double cost;
1070
1071     cost = 20.0 / (pp->pln_effic / 100.0);
1072     if (mission == 'e' || mission == 0)
1073         cost /= 2;              /* escort or intercept */
1074
1075     return ldround(cost * dist / pln_range_max(pp) + 5, 1);
1076 }
1077
1078 int
1079 pln_is_in_orbit(struct plnstr *pp)
1080 {
1081     return (plchr[pp->pln_type].pl_flags & (P_M | P_O)) == P_O
1082         && (pp->pln_flags & PLN_LAUNCHED);
1083 }
1084
1085 /*
1086  * Set PP's tech to TLEV along with everything else that depends on it.
1087  */
1088 void
1089 pln_set_tech(struct plnstr *pp, int tlev)
1090 {
1091     struct plchrstr *pcp = plchr + pp->pln_type;
1092     int limited_range = pp->pln_range < pln_range_max(pp);
1093     int range_max;
1094
1095     if (CANT_HAPPEN(tlev < pcp->pl_tech))
1096         tlev = pcp->pl_tech;
1097     pp->pln_tech = tlev;
1098
1099     range_max = pln_range_max(pp);
1100     if (!limited_range || pp->pln_range > range_max)
1101         pp->pln_range = range_max;
1102 }