Guest User

FGPS

a guest
Sep 3rd, 2014
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 14.00 KB | None | 0 0
  1. /*
  2. FGPS - Advanced GPS System By Fj0rtizFredde!
  3. Credits:
  4. Fj0rtizFredde = Creator Of This Shit!
  5. Whoever made the GetDistanceBetweenPoints & PointAngle function!
  6. Sacky = fcreate function :)
  7. DracoBlue = dcmd :)
  8. Post any suggestions at the sa-mp forums :)
  9. PS! If your server crashes the first time you load this script dont worry.. It's because the system is creating the tables!
  10. */
  11.  
  12. #include <a_samp>
  13. #include <a_sampdb>
  14.  
  15. #define dcmd(%1,%2,%3) if ((strcmp((%3)[1], #%1, true, (%2)) == 0) && ((((%3)[(%2) + 1] == 0) && (dcmd_%1(playerid, "")))||(((%3)[(%2) + 1] == 32) && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1
  16.  
  17. #define GPSFile ("Positions.db") //The file where everything should be saved!
  18. #define MAX_LOCATIONS 50 //How many locations you want to have!
  19. #define UseTd //Comment This if you dont want to use the TextDraw! (To comment put // at the begining of this line)
  20. #define GPSDialogID 1111
  21. enum FGPS
  22. {
  23.     ID,
  24.     Float: LocationX,
  25.     Float: LocationY,
  26.     Float: LocationZ,
  27.     PlaceName[128]
  28. };
  29.  
  30. new GPSInfo[MAX_LOCATIONS][FGPS];
  31. new DB:GPSDB;
  32. new gValue[128];
  33. new GPSObject[MAX_PLAYERS];
  34. new GlobalGpsTimer;
  35. #if defined UseTd
  36.     new Text:GPSTD;
  37. #endif
  38.  
  39. stock Float:PointAngle(playerid, Float:xa, Float:ya, Float:xb, Float:yb)
  40. {
  41.     new Float:carangle;
  42.     new Float:xc, Float:yc;
  43.     new Float:angle;
  44.     xc = floatabs(floatsub(xa,xb));
  45.     yc = floatabs(floatsub(ya,yb));
  46.     if (yc == 0.0 || xc == 0.0)
  47.     {
  48.         if(yc == 0 && xc > 0) angle = 0.0;
  49.         else if(yc == 0 && xc < 0) angle = 180.0;
  50.         else if(yc > 0 && xc == 0) angle = 90.0;
  51.         else if(yc < 0 && xc == 0) angle = 270.0;
  52.         else if(yc == 0 && xc == 0) angle = 0.0;
  53.     }
  54.     else
  55.     {
  56.         angle = atan(xc/yc);
  57.         if(xb > xa && yb <= ya) angle += 90.0;
  58.         else if(xb <= xa && yb < ya) angle = floatsub(90.0, angle);
  59.         else if(xb < xa && yb >= ya) angle -= 90.0;
  60.         else if(xb >= xa && yb > ya) angle = floatsub(270.0, angle);
  61.     }
  62.     GetVehicleZAngle(GetPlayerVehicleID(playerid), carangle);
  63.     return floatadd(angle, -carangle);
  64. }
  65.  
  66. stock fcreate(filename[])
  67. {
  68.     if (fexist(filename)){return false;}
  69.     new File:fhandle = fopen(filename,io_write);
  70.     fclose(fhandle);
  71.     return true;
  72. }
  73.  
  74. stock Float:GetDistanceBetweenPoints(Float:X, Float:Y, Float:Z, Float:PointX, Float:PointY, Float:PointZ)
  75. {
  76.     new Float:Distance;Distance = floatabs(floatsub(X, PointX)) + floatabs(floatsub(Y, PointY)) + floatabs(floatsub(Z, PointZ));
  77.     return Distance;
  78. }
  79.  
  80. public OnFilterScriptInit()
  81. {
  82.     print("\n--------------------------------------");
  83.     print("Freddes GPS System Starts...");
  84.     if(!fexist(GPSFile))
  85.     {
  86.         fcreate(GPSFile);
  87.         printf("The File: %s Was Not Found... So We Created It For You!", GPSFile);
  88.     }
  89.     GPSDB = db_open(GPSFile);
  90.     LoadFGPS();
  91.     //Lets Make A Textdraw :)
  92.     #if defined UseTd
  93.         GPSTD = TextDrawCreate(37.000000, 290.000000, "Distance Left:~n~~n~");
  94.         TextDrawFont(GPSTD, 1);
  95.         TextDrawLetterSize(GPSTD, 0.2, 1.0);
  96.         TextDrawColor(GPSTD, 0x14FF00FF);
  97.         TextDrawSetOutline(GPSTD, 0);
  98.         TextDrawSetProportional(GPSTD, true);
  99.         TextDrawSetShadow(GPSTD, 0);
  100.     #endif
  101.  
  102.     // Start the gpstimer (to check if players have reached their destination
  103.     GlobalGpsTimer = SetTimer("GpsTimer", 1000, true);
  104.  
  105.     return 1;
  106. }
  107.  
  108. forward GpsTimer();
  109. public GpsTimer()
  110. {
  111.     // Setup local variables
  112.     new playerid, Float:x, Float:y, Float:z;
  113.  
  114.     // Loop through all players
  115.     for (playerid = 0; playerid < MAX_PLAYERS; playerid++)
  116.     {
  117.         // Check if this player is connected
  118.         if (IsPlayerConnected(playerid))
  119.         {
  120.             // Check if this player started his gps command
  121.             if (GetPVarInt(playerid,"YEAH") == 1)
  122.             {
  123.                 // Get the coordinates
  124.                 x = GetPVarFloat(playerid, "Spongebob");
  125.                 y = GetPVarFloat(playerid, "Mario");
  126.                 z = GetPVarFloat(playerid, "SpiderPig");
  127.  
  128.                 // Check if the player has reached his destination
  129.                 if (IsPlayerInRangeOfPoint(playerid, 5.0, x, y, z))
  130.                 {
  131.                     DestroyObject(GPSObject[playerid]);
  132.                     SetPVarInt(playerid,"YEAH",0);
  133.                     DeletePVar(playerid,"Spongebob");
  134.                     DeletePVar(playerid,"Mario");
  135.                     DeletePVar(playerid,"SpiderPig");
  136.                     DeletePVar(playerid,"FAIL");
  137.                     #if defined UseTd
  138.                         TextDrawHideForPlayer(playerid, GPSTD);
  139.                     #endif
  140.  
  141.                     SendClientMessage(playerid, 0xFFFFFFFF, "You have reached your destination!");
  142.                 }
  143.             }
  144.         }
  145.     }
  146.  
  147.     return 1;
  148. }
  149.  
  150.  
  151.  
  152. public OnFilterScriptExit()
  153. {
  154.     db_close(GPSDB);
  155.     TextDrawHideForAll(GPSTD);
  156.     TextDrawDestroy(GPSTD);
  157.     print("\n--------------------------------------");
  158.     print("Freddes GPS System Is Now Unloaded...");
  159.     print("\n--------------------------------------");
  160.  
  161.     // Kill the global gps-timer
  162.     KillTimer(GlobalGpsTimer);
  163.  
  164.     return 1;
  165. }
  166.  
  167. public OnPlayerCommandText(playerid, cmdtext[])
  168. {
  169.     dcmd(turnoff,7,cmdtext);
  170.     dcmd(gps,3,cmdtext);
  171.     dcmd(fsave,5,cmdtext);
  172.     dcmd(fedit,5,cmdtext);
  173.     return 0;
  174. }
  175.  
  176. dcmd_gps(playerid, params[])
  177. {
  178.     #pragma unused params
  179.     if(!IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, 0xE60000FF, "You are not in a vehicle!");
  180.     if(GetPVarInt(playerid,"YEAH") == 1) return SendClientMessage(playerid, 0xE60000FF, "Please {00CCFA}/turnoff {E60000}your gps!");
  181.     new string[128], var[2048];
  182.     for(new g = 0; g < sizeof(GPSInfo); g++)
  183.     {
  184.         format(string, 128, "%s\n", GPSInfo[g][PlaceName]);
  185.         strcat(var,string);
  186.     }
  187.     if(!strlen(GPSInfo[0][PlaceName])) return SendClientMessage(playerid, 0xE60000FF, "There is no locations in the database!");
  188.     ShowPlayerDialog(playerid, GPSDialogID, DIALOG_STYLE_LIST, "Freddes GPS System - Select A Location", var, "Ok", "Cancel");
  189.     return 1;
  190. }
  191.  
  192. dcmd_turnoff(playerid, params[])
  193. {
  194.     #pragma unused params
  195.     if(GetPVarInt(playerid,"YEAH") == 0) return SendClientMessage(playerid, 0xE60000FF, "Your GPS is not {00FF15}on{E60000}!");
  196.     DestroyObject(GPSObject[playerid]);
  197.     SetPVarInt(playerid,"YEAH",0);
  198.     DeletePVar(playerid,"Spongebob");
  199.     DeletePVar(playerid,"Mario");
  200.     DeletePVar(playerid,"SpiderPig");
  201.     DeletePVar(playerid,"FAIL");
  202.     #if defined UseTd
  203.         TextDrawHideForPlayer(playerid, GPSTD);
  204.     #endif
  205.     SendClientMessage(playerid, 0xFFFFFFFF, "Your GPS is now off!");
  206.     return 1;
  207. }
  208.  
  209. dcmd_fsave(playerid, params[])
  210. {
  211.     if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xE60000FF, "You are not an Admin!");
  212.     new Float:PPos[3], string[128], query[256];
  213.     //new Float:x, Float:y, Float:z, string[128], query[256];
  214.     //GetPlayerPos(playerid, x, y, z);
  215.     GetPlayerPos(playerid, PPos[0], PPos[1], PPos[2]);
  216.     if(!strlen(params)) return SendClientMessage(playerid, 0xE60000FF, "USAGE: /FSave [Name]");
  217.     GetLastID();  
  218.     //new NewID = strval(gValue);
  219.     format(query, sizeof(query), "INSERT INTO `FGPSSystem` (`LocationX`,`LocationY`,`LocationZ`, `Name`) VALUES('%f','%f','%f','%s');",PPos[0], PPos[1], PPos[2],params);
  220.     //format(query, sizeof(query), "INSERT INTO `FGPSSystem` (`ID`,`LocationX`,`LocationY`,`LocationZ`, `Name`) VALUES('%d','%f','%f','%f','%s');",NewID,PPos[0],PPos[1],PPos[2],params);
  221.     db_query(GPSDB,query);
  222.     ReloadDatabase();
  223.     format(string,sizeof(string),"You have added the place: {00FF33}%s {FFFFFF}to the database", params);
  224.     SendClientMessage(playerid, 0xFFFFFFFF, string);
  225.     return 1;
  226. }
  227.  
  228. dcmd_fedit(playerid, params[])
  229. {
  230.     if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xE60000FF, "You are not an Admin!");
  231.     new string[128], query[256], tmp[2][128];
  232.     if(!strlen(params)) return SendClientMessage(playerid, 0xE60000FF, "USAGE: /Fedit [{2D03FF}OldName{E60000}]{F6FF00},{E60000}[{00FF33}NewName{E60000}]");
  233.     split(params, tmp, ',');
  234.     format(query, sizeof(query), "UPDATE `FGPSSystem` SET `Name` = '%s' WHERE `Name` = '%s'",tmp[1],tmp[0]);
  235.     db_query(GPSDB,query);
  236.     ReloadDatabase();
  237.     format(string,sizeof(string),"You have edit the name of the place: {00FF33}%s {FFFFFF}to {00FF33}%s", tmp[0],tmp[1]);
  238.     SendClientMessage(playerid, 0xFFFFFFFF, string);
  239.     return 1;
  240. }
  241.  
  242. public OnPlayerUpdate(playerid)
  243. {
  244.     if(GetPVarInt(playerid,"YEAH") == 1)
  245.     {
  246.         new Float:VPos[3], Float:Rotation, TDString[128];
  247.         GetVehiclePos(GetPlayerVehicleID(playerid),VPos[0],VPos[1],VPos[2]);
  248.         Rotation = PointAngle(playerid, VPos[0],VPos[1], GetPVarFloat(playerid,"Spongebob"), GetPVarFloat(playerid,"Mario"));
  249.         AttachObjectToVehicle(GPSObject[playerid], GetPlayerVehicleID(playerid), 0.0, 0.0, 1.5, 0.0, 90.0, Rotation);
  250.         #if defined UseTd
  251.             format(TDString, sizeof(TDString), "Distance Left:~n~~n~%.1f Meters",GetDistanceBetweenPoints(VPos[0], VPos[1], VPos[2], GetPVarFloat(playerid,"Spongebob"), GetPVarFloat(playerid,"Mario"),GetPVarFloat(playerid,"SpiderPig")));
  252.             TextDrawSetString(GPSTD, TDString);
  253.         #endif
  254.     }
  255.     return 1;
  256. }
  257.  
  258. public OnPlayerDeath(playerid, killerid, reason)
  259. {
  260.     if(GetPVarInt(playerid,"YEAH") == 1)
  261.     {
  262.         DestroyObject(GPSObject[playerid]);
  263.         SetPVarInt(playerid,"YEAH",0);
  264.         DeletePVar(playerid,"Spongebob");
  265.         DeletePVar(playerid,"Mario");
  266.         DeletePVar(playerid,"SpiderPig");
  267.         DeletePVar(playerid,"FAIL");
  268.         #if defined UseTd
  269.             TextDrawHideForPlayer(playerid, GPSTD);
  270.         #endif
  271.     }
  272.     return 1;
  273. }
  274.  
  275. stock LoadFGPS()
  276. {
  277.     new query[256], DBResult:qresult, count = 0, value[128];
  278.     if(!db_query(DB: GPSDB, "SELECT * FROM `FGPSSystem`"))
  279.     {
  280.         print("No Tables Where Found... So Now We Create Them For You!");
  281.         format(query,sizeof(query),"CREATE TABLE IF NOT EXISTS `FGPSSystem` (`ID` INTEGER PRIMARY KEY AUTOINCREMENT,`LocationX` TEXT,`LocationY` TEXT,`LocationZ` TEXT,`Name` TEXT)");
  282.         db_query(GPSDB,query);
  283.         print("Freddes GPS System Needs To Restart...");
  284.         print("--------------------------------------\n");
  285.         SendRconCommand("exit");
  286.     }
  287.     else
  288.     {
  289.         qresult = db_query(GPSDB,  "SELECT * FROM `FGPSSystem`");
  290.         count = db_num_rows(qresult);
  291.         for(new a=0;a<count;a++)
  292.         {
  293.             if(count >= 1 && count <= MAX_LOCATIONS)
  294.             {
  295.                 db_get_field_assoc(qresult, "ID", value, 5);                GPSInfo[a][ID] = strval(value);
  296.                 db_get_field_assoc(qresult, "LocationX", value, 20);        GPSInfo[a][LocationX] = floatstr(value);
  297.                 db_get_field_assoc(qresult, "LocationY", value, 20);        GPSInfo[a][LocationY] = floatstr(value);
  298.                 db_get_field_assoc(qresult, "LocationZ", value, 20);        GPSInfo[a][LocationZ] = floatstr(value);
  299.                 db_get_field(qresult,4,value,128);                          strmid(GPSInfo[a][PlaceName], value, 0, strlen(value), 128);
  300.                 printf("%d, %f, %f, %f, %s", GPSInfo[a][ID],GPSInfo[a][LocationX],GPSInfo[a][LocationY],GPSInfo[a][LocationZ],GPSInfo[a][PlaceName]);
  301.                 db_next_row(qresult);
  302.             }
  303.         }
  304.         db_free_result(qresult);
  305.         print("Freddes GPS System Is Now Loaded...");
  306.         print("--------------------------------------\n");
  307.     }
  308.     return 1;
  309. }
  310.  
  311. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  312. {
  313.     if((dialogid == GPSDialogID) && response)
  314.     {
  315.         GetPlayerLocationFromId(playerid, listitem);
  316.         return 1;
  317.     }
  318.     return 0;
  319. }
  320.  
  321. stock GetPlayerLocationFromId(playerid,id)
  322. {
  323.     new Query[128], DBResult:qresult, string[128], string2[128], NameStore[128];
  324.     format(Query,sizeof(Query),"SELECT * FROM `FGPSSystem` WHERE ID = '%i'", id);
  325.     qresult = db_query(GPSDB,Query);
  326.     db_get_field_assoc(qresult,"LocationX",string,128);     SetPVarFloat(playerid,"Spongebob",floatstr(string));
  327.     db_get_field_assoc(qresult,"LocationY",string,128);     SetPVarFloat(playerid,"Mario",floatstr(string));
  328.     db_get_field_assoc(qresult,"LocationZ",string,128);     SetPVarFloat(playerid,"SpiderPig",floatstr(string));
  329.     db_get_field_assoc(qresult,"Name",string,128);          SetPVarString(playerid,"FAIL",string);
  330.     GetPVarString(playerid,"FAIL", NameStore, 128);
  331.     format(string2,sizeof(string2),"You have selected {00FF33}%s{FFFFFF}. Follow the arrow to your desination.", NameStore);
  332.     SendClientMessage(playerid, 0xFFFFFFFF, string2);
  333.     GPSObject[playerid] = CreateObject(1318, 0, 0, 0, 0.0, 0.0, 0);
  334.     SetPVarInt(playerid,"YEAH",1);
  335.     #if defined UseTd
  336.         TextDrawShowForPlayer(playerid, GPSTD);
  337.     #endif
  338.     db_free_result(qresult);
  339.     return 1;
  340. }
  341.  
  342. stock ReloadDatabase()
  343. {
  344.     new DBResult:qresult, count = 0, value[128];
  345.     qresult = db_query(GPSDB, "SELECT * FROM `FGPSSystem`");
  346.     count = db_num_rows(qresult);
  347.     for(new a=0;a<count;a++)
  348.     {
  349.         if(count >= 1 && count <= MAX_LOCATIONS)
  350.         {
  351.             db_get_field_assoc(qresult, "ID", value, 5);                GPSInfo[a][ID] = strval(value);
  352.             db_get_field_assoc(qresult, "LocationX", value, 20);        GPSInfo[a][LocationX] = floatstr(value);
  353.             db_get_field_assoc(qresult, "LocationY", value, 20);        GPSInfo[a][LocationY] = floatstr(value);
  354.             db_get_field_assoc(qresult, "LocationZ", value, 20);        GPSInfo[a][LocationZ] = floatstr(value);
  355.             db_get_field(qresult,4,value,128);                          strmid(GPSInfo[a][PlaceName], value, 0, strlen(value), 128);
  356.             db_next_row(qresult);
  357.         }
  358.     }
  359.     db_free_result(qresult);
  360.     return 1;
  361. }
  362.  
  363. stock GetLastID()
  364. {
  365.     new DBResult:qresult, count = 0, Value[128];
  366.     qresult = db_query(GPSDB, "SELECT * FROM `FGPSSystem` ORDER BY `ID` DESC LIMIT 1");
  367.     count = db_num_rows(qresult);
  368.     for(new a=0;a<count;a++)
  369.     {
  370.         if(count <= MAX_LOCATIONS)
  371.         {
  372.             db_get_field_assoc(qresult, "ID", Value, 5);    gValue[a] = Value[a]+1;
  373.             db_next_row(qresult);
  374.         }
  375.     }
  376.     db_free_result(qresult);
  377.     return 1;
  378. }
  379.  
  380. public OnPlayerStateChange(playerid,newstate,oldstate)
  381. {
  382.     if(GetPVarInt(playerid,"YEAH") == 1)
  383.     {
  384.         if(newstate == PLAYER_STATE_ONFOOT && oldstate == PLAYER_STATE_DRIVER)
  385.         {
  386.             DestroyObject(GPSObject[playerid]);
  387.             SetPVarInt(playerid,"YEAH",0);
  388.             DeletePVar(playerid,"Spongebob");
  389.             DeletePVar(playerid,"Mario");
  390.             DeletePVar(playerid,"SpiderPig");
  391.             DeletePVar(playerid,"FAIL");
  392.             #if defined UseTd
  393.                 TextDrawHideForPlayer(playerid, GPSTD);
  394.             #endif
  395.         }
  396.     }
  397.  
  398.     return 1;
  399. }
  400.  
  401. stock split(const strsrc[], strdest[][], delimiter)
  402. {
  403.     new i, li;
  404.     new aNum;
  405.     new len;
  406.     while(i <= strlen(strsrc))
  407.     {
  408.         if(strsrc[i] == delimiter || i == strlen(strsrc))
  409.         {
  410.             len = strmid(strdest[aNum], strsrc, li, i, 128);
  411.             strdest[aNum][len] = 0;
  412.             li = i+1;
  413.             aNum++;
  414.         }
  415.         i++;
  416.     }
  417.     return 1;
  418. }
Advertisement
Add Comment
Please, Sign In to add comment