• 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 Is it possible...

Moj mistrz

Monster Creator
Joined
Feb 1, 2008
Messages
932
Solutions
10
Reaction score
295
Location
Poland
to set creature drop via lua script?
Hello by the way!
I'm using TFS 1.0
You may ask why I need it. It's because I'm working on a spellscript which after use(by monster) will remove some of your gold from container and this gold should be set to monster drop.
 
Last edited:
Yes, this is possible (Personally I do it on 0.3.6 and instead of putting it in the corpse I have it fall on the ground). but it is also possible to place it in the corpse.

I would say do it with a onDeath creaturescript.
Looking at the source for TFS 1.0 I would say it is possible there too using onDeath.
Code:
OnDeath(this, corpse, _lastHitCreature, mostDamageCreature, lastHitUnjustified, mostDamageUnjustified);

using a onDeath Creaturescript you would simply grab the item (in your case gold coins) and count (how many) and put it in the corpse.
 
What about doSetCreatureDropLoot ? Can I set there loot? or it's just true/false. I would like to have it in spell.
Code:
    local combat = createCombatObject()
    setCombatParam(combat, COMBAT_PARAM_EFFECT, CONST_ME_HOLYDAMAGE)
    setCombatParam(combat, COMBAT_PARAM_DISTANCEEFFECT, CONST_ANI_SMALLHOLY)

function onCastSpell(cid, var)
    local target = getCreatureTarget(cid)
    local money = math.random(5,50)
    doPlayerRemoveMoney(target, money)
    doSendDistanceShoot(getCreaturePosition(target), getCreaturePosition(cid), CONST_ANI_SMALLHOLY)
    doSendMagicEffect(getCreaturePosition(cid), CONST_ME_HOLYDAMAGE)
    doCreatureSay(target, "<pickpocket stole you " .. money .. " gold!>", TALKTYPE_ORANGE_1)
    return doCombat(cid, combat, var)
end
That's the script.
 
Simply look it up in TFS 1.0 source and see what it does...

I'll do it for you this time (takes 2 seconds)
Code:
int32_t LuaScriptInterface::luaCreatureSetDropLoot(lua_State* L)
{
// creature:setDropLoot(doDrop)
Creature* creature = getUserdata<Creature>(L, 1);
if (creature) {
creature->setDropLoot(getBoolean(L, 2));
pushBoolean(L, true);
} else {
lua_pushnil(L);
}
return 1;
}
So looking at the above since it says "getBoolean" it means it is a true-false. (Which means it is basically changing if the monster CAN drop loot, such as a summon).
 
Simply look it up in TFS 1.0 source and see what it does...

I'll do it for you this time (takes 2 seconds)
Code:
int32_t LuaScriptInterface::luaCreatureSetDropLoot(lua_State* L)
{
// creature:setDropLoot(doDrop)
Creature* creature = getUserdata<Creature>(L, 1);
if (creature) {
creature->setDropLoot(getBoolean(L, 2));
pushBoolean(L, true);
} else {
lua_pushnil(L);
}
return 1;
}
So looking at the above since it says "getBoolean" it means it is a true-false. (Which means it is basically changing if the monster CAN drop loot, such as a summon).
I thought so :/. So is there any way to make monster drop certain items(and amount) via spell script?
 
Personally, I do it like this: (This is 0.3.6 scripting, but I believe there are equivalent functions for TFS 1.0)
Functions Needed:
  1. getCreaturePosition
  2. getPlayerStorageValue
  3. doCreateItem
  4. getClosestFreeTile
  5. doPlayerSetStorageValue
Code:
function onKill(cid, target)
   if isMonster(target) then
     local position = getCreaturePosition(target)
     local posi = {x = position.x+math.random(-1, 1), y = position.y+math.random(-1, 1), z = position.z}
     
     if getPlayerStorageValue(target, "stolen") > 0 then     
       doCreateItem(getPlayerStorageValue(target, "stolen"), getPlayerStorageValue(target, "stolenAmount"), getClosestFreeTile(cid, position))
       doPlayerSetStorageValue(target, "stolen", -1)
     end
     
   end
   return true
end
 
For looting purposes I like onDeath better, so the script won't execute as many attackers were involved in the battle and also you don't have to create any aditional loot message for the added item.
Code:
function onDeath(cid, corpse, killer, mostDamage, unjustified, mostDamage_unjustified)
    local m = Monster(cid)
    local k = Player(killer)
    local storage = XXXX
 
    if m:isMonster() and k:isPlayer() then 
        local corpse = m:getPosition():getTile():getItemByType(ITEM_TYPE_CONTAINER):getUniqueId()
        if corpse ~= 0 and k:getStorageValue(storage) >= 1 then
            doAddContainerItem(corpse, ITEM_GOLD_COIN, k:getStorageValue(storage)) --I don't know how to make this with metatables lol
            k:setStorageValue(storage, 0)
        end
    end
    return true
end

You only need to change the storage value and set the storage on the spell.

Pd: Is not tested, but should work (?)
 
For looting purposes I like onDeath better, so the script won't execute as many attackers were involved in the battle.
Code:
function onDeath(cid, corpse, killer, mostDamage, unjustified, mostDamage_unjustified)
    local m = Monster(cid)
    local k = Player(killer)
    local storage = XXXX
 
    if m:isMonster() and k:isPlayer() then 
        local corpse = m:getPosition():getTile():getItemByType(ITEM_TYPE_CONTAINER):getUniqueId()
        if corpse ~= 0 and k:getStorageValue(storage) >= 1 then
            doAddContainerItem(corpse, ITEM_GOLD_COIN, k:getStorageValue(storage))
            k:setStorageValue(storage, 0)
        end
    end
    return true
end



You only need to change the storage value and set the storage on the spell.

Pd: Is not tested, but should work (?)

Yep, the above will work too and is more efficient. But you will need to register the above event to every monster that can steal items.
One thing about scripting is, there are a million ways to do anything. And there is not always a "best way", but there is certainly "worse ways".

**EDIT**

HUGE problem with colors script is, if a summon or other monster kills the thing that stole your gold it will not drop it. (if k:isPlayer causes this issue)
 
Yep, the above will work too and is more efficient. But you will need to register the above event to every monster that can steal items.
One thing about scripting is, there are a million ways to do anything. And there is not always a "best way", but there is certainly "worse ways".

**EDIT**

HUGE problem with colors script is, if a summon or other monster kills the thing that stole your gold it will not drop it. (if k:isPlayer causes this issue)

Yeah, I totally forgot about register the creatures and the summons part... But anyway, I'm gonna leave the script there as template if someone needs something similar.
 
Back
Top