Lua Skill scroll based on used weapon

bybbzan

mapper
Joined
Aug 4, 2012
Messages
795
Solutions
2
Reaction score
113
Location
Sweden
Yo!

I request a skill scroll based on the weapon you are using.
For example a knight using an axe, should get axe fighting if he uses the scroll.
However, if he change to a sword, the scroll should add sword fighting.
Is this possible for 0.4?

Thanks in advance.
 
OP
bybbzan

bybbzan

mapper
Joined
Aug 4, 2012
Messages
795
Solutions
2
Reaction score
113
Location
Sweden
Try this, I guess.
Lua:
local skill_levels_to_give = 1

local function addSkillTriesByPercent(cid, skillid, amount)
    amount = math.floor(amount)
    if amount < 1 then
        return false
    end
    for i = 1, amount do
        local cur_skill = getPlayerSkillLevel(cid, skillid)
        local cur_skill_tries = getPlayerSkillTries(cid, skillid)
        local skill_tries = getPlayerRequiredSkillTries(cid, skillId, cur_skill)
        local skill_tries_next = getPlayerRequiredSkillTries(cid, skillId, cur_skill + 1)
        local skill_tries_next_next = getPlayerRequiredSkillTries(cid, skillId, cur_skill + 2)
    
        local current_percent = (cur_skill_tries - skill_tries) / ((skill_tries_next - skill_tries) / 100)
        local next_skill_tries_to_percent = ((skill_tries_next_next - skill_tries_next) / 100) * current_percent
        local cur_skill_tries_to_next_skill_tries = skill_tries_next - cur_skill_tries
    
        local skill_tries_to_give = math.floor(cur_skill_tries_to_next_skill_tries + next_skill_tries_to_percent)
        doPlayerAddSkillTry(cid, skillid, skill_tries_to_give)
    end
    return true
end

local function getPlayerWeaponType(cid)
    local weapon = getPlayerWeapon(cid, true)
    if weapon and weapon.uid > 0 then
        return getItemWeaponType(weapon.uid)
    end
    return WEAPON_NONE
end

local function getPlayerSkillByWeaponType(weaponType)
    local skill = WEAPON_NONE
    if weaponType == WEAPON_SWORD then
        skill = SKILL_SWORD
    elseif weaponType == WEAPON_AXE then
        skill = SKILL_AXE
    elseif weaponType == WEAPON_CLUB then
        skill = SKILL_CLUB
    elseif weaponType == WEAPON_DISTANCE then
        skill = SKILL_DISTANCE
    end
    return skill
end

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local skill = getPlayerSkillByWeaponType(getPlayerWeaponType(cid))
    if skill == WEAPON_NONE then
        doPlayerSendCancel(cid, "You need to have a weapon equipped.")
        return true
    end

    if addSkillTriesByPercent(cid, skill, skill_levels_to_give) == false then
        doPlayerSendCancel(cid, "An error has occurred. Contact administrator. Error: Xikini-404") -- skill_levels_to_give needs to be a number higher then 0.
        return true
    end

    doPlayerSendTextMessage(cid, TALKTYPE_BROADCAST, "CONGRATULATIONS! You gained ".. skill_levels_to_give .." SKILLS from the SKILL SCROLL!.")
    doRemoveItem(item.uid, 1)
    return true
end
Holy smokes, went from sword fighting 17, to 177 in one click.
 

Xikini

I whore myself out for likes
Support Team
Joined
Nov 17, 2010
Messages
5,403
Solutions
361
Reaction score
3,513
Holy smokes, went from sword fighting 17, to 177 in one click.
Working perfectly then. 😅

ummm, okay guess we need more information.

replace the function with this one, and only use '1' for the local.
local skill_levels_to_give = 1

Show us what shows in console.

Lua:
local function addSkillTriesByPercent(cid, skillid, amount)
    amount = math.floor(amount)
    if amount < 1 then
        return false
    end
    for i = 1, amount do
        local cur_skill = getPlayerSkillLevel(cid, skillid)
        local cur_skill_tries = getPlayerSkillTries(cid, skillid)
        local skill_tries = getPlayerRequiredSkillTries(cid, skillId, cur_skill)
        local skill_tries_next = getPlayerRequiredSkillTries(cid, skillId, cur_skill + 1)
        local skill_tries_next_next = getPlayerRequiredSkillTries(cid, skillId, cur_skill + 2)
      
        local current_percent = (cur_skill_tries - skill_tries) / ((skill_tries_next - skill_tries) / 100)
        local next_skill_tries_to_percent = ((skill_tries_next_next - skill_tries_next) / 100) * current_percent
        local cur_skill_tries_to_next_skill_tries = skill_tries_next - cur_skill_tries
      
        local skill_tries_to_give = math.floor(cur_skill_tries_to_next_skill_tries + next_skill_tries_to_percent)
        print(cur_skill)
        print(cur_skill_tries)
        print(skill_tries)
        print(skill_tries_next)
        print(skill_tries_next_next)
      
        print(current_percent)
        print(next_skill_tries_to_percent)
        print(cur_skill_tries_to_next_skill_tries)
      
        print(skill_tries_to_give)
        doPlayerAddSkillTry(cid, skillid, skill_tries_to_give)
    end
    return true
end
 
OP
bybbzan

bybbzan

mapper
Joined
Aug 4, 2012
Messages
795
Solutions
2
Reaction score
113
Location
Sweden
Working perfectly then. 😅

ummm, okay guess we need more information.

replace the function with this one, and only use '1' for the local.
local skill_levels_to_give = 1

Show us what shows in console.

Lua:
local function addSkillTriesByPercent(cid, skillid, amount)
    amount = math.floor(amount)
    if amount < 1 then
        return false
    end
    for i = 1, amount do
        local cur_skill = getPlayerSkillLevel(cid, skillid)
        local cur_skill_tries = getPlayerSkillTries(cid, skillid)
        local skill_tries = getPlayerRequiredSkillTries(cid, skillId, cur_skill)
        local skill_tries_next = getPlayerRequiredSkillTries(cid, skillId, cur_skill + 1)
        local skill_tries_next_next = getPlayerRequiredSkillTries(cid, skillId, cur_skill + 2)
     
        local current_percent = (cur_skill_tries - skill_tries) / ((skill_tries_next - skill_tries) / 100)
        local next_skill_tries_to_percent = ((skill_tries_next_next - skill_tries_next) / 100) * current_percent
        local cur_skill_tries_to_next_skill_tries = skill_tries_next - cur_skill_tries
     
        local skill_tries_to_give = math.floor(cur_skill_tries_to_next_skill_tries + next_skill_tries_to_percent)
        print(cur_skill)
        print(cur_skill_tries)
        print(skill_tries)
        print(skill_tries_next)
        print(skill_tries_next_next)
     
        print(current_percent)
        print(next_skill_tries_to_percent)
        print(cur_skill_tries_to_next_skill_tries)
     
        print(skill_tries_to_give)
        doPlayerAddSkillTry(cid, skillid, skill_tries_to_give)
    end
    return true
end
Changed the function, and just used it once.
This is the result.

Lua:
23
0
445
534
641
-500
-535
534
-1
 

Xikini

I whore myself out for likes
Support Team
Joined
Nov 17, 2010
Messages
5,403
Solutions
361
Reaction score
3,513
Changed the function, and just used it once.
This is the result.

Lua:
23
0
445
534
641
-500
-535
534
-1
uhhh. lol
So somehow.. you have 0 skill tries. and it messed with the rest of the calculations. xD

okay, I guess we'll add in a check for that...
The only thing I don't know.. is whether or not we need to 'correct' your skill tries, or whether we need to just add additional ones..
I'm going to assume the latter.

soo try this
Lua:
local function addSkillTriesByPercent(cid, skillid, amount)
    amount = math.floor(amount)
    if amount < 1 then
        return false
    end
    for i = 1, amount do
        local cur_skill = getPlayerSkillLevel(cid, skillid)
        local cur_skill_tries = getPlayerSkillTries(cid, skillid)
        local skill_tries = getPlayerRequiredSkillTries(cid, skillId, cur_skill)
        local skill_tries_next = getPlayerRequiredSkillTries(cid, skillId, cur_skill + 1)
        local skill_tries_next_next = getPlayerRequiredSkillTries(cid, skillId, cur_skill + 2)

        if cur_skill_tries < skill_tries then
            print("Skill try correction " .. cur_skill_tries)
            doPlayerAddSkillTry(cid, skillid, skill_tries - cur_skill_tries)
            cur_skill_tries = skill_tries
        end
        
        local current_percent = (cur_skill_tries - skill_tries) / ((skill_tries_next - skill_tries) / 100)
        local next_skill_tries_to_percent = ((skill_tries_next_next - skill_tries_next) / 100) * current_percent
        local cur_skill_tries_to_next_skill_tries = skill_tries_next - cur_skill_tries
        
        local skill_tries_to_give = math.floor(cur_skill_tries_to_next_skill_tries + next_skill_tries_to_percent)
        print(cur_skill)
        print(cur_skill_tries)
        print(skill_tries)
        print(skill_tries_next)
        print(skill_tries_next_next)
        
        print(current_percent)
        print(next_skill_tries_to_percent)
        print(cur_skill_tries_to_next_skill_tries)
        
        print(skill_tries_to_give)
        doPlayerAddSkillTry(cid, skillid, skill_tries_to_give)
    end
    return true
