Currently the developers are putting their own money into JC2-MP to keep the servers online.

Please take a few seconds of your time and disable your AdBlock plugin for our website.

Ad revenue is not going to developers, it is used purely for covering our hosting costs.

 

You are also free to donate, which removes all ads from our website!

Patch 0.3 was just released! Full changelog here: https://t.co/4A50m6IKen

2 years ago

Advertisement
July 22, 2019, 05:49:34 am

Author Topic: Basic NPC AI  (Read 15367 times)

SinisterRectus

  • JC2-MP Betatester
  • Sr. Member
  • *****
  • Posts: 451
    • View Profile
Basic NPC AI
« on: April 26, 2015, 02:27:30 am »
I wanted to see how possible (or impossible) it is to implement some form of NPCs. Here is my progress after a week or so.

Video: https://youtu.be/fMwp-mLobog







Their AI is limited to wandering around at random intervals in random directions. No collision checks or path finding or anything like that.

The wireframes are recordings of Rico's bone positions and angles. Animating 3D human models does not seem possible. I could probably build a mecha-NPC with static objects, but I'm not sure how well that would fit in the JC2 universe.

I was able to adapt a custom hit detection script that I was already working on, so you can damage and kill the NPCs. I gave them a very basic hitbox that unfortunately doesn't stop bullets or raycasts. I'd like to use an invisible static object instead, but I was having trouble with getting the objects to spawn all the time (I hear this is going to be fixed in 0.1.5).

The NPCs are synced between the server and clients. In the video above, the sync rate was 200 ms and the stream distance was 500 m.

Not sure how far I'll get with this project. I'll answer questions if you have any, but I don't plan to release any full scripts at this time.
« Last Edit: April 26, 2015, 04:45:34 am by SinisterRectus »

BlueFireGhost

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Basic NPC AI
« Reply #1 on: April 26, 2015, 04:14:11 pm »
-snip-

[Please don't quote massive posts like that.]

DUDE that is awesome
« Last Edit: April 26, 2015, 04:25:20 pm by dreadmullet »

LordNoob

  • Sr. Member
  • ****
  • Posts: 349
    • View Profile
Re: Basic NPC AI
« Reply #2 on: April 26, 2015, 05:45:19 pm »
That's pretty cool. One question though - have you tested how well it works with a large amount of NPCs? As I understand it, the two main hurdles were the aesthetics (it would be quite difficult to use an actual character model here) and the performance when lots of them are needed, in applications such as simulating traffic.

SinisterRectus

  • JC2-MP Betatester
  • Sr. Member
  • *****
  • Posts: 451
    • View Profile
Re: Basic NPC AI
« Reply #3 on: April 26, 2015, 06:46:27 pm »
I run a temporary local server on my desktop, where I also run my client, so it's hard to benchmark. I think I had 3-4 players connected and 5-10 NPCs and I didn't hear any desync complaints.

On the server, each instance of an NPC updates every 20 milliseconds minimum and syncs to clients every 200 ms minimum. This could get slow if there are a lot of players connected, because each NPC has to iterate through every player on the server and check to see if they are within the stream distance. Maybe there is a better way to do this, but it works for now.
« Last Edit: April 26, 2015, 09:37:29 pm by SinisterRectus »

LordNoob

  • Sr. Member
  • ****
  • Posts: 349
    • View Profile
Re: Basic NPC AI
« Reply #4 on: April 26, 2015, 08:41:27 pm »
You could have the server send the table of NPC positions to each client and have the client loop through those and request NPC data from the server if it is within stream distance. I'm not sure what kind of impact this would have on FPS though, if any.

SinisterRectus

  • JC2-MP Betatester
  • Sr. Member
  • *****
  • Posts: 451
    • View Profile
Re: Basic NPC AI
« Reply #5 on: April 26, 2015, 09:36:41 pm »
That just generates more network traffic in both directions.

Edit: I just realized that I will probably have to move some calculations to the client anyway, as this is the only place where the NPCs can interact with the game world.

Edit2: ... or move the game world to the server ...
« Last Edit: April 27, 2015, 05:35:10 pm by SinisterRectus »

Greenhound

  • Jr. Member
  • **
  • Posts: 98
    • View Profile
Re: Basic NPC AI
« Reply #6 on: April 28, 2015, 09:29:21 am »
That just generates more network traffic in both directions.

Edit: I just realized that I will probably have to move some calculations to the client anyway, as this is the only place where the NPCs can interact with the game world.

Edit2: ... or move the game world to the server ...

The most important client functions for pathfinding and physics would be Physics:Raycast and Physics:GetTerrainHeight

You could get the closest client to calculate paths for the AI then broadcast the path as fixed points to the rest of the clients. AFAIK that's how DayZ do it.

jaxm

  • Developer
  • Jr. Member
  • *****
  • Posts: 89
    • View Profile
Re: Basic NPC AI
« Reply #7 on: April 28, 2015, 11:30:39 am »
That just generates more network traffic in both directions.

Edit: I just realized that I will probably have to move some calculations to the client anyway, as this is the only place where the NPCs can interact with the game world.

Edit2: ... or move the game world to the server ...

Hi there, nice work ^^

With the next version of jc-mp we're adding in WorldNetworkObjects, they'll be perfect for handling the streaming in / out of your NPC's.

On the subject of handling the path finding, moving the logic to the client is probably your best option as stated by others you do get access to the Physics. However, moving sufficient information to the server in order to generate paths isn't impossible. A little after I had fleshed out what I wanted to do with Invasion I started looking into writing AI for NPC pedestrians. The approach I ended up going with was moving height map data from the client to the server, that is saving chunks of the game world terrain collisions into 3D tables that the server loads up when it needs to generate a path through an area. The end result would generate paths such as this one



That was using Theta*, I had all the logic for my 'AI NPC's' running on the server, generating paths from information that, well looked like this visualized



My method had its obvious issues, for one they couldn't path under bridges / overhanging objects as there was no world information available. But those sorts of issues could be worked around / fixed.


I look forward to seeing which direction you end up deciding to go with your project :)

