2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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
52 #include "prototypes.h"
55 static int pln_equip(struct plist *, struct ichrstr *, int, char);
58 * Get assembly point argument.
59 * If INPUT is not empty, use it, else prompt for more input using PROMPT.
60 * If this yields a valid assembly point, read it into *AP_SECT and
62 * Else complain and return NULL.
63 * *AP_SECT and BUF[1024] may be modified in either case.
66 get_assembly_point(char *input, struct sctstr *ap_sect, char *buf)
73 p = getstarg(input, "assembly point? ", buf);
76 if (!sarg_xy(p, &x, &y) || !getsect(x, y, ap_sect))
79 /* over own or allied sector is fine */
80 if (ap_sect->sct_own == player->cnum
81 || getrel(getnatp(ap_sect->sct_own), player->cnum) == ALLIED)
84 /* over own or allied ship is fine */
85 snxtitem_xy(&ni, EF_SHIP, x, y);
86 while (nxtitem(&ni, &ship)) {
87 if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
89 if (ship.shp_own == player->cnum
90 || getrel(getnatp(ship.shp_own), player->cnum) == ALLIED)
94 pr("Assembly point not owned by you or an ally!\n");
99 pln_onewaymission(struct sctstr *target, int *shipno, int *flagp)
111 nships = carriersatxy(target->sct_x, target->sct_y,
112 M_FLY | M_CHOPPER, 0, player->cnum);
115 if (!(p = getstarg(0, "Carrier #? ", buf)) || !*p)
119 || !getship(cno, &ship)
121 && (getrel(getnatp(ship.shp_own), player->cnum)
126 if (ship.shp_x != target->sct_x || ship.shp_y != target->sct_y) {
127 pr("Ship #%d not in %s\n", cno,
128 xyas(target->sct_x, target->sct_y, player->cnum));
131 if ((!(mchr[(int)ship.shp_type].m_flags & M_FLY)
132 && !(mchr[(int)ship.shp_type].m_flags & M_XLIGHT)
133 && !(mchr[(int)ship.shp_type].m_flags & M_CHOPPER))
134 || ship.shp_effic < SHP_AIROPS_EFF) {
135 pr("Can't land on %s.\n", prship(&ship));
139 /* clear to land on ship#CNO */
140 pr("landing on carrier %d\n", cno);
141 if (mchr[(int)ship.shp_type].m_flags & M_FLY)
143 if (mchr[(int)ship.shp_type].m_flags & M_CHOPPER)
145 if (mchr[(int)ship.shp_type].m_flags & M_XLIGHT)
153 /* try to land at sector */
154 if (target->sct_own != player->cnum
155 && getrel(getnatp(target->sct_own), player->cnum) != ALLIED) {
156 pr("Nowhere to land at sector %s!\n",
157 xyas(target->sct_x, target->sct_y, player->cnum));
160 if (target->sct_type == SCT_MOUNT) {
161 pr("Nowhere to land at sector %s!\n",
162 xyas(target->sct_x, target->sct_y, player->cnum));
165 if (target->sct_type != SCT_AIRPT || target->sct_effic < 60)
168 /* clear to land at sector */
175 pln_oneway_to_carrier_ok(struct emp_qelem *bomb_list,
176 struct emp_qelem *esc_list, int cno)
178 struct emp_qelem *list, *qp;
180 struct plchrstr *pcp;
183 int nchoppers, nxlight, nplane;
185 if (cno < 0 || !getship(cno, &ship))
189 nchoppers = ship.shp_nchoppers;
190 nxlight = ship.shp_nxlight;
191 nplane = ship.shp_nplane;
192 mcp = &mchr[(int)ship.shp_type];
195 for (list = bomb_list;
197 list = list == bomb_list ? esc_list : NULL) {
198 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
199 /* FIXME duplicates put_plane_on_ship() logic; refactor */
200 plp = (struct plist *)qp;
201 pcp = &plchr[(int)plp->plane.pln_type];
202 if (plp->plane.pln_ship == ship.shp_uid)
204 /* try chopper space */
205 if ((pcp->pl_flags & P_K) && (mcp->m_flags & M_CHOPPER)
206 && nchoppers < mcp->m_nchoppers)
208 /* try xlight space */
209 else if ((pcp->pl_flags & P_E) && (mcp->m_flags & M_XLIGHT)
210 && nxlight < mcp->m_nxlight)
212 /* try plane space */
213 else if ((((pcp->pl_flags & P_L) && (mcp->m_flags & M_FLY))
214 || ((pcp->pl_flags & P_M) && (pcp->pl_flags & P_L)
215 && (mcp->m_flags & M_MSL)))
216 && nplane < mcp->m_nplanes)
219 return 0; /* won't be able to land */
226 pln_newlanding(struct emp_qelem *list, coord tx, coord ty, int cno)
228 struct emp_qelem *qp;
235 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
236 plp = (struct plist *)qp;
239 if (!can_be_on_ship(plp->plane.pln_uid, ship.shp_uid))
240 pr("\t%s cannot land on ship #%d! %s aborts!\n",
241 prplane(&plp->plane), cno, prplane(&plp->plane));
242 else if (!put_plane_on_ship(&plp->plane, &ship))
243 pr("\tNo room on ship #%d! %s aborts!\n", cno,
244 prplane(&plp->plane));
246 if (plp->plane.pln_own != ship.shp_own) {
247 /* plp->plane.pln_own = ship.shp_own;*/
249 "%s %s lands on your %s\n",
251 prplane(&plp->plane), prship(&ship));
255 plp->plane.pln_x = tx;
256 plp->plane.pln_y = ty;
257 getsect(tx, ty, §);
258 if (plp->plane.pln_own != sect.sct_own) {
259 /* plp->plane.pln_own = sect.sct_own;*/
261 "%s %s lands at your sector %s\n",
263 prplane(&plp->plane), xyas(tx, ty, sect.sct_own));
265 plp->plane.pln_ship = cno;
269 putship(ship.shp_uid, &ship);
273 pln_dropoff(struct emp_qelem *list, struct ichrstr *ip, coord tx, coord ty,
276 struct emp_qelem *qp;
279 struct sctstr *sectp;
288 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
289 plp = (struct plist *)qp;
292 if (type == EF_SECTOR) {
294 if (!sectp->sct_own) {
295 if (sectp->sct_type == SCT_WATER)
296 pr("Your %s sink like a rock!\n", ip->i_name);
298 pr("Your %s vanish without a trace.\n", ip->i_name);
301 if (sectp->sct_own != player->cnum
302 && getrel(getnatp(sectp->sct_own), player->cnum) != ALLIED) {
303 pr("You don't own %s! Cargo jettisoned...\n",
304 xyas(tx, ty, player->cnum));
307 if (ip->i_uid == I_CIVIL && sectp->sct_own != sectp->sct_oldown) {
308 pr("%s is occupied. Your civilians suffer from identity crisis and die.\n",
309 xyas(tx, ty, player->cnum));
312 there = sectp->sct_item[ip->i_uid];
316 there = sp->shp_item[ip->i_uid];
317 mp = &mchr[(int)sp->shp_type];
318 max = mp->m_item[ip->i_uid];
322 pr("%d excess %s discarded\n", max - there, ip->i_name);
326 pr("%d %s landed safely", amt, ip->i_name);
327 if (type == EF_SECTOR) {
329 sectp->sct_item[ip->i_uid] = there;
330 if (sectp->sct_own != player->cnum)
331 wu(0, sectp->sct_own, "%s planes drop %d %s in %s\n",
332 cname(player->cnum), amt, ip->i_name,
333 xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
334 pr(" at %s\n", xyas(tx, ty, player->cnum));
335 putsect((struct sctstr *)ptr);
337 struct shpstr *sp = (struct shpstr *)ptr;
338 sp->shp_item[ip->i_uid] = there;
339 if (sp->shp_own != player->cnum)
340 wu(0, sp->shp_own, "%s planes land %d %s on carrier %d\n",
341 cname(player->cnum), amt, ip->i_name, sp->shp_uid);
342 pr(" on carrier #%d\n", sp->shp_uid);
343 putship(sp->shp_uid, sp);
348 pln_mine(struct emp_qelem *list, struct sctstr *sectp)
350 struct emp_qelem *qp;
355 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
356 plp = (struct plist *)qp;
361 if (sectp->sct_type != SCT_WATER) {
362 pr("Your seamines have no effect here.\n");
365 sectp->sct_mines = MIN(sectp->sct_mines + amt, MINES_MAX);
366 pr("%d mines laid in %s.\n", amt,
367 xyas(sectp->sct_x, sectp->sct_y, player->cnum));
368 if (map_set(player->cnum, sectp->sct_x, sectp->sct_y, 'X', 0))
369 writemap(player->cnum);
375 pln_sel(struct nstr_item *ni, struct emp_qelem *list, struct sctstr *ap,
376 int ap_to_target, int rangemult, int wantflags, int nowantflags)
383 struct plchrstr *pcp;
390 while (nxtitem(ni, &plane)) {
393 if (plane.pln_mobil <= 0)
396 if (ontradingblock(EF_PLANE, &plane)) {
397 pr("plane #%d inelligible - it's for sale.\n",
403 range = mapdist(plane.pln_x, plane.pln_y, ap->sct_x, ap->sct_y);
405 pr("%s too far from assembly point\n", prplane(&plane));
408 if (plane.pln_effic < 40) {
409 pr("%s not efficient enough (must be 40%%)\n",
413 range += ap_to_target;
414 pcp = &plchr[(int)plane.pln_type];
418 for (x = 0; x < sizeof(wantflags) * 8; x++) {
420 if ((wantflags & y) == y)
421 if ((pcp->pl_flags & y) != y) {
440 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
444 if ((wantflags & P_L) && (pcp->pl_flags & P_L))
446 if ((wantflags & P_K) && (pcp->pl_flags & P_K))
448 if ((wantflags & P_E) && (pcp->pl_flags & P_E))
457 for (x = 0; x < sizeof(nowantflags) * 8; x++) {
459 if ((nowantflags & y) == y)
460 if ((pcp->pl_flags & y) == y)
467 if (plane.pln_range < range) {
468 pr("%s out of range (%d:%d)\n", prplane(&plane),
469 plane.pln_range, range);
472 if (plane.pln_ship >= 0) {
473 if (!getship(plane.pln_ship, &ship) ||
474 plane.pln_own != player->cnum) {
477 pr("(note) ship not valid for %s\n", prplane(&plane));
478 putplane(plane.pln_uid, &plane);
481 if (!can_be_on_ship(plane.pln_uid, ship.shp_uid))
483 if (ship.shp_effic < SHIP_MINEFF)
485 if (ship.shp_effic < SHP_AIROPS_EFF)
487 /* Can't fly off non-owned ships or non-allied ship */
488 if ((ship.shp_own != player->cnum) &&
489 (getrel(getnatp(ship.shp_own), player->cnum) != ALLIED)) {
490 pr("(note) An ally does not own the ship %s is on\n",
495 if (plane.pln_land >= 0) {
496 if (!getland(plane.pln_land, &land) ||
497 (plane.pln_own != player->cnum)) {
500 pr("(note) land unit not valid for %s\n", prplane(&plane));
501 putplane(plane.pln_uid, &plane);
504 if (!(plchr[(int)plane.pln_type].pl_flags & P_E))
506 if (land.lnd_effic < LAND_MINEFF)
508 if (land.lnd_effic < LND_AIROPS_EFF)
510 /* Can't fly off units in ships or other units */
511 if ((land.lnd_ship >= 0) || (land.lnd_land >= 0))
513 /* Can't fly off non-owned units or non-allied unit */
514 if ((land.lnd_own != player->cnum) &&
515 (getrel(getnatp(land.lnd_own), player->cnum) != ALLIED)) {
516 pr("(note) An ally does not own the unit %s is on\n",
521 /* Now, check the sector status if not on a plane or unit */
522 if ((plane.pln_ship < 0) && (plane.pln_land < 0)) {
523 if (!getsect(plane.pln_x, plane.pln_y, §))
525 /* First, check allied status */
526 /* Can't fly from non-owned sectors or non-allied sectors */
527 if ((sect.sct_own != player->cnum) &&
528 (getrel(getnatp(sect.sct_own), player->cnum) != ALLIED)) {
529 pr("(note) An ally does not own the sector %s is in\n",
534 if ((pcp->pl_flags & P_V) == 0) {
535 if (sect.sct_type != SCT_AIRPT) {
536 pr("%s not at airport\n", prplane(&plane));
539 if (sect.sct_effic < 40) {
540 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));
543 if (rangemult == 2 && sect.sct_effic < 60) {
544 pr("%s is not 60%% efficient, %s can't land there.\n",
545 xyas(sect.sct_x, sect.sct_y, plane.pln_own),
551 pr("%s standing by\n", prplane(&plane));
552 plane.pln_mission = 0;
553 putplane(plane.pln_uid, &plane);
554 plp = malloc(sizeof(struct plist));
559 emp_insque(&plp->queue, list);
564 pln_arm(struct emp_qelem *list, int dist, char mission, struct ichrstr *ip,
565 int flags, int mission_flags)
567 struct emp_qelem *qp;
568 struct emp_qelem *next;
571 for (qp = list->q_forw; qp != list; qp = next) {
573 plp = (struct plist *)qp;
574 if (pln_equip(plp, ip, flags, mission) < 0) {
579 if (flags & (P_S | P_I)) {
580 if (plp->pcp->pl_flags & P_S)
581 mission_flags |= P_S;
582 if (plp->pcp->pl_flags & P_I)
583 mission_flags |= P_I;
585 if (!(plp->pcp->pl_flags & P_H))
586 /* no stealth on this mission */
587 mission_flags &= ~P_H;
588 if (!(plp->pcp->pl_flags & P_X))
589 /* no stealth on this mission */
590 mission_flags &= ~P_X;
591 if (!(plp->pcp->pl_flags & P_A)) {
592 /* no asw on this mission */
593 mission_flags &= ~P_A;
594 /* FIXME escorts turn ASW patrol into ordinary recon */
596 if (!(plp->pcp->pl_flags & P_MINE)) {
597 /* no asw on this mission */
598 mission_flags &= ~P_MINE;
599 /* FIXME no effect */
601 plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
602 pr("%s equipped\n", prplane(&plp->plane));
604 return mission_flags;
608 pln_equip(struct plist *plp, struct ichrstr *ip, int flags, char mission)
610 struct plchrstr *pcp;
624 if (pp->pln_ship >= 0) {
625 getship(pp->pln_ship, &ship);
626 item = ship.shp_item;
628 } else if (pp->pln_land >= 0) {
629 getland(pp->pln_land, &land);
630 item = land.lnd_item;
633 getsect(pp->pln_x, pp->pln_y, §);
634 item = sect.sct_item;
635 own = sect.sct_oldown;
638 if (ip->i_uid == I_CIVIL) {
639 if (pp->pln_own != own) {
640 pr("You don't control those civilians!\n");
645 if (pcp->pl_fuel > item[I_PETROL]) {
646 pr("%s not enough petrol there!\n", prplane(pp));
649 item[I_PETROL] -= pcp->pl_fuel;
651 if ((flags & P_F) == 0) {
657 if (pp->pln_nuketype == -1) {
659 needed = pp->pln_load;
664 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
667 needed = (pp->pln_load * 2) / ip->i_lbs;
670 if ((pcp->pl_flags & P_MINE) == 0)
673 needed = (pp->pln_load * 2) / ip->i_lbs;
676 if ((pcp->pl_flags & (P_V | P_C)) == 0)
679 needed = pp->pln_load / ip->i_lbs;
682 if (pp->pln_nuketype == -1)
688 if (rval < 0 || (itype != I_NONE && needed <= 0)) {
689 pr("%s can't contribute to mission\n", prplane(pp));
692 if (itype != I_NONE) {
694 /* Supply is broken somewhere, so don't use it for now */
695 if (itype == I_SHELL && item[itype] < needed)
696 item[itype] += supply_commod(plp->plane.pln_own,
701 abandon_needed = !!would_abandon(§, itype, needed, NULL);
702 if (item[itype] < needed + abandon_needed) {
703 pr("Not enough %s for %s\n", ichr[itype].i_name, prplane(pp));
706 item[itype] -= needed;
708 if (itype == I_SHELL && (mission == 's' || mission == 'p'))
713 if (pp->pln_ship >= 0) {
714 if (pp->pln_own != ship.shp_own) {
716 "%s %s prepares for takeoff from ship %s\n",
717 cname(pp->pln_own), prplane(pp), prship(&ship));
719 putship(ship.shp_uid, &ship);
720 } else if (pp->pln_land >= 0) {
721 if (pp->pln_own != land.lnd_own) {
723 "%s %s prepares for takeoff from unit %s\n",
724 cname(pp->pln_own), prplane(pp), prland(&land));
726 putland(land.lnd_uid, &land);
728 if (pp->pln_own != sect.sct_own) {
729 wu(0, sect.sct_own, "%s %s prepares for takeoff from %s\n",
730 cname(pp->pln_own), prplane(pp),
731 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
739 pln_put(struct emp_qelem *list)
741 struct emp_qelem *qp;
742 struct emp_qelem *newqp;
748 /* Here is where planes return home from bombing runs.
749 We need to make sure they still have somewhere to return
753 plp = (struct plist *)qp;
755 /* Ok, check out where it wants to land */
756 if (pp->pln_ship >= 0) {
757 /* It is landing on a carrier */
758 getship(pp->pln_ship, &ship);
759 /* We should do more, like make sure it's really
760 a carrier, etc. but for now just make sure it's
762 if (ship.shp_effic < SHIP_MINEFF) {
764 "Ship #%d has been sunk, plane #%d has nowhere to land, and\n"
765 "splashes into the sea.\n",
766 pp->pln_ship, pp->pln_uid);
770 /* Presume we are landing back in a sector. */
771 getsect(pp->pln_x, pp->pln_y, §);
772 if (sect.sct_type == SCT_WATER || sect.sct_type == SCT_WASTE) {
774 "Nowhere to land at %s, plane #%d crashes and burns...\n",
775 xyas(pp->pln_x, pp->pln_y, pp->pln_own), pp->pln_uid);
779 putplane(pp->pln_uid, pp);
788 pln_removedupes(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
790 struct emp_qelem *bomb;
791 struct emp_qelem *esc;
795 if (QEMPTY(bomb_list) || QEMPTY(esc_list))
797 bomb = bomb_list->q_forw;
798 while (bomb != bomb_list) {
799 if (QEMPTY(esc_list)) {
803 esc = esc_list->q_forw;
804 bombp = (struct plist *)bomb;
805 while (esc != esc_list) {
806 escp = (struct plist *)esc;
807 if (escp->plane.pln_uid == bombp->plane.pln_uid) {
819 put_plane_on_ship(struct plnstr *plane, struct shpstr *ship)
821 struct plchrstr *pcp;
824 pcp = &plchr[(int)plane->pln_type];
825 mcp = &mchr[(int)ship->shp_type];
827 if (((int)plane->pln_ship) == ((int)ship->shp_uid))
828 return 1; /* Already on ship */
830 /* Try to put on ship as a chopper plane */
831 if ((pcp->pl_flags & P_K) &&
832 (mcp->m_flags & M_CHOPPER) &&
833 (ship->shp_nchoppers < mcp->m_nchoppers)) {
835 ship->shp_nchoppers++;
836 plane->pln_x = ship->shp_x;
837 plane->pln_y = ship->shp_y;
838 plane->pln_ship = ship->shp_uid;
839 putship(ship->shp_uid, ship);
840 putplane(plane->pln_uid, plane);
844 /* Try to put on ship as an xlight plane */
845 if ((pcp->pl_flags & P_E) &&
846 (mcp->m_flags & M_XLIGHT) &&
847 (ship->shp_nxlight < mcp->m_nxlight)) {
850 plane->pln_x = ship->shp_x;
851 plane->pln_y = ship->shp_y;
852 plane->pln_ship = ship->shp_uid;
853 putship(ship->shp_uid, ship);
854 putplane(plane->pln_uid, plane);
858 /* Try to put on ship as a normal plane */
859 if ((((pcp->pl_flags & P_L) && (mcp->m_flags & M_FLY)) ||
860 ((pcp->pl_flags & P_M) && (pcp->pl_flags & P_L) &&
861 (mcp->m_flags & M_MSL))) &&
862 (ship->shp_nplane < mcp->m_nplanes)) {
865 plane->pln_x = ship->shp_x;
866 plane->pln_y = ship->shp_y;
867 plane->pln_ship = ship->shp_uid;
868 putship(ship->shp_uid, ship);
869 putplane(plane->pln_uid, plane);
878 take_plane_off_ship(struct plnstr *plane, struct shpstr *ship)
880 struct plchrstr *pcp;
883 pcp = &plchr[(int)plane->pln_type];
884 mcp = &mchr[(int)ship->shp_type];
886 /* Try to take off ship as a chopper plane */
887 if ((pcp->pl_flags & P_K) &&
888 (mcp->m_flags & M_CHOPPER) && (ship->shp_nchoppers)) {
890 ship->shp_nchoppers--;
891 plane->pln_ship = -1;
892 putship(ship->shp_uid, ship);
893 putplane(plane->pln_uid, plane);
897 /* Try to take off ship as an xlight plane */
898 if ((pcp->pl_flags & P_E) &&
899 (mcp->m_flags & M_XLIGHT) && (ship->shp_nxlight)) {
902 plane->pln_ship = -1;
903 putship(ship->shp_uid, ship);
904 putplane(plane->pln_uid, plane);
908 /* Try to take off ship as a normal plane */
909 if ((((pcp->pl_flags & P_L) && (mcp->m_flags & M_FLY)) ||
910 ((pcp->pl_flags & P_M) && (pcp->pl_flags & P_L) &&
911 (mcp->m_flags & M_MSL))) && (ship->shp_nplane)) {
914 plane->pln_ship = -1;
915 putship(ship->shp_uid, ship);
916 putplane(plane->pln_uid, plane);
925 take_plane_off_land(struct plnstr *plane, struct lndstr *land)
927 struct plchrstr *pcp;
929 pcp = &plchr[(int)plane->pln_type];
931 /* Try to take off ship as an xlight plane */
932 if ((pcp->pl_flags & P_E) && land->lnd_nxlight) {
935 plane->pln_land = -1;
936 putland(land->lnd_uid, land);
937 putplane(plane->pln_uid, plane);
946 can_be_on_ship(int p, int s)
950 struct plchrstr *pcp;
956 pcp = &plchr[(int)plane.pln_type];
957 mcp = &mchr[(int)ship.shp_type];
959 if (pcp->pl_flags & P_L)
960 if (mcp->m_flags & M_FLY)
963 if (pcp->pl_flags & P_K)
964 if (mcp->m_flags & M_CHOPPER)
967 if (pcp->pl_flags & P_M)
968 if (mcp->m_flags & M_MSL)
971 if (pcp->pl_flags & P_E)
972 if (mcp->m_flags & M_XLIGHT)
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(((double)(100 - 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 struct plchrstr *pcp;
1028 struct mchrstr *mcp;
1033 if (sp->shp_effic < SHIP_MINEFF)
1036 mcp = &mchr[(int)sp->shp_type];
1037 snxtitem_xy(&ni, EF_PLANE, sp->shp_x, sp->shp_y);
1038 while (nxtitem(&ni, &plane)) {
1039 if (plane.pln_own == 0)
1041 if (plane.pln_ship == sp->shp_uid) {
1042 pcp = &plchr[(int)plane.pln_type];
1043 if ((pcp->pl_flags & P_K) && (nchoppers < mcp->m_nchoppers))
1045 else if ((pcp->pl_flags & P_E) && (nxlight < mcp->m_nxlight))
1047 else if ((pcp->pl_flags & P_L) || (pcp->pl_flags & P_M))
1052 if (nplane != sp->shp_nplane ||
1053 nxlight != sp->shp_nxlight || nchoppers != sp->shp_nchoppers) {
1054 sp->shp_nplane = nplane;
1055 sp->shp_nxlight = nxlight;
1056 sp->shp_nchoppers = nchoppers;
1057 putship(sp->shp_uid, sp);
1062 count_land_planes(struct lndstr *lp)
1064 struct nstr_item ni;
1065 struct plnstr plane;
1068 if (lp->lnd_effic < LAND_MINEFF)
1071 snxtitem_all(&ni, EF_PLANE);
1072 while (nxtitem(&ni, &plane)) {
1073 if (plane.pln_own == 0)
1075 if (plane.pln_land == lp->lnd_uid)
1079 if (lp->lnd_nxlight != nplane) {
1080 lp->lnd_nxlight = nplane;
1081 putland(lp->lnd_uid, lp);
1086 count_sect_planes(struct sctstr *sp)
1089 struct nstr_item ni;
1090 struct plnstr plane;
1092 snxtitem_all(&ni, EF_PLANE);
1093 while (nxtitem(&ni, &plane)) {
1096 if (plane.pln_flags & PLN_LAUNCHED)
1098 if (plane.pln_x == sp->sct_x && plane.pln_y == sp->sct_y)
1106 put_plane_on_land(struct plnstr *plane, struct lndstr *land)
1108 struct plchrstr *pcp;
1110 pcp = &plchr[(int)plane->pln_type];
1112 if (((int)plane->pln_land) == ((int)land->lnd_uid))
1113 return 1; /* Already on unit */
1115 /* Try to put on unit as an xlight plane */
1116 if ((pcp->pl_flags & P_E) &&
1117 (land->lnd_nxlight < land->lnd_maxlight)) {
1119 land->lnd_nxlight++;
1120 plane->pln_x = land->lnd_x;
1121 plane->pln_y = land->lnd_y;
1122 plane->pln_land = land->lnd_uid;
1123 putland(land->lnd_uid, land);
1124 putplane(plane->pln_uid, plane);
1128 /* We have failed */
1133 pln_hitchance(struct plnstr *pp, int hardtarget, int type)
1135 struct plchrstr *pcp = plchr + pp->pln_type;
1136 float tfact = (float)(pp->pln_tech - pcp->pl_tech) /
1137 (pp->pln_tech - pcp->pl_tech / 2);
1138 int acc = pp->pln_acc;
1141 if (type == EF_SHIP) {
1142 if (pcp->pl_flags & P_A)
1144 if (!(pcp->pl_flags & P_T))
1147 hitchance = (int)(pp->pln_effic * (1.0 - 0.1 * tfact) *
1148 (1.0 - acc / 100.0)) - hardtarget;
1150 /* smooth out the bottom of the graph with asymtote at 5 -KHS */
1152 hitchance = 5 + ldround(300.0 / (40.0 - hitchance), 1);
1153 if (hitchance > 100)
1158 /* return 0 if there was a nuclear detonation */
1161 pln_damage(struct plnstr *pp, coord x, coord y, char type, int *nukedamp,
1165 struct plchrstr *pcp = plchr + pp->pln_type;
1173 if (pp->pln_nuketype != -1) {
1174 if (nuk_on_plane(&nuke, pp->pln_uid) >= 0) {
1175 mpr(pp->pln_own, "Releasing RV's for %s detonation...\n",
1176 pp->pln_flags & PLN_AIRBURST ? "airburst" : "groundburst");
1177 pp->pln_nuketype = -1;
1178 *nukedamp = detonate(&nuke, x, y,
1179 pp->pln_flags & PLN_AIRBURST);
1186 if (!pp->pln_load) /* e.g. ab, blowing up on launch pad */
1189 i = roll(pp->pln_load) + 1;
1190 if (i > pp->pln_load)
1193 if (pcp->pl_flags & P_M) {
1194 if (pcp->pl_flags & P_MAR)
1196 } else if (pcp->pl_flags & P_T)
1199 aim = 100 - pp->pln_acc;
1213 hitroll = roll(100);
1214 if (hitroll >= 90) {
1217 mpr(pp->pln_own, "BLAM");
1218 } else if (hitroll < aim) {
1221 mpr(pp->pln_own, "Blam");
1225 mpr(pp->pln_own, "blam");
1228 mpr(pp->pln_own, "-");
1231 mpr(pp->pln_own, "\n");
1238 pln_identchance(struct plnstr *pp, int hardtarget, int type)
1241 (100.0 - pln_hitchance(pp, hardtarget, type)) / 100.0;
1242 return (int)(100 - 100 * misschance * misschance);
1246 pln_mobcost(int dist, struct plnstr *pp, int flags)
1250 if ((flags & P_F) || (flags & P_ESC))
1251 cost = 10 * 100 / pp->pln_effic;
1253 cost = 20 * 100 / pp->pln_effic;
1255 cost = ldround((double)cost * dist / pp->pln_range_max, 1);
1257 return MIN(32 + pp->pln_mobil, cost + 5);
1261 * Set PP's tech to TLEV along with everything else that depends on it.
1264 pln_set_tech(struct plnstr *pp, int tlev)
1266 struct plchrstr *pcp = plchr + pp->pln_type;
1267 int tech_diff = tlev - pcp->pl_tech;
1268 int limited_range = pp->pln_range < pp->pln_range_max;
1270 if (CANT_HAPPEN(tech_diff < 0)) {
1275 pp->pln_tech = tlev;
1276 pp->pln_att = PLN_ATTDEF(pcp->pl_att, tech_diff);
1277 pp->pln_def = PLN_ATTDEF(pcp->pl_def, tech_diff);
1278 pp->pln_acc = PLN_ACC(pcp->pl_acc, tech_diff);
1279 pp->pln_range_max = PLN_RAN(pcp->pl_range, tech_diff);
1280 pp->pln_load = PLN_LOAD(pcp->pl_load, tech_diff);
1282 if (!limited_range || pp->pln_range > pp->pln_range_max)
1283 pp->pln_range = pp->pln_range_max;