]> git.pond.sub.org Git - empserver/blob - src/lib/subs/plnsub.c
Revise rules for cargo plane loads
[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-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 pln_equip(struct plist *, struct ichrstr *, 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)
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_B, P_T in WANTFLAGS, or
364  * - it lacks all of the P_F, P_ESC in WANTFLAGS, or
365  * - it lacks all of the P_E, P_L, P_K in WANTFLAGS, or
366  * - it lacks any of the other capabilities in WANTFLAGS, or
367  * - it has any of the capabilities in NOWANTFLAGS.
368  */
369 int
370 pln_capable(struct plnstr *pp, int wantflags, int nowantflags)
371 {
372     int flags = plchr[(int)pp->pln_type].pl_flags;
373
374     if (wantflags & (P_B | P_T)) {
375         if ((flags & wantflags & (P_B | P_T)) == 0)
376             return 0;
377         wantflags &= ~(P_B | P_T);
378     }
379
380     if (wantflags & (P_F | P_ESC)) {
381         if ((flags & wantflags & (P_F | P_ESC)) == 0)
382             return 0;
383         wantflags &= ~(P_F | P_ESC);
384     }
385
386     if (wantflags & (P_E | P_L | P_K)) {
387         if ((flags & wantflags & (P_E | P_L | P_K)) == 0)
388             return 0;
389         wantflags &= ~(P_E | P_L | P_K);
390     }
391
392     if ((flags & wantflags) != wantflags)
393         return 0;
394
395     if (flags & nowantflags)
396         return 0;
397
398     return 1;
399 }
400
401 /*
402  * Return union of capabilities of planes in LIST.
403  */
404 int
405 pln_caps(struct emp_qelem *list)
406 {
407     struct emp_qelem *qp;
408     struct plist *plp;
409     int fl;
410
411     fl = 0;
412     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
413         plp = (struct plist *)qp;
414         fl |= plp->pcp->pl_flags;
415     }
416
417     return fl;
418 }
419
420 /*
421  * Find plane types that can operate from carrier SP.
422  * If MSL find missile types, else non-missile types.
423  * Return a combination of P_L, P_K, P_E.
424  * It's zero if SP can't support air operations due to its type or
425  * state (low efficiency).
426  */
427 int
428 carrier_planes(struct shpstr *sp, int msl)
429 {
430     struct mchrstr *mcp = mchr + sp->shp_type;
431     int res;
432
433     if (sp->shp_effic < SHP_AIROPS_EFF)
434         return 0;
435
436     res = 0;
437     if (mcp->m_flags & M_FLY)
438         res |= P_L;
439     if ((mcp->m_flags & M_MSL) && msl)
440         res |= P_L;
441     if (mcp->m_nchoppers && !msl)
442         res |= P_K;
443     if (mcp->m_nxlight)
444         res |= P_E;
445     return res;
446 }
447
448 int
449 pln_airbase_ok(struct plnstr *pp, int oneway, int noisy)
450 {
451     struct shpstr ship;
452     struct lndstr land;
453     struct sctstr sect;
454     struct plchrstr *pcp = plchr + pp->pln_type;
455
456     if (CANT_HAPPEN(noisy && pp->pln_own != player->cnum))
457         noisy = 0;
458
459     if (pp->pln_ship >= 0) {
460         /* ship: needs to be own or allied, efficient */
461         if (!getship(pp->pln_ship, &ship)) {
462             CANT_REACH();
463             return 0;
464         }
465         if (ship.shp_own != pp->pln_own
466             && getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED) {
467             if (noisy)
468                 pr("(note) An ally does not own the ship %s is on\n",
469                    prplane(pp));
470             return 0;
471         }
472         if (!(carrier_planes(&ship, pcp->pl_flags & P_M) & pcp->pl_flags))
473             return 0;
474
475     } else if (pp->pln_land >= 0) {
476         /* land: needs to be own or allied, efficient, not embarked */
477         if (!getland(pp->pln_land, &land)) {
478             CANT_REACH();
479             return 0;
480         }
481         if (land.lnd_own != pp->pln_own
482             && getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED) {
483             if (noisy)
484                 pr("(note) An ally does not own the unit %s is on\n",
485                    prplane(pp));
486             return 0;
487         }
488         if (land.lnd_effic < LND_AIROPS_EFF || !(pcp->pl_flags & P_E))
489             return 0;
490         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
491             return 0;
492
493     } else {
494         /* sector: needs to be own or allied, efficient airfield */
495         if (!getsect(pp->pln_x, pp->pln_y, &sect)) {
496             CANT_REACH();
497             return 0;
498         }
499
500         if (sect.sct_own != pp->pln_own
501             && getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED) {
502             if (noisy)
503                 pr("(note) An ally does not own the sector %s is in\n",
504                    prplane(pp));
505             return 0;
506         }
507         /* need airfield unless VTOL */
508         if ((pcp->pl_flags & P_V) == 0) {
509             if (sect.sct_type != SCT_AIRPT) {
510                 if (noisy)
511                     pr("%s not at airport\n", prplane(pp));
512                 return 0;
513             }
514             if (sect.sct_effic < 40) {
515                 if (noisy)
516                     pr("%s is not 40%% efficient, %s can't take off from there.\n",
517                        xyas(sect.sct_x, sect.sct_y, pp->pln_own),
518                        prplane(pp));
519                 return 0;
520             }
521             if (!oneway && sect.sct_effic < 60) {
522                 if (noisy)
523                     pr("%s is not 60%% efficient, %s can't land there.\n",
524                        xyas(sect.sct_x, sect.sct_y, pp->pln_own),
525                        prplane(pp));
526                 return 0;
527             }
528         }
529     }
530
531     return 1;
532 }
533
534 void
535 pln_sel(struct nstr_item *ni, struct emp_qelem *list, struct sctstr *ap,
536         int ap_to_target, int rangemult, int wantflags, int nowantflags)
537 {
538     struct plnstr plane;
539     int range;
540     struct plchrstr *pcp;
541     struct plist *plp;
542
543     emp_initque(list);
544     while (nxtitem(ni, &plane)) {
545         if (!player->owner)
546             continue;
547         if (plane.pln_mobil <= 0)
548             continue;
549         if (plane.pln_effic < 40) {
550             pr("%s not efficient enough (must be 40%%)\n",
551                prplane(&plane));
552             continue;
553         }
554         if (!pln_capable(&plane, wantflags, nowantflags))
555             continue;
556         if (opt_MARKET) {
557             if (ontradingblock(EF_PLANE, &plane)) {
558                 pr("plane #%d inelligible - it's for sale.\n",
559                    plane.pln_uid);
560                 continue;
561             }
562         }
563
564         range = mapdist(plane.pln_x, plane.pln_y, ap->sct_x, ap->sct_y);
565         if (range > 4) {
566             pr("%s too far from assembly point\n", prplane(&plane));
567             continue;
568         }
569         range += ap_to_target;
570         range *= rangemult;
571         pcp = &plchr[(int)plane.pln_type];
572         if (plane.pln_range < range) {
573             pr("%s out of range (%d:%d)\n",
574                prplane(&plane), plane.pln_range, range);
575             continue;
576         }
577         if (!pln_airbase_ok(&plane, rangemult != 2, 1))
578             continue;
579         pr("%s standing by\n", prplane(&plane));
580         plane.pln_mission = 0;
581         putplane(plane.pln_uid, &plane);
582         plp = malloc(sizeof(struct plist));
583         plp->misc = 0;
584         plp->bombs = 0;
585         plp->pcp = pcp;
586         plp->plane = plane;
587         emp_insque(&plp->queue, list);
588     }
589 }
590
591 void
592 pln_arm(struct emp_qelem *list, int dist, char mission, struct ichrstr *ip)
593 {
594     struct emp_qelem *qp;
595     struct emp_qelem *next;
596     struct plist *plp;
597     struct plnstr *pp;
598
599     for (qp = list->q_forw; qp != list; qp = next) {
600         next = qp->q_forw;
601         plp = (struct plist *)qp;
602         pp = &plp->plane;
603         getplane(pp->pln_uid, pp);
604         if ((pp->pln_flags & PLN_LAUNCHED)
605             || pln_equip(plp, ip, mission) < 0) {
606             emp_remque(qp);
607             free(qp);
608             continue;
609         }
610         pp->pln_flags |= PLN_LAUNCHED;
611         pp->pln_mobil -= pln_mobcost(dist, pp, mission);
612         putplane(pp->pln_uid, pp);
613         pr("%s equipped\n", prplane(pp));
614     }
615 }
616
617 static int
618 pln_equip(struct plist *plp, struct ichrstr *ip, char mission)
619 {
620     struct plchrstr *pcp;
621     struct plnstr *pp;
622     int load, needed;
623     struct lndstr land;
624     struct shpstr ship;
625     struct sctstr sect;
626     i_type itype;
627     short *item;
628     int own;
629     int abandon_needed;
630
631     pp = &plp->plane;
632     pcp = plp->pcp;
633     if (pp->pln_ship >= 0) {
634         getship(pp->pln_ship, &ship);
635         item = ship.shp_item;
636         own = ship.shp_own;
637     } else if (pp->pln_land >= 0) {
638         getland(pp->pln_land, &land);
639         item = land.lnd_item;
640         own = land.lnd_own;
641     } else {
642         getsect(pp->pln_x, pp->pln_y, &sect);
643         item = sect.sct_item;
644         own = sect.sct_oldown;
645     }
646     if (pcp->pl_fuel > item[I_PETROL]) {
647         pr("%s not enough petrol there!\n", prplane(pp));
648         return -1;
649     }
650     item[I_PETROL] -= pcp->pl_fuel;
651     load = pln_load(pp);
652     itype = I_NONE;
653     switch (mission) {
654     case 's':           /* strategic bomb */
655     case 'p':           /* pinpoint bomb */
656         if (nuk_on_plane(pp) >= 0)
657             break;
658         itype = I_SHELL;
659         break;
660     case 't':           /* transport */
661         if (!(pcp->pl_flags & P_C) || !ip)
662             break;
663         itype = ip->i_uid;
664         load *= 2;
665         break;
666     case 'm':           /* mine */
667         if ((pcp->pl_flags & P_MINE) == 0)
668             break;
669         itype = I_SHELL;
670         load *= 2;
671         break;
672     case 'd':           /* drop */
673         if (!(pcp->pl_flags & P_C) || CANT_HAPPEN(!ip))
674             break;
675         itype = ip->i_uid;
676         if (pcp->pl_flags & P_V)
677             load *= 2;
678         break;
679     case 'a':           /* paradrop */
680         if (!(pcp->pl_flags & P_P))
681             break;
682         itype = I_MILIT;
683         if (pcp->pl_flags & P_V)
684             load *= 2;
685         break;
686     case 'r':           /* reconnaissance */
687     case 'e':           /* escort */
688         load = 0;
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 (itype == I_CIVIL && pp->pln_own != own) {
702             pr("You don't control those civilians!\n");
703             return -1;
704         }
705 #if 0
706         /* Supply is broken somewhere, so don't use it for now */
707         if (itype == I_SHELL && item[itype] < needed)
708             item[itype] += supply_commod(plp->plane.pln_own,
709                                          plp->plane.pln_x,
710                                          plp->plane.pln_y,
711                                          I_SHELL, needed);
712 #endif
713         abandon_needed = !!would_abandon(&sect, itype, needed, NULL);
714         if (item[itype] < needed + abandon_needed) {
715             pr("Not enough %s for %s\n", ichr[itype].i_name, prplane(pp));
716             return -1;
717         }
718         item[itype] -= needed;
719         if (itype == I_SHELL && (mission == 's' || mission == 'p'))
720             plp->bombs = needed;
721         else
722             plp->misc = needed;
723     }
724
725     if (pp->pln_ship >= 0) {
726         if (pp->pln_own != ship.shp_own) {
727             wu(0, ship.shp_own,
728                "%s %s prepares for takeoff from ship %s\n",
729                cname(pp->pln_own), prplane(pp), prship(&ship));
730         }
731         putship(ship.shp_uid, &ship);
732     } else if (pp->pln_land >= 0) {
733         if (pp->pln_own != land.lnd_own) {
734             wu(0, land.lnd_own,
735                "%s %s prepares for takeoff from unit %s\n",
736                cname(pp->pln_own), prplane(pp), prland(&land));
737         }
738         putland(land.lnd_uid, &land);
739     } else {
740         if (pp->pln_own != sect.sct_own) {
741             wu(0, sect.sct_own, "%s %s prepares for takeoff from %s\n",
742                cname(pp->pln_own), prplane(pp),
743                xyas(sect.sct_x, sect.sct_y, sect.sct_own));
744         }
745         putsect(&sect);
746     }
747     return 0;
748 }
749
750 void
751 pln_put(struct emp_qelem *list)
752 {
753     struct emp_qelem *qp, *next;
754
755     for (qp = list->q_forw; qp != list; qp = next) {
756         next = qp->q_forw;
757         pln_put1((struct plist *)qp);
758     }
759 }
760
761 void
762 pln_put1(struct plist *plp)
763 {
764     struct plnstr *pp;
765     struct shpstr ship;
766     struct sctstr sect;
767
768     pp = &plp->plane;
769
770     if (CANT_HAPPEN((pp->pln_flags & PLN_LAUNCHED)
771                     && (plchr[pp->pln_type].pl_flags & P_M)
772                     && pp->pln_effic >= PLANE_MINEFF))
773         pp->pln_effic = 0;   /* bug: missile launched but not used up */
774
775     if (!(pp->pln_flags & PLN_LAUNCHED))
776         ;                       /* never took off */
777     else if (pp->pln_effic < PLANE_MINEFF) {
778         ;                       /* destroyed */
779     } else if (pp->pln_ship >= 0) {
780         /* It is landing on a carrier */
781         getship(pp->pln_ship, &ship);
782         /* We should do more, like make sure it's really
783            a carrier, etc. but for now just make sure it's
784            not sunk. */
785         if (ship.shp_effic < SHIP_MINEFF) {
786             mpr(pp->pln_own,
787                 "Ship #%d has been sunk, plane #%d has nowhere to land, and\n"
788                 "splashes into the sea.\n",
789                 pp->pln_ship, pp->pln_uid);
790             pp->pln_effic = 0;
791         }
792     } else {
793         /* Presume we are landing back in a sector. */
794         getsect(pp->pln_x, pp->pln_y, &sect);
795         if (sect.sct_type == SCT_WATER || sect.sct_type == SCT_WASTE) {
796             mpr(pp->pln_own,
797                 "Nowhere to land at %s, plane #%d crashes and burns...\n",
798                 xyas(pp->pln_x, pp->pln_y, pp->pln_own), pp->pln_uid);
799             pp->pln_effic = 0;
800         }
801     }
802     pp->pln_flags &= ~PLN_LAUNCHED;
803     putplane(pp->pln_uid, pp);
804     emp_remque(&plp->queue);
805     free(plp);
806 }
807
808 /*
809  * Can PP be loaded on a ship of SP's type?
810  * Assume that it already carries N planes, of which NCH are choppers,
811  * NXL xlight, NMSL light missiles, and the rest are light fixed-wing
812  * planes.
813  */
814 int
815 could_be_on_ship(struct plnstr *pp, struct shpstr *sp,
816                  int n, int nch, int nxl, int nmsl)
817 {
818     struct plchrstr *pcp = &plchr[pp->pln_type];
819     struct mchrstr *mcp = &mchr[sp->shp_type];
820
821     if (pcp->pl_flags & P_K)
822         nch++;
823     else if (pcp->pl_flags & P_E)
824         nxl++;
825     else if (!(pcp->pl_flags & P_L))
826         return 0;
827     else if (pcp->pl_flags & P_M)
828         nmsl++;
829     n++;
830
831     n -= MIN(nch, mcp->m_nchoppers);
832     n -= MIN(nxl, mcp->m_nxlight);
833     if (nmsl && !(mcp->m_flags & (M_MSL | M_FLY)))
834         return 0;               /* missile slots wanted */
835     if (nmsl < n && !(mcp->m_flags & M_FLY))
836         return 0;               /* fixed-wing slots wanted */
837     return n <= mcp->m_nplanes;
838 }
839
840 /*
841  * Fit a plane of PP's type on ship SP.
842  * Updating the plane accordingly is the caller's job.
843  * Return whether it fits.
844  */
845 static int
846 fit_plane_on_ship(struct plnstr *pp, struct shpstr *sp)
847 {
848     int n, nch, nxl, nmsl;
849
850     n = shp_nplane(sp, &nch, &nxl, &nmsl);
851     return could_be_on_ship(pp, sp, n, nch, nxl, nmsl);
852 }
853
854 int
855 put_plane_on_ship(struct plnstr *plane, struct shpstr *ship)
856 {
857     if (plane->pln_ship == ship->shp_uid)
858         return 1;               /* Already on ship */
859
860     if (!fit_plane_on_ship(plane, ship))
861         return 0;
862
863     plane->pln_x = ship->shp_x;
864     plane->pln_y = ship->shp_y;
865     plane->pln_ship = ship->shp_uid;
866     putplane(plane->pln_uid, plane);
867     return 1;
868 }
869
870 /*
871  * Fit a plane of PP's type on land unit LP.
872  * Updating the plane accordingly is the caller's job.
873  * Return whether it fits.
874  */
875 static int
876 fit_plane_on_land(struct plnstr *pp, struct lndstr *lp)
877 {
878     struct plchrstr *pcp = plchr + pp->pln_type;
879     struct lchrstr *lcp = lchr + lp->lnd_type;
880
881     return (pcp->pl_flags & P_E) && lnd_nxlight(lp) < lcp->l_nxlight;
882 }
883
884 int
885 put_plane_on_land(struct plnstr *plane, struct lndstr *land)
886 {
887     if (plane->pln_land == land->lnd_uid)
888         return 1;               /* Already on unit */
889
890     if (!fit_plane_on_land(plane, land))
891         return 0;
892
893     plane->pln_x = land->lnd_x;
894     plane->pln_y = land->lnd_y;
895     plane->pln_land = land->lnd_uid;
896     putplane(plane->pln_uid, plane);
897     return 1;
898 }
899
900 void
901 plane_sweep(struct emp_qelem *plane_list, coord x, coord y)
902 {
903     struct plnstr *pp;
904     struct plchrstr *pcp;
905     struct emp_qelem *qp;
906     struct emp_qelem *next;
907     struct plist *ip;
908     struct sctstr sect;
909     int mines_there;
910     int found = 0;
911
912     getsect(x, y, &sect);
913     mines_there = sect.sct_mines;
914
915     if (mines_there == 0)
916         return;
917
918     if (sect.sct_type != SCT_WATER)
919         return;
920
921     for (qp = plane_list->q_forw; ((qp != plane_list) && (mines_there));
922          qp = next) {
923         next = qp->q_forw;
924         ip = (struct plist *)qp;
925         pp = &ip->plane;
926         pcp = ip->pcp;
927         if (!(pcp->pl_flags & P_SWEEP)) /* if it isn't an sweep plane */
928             continue;
929
930         if (chance((100.0 - pln_acc(pp)) / 100.0)) {
931             pr("Sweep! in %s\n",
932                xyas(sect.sct_x, sect.sct_y, pp->pln_own));
933             mines_there--;
934             found = 1;
935         }
936     }
937
938     if (found && map_set(player->cnum, sect.sct_x, sect.sct_y, 'X', 0))
939         writemap(player->cnum);
940     sect.sct_mines = mines_there;
941     putsect(&sect);
942 }
943
944 int
945 pln_hitchance(struct plnstr *pp, int hardtarget, int type)
946 {
947     struct plchrstr *pcp = plchr + pp->pln_type;
948     double tfact = (double)(pp->pln_tech - pcp->pl_tech) /
949         (pp->pln_tech - pcp->pl_tech / 2);
950     int acc = pln_acc(pp);
951     int hitchance;
952
953     if (type == EF_SHIP) {
954         if (pcp->pl_flags & P_A)
955             acc -= 20;
956         if (!(pcp->pl_flags & P_T))
957             acc += 35;
958     }
959     hitchance = (int)(pp->pln_effic * (1.0 - 0.1 * tfact) *
960                       (1.0 - acc / 100.0)) - hardtarget;
961
962     /* smooth out the bottom of the graph with asymtote at 5 -KHS */
963     if (hitchance < 20)
964         hitchance = 5 + ldround(300.0 / (40.0 - hitchance), 1);
965     if (hitchance > 100)
966         hitchance = 100;
967     return hitchance;
968 }
969
970 /* return 0 if there was a nuclear detonation */
971
972 int
973 pln_damage(struct plnstr *pp, coord x, coord y, char type, int *nukedamp,
974            int noisy)
975 {
976     struct nukstr nuke;
977     struct plchrstr *pcp = plchr + pp->pln_type;
978     int load, i;
979     int hitroll;
980     int dam = 0;
981     int aim;
982     int effective = 1;
983     int pinbomber = 0;
984
985     if (getnuke(nuk_on_plane(pp), &nuke)) {
986         mpr(pp->pln_own, "Releasing RV's for %s detonation...\n",
987             pp->pln_flags & PLN_AIRBURST ? "airburst" : "groundburst");
988         *nukedamp = detonate(&nuke, x, y,
989                              pp->pln_flags & PLN_AIRBURST);
990         return 0;
991     }
992     *nukedamp = 0;
993
994     load = pln_load(pp);
995     if (!load)                 /* e.g. ab, blowing up on launch pad */
996         return 0;
997
998     i = roll(load) + 1;
999     if (i > load)
1000         i = load;
1001
1002     if (pcp->pl_flags & P_M) {
1003         if (pcp->pl_flags & P_MAR)
1004             pinbomber = 1;
1005     } else if (pcp->pl_flags & P_T)
1006         pinbomber = 1;
1007
1008     aim = pln_acc(pp);
1009     if (type == 's') {
1010         effective = !pinbomber;
1011         aim = 30 + (pinbomber ? aim : 100 - aim);
1012     } else {
1013         effective = pinbomber;
1014         aim = 100 - aim;
1015     }
1016
1017     while (i--) {
1018         dam += roll(6);
1019         hitroll = roll(100);
1020         if (hitroll >= 90) {
1021             dam += 8;
1022             if (noisy)
1023                 mpr(pp->pln_own, "BLAM");
1024         } else if (hitroll < aim) {
1025             dam += 5;
1026             if (noisy)
1027                 mpr(pp->pln_own, "Blam");
1028         } else {
1029             dam += 1;
1030             if (noisy)
1031                 mpr(pp->pln_own, "blam");
1032         }
1033         if (i && noisy)
1034             mpr(pp->pln_own, "-");
1035     }
1036     if (noisy)
1037         mpr(pp->pln_own, "\n");
1038     if (effective)
1039         dam *= 2;
1040     return dam;
1041 }
1042
1043 int
1044 pln_identchance(struct plnstr *pp, int hardtarget, int type)
1045 {
1046     double misschance =
1047         (100.0 - pln_hitchance(pp, hardtarget, type)) / 100.0;
1048     return (int)(100 - 100 * misschance * misschance);
1049 }
1050
1051 int
1052 pln_mobcost(int dist, struct plnstr *pp, char mission)
1053 {
1054     double cost;
1055
1056     cost = 20.0 / (pp->pln_effic / 100.0);
1057     if (mission == 'e' || mission == 0)
1058         cost /= 2;              /* escort or intercept */
1059
1060     return ldround(cost * dist / pln_range_max(pp) + 5, 1);
1061 }
1062
1063 int
1064 pln_is_in_orbit(struct plnstr *pp)
1065 {
1066     return (plchr[pp->pln_type].pl_flags & (P_M | P_O)) == P_O
1067         && (pp->pln_flags & PLN_LAUNCHED);
1068 }
1069
1070 /*
1071  * Set PP's tech to TLEV along with everything else that depends on it.
1072  */
1073 void
1074 pln_set_tech(struct plnstr *pp, int tlev)
1075 {
1076     struct plchrstr *pcp = plchr + pp->pln_type;
1077     int limited_range = pp->pln_range < pln_range_max(pp);
1078     int range_max;
1079
1080     if (CANT_HAPPEN(tlev < pcp->pl_tech))
1081         tlev = pcp->pl_tech;
1082     pp->pln_tech = tlev;
1083
1084     range_max = pln_range_max(pp);
1085     if (!limited_range || pp->pln_range > range_max)
1086         pp->pln_range = range_max;
1087 }