• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Using Condition

Allesn

Member
Joined
May 4, 2011
Messages
204
Solutions
1
Reaction score
24
Hello, I am using the following script, and I would like to make a modification: add conditions to change the skill (ax, sword, distance, for example), but I can not imagine how it should start to make the condition work, could you help me? Thank you.

LUA:
local rare_popup = true
local rare_text = "*rare*"
local rare_effect = true
local rare_effect_id = CONST_ME_MAGIC_GREEN

local tiers = {
    [1] = {
        prefix = 'rare',
        showattr = false, -- attr prefix will be shown instead
        extra = {0, 0},
        chance = {
            [1] = 5000, -- chance for basic stat
            [2] = 5000 -- chance for second stat
        }
    },

    [2] = {
        prefix = 'epic',
        showattr = false, -- attr prefix will be shown instead
        extra = {7, 10}, -- additional percent bonus
        chance = {
            [1] = 3333,
            [2] = 25000
        }
    },

    [3] = {
        prefix = 'legendary',
        showattr = false, -- attr prefix will be shown instead
        extra = {10, 15},
        chance = {
            [1] = 1000,
            [2] = 100000 -- 2 bonuses always
        }
    },
}

--! attributes
local attr = {
    atk = {
        name = 'atk',
        prefix = 'sharpened',
        percent = {7, 25},
    },
    def = {
        name = 'def',
        prefix = 'fortified',
        percent = {7, 25},
    },
    extradef = {
        name = 'extra def',
        prefix = 'balanced',
        percent = {7, 25},
    },
    arm = {
        name = 'arm',
        prefix = 'flawless',
        percent = {7, 20},
    },
    hitchance = {
        name = 'accuracy',
        prefix = 'accurate',
        percent = {10, 25},
    },
    shootrange = {
        name = 'range',
        prefix = 'powerful',
        percent = {17, 34},
    },
    charges = {
        name = 'charges',
        prefix = 'charged',
        percent = {30, 45},
    },
    duration = {
        name = 'time',
        prefix = 'unique',
        percent = {35, 50},
    },
}

local stats = {
    [1] = {ITEM_ATTRIBUTE_ATTACK, attr.atk},
    [2] = {ITEM_ATTRIBUTE_DEFENSE, attr.def},
    [3] = {ITEM_ATTRIBUTE_EXTRADEFENSE, attr.extradef},
    [4] = {ITEM_ATTRIBUTE_ARMOR, attr.arm},
    [5] = {ITEM_ATTRIBUTE_HITCHANCE, attr.hitchance},
    [6] = {ITEM_ATTRIBUTE_SHOOTRANGE, attr.shootrange},
    [7] = {ITEM_ATTRIBUTE_CHARGES, attr.charges},
    [8] = {ITEM_ATTRIBUTE_DURATION, attr.duration},
}

function stat_getItemDuration(item)
    local it_id = item:getId()
    local tid = ItemType(it_id):getTransformEquipId()
    if tid > 0 then
        item:transform(tid)
        local vx = item:getAttribute(ITEM_ATTRIBUTE_DURATION)
        item:transform(it_id)
        item:removeAttribute(ITEM_ATTRIBUTE_DURATION)
        return vx
    end
    return 0
end

function loot_attrToVal(item, attr)
    local id = ItemType(item:getId())
    local v = {
        [ITEM_ATTRIBUTE_ATTACK] = id:getAttack(),
        [ITEM_ATTRIBUTE_DEFENSE] = id:getDefense(),
        [ITEM_ATTRIBUTE_EXTRADEFENSE] = id:getExtraDefense(),
        [ITEM_ATTRIBUTE_ARMOR] = id:getArmor(),
        [ITEM_ATTRIBUTE_HITCHANCE] = id:getHitChance(),
        [ITEM_ATTRIBUTE_SHOOTRANGE] = id:getShootRange(),
        [ITEM_ATTRIBUTE_CHARGES] = id:getCharges(),
        [ITEM_ATTRIBUTE_DURATION] = stat_getItemDuration(item),
    }
    return v[attr]
end

