TFS 0.X Monster Skull

newby

Member
Joined
Jun 11, 2016
Messages
160
Reaction score
14
I'm trying to add in my server a chance to every monster on spawn, can come with a skull randomly

I've added this on my source code (from: Feature - [CreatureEvent] OnSpawn(cid))

#goto creatureEvent.h :



find [cpp]uint32_t executePrepareDeath(Creature* creature, DeathList deathList);[/cpp]


under it paste [cpp] uint32_t executeOnSpawn(Creature* creature);[/cpp]


find
[cpp]CREATURE_EVENT_DEATH,[/cpp]


replace the line under it with this
[cpp]CREATURE_EVENT_PREPAREDEATH,
CREATURE_EVENT_SPAWN [/cpp]
---------------------------------------------------------------------------------------------------------------------


#goto creatureevent.cpp


find [cpp]else if(tmpStr == "preparedeath")
m_type = CREATURE_EVENT_PREPAREDEATH;[/cpp]


under it paste[cpp]else if(tmpStr == "spawn")
m_type = CREATURE_EVENT_SPAWN; [/cpp]


find [cpp] case CREATURE_EVENT_PREPAREDEATH:
return "onPrepareDeath";[/cpp]


under it paste[cpp] case CREATURE_EVENT_SPAWN:
return "onSpawn";[/cpp]
find [cpp]case CREATURE_EVENT_PREPAREDEATH:
return "cid, deathList";[/cpp]


under itpaste [cpp] case CREATURE_EVENT_SPAWN:
return "cid"; [/cpp]


go to the end of file and paste this
[cpp]uint32_t CreatureEvent::executeOnSpawn(Creature* creature)
{

//onSpawn(cid)
if(m_interface->reserveEnv())
{
ScriptEnviroment* env = m_interface->getEnv();
if(m_scripted == EVENT_SCRIPT_BUFFER)
{
env->setRealPos(creature->getPosition());
std::stringstream scriptstream;


scriptstream << "local cid = " << env->addThing(creature) << std::endl;



if(m_scriptData)
scriptstream << *m_scriptData;


bool result = true;
if(m_interface->loadBuffer(scriptstream.str()))
{
lua_State* L = m_interface->getState();
result = m_interface->getGlobalBool(L, "_result", true);
}


m_interface->releaseEnv();
return result;
}
else
{
#ifdef __DEBUG_LUASCRIPTS__
std::stringstream desc;
desc << creature->getName();
env->setEvent(desc.str());
#endif


env->setScriptId(m_scriptId, m_interface);
env->setRealPos(creature->getPosition());


lua_State* L = m_interface->getState();
m_interface->pushFunction(m_scriptId);


lua_pushnumber(L, env->addThing(creature));


bool result = m_interface->callFunction(1);
m_interface->releaseEnv();
return result;
}
}
else
{
std::clog << "[Error - CreatureEvent::executeCast] Call stack overflow." << std::endl;
return 0;
}
}
[/cpp]


----------------------------------------------------------------------------------------------------------


# goto monster.cpp


find [cpp]
void Monster::eek:nCreatureAppear(const Creature* creature)
{
Creature::eek:nCreatureAppear(creature);
if(creature == this)
{
//We just spawned lets look around to see who is there.
if(isSummon())
isMasterInRange = canSee(master->getPosition());[/cpp]


under it ##press enter key to move down a line or 2## then paste[cpp] CreatureEventList spawn = getCreatureEvents(CREATURE_EVENT_SPAWN);
for(CreatureEventList::iterator it = spawn.begin(); it != spawn.end(); ++it)
(*it)->executeOnSpawn(this); [/cpp]




And you are done.

And add this on my creaturescript:
Code:
<event type="spawn" name="monsterThink" registerTo ="monster" event="script" value="monsterskull.lua"/>
monsterskull.lua
Code:
function onSpawn(cid)
    -- SKULL_NONE = 0
    -- SKULL_WHITE = 3
    -- SKULL_RED = 4
    -- SKULL_BLACK = 5
    local skull, bonus_percent
    local chance = math.random(1, 100)
    if(chance <= 1) then
        skull = 5
        bonus_percent = 5
    elseif(chance >= 2 and chance <= 5) then
        skull = 4
        bonus_percent = 3
    elseif(chance >= 6 and chance <= 20) then
        skull = 3
        bonus_percent = 2
    else
        skull = 0
        bonus_percent = 0
    end
    if(skull > 0) then
        local hp = (getCreatureMaxHealth(cid)) + ((getCreatureMaxHealth(cid) * bonus_percent) / ( 100 ))
        setCreatureMaxHealth(cid, hp)
        doCreatureSetSkullType(cid, skull)
    end



    doBroadcastMessage("Monster ".. getCreatureName(cid).." was created.")
    return true
end
But monster on spawn dont send the broadcastmessage...
Do i missing something? Doing something wrong?
 

Sarah Wesker

ค∂vαηcε รүηтαx ❤
Joined
Mar 16, 2017
Messages
407
Reaction score
215
Location
London
The function is not global and, therefore, you must register the event for each creature, you must try to register the event manually, so you can check!
 
OP
newby

newby

Member
Joined
Jun 11, 2016
Messages
160
Reaction score
14
The function is not global and, therefore, you must register the event for each creature, you must try to register the event manually, so you can check!
I'm sorry i don't understand...
Could you make a example or show it in some way?
 
OP
newby

newby

Member
Joined
Jun 11, 2016
Messages
160
Reaction score
14
You must put it in a monster file, for example rotworm.xml
What are u mean?

Add in every monster.xml something like:
Code:
<script>
        <event name="something"/>
</script>
:eek:
 

Sir Sezago

Scripter/Developer
Joined
Aug 28, 2017
Messages
260
Reaction score
15
What are u mean?

Add in every monster.xml something like:
Code:
<script>
        <event name="something"/>
</script>
:eek:
u need to make random skulls so u need to active it in your sources..
 

Sir Sezago

Scripter/Developer
Joined
Aug 28, 2017
Messages
260
Reaction score
15
Random Skull code with more heath .
Code:
function onSpawn(cid)
               if math.random(1, 50) == 1 and isMonster(cid) then
                   doCreatureSetSkullType(cid, 5)
                   doCreatureSetStorage(cid, 110, 1)
                   setCreatureMaxHealth(cid, getCreatureHealth(cid) + (getCreatureHealth(cid) * 2))
               doCreatureAddHealth(cid, getCreatureHealth(cid) * 2)
               elseif math.random(1, 30) == 1 and isMonster(cid) then
                   doCreatureSetSkullType(cid, 4)
                   doCreatureSetStorage(cid, 111, 1)
                   setCreatureMaxHealth(cid, getCreatureHealth(cid) + (getCreatureHealth(cid) * 0.50))
               doCreatureAddHealth(cid, getCreatureHealth(cid) * 0.50)
               elseif math.random(1, 20) == 1 and isMonster(cid) then
                   doCreatureSetSkullType(cid, 3)
                   doCreatureSetStorage(cid, 112, 1)
            setCreatureMaxHealth(cid, getCreatureHealth(cid) + (getCreatureHealth(cid) * 0.30))
               doCreatureAddHealth(cid, getCreatureHealth(cid) * 0.30)
               end
   return true
end
 
Top