![]() |
|
[Plugin] Modern GPS plugin - Printable Version +- open.mp forum (https://forum.open.mp) +-- Forum: SA-MP (https://forum.open.mp/forumdisplay.php?fid=3) +--- Forum: Releases (https://forum.open.mp/forumdisplay.php?fid=13) +---- Forum: Plugins (https://forum.open.mp/forumdisplay.php?fid=32) +---- Thread: [Plugin] Modern GPS plugin (/showthread.php?tid=66) |
Modern GPS plugin - kristo - 2019-04-13 SA-MP GPS Plugin This plugin offers a way of accessing and manipulating the data of San Andreas map nodes and finding paths between them. It is intended to be a modern and straightforward replacement for RouteConnector. The plugin uses a simple implementation of the A* algorithm for pathfinding. Finding a path from the top-leftmost node on the map to the bottom-rightmost node that consists of 684 nodes takes just a few milliseconds. It is worth noting that this is my first successful project in C and the last time I tried writing something in C was over half a year ago so there is a lot of room for improvement and I am planning to continue improving the code of this plugin as I progress. Advantages over RouteConnector
Installation Simply install to your project: Code: sampctl package install kristoisberg/samp-gps-pluginInclude in your code and begin using the library: PHP Code: #include <GPS>
API Functions CreateMapNode(Float:x, Float:y, Float:z, &MapNode:nodeid)
DestroyMapNode(MapNode:nodeid)
bool:IsValidMapNode(MapNode:nodeid)
GetMapNodePos(MapNode:nodeid, &Float:x, &Float:y, &Float:z)
CreateConnection(MapNode:source, MapNode:target, &Connection:connectionid)
DestroyConnection(Connection:connectionid)
GetConnectionSource(Connection:connectionid, &MapNode:nodeid)
GetConnectionTarget(Connection:connectionid, &MapNode:nodeid)
GetMapNodeConnectionCount(MapNode:nodeid, &count)
GetMapNodeConnection(MapNode:nodeid, index, &Connection:connectionid)
GetConnectionBetweenMapNodes(MapNode:source, MapNode:target, &Connection:connectionid)
GetDistanceBetweenMapNodes(MapNode:first, MapNode:second, &Float:distance)
GetAngleBetweenMapNodes(MapNode:first, MapNode:second, &Float:angle)
GetMapNodeDistanceFromPoint(MapNode:nodeid, Float:x, Float:y, Float:z, &Float:distance)
GetMapNodeAngleFromPoint(MapNode:nodeid, Float:x, Float:y, &Float:angle)
GetClosestMapNodeToPoint(Float:x, Float:y, Float:z, &MapNode:nodeid, MapNode:ignorednode = INVALID_MAP_NODE_ID)
GetHighestMapNodeID()
GetRandomMapNode(&MapNode:nodeid)
SaveMapNodesToFile(const filename[])
FindPath(MapNode:source, MapNode:target, &Path:pathid)
FindPathThreaded(MapNode:source, MapNode:target, const callback[], const format[] = "", {Float, _}:...)
Task:FindPathAsync(MapNode:source, MapNode:target)
bool:IsValidPath(Path:pathid)
GetPathSize(Path:pathid, &size)
GetPathNode(Path:pathid, index, &MapNode:nodeid)
GetPathNodeIndex(Path:pathid, MapNode:nodeid, &index)
GetPathLength(Path:pathid, &Float:length)
DestroyPath(Path:pathid)
Error codes
Examples Threaded pathfinding Finding a path from the position of the player to the LSPD building. PHP Code: CMD:pathtols(playerid) {
? ?new Float:x, Float:y, Float:z, MapNode:start;
? ?GetPlayerPos(playerid, x, y, z);
? ?if (GetClosestMapNodeToPoint(x, y, z, start) != GPS_ERROR_NODE) {
? ? ? ?return SendClientMessage(playerid, COLOR_RED, "Finding a node near you failed, GPS.dat was not loaded.");
? ?}
? ?new MapNode:target;
? ?if (GetClosestMapNodeToPoint(1258.7352, -2036.7100, 59.4561, target)) { // this is also valid since the value of GPS_ERROR_NODE is 0.
? ? ? ?return SendClientMessage(playerid, COLOR_RED, "Finding a node near LSPD failed, GPS.dat was not loaded.");
? ?}
? ?if (FindPathThreaded(start, target, "OnPathToLSFound", "ii", playerid, GetTickCount())) {
? ? ? ?return SendClientMessage(playerid, COLOR_RED, "Pathfinding failed for some reason, you should store this error code and print it out since there are multiple ways it could fail.");
? ?}
? ?SendClientMessage(playerid, COLOR_WHITE, "Finding the path...");
? ?return 1;
}
forward public OnPathToLSFound(Path:pathid, playerid, start_time);
public OnPathToLSFound(Path:pathid, playerid, start_time) {
? ?if (!IsValidPath(pathid)) {
? ? ? ?return SendClientMessage(playerid, COLOR_RED, "Pathfinding failed!");
? ?}
? ?new string[128], size, length;
? ?GetPathSize(size);
? ?GetPathLength(length);
? ?format(string, sizeof(string), "Found a path in %ims. Amount of nodes: %i, length: %fm.", GetTickCount() - start_time, size, length);
? ?new MapNode:nodeid, Float:x, Float:y, Float:z;
? ?for (new index; index < size; index) {
? ? ? ?GetPathNode(pathid, index, nodeid);
? ? ? ?GetMapNodePos(nodeid, x, y, z);
? ? ? ?CreateDynamicPickup(1318, 1, x, y, z);
? ?}
? ?DestroyPath(pathid);
? ?return 1;
}
Asynchronous pathfinding What if you could continue the process within the command while still taking advantage of the benefits of threaded pathfinding? You can, using the magic of PawnPlus tasks. PHP Code: CMD:pathtols(playerid) {
? ?new Float:x, Float:y, Float:z, MapNode:start;
? ?GetPlayerPos(playerid, x, y, z);
? ?if (GetClosestMapNodeToPoint(x, y, z, start)) {
? ? ? ?return SendClientMessage(playerid, COLOR_RED, "Finding a node near you failed, GPS.dat was not loaded.");
? ?}
? ?new MapNode:target;
? ?if (GetClosestMapNodeToPoint(1258.7352, -2036.7100, 59.4561, target)) {
? ? ? ?return SendClientMessage(playerid, COLOR_RED, "Finding a node near LSPD failed, GPS.dat was not loaded.");
? ?}
? ?SendClientMessage(playerid, COLOR_WHITE, "Finding the path...");
? ?new Path:pathid = task_await(FindPathAsync(start, target)); // no error handling here, an AMX error will be thrown instead if the pathfinding fails
? ?new string[128], size, length;
? ?GetPathSize(size);
? ?GetPathLength(length);
? ?format(string, sizeof(string), "Found a path in %ims. Amount of nodes: %i, length: %fm.", GetTickCount() - start_time, size, length);
? ?new MapNode:nodeid, Float:x, Float:y, Float:z, index;
? ?while (!GetPathNode(pathid, index, nodeid)) // also note the alternative method of iterating through path nodes here
? ? ? ?GetMapNodePos(nodeid, x, y, z);
? ? ? ?CreateDynamicPickup(1318, 1, x, y, z);
? ? ? ?index;
? ?}
? ?DestroyPath(pathid);
? ?return 1;
}
Testing To test, simply run the package: Code: sampctl package runCredits
RE: Modern GPS plugin - TommyB - 2019-04-13 Very good plugin!! RE: Modern GPS plugin - solstice - 2019-04-13 nice one RE: Modern GPS plugin - Crayder - 2019-04-25 Could you add the possibility to use navigational mesh triangles? RE: Modern GPS plugin - Sasino97 - 2019-06-11 Hello, I am using this plugin but I don't need any of the GTA:SA road nodes because my gamemode is about aviation. Will it work if I remove the DAT file, and create custom nodes and connection? (I actually already wrote the code but I didn't test it yet) RE: Modern GPS plugin - Gaudeamus271 - 2019-06-11 (2019-06-11, 06:58 AM)Sasino97 Wrote: Hello, I am using this plugin but I don't need any of the GTA:SA road nodes because my gamemode is about aviation. I think it should work well with another DAT file, but it seems?quite difficult to build a DAT file by yourself since i didn't find a proper documentation about its structure. Although you can have a look to the file read method (https://github.com/kristoisberg/samp-gps-plugin/blob/master/src/file.cpp). RE: Modern GPS plugin - Sasino97 - 2019-06-18 (2019-06-11, 09:56 PM)Gaudeamus271 Wrote:(2019-06-11, 06:58 AM)Sasino97 Wrote: Hello, I am using this plugin but I don't need any of the GTA:SA road nodes because my gamemode is about aviation. Hi, thank you for your answer. I am using the plugin without any DAT file and it is working. I am loading the needed?data from JSON files, which is not used only for pure nodes but also for other metadata regarding the gamemode (airport taxiways etc.), and based on this data I am creating the nodes at runtime, so I found the solution. RE: Modern GPS plugin - Gaudeamus271 - 2019-06-19 Nvm, it was late and i didn't noticed you are not using the DAT file. RE: Modern GPS plugin - hastlaking - 2020-04-19 anyone has a filterscript a base or beta one to test RE: Modern GPS plugin - Torque - 2020-07-26 So I installed the plugin, copied the function and command for testing, compiled just fine, and I can't even get to the login screen. What's this .DAT file everybody talking about? Can't seem to find it, do I need it for things to work correctly? RE: Modern GPS plugin - JR_Junior - 2022-02-16 This plugin is fantastic, with it I was able to create an autopilot system, street racing by clicking on the map with automatic checkpoint generation and my own gps system was totally improved with this plugin. Perfect! |