]> git.pond.sub.org Git - empserver/blob - src/lib/subs/plnsub.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / subs / plnsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  plnsub.c: Plane subroutine stuff
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Ken Stevens, 1995
33  *     Steve McClure, 1998-2000
34  */
35
36 #include <config.h>
37
38 #include <math.h>
39 #include "misc.h"
40 #include "player.h"
41 #include "sect.h"
42 #include "ship.h"
43 #include "land.h"
44 #include "item.h"
45 #include "plane.h"
46 #include "xy.h"
47 #include "nsc.h"
48 #include "file.h"
49 #include "nat.h"
50 #include "path.h"
51 #include "prototypes.h"
52 #include "optlist.h"
53
54 static int pln_equip(struct plist *, struct ichrstr *, int, s_char);
55
56 /*
57  * Get assembly point argument.
58  * If INPUT is not empty, use it, else prompt for more input using PROMPT.
59  * If this yields a valid assembly point, read it into *AP_SECT and
60  * return AP_SECT.
61  * Else complain and return NULL.
62  * *AP_SECT and BUF[1024] may be modified in either case.
63  */
64 struct sctstr *
65 get_assembly_point(char *input, struct sctstr *ap_sect, char *buf)
66 {
67     char *p;
68     coord x, y;
69     struct nstr_item ni;
70     struct shpstr ship;
71
72     p = getstarg(input, "assembly point? ", buf);
73     if (!p || *p == 0)
74         return NULL;
75     if (!sarg_xy(p, &x, &y) || !getsect(x, y, ap_sect))
76         return NULL;
77
78     /* over own or allied sector is fine */
79     if (ap_sect->sct_own == player->cnum
80         || getrel(getnatp(ap_sect->sct_own), player->cnum) == ALLIED)
81         return ap_sect;
82
83     /* over own or allied ship is fine */
84     snxtitem_xy(&ni, EF_SHIP, x, y);
85     while (nxtitem(&ni, &ship)) {
86         if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
87             continue;
88         if (ship.shp_own == player->cnum
89             || getrel(getnatp(ship.shp_own), player->cnum) == ALLIED)
90             return ap_sect;
91     }
92
93     pr("Assembly point not owned by you or an ally!\n");
94     return NULL;
95 }
96
97 int
98 pln_onewaymission(struct sctstr *target, int *shipno, int *flagp)
99 {
100     int nships;
101     int cno;
102     int flags;
103     struct shpstr ship;
104     char buf[1024];
105     char *p;
106
107     flags = *flagp;
108
109     /* offer carriers */
110     nships = carriersatxy(target->sct_x, target->sct_y,
111                           M_FLY | M_CHOPPER, 0, player->cnum);
112     if (nships) {
113         for (;;) {
114             if (!(p = getstarg(0, "Carrier #? ", buf)) || !*p)
115                 break;
116             cno = atoi(p);
117             if (cno < 0
118                 || !getship(cno, &ship)
119                 || (!player->owner
120                     && (getrel(getnatp(ship.shp_own), player->cnum)
121                         != ALLIED))) {
122                 pr("Not yours\n");
123                 continue;
124             }
125             if (ship.shp_x != target->sct_x || ship.shp_y != target->sct_y) {
126                 pr("Ship #%d not in %s\n", cno,
127                    xyas(target->sct_x, target->sct_y, player->cnum));
128                 continue;
129             }
130             if ((!(mchr[(int)ship.shp_type].m_flags & M_FLY)
131                  && !(mchr[(int)ship.shp_type].m_flags & M_XLIGHT)
132                  && !(mchr[(int)ship.shp_type].m_flags & M_CHOPPER))
133                 || ship.shp_effic < SHP_AIROPS_EFF) {
134                 pr("Can't land on %s.\n", prship(&ship));
135                 continue;
136             }
137
138             /* clear to land on ship#CNO */
139             pr("landing on carrier %d\n", cno);
140             if (mchr[(int)ship.shp_type].m_flags & M_FLY)
141                 flags |= P_L;
142             if (mchr[(int)ship.shp_type].m_flags & M_CHOPPER)
143                 flags |= P_K;
144             if (mchr[(int)ship.shp_type].m_flags & M_XLIGHT)
145                 flags |= P_E;
146             *shipno = cno;
147             *flagp = flags;
148             return 0;
149         }
150     }
151
152     /* try to land at sector */
153     if (target->sct_own != player->cnum
154         && getrel(getnatp(target->sct_own), player->cnum) != ALLIED) {
155         pr("Nowhere to land at sector %s!\n",
156            xyas(target->sct_x, target->sct_y, player->cnum));
157         return -1;
158     }
159     if (target->sct_type == SCT_MOUNT) {
160         pr("Nowhere to land at sector %s!\n",
161            xyas(target->sct_x, target->sct_y, player->cnum));
162         return -1;
163     }
164     if (target->sct_type != SCT_AIRPT || target->sct_effic < 60)
165         flags |= P_V;
166
167     /* clear to land at sector */
168     *shipno = -1;
169     *flagp = flags;
170     return 0;
171 }
172
173 int
174 pln_oneway_to_carrier_ok(struct emp_qelem *bomb_list,
175                          struct emp_qelem *esc_list, int cno)
176 {
177     struct emp_qelem *list, *qp;
178     struct plist *plp;
179     struct plchrstr *pcp;
180     struct shpstr ship;
181     struct mchrstr *mcp;
182     int nchoppers, nxlight, nplane;
183
184     if (cno < 0 || !getship(cno, &ship))
185         return 0;
186
187     count_planes(&ship);
188     nchoppers = ship.shp_nchoppers;
189     nxlight = ship.shp_nxlight;
190     nplane = ship.shp_nplane;
191     mcp = &mchr[(int)ship.shp_type];
192
193     /* for both lists */
194     for (list = bomb_list;
195          list;
196          list = list == bomb_list ? esc_list : NULL) {
197         for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
198             /* FIXME duplicates put_plane_on_ship() logic; refactor */
199             plp = (struct plist *)qp;
200             pcp = &plchr[(int)plp->plane.pln_type];
201             if (plp->plane.pln_ship == ship.shp_uid)
202                 continue;
203             /* try chopper space */
204             if ((pcp->pl_flags & P_K) && (mcp->m_flags & M_CHOPPER)
205                 && nchoppers < mcp->m_nchoppers)
206                 ++nchoppers;
207             /* try xlight space */
208             else if ((pcp->pl_flags & P_E) && (mcp->m_flags & M_XLIGHT)
209                      && nxlight < mcp->m_nxlight)
210                 ++nxlight;
211             /* try plane space */
212             else if ((((pcp->pl_flags & P_L) && (mcp->m_flags & M_FLY))
213                       || ((pcp->pl_flags & P_M) && (pcp->pl_flags & P_L)
214                           && (mcp->m_flags & M_MSL)))
215                      && nplane < mcp->m_nplanes)
216                 ++nplane;
217             else
218                 return 0;               /* won't be able to land */
219         }
220     }
221     return 1;
222 }
223
224 void
225 pln_newlanding(struct emp_qelem *list, coord tx, coord ty, int cno)
226 {
227     struct emp_qelem *qp;
228     struct plist *plp;
229     struct shpstr ship;
230     struct sctstr sect;
231
232     if (cno >= 0)
233         getship(cno, &ship);
234     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
235         plp = (struct plist *)qp;
236         if (cno >= 0) {
237             count_planes(&ship);
238             if (!can_be_on_ship(plp->plane.pln_uid, ship.shp_uid))
239                 pr("\t%s cannot land on ship #%d! %s aborts!\n",
240                    prplane(&plp->plane), cno, prplane(&plp->plane));
241             else if (!put_plane_on_ship(&plp->plane, &ship))
242                 pr("\tNo room on ship #%d! %s aborts!\n", cno,
243                    prplane(&plp->plane));
244             else {
245                 if (plp->plane.pln_own != ship.shp_own) {
246 /*                                      plp->plane.pln_own = ship.shp_own;*/
247                     wu(0, ship.shp_own,
248                        "%s %s lands on your %s\n",
249                        cname(player->cnum),
250                        prplane(&plp->plane), prship(&ship));
251                 }
252             }
253         } else {
254             plp->plane.pln_x = tx;
255             plp->plane.pln_y = ty;
256             getsect(tx, ty, &sect);
257             if (plp->plane.pln_own != sect.sct_own) {
258 /*                              plp->plane.pln_own = sect.sct_own;*/
259                 wu(0, sect.sct_own,
260                    "%s %s lands at your sector %s\n",
261                    cname(player->cnum),
262                    prplane(&plp->plane), xyas(tx, ty, sect.sct_own));
263             }
264             plp->plane.pln_ship = cno;
265         }
266     }
267     if (cno >= 0)
268         putship(ship.shp_uid, &ship);
269 }
270
271 void
272 pln_dropoff(struct emp_qelem *list, struct ichrstr *ip, coord tx, coord ty,
273             void *ptr, int type)
274 {
275     struct emp_qelem *qp;
276     struct plist *plp;
277     int amt;
278     struct sctstr *sectp;
279     struct shpstr *sp;
280     int there;
281     int max;
282     struct mchrstr *mp;
283
284     if (ip == 0)
285         return;
286     amt = 0;
287     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
288         plp = (struct plist *)qp;
289         amt += plp->misc;
290     }
291     if (type == EF_SECTOR) {
292         sectp = ptr;
293         if (!sectp->sct_own) {
294             if (sectp->sct_type == SCT_WATER)
295                 pr("Your %s sink like a rock!\n", ip->i_name);
296             else
297                 pr("Your %s vanish without a trace.\n", ip->i_name);
298             return;
299         }
300         if (sectp->sct_own != player->cnum
301             && getrel(getnatp(sectp->sct_own), player->cnum) != ALLIED) {
302             pr("You don't own %s!  Cargo jettisoned...\n",
303                xyas(tx, ty, player->cnum));
304             return;
305         }
306         if (ip->i_vtype == I_CIVIL && sectp->sct_own != sectp->sct_oldown) {
307             pr("%s is occupied.  Your civilians suffer from identity crisis and die.\n",
308                xyas(tx, ty, player->cnum));
309             return;
310         }
311         there = sectp->sct_item[ip->i_vtype];
312         max = ITEM_MAX;
313     } else {
314         sp = ptr;
315         there = sp->shp_item[ip->i_vtype];
316         mp = &mchr[(int)sp->shp_type];
317         max = mp->m_item[ip->i_vtype];
318     }
319     there += amt;
320     if (there > max) {
321         pr("%d excess %s discarded\n", max - there, ip->i_name);
322         amt = max - there;
323         there = max;
324     }
325     pr("%d %s landed safely", amt, ip->i_name);
326     if (type == EF_SECTOR) {
327         sectp = ptr;
328         sectp->sct_item[ip->i_vtype] = there;
329         if (sectp->sct_own != player->cnum)
330             wu(0, sectp->sct_own, "%s planes drop %d %s in %s\n",
331                cname(player->cnum), amt, ip->i_name,
332                xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
333         pr(" at %s\n", xyas(tx, ty, player->cnum));
334         putsect((struct sctstr *)ptr);
335     } else {
336         struct shpstr *sp = (struct shpstr *)ptr;
337         sp->shp_item[ip->i_vtype] = there;
338         if (sp->shp_own != player->cnum)
339             wu(0, sp->shp_own, "%s planes land %d %s on carrier %d\n",
340                cname(player->cnum), amt, ip->i_name, sp->shp_uid);
341         pr(" on carrier #%d\n", sp->shp_uid);
342         putship(sp->shp_uid, sp);
343     }
344 }
345
346 void
347 pln_mine(struct emp_qelem *list, struct sctstr *sectp)
348 {
349     struct emp_qelem *qp;
350     struct plist *plp;
351     int amt;
352
353     amt = 0;
354     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
355         plp = (struct plist *)qp;
356         amt += plp->misc;
357
358     }
359     if (amt > 0) {
360         if (sectp->sct_type != SCT_WATER) {
361             pr("Your seamines have no effect here.\n");
362             return;
363         }
364         sectp->sct_mines = MIN(sectp->sct_mines + amt, MINES_MAX);
365         pr("%d mines laid in %s.\n", amt,
366            xyas(sectp->sct_x, sectp->sct_y, player->cnum));
367         if (map_set(player->cnum, sectp->sct_x, sectp->sct_y, 'X', 0))
368             writemap(player->cnum);
369         putsect(sectp);
370     }
371 }
372
373 void
374 pln_sel(struct nstr_item *ni, struct emp_qelem *list, struct sctstr *ap,
375         int ap_to_target, int rangemult, int wantflags, int nowantflags)
376 {
377     struct plnstr plane;
378     struct shpstr ship;
379     struct lndstr land;
380     struct sctstr sect;
381     int range;
382     struct plchrstr *pcp;
383     struct plist *plp;
384     int y;
385     int bad, bad1;
386     unsigned int x;
387
388     emp_initque(list);
389     while (nxtitem(ni, &plane)) {
390         if (!player->owner)
391             continue;
392         if (plane.pln_mobil <= 0)
393             continue;
394         if (opt_MARKET) {
395             if (ontradingblock(EF_PLANE, (int *)&plane)) {
396                 pr("plane #%d inelligible - it's for sale.\n",
397                    plane.pln_uid);
398                 continue;
399             }
400         }
401
402         range = mapdist(plane.pln_x, plane.pln_y, ap->sct_x, ap->sct_y);
403         if (range > 4) {
404             pr("%s too far from assembly point\n", prplane(&plane));
405             continue;
406         }
407         if (plane.pln_effic < 40) {
408             pr("%s not efficient enough (must be 40%%)\n",
409                prplane(&plane));
410             continue;
411         }
412         range += ap_to_target;
413         pcp = &plchr[(int)plane.pln_type];
414         bad = 0;
415         bad1 = 0;
416         if (wantflags) {
417             for (x = 0; x < sizeof(wantflags) * 8; x++) {
418                 y = (1 << x);
419                 if ((wantflags & y) == y)
420                     if ((pcp->pl_flags & y) != y) {
421                         switch (y) {
422                         case P_F:
423                         case P_ESC:
424                             bad1 = 2;
425                             break;
426                         case P_E:
427                         case P_L:
428                         case P_K:
429                             bad1 = 1;
430                             break;
431                         default:
432                             bad = 1;
433                         }
434                     }
435             }
436             if (bad)
437                 continue;
438             if (bad1 == 2) {
439                 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
440                     bad1 = 0;
441             }
442             if (bad1 == 1) {
443                 if ((wantflags & P_L) && (pcp->pl_flags & P_L))
444                     bad1 = 0;
445                 if ((wantflags & P_K) && (pcp->pl_flags & P_K))
446                     bad1 = 0;
447                 if ((wantflags & P_E) && (pcp->pl_flags & P_E))
448                     bad1 = 0;
449             }
450             if (bad1)
451                 continue;
452         }
453         bad = 0;
454         bad1 = 0;
455         if (nowantflags) {
456             for (x = 0; x < sizeof(nowantflags) * 8; x++) {
457                 y = (1 << x);
458                 if ((nowantflags & y) == y)
459                     if ((pcp->pl_flags & y) == y)
460                         bad = 1;
461             }
462             if (bad)
463                 continue;
464         }
465         range *= rangemult;
466         if (plane.pln_range < range) {
467             pr("%s out of range (%d:%d)\n", prplane(&plane),
468                plane.pln_range, range);
469             continue;
470         }
471         if (plane.pln_ship >= 0) {
472             if (!getship(plane.pln_ship, &ship) ||
473                 plane.pln_own != player->cnum) {
474               shipsunk:
475                 plane.pln_effic = 0;
476                 pr("(note) ship not valid for %s\n", prplane(&plane));
477                 putplane(plane.pln_uid, &plane);
478                 continue;
479             }
480             if (!can_be_on_ship(plane.pln_uid, ship.shp_uid))
481                 goto shipsunk;
482             if (ship.shp_effic < SHIP_MINEFF)
483                 goto shipsunk;
484             if (ship.shp_effic < SHP_AIROPS_EFF)
485                 continue;
486             /* Can't fly off non-owned ships or non-allied ship */
487             if ((ship.shp_own != player->cnum) &&
488                 (getrel(getnatp(ship.shp_own), player->cnum) != ALLIED)) {
489                 pr("(note) An ally does not own the ship %s is on\n",
490                    prplane(&plane));
491                 continue;
492             }
493         }
494         if (plane.pln_land >= 0) {
495             if (!getland(plane.pln_land, &land) ||
496                 (plane.pln_own != player->cnum)) {
497               landdead:
498                 plane.pln_effic = 0;
499                 pr("(note) land unit not valid for %s\n", prplane(&plane));
500                 putplane(plane.pln_uid, &plane);
501                 continue;
502             }
503             if (!(plchr[(int)plane.pln_type].pl_flags & P_E))
504                 goto landdead;
505             if (land.lnd_effic < LAND_MINEFF)
506                 goto landdead;
507             if (land.lnd_effic < LND_AIROPS_EFF)
508                 continue;
509             /* Can't fly off units in ships or other units */
510             if ((land.lnd_ship >= 0) || (land.lnd_land >= 0))
511                 continue;
512             /* Can't fly off non-owned units or non-allied unit */
513             if ((land.lnd_own != player->cnum) &&
514                 (getrel(getnatp(land.lnd_own), player->cnum) != ALLIED)) {
515                 pr("(note) An ally does not own the unit %s is on\n",
516                    prplane(&plane));
517                 continue;
518             }
519         }
520         /* Now, check the sector status if not on a plane or unit */
521         if ((plane.pln_ship < 0) && (plane.pln_land < 0)) {
522             if (!getsect(plane.pln_x, plane.pln_y, &sect))
523                 continue;
524             /* First, check allied status */
525             /* Can't fly from non-owned sectors or non-allied sectors */
526             if ((sect.sct_own != player->cnum) &&
527                 (getrel(getnatp(sect.sct_own), player->cnum) != ALLIED)) {
528                 pr("(note) An ally does not own the sector %s is in\n",
529                    prplane(&plane));
530                 continue;
531             }
532             /* non-vtol plane */
533             if ((pcp->pl_flags & P_V) == 0) {
534                 if (sect.sct_type != SCT_AIRPT) {
535                     pr("%s not at airport\n", prplane(&plane));
536                     continue;
537                 }
538                 if (sect.sct_effic < 40) {
539                     pr("%s is not 40%% efficient, %s can't take off from there.\n", xyas(sect.sct_x, sect.sct_y, plane.pln_own), prplane(&plane));
540                     continue;
541                 }
542                 if (rangemult == 2 && sect.sct_effic < 60) {
543                     pr("%s is not 60%% efficient, %s can't land there.\n",
544                        xyas(sect.sct_x, sect.sct_y, plane.pln_own),
545                        prplane(&plane));
546                     continue;
547                 }
548             }
549         }
550         pr("%s standing by\n", prplane(&plane));
551         plane.pln_mission = 0;
552         putplane(plane.pln_uid, &plane);
553         plp = malloc(sizeof(struct plist));
554         plp->misc = 0;
555         plp->bombs = 0;
556         plp->pcp = pcp;
557         plp->plane = plane;
558         emp_insque(&plp->queue, list);
559     }
560 }
561
562 int
563 pln_arm(struct emp_qelem *list, int dist, int mission, struct ichrstr *ip,
564         int flags, int mission_flags)
565 {
566     struct emp_qelem *qp;
567     struct emp_qelem *next;
568     struct plist *plp;
569
570     for (qp = list->q_forw; qp != list; qp = next) {
571         next = qp->q_forw;
572         plp = (struct plist *)qp;
573         if (pln_equip(plp, ip, flags, mission) < 0) {
574             emp_remque(qp);
575             free(qp);
576             continue;
577         }
578         if (flags & (P_S | P_I)) {
579             if (plp->pcp->pl_flags & P_S)
580                 mission_flags |= P_S;
581             if (plp->pcp->pl_flags & P_I)
582                 mission_flags |= P_I;
583         }
584         if (!(plp->pcp->pl_flags & P_H))
585             /* no stealth on this mission */
586             mission_flags &= ~P_H;
587         if (!(plp->pcp->pl_flags & P_X))
588             /* no stealth on this mission */
589             mission_flags &= ~P_X;
590         if (!(plp->pcp->pl_flags & P_A)) {
591             /* no asw on this mission */
592             mission_flags &= ~P_A;
593         }
594         if (!(plp->pcp->pl_flags & P_MINE)) {
595             /* no asw on this mission */
596             mission_flags &= ~P_MINE;
597         }
598         plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
599         pr("%s equipped\n", prplane(&plp->plane));
600     }
601     return mission_flags;
602 }
603
604 static int
605 pln_equip(struct plist *plp, struct ichrstr *ip, int flags, s_char mission)
606 {
607     struct plchrstr *pcp;
608     struct plnstr *pp;
609     int needed;
610     struct lndstr land;
611     struct shpstr ship;
612     struct sctstr sect;
613     i_type itype;
614     int rval;
615     short *item;
616     int own;
617     int abandon_needed;
618
619     pp = &plp->plane;
620     pcp = plp->pcp;
621     if (pp->pln_ship >= 0) {
622         getship(pp->pln_ship, &ship);
623         item = ship.shp_item;
624         own = ship.shp_own;
625     } else if (pp->pln_land >= 0) {
626         getland(pp->pln_land, &land);
627         item = land.lnd_item;
628         own = land.lnd_own;
629     } else {
630         getsect(pp->pln_x, pp->pln_y, &sect);
631         item = sect.sct_item;
632         own = sect.sct_oldown;
633     }
634     if (ip) {
635         if (ip->i_vtype == I_CIVIL) {
636             if (pp->pln_own != own) {
637                 pr("You don't control those civilians!\n");
638                 return -1;
639             }
640         }
641     }
642     if (pcp->pl_fuel > item[I_PETROL]) {
643         pr("%s not enough petrol there!\n", prplane(pp));
644         return -1;
645     }
646     item[I_PETROL] -= pcp->pl_fuel;
647     rval = 0;
648     if ((flags & P_F) == 0) {
649         itype = I_NONE;
650         needed = 0;
651         switch (mission) {
652         case 's':
653         case 'p':
654             if (pp->pln_nuketype == -1) {
655                 itype = I_SHELL;
656                 needed = pp->pln_load;
657             }
658             break;
659         case 't':
660         case 'd':
661             if ((pcp->pl_flags & P_C) == 0 || ip == 0)
662                 break;
663             itype = ip->i_vtype;
664             needed = (pp->pln_load * 2) / ip->i_lbs;
665             break;
666         case 'm':
667             if ((pcp->pl_flags & P_MINE) == 0)
668                 break;
669             itype = I_SHELL;
670             needed = (pp->pln_load * 2) / ip->i_lbs;
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
926     pcp = &plchr[(int)plane->pln_type];
927
928     /* Try to take off ship as an xlight plane */
929     if ((pcp->pl_flags & P_E) && land->lnd_nxlight) {
930
931         land->lnd_nxlight--;
932         plane->pln_land = -1;
933         putland(land->lnd_uid, land);
934         putplane(plane->pln_uid, plane);
935         return 1;
936     }
937
938     /* We have failed */
939     return 0;
940 }
941
942 int
943 can_be_on_ship(int p, int s)
944 {
945     struct plnstr plane;
946     struct shpstr ship;
947     struct plchrstr *pcp;
948     struct mchrstr *mcp;
949
950     getplane(p, &plane);
951     getship(s, &ship);
952
953     pcp = &plchr[(int)plane.pln_type];
954     mcp = &mchr[(int)ship.shp_type];
955
956     if (pcp->pl_flags & P_L)
957         if (mcp->m_flags & M_FLY)
958             return 1;
959
960     if (pcp->pl_flags & P_K)
961         if (mcp->m_flags & M_CHOPPER)
962             return 1;
963
964     if (pcp->pl_flags & P_M)
965         if (mcp->m_flags & M_MSL)
966             return 1;
967
968     if (pcp->pl_flags & P_E)
969         if (mcp->m_flags & M_XLIGHT)
970             return 1;
971
972     return 0;
973 }
974
975 void
976 plane_sweep(struct emp_qelem *plane_list, coord x, coord y)
977 {
978     struct plnstr *pp;
979     struct plchrstr *pcp;
980     struct emp_qelem *qp;
981     struct emp_qelem *next;
982     struct plist *ip;
983     struct sctstr sect;
984     int mines_there;
985     int found = 0;
986
987     getsect(x, y, &sect);
988     mines_there = sect.sct_mines;
989
990     if (mines_there == 0)
991         return;
992
993     if ((sect.sct_type != SCT_WATER) && (sect.sct_type != SCT_HARBR))
994         return;
995
996     for (qp = plane_list->q_forw; ((qp != plane_list) && (mines_there));
997          qp = next) {
998         next = qp->q_forw;
999         ip = (struct plist *)qp;
1000         pp = &ip->plane;
1001         pcp = ip->pcp;
1002         if (!(pcp->pl_flags & P_SWEEP)) /* if it isn't an sweep plane */
1003             continue;
1004
1005         if (chance(((double)(100 - pp->pln_acc)) / 100.0)) {
1006             pr("Sweep! in %s\n",
1007                xyas(sect.sct_x, sect.sct_y, pp->pln_own));
1008             mines_there--;
1009             found = 1;
1010         }
1011     }
1012
1013     if (found && map_set(player->cnum, sect.sct_x, sect.sct_y, 'X', 0))
1014         writemap(player->cnum);
1015     sect.sct_mines = mines_there;
1016     putsect(&sect);
1017 }
1018
1019 void
1020 count_planes(struct shpstr *sp)
1021 {
1022     struct nstr_item ni;
1023     struct plnstr plane;
1024     struct plchrstr *pcp;
1025     struct mchrstr *mcp;
1026     int nplane = 0;
1027     int nchoppers = 0;
1028     int nxlight = 0;
1029
1030     if (sp->shp_effic < SHIP_MINEFF)
1031         return;
1032
1033     mcp = &mchr[(int)sp->shp_type];
1034     snxtitem_xy(&ni, EF_PLANE, sp->shp_x, sp->shp_y);
1035     while (nxtitem(&ni, &plane)) {
1036         if (plane.pln_own == 0)
1037             continue;
1038         if (plane.pln_ship == sp->shp_uid) {
1039             pcp = &plchr[(int)plane.pln_type];
1040             if ((pcp->pl_flags & P_K) && (nchoppers < mcp->m_nchoppers))
1041                 nchoppers++;
1042             else if ((pcp->pl_flags & P_E) && (nxlight < mcp->m_nxlight))
1043                 nxlight++;
1044             else if ((pcp->pl_flags & P_L) || (pcp->pl_flags & P_M))
1045                 nplane++;
1046         }
1047     }
1048
1049     if (nplane != sp->shp_nplane ||
1050         nxlight != sp->shp_nxlight || nchoppers != sp->shp_nchoppers) {
1051         sp->shp_nplane = nplane;
1052         sp->shp_nxlight = nxlight;
1053         sp->shp_nchoppers = nchoppers;
1054         putship(sp->shp_uid, sp);
1055     }
1056 }
1057
1058 void
1059 count_land_planes(struct lndstr *lp)
1060 {
1061     struct nstr_item ni;
1062     struct plnstr plane;
1063     int nplane = 0;
1064
1065     if (lp->lnd_effic < LAND_MINEFF)
1066         return;
1067
1068     snxtitem_all(&ni, EF_PLANE);
1069     while (nxtitem(&ni, &plane)) {
1070         if (plane.pln_own == 0)
1071             continue;
1072         if (plane.pln_land == lp->lnd_uid)
1073             nplane++;
1074     }
1075
1076     if (lp->lnd_nxlight != nplane) {
1077         lp->lnd_nxlight = nplane;
1078         putland(lp->lnd_uid, lp);
1079     }
1080 }
1081
1082 int
1083 count_sect_planes(struct sctstr *sp)
1084 {
1085     int count = 0;
1086     struct nstr_item ni;
1087     struct plnstr plane;
1088
1089     snxtitem_all(&ni, EF_PLANE);
1090     while (nxtitem(&ni, &plane)) {
1091         if (!plane.pln_own)
1092             continue;
1093         if (plane.pln_flags & PLN_LAUNCHED)
1094             continue;
1095         if (plane.pln_x == sp->sct_x && plane.pln_y == sp->sct_y)
1096             ++count;
1097     }
1098
1099     return count;
1100 }
1101
1102 int
1103 put_plane_on_land(struct plnstr *plane, struct lndstr *land)
1104 {
1105     struct plchrstr *pcp;
1106
1107     pcp = &plchr[(int)plane->pln_type];
1108
1109     if (((int)plane->pln_land) == ((int)land->lnd_uid))
1110         return 1;               /* Already on unit */
1111
1112     /* Try to put on unit as an xlight plane */
1113     if ((pcp->pl_flags & P_E) &&
1114         (land->lnd_nxlight < land->lnd_maxlight)) {
1115
1116         land->lnd_nxlight++;
1117         plane->pln_x = land->lnd_x;
1118         plane->pln_y = land->lnd_y;
1119         plane->pln_land = land->lnd_uid;
1120         putland(land->lnd_uid, land);
1121         putplane(plane->pln_uid, plane);
1122         return 1;
1123     }
1124
1125     /* We have failed */
1126     return 0;
1127 }
1128
1129 int
1130 pln_hitchance(struct plnstr *pp, int hardtarget, int type)
1131 {
1132     struct plchrstr *pcp = plchr + pp->pln_type;
1133     float tfact = (float)(pp->pln_tech - pcp->pl_tech) /
1134         (pp->pln_tech - pcp->pl_tech / 2);
1135     int acc = pp->pln_acc;
1136     int hitchance;
1137
1138     if (type == EF_SHIP) {
1139         if (pcp->pl_flags & P_A)
1140             acc -= 20;
1141         if (!(pcp->pl_flags & P_T))
1142             acc += 35;
1143     }
1144     hitchance = (int)(pp->pln_effic * (1.0 - 0.1 * tfact) *
1145                       (1.0 - (float)acc / 100.0)) - hardtarget;
1146
1147     /* smooth out the bottom of the graph with asymtote at 5 -KHS */
1148     if (hitchance < 20)
1149         hitchance = 5 + ldround(300.0 / (40.0 - hitchance), 1);
1150     if (hitchance > 100)
1151         hitchance = 100;
1152     return hitchance;
1153 }
1154
1155 /* return 0 if there was a nuclear detonation */
1156
1157 int
1158 pln_damage(struct plnstr *pp, coord x, coord y, s_char type, int *nukedamp,
1159            int noisy)
1160 {
1161     struct plchrstr *pcp = plchr + pp->pln_type;
1162     int i;
1163     int hitroll;
1164     int dam = 0;
1165     int aim;
1166     int effective = 1;
1167     int pinbomber = 0;
1168
1169     if (pp->pln_nuketype != -1) {
1170         mpr(pp->pln_own, "Releasing RV's for %s detonation...\n",
1171             pp->pln_flags & PLN_AIRBURST ? "airburst" : "groundburst");
1172         *nukedamp = detonate(pp, x, y);
1173         return 0;
1174     } else
1175         *nukedamp = 0;
1176
1177     if (!pp->pln_load)          /* e.g. ab, blowing up on launch pad */
1178         return 0;
1179
1180     i = roll(pp->pln_load) + 1;
1181     if (i > pp->pln_load)
1182         i = pp->pln_load;
1183
1184     if (pcp->pl_flags & P_M) {
1185         if (pcp->pl_flags & P_MAR)
1186             pinbomber = 1;
1187     } else if (pcp->pl_flags & P_T)
1188         pinbomber = 1;
1189
1190     aim = 100 - pp->pln_acc;
1191     if (type == 's') {
1192         if (pinbomber) {
1193             aim = pp->pln_acc;
1194             effective = 0;
1195         }
1196         aim += 30;
1197     } else {
1198         if (!pinbomber) {
1199             effective = 0;
1200         }
1201     }
1202     while (i--) {
1203         dam += roll(6);
1204         hitroll = roll(100);
1205         if (hitroll >= 90) {
1206             dam += 8;
1207             if (noisy)
1208                 mpr(pp->pln_own, "BLAM");
1209         } else if (hitroll < aim) {
1210             dam += 5;
1211             if (noisy)
1212                 mpr(pp->pln_own, "Blam");
1213         } else {
1214             dam += 1;
1215             if (noisy)
1216                 mpr(pp->pln_own, "blam");
1217         }
1218         if (i && noisy)
1219             mpr(pp->pln_own, "-");
1220     }
1221     if (noisy)
1222         mpr(pp->pln_own, "\n");
1223     if (effective)
1224         dam *= 2;
1225     return dam;
1226 }
1227
1228 int
1229 pln_identchance(struct plnstr *pp, int hardtarget, int type)
1230 {
1231     double misschance =
1232         (100.0 - pln_hitchance(pp, hardtarget, type)) / 100.0;
1233     return (int)(100 - 100 * misschance * misschance);
1234 }
1235
1236 int
1237 pln_mobcost(int dist, struct plnstr *pp, int flags)
1238 {
1239     int cost;
1240
1241     if ((flags & P_F) || (flags & P_ESC))
1242         cost = 10 * 100 / pp->pln_effic;
1243     else
1244         cost = 20 * 100 / pp->pln_effic;
1245
1246     cost = ldround((double)cost * dist / pp->pln_range_max, 1);
1247
1248     return MIN(32 + pp->pln_mobil, cost + 5);
1249 }
1250
1251 /*
1252  * Set PP's tech to TLEV along with everything else that depends on it.
1253  */
1254 void
1255 pln_set_tech(struct plnstr *pp, int tlev)
1256 {
1257     struct plchrstr *pcp = plchr + pp->pln_type;
1258     int tech_diff = tlev - pcp->pl_tech;
1259     int limited_range = pp->pln_range < pp->pln_range_max;
1260
1261     if (CANT_HAPPEN(tech_diff < 0)) {
1262       tlev -= tech_diff;
1263       tech_diff = 0;
1264     }
1265
1266     pp->pln_tech = tlev;
1267     pp->pln_att = PLN_ATTDEF(pcp->pl_att, tech_diff);
1268     pp->pln_def = PLN_ATTDEF(pcp->pl_def, tech_diff);
1269     pp->pln_acc = PLN_ACC(pcp->pl_acc, tech_diff);
1270     pp->pln_range_max = PLN_RAN(pcp->pl_range, tech_diff);
1271     pp->pln_load = PLN_LOAD(pcp->pl_load, tech_diff);
1272
1273     if (!limited_range || pp->pln_range > pp->pln_range_max)
1274         pp->pln_range = pp->pln_range_max;
1275 }