Jstylezzz

[MySQL: Un-Threaded] jGarage - Dynamic garages [V1.0b]

Jan 16th, 2014
2,512
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. *                                                           jGarage V1.0b
  3. *                                                       by Jstylezzz
  4. *
  5. *
  6. *
  7. *   To create garages, log in as Rcon and use the command '/creategarage'. To remove a garage use '/removegarage' when standing on it's pickup.
  8. *       Use '/garagehelp' to view the available commands. If the server crashes when creating a garage, you probably forgot the 'garages' folder
  9. *   in the scriptfiles folder. Create it.
  10. *   If you have any questions suggestions or if you find bugs, please post in the release thread. Also, keep the credits, that's all I ask. Thanks!
  11. *
  12. *
  13. */
  14.  
  15. //=== INCLUDES ===//
  16. #include <a_samp> //Credits to the SA-MP Team
  17. #include <a_mysql> //Credits to BlueG
  18. #include <streamer> //Credits to Incognito
  19. #include <sscanf2> //Credits to Y_Less
  20. #include <zcmd> //Credits to Zeex
  21. //=== DEFINES ===//
  22.  
  23. //Config defines, change to your likings
  24. #define MAX_GARAGES 100 //Max garages to be created in the server
  25. #define GARAGE_OWNED_PICKUP 1559 //Change this to the pickup model you prefer. Default: White arrow (diamond)
  26. #define GARAGE_FREE_PICKUP 1273 //Change this to the pickup model you prefer. Default: Green house
  27. #define GARAGE_OWNED_TEXT "Owner: %s\nLocked: %s" //This text will appear at all owned garages
  28. #define GARAGE_FREE_TEXT "FOR SALE!\n Price: %d\n\nUse /buygarage to buy this garage." //This text will appear at all garages that are for sale
  29. #define DD 200.0 //The streamdistance for the textlabels
  30. #define TXTCOLOR 0xF9C50FFF //The textcolor for the textlabels
  31. #define COLOR_USAGE 0xBB4D4DFF //The textcolor for the 'command usage' message
  32. #define COLOR_SUCCESS 0x00AE00FF //The textcolor for the 'command sucessfull' message
  33. #define COLOR_ERROR 0xFF0000FF //The textcolor for the 'error' message
  34. #define COLOR_ORANGE 0xFFA500FF //The color orange
  35. #define COLOR_LIGHTBLUE 0xADD8E6FF //The color light blue
  36.  
  37. #define HOST "localhost"
  38. #define USER "root"
  39. #define PASS ""
  40. #define DB "SAMP"
  41.  
  42. //System defines, no need to change stuff here
  43. #define SCRIPT_VERSION "V1.0b"
  44.  
  45. //=== ENUMS ===//
  46. enum garageInfo{
  47.  
  48.     Owner[24], //Holds the name of the owner
  49.     Owned, //Holds the owned value (1 if owned, 0 if for sale)
  50.     Locked, //The locked status of the garage (0 unlocked, 1 locked)
  51.     Price, //The price of the garage
  52.     Float:PosX, //The outside X position of the garage
  53.     Float:PosY, //The outside Y position of the garage
  54.     Float:PosZ, //The outside Z position of the garage
  55.     Interior, //The internal interior number of the garage
  56.     UID //Unique ID, keeps a unique ID of the garages so the virtualworld doesn't mix up when deleting and reloading garages
  57. }
  58.  
  59.  
  60. //=== NEWS ===//
  61. new gInfo[MAX_GARAGES][garageInfo]; //This is used to access variable from our enumerator
  62. new garageCount; //This will hold the total of loaded garages
  63. new Float:GarageInteriors[][] = //This array holds the coordinates, facing angle and interior ID of the garages.
  64. {
  65.     {616.4642, -124.4003, 997.5993, 90.0, 3.0}, // Small garage
  66.     {617.0011, -74.6962, 997.8426, 90.0, 2.0}, // Medium garage
  67.     {606.4268, -9.9375, 1000.7485, 270.0, 1.0} //Big garage
  68.  
  69. };
  70. new Text3D:garageLabel[MAX_GARAGES]; //Will hold the garage label
  71. new garagePickup[MAX_GARAGES]; //Will hold the garage pickup
  72. new lastGarage[MAX_PLAYERS]; //Will hold the last garage ID the player went in to
  73.  
  74. //=== NATIVE PUBLICS ===//
  75.  
  76. public OnFilterScriptInit()
  77. {
  78.     print("\n--------------------------------------");
  79.     printf(" jGarage %s by Jstylezzz loading..",SCRIPT_VERSION);
  80.     print("--------------------------------------\n");
  81.     mysql_debug(1);
  82.     mysql_connect(HOST,USER,DB,PASS);
  83.     Load_Garages();
  84.  
  85.     return 1;
  86. }
  87.  
  88. public OnFilterScriptExit()
  89. {
  90.     Save_Garages();
  91.     Remove_PickupsAndLabels();
  92.     return 1;
  93. }
  94.  
  95. //=== STOCKS ===//
  96. stock Load_Garages() //Loads all garages
  97. {
  98.     garageCount = 1; //Make a debug garage
  99.     new sql[128] = "SELECT * FROM `garages`";
  100.     new i;
  101.     mysql_query(sql);
  102.     mysql_store_result();
  103.     while(mysql_fetch_row(sql))
  104.     {
  105.  
  106.         sscanf(sql, "e<p<|>s[24]dddfffdd>", gInfo[i]);
  107.         printf("DEBUG: %s %d %d %d %f %f %f %d %d",gInfo[i][Owner],gInfo[i][Owned],gInfo[i][Locked],gInfo[i][Price],gInfo[i][PosX],gInfo[i][PosY],gInfo[i][PosZ],gInfo[i][Interior],gInfo[i][UID]);
  108.         UpdateGarageInfo(i);
  109.         garageCount++;
  110.         i++;
  111.  
  112.     }
  113.     mysql_free_result();
  114.     printf("[jGarage]: Loaded %d garages.",garageCount-1); //The debug garage isn't a real garage, so we take 1 away
  115.     garageCount++; //To prevent overwriting/not detecting of garages
  116. }
  117. stock Save_Garages() //Saves all the garages
  118. {
  119.     for(new i=0; i < garageCount+1; i++)
  120.     {
  121.         new sql[256];
  122.         format(sql,sizeof(sql),"UPDATE `garages` SET \
  123.         `Owner`='%s',\
  124.         `Owned`='%d',\
  125.         `Locked`='%d',\
  126.         `Price`='%d',\
  127.         `PosX`='%f',\
  128.         `PosY`='%f',\
  129.         `PosZ`='%f',\
  130.         `Interior`='%d',\
  131.         `UID`='%d'\
  132.         WHERE `UID`='%d'",
  133.         gInfo[i][Owner],
  134.         gInfo[i][Owned],
  135.         gInfo[i][Locked],
  136.         gInfo[i][Price],
  137.         gInfo[i][PosX],
  138.         gInfo[i][PosY],
  139.         gInfo[i][PosZ],
  140.         gInfo[i][Interior],
  141.         gInfo[i][UID],
  142.         gInfo[i][UID]);
  143.         mysql_query(sql);
  144.     }
  145. }
  146. stock Save_Garage(gid) //Saves a specific garage
  147. {
  148.     new sql[256];
  149.     format(sql,sizeof(sql),"UPDATE `garages` SET \
  150.     `Owner`='%s',\
  151.     `Owned`='%d',\
  152.     `Locked`='%d',\
  153.     `Price`='%d',\
  154.     `PosX`='%f',\
  155.     `PosY`='%f',\
  156.     `PosZ`='%f',\
  157.     `Interior`='%d',\
  158.     `UID`='%d'\
  159.     WHERE `UID`='%d'",
  160.     gInfo[gid][Owner],
  161.     gInfo[gid][Owned],
  162.     gInfo[gid][Locked],
  163.     gInfo[gid][Price],
  164.     gInfo[gid][PosX],
  165.     gInfo[gid][PosY],
  166.     gInfo[gid][PosZ],
  167.     gInfo[gid][Interior],
  168.     gInfo[gid][UID],
  169.     gInfo[gid][UID]);
  170.     mysql_query(sql);
  171.  
  172. }
  173. stock UpdateGarageInfo(gid) //Updates/creates the garage text and label
  174. {
  175.     //Get rid of the old label and pickup (if existing)
  176.     DestroyDynamic3DTextLabel(garageLabel[gid]);
  177.     DestroyDynamicPickup(garagePickup[gid]);
  178.  
  179.     //Re-create them with the correct data
  180.     new ltext[128];
  181.     if(gInfo[gid][Owned] == 1) //If the garage is owned
  182.     {
  183.         format(ltext,128,GARAGE_OWNED_TEXT,gInfo[gid][Owner],GetLockedStatus(gInfo[gid][Locked]));
  184.         garageLabel[gid] = CreateDynamic3DTextLabel(ltext, TXTCOLOR, gInfo[gid][PosX],gInfo[gid][PosY],gInfo[gid][PosZ]+0.1,DD);
  185.         garagePickup[gid] = CreateDynamicPickup(GARAGE_OWNED_PICKUP,1,gInfo[gid][PosX],gInfo[gid][PosY],gInfo[gid][PosZ]+0.2);
  186.     }
  187.     if(gInfo[gid][Owned] == 0)
  188.     {
  189.         format(ltext,128,GARAGE_FREE_TEXT,gInfo[gid][Price]);
  190.         garageLabel[gid] = CreateDynamic3DTextLabel(ltext, TXTCOLOR, gInfo[gid][PosX],gInfo[gid][PosY],gInfo[gid][PosZ]+0.1,DD);
  191.         garagePickup[gid] = CreateDynamicPickup(GARAGE_FREE_PICKUP,1,gInfo[gid][PosX],gInfo[gid][PosY],gInfo[gid][PosZ]);
  192.     }
  193. }
  194. stock GetLockedStatus(value) //Returns 'Locked' or 'Unlocked' according to the value given
  195. {
  196.     new out[64];
  197.     if(value == 1)
  198.     {
  199.         out = "Yes";
  200.     }
  201.     else
  202.     {
  203.         out = "No";
  204.     }
  205.     return out;
  206. }
  207. stock GetPlayerNameEx(playerid)
  208. {
  209.     new pName[24];
  210.     GetPlayerName(playerid,pName,24);
  211.     return pName;
  212. }
  213. stock Remove_PickupsAndLabels()
  214. {
  215.     for(new i=0; i < garageCount+1; i++)
  216.     {
  217.         DestroyDynamic3DTextLabel(garageLabel[i]);
  218.         DestroyDynamicPickup(garagePickup[i]);
  219.     }
  220. }
  221. //=== COMMANDS ===//
  222. CMD:garagehelp(playerid,params[])
  223. {
  224.     SendClientMessage(playerid, COLOR_ORANGE, "jGarage commands:");
  225.     if(!IsPlayerAdmin(playerid))
  226.     {
  227.         SendClientMessage(playerid, COLOR_LIGHTBLUE, "/genter | /gexit | /lockgarage | /buygarage | /sellgarage");
  228.     }
  229.     else
  230.     {
  231.         SendClientMessage(playerid, COLOR_LIGHTBLUE, "/creategarage | /removegarage | /garagetypes | /genter | /gexit | /lockgarage | /buygarage | /sellgarage");
  232.     }
  233.     return 1;
  234. }
  235. CMD:garagetypes(playerid,params[])
  236. {
  237.     if(!IsPlayerAdmin(playerid)) return 0;
  238.     SendClientMessage(playerid, COLOR_ORANGE, "jGarage info - Garage types");
  239.     SendClientMessage(playerid, COLOR_LIGHTBLUE, "Type 0: Small garage");
  240.     SendClientMessage(playerid, COLOR_LIGHTBLUE, "Type 1: Medium garage");
  241.     SendClientMessage(playerid, COLOR_LIGHTBLUE, "Type 2: Big garage");
  242.     return 1;
  243. }
  244. CMD:creategarage(playerid,params[])
  245. {
  246.     if(!IsPlayerAdmin(playerid)) return 0;
  247.     if(garageCount == MAX_GARAGES) return SendClientMessage(playerid, COLOR_USAGE, "The max. amount of garages is reached. Increase the limit in the jGarage filterscript.");
  248.     new price, type;
  249.     if(sscanf(params,"dd",price, type)) return SendClientMessage(playerid, COLOR_USAGE, "Usage: /creategarage <price> <type>  || Use /garagetypes for a list of garage types.");
  250.     new Float:X, Float:Y, Float:Z;
  251.     GetPlayerPos(playerid, X,Y,Z);
  252.     format(gInfo[garageCount][Owner],24,"the State");
  253.     gInfo[garageCount][Owned] = 0;
  254.     gInfo[garageCount][Price] = price;
  255.     gInfo[garageCount][Interior] = type;
  256.     gInfo[garageCount][UID] = garageCount;
  257.     gInfo[garageCount][PosX] = X;
  258.     gInfo[garageCount][PosY] = Y;
  259.     gInfo[garageCount][PosZ] = Z;
  260.     gInfo[garageCount][Locked] = 1;
  261.     new sql[128];
  262.     format(sql,sizeof(sql),"INSERT INTO `garages` (UID) VALUES ('%d')",garageCount); //Insert the UID into the SQL database, and save the data based on the UID
  263.     mysql_query(sql);
  264.     Save_Garage(garageCount);
  265.     UpdateGarageInfo(garageCount);
  266.     garageCount++;
  267.     SendClientMessage(playerid,COLOR_SUCCESS,"Garage created!");
  268.     return 1;
  269. }
  270. CMD:removegarage(playerid,params[])
  271. {
  272.     if(!IsPlayerAdmin(playerid)) return 0;
  273.     for(new i=0; i < garageCount+1; i++)
  274.     {
  275.         if(IsPlayerInRangeOfPoint(playerid, 3.0, gInfo[i][PosX], gInfo[i][PosY], gInfo[i][PosZ]))
  276.         {
  277.             new sql[128];
  278.             format(sql,sizeof(sql),"DELETE FROM `garages` WHERE `UID`='%d'",gInfo[i][UID]);
  279.             mysql_query(sql);
  280.             format(gInfo[i][Owner],24,"REMOVED");
  281.             gInfo[i][Owned] = -999;
  282.             gInfo[i][Price] = -999;
  283.             gInfo[i][Interior] = -999;
  284.             gInfo[i][UID] = -999;
  285.             gInfo[i][PosX] = -999;
  286.             gInfo[i][PosY] = -999;
  287.             gInfo[i][PosZ] = -999;
  288.             gInfo[i][Locked] = -999;
  289.             DestroyDynamic3DTextLabel(garageLabel[i]);
  290.             DestroyDynamicPickup(garagePickup[i]);
  291.             SendClientMessage(playerid, COLOR_SUCCESS, "You have removed this garage.");
  292.             return 1;
  293.         }
  294.     }
  295.     SendClientMessage(playerid, COLOR_ERROR,"Error: You're not near any garage.");
  296.     return 1;
  297. }
  298. CMD:genter(playerid,params[])
  299. {
  300.     for(new i=0; i < garageCount+1; i++)
  301.     {
  302.         if(IsPlayerInRangeOfPoint(playerid, 3.0, gInfo[i][PosX], gInfo[i][PosY], gInfo[i][PosZ]))
  303.         {
  304.        
  305.             if(gInfo[i][Locked] == 1 && strcmp(GetPlayerNameEx(playerid),gInfo[i][Owner])) return SendClientMessage(playerid,COLOR_ERROR,"Error: You're not the owner of this garage. It's locked, you can't enter.");
  306.             new gtype = gInfo[i][Interior];
  307.             if(!IsPlayerInAnyVehicle(playerid))
  308.             {
  309.                 SetPlayerVirtualWorld(playerid,gInfo[i][UID]);
  310.                 SetPlayerInterior(playerid,floatround(GarageInteriors[gtype][4]));
  311.                 SetPlayerPos(playerid,GarageInteriors[gtype][0],GarageInteriors[gtype][1],GarageInteriors[gtype][2]);
  312.                 lastGarage[playerid] = i;
  313.             }
  314.             else
  315.             {
  316.                 new vid = GetPlayerVehicleID(playerid);
  317.                 LinkVehicleToInterior(vid,floatround(GarageInteriors[gtype][4]));
  318.                 SetVehicleVirtualWorld(vid,gInfo[i][UID]);
  319.                 SetPlayerVirtualWorld(playerid,gInfo[i][UID]);
  320.                 SetPlayerInterior(playerid,floatround(GarageInteriors[gtype][4]));
  321.                 SetVehiclePos(vid,GarageInteriors[gtype][0],GarageInteriors[gtype][1],GarageInteriors[gtype][2]);
  322.                 lastGarage[playerid] = i;
  323.             }
  324.             return 1;
  325.  
  326.         }
  327.     }
  328.     SendClientMessage(playerid,COLOR_ERROR,"Error: You're not near any garage. ");
  329.     return 1;
  330. }
  331. CMD:gexit(playerid,params[])
  332. {
  333.     if(lastGarage[playerid] >= 0)
  334.     {
  335.         new lg = lastGarage[playerid];
  336.         if(!IsPlayerInAnyVehicle(playerid))
  337.         {
  338.             SetPlayerPos(playerid,gInfo[lg][PosX],gInfo[lg][PosY],gInfo[lg][PosZ]);
  339.             SetPlayerInterior(playerid,0);
  340.             SetPlayerVirtualWorld(playerid,0);
  341.         }
  342.         else
  343.         {
  344.             new vid = GetPlayerVehicleID(playerid);
  345.             LinkVehicleToInterior(vid,0);
  346.             SetVehicleVirtualWorld(vid,0);
  347.             SetVehiclePos(vid,gInfo[lg][PosX],gInfo[lg][PosY],gInfo[lg][PosZ]);
  348.             SetPlayerVirtualWorld(playerid,0);
  349.             SetPlayerInterior(playerid,0);
  350.         }
  351.         lastGarage[playerid] = -999;
  352.     }
  353.     else return SendClientMessage(playerid,COLOR_ERROR,"Error: You're not in any garage.");
  354.     return 1;
  355. }
  356.            
  357. CMD:buygarage(playerid, params[])
  358. {
  359.     for(new i=0; i < garageCount+1; i++)
  360.     {
  361.         if(IsPlayerInRangeOfPoint(playerid, 3.0, gInfo[i][PosX], gInfo[i][PosY], gInfo[i][PosZ]))
  362.         {
  363.             if(gInfo[i][Owned] == 1) return SendClientMessage(playerid, COLOR_ERROR,"Error: This garage is already owned.");
  364.             if(GetPlayerMoney(playerid) < gInfo[i][Price]) return SendClientMessage(playerid,COLOR_ERROR,"Error: You don't have enough money to buy this garage.");
  365.             GivePlayerMoney(playerid,-gInfo[i][Price]);
  366.             gInfo[i][Price]-= random(5000); //Take some money off of the original price
  367.             format(gInfo[i][Owner],24,"%s",GetPlayerNameEx(playerid));
  368.             gInfo[i][Owned] = 1;
  369.             Save_Garage(i);
  370.             UpdateGarageInfo(i);
  371.             SendClientMessage(playerid,COLOR_SUCCESS,"You have successfully bought this garage.");
  372.             return 1;
  373.         }
  374.     }
  375.     SendClientMessage(playerid,COLOR_ERROR,"Error: You're not near any garage.");
  376.     return 1;
  377. }
  378. CMD:lockgarage(playerid,params[])
  379. {
  380.     for(new i=0; i < garageCount+1; i++)
  381.     {
  382.         if(IsPlayerInRangeOfPoint(playerid, 3.0, gInfo[i][PosX], gInfo[i][PosY], gInfo[i][PosZ]))
  383.         {
  384.             if(strcmp(gInfo[i][Owner],GetPlayerNameEx(playerid))) return SendClientMessage(playerid,COLOR_ERROR,"Error: You're not the owner of this garage.");
  385.             if(gInfo[i][Locked] == 1)
  386.             {
  387.                 gInfo[i][Locked] = 0;
  388.                 UpdateGarageInfo(i);
  389.                 Save_Garage(i);
  390.                 SendClientMessage(playerid,COLOR_SUCCESS,"You have unlocked your garage.");
  391.                 return 1;
  392.             }
  393.             else
  394.             {
  395.                 gInfo[i][Locked] = 1;
  396.                 UpdateGarageInfo(i);
  397.                 Save_Garage(i);
  398.                 SendClientMessage(playerid,COLOR_SUCCESS,"You have locked your garage.");
  399.                 return 1;
  400.             }
  401.         }
  402.     }
  403.     SendClientMessage(playerid,COLOR_ERROR,"Error: You're not near any garage.");
  404.     return 1;
  405. }
  406. CMD:sellgarage(playerid,params[])
  407. {
  408.     for(new i=0; i < garageCount+1; i++)
  409.     {
  410.         if(IsPlayerInRangeOfPoint(playerid, 3.0, gInfo[i][PosX], gInfo[i][PosY], gInfo[i][PosZ]))
  411.         {
  412.             if(strcmp(gInfo[i][Owner],GetPlayerNameEx(playerid))) return SendClientMessage(playerid,COLOR_ERROR,"Error: You're not the owner of this garage.");
  413.             GivePlayerMoney(playerid,gInfo[i][Price]-random(500));
  414.             gInfo[i][Owned] = 0;
  415.             format(gInfo[i][Owner],24,"the State");
  416.             gInfo[i][Locked] = 1;
  417.             UpdateGarageInfo(i);
  418.             Save_Garage(i);
  419.             SendClientMessage(playerid, COLOR_SUCCESS,"You have successfully sold your garage.");
  420.             return 1;
  421.           }
  422.     }
  423.     SendClientMessage(playerid, COLOR_ERROR,"You're not near any garage.");
  424.     return 1;
  425. }
RAW Paste Data