Questions about NPCs and Terrain data

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

Moderators: Lapo, Bax

User avatar
Jihaysse
Posts: 11
Joined: 28 Aug 2021, 12:23

Questions about NPCs and Terrain data

Postby Jihaysse » 21 Oct 2021, 11:28

Hello,

First of all, thank you for this amazing framework. I've tried the MMO demo and it works really great.
After reading the docs and the forums posts related to my issues, I have several questions concerning the architecture of my project with SFS.

A few words about the specifications:
* Turn-based, openworld MMO but the player is on a grid during battle (no grid when not in battle).
* Click-to-move (like a Top-Down RPG). I send the x, y, z coordinates using UserVariables when a player clicks somewhere, and let clients calculate the path with NavMeshAgent on their side. I guess it's more suited than contiunously updating the position (like you probably would in WoW or most MMOs).
* Monsters should be roaming around every X seconds when not in battle. During battle, they'll be on a grid along with the player and wait for their turn to play.
* I have a grid that gets activated on the terrain during battle, I know how to use it for client-side authority but the issue is to make it server-authoritative. Some cells are inactive (e.g. there is a tree or a house on it). Pathfinding & line of sight are currently client-side too.

So, my questions:
1) I've read mixed things about NPCs (note: monsters in my case, not idle vendors/quest givers). There are documentations which say to spawn them as normal SFSUsers, but I've read on the forums that it may be better to spawn them as MMOItems or custom entities. In the docs, it is written that MMOItems are more suited to stuff like powerups, weapons, etc... Which way would fit better for my implementation?

2) I definitely need to make the whole grid system (cells, pathfinding, line of sight calculations etc) server-authoritative. So, the server need to be aware of the grid. Should I (try to) export it as a JSON and use an Extension?

3) The server also needs to be in some way aware of the terrain as the monsters need to be roaming around. The easiest way - from what I've read - would be to use a Unity headless server which lives in the same network as SFS. What are the downsides of such implementation? Can it still handle as much CCU as SFS? What would be my other options (how to tell the server about the terrain?).


Thank you very much for your time, I really appreciate it.

Kind regards,
Jihaysse
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Questions about NPCs and Terrain data

Postby Lapo » 21 Oct 2021, 13:13

Hi,
1) I've read mixed things about NPCs (note: monsters in my case, not idle vendors/quest givers). There are documentations which say to spawn them as normal SFSUsers, but I've read on the forums that it may be better to spawn them as MMOItems or custom entities. In the docs, it is written that MMOItems are more suited to stuff like powerups, weapons, etc... Which way would fit better for my implementation?

You can create NPCs with the SFSApi.createNpc() method. This will create an SFSUser that you can control from the server side.
When is this useful? When you need your NPC to behave like users: i.e. they will appear in the User List of a Room, they can create and access User/Room Variables etc...

If these features are not relevant for your game then you can implement your own entities system, be it based on MMOItems or some other class that fits your app logic.

2) I definitely need to make the whole grid system (cells, pathfinding, line of sight calculations etc) server-authoritative. So, the server need to be aware of the grid. Should I (try to) export it as a JSON and use an Extension?

Yes, sounds like viable solution.

3) The server also needs to be in some way aware of the terrain as the monsters need to be roaming around. The easiest way - from
what I've read - would be to use a Unity headless server which lives in the same network as SFS. What are the downsides of such implementation?

It depends on how complex is this terrain data. If it's something that is too complicated to export on the server side then the headless server is probably the best option.
Can it still handle as much CCU as SFS? What would be my other options (how to tell the server about the terrain?).

That's difficult to say, as it depends on many factors, the main one being the complexity of the logic that needs to be executed for every moving entity.

Question: you mentioned that monsters need to roam around the terrain. If the Unity server is used only for NPC simulation I don't think it's necessary to handle players directly. I think it could probably just run its calculations and then update SFS2X which in turn could update the clients. Makes sense?

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
Jihaysse
Posts: 11
Joined: 28 Aug 2021, 12:23

Re: Questions about NPCs and Terrain data

Postby Jihaysse » 21 Oct 2021, 15:17

You can create NPCs with the SFSApi.createNpc() method. This will create an SFSUser that you can control from the server side.
When is this useful? When you need your NPC to behave like users: i.e. they will appear in the User List of a Room, they can create and access User/Room Variables etc...
I don't need the monsters to appear in the user list, although I need them to be able to access user variables (e.g. a player's health as I believe it's where it belongs?). Is it not possible with a custom implementation (e.g. a Monster class derived from MonoBehaviour)? Or maybe the player's health doesn't belong in UserData? (I mean its current health during a battle, e.g. 64/100, not its "base" health which will be stored in a database).

It depends on how complex is this terrain data. If it's something that is too complicated to export on the server side then the headless server is probably the best option.
The terrain isn't relatively complex as it's a top-down view, something along those lines: https://youtu.be/iPICW4MEgQA?t=909
The thing is, how would I export the terrain data to the server and know if it's a rock, tree or the ground?

