local canCreateBossAroundPos = true -- Can we create around the stone position?
local forceBossCreating = true -- If position is not walkable, should we force the boss creating?
local configs = -- Stone configs
{
-- [uniqueId] = { config }
[1261] = { storageId = 1261, bosses = { 'Hellgorak' }, bossPos = Position(100, 200, 7), teleportPos = Position(100, 200, 7) },
[1262] = { storageId = 1262, bosses = { 'Rat', 'Cave Rat' }, allBosses = true, bossPos = Position(100, 200, 7), teleportPos = Position(100, 200, 7) },
[1270] = { checkAllStoneStorages = true, teleportPos = Position(100, 200, 7) } -- Reward config (room if got all stones above)
}
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
local config = configs[item.uid] -- Our stone config is registered at the 'config' variable (not 'configs'!)
-- Stones configs check
-- Config not found
if not config then
return false -- False to send the "Sorry, not possible." message.
end
local checkAllStoneStorages = config.checkAllStoneStorages
-- Should have either checkAllStoneStorages as true or a storageId setted at the stone config
if not checkAllStoneStorages and not config.storageId then
return false -- False to send the "Sorry, not possible." message.
end
-- If has storageId to check
if config.storageId then
-- Check it meaning that the stone has been already taken
if player:getStorageValue(config.storageId) > -1 then
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have already taken this.")
return true -- True to not send the "Sorry, not possible." message.
-- Player has not the stone storage
else
-- Enable the storageId, meaning that the player took this stone
player:setStorageValue(config.storageId, 1)
end
end
-- Stones storage/count check
local stonesTaken = 0
local totalStones = 0
-- ipairs goes from key 1 to N, pairs goes through all table values
-- "_" is the uid, but call as "_" because we are not using this value (it's just nomenclature)
for _, cfg in pairs(configs) do
-- If has storageId to check
if cfg.storageId then
-- It's a stone to check, so increase the total stones
totalStones = totalStones + 1
-- Player has not the stone storage
if player:getStorageValue(cfg.storageId) == -1 then
-- Player does not have 1 of all storages, so we cannot continue
if checkAllStoneStorages then -- Reward config
return false -- False to send the "Sorry, not possible." message.
end
-- Player has the stone storage
else
-- Increase the stonesTaken meaning the player took this stone (just for count how many stones has been taken)
stonesTaken = stonesTaken + 1
end
end
end
-- If has boss to create
if config.bosses then
-- Create all bosses at list
if config.allBosses then
for _, bossName in ipairs(config.bosses) do -- ipairs, and mentioned, goes from key 1 to N
Game.createMonster(bossName, config.bossPos or fromPosition, canCreateBossAroundPos, forceBossCreating)
end
-- Create only 1 boss
else
Game.createMonster(config.bosses[math.random(#config.bosses)], config.bossPos or fromPosition, canCreateBossAroundPos, forceBossCreating)
end
end
-- If has teleportPos
if config.teleportPos then
-- Teleport player
player:teleportTo(config.teleportPos, false) -- False to: if the teleportPos is around player, then don't do the walking movement
end
-- Message to player
if checkAllStoneStorages then -- Reward config
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Congratulations! You got all stones!")
else
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("You got %d of %d stones.", stonesTaken, totalStones))
end
return true
end