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

feat: monster rename functionality #2621

Merged
merged 37 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
176f258
Update monster_functions.hpp
luanluciano93 May 9, 2024
8bc3703
Update monster_functions.cpp
luanluciano93 May 9, 2024
81f3329
Update monster.cpp
luanluciano93 May 9, 2024
356688e
Code format - (Clang-format)
github-actions[bot] May 9, 2024
f63fe41
Update monster.hpp
luanluciano93 May 9, 2024
033ddb3
Code format - (Clang-format)
github-actions[bot] May 9, 2024
9f35153
Update player.hpp
luanluciano93 May 9, 2024
94c153c
Code format - (Clang-format)
github-actions[bot] May 9, 2024
99a63d0
Update protocolgame.cpp
luanluciano93 May 9, 2024
f90c334
Update protocolgame.hpp
luanluciano93 May 9, 2024
6543ffa
Code format - (Clang-format)
github-actions[bot] May 9, 2024
07c9519
Update monster_functions.cpp
luanluciano93 May 9, 2024
ef286b8
Update player.hpp
luanluciano93 May 9, 2024
d664952
Update monster.cpp
luanluciano93 May 9, 2024
259e3c3
Update monster.cpp
luanluciano93 May 9, 2024
b6e368f
Update monster.cpp
luanluciano93 May 9, 2024
3367611
Update monster.cpp
luanluciano93 May 9, 2024
d6a9047
Update src/creatures/monsters/monster.cpp
luanluciano93 May 10, 2024
aed92ec
Update src/creatures/monsters/monster.cpp
luanluciano93 May 10, 2024
e8f4611
Update src/creatures/monsters/monster.cpp
luanluciano93 May 10, 2024
ff10501
Update monster_functions.hpp
luanluciano93 May 10, 2024
99c97e1
Update monster_functions.cpp
luanluciano93 May 10, 2024
d789b7a
Update protocolgame.cpp
luanluciano93 May 10, 2024
0acbef1
Merge branch 'main' into monster-set-name
luanluciano93 May 10, 2024
a193745
Update monster.cpp
luanluciano93 May 11, 2024
11be6f7
Update monster.cpp
luanluciano93 May 11, 2024
d48ef96
Update monster.cpp
luanluciano93 May 11, 2024
eea191c
Update protocolgame.cpp
luanluciano93 May 11, 2024
eb8a664
Merge branch 'main' into monster-set-name
elsongabriel May 12, 2024
31aaec7
Merge branch 'main' into monster-set-name
elsongabriel May 12, 2024
be6475b
Merge branch 'main' into monster-set-name
luanluciano93 May 14, 2024
2d9ff5f
Merge branch 'main' into monster-set-name
luanluciano93 May 16, 2024
f68a545
Merge branch 'main' into monster-set-name
luanluciano93 Jun 7, 2024
9b5cfd6
Merge branch 'main' into monster-set-name
elsongabriel Jun 11, 2024
e9b1ffc
added setmonstername talkaction and changed create_monster to manage_…
elsongabriel Jun 11, 2024
011b2d7
Lua code format - (Stylua)
github-actions[bot] Jun 11, 2024
9f65de6
Merge branch 'main' into monster-set-name
elsongabriel Jun 11, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
-- @param param: String containing the command parameters.
-- Format: "/m monstername, monstercount, [fiendish/influenced level], spawnRadius, [forceCreate]"
-- Example: "/m rat, 10, fiendish, 5, true"
-- @param: the last param is by default "false", if add "," or any value i'ts set to true
-- @param: the last param is by default "false", if add "," or any value it's set to true
-- @return true if the command is executed successfully, false otherwise.
function createMonster.onSay(player, words, param)
-- create log
Expand Down Expand Up @@ -127,3 +127,33 @@
createMonster:separator(" ")
createMonster:groupType("god")
createMonster:register()

----------------- Rename monster name -----------------
local setMonsterName = TalkAction("/setmonstername")

-- @function setMonsterName.onSay
-- @desc TalkAction to rename nearby monsters within a radius of 4 sqm.
-- Format: "/setmonstername newName"
function setMonsterName.onSay(player, words, param)
if param == "" then
player:sendCancelMessage("Command param required.")
return true
end

