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