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
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.
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.
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
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.
28 * plnsub.c: Plane subroutine stuff
30 * Known contributors to this file:
33 * Steve McClure, 1998-2000
34 * Markus Armbruster, 2004-2005
53 #include "prototypes.h"
58 static int pln_equip(struct plist *, struct ichrstr *, int, char);
59 static int fit_plane_on_ship(struct plnstr *, struct shpstr *);
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
66 * Else complain and return NULL.
67 * *AP_SECT and BUF[1024] may be modified in either case.
70 get_assembly_point(char *input, struct sctstr *ap_sect, char *buf)
77 p = getstarg(input, "assembly point? ", buf);
80 if (!sarg_xy(p, &x, &y) || !getsect(x, y, ap_sect))
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)
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)
93 if (ship.shp_own == player->cnum
94 || getrel(getnatp(ship.shp_own), player->cnum) == ALLIED)
98 pr("Assembly point not owned by you or an ally!\n");
103 pln_onewaymission(struct sctstr *target, int *shipno, int *flagp)
115 nships = carriersatxy(target->sct_x, target->sct_y,
116 M_FLY | M_CHOPPER, 0, player->cnum);
119 if (!(p = getstarg(0, "Carrier #? ", buf)) || !*p)
123 || !getship(cno, &ship)
125 && (getrel(getnatp(ship.shp_own), player->cnum)
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));
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));
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)
147 if (mchr[(int)ship.shp_type].m_flags & M_CHOPPER)
149 if (mchr[(int)ship.shp_type].m_flags & M_XLIGHT)
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));
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));
169 if (target->sct_type != SCT_AIRPT || target->sct_effic < 60)
172 /* clear to land at sector */
179 pln_oneway_to_carrier_ok(struct emp_qelem *bomb_list,
180 struct emp_qelem *esc_list, int cno)
182 struct emp_qelem *list, *qp;
186 if (cno < 0 || !getship(cno, &ship))
192 for (list = bomb_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)
199 if (!fit_plane_on_ship(&plp->plane, &ship))
207 pln_newlanding(struct emp_qelem *list, coord tx, coord ty, int cno)
209 struct emp_qelem *qp;
216 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
217 plp = (struct plist *)qp;
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));
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),
235 plp->plane.pln_x = tx;
236 plp->plane.pln_y = ty;
237 getsect(tx, ty, §);
238 if (plp->plane.pln_own != sect.sct_own) {
239 /* plp->plane.pln_own = sect.sct_own;*/
241 "%s %s lands at your sector %s\n",
243 prplane(&plp->plane), xyas(tx, ty, sect.sct_own));
245 plp->plane.pln_ship = cno;
249 putship(ship.shp_uid, &ship);
253 pln_dropoff(struct emp_qelem *list, struct ichrstr *ip, coord tx, coord ty,
256 struct emp_qelem *qp;
259 struct sctstr *sectp;
268 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
269 plp = (struct plist *)qp;
272 if (type == EF_SECTOR) {
274 if (!sectp->sct_own) {
275 if (sectp->sct_type == SCT_WATER)
276 pr("Your %s sink like a rock!\n", ip->i_name);
278 pr("Your %s vanish without a trace.\n", ip->i_name);
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));
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));
292 there = sectp->sct_item[ip->i_uid];
296 there = sp->shp_item[ip->i_uid];
297 mp = &mchr[(int)sp->shp_type];
298 max = mp->m_item[ip->i_uid];
302 pr("%d excess %s discarded\n", max - there, ip->i_name);
306 pr("%d %s landed safely", amt, ip->i_name);
307 if (type == EF_SECTOR) {
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);
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);
328 pln_mine(struct emp_qelem *list, struct sctstr *sectp)
330 struct emp_qelem *qp;
335 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
336 plp = (struct plist *)qp;
341 if (sectp->sct_type != SCT_WATER) {
342 pr("Your seamines have no effect here.\n");
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);
355 * Has PP's type capabilities satisfying WANTFLAGS and NOWANTFLAGS?
356 * A plane type is capable 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 capabilities in WANTFLAGS, or
360 * - it has any of the capabilities in NOWANTFLAGS.
363 pln_capable(struct plnstr *pp, int wantflags, int nowantflags)
365 int flags = plchr[(int)pp->pln_type].pl_flags;
367 if (wantflags & (P_F | P_ESC)) {
368 if ((flags & wantflags & (P_F | P_ESC)) == 0)
370 wantflags &= ~(P_F | P_ESC);
373 if (wantflags & (P_E | P_L | P_K)) {
374 if ((flags & wantflags & (P_E | P_L | P_K)) == 0)
376 wantflags &= ~(P_E | P_L | P_K);
379 if ((flags & wantflags) != wantflags)
382 if (flags & nowantflags)
389 pln_airbase_ok(struct plnstr *pp, int oneway, int noisy)
394 struct plchrstr *pcp = plchr + pp->pln_type;
396 if (CANT_HAPPEN(noisy && pp->pln_own != player->cnum))
399 if (pp->pln_ship >= 0) {
400 /* ship: needs to be own or allied, efficient */
401 if (!getship(pp->pln_ship, &ship)) {
405 if (CANT_HAPPEN(ship.shp_effic < SHIP_MINEFF
406 || !could_be_on_ship(pp, &ship)))
409 if (ship.shp_own != pp->pln_own
410 && getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED) {
412 pr("(note) An ally does not own the ship %s is on\n",
416 if (ship.shp_effic < SHP_AIROPS_EFF)
419 } else if (pp->pln_land >= 0) {
420 /* land: needs to be own or allied, efficient, not embarked */
421 if (!getland(pp->pln_land, &land)) {
425 if (CANT_HAPPEN(land.lnd_effic < LAND_MINEFF
426 || !(pcp->pl_flags & P_E)))
429 if (land.lnd_own != pp->pln_own
430 && getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED) {
432 pr("(note) An ally does not own the unit %s is on\n",
436 if (land.lnd_effic < LND_AIROPS_EFF)
438 if (land.lnd_ship >= 0 || land.lnd_land >= 0)
442 /* sector: needs to be own or allied, efficient airfield */
443 if (!getsect(pp->pln_x, pp->pln_y, §)) {
448 if (sect.sct_own != pp->pln_own
449 && getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED) {
451 pr("(note) An ally does not own the sector %s is in\n",
455 /* need airfield unless VTOL */
456 if ((pcp->pl_flags & P_V) == 0) {
457 if (sect.sct_type != SCT_AIRPT) {
459 pr("%s not at airport\n", prplane(pp));
462 if (sect.sct_effic < 40) {
464 pr("%s is not 40%% efficient, %s can't take off from there.\n",
465 xyas(sect.sct_x, sect.sct_y, pp->pln_own),
469 if (!oneway && sect.sct_effic < 60) {
471 pr("%s is not 60%% efficient, %s can't land there.\n",
472 xyas(sect.sct_x, sect.sct_y, pp->pln_own),
483 pln_sel(struct nstr_item *ni, struct emp_qelem *list, struct sctstr *ap,
484 int ap_to_target, int rangemult, int wantflags, int nowantflags)
488 struct plchrstr *pcp;
495 while (nxtitem(ni, &plane)) {
498 if (plane.pln_mobil <= 0)
501 if (ontradingblock(EF_PLANE, &plane)) {
502 pr("plane #%d inelligible - it's for sale.\n",
508 range = mapdist(plane.pln_x, plane.pln_y, ap->sct_x, ap->sct_y);
510 pr("%s too far from assembly point\n", prplane(&plane));
513 if (plane.pln_effic < 40) {
514 pr("%s not efficient enough (must be 40%%)\n",
518 range += ap_to_target;
520 pcp = &plchr[(int)plane.pln_type];
521 if (!pln_capable(&plane, wantflags, nowantflags))
523 if (plane.pln_range < range) {
524 pr("%s out of range (%d:%d)\n",
525 prplane(&plane), plane.pln_range, range);
528 if (!pln_airbase_ok(&plane, rangemult != 2, 1))
530 pr("%s standing by\n", prplane(&plane));
531 plane.pln_mission = 0;
532 putplane(plane.pln_uid, &plane);
533 plp = malloc(sizeof(struct plist));
538 emp_insque(&plp->queue, list);
543 pln_arm(struct emp_qelem *list, int dist, char mission, struct ichrstr *ip,
544 int flags, int mission_flags)
546 struct emp_qelem *qp;
547 struct emp_qelem *next;
550 for (qp = list->q_forw; qp != list; qp = next) {
552 plp = (struct plist *)qp;
553 if (pln_equip(plp, ip, flags, mission) < 0) {
558 if (flags & (P_S | P_I)) {
559 if (plp->pcp->pl_flags & P_S)
560 mission_flags |= P_S;
561 if (plp->pcp->pl_flags & P_I)
562 mission_flags |= P_I;
564 if (!(plp->pcp->pl_flags & P_H))
565 /* no stealth on this mission */
566 mission_flags &= ~P_H;
567 if (!(plp->pcp->pl_flags & P_X))
568 /* no stealth on this mission */
569 mission_flags &= ~P_X;
570 if (!(plp->pcp->pl_flags & P_A)) {
571 /* no asw on this mission */
572 mission_flags &= ~P_A;
573 /* FIXME escorts turn ASW patrol into ordinary recon */
575 if (!(plp->pcp->pl_flags & P_MINE)) {
576 /* no asw on this mission */
577 mission_flags &= ~P_MINE;
578 /* FIXME no effect */
580 plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
581 pr("%s equipped\n", prplane(&plp->plane));
583 return mission_flags;
587 pln_equip(struct plist *plp, struct ichrstr *ip, int flags, char mission)
589 struct plchrstr *pcp;
603 if (pp->pln_ship >= 0) {
604 getship(pp->pln_ship, &ship);
605 item = ship.shp_item;
607 } else if (pp->pln_land >= 0) {
608 getland(pp->pln_land, &land);
609 item = land.lnd_item;
612 getsect(pp->pln_x, pp->pln_y, §);
613 item = sect.sct_item;
614 own = sect.sct_oldown;
617 if (ip->i_uid == I_CIVIL) {
618 if (pp->pln_own != own) {
619 pr("You don't control those civilians!\n");
624 if (pcp->pl_fuel > item[I_PETROL]) {
625 pr("%s not enough petrol there!\n", prplane(pp));
628 item[I_PETROL] -= pcp->pl_fuel;
630 if ((flags & P_F) == 0) {
636 if (pp->pln_nuketype == -1) {
638 needed = pp->pln_load;
643 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
646 needed = (pp->pln_load * 2) / ip->i_lbs;
649 if ((pcp->pl_flags & P_MINE) == 0)
652 needed = (pp->pln_load * 2) / ip->i_lbs;
655 if ((pcp->pl_flags & (P_V | P_C)) == 0)
658 needed = pp->pln_load / ip->i_lbs;
661 if (pp->pln_nuketype == -1)
667 if (rval < 0 || (itype != I_NONE && needed <= 0)) {
668 pr("%s can't contribute to mission\n", prplane(pp));
671 if (itype != I_NONE) {
673 /* Supply is broken somewhere, so don't use it for now */
674 if (itype == I_SHELL && item[itype] < needed)
675 item[itype] += supply_commod(plp->plane.pln_own,
680 abandon_needed = !!would_abandon(§, itype, needed, NULL);
681 if (item[itype] < needed + abandon_needed) {
682 pr("Not enough %s for %s\n", ichr[itype].i_name, prplane(pp));
685 item[itype] -= needed;
687 if (itype == I_SHELL && (mission == 's' || mission == 'p'))
692 if (pp->pln_ship >= 0) {
693 if (pp->pln_own != ship.shp_own) {
695 "%s %s prepares for takeoff from ship %s\n",
696 cname(pp->pln_own), prplane(pp), prship(&ship));
698 putship(ship.shp_uid, &ship);
699 } else if (pp->pln_land >= 0) {
700 if (pp->pln_own != land.lnd_own) {
702 "%s %s prepares for takeoff from unit %s\n",
703 cname(pp->pln_own), prplane(pp), prland(&land));
705 putland(land.lnd_uid, &land);
707 if (pp->pln_own != sect.sct_own) {
708 wu(0, sect.sct_own, "%s %s prepares for takeoff from %s\n",
709 cname(pp->pln_own), prplane(pp),
710 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
718 pln_put(struct emp_qelem *list)
720 struct emp_qelem *qp;
721 struct emp_qelem *newqp;
727 /* Here is where planes return home from bombing runs.
728 We need to make sure they still have somewhere to return
732 plp = (struct plist *)qp;
734 /* Ok, check out where it wants to land */
735 if (pp->pln_ship >= 0) {
736 /* It is landing on a carrier */
737 getship(pp->pln_ship, &ship);
738 /* We should do more, like make sure it's really
739 a carrier, etc. but for now just make sure it's
741 if (ship.shp_effic < SHIP_MINEFF) {
743 "Ship #%d has been sunk, plane #%d has nowhere to land, and\n"
744 "splashes into the sea.\n",
745 pp->pln_ship, pp->pln_uid);
749 /* Presume we are landing back in a sector. */
750 getsect(pp->pln_x, pp->pln_y, §);
751 if (sect.sct_type == SCT_WATER || sect.sct_type == SCT_WASTE) {
753 "Nowhere to land at %s, plane #%d crashes and burns...\n",
754 xyas(pp->pln_x, pp->pln_y, pp->pln_own), pp->pln_uid);
758 putplane(pp->pln_uid, pp);
767 pln_removedupes(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
769 struct emp_qelem *bomb;
770 struct emp_qelem *esc;
774 if (QEMPTY(bomb_list) || QEMPTY(esc_list))
776 bomb = bomb_list->q_forw;
777 while (bomb != bomb_list) {
778 if (QEMPTY(esc_list)) {
782 esc = esc_list->q_forw;
783 bombp = (struct plist *)bomb;
784 while (esc != esc_list) {
785 escp = (struct plist *)esc;
786 if (escp->plane.pln_uid == bombp->plane.pln_uid) {
798 * Fit a plane of PP's type on ship SP.
799 * Adjust SP's plane counters.
800 * Updating the plane accordingly is the caller's job.
801 * Return whether it fits.
804 fit_plane_on_ship(struct plnstr *pp, struct shpstr *sp)
806 struct plchrstr *pcp = plchr + pp->pln_type;
807 struct mchrstr *mcp = mchr + sp->shp_type;
810 if (pcp->pl_flags & P_K) {
811 /* chopper, try chopper slot first */
812 if (sp->shp_nchoppers < mcp->m_nchoppers)
813 return ++sp->shp_nchoppers;
814 /* else try plane slot */
816 } else if (pcp->pl_flags & P_E) {
817 /* x-light, try x-light slot first */
818 if (sp->shp_nxlight < mcp->m_nxlight)
819 return ++sp->shp_nxlight;
820 /* else try plane slot */
821 wanted = M_MSL | M_FLY;
822 } else if (!(pcp->pl_flags & P_L)) {
823 /* not light, no go */
825 } else if (pcp->pl_flags & P_M) {
826 /* missile, use plane slot */
827 wanted = M_MSL | M_FLY;
829 /* fixed-wing plane, use plane slot */
833 if ((mcp->m_flags & wanted) == 0)
834 return 0; /* ship not capable */
836 if (sp->shp_nplane < mcp->m_nplanes)
837 return ++sp->shp_nplane;
843 * Fit a plane of PP's type off ship SP.
844 * Adjust SP's plane counters, badly. You need to run count_planes()
845 * before the next fit_plane_on_ship().
846 * Updating the plane accordingly is the caller's job.
849 fit_plane_off_ship(struct plnstr *pp, struct shpstr *sp)
852 * Frees chopper and nxlight slots first, which is why we need to
853 * run count_planes() before fit_plane_on_ship().
855 struct plchrstr *pcp = plchr + pp->pln_type;
857 if (pcp->pl_flags & P_K) {
858 if (sp->shp_nchoppers) {
862 } else if (pcp->pl_flags & P_E) {
863 if (sp->shp_nxlight) {
869 if (CANT_HAPPEN(sp->shp_nplane == 0))
875 put_plane_on_ship(struct plnstr *plane, struct shpstr *ship)
877 if (plane->pln_ship == ship->shp_uid)
878 return 1; /* Already on ship */
880 if (!fit_plane_on_ship(plane, ship))
883 plane->pln_x = ship->shp_x;
884 plane->pln_y = ship->shp_y;
885 plane->pln_ship = ship->shp_uid;
886 putplane(plane->pln_uid, plane);
887 putship(ship->shp_uid, ship);
892 take_plane_off_ship(struct plnstr *plane, struct shpstr *ship)
894 if (CANT_HAPPEN(plane->pln_ship != ship->shp_uid))
897 fit_plane_off_ship(plane, ship);
898 plane->pln_ship = -1;
899 putship(ship->shp_uid, ship);
900 putplane(plane->pln_uid, plane);
904 * Fit a plane of PP's type on land unit LP.
905 * Adjust LP's plane counters.
906 * Updating the plane accordingly is the caller's job.
907 * Return whether it fits.
910 fit_plane_on_land(struct plnstr *pp, struct lndstr *lp)
912 struct plchrstr *pcp = plchr + pp->pln_type;
913 struct lchrstr *lcp = lchr + lp->lnd_type;
915 if ((pcp->pl_flags & P_E) && lp->lnd_nxlight < lcp->l_nxlight)
916 return ++lp->lnd_nxlight;
922 * Fit a plane of PP's type off land unit LP.
923 * Adjust LP's plane counters.
924 * Updating the plane accordingly is the caller's job.
927 fit_plane_off_land(struct plnstr *pp, struct lndstr *lp)
929 struct plchrstr *pcp = plchr + pp->pln_type;
931 if (CANT_HAPPEN(lp->lnd_nxlight == 0))
937 put_plane_on_land(struct plnstr *plane, struct lndstr *land)
939 if (plane->pln_land == land->lnd_uid)
940 return 1; /* Already on unit */
942 if (!fit_plane_on_land(plane, land))
945 plane->pln_x = land->lnd_x;
946 plane->pln_y = land->lnd_y;
947 plane->pln_land = land->lnd_uid;
948 putplane(plane->pln_uid, plane);
949 putland(land->lnd_uid, land);
954 take_plane_off_land(struct plnstr *plane, struct lndstr *land)
956 if (CANT_HAPPEN(plane->pln_ship != land->lnd_uid))
959 fit_plane_off_land(plane, land);
960 plane->pln_land = -1;
961 putland(land->lnd_uid, land);
962 putplane(plane->pln_uid, plane);
966 * Could a plane of PP's type be on on ship SP?
969 could_be_on_ship(struct plnstr *pp, struct shpstr *sp)
974 ship.shp_nplane = ship.shp_nchoppers = ship.shp_nxlight = 0;
975 return fit_plane_on_ship(pp, &ship);
979 plane_sweep(struct emp_qelem *plane_list, coord x, coord y)
982 struct plchrstr *pcp;
983 struct emp_qelem *qp;
984 struct emp_qelem *next;
990 getsect(x, y, §);
991 mines_there = sect.sct_mines;
993 if (mines_there == 0)
996 if ((sect.sct_type != SCT_WATER) && (sect.sct_type != SCT_HARBR))
999 for (qp = plane_list->q_forw; ((qp != plane_list) && (mines_there));
1002 ip = (struct plist *)qp;
1005 if (!(pcp->pl_flags & P_SWEEP)) /* if it isn't an sweep plane */
1008 if (chance((100.0 - pp->pln_acc) / 100.0)) {
1009 pr("Sweep! in %s\n",
1010 xyas(sect.sct_x, sect.sct_y, pp->pln_own));
1016 if (found && map_set(player->cnum, sect.sct_x, sect.sct_y, 'X', 0))
1017 writemap(player->cnum);
1018 sect.sct_mines = mines_there;
1023 count_planes(struct shpstr *sp)
1025 struct nstr_item ni;
1026 struct plnstr plane;
1027 int nplane, nchoppers, nxlight;
1029 if (sp->shp_effic < SHIP_MINEFF)
1032 nplane = sp->shp_nplane;
1034 nchoppers = sp->shp_nchoppers;
1035 sp->shp_nchoppers = 0;
1036 nxlight = sp->shp_nxlight;
1037 sp->shp_nxlight = 0;
1039 snxtitem_xy(&ni, EF_PLANE, sp->shp_x, sp->shp_y);
1040 while (nxtitem(&ni, &plane)) {
1041 if (plane.pln_own == 0)
1043 if (plane.pln_ship == sp->shp_uid) {
1044 if (!fit_plane_on_ship(&plane, sp))
1049 if (nplane != sp->shp_nplane ||
1050 nxlight != sp->shp_nxlight || nchoppers != sp->shp_nchoppers) {
1051 putship(sp->shp_uid, sp);
1056 count_land_planes(struct lndstr *lp)
1058 struct nstr_item ni;
1059 struct plnstr plane;
1062 if (lp->lnd_effic < LAND_MINEFF)
1065 nplane = lp->lnd_nxlight;
1066 lp->lnd_nxlight = 0;
1068 snxtitem_all(&ni, EF_PLANE);
1069 while (nxtitem(&ni, &plane)) {
1070 if (plane.pln_own == 0)
1072 if (plane.pln_land == lp->lnd_uid)
1073 if (!fit_plane_on_land(&plane, lp))
1077 if (lp->lnd_nxlight != nplane)
1078 putland(lp->lnd_uid, lp);
1082 count_sect_planes(struct sctstr *sp)
1085 struct nstr_item ni;
1086 struct plnstr plane;
1088 snxtitem_all(&ni, EF_PLANE);
1089 while (nxtitem(&ni, &plane)) {
1092 if (plane.pln_flags & PLN_LAUNCHED)
1094 if (plane.pln_x == sp->sct_x && plane.pln_y == sp->sct_y)
1102 pln_hitchance(struct plnstr *pp, int hardtarget, int type)
1104 struct plchrstr *pcp = plchr + pp->pln_type;
1105 double tfact = (double)(pp->pln_tech - pcp->pl_tech) /
1106 (pp->pln_tech - pcp->pl_tech / 2);
1107 int acc = pp->pln_acc;
1110 if (type == EF_SHIP) {
1111 if (pcp->pl_flags & P_A)
1113 if (!(pcp->pl_flags & P_T))
1116 hitchance = (int)(pp->pln_effic * (1.0 - 0.1 * tfact) *
1117 (1.0 - acc / 100.0)) - hardtarget;
1119 /* smooth out the bottom of the graph with asymtote at 5 -KHS */
1121 hitchance = 5 + ldround(300.0 / (40.0 - hitchance), 1);
1122 if (hitchance > 100)
1127 /* return 0 if there was a nuclear detonation */
1130 pln_damage(struct plnstr *pp, coord x, coord y, char type, int *nukedamp,
1134 struct plchrstr *pcp = plchr + pp->pln_type;
1142 if (pp->pln_nuketype != -1) {
1143 if (nuk_on_plane(&nuke, pp->pln_uid) >= 0) {
1144 mpr(pp->pln_own, "Releasing RV's for %s detonation...\n",
1145 pp->pln_flags & PLN_AIRBURST ? "airburst" : "groundburst");
1146 pp->pln_nuketype = -1;
1147 *nukedamp = detonate(&nuke, x, y,
1148 pp->pln_flags & PLN_AIRBURST);
1155 if (!pp->pln_load) /* e.g. ab, blowing up on launch pad */
1158 i = roll(pp->pln_load) + 1;
1159 if (i > pp->pln_load)
1162 if (pcp->pl_flags & P_M) {
1163 if (pcp->pl_flags & P_MAR)
1165 } else if (pcp->pl_flags & P_T)
1168 aim = 100 - pp->pln_acc;
1182 hitroll = roll(100);
1183 if (hitroll >= 90) {
1186 mpr(pp->pln_own, "BLAM");
1187 } else if (hitroll < aim) {
1190 mpr(pp->pln_own, "Blam");
1194 mpr(pp->pln_own, "blam");
1197 mpr(pp->pln_own, "-");
1200 mpr(pp->pln_own, "\n");
1207 pln_identchance(struct plnstr *pp, int hardtarget, int type)
1210 (100.0 - pln_hitchance(pp, hardtarget, type)) / 100.0;
1211 return (int)(100 - 100 * misschance * misschance);
1215 pln_mobcost(int dist, struct plnstr *pp, int flags)
1219 cost = 20.0 / (pp->pln_effic / 100.0);
1220 if ((flags & P_F) || (flags & P_ESC))
1223 return ldround(cost * dist / pp->pln_range_max + 5, 1);
1227 * Set PP's tech to TLEV along with everything else that depends on it.
1230 pln_set_tech(struct plnstr *pp, int tlev)
1232 struct plchrstr *pcp = plchr + pp->pln_type;
1233 int tech_diff = tlev - pcp->pl_tech;
1234 int limited_range = pp->pln_range < pp->pln_range_max;
1236 if (CANT_HAPPEN(tech_diff < 0)) {
1241 pp->pln_tech = tlev;
1242 pp->pln_att = PLN_ATTDEF(pcp->pl_att, tech_diff);
1243 pp->pln_def = PLN_ATTDEF(pcp->pl_def, tech_diff);
1244 pp->pln_acc = PLN_ACC(pcp->pl_acc, tech_diff);
1245 pp->pln_range_max = PLN_RAN(pcp->pl_range, tech_diff);
1246 pp->pln_load = PLN_LOAD(pcp->pl_load, tech_diff);
1248 if (!limited_range || pp->pln_range > pp->pln_range_max)
1249 pp->pln_range = pp->pln_range_max;