SinisterRectus

  • JC2-MP Betatester
  • Sr. Member
  • *****
  • Posts: 451
    • View Profile
Re: Basic NPC AI
« Reply #8 on: April 28, 2015, 02:30:04 pm »
Hi jaxm, thanks for the reply. You too, Greenhound. I have considered both options (streaming physics data from clients or pre-calculating it and storing it on the server). I think building a map of the game world for the server to use would be more interesting, so I've started down that route. I ended up with pretty much the same concept as yours, jaxm:

Raycast from above directly downwards and build a grid:



Mark the nodes as inaccessible if their slopes are are too great:



Or mark them as water if they are under y = 200:



Of course, I have the same problem of mapping terrain that is underneath bridges/overhanging objects. I have an idea to fix it that involves iterating in the y direction as well as x and z.

« Last Edit: April 28, 2015, 02:34:24 pm by SinisterRectus »

jaxm

  • Developer
  • Jr. Member
  • *****
  • Posts: 89
    • View Profile
Re: Basic NPC AI
« Reply #9 on: April 28, 2015, 05:27:02 pm »
Nice!  :o Looking good  :)

SinisterRectus

  • JC2-MP Betatester
  • Sr. Member
  • *****
  • Posts: 451
    • View Profile
Re: Basic NPC AI
« Reply #10 on: April 30, 2015, 05:06:36 pm »
I managed to solve the issue of mapping underneath objects by iteratively raycasting in the y direction. As you can see below, nodes are both on top of and below the bridge.



I also had to add a check in the x and z directions to exclude walls and such. Notice how connections are not made through the hangar wall.



My current problem is that a lot of structures like buildings and bridges are hollow, so I end up with grids mapped inside of objects that are not normally accessible.
« Last Edit: April 30, 2015, 05:29:00 pm by SinisterRectus »

Lord_Farquaad

  • Full Member
  • ***
  • Posts: 217
    • View Profile
Re: Basic NPC AI
« Reply #11 on: May 02, 2015, 02:12:22 am »
Just popping in to say that all of this is really awesome!  I can really see this evolving into some sort of full-fledged AI system.  Great work.

misterff1

  • Donator
  • Hero Member
  • *****
  • Posts: 582
    • View Profile
Re: Basic NPC AI
« Reply #12 on: May 07, 2015, 12:30:35 pm »
Absolutely cool to see more people working on this. I have been working on it for some time as well, but since you are much further than me I think I'll let you go do it :) . Btw, maybe it's an idea to let cars drive around on roads. That actually looks normal and can be used in servers. The bones are cool, but like you said there is no way to actually animate a human model in the game

SinisterRectus

  • JC2-MP Betatester
  • Sr. Member
  • *****
  • Posts: 451
    • View Profile
Re: Basic NPC AI
« Reply #13 on: May 07, 2015, 01:31:44 pm »
Btw, maybe it's an idea to let cars drive around on roads. That actually looks normal and can be used in servers.
I looked briefly at autonomous planes while I was working on my autopilot script. It seems that movable, unoccupied vehicles are limited to their stream distance. Even if the position is changed on the server, it's still treated according to the last known occupied position. I guess you can set a huge stream distance, but I don't know how practical that would be. The other solution I can think of is to build custom vehicles from scratch.

LordNoob

  • Sr. Member
  • ****
  • Posts: 349
    • View Profile
Re: Basic NPC AI
« Reply #14 on: May 07, 2015, 03:59:20 pm »
Btw, maybe it's an idea to let cars drive around on roads. That actually looks normal and can be used in servers.
The other solution I can think of is to build custom vehicles from scratch.

I believe that the model files exist for this (albeit in multiple parts), I am not sure if you could get it to work though.