Akira_Yiin

vehicles.inc

Jun 15th, 2015
900
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 5.26 KB | None | 0 0
  1. // yiin
  2.  
  3. #include <a_samp>
  4.  
  5. /* pawno.exe
  6.     native ORM:GetVehicleORM(vehicleid);
  7.     native GetVehicleUID(vehicleid);
  8.     native AddVehicle(model, Float:x, Float:y, Float:z, Float:a, color1 = -1, color2 = -1);
  9.     native LoadVehicle(sqlid = -1, row = 0, applydata = true);
  10.     native SaveVehicle(vehicleid, update_pos = true, remove = false);
  11.     native DeleteVehicle(vehicleid);
  12. */
  13.  
  14. // functions
  15. forward ORM:CreateVehicleORM(vehicleid);
  16. forward ORM:GetVehicleORM(vehicleid);
  17.  
  18. // callbacks
  19. forward OnVehicleInsert(vehicleid);
  20. forward OnCreateVehicleORM(ORM:ormid, vehicleid);
  21.  
  22. #define format_query( format(query, sizeof(query),
  23. static query[500];
  24.  
  25. static
  26.     database,
  27.     ORM:vehicle_ORM[MAX_VEHICLES],
  28.     vehicle_ID[MAX_VEHICLES],
  29.     vehicle_Model[MAX_VEHICLES],
  30.     vehicle_Color1[MAX_VEHICLES],
  31.     vehicle_Color2[MAX_VEHICLES],
  32.     Float:vehicle_Health[MAX_VEHICLES],
  33.     Float:vehicle_PosX[MAX_VEHICLES],
  34.     Float:vehicle_PosY[MAX_VEHICLES],
  35.     Float:vehicle_PosZ[MAX_VEHICLES],
  36.     Float:vehicle_PosA[MAX_VEHICLES]
  37. ;
  38.  
  39. stock vehicle_ChangeVehicleColor(vehicleid, color1, color2) {
  40.     vehicle_Color1[vehicleid] = color1;
  41.     vehicle_Color2[vehicleid] = color2;
  42.     return ChangeVehicleColor(vehicleid, color1, color2);
  43. }
  44. #if defined _ALS_ChangeVehicleColor
  45.     #undef ChangeVehicleColor
  46. #else
  47.     #define _ALS_ChangeVehicleColor
  48. #endif
  49. #define ChangeVehicleColor vehicle_ChangeVehicleColor
  50.  
  51. stock ORM:GetVehicleORM(vehicleid) {
  52.     return vehicle_ORM[vehicleid];
  53. }
  54.  
  55. stock GetVehicleUID(vehicleid) {
  56.     return vehicle_ID[vehicleid];
  57. }
  58.  
  59. stock static ORM:CreateVehicleORM(vehicleid) {
  60.     new ORM:ormid = vehicle_ORM[vehicleid] = orm_create("vehicles");
  61.  
  62.     orm_addvar_int(ormid, vehicle_ID[vehicleid], "uid");
  63.     orm_setkey(ormid, "uid");
  64.     orm_addvar_int(ormid, vehicle_Model[vehicleid], "model");
  65.     orm_addvar_int(ormid, vehicle_Color1[vehicleid], "color1");
  66.     orm_addvar_int(ormid, vehicle_Color2[vehicleid], "color2");
  67.     orm_addvar_float(ormid, vehicle_PosX[vehicleid], "x");
  68.     orm_addvar_float(ormid, vehicle_PosY[vehicleid], "y");
  69.     orm_addvar_float(ormid, vehicle_PosZ[vehicleid], "z");
  70.     orm_addvar_float(ormid, vehicle_PosA[vehicleid], "a");
  71.     orm_addvar_float(ormid, vehicle_Health[vehicleid], "health");
  72.  
  73.     CallLocalFunction("OnCreateVehicleORM", "ii", _:ormid, vehicleid);
  74.  
  75.     return ormid;
  76. }
  77.  
  78. stock static ApplyVehicleData(vehicleid, setpos = true) {
  79.     if(setpos) {
  80.         SetVehiclePos(vehicleid,
  81.             vehicle_PosX[vehicleid],
  82.             vehicle_PosY[vehicleid],
  83.             vehicle_PosZ[vehicleid]);
  84.         SetVehicleZAngle(vehicleid,
  85.             vehicle_PosA[vehicleid]);
  86.     }
  87.     SetVehicleHealth(vehicleid, vehicle_Health[vehicleid]);
  88.     ChangeVehicleColor(vehicleid, vehicle_Color1[vehicleid], vehicle_Color2[vehicleid]);
  89. }
  90.  
  91. stock AddVehicle(model, Float:x, Float:y, Float:z, Float:a, color1 = -1, color2 = -1) {
  92.     new vehicleid = CreateVehicle(model, x, y, z, a, color1, color2, -1);
  93.     if(vehicleid == INVALID_VEHICLE_ID) {
  94.         return INVALID_VEHICLE_ID;
  95.     }
  96.     new ORM:ormid = CreateVehicleORM(vehicleid);
  97.  
  98.     vehicle_Model[vehicleid] = model;
  99.     vehicle_Color1[vehicleid] = color1;
  100.     vehicle_Color2[vehicleid] = color2;
  101.     vehicle_PosX[vehicleid] = x;
  102.     vehicle_PosY[vehicleid] = y;
  103.     vehicle_PosZ[vehicleid] = z;
  104.     vehicle_PosA[vehicleid] = a;
  105.  
  106.     ApplyVehicleData(vehicleid, .setpos = false);
  107.     orm_insert(ormid, "OnVehicleInsert", "i", vehicleid);
  108.     return vehicleid;
  109. }
  110.  
  111. stock LoadVehicle(sqlid = -1, row = 0, applydata = true) {
  112.     new Cache:cache;
  113.     if(sqlid != -1) {
  114.         format_query("SELECT * FROM vehicles WHERE uid = %i", sqlid);
  115.         cache = mysql_query(database, query);
  116.     }
  117.  
  118.     new model = cache_get_field_content_int(row, "model");
  119.     new vehicleid = CreateVehicle(model, 0, 0, 0, 0, 0, 0, 0);
  120.  
  121.     new ORM:ormid = CreateVehicleORM(vehicleid);
  122.  
  123.     orm_apply_cache(ormid, row);
  124.  
  125.     if(applydata) {
  126.         ApplyVehicleData(vehicleid);
  127.     }
  128.  
  129.     if(sqlid != -1) {
  130.         cache_delete(cache);
  131.     }
  132.  
  133.     return vehicleid;
  134. }
  135.  
  136. stock SaveVehicle(vehicleid, update_pos = true, remove = false) {
  137.     if(vehicle_ID[vehicleid] != 0) {
  138.         if(update_pos) {
  139.             GetVehiclePos(vehicleid,
  140.                 vehicle_PosX[vehicleid],
  141.                 vehicle_PosY[vehicleid],
  142.                 vehicle_PosZ[vehicleid]);
  143.             GetVehicleZAngle(vehicleid,
  144.                 vehicle_PosA[vehicleid]);
  145.         }
  146.         orm_update(vehicle_ORM[vehicleid]);
  147.         if(remove) {
  148.             orm_destroy(vehicle_ORM[vehicleid]);
  149.             DestroyVehicle(vehicleid);
  150.         }
  151.     }
  152. }
  153.  
  154. stock DeleteVehicle(vehicleid) {
  155.     if(vehicle_ID[vehicleid] != 0) {
  156.         orm_delete(vehicle_ORM[vehicleid]);
  157.         orm_destroy(vehicle_ORM[vehicleid]);
  158.         DestroyVehicle(vehicleid);
  159.     }
  160. }
  161.  
  162. public OnVehicleRespray(playerid, vehicleid, color1, color2)
  163. {
  164.     vehicle_Color1[vehicleid] = color1;
  165.     vehicle_Color2[vehicleid] = color2;
  166. #if defined vehicles_OnVehicleRespray
  167.     vehicles_OnVehicleRespray(playerid, vehicleid, color1, color2);
  168. #endif
  169.     return 1;
  170. }
  171. #if defined _ALS_OnVehicleRespray
  172.     #undef OnVehicleRespray
  173. #else
  174.     #define _ALS_OnVehicleRespray
  175. #endif
  176. #define OnVehicleRespray vehicles_OnVehicleRespray
  177. #if defined vehicles_OnVehicleRespray
  178.     forward vehicles_OnVehicleRespray(playerid, vehicleid, color1, color2);
  179. #endif
  180.  
  181. vehicles_mysql_connect(host[], user[], database[], password[]) {
  182.     return (database = mysql_connect(host, user, database, password));
  183. }
  184. #if defined _ALS_mysql_connect
  185.     #undef mysql_connect
  186. #else
  187.     #define _ALS_mysql_connect
  188. #endif
  189. #define mysql_connect vehicles_mysql_connect
Advertisement
Add Comment
Please, Sign In to add comment