Implementing Monsters ( NPC's ) into my mmo...

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

genar
Posts: 137
Joined: 13 Jul 2017, 11:49

Implementing Monsters ( NPC's ) into my mmo...

Postby genar » 04 Jun 2018, 14:36

Dear SmartFoxServer team !

Im really glad that you guys developed this awesome api ... it really makes things so much easier and helped me to develop my first mobile mmorpg...

But theres one problem... im currently implementing Monsters/NPC's into my game... and there a lots of them. My game is basically an open world game where you can go where ever you want. And mostly i generate about 20-50 monsters near the player, most of them are in his field of view. I generate them server side and push them right into my database. The client gets a ISFSArray and processes it to make those monster spawn... but theres one problem.

They dont act like npc's or monster... they are just some objects i spawn. Thats why i want to bring them alive.

But i have no clue how i should do that... I saw i could use the NPCManager to create User like instances. This may work great with a little amount of monsters... but wasting 20-50 user slots for NPC's isnt really an option, because i use the free smartfox server version and im still a student and dont have the money to upgrade.

Futhermore each of those monster should just walk to another position every few seconds. They should have some kind of trigger area where they start attacking the player. But thats all... nothing special.

So what else could i do ? Isnt there a way to disable the consumption of user slots by npcs ?
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Implementing Monsters ( NPC's ) into my mmo...

Postby Lapo » 05 Jun 2018, 08:46

Hi,
you don't need to create NPCs as Users. You can create your own entities and send updates about them to the players.
If you use MMORooms you could create them as MMOItems.
Otherwise they are just objects in memory, managed by your Extension which will update the clients which will animate them.

Makes sense?

You mentioned the NPCManager so you probably have already read these?
https://smartfoxserver.com/blog/ways-of ... -game-p-1/
https://smartfoxserver.com/blog/ways-of ... -game-p-2/

If not I'd recommend the first one in particular. The strategy explained can be applied for any type of NPCs.

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
genar
Posts: 137
Joined: 13 Jul 2017, 11:49

Re: Implementing Monsters ( NPC's ) into my mmo...

Postby genar » 05 Jun 2018, 10:10

Lapo wrote:Hi,
you don't need to create NPCs as Users. You can create your own entities and send updates about them to the players.
If you use MMORooms you could create them as MMOItems.
Otherwise they are just objects in memory, managed by your Extension which will update the clients which will animate them.

Makes sense?

You mentioned the NPCManager so you probably have already read these?
https://smartfoxserver.com/blog/ways-of ... -game-p-1/
https://smartfoxserver.com/blog/ways-of ... -game-p-2/

If not I'd recommend the first one in particular. The strategy explained can be applied for any type of NPCs.

Cheers


Thanks a lot ! :) One more question... for example... if i generate a few of those mmoitems as "NPC'S"... how should i update them ? Or how could i attach my own AI logic ? Should i just put them into a list and call an extension which loops over this list every few seconds ? And even if then... i have no idea how i could attach my code ... An little example would be great >.<
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Implementing Monsters ( NPC's ) into my mmo...

Postby Lapo » 06 Jun 2018, 07:51

genar wrote: Or how could i attach my own AI logic ? Should i just put them into a list and call an extension which loops over this list every few seconds ?

Yes, that's usually how it is done.
You can add a Task to the SFS scheduler every X milliseconds (for example 300ms) which will call your NPC logic which cycles through all NPCs and update their position, behavior etc...

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
Mrm83
Posts: 155
Joined: 17 Dec 2017, 04:02

Re: Implementing Monsters ( NPC's ) into my mmo...

Postby Mrm83 » 07 Jun 2018, 16:16

I am actually trying to implement this as well and i am thinking about this approach right now..

My npc logic will be on a separate server running as a client. SFS server will be requested to create X number of npcs and will respond back with all their ids to the server client.

server client will then create those X number of npcs and perform their logics. Every set interval, the server client will send SFS all their updated stats and SFS will update each accordingly to send to users within aoi.

Does this seem like a good approach? Is it performance heavy or is there a better way?

it seems like a lot of frequent traffic back and forth between server client and sfs.
ie:
if the game has 10000 npcs, the client server will need to send an array with 10000 npc stat to SFS and SFS will need to handle that every 50ms.
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Implementing Monsters ( NPC's ) into my mmo...

Postby Lapo » 08 Jun 2018, 08:17

Mrm83 wrote:I am actually trying to implement this as well and i am thinking about this approach right now..

My npc logic will be on a separate server running as a client. SFS server will be requested to create X number of npcs and will respond back with all their ids to the server client.

Does this seem like a good approach? Is it performance heavy or is there a better way?

It seems a little excessive to run a separate server for NPCs. Normally I would recommend to run everything on the SFS2X server side.
Unless your NPCs all require a very complex logic that justifies running them on separate hardware. I am not sure this is the case?

it seems like a lot of frequent traffic back and forth between server client and sfs.

Yes, that's another possible downside.

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
Mrm83
Posts: 155
Joined: 17 Dec 2017, 04:02

Re: Implementing Monsters ( NPC's ) into my mmo...

Postby Mrm83 » 08 Jun 2018, 10:23

Yes, it will need to be done outside of sfs as my npc server handles all the npc (monster) logic. There is pathing that needs to be calculated base on the map so sfs won't be able to handle that as it doesn't have map awareness.

The npc server will need to be aware of all players who are within the npc's aoi as well so sfs will need to update the npc server so it can calculate the pathing.

So I see problems to the approach (maybe not a problem at all so please correct me)

1- on every player movement update sfs needs to see if there are mmoitems (npc) within player aoi. If there are mmoitems within aoi, I need the npc server to be aware of this player so sfs will send a request to npc server about this player position.
Problem: doing proximity lookup for every player movement seems lots of processing on sfs? Or is it ok?

2- npc server moving lets say 5000 npc and notifying sfs about it's position every 100ms. So that's 5000 requesthandler call every 100ms.. This is probably too much for the sfs server? If it is, what would be a more realistic acceptable number? I am Assuming 1000 ccu all hunting at the same time with 5 monsters on screen. So that's 5000 updates of position and possibly another 5000 about its hp and status updates from sfs back to npc server.

3- not sure if it makes a difference or not.. but would it be the same in terms of traffic speed if the npc server is running on the same instance of the sfs server vs npc server running on another instance/stack/node (not sure what you call it) in the cloud (ie: jelastic1-sfs, jelastix2-npc)
genar
Posts: 137
Joined: 13 Jul 2017, 11:49

Re: Implementing Monsters ( NPC's ) into my mmo...

Postby genar » 08 Jun 2018, 10:48

Lapo wrote:
Mrm83 wrote:I am actually trying to implement this as well and i am thinking about this approach right now..

My npc logic will be on a separate server running as a client. SFS server will be requested to create X number of npcs and will respond back with all their ids to the server client.

Does this seem like a good approach? Is it performance heavy or is there a better way?

It seems a little excessive to run a separate server for NPCs. Normally I would recommend to run everything on the SFS2X server side.
Unless your NPCs all require a very complex logic that justifies running them on separate hardware. I am not sure this is the case?

it seems like a lot of frequent traffic back and forth between server client and sfs.

Yes, that's another possible downside.

Hope it helps


Well i finally managed to implement my own NPC system...

1. I generate all the monsters/mobs and npc's and pack them into a big ISFArray
2. I insert all those entitys into my database ( this only takes about 20 - 50 ms )
3. I start an task scheduler for this generated bunch of entitys. On task start the scheduler loops over his assigned entitys to create and spawn in the
certain mmoItems.
4. The task scheduler loops over his assigned entity list every second to update the mobs. Futhermore it checks at the same time for each entity, if its
either dead or if there arent players nearby ( aoi, proximity ). If thats the case, i either delete the entity from the list and the db or just from the
list.
5. With this mechanic every mob generation extension call will deploy one new task scheduler which processes the certain entitys. There isnt one
big loop for all the entitys, instead there many little ones which makes this system more flexible. Futhermore, to avoid redundancies, theres also
a global map which contains all loaded and active mobs. When the task scheduler starts, it checks if the list doesnt contain the certain mob. This
avoids that multiple task scheduler calculates the same mob.

Well i havent checked the performance yet, but it seems to run pretty smooth. And due to the task scheduler, which works like a multi thread, it works pretty great.
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Implementing Monsters ( NPC's ) into my mmo...

Postby Lapo » 08 Jun 2018, 13:56

genar wrote:Well i finally managed to implement my own NPC system...

Sounds great.
One advice: if you're creating a lot of Task Schedulers you should consider a different approach, where instead you run just a few Schedulers or even better just one that takes care of all tasks you need to run.

Tasks are not very expensive but TaskSchedulers are.
Even if you have hundreds of Tasks you can still them run everything inside one Scheduler. Scheduler manage threads very efficiently and it's not a problem to configure a Scheduler with 50-100 threads (or even more, if necessary).

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Implementing Monsters ( NPC's ) into my mmo...

Postby Lapo » 08 Jun 2018, 14:27

Mrm83 wrote:1- on every player movement update sfs needs to see if there are mmoitems (npc) within player aoi. If there are mmoitems within aoi, I need the npc server to be aware of this player so sfs will send a request to npc server about this player position.
Problem: doing proximity lookup for every player movement seems lots of processing on sfs? Or is it ok?

The amount of processing depends on the number of NPCs. The higher the count the more processing needs to be done but it's difficult to predict how "heavy" this could as it depends on multiple variables, including the hardware specs.

2- npc server moving lets say 5000 npc and notifying sfs about it's position every 100ms. So that's 5000 requesthandler call every 100ms.. This is probably too much for the sfs server?

Not really, Moving an NPC is very inexpensive. Also you don't need to send one single request at a time but rather bundle them and send them together (not necessarily all of them, but you get the idea). In any case it would be highly advisable that both servers (NPC, SFS2X) run in the same network to avoid adding lag.

3- not sure if it makes a difference or not.. but would it be the same in terms of traffic speed if the npc server is running on the same instance of the sfs server vs npc server running on another instance/stack/node (not sure what you call it) in the cloud (ie: jelastic1-sfs, jelastix2-npc)

Running two machines in the same network is sufficiently fast.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
Mrm83
Posts: 155
Joined: 17 Dec 2017, 04:02

Re: Implementing Monsters ( NPC's ) into my mmo...

Postby Mrm83 » 08 Jun 2018, 15:46

Lapo wrote:
Mrm83 wrote:1- on every player movement update sfs needs to see if there are mmoitems (npc) within player aoi. If there are mmoitems within aoi, I need the npc server to be aware of this player so sfs will send a request to npc server about this player position.
Problem: doing proximity lookup for every player movement seems lots of processing on sfs? Or is it ok?

The amount of processing depends on the number of NPCs. The higher the count the more processing needs to be done but it's difficult to predict how "heavy" this could as it depends on multiple variables, including the hardware specs.

Cheers


I know its hard to predict, but with the experiences you have could you get me a rough ok/not ok assuming the following?
server:
aws ec2 m4.large or jelastic equavlent for running sfs only
another server or same server for client server to handle npc logic
10000 npcs
1000 players (100ms position updates)
so i guess this would be 10000 proximity checks every second

defining limits early would really help me in architecting the game
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Implementing Monsters ( NPC's ) into my mmo...

Postby Lapo » 11 Jun 2018, 07:42

What you're asking is very specific and even with our experience with SFS2X I am not able to say how it will work under those specific circumstances. I cannot simulate that in my mind and tell you how well it will run :) I am not that good :)

The best idea in these cases is to create a small test prototype that creates 10K NPCs, puts them in an MMORoom and runs the proximity tests. Then you deploy it onto the m4.large instance and see how it behaves.

Based on the results you will be able to understand if the EC2 instance is enough, or if you need something smaller or bigger.

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 129 guests