• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Action .::1-Row Slot Machine::. TFS [0.3/0.4/1.0/1.2]

hodleo

Formerly cbrm -Crypto enthusiast, Retired scripter
Staff member
Global Moderator
Joined
Jan 6, 2009
Messages
6,598
Solutions
3
Reaction score
955
Location
Caribbean Sea
A script someone requested me, just a slot machine. Tested in TFS 0.3.6, TFS 0.4 rev3884, TFS 1.0 and recently TFS 1.2

Video (available in HD):

Default Fruits:
lCbgq9mQDC.png


Default Prizes Combos:
Visually ilustrated
bMbkthqsZr.png


Code form
Code:
WIN = {
-- [FRUITS] = {PRIZE,#PRIZE}]
    --MIXED COMBINATIONS
        [{CHERRY,PUMPKIN,CHERRY}] = {2160,2},
        [{LEMON,MELON,LEMON}] = {2160,1},
    --TRIPLE COMBINATIONS
        [{BERRY,BERRY,BERRY}] = {2152,80},
        [{MANGO,MANGO,MANGO}] = {2152,60},
        [{PUMPKIN,PUMPKIN,PUMPKIN}] = {2152,80},
        [{MELON,MELON,MELON}] = {2152,50},
        [{BANANA,BANANA,BANANA}] = {2152,40},
        [{LEMON,LEMON,LEMON}] = {2152,25},
        [{CHERRY,CHERRY,CHERRY}] = {2152,20},
        [{ORANGE,ORANGE,ORANGE}] = {2152,30},
        [{APPLE,APPLE,APPLE}] = {2152,10},
    --ANY COMBINATIONS
        [{ANY,PUMPKIN,PUMPKIN}] = {2152,5},
        [{PUMPKIN,PUMPKIN,ANY}] = {2152,5},
        [{PUMPKIN,ANY,PUMPKIN}] = {2152,10},
        [{ANY,CHERRY,CHERRY}] = {2152,4},
        [{CHERRY,CHERRY,ANY}] = {2152,4},
        [{CHERRY,ANY,CHERRY}] = {2152,8},
        [{ANY,LEMON,LEMON}] = {2152,5},
        [{LEMON,LEMON,ANY}] = {2152,5},
        [{LEMON,ANY,LEMON}] = {2152,5},
    },

Action IDs used
TFS 0.3/0.4 (1, 2, 3, 4)
TFS 1.0/1.2 (100, 101, 102, 103, 104)

Information:
*All thrown items to the slot tiles will be erased to avoid cheating and debug
*Once player pulls lever he won't be able to move from his position until he finishes the game
*If player doesn't pull the lever the slot machine will do it (configurable SETUP.LIMIT)
*Player can't pull the lever diagonal to it, try to map the slot machine(s) like in sample image
*If player disappears, slot machine resets itself (TFS 0.3)

Script Setup:
NOTE: If you use TFS 1.0, you have to add a missing line at data/global.lua

Find function Position.getNextPosition, replace it with:
Code:
function Position.getNextPosition(self, direction, steps)
    steps = steps or 1
    if direction == WEST then
        self.x = self.x - steps
    elseif direction == EAST then
        self.x = self.x + steps
    elseif direction == NORTH then
        self.y = self.y - steps
    elseif direction == SOUTH then
        self.y = self.y + steps
    end
    return self
end

If you have TFS 1.2, add the missing line at data/lib/core/position.lua
Find function Position.getNextPosition, replace it with:
Code:
function Position:getNextPosition(direction, steps)
    local offset = Position.directionOffset[direction]
    if offset then
        steps = steps or 1
        self.x = self.x + offset.x * steps
        self.y = self.y + offset.y * steps
    end
    return self
end

TFS 1.2 requires you to compile player method onMove:
https://github.com/otland/forgottenserver/commit/55dba115fc4f351af9416b06d9711ed8680933be

data/actions/actions.xml
tfs 0.3/0.4

Code:
<action uniqueid="6297-6301" event="script" value="slot.lua"/>
tfs 1.0/1.2

Code:
<action fromuid="6297" touid="6301" script="slot.lua"/>


data/actions/scripts/slot.lua
tfs 0.3/0.4

Code:
--[[
    .::1-Row Slot Machine::.
          for TFS 0.3/4
      by Cybermaster (cbrm)
]]--

function Position(x, y, z, stackpos)
    local position = {x = x, y = y, z = z}
    if stackpos then
        position.stackpos = stackpos
    end
    return position
end

--    1. FRUITS
TEMP, FRUITS = {
    ['APPLE'] = 2674,    ['BANANA'] = 2676,        ['BERRY'] = 2680,
    ['CHERRY'] = 2679,   ['LEMON'] = 8841,        ['MANGO'] = 5097,
    ['MELON'] = 2682,    ['ORANGE'] = 2675,        ['PUMPKIN'] = 2683
}, {}

for name, id in pairs(TEMP) do
    _G[name] = id
    table.insert(FRUITS, id)
end

--    2. CONFIGURATION
SETUP = {
    MONEY = 1000, --REQUIRED MONEY(gp) TO PLAY SLOT MACHINE
    TIME = 200, --MILLISECONDS TO SWITCH FRUITS
    LIMIT = 20, --COUNTER TO STOP CHANGING FRUIT IF PLAYER DOESN'T (decreases each SETUP.TIME)
    LEVER = {9825, 9826},
    WIN = {
    -- [FRUITS] = {PRIZE,#PRIZE}]
        --MIXED COMBINATIONS
            [{CHERRY,PUMPKIN,CHERRY}] = {2160,2},
            [{LEMON,MELON,LEMON}] = {2160,1},
        --TRIPLE COMBINATIONS
            [{BERRY,BERRY,BERRY}] = {2152,80},
            [{MANGO,MANGO,MANGO}] = {2152,60},
            [{PUMPKIN,PUMPKIN,PUMPKIN}] = {2152,80},
            [{MELON,MELON,MELON}] = {2152,50},
            [{BANANA,BANANA,BANANA}] = {2152,40},
            [{LEMON,LEMON,LEMON}] = {2152,25},
            [{CHERRY,CHERRY,CHERRY}] = {2152,20},
            [{ORANGE,ORANGE,ORANGE}] = {2152,30},
            [{APPLE,APPLE,APPLE}] = {2152,10},
        --ANY COMBINATIONS
            [{ANY,PUMPKIN,PUMPKIN}] = {2152,5},
            [{PUMPKIN,PUMPKIN,ANY}] = {2152,5},
            [{PUMPKIN,ANY,PUMPKIN}] = {2152,10},
            [{ANY,CHERRY,CHERRY}] = {2152,4},
            [{CHERRY,CHERRY,ANY}] = {2152,4},
            [{CHERRY,ANY,CHERRY}] = {2152,8},
            [{ANY,LEMON,LEMON}] = {2152,5},
            [{LEMON,LEMON,ANY}] = {2152,5},
            [{LEMON,ANY,LEMON}] = {2152,5},
        },
    MSG = {'Bingo!','Lucky!','Jackpot!','Win!'},
    POS = {    --[LEVER.UNIQUEID] = {direction to row, distance from lever to row, position of lever}
        [6297] = {direction = SOUTH, distance = 2, position = Position(831, 977, 7)},
        [6298] = {direction = SOUTH, distance = 2, position = Position(835, 977, 7)},
        [6299] = {direction = EAST, distance = 2, position = Position(852, 961, 7)},
        [6300] = {direction = NORTH, distance = 2, position = Position(846, 959, 7)},
        [6301] = {direction = WEST, distance = 2, position = Position(842, 961, 7)},
    },
}

for lever, row in pairs(SETUP.POS) do
    local position = getPositionByDirection(row.position, row.direction, row.distance)
    for tile = 0, 2 do
        if row.direction % 2 == 0 then
            SETUP.POS[lever][tile+1] = Position(position.x+tile, position.y, position.z, 1)
        else
            SETUP.POS[lever][tile+1] = Position(position.x, position.y+tile, position.z, 1)
        end
    end
end

--    3. FUNCTIONS
function switchLever(lev)
    return doTransformItem(lev.uid, lev.itemid == SETUP.LEVER[1] and SETUP.LEVER[2] or SETUP.LEVER[1])
end

function verifyRow(cid, pos)
    local result = false
    for combo, profit in pairs(SETUP.WIN) do
        if (getTileItemById(pos[1], combo[1]).uid > 0) or (combo[1] == ANY) then
            if (getTileItemById(pos[2], combo[2]).uid > 0) or (combo[2] == ANY) then
                if (getTileItemById(pos[3], combo[3]).uid > 0) or (combo[3] == ANY) then
                    result = true
                    doPlayerAddItem(cid, profit[1], profit[2] or 1, true)
                    doSendAnimatedText(getThingPos(cid), SETUP.MSG[math.random(#SETUP.MSG)], 66)
                    doPlayerSendTextMessage(cid, MESSAGE_STATUS_WARNING, 'Congratulations!! You won ' .. profit[2] .. ' ' .. getItemPluralNameById(profit[1]) ..'!')
                    break
                end
            end
        end
    end

    for tile = 1, 3 do
        doRemoveItem(getTileThingByPos(pos[tile]).uid)
        doSendMagicEffect(pos[tile], result and CONST_ME_GIFT_WRAPS or CONST_ME_EXPLOSIONHIT)
    end

    return not result and doPlayerSendTextMessage(cid, MESSAGE_STATUS_WARNING, 'You have lost in the Slot Machine :( Try again')
end

--    4. SCRIPT
function onUse(cid, item, fromPosition, itemEx, toPosition)

    if getDirectionTo(getThingPos(cid), fromPosition) > 3 then
        return true
    end

    local function getLever()
        return getTileThingByPos(fromPosition)
    end

    local function doFruit(pos, id, limit)
        if not isPlayer(cid) then
            doItemEraseAttribute(item.uid, 'aid')
            for tile = 1, 3 do
                if getTileThingByPos(pos[tile]).uid > 0 then
                    doRemoveItem(getTileThingByPos(pos[tile]).uid)
                end
            end
            return true
        end

        if getTileThingByPos(pos[id]).itemid < 1 then
            doSendMagicEffect(pos[id], CONST_ME_POFF)
            doCreateItem(FRUITS[math.random(#FRUITS)], 1, pos[id])
        else
            doTransformItem(getTileThingByPos(pos[id]).uid, FRUITS[math.random(#FRUITS)])
        end

        if limit < 1 then
            doSendMagicEffect(pos[id], math.random(28, 30))
            doTransformItem(getTileThingByPos(pos[id]).uid, FRUITS[math.random(#FRUITS)])
            doItemSetAttribute(getLever().uid, 'aid', getLever().actionid+1)
            switchLever(getLever())
        elseif getLever().actionid > id then
            doSendMagicEffect(pos[id], math.random(28, 30))
            doTransformItem(getTileThingByPos(pos[id]).uid, FRUITS[math.random(#FRUITS)])
        else
            addEvent(doFruit, SETUP.TIME, pos, id, limit-1)
        end
    end

    if item.actionid == 0 then
        if not doPlayerRemoveMoney(cid, SETUP.MONEY) then
            return doPlayerSendTextMessage(cid, MESSAGE_STATUS_WARNING, 'You need ' .. SETUP.MONEY ..' gps to play Slot Machine.')
        end
        doItemSetAttribute(item.uid, 'aid', 1)
        doCreatureSetNoMove(cid, true)
        switchLever(item)
        doSendAnimatedText(getThingPos(cid), '-$' .. SETUP.MONEY, 180)
        for tile = 1, 3 do
            doFruit(SETUP.POS[item.uid], tile, tile*SETUP.LIMIT)
        end
    elseif isInArray({1,2,3}, item.actionid) then
        doItemSetAttribute(item.uid, 'aid', item.actionid+1)
        switchLever(item)
    elseif item.actionid == 4 then
        switchLever(item)
        doCreatureSetNoMove(cid, false)
        verifyRow(cid, SETUP.POS[item.uid])
        doItemEraseAttribute(item.uid, 'aid')
    end
    return true
end
 
Last edited:
tfs 1.0
Code:
--[[
    .::1-Row Slot Machine::.
          For TFS 1.0
      by Cybermaster (cbrm)
]]--

--    1. FRUITS
TEMP, FRUITS = {
    ['APPLE'] = 2674,    ['BANANA'] = 2676,    ['BERRY'] = 2680,
    ['CHERRY'] = 2679,    ['LEMON'] = 8841,    ['MANGO'] = 5097,
    ['MELON'] = 2682,    ['ORANGE'] = 2675,    ['PUMPKIN'] = 2683
}, {}

for name, id in pairs(TEMP) do
    _G[name] = id
    table.insert(FRUITS, id)
end

--    2. CONFIGURATION
SETUP = {
    MONEY = 1000, --REQUIRED MONEY(gp) TO PLAY SLOT MACHINE
    TIME = 200, --MILLISECONDS TO SWITCH FRUITS
    LIMIT = 20, --COUNTER TO STOP CHANGING FRUIT IF PLAYER DOESN'T (decreases each SETUP.TIME)
    LEVER = {1945, 1946},
    WIN = {
    -- [FRUITS] = {PRIZE,#PRIZE}]
        --MIXED COMBINATIONS
            [{CHERRY,PUMPKIN,CHERRY}] = {2160,2},
            [{LEMON,MELON,LEMON}] = {2160,1},
        --TRIPLE COMBINATIONS
            [{BERRY,BERRY,BERRY}] = {2152,80},
            [{MANGO,MANGO,MANGO}] = {2152,60},
            [{PUMPKIN,PUMPKIN,PUMPKIN}] = {2152,80},
            [{MELON,MELON,MELON}] = {2152,50},
            [{BANANA,BANANA,BANANA}] = {2152,40},
            [{LEMON,LEMON,LEMON}] = {2152,25},
            [{CHERRY,CHERRY,CHERRY}] = {2152,20},
            [{ORANGE,ORANGE,ORANGE}] = {2152,30},
            [{APPLE,APPLE,APPLE}] = {2152,10},
        --ANY COMBINATIONS
            [{ANY,PUMPKIN,PUMPKIN}] = {2152,5},
            [{PUMPKIN,PUMPKIN,ANY}] = {2152,5},
            [{PUMPKIN,ANY,PUMPKIN}] = {2152,10},
            [{ANY,CHERRY,CHERRY}] = {2152,4},
            [{CHERRY,CHERRY,ANY}] = {2152,4},
            [{CHERRY,ANY,CHERRY}] = {2152,8},
            [{ANY,LEMON,LEMON}] = {2152,5},
            [{LEMON,LEMON,ANY}] = {2152,5},
            [{LEMON,ANY,LEMON}] = {2152,5},
        },
    MSG = {'Bingo!','Lucky!','Jackpot!','Win!'},
    POS = {    --[LEVER.UNIQUEID] = {direction to row, distance from lever to row, position of lever}
        [6297] = {direction = SOUTH, distance = 2, pos = Position(116, 329, 7)},
        [6298] = {direction = EAST, distance = 2, pos = Position(120, 325, 7)},
        [6299] = {direction = NORTH, distance = 2, pos = Position(116, 323, 7)},
        [6300] = {direction = WEST, distance = 2, pos = Position(114, 325, 7)},
    },
}

for lever, row in pairs(SETUP.POS) do
    local position = Position.getNextPosition(row.pos, row.direction, row.distance)
    for tile = 0, 2 do
        if row.direction % 2 == 0 then
            SETUP.POS[lever][tile+101] = Position(position.x+tile, position.y, position.z, 1)
        else
            SETUP.POS[lever][tile+101] = Position(position.x, position.y+tile, position.z, 1)
        end
    end
end

--    3. FUNCTIONS
function switchLever(lev)
    return doTransformItem(lev.uid, lev.itemid == SETUP.LEVER[1] and SETUP.LEVER[2] or SETUP.LEVER[1])
end

function choose(...)
    local arg, ret = {...}
    if type(arg[1]) == 'table' then
        ret = arg[1][math.random(#arg[1])]
    else
        ret = arg[math.random(#arg)]
    end
    return ret
end

function verifyRow(cid, pos)
    local result = FALSE
    for combo, profit in pairs(SETUP.WIN) do
        if (getTileItemById(pos[101], combo[1]).uid > 0) or (combo[1] == ANY) then
            if (getTileItemById(pos[102], combo[2]).uid > 0) or (combo[2] == ANY) then
                if (getTileItemById(pos[103], combo[3]).uid > 0) or (combo[3] == ANY) then
                    result = TRUE
                    doPlayerAddItem(cid, profit[1], profit[2] or 1, TRUE)
                    doCreatureSay(cid, choose(SETUP.MSG), TALKTYPE_ORANGE_1)
                    doPlayerSendTextMessage(cid, MESSAGE_STATUS_WARNING, 'Congratulations!! You won ' .. profit[2] .. ' ' .. getItemDescriptions(profit[1]).plural ..'!')
                    break
                end
            end
        end
    end

    for tile = 101, 103 do
        doRemoveItem(getTileThingByPos(pos[tile]).uid)
        doSendMagicEffect(pos[tile], result and CONST_ME_GIFT_WRAPS or CONST_ME_EXPLOSIONHIT)
    end

    return not result and doPlayerSendTextMessage(cid, MESSAGE_STATUS_WARNING, 'You have lost in the Slot Machine :( Try again')
end

function getDirectionTo(pos1, pos2)
    local dir = SOUTH
    if(pos1.x > pos2.x) then
        dir = WEST
        if(pos1.y > pos2.y) then
            dir = NORTHWEST
        elseif(pos1.y < pos2.y) then
            dir = SOUTHWEST
        end
    elseif(pos1.x < pos2.x) then
        dir = EAST
        if(pos1.y > pos2.y) then
            dir = NORTHEAST
        elseif(pos1.y < pos2.y) then
            dir = SOUTHEAST
        end
    elseif(pos1.y > pos2.y) then
        dir = NORTH
    elseif(pos1.y < pos2.y) then
        dir = SOUTH
    end

    return dir
end

--    4. SCRIPT
function onUse(cid, item, fromPosition, itemEx, toPosition)

    if getDirectionTo(getThingPos(cid), fromPosition) > 3 then
        return TRUE
    end

    local function getLever()
        return getTileThingByPos(fromPosition)
    end

    if item.actionid == 0 then
        doSetItemActionId(item.uid, 100)
    end

    local function doFruit(pos, id, limit)
        if not isPlayer(cid) then
            doSetItemActionId(item.uid, 100)
            for tile = 100, 103 do
                if getTileThingByPos(pos[tile]).uid > 0 then
                    doRemoveItem(getTileThingByPos(pos[tile]).uid)
                end
            end
            return TRUE
        end
   
        if getTileThingByPos(pos[id]).itemid < 1 then
            doSendMagicEffect(pos[id], CONST_ME_POFF)
            local fruit = doCreateItemEx(choose(FRUITS))
            doSetItemActionId(fruit, 100)
            doTileAddItemEx(pos[id], fruit)
        else
            doTransformItem(getTileThingByPos(pos[id]).uid, choose(FRUITS))
        end
   
        if limit < 1 then
            doSendMagicEffect(pos[id], math.random(28, 30))
            doTransformItem(getTileThingByPos(pos[id]).uid, choose(FRUITS))
            doSetItemActionId(getLever().uid, getLever().actionid+1)
            switchLever(getLever())
        elseif getLever().actionid > id then
            doSendMagicEffect(pos[id], math.random(28, 30))
            doTransformItem(getTileThingByPos(pos[id]).uid, choose(FRUITS))
        else
            addEvent(doFruit, SETUP.TIME, pos, id, limit-1)
        end
    end

    if item.actionid == 100 then
        if not doPlayerRemoveMoney(cid, SETUP.MONEY) then
            return doPlayerSendTextMessage(cid, MESSAGE_STATUS_WARNING, 'You need ' .. SETUP.MONEY ..' gps to play Slot Machine.')
        end
   
        doSetItemActionId(item.uid, 101)
        mayNotMove(cid, TRUE)
        switchLever(item)
        doCreatureSay(cid, '-$' .. SETUP.MONEY, TALKTYPE_ORANGE_1)
        for tile = 101, 103 do
            doFruit(SETUP.POS[item.uid], tile, (tile-100)*SETUP.LIMIT)
        end
    elseif isInArray({101,102,103}, item.actionid) then
        doSetItemActionId(item.uid, item.actionid+1)
        switchLever(item)
    elseif item.actionid == 104 then
        switchLever(item)
        mayNotMove(cid, FALSE)
        verifyRow(cid, SETUP.POS[item.uid])
        doSetItemActionId(item.uid, 100)
    end
    return TRUE
end

tfs 1.2
Code:
--[[
    .::1-Row Slot Machine::.
          For TFS 1.2
      by Cybermaster (cbrm)
]]--

--    1. FRUITS
TEMP, FRUITS = {
    ['APPLE'] = 2674,    ['BANANA'] = 2676,    ['BERRY'] = 2680,
    ['CHERRY'] = 2679,    ['LEMON'] = 8841,    ['MANGO'] = 5097,
    ['MELON'] = 2682,    ['ORANGE'] = 2675,    ['PUMPKIN'] = 2683
}, {}

for name, id in pairs(TEMP) do
    _G[name] = id
    table.insert(FRUITS, id)
end

--    2. CONFIGURATION
SETUP = {
    MONEY = 1000, --REQUIRED MONEY(gp) TO PLAY SLOT MACHINE
    TIME = 200, --MILLISECONDS TO SWITCH FRUITS
    LIMIT = 20, --COUNTER TO STOP CHANGING FRUIT IF PLAYER DOESN'T (decreases each SETUP.TIME)
    LEVER = {6297, 6298},
    WIN = {
    -- [FRUITS] = {PRIZE,#PRIZE}]
        --MIXED COMBINATIONS
            [{CHERRY,PUMPKIN,CHERRY}] = {2160,2},
            [{LEMON,MELON,LEMON}] = {2160,1},
        --TRIPLE COMBINATIONS
            [{BERRY,BERRY,BERRY}] = {2152,80},
            [{MANGO,MANGO,MANGO}] = {2152,60},
            [{PUMPKIN,PUMPKIN,PUMPKIN}] = {2152,80},
            [{MELON,MELON,MELON}] = {2152,50},
            [{BANANA,BANANA,BANANA}] = {2152,40},
            [{LEMON,LEMON,LEMON}] = {2152,25},
            [{CHERRY,CHERRY,CHERRY}] = {2152,20},
            [{ORANGE,ORANGE,ORANGE}] = {2152,30},
            [{APPLE,APPLE,APPLE}] = {2152,10},
        --ANY COMBINATIONS
            [{ANY,PUMPKIN,PUMPKIN}] = {2152,5},
            [{PUMPKIN,PUMPKIN,ANY}] = {2152,5},
            [{PUMPKIN,ANY,PUMPKIN}] = {2152,10},
            [{ANY,CHERRY,CHERRY}] = {2152,4},
            [{CHERRY,CHERRY,ANY}] = {2152,4},
            [{CHERRY,ANY,CHERRY}] = {2152,8},
            [{ANY,LEMON,LEMON}] = {2152,5},
            [{LEMON,LEMON,ANY}] = {2152,5},
            [{LEMON,ANY,LEMON}] = {2152,5},
        },
    MSG = {'Bingo!','Lucky!','Jackpot!','Win!'},
    POS = {    --[LEVER.UNIQUEID] = {direction to row, distance from lever to row, position of lever}
        [6297] = {direction = SOUTH, distance = 2, pos = Position(97, 83, 7)},
        [6299] = {direction = EAST, distance = 2, pos = Position(105, 78, 7)},
        [6300] = {direction = NORTH, distance = 2, pos = Position(99, 76, 7)},
        [6301] = {direction = WEST, distance = 2, pos = Position(95, 78, 7)},
    },
}

for lever, row in pairs(SETUP.POS) do 
    local position = row.pos:getNextPosition(row.direction, row.distance)
    for tile = 0, 2 do
        if row.direction % 2 == 0 then
            SETUP.POS[lever][tile+101] = Position(position.x+tile, position.y, position.z, 1)
        else
            SETUP.POS[lever][tile+101] = Position(position.x, position.y+tile, position.z, 1)
        end
    end
end

--    3. FUNCTIONS
MAY_NOT_MOVE = 20155
function mayNotMove(cid, bool)
    Player(cid):setStorageValue(MAY_NOT_MOVE, bool and 1 or -1)
end

function choose(...)
    local arg, ret = {...}
    if type(arg[1]) == 'table' then
        ret = arg[1][math.random(#arg[1])]
    else
        ret = arg[math.random(#arg)]
    end
    return ret
end

local function switchLever(lev) 
    return doTransformItem(lev.uid, lev.itemid == SETUP.LEVER[1] and SETUP.LEVER[2] or SETUP.LEVER[1])
end

local function verifyRow(cid, pos)
    local result = false
    for combo, profit in pairs(SETUP.WIN) do
        if (getTileItemById(pos[101], combo[1]).uid > 0) or (combo[1] == ANY) then
            if (getTileItemById(pos[102], combo[2]).uid > 0) or (combo[2] == ANY) then
                if (getTileItemById(pos[103], combo[3]).uid > 0) or (combo[3] == ANY) then
                    result = true
                    doPlayerAddItem(cid, profit[1], profit[2] or 1, true)
                    doCreatureSay(cid, choose(SETUP.MSG), TALKTYPE_ORANGE_1)
                    doPlayerSendTextMessage(cid, MESSAGE_STATUS_WARNING, 'Congratulations!! You won ' .. profit[2] .. ' ' .. getItemDescriptions(profit[1]).plural ..'!')
                    break
                end
            end
        end
    end

    for tile = 101, 103 do
        doRemoveItem(getTileThingByPos(pos[tile]).uid)
        doSendMagicEffect(pos[tile], result and CONST_ME_GIFT_WRAPS or CONST_ME_EXPLOSIONHIT)
    end

    return not result and doPlayerSendTextMessage(cid, MESSAGE_STATUS_WARNING, 'You have lost in the Slot Machine :( Try again')
end

local function getDirectionTo(pos1, pos2)
    local dir = SOUTH
    if(pos1.x > pos2.x) then
        dir = WEST
        if(pos1.y > pos2.y) then
            dir = NORTHWEST
        elseif(pos1.y < pos2.y) then
            dir = SOUTHWEST
        end
    elseif(pos1.x < pos2.x) then
        dir = EAST
        if(pos1.y > pos2.y) then
            dir = NORTHEAST
        elseif(pos1.y < pos2.y) then
            dir = SOUTHEAST
        end
    elseif(pos1.y > pos2.y) then
        dir = NORTH
    elseif(pos1.y < pos2.y) then
        dir = SOUTH
    end
    return dir
end

--    4. SCRIPT
function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    if getDirectionTo(player:getPosition(), fromPosition) > 3 then
        return true
    end
 
      local pid = player:getId()
    local function getLever()
        for _, id in ipairs(SETUP.LEVER) do
            local lever = getTileItemById(fromPosition, id)
            if lever.uid > 0 then
                return lever
            end
        end      
    end
 
    if item.actionid == 0 then
        doSetItemActionId(item.uid, 100)
    end
    local function doFruit(pos, id, limit)
        if not player:isPlayer() then
            doSetItemActionId(item.uid, 100)
            for tile = 100, 103 do
                if getTileThingByPos(pos[tile]).uid > 0 then
                    doRemoveItem(getTileThingByPos(pos[tile]).uid)
                end
            end
            return true
        end
     
        if getTileThingByPos(pos[id]).itemid < 1 then
            doSendMagicEffect(pos[id], CONST_ME_POFF)
            local fruit = doCreateItemEx(choose(FRUITS))
            doSetItemActionId(fruit, 100)
            doTileAddItemEx(pos[id], fruit)
        else
            doTransformItem(getTileThingByPos(pos[id]).uid, choose(FRUITS))
        end
     
        if limit < 1 then
            doSendMagicEffect(pos[id], math.random(28, 30))
            doTransformItem(getTileThingByPos(pos[id]).uid, choose(FRUITS))
            switchLever(getLever())
            doSetItemActionId(getLever().uid, getLever().actionid+1)
        elseif getLever().actionid > id then
            doSendMagicEffect(pos[id], math.random(28, 30))
            doTransformItem(getTileThingByPos(pos[id]).uid, choose(FRUITS))
        else
            addEvent(doFruit, SETUP.TIME, pos, id, limit-1)
        end
    end

    if item.actionid == 100 then      
        if not player:removeMoney(SETUP.MONEY) then
            return player:sendTextMessage(MESSAGE_STATUS_WARNING, 'You need ' .. SETUP.MONEY ..' gps to play Slot Machine.')
        end
     
        doSetItemActionId(item.uid, 101)
        mayNotMove(pid, true)
        switchLever(item)
        player:say('-$' .. SETUP.MONEY, TALKTYPE_ORANGE_1)
        for tile = 101, 103 do
            doFruit(SETUP.POS[item.uid], tile, (tile-100)*SETUP.LIMIT)
        end
    elseif isInArray({101,102,103}, item.actionid) then
        switchLever(item)
        doSetItemActionId(item.uid, item.actionid+1)
    elseif item.actionid == 104 then
        switchLever(item)
        mayNotMove(pid, false)
        verifyRow(pid, SETUP.POS[item.uid])
        doSetItemActionId(item.uid, 100)
    end
    return true
end

data/movements/movements.xml
tfs 0.3/0.4

Code:
<movevent type="AddItem" tileitem="1" actionid="6577" event="script" value="slot.lua"/>
tfs 1.0
Code:
<movevent event="AddItem" tileitem="1" actionid="6577" script="slot.lua"/>


data/movements/scripts/slot.lua
tfs 0.3/0.4

Code:
function onAddItem(moveItem, tileItem, position, cid)
    if isPlayer(cid) then
        doRemoveItem(moveItem.uid)
        doSendMagicEffect(position, CONST_ME_EXPLOSIONHIT)
    end
end
tfs 1.0
Code:
function onAddItem(moveitem, tileitem, position)
    if not isInArray({100,101,102,103,104}, moveitem.actionid) then
        doRemoveItem(moveitem.uid)
        doSendMagicEffect(position, CONST_ME_POFF)
    end
    return TRUE
end

tfs 1.2
1. Add @data/creaturescripts/login.lua
Code:
player:setStorageValue(MAY_NOT_MOVE, -1)

2. Make sure player methods onMoveItem and onMove are enabled:

data/events/events.xml
Code:
<event class="Player" method="onMoveItem" enabled="1" />
<event class="Player" method="onMove" enabled="1"/>

3. Then edit those two player methods
data/events/scripts/player.lua
Code:
function Player:onMoveItem(item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    local tile = Tile(toPosition)
    if tile and tile:getGround() and (tile:getGround().actionid == 6577) then
        self:sendCancelMessage('Sorry, not possible.')
        return false
    end
    return true
end

function Player:onMove(direction)
    if self:getStorageValue(MAY_NOT_MOVE) == 1 then
        return false
    end
    return true
end

Map Configuration:
Use this sample to guide yourself to set up your slot machines depending on the direction.
Don't forget to set actionid 6577 to the ground tiles of slot rows and lever!
lpXykPg.png
 
Last edited:
~Any item thrown to the slot tiles will be deleted to avoid cheating or bugging.

Put some walls and a door and make a UniqueID where it teleports to the tile when clicking the door, just to prevents other ppl throwing items at the slot.
oyqDSymeFQ.png


oh and if someone is trying to get in while theres a player inside, a msg like "Theres already someone using this slot machine"
 
actually i made it because the same player using the slot machine could be the one throwing the items to cheat
no walls or doors required, just map the slot machines stands like in the last picture
other players outside cannot kick or move player while using slot machine, so no need of all that what you say :p
 
So, when we 're playing and use the lever just stop in the fruit or 've to wait the machine automatically chooses ?
sorry sux english x.x
 
So, when we 're playing and use the lever just stop in the fruit or 've to wait the machine automatically chooses ?
sorry sux english x.x
actually both
if player doesn't pull lever to stop the fruits in 7 seconds(default time) each one will be automatically stopping(lever changes alone too :ninja: like a ghost playing the slot machine:p)

you mean out action id on the stone table or the ground [both are under the lever]
@.@

to the ground, or else to the stone table, it will work anyways
 
nice i made one very similar to this for a request, i used the dice instead of fruit, where each dice has a random chance of appearing. also mine lacked the "animated rolling" which i thought was a very nice touch. i like the script rep ++ the one i made has 5x reels and i was looking to add 3x reels too so ill add this one and use both. nice release
 
Can you tell me the exact win/payout percentage of this? I have a lua 3x3 slot machine based off a real slot machine with 98%~ payout (incl. jackpot).. I'm too lazy to do the math for this one :)
 
Can you tell me the exact win/payout percentage of this? I have a lua 3x3 slot machine based off a real slot machine with 98%~ payout (incl. jackpot).. I'm too lazy to do the math for this one :)
that depends on:

1) # fruits in machine (default is 9)
lCbgq9mQDC.png

Lua:
--FRUITS THAT WILL RANDOMLY APPEAR AND SWITCH
local fruits = {2674,2675,2676,2679,2680,2682,2683,5097,8841}

if you add more fruits % reduces

2) # of combos (by default there are 20 combos)

bMbkthqsZr.png

Lua:
--PRIZES TABLES
local win = {
-- [{FRUIT.1,FRUIT.2,FRUIT.3} = {PRIZE,#PRIZE}]
    --MIXED COMBOS
    [{2679,2683,2679}] = {2160,2},  -- cherry-pumpkin-cherry
    [{8841,2682,8841}] = {2160,1},  -- lemon-melon-lemon
    --TRIPLE COMBOS
    [{2680,2680,2680}] = {2152,80}, -- triple strawberry
    [{5097,5097,5097}] = {2152,60}, -- triple mango
    [{2683,2683,2683}] = {2152,80}, -- triple pumpkin
    [{2682,2682,2682}] = {2152,50}, -- triple melon
    [{2676,2676,2676}] = {2152,40}, -- triple banana
    [{8841,8841,8841}] = {2152,25}, -- triple lemon
    [{2679,2679,2679}] = {2152,20}, -- triple cherry
    [{2675,2675,2675}] = {2152,30}, -- triple orange
    [{2674,2674,2674}] = {2152,10}, -- triple apple
    --ANY COMBOS
    [{ANY,2683,2683}] = {2152,5}, -- double pumpkin right
    [{2683,2683,ANY}] = {2152,5}, -- double pumpkin left
    [{2683,ANY,2683}] = {2152,10}, -- pumpkin sides combo
    [{ANY,2679,2679}] = {2152,4}, -- double cherry right
    [{2679,2679,ANY}] = {2152,4}, -- double cherry left
    [{2679,ANY,2679}] = {2152,8}, -- cherry sides combo
    [{ANY,8841,8841}] = {2152,5}, -- double lemon right
    [{8841,8841,ANY}] = {2152,5}, -- double lemon left
    [{8841,ANY,8841}] = {2152,5}, -- lemon sides combo
}
if you add all possible combos to lua script you know it'll be 100%, player wins in any try

so, by default luck is low(i don't have the exact % for u now, many fruits :p), user should add more combos
 
i dont know what happens it doesnt work for me it doesnt create the fruts :/, im using TFS 0.3.6pl1
please help )'; i rly liked it )';
 
i'll test it at 0.3.6 now to tell u results...as I scripted it in tfs 0.4
 
Another question, i 've liked too much the script and wanna post in OTBR, can i do this ?
Yeah, of course, i 'll put your credits.
 
CyberM Got Anything I g2g in about 10min )';
Worked perfectly in TFS 0.3.6pl1, perhaps you are doing something wrong, check the instructions from main post carefully.
In any case just PM me to see the bug, must be smth easy to fix since you are misconfiguring something.

@thread i've added an additional explanation I forgot, about how to configure the positions of each row in the script
 
This is pretty cool, I like the idea.
 
Back
Top