Changing level of a monster from code.

Support for the latest build of L2J Server, get help here with installations, upgrades, problems.
Do not post bugs reports here, use viewforum.php?f=77 instead.
There is no support for other server builds than the official provided by l2jserver.com
Forum rules
READ NOW: L2j Forums Rules of Conduct
Post Reply
Painfull
Posts: 12
Joined: Thu Feb 03, 2011 10:34 am

Changing level of a monster from code.

Post by Painfull »

Hello there. It might be the first time posting here so please forgive me if i do something wrong.

I am using one of the latest revisions of the L2JServer for H5 Chronicle with some custom things i have made so far on my own.
Sorry but i still can't figure out how to report the revision of the datapack and core!

The thing is that i want to change the level of a monster (only an instance of it) for an event i'm creating. For what i've checked so far i cannot find a "setLevel()" method on any of the classes assosiated with L2Npc and the only solution seems to be to set a new StatsSet to the L2NpcTemplate object. Still i cannot find a way to get this StatsSet without changing any of the core code.

Being a Java developer for years and years i can program lots of things but when it comes to that big projects i want an experts opinion before changing anything.
Just to sum up the question: " Is there a way to change the level of an Instance of monster from code? "

Thanks in advance.
Java and Android Developer.
New in L2J Development. Work in progress :)
User avatar
Avanael92
Advanced User
Advanced User
Posts: 189
Joined: Thu Aug 07, 2014 5:26 pm
Location: Germany

Re: Changing level of a monster from code.

Post by Avanael92 »

You can create custom NPC mobs based on XML in data/stats/npcs/custom, even the same ones with different IDs and levels, then you only have to spawn them from code :)

In case you don't know already, you have to enable to allow custom settings in config which is inside the General.properties.
HorridoJoho
L2j Senior Developer
L2j Senior Developer
Posts: 795
Joined: Sun Aug 14, 2005 11:27 am

Re: Changing level of a monster from code.

Post by HorridoJoho »

There is a way and that is, as you found out already, through the StatsSet. The Problem with an NPC is that it's level is always the one in the template and a template is immutable. The StatsSet is loaded into the template fields and then the StatsSet is not stored, that is why you can't get a stats set from a template.

In L2Character, you have the setTemplate method. Here you can replace the current L2NpcTemplate with a new one. So you would have to construct it using a StatsSet in the same way as the code which loads the templates from the xml files.

You probably also need to refresh the NpcStatus (HP MP etc.) object of the npc in case you want the current vital status of the npc to change. Additionally you need to broadcast the NPC info when you are done, so the changes are reflected to the players around.

To sum it up: The current implementation doesn't allow NPCs to level up and thus needs some manual work to accomplish this.
JMD
Advanced User
Advanced User
Posts: 1440
Joined: Wed Apr 15, 2009 10:07 am

Re: Changing level of a monster from code.

Post by JMD »

That would be nice to have in order to make mobs scale with the level of the player.
Painfull
Posts: 12
Joined: Thu Feb 03, 2011 10:34 am

Re: Changing level of a monster from code.

Post by Painfull »

Well i found out a solution that doesn't involve all these actions that HorridoJoho said are needed.
As for the answer that Avanael92 gave me, i prefer to change it dynamically, not have them static in xmls.

You have to add this method to the L2NpcTemplate class anywhere you want (ok after getLevel() method is alright).

Code: Select all

public void setLevel(int level)
{
	_level = (byte) level;
}
Once you do that then you have to create an L2Spawn object and then get the template and set the level.

Code: Select all

L2Spawn mySpawn = new L2Spawn(npc_id);
mySpawn.getTemplate().setLevel(level_prefered);
SpawnTable.getInstance().addNewSpawn(mySpawn, //prefered option true or false // read documentation);
mySpawn.init();
and then you have a monster of the level you want.

This way you can create dynamic mobs based on the templateId you can add (same as setLevel but you have to add it) and more.

Changing the stats of an Npc seems to be harder though because of the immutable HashMaps used to define them. So changing the level only is not gonna give you the results you want. I'm still working on it.

@HorridoJoho, i think that broadcasting the change won't have any effect. Once the mob is spawned and a player teleports near it, the player already has the info about the npc.
Java and Android Developer.
New in L2J Development. Work in progress :)
HorridoJoho
L2j Senior Developer
L2j Senior Developer
Posts: 795
Joined: Sun Aug 14, 2005 11:27 am

Re: Changing level of a monster from code.

Post by HorridoJoho »

Painfull wrote: Mon Feb 19, 2018 7:47 am Well i found out a solution that doesn't involve all these actions that HorridoJoho said are needed.
Yes it is possible by adding a L2NpcTemplate#setLevel method, but that would change the level for all NPCs having the same template. I'd rather would change the logic of NpcStat#getLevel and L2Npc#getLevel. Both currently return the level from the template.

The proper way would be to use the _level field in the CharStat class:
  • Remove the method NpcStat#getLevel
  • In NpcStat#NpcStat call setLevel(activeChar.getTemplate().getLevel()) after the super call
  • Modify the L2Npc#getLevel method to return NpcStat#getLevel
  • Add a L2Npc#setLevel method which calls NpcStat#setLevel
This way the npc level is initialized with the default level specified by the template, but the actual level of the NPC is independent of it's template. Now prior to spawning an NPC you can call L2Npc#setLevel.
Painfull wrote: Mon Feb 19, 2018 7:47 am @HorridoJoho, i think that broadcasting the change won't have any effect. Once the mob is spawned and a player teleports near it, the player already has the info about the npc.
I am not entirely sure about that but keep in mind that the level change is not automatically broadcast to nearby players. If there are any visual indicators about the level of the NPC, a broadcast is needed for sorrounding pleayer so thoose visual indicators are also updated.

---

When you want to modify the NPC stats, you need to tinker with the Stat calculators. They are stored inside the L2Character. It is an array which maps to the ordinaries of the Stat enum fields. One idea could be to scale the base stats by doing something like:

Code: Select all

npc.getTemplate().getPAtk() * npc.getLevel() / npc.getTemplate().getLevel()
Post Reply