• 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 Function Create custom monster by lua script (custow name,hp,exp,outfit,spells)

MeNi

*^#%$%
Joined
Jul 23, 2008
Messages
183
Reaction score
12
Hello. I am releasing lua function i made to create custom monsters by lua script.

alalalala.jpg


function code -> use: doCreateCustomMonster(name, pos, outfit, health, spells, corpse, distance, experience )
C++:
int32_t LuaInterface::luaDoCreateCustomMonster(lua_State* L)
{
    //doCreateCustomMonster(name, pos, outfit, health, spells, corpse, distance, experience )
   
    // created By MeNi for otland.net //
   
    uint64_t health,corpse,distance,experience;
    Outfit_t outfit;
    PositionEx pos;
    MonsterType* pobranyTyp = NULL;
    pobranyTyp = new MonsterType();

    experience = popNumber(L);
    distance = popNumber(L);
    corpse = popNumber(L);
    std::string spells = popString(L);
    health = popNumber(L);
    outfit = popOutfit(L);
    popPosition(L, pos);
    std::string name = popString(L);
   
    Monster* monster;
   
    pobranyTyp->spellAttackList.clear();
   
    pobranyTyp->health = health;
    pobranyTyp->healthMax = health;
       pobranyTyp->outfit = outfit;
    pobranyTyp->name = name;
    pobranyTyp->nameDescription = name;
    pobranyTyp->lookCorpse = corpse;
    pobranyTyp->targetDistance = distance;
    pobranyTyp->experience = experience;
   
    pobranyTyp->isSummonable =
    pobranyTyp->isIllusionable =
    pobranyTyp->isConvinceable =
    pobranyTyp->isWalkable =
    pobranyTyp->pushable = false;
   
    pobranyTyp->isAttackable =
    pobranyTyp->isHostile =
    pobranyTyp->canPushItems =
    pobranyTyp->canPushCreatures = true;
   
    pobranyTyp->defense = 50;
    pobranyTyp->armor = 80;
    pobranyTyp->baseSpeed = 200;
    pobranyTyp->changeTargetSpeed =
    pobranyTyp->changeTargetChance = 0;
       
    xmlDocPtr doc = xmlParseMemory(spells.c_str(), spells.length());
    xmlNodePtr root = xmlDocGetRootElement(doc);

    xmlNodePtr tmpNode = root->children;

    while(tmpNode)
    {
        if(!xmlStrcmp(tmpNode->name, (const xmlChar*)"attack"))
        {
            spellBlock_t sb;
            if(g_monsters.deserializeSpell(tmpNode, sb, "doCreateCustomMonster"))
                pobranyTyp->spellAttackList.push_back(sb);
        }
        tmpNode = tmpNode->next;
    }
   

    monster = Monster::createMonster(pobranyTyp);
   
    if(!g_game.placeCreature(monster, pos, false, false))
    {
        delete monster;

        lua_pushboolean(L, true);
        return 1;
    }

    ScriptEnviroment* env = getEnv();
    lua_pushnumber(L, env->addThing((Thing*)monster));
    return 1;
}

u must move in monsters.h from "private" to "public"
C++:
bool deserializeSpell(xmlNodePtr node, spellBlock_t& sb, const std::string& description = "");

sample talkaction to test script: use: /custom name,lookType,health,exp

Lua:
local spells = {
[1] = {used = 0, text = '<attack name="melee" interval="2000" chance="100" range="5" radius="1" target="0"><attribute key="areaEffect" value="fire"/></attack>'},
[2] = {used = 0, text = '<attack name="melee" interval="1200" chance="100" range="5" radius="1" target="0"><attribute key="areaEffect" value="energyarea"/></attack>'},
[3] = {used = 0, text = '<attack name="melee" interval="700" chance="100" range="5" radius="1" target="0"><attribute key="areaEffect" value="mortarea"/></attack>'}
}

local spellsNumber = 2

function randomNum(range)

 local rand = 0
 good = false

 math.randomseed( os.time() )
 math.random()
 math.random()
 math.random()
 
while (not good) do
    rand = math.random(range)
   
    if (spells[rand].used == 0) then
        spells[rand].used = 1
        good = true
    else
        good = false
    end
   
end

return rand

end


function onSay(cid, words, param, channel)

    -- /custom name,lookType,health,exp
    if(param == '') then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Command param required.")
        return true
    end
    local t = string.explode(param, ",")
    if(not t[4]) then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Command require 2 params.")
        return true
    end
   
    local position = getCreaturePosition(cid)
    outfit = {lookType = t[2], lookHead = 0, lookBody = 0, lookLegs = 0, lookFeet = 0}
    local attacks = "<a>"
    local num
   
    for i=1,spellsNumber,1 do
        num = randomNum(table.getn(spells))
        attacks = attacks .. spells[num].text
    end
   
    attacks = attacks .. "</a>"
    -- doCreateCustomMonster(name, pos, outfit, health, spells, corpse, distance, exp )
    doCreateCustomMonster(t[1],position,outfit,t[3],attacks, 6324, 1, t[4])
   
    for i=1,table.getn(spells),1 do
        spells[i].used = 0
    end
   
    attacks = ""


    return true
end
 
Last edited:
add defenses, inmunities and creature events tags and will be perfect!
 
Last edited:
I have any question.
If i say /custom name,444,222,122,Meteor, this function create a new xml with monster?
 
If you are thinking out a new version of the function would be good as many people say here is that you add attributes that
missing, you could create 3 different modes, weak, normal, strong and Offer a random attack mode depending on the desired monster create
 
It doesn't work:
PHP:
[28/02/2011 20:26:02] [Error - TalkAction Interface] 
[28/02/2011 20:26:02] data/talkactions/scripts/moje/test.lua:onSay
[28/02/2011 20:26:02] Description: 
[28/02/2011 20:26:02] data/talkactions/scripts/moje/test.lua:61: attempt to call global 'doCreateCustomMonster' (a nil value)
[28/02/2011 20:26:02] stack traceback:
[28/02/2011 20:26:02] 	data/talkactions/scripts/moje/test.lua:61: in function <data/talkactions/scripts/moje/test.lua:36>
I think that this function not loaded attacks.
 
Back
Top