20.07.2012, 20:16
Im messing around with NPCs more and more, and a while ago I had an idea of abusing NPCs for something not new, but useful.
You probably know that sa-mp's NPCs are running in their own process, samp-npc(.exe). Because of this they also run in their own thread, completely independent from the samp server. But you can still communicate bewteen the servers script, and the NPCs script using messages, commands, etc. So why no abuse this, and let a NPC calculate intensive tasks, while the server keeps focused on his main task.
The idea is extremely easy, though I havent read about it in the forum yet (or missed it somehow).
The NPC script contains the OnClientMessage callback that reports all clientmessages that are sent to the NPC. In this callback you wait for a specific "task code" sent by the server, to execute some task, let it be a simple chat answer, or a complex calculation that takes minutes. During that time the server of course wont lag, as samp-npc runs the calculating thread. When the task is finished, you can report the result to the server using commands or chat messages, and again include the "task code". If the server now receives a message by a NPC, that contains a known task code, he can e.g. call the OnLongTask1Finished(result) callback.
In reality "exporting" short tasks doesnt pay out of course. Parsing the parameters out of the sent message takes some time on NPC and server side of course, so the threaded task might take way longer than it would in the normal server thread.
Another big problem is the separated memory. Because NPC and server run in their own processes, they cant access each others variables, functions, etc. This limits the jobs the "thread npcs" could do a lot. They also cant change server properties like spawning cars, affecting other players etc, unless they sent another code to the server, that tells him to do it, but this will be really messy and inefficient.
Nevertheless, I think that there are a lot of things that could profit from this kind of multithreading.
I tested some examples, and as it seems samp-npc is even faster than the server in some cases. A simple floatsqroot based GetPointToPoint is up to 300% faster as threaded task in the NPC script, than on the server. However this does not include time for parsing the parameters.
For testing I used my RNPC include/NPCmode as they already provide a good npc<->server communication
You probably know that sa-mp's NPCs are running in their own process, samp-npc(.exe). Because of this they also run in their own thread, completely independent from the samp server. But you can still communicate bewteen the servers script, and the NPCs script using messages, commands, etc. So why no abuse this, and let a NPC calculate intensive tasks, while the server keeps focused on his main task.
The idea is extremely easy, though I havent read about it in the forum yet (or missed it somehow).
The NPC script contains the OnClientMessage callback that reports all clientmessages that are sent to the NPC. In this callback you wait for a specific "task code" sent by the server, to execute some task, let it be a simple chat answer, or a complex calculation that takes minutes. During that time the server of course wont lag, as samp-npc runs the calculating thread. When the task is finished, you can report the result to the server using commands or chat messages, and again include the "task code". If the server now receives a message by a NPC, that contains a known task code, he can e.g. call the OnLongTask1Finished(result) callback.
In reality "exporting" short tasks doesnt pay out of course. Parsing the parameters out of the sent message takes some time on NPC and server side of course, so the threaded task might take way longer than it would in the normal server thread.
Another big problem is the separated memory. Because NPC and server run in their own processes, they cant access each others variables, functions, etc. This limits the jobs the "thread npcs" could do a lot. They also cant change server properties like spawning cars, affecting other players etc, unless they sent another code to the server, that tells him to do it, but this will be really messy and inefficient.
Nevertheless, I think that there are a lot of things that could profit from this kind of multithreading.
I tested some examples, and as it seems samp-npc is even faster than the server in some cases. A simple floatsqroot based GetPointToPoint is up to 300% faster as threaded task in the NPC script, than on the server. However this does not include time for parsing the parameters.
For testing I used my RNPC include/NPCmode as they already provide a good npc<->server communication