streamline act_on_act()
Refine the code from pull request #777 by changing act_on_act() to take 3 arguments instead of 6. x,y and dx,dy are mutually exclusive so it doesn't need both pairs provided that the caller is adjusted to pass the ones appropriate for the action, and dir is easily derived from dx,dy for the couple of cases that use it.
This commit is contained in:
61
src/cmd.c
61
src/cmd.c
@@ -1,4 +1,4 @@
|
|||||||
/* NetHack 3.7 cmd.c $NHDT-Date: 1654899519 2022/06/10 22:18:39 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.571 $ */
|
/* NetHack 3.7 cmd.c $NHDT-Date: 1655114986 2022/06/13 10:09:46 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.572 $ */
|
||||||
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
|
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
|
||||||
/*-Copyright (c) Robert Patrick Rankin, 2013. */
|
/*-Copyright (c) Robert Patrick Rankin, 2013. */
|
||||||
/* NetHack may be freely redistributed. See license for details. */
|
/* NetHack may be freely redistributed. See license for details. */
|
||||||
@@ -151,7 +151,7 @@ static void reset_cmd_vars(boolean);
|
|||||||
static void mcmd_addmenu(winid, int, const char *);
|
static void mcmd_addmenu(winid, int, const char *);
|
||||||
static char here_cmd_menu(void);
|
static char here_cmd_menu(void);
|
||||||
static char there_cmd_menu(int, int, int);
|
static char there_cmd_menu(int, int, int);
|
||||||
static void act_on_act(int, int, int, int, int, int);
|
static void act_on_act(int, int, int);
|
||||||
static char readchar_core(int *, int *, int *);
|
static char readchar_core(int *, int *, int *);
|
||||||
static char *parse(void);
|
static char *parse(void);
|
||||||
static void show_direction_keys(winid, char, boolean);
|
static void show_direction_keys(winid, char, boolean);
|
||||||
@@ -490,9 +490,9 @@ doextcmd(void)
|
|||||||
|
|
||||||
/* format extended command flags for display */
|
/* format extended command flags for display */
|
||||||
static char *
|
static char *
|
||||||
doc_extcmd_flagstr(winid menuwin,
|
doc_extcmd_flagstr(
|
||||||
/* if Null, add a footnote to the menu */
|
winid menuwin,
|
||||||
const struct ext_func_tab *efp)
|
const struct ext_func_tab *efp) /* if Null, add a footnote to the menu */
|
||||||
{
|
{
|
||||||
static char Abuf[10]; /* 5 would suffice: {'[','m','A',']','\0'} */
|
static char Abuf[10]; /* 5 would suffice: {'[','m','A',']','\0'} */
|
||||||
|
|
||||||
@@ -4459,9 +4459,10 @@ show_direction_keys(
|
|||||||
an invalid direction after a prefix key ('F', 'g', 'm', &c), which
|
an invalid direction after a prefix key ('F', 'g', 'm', &c), which
|
||||||
might be bogus but could be up, down, or self when not applicable */
|
might be bogus but could be up, down, or self when not applicable */
|
||||||
static boolean
|
static boolean
|
||||||
help_dir(char sym,
|
help_dir(
|
||||||
int spkey, /* NHKF_ code for prefix key, if used, or for ESC */
|
char sym,
|
||||||
const char *msg)
|
int spkey, /* NHKF_ code for prefix key, if used, or for ESC */
|
||||||
|
const char *msg)
|
||||||
{
|
{
|
||||||
static const char wiz_only_list[] = "EFGIVW";
|
static const char wiz_only_list[] = "EFGIVW";
|
||||||
char ctrl;
|
char ctrl;
|
||||||
@@ -4905,15 +4906,19 @@ there_cmd_menu_common(
|
|||||||
return K;
|
return K;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* queue up command(s) to perform #therecmdmenu action */
|
||||||
static void
|
static void
|
||||||
act_on_act(int act, int x, int y, int dx, int dy, int dir)
|
act_on_act(
|
||||||
|
int act, /* action */
|
||||||
|
int dx, int dy) /* delta to adjacent spot (except for couple of cases) */
|
||||||
{
|
{
|
||||||
struct obj *otmp;
|
struct obj *otmp;
|
||||||
|
int dir;
|
||||||
|
|
||||||
switch (act) {
|
switch (act) {
|
||||||
case MCMD_TRAVEL:
|
case MCMD_TRAVEL:
|
||||||
iflags.travelcc.x = u.tx = x;
|
iflags.travelcc.x = u.tx = dx; /* caller passed x via dx */
|
||||||
iflags.travelcc.y = u.ty = y;
|
iflags.travelcc.y = u.ty = dy; /* and y via dy */
|
||||||
cmdq_add_ec(dotravel_target);
|
cmdq_add_ec(dotravel_target);
|
||||||
break;
|
break;
|
||||||
case MCMD_THROW_OBJ:
|
case MCMD_THROW_OBJ:
|
||||||
@@ -4962,7 +4967,8 @@ act_on_act(int act, int x, int y, int dx, int dy, int dir)
|
|||||||
cmdq_add_dir(dx, dy, 0);
|
cmdq_add_dir(dx, dy, 0);
|
||||||
break;
|
break;
|
||||||
case MCMD_MOVE_DIR:
|
case MCMD_MOVE_DIR:
|
||||||
cmdq_add_ec(move_funcs[xytod(dx, dy)][MV_WALK]);
|
dir = xytod(dx, dy);
|
||||||
|
cmdq_add_ec(move_funcs[dir][MV_WALK]);
|
||||||
break;
|
break;
|
||||||
case MCMD_RIDE:
|
case MCMD_RIDE:
|
||||||
cmdq_add_ec(doride);
|
cmdq_add_ec(doride);
|
||||||
@@ -4983,6 +4989,7 @@ act_on_act(int act, int x, int y, int dx, int dy, int dir)
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case MCMD_ATTACK_NEXT2U:
|
case MCMD_ATTACK_NEXT2U:
|
||||||
|
dir = xytod(dx, dy);
|
||||||
cmdq_add_ec(move_funcs[dir][MV_WALK]);
|
cmdq_add_ec(move_funcs[dir][MV_WALK]);
|
||||||
break;
|
break;
|
||||||
case MCMD_TALK:
|
case MCMD_TALK:
|
||||||
@@ -5041,8 +5048,8 @@ act_on_act(int act, int x, int y, int dx, int dy, int dir)
|
|||||||
cmdq_add_ec(dolook);
|
cmdq_add_ec(dolook);
|
||||||
break;
|
break;
|
||||||
case MCMD_LOOK_AT:
|
case MCMD_LOOK_AT:
|
||||||
g.clicklook_cc.x = x;
|
g.clicklook_cc.x = dx; /* caller passed x via dx */
|
||||||
g.clicklook_cc.y = y;
|
g.clicklook_cc.y = dy; /* and y via dy */
|
||||||
cmdq_add_ec(doclicklook);
|
cmdq_add_ec(doclicklook);
|
||||||
break;
|
break;
|
||||||
case MCMD_UNTRAP_HERE:
|
case MCMD_UNTRAP_HERE:
|
||||||
@@ -5056,10 +5063,13 @@ act_on_act(int act, int x, int y, int dx, int dy, int dir)
|
|||||||
case MCMD_CAST_SPELL:
|
case MCMD_CAST_SPELL:
|
||||||
cmdq_add_ec(docast);
|
cmdq_add_ec(docast);
|
||||||
break;
|
break;
|
||||||
default: break;
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define act_needs_xy(act) ((act) == MCMD_TRAVEL || (act) == MCMD_LOOK_AT)
|
||||||
|
|
||||||
/* offer choice of actions to perform at adjacent location <x,y> */
|
/* offer choice of actions to perform at adjacent location <x,y> */
|
||||||
static char
|
static char
|
||||||
there_cmd_menu(int x, int y, int mod)
|
there_cmd_menu(int x, int y, int mod)
|
||||||
@@ -5069,7 +5079,6 @@ there_cmd_menu(int x, int y, int mod)
|
|||||||
int npick = 0, K = 0;
|
int npick = 0, K = 0;
|
||||||
menu_item *picks = (menu_item *) 0;
|
menu_item *picks = (menu_item *) 0;
|
||||||
int dx = sgn(x - u.ux), dy = sgn(y - u.uy);
|
int dx = sgn(x - u.ux), dy = sgn(y - u.uy);
|
||||||
int dir = xytod(dx, dy);
|
|
||||||
int act = MCMD_NOTHING;
|
int act = MCMD_NOTHING;
|
||||||
|
|
||||||
win = create_nhwindow(NHW_MENU);
|
win = create_nhwindow(NHW_MENU);
|
||||||
@@ -5085,18 +5094,22 @@ there_cmd_menu(int x, int y, int mod)
|
|||||||
|
|
||||||
if (!K) {
|
if (!K) {
|
||||||
/* no menu options, try to move */
|
/* no menu options, try to move */
|
||||||
if (next2u(x, y) && !test_move(u.ux, u.uy, dx, dy, TEST_MOVE))
|
if (next2u(x, y) && !test_move(u.ux, u.uy, dx, dy, TEST_MOVE)) {
|
||||||
|
int dir = xytod(dx, dy);
|
||||||
|
|
||||||
cmdq_add_ec(move_funcs[dir][MV_WALK]);
|
cmdq_add_ec(move_funcs[dir][MV_WALK]);
|
||||||
else if (flags.travelcmd) {
|
} else if (flags.travelcmd) {
|
||||||
iflags.travelcc.x = u.tx = x;
|
iflags.travelcc.x = u.tx = x;
|
||||||
iflags.travelcc.y = u.ty = y;
|
iflags.travelcc.y = u.ty = y;
|
||||||
cmdq_add_ec(dotravel_target);
|
cmdq_add_ec(dotravel_target);
|
||||||
}
|
}
|
||||||
npick = 0;
|
npick = 0;
|
||||||
ch = '\0';
|
ch = '\0';
|
||||||
} else if ((K == 1) && (act != MCMD_NOTHING)) {
|
} else if (K == 1 && act != MCMD_NOTHING) {
|
||||||
destroy_nhwindow(win);
|
destroy_nhwindow(win);
|
||||||
act_on_act(act, x, y, dx, dy, dir);
|
if (act_needs_xy(act))
|
||||||
|
dx = x, dy = y;
|
||||||
|
act_on_act(act, dx, dy);
|
||||||
return '\0';
|
return '\0';
|
||||||
} else {
|
} else {
|
||||||
end_menu(win, "What do you want to do?");
|
end_menu(win, "What do you want to do?");
|
||||||
@@ -5106,15 +5119,19 @@ there_cmd_menu(int x, int y, int mod)
|
|||||||
destroy_nhwindow(win);
|
destroy_nhwindow(win);
|
||||||
if (npick > 0) {
|
if (npick > 0) {
|
||||||
act = picks->item.a_int;
|
act = picks->item.a_int;
|
||||||
|
|
||||||
free((genericptr_t) picks);
|
free((genericptr_t) picks);
|
||||||
|
|
||||||
act_on_act(act, x, y, dx, dy, dir);
|
/* most actions want <dx,dy> but a few want absolute <x,y> */
|
||||||
|
if (act_needs_xy(act))
|
||||||
|
dx = x, dy = y;
|
||||||
|
act_on_act(act, dx, dy);
|
||||||
return '\0';
|
return '\0';
|
||||||
}
|
}
|
||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#undef act_needs_xy
|
||||||
|
|
||||||
static char
|
static char
|
||||||
here_cmd_menu(void)
|
here_cmd_menu(void)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user