• 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 Monster of the day (Group)

DukeeH

Active Member
Joined
Dec 6, 2010
Messages
550
Solutions
3
Reaction score
38
Hello, I'm using TFS 0.3.7/OTX2.

I'm trying to change a code by @buchaLL

He made a monster of the day code, but on my server I have a few dragon lords, I want that if the monster of the day is dragon lord, it affects all the ones in the group.
I wasn't able to use a table for this.

Monster Picker, would be a globalevent at midnight:
Lua:
math.randomseed(os.time())
local boostedMonstersList = {"dragon lord", "frost dragon", "hydra", "warlock", "medusa", "grim reaper", "demon"}
local randomMonster = math.random(#boostedMonstersList)

setGlobalStorageValue(BOOSTED_MONSTER, boostedMonstersList[randomMonster])
setGlobalStorageValue(BOOSTED_EXP_BONUS, math.random(20, 50))
setGlobalStorageValue(BOOSTED_LOOT_BONUS, math.random(20, 50))

    local spawn = {x = 250, y = 195, z = 14}  --  monster spawn position
    doCreateMonster(boostedMonstersList[randomMonster], spawn)
    print("Today's boosted monster is: " .. boostedMonstersList[randomMonster])

Lua:
local ignoredList = {1987}
function addBonusLoot(position, name)
    for i = 0, 255 do
        position.stackpos = i
        corpse = getTileThingByPos(position)
        if corpse.uid > 0 and isCorpse(corpse.uid) then
            break
        end
    end
    local newRate = (1 + (getGlobalStorageValue(12382)/100)) * getConfigValue("rateLoot")
    local monsterLoot = getMonsterLootList(name)
    local mainbp = doCreateItemEx(1987, 1)
    for i, loot in pairs(monsterLoot) do
        if math.random(1, 100000) <= newRate * loot.chance then
            if #ignoredList > 0 then
                if (not isInArray(ignoredList, loot.id)) then
                    doAddContainerItem(mainbp, loot.id, loot.countmax and math.random(1, loot.countmax) or 1)
                end
            else
                doAddContainerItem(mainbp, loot.id, loot.countmax and math.random(1, loot.countmax) or 1)
            end
        end
        doAddContainerItemEx(corpse.uid, mainbp)
    end
end

boostedMonstersListEx = {
[1] = {"dragon lord", "dragon lord vip", "fast dlord", "hard dlord"},
[2] = {"frost dragon", "frost dragon vip", "fast fdragon", "hard fdragon"},
[3] = {"hydra", "hydra vip", "fast hydra", "hard hydra"},
[4] = {"warlock", "warlock vip", "fast warlock", "hard warlock"},
[5] = {"medusa", "medusa vip", "fast medusa", "hard medusa"},
[6] = {"grim reaper", "grim reaper vip", "fast greaper", "hard greaper"},
[6] = {"demon", "demon vip", "fast demon", "hard demon"}
}

function onDeath(cid, corpse, deathList)
local BOOSTED_MONSTER = getGlobalStorageValue(12380)
local BOOSTED_EXP_BONUS = getGlobalStorageValue(12381)
local master = getCreatureMaster(cid)
  if (master and master ~= cid) then
    return true
  end

  local curMonster = getCreatureName(cid):lower()

    if curMonster == BOOSTED_MONSTER then
        for i = 1, #deathList do
            local bonusExp =  getMonsterInfo(getCreatureName(cid)).experience/i * getPlayerRates(deathList[i])[SKILL__LEVEL] * getExperienceStage(getPlayerLevel(deathList[i])) * BOOSTED_EXP_BONUS/100
            
            doPlayerAddExperience(deathList[i], bonusExp)
            doSendAnimatedText(getPlayerPosition(deathList[i]), bonusExp, 215)

            addEvent(addBonusLoot, 10, getCreaturePosition(cid), getCreatureName(cid))
            
        end
    end
    return true
end

And this is the creaturescript, but what I need is that, if the storage is "dragon lord" it works for all that dragon lords on the group.
How to do it to use table? I've tried to add some prints but I couldn't.
And another thing is that sometimes it is working, and sometimes don't.
If I print bonusexp, it works okay, but I kill the monster and it doesn't give the exp and the animated text sometimes.

Thanks!
 
Solution
Hello, I'm using TFS 0.3.7/OTX2.

I'm trying to change a code by @buchaLL

He made a monster of the day code, but on my server I have a few dragon lords, I want that if the monster of the day is dragon lord, it affects all the ones in the group.
I wasn't able to use a table for this.

Monster Picker, would be a globalevent at midnight:
Lua:
math.randomseed(os.time())
local boostedMonstersList = {"dragon lord", "frost dragon", "hydra", "warlock", "medusa", "grim reaper", "demon"}
local randomMonster = math.random(#boostedMonstersList)

setGlobalStorageValue(BOOSTED_MONSTER, boostedMonstersList[randomMonster])
setGlobalStorageValue(BOOSTED_EXP_BONUS, math.random(20, 50))
setGlobalStorageValue(BOOSTED_LOOT_BONUS, math.random(20, 50))...

Mummrik

Hey!
Joined
Oct 22, 2007
Messages
707
Solutions
28
Reaction score
126
Location
Sweden
Hello, I'm using TFS 0.3.7/OTX2.

I'm trying to change a code by @buchaLL

He made a monster of the day code, but on my server I have a few dragon lords, I want that if the monster of the day is dragon lord, it affects all the ones in the group.
I wasn't able to use a table for this.

Monster Picker, would be a globalevent at midnight:
Lua:
math.randomseed(os.time())
local boostedMonstersList = {"dragon lord", "frost dragon", "hydra", "warlock", "medusa", "grim reaper", "demon"}
local randomMonster = math.random(#boostedMonstersList)

setGlobalStorageValue(BOOSTED_MONSTER, boostedMonstersList[randomMonster])
setGlobalStorageValue(BOOSTED_EXP_BONUS, math.random(20, 50))
setGlobalStorageValue(BOOSTED_LOOT_BONUS, math.random(20, 50))

    local spawn = {x = 250, y = 195, z = 14}  --  monster spawn position
    doCreateMonster(boostedMonstersList[randomMonster], spawn)
    print("Today's boosted monster is: " .. boostedMonstersList[randomMonster])

Lua:
local ignoredList = {1987}
function addBonusLoot(position, name)
    for i = 0, 255 do
        position.stackpos = i
        corpse = getTileThingByPos(position)
        if corpse.uid > 0 and isCorpse(corpse.uid) then
            break
        end
    end
    local newRate = (1 + (getGlobalStorageValue(12382)/100)) * getConfigValue("rateLoot")
    local monsterLoot = getMonsterLootList(name)
    local mainbp = doCreateItemEx(1987, 1)
    for i, loot in pairs(monsterLoot) do
        if math.random(1, 100000) <= newRate * loot.chance then
            if #ignoredList > 0 then
                if (not isInArray(ignoredList, loot.id)) then
                    doAddContainerItem(mainbp, loot.id, loot.countmax and math.random(1, loot.countmax) or 1)
                end
            else
                doAddContainerItem(mainbp, loot.id, loot.countmax and math.random(1, loot.countmax) or 1)
            end
        end
        doAddContainerItemEx(corpse.uid, mainbp)
    end
end

boostedMonstersListEx = {
[1] = {"dragon lord", "dragon lord vip", "fast dlord", "hard dlord"},
[2] = {"frost dragon", "frost dragon vip", "fast fdragon", "hard fdragon"},
[3] = {"hydra", "hydra vip", "fast hydra", "hard hydra"},
[4] = {"warlock", "warlock vip", "fast warlock", "hard warlock"},
[5] = {"medusa", "medusa vip", "fast medusa", "hard medusa"},
[6] = {"grim reaper", "grim reaper vip", "fast greaper", "hard greaper"},
[6] = {"demon", "demon vip", "fast demon", "hard demon"}
}

function onDeath(cid, corpse, deathList)
local BOOSTED_MONSTER = getGlobalStorageValue(12380)
local BOOSTED_EXP_BONUS = getGlobalStorageValue(12381)
local master = getCreatureMaster(cid)
  if (master and master ~= cid) then
    return true
  end

  local curMonster = getCreatureName(cid):lower()

    if curMonster == BOOSTED_MONSTER then
        for i = 1, #deathList do
            local bonusExp =  getMonsterInfo(getCreatureName(cid)).experience/i * getPlayerRates(deathList[i])[SKILL__LEVEL] * getExperienceStage(getPlayerLevel(deathList[i])) * BOOSTED_EXP_BONUS/100
 
            doPlayerAddExperience(deathList[i], bonusExp)
            doSendAnimatedText(getPlayerPosition(deathList[i]), bonusExp, 215)

            addEvent(addBonusLoot, 10, getCreaturePosition(cid), getCreatureName(cid))
 
        end
    end
    return true
end

And this is the creaturescript, but what I need is that, if the storage is "dragon lord" it works for all that dragon lords on the group.
How to do it to use table? I've tried to add some prints but I couldn't.
And another thing is that sometimes it is working, and sometimes don't.
If I print bonusexp, it works okay, but I kill the monster and it doesn't give the exp and the animated text sometimes.

Thanks!

First of not sure if you should use onDeath instead use onPrepareDeath (if it exists)

Then you could add something like this to lua libs (isInArray(array, element) does exist in TFS use that instead)

Lua:
function table.contains(table, element)
  for _, value in pairs(table) do
    if value == element then
      return true
    end
  end
  return false
end

To check if the monster exists in your boosted monster table you would call it this way

Lua:
boostedMonstersListEx = {
[1] = {"dragon lord", "dragon lord vip", "fast dlord", "hard dlord"},
[2] = {"frost dragon", "frost dragon vip", "fast fdragon", "hard fdragon"},
[3] = {"hydra", "hydra vip", "fast hydra", "hard hydra"},
[4] = {"warlock", "warlock vip", "fast warlock", "hard warlock"},
[5] = {"medusa", "medusa vip", "fast medusa", "hard medusa"},
[6] = {"grim reaper", "grim reaper vip", "fast greaper", "hard greaper"},
[6] = {"demon", "demon vip", "fast demon", "hard demon"}
}

-- Make sure to not use hard coded values when you implement in your code
if table.contains(boostedMonstersListEx[1], "dragon lord vip") then
    print("You killed a boosted dragon lord")
end

-- This is also a valid way, and is the more correct one. Whitout the need to add functions to your libs
if isInArray(boostedMonstersListEx[1], "dragon lord vip") then
    print("You killed a boosted dragon lord")
end

Edit:
Full implementation to your script, notice I did make the table.contains() a local function in your script.
That should go to your lua libs instead so you could use it in other scripts instead of implementing a local function in every script that need that function
Lua:
local ignoredList = {1987}
function addBonusLoot(position, name)
    for i = 0, 255 do
        position.stackpos = i
        corpse = getTileThingByPos(position)
        if corpse.uid > 0 and isCorpse(corpse.uid) then
            break
        end
    end
    local newRate = (1 + (getGlobalStorageValue(12382)/100)) * getConfigValue("rateLoot")
    local monsterLoot = getMonsterLootList(name)
    local mainbp = doCreateItemEx(1987, 1)
    for i, loot in pairs(monsterLoot) do
        if math.random(1, 100000) <= newRate * loot.chance then
            if #ignoredList > 0 then
                if (not isInArray(ignoredList, loot.id)) then
                    doAddContainerItem(mainbp, loot.id, loot.countmax and math.random(1, loot.countmax) or 1)
                end
            else
                doAddContainerItem(mainbp, loot.id, loot.countmax and math.random(1, loot.countmax) or 1)
            end
        end
        doAddContainerItemEx(corpse.uid, mainbp)
    end
end

boostedMonstersListEx = {
[1] = {"dragon lord", "dragon lord vip", "fast dlord", "hard dlord"},
[2] = {"frost dragon", "frost dragon vip", "fast fdragon", "hard fdragon"},
[3] = {"hydra", "hydra vip", "fast hydra", "hard hydra"},
[4] = {"warlock", "warlock vip", "fast warlock", "hard warlock"},
[5] = {"medusa", "medusa vip", "fast medusa", "hard medusa"},
[6] = {"grim reaper", "grim reaper vip", "fast greaper", "hard greaper"},
[6] = {"demon", "demon vip", "fast demon", "hard demon"}
}

-- Add this code to lua libs instead, dont forget to remove local when adding to libs
local function table.contains(table, element)
  for _, value in pairs(table) do
    if value == element then
      return true
    end
  end
  return false
end
-- Add above code to lua libs instead

function onDeath(cid, corpse, deathList)
local BOOSTED_MONSTER = getGlobalStorageValue(12380)

local master = getCreatureMaster(cid)
  if (master and master ~= cid) then
    return true
  end

  local curMonster = getCreatureName(cid):lower()
    if table.contains(boostedMonstersListEx[BOOSTED_MONSTER], curMonster) then
        local BOOSTED_EXP_BONUS = getGlobalStorageValue(12381)
        for i = 1, #deathList do
            local bonusExp =  getMonsterInfo(getCreatureName(cid)).experience/i * getPlayerRates(deathList[i])[SKILL__LEVEL] * getExperienceStage(getPlayerLevel(deathList[i])) * BOOSTED_EXP_BONUS/100
       
            doPlayerAddExperience(deathList[i], bonusExp)
            doSendAnimatedText(getPlayerPosition(deathList[i]), bonusExp, 215)

            addEvent(addBonusLoot, 10, getCreaturePosition(cid), getCreatureName(cid))
       
        end
    end
    return true
end

Edit2:
isInArray(array, element) is the same as the local table.contains(table, element) function, so you could just change the if statement to use isInArray(boostedMonstersListEx[BOOSTED_MONSTER], curMonster) instead. I knew there was something implemented inside TFS i just couldn't remember it. anyway I wont update the code example since someone might learn from it :)
 
Last edited:
Solution
OP
OP
DukeeH

DukeeH

Active Member
Joined
Dec 6, 2010
Messages
550
Solutions
3
Reaction score
38
First of not sure if you should use onDeath instead use onPrepareDeath (if it exists)

Then you could add something like this to lua libs (isInArray(array, element) does exist in TFS use that instead)

Lua:
function table.contains(table, element)
  for _, value in pairs(table) do
    if value == element then
      return true
    end
  end
  return false
end

To check if the monster exists in your boosted monster table you would call it this way

Lua:
boostedMonstersListEx = {
[1] = {"dragon lord", "dragon lord vip", "fast dlord", "hard dlord"},
[2] = {"frost dragon", "frost dragon vip", "fast fdragon", "hard fdragon"},
[3] = {"hydra", "hydra vip", "fast hydra", "hard hydra"},
[4] = {"warlock", "warlock vip", "fast warlock", "hard warlock"},
[5] = {"medusa", "medusa vip", "fast medusa", "hard medusa"},
[6] = {"grim reaper", "grim reaper vip", "fast greaper", "hard greaper"},
[6] = {"demon", "demon vip", "fast demon", "hard demon"}
}

-- Make sure to not use hard coded values when you implement in your code
if table.contains(boostedMonstersListEx[1], "dragon lord vip") then
    print("You killed a boosted dragon lord")
end

-- This is also a valid way, and is the more correct one. Whitout the need to add functions to your libs
if isInArray(boostedMonstersListEx[1], "dragon lord vip") then
    print("You killed a boosted dragon lord")
end

Edit:
Full implementation to your script, notice I did make the table.contains() a local function in your script.
That should go to your lua libs instead so you could use it in other scripts instead of implementing a local function in every script that need that function
Lua:
local ignoredList = {1987}
function addBonusLoot(position, name)
    for i = 0, 255 do
        position.stackpos = i
        corpse = getTileThingByPos(position)
        if corpse.uid > 0 and isCorpse(corpse.uid) then
            break
        end
    end
    local newRate = (1 + (getGlobalStorageValue(12382)/100)) * getConfigValue("rateLoot")
    local monsterLoot = getMonsterLootList(name)
    local mainbp = doCreateItemEx(1987, 1)
    for i, loot in pairs(monsterLoot) do
        if math.random(1, 100000) <= newRate * loot.chance then
            if #ignoredList > 0 then
                if (not isInArray(ignoredList, loot.id)) then
                    doAddContainerItem(mainbp, loot.id, loot.countmax and math.random(1, loot.countmax) or 1)
                end
            else
                doAddContainerItem(mainbp, loot.id, loot.countmax and math.random(1, loot.countmax) or 1)
            end
        end
        doAddContainerItemEx(corpse.uid, mainbp)
    end
end

boostedMonstersListEx = {
[1] = {"dragon lord", "dragon lord vip", "fast dlord", "hard dlord"},
[2] = {"frost dragon", "frost dragon vip", "fast fdragon", "hard fdragon"},
[3] = {"hydra", "hydra vip", "fast hydra", "hard hydra"},
[4] = {"warlock", "warlock vip", "fast warlock", "hard warlock"},
[5] = {"medusa", "medusa vip", "fast medusa", "hard medusa"},
[6] = {"grim reaper", "grim reaper vip", "fast greaper", "hard greaper"},
[6] = {"demon", "demon vip", "fast demon", "hard demon"}
}

-- Add this code to lua libs instead, dont forget to remove local when adding to libs
local function table.contains(table, element)
  for _, value in pairs(table) do
    if value == element then
      return true
    end
  end
  return false
end
-- Add above code to lua libs instead

function onDeath(cid, corpse, deathList)
local BOOSTED_MONSTER = getGlobalStorageValue(12380)

local master = getCreatureMaster(cid)
  if (master and master ~= cid) then
    return true
  end

  local curMonster = getCreatureName(cid):lower()
    if table.contains(boostedMonstersListEx[BOOSTED_MONSTER], curMonster) then
        local BOOSTED_EXP_BONUS = getGlobalStorageValue(12381)
        for i = 1, #deathList do
            local bonusExp =  getMonsterInfo(getCreatureName(cid)).experience/i * getPlayerRates(deathList[i])[SKILL__LEVEL] * getExperienceStage(getPlayerLevel(deathList[i])) * BOOSTED_EXP_BONUS/100
      
            doPlayerAddExperience(deathList[i], bonusExp)
            doSendAnimatedText(getPlayerPosition(deathList[i]), bonusExp, 215)

            addEvent(addBonusLoot, 10, getCreaturePosition(cid), getCreatureName(cid))
      
        end
    end
    return true
end

Edit2:
isInArray(array, element) is the same as the local table.contains(table, element) function, so you could just change the if statement to use isInArray(boostedMonstersListEx[BOOSTED_MONSTER], curMonster) instead. I knew there was something implemented inside TFS i just couldn't remember it. anyway I wont update the code example since someone might learn from it :)
I've tried to use isInArray, but I think I was using it wrongly.
Will change the globalevent for numbers storage and try to to it this way.
Thank you so much once again.
 

Mummrik

Hey!
Joined
Oct 22, 2007
Messages
707
Solutions
28
Reaction score
126
Location
Sweden
I've tried to use isInArray, but I think I was using it wrongly.
Will change the globalevent for numbers storage and try to to it this way.
Thank you so much once again.
You probably just used boostedMonstersListEx as table/array parameter, but that is a table containing tables whit the data that you want to check so thats why you have to use it like this boostedMonstersListEx[1], it can be tricky understanding multidimension arrays/tables in the begining
I'm glad I could help you.
 
OP
OP
DukeeH

DukeeH

Active Member
Joined
Dec 6, 2010
Messages
550
Solutions
3
Reaction score
38
@Mummrik
Yes, is a bit advanced, but i'm reading about this
Just one more question, how to get the first value inside an array inside a table?
For example, if I want to get the name on bold inside the table

boostedMonstersListEx = {
[1] = {"dragon lord", "dragon lord vip", "fast dlord", "hard dlord"},
}

I was thinking of something like boostedMonstersListEx[1][1], but doesn't make sense.

Edit.
Nevermind, im right.
 

Mummrik

Hey!
Joined
Oct 22, 2007
Messages
707
Solutions
28
Reaction score
126
Location
Sweden
@Mummrik
Yes, is a bit advanced, but i'm reading about this
Just one more question, how to get the first value inside an array inside a table?
For example, if I want to get the name on bold inside the table

boostedMonstersListEx = {
[1] = {"dragon lord", "dragon lord vip", "fast dlord", "hard dlord"},
}

I was thinking of something like boostedMonstersListEx[1][1], but doesn't make sense.

Edit.
Nevermind, im right.
That is the correct way, see everything inside a table/array as a number and to access it just type the index number to get it.

I love seeing when someone is posting "i'm reading about this"đź‘Ť keep it up and you might be the one teaching me :D
 
Top