Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411507 Posts in 69374 Topics- by 58429 Members - Latest Member: Alternalo

April 26, 2024, 04:00:18 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Handling more and more NPC's in a procgen universe
Pages: [1]
Print
Author Topic: Handling more and more NPC's in a procgen universe  (Read 657 times)
PetSkull
Level 2
**


View Profile
« on: June 11, 2018, 12:49:16 AM »

Hi,

for a while I've been creating a 2D procedurally generated space game, in the spirit of the original Elite series. And so far I've created NPC's that will live for the life span of the game, meaning that more and more NPC's will be created on the fly as the game progresses. All of these NPC's have their own life, own missions and own goals and right now I'm updating them on each tick, even though they might cover my entire universe and are by no means visible to the player.

On my test setup I have created 10 star systems each with up to 8 planets and so far it has created 422 NPC's roaming the universe. I can only imagine how this number will expand as my universe expands over time.

I can see a problem with updating all of them at every tick as I will need more and more cycles as the number increases and this I have to avoid. So far my idea is to pool the NPC's in chunks and only update 2 chunks on every tick.

Chunk 1 : NPC's on the same planet as me
Chunk 2 : A fixed number of NPC's on other planets
Chunk 3 : A fixed number of NPC's on other planets
Chunk 4 : etc

So on tick 1 I will update chunk 1 and 2. On tick 2 I will update chunk 1 and 3 and so on.

But will this be ok for the game? I'm unsure. This means that NPC's on other systems wont get updated as often, but I'm not sure if it will have any impact on the gameplay, will the player even notice this?

How do you deal with this problem?



Logged

TheSambassador
TIGBaby
*


View Profile
« Reply #1 on: June 11, 2018, 01:33:07 PM »

What are your NPCs doing on every tick?  What sort of things are you simulating?

You definitely can't update the entire universe on every tick.  That won't scale at all.  Ideally, you only would update the NPCs that matter, which in most games are the NPCs that your player can actually see / interact with. 

If it was extremely important that the NPCs get "fully simulated", you could consider something like this:

- When the player gets far enough from the NPC, you "unload" it and stop updating it.
- When you unload the NPC, keep track of the current "tick count" or some other sort of "time" variable to keep track of when the NPC was unloaded.
- Then, when your player comes close enough to the NPC again, you can re-load the NPC, check when the last time it was updated, and then simulate a number of ticks to "catch it up" to the current time. 

However, this might have some consequences if you leave an area of the universe for a long period of time, then come back, and you have millions of ticks for a bunch of NPCs to simulate.  If you have a loading screen for this you could just do all this then, but otherwise you'd need to spread it across multiple frames or something to stop it from slowing down your game.
Logged
PetSkull
Level 2
**


View Profile
« Reply #2 on: June 11, 2018, 01:59:21 PM »

All NPC's have their own missions and own lives to live. A trader will travel from planet to planet, buying and selling goods, influencing the supply/demand and prices of commodities where he visits. Pirates will attack lonely traders and you for goods. Police will guard star ports and attack offenders. Escorts will escort larger ships to and from start ports/targets. So every NPC has a job in the universe and has influence on the universe's expansion.

This is the reason why I can't just let them sleep when far from the player.

But I'm moving further towards my original idea of updating them in chunks, 2 chunks per game tick. And always updating the NPC's on the same planet as the player.
Logged

Tattomoosa
Level 0
**

Making The Decision Engine


View Profile WWW
« Reply #3 on: June 11, 2018, 02:53:41 PM »

Chunks is a reasonable way to handle it for now but if you want to keep getting bigger, eventually you're going to have to limit your actors or use a simplified (and/or asynchronous) simulation for actors further away.

Can you simulate far away NPCs in 2D? Can you get the distance an NPC far from the player has to travel, disable the NPC and count down a timer until they reach their destination, then re-enable them? Reduce distant battles to simple dice rolls? Etc. Maybe you can ONLY simulate a full NPC when it's completed an action or it's current action is interrupted, and otherwise use just a simplified algorithm, like path-finding if they're moving.

There's a lot you can do, but it depends on the rules of your game. Maybe you wind up with 2 or 3 related systems at different 'levels of detail'. Even then, there will always be an upper limit on what you can simulate.
Logged

PetSkull
Level 2
**


View Profile
« Reply #4 on: June 12, 2018, 10:06:26 PM »

I like your idea of simplified simulation when the NPC is far away and wont impact the current events. I will look into that. Yesterday I implemented chunking and it works flawlessly for now. But I gotta see how it performs when numbers get higher. Right now it's tuned to handle up to 3000 NPC's in the entire universe, without NPC's being skipped entirely.
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic