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

How to make table from 1 to 10

Dran Ryszard

Member
Joined
Apr 25, 2023
Messages
52
Reaction score
12
Location
Poland
Hi, how do I make a table like that?

Lua:
    local monsterName = {
        [{1,10}] = {name = "Orc"},
        [{11,20}] = {name = "Orc Spearman"}
    }

local monsterType = MonsterType(monsterName[player_skill_level].name)

I tried to spawn monster X if the player has a skill level from X to Y, but I don't know how to make it in the table. With one value in [], it works, but like that, not, so any help?
 
You can do something like that

Lua:
local monsterSkills = {
    [0] = {
        monsters = {
            {
                name = {"Orc", "Rat"},
                amount = 3
            },
            {
                name = {"Bonelord"},
                amount = 2
            }
        },
        skillFrom = 10,
        skillTo = 20,
        skills = {
            SKILL_CLUB,
            SKILL_SWORD,
            SKILL_AXE,
            SKILL_SHIELD
        }
    },
    
    [1] = {
        monsters = {
            {
                name = {"Dragon"},
                amount = 1
            }
        },
        skillFrom = 30,
        skillTo = 40,
        skills = {
            SKILL_MAGLEVEL
        }
    }
}
 
Hi, how do I make a table like that?

Lua:
    local monsterName = {
        [{1,10}] = {name = "Orc"},
        [{11,20}] = {name = "Orc Spearman"}
    }

local monsterType = MonsterType(monsterName[player_skill_level].name)

I tried to spawn monster X if the player has a skill level from X to Y, but I don't know how to make it in the table. With one value in [], it works, but like that, not, so any help?

Lua:
local monstersConfig = { -- Should be organized by ascending skillLevel
  { skillLevel = 10, name = 'Orc' },
  { skillLevel = 20, name = 'Orc Spearman' },
}

-- Find monsterConfig according to playerSkillLevel
local monsterConfig
for i = #monstersConfig, 1, -1 do -- Reverse order
  local _monsterConfig = monstersConfig[i]
  if playerSkillLevel <= _monsterConfig.skillLevel then
    monsterConfig = _monsterConfig
    break
  end
end

if monsterConfig then
  -- Game.createMonster(monsterConfig.name, position) -- See Game.createMonster parameters in luascript.cpp file
end
 
@River KA , I have problem now, because before, when I used [1] = {name = "Orc"}, it worked normally, but when I use your script, now monsters that should be spawned are not summons. I don't know why, because I'm using a clean Utevo Res script.




Something like this line does not work: creature:addSummon(summon)

local summon = Game.createMonster(monsterConfig.name, position, true)
 
@River KA , I have problem now, because before, when I used [1] = {name = "Orc"}, it worked normally, but when I use your script, now monsters that should be spawned are not summons. I don't know why, because I'm using a clean Utevo Res script.




Something like this line does not work: creature:addSummon(summon)

local summon = Game.createMonster(monsterConfig.name, position, true)
Show full script
 
@Levi999x Oh yeah, sorry I forgot.

Lua:
function onCastSpell(creature, variant)
    if creature:getSkull() == SKULL_BLACK then
        creature:sendCancelMessage(RETURNVALUE_NOTPOSSIBLE)
        return false
    end
    local mlvl = creature:getSkillLevel(SKILL_MAGLEVEL)

    local monstersConfig = { -- Should be organized by ascending skillLevel
        { skillLevel = 10, skillLevelMin = 1, name = 'Orc' },
        { skillLevel = 20, skillLevelMin = 11, name = 'Orc Spearman' },
    }

    -- Find monsterConfig according to playerSkillLevel
    local monsterConfig
    for i = #monstersConfig, 1, -1 do -- Reverse order
        local _monsterConfig = monstersConfig[i]
        if mlvl <= _monsterConfig.skillLevel and mlvl >= _monsterConfig.skillLevelMin then
            monsterConfig = _monsterConfig
            break
        end
    end

    if not creature:hasFlag(PlayerFlag_CanSummonAll) then
        if not monsterType:isSummonable() then
            creature:sendCancelMessage(RETURNVALUE_NOTPOSSIBLE)
            creature:getPosition():sendMagicEffect(CONST_ME_POFF)
            return false
        end

        if #creature:getSummons() >= 1 then
            creature:sendCancelMessage("You cannot summon more creatures.")
            creature:getPosition():sendMagicEffect(CONST_ME_POFF)
            return false
        end
    end

    local position = creature:getPosition()
    local summon = Game.createMonster(monsterConfig.name, position, true)
    if not summon then
        creature:sendCancelMessage(RETURNVALUE_NOTENOUGHROOM)
        position:sendMagicEffect(CONST_ME_POFF)
        return false
    end

    creature:addMana(-manaCost)
    creature:addManaSpent(manaCost)
    creature:addSummon(summon)
    summon:setSkull(SKULL_GREEN)
    position:sendMagicEffect(CONST_ME_MORTAREA)
    summon:getPosition():sendMagicEffect(CONST_ME_BIGCLOUDS)
    creature:addSkillTries(SKILL_MAGLEVEL, math.random(5,15))
    return true
