Skip to content

Commit

Permalink
Improve appear and remove map spectator
Browse files Browse the repository at this point in the history
  • Loading branch information
ramon-bernardo committed Nov 5, 2024
1 parent 8e9f360 commit 76ded38
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 124 deletions.
26 changes: 15 additions & 11 deletions src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,18 +426,21 @@ void Creature::onRemoveTileItem(const Tile* tile, const Position& pos, const Ite
}
}

void Creature::onCreatureAppear(Creature* creature, bool isLogin)
void Creature::onAppear(bool isLogin)
{
if (creature == this) {
if (useCacheMap()) {
isMapLoaded = true;
updateMapCache();
}
if (useCacheMap()) {
isMapLoaded = true;
updateMapCache();
}

if (isLogin) {
setLastPosition(getPosition());
}
} else if (isMapLoaded) {
if (isLogin) {
setLastPosition(getPosition());
}
}

void Creature::onCreatureAppear(Creature* creature, bool isLogin)
{
if (isMapLoaded) {
if (creature->getPosition().z == getPosition().z) {
updateTileCache(creature->getTile(), creature->getPosition());
}
Expand All @@ -447,7 +450,8 @@ void Creature::onCreatureAppear(Creature* creature, bool isLogin)
void Creature::onRemoveCreature(Creature* creature, bool)
{
onCreatureDisappear(creature, true);
if (creature != this && isMapLoaded) {

if (isMapLoaded) {
if (creature->getPosition().z == getPosition().z) {
updateTileCache(creature->getTile(), creature->getPosition());
}
Expand Down
3 changes: 3 additions & 0 deletions src/creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,11 @@ class Creature : virtual public Thing
const Item* newItem, const ItemType& newType);
virtual void onRemoveTileItem(const Tile* tile, const Position& pos, const ItemType& iType, const Item* item);

virtual void onAppear(bool isLogin);
virtual void onCreatureAppear(Creature* creature, bool isLogin);
virtual void onRemove(bool isLogout) {}

Check failure on line 292 in src/creature.h

View workflow job for this annotation

GitHub Actions / test

unused parameter ‘isLogout’ [-Werror=unused-parameter]

Check failure on line 292 in src/creature.h

View workflow job for this annotation

GitHub Actions / test

unused parameter ‘isLogout’ [-Werror=unused-parameter]

Check failure on line 292 in src/creature.h

View workflow job for this annotation

GitHub Actions / test

unused parameter ‘isLogout’ [-Werror=unused-parameter]

Check failure on line 292 in src/creature.h

View workflow job for this annotation

GitHub Actions / test

unused parameter ‘isLogout’ [-Werror=unused-parameter]

Check failure on line 292 in src/creature.h

View workflow job for this annotation

GitHub Actions / test

unused parameter ‘isLogout’ [-Werror=unused-parameter]
virtual void onRemoveCreature(Creature* creature, bool isLogout);

virtual void onCreatureMove(Creature* creature, const Tile* newTile, const Position& newPos, const Tile* oldTile,
const Position& oldPos, bool teleport);

Expand Down
12 changes: 10 additions & 2 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,11 @@ bool Game::placeCreature(Creature* creature, const Position& pos, bool extendedP
}

for (Creature* spectator : spectators) {
spectator->onCreatureAppear(creature, true);
if (spectator == creature) {
spectator->onAppear(true);
} else {
spectator->onCreatureAppear(creature, true);
}
}

creature->getParent()->postAddNotification(creature, nullptr, 0);
Expand Down Expand Up @@ -568,7 +572,11 @@ bool Game::removeCreature(Creature* creature, bool isLogout /* = true*/)

// event method
for (Creature* spectator : spectators) {
spectator->onRemoveCreature(creature, isLogout);
if (spectator == creature) {
spectator->onRemove(isLogout);
} else {
spectator->onRemoveCreature(creature, isLogout);
}
}

Creature* master = creature->getMaster();
Expand Down
84 changes: 67 additions & 17 deletions src/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,38 @@ bool Monster::canWalkOnFieldType(CombatType_t combatType) const

void Monster::onAttackedCreatureDisappear(bool) { attackTicks = 0; }

void Monster::onAppear(bool isLogin)
{
Creature::onAppear(isLogin);

if (mType->info.creatureAppearEvent != -1) {
// onCreatureAppear(self, creature)
LuaScriptInterface* scriptInterface = mType->info.scriptInterface;
if (!tfs::lua::reserveScriptEnv()) {
std::cout << "[Error - Monster::onCreatureAppear] Call stack overflow" << std::endl;
return;
}

ScriptEnvironment* env = tfs::lua::getScriptEnv();
env->setScriptId(mType->info.creatureAppearEvent, scriptInterface);

lua_State* L = scriptInterface->getLuaState();
scriptInterface->pushFunction(mType->info.creatureAppearEvent);

tfs::lua::pushUserdata(L, this);
tfs::lua::setMetatable(L, -1, "Monster");

tfs::lua::pushUserdata(L, this);
tfs::lua::setCreatureMetatable(L, -1, this);

if (scriptInterface->callFunction(2)) {
return;
}
}

onCreatureEnter(this);
}

void Monster::onCreatureAppear(Creature* creature, bool isLogin)
{
Creature::onCreatureAppear(creature, isLogin);
Expand Down Expand Up @@ -138,17 +170,43 @@ void Monster::onCreatureAppear(Creature* creature, bool isLogin)
}
}

if (creature == this) {
// We just spawned lets look around to see who is there.
if (isSummon()) {
isMasterInRange = canSee(getMaster()->getPosition());
onCreatureEnter(creature);
}

void Monster::onRemove(bool isLogout)
{
Creature::onRemove(isLogout);

if (mType->info.creatureDisappearEvent != -1) {
// onCreatureDisappear(self, creature)
LuaScriptInterface* scriptInterface = mType->info.scriptInterface;
if (!tfs::lua::reserveScriptEnv()) {
std::cout << "[Error - Monster::onCreatureDisappear] Call stack overflow" << std::endl;
return;
}

updateTargetList();
updateIdleStatus();
} else {
onCreatureEnter(creature);
ScriptEnvironment* env = tfs::lua::getScriptEnv();
env->setScriptId(mType->info.creatureDisappearEvent, scriptInterface);

lua_State* L = scriptInterface->getLuaState();
scriptInterface->pushFunction(mType->info.creatureDisappearEvent);

tfs::lua::pushUserdata(L, this);
tfs::lua::setMetatable(L, -1, "Monster");

tfs::lua::pushUserdata(L, this);
tfs::lua::setCreatureMetatable(L, -1, this);

if (scriptInterface->callFunction(2)) {
return;
}
}

if (spawn) {
spawn->startSpawnCheck();
}

setIdle(true);
}

void Monster::onRemoveCreature(Creature* creature, bool isLogout)
Expand Down Expand Up @@ -180,15 +238,7 @@ void Monster::onRemoveCreature(Creature* creature, bool isLogout)
}
}

if (creature == this) {
if (spawn) {
spawn->startSpawnCheck();
}

setIdle(true);
} else {
onCreatureLeave(creature);
}
onCreatureLeave(creature);
}

void Monster::onCreatureMove(Creature* creature, const Tile* newTile, const Position& newPos, const Tile* oldTile,
Expand Down
2 changes: 2 additions & 0 deletions src/monster.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ class Monster final : public Creature

void onAttackedCreatureDisappear(bool isLogout) override;

void onAppear(bool isLogin) override;
void onCreatureAppear(Creature* creature, bool isLogin) override;
void onRemove(bool isLogout) override;
void onRemoveCreature(Creature* creature, bool isLogout) override;
void onCreatureMove(Creature* creature, const Tile* newTile, const Position& newPos, const Tile* oldTile,
const Position& oldPos, bool teleport) override;
Expand Down
43 changes: 28 additions & 15 deletions src/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,25 +410,32 @@ void Npc::loadNpcTypeInfo()
sightY = npcType->sightY;
}

void Npc::onAppear(bool isLogin)
{
Creature::onAppear(isLogin);

if (walkTicks > 0) {
addEventWalk();
}

if (npcEventHandler) {
npcEventHandler->onCreatureAppear(this);
}
}

void Npc::onCreatureAppear(Creature* creature, bool isLogin)
{
Creature::onCreatureAppear(creature, isLogin);

if (creature == this) {
if (walkTicks > 0) {
addEventWalk();
}

if (npcEventHandler) {
npcEventHandler->onCreatureAppear(creature);
}
} else if (creature->getPlayer()) {
if (creature->getPlayer()) {
if (npcEventHandler) {
npcEventHandler->onCreatureAppear(creature);
}
}
}

void Npc::onCreatureAppear(Creature* creature, bool isLogin) {}

Check failure on line 437 in src/npc.cpp

View workflow job for this annotation

GitHub Actions / test

redefinition of ‘void Npc::onCreatureAppear(Creature*, bool)’

bool NpcType::loadCallback(NpcScriptInterface* scriptInterface)
{
int32_t id = scriptInterface->getEvent();
Expand Down Expand Up @@ -459,16 +466,22 @@ bool NpcType::loadCallback(NpcScriptInterface* scriptInterface)
return true;
}

void Npc::onRemove(bool isLogout)
{
Creature::onRemove(isLogout);

closeAllShopWindows();

if (npcEventHandler) {
npcEventHandler->onCreatureDisappear(this);
}
}

void Npc::onRemoveCreature(Creature* creature, bool isLogout)
{
Creature::onRemoveCreature(creature, isLogout);

if (creature == this) {
closeAllShopWindows();
if (npcEventHandler) {
npcEventHandler->onCreatureDisappear(creature);
}
} else if (creature->getPlayer()) {
if (creature->getPlayer()) {
if (npcEventHandler) {
npcEventHandler->onCreatureDisappear(creature);
}
Expand Down
2 changes: 2 additions & 0 deletions src/npc.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ class Npc final : public Creature
std::map<std::string, std::string> parameters;

private:
void onAppear(bool isLogin) override;
void onCreatureAppear(Creature* creature, bool isLogin) override;
void onRemove(bool isLogout) override;
void onRemoveCreature(Creature* creature, bool isLogout) override;
void onCreatureMove(Creature* creature, const Tile* newTile, const Position& newPos, const Tile* oldTile,
const Position& oldPos, bool teleport) override;
Expand Down
Loading

0 comments on commit 76ded38

Please sign in to comment.