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