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: features OTCR natively in canary #3061

Draft
wants to merge 44 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
4c54d85
1/6 feature: Creature : AttachedEffect, Shader
kokekanon Nov 4, 2024
745b7b8
2/7 feature: Map Shader
kokekanon Nov 4, 2024
ee26730
3/7 feature: TypingIcon
kokekanon Nov 5, 2024
23e166a
review: mehah
kokekanon Nov 5, 2024
eae4112
4/7 feature: Item Shader
kokekanon Nov 5, 2024
74ea55f
5/7 send disableFeature / enableFeature
kokekanon Nov 5, 2024
b09547e
review: dudantas
kokekanon Nov 5, 2024
9484492
fix sonarcloud bot
kokekanon Nov 5, 2024
f490ce4
fix sonarcloud bot 2
kokekanon Nov 5, 2024
1437b7e
fix: wrong review
dudantas Nov 5, 2024
67777a9
Code format - (Clang-format)
github-actions[bot] Nov 5, 2024
6766389
fix: itemShader old protocol otcr
kokekanon Nov 5, 2024
7c45b97
clean variable
kokekanon Nov 6, 2024
bd1854c
fix return get shader
kokekanon Nov 6, 2024
0ff915e
missing content player:getAttachedEffects
kokekanon Nov 6, 2024
f538950
simple test Talkations. delete when finished
kokekanon Nov 6, 2024
0217e80
Lua code format - (Stylua)
github-actions[bot] Nov 6, 2024
4b5fbad
Merge remote-tracking branch 'upstream/main' into kokekanon/all-featu…
kokekanon Nov 6, 2024
9fe26c5
format
kokekanon Nov 6, 2024
7956f08
Merge remote-tracking branch 'upstream/main' into kokekanon/all-featu…
kokekanon Nov 6, 2024
8d6961d
improve: variable name
kokekanon Nov 6, 2024
558b212
Merge branch 'kokekanon/all-feature-redemption' of https://github.com…
kokekanon Nov 6, 2024
3744f16
game_outfit functional version, not cleaned or optimized
kokekanon Nov 6, 2024
789354a
this requires review canary team .no break v8 and cipsoft
kokekanon Nov 7, 2024
6a23e40
Lua code format - (Stylua)
github-actions[bot] Nov 7, 2024
36da43e
fix: review sonarcloud ?
kokekanon Nov 7, 2024
8e939e5
Code format - (Clang-format)
github-actions[bot] Nov 7, 2024
c551152
Merge remote-tracking branch 'upstream/main' into kokekanon/all-featu…
kokekanon Nov 7, 2024
5b5dc84
Merge branch 'kokekanon/all-feature-redemption' of https://github.com…
kokekanon Nov 7, 2024
9e763f7
simple test
kokekanon Nov 7, 2024
300f92c
Lua code format - (Stylua)
github-actions[bot] Nov 7, 2024
3bb868b
Merge remote-tracking branch 'upstream/main' into kokekanon/all-featu…
kokekanon Nov 8, 2024
ef754d0
Merge branch 'kokekanon/all-feature-redemption' of https://github.com…
kokekanon Nov 8, 2024
6b27bb9
Merge remote-tracking branch 'upstream/main' into kokekanon/all-featu…
kokekanon Nov 10, 2024
6097de3
Merge remote-tracking branch 'upstream/main' into kokekanon/all-featu…
kokekanon Nov 10, 2024
ec0bd43
murge
kokekanon Nov 10, 2024
e30fd35
fix: compilation windows
kokekanon Nov 10, 2024
29919ed
Code format - (Clang-format)
github-actions[bot] Nov 10, 2024
6ed239c
Merge remote-tracking branch 'upstream/main' into kokekanon/all-featu…
kokekanon Nov 13, 2024
2c46d0a
Merge remote-tracking branch 'upstream/main' into kokekanon/all-featu…
kokekanon Nov 16, 2024
687d0b2
Merge remote-tracking branch 'upstream/main' into kokekanon/all-featu…
kokekanon Nov 18, 2024
dce1886
Merge remote-tracking branch 'upstream/main' into kokekanon/all-featu…
kokekanon Nov 25, 2024
f44bf10
Merge remote-tracking branch 'upstream/main' into kokekanon/all-featu…
kokekanon Nov 29, 2024
8350458
Merge remote-tracking branch 'upstream/main' into kokekanon/all-featu…
kokekanon Dec 12, 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
13 changes: 13 additions & 0 deletions config.lua.dist
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,16 @@ metricsPrometheusAddress = "0.0.0.0:9464"
--- OStream
metricsEnableOstream = false
metricsOstreamInterval = 1000

