Guest User

Dynamic Car Saving System - SA-MP

a guest
Dec 20th, 2013
1,263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.39 KB | None | 0 0
  1. /*
  2.                 Dynamic Server Cars - Tayab Soomro
  3.  
  4.                 This script is proudly created by me (Tayab). I've initiated working on SA-MP Scripting nowadays, day by day
  5.                 I learn something new which I did not know yesterday. So much so that today I am able to create my own Dynamic
  6.                 Car Saving System.
  7.  
  8.                 It's a SQLite based script which can easily be converted to almost any saving system out there. If having problem doing
  9.                 that, don't hesitate to contact me.
  10.  
  11. CREDITS:
  12.  
  13. Tayab Soomro - Developing Script
  14. Y_LESS - SSCANF
  15. BEN - General Help
  16. Unknown - ZCMD Command System
  17. Unknown - SQLite
  18.  
  19. UPDATES:
  20.  
  21. - Added /deleteveh command which allows you to delete the vehicle from the server.
  22. - Now you can get the name of the car by the ID /vehname.
  23. - Some minor changes throughout the script.
  24.  
  25.  
  26. */
  27. #define FILTERSCRIPT
  28.  
  29. #include <a_samp>
  30. #include <zcmd>
  31. #include <sscanf2>
  32.  
  33. #undef MAX_VEHICLES
  34. #define MAX_VEHICLES    200 // Put the desired amount of vehicles you want in game.
  35. #define USER_DB     "example.db" // Put your exisiting SQLite Database
  36. #define MAX_INI_ENTRY_TEXT 80
  37.  
  38. #define COLOR_GREY      0xCCCCCCFF
  39. #define COLOR_WHITE     0xFFFFFFFF
  40. #define COLOR_ORANGE    0xFF8800FF
  41.  
  42. //NEWs
  43. new DB:Users;
  44. new query[256];
  45. new str[128];
  46. new data[128];
  47. new Vehicle[MAX_VEHICLES];
  48.  
  49. public OnFilterScriptInit()
  50. {
  51.         Users = db_open(USER_DB);
  52.         print("==========================================================");
  53.         print("==========================================================");
  54.         print("==== SA-MP Dynamic Server Car Saving System - SQLite =====");
  55.         print("=========== Developed by Tayab Soomro  ===================");
  56.         print("==========================================================");
  57.         LoadCars(); // Loading cars, will work on it in a second.
  58.         format(query,sizeof(query),"CREATE TABLE IF NOT EXISTS `cars` (id INTEGER PRIMARY KEY, name, modelid, carx, cary, carz, caranlge)");
  59.         // Creates the table cars if it doesn't exist.
  60.         db_query(Users,query);
  61.         // Run the above query
  62.         return 1;
  63. }
  64.  
  65. public OnFilterScriptExit()
  66. {
  67.         print("==========================================================");
  68.         print("==========================================================");
  69.         print("==== SA-MP Dynamic Server Car Saving System - SQLite =====");
  70.         print("=========== Developed by Tayab Soomro  ===================");
  71.         print("==========================================================");
  72.         for(new i = 0 ; i < MAX_VEHICLES; i++)
  73.                 DestroyVehicle(Vehicle[i]); // Destory all the vehicles.
  74.         return 1;
  75. }
  76.  
  77. CMD:saveveh(playerid,params[])
  78. {
  79.         new Float:Pos[4];
  80.         if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid,COLOR_GREY,"You're not autorized to use this command.");
  81.         if(!IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid,COLOR_GREY,"This is a vehicle command.");
  82.         new vID = GetPlayerVehicleID(playerid);
  83.         GetVehiclePos(vID,Pos[0],Pos[1],Pos[2]);
  84.         GetVehicleZAngle(vID,Pos[3]);
  85.         new vModel = GetVehicleModel(vID);
  86.         format(query,sizeof(query),"INSERT INTO `cars` (name,modelid,carx,cary,carz,carzangle) VALUES ('%s','%d','%f','%f','%f','%f')",GetVehicleName(vID),vModel,Pos[0],Pos[1],Pos[2],Pos[3]);
  87.         db_query(Users,query);
  88.         GameTextForPlayer(playerid,"~g~Adding Car",2000,3);
  89.         LoadCars();
  90.         SetTimer("Success",2000,false);
  91.         return 1;
  92. }
  93. CMD:deleteveh(playerid,params[])
  94. {
  95.     new id,DBResult:Result;
  96.     if(sscanf(params,"d",id)) return SendClientMessage(playerid,COLOR_WHITE,"USAGE: /deleteveh [id]");
  97.     format(query,sizeof(query),"SELECT `id` FROM `cars` WHERE `id` = '%d'",id);
  98.     Result = db_query(Users,query);
  99.     new rows = db_num_rows(Result);
  100.     if(!rows) return SendClientMessage(playerid,COLOR_GREY,"No results found in database with that ID.");
  101.     format(query,sizeof(query),"DELETE * FROM `cars` WHERE `id` = '%d'",id);
  102.     db_query(Users,query);
  103.     format(str,sizeof(str),"You've successfully deleted vehicle ID: %d",id);
  104.     SendClientMessage(playerid,COLOR_ORANGE,str);
  105.     for(new v = 0; v < MAX_VEHICLES; v++)
  106.         SetVehicleToRespawn(v);
  107.     return 1;
  108. }
  109. CMD:vehname(playerid,params[])
  110. {
  111.     new id,DBResult:Result[2];
  112.     if(sscanf(params,"d",id)) return SendClientMessage(playerid,COLOR_WHITE,"USAGE: /vehname [id]");
  113.     format(query,sizeof(query),"SELECT `id` FROM `cars` WHERE `id` = '%d'",id);
  114.     Result[0] = db_query(Users,query);
  115.     new rows = db_num_rows(Result[0]);
  116.     if(!rows) return print("Invalid vehicle ID in the stock GetVehicleNameByID");
  117.     format(query,sizeof(query),"SELECT `name` FROM `cars` WHERE `id` = '%d'",id);
  118.     Result[1] = db_query(Users,query);
  119.     if(db_num_rows(Result[1]))
  120.     {
  121.         db_get_field(Result[1],0,data,sizeof(data));
  122.         format(str,sizeof(str),"The vehicle name of ID: %d is {FFFFFF}%s",id,data);
  123.     }
  124.     SendClientMessage(playerid,COLOR_WHITE,str);
  125.     return 1;
  126. }
  127. stock LoadCars() // A stock which loads the all the cars from the database to the server.
  128. {
  129.         new id = 1;
  130.         new DBResult: result;
  131.         while(id < MAX_VEHICLES)
  132.         {
  133.             new modelid,Float:carx,Float:cary,Float:carz,Float:carzangle;
  134.             format(query,sizeof(query),"SELECT * FROM `cars` WHERE `id` = '%d'",id);
  135.             result = db_query(Users,query);
  136.             if(db_num_rows(result))
  137.             {
  138.                 db_get_field_assoc(result, "modelid",data,sizeof(data)); modelid = strval(data);
  139.                 db_get_field_assoc(result, "carx",data,sizeof(data)); carx = floatstr(data);
  140.                 db_get_field_assoc(result, "cary",data,sizeof(data)); cary = floatstr(data);
  141.                 db_get_field_assoc(result, "carz",data,sizeof(data)); carz = floatstr(data);
  142.                 db_get_field_assoc(result, "carzangle",data,sizeof(data)); carzangle = floatstr(data);
  143.                 Vehicle[id] = CreateVehicle(modelid,carx,cary,carz,carzangle,random(225),random(225),60);
  144.             }
  145.             db_free_result(result);
  146.             id++;
  147.         }
  148.         return 1;
  149. }
  150. new VehicleNames[][] =
  151. {
  152.     "Landstalker", "Bravura", "Buffalo", "Linerunner", "Perrenial", "Sentinel",
  153.     "Dumper", "Firetruck", "Trashmaster", "Stretch", "Manana", "Infernus",
  154.     "Voodoo", "Pony", "Mule", "Cheetah", "Ambulance", "Leviathan", "Moonbeam",
  155.     "Esperanto", "Taxi", "Washington", "Bobcat", "Whoopee", "BF Injection",
  156.     "Hunter", "Premier", "Enforcer", "Securicar", "Banshee", "Predator", "Bus",
  157.     "Rhino", "Barracks", "Hotknife", "Trailer", "Previon", "Coach", "Cabbie",
  158.     "Stallion", "Rumpo", "RC Bandit", "Romero", "Packer", "Monster", "Admiral",
  159.     "Squalo", "Seasparrow", "Pizzaboy", "Tram", "Trailer", "Turismo", "Speeder",
  160.     "Reefer", "Tropic", "Flatbed", "Yankee", "Caddy", "Solair", "Berkley's RC Van",
  161.     "Skimmer", "PCJ-600", "Faggio", "Freeway", "RC Baron", "RC Raider", "Glendale",
  162.     "Oceanic","Sanchez", "Sparrow", "Patriot", "Quad", "Coastguard", "Dinghy",
  163.     "Hermes", "Sabre", "Rustler", "ZR-350", "Walton", "Regina", "Comet", "BMX",
  164.     "Burrito", "Camper", "Marquis", "Baggage", "Dozer", "Maverick", "News Chopper",
  165.     "Rancher", "FBI Rancher", "Virgo", "Greenwood", "Jetmax", "Hotring", "Sandking",
  166.     "Blista Compact", "Police Maverick", "Boxville", "Benson", "Mesa", "RC Goblin",
  167.     "Hotring Racer A", "Hotring Racer B", "Bloodring Banger", "Rancher", "Super GT",
  168.     "Elegant", "Journey", "Bike", "Mountain Bike", "Beagle", "Cropduster", "Stunt",
  169.     "Tanker", "Roadtrain", "Nebula", "Majestic", "Buccaneer", "Shamal", "Hydra",
  170.     "FCR-900", "NRG-500", "HPV1000", "Cement Truck", "Tow Truck", "Fortune",
  171.     "Cadrona", "FBI Truck", "Willard", "Forklift", "Tractor", "Combine", "Feltzer",
  172.     "Remington", "Slamvan", "Blade", "Freight", "Streak", "Vortex", "Vincent",
  173.     "Bullet", "Clover", "Sadler", "Firetruck", "Hustler", "Intruder", "Primo",
  174.     "Cargobob", "Tampa", "Sunrise", "Merit", "Utility", "Nevada", "Yosemite",
  175.     "Windsor", "Monster", "Monster", "Uranus", "Jester", "Sultan", "Stratium",
  176.     "Elegy", "Raindance", "RC Tiger", "Flash", "Tahoma", "Savanna", "Bandito",
  177.     "Freight Flat", "Streak Carriage", "Kart", "Mower", "Dune", "Sweeper",
  178.     "Broadway", "Tornado", "AT-400", "DFT-30", "Huntley", "Stafford", "BF-400",
  179.     "News Van", "Tug", "Trailer", "Emperor", "Wayfarer", "Euros", "Hotdog", "Club",
  180.     "Freight Box", "Trailer", "Andromada", "Dodo", "RC Cam", "Launch", "Police Car",
  181.     "Police Car", "Police Car", "Police Ranger", "Picador", "S.W.A.T", "Alpha",
  182.     "Phoenix", "Glendale", "Sadler", "Luggage", "Luggage", "Stairs", "Boxville",
  183.     "Tiller", "Utility Trailer"
  184. };
  185.  
  186. stock GetVehicleName(vehicleid) // Gets the vehicle name by it's ID
  187. {
  188.     format(str,sizeof(str),"%s",VehicleNames[GetVehicleModel(vehicleid) - 400]);
  189.     return str;
  190. }
  191. stock DB_Escape(text[])
  192. {
  193.     new
  194.         ret[MAX_INI_ENTRY_TEXT * 2],
  195.         ch,
  196.         i,
  197.         j;
  198.     while ((ch = text[i++]) && j < sizeof (ret))
  199.     {
  200.         if (ch == '\'')
  201.         {
  202.             if (j < sizeof (ret) - 2)
  203.             {
  204.                 ret[j++] = '\'';
  205.                 ret[j++] = '\'';
  206.             }
  207.         }
  208.         else if (j < sizeof (ret))
  209.         {
  210.             ret[j++] = ch;
  211.         }
  212.         else
  213.         {
  214.             j++;
  215.         }
  216.     }
  217.     ret[sizeof (ret) - 1] = '\0';
  218.     return ret;
  219. }
  220. forward Success();
  221. public Success()
  222. {
  223.     for(new i = 0; i < MAX_PLAYERS; i++)
  224.     {
  225.         format(str,sizeof(str),"You've successfully added the vehicle!");
  226.         SendClientMessage(i,COLOR_ORANGE,str);
  227.     }
  228.     return 1;
  229. }
Advertisement
Add Comment
Please, Sign In to add comment