Skip to content

Commit

Permalink
Merge branch 'main' into beats-fixs
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Oct 18, 2024
2 parents 9c67268 + 1af76e2 commit 2979ca2
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 107 deletions.
18 changes: 0 additions & 18 deletions data-otservbr-global/lib/quests/soul_war.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1569,21 +1569,3 @@ function Creature:applyZoneEffect(var, combat, zoneName)

return true
end

function string.toPosition(str)
local patterns = {
-- table format
"{%s*x%s*=%s*(%d+)%s*,%s*y%s*=%s*(%d+)%s*,%s*z%s*=%s*(%d+)%s*}",
-- Position format
"Position%s*%((%d+)%s*,%s*(%d+)%s*,%s*(%d+)%s*%)",
-- x, y, z format
"(%d+)%s*,%s*(%d+)%s*,%s*(%d+)",
}

for _, pattern in ipairs(patterns) do
local x, y, z = string.match(str, pattern)
if x and y and z then
return Position(tonumber(x), tonumber(y), tonumber(z))
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function bossEntrance.onStepIn(creature, item, position, fromPosition, toPositio
if timeLeft > 0 then
player:teleportTo(fromPosition, true)
player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have to wait " .. getTimeInWords(timeLeft) .. " to face " .. bossName .. " again!")
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have to wait " .. Game.getTimeInWords(timeLeft) .. " to face " .. bossName .. " again!")
player:getPosition():sendMagicEffect(CONST_ME_POFF)
return true
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function teleportBoss.onStepIn(creature, item, position, fromPosition)
if timeLeft > 0 then
player:teleportTo(config.exitPosition, true)
player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have to wait " .. getTimeInWords(timeLeft) .. " to face " .. config.bossName .. " again!")
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have to wait " .. Game.getTimeInWords(timeLeft) .. " to face " .. config.bossName .. " again!")
player:getPosition():sendMagicEffect(CONST_ME_POFF)
return false
end
Expand Down
2 changes: 1 addition & 1 deletion data/events/scripts/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ function Player:onLookInBattleList(creature, distance)
if master and table.contains(summons, creature:getName():lower()) then
local familiarSummonTime = master:kv():get("familiar-summon-time") or 0
description = description .. " (Master: " .. master:getName() .. "). \z
It will disappear in " .. getTimeInWords(familiarSummonTime - os.time())
It will disappear in " .. Game.getTimeInWords(familiarSummonTime - os.time())
end
end
if self:getGroup():getAccess() then
Expand Down
2 changes: 1 addition & 1 deletion data/libs/functions/boss_lever.lua
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ function BossLever:onUse(player)
local currentTime = os.time()
if lastEncounter and currentTime < lastEncounter then
local timeLeft = lastEncounter - currentTime
local timeMessage = getTimeInWords(timeLeft) .. " to face " .. self.name .. " again!"
local timeMessage = Game.getTimeInWords(timeLeft) .. " to face " .. self.name .. " again!"
local message = "You have to wait " .. timeMessage

if currentPlayer ~= player then
Expand Down
62 changes: 0 additions & 62 deletions data/libs/functions/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,43 +65,6 @@ function getTitle(uid)
return false
end

function getTimeInWords(secsParam)
local secs = tonumber(secsParam)
local days = math.floor(secs / (24 * 3600))
secs = secs - (days * 24 * 3600)
local hours, minutes, seconds = getHours(secs), getMinutes(secs), getSeconds(secs)
local timeStr = ""

if days > 0 then
timeStr = days .. (days > 1 and " days" or " day")
end

if hours > 0 then
if timeStr ~= "" then
timeStr = timeStr .. ", "
end

timeStr = timeStr .. hours .. (hours > 1 and " hours" or " hour")
end

if minutes > 0 then
if timeStr ~= "" then
timeStr = timeStr .. ", "
end

timeStr = timeStr .. minutes .. (minutes > 1 and " minutes" or " minute")
end

if seconds > 0 then
if timeStr ~= "" then
timeStr = timeStr .. " and "
end

timeStr = timeStr .. seconds .. (seconds > 1 and " seconds" or " second")
end
return timeStr
end

function getLootRandom(modifier)
local multi = (configManager.getNumber(configKeys.RATE_LOOT) * SCHEDULE_LOOT_RATE) * (modifier or 1)
return math.random(0, MAX_LOOTCHANCE) * 100 / math.max(1, multi)
Expand Down Expand Up @@ -949,31 +912,6 @@ function SetInfluenced(monsterType, monster, player, influencedLevel)
monster:setForgeStack(influencedLevel)
end

function getHours(seconds)
return math.floor((seconds / 60) / 60)
end

function getMinutes(seconds)
return math.floor(seconds / 60) % 60
end

function getSeconds(seconds)
return seconds % 60
end

function getTime(seconds)
local hours, minutes = getHours(seconds), getMinutes(seconds)
if minutes > 59 then
minutes = minutes - hours * 60
end

if minutes < 10 then
minutes = "0" .. minutes
end

return hours .. ":" .. minutes .. "h"
end

function ReloadDataEvent(cid)
local player = Player(cid)
if not player then
Expand Down
34 changes: 34 additions & 0 deletions data/libs/functions/game.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,37 @@ function Game.setStorageValue(key, value)

globalStorageTable[key] = value
end

function Game.getTimeInWords(seconds)
local days = math.floor(seconds / (24 * 3600))
seconds = seconds % (24 * 3600)
local hours = math.floor(seconds / 3600)
seconds = seconds % 3600
local minutes = math.floor(seconds / 60)
seconds = seconds % 60

local timeParts = {}

if days > 0 then
table.insert(timeParts, days .. (days > 1 and " days" or " day"))
end

if hours > 0 then
table.insert(timeParts, hours .. (hours > 1 and " hours" or " hour"))
end

if minutes > 0 then
table.insert(timeParts, minutes .. (minutes > 1 and " minutes" or " minute"))
end

if seconds > 0 or #timeParts == 0 then
table.insert(timeParts, seconds .. (seconds > 1 and " seconds" or " second"))
end

local timeStr = table.concat(timeParts, ", ")
local lastComma = timeStr:find(", [%a%d]+$")
if lastComma then
timeStr = timeStr:sub(1, lastComma - 1) .. " and" .. timeStr:sub(lastComma + 1)
end
return timeStr
end
16 changes: 16 additions & 0 deletions data/libs/functions/string.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,19 @@ end
string.capitalize = function(str)
return str:gsub("%f[%a].", string.upper)
end

function string.toPosition(inputString)
local positionPatterns = {
"{%s*x%s*=%s*(%d+)%s*,%s*y%s*=%s*(%d+)%s*,%s*z%s*=%s*(%d+)%s*}",
"Position%s*%((%d+)%s*,%s*(%d+)%s*,%s*(%d+)%s*%)",
"(%d+)%s*,%s*(%d+)%s*,%s*(%d+)",
}

for _, pattern in ipairs(positionPatterns) do
local posX, posY, posZ = string.match(inputString, pattern)
if posX and posY and posZ then
return Position(tonumber(posX), tonumber(posY), tonumber(posZ))
end
end
return nil
end
6 changes: 3 additions & 3 deletions data/libs/systems/concoctions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function Concoction:init(player, sendMessage)
return
end
eventPlayer:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your concoction " .. name .. " is still active for another " .. duration .. ".")
end, 500, player:getId(), self.name, getTimeInWords(self:timeLeft(player)))
end, 500, player:getId(), self.name, Game.getTimeInWords(self:timeLeft(player)))
end
end

Expand All @@ -180,7 +180,7 @@ function Concoction:activate(player, item)
local cooldown = self:cooldown()
if self:lastActivatedAt(player) + cooldown > os.time() then
local cooldownLeft = self:lastActivatedAt(player) + cooldown - os.time()
player:sendTextMessage(MESSAGE_FAILURE, "You must wait " .. getTimeInWords(cooldownLeft) .. " before using " .. item:getName() .. " again.")
player:sendTextMessage(MESSAGE_FAILURE, "You must wait " .. Game.getTimeInWords(cooldownLeft) .. " before using " .. item:getName() .. " again.")
return true
end
self:timeLeft(player, self:totalDuration())
Expand All @@ -191,7 +191,7 @@ function Concoction:activate(player, item)
self.config.callback(player, self.config)
else
self:addCondition(player)
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have activated " .. item:getName() .. ". It will last for " .. getTimeInWords(self:totalDuration()) .. consumptionString .. ".")
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have activated " .. item:getName() .. ". It will last for " .. Game.getTimeInWords(self:totalDuration()) .. consumptionString .. ".")
if self:tickType() == ConcoctionTickType.Online then
addEvent(tick, updateInterval * 1000, self.id, player:getId(), updateInterval)
end
Expand Down
2 changes: 1 addition & 1 deletion data/scripts/eventcallbacks/player/on_look.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ local function handleCreatureDescription(inspectedThing, lookDistance)
local monsterMaster = inspectedThing:getMaster()
if monsterMaster and table.contains({ "sorcerer familiar", "knight familiar", "druid familiar", "paladin familiar" }, inspectedThing:getName():lower()) then
local summonTimeRemaining = monsterMaster:kv():get("familiar-summon-time") or 0
descriptionText = string.format("%s (Master: %s). It will disappear in %s", descriptionText, monsterMaster:getName(), getTimeInWords(summonTimeRemaining - os.time()))
descriptionText = string.format("%s (Master: %s). It will disappear in %s", descriptionText, monsterMaster:getName(), Game.getTimeInWords(summonTimeRemaining - os.time()))
end
end

