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