Advertisement
Guest User

Advanced GPS System

a guest
Jan 31st, 2014
1,022
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 13.34 KB | None | 0 0
  1. /*
  2. Cпециально для www.Pro-Pawn.ru
  3. */
  4.  
  5. #include <a_samp>
  6. #include <a_sampdb>
  7.  
  8. #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
  9.  
  10. #define GPSFile ("Positions.db") //Файл в который происходит сохранение
  11. #define MAX_LOCATIONS 50 //Максимальное кол-во локаций
  12. #define UseTd // Поставь в начало этой строки   //  если не хочешь использовать TextDraw
  13. #define GPSDialogID 1111
  14. enum FGPS
  15. {
  16.     ID,
  17.     Float: LocationX,
  18.     Float: LocationY,
  19.     Float: LocationZ,
  20.     PlaceName[128]
  21. };
  22.  
  23. new GPSInfo[MAX_LOCATIONS][FGPS];
  24. new DB:GPSDB;
  25. new gValue[128];
  26. new GPSObject[MAX_PLAYERS];
  27. #if defined UseTd
  28.     new Text:GPSTD;
  29. #endif
  30.  
  31. stock Float:PointAngle(playerid, Float:xa, Float:ya, Float:xb, Float:yb)
  32. {
  33.     new Float:carangle;
  34.     new Float:xc, Float:yc;
  35.     new Float:angle;
  36.     xc = floatabs(floatsub(xa,xb));
  37.     yc = floatabs(floatsub(ya,yb));
  38.     if (yc == 0.0 || xc == 0.0)
  39.     {
  40.         if(yc == 0 && xc > 0) angle = 0.0;
  41.         else if(yc == 0 && xc < 0) angle = 180.0;
  42.         else if(yc > 0 && xc == 0) angle = 90.0;
  43.         else if(yc < 0 && xc == 0) angle = 270.0;
  44.         else if(yc == 0 && xc == 0) angle = 0.0;
  45.     }
  46.     else
  47.     {
  48.         angle = atan(xc/yc);
  49.         if(xb > xa && yb <= ya) angle += 90.0;
  50.         else if(xb <= xa && yb < ya) angle = floatsub(90.0, angle);
  51.         else if(xb < xa && yb >= ya) angle -= 90.0;
  52.         else if(xb >= xa && yb > ya) angle = floatsub(270.0, angle);
  53.     }
  54.     GetVehicleZAngle(GetPlayerVehicleID(playerid), carangle);
  55.     return floatadd(angle, -carangle);
  56. }
  57.  
  58. stock fcreate(filename[])
  59. {
  60.     if (fexist(filename)){return false;}
  61.     new File:fhandle = fopen(filename,io_write);
  62.     fclose(fhandle);
  63.     return true;
  64. }
  65.  
  66. stock Float:GetDistanceBetweenPoints(Float:X, Float:Y, Float:Z, Float:PointX, Float:PointY, Float:PointZ)
  67. {
  68.     new Float:Distance;Distance = floatabs(floatsub(X, PointX)) + floatabs(floatsub(Y, PointY)) + floatabs(floatsub(Z, PointZ));
  69.     return Distance;
  70. }
  71.  
  72. public OnFilterScriptInit()
  73. {
  74.     print("\n--------------------------------------");
  75.     print("Freddes GPS System старт...");
  76.     if(!fexist(GPSFile))
  77.     {
  78.         fcreate(GPSFile);
  79.         printf("Файл: %s не найден... Создание!", GPSFile);
  80.     }
  81.     GPSDB = db_open(GPSFile);
  82.     LoadFGPS();
  83.     //Lets Make A Textdraw :)
  84.     #if defined UseTd
  85.         GPSTD = TextDrawCreate(37.000000, 290.000000, "Distance Left:~n~~n~");
  86.         TextDrawBackgroundColor(GPSTD, 255);
  87.         TextDrawFont(GPSTD, 1);
  88.         TextDrawLetterSize(GPSTD, 0.509999, 1.300000);
  89.         TextDrawColor(GPSTD, -1);
  90.         TextDrawSetOutline(GPSTD, 1);
  91.         TextDrawSetProportional(GPSTD, 1);
  92.     #endif
  93.     return 1;
  94. }
  95.  
  96. public OnFilterScriptExit()
  97. {
  98.     db_close(GPSDB);
  99.     TextDrawHideForAll(GPSTD);
  100.     TextDrawDestroy(GPSTD);
  101.     print("\n--------------------------------------");
  102.     print("Freddes GPS System загружен...");
  103.     print("\n--------------------------------------");
  104.     return 1;
  105. }
  106.  
  107. public OnPlayerCommandText(playerid, cmdtext[])
  108. {
  109.     dcmd(turnoff,7,cmdtext);  // выключить gps
  110.     dcmd(gps,3,cmdtext);      // выбрать пункт назначения
  111.     dcmd(fsave,5,cmdtext);    // создать локацию (для rcon админа)
  112.     dcmd(fedit,5,cmdtext);    // редактировать локацию (для rcon админа)
  113.     return 0;
  114. }
  115.  
  116. dcmd_gps(playerid, params[])
  117. {
  118.     #pragma unused params
  119.     if(!IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, 0xE60000FF, "Вы не в машине");
  120.     if(GetPVarInt(playerid,"YEAH") == 1) return SendClientMessage(playerid, 0xE60000FF, "Введите {00CCFA}/turnoff {E60000}чтобы отключить предыдущую позицию");
  121.     new string[128], var[2048];
  122.     for(new g = 0; g < sizeof(GPSInfo); g++)
  123.     {
  124.         format(string, 128, "%s\n", GPSInfo[g][PlaceName]);
  125.         strcat(var,string);
  126.     }
  127.     if(!strlen(GPSInfo[0][PlaceName])) return SendClientMessage(playerid, 0xE60000FF, "Нет локаций в базу данных");
  128.     ShowPlayerDialog(playerid, GPSDialogID, DIALOG_STYLE_LIST, "Система GPS - выберите локацию", var, "Ок", "Отмена");
  129.     return 1;
  130. }
  131.  
  132. dcmd_turnoff(playerid, params[])
  133. {
  134.     #pragma unused params
  135.     if(GetPVarInt(playerid,"YEAH") == 0) return SendClientMessage(playerid, 0xE60000FF, "GPS уже {00FF15}выключен{E60000}");
  136.     DisablePlayerCheckpoint(playerid);
  137.     DestroyObject(GPSObject[playerid]);
  138.     SetPVarInt(playerid,"YEAH",0);
  139.     DeletePVar(playerid,"Spongebob");
  140.     DeletePVar(playerid,"Mario");
  141.     DeletePVar(playerid,"SpiderPig");
  142.     DeletePVar(playerid,"FAIL");
  143.     #if defined UseTd
  144.         TextDrawHideForPlayer(playerid, GPSTD);
  145.     #endif
  146.     SendClientMessage(playerid, 0xFFFFFFFF, "Вы отключили GPS");
  147.     return 1;
  148. }
  149.  
  150. dcmd_fsave(playerid, params[])
  151. {
  152.     if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xE60000FF, "Вы не администратор");
  153.     new Float:PPos[3], string[128], query[256];
  154.     GetPlayerPos(playerid, PPos[0], PPos[1], PPos[2]);
  155.     if(!strlen(params)) return SendClientMessage(playerid, 0xE60000FF, "Использование: /FSave [имя]");
  156.     GetLastID();
  157.     new NewID = strval(gValue);
  158.     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);
  159.     db_query(GPSDB,query);
  160.     ReloadDatabase();
  161.     format(string,sizeof(string),"Вы добавили: {00FF33}%s {FFFFFF}в базу данных", params);
  162.     SendClientMessage(playerid, 0xFFFFFFFF, string);
  163.     return 1;
  164. }
  165.  
  166. dcmd_fedit(playerid, params[])
  167. {
  168.     if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xE60000FF, "Вы не администратор");
  169.     new string[128], query[256], tmp[2][128];
  170.     if(!strlen(params)) return SendClientMessage(playerid, 0xE60000FF, "Использование: /Fedit [{2D03FF}Старое имя{E60000}]{F6FF00} , {E60000}[{00FF33}Новое имя{E60000}]");
  171.     split(params, tmp, ',');
  172.     format(query, sizeof(query), "UPDATE `FGPSSystem` SET `Name` = '%s' WHERE `Name` = '%s'",tmp[1],tmp[0]);
  173.     db_query(GPSDB,query);
  174.     ReloadDatabase();
  175.     format(string,sizeof(string),"Вы изменили имя локации: {00FF33}%s {FFFFFF}на {00FF33}%s", tmp[0],tmp[1]);
  176.     SendClientMessage(playerid, 0xFFFFFFFF, string);
  177.     return 1;
  178. }
  179.  
  180. public OnPlayerUpdate(playerid)
  181. {
  182.     if(GetPVarInt(playerid,"YEAH") == 1)
  183.     {
  184.         new Float:VPos[3], Float:Rotation, TDString[128];
  185.         GetVehiclePos(GetPlayerVehicleID(playerid),VPos[0],VPos[1],VPos[2]);
  186.         Rotation = PointAngle(playerid, VPos[0],VPos[1], GetPVarFloat(playerid,"Spongebob"), GetPVarFloat(playerid,"Mario"));
  187.         AttachObjectToVehicle(GPSObject[playerid], GetPlayerVehicleID(playerid), 0.0, 0.0, 1.5, 0.0, 90.0, Rotation);
  188.         #if defined UseTd
  189.             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")));
  190.             TextDrawSetString(GPSTD, TDString);
  191.         #endif
  192.     }
  193.     return 1;
  194. }
  195.  
  196. public OnPlayerDeath(playerid, killerid, reason)
  197. {
  198.     if(GetPVarInt(playerid,"YEAH") == 1)
  199.     {
  200.         DisablePlayerCheckpoint(playerid);
  201.         DestroyObject(GPSObject[playerid]);
  202.         SetPVarInt(playerid,"YEAH",0);
  203.         DeletePVar(playerid,"Spongebob");
  204.         DeletePVar(playerid,"Mario");
  205.         DeletePVar(playerid,"SpiderPig");
  206.         DeletePVar(playerid,"FAIL");
  207.         TextDrawHideForPlayer(playerid, GPSTD);
  208.     }
  209.     return 1;
  210. }
  211.  
  212. stock LoadFGPS()
  213. {
  214.     new query[256], DBResult:qresult, count = 0, value[128];
  215.     if(!db_query(DB: GPSDB, "SELECT * FROM `FGPSSystem`"))
  216.     {
  217.         print("Таблица не найдена... Создание таблицы!");
  218.         format(query,sizeof(query),"CREATE TABLE IF NOT EXISTS `FGPSSystem` (`ID`, `LocationX`, `LocationY`, `LocationZ`, `Name`)");
  219.         db_query(GPSDB,query);
  220.         print("Freddes GPS System нуждается в рестарте...");
  221.         print("--------------------------------------\n");
  222.         SendRconCommand("exit");
  223.     }
  224.     else
  225.     {
  226.         qresult = db_query(GPSDB,  "SELECT * FROM `FGPSSystem`");
  227.         count = db_num_rows(qresult);
  228.         for(new a=0;a<count;a++)
  229.         {
  230.             if(count >= 1 && count <= MAX_LOCATIONS)
  231.             {
  232.                 db_get_field_assoc(qresult, "ID", value, 5);                GPSInfo[a][ID] = strval(value);
  233.                 db_get_field_assoc(qresult, "LocationX", value, 20);        GPSInfo[a][LocationX] = floatstr(value);
  234.                 db_get_field_assoc(qresult, "LocationY", value, 20);        GPSInfo[a][LocationY] = floatstr(value);
  235.                 db_get_field_assoc(qresult, "LocationZ", value, 20);        GPSInfo[a][LocationZ] = floatstr(value);
  236.                 db_get_field(qresult,4,value,128);                          strmid(GPSInfo[a][PlaceName], value, 0, strlen(value), 128);
  237.                 printf("%d, %f, %f, %f, %s", GPSInfo[a][ID],GPSInfo[a][LocationX],GPSInfo[a][LocationY],GPSInfo[a][LocationZ],GPSInfo[a][PlaceName]);
  238.                 db_next_row(qresult);
  239.             }
  240.         }
  241.         db_free_result(qresult);
  242.         print("Freddes GPS System загружен...");
  243.         print("--------------------------------------\n");
  244.     }
  245.     return 1;
  246. }
  247.  
  248. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  249. {
  250.     if(dialogid == GPSDialogID && response)
  251.     {
  252.         GetPlayerLocationFromId(playerid, listitem);
  253.         return 1;
  254.     }
  255.     return 0;
  256. }
  257.  
  258. stock GetPlayerLocationFromId(playerid,id)
  259. {
  260.     new Query[128], DBResult:qresult, string[128], string2[128], NameStore[128];
  261.     format(Query,sizeof(Query),"SELECT `LocationX`, `LocationY`, `LocationZ`, `Name` FROM `FGPSSystem` WHERE ID = '%d'", id);
  262.     qresult = db_query(GPSDB,Query);
  263.     db_get_field_assoc(qresult,"LocationX",string,128);     SetPVarFloat(playerid,"Spongebob",floatstr(string));
  264.     db_get_field_assoc(qresult,"LocationY",string,128);     SetPVarFloat(playerid,"Mario",floatstr(string));
  265.     db_get_field_assoc(qresult,"LocationZ",string,128);     SetPVarFloat(playerid,"SpiderPig",floatstr(string));
  266.     db_get_field_assoc(qresult,"Name",string,128);          SetPVarString(playerid,"FAIL",string);
  267.     DisablePlayerCheckpoint(playerid); //To make sure it dont bug :)
  268.     SetPlayerCheckpoint(playerid, GetPVarFloat(playerid,"Spongebob"), GetPVarFloat(playerid,"Mario"), GetPVarFloat(playerid,"SpiderPig"), 3.0);
  269.     GetPVarString(playerid,"FAIL", NameStore, 128);
  270.     format(string2,sizeof(string2),"Вы отметили на карте точку {00FF33}%s{FFFFFF}.", NameStore);
  271.     SendClientMessage(playerid, 0xFFFFFFFF, string2);
  272.     GPSObject[playerid] = CreateObject(1318, 0, 0, 0, 0.0, 0.0, 0);
  273.     SetPVarInt(playerid,"YEAH",1);
  274.     #if defined UseTd
  275.         TextDrawShowForPlayer(playerid, GPSTD);
  276.     #endif
  277.     db_free_result(qresult);
  278.     return 1;
  279. }
  280.  
  281. stock ReloadDatabase()
  282. {
  283.     new DBResult:qresult, count = 0, value[128];
  284.     qresult = db_query(GPSDB, "SELECT * FROM `FGPSSystem`");
  285.     count = db_num_rows(qresult);
  286.     for(new a=0;a<count;a++)
  287.     {
  288.         if(count >= 1 && count <= MAX_LOCATIONS)
  289.         {
  290.             db_get_field_assoc(qresult, "ID", value, 5);                GPSInfo[a][ID] = strval(value);
  291.             db_get_field_assoc(qresult, "LocationX", value, 20);        GPSInfo[a][LocationX] = floatstr(value);
  292.             db_get_field_assoc(qresult, "LocationY", value, 20);        GPSInfo[a][LocationY] = floatstr(value);
  293.             db_get_field_assoc(qresult, "LocationZ", value, 20);        GPSInfo[a][LocationZ] = floatstr(value);
  294.             db_get_field(qresult,4,value,128);                          strmid(GPSInfo[a][PlaceName], value, 0, strlen(value), 128);
  295.             db_next_row(qresult);
  296.         }
  297.     }
  298.     db_free_result(qresult);
  299.     return 1;
  300. }
  301.  
  302. stock GetLastID()
  303. {
  304.     new DBResult:qresult, count = 0, Value[128];
  305.     qresult = db_query(GPSDB, "SELECT * FROM `FGPSSystem` ORDER BY `ID` DESC LIMIT 1");
  306.     count = db_num_rows(qresult);
  307.     for(new a=0;a<count;a++)
  308.     {
  309.         if(count <= MAX_LOCATIONS)
  310.         {
  311.             db_get_field_assoc(qresult, "ID", Value, 5);    gValue[a] = Value[a]+1;
  312.             db_next_row(qresult);
  313.         }
  314.     }
  315.     db_free_result(qresult);
  316.     return 1;
  317. }
  318.  
  319. public OnPlayerEnterCheckpoint(playerid)
  320. {
  321.     if(GetPVarInt(playerid,"YEAH") == 1)
  322.     {
  323.         SendClientMessage(playerid, 0xFFFFFFFF, "Вы приехали в пункт назначения");
  324.         DisablePlayerCheckpoint(playerid);
  325.         DestroyObject(GPSObject[playerid]);
  326.         SetPVarInt(playerid,"YEAH",0);
  327.         DeletePVar(playerid,"Spongebob");
  328.         DeletePVar(playerid,"Mario");
  329.         DeletePVar(playerid,"SpiderPig");
  330.         DeletePVar(playerid,"FAIL");
  331.         TextDrawHideForPlayer(playerid, GPSTD);
  332.     }
  333.     return 1;
  334. }
  335.  
  336. public OnPlayerStateChange(playerid,newstate,oldstate)
  337. {
  338.     if(newstate == PLAYER_STATE_ONFOOT && oldstate == PLAYER_STATE_DRIVER)
  339.     {
  340.         DisablePlayerCheckpoint(playerid);
  341.         DestroyObject(GPSObject[playerid]);
  342.         SetPVarInt(playerid,"YEAH",0);
  343.         DeletePVar(playerid,"Spongebob");
  344.         DeletePVar(playerid,"Mario");
  345.         DeletePVar(playerid,"SpiderPig");
  346.         DeletePVar(playerid,"FAIL");
  347.         #if defined UseTd
  348.             TextDrawHideForPlayer(playerid, GPSTD);
  349.         #endif
  350.     }
  351.     return 1;
  352. }
  353.  
  354. stock split(const strsrc[], strdest[][], delimiter)
  355. {
  356.     new i, li;
  357.     new aNum;
  358.     new len;
  359.     while(i <= strlen(strsrc))
  360.     {
  361.         if(strsrc[i] == delimiter || i == strlen(strsrc))
  362.         {
  363.             len = strmid(strdest[aNum], strsrc, li, i, 128);
  364.             strdest[aNum][len] = 0;
  365.             li = i+1;
  366.             aNum++;
  367.         }
  368.         i++;
  369.     }
  370.     return 1;
  371. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement