• 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!

Lua Changing the values of Knight skills

darkmu

Active Member
Joined
Aug 26, 2007
Messages
253
Solutions
1
Reaction score
45
Location
Paraná,Brazil
Can someone give me a light ..

I'm trying to make a system for TFS 1.3 in which the knights will be able to exchange their strongest skill for another skill.

Example:

I have skill 120 of sword, however I want to switch to AXE, so I will invert take the values of the AXE put in the skill of SWORD and the SWORD put in the AXE.

Lua:
local skills, tableSkills  = {
    [1] = {'Club', function(player) return player:getSkillLevel(SKILL_CLUB) end, bestSkill = false},
    [2] = {'Sword', function(player) return player:getSkillLevel(SKILL_SWORD) end, bestSkill = false},
    [3] = {'Axe', function(player) return player:getSkillLevel(SKILL_AXE) end, bestSkill = false}
}, {}

local function checkStrongSkill(player, tableSkill)
    local skillMaisAlto = 0
    
    for i = 1, #skills do
        if  skills[i][2](player) > skillMaisAlto then
            table.insert(tableSkills, {skillName = skills[i][1], skillNumber = skills[i][2](player), bestSkill = true})           
            skillMaisAlto = skills[i][2](player)
        else
            table.insert(tableSkills, {skillName = skills[i][1], skillNumber = skills[i][2](player), bestSkill = false})
        end
    end   
    
    return tableSkills
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    
    --local check = checkStrongSkill(player, skills)
    
    --print(#check)
    
    return TRUE
    
end
 

Snavy

Bakasta
Senator
Joined
Apr 1, 2012
Messages
1,247
Solutions
71
Reaction score
587
Location
Hell
Anybody can help me?
Tested on 1.3.
This will "swap" top 2 skills.

Credits to @Morrison - reference

lua
Lua:
local skills =  {
    SKILL_CLUB,
    SKILL_SWORD,
    SKILL_AXE
}

local function getPlayerSkills(player, tableSkill)
    local skillTable = {}
    for _, skill in pairs(skills) do
        table.insert(skillTable, {skill, player:getSkillLevel(skill)})
    end

    -- descending order (highest to smallest)
    table.sort(skillTable, function(a, b) return a[2] > b[2] end)
    return skillTable
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local playerSkills = getPlayerSkills(player, skills)
    local firstSkill   = playerSkills[1]
    local secondSkill  = playerSkills[2]

    local diff = firstSkill[2] - secondSkill[2]
    player:setSkillLevel(firstSkill[1], firstSkill[2] - diff)
    player:setSkillLevel(secondSkill[1], secondSkill[2] + diff)
    return true
end

player.cpp
C++:
void Player::setSkillLevel(uint64_t skillType, uint64_t newSkillLevel) {
    skills[skillType].level = newSkillLevel;
    skills[skillType].tries = 0;
    skills[skillType].percent = Player::getPercentLevel(skills[skillType].tries, vocation->getReqSkillTries(skillType, skills[skillType].level));

    sendSkills();
}

player.h
C++:
void setSkillLevel(uint64_t skillType, uint64_t newSkillLevel);

luascript.cpp
C++:
registerMethod("Player", "setSkillLevel", LuaScriptInterface::luaPlayerSetSkillLevel);
C++:
int LuaScriptInterface::luaPlayerSetSkillLevel(lua_State* L)
{
    // player:setSkillLevel()
    Player* player = getUserdata<Player>(L, 1);
    if (player) {
        int64_t skillType = getNumber<int64_t>(L, 2);
        int64_t newSkillLevel = getNumber<int64_t>(L, 3);
        player->setSkillLevel(skillType, newSkillLevel);
        pushBoolean(L, true);
    } else {
        lua_pushnil(L);
    }
    return 1;
}

luascript.h
C++:
static int luaPlayerSetSkillLevel(lua_State* L);
 
Top