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