Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target enemies based on walk progress #7473

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Source/msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1554,7 +1554,7 @@ size_t OnAttackMonster(const TCmd *pCmd, Player &player)
const uint16_t monsterIdx = SDL_SwapLE16(message.wParam1);

if (gbBufferMsgs != 1 && player.isOnActiveLevel() && monsterIdx < MaxMonsters) {
Point position = Monsters[monsterIdx].position.future;
Point position = GetTargetMonsterPosition(Monsters[monsterIdx], player);
if (player.position.tile.WalkingDistance(position) > 1)
MakePlrPath(player, position, false);
player.destAction = ACTION_ATTACKMON;
Expand All @@ -1570,7 +1570,7 @@ size_t OnAttackPlayer(const TCmd *pCmd, Player &player)
const uint16_t playerIdx = SDL_SwapLE16(message.wParam1);

if (gbBufferMsgs != 1 && player.isOnActiveLevel() && playerIdx < Players.size()) {
MakePlrPath(player, Players[playerIdx].position.future, false);
MakePlrPath(player, GetTargetPlayerPosition(Players[playerIdx], player), false);
player.destAction = ACTION_ATTACKPLR;
player.destParam1 = playerIdx;
}
Expand Down
117 changes: 79 additions & 38 deletions Source/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,40 @@ std::vector<Player> Players;
Player *InspectPlayer;
bool MyPlayerIsDead;

WorldTilePosition GetTargetMonsterPosition(const Monster &monster, const Player &player)
{
if (IsAnyOf(player.destAction, ACTION_RATTACKMON, ACTION_RATTACKPLR, ACTION_SPELLMON, ACTION_SPELLPLR))
return monster.position.future;

WorldTilePosition targetPosition = monster.position.future;

if (monster.isWalking()) {
auto progress = monster.animInfo.getAnimationProgress();
if (progress <= 64) {
kphoenix137 marked this conversation as resolved.
Show resolved Hide resolved
targetPosition = monster.position.tile;
}
}

return targetPosition;
}

WorldTilePosition GetTargetPlayerPosition(const Player &targetPlayer, const Player &player)
{
if (IsAnyOf(player.destAction, ACTION_RATTACKMON, ACTION_RATTACKPLR, ACTION_SPELLMON, ACTION_SPELLPLR))
return targetPlayer.position.future;

WorldTilePosition targetPosition = targetPlayer.position.future;

if (targetPlayer.isWalking()) {
auto progress = targetPlayer.AnimInfo.getAnimationProgress();
if (progress <= 64) {
StephenCWills marked this conversation as resolved.
Show resolved Hide resolved
targetPosition = targetPlayer.position.tile;
}
}

return targetPosition;
}

namespace {

struct DirectionSettings {
Expand Down Expand Up @@ -1076,6 +1110,13 @@ void TryDisarm(const Player &player, Object &object)
}
}

bool IsPlayerNextToTarget(Player &player, const WorldTilePosition &target)
{
int x = std::abs(player.position.tile.x - target.x);
int y = std::abs(player.position.tile.y - target.y);
return x <= 1 && y <= 1;
}

void CheckNewPath(Player &player, bool pmWillBeCalled)
{
int x = 0;
Expand All @@ -1097,8 +1138,9 @@ void CheckNewPath(Player &player, bool pmWillBeCalled)
player.Stop();
return;
}
if (player.destAction == ACTION_ATTACKMON)
MakePlrPath(player, monster->position.future, false);
if (player.destAction == ACTION_ATTACKMON) {
MakePlrPath(player, GetTargetMonsterPosition(*monster, player), false);
}
break;
case ACTION_ATTACKPLR:
case ACTION_RATTACKPLR:
Expand All @@ -1108,8 +1150,9 @@ void CheckNewPath(Player &player, bool pmWillBeCalled)
player.Stop();
return;
}
if (player.destAction == ACTION_ATTACKPLR)
MakePlrPath(player, target->position.future, false);
if (player.destAction == ACTION_ATTACKPLR) {
MakePlrPath(player, GetTargetPlayerPosition(*target, player), false);
}
break;
case ACTION_OPERATE:
case ACTION_DISARM:
Expand All @@ -1125,21 +1168,21 @@ void CheckNewPath(Player &player, bool pmWillBeCalled)
}

Direction d;

