Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mattyx14 committed Nov 23, 2023
1 parent 0cd4174 commit e100a83
Show file tree
Hide file tree
Showing 20 changed files with 116 additions and 21 deletions.
2 changes: 2 additions & 0 deletions config.lua.dist
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ houseRentRate = 1.0
houseOwnedByAccount = false
houseBuyLevel = 100
housePurchasedShowPrice = false
houseLoseAfterInactivity = 30 -- days; 0 = never
onlyInvitedCanMoveHouseItems = true
togglehouseTransferOnRestart = true

Expand Down Expand Up @@ -479,6 +480,7 @@ vipBonusSkill = 0
vipAutoLootVipOnly = false
vipStayOnline = false
vipFamiliarTimeCooldownReduction = 0
vipKeepHouse = false

-- NOTE: set rewardChestCollectEnabled to true to enable the reward collect system
-- NOTE set rewardChestMaxCollectItems max items per collect action
Expand Down
2 changes: 2 additions & 0 deletions src/config/config_definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ enum booleanConfig_t {
TOGGLE_ATTACK_SPEED_ONFIST,
VIP_SYSTEM_ENABLED,
VIP_AUTOLOOT_VIP_ONLY,
VIP_KEEP_HOUSE,
VIP_STAY_ONLINE,
REWARD_CHEST_COLLECT_ENABLED,
TOGGLE_MOUNT_IN_PZ,
Expand Down Expand Up @@ -145,6 +146,7 @@ enum integerConfig_t {
RATE_KILLING_IN_THE_NAME_OF_POINTS,
HOUSE_PRICE_PER_SQM,
HOUSE_BUY_LEVEL,
HOUSE_LOSE_AFTER_INACTIVITY,
MAX_MESSAGEBUFFER,
ACTIONS_DELAY_INTERVAL,
EX_ACTIONS_DELAY_INTERVAL,
Expand Down
2 changes: 2 additions & 0 deletions src/config/configmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ bool ConfigManager::load() {

integer[HOUSE_PRICE_PER_SQM] = getGlobalNumber(L, "housePriceEachSQM", 1000);
integer[HOUSE_BUY_LEVEL] = getGlobalNumber(L, "houseBuyLevel", 0);
integer[HOUSE_LOSE_AFTER_INACTIVITY] = getGlobalNumber(L, "houseLoseAfterInactivity", 0);
boolean[HOUSE_PURSHASED_SHOW_PRICE] = getGlobalBoolean(L, "housePurchasedShowPrice", false);
boolean[ONLY_INVITED_CAN_MOVE_HOUSE_ITEMS] = getGlobalBoolean(L, "onlyInvitedCanMoveHouseItems", true);

Expand Down Expand Up @@ -384,6 +385,7 @@ bool ConfigManager::load() {
integer[VIP_BONUS_LOOT] = getGlobalNumber(L, "vipBonusLoot", 0);
integer[VIP_BONUS_SKILL] = getGlobalNumber(L, "vipBonusSkill", 0);
boolean[VIP_AUTOLOOT_VIP_ONLY] = getGlobalBoolean(L, "vipAutoLootVipOnly", false);
boolean[VIP_KEEP_HOUSE] = getGlobalBoolean(L, "vipKeepHouse", false);
boolean[VIP_STAY_ONLINE] = getGlobalBoolean(L, "vipStayOnline", false);
integer[VIP_FAMILIAR_TIME_COOLDOWN_REDUCTION] = getGlobalNumber(L, "vipFamiliarTimeCooldownReduction", 0);

Expand Down
5 changes: 3 additions & 2 deletions src/creatures/combat/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,19 +297,20 @@ ReturnValue Combat::canDoCombat(std::shared_ptr<Creature> attacker, std::shared_
return RETURNVALUE_NOERROR;
}

auto targetPlayer = target ? target->getPlayer() : nullptr;
if (target) {
std::shared_ptr<Tile> tile = target->getTile();
if (tile->hasProperty(CONST_PROP_BLOCKPROJECTILE)) {
return RETURNVALUE_NOTENOUGHROOM;
}
if (tile->hasFlag(TILESTATE_PROTECTIONZONE)) {
return RETURNVALUE_ACTIONNOTPERMITTEDINPROTECTIONZONE;
auto permittedOnPz = targetPlayer ? targetPlayer->hasPermittedConditionInPZ() : false;
return permittedOnPz ? RETURNVALUE_NOERROR : RETURNVALUE_ACTIONNOTPERMITTEDINPROTECTIONZONE;
}
}

if (attacker) {
const std::shared_ptr<Creature> attackerMaster = attacker->getMaster();
auto targetPlayer = target ? target->getPlayer() : nullptr;
if (targetPlayer) {
if (targetPlayer->hasFlag(PlayerFlags_t::CannotBeAttacked)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ bool Creature::dropCorpse(std::shared_ptr<Creature> lastHitCreature, std::shared
auto monster = getMonster();
if (monster && !monster->isRewardBoss()) {
std::ostringstream lootMessage;
lootMessage << "Loot of " << getNameDescription() << ": " << corpse->getContainer()->getContentDescription(player->getProtocolVersion() < 1200);
lootMessage << "Loot of " << getNameDescription() << ": " << corpse->getContainer()->getContentDescription(player->getProtocolVersion() < 1200) << ".";
auto suffix = corpse->getContainer()->getAttribute<std::string>(ItemAttribute_t::LOOTMESSAGE_SUFFIX);
if (!suffix.empty()) {
lootMessage << suffix;
Expand Down
12 changes: 12 additions & 0 deletions src/creatures/creature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,27 @@ class Creature : virtual public Thing, public SharedObject {
std::shared_ptr<Creature> getCreature() override final {
return static_self_cast<Creature>();
}
std::shared_ptr<const Creature> getCreature() const override final {
return static_self_cast<Creature>();
}
virtual std::shared_ptr<Player> getPlayer() {
return nullptr;
}
virtual std::shared_ptr<const Player> getPlayer() const {
return nullptr;
}
virtual std::shared_ptr<Npc> getNpc() {
return nullptr;
}
virtual std::shared_ptr<const Npc> getNpc() const {
return nullptr;
}
virtual std::shared_ptr<Monster> getMonster() {
return nullptr;
}
virtual std::shared_ptr<const Monster> getMonster() const {
return nullptr;
}

virtual const std::string &getName() const = 0;
// Real creature name, set on creature creation "createNpcType(typeName) and createMonsterType(typeName)"
Expand Down
3 changes: 3 additions & 0 deletions src/creatures/monsters/monster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class Monster final : public Creature {
std::shared_ptr<Monster> getMonster() override {
return static_self_cast<Monster>();
}
std::shared_ptr<const Monster> getMonster() const override {
return static_self_cast<Monster>();
}

void setID() override {
if (id == 0) {
Expand Down
3 changes: 3 additions & 0 deletions src/creatures/npcs/npc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class Npc final : public Creature {
std::shared_ptr<Npc> getNpc() override {
return static_self_cast<Npc>();
}
std::shared_ptr<const Npc> getNpc() const override {
return static_self_cast<Npc>();
}

void setID() override {
if (id == 0) {
Expand Down
25 changes: 25 additions & 0 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6722,6 +6722,10 @@ std::pair<std::vector<std::shared_ptr<Item>>, std::map<uint16_t, std::map<uint8_
continue;
}

if (item->isStoreItem()) {
continue;
}

const ItemType &itemType = Item::items[item->getID()];
if (itemType.wareId == 0) {
continue;
Expand Down Expand Up @@ -7712,3 +7716,24 @@ std::shared_ptr<Container> Player::getLootPouch() {

return container;
}

bool Player::hasPermittedConditionInPZ() const {
static const std::unordered_set<ConditionType_t> allowedConditions = {
CONDITION_ENERGY,
CONDITION_FIRE,
CONDITION_POISON,
CONDITION_BLEEDING,
CONDITION_CURSED,
CONDITION_DAZZLED
};

bool hasPermittedCondition = false;
for (auto condition : allowedConditions) {
if (getCondition(condition)) {
hasPermittedCondition = true;
break;
}
}

return hasPermittedCondition;
}
9 changes: 9 additions & 0 deletions src/creatures/players/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ class Player final : public Creature, public Cylinder, public Bankable {
std::shared_ptr<Player> getPlayer() override {
return static_self_cast<Player>();
}
std::shared_ptr<const Player> getPlayer() const override {
return static_self_cast<Player>();
}

static std::shared_ptr<Task> createPlayerTask(uint32_t delay, std::function<void(void)> f, std::string context);

Expand Down Expand Up @@ -341,6 +344,10 @@ class Player final : public Creature, public Cylinder, public Bankable {
operatingSystem = clientos;
}

bool isOldProtocol() {
return client && client->oldProtocol;
}

uint32_t getProtocolVersion() const {
if (!client) {
return 0;
Expand Down Expand Up @@ -2541,6 +2548,8 @@ class Player final : public Creature, public Cylinder, public Bankable {

std::shared_ptr<Container> getLootPouch();

bool hasPermittedConditionInPZ() const;

private:
friend class PlayerLock;
std::mutex mutex;
Expand Down
6 changes: 3 additions & 3 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1654,11 +1654,9 @@ ReturnValue Game::checkMoveItemToCylinder(std::shared_ptr<Player> player, std::s
}
}

if (item->isStoreItem() && !toHouseTile) {
if (item->isStoreItem() && !house) {
return RETURNVALUE_NOTPOSSIBLE;
}

return RETURNVALUE_NOERROR;
}
}

Expand Down Expand Up @@ -4216,6 +4214,8 @@ void Game::playerStashWithdraw(uint32_t playerId, uint16_t itemId, uint32_t coun
if (player->isDepotSearchOpenOnItem(itemId)) {
player->requestDepotSearchItem(itemId, 0);
}

player->sendOpenStash(true);
}

void Game::playerSeekInContainer(uint32_t playerId, uint8_t containerId, uint16_t index, uint8_t containerCategory) {
Expand Down
7 changes: 2 additions & 5 deletions src/io/iomarket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,9 @@ void IOMarket::processExpiredOffers(DBResult_ptr result, bool) {
continue;
}

std::shared_ptr<Player> player = g_game().getPlayerByGUID(playerId);
std::shared_ptr<Player> player = g_game().getPlayerByGUID(playerId, true);
if (!player) {
player = std::make_shared<Player>(nullptr);
if (!IOLoginData::loadPlayerById(player, playerId)) {
continue;
}
continue;
}

if (itemType.stackable) {
Expand Down
4 changes: 4 additions & 0 deletions src/items/containers/container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class Container : public Item, public Cylinder {
return static_self_cast<Container>();
}

std::shared_ptr<const Container> getContainer() const override final {
return static_self_cast<Container>();
}

std::shared_ptr<Container> getRootContainer();

virtual std::shared_ptr<DepotLocker> getDepotLocker() {
Expand Down
3 changes: 3 additions & 0 deletions src/items/item.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ class Item : virtual public Thing, public ItemProperties, public SharedObject {
std::shared_ptr<Item> getItem() override final {
return static_self_cast<Item>();
}
std::shared_ptr<const Item> getItem() const override final {
return static_self_cast<Item>();
}
virtual std::shared_ptr<Teleport> getTeleport() {
return nullptr;
}
Expand Down
6 changes: 3 additions & 3 deletions src/items/thing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ class Thing {
virtual std::shared_ptr<Container> getContainer() {
return nullptr;
}
virtual std::shared_ptr<Container> getContainer() const {
virtual std::shared_ptr<const Container> getContainer() const {
return nullptr;
}
virtual std::shared_ptr<Item> getItem() {
return nullptr;
}
virtual std::shared_ptr<Item> getItem() const {
virtual std::shared_ptr<const Item> getItem() const {
return nullptr;
}
virtual std::shared_ptr<Creature> getCreature() {
return nullptr;
}
virtual std::shared_ptr<Creature> getCreature() const {
virtual std::shared_ptr<const Creature> getCreature() const {
return nullptr;
}

Expand Down
1 change: 1 addition & 0 deletions src/lua/functions/core/game/config_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ void ConfigFunctions::init(lua_State* L) {
registerEnumIn(L, "configKeys", RATE_KILLING_IN_THE_NAME_OF_POINTS);
registerEnumIn(L, "configKeys", HOUSE_PRICE_PER_SQM);
registerEnumIn(L, "configKeys", HOUSE_BUY_LEVEL);
registerEnumIn(L, "configKeys", HOUSE_LOSE_AFTER_INACTIVITY);
registerEnumIn(L, "configKeys", MAX_MESSAGEBUFFER);
registerEnumIn(L, "configKeys", ACTIONS_DELAY_INTERVAL);
registerEnumIn(L, "configKeys", EX_ACTIONS_DELAY_INTERVAL);
Expand Down
5 changes: 5 additions & 0 deletions src/lua/global/shared_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class SharedObject : public std::enable_shared_from_this<SharedObject> {
return std::static_pointer_cast<T>(shared_from_this());
}

template <typename T>
std::shared_ptr<const T> static_self_cast() const {
return std::static_pointer_cast<const T>(shared_from_this());
}

template <typename T>
std::shared_ptr<T> dynamic_self_cast() {
return std::dynamic_pointer_cast<T>(shared_from_this());
Expand Down
26 changes: 21 additions & 5 deletions src/map/house/house.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,11 +752,6 @@ void Houses::payHouses(RentPeriod_t rentPeriod) const {
continue;
}

const uint32_t rent = house->getRent();
if (rent == 0 || house->getPaidUntil() > currentTime) {
continue;
}

const uint32_t ownerId = house->getOwner();
const auto &town = g_game().map.towns.getTown(house->getTownId());
if (!town) {
Expand All @@ -770,6 +765,27 @@ void Houses::payHouses(RentPeriod_t rentPeriod) const {
continue;
}

// Player hasn't logged in for a while, reset house owner
auto daysToReset = g_configManager().getNumber(HOUSE_LOSE_AFTER_INACTIVITY);
if (daysToReset > 0) {
auto daysSinceLastLogin = (currentTime - player->getLastLoginSaved()) / (60 * 60 * 24);
bool vipKeep = g_configManager().getBoolean(VIP_KEEP_HOUSE) && player->isVip();
bool activityKeep = daysSinceLastLogin < daysToReset;
if (vipKeep && !activityKeep) {
g_logger().info("Player {} has not logged in for {} days, but is a VIP, so the house will not be reset.", player->getName(), daysToReset);
} else if (!vipKeep && !activityKeep) {
g_logger().info("Player {} has not logged in for {} days, so the house will be reset.", player->getName(), daysToReset);
house->setOwner(0, true, player);
g_saveManager().savePlayer(player);
continue;
}
}

const uint32_t rent = house->getRent();
if (rent == 0 || house->getPaidUntil() > currentTime) {
continue;
}

if (player->getBankBalance() >= rent) {
g_game().removeMoney(player, rent, 0, true);

Expand Down
12 changes: 11 additions & 1 deletion src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@ void ProtocolGame::login(const std::string &name, uint32_t accountId, OperatingS
writeToOutputBuffer(opcodeMessage);
}

g_logger().debug("Player logging in in version '{}' and oldProtocol '{}'", getVersion(), oldProtocol);

// dispatcher thread
std::shared_ptr<Player> foundPlayer = g_game().getPlayerUniqueLogin(name);
if (!foundPlayer) {
Expand Down Expand Up @@ -734,6 +736,11 @@ void ProtocolGame::onRecvFirstMessage(NetworkMessage &msg) {

std::shared_ptr<Player> foundPlayer = g_game().getPlayerUniqueLogin(characterName);
if (foundPlayer && foundPlayer->client) {
if (foundPlayer->getProtocolVersion() != getVersion() && foundPlayer->isOldProtocol() != oldProtocol) {
disconnectClient(fmt::format("You are already logged in using protocol '{}'. Please log out from the other session to connect here.", foundPlayer->getProtocolVersion()));
return;
}

foundPlayer->client->disconnectClient("You are already connected through another client. Please use only one client at a time!");
}

Expand Down Expand Up @@ -6899,7 +6906,10 @@ void ProtocolGame::sendPreyData(const std::unique_ptr<PreySlot> &slot) {
}

if (oldProtocol) {
msg.add<uint16_t>(static_cast<uint16_t>(std::max<uint32_t>(std::max<uint32_t>(static_cast<uint32_t>(((slot->freeRerollTimeStamp - OTSYS_TIME()) / 1000)), 0), 0)));
auto currentTime = OTSYS_TIME();
auto timeDiffMs = (slot->freeRerollTimeStamp > currentTime) ? (slot->freeRerollTimeStamp - currentTime) : 0;
auto timeDiffMinutes = timeDiffMs / 60000;
msg.add<uint16_t>(timeDiffMinutes ? timeDiffMinutes : 0);
} else {
msg.add<uint32_t>(std::max<uint32_t>(static_cast<uint32_t>(((slot->freeRerollTimeStamp - OTSYS_TIME()) / 1000)), 0));
msg.addByte(static_cast<uint8_t>(slot->option));
Expand Down
2 changes: 1 addition & 1 deletion src/server/network/protocol/protocolstatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

std::string ProtocolStatus::SERVER_NAME = "OTX Server";
std::string ProtocolStatus::SERVER_VERSION = "6.2";
std::string ProtocolStatus::SERVER_DEVELOPERS = "OpenTibiaBR Organization under. Base: Canary (3.1.1). And data edited by: Mattyx14";
std::string ProtocolStatus::SERVER_DEVELOPERS = "OpenTibiaBR Organization. Based on: Canary (3.1.1). And data edited by: Mattyx14";

std::map<uint32_t, int64_t> ProtocolStatus::ipConnectMap;
const uint64_t ProtocolStatus::start = OTSYS_TIME();
Expand Down

0 comments on commit e100a83

Please sign in to comment.