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