Expand Down
24 changes: 15 additions & 9 deletions src/io/fileloader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ class PropStream {
return false;
}

std::span<const unsigned char> sourceSpan(reinterpret_cast<const unsigned char*>(p), sizeof(T));
std::array<unsigned char, sizeof(T)> tempBuffer;
std::ranges::copy(sourceSpan, tempBuffer.begin());
std::span<const char> charSpan { p, sizeof(T) };
auto byteSpan = std::as_bytes(charSpan);

std::array<std::byte, sizeof(T)> tempBuffer;
std::ranges::copy(byteSpan, tempBuffer.begin());

ret = std::bit_cast<T>(tempBuffer);

p += sizeof(T);
Expand All @@ -93,10 +96,11 @@ class PropStream {
return false;
}

std::vector<unsigned char> tempBuffer(strLen);
std::span<const unsigned char> sourceSpan(reinterpret_cast<const unsigned char*>(p), strLen);
std::vector<char> tempBuffer(strLen);
std::span<const char> sourceSpan(p, strLen);
std::ranges::copy(sourceSpan, tempBuffer.begin());
ret.assign(reinterpret_cast<const char*>(tempBuffer.data()), strLen);

ret.assign(tempBuffer.begin(), tempBuffer.end());

p += strLen;

Expand Down Expand Up @@ -136,9 +140,11 @@ class PropWriteStream {

template <typename T>
void write(T add) {
char* addr = reinterpret_cast<char*>(&add);
std::span<const char> sourceSpan(addr, sizeof(T));
std::ranges::copy(sourceSpan, std::back_inserter(buffer));
static_assert(std::is_trivially_copyable_v<T>, "Type T must be trivially copyable");

auto byteArray = std::bit_cast<std::array<char, sizeof(T)>>(add);
std::span<const char> charSpan(byteArray);
std::ranges::copy(charSpan, std::back_inserter(buffer));
}

void writeString(const std::string &str) {
Expand Down
12 changes: 2 additions & 10 deletions src/io/functions/iologindata_load_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -741,10 +741,6 @@ void IOLoginDataLoad::loadPlayerPreyClass(const std::shared_ptr<Player> &player,
query << "SELECT * FROM `player_prey` WHERE `player_id` = " << player->getGUID();
if ((result = db.storeQuery(query.str()))) {
do {
auto selectedRaceId = result->getNumber<uint16_t>("raceid");
if (selectedRaceId == 0) {
continue;
}
auto slot = std::make_unique<PreySlot>(static_cast<PreySlot_t>(result->getNumber<uint16_t>("slot")));
auto state = static_cast<PreyDataState_t>(result->getNumber<uint16_t>("state"));
if (slot->id == PreySlot_Two && state == PreyDataState_Locked) {
Expand All @@ -756,7 +752,7 @@ void IOLoginDataLoad::loadPlayerPreyClass(const std::shared_ptr<Player> &player,
} else {
slot->state = state;
}
slot->selectedRaceId = selectedRaceId;
slot->selectedRaceId = result->getNumber<uint16_t>("raceid");
slot->option = static_cast<PreyOption_t>(result->getNumber<uint16_t>("option"));
slot->bonus = static_cast<PreyBonus_t>(result->getNumber<uint16_t>("bonus_type"));
slot->bonusRarity = static_cast<uint8_t>(result->getNumber<uint16_t>("bonus_rarity"));
Expand Down Expand Up @@ -792,10 +788,6 @@ void IOLoginDataLoad::loadPlayerTaskHuntingClass(const std::shared_ptr<Player> &
query << "SELECT * FROM `player_taskhunt` WHERE `player_id` = " << player->getGUID();
if ((result = db.storeQuery(query.str()))) {
do {
auto selectedRaceId = result->getNumber<uint16_t>("raceid");
if (selectedRaceId == 0) {
continue;
}
auto slot = std::make_unique<TaskHuntingSlot>(static_cast<PreySlot_t>(result->getNumber<uint16_t>("slot")));
auto state = static_cast<PreyTaskDataState_t>(result->getNumber<uint16_t>("state"));
if (slot->id == PreySlot_Two && state == PreyTaskDataState_Locked) {
Expand All @@ -807,7 +799,7 @@ void IOLoginDataLoad::loadPlayerTaskHuntingClass(const std::shared_ptr<Player> &
} else {
slot->state = state;
}
slot->selectedRaceId = selectedRaceId;
slot->selectedRaceId = result->getNumber<uint16_t>("raceid");
slot->upgrade = result->getNumber<bool>("upgrade");
slot->rarity = static_cast<uint8_t>(result->getNumber<uint16_t>("rarity"));
slot->currentKills = result->getNumber<uint16_t>("kills");
Expand Down

0 comments on commit 2979ca2

Please sign in to comment.