]> git.pond.sub.org Git - empserver/blob - src/lib/subs/plnsub.c
2532b10ec50fd03cf589c7c5ce4c4cd56d78976b
[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     int abandon_needed;
612
613     pp = &plp->plane;
614     pcp = plp->pcp;
615     if (pp->pln_ship >= 0) {
616         getship(pp->pln_ship, &ship);
617         item = ship.shp_item;
618         own = ship.shp_own;
619     } else if (pp->pln_land >= 0) {
620         getland(pp->pln_land, &land);
621         item = land.lnd_item;
622         own = land.lnd_own;
623     } else {
624         getsect(pp->pln_x, pp->pln_y, &sect);
625         item = sect.sct_item;
626         own = sect.sct_oldown;
627     }
628     if (ip) {
629         if (ip->i_vtype == I_CIVIL) {
630             if (pp->pln_own != own) {
631                 pr("You don't control those civilians!\n");
632                 return -1;
633             }
634         }
635     }
636     if (pcp->pl_fuel > item[I_PETROL]) {
637         pr("%s not enough petrol there!\n", prplane(pp));
638         return -1;
639     }
640     item[I_PETROL] -= pcp->pl_fuel;
641     rval = 0;
642     if ((flags & P_F) == 0) {
643         itype = I_NONE;
644         needed = 0;
645         switch (mission) {
646         case 's':
647         case 'p':
648             if (pp->pln_nuketype == -1) {
649                 itype = I_SHELL;
650                 needed = pp->pln_load;
651             }
652             break;
653         case 't':
654             if ((pcp->pl_flags & P_C) == 0 || ip == 0)
655                 break;
656             itype = ip->i_vtype;
657             needed = (pp->pln_load * 2) / ip->i_lbs;
658             break;
659         case 'd':
660             itype = ip->i_vtype;
661             needed = (pp->pln_load * 2) / ip->i_lbs;
662             /* Is this mine dropping excursion? */
663             if (itype == I_SHELL && (pcp->pl_flags & P_MINE))
664                 break;
665             /* Is this a cargo drop? */
666             if ((pcp->pl_flags & P_C) == 0 || ip == 0) {
667                 itype = 0;
668                 needed = 0;
669                 break;
670             }
671             break;
672         case 'a':
673             if ((pcp->pl_flags & (P_V | P_C)) == 0)
674                 break;
675             itype = I_MILIT;
676             needed = pp->pln_load / ip->i_lbs;
677             break;
678         case 'n':
679             if (pp->pln_nuketype == -1)
680                 rval = -1;
681             break;
682         default:
683             break;
684         }
685         if (rval < 0 || (itype != I_NONE && needed <= 0)) {
686             pr("%s can't contribute to mission\n", prplane(pp));
687             return -1;
688         }
689         if (itype != I_NONE) {
690 #if 0
691             /* Supply is broken somewhere, so don't use it for now */
692             if (itype == I_SHELL && item[itype] < needed)
693                 item[itype] += supply_commod(plp->plane.pln_own,
694                                              plp->plane.pln_x,
695                                              plp->plane.pln_y,
696                                              I_SHELL, needed);
697 #endif
698             abandon_needed = would_abandon(&sect, itype, needed, NULL);
699             if (item[itype] < needed + abandon_needed) {
700                 pr("Not enough %s for %s\n", ichr[itype].i_name, prplane(pp));
701                 return -1;
702             }
703             item[itype] -= needed;
704         }
705         if (itype == I_SHELL && (mission == 's' || mission == 'p'))
706             plp->bombs = needed;
707         else
708             plp->misc = needed;
709     }
710     if (pp->pln_ship >= 0) {
711         if (pp->pln_own != ship.shp_own) {
712             wu(0, ship.shp_own,
713                "%s %s prepares for takeoff from ship %s\n",
714                cname(pp->pln_own), prplane(pp), prship(&ship));
715         }
716         putship(ship.shp_uid, &ship);
717     } else if (pp->pln_land >= 0) {
718         if (pp->pln_own != land.lnd_own) {
719             wu(0, land.lnd_own,
720                "%s %s prepares for takeoff from unit %s\n",
721                cname(pp->pln_own), prplane(pp), prland(&land));
722         }
723         putland(land.lnd_uid, &land);
724     } else {
725         if (pp->pln_own != sect.sct_own) {
726             wu(0, sect.sct_own, "%s %s prepares for takeoff from %s\n",
727                cname(pp->pln_own), prplane(pp), xyas(sect.sct_x,
728                                                      sect.sct_y,
729                                                      sect.sct_own));
730         }
731         putsect(&sect);
732     }
733     return rval;
734 }
735
736 void
737 pln_put(struct emp_qelem *list)
738 {
739     struct emp_qelem *qp;
740     struct emp_qelem *newqp;
741     struct plist *plp;
742     struct plnstr *pp;
743     struct shpstr ship;
744     struct sctstr sect;
745
746     /* Here is where planes return home from bombing runs.
747        We need to make sure they still have somewhere to return
748        home to! */
749     qp = list->q_forw;
750     while (qp != list) {
751         plp = (struct plist *)qp;
752         pp = &plp->plane;
753         /* Ok, check out where it wants to land */
754         if (pp->pln_ship >= 0) {
755             /* It is landing on a carrier */
756             getship(pp->pln_ship, &ship);
757             /* We should do more, like make sure it's really
758                a carrier, etc. but for now just make sure it's
759                not sunk. */
760             if (ship.shp_effic < SHIP_MINEFF) {
761                 mpr(pp->pln_own,
762                     "Ship #%d has been sunk, plane #%d has nowhere to land, and\nsplashes into the sea.\n",
763                     pp->pln_ship, pp->pln_uid);
764                 pp->pln_effic = 0;
765             }
766         } else {
767             /* Presume we are landing back in a sector. */
768             getsect(pp->pln_x, pp->pln_y, &sect);
769             if (sect.sct_type == SCT_WATER || sect.sct_type == SCT_WASTE) {
770                 mpr(pp->pln_own,
771                     "Nowhere to land at %s, plane #%d crashes and burns...\n",
772                     xyas(pp->pln_x, pp->pln_y, pp->pln_own), pp->pln_uid);
773                 pp->pln_effic = 0;
774             }
775         }
776         putplane(pp->pln_uid, pp);
777         newqp = qp->q_forw;
778         emp_remque(qp);
779         free(qp);
780         qp = newqp;
781     }
782 }
783
784 void
785 pln_removedupes(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
786 {
787     struct emp_qelem *bomb;
788     struct emp_qelem *esc;
789     struct plist *bombp;
790     struct plist *escp;
791
792     if (QEMPTY(bomb_list) || QEMPTY(esc_list))
793         return;
794     bomb = bomb_list->q_forw;
795     while (bomb != bomb_list) {
796         if (QEMPTY(esc_list)) {
797             bomb = bomb_list;
798             continue;
799         }
800         esc = esc_list->q_forw;
801         bombp = (struct plist *)bomb;
802         while (esc != esc_list) {
803             escp = (struct plist *)esc;
804             if (escp->plane.pln_uid == bombp->plane.pln_uid) {
805                 emp_remque(esc);
806                 free(esc);
807                 esc = esc_list;
808             } else
809                 esc = esc->q_forw;
810         }
811         bomb = bomb->q_forw;
812     }
813 }
814
815 int
816 put_plane_on_ship(struct plnstr *plane, struct shpstr *ship)
817 {
818     struct plchrstr *pcp;
819     struct mchrstr *mcp;
820
821     pcp = &plchr[(int)plane->pln_type];
822     mcp = &mchr[(int)ship->shp_type];
823
824     if (((int)plane->pln_ship) == ((int)ship->shp_uid))
825         return 1;               /* Already on ship */
826
827     /* Try to put on ship as a chopper plane */
828     if ((pcp->pl_flags & P_K) &&
829         (mcp->m_flags & M_CHOPPER) &&
830         (ship->shp_nchoppers < mcp->m_nchoppers)) {
831
832         ship->shp_nchoppers++;
833         plane->pln_x = ship->shp_x;
834         plane->pln_y = ship->shp_y;
835         plane->pln_ship = ship->shp_uid;
836         putship(ship->shp_uid, ship);
837         putplane(plane->pln_uid, plane);
838         return 1;
839     }
840
841     /* Try to put on ship as an xlight plane */
842     if ((pcp->pl_flags & P_E) &&
843         (mcp->m_flags & M_XLIGHT) &&
844         (ship->shp_nxlight < mcp->m_nxlight)) {
845
846         ship->shp_nxlight++;
847         plane->pln_x = ship->shp_x;
848         plane->pln_y = ship->shp_y;
849         plane->pln_ship = ship->shp_uid;
850         putship(ship->shp_uid, ship);
851         putplane(plane->pln_uid, plane);
852         return 1;
853     }
854
855     /* Try to put on ship as a normal plane */
856     if ((((pcp->pl_flags & P_L) && (mcp->m_flags & M_FLY)) ||
857          ((pcp->pl_flags & P_M) && (pcp->pl_flags & P_L) &&
858           (mcp->m_flags & M_MSL))) &&
859         (ship->shp_nplane < mcp->m_nplanes)) {
860
861         ship->shp_nplane++;
862         plane->pln_x = ship->shp_x;
863         plane->pln_y = ship->shp_y;
864         plane->pln_ship = ship->shp_uid;
865         putship(ship->shp_uid, ship);
866         putplane(plane->pln_uid, plane);
867         return 1;
868     }
869
870     /* We have failed */
871     return 0;
872 }
873
874 int
875 take_plane_off_ship(struct plnstr *plane, struct shpstr *ship)
876 {
877     struct plchrstr *pcp;
878     struct mchrstr *mcp;
879
880     pcp = &plchr[(int)plane->pln_type];
881     mcp = &mchr[(int)ship->shp_type];
882
883     /* Try to take off ship as a chopper plane */
884     if ((pcp->pl_flags & P_K) &&
885         (mcp->m_flags & M_CHOPPER) && (ship->shp_nchoppers)) {
886
887         ship->shp_nchoppers--;
888         plane->pln_ship = -1;
889         putship(ship->shp_uid, ship);
890         putplane(plane->pln_uid, plane);
891         return 1;
892     }
893
894     /* Try to take off ship as an xlight plane */
895     if ((pcp->pl_flags & P_E) &&
896         (mcp->m_flags & M_XLIGHT) && (ship->shp_nxlight)) {
897
898         ship->shp_nxlight--;
899         plane->pln_ship = -1;
900         putship(ship->shp_uid, ship);
901         putplane(plane->pln_uid, plane);
902         return 1;
903     }
904
905     /* Try to take off ship as a normal plane */
906     if ((((pcp->pl_flags & P_L) && (mcp->m_flags & M_FLY)) ||
907          ((pcp->pl_flags & P_M) && (pcp->pl_flags & P_L) &&
908           (mcp->m_flags & M_MSL))) && (ship->shp_nplane)) {
909
910         ship->shp_nplane--;
911         plane->pln_ship = -1;
912         putship(ship->shp_uid, ship);
913         putplane(plane->pln_uid, plane);
914         return 1;
915     }
916
917     /* We have failed */
918     return 0;
919 }
920
921 int
922 take_plane_off_land(struct plnstr *plane, struct lndstr *land)
923 {
924     struct plchrstr *pcp;
925     struct lchrstr *lcp;
926
927     pcp = &plchr[(int)plane->pln_type];
928     lcp = &lchr[(int)land->lnd_type];
929
930     /* Try to take off ship as an xlight plane */
931     if ((pcp->pl_flags & P_E) &&
932         (lcp->l_flags & L_XLIGHT) && (land->lnd_nxlight)) {
933
934         land->lnd_nxlight--;
935         plane->pln_land = -1;
936         putland(land->lnd_uid, land);
937         putplane(plane->pln_uid, plane);
938         return 1;
939     }
940
941     /* We have failed */
942     return 0;
943 }
944
945 int
946 can_be_on_ship(int p, int s)
947 {
948     struct plnstr plane;
949     struct shpstr ship;
950     struct plchrstr *pcp;
951     struct mchrstr *mcp;
952
953     getplane(p, &plane);
954     getship(s, &ship);
955
956     pcp = &plchr[(int)plane.pln_type];
957     mcp = &mchr[(int)ship.shp_type];
958
959     if (pcp->pl_flags & P_L)
960         if (mcp->m_flags & M_FLY)
961             return 1;
962
963     if (pcp->pl_flags & P_K)
964         if (mcp->m_flags & M_CHOPPER)
965             return 1;
966
967     if (pcp->pl_flags & P_M)
968         if (mcp->m_flags & M_MSL)
969             return 1;
970
971     if (pcp->pl_flags & P_E)
972         if (mcp->m_flags & M_XLIGHT)
973             return 1;
974
975     return 0;
976 }
977
978 void
979 plane_sweep(struct emp_qelem *plane_list, coord x, coord y)
980 {
981     struct plnstr *pp;
982     struct plchrstr *pcp;
983     struct emp_qelem *qp;
984     struct emp_qelem *next;
985     struct plist *ip;
986     struct sctstr sect;
987     int mines_there;
988     int found = 0;
989
990     getsect(x, y, &sect);
991     mines_there = sect.sct_mines;
992
993     if (mines_there == 0)
994         return;
995
996     if ((sect.sct_type != SCT_WATER) && (sect.sct_type != SCT_HARBR))
997         return;
998
999     for (qp = plane_list->q_forw; ((qp != plane_list) && (mines_there));
1000          qp = next) {
1001         next = qp->q_forw;
1002         ip = (struct plist *)qp;
1003         pp = &ip->plane;
1004         pcp = ip->pcp;
1005         if (!(pcp->pl_flags & P_SWEEP)) /* if it isn't an sweep plane */
1006             continue;
1007
1008         if (chance(((double)(100 - pp->pln_acc)) / 100.0)) {
1009             pr("Sweep! in %s\n",
1010                xyas(sect.sct_x, sect.sct_y, pp->pln_own));
1011             mines_there--;
1012             found = 1;
1013         }
1014     }
1015
1016     if (found && map_set(player->cnum, sect.sct_x, sect.sct_y, 'X', 0))
1017         writemap(player->cnum);
1018     sect.sct_mines = mines_there;
1019     putsect(&sect);
1020 }
1021
1022 void
1023 count_planes(struct shpstr *sp)
1024 {
1025     struct nstr_item ni;
1026     struct plnstr plane;
1027     struct plchrstr *pcp;
1028     struct mchrstr *mcp;
1029     int nplane = 0;
1030     int nchoppers = 0;
1031     int nxlight = 0;
1032
1033     if (sp->shp_effic < SHIP_MINEFF)
1034         return;
1035
1036     mcp = &mchr[(int)sp->shp_type];
1037     snxtitem_xy(&ni, EF_PLANE, sp->shp_x, sp->shp_y);
1038     while (nxtitem(&ni, &plane)) {
1039         if (plane.pln_own == 0)
1040             continue;
1041         if (plane.pln_ship == sp->shp_uid) {
1042             pcp = &plchr[(int)plane.pln_type];
1043             if ((pcp->pl_flags & P_K) && (nchoppers < mcp->m_nchoppers))
1044                 nchoppers++;
1045             else if ((pcp->pl_flags & P_E) && (nxlight < mcp->m_nxlight))
1046                 nxlight++;
1047             else if ((pcp->pl_flags & P_L) || (pcp->pl_flags & P_M))
1048                 nplane++;
1049         }
1050     }
1051
1052     if (nplane != sp->shp_nplane ||
1053         nxlight != sp->shp_nxlight || nchoppers != sp->shp_nchoppers) {
1054         sp->shp_nplane = nplane;
1055         sp->shp_nxlight = nxlight;
1056         sp->shp_nchoppers = nchoppers;
1057         putship(sp->shp_uid, sp);
1058     }
1059 }
1060
1061 void
1062 count_land_planes(struct lndstr *lp)
1063 {
1064     struct nstr_item ni;
1065     struct plnstr plane;
1066     int nplane = 0;
1067
1068     if (lp->lnd_effic < LAND_MINEFF)
1069         return;
1070
1071     snxtitem_all(&ni, EF_PLANE);
1072     while (nxtitem(&ni, &plane)) {
1073         if (plane.pln_own == 0)
1074             continue;
1075         if (plane.pln_land == lp->lnd_uid)
1076             nplane++;
1077     }
1078
1079     if (lp->lnd_nxlight != nplane) {
1080         lp->lnd_nxlight = nplane;
1081         putland(lp->lnd_uid, lp);
1082     }
1083 }
1084
1085 int
1086 count_sect_planes(struct sctstr *sp)
1087 {
1088     int count = 0;
1089     struct nstr_item ni;
1090     struct plnstr plane;
1091
1092     snxtitem_all(&ni, EF_PLANE);
1093     while (nxtitem(&ni, &plane)) {
1094         if (!plane.pln_own)
1095             continue;
1096         if (plane.pln_flags & PLN_LAUNCHED)
1097             continue;
1098         if (plane.pln_x == sp->sct_x && plane.pln_y == sp->sct_y)
1099             ++count;
1100     }
1101
1102     return count;
1103 }
1104
1105 int
1106 put_plane_on_land(struct plnstr *plane, struct lndstr *land)
1107 {
1108     struct plchrstr *pcp;
1109     struct lchrstr *lcp;
1110
1111     pcp = &plchr[(int)plane->pln_type];
1112     lcp = &lchr[(int)land->lnd_type];
1113
1114     if (((int)plane->pln_land) == ((int)land->lnd_uid))
1115         return 1;               /* Already on unit */
1116
1117     /* Try to put on unit as an xlight plane */
1118     if ((pcp->pl_flags & P_E) &&
1119         (lcp->l_flags & L_XLIGHT) &&
1120         (land->lnd_nxlight < lcp->l_nxlight)) {
1121
1122         land->lnd_nxlight++;
1123         plane->pln_x = land->lnd_x;
1124         plane->pln_y = land->lnd_y;
1125         plane->pln_land = land->lnd_uid;
1126         putland(land->lnd_uid, land);
1127         putplane(plane->pln_uid, plane);
1128         return 1;
1129     }
1130
1131     /* We have failed */
1132     return 0;
1133 }
1134
1135 int
1136 pln_hitchance(struct plnstr *pp, int hardtarget, int type)
1137 {
1138     struct plchrstr *pcp = plchr + pp->pln_type;
1139     float tfact = (float)(pp->pln_tech - pcp->pl_tech) /
1140         (pp->pln_tech - pcp->pl_tech / 2);
1141     int acc = pp->pln_acc;
1142     int hitchance;
1143
1144     if (type == EF_SHIP) {
1145         if (pcp->pl_flags & P_A)
1146             acc -= 20;
1147         if (!(pcp->pl_flags & P_T))
1148             acc += 35;
1149     }
1150     hitchance = (int)(pp->pln_effic * (1.0 - 0.1 * tfact) *
1151                       (1.0 - (float)acc / 100.0)) - hardtarget;
1152
1153     /* smooth out the bottom of the graph with asymtote at 5 -KHS */
1154     if (hitchance < 20)
1155         hitchance = 5 + ldround(300.0 / (40.0 - hitchance), 1);
1156     if (hitchance > 100)
1157         hitchance = 100;
1158     return hitchance;
1159 }
1160
1161 /* return 0 if there was a nuclear detonation */
1162
1163 int
1164 pln_damage(struct plnstr *pp, coord x, coord y, s_char type, int *nukedamp,
1165            int noisy)
1166 {
1167     struct plchrstr *pcp = plchr + pp->pln_type;
1168     int i;
1169     int hitroll;
1170     int dam = 0;
1171     int aim;
1172     int effective = 1;
1173     int pinbomber = 0;
1174
1175     if (pp->pln_nuketype != -1) {
1176         mpr(pp->pln_own, "Releasing RV's for %s detonation...\n",
1177             pp->pln_flags & PLN_AIRBURST ? "airburst" : "groundburst");
1178         *nukedamp = detonate(pp, x, y);
1179         return 0;
1180     } else
1181         *nukedamp = 0;
1182
1183     if (!pp->pln_load)          /* e.g. ab, blowing up on launch pad */
1184         return 0;
1185
1186     i = roll(pp->pln_load) + 1;
1187     if (i > pp->pln_load)
1188         i = pp->pln_load;
1189
1190     if (pcp->pl_flags & P_M) {
1191         if (pcp->pl_flags & P_MAR)
1192             pinbomber = 1;
1193     } else if (pcp->pl_flags & P_T)
1194         pinbomber = 1;
1195
1196     aim = 100 - pp->pln_acc;
1197     if (type == 's') {
1198         if (pinbomber) {
1199             aim = pp->pln_acc;
1200             effective = 0;
1201         }
1202         aim += 30;
1203     } else {
1204         if (!pinbomber) {
1205             effective = 0;
1206         }
1207     }
1208     while (i--) {
1209         dam += roll(6);
1210         hitroll = roll(100);
1211         if (hitroll >= 90) {
1212             dam += 8;
1213             if (noisy)
1214                 mpr(pp->pln_own, "BLAM");
1215         } else if (hitroll < aim) {
1216             dam += 5;
1217             if (noisy)
1218                 mpr(pp->pln_own, "Blam");
1219         } else {
1220             dam += 1;
1221             if (noisy)
1222                 mpr(pp->pln_own, "blam");
1223         }
1224         if (i && noisy)
1225             mpr(pp->pln_own, "-");
1226     }
1227     if (noisy)
1228         mpr(pp->pln_own, "\n");
1229     if (effective)
1230         dam *= 2;
1231     return dam;
1232 }
1233
1234 int
1235 pln_identchance(struct plnstr *pp, int hardtarget, int type)
1236 {
1237     double misschance =
1238         (100.0 - pln_hitchance(pp, hardtarget, type)) / 100.0;
1239     return (int)(100 - 100 * misschance * misschance);
1240 }
1241
1242 int
1243 pln_mobcost(int dist, struct plnstr *pp, int flags)
1244 {
1245     int cost;
1246
1247     if ((flags & P_F) || (flags & P_ESC))
1248         cost = 10 * 100 / pp->pln_effic;
1249     else
1250         cost = 20 * 100 / pp->pln_effic;
1251
1252     cost = ldround((double)cost * dist / pp->pln_range_max, 1);
1253
1254     return min(32 + pp->pln_mobil, cost + 5);
1255 }
1256
1257 /*
1258  * Set PP's tech to TLEV along with everything else that depends on it.
1259  */
1260 void
1261 pln_set_tech(struct plnstr *pp, int tlev)
1262 {
1263     struct plchrstr *pcp = plchr + pp->pln_type;
1264     int tech_diff = tlev - pcp->pl_tech;
1265     int limited_range = pp->pln_range < pp->pln_range_max;
1266
1267     if (CANT_HAPPEN(tech_diff < 0)) {
1268       tlev -= tech_diff;
1269       tech_diff = 0;
1270     }
1271
1272     pp->pln_tech = tlev;
1273     pp->pln_att = PLN_ATTDEF(pcp->pl_att, tech_diff);
1274     pp->pln_def = PLN_ATTDEF(pcp->pl_def, tech_diff);
1275     pp->pln_acc = PLN_ACC(pcp->pl_acc, tech_diff);
1276     pp->pln_range_max = PLN_RAN(pcp->pl_range, tech_diff);
1277     pp->pln_load = PLN_LOAD(pcp->pl_load, tech_diff);
1278
1279     if (!limited_range || pp->pln_range > pp->pln_range_max)
1280         pp->pln_range = pp->pln_range_max;
1281 }