-- OTC Features
-- NOTE: Features added in this list will be forced to be used on the client
-- These features can be found in "modules/gamelib/const.lua"
OTCRFeatures = {
enable = {
101, -- g_game.enableFeature(GameItemShader)
102, -- g_game.enableFeature(GameCreatureAttachedEffect)
103 -- g_game.enableFeature(GameCreatureShader)
},
disable = { -- utility: for example in 13.10 g_game.disableFeature(GameSequencedPackets)
}
}
42 changes: 42 additions & 0 deletions src/config/configmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ bool ConfigManager::load() {
loadStringConfig(L, WORLD_TYPE, "worldType", "pvp");
loadStringConfig(L, LOGLEVEL, "logLevel", "info");

loadLuaOTCFeatures(L);

loaded = true;
lua_close(L);
return true;
Expand Down Expand Up @@ -464,3 +466,43 @@ float ConfigManager::getFloat(const ConfigKey_t &key, const std::source_location
g_logger().warn("[{}] accessing invalid or wrong type index: {}[{}]. Called line: {}:{}, in {}", __FUNCTION__, magic_enum::enum_name(key), fmt::underlying(key), location.line(), location.column(), location.function_name());
return 0.0f;
}

void ConfigManager::loadLuaOTCFeatures(lua_State* L) {
lua_getglobal(L, "OTCRFeatures");
if (!lua_istable(L, -1)) {
return;
}

lua_pushstring(L, "enable");
lua_gettable(L, -2);
if (lua_istable(L, -1)) {
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
const auto feature = static_cast<uint8_t>(lua_tointeger(L, -1));
enabledOTCFeatures.push_back(feature);
lua_pop(L, 1);
}
}
lua_pop(L, 1);

lua_pushstring(L, "disable");
lua_gettable(L, -2);
if (lua_istable(L, -1)) {
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
const auto feature = static_cast<uint8_t>(lua_tointeger(L, -1));
disabledOTCFeatures.push_back(feature);
lua_pop(L, 1);
}
}
lua_pop(L, 1);

lua_pop(L, 1);
}
OTCFeatures ConfigManager::getEnabledOTCFeatures() const {
return enabledOTCFeatures;
}

OTCFeatures ConfigManager::getDisabledOTCFeatures() const {
return disabledOTCFeatures;
}
6 changes: 6 additions & 0 deletions src/config/configmanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "config_enums.hpp"

using ConfigValue = std::variant<std::string, int32_t, bool, float>;
using OTCFeatures = std::vector<uint8_t>;

class ConfigManager {
public:
Expand Down Expand Up @@ -40,6 +41,8 @@ class ConfigManager {
[[nodiscard]] int32_t getNumber(const ConfigKey_t &key, const std::source_location &location = std::source_location::current()) const;
[[nodiscard]] bool getBoolean(const ConfigKey_t &key, const std::source_location &location = std::source_location::current()) const;
[[nodiscard]] float getFloat(const ConfigKey_t &key, const std::source_location &location = std::source_location::current()) const;
OTCFeatures getEnabledOTCFeatures() const;
OTCFeatures getDisabledOTCFeatures() const;

private:
phmap::flat_hash_map<ConfigKey_t, ConfigValue> configs;
Expand All @@ -50,6 +53,9 @@ class ConfigManager {

std::string configFileLua = { "config.lua" };
bool loaded = false;
OTCFeatures enabledOTCFeatures = {};
OTCFeatures disabledOTCFeatures = {};
void loadLuaOTCFeatures(lua_State* L);
};

constexpr auto g_configManager = ConfigManager::getInstance;
17 changes: 17 additions & 0 deletions src/creatures/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2002,3 +2002,20 @@ void Creature::sendAsyncTasks() {
},
TaskGroup::WalkParallel);
}