function assign_loot_Stat(c)
    local rares = 0
    local h = c:getItemHoldingCount()
    if h > 0 then
        for i = 1, h do
            local available_stats = {}
            local it_u = c:getItem(i - 1)
            local it_id = ItemType(it_u:getId())
            if it_u:isContainer() then
                local crares = assign_loot_Stat(it_u)
                rares = rares + crares
            else
                if not it_id:isStackable() then
                    local wp = it_id:getWeaponType()
                    if wp > 0 then
                        if wp == WEAPON_SHIELD then -- type shield
                            table.insert(available_stats, stats[2])
                        elseif wp == WEAPON_DISTANCE then -- type bow
                            table.insert(available_stats, stats[5])
                            table.insert(available_stats, stats[6])
                        elseif wp == WEAPON_WAND then -- type wand
                            table.insert(available_stats, stats[6])
                        elseif isInArray({WEAPON_SWORD, WEAPON_CLUB, WEAPON_AXE}, wp) then -- melee weapon

                            if it_id:getAttack() > 0 then
                                table.insert(available_stats, stats[1])
                            end
                           
                            if it_id:getDefense() > 0 then
                                table.insert(available_stats, stats[2])
                            end
                           
                            if it_id:getExtraDefense() ~= 0 then
                                table.insert(available_stats, stats[3])
                            end
                        end
                    else -- armors, amulets, runes and rings
                        if it_id:getArmor() > 0 then
                            table.insert(available_stats, stats[4])
                        end

                        if it_id:getCharges() > 0 then
                            table.insert(available_stats, stats[7])
                        end

                        local eq_id = it_id:getTransformEquipId()
                        if eq_id > 0 then
                            table.insert(available_stats, stats[8])
                        end
                    end
                end
            end

            if #available_stats > 0 then
                -- skips it all if it's empty
                local tier = math.random(1, #tiers)
                if #tiers[tier].chance > 0 then
                    local statsStored = 0
                    local stats_used = {}
                    for stat = 1, #tiers[tier].chance do
                        if #available_stats > 0 then
                            -- stops if no more stats available
                            if stat - 1 == statsStored then
                                -- checks when it's time to stop adding stats
                                if math.random(1, 100000) <= tiers[tier].chance[stat] then
                                    statsStored = statsStored + 1

                                    local selected_stat = math.random(1, #available_stats)
                                    table.insert(stats_used, available_stats[selected_stat])
                                    table.remove(available_stats, selected_stat)
                                end
                            end
                        end
                    end

                    if #stats_used > 0 then
                        rares = rares + 1
                        local stat_desc = {}
                        for stat = 1, #stats_used do
                            local v = math.random(
                                stats_used[stat][2].percent[1],
                                stats_used[stat][2].percent[2]
                            ) + math.random(
                                tiers[tier].extra[1],
                                tiers[tier].extra[2]
                            )
                            local basestat = loot_attrToVal(it_u, stats_used[stat][1])
                            it_u:setAttribute(stats_used[stat][1], basestat + math.abs(basestat * v / 100))
                            table.insert(stat_desc, '[' .. stats_used[stat][2].name .. ': +' .. v .. '%]')
                        end

                        if tiers[tier].showattr then
                            for stat = 1, #stats_used do
                                it_u:setAttribute(ITEM_ATTRIBUTE_NAME, "[" .. stats_used[stat][2].prefix .. "]" .. it_u:getAttribute(ITEM_ATTRIBUTE_NAME))
                            end
                            it_u:setAttribute(ITEM_ATTRIBUTE_NAME, it_u:getAttribute(ITEM_ATTRIBUTE_NAME) .. " " .. it_id:getName())
                        else
                            it_u:setAttribute(ITEM_ATTRIBUTE_NAME, tiers[tier].prefix .. " " .. it_id:getName())
                        end

                        it_u:setAttribute(ITEM_ATTRIBUTE_DESCRIPTION, table.concat(stat_desc, "\n"))
                    end
                end
            end
        end
    end
    return rares
end

function find_loot_Container(pos)
    local rares = 0
    local c = Tile(pos):getTopDownItem()
    if c ~= nil then
        if c:isContainer() then
            rares = rares + assign_loot_Stat(c)
            if rares > 0 then
                if rare_popup then
                    local spectators = Game.getSpectators(pos, false, true, 7, 7, 5, 5)
                    -- TODO
                    -- player:say("Wow, a rare item!", TALKTYPE_MONSTER_SAY)
                    for i = 1, #spectators do
                        spectators[i]:say(rare_text, TALKTYPE_MONSTER_SAY, false, spectators[i], pos)
                    end
                end

                if rare_effect then
                    pos:sendMagicEffect(rare_effect_id)
                end
            end
            return true
        end
    end
end

function onKill(player, target, lastHit)
    if (not isSummon(target)) then
        addEvent(find_loot_Container, 2, target:getPosition())
    end
    player:save()
    return true
end

function onLogin(player)
    player:registerEvent("randomstats_loot")
    return true
end
 
This script doesn't affect the player directly it only affects the item a player equips, that is why there are no condition methods added to it.
In order to add a condition to the player you would want to create a base onEquip/DeEquip script that references this script. It is a little bit involved for me to look through at this time but i might be able to steer you in the right direction.

First thing is 1st create a onEquip/DeEquip script and set all the equipment you intend to associate with the conditions I am sure there is one on this forum you can use as a template.
Next write a method which parses the text attribute of the item and looks for specific keywords such as "legendary" or "epic".
Finally only apply the conditions if the item meets the criteria.
 
This script doesn't affect the player directly it only affects the item a player equips, that is why there are no condition methods added to it.
In order to add a condition to the player you would want to create a base onEquip/DeEquip script that references this script. It is a little bit involved for me to look through at this time but i might be able to steer you in the right direction.

First thing is 1st create a onEquip/DeEquip script and set all the equipment you intend to associate with the conditions I am sure there is one on this forum you can use as a template.
Next write a method which parses the text attribute of the item and looks for specific keywords such as "legendary" or "epic".
Finally only apply the conditions if the item meets the criteria.

Is there a way then to change the attribute of skills without having to do all this or without having to use condition?
 
Is there a way then to change the attribute of skills without having to do all this or without having to use condition?
Looks to me like this script takes the base value of the item and increases and decreases the value depending on its "rarity" and then applies it to the item? If this is the case then I would think altering the tiers's extra sub-table and attr's prefix sub-table would yield the desired result.
 
Looks to me like this script takes the base value of the item and increases and decreases the value depending on its "rarity" and then applies it to the item? If this is the case then I would think altering the tiers's extra sub-table and attr's prefix sub-table would yield the desired result.
Okay, I'll see some way to use attr to change the value of the skill. Thanks for helping with my question.
 
Back
Top