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