From be3ab5357d0ba847aef6d74a87c56edb2e18034b Mon Sep 17 00:00:00 2001 From: Christopher Reimer Date: Mon, 30 Jan 2023 20:47:39 +0100 Subject: [PATCH 1/6] Rename AI_LookAt to AI_LookAtNpc AI_LookAt is actually for waypoints and not NPCs --- game/game/constants.h | 2 +- game/game/gamescript.cpp | 2 +- game/world/aiqueue.cpp | 6 +++--- game/world/aiqueue.h | 2 +- game/world/objects/npc.cpp | 16 ++++++++-------- game/world/objects/npc.h | 5 +++-- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/game/game/constants.h b/game/game/constants.h index 27739151f..39bdc6c79 100644 --- a/game/game/constants.h +++ b/game/game/constants.h @@ -289,7 +289,7 @@ enum ItmFlags : uint32_t { enum Action:uint32_t { AI_None =0, - AI_LookAt, + AI_LookAtNpc, AI_StopLookAt, AI_RemoveWeapon, AI_TurnToNpc, diff --git a/game/game/gamescript.cpp b/game/game/gamescript.cpp index 94bef2d66..eb4bf0461 100644 --- a/game/game/gamescript.cpp +++ b/game/game/gamescript.cpp @@ -2549,7 +2549,7 @@ void GameScript::ai_lookatnpc(std::shared_ptr selfRef, std::shar auto npc = findNpc(npcRef); auto self = findNpc(selfRef); if(self!=nullptr) - self->aiPush(AiQueue::aiLookAt(npc)); + self->aiPush(AiQueue::aiLookAtNpc(npc)); } void GameScript::ai_removeweapon(std::shared_ptr npcRef) { diff --git a/game/world/aiqueue.cpp b/game/world/aiqueue.cpp index afa495413..1d6f54f23 100644 --- a/game/world/aiqueue.cpp +++ b/game/world/aiqueue.cpp @@ -36,7 +36,7 @@ void AiQueue::clear() { void AiQueue::pushBack(AiAction&& a) { if(aiActions.size()>0) { - if(aiActions.back().act==AI_LookAt && a.act==AI_LookAt) { + if(aiActions.back().act==AI_LookAtNpc && a.act==AI_LookAtNpc) { aiActions.back() = a; return; } @@ -72,9 +72,9 @@ void AiQueue::onWldItemRemoved(const Item& itm) { i.item = nullptr; } -AiQueue::AiAction AiQueue::aiLookAt(Npc* other) { +AiQueue::AiAction AiQueue::aiLookAtNpc(Npc* other) { AiAction a; - a.act = AI_LookAt; + a.act = AI_LookAtNpc; a.target = other; return a; } diff --git a/game/world/aiqueue.h b/game/world/aiqueue.h index eeaa995d1..be00debfe 100644 --- a/game/world/aiqueue.h +++ b/game/world/aiqueue.h @@ -42,7 +42,7 @@ class AiQueue { void onWldItemRemoved(const Item& itm); - static AiAction aiLookAt(Npc* other); + static AiAction aiLookAtNpc(Npc* other); static AiAction aiStopLookAt(); static AiAction aiRemoveWeapon(); static AiAction aiTurnToNpc(Npc *other); diff --git a/game/world/objects/npc.cpp b/game/world/objects/npc.cpp index 70f2e6d78..2f7a684c3 100644 --- a/game/world/objects/npc.cpp +++ b/game/world/objects/npc.cpp @@ -257,7 +257,7 @@ void Npc::load(Serialize &fin, size_t id) { loadAiState(fin); fin.read(currentInteract,currentOther,currentVictum); - fin.read(currentLookAt,currentTarget,nearestEnemy); + fin.read(currentLookAtNpc,currentTarget,nearestEnemy); go2.load(fin); fin.read(currentFp,currentFpLock); @@ -677,7 +677,7 @@ Vec3 Npc::centerPosition() const { } Npc *Npc::lookAtTarget() const { - return currentLookAt; + return currentLookAtNpc; } std::string_view Npc::portalName() { @@ -1224,11 +1224,11 @@ bool Npc::implPointAt(const Tempest::Vec3& to) { return (setAnimAngGet(Npc::Anim::PointAt,comb)!=nullptr); } -bool Npc::implLookAt(uint64_t dt) { - if(currentLookAt==nullptr) +bool Npc::implLookAtNpc(uint64_t dt) { + if(currentLookAtNpc==nullptr) return false; auto selfHead = visual.mapHeadBone(); - auto otherHead = currentLookAt->visual.mapHeadBone(); + auto otherHead = currentLookAtNpc->visual.mapHeadBone(); auto dvec = otherHead - selfHead; return implLookAt(dvec.x,dvec.y,dvec.z,dt); } @@ -1988,7 +1988,7 @@ void Npc::tick(uint64_t dt) { } if(!isDown()) { - implLookAt(dt); + implLookAtNpc(dt); if(implAtack(dt)) return; @@ -2010,8 +2010,8 @@ void Npc::nextAiAction(AiQueue& queue, uint64_t dt) { auto act = queue.pop(); switch(act.act) { case AI_None: break; - case AI_LookAt:{ - currentLookAt=act.target; + case AI_LookAtNpc:{ + currentLookAtNpc=act.target; break; } case AI_TurnToNpc: { diff --git a/game/world/objects/npc.h b/game/world/objects/npc.h index b9d6dec10..2bfa621b6 100644 --- a/game/world/objects/npc.h +++ b/game/world/objects/npc.h @@ -455,7 +455,7 @@ class Npc final { gtime endTime(const Routine& r) const; bool implPointAt(const Tempest::Vec3& to); - bool implLookAt (uint64_t dt); + bool implLookAtNpc(uint64_t dt); bool implLookAt (float dx, float dy, float dz, uint64_t dt); bool implTurnTo (const Npc& oth, uint64_t dt); bool implTurnTo (const Npc& oth, bool noAnim, uint64_t dt); @@ -571,7 +571,8 @@ class Npc final { Npc* currentOther =nullptr; Npc* currentVictum =nullptr; - Npc* currentLookAt =nullptr; + WayPoint* currentLookAt=nullptr; + Npc* currentLookAtNpc=nullptr; Npc* currentTarget =nullptr; Npc* nearestEnemy =nullptr; AiOuputPipe* outputPipe =nullptr; From c5199fed545c008c8fd71c0c55a90a58e6608daa Mon Sep 17 00:00:00 2001 From: Christopher Reimer Date: Mon, 30 Jan 2023 21:10:46 +0100 Subject: [PATCH 2/6] Implemented AI_LookAt --- game/game/constants.h | 1 + game/game/gamescript.cpp | 8 ++++++++ game/game/gamescript.h | 1 + game/world/aiqueue.cpp | 7 +++++++ game/world/aiqueue.h | 1 + game/world/objects/npc.cpp | 17 ++++++++++++++++- game/world/objects/npc.h | 3 ++- 7 files changed, 36 insertions(+), 2 deletions(-) diff --git a/game/game/constants.h b/game/game/constants.h index 39bdc6c79..5be8661a7 100644 --- a/game/game/constants.h +++ b/game/game/constants.h @@ -336,6 +336,7 @@ enum Action:uint32_t { AI_PointAt, AI_StopPointAt, AI_PrintScreen, + AI_LookAt }; diff --git a/game/game/gamescript.cpp b/game/game/gamescript.cpp index eb4bf0461..a07f97266 100644 --- a/game/game/gamescript.cpp +++ b/game/game/gamescript.cpp @@ -209,6 +209,7 @@ void GameScript::initCommon() { bindExternal("ai_standupquick", &GameScript::ai_standupquick); bindExternal("ai_continueroutine", &GameScript::ai_continueroutine); bindExternal("ai_stoplookat", &GameScript::ai_stoplookat); + bindExternal("ai_lookat", &GameScript::ai_lookat); bindExternal("ai_lookatnpc", &GameScript::ai_lookatnpc); bindExternal("ai_removeweapon", &GameScript::ai_removeweapon); bindExternal("ai_turntonpc", &GameScript::ai_turntonpc); @@ -2545,6 +2546,13 @@ void GameScript::ai_stoplookat(std::shared_ptr selfRef) { self->aiPush(AiQueue::aiStopLookAt()); } +void GameScript::ai_lookat(std::shared_ptr selfRef, std::string_view waypoint) { + auto self = findNpc(selfRef); + auto to = world().findPoint(waypoint); + if(self!=nullptr) + self->aiPush(AiQueue::aiLookAt(to)); + } + void GameScript::ai_lookatnpc(std::shared_ptr selfRef, std::shared_ptr npcRef) { auto npc = findNpc(npcRef); auto self = findNpc(selfRef); diff --git a/game/game/gamescript.h b/game/game/gamescript.h index 20f5b0747..98bf0a8b7 100644 --- a/game/game/gamescript.h +++ b/game/game/gamescript.h @@ -341,6 +341,7 @@ class GameScript final { void ai_standupquick (std::shared_ptr selfRef); void ai_continueroutine (std::shared_ptr selfRef); void ai_stoplookat (std::shared_ptr selfRef); + void ai_lookat (std::shared_ptr selfRef, std::string_view waypoint); void ai_lookatnpc (std::shared_ptr selfRef, std::shared_ptr npcRef); void ai_removeweapon (std::shared_ptr npcRef); void ai_turntonpc (std::shared_ptr selfRef, std::shared_ptr npcRef); diff --git a/game/world/aiqueue.cpp b/game/world/aiqueue.cpp index 1d6f54f23..15f5ec56d 100644 --- a/game/world/aiqueue.cpp +++ b/game/world/aiqueue.cpp @@ -72,6 +72,13 @@ void AiQueue::onWldItemRemoved(const Item& itm) { i.item = nullptr; } +AiQueue::AiAction AiQueue::aiLookAt(const WayPoint* to) { + AiAction a; + a.act = AI_LookAt; + a.point = to; + return a; + } + AiQueue::AiAction AiQueue::aiLookAtNpc(Npc* other) { AiAction a; a.act = AI_LookAtNpc; diff --git a/game/world/aiqueue.h b/game/world/aiqueue.h index be00debfe..2c2b91cdd 100644 --- a/game/world/aiqueue.h +++ b/game/world/aiqueue.h @@ -42,6 +42,7 @@ class AiQueue { void onWldItemRemoved(const Item& itm); + static AiAction aiLookAt(const WayPoint* to); static AiAction aiLookAtNpc(Npc* other); static AiAction aiStopLookAt(); static AiAction aiRemoveWeapon(); diff --git a/game/world/objects/npc.cpp b/game/world/objects/npc.cpp index 2f7a684c3..76b664ec8 100644 --- a/game/world/objects/npc.cpp +++ b/game/world/objects/npc.cpp @@ -1224,6 +1224,13 @@ bool Npc::implPointAt(const Tempest::Vec3& to) { return (setAnimAngGet(Npc::Anim::PointAt,comb)!=nullptr); } +bool Npc::implLookAtWp(uint64_t dt) { + if(currentLookAt==nullptr) + return false; + auto dvec = currentLookAt->position(); + return implLookAt(dvec.x,dvec.y,dvec.z,dt); +} + bool Npc::implLookAtNpc(uint64_t dt) { if(currentLookAtNpc==nullptr) return false; @@ -1988,7 +1995,8 @@ void Npc::tick(uint64_t dt) { } if(!isDown()) { - implLookAtNpc(dt); + implLookAtNpc(dt); + implLookAtWp(dt); if(implAtack(dt)) return; @@ -2011,9 +2019,15 @@ void Npc::nextAiAction(AiQueue& queue, uint64_t dt) { switch(act.act) { case AI_None: break; case AI_LookAtNpc:{ + currentLookAt=nullptr; currentLookAtNpc=act.target; break; } + case AI_LookAt:{ + currentLookAtNpc=nullptr; + currentLookAt=act.point; + break; + } case AI_TurnToNpc: { const auto st = bodyStateMasked(); if(interactive()==nullptr && (st==BS_WALK || st==BS_SNEAK)) { @@ -2073,6 +2087,7 @@ void Npc::nextAiAction(AiQueue& queue, uint64_t dt) { break; } case AI_StopLookAt: + currentLookAtNpc=nullptr; currentLookAt=nullptr; visual.setHeadRotation(0,0); break; diff --git a/game/world/objects/npc.h b/game/world/objects/npc.h index 2bfa621b6..076998850 100644 --- a/game/world/objects/npc.h +++ b/game/world/objects/npc.h @@ -455,6 +455,7 @@ class Npc final { gtime endTime(const Routine& r) const; bool implPointAt(const Tempest::Vec3& to); + bool implLookAtWp(uint64_t dt); bool implLookAtNpc(uint64_t dt); bool implLookAt (float dx, float dy, float dz, uint64_t dt); bool implTurnTo (const Npc& oth, uint64_t dt); @@ -571,7 +572,7 @@ class Npc final { Npc* currentOther =nullptr; Npc* currentVictum =nullptr; - WayPoint* currentLookAt=nullptr; + const WayPoint* currentLookAt=nullptr; Npc* currentLookAtNpc=nullptr; Npc* currentTarget =nullptr; Npc* nearestEnemy =nullptr; From 4724d550660485d435c43acf2e1eb4afeed3e73a Mon Sep 17 00:00:00 2001 From: Christopher Reimer Date: Thu, 2 Feb 2023 20:06:40 +0100 Subject: [PATCH 3/6] Fix Rename --- game/world/objects/npc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game/world/objects/npc.cpp b/game/world/objects/npc.cpp index 76b664ec8..aeb1bd0e6 100644 --- a/game/world/objects/npc.cpp +++ b/game/world/objects/npc.cpp @@ -201,7 +201,7 @@ void Npc::save(Serialize &fout, size_t id) { saveAiState(fout); fout.write(currentInteract,currentOther,currentVictum); - fout.write(currentLookAt,currentTarget,nearestEnemy); + fout.write(currentLookAtNpc,currentTarget,nearestEnemy); go2.save(fout); fout.write(currentFp,currentFpLock); From 756120631d54ed5606b676a6e005a19b55576af7 Mon Sep 17 00:00:00 2001 From: Christopher Reimer Date: Thu, 2 Feb 2023 20:14:17 +0100 Subject: [PATCH 4/6] Fix Serialization --- game/game/serialize.h | 2 +- game/world/objects/npc.cpp | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/game/game/serialize.h b/game/game/serialize.h index 2c892680a..e1a9a7d15 100644 --- a/game/game/serialize.h +++ b/game/game/serialize.h @@ -33,7 +33,7 @@ class SaveGameHeader; class Serialize { public: enum Version : uint16_t { - Current = 41 + Current = 42 }; Serialize(Tempest::ODevice& fout); Serialize(Tempest::IDevice& fin); diff --git a/game/world/objects/npc.cpp b/game/world/objects/npc.cpp index aeb1bd0e6..fb6e5eaf3 100644 --- a/game/world/objects/npc.cpp +++ b/game/world/objects/npc.cpp @@ -202,6 +202,7 @@ void Npc::save(Serialize &fout, size_t id) { fout.write(currentInteract,currentOther,currentVictum); fout.write(currentLookAtNpc,currentTarget,nearestEnemy); + fout.write(currentLookAt); go2.save(fout); fout.write(currentFp,currentFpLock); @@ -258,6 +259,9 @@ void Npc::load(Serialize &fin, size_t id) { fin.read(currentInteract,currentOther,currentVictum); fin.read(currentLookAtNpc,currentTarget,nearestEnemy); + if (fin.version()>42) { + fin.read(currentLookAt); + } go2.load(fin); fin.read(currentFp,currentFpLock); @@ -1995,8 +1999,8 @@ void Npc::tick(uint64_t dt) { } if(!isDown()) { - implLookAtNpc(dt); - implLookAtWp(dt); + implLookAtNpc(dt); + implLookAtWp(dt); if(implAtack(dt)) return; From 7a9e92b1517608115bfa00e445dc0ede2360bbca Mon Sep 17 00:00:00 2001 From: Try Date: Fri, 3 Feb 2023 16:16:54 +0300 Subject: [PATCH 5/6] Update npc.cpp --- game/world/objects/npc.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/game/world/objects/npc.cpp b/game/world/objects/npc.cpp index fb6e5eaf3..a219a87ea 100644 --- a/game/world/objects/npc.cpp +++ b/game/world/objects/npc.cpp @@ -201,8 +201,7 @@ void Npc::save(Serialize &fout, size_t id) { saveAiState(fout); fout.write(currentInteract,currentOther,currentVictum); - fout.write(currentLookAtNpc,currentTarget,nearestEnemy); - fout.write(currentLookAt); + fout.write(currentLookAt,currentLookAtNpc,currentTarget,nearestEnemy); go2.save(fout); fout.write(currentFp,currentFpLock); @@ -258,10 +257,9 @@ void Npc::load(Serialize &fin, size_t id) { loadAiState(fin); fin.read(currentInteract,currentOther,currentVictum); - fin.read(currentLookAtNpc,currentTarget,nearestEnemy); - if (fin.version()>42) { + if(fin.version()>42) fin.read(currentLookAt); - } + fin.read(currentLookAtNpc,currentTarget,nearestEnemy); go2.load(fin); fin.read(currentFp,currentFpLock); @@ -1233,7 +1231,7 @@ bool Npc::implLookAtWp(uint64_t dt) { return false; auto dvec = currentLookAt->position(); return implLookAt(dvec.x,dvec.y,dvec.z,dt); -} + } bool Npc::implLookAtNpc(uint64_t dt) { if(currentLookAtNpc==nullptr) From 17b299b30451936e8a831d15657012c6ef96492a Mon Sep 17 00:00:00 2001 From: Try Date: Fri, 3 Feb 2023 16:18:36 +0300 Subject: [PATCH 6/6] Update npc.cpp --- game/world/objects/npc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game/world/objects/npc.cpp b/game/world/objects/npc.cpp index a219a87ea..e7fb8e2db 100644 --- a/game/world/objects/npc.cpp +++ b/game/world/objects/npc.cpp @@ -257,7 +257,7 @@ void Npc::load(Serialize &fin, size_t id) { loadAiState(fin); fin.read(currentInteract,currentOther,currentVictum); - if(fin.version()>42) + if(fin.version()>=42) fin.read(currentLookAt); fin.read(currentLookAtNpc,currentTarget,nearestEnemy);