end
 
OP
bybbzan

bybbzan

mapper
Joined
Aug 4, 2012
Messages
795
Solutions
2
Reaction score
113
Location
Sweden
uhhh. lol
So somehow.. you have 0 skill tries. and it messed with the rest of the calculations. xD

okay, I guess we'll add in a check for that...
The only thing I don't know.. is whether or not we need to 'correct' your skill tries, or whether we need to just add additional ones..
I'm going to assume the latter.

soo try this
Lua:
local function addSkillTriesByPercent(cid, skillid, amount)
    amount = math.floor(amount)
    if amount < 1 then
        return false
    end
    for i = 1, amount do
        local cur_skill = getPlayerSkillLevel(cid, skillid)
        local cur_skill_tries = getPlayerSkillTries(cid, skillid)
        local skill_tries = getPlayerRequiredSkillTries(cid, skillId, cur_skill)
        local skill_tries_next = getPlayerRequiredSkillTries(cid, skillId, cur_skill + 1)
        local skill_tries_next_next = getPlayerRequiredSkillTries(cid, skillId, cur_skill + 2)

        if cur_skill_tries < skill_tries then
            print("Skill try correction " .. cur_skill_tries)
            doPlayerAddSkillTry(cid, skillid, skill_tries - cur_skill_tries)
            cur_skill_tries = skill_tries
        end
       
        local current_percent = (cur_skill_tries - skill_tries) / ((skill_tries_next - skill_tries) / 100)
        local next_skill_tries_to_percent = ((skill_tries_next_next - skill_tries_next) / 100) * current_percent
        local cur_skill_tries_to_next_skill_tries = skill_tries_next - cur_skill_tries
       
        local skill_tries_to_give = math.floor(cur_skill_tries_to_next_skill_tries + next_skill_tries_to_percent)
        print(cur_skill)
        print(cur_skill_tries)
        print(skill_tries)
        print(skill_tries_next)
        print(skill_tries_next_next)
       
        print(current_percent)
        print(next_skill_tries_to_percent)
        print(cur_skill_tries_to_next_skill_tries)
       
        print(skill_tries_to_give)
        doPlayerAddSkillTry(cid, skillid, skill_tries_to_give)
    end
    return true
end
We keep on going.. or (you) lol.
Now i got this result:

Lua:
Skill try correction 0
101
668778304
668778304
802534080
963040768
0
0
133755776
133755776
 

Xikini

I whore myself out for likes
Support Team
Joined
Nov 17, 2010
Messages
5,403
Solutions
361
Reaction score
3,513
We keep on going.. or (you) lol.
Now i got this result:

Lua:
Skill try correction 0
101
668778304
668778304
802534080
963040768
0
0
133755776
133755776
I mean, from these prints.. it looks like it worked as intended..
What happened in-game?
 
OP
bybbzan

bybbzan

mapper
Joined
Aug 4, 2012
Messages
795
Solutions
2
Reaction score
113
Location
Sweden
I mean, from these prints.. it looks like it worked as intended..
What happened in-game?

So, i made a new character, with a sword equipped.
This is before i use the scroll
1.PNG
This is when i used the scroll
2.PNG
This is when i used the scroll, again
3.PNG
 

Xikini

I whore myself out for likes
Support Team
Joined
Nov 17, 2010
Messages
5,403
Solutions
361
Reaction score
3,513
After some testing, here's the working code.

The below code will only work properly with skill_rate of 1 inside of config.lua

