· 9 years ago · Feb 03, 2017, 03:28 PM
1/*
2FGPS - Advanced GPS System By Fj0rtizFredde!
3Credits:
4Fj0rtizFredde = Creator Of This Shit!
5Whoever made the GetDistanceBetweenPoints & PointAngle function!
6Sacky = fcreate function :)
7DracoBlue = dcmd :)
8Post any suggestions at the sa-mp forums :)
9PS! 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
21enum FGPS
22{
23 ID,
24 Float: LocationX,
25 Float: LocationY,
26 Float: LocationZ,
27 PlaceName[128]
28};
29
30new GPSInfo[MAX_LOCATIONS][FGPS];
31new DB:GPSDB;
32new gValue[128];
33new GPSObject[MAX_PLAYERS];
34new GlobalGpsTimer;
35#if defined UseTd
36 new Text:GPSTD;
37#endif
38
39stock 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
66stock 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
74stock 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
80public OnFilterScriptInit()
81{
82 if(!fexist(GPSFile))
83 {
84 fcreate(GPSFile);
85 }
86 GPSDB = db_open(GPSFile);
87 LoadFGPS();
88 //Lets Make A Textdraw :)
89 #if defined UseTd
90 GPSTD = TextDrawCreate(37.000000, 290.000000, "Distance Left:~n~~n~");
91 TextDrawFont(GPSTD, 1);
92 TextDrawLetterSize(GPSTD, 0.2, 1.0);
93 TextDrawColor(GPSTD, 0x14FF00FF);
94 TextDrawSetOutline(GPSTD, 0);
95 TextDrawSetProportional(GPSTD, true);
96 TextDrawSetShadow(GPSTD, 0);
97 #endif
98
99 // Start the gpstimer (to check if players have reached their destination
100 GlobalGpsTimer = SetTimer("GpsTimer", 1000, true);
101
102 return 1;
103}
104
105forward GpsTimer();
106public GpsTimer()
107{
108 // Setup local variables
109 new playerid, Float:x, Float:y, Float:z;
110
111 // Loop through all players
112 for (playerid = 0; playerid < MAX_PLAYERS; playerid++)
113 {
114 // Check if this player is connected
115 if (IsPlayerConnected(playerid))
116 {
117 // Check if this player started his gps command
118 if (GetPVarInt(playerid,"YEAH") == 1)
119 {
120 // Get the coordinates
121 x = GetPVarFloat(playerid, "Spongebob");
122 y = GetPVarFloat(playerid, "Mario");
123 z = GetPVarFloat(playerid, "SpiderPig");
124
125 // Check if the player has reached his destination
126 if (IsPlayerInRangeOfPoint(playerid, 5.0, x, y, z))
127 {
128 DestroyObject(GPSObject[playerid]);
129 SetPVarInt(playerid,"YEAH",0);
130 DeletePVar(playerid,"Spongebob");
131 DeletePVar(playerid,"Mario");
132 DeletePVar(playerid,"SpiderPig");
133 DeletePVar(playerid,"FAIL");
134 #if defined UseTd
135 TextDrawHideForPlayer(playerid, GPSTD);
136 #endif
137
138 SendClientMessage(playerid, 0x00000000, "{FFFFFF}[BE | GPS]: {00C0FF}Stigli ste na odrediste!");
139 }
140 }
141 }
142 }
143
144 return 1;
145}
146
147
148
149public OnFilterScriptExit()
150{
151 db_close(GPSDB);
152 TextDrawHideForAll(GPSTD);
153 TextDrawDestroy(GPSTD);
154
155 // Kill the global gps-timer
156 KillTimer(GlobalGpsTimer);
157
158 return 1;
159}
160
161public OnPlayerCommandText(playerid, cmdtext[])
162{
163 dcmd(gps,3,cmdtext);
164 dcmd(fsave,5,cmdtext);
165 dcmd(fedit,5,cmdtext);
166 return 0;
167}
168
169dcmd_gps(playerid, params[])
170{
171 #pragma unused params
172 new string[128], var[2048];
173 for(new g = 0; g < sizeof(GPSInfo); g++)
174 {
175 format(string, 128, "%s\n", GPSInfo[g][PlaceName]);
176 strcat(var,string);
177 }
178 if(!strlen(GPSInfo[0][PlaceName])) return SendClientMessage(playerid, 0xE60000FF, "There is no locations in the database!");
179 ShowPlayerDialog(playerid, GPSDialogID, DIALOG_STYLE_LIST, "Odabir lokacije:", var, "Ok", "Cancel");
180 return 1;
181}
182
183dcmd_fsave(playerid, params[])
184{
185 if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xE60000FF, "You are not an Admin!");
186 new Float:PPos[3], string[128], query[256];
187 //new Float:x, Float:y, Float:z, string[128], query[256];
188 //GetPlayerPos(playerid, x, y, z);
189 GetPlayerPos(playerid, PPos[0], PPos[1], PPos[2]);
190 if(!strlen(params)) return SendClientMessage(playerid, 0xE60000FF, "USAGE: /FSave [Name]");
191 GetLastID();
192 //new NewID = strval(gValue);
193 format(query, sizeof(query), "INSERT INTO `FGPSSystem` (`LocationX`,`LocationY`,`LocationZ`, `Name`) VALUES('%f','%f','%f','%s');",PPos[0], PPos[1], PPos[2],params);
194 //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);
195 db_query(GPSDB,query);
196 ReloadDatabase();
197 format(string,sizeof(string),"Dodali ste mjesto {00FF33}%s {FFFFFF}u databazu", params);
198 SendClientMessage(playerid, 0xFFFFFFFF, string);
199 return 1;
200}
201
202dcmd_fedit(playerid, params[])
203{
204 if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xE60000FF, "You are not an Admin!");
205 new string[128], query[256], tmp[2][128];
206 if(!strlen(params)) return SendClientMessage(playerid, 0xE60000FF, "USAGE: /Fedit [{2D03FF}OldName{E60000}]{F6FF00},{E60000}[{00FF33}NewName{E60000}]");
207 split(params, tmp, ',');
208 format(query, sizeof(query), "UPDATE `FGPSSystem` SET `Name` = '%s' WHERE `Name` = '%s'",tmp[1],tmp[0]);
209 db_query(GPSDB,query);
210 ReloadDatabase();
211 format(string,sizeof(string),"You have edit the name of the place: {00FF33}%s {FFFFFF}to {00FF33}%s", tmp[0],tmp[1]);
212 SendClientMessage(playerid, 0xFFFFFFFF, string);
213 return 1;
214}
215
216public OnPlayerUpdate(playerid)
217{
218 if(GetPVarInt(playerid,"YEAH") == 1)
219 {
220 new Float:VPos[3], Float:Rotation, TDString[128];
221 GetVehiclePos(GetPlayerVehicleID(playerid),VPos[0],VPos[1],VPos[2]);
222 Rotation = PointAngle(playerid, VPos[0],VPos[1], GetPVarFloat(playerid,"Spongebob"), GetPVarFloat(playerid,"Mario"));
223 AttachObjectToVehicle(GPSObject[playerid], GetPlayerVehicleID(playerid), 0.0, 0.0, 1.5, 0.0, 90.0, Rotation);
224 #if defined UseTd
225 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")));
226 TextDrawSetString(GPSTD, TDString);
227 #endif
228 }
229 return 1;
230}
231
232public OnPlayerDeath(playerid, killerid, reason)
233{
234 if(GetPVarInt(playerid,"YEAH") == 1)
235 {
236 DestroyObject(GPSObject[playerid]);
237 SetPVarInt(playerid,"YEAH",0);
238 DeletePVar(playerid,"Spongebob");
239 DeletePVar(playerid,"Mario");
240 DeletePVar(playerid,"SpiderPig");
241 DeletePVar(playerid,"FAIL");
242 #if defined UseTd
243 TextDrawHideForPlayer(playerid, GPSTD);
244 #endif
245 }
246 return 1;
247}
248
249stock LoadFGPS()
250{
251 new query[256], DBResult:qresult, count = 0, value[128];
252 if(!db_query(DB: GPSDB, "SELECT * FROM `FGPSSystem`"))
253 {
254 print("No Tables Where Found... So Now We Create Them For You!");
255 format(query,sizeof(query),"CREATE TABLE IF NOT EXISTS `FGPSSystem` (`ID` INTEGER PRIMARY KEY AUTOINCREMENT,`LocationX` TEXT,`LocationY` TEXT,`LocationZ` TEXT,`Name` TEXT)");
256 db_query(GPSDB,query);
257 print("Freddes GPS System Needs To Restart...");
258 print("--------------------------------------\n");
259 SendRconCommand("exit");
260 }
261 else
262 {
263 qresult = db_query(GPSDB, "SELECT * FROM `FGPSSystem`");
264 count = db_num_rows(qresult);
265 for(new a=0;a<count;a++)
266 {
267 if(count >= 1 && count <= MAX_LOCATIONS)
268 {
269 db_get_field_assoc(qresult, "ID", value, 5); GPSInfo[a][ID] = strval(value);
270 db_get_field_assoc(qresult, "LocationX", value, 20); GPSInfo[a][LocationX] = floatstr(value);
271 db_get_field_assoc(qresult, "LocationY", value, 20); GPSInfo[a][LocationY] = floatstr(value);
272 db_get_field_assoc(qresult, "LocationZ", value, 20); GPSInfo[a][LocationZ] = floatstr(value);
273 db_get_field(qresult,4,value,128); strmid(GPSInfo[a][PlaceName], value, 0, strlen(value), 128);
274 printf("%d, %f, %f, %f, %s", GPSInfo[a][ID],GPSInfo[a][LocationX],GPSInfo[a][LocationY],GPSInfo[a][LocationZ],GPSInfo[a][PlaceName]);
275 db_next_row(qresult);
276 }
277 }
278 db_free_result(qresult);
279 print("Freddes GPS System Is Now Loaded...");
280 print("--------------------------------------\n");
281 }
282 return 1;
283}
284
285public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
286{
287 if((dialogid == GPSDialogID) && response)
288 {
289 GetPlayerLocationFromId(playerid, listitem);
290 return 1;
291 }
292 return 0;
293}
294
295stock GetPlayerLocationFromId(playerid,id)
296{
297 new Query[128], DBResult:qresult, string[128], string2[128], NameStore[128];
298 format(Query,sizeof(Query),"SELECT * FROM `FGPSSystem` WHERE ID = '%i'", id);
299 qresult = db_query(GPSDB,Query);
300 db_get_field_assoc(qresult,"LocationX",string,128); SetPVarFloat(playerid,"Spongebob",floatstr(string));
301 db_get_field_assoc(qresult,"LocationY",string,128); SetPVarFloat(playerid,"Mario",floatstr(string));
302 db_get_field_assoc(qresult,"LocationZ",string,128); SetPVarFloat(playerid,"SpiderPig",floatstr(string));
303 db_get_field_assoc(qresult,"Name",string,128); SetPVarString(playerid,"FAIL",string);
304 GetPVarString(playerid,"FAIL", NameStore, 128);
305 format(string2,sizeof(string2),"{FFFFFF}[BE | GPS]: {00C0FF}Odabrana destinacija: %s | Pratite strelicu do destinacije!", NameStore);
306 SendClientMessage(playerid, 0xFFFFFFFF, string2);
307 GPSObject[playerid] = CreateObject(1318, 0, 0, 0, 0.0, 0.0, 0);
308 SetPVarInt(playerid,"YEAH",1);
309 #if defined UseTd
310 TextDrawShowForPlayer(playerid, GPSTD);
311 #endif
312 db_free_result(qresult);
313 return 1;
314}
315
316stock ReloadDatabase()
317{
318 new DBResult:qresult, count = 0, value[128];
319 qresult = db_query(GPSDB, "SELECT * FROM `FGPSSystem`");
320 count = db_num_rows(qresult);
321 for(new a=0;a<count;a++)
322 {
323 if(count >= 1 && count <= MAX_LOCATIONS)
324 {
325 db_get_field_assoc(qresult, "ID", value, 5); GPSInfo[a][ID] = strval(value);
326 db_get_field_assoc(qresult, "LocationX", value, 20); GPSInfo[a][LocationX] = floatstr(value);
327 db_get_field_assoc(qresult, "LocationY", value, 20); GPSInfo[a][LocationY] = floatstr(value);
328 db_get_field_assoc(qresult, "LocationZ", value, 20); GPSInfo[a][LocationZ] = floatstr(value);
329 db_get_field(qresult,4,value,128); strmid(GPSInfo[a][PlaceName], value, 0, strlen(value), 128);
330 db_next_row(qresult);
331 }
332 }
333 db_free_result(qresult);
334 return 1;
335}
336
337stock GetLastID()
338{
339 new DBResult:qresult, count = 0, Value[128];
340 qresult = db_query(GPSDB, "SELECT * FROM `FGPSSystem` ORDER BY `ID` DESC LIMIT 1");
341 count = db_num_rows(qresult);
342 for(new a=0;a<count;a++)
343 {
344 if(count <= MAX_LOCATIONS)
345 {
346 db_get_field_assoc(qresult, "ID", Value, 5); gValue[a] = Value[a]+1;
347 db_next_row(qresult);
348 }
349 }
350 db_free_result(qresult);
351 return 1;
352}
353
354public OnPlayerStateChange(playerid,newstate,oldstate)
355{
356 if(GetPVarInt(playerid,"YEAH") == 1)
357 {
358 if(newstate == PLAYER_STATE_ONFOOT && oldstate == PLAYER_STATE_DRIVER)
359 {
360 DestroyObject(GPSObject[playerid]);
361 SetPVarInt(playerid,"YEAH",0);
362 DeletePVar(playerid,"Spongebob");
363 DeletePVar(playerid,"Mario");
364 DeletePVar(playerid,"SpiderPig");
365 DeletePVar(playerid,"FAIL");
366 #if defined UseTd
367 TextDrawHideForPlayer(playerid, GPSTD);
368 #endif
369 }
370 }
371
372 return 1;
373}
374
375stock split(const strsrc[], strdest[][], delimiter)
376{
377 new i, li;
378 new aNum;
379 new len;
380 while(i <= strlen(strsrc))
381 {
382 if(strsrc[i] == delimiter || i == strlen(strsrc))
383 {
384 len = strmid(strdest[aNum], strsrc, li, i, 128);
385 strdest[aNum][len] = 0;
386 li = i+1;
387 aNum++;
388 }
389 i++;
390 }
391 return 1;
392}