local split = param:split(",")
local monsterNewName = split[1]

local spectators, spectator = Game.getSpectators(player:getPosition(), false, false, 4, 4, 4, 4)

Check warning on line 146 in data/scripts/talkactions/god/manage_monster.lua

View workflow job for this annotation

GitHub Actions / luacheck

[luacheck] reported by reviewdog 🐶 value assigned to variable 'spectator' is unused Raw Output: data/scripts/talkactions/god/manage_monster.lua:146:20: value assigned to variable 'spectator' is unused
for i = 1, #spectators do
spectator = spectators[i]
if spectator:isMonster() then
spectator:setName(monsterNewName)
end
end

return true
end

setMonsterName:separator(" ")
setMonsterName:groupType("god")
setMonsterName:register()
33 changes: 32 additions & 1 deletion src/creatures/monsters/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ std::shared_ptr<Monster> Monster::createMonster(const std::string &name) {

Monster::Monster(const std::shared_ptr<MonsterType> mType) :
Creature(),
strDescription(asLowerCaseString(mType->nameDescription)),
nameDescription(asLowerCaseString(mType->nameDescription)),
mType(mType) {
defaultOutfit = mType->info.outfit;
currentOutfit = mType->info.outfit;
Expand Down Expand Up @@ -64,6 +64,37 @@ void Monster::removeList() {
g_game().removeMonster(static_self_cast<Monster>());
}

const std::string &Monster::getName() const {
if (name.empty()) {
return mType->name;
}
return name;
}

void Monster::setName(const std::string &name) {
if (getName() == name) {
return;
}

this->name = name;

// NOTE: Due to how client caches known creatures,
// it is not feasible to send creature update to everyone that has ever met it
auto spectators = Spectators().find<Player>(position, true);
luanluciano93 marked this conversation as resolved.
Show resolved Hide resolved
for (const auto &spectator : spectators) {
if (const auto &tmpPlayer = spectator->getPlayer()) {
tmpPlayer->sendUpdateTileCreature(static_self_cast<Monster>());
}
}
}

const std::string &Monster::getNameDescription() const {
if (nameDescription.empty()) {
return mType->nameDescription;
}
return nameDescription;
}

bool Monster::canWalkOnFieldType(CombatType_t combatType) const {
switch (combatType) {
case COMBAT_ENERGYDAMAGE:
Expand Down
20 changes: 11 additions & 9 deletions src/creatures/monsters/monster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,22 @@ class Monster final : public Creature {
}
}

void removeList() override;
void addList() override;
void removeList() override;

const std::string &getName() const override;
void setName(const std::string &name);

const std::string &getName() const override {
return mType->name;
}
// Real monster name, set on monster creation "createMonsterType(typeName)"
const std::string &getTypeName() const override {
return mType->typeName;
}
const std::string &getNameDescription() const override {
return mType->nameDescription;
}
const std::string &getNameDescription() const override;
void setNameDescription(const std::string &nameDescription) {
this->nameDescription = nameDescription;
};
std::string getDescription(int32_t) override {
return strDescription + '.';
return nameDescription + '.';
}

CreatureType_t getType() const override {
Expand Down Expand Up @@ -363,7 +364,8 @@ class Monster final : public Creature {
uint16_t forgeStack = 0;
ForgeClassifications_t monsterForgeClassification = ForgeClassifications_t::FORGE_NORMAL_MONSTER;

std::string strDescription;
std::string name;
std::string nameDescription;

std::shared_ptr<MonsterType> mType;
SpawnMonster* spawnMonster = nullptr;
Expand Down
9 changes: 7 additions & 2 deletions src/creatures/players/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ class Player final : public Creature, public Cylinder, public Bankable {
const std::string &getName() const override {
return name;
}
void setName(std::string newName) {
this->name = std::move(newName);
void setName(const std::string &name) {
this->name = name;
}
const std::string &getTypeName() const override {
return name;
Expand Down Expand Up @@ -1070,6 +1070,11 @@ class Player final : public Creature, public Cylinder, public Bankable {
client->sendRemoveTileThing(pos, stackpos);
}
}
void sendUpdateTileCreature(const std::shared_ptr<Creature> creature) {
if (client) {
client->sendUpdateTileCreature(creature->getPosition(), creature->getTile()->getClientIndexOfCreature(static_self_cast<Player>(), creature), creature);
}
}
void sendUpdateTile(std::shared_ptr<Tile> updateTile, const Position &pos) {
if (client) {
client->sendUpdateTile(updateTile, pos);
Expand Down
20 changes: 19 additions & 1 deletion src/lua/functions/creatures/monster/monster_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ int MonsterFunctions::luaMonsterSetType(lua_State* L) {
}
// Assign new MonsterType
monster->mType = mType;
monster->strDescription = asLowerCaseString(mType->nameDescription);
monster->nameDescription = asLowerCaseString(mType->nameDescription);
monster->defaultOutfit = mType->info.outfit;
monster->currentOutfit = mType->info.outfit;
monster->skull = mType->info.skull;
Expand Down Expand Up @@ -529,6 +529,24 @@ int MonsterFunctions::luaMonsterGetName(lua_State* L) {
return 1;
}

int MonsterFunctions::luaMonsterSetName(lua_State* L) {
// monster:setName(name[, nameDescription])
auto monster = getUserdataShared<Monster>(L, 1);
if (!monster) {
reportErrorFunc(getErrorDesc(LUA_ERROR_MONSTER_NOT_FOUND));
pushBoolean(L, false);
return 0;
}

monster->setName(getString(L, 2));
if (lua_gettop(L) >= 3) {
monster->setNameDescription(getString(L, 3));
}

pushBoolean(L, true);
return 1;
}

int MonsterFunctions::luaMonsterHazard(lua_State* L) {
// get: monster:hazard() ; set: monster:hazard(hazard)
std::shared_ptr<Monster> monster = getUserdataShared<Monster>(L, 1);
Expand Down
2 changes: 2 additions & 0 deletions src/lua/functions/creatures/monster/monster_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class MonsterFunctions final : LuaScriptInterface {
registerMethod(L, "Monster", "isForgeable", MonsterFunctions::luaMonsterIsForgeable);

registerMethod(L, "Monster", "getName", MonsterFunctions::luaMonsterGetName);
registerMethod(L, "Monster", "setName", MonsterFunctions::luaMonsterSetName);

registerMethod(L, "Monster", "hazard", MonsterFunctions::luaMonsterHazard);
registerMethod(L, "Monster", "hazardCrit", MonsterFunctions::luaMonsterHazardCrit);
Expand Down Expand Up @@ -116,6 +117,7 @@ class MonsterFunctions final : LuaScriptInterface {
static int luaMonsterIsForgeable(lua_State* L);

static int luaMonsterGetName(lua_State* L);
static int luaMonsterSetName(lua_State* L);

static int luaMonsterHazard(lua_State* L);
static int luaMonsterHazardCrit(lua_State* L);
Expand Down
33 changes: 25 additions & 8 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5947,7 +5947,7 @@ void ProtocolGame::sendCreatureTurn(std::shared_ptr<Creature> creature, uint32_t
NetworkMessage msg;
msg.addByte(0x6B);
msg.addPosition(creature->getPosition());
msg.addByte(stackPos);
msg.addByte(static_cast<uint8_t>(stackPos));
msg.add<uint16_t>(0x63);
msg.add<uint32_t>(creature->getID());
msg.addByte(creature->getDirection());
Expand Down Expand Up @@ -6385,7 +6385,7 @@ void ProtocolGame::sendAddTileItem(const Position &pos, uint32_t stackpos, std::
NetworkMessage msg;
msg.addByte(0x6A);
msg.addPosition(pos);
msg.addByte(stackpos);
msg.addByte(static_cast<uint8_t>(stackpos));
AddItem(msg, item);
writeToOutputBuffer(msg);
}
Expand All @@ -6398,7 +6398,7 @@ void ProtocolGame::sendUpdateTileItem(const Position &pos, uint32_t stackpos, st
NetworkMessage msg;
msg.addByte(0x6B);
msg.addPosition(pos);
msg.addByte(stackpos);
msg.addByte(static_cast<uint8_t>(stackpos));
AddItem(msg, item);
writeToOutputBuffer(msg);
}
Expand All @@ -6413,6 +6413,23 @@ void ProtocolGame::sendRemoveTileThing(const Position &pos, uint32_t stackpos) {
writeToOutputBuffer(msg);
}

void ProtocolGame::sendUpdateTileCreature(const Position &pos, uint32_t stackpos, const std::shared_ptr<Creature> creature) {
if (!canSee(pos)) {
return;
}

NetworkMessage msg;
msg.addByte(0x6B);
msg.addPosition(pos);
msg.addByte(static_cast<uint8_t>(stackpos));

bool known;
uint32_t removedKnown;
checkCreatureAsKnown(creature->getID(), known, removedKnown);
AddCreature(msg, creature, false, removedKnown);
writeToOutputBuffer(msg);
}

void ProtocolGame::sendUpdateTile(std::shared_ptr<Tile> tile, const Position &pos) {
if (!canSee(pos)) {
return;
Expand Down Expand Up @@ -6484,7 +6501,7 @@ void ProtocolGame::sendAddCreature(std::shared_ptr<Creature> creature, const Pos
NetworkMessage msg;
msg.addByte(0x6A);
msg.addPosition(pos);
msg.addByte(stackpos);
msg.addByte(static_cast<uint8_t>(stackpos));

bool known;
uint32_t removedKnown;
Expand Down Expand Up @@ -6641,7 +6658,7 @@ void ProtocolGame::sendMoveCreature(std::shared_ptr<Creature> creature, const Po
} else {
msg.addByte(0x6D);
msg.addPosition(oldPos);
msg.addByte(oldStackPos);
msg.addByte(static_cast<uint8_t>(oldStackPos));
msg.addPosition(newPos);
}

Expand Down Expand Up @@ -6676,7 +6693,7 @@ void ProtocolGame::sendMoveCreature(std::shared_ptr<Creature> creature, const Po
NetworkMessage msg;
msg.addByte(0x6D);
msg.addPosition(oldPos);
msg.addByte(oldStackPos);
msg.addByte(static_cast<uint8_t>(oldStackPos));
msg.addPosition(newPos);
writeToOutputBuffer(msg);
}
Expand Down Expand Up @@ -7836,7 +7853,7 @@ void ProtocolGame::RemoveTileThing(NetworkMessage &msg, const Position &pos, uin

msg.addByte(0x6C);
msg.addPosition(pos);
msg.addByte(stackpos);
msg.addByte(static_cast<uint8_t>(stackpos));
}

void ProtocolGame::sendKillTrackerUpdate(std::shared_ptr<Container> corpse, const std::string &name, const Outfit_t creatureOutfit) {
Expand Down Expand Up @@ -8279,7 +8296,7 @@ void ProtocolGame::reloadCreature(std::shared_ptr<Creature> creature) {
if (knownCreatureSet.contains(creature->getID())) {
msg.addByte(0x6B);
msg.addPosition(creature->getPosition());
msg.addByte(stackpos);
msg.addByte(static_cast<uint8_t>(stackpos));
AddCreature(msg, creature, false, 0);
} else {
sendAddCreature(creature, creature->getPosition(), stackpos, false);
Expand Down
1 change: 1 addition & 0 deletions src/server/network/protocol/protocolgame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ class ProtocolGame final : public Protocol {
void sendAddTileItem(const Position &pos, uint32_t stackpos, std::shared_ptr<Item> item);
void sendUpdateTileItem(const Position &pos, uint32_t stackpos, std::shared_ptr<Item> item);
void sendRemoveTileThing(const Position &pos, uint32_t stackpos);
void sendUpdateTileCreature(const Position &pos, uint32_t stackpos, const std::shared_ptr<Creature> creature);
void sendUpdateTile(std::shared_ptr<Tile> tile, const Position &pos);

void sendAddCreature(std::shared_ptr<Creature> creature, const Position &pos, int32_t stackpos, bool isLogin);
Expand Down
Loading