That's difficult to say, as it depends on many factors, the main one being the complexity of the logic that needs to be executed for every moving entity.
The "roaming" logic is fairly simple: find a viable destination (Vector3) each 10~20 seconds, send that destination accross the network as x,y,z, client receives the data, client calculates the path and makes the entity moves to that destination. So I believe it's a really simple implementation. It doesn't matter if 2 clients doesn't have exactly the same path as long as they have the same final position. The issue here is the first step: to find a viable destination around the monster, let's say in a Vector3(5, 5, 5) distance. If I was doing it randomly, it may go to a rock or a house, or somewhere it doesn't belong. That's why I guess I need the terrain data to be on the server, is that right?

Question: you mentioned that monsters need to roam around the terrain. If the Unity server is used only for NPC simulation I don't think it's necessary to handle players directly. I think it could probably just run its calculations and then update SFS2X which in turn could update the clients. Makes sense?
The Unity server would indeed only be for NPC simulation (find a viable destination, know which cells are available during a battle, etc...). This is what I was thinking too. But maybe it's easier in my project to run everything on SFS without a Unity server? I feel like it might complicate stuff as I have a pretty basic terrain although I have no idea how to even start coding the pathfinding/line of sight algorithms on the server.

Kind regards,
Jihaysse
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Questions about NPCs and Terrain data

Postby Lapo » 22 Oct 2021, 10:02

I don't need the monsters to appear in the user list, although I need them to be able to access user variables (e.g. a player's health as I believe it's where it belongs?). Is it not possible with a custom implementation (e.g. a Monster class derived from MonoBehaviour)?

If you implement the whole monsters AI in the Unity headless server you can do as you please. It doesn't matter what type of class you use / inherit.

The thing is, how would I export the terrain data to the server and know if it's a rock, tree or the ground?

That I don't know :)
It think it requires deeper analysis of what data you have and how it can be exported.

The "roaming" logic is fairly simple: find a viable destination (Vector3) each 10~20 seconds, send that destination accross the network as x,y,z, client receives the data, client calculates the path and makes the entity moves to that destination. So I believe it's a really simple implementation. It doesn't matter if 2 clients doesn't have exactly the same path as long as they have the same final position. The issue here is the first step: to find a viable destination around the monster, let's say in a Vector3(5, 5, 5) distance. If I was doing it randomly, it may go to a rock or a house, or somewhere it doesn't belong. That's why I guess I need the terrain data to be on the server, is that right?

Maybe what you need is a simplified map for the server side, which only contains all the obstacles or non-walkable tiles/areas.
I guess to generate such data one would have to integrate some extra code in the Unity Editor to process the terrain and produce the static map for the server side.

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Jihaysse
Posts: 11
Joined: 28 Aug 2021, 12:23

Re: Questions about NPCs and Terrain data

Postby Jihaysse » 22 Oct 2021, 13:15

If you implement the whole monsters AI in the Unity headless server you can do as you please. It doesn't matter what type of class you use / inherit.
Can I use a custom implementation without a Unity headless server? (e.g. in an Extension). Will they be able to access UserVariables? I'd prefer not to deal with a headless server if I don't need to (see next answer) and I'm afraid of the scalability of such implementation. Also, I'd prefer monsters not to appear in a User list.

Maybe what you need is a simplified map for the server side, which only contains all the obstacles or non-walkable tiles/areas.

That's also what I thought yesterday. The solution I found would be:
* Generate the grid on Unity. I can manually deactivate the cells where there is a tree, rock or whatever.
* Upload a JSON of the grid as an Extension (which is probably the hardest part)
* The server doesn't know anything about the terrain itself but knows about the grid => monsters (NPC) will only be able to move using the grid. They can randomly choose a close cell and send that destination cell to the clients, which will calculate the path themselves. Players are free to move how they want (while not in battle) but monsters will only move in a grid pattern. Although not perfect, I believe it simplifies a lot my issue and I believe it won't be noticeable for players or decrease immersion by much.

I guess having the grid on the server may also solve my issue of server-authoritative line-of-sight/pathfinding but, one thing at a time :D

Thanks for the help, Lapo!

Kind regards,
Jihaysse
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Questions about NPCs and Terrain data

Postby Lapo » 23 Oct 2021, 09:49

Can I use a custom implementation without a Unity headless server? (e.g. in an Extension).

If you can translate the Unity terrain data to some other format that can be loaded by your Java Extension then Yes.

Will they be able to access UserVariables?

Yes. Extension code can access any User data, not just User Variables.

I'd prefer not to deal with a headless server if I don't need to (see next answer) and I'm afraid of the scalability of such implementation.

Yeah, the scalability aspect would require some testing to see how much it can handle.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 66 guests