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

Fixing summon searching player target and icons update #395

Merged
merged 2 commits into from
May 27, 2022
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
9 changes: 2 additions & 7 deletions data/lib/core/functions/creature.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,13 @@ function Creature:setItemOutfit(item, time)
return true
end

function Creature:addSummon(monster)
function Creature:setSummon(monster)
local summon = Monster(monster)
if not summon then
return false
end

summon:setTarget(nil)
summon:setFollowCreature(nil)
summon:setDropLoot(false)
summon:setSkillLoss(false)
summon:setMaster(self)

summon:setMaster(self, true)
return true
end

Expand Down
2 changes: 1 addition & 1 deletion data/scripts/actions/other/music.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function music.onUse(player, item, fromPosition, target, toPosition, isHotkey)
if instrument.monster and chance then
local monster = Game.createMonster(instrument.monster, player:getPosition(), true)
if monster then
player:addSummon(monster)
player:setSummon(monster)
end
elseif instrument.itemId and chance then
player:addItem(instrument.itemId, instrument.itemCount)
Expand Down
2 changes: 1 addition & 1 deletion data/scripts/runes/animate_dead_rune.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function rune.onCastSpell(player, variant)
local summon = Game.createMonster("Skeleton", position, true, true)
if summon then
corpse:remove()
player:addSummon(summon)
player:setSummon(summon)
position:sendMagicEffect(CONST_ME_MAGIC_BLUE)
return true
end
Expand Down
2 changes: 1 addition & 1 deletion data/scripts/runes/convince_creature.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function rune.onCastSpell(creature, variant, isHotkey)

