[Include] RNPC Pool - Quick, direct and fully managed access to a pool of NPCs
#1

This include doesnt really provide a special feature, it is mainly an include to optimize NPC usage, reduce wasted player slots, and allow a better game experience when working with NPCs.

What is it?

The include actually provides a pool of ready RNPCs that can be used anytime for anything. It will fill the pool on server start, and once the NPCs connected they can be claimed for any task, and later returned to the pool to be available again for the next task. This might sound pretty useless, but this system has great advantages when working with many NPCs with minor roles - like NPCs standing in a shop - or in general NPC streaming.
You could either connect 50 NPCs and put them all in the shops, where they are standing all day, taking up 50 player slots, even if theres noone in their shop.
Or you connect a pool of 10 NPCs, and whenever a player enters a shop, a NPC from the pool is picked and teleported into the shop. And once the shop is empty again, the NPC is returned to the pool to be used again later.
It is very unlikely that there is some player in every shop at the same time, so by using the pool you can reduce the wasted slots without affecting the game experience. This can safe you real money if youre paying your server hoster per slots.

Sure, you could just connect a new NPC when a player enters a shop, and kick him once the player leaves, but the major problem about this is, that NPCs - just like players - need some seconds to connect and spawn. And it would totally ruin the game experience when the shop owner suddenly pops up seconds after you entered the shop.
--> Streaming NPCs nicely without a pool is impossible.

This include doesnt provide any streaming or NPC actions, it just offers the pool. I might however release an example script later that explains how to use the pool for streaming, but it really isnt difficult. The pool got a constant maximum size, and a current size that can be changed during the runtime. So if you notice youre running low on available NPCs you can simply increase the pool without restarting the server. Or if youre low on player slots, you can reduce the pool size to get additional free slots.


Usage:
This include is based on RNPC, so it requires the RNPC plugin.
Theres just some small reference to it, so it may also be changed to work with FCNPC or the stock NPCs.
Consider that connecting too many NPCs at once may cause flood warnings. So do not increase the pool size in too big steps, or the pool might get buggy.

before including this include in your script, you may define one of these settings:
pawn Код:
#define RNPC_POOL_MAXSIZE   // The absolute maximum pool size (default: 50)
#define RNPC_POOL_STARTSIZE  // The initial size of the pool, will be filled on server start
Anywhere in your script you might use one of these functions then:
pawn Код:
// Returns the playerid of a idle NPC from the pool, or INVALID_PLAYER_ID if the pool is empty
// The NPC counts as "in use" then, and must be returned later to be available again.
// fortask sets an ID for the current task of the NPC, this might be helpful for some stuff
PickRNPCFromPool(fortask)

// Returns the ID of the current task of the pool NPC,
// or -1 if the NPC either isnt in the pool, or currently got no task
GetPoolRNPCRole(npcid)

// Puts a NPC back into the pool, making him available for
// later use again
ReturnRNPCToPool(npcid)

// Sets the desired new size of the pool, either kicking
// NPCs, or connecting new ones
// The noactivekick parameter sets whether when reducing the
// size, only inactive NPCs should be kicked, even if the new pool
// size cant be reached then
// Returns the new pool size
SetRNPCPoolSize(newsize, noactivekick=1)

// Callback: Is called when the pool is used up, and
// a NPC was requested, but couldnt be provided
// might be helpful to increase the pool size automatically
OnRNPCPoolFull(curSize, maxSize)
The source code also is widely documented, so this include may be used to learn more about how a pool could work.

Im very aware that this isnt interesting for everyone, but Im pretty sure that most people working with NPCs can benefit from it.
Any questions or suggestions? Just post them here.

Download/Links
Version 1.0 (3.7.2014)
RNPC plugin
Reply
#2

Pretty nice include.
Reply
#3

I like the idea of connecting all the NPCs you need it keeps the player ids organized.
Reply
#4

Yep, experimented a bit with it so far, and its definitely the most comfortable way of "spawning" NPCs ive had so far, no need to use OnPlayerConnect and OnPlayerSpawn to set up the NPCs, just get an ID and it can be used instantly.
I might change this a bit, to give an option to vary the pool size automatically, so whenever a NPC from the pool is picked, another one connects to the server. And when returning it it gets disconnected, to always have a specific amount of "buffered" NPCs ready. I think that could be even more handy, as long as the buffer size is big enough, as you wouldnt need to care about connecting NPCs manually at all then.

Im currently working on an example script to show how to use this for NPC streaming, specifically to populate shops dynamically. That should make the use of this include much clearer.
Reply
#5

I think it's better to just keep all the NPCs you need connected I will give some reasons.

- It's best to keep player slots separate from NPC slots
- Dynamically connecting NPCs will change the server slot count which some server owners might not want

I'm not saying that it won't work but from a quality point of view its probably not a good choice.
Reply
#6

Quote:
Originally Posted by [uL]Pottus
Посмотреть сообщение
I think it's better to just keep all the NPCs you need connected I will give some reasons.

- It's best to keep player slots separate from NPC slots
- Dynamically connecting NPCs will change the server slot count which some server owners might not want

I'm not saying that it won't work but from a quality point of view its probably not a good choice.
Id say that depends on the point of view. It might look better if the total player slots are constantly the same, but by means of performance, additional players lead to decreased performance, and thats true for real players and NPCs, even if they arent streamed in for the others (the impact will be small, but existing). Also, what I mentioned in the first post, NPC streaming can safe player slots when they are needed. When going with gamehosters you pay for slots, including the NPCs, so why pay for 10 if you never need more than 5 at the same time? NPCs usually arent important enough to justify blocking the slot from a real player. Id rather play with one more real player than with one more shop NPC if the server is full
But yep, that mostly depends on the server itself, and the importance of the NPCs roles. If its a NPC zombie mode its pretty much the opposite of course, thats why it definitely will be toggleable if I decide to add it.
Reply
#7

I'm not saying it's a bad idea just pointing out why someone might not want to use it but hey if someone can only afford $10 a month they might need to do that.

@Edit

One thing here.

pawn Код:
// Returns the ID of the current task of the pool NPC,
// or -1 if the NPC either isnt in the pool, or currently got no task
GetPoolRNPCRole(npcid)
I think you should make a distinction if the NPC isn't in the pool or has no task then make some defines.

pawn Код:
#define RNPC_ROLE_NOPOOL -1
#define RNPC_ROLE_NOTASK -2
Reply
#8

Good work!
Reply
#9

Okay;
Код:
(*)\pawno\include\rnpc_pool.inc(221) : error 004: function "RNPCPOOL_OnRNPCPlaybackStopped" is not implemented
Any thoughts on how this could be possible?
Reply
#10

interesting i will read this later on ..
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)