void Creature::attachEffectById(uint16_t id) {
auto it = std::ranges::find(attachedEffectList, id);
if (it != attachedEffectList.end()) {
return;
}
attachedEffectList.push_back(id);
g_game().sendAttachedEffect(static_self_cast<Creature>(), id);
}
void Creature::detachEffectById(uint16_t id) {
auto it = std::ranges::find(attachedEffectList, id);
if (it == attachedEffectList.end()) {
return;
}
attachedEffectList.erase(it);
g_game().sendDetachEffect(static_self_cast<Creature>(), id);
}
13 changes: 13 additions & 0 deletions src/creatures/creature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,17 @@ class Creature : virtual public Thing, public SharedObject {
void setCharmChanceModifier(int8_t value) {
charmChanceModifier = value;
}
std::string getShader() const {
return shader;
}
void setShader(const std::string_view &shaderName) {
Copy link
Member

Choose a reason for hiding this comment

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

You can remove the & as well; passing a std::string_view by reference actually increases overhead. std::string_view is intended to be passed by value since it is simply a lightweight view of the string.

shader = shaderName;
}
void attachEffectById(uint16_t id);
void detachEffectById(uint16_t id);
const std::vector<uint16_t> getAttachedEffectList() const {
return attachedEffectList;
}

protected:
enum FlagAsyncClass_t : uint8_t {
Expand Down Expand Up @@ -881,4 +892,6 @@ class Creature : virtual public Thing, public SharedObject {
}

uint8_t m_flagAsyncTask = 0;
std::vector<uint16_t> attachedEffectList;
std::string shader;
};
46 changes: 46 additions & 0 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9572,6 +9572,52 @@ void Player::sendLootContainers() const {
}
}

// OTCR Features

void Player::sendAttachedEffect(const std::shared_ptr<Creature> &creature, uint16_t effectId) const {
if (!client || !creature) {
return;
}

if (creature->getPlayer()) {
client->sendAttachedEffect(creature, effectId);
}
}

void Player::sendDetachEffect(const std::shared_ptr<Creature> &creature, uint16_t effectId) const {
if (!client || !creature) {
return;
}
if (creature->getPlayer()) {
client->sendDetachEffect(creature, effectId);
}
}

void Player::sendShader(const std::shared_ptr<Creature> &creature, const std::string_view &shaderName) const {
if (!client || !creature) {
return;
}
if (creature->getPlayer()) {
client->sendShader(creature, shaderName);
}
}

void Player::sendMapShader(const std::string_view &shaderName) const {
if (!client) {
return;
}

client->sendMapShader(shaderName);
}

void Player::sendPlayerTyping(const std::shared_ptr<Creature> &creature, uint8_t typing) const {
if (!client) {
return;
}

client->sendPlayerTyping(creature, typing);
}

