From aa797ff039774752951ca5284e434ccb31b2d1e7 Mon Sep 17 00:00:00 2001 From: Marco Date: Mon, 19 Feb 2024 17:31:09 -0300 Subject: [PATCH] refactor: script improvements and remove unused config (#2242) --- config.lua.dist | 2 - .../others/spawns_npc_by_time.lua | 69 ------------------- .../event-raven_herb_bush.lua | 69 +++++++------------ .../world_changes/spawns_npc_by_time.lua | 30 ++++++++ src/config/config_enums.hpp | 1 - src/config/configmanager.cpp | 1 - 6 files changed, 56 insertions(+), 116 deletions(-) delete mode 100644 data-otservbr-global/scripts/globalevents/others/spawns_npc_by_time.lua create mode 100644 data-otservbr-global/scripts/world_changes/spawns_npc_by_time.lua diff --git a/config.lua.dist b/config.lua.dist index e5188d15b65..6831b799152 100644 --- a/config.lua.dist +++ b/config.lua.dist @@ -226,7 +226,6 @@ onlyPremiumAccount = false -- Customs -- NOTE: weatherRain = true, activates weather raining effects -- NOTE: thunderEffect = true, activates thunder effects --- NOTE: allConsoleLog = true, show all message logs -- NOTE: stashMoving = true, stow an container inside your stash -- NOTE: depotChest, the non-stackable items will be moved to the selected depot chest(I - XVIII). -- NOTE: autoBank = true, the dropped coins from monsters will be automatically deposited to your bank account. @@ -241,7 +240,6 @@ onlyPremiumAccount = false -- NOTE: randomMonsterSpawn = true, will enable monsters from the same spawn to be randomized between them, thus making a variable hunt weatherRain = false thunderEffect = false -allConsoleLog = false stashMoving = false depotChest = 4 autoLoot = false diff --git a/data-otservbr-global/scripts/globalevents/others/spawns_npc_by_time.lua b/data-otservbr-global/scripts/globalevents/others/spawns_npc_by_time.lua deleted file mode 100644 index 060c4277be0..00000000000 --- a/data-otservbr-global/scripts/globalevents/others/spawns_npc_by_time.lua +++ /dev/null @@ -1,69 +0,0 @@ --- enum LightState_t --- LIGHT_STATE_DAY, --- LIGHT_STATE_NIGHT, --- LIGHT_STATE_SUNSET, --- LIGHT_STATE_SUNRISE, -local periods = { - [LIGHT_STATE_NIGHT] = "Night", - [LIGHT_STATE_DAY] = "Day", - [LIGHT_STATE_SUNRISE] = "Sunrise", - [LIGHT_STATE_SUNSET] = "Sunset", -} - -local spawns = { - -- spawnByType day / night - [1] = { -- spawn in night - name = "Ghostly Wolf", - spawn = LIGHT_STATE_SUNSET, - despawn = LIGHT_STATE_SUNRISE, - position = { x = 33332, y = 32052, z = 7 }, - }, - [2] = { -- spawn in night - name = "Talila", - spawn = LIGHT_STATE_SUNSET, - despawn = LIGHT_STATE_SUNRISE, - position = { x = 33504, y = 32222, z = 7 }, - }, - [3] = { -- spawn in day - name = "Valindara", - spawn = LIGHT_STATE_SUNRISE, - despawn = LIGHT_STATE_SUNSET, - position = { x = 33504, y = 32222, z = 7 }, - }, -} - -local spawnsByTime = GlobalEvent("spawnsByTime") -function spawnsByTime.onPeriodChange(period, light) - local time = getWorldTime() - - if configManager.getBoolean(configKeys.ALL_CONSOLE_LOG) then - logger.info("Starting {} Current light is {} and it's {} Tibian Time", periods[period], light, getFormattedWorldTime(time)) - end - for index, value in pairs(spawns) do - if value.spawn == period then - -- Adding - local spawn = Game.createNpc(value.name, value.position) - if spawn then - if configManager.getBoolean(configKeys.ALL_CONSOLE_LOG) then - logger.info("NPC {} added", value.name) - end - spawn:setMasterPos(value.position) - spawn:getPosition():sendMagicEffect(CONST_ME_TELEPORT) - end - elseif value.despawn == period then - -- Removing - local target = Npc(value.name) - if target then - if configManager.getBoolean(configKeys.ALL_CONSOLE_LOG) then - logger.info("NPC {} removed", value.name) - end - target:getPosition():sendMagicEffect(CONST_ME_TELEPORT) - target:remove() - end - end - end - - return true -end - -spawnsByTime:register() diff --git a/data-otservbr-global/scripts/quests/threatened_dreams/event-raven_herb_bush.lua b/data-otservbr-global/scripts/quests/threatened_dreams/event-raven_herb_bush.lua index 7f35b07170e..089935aad41 100644 --- a/data-otservbr-global/scripts/quests/threatened_dreams/event-raven_herb_bush.lua +++ b/data-otservbr-global/scripts/quests/threatened_dreams/event-raven_herb_bush.lua @@ -1,74 +1,57 @@ -local ThreatenedDreams = Storage.Quest.U11_40.ThreatenedDreams -local periods = { - [LIGHT_STATE_NIGHT] = "Night", - [LIGHT_STATE_DAY] = "Day", - [LIGHT_STATE_SUNRISE] = "Sunrise", - [LIGHT_STATE_SUNSET] = "Sunset", -} local config = { - -- createByType day / night - [1] = { -- create in night - bushId = 25783, - createItem = LIGHT_STATE_NIGHT, - removeItem = LIGHT_STATE_SUNRISE, - pos = Position(33497, 32196, 7), - herbId = 5953, - herbWeight = 1, - storage = ThreatenedDreams.Mission03.RavenHerbTimer, - }, + bushId = 25783, + createItem = LIGHT_STATE_NIGHT, + removeItem = LIGHT_STATE_SUNRISE, + pos = Position(33497, 32196, 7), + herbId = 5953, + herbWeight = 1, + storage = ThreatenedDreams.Mission03.RavenHerbTimer, } local createRavenHerb = GlobalEvent("createRavenHerb") function createRavenHerb.onPeriodChange(period, light) - local time = getWorldTime() - - if configManager.getBoolean(configKeys.ALL_CONSOLE_LOG) then - logger.info("Starting {} Current light is {} and it's {} Tibian Time", periods[period], light, getFormattedWorldTime(time)) - end - for index, item in pairs(config) do - if item.createItem == period then -- Adding - local createItem = Game.createItem(item.bushId, 1, item.pos) - createItem:setActionId(item.storage) - if createItem then - item.pos:sendMagicEffect(CONST_ME_BIGPLANTS) - end - elseif item.removeItem == period then -- Removing - local target = Tile(item.pos):getItemById(item.bushId) - if target then - item.pos:removeItem(item.bushId, CONST_ME_BIGPLANTS) - end + local pos = config.pos + if config.createItem == period then + local createItem = Game.createItem(config.bushId, 1, pos) + if createItem then + pos:sendMagicEffect(CONST_ME_BIGPLANTS) + end + elseif config.removeItem == period then + local target = Tile(pos):getItemById(config.bushId) + if target then + pos:removeItem(config.bushId, CONST_ME_BIGPLANTS) end end - return true end createRavenHerb:register() local ravenHerb = Action() + function ravenHerb.onUse(player, item, fromPosition, target, toPosition, isHotkey) - local herbConfig = config[1] - local message = "You have found a " .. getItemName(herbConfig.herbId) .. "." + local message = "You have found a " .. getItemName(config.herbId) .. "." local backpack = player:getSlotItem(CONST_SLOT_BACKPACK) if not backpack or backpack:getEmptySlots(true) < 1 then player:sendTextMessage(MESSAGE_EVENT_ADVANCE, message .. " But you have no room to take it.") return true end - if (player:getFreeCapacity() / 100) < herbConfig.herbWeight then - player:sendTextMessage(MESSAGE_EVENT_ADVANCE, message .. ". Weighing " .. herbConfig.herbWeight .. " oz, it is too heavy for you to carry.") + + if (player:getFreeCapacity() / 100) < config.herbWeight then + player:sendTextMessage(MESSAGE_EVENT_ADVANCE, message .. ". Weighing " .. config.herbWeight .. " oz, it is too heavy for you to carry.") return true end - if player:getStorageValue(herbConfig.storage) > os.time() then + if player:getStorageValue(config.storage) > os.time() then player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "The raven herb cannot be collected right now.") return true end - player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have found a " .. getItemName(herbConfig.herbId) .. ".") - player:setStorageValue(herbConfig.storage, os.time() + 60 * 30 * 1000) -- Can be collected on next cycle - player:addItem(herbConfig.herbId, 1) + player:sendTextMessage(MESSAGE_EVENT_ADVANCE, message) + player:setStorageValue(config.storage, os.time() + 60 * 30 * 1000) + player:addItem(config.herbId, 1) return true end diff --git a/data-otservbr-global/scripts/world_changes/spawns_npc_by_time.lua b/data-otservbr-global/scripts/world_changes/spawns_npc_by_time.lua new file mode 100644 index 00000000000..4679804b6a5 --- /dev/null +++ b/data-otservbr-global/scripts/world_changes/spawns_npc_by_time.lua @@ -0,0 +1,30 @@ +local npcSpawns = { + { name = "Ghostly Wolf", spawnPeriod = LIGHT_STATE_SUNSET, despawnPeriod = LIGHT_STATE_SUNRISE, position = { x = 33332, y = 32052, z = 7 } }, + { name = "Talila", spawnPeriod = LIGHT_STATE_SUNSET, despawnPeriod = LIGHT_STATE_SUNRISE, position = { x = 33504, y = 32222, z = 7 } }, + { name = "Valindara", spawnPeriod = LIGHT_STATE_SUNRISE, despawnPeriod = LIGHT_STATE_SUNSET, position = { x = 33504, y = 32222, z = 7 } }, +} + +local spawnsNpcByTime = GlobalEvent("SpawnsNpcByTime") + +function spawnsByTimeEvent.onPeriodChange(period) + for _, npcSpawn in ipairs(npcSpawns) do + if npcSpawn.spawnPeriod == period then + local spawnNpc = Game.createNpc(npcSpawn.name, npcSpawn.position) + if spawnNpc then + spawnNpc:setMasterPos(npcSpawn.position) + npcSpawn.position:sendMagicEffect(CONST_ME_TELEPORT) + logger.info("[NPC Spawn] {} has spawned", npcSpawn.name) + end + elseif npcSpawn.despawnPeriod == period then + local despawnNpc = Npc(npcSpawn.name) + if despawnNpc then + despawnNpc:getPosition():sendMagicEffect(CONST_ME_TELEPORT) + despawnNpc:remove() + logger.info("[NPC Despawn] {} has despawned", npcSpawn.name) + end + end + end + return true +end + +spawnsNpcByTime:register() diff --git a/src/config/config_enums.hpp b/src/config/config_enums.hpp index 7432f955ff9..102352b457e 100644 --- a/src/config/config_enums.hpp +++ b/src/config/config_enums.hpp @@ -17,7 +17,6 @@ enum ConfigKey_t : uint16_t { ALLOW_BLOCK_SPAWN, ALLOW_CHANGEOUTFIT, ALLOW_RELOAD, - ALL_CONSOLE_LOG, AUTH_TYPE, AUTOBANK, AUTOLOOT, diff --git a/src/config/configmanager.cpp b/src/config/configmanager.cpp index 7842d7b4d88..7ca54a2db41 100644 --- a/src/config/configmanager.cpp +++ b/src/config/configmanager.cpp @@ -126,7 +126,6 @@ bool ConfigManager::load() { loadBoolConfig(L, WEATHER_RAIN, "weatherRain", false); loadBoolConfig(L, WEATHER_THUNDER, "thunderEffect", false); - loadBoolConfig(L, ALL_CONSOLE_LOG, "allConsoleLog", false); loadBoolConfig(L, TOGGLE_FREE_QUEST, "toggleFreeQuest", true); loadBoolConfig(L, AUTOLOOT, "autoLoot", false); loadBoolConfig(L, AUTOBANK, "autoBank", false);