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

[TFS] 2008-02-01 Some LUA Functions

Lejjo

New Member
Joined
Sep 21, 2007
Messages
78
Reaction score
1
Which LUA Functions?
doCreatureChangeMaxHealth(cid, newHealth)
doCreatureChangeMaxMana(cid, newMana)
getPlayerSummonCount(cid)
getCreatureSummonCountByName(cid, name)


Start of the code:

luascrpt.cpp

Add...
Code:
	//doCreatureChangeMaxHealth(cid, newHealth)
	lua_register(m_luaState, "doCreatureChangeMaxHealth", LuaScriptInterface::luaDoCreatureChangeMaxHealth);
	
	//doCreatureChangeMaxMana(cid, newMana)
	lua_register(m_luaState, "doCreatureChangeMaxMana", LuaScriptInterface::luaDoCreatureChangeMaxMana);

	//getPlayerSummonCount(cid)
	lua_register(m_luaState, "getPlayerSummonCount", LuaScriptInterface::luaGetPlayerSummonCount);
	
	//getCreatureSummonCountByName(cid, name)
	lua_register(m_luaState, "getCreatureSummonCountByName", LuaScriptInterface::luaGetCreatureSummonCountByName);

...under
Code:
	//escapeString(str)
	lua_register(m_luaState, "escapeString", LuaScriptInterface::luaEscapeString);



Add...
Code:
int32_t LuaScriptInterface::luaDoCreatureChangeMaxHealth(lua_State* L)
{
	//doCreatureChangeMaxHealth(uid,newHealth)
	int32_t healthChange = (int32_t)popNumber(L);
	uint32_t cid = popNumber(L);
	ScriptEnviroment* env = getScriptEnv();
	Creature* creature = env->getCreatureByUID(cid);
	if(creature)
	{
 	    creature->changeMaxHealth(healthChange);
		lua_pushnumber(L, LUA_NO_ERROR);
	}
	else
	{
		reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
		lua_pushnumber(L, LUA_ERROR);
	}
	return 1;
}

int32_t LuaScriptInterface::luaDoCreatureChangeMaxMana(lua_State* L)
{
	//doCreatureChangeMaxMana(uid,newMana)
	int32_t manaChange = (int32_t)popNumber(L);
	uint32_t cid = popNumber(L);
	ScriptEnviroment* env = getScriptEnv();
	Creature* creature = env->getCreatureByUID(cid);
	if(creature)
	{
 	    creature->changeMaxMana(manaChange);
		lua_pushnumber(L, LUA_NO_ERROR);
	}
	else
	{
		reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
		lua_pushnumber(L, LUA_ERROR);
	}
	return 1;
}

int32_t LuaScriptInterface::luaGetPlayerSummonCount(lua_State* L)
{
	//getPlayerSummonCount(cid)
	uint32_t cid = popNumber(L);
	ScriptEnviroment* env = getScriptEnv();
	Creature* creature = env->getCreatureByUID(cid);
	if(creature)
	{
		int32_t summons = creature->getSummonCount();
		lua_pushnumber(L, summons);
	}
	else
	{
		reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
		lua_pushnumber(L, LUA_ERROR);
	}
	return 1;
}

int LuaScriptInterface::luaGetCreatureSummonCountByName(lua_State *L)
{
	std::string name = popString(L);
	uint32_t uid = popNumber(L);

	ScriptEnviroment* env = getScriptEnv();
	
	Creature* creature = env->getCreatureByUID(uid);
	if(creature){
        int count = 0;
        std::list<Creature*>::iterator cit;
        for(cit = creature->summons.begin(); cit != creature->summons.end(); ++cit){
            if((*cit)->getName() == name){
                count = count + 1;
            }
        }
        if(count > 0){
            lua_pushnumber(L, count);
        }
    }
	else{
		reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
		lua_pushnumber(L, LUA_ERROR);
	}
	return 1;
}

...under
Code:
int32_t LuaScriptInterface::luaEscapeString(lua_State* L)
{
	//escapeString(str)
	std::string str = popString(L);
	lua_pushstring(L, Database::escapeString(str).c_str());
	return 1;
}


luascrpt.h

Add...
Code:
        static int32_t luaDoCreatureChangeMaxHealth(lua_State* L);
	    static int32_t luaDoCreatureChangeMaxMana(lua_State* L);
	    static int32_t luaDoCreatureChangeHealth(lua_State* L);
	    static int32_t luaDoCreatureChangeMana(lua_State* L);
	
	    static int32_t luaGetPlayerSummonCount(lua_State* L);
	    static int luaGetCreatureSummonCountByName(lua_State *L);

...under
Code:
		static int32_t luaEscapeString(lua_State* L);


creature.cpp

Add...
Code:
void Creature::changeMaxHealth(int32_t healthChange)
{
   
    healthMax = (int32_t)healthChange;

	g_game.addCreatureHealth(this);
}

void Creature::changeMaxMana(int32_t manaChange)
{
		manaMax = (int32_t)manaChange;
		
}

...above
Code:
void Creature::changeHealth(int32_t healthChange)
{
	if(healthChange > 0)
		health += std::min(healthChange, getMaxHealth() - health);
	else
		health = std::max((int32_t)0, health + healthChange);

	g_game.addCreatureHealth(this);
}


creature.h

Add...
Code:
        virtual void changeMaxHealth(int32_t healthChange);
        virtual void changeMaxMana(int32_t manaChange);

...above
Code:
		virtual void changeHealth(int32_t healthChange);
End of the code:

Rebuild all and your are done.

Explanation
doCreatureChangeMaxHealth(cid, 500) - Means, the max hp of that creature will be 500. If he/she has 1000 hp, he/she will get 500 as MAX hp. If he/she has 200 then he/she will get 500 as MAX hp. So it doesn't matter how much you have, just write how much hp you want it to be as MAX. Note that you may need creaturescripts for this, incase he/she dies/reloggs.

doCreatureChangeMaxMana(cid, 500) - Same as above, but on mana

getPlayerSummonCount(cid) - Returns the ammount of total summons the player has.

getCreatureSummonCountByName(cid, rat) - Returns the ammount of total summons the player has with the name "rat".

Please leave a comment :)
And if there is a better way to do this, please tell me then. Still learning :)

Yours,
Kadj
 
Last edited:
I don't have any account on OpenLua and never entered their side so _I_ didn't.
 
Man I was searching for this, just PM you..... lol didn't see this post first after some search...

Any way JUST WHAT I NEED IT.
Thanks to you and all the people that had something to do with it.

Keep it up :thumbup:
 
With this, and the bearform, great! :D Can finaly make 3 stages of bearform,
stage 1 300+ hp, 600- mana, 80+ fist,
stage 2 500+hp, 1000- mana, 140+ fist,
stage 3 700+ hp, 1400- mana, 200+ fist. [changing max hp, and max mana]

Love theese functions, I can make lots of funny "forms" now easy ^^
doCreatureChangeMaxMana(cid, getPlayerMaxMana(cid) + 500)
doCreatureChangeMaxHealth(cid, getCreatureMaxHealth(cid) - 1500)

Thx! ^^
 
Back
Top