end
Post automatically merged:

//Edit Okay, I'm an idiot; I forgot to remove the line with mana.
Code:
creature:addMana(-manaCost)
    creature:addManaSpent(manaCost)
 
With this change, this script should work better.
playerSkillLevel >= _monsterConfig.skillLevel

I think it's the same as <=, because if I have X monsters with better levels, then it spawns the best monster in the table. Same as in the original script, when I have only < =, then with skill 5, I get Orc Spearman, when in the table, Orc = skill value. So I added min value to the script, and it worked well, but I'm not good at scripting, so I don't know.

BTW, does someone know maybe how to make summons teleport to the player? If he changes floors or runs too fast,
TFS 1.5 8.6 by Nekiro
 
No it is not the same. Just add for yourself higher skill than you declared in table, but in your script this will work correct. so nvm.
but if you wanna script like in topic then:

Lua:
local monstersConfig = { -- Should be organized by ascending skillLevel
    [{1, 10} ] = 'Orc',
    [{11, 20}] = 'Orc Spearman',
    --[[
    This will not spawn any monster when you have higher skill level than declared in table.
    So you have to set last key in table like this:
    [{prevSkillLevel[2] + 1, maxSkillLevel] = 'last monster'
    for example maxSkillLevel = 100, prevSkillLevel[2] = 20
    [{21, 100] = 'last monster'
    ]]--
}
 
function onCastSpell(creature, variant)
    if creature:getSkull() == SKULL_BLACK then
        creature:sendCancelMessage(RETURNVALUE_NOTPOSSIBLE)
        return false
    end
 
    local mlvl = creature:getSkillLevel(SKILL_MAGLEVEL)
    -- Find monsterConfig according to playerSkillLevel
    local monsterName
    for skillLevel, name in next, monstersConfig, nil do
        if mlvl >= skillLevel[1] and mlvl <= skillLevel[2] then
            monsterName = name
            break
        end
    end
 
    local position = creature:getPosition()
    if not monsterName then
        creature:sendCancelMessage(RETURNVALUE_NOTPOSSIBLE)
        position:sendMagicEffect(CONST_ME_POFF)
        return false
    end

    if not creature:hasFlag(PlayerFlag_CanSummonAll) then
        if not monsterType:isSummonable() then
            creature:sendCancelMessage(RETURNVALUE_NOTPOSSIBLE)
            position:sendMagicEffect(CONST_ME_POFF)
            return false
        end

        if #creature:getSummons() >= 1 then
            creature:sendCancelMessage("You cannot summon more creatures.")
            position:sendMagicEffect(CONST_ME_POFF)
            return false
        end
    end

    local summon = Game.createMonster(monsterName, position, true)
    if not summon then
        creature:sendCancelMessage(RETURNVALUE_NOTENOUGHROOM)
        position:sendMagicEffect(CONST_ME_POFF)
        return false
    end

    --creature:addMana(-manaCost)
    --creature:addManaSpent(manaCost)
    creature:addSummon(summon)
    creature:addSkillTries(SKILL_MAGLEVEL, math.random(5,15))
    position:sendMagicEffect(CONST_ME_MORTAREA)
    summon:setSkull(SKULL_GREEN)
    summon:getPosition():sendMagicEffect(CONST_ME_BIGCLOUDS)
    return true
end
 
Last edited:
Back
Top