]> git.pond.sub.org Git - empserver/blob - src/lib/subs/plnsub.c
(pln_airbase_ok): Plug information leak: carrier owner was checked
[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 /*
355  * Has PP's type capabilities satisfying WANTFLAGS and NOWANTFLAGS?
356  * A plane type is capable unless
357  * - it lacks all of the P_F, P_ESC in WANTFLAGS, or
358  * - it lacks all of the P_E, P_L, P_K in WANTFLAGS, or
359  * - it lacks any of the other capabilities in WANTFLAGS, or
360  * - it has any of the capabilities in NOWANTFLAGS.
361  */
362 int
363 pln_capable(struct plnstr *pp, int wantflags, int nowantflags)
364 {
365     int flags = plchr[(int)pp->pln_type].pl_flags;
366
367     if (wantflags & (P_F | P_ESC)) {
368         if ((flags & wantflags & (P_F | P_ESC)) == 0)
369             return 0;
370         wantflags &= ~(P_F | P_ESC);
371     }
372
373     if (wantflags & (P_E | P_L | P_K)) {
374         if ((flags & wantflags & (P_E | P_L | P_K)) == 0)
375             return 0;
376         wantflags &= ~(P_E | P_L | P_K);
377     }
378
379     if ((flags & wantflags) != wantflags)
380         return 0;
381
382     if (flags & nowantflags)
383         return 0;
384
385     return 1;
386 }
387
388 static int
389 pln_airbase_ok(struct plnstr *pp, int oneway)
390 {
391     struct shpstr ship;
392     struct lndstr land;
393     struct sctstr sect;
394     struct plchrstr *pcp = plchr + pp->pln_type;
395
396     if (CANT_HAPPEN(pp->pln_own != player->cnum))
397         return 0;
398
399     if (pp->pln_ship >= 0) {
400         if (!getship(pp->pln_ship, &ship)) {
401             CANT_REACH();
402             return 0;
403         }
404         if (CANT_HAPPEN(ship.shp_effic < SHIP_MINEFF
405                         || !could_be_on_ship(pp, &ship)))
406             return 0;
407
408         if ((ship.shp_own != player->cnum) &&
409             (getrel(getnatp(ship.shp_own), player->cnum) != ALLIED)) {
410             pr("(note) An ally does not own the ship %s is on\n",
411                prplane(pp));
412             return 0;
413         }
414         if (ship.shp_effic < SHP_AIROPS_EFF)
415             return 0;
416
417     } else if (pp->pln_land >= 0) {
418         if (!getland(pp->pln_land, &land)) {
419             CANT_REACH();
420             return 0;
421         }
422         if (CANT_HAPPEN(land.lnd_effic < LAND_MINEFF
423                         || !(pcp->pl_flags & P_E)))
424             return 0;
425
426         if ((land.lnd_own != player->cnum) &&
427             (getrel(getnatp(land.lnd_own), player->cnum) != ALLIED)) {
428             pr("(note) An ally does not own the unit %s is on\n",
429                prplane(pp));
430             return 0;
431         }
432         if (land.lnd_effic < LND_AIROPS_EFF)
433             return 0;
434         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
435             return 0;
436
437     } else {
438         if (!getsect(pp->pln_x, pp->pln_y, &sect)) {
439             CANT_REACH();
440             return 0;
441         }
442         /* First, check allied status */
443         /* Can't fly from non-owned sectors or non-allied sectors */
444         if ((sect.sct_own != player->cnum) &&
445             (getrel(getnatp(sect.sct_own), player->cnum) != ALLIED)) {
446             pr("(note) An ally does not own the sector %s is in\n",
447                prplane(pp));
448             return 0;
449         }
450         /* non-vtol plane */
451         if ((pcp->pl_flags & P_V) == 0) {
452             if (sect.sct_type != SCT_AIRPT) {
453                 pr("%s not at airport\n", prplane(pp));
454                 return 0;
455             }
456             if (sect.sct_effic < 40) {
457                 pr("%s is not 40%% efficient, %s can't take off from there.\n", xyas(sect.sct_x, sect.sct_y, pp->pln_own), prplane(pp));
458                 return 0;
459             }
460             if (!oneway && sect.sct_effic < 60) {
461                 pr("%s is not 60%% efficient, %s can't land there.\n",
462                    xyas(sect.sct_x, sect.sct_y, pp->pln_own),
463                    prplane(pp));
464                 return 0;
465             }
466         }
467     }
468
469     return 1;
470 }
471
472 void
473 pln_sel(struct nstr_item *ni, struct emp_qelem *list, struct sctstr *ap,
474         int ap_to_target, int rangemult, int wantflags, int nowantflags)
475 {
476     struct plnstr plane;
477     int range;
478     struct plchrstr *pcp;
479     struct plist *plp;
480     int y;
481     int bad, bad1;
482     unsigned x;
483
484     emp_initque(list);
485     while (nxtitem(ni, &plane)) {
486         if (!player->owner)
487             continue;
488         if (plane.pln_mobil <= 0)
489             continue;
490         if (opt_MARKET) {
491             if (ontradingblock(EF_PLANE, &plane)) {
492                 pr("plane #%d inelligible - it's for sale.\n",
493                    plane.pln_uid);
494                 continue;
495             }
496         }
497
498         range = mapdist(plane.pln_x, plane.pln_y, ap->sct_x, ap->sct_y);
499         if (range > 4) {
500             pr("%s too far from assembly point\n", prplane(&plane));
501             continue;
502         }
503         if (plane.pln_effic < 40) {
504             pr("%s not efficient enough (must be 40%%)\n",
505                prplane(&plane));
506             continue;
507         }
508         range += ap_to_target;
509         range *= rangemult;
510         pcp = &plchr[(int)plane.pln_type];
511         if (!pln_capable(&plane, wantflags, nowantflags))
512             continue;
513         if (plane.pln_range < range) {
514             pr("%s out of range (%d:%d)\n",
515                prplane(&plane), plane.pln_range, range);
516             continue;
517         }
518         if (!pln_airbase_ok(&plane, rangemult != 2))
519             continue;
520         pr("%s standing by\n", prplane(&plane));
521         plane.pln_mission = 0;
522         putplane(plane.pln_uid, &plane);
523         plp = malloc(sizeof(struct plist));
524         plp->misc = 0;
525         plp->bombs = 0;
526         plp->pcp = pcp;
527         plp->plane = plane;
528         emp_insque(&plp->queue, list);
529     }
530 }
531
532 int
533 pln_arm(struct emp_qelem *list, int dist, char mission, struct ichrstr *ip,
534         int flags, int mission_flags)
535 {
536     struct emp_qelem *qp;
537     struct emp_qelem *next;
538     struct plist *plp;
539
540     for (qp = list->q_forw; qp != list; qp = next) {
541         next = qp->q_forw;
542         plp = (struct plist *)qp;
543         if (pln_equip(plp, ip, flags, mission) < 0) {
544             emp_remque(qp);
545             free(qp);
546             continue;
547         }
548         if (flags & (P_S | P_I)) {
549             if (plp->pcp->pl_flags & P_S)
550                 mission_flags |= P_S;
551             if (plp->pcp->pl_flags & P_I)
552                 mission_flags |= P_I;
553         }
554         if (!(plp->pcp->pl_flags & P_H))
555             /* no stealth on this mission */
556             mission_flags &= ~P_H;
557         if (!(plp->pcp->pl_flags & P_X))
558             /* no stealth on this mission */
559             mission_flags &= ~P_X;
560         if (!(plp->pcp->pl_flags & P_A)) {
561             /* no asw on this mission */
562             mission_flags &= ~P_A;
563             /* FIXME escorts turn ASW patrol into ordinary recon */
564         }
565         if (!(plp->pcp->pl_flags & P_MINE)) {
566             /* no asw on this mission */
567             mission_flags &= ~P_MINE;
568             /* FIXME no effect */
569         }
570         plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
571         pr("%s equipped\n", prplane(&plp->plane));
572     }
573     return mission_flags;
574 }
575
576 static int
577 pln_equip(struct plist *plp, struct ichrstr *ip, int flags, char mission)
578 {
579     struct plchrstr *pcp;
580     struct plnstr *pp;
581     int needed;
582     struct lndstr land;
583     struct shpstr ship;
584     struct sctstr sect;
585     i_type itype;
586     int rval;
587     short *item;
588     int own;
589     int abandon_needed;
590
591     pp = &plp->plane;
592     pcp = plp->pcp;
593     if (pp->pln_ship >= 0) {
594         getship(pp->pln_ship, &ship);
595         item = ship.shp_item;
596         own = ship.shp_own;
597     } else if (pp->pln_land >= 0) {
598         getland(pp->pln_land, &land);
599         item = land.lnd_item;
600         own = land.lnd_own;
601     } else {
602         getsect(pp->pln_x, pp->pln_y, &sect);
603         item = sect.sct_item;
604         own = sect.sct_oldown;
605     }
606     if (ip) {
607         if (ip->i_uid == I_CIVIL) {
608             if (pp->pln_own != own) {
609                 pr("You don't control those civilians!\n");
610                 return -1;
611             }
612         }
613     }
614     if (pcp->pl_fuel > item[I_PETROL]) {
615         pr("%s not enough petrol there!\n", prplane(pp));
616         return -1;
617     }
618     item[I_PETROL] -= pcp->pl_fuel;
619     rval = 0;
620     if ((flags & P_F) == 0) {
621         itype = I_NONE;
622         needed = 0;
623         switch (mission) {
624         case 's':
625         case 'p':
626             if (pp->pln_nuketype == -1) {
627                 itype = I_SHELL;
628                 needed = pp->pln_load;
629             }
630             break;
631         case 't':
632         case 'd':
633             if ((pcp->pl_flags & P_C) == 0 || ip == 0)
634                 break;
635             itype = ip->i_uid;
636             needed = (pp->pln_load * 2) / ip->i_lbs;
637             break;
638         case 'm':
639             if ((pcp->pl_flags & P_MINE) == 0)
640                 break;
641             itype = I_SHELL;
642             needed = (pp->pln_load * 2) / ip->i_lbs;
643             break;
644         case 'a':
645             if ((pcp->pl_flags & (P_V | P_C)) == 0)
646                 break;
647             itype = I_MILIT;
648             needed = pp->pln_load / ip->i_lbs;
649             break;
650         case 'n':
651             if (pp->pln_nuketype == -1)
652                 rval = -1;
653             break;
654         default:
655             break;
656         }
657         if (rval < 0 || (itype != I_NONE && needed <= 0)) {
658             pr("%s can't contribute to mission\n", prplane(pp));
659             return -1;
660         }
661         if (itype != I_NONE) {
662 #if 0
663             /* Supply is broken somewhere, so don't use it for now */
664             if (itype == I_SHELL && item[itype] < needed)
665                 item[itype] += supply_commod(plp->plane.pln_own,
666                                              plp->plane.pln_x,
667                                              plp->plane.pln_y,
668                                              I_SHELL, needed);
669 #endif
670             abandon_needed = !!would_abandon(&sect, itype, needed, NULL);
671             if (item[itype] < needed + abandon_needed) {
672                 pr("Not enough %s for %s\n", ichr[itype].i_name, prplane(pp));
673                 return -1;
674             }
675             item[itype] -= needed;
676         }
677         if (itype == I_SHELL && (mission == 's' || mission == 'p'))
678             plp->bombs = needed;
679         else
680             plp->misc = needed;
681     }
682     if (pp->pln_ship >= 0) {
683         if (pp->pln_own != ship.shp_own) {
684             wu(0, ship.shp_own,
685                "%s %s prepares for takeoff from ship %s\n",
686                cname(pp->pln_own), prplane(pp), prship(&ship));
687         }
688         putship(ship.shp_uid, &ship);
689     } else if (pp->pln_land >= 0) {
690         if (pp->pln_own != land.lnd_own) {
691             wu(0, land.lnd_own,
692                "%s %s prepares for takeoff from unit %s\n",
693                cname(pp->pln_own), prplane(pp), prland(&land));
694         }
695         putland(land.lnd_uid, &land);
696     } else {
697         if (pp->pln_own != sect.sct_own) {
698             wu(0, sect.sct_own, "%s %s prepares for takeoff from %s\n",
699                cname(pp->pln_own), prplane(pp),
700                xyas(sect.sct_x, sect.sct_y, sect.sct_own));
701         }
702         putsect(&sect);
703     }
704     return rval;
705 }
706
707 void
708 pln_put(struct emp_qelem *list)
709 {
710     struct emp_qelem *qp;
711     struct emp_qelem *newqp;
712     struct plist *plp;
713     struct plnstr *pp;
714     struct shpstr ship;
715     struct sctstr sect;
716
717     /* Here is where planes return home from bombing runs.
718        We need to make sure they still have somewhere to return
719        home to! */
720     qp = list->q_forw;
721     while (qp != list) {
722         plp = (struct plist *)qp;
723         pp = &plp->plane;
724         /* Ok, check out where it wants to land */
725         if (pp->pln_ship >= 0) {
726             /* It is landing on a carrier */
727             getship(pp->pln_ship, &ship);
728             /* We should do more, like make sure it's really
729                a carrier, etc. but for now just make sure it's
730                not sunk. */
731             if (ship.shp_effic < SHIP_MINEFF) {
732                 mpr(pp->pln_own,
733                     "Ship #%d has been sunk, plane #%d has nowhere to land, and\n"
734                     "splashes into the sea.\n",
735                     pp->pln_ship, pp->pln_uid);
736                 pp->pln_effic = 0;
737             }
738         } else {
739             /* Presume we are landing back in a sector. */
740             getsect(pp->pln_x, pp->pln_y, &sect);
741             if (sect.sct_type == SCT_WATER || sect.sct_type == SCT_WASTE) {
742                 mpr(pp->pln_own,
743                     "Nowhere to land at %s, plane #%d crashes and burns...\n",
744                     xyas(pp->pln_x, pp->pln_y, pp->pln_own), pp->pln_uid);
745                 pp->pln_effic = 0;
746             }
747         }
748         putplane(pp->pln_uid, pp);
749         newqp = qp->q_forw;
750         emp_remque(qp);
751         free(qp);
752         qp = newqp;
753     }
754 }
755
756 void
757 pln_removedupes(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
758 {
759     struct emp_qelem *bomb;
760     struct emp_qelem *esc;
761     struct plist *bombp;
762     struct plist *escp;
763
764     if (QEMPTY(bomb_list) || QEMPTY(esc_list))
765         return;
766     bomb = bomb_list->q_forw;
767     while (bomb != bomb_list) {
768         if (QEMPTY(esc_list)) {
769             bomb = bomb_list;
770             continue;
771         }
772         esc = esc_list->q_forw;
773         bombp = (struct plist *)bomb;
774         while (esc != esc_list) {
775             escp = (struct plist *)esc;
776             if (escp->plane.pln_uid == bombp->plane.pln_uid) {
777                 emp_remque(esc);
778                 free(esc);
779                 esc = esc_list;
780             } else
781                 esc = esc->q_forw;
782         }
783         bomb = bomb->q_forw;
784     }
785 }
786
787 /*
788  * Fit a plane of PP's type on ship SP.
789  * Adjust SP's plane counters.
790  * Updating the plane accordingly is the caller's job.
791  * Return whether it fits.
792  */
793 static int
794 fit_plane_on_ship(struct plnstr *pp, struct shpstr *sp)
795 {
796     struct plchrstr *pcp = plchr + pp->pln_type;
797     struct mchrstr *mcp = mchr + sp->shp_type;
798     int wanted;
799
800     if (pcp->pl_flags & P_K) {
801         /* chopper, try chopper slot first */
802         if (sp->shp_nchoppers < mcp->m_nchoppers)
803             return ++sp->shp_nchoppers;
804         /* else try plane slot */
805         wanted = M_FLY;
806     } else if (pcp->pl_flags & P_E) {
807         /* x-light, try x-light slot first */
808         if (sp->shp_nxlight < mcp->m_nxlight)
809             return ++sp->shp_nxlight;
810         /* else try plane slot */
811         wanted = M_MSL | M_FLY;
812     } else if (!(pcp->pl_flags & P_L)) {
813         /* not light, no go */
814         wanted = 0;
815     } else if (pcp->pl_flags & P_M) {
816         /* missile, use plane slot */
817         wanted = M_MSL | M_FLY;
818     } else {
819         /* fixed-wing plane, use plane slot */
820         wanted = M_FLY;
821     }
822
823     if ((mcp->m_flags & wanted) == 0)
824         return 0;               /* ship not capable */
825
826     if (sp->shp_nplane < mcp->m_nplanes)
827         return ++sp->shp_nplane;
828
829     return 0;
830 }
831
832 /*
833  * Fit a plane of PP's type off ship SP.
834  * Adjust SP's plane counters, badly.  You need to run count_planes()
835  * before the next fit_plane_on_ship().
836  * Updating the plane accordingly is the caller's job.
837  */
838 static void
839 fit_plane_off_ship(struct plnstr *pp, struct shpstr *sp)
840 {
841     /*
842      * Frees chopper and nxlight slots first, which is why we need to
843      * run count_planes() before fit_plane_on_ship().
844      */
845     struct plchrstr *pcp = plchr + pp->pln_type;
846
847     if (pcp->pl_flags & P_K) {
848         if (sp->shp_nchoppers) {
849             sp->shp_nchoppers--;
850             return;
851         }
852     } else if (pcp->pl_flags & P_E) {
853         if (sp->shp_nxlight) {
854             sp->shp_nxlight--;
855             return;
856         }
857     }
858
859     if (CANT_HAPPEN(sp->shp_nplane == 0))
860         sp->shp_nplane = 1;
861     sp->shp_nplane--;
862 }
863
864 int
865 put_plane_on_ship(struct plnstr *plane, struct shpstr *ship)
866 {
867     if (plane->pln_ship == ship->shp_uid)
868         return 1;               /* Already on ship */
869
870     if (!fit_plane_on_ship(plane, ship))
871         return 0;
872
873     plane->pln_x = ship->shp_x;
874     plane->pln_y = ship->shp_y;
875     plane->pln_ship = ship->shp_uid;
876     putplane(plane->pln_uid, plane);
877     putship(ship->shp_uid, ship);
878     return 1;
879 }
880
881 void
882 take_plane_off_ship(struct plnstr *plane, struct shpstr *ship)
883 {
884     if (CANT_HAPPEN(plane->pln_ship != ship->shp_uid))
885         return;
886
887     fit_plane_off_ship(plane, ship);
888     plane->pln_ship = -1;
889     putship(ship->shp_uid, ship);
890     putplane(plane->pln_uid, plane);
891 }
892
893 /*
894  * Fit a plane of PP's type on land unit LP.
895  * Adjust LP's plane counters.
896  * Updating the plane accordingly is the caller's job.
897  * Return whether it fits.
898  */
899 static int
900 fit_plane_on_land(struct plnstr *pp, struct lndstr *lp)
901 {
902     struct plchrstr *pcp = plchr + pp->pln_type;
903     struct lchrstr *lcp = lchr + lp->lnd_type;
904
905     if ((pcp->pl_flags & P_E) && lp->lnd_nxlight < lcp->l_nxlight)
906         return ++lp->lnd_nxlight;
907
908     return 0;
909 }
910
911 /*
912  * Fit a plane of PP's type off land unit LP.
913  * Adjust LP's plane counters.
914  * Updating the plane accordingly is the caller's job.
915  */
916 static void
917 fit_plane_off_land(struct plnstr *pp, struct lndstr *lp)
918 {
919     struct plchrstr *pcp = plchr + pp->pln_type;
920
921     if (CANT_HAPPEN(lp->lnd_nxlight == 0))
922         lp->lnd_nxlight = 1;
923     lp->lnd_nxlight--;
924 }
925
926 int
927 put_plane_on_land(struct plnstr *plane, struct lndstr *land)
928 {
929     if (plane->pln_land == land->lnd_uid)
930         return 1;               /* Already on unit */
931
932     if (!fit_plane_on_land(plane, land))
933         return 0;
934
935     plane->pln_x = land->lnd_x;
936     plane->pln_y = land->lnd_y;
937     plane->pln_land = land->lnd_uid;
938     putplane(plane->pln_uid, plane);
939     putland(land->lnd_uid, land);
940     return 1;
941 }
942
943 void
944 take_plane_off_land(struct plnstr *plane, struct lndstr *land)
945 {
946     if (CANT_HAPPEN(plane->pln_ship != land->lnd_uid))
947         return;
948
949     fit_plane_off_land(plane, land);
950     plane->pln_land = -1;
951     putland(land->lnd_uid, land);
952     putplane(plane->pln_uid, plane);
953 }
954
955 /*
956  * Could a plane of PP's type be on on ship SP?
957  */
958 int
959 could_be_on_ship(struct plnstr *pp, struct shpstr *sp)
960 {
961     struct shpstr ship;
962
963     ship = *sp;
964     ship.shp_nplane = ship.shp_nchoppers = ship.shp_nxlight = 0;
965     return fit_plane_on_ship(pp, &ship);
966 }
967
968 void
969 plane_sweep(struct emp_qelem *plane_list, coord x, coord y)
970 {
971     struct plnstr *pp;
972     struct plchrstr *pcp;
973     struct emp_qelem *qp;
974     struct emp_qelem *next;
975     struct plist *ip;
976     struct sctstr sect;
977     int mines_there;
978     int found = 0;
979
980     getsect(x, y, &sect);
981     mines_there = sect.sct_mines;
982
983     if (mines_there == 0)
984         return;
985
986     if ((sect.sct_type != SCT_WATER) && (sect.sct_type != SCT_HARBR))
987         return;
988
989     for (qp = plane_list->q_forw; ((qp != plane_list) && (mines_there));
990          qp = next) {
991         next = qp->q_forw;
992         ip = (struct plist *)qp;
993         pp = &ip->plane;
994         pcp = ip->pcp;
995         if (!(pcp->pl_flags & P_SWEEP)) /* if it isn't an sweep plane */
996             continue;
997
998         if (chance((100.0 - pp->pln_acc) / 100.0)) {
999             pr("Sweep! in %s\n",
1000                xyas(sect.sct_x, sect.sct_y, pp->pln_own));
1001             mines_there--;
1002             found = 1;
1003         }
1004     }
1005
1006     if (found && map_set(player->cnum, sect.sct_x, sect.sct_y, 'X', 0))
1007         writemap(player->cnum);
1008     sect.sct_mines = mines_there;
1009     putsect(&sect);
1010 }
1011
1012 void
1013 count_planes(struct shpstr *sp)
1014 {
1015     struct nstr_item ni;
1016     struct plnstr plane;
1017     int nplane, nchoppers, nxlight;
1018
1019     if (sp->shp_effic < SHIP_MINEFF)
1020         return;
1021
1022     nplane = sp->shp_nplane;
1023     sp->shp_nplane = 0;
1024     nchoppers = sp->shp_nchoppers;
1025     sp->shp_nchoppers = 0;
1026     nxlight = sp->shp_nxlight;
1027     sp->shp_nxlight = 0;
1028
1029     snxtitem_xy(&ni, EF_PLANE, sp->shp_x, sp->shp_y);
1030     while (nxtitem(&ni, &plane)) {
1031         if (plane.pln_own == 0)
1032             continue;
1033         if (plane.pln_ship == sp->shp_uid) {
1034             if (!fit_plane_on_ship(&plane, sp))
1035                 CANT_REACH();
1036         }
1037     }
1038
1039     if (nplane != sp->shp_nplane ||
1040         nxlight != sp->shp_nxlight || nchoppers != sp->shp_nchoppers) {
1041         putship(sp->shp_uid, sp);
1042     }
1043 }
1044
1045 void
1046 count_land_planes(struct lndstr *lp)
1047 {
1048     struct nstr_item ni;
1049     struct plnstr plane;
1050     int nplane;
1051
1052     if (lp->lnd_effic < LAND_MINEFF)
1053         return;
1054
1055     nplane = lp->lnd_nxlight;
1056     lp->lnd_nxlight = 0;
1057
1058     snxtitem_all(&ni, EF_PLANE);
1059     while (nxtitem(&ni, &plane)) {
1060         if (plane.pln_own == 0)
1061             continue;
1062         if (plane.pln_land == lp->lnd_uid)
1063             if (!fit_plane_on_land(&plane, lp))
1064                 CANT_REACH();
1065     }
1066
1067     if (lp->lnd_nxlight != nplane)
1068         putland(lp->lnd_uid, lp);
1069 }
1070
1071 int
1072 count_sect_planes(struct sctstr *sp)
1073 {
1074     int count = 0;
1075     struct nstr_item ni;
1076     struct plnstr plane;
1077
1078     snxtitem_all(&ni, EF_PLANE);
1079     while (nxtitem(&ni, &plane)) {
1080         if (!plane.pln_own)
1081             continue;
1082         if (plane.pln_flags & PLN_LAUNCHED)
1083             continue;
1084         if (plane.pln_x == sp->sct_x && plane.pln_y == sp->sct_y)
1085             ++count;
1086     }
1087
1088     return count;
1089 }
1090
1091 int
1092 pln_hitchance(struct plnstr *pp, int hardtarget, int type)
1093 {
1094     struct plchrstr *pcp = plchr + pp->pln_type;
1095     double tfact = (double)(pp->pln_tech - pcp->pl_tech) /
1096         (pp->pln_tech - pcp->pl_tech / 2);
1097     int acc = pp->pln_acc;
1098     int hitchance;
1099
1100     if (type == EF_SHIP) {
1101         if (pcp->pl_flags & P_A)
1102             acc -= 20;
1103         if (!(pcp->pl_flags & P_T))
1104             acc += 35;
1105     }
1106     hitchance = (int)(pp->pln_effic * (1.0 - 0.1 * tfact) *
1107                       (1.0 - acc / 100.0)) - hardtarget;
1108
1109     /* smooth out the bottom of the graph with asymtote at 5 -KHS */
1110     if (hitchance < 20)
1111         hitchance = 5 + ldround(300.0 / (40.0 - hitchance), 1);
1112     if (hitchance > 100)
1113         hitchance = 100;
1114     return hitchance;
1115 }
1116
1117 /* return 0 if there was a nuclear detonation */
1118
1119 int
1120 pln_damage(struct plnstr *pp, coord x, coord y, char type, int *nukedamp,
1121            int noisy)
1122 {
1123     struct nukstr nuke;
1124     struct plchrstr *pcp = plchr + pp->pln_type;
1125     int i;
1126     int hitroll;
1127     int dam = 0;
1128     int aim;
1129     int effective = 1;
1130     int pinbomber = 0;
1131
1132     if (pp->pln_nuketype != -1) {
1133         if (nuk_on_plane(&nuke, pp->pln_uid) >= 0) {
1134             mpr(pp->pln_own, "Releasing RV's for %s detonation...\n",
1135                 pp->pln_flags & PLN_AIRBURST ? "airburst" : "groundburst");
1136             pp->pln_nuketype = -1;
1137             *nukedamp = detonate(&nuke, x, y,
1138                                  pp->pln_flags & PLN_AIRBURST);
1139             return 0;
1140         }
1141         CANT_REACH();
1142     }
1143     *nukedamp = 0;
1144
1145     if (!pp->pln_load)          /* e.g. ab, blowing up on launch pad */
1146         return 0;
1147
1148     i = roll(pp->pln_load) + 1;
1149     if (i > pp->pln_load)
1150         i = pp->pln_load;
1151
1152     if (pcp->pl_flags & P_M) {
1153         if (pcp->pl_flags & P_MAR)
1154             pinbomber = 1;
1155     } else if (pcp->pl_flags & P_T)
1156         pinbomber = 1;
1157
1158     aim = 100 - pp->pln_acc;
1159     if (type == 's') {
1160         if (pinbomber) {
1161             aim = pp->pln_acc;
1162             effective = 0;
1163         }
1164         aim += 30;
1165     } else {
1166         if (!pinbomber) {
1167             effective = 0;
1168         }
1169     }
1170     while (i--) {
1171         dam += roll(6);
1172         hitroll = roll(100);
1173         if (hitroll >= 90) {
1174             dam += 8;
1175             if (noisy)
1176                 mpr(pp->pln_own, "BLAM");
1177         } else if (hitroll < aim) {
1178             dam += 5;
1179             if (noisy)
1180                 mpr(pp->pln_own, "Blam");
1181         } else {
1182             dam += 1;
1183             if (noisy)
1184                 mpr(pp->pln_own, "blam");
1185         }
1186         if (i && noisy)
1187             mpr(pp->pln_own, "-");
1188     }
1189     if (noisy)
1190         mpr(pp->pln_own, "\n");
1191     if (effective)
1192         dam *= 2;
1193     return dam;
1194 }
1195
1196 int
1197 pln_identchance(struct plnstr *pp, int hardtarget, int type)
1198 {
1199     double misschance =
1200         (100.0 - pln_hitchance(pp, hardtarget, type)) / 100.0;
1201     return (int)(100 - 100 * misschance * misschance);
1202 }
1203
1204 int
1205 pln_mobcost(int dist, struct plnstr *pp, int flags)
1206 {
1207     double cost;
1208
1209     cost = 20.0 / (pp->pln_effic / 100.0);
1210     if ((flags & P_F) || (flags & P_ESC))
1211         cost /= 2;
1212
1213     return ldround(cost * dist / pp->pln_range_max + 5, 1);
1214 }
1215
1216 /*
1217  * Set PP's tech to TLEV along with everything else that depends on it.
1218  */
1219 void
1220 pln_set_tech(struct plnstr *pp, int tlev)
1221 {
1222     struct plchrstr *pcp = plchr + pp->pln_type;
1223     int tech_diff = tlev - pcp->pl_tech;
1224     int limited_range = pp->pln_range < pp->pln_range_max;
1225
1226     if (CANT_HAPPEN(tech_diff < 0)) {
1227       tlev -= tech_diff;
1228       tech_diff = 0;
1229     }
1230
1231     pp->pln_tech = tlev;
1232     pp->pln_att = PLN_ATTDEF(pcp->pl_att, tech_diff);
1233     pp->pln_def = PLN_ATTDEF(pcp->pl_def, tech_diff);
1234     pp->pln_acc = PLN_ACC(pcp->pl_acc, tech_diff);
1235     pp->pln_range_max = PLN_RAN(pcp->pl_range, tech_diff);
1236     pp->pln_load = PLN_LOAD(pcp->pl_load, tech_diff);
1237
1238     if (!limited_range || pp->pln_range > pp->pln_range_max)
1239         pp->pln_range = pp->pln_range_max;
1240 }