Refactor missile interception code

Keep only the common part in msl_intercept(), and give it internal
linkage.  Wrap new msl_abm_intercept() and msl_asat_intercept() around
it.  They are simpler to use.
This commit is contained in:
Markus Armbruster 2009-10-10 18:26:31 -04:00
parent de2fae0ee6
commit 0060e48ea4
3 changed files with 51 additions and 51 deletions

View file

@ -492,11 +492,12 @@ extern int move_ground(struct sctstr *, struct sctstr *,
int, int *); int, int *);
extern int fly_map(coord, coord); extern int fly_map(coord, coord);
/* mslsub.c */ /* mslsub.c */
extern int msl_intercept(coord, coord, natid, int, int, int, int);
extern int msl_hit(struct plnstr *, int, int, int, int, char *, extern int msl_hit(struct plnstr *, int, int, int, int, char *,
coord, coord, int); coord, coord, int);
extern void msl_sel(struct emp_qelem *, coord, coord, natid, int, extern void msl_sel(struct emp_qelem *, coord, coord, natid, int,
int, int); int, int);
extern int msl_abm_intercept(struct plnstr *, coord, coord, int);
extern int msl_asat_intercept(struct plnstr *, coord, coord);
/* mtch.c */ /* mtch.c */
extern int comtch(char *, struct cmndstr *, int); extern int comtch(char *, struct cmndstr *, int);
/* natarg.c */ /* natarg.c */

View file

@ -360,9 +360,8 @@ launch_sat(struct plnstr *pp)
pr("Your trajectory was a little off.\n"); pr("Your trajectory was a little off.\n");
} }
nreport(player->cnum, N_LAUNCH, 0, 1); nreport(player->cnum, N_LAUNCH, 0, 1);
if (msl_intercept(sx, sy, pp->pln_own, pln_def(pp), 0, P_O, 0)) { if (msl_asat_intercept(pp, sx, sy))
return RET_OK; return RET_OK;
}
pp->pln_x = sx; pp->pln_x = sx;
pp->pln_y = sy; pp->pln_y = sy;
CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED); CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);

View file

