local slot = Action()
local config = {
cost = 1000,
baseSpeed = 70,
slowStep = 40,
winChance = 70,
fruits = {2674, 2676, 2680, 2679, 8841, 5097, 2682, 2675, 2683},
rewards = {
{item = 2160, count = 1, chance = 40},
{item = 2152, count = 50, chance = 30},
{item = 2148, count = 100, chance = 20},
{item = 2674, count = 5, chance = 10}
},
positions = {
[6297] = {
{x=2995,y=3114,z=7},
{x=2996,y=3114,z=7},
{x=2997,y=3114,z=7},
},
[6298] = {
{x=2992,y=3116,z=7},
{x=2993,y=3116,z=7},
{x=2994,y=3116,z=7},
}
}
}
local activePlayers = {}
local function randomFruit()
return config.fruits[math.random(#config.fruits)]
end
local function simpleFX(pos)
Position(pos):sendMagicEffect(CONST_ME_MAGIC_BLUE)
end
local function getRandomReward()
local roll = math.random(100)
local current = 0
for _, r in ipairs(config.rewards) do
current = current + r.chance
if roll <= current then
return r
end
end
return config.rewards[1]
end
local function getResult()
local roll = math.random(100)
if roll <= config.winChance then
local f = randomFruit()
return {f, f, f}, nil, "WIN"
end
local a = randomFruit()
local b = randomFruit()
local c = randomFruit()
while a == b and b == c do
b = randomFruit()
c = randomFruit()
end
return {a, b, c}, nil, "LOSE"
end
local function giveReward(playerId, reward, type)
local player = Player(playerId)
if not player then return end
if type == "WIN" then
reward = getRandomReward()
player:addItem(reward.item, reward.count)
player:sendTextMessage(MESSAGE_STATUS_WARNING,
"You won " .. reward.count .. " " .. ItemType(reward.item):getName())
else
player:sendTextMessage(MESSAGE_STATUS_WARNING,
"You lost the slot machine!")
end
Position(player:getPosition()):sendMagicEffect(CONST_ME_FIREWORK_YELLOW)
activePlayers[playerId] = nil
end
local function spinReel(playerId, pos, finalItem, rounds, speed, isLast, reward, type)
local player = Player(playerId)
if not player then return end
local tile = Tile(pos)
if not tile then return end
local item = tile:getTopDownItem()
if item then item:remove() end
simpleFX(pos)
if rounds <= 0 then
Game.createItem(finalItem, 1, pos)
Position(pos):sendMagicEffect(CONST_ME_EXPLOSIONAREA)
if isLast then
addEvent(giveReward, 300, playerId, reward, type)
end
return
end
Game.createItem(randomFruit(), 1, pos)
speed = speed + config.slowStep
addEvent(spinReel, speed, playerId, pos, finalItem, rounds - 1, speed, isLast, reward, type)
end
function slot.onUse(player, item, fromPosition, target, toPosition, isHotkey)
local playerId = player:getId()
if activePlayers[playerId] then
player:sendCancelMessage("Wait until spin ends.")
return true
end
local positions = config.positions[item.uid]
if not positions then
player:sendCancelMessage("Machine not configured.")
return true
end
local money = player:getMoney()
if money < config.cost then
player:sendTextMessage(MESSAGE_STATUS_WARNING,
"You need " .. config.cost .. " gold coins. You only have " .. money .. ".")
return true
end
player:removeMoney(config.cost)
activePlayers[playerId] = true
player:say("-$" .. config.cost, TALKTYPE_MONSTER_SAY)
local result, reward, type = getResult()
spinReel(playerId, positions[1], result[1], 20, config.baseSpeed, false, reward, type)
addEvent(spinReel, 300, playerId, positions[2], result[2], 22, config.baseSpeed, false, reward, type)
addEvent(spinReel, 600, playerId, positions[3], result[3], 24, config.baseSpeed, true, reward, type)
return true
end
for uid, _ in pairs(config.positions) do
slot:uid(uid)
end
slot:register()