Skip to content

Commit

Permalink
Implmeent 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 the crazy Baal Netbek in the swamp where the
player character ends the dialog with turning away saying
"This guy won't be of help"
  • Loading branch information
mmind committed Jan 15, 2025
1 parent 75226a3 commit ac223f4
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 4 deletions.
1 change: 1 addition & 0 deletions game/game/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,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 @@ -238,6 +238,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 @@ -2787,6 +2788,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 @@ -369,6 +369,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
52 changes: 48 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 Down Expand Up @@ -2201,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
// currentLookAt = nullptr;
// currentLookAtNpc = nullptr;
break;
}
case AI_TurnToNpc: {
const auto st = bodyStateMasked();
if(interactive()==nullptr && (st==BS_WALK || st==BS_SNEAK)) {
Expand Down Expand Up @@ -3251,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
2 changes: 2 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,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, bool noAnim, uint64_t dt);
bool implTurnTo (float dx, float dz, bool noAnim, uint64_t dt);
Expand Down

0 comments on commit ac223f4

Please sign in to comment.