NPC implementation in VCMP 0.4 Servers

habi

  • Sr. Member
  • Posts: 400
NPC implementation in VCMP 0.4 Servers
«  »Last edited
Introduction

An attempt to implement Non Playable Characters (NPC) for vc-mp has been made. The working of this may be taken similar to SA-MP.

NPCs also known as bots allows you to create NPCs inside your server who will work like virtual players. Like normal players, they use server slot. Their actions are scripted by squirrel scripts. These scripts with 'artificial intelligence' of NPC reside in npcscripts directory. If you want to have 10 different NPC doing 10 different things, you might want to script 10 different scripts.

Each NPC run in its own process. This might make their usage and working economical. The NPC scripts have functions as well as callbacks.

NPC-Wiki ( Click image to open )


Functions and callbacks documentation
Code: [Select]
---------------------------------------------------------------------------
NPC Callbacks
---------------
OnNPCScriptLoad()
OnNPCScriptUnload()
OnNPCConnect([integer] myplayerid)
OnNPCDisconnect([integer] reason)
OnNPCSpawn()
OnNPCEnterVehicle([integer] vehicleid, [integer] seatid )
OnNPCExitVehicle()
OnClientMessage([integer] r, [integer] g, [integer] b, [string] message )
OnPlayerDeath([integer] playerid )
OnPlayerText([integer] playerid, [string] message )
OnPlayerStreamIn([integer] playerid )
OnPlayerStreamOut([integer] playerid )
OnVehicleStreamIn([integer] vehicleid )
OnVehicleStreamOut([integer[ vehicleid )
OnRecordingPlaybackEnd()
-------------------------------------------------------------------------------
--------------
NPC Functions
--------------
StartRecordingPlayback([integer] playbacktype, [string] recordname )
StopRecordingPlayback()
PauseRecordingPlayback()
ResumeRecordingPlayback()
SendChat([string] text)
SendCommand([string] command) //without '/'
GetMyPos()
SetMyPos( [Vector] position )
GetMyFacingAngle()
SetMyFacingAngle( [float] angle ) //in radians
GetDistanceFromMeToPoint( [Vector] point )
IsVehicleStreamedIn( [integer] vehicleid )
IsPlayerStreamedIn( [integer] playerid )

[integer] SetTimer( [string] FunctionName, [integer] interval, [integer] repeat) //repeat=1 means loop
KillTimer( [integer] timerid )
[integer] GetPlayerState( [integer] playerid )
[Vector] GetPlayerPos( [integer] playerid )
[integer] GetPlayerVehicleID( [integer] playerid )
[integer] GetPlayerArmedWeapon( [integer] playerid )
[integer] GetPlayerHealth( [integer] playerid )
[integer] GetPlayerArmour( [integer] playerid )
[integer] GetPlayerKeys( [integer] playerid )
[float(in radians)] GetPlayerFacingAngle( [integer] playerid )
[bool]IsPlayerInRangeOfPoint( [integer] playerid, [float] radius, [Vector] point )
[string/null] GetPlayerName( [integer] playerid )
[bool] IsPlayerConnected( [integer] playerid )
----------------------------------------------------------------------------------------------------------------
Constants
---------
PLAYER_RECORDING_TYPE_ONFOOT <-  1
PLAYER_RECORDING_TYPE_DRIVER <- 2

MAX_PLAYERS <- 100
MAX_PLAYER_NAME <- 24
MAX_VEHICLES <- 1000
INVALID_PLAYER_ID <- 0xFF
INVALID_VEHICLE_ID <- 0

PLAYER_STATE_NONE <- 0
PLAYER_STATE_ONFOOT <- 1
PLAYER_STATE_AIM <- 2
PLAYER_STATE_DRIVER <- 3
PLAYER_STATE_PASSENGER <- 4
PLAYER_STATE_ENTER_VEHICLE_DRIVER <- 5
PLAYER_STATE_ENTER_VEHICLE_PASSENGER<- 6
PLAYER_STATE_EXIT_VEHICLE <- 7
PLAYER_STATE_WASTED <- 8
PLAYER_STATE_SPAWNED <- 9

Limitation:

Application Download links ( New v1.1 )(Bugs Fixed)

Windows:    Windows x86          Windows x64

Linux:         Linux x86          Linux x64

Plugin for squirrel download
The downloads contain plugin for squirrel:npc04relxx
This squirrel plugin offers the functions:
Code: [Select]
Squirrel Add-On Plugin npc04relxx functions
-------------------------------------------
ConnectNPC( [string] name, [string] script, [string] host="127.0.0.1")
[bool]IsPlayerNPC( [integer] playerid )
[bool]StartRecordingPlayerData( [integer] playerid, [integer] recordtype, [string]recordname )//recordname without .rec extn
[bool]StopRecordingPlayerData( [integer] playerid )

Click to see the NPC Wiki (fully developed with examples)(host moved, no syntax highlighting)


Source: Click here ( github )

License: GPL v3

Discord:
A discord has been setup at my friends request to discuss about NPC question, answers: here

Tutorials : See the tutorials board or Creating an idle NPC

Video tutorial: Video Tutorial

See them


Note: An artificial mechanism used in StopRecordingPlayback to stop the players vehicle.

AroliS^

  • Jr. Member
  • PRO
  • Posts: 65
Lemme love ya

Sebastian

  • VC:MP Beta Tester
  • Something like this: hga hga hga
  • Posts: 1,201
Re: An implementation of NPC in VCMP 0.4
« Reply #2,  »
Now imagine the Vice City coming to life, with NPCs simulating the traffic. I would love to see that!
(if you guys do that, please tell me the server ip)

habi, man, I don't know what will happen to vc:mp in the future, but I'm glad you shown people a way to play with NPCs !
Everybody screaming for them. Well, here they are! Everything looks pretty easy to use.

Thanks for this!

MEGAMIND

  • Hero Member
  • Developer of Webnet Official
  • Posts: 807
Re: An implementation of NPC in VCMP 0.4
« Reply #3,  »Last edited
A long wait came to reality, thanks @habi + a video tutorial about implementing and creating an npc would be appreciated

hDev

  • Newbie
  • Posts: 31

habi

  • Sr. Member
  • Posts: 400

Spiller

  • Newbie
  • Designer
  • Posts: 33

habi

  • Sr. Member
  • Posts: 400
Re: An implementation of NPC in VCMP 0.4
« Reply #7,  »Last edited
Quote from Spiller on April 6th, 05:52 AM
Though I'm not sure if I'll go through the pain of working with the squirrel plugin again
Actually, this works with the squirrel plugin. This is supposed to work with java plugin also, if you are using it. i.e. if your server language is java, you still can npc without switching to squirrel. In that case a separate plugin is to be made, a plugin that attaches to the java host, and creating two functions "ConnectNPC" and "IsPlayerNPC". (The codes are available. Check the first post. )
The intelligence of NPC lies in the scripts which ofcourse have to be scripted in squirrel.

If you are recording actions of player (tutorial 2),  you can run a blank squirrel server with above npc04relxx module.  Then copy the. rec file to its place. Then make NPC Script which may use the. rec  file.  Then from java call the npc program using system command.

Athanatos

  • Hero Member
  • Throwing random words to increase my post counter
  • Posts: 1,029
Developer of RCNR, Zombie Survival, VLSM(C++) and Infected City Survival

habi

  • Sr. Member
  • Posts: 400
Re: NPC implementation in VCMP 0.4
« Reply #9,  »Last edited
Yes, earlier plugin called "actor" is very similar to this. Here are the reasons why i rewrite it.

First, "actor" plugin used "server hooks" somewhere. So a possible bug in the plugin will affect the server.
But in the new model,  any possible bug cannot affect server because server calls an external application to run. A bug will result in the failing of the external application and not the server.

Secondly, in the earlier plugin if you want to check say "a specific actor (say John) entered a vehicle,
1. you have to first find squirrel onPlayerVehicleEnter function in main.nut
2. Check if the player is an actor
3. Check if the name of the player == "John"
4. Script the codes
But in the new model, the code is separate. It is not coded in main.nut.
It is a separate nut file which determines all events of John like onNPCEnterVehicle, onNPCSpawn, onClientMessage, etc. And you connect John with the script file. ( It is like saying "Hey John, take this script file and connect to my server " )


Re: NPC implementation in VCMP 0.4 Servers
« Reply #10,  »Last edited
Working with npcs and sniper rifles,


'habi' in the image is npc and it is shooting me with a weapon 29 (laser sniper)

This function in npc's script
Code: [Select]
FireSniperRifle([integer]wep(28/29), [float]aimPosX, [float]aimPosY, [float]aimPosZ, [float](alpha), [float]Angle ) where alpha is an angle in radians. 0. 0 means complete horizonal, postive means above, negative means below. can be obtained from aimdir.y. Angle - npc's facing angle if faced to target.
which fires a sniper shot from npc to any target.

Need to perfect some codes and will release here.

Version 1.3
npcclient.exe here

Changelog below
(click to show/hide)
Version 1.3 Changes (15.11.2022)
Extended Timer implemented SetTimerEx (https://npc-for-vcmp.sourceforge.io/wikiw/index.php/SetTimerEx)
Fixed npc running unwantedly just after sending some packet.
Added boolean parameter for crouching in SendOnFootSyncData