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