creature:addMana(-manaCost)
creature:addManaSpent(manaCost)
creature:addSummon(target)
creature:setSummon(target)
creature:getPosition():sendMagicEffect(CONST_ME_MAGIC_BLUE)
return true
end
Expand Down
11 changes: 9 additions & 2 deletions src/creatures/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ BlockType_t Creature::blockHit(Creature* attacker, CombatType_t combatType, int3
bool Creature::setAttackedCreature(Creature* creature)
{
if (creature) {
if (this->getMonster() && this->getMonster()->isFamiliar() && this->getTile()->hasFlag(TILESTATE_PROTECTIONZONE)) {
if (this->getMonster() && this->getMonster()->isFamiliar() && this->getTile() && this->getTile()->hasFlag(TILESTATE_PROTECTIONZONE)) {
return false;
}

Expand Down Expand Up @@ -1193,14 +1193,21 @@ void Creature::onGainExperience(uint64_t gainExp, Creature* target)
}
}

bool Creature::setMaster(Creature* newMaster) {
bool Creature::setMaster(Creature* newMaster, bool reloadCreature/* = false*/) {
// Persists if this creature has ever been a summon
this->summoned = true;

if (!newMaster && !master) {
return false;
}

// Reloading summon icon/knownCreature and reset informations (follow/dropLoot/skillLoss)
if (reloadCreature) {
setFollowCreature(nullptr);
setDropLoot(false);
setSkillLoss(false);
g_game().reloadCreature(this);
}
if (newMaster) {
incrementReferenceCounter();
newMaster->summons.push_back(this);
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class Creature : virtual public Thing
virtual BlockType_t blockHit(Creature* attacker, CombatType_t combatType, int32_t& damage,
bool checkDefense = false, bool checkArmor = false, bool field = false);

bool setMaster(Creature* newMaster);
bool setMaster(Creature* newMaster, bool reloadCreature = false);

void removeMaster() {
if (master) {
Expand Down
8 changes: 3 additions & 5 deletions src/creatures/monsters/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ bool Monster::isTarget(const Creature* creature) const
return false;
}
Faction_t targetFaction = creature->getFaction();
if (getFaction() != FACTION_DEFAULT) {
if (getFaction() != FACTION_DEFAULT && master && master->getMonster()) {
return isEnemyFaction(targetFaction);
}
return true;
Expand All @@ -733,7 +733,7 @@ bool Monster::selectTarget(Creature* creature)
}

if (isHostile() || isSummon()) {
if (setAttackedCreature(creature) && !isSummon()) {
if (setAttackedCreature(creature)) {
g_dispatcher().addTask(createTask(std::bind(&Game::checkCreatureAttack, &g_game(), getID())));
}
}
Expand Down Expand Up @@ -1091,9 +1091,7 @@ void Monster::onThinkDefense(uint32_t interval)
Monster* summon = Monster::createMonster(summonBlock.name);
if (summon) {
if (g_game().placeCreature(summon, getPosition(), false, summonBlock.force)) {
summon->setDropLoot(false);
summon->setSkillLoss(false);
summon->setMaster(this);
summon->setMaster(this, true);
g_game().addMagicEffect(getPosition(), CONST_ME_MAGIC_BLUE);
g_game().addMagicEffect(summon->getPosition(), CONST_ME_TELEPORT);
} else {
Expand Down
13 changes: 13 additions & 0 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5521,6 +5521,19 @@ void Game::updateCreatureIcon(const Creature* creature)
}
}

void Game::reloadCreature(const Creature* creature)
{
SpectatorHashSet spectators;
map.getSpectators(spectators, creature->getPosition(), false, true);
for (Creature* spectator : spectators) {
Player* tmpPlayer = spectator->getPlayer();
if (!tmpPlayer) {
continue;
}
tmpPlayer->reloadCreature(creature);
}
}

bool Game::combatBlockHit(CombatDamage& damage, Creature* attacker, Creature* target, bool checkDefense, bool checkArmor, bool field)
{
if (damage.primary.type == COMBAT_NONE && damage.secondary.type == COMBAT_NONE) {
Expand Down
1 change: 1 addition & 0 deletions src/game/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ class Game
void internalCreatureChangeVisible(Creature* creature, bool visible);
void changeLight(const Creature* creature);
void updateCreatureIcon(const Creature* creature);
void reloadCreature(const Creature* creature);
void updateCreatureSkull(const Creature* player);
void updatePlayerShield(Player* player);
void updateCreatureType(Creature* creature);
Expand Down
4 changes: 1 addition & 3 deletions src/lua/functions/core/game/game_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,7 @@ int GameFunctions::luaGameCreateMonster(lua_State* L) {
if (lua_gettop(L) >= 5) {
Creature* master = getCreature(L, 5);
if (master) {
monster->setMaster(master);
monster->setDropLoot(false);
monster->setSkillLoss(false);
monster->setMaster(master, true);
isSummon = true;
}
}
Expand Down
19 changes: 17 additions & 2 deletions src/lua/functions/creatures/creature_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,20 @@ int CreatureFunctions::luaCreatureGetMaster(lua_State* L) {
return 1;
}

int CreatureFunctions::luaCreatureReload(lua_State* L)
{
// creature:reload()
Creature* creature = getUserdata<Creature>(L, 1);
if (!creature) {
lua_pushnil(L);
return 1;
}

g_game().reloadCreature(creature);
pushBoolean(L, true);
return 1;
}

int CreatureFunctions::luaCreatureSetMaster(lua_State* L) {
// creature:setMaster(master)
Creature* creature = getUserdata<Creature>(L, 1);
Expand All @@ -286,8 +300,9 @@ int CreatureFunctions::luaCreatureSetMaster(lua_State* L) {
return 1;
}

pushBoolean(L, creature->setMaster(getCreature(L, 2)));
g_game().updateCreatureType(creature);
pushBoolean(L, creature->setMaster(getCreature(L, 2), true));
// Reloading creature icon/knownCreature
CreatureFunctions::luaCreatureReload(L);
return 1;
}

Expand Down
3 changes: 3 additions & 0 deletions src/lua/functions/creatures/creature_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class CreatureFunctions final : LuaScriptInterface {
registerMethod(L, "Creature", "setTarget", CreatureFunctions::luaCreatureSetTarget);
registerMethod(L, "Creature", "getFollowCreature", CreatureFunctions::luaCreatureGetFollowCreature);
registerMethod(L, "Creature", "setFollowCreature", CreatureFunctions::luaCreatureSetFollowCreature);
registerMethod(L, "Creature", "reload", CreatureFunctions::luaCreatureReload);
registerMethod(L, "Creature", "getMaster", CreatureFunctions::luaCreatureGetMaster);
registerMethod(L, "Creature", "setMaster", CreatureFunctions::luaCreatureSetMaster);
registerMethod(L, "Creature", "getLight", CreatureFunctions::luaCreatureGetLight);
Expand Down Expand Up @@ -123,6 +124,8 @@ class CreatureFunctions final : LuaScriptInterface {
static int luaCreatureGetFollowCreature(lua_State* L);
static int luaCreatureSetFollowCreature(lua_State* L);

static int luaCreatureReload(lua_State* L);

static int luaCreatureGetMaster(lua_State* L);
static int luaCreatureSetMaster(lua_State* L);

Expand Down