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