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

2 additional AI turn actions #718

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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: 3 additions & 1 deletion game/game/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,9 @@ enum Action:uint32_t {
AI_PointAt,
AI_StopPointAt,
AI_PrintScreen,
AI_LookAt
AI_LookAt,
AI_WhirlToNpc,
AI_TurnAway,
};


Expand Down
16 changes: 16 additions & 0 deletions game/game/gamescript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ void GameScript::initCommon() {
bindExternal("ai_lookatnpc", &GameScript::ai_lookatnpc);
bindExternal("ai_removeweapon", &GameScript::ai_removeweapon);
bindExternal("ai_unreadyspell", &GameScript::ai_unreadyspell);
bindExternal("ai_turnaway", &GameScript::ai_turnaway);
bindExternal("ai_turntonpc", &GameScript::ai_turntonpc);
bindExternal("ai_whirlaround", &GameScript::ai_whirlaround);
bindExternal("ai_outputsvm", &GameScript::ai_outputsvm);
bindExternal("ai_outputsvm_overlay", &GameScript::ai_outputsvm_overlay);
bindExternal("ai_startstate", &GameScript::ai_startstate);
Expand Down Expand Up @@ -2786,13 +2788,27 @@ void GameScript::ai_unreadyspell(std::shared_ptr<zenkit::INpc> npcRef) {
npc->aiPush(AiQueue::aiRemoveWeapon());
}

void GameScript::ai_turnaway(std::shared_ptr<zenkit::INpc> selfRef, std::shared_ptr<zenkit::INpc> npcRef) {
auto npc = findNpc(npcRef);
auto self = findNpc(selfRef);
if(self!=nullptr)
self->aiPush(AiQueue::aiTurnAway(npc));
}

void GameScript::ai_turntonpc(std::shared_ptr<zenkit::INpc> selfRef, std::shared_ptr<zenkit::INpc> npcRef) {
auto npc = findNpc(npcRef);
auto self = findNpc(selfRef);
if(self!=nullptr)
self->aiPush(AiQueue::aiTurnToNpc(npc));
}

void GameScript::ai_whirlaround(std::shared_ptr<zenkit::INpc> selfRef, std::shared_ptr<zenkit::INpc> npcRef) {
auto npc = findNpc(npcRef);
auto self = findNpc(selfRef);
if(self!=nullptr)
self->aiPush(AiQueue::aiWhirlToNpc(npc));
}

void GameScript::ai_outputsvm(std::shared_ptr<zenkit::INpc> selfRef, std::shared_ptr<zenkit::INpc> targetRef, std::string_view name) {
auto target = findNpc(targetRef);
auto self = findNpc(selfRef);
Expand Down
2 changes: 2 additions & 0 deletions game/game/gamescript.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,9 @@ class GameScript final {
void ai_lookatnpc (std::shared_ptr<zenkit::INpc> selfRef, std::shared_ptr<zenkit::INpc> npcRef);
void ai_removeweapon (std::shared_ptr<zenkit::INpc> npcRef);
void ai_unreadyspell (std::shared_ptr<zenkit::INpc> npcRef);
void ai_turnaway (std::shared_ptr<zenkit::INpc> selfRef, std::shared_ptr<zenkit::INpc> npcRef);
void ai_turntonpc (std::shared_ptr<zenkit::INpc> selfRef, std::shared_ptr<zenkit::INpc> npcRef);
void ai_whirlaround (std::shared_ptr<zenkit::INpc> selfRef, std::shared_ptr<zenkit::INpc> npcRef);
void ai_outputsvm (std::shared_ptr<zenkit::INpc> selfRef, std::shared_ptr<zenkit::INpc> targetRef, std::string_view name);
void ai_outputsvm_overlay(std::shared_ptr<zenkit::INpc> selfRef, std::shared_ptr<zenkit::INpc> targetRef, std::string_view name);
void ai_startstate (std::shared_ptr<zenkit::INpc> selfRef, int func, int state, std::string_view wp);
Expand Down
14 changes: 14 additions & 0 deletions game/world/aiqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,27 @@ AiQueue::AiAction AiQueue::aiRemoveWeapon() {
return a;
}

AiQueue::AiAction AiQueue::aiTurnAway(Npc *other) {
AiAction a;
a.act = AI_TurnAway;
a.target = other;
return a;
}

AiQueue::AiAction AiQueue::aiTurnToNpc(Npc *other) {
AiAction a;
a.act = AI_TurnToNpc;
a.target = other;
return a;
}

AiQueue::AiAction AiQueue::aiWhirlToNpc(Npc *other) {
AiAction a;
a.act = AI_WhirlToNpc;
a.target = other;
return a;
}

AiQueue::AiAction AiQueue::aiGoToNpc(Npc *other) {
AiAction a;
a.act = AI_GoToNpc;
Expand Down
2 changes: 2 additions & 0 deletions game/world/aiqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ class AiQueue {
static AiAction aiLookAtNpc(Npc* other);
static AiAction aiStopLookAt();
static AiAction aiRemoveWeapon();
static AiAction aiTurnAway (Npc *other);
static AiAction aiTurnToNpc(Npc *other);
static AiAction aiWhirlToNpc(Npc *other);
static AiAction aiGoToNpc (Npc *other);
static AiAction aiGoToNextFp(std::string_view fp);
static AiAction aiStartState(ScriptFn stateFn, int behavior, Npc *other, Npc* victum, std::string_view wp);
Expand Down
83 changes: 79 additions & 4 deletions game/world/objects/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,21 @@ bool Npc::implLookAt(float dx, float dy, float dz, uint64_t dt) {
return false;
}

bool Npc::implTurnAway(const Npc &oth, uint64_t dt) {
if(&oth==this)
return true;

auto dx = oth.x-x;
auto dz = oth.z-z;
float a = angleDir(dx, dz) + 180;
if(a > 360)
a-= 360;

auto gl = guild();
float step = float(owner.script().guildVal().turn_speed[gl]);
return rotateTo(a,step,false,dt);
}

bool Npc::implTurnTo(const Npc &oth, uint64_t dt) {
if(&oth==this)
return true;
Expand All @@ -1352,6 +1367,16 @@ bool Npc::implTurnTo(float dx, float dz, bool noAnim, uint64_t dt) {
return rotateTo(dx,dz,step,noAnim,dt);
}

bool Npc::implWhirlTo(const Npc &oth, uint64_t dt) {
if(&oth==this)
return true;
auto dx = oth.x-x;
auto dz = oth.z-z;
auto gl = guild();
float step = float(owner.script().guildVal().turn_speed[gl]) * 2;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on video evidence you shared previously:

https://youtu.be/GKpt0Y07gJw?t=1538 ... when talking to Xardas' daemon, which is this sequence from above

Rotation speed appears(not 100% sure) to be the same, but with different animation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhhh ... you're right ... thanks for noticing that discrepancy.

Digging deeper, the animation data does have t_surprise_ccw and t_surprise_cw states and G1 data comes uncompiled with "//WhirlAround" comments intact above those :-) .

Additionally, it looks like in G1 everybody including humans have the whirl animation, where in G2, monsters have it - including the newly added ones - but not humans.

So I'll continue digging :-) .

return rotateTo(dx,dz,step,false,dt);
}

bool Npc::implGoTo(uint64_t dt) {
float dist = 0;
if(go2.npc) {
Expand Down Expand Up @@ -2191,6 +2216,27 @@ void Npc::nextAiAction(AiQueue& queue, uint64_t dt) {
currentLookAt=act.point;
break;
}
case AI_TurnAway: {
const auto st = bodyStateMasked();
if(interactive()==nullptr && (st==BS_WALK || st==BS_SNEAK)) {
stopWalkAnimation();
queue.pushFront(std::move(act));
break;
}
if(interactive()==nullptr) {
stopWalkAnimation();
visual.stopDlgAnim(*this);
}
if(act.target!=nullptr && implTurnAway(*act.target,dt)) {
queue.pushFront(std::move(act));
break;
}
// Not looking quite correct in dialogs, when npc turns around
// Example: Esteban dialog
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy-paste?

// currentLookAt = nullptr;
// currentLookAtNpc = nullptr;
break;
}
case AI_TurnToNpc: {
const auto st = bodyStateMasked();
if(interactive()==nullptr && (st==BS_WALK || st==BS_SNEAK)) {
Expand All @@ -2212,6 +2258,27 @@ void Npc::nextAiAction(AiQueue& queue, uint64_t dt) {
// currentLookAtNpc = nullptr;
break;
}
case AI_WhirlToNpc: {
const auto st = bodyStateMasked();
if(interactive()==nullptr && (st==BS_WALK || st==BS_SNEAK)) {
stopWalkAnimation();
queue.pushFront(std::move(act));
break;
}
if(interactive()==nullptr) {
stopWalkAnimation();
visual.stopDlgAnim(*this);
}
if(act.target!=nullptr && implWhirlTo(*act.target,dt)) {
queue.pushFront(std::move(act));
break;
}
// Not looking quite correct in dialogs, when npc turns around
// Example: Esteban dialog
// currentLookAt = nullptr;
// currentLookAtNpc = nullptr;
break;
}
case AI_GoToNpc:
if(!setInteraction(nullptr)) {
queue.pushFront(std::move(act));
Expand Down Expand Up @@ -3220,18 +3287,26 @@ bool Npc::turnTo(float dx, float dz, bool noAnim, uint64_t dt) {
}

bool Npc::rotateTo(float dx, float dz, float step, bool noAnim, uint64_t dt) {
//step *= (float(dt)/1000.f)*60.f/100.f;
step *= (float(dt)/1000.f);

if(dx==0.f && dz==0.f) {
setAnimRotate(0);
return false;
}

return rotateTo(angleDir(dx, dz), step, noAnim, dt);
}

bool Npc::rotateTo(float a, float step, bool noAnim, uint64_t dt) {
if(a==0.f) {
setAnimRotate(0);
return false;
}

if(!isRotationAllowed())
return false;

float a = angleDir(dx,dz);
//step *= (float(dt)/1000.f)*60.f/100.f;
step *= (float(dt)/1000.f);

float da = a-angle;

if(noAnim || std::cos(double(da)*M_PI/180.0)>0) {
Expand Down
3 changes: 3 additions & 0 deletions game/world/objects/npc.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ class Npc final {

bool turnTo (float dx, float dz, bool noAnim, uint64_t dt);
bool rotateTo(float dx, float dz, float speed, bool anim, uint64_t dt);
bool rotateTo(float a, float step, bool noAnim, uint64_t dt);
bool isRotationAllowed() const;
auto playAnimByName(std::string_view name, BodyState bs) -> const Animation::Sequence*;

Expand Down Expand Up @@ -471,9 +472,11 @@ class Npc final {
bool implLookAtWp(uint64_t dt);
bool implLookAtNpc(uint64_t dt);
bool implLookAt (float dx, float dy, float dz, uint64_t dt);
bool implTurnAway(const Npc& oth, uint64_t dt);
bool implTurnTo (const Npc& oth, uint64_t dt);
bool implTurnTo (const Npc& oth, bool noAnim, uint64_t dt);
bool implTurnTo (float dx, float dz, bool noAnim, uint64_t dt);
bool implWhirlTo(const Npc& oth, uint64_t dt);
bool implGoTo (uint64_t dt);
bool implGoTo (uint64_t dt, float destDist);
bool implAttack (uint64_t dt);
Expand Down