Skip to content

Commit

Permalink
Implement function ai_turnaway
Browse files Browse the repository at this point in the history
Defined as 'npc' turns away from 'other', just like turnTo
but +180 degrees.

One user is for example Baal Netbek in the swamp where the player
character ends the dialog with turning away saying "This guy won't
be of help", the other example is the fire demon in Xardas' tower,
where the player first turns away before whirling back to the demon.
  • Loading branch information
mmind committed Feb 13, 2025
1 parent 3b09302 commit 59ae819
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions game/game/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ enum Action:uint32_t {
AI_PrintScreen,
AI_LookAt,
AI_WhirlToNpc,
AI_TurnAway,
};


Expand Down
8 changes: 8 additions & 0 deletions game/game/gamescript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ 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);
Expand Down Expand Up @@ -2921,6 +2922,13 @@ 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);
Expand Down
1 change: 1 addition & 0 deletions game/game/gamescript.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ 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);
Expand Down
7 changes: 7 additions & 0 deletions game/world/aiqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ 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;
Expand Down
1 change: 1 addition & 0 deletions game/world/aiqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ 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);
Expand Down
23 changes: 23 additions & 0 deletions game/world/objects/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,18 @@ 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;

// turn npc's back to oth, so calculate direction from oth to npc
auto dx = x-oth.x;
auto dz = z-oth.z;
auto gl = guild();
float step = float(owner.script().guildVal().turn_speed[gl]);
return rotateTo(dx,dz,step,AnimationSolver::TurnType::Std,dt);
}

bool Npc::implTurnTo(const Npc &oth, uint64_t dt) {
if(&oth==this)
return true;
Expand Down Expand Up @@ -2209,6 +2221,17 @@ void Npc::nextAiAction(AiQueue& queue, uint64_t dt) {
currentLookAt=act.point;
break;
}
case AI_TurnAway: {
if(!prepareTurn()) {
queue.pushFront(std::move(act));
break;
}
if(act.target!=nullptr && implTurnAway(*act.target,dt)) {
queue.pushFront(std::move(act));
break;
}
break;
}
case AI_TurnToNpc: {
if(!prepareTurn()) {
queue.pushFront(std::move(act));
Expand Down
1 change: 1 addition & 0 deletions game/world/objects/npc.h
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ 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, AnimationSolver::TurnType anim, uint64_t dt);
bool implTurnTo (float dx, float dz, AnimationSolver::TurnType anim, uint64_t dt);
Expand Down

0 comments on commit 59ae819

Please sign in to comment.