]> git.pond.sub.org Git - empserver/blob - src/lib/subs/plnsub.c
Update copyright notice
[empserver] / src / lib / subs / plnsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2012, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  plnsub.c: Plane subroutine stuff
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Ken Stevens, 1995
32  *     Steve McClure, 1998-2000
33  *     Markus Armbruster, 2004-2010
34  */
35
36 #include <config.h>
37
38 #include "file.h"
39 #include "item.h"
40 #include "land.h"
41 #include "map.h"
42 #include "misc.h"
43 #include "nat.h"
44 #include "nsc.h"
45 #include "nuke.h"
46 #include "optlist.h"
47 #include "plane.h"
48 #include "player.h"
49 #include "prototypes.h"
50 #include "sect.h"
51 #include "ship.h"
52 #include "xy.h"
53
54 static int fit_plane_on_ship(struct plnstr *, struct shpstr *);
55
56 /*
57  * Get planes and escorts argument.
58  * Read planes into *NI_BOMB, and (optional) escorts into *NI_ESC.
59  * If INPUT_BOMB is not empty, use it, else prompt for more input.
60  * Same for INPUT_ESC.
61  * If we got a plane argument, initialize *NI_BOMB and *NI_ESC, and
62  * return 0.
63  * Else return -1 (*NI_BOMB and *NI_ESC may be modified).
64  */
65 int
66 get_planes(struct nstr_item *ni_bomb, struct nstr_item *ni_esc,
67            char *input_bomb, char *input_esc)
68 {
69     if (!snxtitem(ni_bomb, EF_PLANE, input_bomb, NULL))
70         return -1;
71     if (!snxtitem(ni_esc, EF_PLANE, input_esc, "escort(s)? ")) {
72         if (player->aborted)
73             return -1;
74         pr("No escorts...\n");
75     }
76     return 0;
77 }
78
79 /*
80  * Get assembly point argument.
81  * If INPUT is not empty, use it, else prompt for more input using PROMPT.
82  * If this yields a valid assembly point, read it into *AP_SECT and
83  * return AP_SECT.
84  * Else complain and return NULL.
85  * *AP_SECT and BUF[1024] may be modified in either case.
86  */
87 struct sctstr *
88 get_assembly_point(char *input, struct sctstr *ap_sect, char *buf)
89 {
90     char *p;
91     coord x, y;
92     struct nstr_item ni;
93     struct shpstr ship;
94
95     p = getstarg(input, "assembly point? ", buf);
96     if (!p || *p == 0)
97         return NULL;
98     if (!sarg_xy(p, &x, &y) || !getsect(x, y, ap_sect))
99         return NULL;
100
101     /* over own or allied sector is fine */
102     if (relations_with(ap_sect->sct_own, player->cnum) == ALLIED)
103         return ap_sect;
104
105     /* over own or allied ship is fine */
106     snxtitem_xy(&ni, EF_SHIP, x, y);
107     while (nxtitem(&ni, &ship)) {
108         if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
109             continue;
110         if (relations_with(ship.shp_own, player->cnum) == ALLIED)
111             return ap_sect;
112     }
113
114     pr("Assembly point not owned by you or an ally!\n");
115     return NULL;
116 }
117
118 int
119 pln_onewaymission(struct sctstr *target, int *shipno, int *flagp)
120 {
121     int nships;
122     int cno;
123     int flags, fl;
124     struct shpstr ship;
125     char buf[1024];
126     char *p;
127
128     flags = *flagp;
129
130     /* offer carriers */
131     nships = carriersatxy(target->sct_x, target->sct_y, player->cnum);
132     if (nships) {
133         for (;;) {
134             p = getstring("Carrier #? ", buf);
135             if (!p)
136                 return -1;
137             if (!*p)
138                 break;
139             cno = atoi(p);
140             if (cno < 0
141                 || !getship(cno, &ship)
142                 || (!player->owner
143                     && (relations_with(ship.shp_own, player->cnum)
144                         != ALLIED))) {
145                 pr("Not yours\n");
146                 continue;
147             }
148             if (ship.shp_x != target->sct_x || ship.shp_y != target->sct_y) {
149                 pr("Ship #%d not in %s\n", cno,
150                    xyas(target->sct_x, target->sct_y, player->cnum));
151                 continue;
152             }
153             fl = carrier_planes(&ship, 0);
154             if (fl == 0) {
155                 pr("Can't land on %s.\n", prship(&ship));
156                 continue;
157             }
158             /* clear to land on ship#CNO */
159             pr("landing on carrier %d\n", cno);
160             flags |= fl;
161             *shipno = cno;
162             *flagp = flags;
163             return 0;
164         }
165     }
166
167     /* try to land at sector */
168     if (relations_with(target->sct_own, player->cnum) != ALLIED) {
169         pr("Nowhere to land at sector %s!\n",
170            xyas(target->sct_x, target->sct_y, player->cnum));
171         return -1;
172     }
173     if (target->sct_type == SCT_MOUNT) {
174         pr("Nowhere to land at sector %s!\n",
175            xyas(target->sct_x, target->sct_y, player->cnum));
176         return -1;
177     }
178     if (target->sct_type != SCT_AIRPT || target->sct_effic < 60)
179         flags |= P_V;
180
181     /* clear to land at sector */
182     *shipno = -1;
183     *flagp = flags;
184     return 0;
185 }
186
187 int
188 pln_oneway_to_carrier_ok(struct emp_qelem *bomb_list,
189                          struct emp_qelem *esc_list, int cno)
190 {
191     struct emp_qelem *list, *qp;
192     struct plist *plp;
193     struct shpstr ship;
194
195     if (cno < 0 || !getship(cno, &ship))
196         return 0;
197
198     /* for both lists */
199     for (list = bomb_list;
200          list;
201          list = list == bomb_list ? esc_list : NULL) {
202         for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
203             plp = (struct plist *)qp;
204             if (plp->plane.pln_ship == ship.shp_uid)
205                 continue;
206             if (!fit_plane_on_ship(&plp->plane, &ship))
207                 return 0;
208         }
209     }
210     return 1;
211 }
212
213 void
214 pln_newlanding(struct emp_qelem *list, coord tx, coord ty, int cno)
215 {
216     struct emp_qelem *qp;
217     struct plist *plp;
218     struct shpstr ship;
219     struct sctstr sect;
220
221     if (cno >= 0)
222         getship(cno, &ship);
223     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
224         plp = (struct plist *)qp;
225         if (cno >= 0) {
226             if (!could_be_on_ship(&plp->plane, &ship, 0, 0, 0, 0))
227                 pr("\t%s cannot land on ship #%d! %s aborts!\n",
228                    prplane(&plp->plane), cno, prplane(&plp->plane));
229             else if (!put_plane_on_ship(&plp->plane, &ship))
230                 pr("\tNo room on ship #%d! %s aborts!\n",
231                    cno, prplane(&plp->plane));
232             else {
233                 if (plp->plane.pln_own != ship.shp_own) {
234                     wu(0, ship.shp_own, "%s %s lands on your %s\n",
235                        cname(player->cnum), prplane(&plp->plane),
236                        prship(&ship));
237                 }
238             }
239         } else {
240             plp->plane.pln_x = tx;
241             plp->plane.pln_y = ty;
242             getsect(tx, ty, &sect);
243             if (plp->plane.pln_own != sect.sct_own) {
244                 wu(0, sect.sct_own,
245                    "%s %s lands at your sector %s\n",
246                    cname(player->cnum),
247                    prplane(&plp->plane), xyas(tx, ty, sect.sct_own));
248             }
249             plp->plane.pln_ship = cno;
250         }
251     }
252 }
253
254 void
255 pln_dropoff(struct emp_qelem *list, struct ichrstr *ip, coord tx, coord ty,
256             int cno)
257 {
258     struct emp_qelem *qp;
259     struct plist *plp;
260     int amt;
261     struct sctstr sect;
262     struct shpstr ship;
263     int there;
264     int max;
265
266     if (!ip)
267         return;
268     amt = 0;
269     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
270         plp = (struct plist *)qp;
271         amt += plp->load;
272     }
273     if (cno < 0) {
274         getsect(tx, ty, &sect);
275         if (!sect.sct_own) {
276             if (sect.sct_type == SCT_WATER)
277                 pr("Your %s sink like a rock!\n", ip->i_name);
278             else
279                 pr("Your %s vanish without a trace.\n", ip->i_name);
280             return;
281         }
282         if (relations_with(sect.sct_own, player->cnum) != ALLIED) {
283             pr("You don't own %s!  Cargo jettisoned...\n",
284                xyas(tx, ty, player->cnum));
285             return;
286         }
287         if (ip->i_uid == I_CIVIL && sect.sct_own != sect.sct_oldown) {
288             pr("%s is occupied.  Your civilians suffer from identity crisis and die.\n",
289                xyas(tx, ty, player->cnum));
290             return;
291         }
292         there = sect.sct_item[ip->i_uid];
293         max = ITEM_MAX;
294     } else {
295         getship(cno, &ship);
296         there = ship.shp_item[ip->i_uid];
297         max = mchr[ship.shp_type].m_item[ip->i_uid];
298     }
299     there += amt;
300     if (there > max) {
301         pr("%d excess %s discarded\n", there - max, ip->i_name);
302         amt -= there - max;
303         there = max;
304     }
305     pr("%d %s landed safely", amt, ip->i_name);
306     if (cno < 0) {
307         sect.sct_item[ip->i_uid] = there;
308         if (sect.sct_own != player->cnum)
309             wu(0, sect.sct_own, "%s planes drop %d %s in %s\n",
310                cname(player->cnum), amt, ip->i_name,
311                xyas(tx, ty, sect.sct_own));
312         pr(" at %s\n", xyas(tx, ty, player->cnum));
313         putsect(&sect);
314     } else {
315         ship.shp_item[ip->i_uid] = there;
316         if (ship.shp_own != player->cnum)
317             wu(0, ship.shp_own, "%s planes land %d %s on carrier %d\n",
318                cname(player->cnum), amt, ip->i_name, ship.shp_uid);
319         pr(" on carrier #%d\n", ship.shp_uid);
320         putship(ship.shp_uid, &ship);
321     }
322 }
323
324 void
325 pln_mine(struct emp_qelem *list, coord tx, coord ty)
326 {
327     struct emp_qelem *qp;
328     struct plist *plp;
329     int amt;
330     struct sctstr sect;
331
332     amt = 0;
333     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
334         plp = (struct plist *)qp;
335         amt += plp->load;
336
337     }
338     if (amt > 0) {
339         getsect(tx, ty, &sect);
340         if (sect.sct_type != SCT_WATER) {
341             pr("Your seamines have no effect here.\n");
342             return;
343         }
344         sect.sct_mines = MIN(sect.sct_mines + amt, MINES_MAX);
345         pr("%d mines laid in %s.\n", amt, xyas(tx, ty, player->cnum));
346         if (map_set(player->cnum, tx, ty, 'X', 0))
347             writemap(player->cnum);
348         putsect(&sect);
349     }
350 }
351
352 /*
353  * Has PP's type capabilities satisfying WANTFLAGS and NOWANTFLAGS?
354  * A plane type is capable unless
355  * - it lacks all of the P_B, P_T in WANTFLAGS, or
356  * - it lacks all of the P_F, P_ESC in WANTFLAGS, or
357  * - it lacks all of the P_E, P_L, P_K in WANTFLAGS, or
358  * - it lacks any of the other capabilities in WANTFLAGS, or
359  * - it has any of the capabilities in NOWANTFLAGS.
360  */
361 int
362 pln_capable(struct plnstr *pp, int wantflags, int nowantflags)
363 {
364     int flags = plchr[(int)pp->pln_type].pl_flags;
365
366     if (wantflags & (P_B | P_T)) {
367         if ((flags & wantflags & (P_B | P_T)) == 0)
368             return 0;
369         wantflags &= ~(P_B | P_T);
370     }
371
372     if (wantflags & (P_F | P_ESC)) {
373         if ((flags & wantflags & (P_F | P_ESC)) == 0)
374             return 0;
375         wantflags &= ~(P_F | P_ESC);
376     }
377
378     if (wantflags & (P_E | P_L | P_K)) {
379         if ((flags & wantflags & (P_E | P_L | P_K)) == 0)
380             return 0;
381         wantflags &= ~(P_E | P_L | P_K);
382     }
383
384     if ((flags & wantflags) != wantflags)
385         return 0;
386
387     if (flags & nowantflags)
388         return 0;
389
390     return 1;
391 }
392
393 /*
394  * Return union of capabilities of planes in LIST.
395  */
396 int
397 pln_caps(struct emp_qelem *list)
398 {
399     struct emp_qelem *qp;
400     struct plist *plp;
401     int fl;
402
403     fl = 0;
404     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
405         plp = (struct plist *)qp;
406         fl |= plp->pcp->pl_flags;
407     }
408
409     return fl;
410 }
411
412 /*
413  * Find plane types that can operate from carrier SP.
414  * If MSL find missile types, else non-missile types.
415  * Return a combination of P_L, P_K, P_E.
416  * It's zero if SP can't support air operations due to its type or
417  * state (low efficiency).
418  */
419 int
420 carrier_planes(struct shpstr *sp, int msl)
421 {
422     struct mchrstr *mcp = mchr + sp->shp_type;
423     int res;
424
425     if (sp->shp_effic < SHP_AIROPS_EFF)
426         return 0;
427
428     res = 0;
429     if (mcp->m_flags & M_FLY)
430         res |= P_L;
431     if ((mcp->m_flags & M_MSL) && msl)
432         res |= P_L;
433     if (mcp->m_nchoppers && !msl)
434         res |= P_K;
435     if (mcp->m_nxlight)
436         res |= P_E;
437     return res;
438 }
439
440 int
441 pln_airbase_ok(struct plnstr *pp, int oneway, int noisy)
442 {
443     struct shpstr ship;
444     struct lndstr land;
445     struct sctstr sect;
446     struct plchrstr *pcp = plchr + pp->pln_type;
447
448     if (CANT_HAPPEN(noisy && pp->pln_own != player->cnum))
449         noisy = 0;
450
451     if (pp->pln_ship >= 0) {
452         /* ship: needs to be own or allied, efficient */
453         if (!getship(pp->pln_ship, &ship)) {
454             CANT_REACH();
455             return 0;
456         }
457         if (relations_with(ship.shp_own, pp->pln_own) != ALLIED) {
458             if (noisy)
459                 pr("(note) An ally does not own the ship %s is on\n",
460                    prplane(pp));
461             return 0;
462         }
463         if (!(carrier_planes(&ship, pcp->pl_flags & P_M) & pcp->pl_flags))
464             return 0;
465
466     } else if (pp->pln_land >= 0) {
467         /* land: needs to be own or allied, efficient, not embarked */
468         if (!getland(pp->pln_land, &land)) {
469             CANT_REACH();
470             return 0;
471         }
472         if (relations_with(land.lnd_own, pp->pln_own) != ALLIED) {
473             if (noisy)
474                 pr("(note) An ally does not own the unit %s is on\n",
475                    prplane(pp));
476             return 0;
477         }
478         if (land.lnd_effic < LND_AIROPS_EFF || !(pcp->pl_flags & P_E))
479             return 0;
480         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
481             return 0;
482
483     } else {
484         /* sector: needs to be own or allied, efficient airfield */
485         if (!getsect(pp->pln_x, pp->pln_y, &sect)) {
486             CANT_REACH();
487             return 0;
488         }
489
490         if (relations_with(sect.sct_own, pp->pln_own) != ALLIED) {
491             if (noisy)
492                 pr("(note) An ally does not own the sector %s is in\n",
493                    prplane(pp));
494             return 0;
495         }
496         /* need airfield unless VTOL */
497         if ((pcp->pl_flags & P_V) == 0) {
498             if (sect.sct_type != SCT_AIRPT) {
499                 if (noisy)
500                     pr("%s not at airport\n", prplane(pp));
501                 return 0;
502             }
503             if (sect.sct_effic < 40) {
504                 if (noisy)
505                     pr("%s is not 40%% efficient, %s can't take off from there.\n",
506                        xyas(sect.sct_x, sect.sct_y, player->cnum),
507                        prplane(pp));
508                 return 0;
509             }
510             if (!oneway && sect.sct_effic < 60) {
511                 if (noisy)
512                     pr("%s is not 60%% efficient, %s can't land there.\n",
513                        xyas(sect.sct_x, sect.sct_y, player->cnum),
514                        prplane(pp));
515                 return 0;
516             }
517         }
518     }
519
520     return 1;
521 }
522
523 void
524 pln_sel(struct nstr_item *ni, struct emp_qelem *list, struct sctstr *ap,
525         int ap_to_target, int rangemult, int wantflags, int nowantflags)
526 {
527     struct plnstr plane;
528     int range;
529     struct plchrstr *pcp;
530     struct plist *plp;
531
532     emp_initque(list);
533     while (nxtitem(ni, &plane)) {
534         /*
535          * It would be nice to let deities fly foreign planes, but
536          * much of the code assumes that only the plane's owner can
537          * fly it.
538          */
539         if (!plane.pln_own || plane.pln_own != player->cnum)
540             continue;
541         if (plane.pln_mobil <= 0)
542             continue;
543         if (plane.pln_effic < 40) {
544             pr("%s not efficient enough (must be 40%%)\n",
545                prplane(&plane));
546             continue;
547         }
548         if (!pln_capable(&plane, wantflags, nowantflags))
549             continue;
550         if (opt_MARKET) {
551             if (ontradingblock(EF_PLANE, &plane)) {
552                 pr("plane #%d inelligible - it's for sale.\n",
553                    plane.pln_uid);
554                 continue;
555             }
556         }
557
558         range = mapdist(plane.pln_x, plane.pln_y, ap->sct_x, ap->sct_y);
559         if (range > 4) {
560             pr("%s too far from assembly point\n", prplane(&plane));
561             continue;
562         }
563         range += ap_to_target;
564         range *= rangemult;
565         pcp = &plchr[(int)plane.pln_type];
566         if (plane.pln_range < range) {
567             pr("%s out of range (%d:%d)\n",
568                prplane(&plane), plane.pln_range, range);
569             continue;
570         }
571         if (!pln_airbase_ok(&plane, rangemult != 2, 1))
572             continue;
573         pr("%s standing by\n", prplane(&plane));
574         plane.pln_mission = 0;
575         putplane(plane.pln_uid, &plane);
576         plp = malloc(sizeof(struct plist));
577         plp->load = 0;
578         plp->pcp = pcp;
579         plp->plane = plane;
580         emp_insque(&plp->queue, list);
581     }
582 }
583
584 void
585 pln_arm(struct emp_qelem *list, int dist, char mission, struct ichrstr *ip)
586 {
587     struct emp_qelem *qp;
588     struct emp_qelem *next;
589     struct plist *plp;
590     struct plnstr *pp;
591
592     for (qp = list->q_forw; qp != list; qp = next) {
593         next = qp->q_forw;
594         plp = (struct plist *)qp;
595         pp = &plp->plane;
596         getplane(pp->pln_uid, pp);
597         if ((pp->pln_flags & PLN_LAUNCHED)
598             || pln_equip(plp, ip, mission) < 0) {
599             emp_remque(qp);
600             free(qp);
601             continue;
602         }
603         pp->pln_flags |= PLN_LAUNCHED;
604         pp->pln_mobil -= pln_mobcost(dist, pp, mission);
605         putplane(pp->pln_uid, pp);
606         pr("%s equipped\n", prplane(pp));
607     }
608 }
609
610 int
611 pln_equip(struct plist *plp, struct ichrstr *ip, char mission)
612 {
613     struct plchrstr *pcp;
614     struct plnstr *pp;
615     int load, needed;
616     struct lndstr land;
617     struct shpstr ship;
618     struct sctstr sect;
619     i_type itype;
620     short *item;
621     int own;
622     int abandon_needed;
623
624     pp = &plp->plane;
625     pcp = plp->pcp;
626     if (pp->pln_ship >= 0) {
627         getship(pp->pln_ship, &ship);
628         item = ship.shp_item;
629         own = ship.shp_own;
630     } else if (pp->pln_land >= 0) {
631         getland(pp->pln_land, &land);
632         item = land.lnd_item;
633         own = land.lnd_own;
634     } else {
635         getsect(pp->pln_x, pp->pln_y, &sect);
636         item = sect.sct_item;
637         own = sect.sct_oldown;
638     }
639     if (pcp->pl_fuel > item[I_PETROL]) {
640         pr("%s not enough petrol there!\n", prplane(pp));
641         return -1;
642     }
643     item[I_PETROL] -= pcp->pl_fuel;
644     load = pln_load(pp);
645     itype = I_NONE;
646     switch (mission) {
647     case 's':           /* strategic bomb */
648     case 'p':           /* pinpoint bomb */
649         itype = I_SHELL;
650         break;
651     case 't':           /* transport */
652         if (!(pcp->pl_flags & P_C) || !ip)
653             break;
654         itype = ip->i_uid;
655         load *= 2;
656         break;
657     case 'm':           /* mine */
658         if ((pcp->pl_flags & P_MINE) == 0)
659             break;
660         itype = I_SHELL;
661         load *= 2;
662         break;
663     case 'd':           /* drop */
664         if (!(pcp->pl_flags & P_C) || CANT_HAPPEN(!ip))
665             break;
666         itype = ip->i_uid;
667         if (pcp->pl_flags & P_V)
668             load *= 2;
669         break;
670     case 'a':           /* paradrop */
671         if (!(pcp->pl_flags & P_P))
672             break;
673         itype = I_MILIT;
674         if (pcp->pl_flags & P_V)
675             load *= 2;
676         break;
677     case 'r':           /* reconnaissance */
678     case 'e':           /* escort */
679         load = 0;
680         break;
681     case 'i':           /* missile interception */
682         if (CANT_HAPPEN(!(pcp->pl_flags & P_M)
683                         || !(pcp->pl_flags & (P_N | P_O))))
684             break;
685         if (load)
686             itype = I_SHELL;
687         break;
688     default:
689         CANT_REACH();
690         load = 0;
691     }
692
693     if (itype != I_NONE) {
694         needed = load / ichr[itype].i_lbs;
695         if (needed <= 0) {
696             pr("%s can't contribute to mission\n", prplane(pp));
697             return -1;
698         }
699         if (nuk_on_plane(pp) >= 0) {
700             if (mission == 's' || mission == 't')
701                 needed = 0;
702             else {
703                 pr("%s can't fly this mission"
704                    " while it is carrying a nuclear weapon",
705                    prplane(pp));
706                 return -1;
707             }
708         }
709         if (itype == I_CIVIL && pp->pln_own != own) {
710             pr("You don't control those civilians!\n");
711             return -1;
712         }
713 #if 0
714         /* Supply is broken somewhere, so don't use it for now */
715         if (itype == I_SHELL && item[itype] < needed)
716             item[itype] += supply_commod(plp->plane.pln_own,
717                                          plp->plane.pln_x,
718                                          plp->plane.pln_y,
719                                          I_SHELL, needed);
720 #endif
721         if (pp->pln_ship >= 0 || pp->pln_land >= 0)
722             abandon_needed = 0;
723         else
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 }