/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
- * Ken Stevens, Steve McClure
+ * Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Ken Stevens, Steve McClure, Markus Armbruster
*
- * This program is free software; you can redistribute it and/or modify
+ * Empire is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ---
*
* ---
*
* plnsub.c: Plane subroutine stuff
- *
+ *
* Known contributors to this file:
* Dave Pare, 1986
* Ken Stevens, 1995
* Steve McClure, 1998-2000
- * Markus Armbruster, 2004-2008
+ * Markus Armbruster, 2004-2020
*/
#include <config.h>
-#include "file.h"
+#include "chance.h"
+#include "empobj.h"
#include "item.h"
#include "land.h"
-#include "lost.h"
#include "map.h"
#include "misc.h"
#include "nat.h"
#include "nsc.h"
#include "nuke.h"
#include "optlist.h"
-#include "path.h"
+#include "plague.h"
#include "plane.h"
#include "player.h"
#include "prototypes.h"
#include "ship.h"
#include "xy.h"
-static int pln_equip(struct plist *, struct ichrstr *, int, char);
-static int fit_plane_on_ship(struct plnstr *, struct shpstr *);
+static int ship_can_carry(struct shpstr *, int, int, int, int);
+static int inc_shp_nplane(struct plnstr *, int *, int *, int *);
+
+/*
+ * Get planes and escorts argument.
+ * Read planes into *@ni_bomb, and (optional) escorts into *@ni_esc.
+ * If @input_bomb is not empty, use it, else prompt for more input.
+ * Same for @input_esc.
+ * If we got a plane argument, initialize *@ni_bomb and *@ni_esc, and
+ * return 0.
+ * Else return -1 (*@ni_bomb and *@ni_esc may be modified).
+ */
+int
+get_planes(struct nstr_item *ni_bomb, struct nstr_item *ni_esc,
+ char *input_bomb, char *input_esc)
+{
+ if (!snxtitem(ni_bomb, EF_PLANE, input_bomb, NULL))
+ return -1;
+ if (!snxtitem(ni_esc, EF_PLANE, input_esc, "escort(s)? ")) {
+ if (player->aborted)
+ return -1;
+ pr("No escorts...\n");
+ }
+ return 0;
+}
/*
* Get assembly point argument.
- * If INPUT is not empty, use it, else prompt for more input using PROMPT.
- * If this yields a valid assembly point, read it into *AP_SECT and
- * return AP_SECT.
+ * If @input is not empty, use it.
+ * Else prompt for more input using @prompt.
+ * If this yields a valid assembly point, read it into *@ap_sect and
+ * return @ap_sect.
* Else complain and return NULL.
- * *AP_SECT and BUF[1024] may be modified in either case.
+ * *@ap_sect and @buf[1024] may be modified in either case.
*/
struct sctstr *
get_assembly_point(char *input, struct sctstr *ap_sect, char *buf)
return NULL;
/* over own or allied sector is fine */
- if (ap_sect->sct_own == player->cnum
- || getrel(getnatp(ap_sect->sct_own), player->cnum) == ALLIED)
+ if (relations_with(ap_sect->sct_own, player->cnum) == ALLIED)
return ap_sect;
/* over own or allied ship is fine */
snxtitem_xy(&ni, EF_SHIP, x, y);
while (nxtitem(&ni, &ship)) {
- if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
+ if (!ship.shp_own)
continue;
- if (ship.shp_own == player->cnum
- || getrel(getnatp(ship.shp_own), player->cnum) == ALLIED)
+ if (relations_with(ship.shp_own, player->cnum) == ALLIED)
return ap_sect;
}
return NULL;
}
+/*
+ * Find out whether planes can fly one-way to @x,@y.
+ * Offer the player any carriers there. If he chooses one, read it
+ * into @target->ship. Else read the target sector into @target->sect.
+ * If planes can land there, set required plane flags in *@flagsp, and
+ * return 0. Else return -1.
+ */
int
-pln_onewaymission(struct sctstr *target, int *shipno, int *flagp)
+pln_where_to_land(coord x, coord y,
+ union empobj_storage *target, int *flagsp)
{
+ /* Keep conditions for landing consistent with pln_airbase_ok() */
int nships;
int cno;
- int flags, fl;
- struct shpstr ship;
+ int fl;
char buf[1024];
char *p;
- flags = *flagp;
-
/* offer carriers */
- nships = carriersatxy(target->sct_x, target->sct_y, player->cnum);
+ nships = carriersatxy(x, y, player->cnum);
if (nships) {
for (;;) {
- if (!(p = getstarg(0, "Carrier #? ", buf)) || !*p)
+ p = getstring("Carrier #? ", buf);
+ if (!p)
+ return -1;
+ if (!*p)
break;
cno = atoi(p);
- if (cno < 0
- || !getship(cno, &ship)
+ if (!getship(cno, &target->ship)
|| (!player->owner
- && (getrel(getnatp(ship.shp_own), player->cnum)
+ && (relations_with(target->ship.shp_own, player->cnum)
!= ALLIED))) {
pr("Not yours\n");
continue;
}
- if (ship.shp_x != target->sct_x || ship.shp_y != target->sct_y) {
- pr("Ship #%d not in %s\n", cno,
- xyas(target->sct_x, target->sct_y, player->cnum));
+ if (target->ship.shp_x != x || target->ship.shp_y != y) {
+ pr("Ship #%d not in %s\n", cno, xyas(x, y, player->cnum));
continue;
}
- fl = carrier_planes(&ship, 0);
+ fl = carrier_planes(&target->ship, 0);
if (fl == 0) {
- pr("Can't land on %s.\n", prship(&ship));
+ pr("Can't land on %s.\n", prship(&target->ship));
continue;
}
/* clear to land on ship#CNO */
pr("landing on carrier %d\n", cno);
- flags |= fl;
- *shipno = cno;
- *flagp = flags;
+ *flagsp |= fl;
return 0;
}
}
/* try to land at sector */
- if (target->sct_own != player->cnum
- && getrel(getnatp(target->sct_own), player->cnum) != ALLIED) {
- pr("Nowhere to land at sector %s!\n",
- xyas(target->sct_x, target->sct_y, player->cnum));
+ getsect(x, y, &target->sect);
+ if (relations_with(target->sect.sct_own, player->cnum) != ALLIED) {
+ pr("Nowhere to land at sector %s!\n", xyas(x, y, player->cnum));
return -1;
}
- if (target->sct_type == SCT_MOUNT) {
- pr("Nowhere to land at sector %s!\n",
- xyas(target->sct_x, target->sct_y, player->cnum));
- return -1;
- }
- if (target->sct_type != SCT_AIRPT || target->sct_effic < 60)
- flags |= P_V;
-
/* clear to land at sector */
- *shipno = -1;
- *flagp = flags;
+ if (target->sect.sct_type == SCT_MOUNT) {
+ *flagsp |= P_K;
+ }
+ if (target->sect.sct_type != SCT_AIRPT || target->sect.sct_effic < 60)
+ *flagsp |= P_V;
return 0;
}
int
-pln_oneway_to_carrier_ok(struct emp_qelem *bomb_list,
- struct emp_qelem *esc_list, int cno)
+pln_can_land_on_carrier(struct emp_qelem *bomb_list,
+ struct emp_qelem *esc_list,
+ struct shpstr *sp)
+
{
+ int n, nch, nxl, nmsl;
struct emp_qelem *list, *qp;
struct plist *plp;
- struct shpstr ship;
-
- if (cno < 0 || !getship(cno, &ship))
- return 0;
- count_planes(&ship);
+ n = shp_nplane(sp, &nch, &nxl, &nmsl);
/* for both lists */
for (list = bomb_list;
list = list == bomb_list ? esc_list : NULL) {
for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
plp = (struct plist *)qp;
- if (plp->plane.pln_ship == ship.shp_uid)
+ if (plp->plane.pln_ship == sp->shp_uid)
continue;
- if (!fit_plane_on_ship(&plp->plane, &ship))
+ n++;
+ if (!inc_shp_nplane(&plp->plane, &nch, &nxl, &nmsl))
return 0;
}
}
- return 1;
+ return ship_can_carry(sp, n, nch, nxl, nmsl);
}
void
for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
plp = (struct plist *)qp;
if (cno >= 0) {
- count_planes(&ship);
if (!could_be_on_ship(&plp->plane, &ship))
pr("\t%s cannot land on ship #%d! %s aborts!\n",
prplane(&plp->plane), cno, prplane(&plp->plane));
cname(player->cnum), prplane(&plp->plane),
prship(&ship));
}
+ if (plp->pstage == PLG_INFECT
+ && ship.shp_pstage == PLG_HEALTHY)
+ ship.shp_pstage = PLG_EXPOSED;
}
} else {
plp->plane.pln_x = tx;
cname(player->cnum),
prplane(&plp->plane), xyas(tx, ty, sect.sct_own));
}
+ if (plp->pstage == PLG_INFECT
+ && sect.sct_pstage == PLG_HEALTHY)
+ sect.sct_pstage = PLG_EXPOSED;
plp->plane.pln_ship = cno;
}
}
- if (cno >= 0)
- putship(ship.shp_uid, &ship);
}
void
pln_dropoff(struct emp_qelem *list, struct ichrstr *ip, coord tx, coord ty,
- void *ptr, int type)
+ int cno)
{
struct emp_qelem *qp;
struct plist *plp;
int amt;
- struct sctstr *sectp;
- struct shpstr *sp;
+ struct sctstr sect;
+ struct shpstr ship;
int there;
int max;
- struct mchrstr *mp;
+ int pstage;
- if (ip == 0)
+ if (!ip)
return;
- amt = 0;
- for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
- plp = (struct plist *)qp;
- amt += plp->misc;
- }
- if (type == EF_SECTOR) {
- sectp = ptr;
- if (!sectp->sct_own) {
- if (sectp->sct_type == SCT_WATER)
+ if (cno < 0) {
+ getsect(tx, ty, §);
+ if (!sect.sct_own) {
+ if (sect.sct_type == SCT_WATER)
pr("Your %s sink like a rock!\n", ip->i_name);
else
pr("Your %s vanish without a trace.\n", ip->i_name);
return;
}
- if (sectp->sct_own != player->cnum
- && getrel(getnatp(sectp->sct_own), player->cnum) != ALLIED) {
+ if (relations_with(sect.sct_own, player->cnum) != ALLIED) {
pr("You don't own %s! Cargo jettisoned...\n",
xyas(tx, ty, player->cnum));
return;
}
- if (ip->i_uid == I_CIVIL && sectp->sct_own != sectp->sct_oldown) {
+ if (ip->i_uid == I_CIVIL && sect.sct_own != sect.sct_oldown) {
pr("%s is occupied. Your civilians suffer from identity crisis and die.\n",
xyas(tx, ty, player->cnum));
return;
}
- there = sectp->sct_item[ip->i_uid];
+ there = sect.sct_item[ip->i_uid];
max = ITEM_MAX;
+ pstage = sect.sct_pstage;
} else {
- sp = ptr;
- there = sp->shp_item[ip->i_uid];
- mp = &mchr[(int)sp->shp_type];
- max = mp->m_item[ip->i_uid];
+ getship(cno, &ship);
+ there = ship.shp_item[ip->i_uid];
+ max = mchr[ship.shp_type].m_item[ip->i_uid];
+ pstage = ship.shp_pstage;
+ }
+
+ amt = 0;
+ for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
+ plp = (struct plist *)qp;
+ amt += plp->load;
+ if (plp->load
+ && plp->pstage == PLG_INFECT && pstage == PLG_HEALTHY)
+ pstage = PLG_EXPOSED;
}
+
there += amt;
if (there > max) {
- pr("%d excess %s discarded\n", max - there, ip->i_name);
- amt = max - there;
+ pr("%d excess %s discarded\n", there - max, ip->i_name);
+ amt -= there - max;
there = max;
}
pr("%d %s landed safely", amt, ip->i_name);
- if (type == EF_SECTOR) {
- sectp = ptr;
- sectp->sct_item[ip->i_uid] = there;
- if (sectp->sct_own != player->cnum)
- wu(0, sectp->sct_own, "%s planes drop %d %s in %s\n",
+ if (cno < 0) {
+ sect.sct_item[ip->i_uid] = there;
+ sect.sct_pstage = pstage;
+ if (sect.sct_own != player->cnum)
+ wu(0, sect.sct_own, "%s planes drop %d %s in %s\n",
cname(player->cnum), amt, ip->i_name,
- xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
+ xyas(tx, ty, sect.sct_own));
pr(" at %s\n", xyas(tx, ty, player->cnum));
- putsect((struct sctstr *)ptr);
+ putsect(§);
} else {
- struct shpstr *sp = (struct shpstr *)ptr;
- sp->shp_item[ip->i_uid] = there;
- if (sp->shp_own != player->cnum)
- wu(0, sp->shp_own, "%s planes land %d %s on carrier %d\n",
- cname(player->cnum), amt, ip->i_name, sp->shp_uid);
- pr(" on carrier #%d\n", sp->shp_uid);
- putship(sp->shp_uid, sp);
+ ship.shp_item[ip->i_uid] = there;
+ ship.shp_pstage = pstage;
+ if (ship.shp_own != player->cnum)
+ wu(0, ship.shp_own, "%s planes land %d %s on carrier %d\n",
+ cname(player->cnum), amt, ip->i_name, ship.shp_uid);
+ pr(" on carrier #%d\n", ship.shp_uid);
+ putship(ship.shp_uid, &ship);
}
}
void
-pln_mine(struct emp_qelem *list, struct sctstr *sectp)
+pln_mine(struct emp_qelem *list, coord tx, coord ty)
{
struct emp_qelem *qp;
struct plist *plp;
int amt;
+ struct sctstr sect;
amt = 0;
for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
plp = (struct plist *)qp;
- amt += plp->misc;
+ amt += plp->load;
}
if (amt > 0) {
- if (sectp->sct_type != SCT_WATER) {
- pr("Your seamines have no effect here.\n");
+ getsect(tx, ty, §);
+ if (sect.sct_type != SCT_WATER) {
+ pr("Your sea mines have no effect here.\n");
return;
}
- sectp->sct_mines = MIN(sectp->sct_mines + amt, MINES_MAX);
- pr("%d mines laid in %s.\n", amt,
- xyas(sectp->sct_x, sectp->sct_y, player->cnum));
- if (map_set(player->cnum, sectp->sct_x, sectp->sct_y, 'X', 0))
+ sect.sct_mines = MIN(sect.sct_mines + amt, MINES_MAX);
+ pr("%d mines laid in %s.\n", amt, xyas(tx, ty, player->cnum));
+ if (map_set(player->cnum, tx, ty, 'X', 0))
writemap(player->cnum);
- putsect(sectp);
+ putsect(§);
}
}
/*
- * Has PP's type capabilities satisfying WANTFLAGS and NOWANTFLAGS?
+ * Has @pp's type capabilities satisfying @wantflags and @nowantflags?
* A plane type is capable unless
- * - it lacks all of the P_F, P_ESC in WANTFLAGS, or
- * - it lacks all of the P_E, P_L, P_K in WANTFLAGS, or
- * - it lacks any of the other capabilities in WANTFLAGS, or
- * - it has any of the capabilities in NOWANTFLAGS.
+ * - it lacks all of the P_B, P_T in @wantflags, or
+ * - it lacks all of the P_F, P_ESC in @wantflags, or
+ * - it lacks all of the P_E, P_L, P_K in @wantflags, or
+ * - it lacks any of the other capabilities in @wantflags, or
+ * - it has any of the capabilities in @nowantflags.
*/
int
pln_capable(struct plnstr *pp, int wantflags, int nowantflags)
{
int flags = plchr[(int)pp->pln_type].pl_flags;
+ if (wantflags & (P_B | P_T)) {
+ if ((flags & wantflags & (P_B | P_T)) == 0)
+ return 0;
+ wantflags &= ~(P_B | P_T);
+ }
+
if (wantflags & (P_F | P_ESC)) {
if ((flags & wantflags & (P_F | P_ESC)) == 0)
return 0;
}
/*
- * Find plane types that can operate from carrier SP.
- * If MSL find missile types, else non-missile types.
+ * Return union of capabilities of planes in @list.
+ */
+int
+pln_caps(struct emp_qelem *list)
+{
+ struct emp_qelem *qp;
+ struct plist *plp;
+ int fl;
+
+ fl = 0;
+ for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
+ plp = (struct plist *)qp;
+ fl |= plp->pcp->pl_flags;
+ }
+
+ return fl;
+}
+
+/*
+ * Find plane types that can operate from carrier @sp.
+ * If @msl find missile types, else non-missile types.
* Return a combination of P_L, P_K, P_E.
- * It's zero if SP can't support air operations due to its type or
+ * It's zero if @sp can't support air operations due to its type or
* state (low efficiency).
*/
int
return res;
}
+/*
+ * Can @pp operate out its sector?
+ * If @oneway, consider only takeoff, else takeoff and landing.
+ * If @noisy, report to current player when it can't.
+ */
int
pln_airbase_ok(struct plnstr *pp, int oneway, int noisy)
{
+ /* Keep conditions for landing consistent with pln_where_to_land() */
struct shpstr ship;
struct lndstr land;
struct sctstr sect;
CANT_REACH();
return 0;
}
- if (ship.shp_own != pp->pln_own
- && getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED) {
+ if (relations_with(ship.shp_own, pp->pln_own) != ALLIED) {
if (noisy)
pr("(note) An ally does not own the ship %s is on\n",
prplane(pp));
CANT_REACH();
return 0;
}
- if (land.lnd_own != pp->pln_own
- && getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED) {
+ if (relations_with(land.lnd_own, pp->pln_own) != ALLIED) {
if (noisy)
pr("(note) An ally does not own the unit %s is on\n",
prplane(pp));
return 0;
} else {
- /* sector: needs to be own or allied, efficient airfield */
+ /* sector: needs to be own or allied, efficient, suitable type */
if (!getsect(pp->pln_x, pp->pln_y, §)) {
CANT_REACH();
return 0;
}
+ /* mountain requires helo or missile */
+ if (sect.sct_type == SCT_MOUNT && !(pcp->pl_flags & (P_K | P_M))) {
+ if (noisy)
+ pr("(note) %s is in a mountain and can't take off\n",
+ prplane(pp));
+ return 0;
+ }
- if (sect.sct_own != pp->pln_own
- && getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED) {
+ if (relations_with(sect.sct_own, pp->pln_own) != ALLIED) {
if (noisy)
pr("(note) An ally does not own the sector %s is in\n",
prplane(pp));
if (sect.sct_effic < 40) {
if (noisy)
pr("%s is not 40%% efficient, %s can't take off from there.\n",
- xyas(sect.sct_x, sect.sct_y, pp->pln_own),
+ xyas(sect.sct_x, sect.sct_y, player->cnum),
prplane(pp));
return 0;
}
if (!oneway && sect.sct_effic < 60) {
if (noisy)
pr("%s is not 60%% efficient, %s can't land there.\n",
- xyas(sect.sct_x, sect.sct_y, pp->pln_own),
+ xyas(sect.sct_x, sect.sct_y, player->cnum),
prplane(pp));
return 0;
}
emp_initque(list);
while (nxtitem(ni, &plane)) {
- if (!player->owner)
+ /*
+ * It would be nice to let deities fly foreign planes, but
+ * much of the code assumes that only the plane's owner can
+ * fly it.
+ */
+ if (!plane.pln_own || plane.pln_own != player->cnum)
continue;
if (plane.pln_mobil <= 0)
continue;
continue;
if (opt_MARKET) {
if (ontradingblock(EF_PLANE, &plane)) {
- pr("plane #%d inelligible - it's for sale.\n",
+ pr("plane #%d ineligible - it's for sale.\n",
plane.pln_uid);
continue;
}
plane.pln_mission = 0;
putplane(plane.pln_uid, &plane);
plp = malloc(sizeof(struct plist));
- plp->misc = 0;
- plp->bombs = 0;
+ plp->load = 0;
+ plp->pstage = PLG_HEALTHY;
plp->pcp = pcp;
plp->plane = plane;
emp_insque(&plp->queue, list);
}
}
-int
-pln_arm(struct emp_qelem *list, int dist, char mission, struct ichrstr *ip,
- int flags, int mission_flags)
+void
+pln_arm(struct emp_qelem *list, int dist, char mission, struct ichrstr *ip)
{
struct emp_qelem *qp;
struct emp_qelem *next;
pp = &plp->plane;
getplane(pp->pln_uid, pp);
if ((pp->pln_flags & PLN_LAUNCHED)
- || pln_equip(plp, ip, flags, mission) < 0) {
+ || pln_equip(plp, ip, mission) < 0) {
emp_remque(qp);
free(qp);
continue;
}
- if (flags & (P_S | P_I)) {
- if (plp->pcp->pl_flags & P_S)
- mission_flags |= P_S;
- if (plp->pcp->pl_flags & P_I)
- mission_flags |= P_I;
- }
- if (!(plp->pcp->pl_flags & P_H))
- /* no stealth on this mission */
- mission_flags &= ~P_H;
- if (!(plp->pcp->pl_flags & P_X))
- /* no stealth on this mission */
- mission_flags &= ~P_X;
- if (!(plp->pcp->pl_flags & P_A)) {
- /* no asw on this mission */
- mission_flags &= ~P_A;
- /* FIXME escorts turn ASW patrol into ordinary recon */
- }
- if (!(plp->pcp->pl_flags & P_MINE)) {
- /* no asw on this mission */
- mission_flags &= ~P_MINE;
- /* FIXME no effect */
- }
pp->pln_flags |= PLN_LAUNCHED;
- pp->pln_mobil -= pln_mobcost(dist, pp, flags);
+ pp->pln_mobil -= pln_mobcost(dist, pp, mission);
putplane(pp->pln_uid, pp);
pr("%s equipped\n", prplane(pp));
}
- return mission_flags;
}
-static int
-pln_equip(struct plist *plp, struct ichrstr *ip, int flags, char mission)
+int
+pln_equip(struct plist *plp, struct ichrstr *ip, char mission)
{
struct plchrstr *pcp;
struct plnstr *pp;
pcp = plp->pcp;
if (pp->pln_ship >= 0) {
getship(pp->pln_ship, &ship);
+ plp->pstage = ship.shp_pstage;
item = ship.shp_item;
own = ship.shp_own;
} else if (pp->pln_land >= 0) {
getland(pp->pln_land, &land);
+ plp->pstage = land.lnd_pstage;
item = land.lnd_item;
own = land.lnd_own;
} else {
getsect(pp->pln_x, pp->pln_y, §);
+ plp->pstage = sect.sct_pstage;
item = sect.sct_item;
own = sect.sct_oldown;
}
- if (ip) {
- if (ip->i_uid == I_CIVIL) {
- if (pp->pln_own != own) {
- pr("You don't control those civilians!\n");
- return -1;
- }
- }
- }
if (pcp->pl_fuel > item[I_PETROL]) {
pr("%s not enough petrol there!\n", prplane(pp));
return -1;
}
item[I_PETROL] -= pcp->pl_fuel;
- if ((flags & P_F) == 0) {
- load = pln_load(pp);
- itype = I_NONE;
- needed = 0;
- switch (mission) {
- case 's': /* strategic bomb */
- case 'p': /* pinpoint bomb */
- if (pp->pln_nuketype == -1) {
- itype = I_SHELL;
- needed = load;
- }
+ load = pln_load(pp);
+ itype = I_NONE;
+ switch (mission) {
+ case 's': /* strategic bomb */
+ case 'p': /* pinpoint bomb */
+ itype = I_SHELL;
+ break;
+ case 't': /* transport */
+ if (!(pcp->pl_flags & P_C) || !ip)
break;
- case 't': /* transport */
- case 'd': /* drop */
- if ((pcp->pl_flags & P_C) == 0 || ip == 0)
- break;
- itype = ip->i_uid;
- needed = (load * 2) / ip->i_lbs;
+ itype = ip->i_uid;
+ load *= 2;
+ break;
+ case 'm': /* mine */
+ if ((pcp->pl_flags & P_MINE) == 0)
break;
- case 'm': /* mine */
- if ((pcp->pl_flags & P_MINE) == 0)
- break;
- itype = I_SHELL;
- needed = (load * 2) / ip->i_lbs;
+ itype = I_SHELL;
+ load *= 2;
+ break;
+ case 'd': /* drop */
+ if (!(pcp->pl_flags & P_C) || CANT_HAPPEN(!ip))
break;
- case 'a': /* paradrop */
- if ((pcp->pl_flags & (P_V | P_C)) == 0)
- break;
- itype = I_MILIT;
- needed = load / ip->i_lbs;
+ itype = ip->i_uid;
+ if (pcp->pl_flags & P_V)
+ load *= 2;
+ break;
+ case 'a': /* paradrop */
+ if (!(pcp->pl_flags & P_P))
break;
- case 'r': /* reconnaissance */
+ itype = I_MILIT;
+ if (pcp->pl_flags & P_V)
+ load *= 2;
+ break;
+ case 'r': /* reconnaissance */
+ case 'e': /* escort */
+ load = 0;
+ break;
+ case 'i': /* missile interception */
+ if (CANT_HAPPEN(!(pcp->pl_flags & P_M)
+ || !(pcp->pl_flags & (P_N | P_O))))
break;
- default:
- CANT_REACH();
- }
- if (itype != I_NONE && needed <= 0) {
+ if (load)
+ itype = I_SHELL;
+ break;
+ default:
+ CANT_REACH();
+ load = 0;
+ }
+
+ if (itype != I_NONE) {
+ needed = load / ichr[itype].i_lbs;
+ if (needed <= 0) {
pr("%s can't contribute to mission\n", prplane(pp));
return -1;
}
- if (itype != I_NONE) {
-#if 0
- /* Supply is broken somewhere, so don't use it for now */
- if (itype == I_SHELL && item[itype] < needed)
- item[itype] += supply_commod(plp->plane.pln_own,
- plp->plane.pln_x,
- plp->plane.pln_y,
- I_SHELL, needed);
-#endif
- abandon_needed = !!would_abandon(§, itype, needed, NULL);
- if (item[itype] < needed + abandon_needed) {
- pr("Not enough %s for %s\n", ichr[itype].i_name, prplane(pp));
+ if (nuk_on_plane(pp) >= 0) {
+ if (mission == 's' || mission == 't')
+ needed = 0;
+ else {
+ pr("%s can't fly this mission"
+ " while it is carrying a nuclear weapon\n",
+ prplane(pp));
return -1;
}
- item[itype] -= needed;
}
- if (itype == I_SHELL && (mission == 's' || mission == 'p'))
- plp->bombs = needed;
+ if (itype == I_CIVIL && pp->pln_own != own) {
+ pr("You don't control those civilians!\n");
+ return -1;
+ }
+#if 0
+ /* Supply is broken somewhere, so don't use it for now */
+ if (itype == I_SHELL && item[itype] < needed)
+ item[itype] += supply_commod(plp->plane.pln_own,
+ plp->plane.pln_x,
+ plp->plane.pln_y,
+ I_SHELL, needed);
+#endif
+ if (pp->pln_ship >= 0 || pp->pln_land >= 0)
+ abandon_needed = 0;
else
- plp->misc = needed;
+ abandon_needed = !!would_abandon(§, itype, needed, NULL);
+ if (item[itype] < needed + abandon_needed) {
+ pr("Not enough %s for %s\n", ichr[itype].i_name, prplane(pp));
+ return -1;
+ }
+ item[itype] -= needed;
+ plp->load = needed;
}
+
if (pp->pln_ship >= 0) {
if (pp->pln_own != ship.shp_own) {
wu(0, ship.shp_own,
{
struct plnstr *pp;
struct shpstr ship;
- struct lndstr land;
struct sctstr sect;
pp = &plp->plane;
if (!(pp->pln_flags & PLN_LAUNCHED))
; /* never took off */
else if (pp->pln_effic < PLANE_MINEFF) {
- /* destroyed */
- if (pp->pln_ship >= 0) {
- getship(pp->pln_ship, &ship);
- take_plane_off_ship(pp, &ship);
- }
- if (pp->pln_land >= 0) {
- getland(pp->pln_land, &land);
- take_plane_off_land(pp, &land);
- }
+ ; /* destroyed */
} else if (pp->pln_ship >= 0) {
/* It is landing on a carrier */
getship(pp->pln_ship, &ship);
/* We should do more, like make sure it's really
a carrier, etc. but for now just make sure it's
not sunk. */
- if (ship.shp_effic < SHIP_MINEFF) {
+ if (!ship.shp_own) {
mpr(pp->pln_own,
"Ship #%d has been sunk, plane #%d has nowhere to land, and\n"
"splashes into the sea.\n",
}
/*
- * Fit a plane of PP's type on ship SP.
- * Adjust SP's plane counters.
- * Updating the plane accordingly is the caller's job.
- * Return whether it fits.
+ * Can a carrier of @sp's type carry this load of planes?
+ * The load consists of @n planes, of which @nch are choppers, @nxl
+ * xlight, @nmsl light missiles, and the rest are light fixed-wing
+ * planes.
*/
static int
-fit_plane_on_ship(struct plnstr *pp, struct shpstr *sp)
+ship_can_carry(struct shpstr *sp, int n, int nch, int nxl, int nmsl)
{
- struct plchrstr *pcp = plchr + pp->pln_type;
- struct mchrstr *mcp = mchr + sp->shp_type;
- int wanted;
-
- if (pcp->pl_flags & P_K) {
- /* chopper, try chopper slot first */
- if (sp->shp_nchoppers < mcp->m_nchoppers)
- return ++sp->shp_nchoppers;
- /* else try plane slot */
- wanted = M_FLY;
- } else if (pcp->pl_flags & P_E) {
- /* x-light, try x-light slot first */
- if (sp->shp_nxlight < mcp->m_nxlight)
- return ++sp->shp_nxlight;
- /* else try plane slot */
- wanted = M_MSL | M_FLY;
- } else if (!(pcp->pl_flags & P_L)) {
- /* not light, no go */
- wanted = 0;
- } else if (pcp->pl_flags & P_M) {
- /* missile, use plane slot */
- wanted = M_MSL | M_FLY;
- } else {
- /* fixed-wing plane, use plane slot */
- wanted = M_FLY;
- }
-
- if ((mcp->m_flags & wanted) == 0)
- return 0; /* ship not capable */
+ struct mchrstr *mcp = &mchr[sp->shp_type];
+ int nfw = n - nch - nxl - nmsl;
+
+ if (nch > mcp->m_nchoppers) /* overflow into fixed-wing slots */
+ nfw += nch - mcp->m_nchoppers;
+ if (nxl > mcp->m_nxlight) /* overflow into missile slots */
+ nmsl += nxl - mcp->m_nxlight;
+ if (nmsl && !(mcp->m_flags & (M_MSL | M_FLY)))
+ return 0; /* missile slots wanted */
+ if (nfw && !(mcp->m_flags & M_FLY))
+ return 0; /* fixed-wing slots wanted */
+ return nfw + nmsl <= mcp->m_nplanes;
+}
- if (sp->shp_nplane < mcp->m_nplanes)
- return ++sp->shp_nplane;
+/*
+ * Increment carrier plane counters for @pp.
+ * If it's a chopper, increment *@nch.
+ * Else, if it's x-light, increment *@nxl.
+ * Else, if it's a light missile, increment *@msl.
+ * Return non-zero if it's a chopper, x-light or light.
+ */
+static int
+inc_shp_nplane(struct plnstr *pp, int *nch, int *nxl, int *nmsl)
+{
+ struct plchrstr *pcp = &plchr[pp->pln_type];
- return 0;
+ if (pcp->pl_flags & P_K)
+ (*nch)++;
+ else if (pcp->pl_flags & P_E)
+ (*nxl)++;
+ else if (!(pcp->pl_flags & P_L))
+ return 0;
+ else if (pcp->pl_flags & P_M)
+ (*nmsl)++;
+ return 1;
}
/*
- * Fit a plane of PP's type off ship SP.
- * Adjust SP's plane counters, badly. You need to run count_planes()
- * before the next fit_plane_on_ship().
- * Updating the plane accordingly is the caller's job.
+ * Can @pp be loaded on a ship of @sp's type?
*/
-static void
-fit_plane_off_ship(struct plnstr *pp, struct shpstr *sp)
+int
+could_be_on_ship(struct plnstr *pp, struct shpstr *sp)
{
- /*
- * Frees chopper and nxlight slots first, which is why we need to
- * run count_planes() before fit_plane_on_ship().
- */
- struct plchrstr *pcp = plchr + pp->pln_type;
+ int nch = 0, nxl = 0, nmsl = 0;
- if (pcp->pl_flags & P_K) {
- if (sp->shp_nchoppers) {
- sp->shp_nchoppers--;
- return;
- }
- } else if (pcp->pl_flags & P_E) {
- if (sp->shp_nxlight) {
- sp->shp_nxlight--;
- return;
- }
- }
-
- if (CANT_HAPPEN(sp->shp_nplane == 0))
- sp->shp_nplane = 1;
- sp->shp_nplane--;
+ if (!inc_shp_nplane(pp, &nch, &nxl, &nmsl))
+ return 0;
+ return ship_can_carry(sp, 1, nch, nxl, nmsl);
}
int
put_plane_on_ship(struct plnstr *plane, struct shpstr *ship)
{
+ int n, nch, nxl, nmsl;
+
if (plane->pln_ship == ship->shp_uid)
return 1; /* Already on ship */
- if (!fit_plane_on_ship(plane, ship))
- return 0;
+ n = shp_nplane(ship, &nch, &nxl, &nmsl);
+ if (!inc_shp_nplane(plane, &nch, &nxl, &nmsl))
+ return 0; /* not a carrier plane */
+ if (!ship_can_carry(ship, n + 1, nch, nxl, nmsl))
+ return 0; /* no space */
plane->pln_x = ship->shp_x;
plane->pln_y = ship->shp_y;
plane->pln_ship = ship->shp_uid;
putplane(plane->pln_uid, plane);
- putship(ship->shp_uid, ship);
return 1;
}
-void
-take_plane_off_ship(struct plnstr *plane, struct shpstr *ship)
-{
- if (CANT_HAPPEN(plane->pln_ship != ship->shp_uid))
- return;
-
- fit_plane_off_ship(plane, ship);
- plane->pln_ship = -1;
- putship(ship->shp_uid, ship);
- putplane(plane->pln_uid, plane);
-}
-
-/*
- * Fit a plane of PP's type on land unit LP.
- * Adjust LP's plane counters.
- * Updating the plane accordingly is the caller's job.
- * Return whether it fits.
- */
-static int
-fit_plane_on_land(struct plnstr *pp, struct lndstr *lp)
-{
- struct plchrstr *pcp = plchr + pp->pln_type;
- struct lchrstr *lcp = lchr + lp->lnd_type;
-
- if ((pcp->pl_flags & P_E) && lp->lnd_nxlight < lcp->l_nxlight)
- return ++lp->lnd_nxlight;
-
- return 0;
-}
-
-/*
- * Fit a plane of PP's type off land unit LP.
- * Adjust LP's plane counters.
- * Updating the plane accordingly is the caller's job.
- */
-static void
-fit_plane_off_land(struct plnstr *pp, struct lndstr *lp)
-{
- if (CANT_HAPPEN(lp->lnd_nxlight == 0))
- lp->lnd_nxlight = 1;
- lp->lnd_nxlight--;
-}
-
int
put_plane_on_land(struct plnstr *plane, struct lndstr *land)
{
if (plane->pln_land == land->lnd_uid)
return 1; /* Already on unit */
-
- if (!fit_plane_on_land(plane, land))
+ if (!(plchr[plane->pln_type].pl_flags & P_E))
+ return 0;
+ if (lnd_nxlight(land) >= lchr[land->lnd_type].l_nxlight)
return 0;
plane->pln_x = land->lnd_x;
plane->pln_y = land->lnd_y;
plane->pln_land = land->lnd_uid;
putplane(plane->pln_uid, plane);
- putland(land->lnd_uid, land);
return 1;
}
-void
-take_plane_off_land(struct plnstr *plane, struct lndstr *land)
-{
- if (CANT_HAPPEN(plane->pln_land != land->lnd_uid))
- return;
-
- fit_plane_off_land(plane, land);
- plane->pln_land = -1;
- putland(land->lnd_uid, land);
- putplane(plane->pln_uid, plane);
-}
-
-/*
- * Could a plane of PP's type be on on ship SP?
- */
-int
-could_be_on_ship(struct plnstr *pp, struct shpstr *sp)
-{
- struct shpstr ship;
-
- ship = *sp;
- ship.shp_nplane = ship.shp_nchoppers = ship.shp_nxlight = 0;
- return fit_plane_on_ship(pp, &ship);
-}
-
void
plane_sweep(struct emp_qelem *plane_list, coord x, coord y)
{
if (mines_there == 0)
return;
- if ((sect.sct_type != SCT_WATER) && (sect.sct_type != SCT_HARBR))
+ if (sect.sct_type != SCT_WATER)
return;
for (qp = plane_list->q_forw; ((qp != plane_list) && (mines_there));
if (chance((100.0 - pln_acc(pp)) / 100.0)) {
pr("Sweep! in %s\n",
- xyas(sect.sct_x, sect.sct_y, pp->pln_own));
+ xyas(sect.sct_x, sect.sct_y, player->cnum));
mines_there--;
found = 1;
}
putsect(§);
}
-void
-count_planes(struct shpstr *sp)
-{
- struct nstr_item ni;
- struct plnstr plane;
- int nplane, nchoppers, nxlight;
-
- if (sp->shp_effic < SHIP_MINEFF)
- return;
-
- nplane = sp->shp_nplane;
- sp->shp_nplane = 0;
- nchoppers = sp->shp_nchoppers;
- sp->shp_nchoppers = 0;
- nxlight = sp->shp_nxlight;
- sp->shp_nxlight = 0;
-
- snxtitem_xy(&ni, EF_PLANE, sp->shp_x, sp->shp_y);
- while (nxtitem(&ni, &plane)) {
- if (plane.pln_own == 0)
- continue;
- if (plane.pln_ship == sp->shp_uid) {
- if (!fit_plane_on_ship(&plane, sp))
- CANT_REACH();
- }
- }
-
- if (nplane != sp->shp_nplane ||
- nxlight != sp->shp_nxlight || nchoppers != sp->shp_nchoppers) {
- putship(sp->shp_uid, sp);
- }
-}
-
-void
-count_land_planes(struct lndstr *lp)
-{
- struct nstr_item ni;
- struct plnstr plane;
- int nplane;
-
- if (lp->lnd_effic < LAND_MINEFF)
- return;
-
- nplane = lp->lnd_nxlight;
- lp->lnd_nxlight = 0;
-
- snxtitem_all(&ni, EF_PLANE);
- while (nxtitem(&ni, &plane)) {
- if (plane.pln_own == 0)
- continue;
- if (plane.pln_land == lp->lnd_uid)
- if (!fit_plane_on_land(&plane, lp))
- CANT_REACH();
- }
-
- if (lp->lnd_nxlight != nplane)
- putland(lp->lnd_uid, lp);
-}
-
int
pln_hitchance(struct plnstr *pp, int hardtarget, int type)
{
return hitchance;
}
-/* return 0 if there was a nuclear detonation */
-
int
-pln_damage(struct plnstr *pp, coord x, coord y, char type, int *nukedamp,
- int noisy)
+pln_damage(struct plnstr *pp, char type, char *noisy)
{
- struct nukstr nuke;
struct plchrstr *pcp = plchr + pp->pln_type;
- int load, i;
- int hitroll;
+ int load, i, hitroll, aim, len;
int dam = 0;
- int aim;
int effective = 1;
int pinbomber = 0;
+ char buf[80];
- if (pp->pln_nuketype != -1) {
- if (nuk_on_plane(&nuke, pp->pln_uid) >= 0) {
- mpr(pp->pln_own, "Releasing RV's for %s detonation...\n",
- pp->pln_flags & PLN_AIRBURST ? "airburst" : "groundburst");
- pp->pln_nuketype = -1;
- *nukedamp = detonate(&nuke, x, y,
- pp->pln_flags & PLN_AIRBURST);
- return 0;
- }
- CANT_REACH();
- }
- *nukedamp = 0;
+ if (CANT_HAPPEN(nuk_on_plane(pp) >= 0))
+ return 0;
load = pln_load(pp);
if (!load) /* e.g. ab, blowing up on launch pad */
aim = 100 - aim;
}
+ len = noisy ? snprintf(buf, sizeof(buf), "%s", noisy) : 0;
while (i--) {
+ if (noisy) {
+ if (len > 75) {
+ mpr(pp->pln_own, "%s\n", buf);
+ len = 0;
+ }
+ }
dam += roll(6);
hitroll = roll(100);
if (hitroll >= 90) {
dam += 8;
if (noisy)
- mpr(pp->pln_own, "BLAM");
+ len += sprintf(buf + len, "BLAM");
} else if (hitroll < aim) {
dam += 5;
if (noisy)
- mpr(pp->pln_own, "Blam");
+ len += sprintf(buf + len, "Blam");
} else {
dam += 1;
if (noisy)
- mpr(pp->pln_own, "blam");
+ len += sprintf(buf + len, "blam");
+ }
+ if (noisy) {
+ if (i)
+ len += sprintf(buf + len, "-");
}
- if (i && noisy)
- mpr(pp->pln_own, "-");
}
- if (noisy)
- mpr(pp->pln_own, "\n");
+ if (noisy && len)
+ mpr(pp->pln_own, "%s\n", buf);
if (effective)
dam *= 2;
return dam;
}
int
-pln_mobcost(int dist, struct plnstr *pp, int flags)
+pln_mobcost(int dist, struct plnstr *pp, char mission)
{
double cost;
cost = 20.0 / (pp->pln_effic / 100.0);
- if ((flags & P_F) || (flags & P_ESC))
- cost /= 2;
+ if (mission == 'e' || mission == 0)
+ cost /= 2; /* escort or intercept */
return ldround(cost * dist / pln_range_max(pp) + 5, 1);
}
}
/*
- * Set PP's tech to TLEV along with everything else that depends on it.
+ * Set @pp's tech to @tlev along with everything else that depends on it.
*/
void
pln_set_tech(struct plnstr *pp, int tlev)