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