@ -122,10 +122,8 @@ msl_hit(struct plnstr *pp, int hardtarget, int type, int news_item,
xyas(x, y, victim)); xyas(x, y, victim));
if ((pcp->pl_flags & P_T && !(pcp->pl_flags & P_MAR))) { if ((pcp->pl_flags & P_T && !(pcp->pl_flags & P_MAR))) {
if (msl_intercept(x, y, pp->pln_own, pln_def(pp), if (msl_abm_intercept(pp, x, y, sublaunch))
sublaunch, P_N, P_O)) {
return 0; return 0;
}
} }
if (pcp->pl_flags & P_MAR) { if (pcp->pl_flags & P_MAR) {
if (shp_missile_defense(x, y, pp->pln_own, pln_def(pp))) { if (shp_missile_defense(x, y, pp->pln_own, pln_def(pp))) {
@ -203,15 +201,13 @@ msl_sel(struct emp_qelem *list, coord x, coord y, natid victim,
} }
} }
int static int
msl_intercept(coord x, coord y, natid bombown, int hardtarget, msl_intercept(struct plnstr *msl, struct sctstr *sp, int sublaunch,
int sublaunch, int wantflags, int nowantflags) struct emp_qelem *irvlist, char *att_name, char *def_name,
int news_item)
{ {
struct plnstr *pp; struct plnstr *pp;
struct plchrstr *pcp; struct plchrstr *pcp;
struct sctstr sect;
struct emp_qelem *irvlist;
struct emp_qelem foo;
struct emp_qelem *intlist; struct emp_qelem *intlist;
struct emp_qelem intfoo; struct emp_qelem intfoo;
struct emp_qelem *qp; struct emp_qelem *qp;
@ -219,32 +215,8 @@ msl_intercept(coord x, coord y, natid bombown, int hardtarget,
struct plist *ip; struct plist *ip;
int icount = 0; int icount = 0;
short destroyed; short destroyed;
char *att_name; char *who = sublaunch ? "" : cname(msl->pln_own);
char *def_name;
int news_item;
char *who = sublaunch ? "" : cname(bombown);
getsect(x, y, &sect);
if (wantflags == P_O && !nowantflags) {
att_name = "satellite";
def_name = "a-sat missile";
news_item = N_SAT_KILL;
CANT_HAPPEN(sublaunch);
mpr(sect.sct_own, "%s has positioned a satellite over %s\n",
cname(bombown), xyas(x, y, sect.sct_own));
} else if (wantflags == P_N && nowantflags == P_O) {
att_name = "warhead";
def_name = "abm";
news_item = sublaunch ? N_NUKE_SSTOP : N_NUKE_STOP;
} else {
att_name = "elephant";
def_name = "tomato"; /* heh -KHS */
news_item = N_NUKE_STOP;
}
irvlist = &foo;
/* get all hostile abms in range */
msl_sel(irvlist, x, y, bombown, wantflags, nowantflags, 0);
intlist = &intfoo; intlist = &intfoo;
emp_initque(intlist); emp_initque(intlist);
/* First choose interceptors belonging to the target sector */ /* First choose interceptors belonging to the target sector */
@ -253,7 +225,7 @@ msl_intercept(coord x, coord y, natid bombown, int hardtarget,
next = qp->q_forw; next = qp->q_forw;
ip = (struct plist *)qp; ip = (struct plist *)qp;
pp = &ip->plane; pp = &ip->plane;
if (pp->pln_own != sect.sct_own) if (pp->pln_own != sp->sct_own)
continue; continue;
pcp = ip->pcp; pcp = ip->pcp;
if (mission_pln_equip(ip, NULL, 'i') < 0) { if (mission_pln_equip(ip, NULL, 'i') < 0) {
@ -295,7 +267,7 @@ msl_intercept(coord x, coord y, natid bombown, int hardtarget,
free(qp); free(qp);
} }
if (icount == 0) { if (icount == 0) {
mpr(sect.sct_own, "No %ss launched to intercept.\n", def_name); mpr(sp->sct_own, "No %ss launched to intercept.\n", def_name);
return 0; return 0;
} }
@ -308,26 +280,26 @@ msl_intercept(coord x, coord y, natid bombown, int hardtarget,
pp = &ip->plane; pp = &ip->plane;
pcp = ip->pcp; pcp = ip->pcp;
mpr(bombown, "%s %s launched in defense!\n", mpr(msl->pln_own, "%s %s launched in defense!\n",
cname(pp->pln_own), def_name); cname(pp->pln_own), def_name);
if (sect.sct_own == pp->pln_own) { if (sp->sct_own == pp->pln_own) {
mpr(sect.sct_own, "%s launched to intercept %s %s!\n", mpr(sp->sct_own, "%s launched to intercept %s %s!\n",
def_name, who, att_name); def_name, who, att_name);
} else { } else {
mpr(sect.sct_own, mpr(sp->sct_own,
"%s launched an %s to intercept the %s %s!\n", "%s launched an %s to intercept the %s %s!\n",
cname(pp->pln_own), def_name, who, att_name); cname(pp->pln_own), def_name, who, att_name);
mpr(pp->pln_own, mpr(pp->pln_own,
"%s launched to intercept %s %s arcing towards %s territory!\n", "%s launched to intercept %s %s arcing towards %s territory!\n",
def_name, who, att_name, cname(sect.sct_own)); def_name, who, att_name, cname(sp->sct_own));
} }
if (msl_hit(pp, hardtarget, EF_PLANE, news_item, news_item, if (msl_hit(pp, pln_def(msl), EF_PLANE, news_item, news_item,
att_name, x, y, bombown)) { att_name, sp->sct_x, sp->sct_y, msl->pln_own)) {
mpr(bombown, "%s destroyed by %s %s!\n", mpr(msl->pln_own, "%s destroyed by %s %s!\n",
att_name, cname(pp->pln_own), def_name); att_name, cname(pp->pln_own), def_name);
mpr(sect.sct_own, "%s %s intercepted!\n", who, att_name); mpr(sp->sct_own, "%s %s intercepted!\n", who, att_name);
if (sect.sct_own != pp->pln_own) if (sp->sct_own != pp->pln_own)
mpr(pp->pln_own, "%s %s intercepted!\n", who, att_name); mpr(pp->pln_own, "%s %s intercepted!\n", who, att_name);
destroyed = 1; destroyed = 1;
} }
@ -346,10 +318,38 @@ msl_intercept(coord x, coord y, natid bombown, int hardtarget,
if (destroyed) if (destroyed)
return 1; return 1;
if (icount) { if (icount) {
mpr(bombown, "%s made it through %s defenses!\n", mpr(msl->pln_own, "%s made it through %s defenses!\n",
att_name, def_name); att_name, def_name);
mpr(sect.sct_own, "%s made it through %s defenses!\n", mpr(sp->sct_own, "%s made it through %s defenses!\n",
att_name, def_name); att_name, def_name);
} }
return 0; return 0;
} }
int
msl_abm_intercept(struct plnstr *msl, coord x, coord y, int sublaunch)
{
struct sctstr sect;
struct emp_qelem irvlist;
getsect(x, y, &sect);
msl_sel(&irvlist, x, y, msl->pln_own, P_N, P_O, 0);
return msl_intercept(msl, &sect, sublaunch,
&irvlist, "warhead", "abm",
sublaunch ? N_NUKE_SSTOP : N_NUKE_STOP);
}
int
msl_asat_intercept(struct plnstr *msl, coord x, coord y)
{
struct sctstr sect;
struct emp_qelem irvlist;
getsect(x, y, &sect);
mpr(sect.sct_own, "%s has positioned a satellite over %s\n",
cname(msl->pln_own), xyas(x, y, sect.sct_own));
msl_sel(&irvlist, x, y, msl->pln_own, P_O, 0, 0);
return msl_intercept(msl, &sect, 0,
&irvlist, "satellite", "a-sat missile",
N_SAT_KILL);
}