void Player::sendSingleSoundEffect(const Position &pos, SoundEffect_t id, SourceEffect_t source) const {
if (client) {
client->sendSingleSoundEffect(pos, id, source);
Expand Down
13 changes: 13 additions & 0 deletions src/creatures/players/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,18 @@ class Player final : public Creature, public Cylinder, public Bankable {

uint16_t getPlayerVocationEnum() const;

void sendAttachedEffect(const std::shared_ptr<Creature> &creature, uint16_t effectId) const;
void sendDetachEffect(const std::shared_ptr<Creature> &creature, uint16_t effectId) const;
void sendShader(const std::shared_ptr<Creature> &creature, const std::string_view &shaderName) const;
void sendMapShader(const std::string_view &shaderName) const;
const std::string_view &getMapShader() const {
return mapShader;
}
void setMapShader(const std::string_view &shaderName) {
this->mapShader = shaderName;
}
void sendPlayerTyping(const std::shared_ptr<Creature> &creature, uint8_t typing) const;

private:
friend class PlayerLock;
std::mutex mutex;
Expand Down Expand Up @@ -1371,6 +1383,7 @@ class Player final : public Creature, public Cylinder, public Bankable {
std::string name;
std::string guildNick;
std::string loyaltyTitle;
std::string_view mapShader;
Copy link
Member

Choose a reason for hiding this comment

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

Here, it will remain as std::string. std::string_view is used to avoid copying the string, but in the final context where it needs to be stored or modified, it must be a std::string.


Skill skills[SKILL_LAST + 1];
LightInfo itemsLight;
Expand Down
77 changes: 77 additions & 0 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10852,3 +10852,80 @@ void Game::updatePlayersOnline() const {
g_logger().error("[Game::updatePlayersOnline] Failed to update players online.");
}
}

void Game::sendAttachedEffect(const std::shared_ptr<Creature> &creature, uint16_t effectId) {
auto spectators = Spectators().find<Player>(creature->getPosition(), true);
for (const auto &spectator : spectators) {
const auto &player = spectator->getPlayer();
if (player) {
player->sendAttachedEffect(creature, effectId);
}
}
}

void Game::sendDetachEffect(const std::shared_ptr<Creature> &creature, uint16_t effectId) {
auto spectators = Spectators().find<Player>(creature->getPosition(), true);
for (const auto &spectator : spectators) {
const auto &player = spectator->getPlayer();
if (player) {
player->sendDetachEffect(creature, effectId);
}
}
}

void Game::updateCreatureShader(const std::shared_ptr<Creature> &creature) {
auto spectators = Spectators().find<Player>(creature->getPosition(), true);
for (const auto &spectator : spectators) {
const auto &player = spectator->getPlayer();
if (player) {
player->sendShader(creature, creature->getShader());
}
}
}

void Game::playerSetTyping(uint32_t playerId, uint8_t typing) {
const auto &player = getPlayerByID(playerId);
if (!player) {
return;
}
for (const auto &spectator : Spectators().find<Player>(player->getPosition(), true)) {
if (const auto &tmpPlayer = spectator->getPlayer()) {
tmpPlayer->sendPlayerTyping(player, typing);
}
}
}

void Game::refreshItem(const std::shared_ptr<Item> &item) {
if (!item || !item->getParent()) {
return;
}
const auto &parent = item->getParent();
if (const auto &creature = parent->getCreature()) {
if (const auto &player = creature->getPlayer()) {
int32_t index = player->getThingIndex(item);
if (index > -1) {
player->sendInventoryItem(static_cast<Slots_t>(index), item);
}
}
return;
}
if (const auto &container = parent->getContainer()) {
int32_t index = container->getThingIndex(item);
if (index > -1 && index <= std::numeric_limits<uint16_t>::max()) {
const auto spectators = Spectators().find<Player>(container->getPosition(), false, 2, 2, 2, 2);
// send to client
for (const auto &spectator : spectators) {
spectator->getPlayer()->sendUpdateContainerItem(container, static_cast<uint16_t>(index), item);
}
}
return;
}
if (const auto &tile = parent->getTile()) {
const auto spectators = Spectators().find<Player>(tile->getPosition(), true);
// send to client
for (const auto &spectator : spectators) {
spectator->getPlayer()->sendUpdateTileItem(tile, tile->getPosition(), item);
}
return;
}
}
5 changes: 5 additions & 0 deletions src/game/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,11 @@ class Game {
const std::map<uint8_t, std::string> &getBlessingNames();
const std::unordered_map<uint16_t, std::string> &getHirelingSkills();
const std::unordered_map<uint16_t, std::string> &getHirelingOutfits();
void sendAttachedEffect(const std::shared_ptr<Creature> &creature, uint16_t effectId);
void sendDetachEffect(const std::shared_ptr<Creature> &creature, uint16_t effectId);
void updateCreatureShader(const std::shared_ptr<Creature> &creature);
void playerSetTyping(uint32_t playerId, uint8_t typing);
void refreshItem(const std::shared_ptr<Item> &item);

private:
std::map<uint16_t, Achievement> m_achievements;
Expand Down
18 changes: 18 additions & 0 deletions src/items/item.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,24 @@ class ItemProperties {
return getCorpseOwner() == static_cast<uint32_t>(std::numeric_limits<int32_t>::max());
}

void setShader(const std::string_view &shaderName) {
if (shaderName.empty()) {
removeCustomAttribute("shader");
return;
}

setCustomAttribute("shader", std::string(shaderName)); // temp
}

bool hasShader() const {
return getCustomAttribute("shader") != nullptr;
}

std::string_view getShader() const {
const CustomAttribute* shader = getCustomAttribute("shader");
return shader ? shader->getString() : "";
}

protected:
std::unique_ptr<ItemAttribute> &initAttributePtr() {
if (!attributePtr) {
Expand Down
Loading
Loading