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

Solved Addskill causing intense lag

EnforcedRPG

Nubwarz.no-ip.org
Joined
Nov 29, 2012
Messages
39
Reaction score
2
Heya everyone, I finally got my "Skill Reset" script working but it causes crazy lag when a new character is created and logged in (or whenever someone logs in with 10~ skills or 0~ magic level)

Here's the script:
Lua:
function onLogin(cid)
local playerVoc = getPlayerVocation(cid)
local samount = 75 - (getPlayerSkillLevel(cid, 2))
local aamount = 75 - (getPlayerSkillLevel(cid, 3))
local camount = 75 - (getPlayerSkillLevel(cid, 1))
local damount = 80 - (getPlayerSkillLevel(cid, 4))
local shieldp = 70 - (getPlayerSkillLevel(cid, 5))
local shieldk = 75 - (getPlayerSkillLevel(cid, 5))
local shieldm = 20 - (getPlayerSkillLevel(cid, 5))
local magicm = 60 - (getPlayerMagLevel(cid))
local magicp = 15 - (getPlayerMagLevel(cid))
local magick = 6 - (getPlayerMagLevel(cid))

if playerVoc == 1 then
	doPlayerAddSkill(cid, 5, shieldm)
	doPlayerAddMagLevel(cid, magicm)
end

if playerVoc == 2 then
	doPlayerAddSkill(cid, 5, shieldm)
	doPlayerAddMagLevel(cid, magicm)
end

if playerVoc == 3 then
	doPlayerAddSkill(cid, 4, damount)	
	doPlayerAddSkill(cid, 5, shieldp)
	doPlayerAddMagLevel(cid, magicp)	
end
if playerVoc == 4 then
	doPlayerAddSkill(cid, 2, samount)	 
	doPlayerAddSkill(cid, 1, camount)
	doPlayerAddSkill(cid, 3, aamount)
	doPlayerAddSkill(cid, 5, shieldk)	
	doPlayerAddMagLevel(cid, magick)	
end
return TRUE
end

I'm guessing it has something to do with the sources, not with the script but I have no idea :/

Thank you for any help! :D

------------------------------- Edit --------------------------------

The lag does not occur if the skills are close to what they are going to be reset to.. For example:
A knight has 73 sword fighting when they log in, it'll advance to 75 on login. It won't cause lag since it's only adding 2 skills but is there any way how to make it not cause any lag?
 
Last edited:
I've looked into the sources and this is what I could find:
Luascript.cpp:
Lua:
int32_t LuaInterface::luaDoPlayerAddSkillTry(lua_State* L)
{
	//doPlayerAddSkillTry(uid, skillid, n[, useMultiplier = true])
	bool multiplier = true;
	if(lua_gettop(L) > 3)
		multiplier = popNumber(L);

	uint64_t n = popNumber(L);
	uint16_t skillid = popNumber(L);

	ScriptEnviroment* env = getEnv();
	if(Player* player = env->getPlayerByUID(popNumber(L)))
	{
		player->addSkillAdvance((skills_t)skillid, n, multiplier);
		lua_pushboolean(L, true);
	}
	else
	{
		errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
		lua_pushboolean(L, false);
	}

	return 1;
}

Player.cpp:
Lua:
void Player::addSkillAdvance(skills_t skill, uint64_t count, bool useMultiplier/* = true*/)
{
	if(!count)
		return;

	//player has reached max skill
	uint64_t currReqTries = vocation->getReqSkillTries(skill, skills[skill][SKILL_LEVEL]),
		nextReqTries = vocation->getReqSkillTries(skill, skills[skill][SKILL_LEVEL] + 1);
	if(currReqTries > nextReqTries)
		return;

	if(useMultiplier)
		count = uint64_t((double)count * rates[skill] * g_config.getDouble(ConfigManager::RATE_SKILL));

	std::stringstream s;
	while(skills[skill][SKILL_TRIES] + count >= nextReqTries)
	{
		count -= nextReqTries - skills[skill][SKILL_TRIES];
	 	skills[skill][SKILL_TRIES] = skills[skill][SKILL_PERCENT] = 0;
		skills[skill][SKILL_LEVEL]++;

		s.str("");
		s << "You advanced in " << getSkillName(skill);
		if(g_config.getBool(ConfigManager::ADVANCING_SKILL_LEVEL))
			s << " [" << skills[skill][SKILL_LEVEL] << "]";

		s << ".";
		sendTextMessage(MSG_EVENT_ADVANCE, s.str().c_str());

		CreatureEventList advanceEvents = getCreatureEvents(CREATURE_EVENT_ADVANCE);
		for(CreatureEventList::iterator it = advanceEvents.begin(); it != advanceEvents.end(); ++it)
			(*it)->executeAdvance(this, skill, (skills[skill][SKILL_LEVEL] - 1), skills[skill][SKILL_LEVEL]);

		currReqTries = nextReqTries;
		nextReqTries = vocation->getReqSkillTries(skill, skills[skill][SKILL_LEVEL] + 1);
		if(currReqTries > nextReqTries)
		{
			count = 0;
			break;
		}
	}

	if(count)
		skills[skill][SKILL_TRIES] += count;

	//update percent
	uint16_t newPercent = Player::getPercentLevel(skills[skill][SKILL_TRIES], nextReqTries);
 	if(skills[skill][SKILL_PERCENT] != newPercent)
	{
		skills[skill][SKILL_PERCENT] = newPercent;
		sendSkills();
 	}
	else if(!s.str().empty())
		sendSkills();
}

I'm guessing it's somewhere in here but I'm not really sure I'm a complete noob with C++... or maybe I'm just a complete nublet ;) haha

Anyways, if anyone knows how to fix it, I'd greatly appreciate it! :D

- - - Updated - - -

Or I thought of a new idea, starting knights with 75 skills mages, 60 ml, paladins, 80 dist/70 shield etc.. in the sources... I found where to do it but I don't really wanna screw anything up :/

This is the part in the sources:
Lua:
bool IOLoginData::createCharacter(uint32_t accountId, std::string characterName, int32_t vocationId, uint16_t sex)
{
	if(playerExists(characterName))
		return false;

	Vocation* vocation = Vocations::getInstance()->getVocation(vocationId);
	Vocation* rookVoc = Vocations::getInstance()->getVocation(0);

	uint16_t healthMax = 150, manaMax = 0, capMax = 400, lookType = 136;
	if(sex % 2)
		lookType = 128;

	uint32_t level = g_config.getNumber(ConfigManager::START_LEVEL), tmpLevel = std::min((uint32_t)7, (level - 1));
	uint64_t exp = 0;
	if(level > 1)
		exp = Player::getExpForLevel(level);

	if(tmpLevel > 0)
	{
		healthMax += rookVoc->getGain(GAIN_HEALTH) * tmpLevel;
		manaMax += rookVoc->getGain(GAIN_MANA) * tmpLevel;
		capMax += rookVoc->getGainCap() * tmpLevel;
		if(level > 8)
		{
			tmpLevel = level - 8;
			healthMax += vocation->getGain(GAIN_HEALTH) * tmpLevel;
			manaMax += vocation->getGain(GAIN_MANA) * tmpLevel;
			capMax += vocation->getGainCap() * tmpLevel;
		}
	}

	Database* db = Database::getInstance();
	DBQuery query;

	query << "INSERT INTO `players` (`id`, `name`, `world_id`, `group_id`, `account_id`, `level`, `vocation`, `health`, `healthmax`, `experience`, `lookbody`, `lookfeet`, `lookhead`, `looklegs`, `looktype`, `lookaddons`, `maglevel`, `mana`, `manamax`, `manaspent`, `soul`, `town_id`, `posx`, `posy`, `posz`, `conditions`, `cap`, `sex`, `lastlogin`, `lastip`, `skull`, `skulltime`, `save`, `rank_id`, `guildnick`, `lastlogout`, `blessings`, `online`) VALUES (NULL, " << db->escapeString(characterName) << ", " << g_config.getNumber(ConfigManager::WORLD_ID) << ", 1, " << accountId << ", " << level << ", " << vocationId << ", " << healthMax << ", " << healthMax << ", " << exp << ", 68, 76, 78, 39, " << lookType << ", 0, " << g_config.getNumber(ConfigManager::START_MAGICLEVEL) << ", " << manaMax << ", " << manaMax << ", 0, 100, " << g_config.getNumber(ConfigManager::SPAWNTOWN_ID) << ", " << g_config.getNumber(ConfigManager::SPAWNPOS_X) << ", " << g_config.getNumber(ConfigManager::SPAWNPOS_Y) << ", " << g_config.getNumber(ConfigManager::SPAWNPOS_Z) << ", 0, " << capMax << ", " << sex << ", 0, 0, 0, 0, 1, 0, '', 0, 0, 0)";
	return db->query(query.str());
}

Atleast I think :p

- - - Updated - - -

Nevermind, I just made a new script that will add the skills directly to the database the first time they log in... Anyways thank you everyone that atleast took time to look/read this!

~~Solved~~
 
Back
Top