]> git.pond.sub.org Git - empserver/blob - src/lib/subs/plnsub.c
(pln_wanted): Rewrite. Old version was incomprehensible, slow and
[empserver] / src / lib / subs / plnsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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-2005
35  */
36
37 #include <config.h>
38
39 #include <math.h>
40 #include "file.h"
41 #include "item.h"
42 #include "land.h"
43 #include "lost.h"
44 #include "map.h"
45 #include "misc.h"
46 #include "nat.h"
47 #include "nsc.h"
48 #include "nuke.h"
49 #include "optlist.h"
50 #include "path.h"
51 #include "plane.h"
52 #include "player.h"
53 #include "prototypes.h"
54 #include "sect.h"
55 #include "ship.h"
56 #include "xy.h"
57
58 static int pln_equip(struct plist *, struct ichrstr *, int, char);
59 static int fit_plane_on_ship(struct plnstr *, struct shpstr *);
60
61 /*
62  * Get assembly point argument.
63  * If INPUT is not empty, use it, else prompt for more input using PROMPT.
64  * If this yields a valid assembly point, read it into *AP_SECT and
65  * return AP_SECT.
66  * Else complain and return NULL.
67  * *AP_SECT and BUF[1024] may be modified in either case.
68  */
69 struct sctstr *
70 get_assembly_point(char *input, struct sctstr *ap_sect, char *buf)
71 {
72     char *p;
73     coord x, y;
74     struct nstr_item ni;
75     struct shpstr ship;
76
77     p = getstarg(input, "assembly point? ", buf);
78     if (!p || *p == 0)
79         return NULL;
80     if (!sarg_xy(p, &x, &y) || !getsect(x, y, ap_sect))
81         return NULL;
82
83     /* over own or allied sector is fine */
84     if (ap_sect->sct_own == player->cnum
85         || getrel(getnatp(ap_sect->sct_own), player->cnum) == ALLIED)
86         return ap_sect;
87
88     /* over own or allied ship is fine */
89     snxtitem_xy(&ni, EF_SHIP, x, y);
90     while (nxtitem(&ni, &ship)) {
91         if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
92             continue;
93         if (ship.shp_own == player->cnum
94             || getrel(getnatp(ship.shp_own), player->cnum) == ALLIED)
95             return ap_sect;
96     }
97
98     pr("Assembly point not owned by you or an ally!\n");
99     return NULL;
100 }
101
102 int
103 pln_onewaymission(struct sctstr *target, int *shipno, int *flagp)
104 {
105     int nships;
106     int cno;
107     int flags;
108     struct shpstr ship;
109     char buf[1024];
110     char *p;
111
112     flags = *flagp;
113
114     /* offer carriers */
115     nships = carriersatxy(target->sct_x, target->sct_y,
116                           M_FLY | M_CHOPPER, 0, player->cnum);
117     if (nships) {
118         for (;;) {
119             if (!(p = getstarg(0, "Carrier #? ", buf)) || !*p)
120                 break;
121             cno = atoi(p);
122             if (cno < 0
123                 || !getship(cno, &ship)
124                 || (!player->owner
125                     && (getrel(getnatp(ship.shp_own), player->cnum)
126                         != ALLIED))) {
127                 pr("Not yours\n");
128                 continue;
129             }
130             if (ship.shp_x != target->sct_x || ship.shp_y != target->sct_y) {
131                 pr("Ship #%d not in %s\n", cno,
132                    xyas(target->sct_x, target->sct_y, player->cnum));
133                 continue;
134             }
135             if ((!(mchr[(int)ship.shp_type].m_flags & M_FLY)
136                  && !(mchr[(int)ship.shp_type].m_flags & M_XLIGHT)
137                  && !(mchr[(int)ship.shp_type].m_flags & M_CHOPPER))
138                 || ship.shp_effic < SHP_AIROPS_EFF) {
139                 pr("Can't land on %s.\n", prship(&ship));
140                 continue;
141             }
142
143             /* clear to land on ship#CNO */
144             pr("landing on carrier %d\n", cno);
145             if (mchr[(int)ship.shp_type].m_flags & M_FLY)
146                 flags |= P_L;
147             if (mchr[(int)ship.shp_type].m_flags & M_CHOPPER)
148                 flags |= P_K;
149             if (mchr[(int)ship.shp_type].m_flags & M_XLIGHT)
150                 flags |= P_E;
151             *shipno = cno;
152             *flagp = flags;
153             return 0;
154         }
155     }
156
157     /* try to land at sector */
158     if (target->sct_own != player->cnum
159         && getrel(getnatp(target->sct_own), player->cnum) != ALLIED) {
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_MOUNT) {
165         pr("Nowhere to land at sector %s!\n",
166            xyas(target->sct_x, target->sct_y, player->cnum));
167         return -1;
168     }
169     if (target->sct_type != SCT_AIRPT || target->sct_effic < 60)
170         flags |= P_V;
171
172     /* clear to land at sector */
173     *shipno = -1;
174     *flagp = flags;
175     return 0;
176 }
177
178 int
179 pln_oneway_to_carrier_ok(struct emp_qelem *bomb_list,
180                          struct emp_qelem *esc_list, int cno)
181 {
182     struct emp_qelem *list, *qp;
183     struct plist *plp;
184     struct shpstr ship;
185
186     if (cno < 0 || !getship(cno, &ship))
187         return 0;
188
189     count_planes(&ship);
190
191     /* for both lists */
192     for (list = bomb_list;
193          list;
194          list = list == bomb_list ? esc_list : NULL) {
195         for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
196             plp = (struct plist *)qp;
197             if (plp->plane.pln_ship == ship.shp_uid)
198                 continue;
199             if (!fit_plane_on_ship(&plp->plane, &ship))
200                 return 0;
201         }
202     }
203     return 1;
204 }
205
206 void
207 pln_newlanding(struct emp_qelem *list, coord tx, coord ty, int cno)
208 {
209     struct emp_qelem *qp;
210     struct plist *plp;
211     struct shpstr ship;
212     struct sctstr sect;
213
214     if (cno >= 0)
215         getship(cno, &ship);
216     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
217         plp = (struct plist *)qp;
218         if (cno >= 0) {
219             count_planes(&ship);
220             if (!could_be_on_ship(&plp->plane, &ship))
221                 pr("\t%s cannot land on ship #%d! %s aborts!\n",
222                    prplane(&plp->plane), cno, prplane(&plp->plane));
223             else if (!put_plane_on_ship(&plp->plane, &ship))
224                 pr("\tNo room on ship #%d! %s aborts!\n",
225                    cno, prplane(&plp->plane));
226             else {
227                 if (plp->plane.pln_own != ship.shp_own) {
228 /*                                      plp->plane.pln_own = ship.shp_own;*/
229                     wu(0, ship.shp_own, "%s %s lands on your %s\n",
230                        cname(player->cnum), prplane(&plp->plane),
231                        prship(&ship));
232                 }
233             }
234         } else {
235             plp->plane.pln_x = tx;
236             plp->plane.pln_y = ty;
237             getsect(tx, ty, &sect);
238             if (plp->plane.pln_own != sect.sct_own) {
239 /*                              plp->plane.pln_own = sect.sct_own;*/
240                 wu(0, sect.sct_own,
241                    "%s %s lands at your sector %s\n",
242                    cname(player->cnum),
243                    prplane(&plp->plane), xyas(tx, ty, sect.sct_own));
244             }
245             plp->plane.pln_ship = cno;
246         }
247     }
248     if (cno >= 0)
249         putship(ship.shp_uid, &ship);
250 }
251
252 void
253 pln_dropoff(struct emp_qelem *list, struct ichrstr *ip, coord tx, coord ty,
254             void *ptr, int type)
255 {
256     struct emp_qelem *qp;
257     struct plist *plp;
258     int amt;
259     struct sctstr *sectp;
260     struct shpstr *sp;
261     int there;
262     int max;
263     struct mchrstr *mp;
264
265     if (ip == 0)
266         return;
267     amt = 0;
268     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
269         plp = (struct plist *)qp;
270         amt += plp->misc;
271     }
272     if (type == EF_SECTOR) {
273         sectp = ptr;
274         if (!sectp->sct_own) {
275             if (sectp->sct_type == SCT_WATER)
276                 pr("Your %s sink like a rock!\n", ip->i_name);
277             else
278                 pr("Your %s vanish without a trace.\n", ip->i_name);
279             return;
280         }
281         if (sectp->sct_own != player->cnum
282             && getrel(getnatp(sectp->sct_own), player->cnum) != ALLIED) {
283             pr("You don't own %s!  Cargo jettisoned...\n",
284                xyas(tx, ty, player->cnum));
285             return;
286         }
287         if (ip->i_uid == I_CIVIL && sectp->sct_own != sectp->sct_oldown) {
288             pr("%s is occupied.  Your civilians suffer from identity crisis and die.\n",
289                xyas(tx, ty, player->cnum));
290             return;
291         }
292         there = sectp->sct_item[ip->i_uid];
293         max = ITEM_MAX;
294     } else {
295         sp = ptr;
296         there = sp->shp_item[ip->i_uid];
297         mp = &mchr[(int)sp->shp_type];
298         max = mp->m_item[ip->i_uid];
299     }
300     there += amt;
301     if (there > max) {
302         pr("%d excess %s discarded\n", max - there, ip->i_name);
303         amt = max - there;
304         there = max;
305     }
306     pr("%d %s landed safely", amt, ip->i_name);
307     if (type == EF_SECTOR) {
308         sectp = ptr;
309         sectp->sct_item[ip->i_uid] = there;
310         if (sectp->sct_own != player->cnum)
311             wu(0, sectp->sct_own, "%s planes drop %d %s in %s\n",
312                cname(player->cnum), amt, ip->i_name,
313                xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
314         pr(" at %s\n", xyas(tx, ty, player->cnum));
315         putsect((struct sctstr *)ptr);
316     } else {
317         struct shpstr *sp = (struct shpstr *)ptr;
318         sp->shp_item[ip->i_uid] = there;
319         if (sp->shp_own != player->cnum)
320             wu(0, sp->shp_own, "%s planes land %d %s on carrier %d\n",
321                cname(player->cnum), amt, ip->i_name, sp->shp_uid);
322         pr(" on carrier #%d\n", sp->shp_uid);
323         putship(sp->shp_uid, sp);
324     }
325 }
326
327 void
328 pln_mine(struct emp_qelem *list, struct sctstr *sectp)
329 {
330     struct emp_qelem *qp;
331     struct plist *plp;
332     int amt;
333
334     amt = 0;
335     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
336         plp = (struct plist *)qp;
337         amt += plp->misc;
338
339     }
340     if (amt > 0) {
341         if (sectp->sct_type != SCT_WATER) {
342             pr("Your seamines have no effect here.\n");
343             return;
344         }
345         sectp->sct_mines = MIN(sectp->sct_mines + amt, MINES_MAX);
346         pr("%d mines laid in %s.\n", amt,
347            xyas(sectp->sct_x, sectp->sct_y, player->cnum));
348         if (map_set(player->cnum, sectp->sct_x, sectp->sct_y, 'X', 0))
349             writemap(player->cnum);
350         putsect(sectp);
351     }
352 }
353
354 /*
355  * Is a plane of PP's type wanted?
356  * A plane type is wanted unless
357  * - it lacks all of the P_F, P_ESC in wantflags, or
358  * - it lacks all of the P_E, P_L, P_K in wantflags, or
359  * - it lacks any of the other flags in wantflags, or
360  * - it has any of the flags in nowantflags.
361  */
362 static int
363 pln_wanted(struct plnstr *pp, int wantflags, int nowantflags)
364 {
365     int flags = plchr[(int)pp->pln_type].pl_flags;
366
367     if (wantflags & (P_F | P_ESC)) {
368         if ((flags & wantflags & (P_F | P_ESC)) == 0)
369             return 0;
370         wantflags &= ~(P_F | P_ESC);
371     }
372
373     if (wantflags & (P_E | P_L | P_K)) {
374         if ((flags & wantflags & (P_E | P_L | P_K)) == 0)
375             return 0;
376         wantflags &= ~(P_E | P_L | P_K);
377     }
378
379     if ((flags & wantflags) != wantflags)
380         return 0;
381
382     if (flags & nowantflags)
383         return 0;
384
385     return 1;
386 }
387
388 void
389 pln_sel(struct nstr_item *ni, struct emp_qelem *list, struct sctstr *ap,
390         int ap_to_target, int rangemult, int wantflags, int nowantflags)
391 {
392     struct plnstr plane;
393     struct shpstr ship;
394     struct lndstr land;
395     struct sctstr sect;
396     int range;
397     struct plchrstr *pcp;
398     struct plist *plp;
399     int y;
400     int bad, bad1;
401     unsigned x;
402
403     emp_initque(list);
404     while (nxtitem(ni, &plane)) {
405         if (!player->owner)
406             continue;
407         if (plane.pln_mobil <= 0)
408             continue;
409         if (opt_MARKET) {
410             if (ontradingblock(EF_PLANE, &plane)) {
411                 pr("plane #%d inelligible - it's for sale.\n",
412                    plane.pln_uid);
413                 continue;
414             }
415         }
416
417         range = mapdist(plane.pln_x, plane.pln_y, ap->sct_x, ap->sct_y);
418         if (range > 4) {
419             pr("%s too far from assembly point\n", prplane(&plane));
420             continue;
421         }
422         if (plane.pln_effic < 40) {
423             pr("%s not efficient enough (must be 40%%)\n",
424                prplane(&plane));
425             continue;
426         }
427         range += ap_to_target;
428         range *= rangemult;
429         pcp = &plchr[(int)plane.pln_type];
430         if (!pln_wanted(&plane, wantflags, nowantflags))
431             continue;
432         if (plane.pln_range < range) {
433             pr("%s out of range (%d:%d)\n",
434                prplane(&plane), plane.pln_range, range);
435             continue;
436         }
437         if (plane.pln_ship >= 0) {
438             if (!getship(plane.pln_ship, &ship) ||
439                 plane.pln_own != player->cnum) {
440               shipsunk:
441                 plane.pln_effic = 0;
442                 pr("(note) ship not valid for %s\n", prplane(&plane));
443                 putplane(plane.pln_uid, &plane);
444                 continue;
445             }
446             if (!could_be_on_ship(&plane, &ship))
447                 goto shipsunk;
448             if (ship.shp_effic < SHIP_MINEFF)
449                 goto shipsunk;
450             if (ship.shp_effic < SHP_AIROPS_EFF)
451                 continue;
452             /* Can't fly off non-owned ships or non-allied ship */
453             if ((ship.shp_own != player->cnum) &&
454                 (getrel(getnatp(ship.shp_own), player->cnum) != ALLIED)) {
455                 pr("(note) An ally does not own the ship %s is on\n",
456                    prplane(&plane));
457                 continue;
458             }
459         }
460         if (plane.pln_land >= 0) {
461             if (!getland(plane.pln_land, &land) ||
462                 (plane.pln_own != player->cnum)) {
463               landdead:
464                 plane.pln_effic = 0;
465                 pr("(note) land unit not valid for %s\n", prplane(&plane));
466                 putplane(plane.pln_uid, &plane);
467                 continue;
468             }
469             if (!(plchr[(int)plane.pln_type].pl_flags & P_E))
470                 goto landdead;
471             if (land.lnd_effic < LAND_MINEFF)
472                 goto landdead;
473             if (land.lnd_effic < LND_AIROPS_EFF)
474                 continue;
475             /* Can't fly off units in ships or other units */
476             if ((land.lnd_ship >= 0) || (land.lnd_land >= 0))
477                 continue;
478             /* Can't fly off non-owned units or non-allied unit */
479             if ((land.lnd_own != player->cnum) &&
480                 (getrel(getnatp(land.lnd_own), player->cnum) != ALLIED)) {
481                 pr("(note) An ally does not own the unit %s is on\n",
482                    prplane(&plane));
483                 continue;
484             }
485         }
486         /* Now, check the sector status if not on a plane or unit */
487         if ((plane.pln_ship < 0) && (plane.pln_land < 0)) {
488             if (!getsect(plane.pln_x, plane.pln_y, &sect))
489                 continue;
490             /* First, check allied status */
491             /* Can't fly from non-owned sectors or non-allied sectors */
492             if ((sect.sct_own != player->cnum) &&
493                 (getrel(getnatp(sect.sct_own), player->cnum) != ALLIED)) {
494                 pr("(note) An ally does not own the sector %s is in\n",
495                    prplane(&plane));
496                 continue;
497             }
498             /* non-vtol plane */
499             if ((pcp->pl_flags & P_V) == 0) {
500                 if (sect.sct_type != SCT_AIRPT) {
501                     pr("%s not at airport\n", prplane(&plane));
502                     continue;
503                 }
504                 if (sect.sct_effic < 40) {
505                     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));
506                     continue;
507                 }
508                 if (rangemult == 2 && sect.sct_effic < 60) {
509                     pr("%s is not 60%% efficient, %s can't land there.\n",
510                        xyas(sect.sct_x, sect.sct_y, plane.pln_own),
511                        prplane(&plane));
512                     continue;
513                 }
514             }
515         }
516         pr("%s standing by\n", prplane(&plane));
517         plane.pln_mission = 0;
518         putplane(plane.pln_uid, &plane);
519         plp = malloc(sizeof(struct plist));
520         plp->misc = 0;
521         plp->bombs = 0;
522         plp->pcp = pcp;
523         plp->plane = plane;
524         emp_insque(&plp->queue, list);
525     }
526 }
527
528 int
529 pln_arm(struct emp_qelem *list, int dist, char mission, struct ichrstr *ip,
530         int flags, int mission_flags)
531 {
532     struct emp_qelem *qp;
533     struct emp_qelem *next;
534     struct plist *plp;
535
536     for (qp = list->q_forw; qp != list; qp = next) {
537         next = qp->q_forw;
538         plp = (struct plist *)qp;
539         if (pln_equip(plp, ip, flags, mission) < 0) {
540             emp_remque(qp);
541             free(qp);
542             continue;
543         }
544         if (flags & (P_S | P_I)) {
545             if (plp->pcp->pl_flags & P_S)
546                 mission_flags |= P_S;
547             if (plp->pcp->pl_flags & P_I)
548                 mission_flags |= P_I;
549         }
550         if (!(plp->pcp->pl_flags & P_H))
551             /* no stealth on this mission */
552             mission_flags &= ~P_H;
553         if (!(plp->pcp->pl_flags & P_X))
554             /* no stealth on this mission */
555             mission_flags &= ~P_X;
556         if (!(plp->pcp->pl_flags & P_A)) {
557             /* no asw on this mission */
558             mission_flags &= ~P_A;
559             /* FIXME escorts turn ASW patrol into ordinary recon */
560         }
561         if (!(plp->pcp->pl_flags & P_MINE)) {
562             /* no asw on this mission */
563             mission_flags &= ~P_MINE;
564             /* FIXME no effect */
565         }
566         plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
567         pr("%s equipped\n", prplane(&plp->plane));
568     }
569     return mission_flags;
570 }
571
572 static int
573 pln_equip(struct plist *plp, struct ichrstr *ip, int flags, char mission)
574 {
575     struct plchrstr *pcp;
576     struct plnstr *pp;
577     int needed;
578     struct lndstr land;
579     struct shpstr ship;
580     struct sctstr sect;
581     i_type itype;
582     int rval;
583     short *item;
584     int own;
585     int abandon_needed;
586
587     pp = &plp->plane;
588     pcp = plp->pcp;
589     if (pp->pln_ship >= 0) {
590         getship(pp->pln_ship, &ship);
591         item = ship.shp_item;
592         own = ship.shp_own;
593     } else if (pp->pln_land >= 0) {
594         getland(pp->pln_land, &land);
595         item = land.lnd_item;
596         own = land.lnd_own;
597     } else {
598         getsect(pp->pln_x, pp->pln_y, &sect);
599         item = sect.sct_item;
600         own = sect.sct_oldown;
601     }
602     if (ip) {
603         if (ip->i_uid == I_CIVIL) {
604             if (pp->pln_own != own) {
605                 pr("You don't control those civilians!\n");
606                 return -1;
607             }
608         }
609     }
610     if (pcp->pl_fuel > item[I_PETROL]) {
611         pr("%s not enough petrol there!\n", prplane(pp));
612         return -1;
613     }
614     item[I_PETROL] -= pcp->pl_fuel;
615     rval = 0;
616     if ((flags & P_F) == 0) {
617         itype = I_NONE;
618         needed = 0;
619         switch (mission) {
620         case 's':
621         case 'p':
622             if (pp->pln_nuketype == -1) {
623                 itype = I_SHELL;
624                 needed = pp->pln_load;
625             }
626             break;
627         case 't':
628         case 'd':
629             if ((pcp->pl_flags & P_C) == 0 || ip == 0)
630                 break;
631             itype = ip->i_uid;
632             needed = (pp->pln_load * 2) / ip->i_lbs;
633             break;
634         case 'm':
635             if ((pcp->pl_flags & P_MINE) == 0)
636                 break;
637             itype = I_SHELL;
638             needed = (pp->pln_load * 2) / ip->i_lbs;
639             break;
640         case 'a':
641             if ((pcp->pl_flags & (P_V | P_C)) == 0)
642                 break;
643             itype = I_MILIT;
644             needed = pp->pln_load / ip->i_lbs;
645             break;
646         case 'n':
647             if (pp->pln_nuketype == -1)
648                 rval = -1;
649             break;
650         default:
651             break;
652         }
653         if (rval < 0 || (itype != I_NONE && needed <= 0)) {
654             pr("%s can't contribute to mission\n", prplane(pp));
655             return -1;
656         }
657         if (itype != I_NONE) {
658 #if 0
659             /* Supply is broken somewhere, so don't use it for now */
660             if (itype == I_SHELL && item[itype] < needed)
661                 item[itype] += supply_commod(plp->plane.pln_own,
662                                              plp->plane.pln_x,
663                                              plp->plane.pln_y,
664                                              I_SHELL, needed);
665 #endif
666             abandon_needed = !!would_abandon(&sect, itype, needed, NULL);
667             if (item[itype] < needed + abandon_needed) {
668                 pr("Not enough %s for %s\n", ichr[itype].i_name, prplane(pp));
669                 return -1;
670             }
671             item[itype] -= needed;
672         }
673         if (itype == I_SHELL && (mission == 's' || mission == 'p'))
674             plp->bombs = needed;
675         else
676             plp->misc = needed;
677     }
678     if (pp->pln_ship >= 0) {
679         if (pp->pln_own != ship.shp_own) {
680             wu(0, ship.shp_own,
681                "%s %s prepares for takeoff from ship %s\n",
682                cname(pp->pln_own), prplane(pp), prship(&ship));
683         }
684         putship(ship.shp_uid, &ship);
685     } else if (pp->pln_land >= 0) {
686         if (pp->pln_own != land.lnd_own) {
687             wu(0, land.lnd_own,
688                "%s %s prepares for takeoff from unit %s\n",
689                cname(pp->pln_own), prplane(pp), prland(&land));
690         }
691         putland(land.lnd_uid, &land);
692     } else {
693         if (pp->pln_own != sect.sct_own) {
694             wu(0, sect.sct_own, "%s %s prepares for takeoff from %s\n",
695                cname(pp->pln_own), prplane(pp),
696                xyas(sect.sct_x, sect.sct_y, sect.sct_own));
697         }
698         putsect(&sect);
699     }
700     return rval;
701 }
702
703 void
704 pln_put(struct emp_qelem *list)
705 {
706     struct emp_qelem *qp;
707     struct emp_qelem *newqp;
708     struct plist *plp;
709     struct plnstr *pp;
710     struct shpstr ship;
711     struct sctstr sect;
712
713     /* Here is where planes return home from bombing runs.
714        We need to make sure they still have somewhere to return
715        home to! */
716     qp = list->q_forw;
717     while (qp != list) {
718         plp = (struct plist *)qp;
719         pp = &plp->plane;
720         /* Ok, check out where it wants to land */
721         if (pp->pln_ship >= 0) {
722             /* It is landing on a carrier */
723             getship(pp->pln_ship, &ship);
724             /* We should do more, like make sure it's really
725                a carrier, etc. but for now just make sure it's
726                not sunk. */
727             if (ship.shp_effic < SHIP_MINEFF) {
728                 mpr(pp->pln_own,
729                     "Ship #%d has been sunk, plane #%d has nowhere to land, and\n"
730                     "splashes into the sea.\n",
731                     pp->pln_ship, pp->pln_uid);
732                 pp->pln_effic = 0;
733             }
734         } else {
735             /* Presume we are landing back in a sector. */
736             getsect(pp->pln_x, pp->pln_y, &sect);
737             if (sect.sct_type == SCT_WATER || sect.sct_type == SCT_WASTE) {
738                 mpr(pp->pln_own,
739                     "Nowhere to land at %s, plane #%d crashes and burns...\n",
740                     xyas(pp->pln_x, pp->pln_y, pp->pln_own), pp->pln_uid);
741                 pp->pln_effic = 0;
742             }
743         }
744         putplane(pp->pln_uid, pp);
745         newqp = qp->q_forw;
746         emp_remque(qp);
747         free(qp);
748         qp = newqp;
749     }
750 }
751
752 void
753 pln_removedupes(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
754 {
755     struct emp_qelem *bomb;
756     struct emp_qelem *esc;
757     struct plist *bombp;
758     struct plist *escp;
759
760     if (QEMPTY(bomb_list) || QEMPTY(esc_list))
761         return;
762     bomb = bomb_list->q_forw;
763     while (bomb != bomb_list) {
764         if (QEMPTY(esc_list)) {
765             bomb = bomb_list;
766             continue;
767         }
768         esc = esc_list->q_forw;
769         bombp = (struct plist *)bomb;
770         while (esc != esc_list) {
771             escp = (struct plist *)esc;
772             if (escp->plane.pln_uid == bombp->plane.pln_uid) {
773                 emp_remque(esc);
774                 free(esc);
775                 esc = esc_list;
776             } else
777                 esc = esc->q_forw;
778         }
779         bomb = bomb->q_forw;
780     }
781 }
782
783 /*
784  * Fit a plane of PP's type on ship SP.
785  * Adjust SP's plane counters.
786  * Updating the plane accordingly is the caller's job.
787  * Return whether it fits.
788  */
789 static int
790 fit_plane_on_ship(struct plnstr *pp, struct shpstr *sp)
791 {
792     struct plchrstr *pcp = plchr + pp->pln_type;
793     struct mchrstr *mcp = mchr + sp->shp_type;
794     int wanted;
795
796     if (pcp->pl_flags & P_K) {
797         /* chopper, try chopper slot first */
798         if (sp->shp_nchoppers < mcp->m_nchoppers)
799             return ++sp->shp_nchoppers;
800         /* else try plane slot */
801         wanted = M_FLY;
802     } else if (pcp->pl_flags & P_E) {
803         /* x-light, try x-light slot first */
804         if (sp->shp_nxlight < mcp->m_nxlight)
805             return ++sp->shp_nxlight;
806         /* else try plane slot */
807         wanted = M_MSL | M_FLY;
808     } else if (!(pcp->pl_flags & P_L)) {
809         /* not light, no go */
810         wanted = 0;
811     } else if (pcp->pl_flags & P_M) {
812         /* missile, use plane slot */
813         wanted = M_MSL | M_FLY;
814     } else {
815         /* fixed-wing plane, use plane slot */
816         wanted = M_FLY;
817     }
818
819     if ((mcp->m_flags & wanted) == 0)
820         return 0;               /* ship not capable */
821
822     if (sp->shp_nplane < mcp->m_nplanes)
823         return ++sp->shp_nplane;
824
825     return 0;
826 }
827
828 /*
829  * Fit a plane of PP's type off ship SP.
830  * Adjust SP's plane counters, badly.  You need to run count_planes()
831  * before the next fit_plane_on_ship().
832  * Updating the plane accordingly is the caller's job.
833  */
834 static void
835 fit_plane_off_ship(struct plnstr *pp, struct shpstr *sp)
836 {
837     /*
838      * Frees chopper and nxlight slots first, which is why we need to
839      * run count_planes() before fit_plane_on_ship().
840      */
841     struct plchrstr *pcp = plchr + pp->pln_type;
842
843     if (pcp->pl_flags & P_K) {
844         if (sp->shp_nchoppers) {
845             sp->shp_nchoppers--;
846             return;
847         }
848     } else if (pcp->pl_flags & P_E) {
849         if (sp->shp_nxlight) {
850             sp->shp_nxlight--;
851             return;
852         }
853     }
854
855     if (CANT_HAPPEN(sp->shp_nplane == 0))
856         sp->shp_nplane = 1;
857     sp->shp_nplane--;
858 }
859
860 int
861 put_plane_on_ship(struct plnstr *plane, struct shpstr *ship)
862 {
863     if (plane->pln_ship == ship->shp_uid)
864         return 1;               /* Already on ship */
865
866     if (!fit_plane_on_ship(plane, ship))
867         return 0;
868
869     plane->pln_x = ship->shp_x;
870     plane->pln_y = ship->shp_y;
871     plane->pln_ship = ship->shp_uid;
872     putplane(plane->pln_uid, plane);
873     putship(ship->shp_uid, ship);
874     return 1;
875 }
876
877 void
878 take_plane_off_ship(struct plnstr *plane, struct shpstr *ship)
879 {
880     if (CANT_HAPPEN(plane->pln_ship != ship->shp_uid))
881         return;
882
883     fit_plane_off_ship(plane, ship);
884     plane->pln_ship = -1;
885     putship(ship->shp_uid, ship);
886     putplane(plane->pln_uid, plane);
887 }
888
889 /*
890  * Fit a plane of PP's type on land unit LP.
891  * Adjust LP's plane counters.
892  * Updating the plane accordingly is the caller's job.
893  * Return whether it fits.
894  */
895 static int
896 fit_plane_on_land(struct plnstr *pp, struct lndstr *lp)
897 {
898     struct plchrstr *pcp = plchr + pp->pln_type;
899     struct lchrstr *lcp = lchr + lp->lnd_type;
900
901     if ((pcp->pl_flags & P_E) && lp->lnd_nxlight < lcp->l_nxlight)
902         return ++lp->lnd_nxlight;
903
904     return 0;
905 }
906
907 /*
908  * Fit a plane of PP's type off land unit LP.
909  * Adjust LP's plane counters.
910  * Updating the plane accordingly is the caller's job.
911  */
912 static void
913 fit_plane_off_land(struct plnstr *pp, struct lndstr *lp)
914 {
915     struct plchrstr *pcp = plchr + pp->pln_type;
916
917     if (CANT_HAPPEN(lp->lnd_nxlight == 0))
918         lp->lnd_nxlight = 1;
919     lp->lnd_nxlight--;
920 }
921
922 int
923 put_plane_on_land(struct plnstr *plane, struct lndstr *land)
924 {
925     if (plane->pln_land == land->lnd_uid)
926         return 1;               /* Already on unit */
927
928     if (!fit_plane_on_land(plane, land))
929         return 0;
930
931     plane->pln_x = land->lnd_x;
932     plane->pln_y = land->lnd_y;
933     plane->pln_land = land->lnd_uid;
934     putplane(plane->pln_uid, plane);
935     putland(land->lnd_uid, land);
936     return 1;
937 }
938
939 void
940 take_plane_off_land(struct plnstr *plane, struct lndstr *land)
941 {
942     if (CANT_HAPPEN(plane->pln_ship != land->lnd_uid))
943         return;
944
945     fit_plane_off_land(plane, land);
946     plane->pln_land = -1;
947     putland(land->lnd_uid, land);
948     putplane(plane->pln_uid, plane);
949 }
950
951 /*
952  * Could a plane of PP's type be on on ship SP?
953  */
954 int
955 could_be_on_ship(struct plnstr *pp, struct shpstr *sp)
956 {
957     struct shpstr ship;
958
959     ship = *sp;
960     ship.shp_nplane = ship.shp_nchoppers = ship.shp_nxlight = 0;
961     return fit_plane_on_ship(pp, &ship);
962 }
963
964 void
965 plane_sweep(struct emp_qelem *plane_list, coord x, coord y)
966 {
967     struct plnstr *pp;
968     struct plchrstr *pcp;
969     struct emp_qelem *qp;
970     struct emp_qelem *next;
971     struct plist *ip;
972     struct sctstr sect;
973     int mines_there;
974     int found = 0;
975
976     getsect(x, y, &sect);
977     mines_there = sect.sct_mines;
978
979     if (mines_there == 0)
980         return;
981
982     if ((sect.sct_type != SCT_WATER) && (sect.sct_type != SCT_HARBR))
983         return;
984
985     for (qp = plane_list->q_forw; ((qp != plane_list) && (mines_there));
986          qp = next) {
987         next = qp->q_forw;
988         ip = (struct plist *)qp;
989         pp = &ip->plane;
990         pcp = ip->pcp;
991         if (!(pcp->pl_flags & P_SWEEP)) /* if it isn't an sweep plane */
992             continue;
993
994         if (chance((100.0 - pp->pln_acc) / 100.0)) {
995             pr("Sweep! in %s\n",
996                xyas(sect.sct_x, sect.sct_y, pp->pln_own));
997             mines_there--;
998             found = 1;
999         }
1000     }
1001
1002     if (found && map_set(player->cnum, sect.sct_x, sect.sct_y, 'X', 0))
1003         writemap(player->cnum);
1004     sect.sct_mines = mines_there;
1005     putsect(&sect);
1006 }
1007
1008 void
1009 count_planes(struct shpstr *sp)
1010 {
1011     struct nstr_item ni;
1012     struct plnstr plane;
1013     int nplane, nchoppers, nxlight;
1014
1015     if (sp->shp_effic < SHIP_MINEFF)
1016         return;
1017
1018     nplane = sp->shp_nplane;
1019     sp->shp_nplane = 0;
1020     nchoppers = sp->shp_nchoppers;
1021     sp->shp_nchoppers = 0;
1022     nxlight = sp->shp_nxlight;
1023     sp->shp_nxlight = 0;
1024
1025     snxtitem_xy(&ni, EF_PLANE, sp->shp_x, sp->shp_y);
1026     while (nxtitem(&ni, &plane)) {
1027         if (plane.pln_own == 0)
1028             continue;
1029         if (plane.pln_ship == sp->shp_uid) {
1030             if (!fit_plane_on_ship(&plane, sp))
1031                 CANT_REACH();
1032         }
1033     }
1034
1035     if (nplane != sp->shp_nplane ||
1036         nxlight != sp->shp_nxlight || nchoppers != sp->shp_nchoppers) {
1037         putship(sp->shp_uid, sp);
1038     }
1039 }
1040
1041 void
1042 count_land_planes(struct lndstr *lp)
1043 {
1044     struct nstr_item ni;
1045     struct plnstr plane;
1046     int nplane;
1047
1048     if (lp->lnd_effic < LAND_MINEFF)
1049         return;
1050
1051     nplane = lp->lnd_nxlight;
1052     lp->lnd_nxlight = 0;
1053
1054     snxtitem_all(&ni, EF_PLANE);
1055     while (nxtitem(&ni, &plane)) {
1056         if (plane.pln_own == 0)
1057             continue;
1058         if (plane.pln_land == lp->lnd_uid)
1059             if (!fit_plane_on_land(&plane, lp))
1060                 CANT_REACH();
1061     }
1062
1063     if (lp->lnd_nxlight != nplane)
1064         putland(lp->lnd_uid, lp);
1065 }
1066
1067 int
1068 count_sect_planes(struct sctstr *sp)
1069 {
1070     int count = 0;
1071     struct nstr_item ni;
1072     struct plnstr plane;
1073
1074     snxtitem_all(&ni, EF_PLANE);
1075     while (nxtitem(&ni, &plane)) {
1076         if (!plane.pln_own)
1077             continue;
1078         if (plane.pln_flags & PLN_LAUNCHED)
1079             continue;
1080         if (plane.pln_x == sp->sct_x && plane.pln_y == sp->sct_y)
1081             ++count;
1082     }
1083
1084     return count;
1085 }
1086
1087 int
1088 pln_hitchance(struct plnstr *pp, int hardtarget, int type)
1089 {
1090     struct plchrstr *pcp = plchr + pp->pln_type;
1091     double tfact = (double)(pp->pln_tech - pcp->pl_tech) /
1092         (pp->pln_tech - pcp->pl_tech / 2);
1093     int acc = pp->pln_acc;
1094     int hitchance;
1095
1096     if (type == EF_SHIP) {
1097         if (pcp->pl_flags & P_A)
1098             acc -= 20;
1099         if (!(pcp->pl_flags & P_T))
1100             acc += 35;
1101     }
1102     hitchance = (int)(pp->pln_effic * (1.0 - 0.1 * tfact) *
1103                       (1.0 - acc / 100.0)) - hardtarget;
1104
1105     /* smooth out the bottom of the graph with asymtote at 5 -KHS */
1106     if (hitchance < 20)
1107         hitchance = 5 + ldround(300.0 / (40.0 - hitchance), 1);
1108     if (hitchance > 100)
1109         hitchance = 100;
1110     return hitchance;
1111 }
1112
1113 /* return 0 if there was a nuclear detonation */
1114
1115 int
1116 pln_damage(struct plnstr *pp, coord x, coord y, char type, int *nukedamp,
1117            int noisy)
1118 {
1119     struct nukstr nuke;
1120     struct plchrstr *pcp = plchr + pp->pln_type;
1121     int i;
1122     int hitroll;
1123     int dam = 0;
1124     int aim;
1125     int effective = 1;
1126     int pinbomber = 0;
1127
1128     if (pp->pln_nuketype != -1) {
1129         if (nuk_on_plane(&nuke, pp->pln_uid) >= 0) {
1130             mpr(pp->pln_own, "Releasing RV's for %s detonation...\n",
1131                 pp->pln_flags & PLN_AIRBURST ? "airburst" : "groundburst");
1132             pp->pln_nuketype = -1;
1133             *nukedamp = detonate(&nuke, x, y,
1134                                  pp->pln_flags & PLN_AIRBURST);
1135             return 0;
1136         }
1137         CANT_REACH();
1138     }
1139     *nukedamp = 0;
1140
1141     if (!pp->pln_load)          /* e.g. ab, blowing up on launch pad */
1142         return 0;
1143
1144     i = roll(pp->pln_load) + 1;
1145     if (i > pp->pln_load)
1146         i = pp->pln_load;
1147
1148     if (pcp->pl_flags & P_M) {
1149         if (pcp->pl_flags & P_MAR)
1150             pinbomber = 1;
1151     } else if (pcp->pl_flags & P_T)
1152         pinbomber = 1;
1153
1154     aim = 100 - pp->pln_acc;
1155     if (type == 's') {
1156         if (pinbomber) {
1157             aim = pp->pln_acc;
1158             effective = 0;
1159         }
1160         aim += 30;
1161     } else {
1162         if (!pinbomber) {
1163             effective = 0;
1164         }
1165     }
1166     while (i--) {
1167         dam += roll(6);
1168         hitroll = roll(100);
1169         if (hitroll >= 90) {
1170             dam += 8;
1171             if (noisy)
1172                 mpr(pp->pln_own, "BLAM");
1173         } else if (hitroll < aim) {
1174             dam += 5;
1175             if (noisy)
1176                 mpr(pp->pln_own, "Blam");
1177         } else {
1178             dam += 1;
1179             if (noisy)
1180                 mpr(pp->pln_own, "blam");
1181         }
1182         if (i && noisy)
1183             mpr(pp->pln_own, "-");
1184     }
1185     if (noisy)
1186         mpr(pp->pln_own, "\n");
1187     if (effective)
1188         dam *= 2;
1189     return dam;
1190 }
1191
1192 int
1193 pln_identchance(struct plnstr *pp, int hardtarget, int type)
1194 {
1195     double misschance =
1196         (100.0 - pln_hitchance(pp, hardtarget, type)) / 100.0;
1197     return (int)(100 - 100 * misschance * misschance);
1198 }
1199
1200 int
1201 pln_mobcost(int dist, struct plnstr *pp, int flags)
1202 {
1203     double cost;
1204
1205     cost = 20.0 / (pp->pln_effic / 100.0);
1206     if ((flags & P_F) || (flags & P_ESC))
1207         cost /= 2;
1208
1209     return ldround(cost * dist / pp->pln_range_max + 5, 1);
1210 }
1211
1212 /*
1213  * Set PP's tech to TLEV along with everything else that depends on it.
1214  */
1215 void
1216 pln_set_tech(struct plnstr *pp, int tlev)
1217 {
1218     struct plchrstr *pcp = plchr + pp->pln_type;
1219     int tech_diff = tlev - pcp->pl_tech;
1220     int limited_range = pp->pln_range < pp->pln_range_max;
1221
1222     if (CANT_HAPPEN(tech_diff < 0)) {
1223       tlev -= tech_diff;
1224       tech_diff = 0;
1225     }
1226
1227     pp->pln_tech = tlev;
1228     pp->pln_att = PLN_ATTDEF(pcp->pl_att, tech_diff);
1229     pp->pln_def = PLN_ATTDEF(pcp->pl_def, tech_diff);
1230     pp->pln_acc = PLN_ACC(pcp->pl_acc, tech_diff);
1231     pp->pln_range_max = PLN_RAN(pcp->pl_range, tech_diff);
1232     pp->pln_load = PLN_LOAD(pcp->pl_load, tech_diff);
1233
1234     if (!limited_range || pp->pln_range > pp->pln_range_max)
1235         pp->pln_range = pp->pln_range_max;
1236 }