]> git.pond.sub.org Git - empserver/blob - src/lib/subs/plnsub.c
(drop): A mining mission targeting land called pln_dropoff() instead
[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         if (sectp->sct_type != SCT_WATER) {
359             pr("Your seamines have no effect here.\n");
360             return;
361         }
362         sectp->sct_mines = min(sectp->sct_mines + amt, MINES_MAX);
363         pr("%d mines laid in %s.\n", amt,
364            xyas(sectp->sct_x, sectp->sct_y, player->cnum));
365         if (map_set(player->cnum, sectp->sct_x, sectp->sct_y, 'X', 0))
366             writemap(player->cnum);
367         putsect(sectp);
368     }
369 }
370
371 void
372 pln_sel(struct nstr_item *ni, struct emp_qelem *list, struct sctstr *ap,
373         int ap_to_target, int rangemult, int wantflags, int nowantflags)
374 {
375     struct plnstr plane;
376     struct shpstr ship;
377     struct lndstr land;
378     struct sctstr sect;
379     int range;
380     struct plchrstr *pcp;
381     struct plist *plp;
382     int y;
383     int bad, bad1;
384     unsigned int x;
385
386     emp_initque(list);
387     while (nxtitem(ni, &plane)) {
388         if (!player->owner)
389             continue;
390         if (plane.pln_mobil <= 0)
391             continue;
392         if (opt_MARKET) {
393             if (ontradingblock(EF_PLANE, (int *)&plane)) {
394                 pr("plane #%d inelligible - it's for sale.\n",
395                    plane.pln_uid);
396                 continue;
397             }
398         }
399
400         range = mapdist(plane.pln_x, plane.pln_y, ap->sct_x, ap->sct_y);
401         if (range > 4) {
402             pr("%s too far from assembly point\n", prplane(&plane));
403             continue;
404         }
405         if (plane.pln_effic < 40) {
406             pr("%s not efficient enough (must be 40%%)\n",
407                prplane(&plane));
408             continue;
409         }
410         range += ap_to_target;
411         pcp = &plchr[(int)plane.pln_type];
412         bad = 0;
413         bad1 = 0;
414         if (wantflags) {
415             for (x = 0; x < sizeof(wantflags) * 8; x++) {
416                 y = (1 << x);
417                 if ((wantflags & y) == y)
418                     if ((pcp->pl_flags & y) != y) {
419                         switch (y) {
420                         case P_F:
421                         case P_ESC:
422                             bad1 = 2;
423                             break;
424                         case P_E:
425                         case P_L:
426                         case P_K:
427                             bad1 = 1;
428                             break;
429                         default:
430                             bad = 1;
431                         }
432                     }
433             }
434             if (bad)
435                 continue;
436             if (bad1 == 2) {
437                 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
438                     bad1 = 0;
439             }
440             if (bad1 == 1) {
441                 if ((wantflags & P_L) && (pcp->pl_flags & P_L))
442                     bad1 = 0;
443                 if ((wantflags & P_K) && (pcp->pl_flags & P_K))
444                     bad1 = 0;
445                 if ((wantflags & P_E) && (pcp->pl_flags & P_E))
446                     bad1 = 0;
447             }
448             if (bad1)
449                 continue;
450         }
451         bad = 0;
452         bad1 = 0;
453         if (nowantflags) {
454             for (x = 0; x < sizeof(nowantflags) * 8; x++) {
455                 y = (1 << x);
456                 if ((nowantflags & y) == y)
457                     if ((pcp->pl_flags & y) == y)
458                         bad = 1;
459             }
460             if (bad)
461                 continue;
462         }
463         range *= rangemult;
464         if (plane.pln_range < range) {
465             pr("%s out of range (%d:%d)\n", prplane(&plane),
466                plane.pln_range, range);
467             continue;
468         }
469         if (plane.pln_ship >= 0) {
470             if (!getship(plane.pln_ship, &ship) ||
471                 plane.pln_own != player->cnum) {
472               shipsunk:
473                 plane.pln_effic = 0;
474                 pr("(note) ship not valid for %s\n", prplane(&plane));
475                 putplane(plane.pln_uid, &plane);
476                 continue;
477             }
478             if (!can_be_on_ship(plane.pln_uid, ship.shp_uid))
479                 goto shipsunk;
480             if (ship.shp_effic < SHIP_MINEFF)
481                 goto shipsunk;
482             if (ship.shp_effic < SHP_AIROPS_EFF)
483                 continue;
484             /* Can't fly off non-owned ships or non-allied ship */
485             if ((ship.shp_own != player->cnum) &&
486                 (getrel(getnatp(ship.shp_own), player->cnum) != ALLIED)) {
487                 pr("(note) An ally does not own the ship %s is on\n",
488                    prplane(&plane));
489                 continue;
490             }
491         }
492         if (plane.pln_land >= 0) {
493             if (!getland(plane.pln_land, &land) ||
494                 (plane.pln_own != player->cnum)) {
495               landdead:
496                 plane.pln_effic = 0;
497                 pr("(note) land unit not valid for %s\n", prplane(&plane));
498                 putplane(plane.pln_uid, &plane);
499                 continue;
500             }
501             if (!(plchr[(int)plane.pln_type].pl_flags & P_E))
502                 goto landdead;
503             if (land.lnd_effic < LAND_MINEFF)
504                 goto landdead;
505             if (land.lnd_effic < LND_AIROPS_EFF)
506                 continue;
507             /* Can't fly off units in ships or other units */
508             if ((land.lnd_ship >= 0) || (land.lnd_land >= 0))
509                 continue;
510             /* Can't fly off non-owned units or non-allied unit */
511             if ((land.lnd_own != player->cnum) &&
512                 (getrel(getnatp(land.lnd_own), player->cnum) != ALLIED)) {
513                 pr("(note) An ally does not own the unit %s is on\n",
514                    prplane(&plane));
515                 continue;
516             }
517         }
518         /* Now, check the sector status if not on a plane or unit */
519         if ((plane.pln_ship < 0) && (plane.pln_land < 0)) {
520             if (!getsect(plane.pln_x, plane.pln_y, &sect))
521                 continue;
522             /* First, check allied status */
523             /* Can't fly from non-owned sectors or non-allied sectors */
524             if ((sect.sct_own != player->cnum) &&
525                 (getrel(getnatp(sect.sct_own), player->cnum) != ALLIED)) {
526                 pr("(note) An ally does not own the sector %s is in\n",
527                    prplane(&plane));
528                 continue;
529             }
530             /* non-vtol plane */
531             if ((pcp->pl_flags & P_V) == 0) {
532                 if (sect.sct_type != SCT_AIRPT) {
533                     pr("%s not at airport\n", prplane(&plane));
534                     continue;
535                 }
536                 if (sect.sct_effic < 40) {
537                     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));
538                     continue;
539                 }
540                 if (rangemult == 2 && sect.sct_effic < 60) {
541                     pr("%s is not 60%% efficient, %s can't land there.\n",
542                        xyas(sect.sct_x, sect.sct_y, plane.pln_own),
543                        prplane(&plane));
544                     continue;
545                 }
546             }
547         }
548         pr("%s standing by\n", prplane(&plane));
549         plane.pln_mission = 0;
550         putplane(plane.pln_uid, &plane);
551         plp = malloc(sizeof(struct plist));
552         plp->misc = 0;
553         plp->bombs = 0;
554         plp->pcp = pcp;
555         plp->plane = plane;
556         emp_insque(&plp->queue, list);
557     }
558 }
559
560 int
561 pln_arm(struct emp_qelem *list, int dist, int mission, struct ichrstr *ip,
562         int flags, int mission_flags)
563 {
564     struct emp_qelem *qp;
565     struct emp_qelem *next;
566     struct plist *plp;
567
568     for (qp = list->q_forw; qp != list; qp = next) {
569         next = qp->q_forw;
570         plp = (struct plist *)qp;
571         if (pln_equip(plp, ip, flags, mission) < 0) {
572             emp_remque(qp);
573             free(qp);
574             continue;
575         }
576         if (flags & (P_S | P_I)) {
577             if (plp->pcp->pl_flags & P_S)
578                 mission_flags |= P_S;
579             if (plp->pcp->pl_flags & P_I)
580                 mission_flags |= P_I;
581         }
582         if (!(plp->pcp->pl_flags & P_H))
583             /* no stealth on this mission */
584             mission_flags &= ~P_H;
585         if (!(plp->pcp->pl_flags & P_X))
586             /* no stealth on this mission */
587             mission_flags &= ~P_X;
588         if (!(plp->pcp->pl_flags & P_A)) {
589             /* no asw on this mission */
590             mission_flags &= ~P_A;
591         }
592         if (!(plp->pcp->pl_flags & P_MINE)) {
593             /* no asw on this mission */
594             mission_flags &= ~P_MINE;
595         }
596         plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
597         pr("%s equipped\n", prplane(&plp->plane));
598     }
599     return mission_flags;
600 }
601
602 static int
603 pln_equip(struct plist *plp, struct ichrstr *ip, int flags, s_char mission)
604 {
605     struct plchrstr *pcp;
606     struct plnstr *pp;
607     int needed;
608     struct lndstr land;
609     struct shpstr ship;
610     struct sctstr sect;
611     i_type itype;
612     int rval;
613     short *item;
614     int own;
615     int abandon_needed;
616
617     pp = &plp->plane;
618     pcp = plp->pcp;
619     if (pp->pln_ship >= 0) {
620         getship(pp->pln_ship, &ship);
621         item = ship.shp_item;
622         own = ship.shp_own;
623     } else if (pp->pln_land >= 0) {
624         getland(pp->pln_land, &land);
625         item = land.lnd_item;
626         own = land.lnd_own;
627     } else {
628         getsect(pp->pln_x, pp->pln_y, &sect);
629         item = sect.sct_item;
630         own = sect.sct_oldown;
631     }
632     if (ip) {
633         if (ip->i_vtype == I_CIVIL) {
634             if (pp->pln_own != own) {
635                 pr("You don't control those civilians!\n");
636                 return -1;
637             }
638         }
639     }
640     if (pcp->pl_fuel > item[I_PETROL]) {
641         pr("%s not enough petrol there!\n", prplane(pp));
642         return -1;
643     }
644     item[I_PETROL] -= pcp->pl_fuel;
645     rval = 0;
646     if ((flags & P_F) == 0) {
647         itype = I_NONE;
648         needed = 0;
649         switch (mission) {
650         case 's':
651         case 'p':
652             if (pp->pln_nuketype == -1) {
653                 itype = I_SHELL;
654                 needed = pp->pln_load;
655             }
656             break;
657         case 't':
658         case 'd':
659             if ((pcp->pl_flags & P_C) == 0 || ip == 0)
660                 break;
661             itype = ip->i_vtype;
662             needed = (pp->pln_load * 2) / ip->i_lbs;
663             break;
664         case 'm':
665             if ((pcp->pl_flags & P_MINE) == 0)
666                 break;
667             itype = I_SHELL;
668             needed = (pp->pln_load * 2) / ip->i_lbs;
669             break;
670         case 'a':
671             if ((pcp->pl_flags & (P_V | P_C)) == 0)
672                 break;
673             itype = I_MILIT;
674             needed = pp->pln_load / ip->i_lbs;
675             break;
676         case 'n':
677             if (pp->pln_nuketype == -1)
678                 rval = -1;
679             break;
680         default:
681             break;
682         }
683         if (rval < 0 || (itype != I_NONE && needed <= 0)) {
684             pr("%s can't contribute to mission\n", prplane(pp));
685             return -1;
686         }
687         if (itype != I_NONE) {
688 #if 0
689             /* Supply is broken somewhere, so don't use it for now */
690             if (itype == I_SHELL && item[itype] < needed)
691                 item[itype] += supply_commod(plp->plane.pln_own,
692                                              plp->plane.pln_x,
693                                              plp->plane.pln_y,
694                                              I_SHELL, needed);
695 #endif
696             abandon_needed = !!would_abandon(&sect, itype, needed, NULL);
697             if (item[itype] < needed + abandon_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 }