Skip to content

Commit

Permalink
A clua monster method for finding its summoner
Browse files Browse the repository at this point in the history
The crawl UI highlights a monster's summoner in LOS examination mode,
but there was previously no way to determine a monster's summoner from
clua. This commit adds a summoner_pos() method to the monster info table
that returns a pair of LOS coordinates of the monster's summoner. It
returns nil if the monster's isn't summoned or it the monster or its
summoner are not in LOS. I've copied the summoning monster's mid into
the monster_info class and updated the examination mode UI to use this
information. This way we have a centralized check for whether the
summoner is valid to reveal, at least regardless of LOS concerns.
  • Loading branch information
gammafunk committed Oct 4, 2024
1 parent 933e26f commit 4438645
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
15 changes: 5 additions & 10 deletions crawl-ref/source/directn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2020,15 +2020,11 @@ void direction_chooser::do_redraws()

coord_def direction_chooser::find_summoner()
{
const monster* mon = monster_at(target());

if (mon && mon->is_summoned()
// Don't leak information about rakshasa mirrored illusions.
&& !mon->has_ench(ENCH_PHANTOM_MIRROR)
// Don't leak information about invisible or out-of-los summons.
&& you.can_see(*mon))
const auto *mon = monster_at(target());
if (mon && mon->is_summoned() && you.can_see(*mon))
{
const monster *summ = monster_by_mid(mon->summoner);
monster_info mi(mon);
const monster *summ = monster_by_mid(mi.summoner_id);
// Don't leak information about invisible summoners.
if (summ && you.can_see(*summ))
return summ->pos();
Expand All @@ -2039,8 +2035,7 @@ coord_def direction_chooser::find_summoner()
void direction_chooser::highlight_summoner(crawl_view_buffer &vbuf)
{
const coord_def summ_loc = find_summoner();

if (summ_loc == INVALID_COORD || !you.see_cell(summ_loc))
if (summ_loc == INVALID_COORD)
return;

auto& cell = vbuf(grid2view(summ_loc) - 1);
Expand Down
27 changes: 27 additions & 0 deletions crawl-ref/source/l-moninf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,32 @@ LUAFN(moninf_get_name)
return 1;
}

/*
* The x,y coordinates of the monster that summoned this monster, in player
* centered coordinates. If the monster was not summoned by another monster
* that's currently in LOS, return nil.
* @treturn int
* @treturn int
* @function pos
*/
LUAFN(moninf_get_summoner_pos)
{
MONINF(ls, 1, mi);

const auto *summoner = monster_by_mid(mi->summoner_id);
if (summoner && you.can_see(*summoner))
{
lua_pushnumber(ls, summoner->pos().x - you.pos().x);
lua_pushnumber(ls, summoner->pos().y - you.pos().y);
return 2;
}
else
{
lua_pushnil(ls);
return 1;
}
}

static const struct luaL_reg moninf_lib[] =
{
MIREG(type),
Expand Down Expand Up @@ -895,6 +921,7 @@ static const struct luaL_reg moninf_lib[] =
MIREG(x_pos),
MIREG(y_pos),
MIREG(pos),
MIREG(summoner_pos),
MIREG(avg_local_depth),
MIREG(avg_local_prob),

Expand Down
8 changes: 7 additions & 1 deletion crawl-ref/source/mon-info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,8 @@ monster_info::monster_info(monster_type p_type, monster_type p_base_type)
props[MUTANT_BEAST_FACETS].get_vector().push_back(i);
}

client_id = 0;
client_id = MID_NOBODY;
summoner_id = MID_NOBODY;
}

static description_level_type _article_for(const actor* a)
Expand Down Expand Up @@ -448,17 +449,22 @@ monster_info::monster_info(const monster* m, int milev)

_colour = m->colour;

summoner_id = MID_NOBODY;
if (m->is_summoned()
&& !(m->flags & MF_PERSISTS)
&& (!m->has_ench(ENCH_PHANTOM_MIRROR) || m->friendly()))
{
mb.set(MB_SUMMONED);

if (m->is_abjurable())
mb.set(MB_ABJURABLE);
else
mb.set(MB_MINION);

if (m->type == MONS_SPELLFORGED_SERVITOR && m->summoner == MID_PLAYER)
mb.set(MB_PLAYER_SERVITOR);

summoner_id = m->summoner;
}
else if ((m->is_unrewarding()
|| testbits(m->flags, MF_NO_REWARD)
Expand Down
3 changes: 2 additions & 1 deletion crawl-ref/source/mon-info.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ struct monster_info_base
bool umbraed;
int shield_bonus;

uint32_t client_id;
mid_t client_id;
mid_t summoner_id;
};

// Monster info used by the pane; precomputes some data
Expand Down

0 comments on commit 4438645

Please sign in to comment.