Heres the new update! Due to the major feature updates I decided to make it 0.4, even though I had other stuff planned for 0.4.
In short, 0.4 makes NPCs killable.
Slightly longer, heres a list of all changes:
- Added serversided health management for NPCs, with correct handling of NPC deaths
- Correctly synced OnRNPCPlaybackStopped
- Fixed a major memory leak bug that messed up NPC control in timers (that one was also fixed in the 0.3.4 package earlier today)
- New callbacks added
- OnRNPCDeath(npcid, killerid, reason)
- OnRNPCVehicleHit(npcid, driverid, vehicleid, times)
- New functions added
- RNPC_PauseRecordingPlayback(npcid) / RNPC_ResumeRecordingPlayback(npcid)
- RNPC_ToggleVehicleCollisionCheck(npcid,enable)
- RNPC_SetShootable(npcid,enable)
- SetRNPCHealth(npcid, Float:health, issuer=INVALID_PLAYER_ID, reason=53) / Float:GetRNPCHealth(npcid)
- IsVehicleOnPlayer(playerid, vehicleid)
- RespawnRNPC(npcid)
And in long:
This update finally brings in a feature that I originally didnt even want to add. NPCs now got a working equivalent of health, they can be
damaged, shot, hit by vehicles, and killed. But just if you want them to.
In order to stick with RNPCs aim of maximum customizability I had to make some tricky decisions. But after all im quite happy with the result. You can turn on/off automatic weapon and vehicle damage. So either you enable it (default: disabled) and accept what I thought would be good damage values (GTA standard damage), or you keep it disabled, and instead use the new and old callbacks to handle damage on your own way.
Same about the stuff that happens when a NPC dies or gets hit by a car. Dead NPCs respawn automatically after 4 seconds, play a random one of the 4 default death-fall animations, and OnPlayerDeath is called to treat NPC kills like normal player kills. If you dont like that, return 1 in OnRNPCDeath to skip those default actions. Anyways, dead NPCs will ignore playback requests, so there is no chance a dead NPC is accidentally moving.
If a NPC is hit by a car, and collision detection is actually turned on for him, he will pause the playback, get (160*vehicle_velocity) damage, and 1 damage for every 100ms the vehicle is still standing on top of them. After 2200ms he will resume the playback, thats about the time until he stands up. Again, if you dont like those damage values, return 1 in OnRNPCVehicleHit to skip the damage.
Performance
I did a quick performance test for this version, and managed to fire a minigun into a bulk of 330 NPCs running after me, without noticeable server lag. Then the client crashed. There was no big server lag, though the client lagged like hell, probably because the NPCs run through each other, and I guess 330 intersecting player models are too much for the GTA engine.
Those NPCs had the vehicle collision check turned off though. That collision check is the first thing that actually makes use of a concept I thought of quite a while ago: it uses the NPC scripts to calculate stuff in an own process without lagging the server. Each NPC with the detection turned on runs a timer that checks for occupied vehicles nearby. If there is one, the NPC reports that to the server for a closer check (NPC scripts miss some functions that are needed for the exact check, so this has to be done by the server). That way the checks can run in a fast 100ms interval without lagging the server. Anyways, if there are many players in vehicles, driving through many NPCs, there are going to be thousands of vehicle reports coming from the NPCs every second, and this got good potential to lag the server, even though the exact collision check isnt very complex.
It is very hard to give a general suggestion for how many NPCs should have collision enabled, this greatly depends on the server. Best thing is to try it, maybe do a stress test with many players and many NPCs.
Known new bugs and things to consider- NPCs sometimes will stop vehicles on hit, like driving against a wall. This especially happens when driving into a group of NPCs. Its a SA-MP thing, and theres nothing RNPC could do against it.
- NPCs killed by a vehicle will stand up after dying. I had to decide for this, or to apply a manual animation, but the latter one would block vehicles and make driving over npcs really annoying. So I rather picked the "ugly" option.
- The minimum collision range for vehicles is 6.0 (can be changed in the RNPC npc script). Vehicles longer than that wont trigger OnRNPCVehicleHit, until the rammed NPC gets into that range. I picked that value as a good mean, there are just a few vehicles longer than that (mostly planes), but it improves performance.
- Dead NPCs might keep moving. This is a very rare "bug" caused by asynchronism. Dead NPCs normally ignore playback requests, but only after they stopped their last playback (they stop automatically when dying). This is a 5-10ms timeframe, so it happens very rarely for NPCs moved in a timer. A good and very simple fix for this is to check if a NPC is dead (rnpcData[npcid][RNPC_DEAD]) before moving NPCs in timers.
So much to the new version. I hope with that new mid-level damage management RNPC got one step closer to becoming an easy NPC solution.
For detailled description of all new functions check the
RNPC wiki page (Edit: it is updated now)
The download link is on the first page. Additionally theres a pastebin link for a very basic "zombie npc" include that allows easy testing of the new damage management.
I know this might sound difficult in some points. Feel free to ask if anything of this is too confusing.
Have fun!
(damn those release posts get longer every version, no wonder there arent many people using it if they need to read a 100000 word text before
)