]> git.pond.sub.org Git - empserver/blob - src/lib/subs/plnsub.c
Fix pln_equip()'s check for foreign civilians
[empserver] / src / lib / subs / plnsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  plnsub.c: Plane subroutine stuff
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Ken Stevens, 1995
33  *     Steve McClure, 1998-2000
34  *     Markus Armbruster, 2004-2009
35  */
36
37 #include <config.h>
38
39 #include "file.h"
40 #include "item.h"
41 #include "land.h"
42 #include "lost.h"
43 #include "map.h"
44 #include "misc.h"
45 #include "nat.h"
46 #include "nsc.h"
47 #include "nuke.h"
48 #include "optlist.h"
49 #include "path.h"
50 #include "plane.h"
51 #include "player.h"
52 #include "prototypes.h"
53 #include "sect.h"
54 #include "ship.h"
55 #include "xy.h"
56
57 static int pln_equip(struct plist *, struct ichrstr *, int, char);
58 static int fit_plane_on_ship(struct plnstr *, struct shpstr *);
59
60 /*
61  * Get planes and escorts argument.
62  * Read planes into *NI_BOMB, and (optional) escorts into *NI_ESC.
63  * If INPUT_BOMB is not empty, use it, else prompt for more input.
64  * Same for INPUT_ESC.
65  * If we got a plane argument, initialize *NI_BOMB and *NI_ESC, and
66  * return 0.
67  * Else return -1 (*NI_BOMB and *NI_ESC may be modified).
68  */
69 int
70 get_planes(struct nstr_item *ni_bomb, struct nstr_item *ni_esc,
71            char *input_bomb, char *input_esc)
72 {
73     if (!snxtitem(ni_bomb, EF_PLANE, input_bomb, NULL))
74         return -1;
75     if (!snxtitem(ni_esc, EF_PLANE, input_esc, "escort(s)? ")) {
76         if (player->aborted)
77             return -1;
78         pr("No escorts...\n");
79     }
80     return 0;
81 }
82
83 /*
84  * Get assembly point argument.
85  * If INPUT is not empty, use it, else prompt for more input using PROMPT.
86  * If this yields a valid assembly point, read it into *AP_SECT and
87  * return AP_SECT.
88  * Else complain and return NULL.
89  * *AP_SECT and BUF[1024] may be modified in either case.
90  */
91 struct sctstr *
92 get_assembly_point(char *input, struct sctstr *ap_sect, char *buf)
93 {
94     char *p;
95     coord x, y;
96     struct nstr_item ni;
97     struct shpstr ship;
98
99     p = getstarg(input, "assembly point? ", buf);
100     if (!p || *p == 0)
101         return NULL;
102     if (!sarg_xy(p, &x, &y) || !getsect(x, y, ap_sect))
103         return NULL;
104
105     /* over own or allied sector is fine */
106     if (ap_sect->sct_own == player->cnum
107         || getrel(getnatp(ap_sect->sct_own), player->cnum) == ALLIED)
108         return ap_sect;
109
110     /* over own or allied ship is fine */
111     snxtitem_xy(&ni, EF_SHIP, x, y);
112     while (nxtitem(&ni, &ship)) {
113         if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
114             continue;
115         if (ship.shp_own == player->cnum
116             || getrel(getnatp(ship.shp_own), player->cnum) == ALLIED)
117             return ap_sect;
118     }
119
120     pr("Assembly point not owned by you or an ally!\n");
121     return NULL;
122 }
123
124 int
125 pln_onewaymission(struct sctstr *target, int *shipno, int *flagp)
126 {
127     int nships;
128     int cno;
129     int flags, fl;
130     struct shpstr ship;
131     char buf[1024];
132     char *p;
133
134     flags = *flagp;
135
136     /* offer carriers */
137     nships = carriersatxy(target->sct_x, target->sct_y, player->cnum);
138     if (nships) {
139         for (;;) {
140             p = getstring("Carrier #? ", buf);
141             if (!p)
142                 return -1;
143             if (!*p)
144                 break;
145             cno = atoi(p);
146             if (cno < 0
147                 || !getship(cno, &ship)
148                 || (!player->owner
149                     && (getrel(getnatp(ship.shp_own), player->cnum)
150                         != ALLIED))) {
151                 pr("Not yours\n");
152                 continue;
153             }
154             if (ship.shp_x != target->sct_x || ship.shp_y != target->sct_y) {
155                 pr("Ship #%d not in %s\n", cno,
156                    xyas(target->sct_x, target->sct_y, player->cnum));
157                 continue;
158             }
159             fl = carrier_planes(&ship, 0);
160             if (fl == 0) {
161                 pr("Can't land on %s.\n", prship(&ship));
162                 continue;
163             }
164             /* clear to land on ship#CNO */
165             pr("landing on carrier %d\n", cno);
166             flags |= fl;
167             *shipno = cno;
168             *flagp = flags;
169             return 0;
170         }
171     }
172
173     /* try to land at sector */
174     if (target->sct_own != player->cnum
175         && getrel(getnatp(target->sct_own), player->cnum) != ALLIED) {
176         pr("Nowhere to land at sector %s!\n",
177            xyas(target->sct_x, target->sct_y, player->cnum));
178         return -1;
179     }
180     if (target->sct_type == SCT_MOUNT) {
181         pr("Nowhere to land at sector %s!\n",
182            xyas(target->sct_x, target->sct_y, player->cnum));
183         return -1;
184     }
185     if (target->sct_type != SCT_AIRPT || target->sct_effic < 60)
186         flags |= P_V;
187
188     /* clear to land at sector */
189     *shipno = -1;
190     *flagp = flags;
191     return 0;
192 }
193
194 int
195 pln_oneway_to_carrier_ok(struct emp_qelem *bomb_list,
196                          struct emp_qelem *esc_list, int cno)
197 {
198     struct emp_qelem *list, *qp;
199     struct plist *plp;
200     struct shpstr ship;
201
202     if (cno < 0 || !getship(cno, &ship))
203         return 0;
204
205     /* for both lists */
206     for (list = bomb_list;
207          list;
208          list = list == bomb_list ? esc_list : NULL) {
209         for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
210             plp = (struct plist *)qp;
211             if (plp->plane.pln_ship == ship.shp_uid)
212                 continue;
213             if (!fit_plane_on_ship(&plp->plane, &ship))
214                 return 0;
215         }
216     }
217     return 1;
218 }
219
220 void
221 pln_newlanding(struct emp_qelem *list, coord tx, coord ty, int cno)
222 {
223     struct emp_qelem *qp;
224     struct plist *plp;
225     struct shpstr ship;
226     struct sctstr sect;
227
228     if (cno >= 0)
229         getship(cno, &ship);
230     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
231         plp = (struct plist *)qp;
232         if (cno >= 0) {
233             if (!could_be_on_ship(&plp->plane, &ship, 0, 0, 0, 0))
234                 pr("\t%s cannot land on ship #%d! %s aborts!\n",
235                    prplane(&plp->plane), cno, prplane(&plp->plane));
236             else if (!put_plane_on_ship(&plp->plane, &ship))
237                 pr("\tNo room on ship #%d! %s aborts!\n",
238                    cno, prplane(&plp->plane));
239             else {
240                 if (plp->plane.pln_own != ship.shp_own) {
241                     wu(0, ship.shp_own, "%s %s lands on your %s\n",
242                        cname(player->cnum), prplane(&plp->plane),
243                        prship(&ship));
244                 }
245             }
246         } else {
247             plp->plane.pln_x = tx;
248             plp->plane.pln_y = ty;
249             getsect(tx, ty, &sect);
250             if (plp->plane.pln_own != sect.sct_own) {
251                 wu(0, sect.sct_own,
252                    "%s %s lands at your sector %s\n",
253                    cname(player->cnum),
254                    prplane(&plp->plane), xyas(tx, ty, sect.sct_own));
255             }
256             plp->plane.pln_ship = cno;
257         }
258     }
259 }
260
261 void
262 pln_dropoff(struct emp_qelem *list, struct ichrstr *ip, coord tx, coord ty,
263             int cno)
264 {
265     struct emp_qelem *qp;
266     struct plist *plp;
267     int amt;
268     struct sctstr sect;
269     struct shpstr ship;
270     int there;
271     int max;
272
273     if (!ip)
274         return;
275     amt = 0;
276     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
277         plp = (struct plist *)qp;
278         amt += plp->misc;
279     }
280     if (cno < 0) {
281         getsect(tx, ty, &sect);
282         if (!sect.sct_own) {
283             if (sect.sct_type == SCT_WATER)
284                 pr("Your %s sink like a rock!\n", ip->i_name);
285             else
286                 pr("Your %s vanish without a trace.\n", ip->i_name);
287             return;
288         }
289         if (sect.sct_own != player->cnum
290             && getrel(getnatp(sect.sct_own), player->cnum) != ALLIED) {
291             pr("You don't own %s!  Cargo jettisoned...\n",
292                xyas(tx, ty, player->cnum));
293             return;
294         }
295         if (ip->i_uid == I_CIVIL && sect.sct_own != sect.sct_oldown) {
296             pr("%s is occupied.  Your civilians suffer from identity crisis and die.\n",
297                xyas(tx, ty, player->cnum));
298             return;
299         }
300         there = sect.sct_item[ip->i_uid];
301         max = ITEM_MAX;
302     } else {
303         getship(cno, &ship);
304         there = ship.shp_item[ip->i_uid];
305         max = mchr[ship.shp_type].m_item[ip->i_uid];
306     }
307     there += amt;
308     if (there > max) {
309         pr("%d excess %s discarded\n", max - there, ip->i_name);
310         amt = max - there;
311         there = max;
312     }
313     pr("%d %s landed safely", amt, ip->i_name);
314     if (cno < 0) {
315         sect.sct_item[ip->i_uid] = there;
316         if (sect.sct_own != player->cnum)
317             wu(0, sect.sct_own, "%s planes drop %d %s in %s\n",
318                cname(player->cnum), amt, ip->i_name,
319                xyas(tx, ty, sect.sct_own));
320         pr(" at %s\n", xyas(tx, ty, player->cnum));
321         putsect(&sect);
322     } else {
323         ship.shp_item[ip->i_uid] = there;
324         if (ship.shp_own != player->cnum)
325             wu(0, ship.shp_own, "%s planes land %d %s on carrier %d\n",
326                cname(player->cnum), amt, ip->i_name, ship.shp_uid);
327         pr(" on carrier #%d\n", ship.shp_uid);
328         putship(ship.shp_uid, &ship);
329     }
330 }
331
332 void
333 pln_mine(struct emp_qelem *list, coord tx, coord ty)
334 {
335     struct emp_qelem *qp;
336     struct plist *plp;
337     int amt;
338     struct sctstr sect;
339
340     amt = 0;
341     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
342         plp = (struct plist *)qp;
343         amt += plp->misc;
344
345     }
346     if (amt > 0) {
347         getsect(tx, ty, &sect);
348         if (sect.sct_type != SCT_WATER) {
349             pr("Your seamines have no effect here.\n");
350             return;
351         }
352         sect.sct_mines = MIN(sect.sct_mines + amt, MINES_MAX);
353         pr("%d mines laid in %s.\n", amt, xyas(tx, ty, player->cnum));
354         if (map_set(player->cnum, tx, ty, 'X', 0))
355             writemap(player->cnum);
356         putsect(&sect);
357     }
358 }
359
360 /*
361  * Has PP's type capabilities satisfying WANTFLAGS and NOWANTFLAGS?
362  * A plane type is capable unless
363  * - it lacks all of the P_F, P_ESC in WANTFLAGS, or
364  * - it lacks all of the P_E, P_L, P_K in WANTFLAGS, or
365  * - it lacks any of the other capabilities in WANTFLAGS, or
366  * - it has any of the capabilities in NOWANTFLAGS.
367  */
368 int
369 pln_capable(struct plnstr *pp, int wantflags, int nowantflags)
370 {
371     int flags = plchr[(int)pp->pln_type].pl_flags;
372
373     if (wantflags & (P_F | P_ESC)) {
374         if ((flags & wantflags & (P_F | P_ESC)) == 0)
375             return 0;
376         wantflags &= ~(P_F | P_ESC);
377     }
378
379     if (wantflags & (P_E | P_L | P_K)) {
380         if ((flags & wantflags & (P_E | P_L | P_K)) == 0)
381             return 0;
382         wantflags &= ~(P_E | P_L | P_K);
383     }
384
385     if ((flags & wantflags) != wantflags)
386         return 0;
387
388     if (flags & nowantflags)
389         return 0;
390
391     return 1;
392 }
393
394 /*
395  * Return union of capabilities of planes in LIST.
396  */
397 int
398 pln_caps(struct emp_qelem *list)
399 {
400     struct emp_qelem *qp;
401     struct plist *plp;
402     int fl;
403
404     fl = 0;
405     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
406         plp = (struct plist *)qp;
407         fl |= plp->pcp->pl_flags;
408     }
409
410     return fl;
411 }
412
413 /*
414  * Find plane types that can operate from carrier SP.
415  * If MSL find missile types, else non-missile types.
416  * Return a combination of P_L, P_K, P_E.
417  * It's zero if SP can't support air operations due to its type or
418  * state (low efficiency).
419  */
420 int
421 carrier_planes(struct shpstr *sp, int msl)
422 {
423     struct mchrstr *mcp = mchr + sp->shp_type;
424     int res;
425
426     if (sp->shp_effic < SHP_AIROPS_EFF)
427         return 0;
428
429     res = 0;
430     if (mcp->m_flags & M_FLY)
431         res |= P_L;
432     if ((mcp->m_flags & M_MSL) && msl)
433         res |= P_L;
434     if (mcp->m_nchoppers && !msl)
435         res |= P_K;
436     if (mcp->m_nxlight)
437         res |= P_E;
438     return res;
439 }
440
441 int
442 pln_airbase_ok(struct plnstr *pp, int oneway, int noisy)
443 {
444     struct shpstr ship;
445     struct lndstr land;
446     struct sctstr sect;
447     struct plchrstr *pcp = plchr + pp->pln_type;
448
449     if (CANT_HAPPEN(noisy && pp->pln_own != player->cnum))
450         noisy = 0;
451
452     if (pp->pln_ship >= 0) {
453         /* ship: needs to be own or allied, efficient */
454         if (!getship(pp->pln_ship, &ship)) {
455             CANT_REACH();
456             return 0;
457         }
458         if (ship.shp_own != pp->pln_own
459             && getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED) {
460             if (noisy)
461                 pr("(note) An ally does not own the ship %s is on\n",
462                    prplane(pp));
463             return 0;
464         }
465         if (!(carrier_planes(&ship, pcp->pl_flags & P_M) & pcp->pl_flags))
466             return 0;
467
468     } else if (pp->pln_land >= 0) {
469         /* land: needs to be own or allied, efficient, not embarked */
470         if (!getland(pp->pln_land, &land)) {
471             CANT_REACH();
472             return 0;
473         }
474         if (land.lnd_own != pp->pln_own
475             && getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED) {
476             if (noisy)
477                 pr("(note) An ally does not own the unit %s is on\n",
478                    prplane(pp));
479             return 0;
480         }
481         if (land.lnd_effic < LND_AIROPS_EFF || !(pcp->pl_flags & P_E))
482             return 0;
483         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
484             return 0;
485
486     } else {
487         /* sector: needs to be own or allied, efficient airfield */
488         if (!getsect(pp->pln_x, pp->pln_y, &sect)) {
489             CANT_REACH();
490             return 0;
491         }
492
493         if (sect.sct_own != pp->pln_own
494             && getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED) {
495             if (noisy)
496                 pr("(note) An ally does not own the sector %s is in\n",
497                    prplane(pp));
498             return 0;
499         }
500         /* need airfield unless VTOL */
501         if ((pcp->pl_flags & P_V) == 0) {
502             if (sect.sct_type != SCT_AIRPT) {
503                 if (noisy)
504                     pr("%s not at airport\n", prplane(pp));
505                 return 0;
506             }
507             if (sect.sct_effic < 40) {
508                 if (noisy)
509                     pr("%s is not 40%% efficient, %s can't take off from there.\n",
510                        xyas(sect.sct_x, sect.sct_y, pp->pln_own),
511                        prplane(pp));
512                 return 0;
513             }
514             if (!oneway && sect.sct_effic < 60) {
515                 if (noisy)
516                     pr("%s is not 60%% efficient, %s can't land there.\n",
517                        xyas(sect.sct_x, sect.sct_y, pp->pln_own),
518                        prplane(pp));
519                 return 0;
520             }
521         }
522     }
523
524     return 1;
525 }
526
527 void
528 pln_sel(struct nstr_item *ni, struct emp_qelem *list, struct sctstr *ap,
529         int ap_to_target, int rangemult, int wantflags, int nowantflags)
530 {
531     struct plnstr plane;
532     int range;
533     struct plchrstr *pcp;
534     struct plist *plp;
535
536     emp_initque(list);
537     while (nxtitem(ni, &plane)) {
538         if (!player->owner)
539             continue;
540         if (plane.pln_mobil <= 0)
541             continue;
542         if (plane.pln_effic < 40) {
543             pr("%s not efficient enough (must be 40%%)\n",
544                prplane(&plane));
545             continue;
546         }
547         if (!pln_capable(&plane, wantflags, nowantflags))
548             continue;
549         if (opt_MARKET) {
550             if (ontradingblock(EF_PLANE, &plane)) {
551                 pr("plane #%d inelligible - it's for sale.\n",
552                    plane.pln_uid);
553                 continue;
554             }
555         }
556
557         range = mapdist(plane.pln_x, plane.pln_y, ap->sct_x, ap->sct_y);
558         if (range > 4) {
559             pr("%s too far from assembly point\n", prplane(&plane));
560             continue;
561         }
562         range += ap_to_target;
563         range *= rangemult;
564         pcp = &plchr[(int)plane.pln_type];
565         if (plane.pln_range < range) {
566             pr("%s out of range (%d:%d)\n",
567                prplane(&plane), plane.pln_range, range);
568             continue;
569         }
570         if (!pln_airbase_ok(&plane, rangemult != 2, 1))
571             continue;
572         pr("%s standing by\n", prplane(&plane));
573         plane.pln_mission = 0;
574         putplane(plane.pln_uid, &plane);
575         plp = malloc(sizeof(struct plist));
576         plp->misc = 0;
577         plp->bombs = 0;
578         plp->pcp = pcp;
579         plp->plane = plane;
580         emp_insque(&plp->queue, list);
581     }
582 }
583
584 void
585 pln_arm(struct emp_qelem *list, int dist, char mission, struct ichrstr *ip,
586         int flags)
587 {
588     struct emp_qelem *qp;
589     struct emp_qelem *next;
590     struct plist *plp;
591     struct plnstr *pp;
592
593     for (qp = list->q_forw; qp != list; qp = next) {
594         next = qp->q_forw;
595         plp = (struct plist *)qp;
596         pp = &plp->plane;
597         getplane(pp->pln_uid, pp);
598         if ((pp->pln_flags & PLN_LAUNCHED)
599             || pln_equip(plp, ip, flags, mission) < 0) {
600             emp_remque(qp);
601             free(qp);
602             continue;
603         }
604         pp->pln_flags |= PLN_LAUNCHED;
605         pp->pln_mobil -= pln_mobcost(dist, pp, flags);
606         putplane(pp->pln_uid, pp);
607         pr("%s equipped\n", prplane(pp));
608     }
609 }
610
611 static int
612 pln_equip(struct plist *plp, struct ichrstr *ip, int flags, char mission)
613 {
614     struct plchrstr *pcp;
615     struct plnstr *pp;
616     int load, needed;
617     struct lndstr land;
618     struct shpstr ship;
619     struct sctstr sect;
620     i_type itype;
621     short *item;
622     int own;
623     int abandon_needed;
624
625     pp = &plp->plane;
626     pcp = plp->pcp;
627     if (pp->pln_ship >= 0) {
628         getship(pp->pln_ship, &ship);
629         item = ship.shp_item;
630         own = ship.shp_own;
631     } else if (pp->pln_land >= 0) {
632         getland(pp->pln_land, &land);
633         item = land.lnd_item;
634         own = land.lnd_own;
635     } else {
636         getsect(pp->pln_x, pp->pln_y, &sect);
637         item = sect.sct_item;
638         own = sect.sct_oldown;
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     if ((flags & P_F) == 0) {
646         load = pln_load(pp);
647         itype = I_NONE;
648         needed = 0;
649         switch (mission) {
650         case 's':               /* strategic bomb */
651         case 'p':               /* pinpoint bomb */
652             if (nuk_on_plane(pp) < 0) {
653                 itype = I_SHELL;
654                 needed = load;
655             }
656             break;
657         case 't':               /* transport */
658         case 'd':               /* drop */
659             if (!(pcp->pl_flags & P_C) || !ip)
660                 break;
661             itype = ip->i_uid;
662             needed = (load * 2) / ip->i_lbs;
663             break;
664         case 'm':               /* mine */
665             if ((pcp->pl_flags & P_MINE) == 0)
666                 break;
667             itype = I_SHELL;
668             needed = (load * 2) / ip->i_lbs;
669             break;
670         case 'a':               /* paradrop */
671             if ((pcp->pl_flags & (P_V | P_C)) == 0)
672                 break;
673             itype = I_MILIT;
674             needed = load / ip->i_lbs;
675             break;
676         case 'r':               /* reconnaissance */
677             break;
678         default:
679             CANT_REACH();
680         }
681         if (itype != I_NONE && needed <= 0) {
682             pr("%s can't contribute to mission\n", prplane(pp));
683             return -1;
684         }
685         if (itype == I_CIVIL && pp->pln_own != own) {
686             pr("You don't control those civilians!\n");
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),
728                xyas(sect.sct_x, sect.sct_y, sect.sct_own));
729         }
730         putsect(&sect);
731     }
732     return 0;
733 }
734
735 void
736 pln_put(struct emp_qelem *list)
737 {
738     struct emp_qelem *qp, *next;
739
740     for (qp = list->q_forw; qp != list; qp = next) {
741         next = qp->q_forw;
742         pln_put1((struct plist *)qp);
743     }
744 }
745
746 void
747 pln_put1(struct plist *plp)
748 {
749     struct plnstr *pp;
750     struct shpstr ship;
751     struct sctstr sect;
752
753     pp = &plp->plane;
754
755     if (CANT_HAPPEN((pp->pln_flags & PLN_LAUNCHED)
756                     && (plchr[pp->pln_type].pl_flags & P_M)
757                     && pp->pln_effic >= PLANE_MINEFF))
758         pp->pln_effic = 0;   /* bug: missile launched but not used up */
759
760     if (!(pp->pln_flags & PLN_LAUNCHED))
761         ;                       /* never took off */
762     else if (pp->pln_effic < PLANE_MINEFF) {
763         ;                       /* destroyed */
764     } else if (pp->pln_ship >= 0) {
765         /* It is landing on a carrier */
766         getship(pp->pln_ship, &ship);
767         /* We should do more, like make sure it's really
768            a carrier, etc. but for now just make sure it's
769            not sunk. */
770         if (ship.shp_effic < SHIP_MINEFF) {
771             mpr(pp->pln_own,
772                 "Ship #%d has been sunk, plane #%d has nowhere to land, and\n"
773                 "splashes into the sea.\n",
774                 pp->pln_ship, pp->pln_uid);
775             pp->pln_effic = 0;
776         }
777     } else {
778         /* Presume we are landing back in a sector. */
779         getsect(pp->pln_x, pp->pln_y, &sect);
780         if (sect.sct_type == SCT_WATER || sect.sct_type == SCT_WASTE) {
781             mpr(pp->pln_own,
782                 "Nowhere to land at %s, plane #%d crashes and burns...\n",
783                 xyas(pp->pln_x, pp->pln_y, pp->pln_own), pp->pln_uid);
784             pp->pln_effic = 0;
785         }
786     }
787     pp->pln_flags &= ~PLN_LAUNCHED;
788     putplane(pp->pln_uid, pp);
789     emp_remque(&plp->queue);
790     free(plp);
791 }
792
793 /*
794  * Can PP be loaded on a ship of SP's type?
795  * Assume that it already carries N planes, of which NCH are choppers,
796  * NXL xlight, NMSL light missiles, and the rest are light fixed-wing
797  * planes.
798  */
799 int
800 could_be_on_ship(struct plnstr *pp, struct shpstr *sp,
801                  int n, int nch, int nxl, int nmsl)
802 {
803     struct plchrstr *pcp = &plchr[pp->pln_type];
804     struct mchrstr *mcp = &mchr[sp->shp_type];
805
806     if (pcp->pl_flags & P_K)
807         nch++;
808     else if (pcp->pl_flags & P_E)
809         nxl++;
810     else if (!(pcp->pl_flags & P_L))
811         return 0;
812     else if (pcp->pl_flags & P_M)
813         nmsl++;
814     n++;
815
816     n -= MIN(nch, mcp->m_nchoppers);
817     n -= MIN(nxl, mcp->m_nxlight);
818     if (nmsl && !(mcp->m_flags & (M_MSL | M_FLY)))
819         return 0;               /* missile slots wanted */
820     if (nmsl < n && !(mcp->m_flags & M_FLY))
821         return 0;               /* fixed-wing slots wanted */
822     return n <= mcp->m_nplanes;
823 }
824
825 /*
826  * Fit a plane of PP's type on ship SP.
827  * Updating the plane accordingly is the caller's job.
828  * Return whether it fits.
829  */
830 static int
831 fit_plane_on_ship(struct plnstr *pp, struct shpstr *sp)
832 {
833     int n, nch, nxl, nmsl;
834
835     n = shp_nplane(sp, &nch, &nxl, &nmsl);
836     return could_be_on_ship(pp, sp, n, nch, nxl, nmsl);
837 }
838
839 int
840 put_plane_on_ship(struct plnstr *plane, struct shpstr *ship)
841 {
842     if (plane->pln_ship == ship->shp_uid)
843         return 1;               /* Already on ship */
844
845     if (!fit_plane_on_ship(plane, ship))
846         return 0;
847
848     plane->pln_x = ship->shp_x;
849     plane->pln_y = ship->shp_y;
850     plane->pln_ship = ship->shp_uid;
851     putplane(plane->pln_uid, plane);
852     return 1;
853 }
854
855 /*
856  * Fit a plane of PP's type on land unit LP.
857  * Updating the plane accordingly is the caller's job.
858  * Return whether it fits.
859  */
860 static int
861 fit_plane_on_land(struct plnstr *pp, struct lndstr *lp)
862 {
863     struct plchrstr *pcp = plchr + pp->pln_type;
864     struct lchrstr *lcp = lchr + lp->lnd_type;
865
866     return (pcp->pl_flags & P_E) && lnd_nxlight(lp) < lcp->l_nxlight;
867 }
868
869 int
870 put_plane_on_land(struct plnstr *plane, struct lndstr *land)
871 {
872     if (plane->pln_land == land->lnd_uid)
873         return 1;               /* Already on unit */
874
875     if (!fit_plane_on_land(plane, land))
876         return 0;
877
878     plane->pln_x = land->lnd_x;
879     plane->pln_y = land->lnd_y;
880     plane->pln_land = land->lnd_uid;
881     putplane(plane->pln_uid, plane);
882     return 1;
883 }
884
885 void
886 plane_sweep(struct emp_qelem *plane_list, coord x, coord y)
887 {
888     struct plnstr *pp;
889     struct plchrstr *pcp;
890     struct emp_qelem *qp;
891     struct emp_qelem *next;
892     struct plist *ip;
893     struct sctstr sect;
894     int mines_there;
895     int found = 0;
896
897     getsect(x, y, &sect);
898     mines_there = sect.sct_mines;
899
900     if (mines_there == 0)
901         return;
902
903     if (sect.sct_type != SCT_WATER)
904         return;
905
906     for (qp = plane_list->q_forw; ((qp != plane_list) && (mines_there));
907          qp = next) {
908         next = qp->q_forw;
909         ip = (struct plist *)qp;
910         pp = &ip->plane;
911         pcp = ip->pcp;
912         if (!(pcp->pl_flags & P_SWEEP)) /* if it isn't an sweep plane */
913             continue;
914
915         if (chance((100.0 - pln_acc(pp)) / 100.0)) {
916             pr("Sweep! in %s\n",
917                xyas(sect.sct_x, sect.sct_y, pp->pln_own));
918             mines_there--;
919             found = 1;
920         }
921     }
922
923     if (found && map_set(player->cnum, sect.sct_x, sect.sct_y, 'X', 0))
924         writemap(player->cnum);
925     sect.sct_mines = mines_there;
926     putsect(&sect);
927 }
928
929 int
930 pln_hitchance(struct plnstr *pp, int hardtarget, int type)
931 {
932     struct plchrstr *pcp = plchr + pp->pln_type;
933     double tfact = (double)(pp->pln_tech - pcp->pl_tech) /
934         (pp->pln_tech - pcp->pl_tech / 2);
935     int acc = pln_acc(pp);
936     int hitchance;
937
938     if (type == EF_SHIP) {
939         if (pcp->pl_flags & P_A)
940             acc -= 20;
941         if (!(pcp->pl_flags & P_T))
942             acc += 35;
943     }
944     hitchance = (int)(pp->pln_effic * (1.0 - 0.1 * tfact) *
945                       (1.0 - acc / 100.0)) - hardtarget;
946
947     /* smooth out the bottom of the graph with asymtote at 5 -KHS */
948     if (hitchance < 20)
949         hitchance = 5 + ldround(300.0 / (40.0 - hitchance), 1);
950     if (hitchance > 100)
951         hitchance = 100;
952     return hitchance;
953 }
954
955 /* return 0 if there was a nuclear detonation */
956
957 int
958 pln_damage(struct plnstr *pp, coord x, coord y, char type, int *nukedamp,
959            int noisy)
960 {
961     struct nukstr nuke;
962     struct plchrstr *pcp = plchr + pp->pln_type;
963     int load, i;
964     int hitroll;
965     int dam = 0;
966     int aim;
967     int effective = 1;
968     int pinbomber = 0;
969
970     if (getnuke(nuk_on_plane(pp), &nuke)) {
971         mpr(pp->pln_own, "Releasing RV's for %s detonation...\n",
972             pp->pln_flags & PLN_AIRBURST ? "airburst" : "groundburst");
973         *nukedamp = detonate(&nuke, x, y,
974                              pp->pln_flags & PLN_AIRBURST);
975         return 0;
976     }
977     *nukedamp = 0;
978
979     load = pln_load(pp);
980     if (!load)                 /* e.g. ab, blowing up on launch pad */
981         return 0;
982
983     i = roll(load) + 1;
984     if (i > load)
985         i = load;
986
987     if (pcp->pl_flags & P_M) {
988         if (pcp->pl_flags & P_MAR)
989             pinbomber = 1;
990     } else if (pcp->pl_flags & P_T)
991         pinbomber = 1;
992
993     aim = pln_acc(pp);
994     if (type == 's') {
995         effective = !pinbomber;
996         aim = 30 + (pinbomber ? aim : 100 - aim);
997     } else {
998         effective = pinbomber;
999         aim = 100 - aim;
1000     }
1001
1002     while (i--) {
1003         dam += roll(6);
1004         hitroll = roll(100);
1005         if (hitroll >= 90) {
1006             dam += 8;
1007             if (noisy)
1008                 mpr(pp->pln_own, "BLAM");
1009         } else if (hitroll < aim) {
1010             dam += 5;
1011             if (noisy)
1012                 mpr(pp->pln_own, "Blam");
1013         } else {
1014             dam += 1;
1015             if (noisy)
1016                 mpr(pp->pln_own, "blam");
1017         }
1018         if (i && noisy)
1019             mpr(pp->pln_own, "-");
1020     }
1021     if (noisy)
1022         mpr(pp->pln_own, "\n");
1023     if (effective)
1024         dam *= 2;
1025     return dam;
1026 }
1027
1028 int
1029 pln_identchance(struct plnstr *pp, int hardtarget, int type)
1030 {
1031     double misschance =
1032         (100.0 - pln_hitchance(pp, hardtarget, type)) / 100.0;
1033     return (int)(100 - 100 * misschance * misschance);
1034 }
1035
1036 int
1037 pln_mobcost(int dist, struct plnstr *pp, int flags)
1038 {
1039     double cost;
1040
1041     cost = 20.0 / (pp->pln_effic / 100.0);
1042     if ((flags & P_F) || (flags & P_ESC))
1043         cost /= 2;
1044
1045     return ldround(cost * dist / pln_range_max(pp) + 5, 1);
1046 }
1047
1048 int
1049 pln_is_in_orbit(struct plnstr *pp)
1050 {
1051     return (plchr[pp->pln_type].pl_flags & (P_M | P_O)) == P_O
1052         && (pp->pln_flags & PLN_LAUNCHED);
1053 }
1054
1055 /*
1056  * Set PP's tech to TLEV along with everything else that depends on it.
1057  */
1058 void
1059 pln_set_tech(struct plnstr *pp, int tlev)
1060 {
1061     struct plchrstr *pcp = plchr + pp->pln_type;
1062     int limited_range = pp->pln_range < pln_range_max(pp);
1063     int range_max;
1064
1065     if (CANT_HAPPEN(tlev < pcp->pl_tech))
1066         tlev = pcp->pl_tech;
1067     pp->pln_tech = tlev;
1068
1069     range_max = pln_range_max(pp);
1070     if (!limited_range || pp->pln_range > range_max)
1071         pp->pln_range = range_max;
1072 }