In OP's case, his skill rate was 35, and it doesn't work with such low numbers (since it's impossible to give less then 1 point at a time.. and each 1 point = 35 on the server with those rates), so we added a skill_level check, for level 50, so that the skill requirement to advance to the next level would have high enough numbers to work accurately.
Lua:
local skill_levels_to_give = 1

local function addSkillTriesByPercent(cid, skillid, amount)
    amount = math.floor(amount)
    if amount < 1 then
        return false
    end
    for i = 1, amount do
        local cur_skill = getPlayerSkillLevel(cid, skillid)
        local cur_skill_tries = getPlayerSkillTries(cid, skillid)
        local skill_tries_next = getPlayerRequiredSkillTries(cid, skillId, cur_skill + 1)
        local skill_tries_next_next = getPlayerRequiredSkillTries(cid, skillId, cur_skill + 2)
        
        local current_percent = (cur_skill_tries / skill_tries_next) * 100
        local next_skill_tries_to_percent = (skill_tries_next_next / 100) * current_percent
        local cur_skill_tries_to_next_skill_tries = skill_tries_next - cur_skill_tries
        
        local skill_tries_to_give = (cur_skill_tries_to_next_skill_tries + next_skill_tries_to_percent) / getConfigInfo('rateSkill')
        doPlayerAddSkillTry(cid, skillid, skill_tries_to_give)
    end
    return true
end

local function getPlayerWeaponType(cid)
    local weapon = getPlayerWeapon(cid, true)
    if weapon and weapon.uid > 0 then
        return getItemWeaponType(weapon.uid)
    end
    return WEAPON_NONE
end

local function getPlayerSkillByWeaponType(weaponType)
    local skill = WEAPON_NONE
    if weaponType == WEAPON_SWORD then
        skill = SKILL_SWORD
    elseif weaponType == WEAPON_AXE then
        skill = SKILL_AXE
    elseif weaponType == WEAPON_CLUB then
        skill = SKILL_CLUB
    elseif weaponType == WEAPON_DISTANCE then
        skill = SKILL_DISTANCE
    end
    return skill
end

function onUse(cid, item, fromPosition, itemEx, toPosition)    
    local skill = getPlayerSkillByWeaponType(getPlayerWeaponType(cid))
    if skill == WEAPON_NONE then
        doPlayerSendCancel(cid, "You need to have a weapon equipped.")
        return true
    end
    
    if addSkillTriesByPercent(cid, skill, skill_levels_to_give) == false then
        doPlayerSendCancel(cid, "An error has occurred. Contact administrator. Error: Xikini-404") -- skill_levels_to_give needs to be a number higher then 0.
        return true
    end
    doPlayerSendTextMessage(cid, TALKTYPE_BROADCAST, "CONGRATULATIONS! You gained ".. skill_levels_to_give .." SKILL" .. (skill_levels_to_give > 1 and "S" or "") .. " from the SKILL SCROLL!.")
    doRemoveItem(item.uid, 1)
    return true
end
 

Shadow_

Veteran OT User
Joined
Jun 2, 2018
Messages
901
Solutions
29
Reaction score
331
local function getPlayerWeaponType(cid) local weapon = getPlayerWeapon(cid, true) if weapon and weapon.uid > 0 then return getItemWeaponType(weapon.uid) end return WEAPON_NONE end local function getPlayerSkillByWeaponType(weaponType) local skill = WEAPON_NONE if weaponType == WEAPON_SWORD then skill = SKILL_SWORD elseif weaponType == WEAPON_AXE then skill = SKILL_AXE elseif weaponType == WEAPON_CLUB then skill = SKILL_CLUB elseif weaponType == WEAPON_DISTANCE then skill = SKILL_DISTANCE end return skill end
you did a great job, but why? maybe
Lua:
local skills = {SKILL_SWORD,SKILL_CLUB,SKILL_AXE,SKILL_DIST}
local skillu = -1
for i, #skills do
        if player:getSkillLevel(skills[i]) > skillu then
            skillu = skills[i]
        end
end

player:addSkillLevel(skillu)
 
Last edited:

Xikini

I whore myself out for likes
Support Team
Joined
Nov 17, 2010
Messages
5,403
Solutions
361
Reaction score
3,513
you did a great job, but why? maybe
Lua:
local skills = {SKILL_SWORD,SKILL_CLUB,SKILL_AXE,SKILL_DIST}
local skillu = nil
for i, #skills do
        if player:getSkillLevel(skills[i]) > -1 then
            skillu = skills[i]
        end
end

player:addSkillLevel(skillu)
I'm all for being proved wrong, but what would your code even accomplish?
Every skill is greater then -1 by default.
 

Shadow_

Veteran OT User
Joined
Jun 2, 2018
Messages
901
Solutions
29
Reaction score
331
I'm all for being proved wrong, but what would your code even accomplish?
Every skill is greater then -1 by default.
sorry, it is maximum technique editted main command, it will add the first one which is 0 then the second one with is 100 (MAIN) the third one is 90 won't add it so you got the main one, because the player might be weilding something for a task or mission or whatever idk i think thats safer
 
Top