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