Skip to content

Commit

Permalink
Merge branch 'main' into fix_stash_lootpouch
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas authored Nov 23, 2023
2 parents 625dd0a + 2bb39d3 commit d9bf1b4
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 45 deletions.
2 changes: 1 addition & 1 deletion data-canary/scripts/movements/tiles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function tile.onStepIn(creature, item, position, fromPosition)
local depotItem = playerPosition:getTile():getItemByType(ITEM_TYPE_DEPOT)
if depotItem ~= nil then
local depotItems = 0
for id = 1, configManager.getNumber("depotBoxes") do
for id = 1, configManager.getNumber(configKeys.DEPOT_BOXES) do
depotItems = depotItems + player:getDepotChest(id, true):getItemHoldingCount()
end
player:sendTextMessage(MESSAGE_FAILURE, "Your depot contains " .. depotItems .. " item" .. (depotItems > 1 and "s." or ".") .. "\
Expand Down
2 changes: 1 addition & 1 deletion data-otservbr-global/scripts/movements/others/tiles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function tiles.onStepIn(creature, item, position, fromPosition)

if depotItem ~= nil then
local depotItems = 0
for id = 1, configManager.getNumber("depotBoxes") do
for id = 1, configManager.getNumber(configKeys.DEPOT_BOXES) do
depotItems = depotItems + player:getDepotChest(id, true):getItemHoldingCount()
end

Expand Down
22 changes: 7 additions & 15 deletions data/libs/hazard_lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,30 +88,22 @@ function Hazard:getPlayerMaxLevel(player)
local fromStorage = player:getStorageValue(self.storageMax)
return fromStorage <= 0 and 1 or fromStorage
end
local fromKV = player:kv():scoped(self.name):get("maxLevel")
local fromKV = player:kv():scoped(self.name):get("maxLevel") or 1

return fromKV <= 0 and 1 or fromKV
end

function Hazard:levelUp(player)
if self.storageMax and self.storageCurrent then
local current = self:getPlayerCurrentLevel(player)
local max = self:getPlayerMaxLevel(player)
if current == max then
self:setPlayerMaxLevel(player, max + 1)
end
return
end

local current = player:kv(self.name):get("currentLevel")
local max = player:kv(self.name):get("maxLevel")
local current = self:getPlayerCurrentLevel(player)
local max = self:getPlayerMaxLevel(player)
if current == max then
player:kv(self.name):set("maxLevel", max + 1)
self:setPlayerMaxLevel(player, max + 1)
end
end

function Hazard:setPlayerMaxLevel(player, level)
if level > self.maxLevelLevel then
level = self.maxLevelLevel
if level > self.maxLevel then
level = self.maxLevel
end

if self.storageMax then
Expand Down
16 changes: 8 additions & 8 deletions src/game/scheduling/dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ Dispatcher &Dispatcher::getInstance() {
}

void Dispatcher::init() {
updateClock();
UPDATE_OTSYS_TIME();

threadPool.addLoad([this] {
std::unique_lock asyncLock(dummyMutex);

while (!threadPool.getIoContext().stopped()) {
updateClock();
UPDATE_OTSYS_TIME();

executeEvents();
executeScheduledEvents();
Expand Down Expand Up @@ -104,7 +104,7 @@ void Dispatcher::executeScheduledEvents() {
auto it = scheduledTasks.begin();
while (it != scheduledTasks.end()) {
const auto &task = *it;
if (task->getTime() > Task::TIME_NOW) {
if (task->getTime() > OTSYS_TIME()) {
break;
}

Expand Down Expand Up @@ -168,17 +168,17 @@ void Dispatcher::mergeEvents() {
checkPendingTasks();
}

std::chrono::nanoseconds Dispatcher::timeUntilNextScheduledTask() const {
static constexpr auto CHRONO_NANO_0 = std::chrono::nanoseconds(0);
static constexpr auto CHRONO_MILI_MAX = std::chrono::milliseconds::max();
std::chrono::milliseconds Dispatcher::timeUntilNextScheduledTask() const {
constexpr auto CHRONO_0 = std::chrono::milliseconds(0);
constexpr auto CHRONO_MILI_MAX = std::chrono::milliseconds::max();

if (scheduledTasks.empty()) {
return CHRONO_MILI_MAX;
}

const auto &task = *scheduledTasks.begin();
const auto timeRemaining = task->getTime() - Task::TIME_NOW;
return std::max<std::chrono::nanoseconds>(timeRemaining, CHRONO_NANO_0);
const auto timeRemaining = std::chrono::milliseconds(task->getTime() - OTSYS_TIME());
return std::max<std::chrono::milliseconds>(timeRemaining, CHRONO_0);
}

void Dispatcher::addEvent(std::function<void(void)> &&f, std::string_view context, uint32_t expiresAfterMs) {
Expand Down
9 changes: 2 additions & 7 deletions src/game/scheduling/dispatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ enum class DispatcherType : uint8_t {

struct DispatcherContext {
bool isOn() const {
return Task::TIME_NOW != SYSTEM_TIME_ZERO;
return OTSYS_TIME() != 0;
}

bool isGroup(const TaskGroup _group) const {
Expand Down Expand Up @@ -134,11 +134,6 @@ class Dispatcher {
private:
thread_local static DispatcherContext dispacherContext;

// Update Time Cache
static void updateClock() {
Task::TIME_NOW = std::chrono::system_clock::now();
}

const auto &getThreadTask() const {
return threads[ThreadPool::getThreadId()];
}
Expand All @@ -159,7 +154,7 @@ class Dispatcher {

inline void executeSerialEvents(std::vector<Task> &tasks);
inline void executeParallelEvents(std::vector<Task> &tasks, const uint8_t groupId);
inline std::chrono::nanoseconds timeUntilNextScheduledTask() const;
inline std::chrono::milliseconds timeUntilNextScheduledTask() const;

inline void checkPendingTasks() {
hasPendingTasks = false;
Expand Down
2 changes: 0 additions & 2 deletions src/game/scheduling/task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include "pch.hpp"
#include "task.hpp"
#include "lib/logging/log_with_spd_log.hpp"

std::chrono::system_clock::time_point Task::TIME_NOW = SYSTEM_TIME_ZERO;
std::atomic_uint_fast64_t Task::LAST_EVENT_ID = 0;

bool Task::execute() const {
Expand Down
16 changes: 6 additions & 10 deletions src/game/scheduling/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,15 @@
#include "utils/tools.hpp"
#include <unordered_set>

static constexpr auto SYSTEM_TIME_ZERO = std::chrono::system_clock::time_point(std::chrono::milliseconds(0));

class Task {
public:
static std::chrono::system_clock::time_point TIME_NOW;

Task(uint32_t expiresAfterMs, std::function<void(void)> &&f, std::string_view context) :
func(std::move(f)), context(context), utime(TIME_NOW), expiration(expiresAfterMs > 0 ? TIME_NOW + std::chrono::milliseconds(expiresAfterMs) : SYSTEM_TIME_ZERO) {
func(std::move(f)), context(context), utime(OTSYS_TIME()), expiration(expiresAfterMs > 0 ? OTSYS_TIME() + expiresAfterMs : 0) {
assert(!this->context.empty() && "Context cannot be empty!");
}

Task(std::function<void(void)> &&f, std::string_view context, uint32_t delay, bool cycle = false, bool log = true) :
func(std::move(f)), context(context), utime(TIME_NOW + std::chrono::milliseconds(delay)), delay(delay), cycle(cycle), log(log) {
func(std::move(f)), context(context), utime(OTSYS_TIME() + delay), delay(delay), cycle(cycle), log(log) {
assert(!this->context.empty() && "Context cannot be empty!");
}

Expand Down Expand Up @@ -54,7 +50,7 @@ class Task {
}

bool hasExpired() const {
return expiration != SYSTEM_TIME_ZERO && expiration < TIME_NOW;
return expiration != 0 && expiration < OTSYS_TIME();
}

bool isCycle() const {
Expand All @@ -75,7 +71,7 @@ class Task {
static std::atomic_uint_fast64_t LAST_EVENT_ID;

void updateTime() {
utime = TIME_NOW + std::chrono::milliseconds(delay);
utime = OTSYS_TIME() + delay;
}

bool hasTraceableContext() const {
Expand Down Expand Up @@ -117,8 +113,8 @@ class Task {
std::function<void(void)> func = nullptr;
std::string_view context;

std::chrono::system_clock::time_point utime = SYSTEM_TIME_ZERO;
std::chrono::system_clock::time_point expiration = SYSTEM_TIME_ZERO;
int64_t utime = 0;
int64_t expiration = 0;

uint64_t id = 0;
uint32_t delay = 0;
Expand Down
7 changes: 6 additions & 1 deletion src/utils/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1454,8 +1454,13 @@ const char* getReturnMessage(ReturnValue value) {
}
}

int64_t OTSYSTIME = 0;
void UPDATE_OTSYS_TIME() {
OTSYSTIME = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}

int64_t OTSYS_TIME() {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
return OTSYSTIME;
}

SpellGroup_t stringToSpellGroup(const std::string &value) {
Expand Down
1 change: 1 addition & 0 deletions src/utils/tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ bool isCaskItem(uint16_t itemId);
std::string getObjectCategoryName(ObjectCategory_t category);

int64_t OTSYS_TIME();
void UPDATE_OTSYS_TIME();

SpellGroup_t stringToSpellGroup(const std::string &value);

Expand Down

0 comments on commit d9bf1b4

Please sign in to comment.