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

Implemented AI_LOOKAT #397

Merged
merged 6 commits into from
Feb 3, 2023
Merged
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
3 changes: 2 additions & 1 deletion game/game/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -336,6 +336,7 @@ enum Action:uint32_t {
AI_PointAt,
AI_StopPointAt,
AI_PrintScreen,
AI_LookAt
};


Expand Down
10 changes: 9 additions & 1 deletion game/game/gamescript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -2545,11 +2546,18 @@ void GameScript::ai_stoplookat(std::shared_ptr<phoenix::c_npc> selfRef) {
self->aiPush(AiQueue::aiStopLookAt());
}

void GameScript::ai_lookat(std::shared_ptr<phoenix::c_npc> 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<phoenix::c_npc> selfRef, std::shared_ptr<phoenix::c_npc> npcRef) {
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<phoenix::c_npc> npcRef) {
Expand Down
1 change: 1 addition & 0 deletions game/game/gamescript.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ class GameScript final {
void ai_standupquick (std::shared_ptr<phoenix::c_npc> selfRef);
void ai_continueroutine (std::shared_ptr<phoenix::c_npc> selfRef);
void ai_stoplookat (std::shared_ptr<phoenix::c_npc> selfRef);
void ai_lookat (std::shared_ptr<phoenix::c_npc> selfRef, std::string_view waypoint);
void ai_lookatnpc (std::shared_ptr<phoenix::c_npc> selfRef, std::shared_ptr<phoenix::c_npc> npcRef);
void ai_removeweapon (std::shared_ptr<phoenix::c_npc> npcRef);
void ai_turntonpc (std::shared_ptr<phoenix::c_npc> selfRef, std::shared_ptr<phoenix::c_npc> npcRef);
Expand Down
2 changes: 1 addition & 1 deletion game/game/serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 9 additions & 2 deletions game/world/aiqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -72,9 +72,16 @@ void AiQueue::onWldItemRemoved(const Item& itm) {
i.item = nullptr;
}

AiQueue::AiAction AiQueue::aiLookAt(Npc* other) {
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;
a.target = other;
return a;
}
Expand Down
3 changes: 2 additions & 1 deletion game/world/aiqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class AiQueue {

void onWldItemRemoved(const Item& itm);

static AiAction aiLookAt(Npc* other);
static AiAction aiLookAt(const WayPoint* to);
static AiAction aiLookAtNpc(Npc* other);
static AiAction aiStopLookAt();
static AiAction aiRemoveWeapon();
static AiAction aiTurnToNpc(Npc *other);
Expand Down
31 changes: 24 additions & 7 deletions game/world/objects/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(currentLookAt,currentLookAtNpc,currentTarget,nearestEnemy);

go2.save(fout);
fout.write(currentFp,currentFpLock);
Expand Down Expand Up @@ -257,7 +257,9 @@ void Npc::load(Serialize &fin, size_t id) {
loadAiState(fin);

fin.read(currentInteract,currentOther,currentVictum);
fin.read(currentLookAt,currentTarget,nearestEnemy);
if(fin.version()>=42)
fin.read(currentLookAt);
fin.read(currentLookAtNpc,currentTarget,nearestEnemy);
Copy link
Owner

Choose a reason for hiding this comment

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

  1. write code wasn't updated to mirror read
  2. Waypoint currentLookAt is not serialized

For proper serialization, you need to bump save file version in Serialize::Version and add check for fin.version() before reading new data.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not exactly sure how the serialization works. But I think it's fixed now


go2.load(fin);
fin.read(currentFp,currentFpLock);
Expand Down Expand Up @@ -677,7 +679,7 @@ Vec3 Npc::centerPosition() const {
}

Npc *Npc::lookAtTarget() const {
return currentLookAt;
return currentLookAtNpc;
}

std::string_view Npc::portalName() {
Expand Down Expand Up @@ -1224,11 +1226,18 @@ bool Npc::implPointAt(const Tempest::Vec3& to) {
return (setAnimAngGet(Npc::Anim::PointAt,comb)!=nullptr);
}

bool Npc::implLookAt(uint64_t dt) {
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;
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);
}
Expand Down Expand Up @@ -1988,7 +1997,8 @@ void Npc::tick(uint64_t dt) {
}

if(!isDown()) {
implLookAt(dt);
implLookAtNpc(dt);
implLookAtWp(dt);

if(implAtack(dt))
return;
Expand All @@ -2010,8 +2020,14 @@ void Npc::nextAiAction(AiQueue& queue, uint64_t dt) {
auto act = queue.pop();
switch(act.act) {
case AI_None: break;
case AI_LookAtNpc:{
currentLookAt=nullptr;
currentLookAtNpc=act.target;
break;
}
case AI_LookAt:{
currentLookAt=act.target;
currentLookAtNpc=nullptr;
currentLookAt=act.point;
break;
}
case AI_TurnToNpc: {
Expand Down Expand Up @@ -2073,6 +2089,7 @@ void Npc::nextAiAction(AiQueue& queue, uint64_t dt) {
break;
}
case AI_StopLookAt:
currentLookAtNpc=nullptr;
currentLookAt=nullptr;
visual.setHeadRotation(0,0);
break;
Expand Down
6 changes: 4 additions & 2 deletions game/world/objects/npc.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,8 @@ class Npc final {
gtime endTime(const Routine& r) const;

bool implPointAt(const Tempest::Vec3& to);
bool implLookAt (uint64_t dt);
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);
bool implTurnTo (const Npc& oth, bool noAnim, uint64_t dt);
Expand Down Expand Up @@ -571,7 +572,8 @@ class Npc final {
Npc* currentOther =nullptr;
Npc* currentVictum =nullptr;

Npc* currentLookAt =nullptr;
const WayPoint* currentLookAt=nullptr;
Npc* currentLookAtNpc=nullptr;
Npc* currentTarget =nullptr;
Npc* nearestEnemy =nullptr;
AiOuputPipe* outputPipe =nullptr;
Expand Down