if (player.walkpath[0] != WALK_NONE) {
if (player._pmode == PM_STAND) {
if (&player == MyPlayer) {
if (player.destAction == ACTION_ATTACKMON || player.destAction == ACTION_ATTACKPLR) {
if (player.destAction == ACTION_ATTACKMON) {
x = std::abs(player.position.future.x - monster->position.future.x);
y = std::abs(player.position.future.y - monster->position.future.y);
d = GetDirection(player.position.future, monster->position.future);
} else {
x = std::abs(player.position.future.x - target->position.future.x);
y = std::abs(player.position.future.y - target->position.future.y);
d = GetDirection(player.position.future, target->position.future);
}
WorldTilePosition targetPosition;

if (player.destAction == ACTION_ATTACKMON)
targetPosition = GetTargetMonsterPosition(*monster, player);
else
targetPosition = GetTargetPlayerPosition(*target, player);

if (x < 2 && y < 2) {
d = GetDirection(player.position.future, targetPosition);

if (IsPlayerNextToTarget(player, targetPosition)) {
ClrPlrPath(player);
if (player.destAction == ACTION_ATTACKMON && monster->talkMsg != TEXT_NONE && monster->talkMsg != TEXT_VILE14) {
TalktoMonster(player, *monster);
Expand Down Expand Up @@ -1203,10 +1246,10 @@ void CheckNewPath(Player &player, bool pmWillBeCalled)
StartAttack(player, d, pmWillBeCalled);
break;
case ACTION_ATTACKMON:
x = std::abs(player.position.tile.x - monster->position.future.x);
y = std::abs(player.position.tile.y - monster->position.future.y);
if (x <= 1 && y <= 1) {
d = GetDirection(player.position.future, monster->position.future);
auto monsterPosition = GetTargetMonsterPosition(*monster, player);

if (IsPlayerNextToTarget(player, monsterPosition)) {
d = GetDirection(player.position.future, monsterPosition);
if (monster->talkMsg != TEXT_NONE && monster->talkMsg != TEXT_VILE14) {
TalktoMonster(player, *monster);
} else {
Expand All @@ -1215,10 +1258,9 @@ void CheckNewPath(Player &player, bool pmWillBeCalled)
}
break;
case ACTION_ATTACKPLR:
x = std::abs(player.position.tile.x - target->position.future.x);
y = std::abs(player.position.tile.y - target->position.future.y);
if (x <= 1 && y <= 1) {
d = GetDirection(player.position.future, target->position.future);
auto targetPosition = GetTargetPlayerPosition(*target, player);
if (IsPlayerNextToTarget(player, targetPosition)) {
d = GetDirection(player.position.future, targetPosition);
StartAttack(player, d, pmWillBeCalled);
}
break;
Expand Down Expand Up @@ -1325,18 +1367,17 @@ void CheckNewPath(Player &player, bool pmWillBeCalled)
StartAttack(player, d, pmWillBeCalled);
player.destAction = ACTION_NONE;
} else if (player.destAction == ACTION_ATTACKMON) {
x = std::abs(player.position.tile.x - monster->position.future.x);
y = std::abs(player.position.tile.y - monster->position.future.y);
if (x <= 1 && y <= 1) {
d = GetDirection(player.position.future, monster->position.future);
auto monsterPosition = GetTargetMonsterPosition(*monster, player);
if (IsPlayerNextToTarget(player, monsterPosition)) {
d = GetDirection(player.position.future, monsterPosition);
StartAttack(player, d, pmWillBeCalled);
}
player.destAction = ACTION_NONE;
} else if (player.destAction == ACTION_ATTACKPLR) {
x = std::abs(player.position.tile.x - target->position.future.x);
y = std::abs(player.position.tile.y - target->position.future.y);
if (x <= 1 && y <= 1) {
d = GetDirection(player.position.future, target->position.future);
auto targetPosition = GetTargetPlayerPosition(*target, player);

if (IsPlayerNextToTarget(player, targetPosition)) {
d = GetDirection(player.position.future, targetPosition);
StartAttack(player, d, pmWillBeCalled);
}
player.destAction = ACTION_NONE;
Expand Down Expand Up @@ -1840,43 +1881,43 @@ void Player::UpdatePreviewCelSprite(_cmd_id cmdId, Point point, uint16_t wParam1
switch (cmdId) {
case _cmd_id::CMD_RATTACKID: {
Monster &monster = Monsters[wParam1];
dir = GetDirection(position.future, monster.position.future);
dir = GetDirection(position.future, GetTargetMonsterPosition(monster, *this));
graphic = player_graphic::Attack;
break;
}
case _cmd_id::CMD_SPELLID: {
Monster &monster = Monsters[wParam1];
dir = GetDirection(position.future, monster.position.future);
dir = GetDirection(position.future, GetTargetMonsterPosition(monster, *this));
graphic = GetPlayerGraphicForSpell(static_cast<SpellID>(wParam2));
break;
}
case _cmd_id::CMD_ATTACKID: {
Monster &monster = Monsters[wParam1];
point = monster.position.future;
point = GetTargetMonsterPosition(monster, *this);
minimalWalkDistance = 2;
if (!CanTalkToMonst(monster)) {
dir = GetDirection(position.future, monster.position.future);
dir = GetDirection(position.future, point);
graphic = player_graphic::Attack;
}
break;
}
case _cmd_id::CMD_RATTACKPID: {
Player &targetPlayer = Players[wParam1];
dir = GetDirection(position.future, targetPlayer.position.future);
dir = GetDirection(position.future, GetTargetPlayerPosition(targetPlayer, *this));
graphic = player_graphic::Attack;
break;
}
case _cmd_id::CMD_SPELLPID: {
Player &targetPlayer = Players[wParam1];
dir = GetDirection(position.future, targetPlayer.position.future);
dir = GetDirection(position.future, GetTargetPlayerPosition(targetPlayer, *this));
graphic = GetPlayerGraphicForSpell(static_cast<SpellID>(wParam2));
break;
}
case _cmd_id::CMD_ATTACKPID: {
Player &targetPlayer = Players[wParam1];
point = targetPlayer.position.future;
point = GetTargetPlayerPosition(targetPlayer, *this);
minimalWalkDistance = 2;
dir = GetDirection(position.future, targetPlayer.position.future);
dir = GetDirection(position.future, point);
graphic = player_graphic::Attack;
break;
}
Expand Down
3 changes: 3 additions & 0 deletions Source/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,9 @@ inline bool IsInspectingPlayer()
}
extern bool MyPlayerIsDead;

WorldTilePosition GetTargetMonsterPosition(const Monster &monster, const Player &player);
WorldTilePosition GetTargetPlayerPosition(const Player &targetPlayer, const Player &player);

Player *PlayerAtPosition(Point position, bool ignoreMovingPlayers = false);

void LoadPlrGFX(Player &player, player_graphic graphic);
Expand Down
Loading