core.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #include <YSI_Coding\y_hooks>
  2. forward OnVehicleCreated(sqlid);
  3. forward OnVehicleDeleted(sqlid);
  4. forward OnServerVehicleSpawned(vehicleid);
  5. forward OnServerVehicleDespawned(vehicleid);
  6. #define MAX_VEHICLE_PLATE 33
  7. static VehicleSQLID[MAX_VEHICLES];
  8. static VehicleOwnerSQLID[MAX_VEHICLES];
  9. static VehiclePrice[MAX_VEHICLES];
  10. static VehicleModel[MAX_VEHICLES];
  11. static VehiclePlate[MAX_VEHICLES][MAX_VEHICLE_PLATE];
  12. static VehicleColOne[MAX_VEHICLES];
  13. static VehicleColTwo[MAX_VEHICLES];
  14. static Float:VehiclePosX[MAX_VEHICLES];
  15. static Float:VehiclePosY[MAX_VEHICLES];
  16. static Float:VehiclePosZ[MAX_VEHICLES];
  17. static Float:VehiclePosA[MAX_VEHICLES];
  18. static VehicleWorld[MAX_VEHICLES];
  19. static VehicleInterior[MAX_VEHICLES];
  20. hook OnMySQLPreClose()
  21. {
  22. foreach(new i : Vehicle)
  23. {
  24. if(VehicleSQLID[i] != 0)
  25. {
  26. new world = GetVehicleVirtualWorld(i);
  27. new Float:x, Float:y, Float:z, Float:a;
  28. GetVehiclePos(i, x, y, z);
  29. GetVehicleZAngle(i, a);
  30. new query[184];
  31. mysql_format(MySQL_GetHandle(), query, sizeof(query), "UPDATE vehicles SET pos_x = %f, pos_y = %f, pos_z = %f, pos_a = %f, world = %d WHERE id = %d", x, y, z, a, world, VehicleSQLID[i]);
  32. mysql_tquery(MySQL_GetHandle(), query);
  33. }
  34. }
  35. }
  36. CMD:createvehicle(playerid, params[])
  37. {
  38. new modelid, colOne, colTwo;
  39. if(sscanf(params, "iii", modelid, colOne, colTwo)) return SendSyntaxMessage(playerid, "/createvehicle (modelid) (color one) (color two)");
  40. new Float:x, Float:y, Float:z, Float:a;
  41. GetPlayerPos(playerid, x, y, z);
  42. GetPlayerFacingAngle(playerid, a);
  43. new world = GetPlayerVirtualWorld(playerid);
  44. new interior = GetPlayerInterior(playerid);
  45. inline const OnVehicleInserted()
  46. {
  47. new sqlid = cache_insert_id();
  48. printf("Vehicle ID %d has been created.", sqlid);
  49. SendAdminWarningF(playerid, "You have created a vehicle. (SQL ID %d)", sqlid);
  50. CallRemoteFunction("OnVehicleCreated", "i", sqlid);
  51. }
  52. MySQL_TQueryInline(MySQL_GetHandle(), using inline OnVehicleInserted, "INSERT INTO vehicles (owner_id, price, model, plate, col_one, col_two, pos_x, pos_y, pos_z, pos_a, world, interior) VALUES (0, 0, %d, 'null', %d, %d, %f, %f, %f, %f, %d, %d)", modelid, colOne, colTwo, x, y, z, a, world, interior);
  53. return 1;
  54. }
  55. CMD:deletevehicle(playerid, params[])
  56. {
  57. new sqlid;
  58. if(sscanf(params, "i", sqlid)) return SendSyntaxMessage(playerid, "/deletevehicle (database ID)");
  59. inline OnVehicleRemoved()
  60. {
  61. if(cache_affected_rows())
  62. {
  63. CallRemoteFunction("OnVehicleDeleted", "i", sqlid);
  64. foreach(new i : Vehicle)
  65. {
  66. if(VehicleSQLID[i] == sqlid)
  67. {
  68. CallRemoteFunction("OnServerVehicleDespawned", "i", i);
  69. VehicleSQLID[i] = 0;
  70. VehicleOwnerSQLID[i] = 0;
  71. DestroyVehicle(i);
  72. break;
  73. }
  74. }
  75. printf("Vehicle ID %d has been deleted.", sqlid);
  76. SendAdminWarningF(playerid, "You have deleted a vehicle. (SQL ID %d)", sqlid);
  77. }
  78. else SendAdminWarningF(playerid, "That vehicle (SQL ID %d) could not be found in the database.", sqlid);
  79. }
  80. MySQL_TQueryInline(MySQL_GetHandle(), using inline OnVehicleRemoved, "DELETE FROM vehicles WHERE id = %d", sqlid);
  81. return 1;
  82. }
  83. CMD:spawnvehicle(playerid, params[])
  84. {
  85. new sqlid;
  86. if(sscanf(params, "i", sqlid)) return SendSyntaxMessage(playerid, "/spawnvehicle (database ID)");
  87. foreach(new i : Vehicle)
  88. {
  89. if(VehicleSQLID[i] == sqlid) return SendErrorMessage(playerid, "That vehicle is already spawned.");
  90. }
  91. inline const OnVehicleFound()
  92. {
  93. if(cache_num_rows())
  94. {
  95. new id;
  96. new ownerid;
  97. new model;
  98. new price;
  99. new plate[MAX_VEHICLE_PLATE];
  100. new colOne, colTwo;
  101. new world, interior;
  102. new Float:x, Float:y, Float:z, Float:a;
  103. cache_get_value_name_int(0, "id", id);
  104. cache_get_value_name_int(0, "owner_id", ownerid);
  105. cache_get_value_name_int(0, "model", model);
  106. cache_get_value_name_int(0, "price", price);
  107. cache_get_value_name(0, "plate", plate);
  108. cache_get_value_name_int(0, "col_one", colOne);
  109. cache_get_value_name_int(0, "col_two", colTwo);
  110. cache_get_value_name_float(0, "pos_x", x);
  111. cache_get_value_name_float(0, "pos_y", y);
  112. cache_get_value_name_float(0, "pos_z", z);
  113. cache_get_value_name_float(0, "pos_a", a);
  114. cache_get_value_name_int(0, "world", world);
  115. cache_get_value_name_int(0, "interior", interior);
  116. new sessionid = CreateVehicle(model, x, y, z, a, colOne, colTwo, -1);
  117. SetVehicleVirtualWorld(sessionid, world);
  118. LinkVehicleToInterior(sessionid, interior);
  119. SetVehicleNumberPlate(sessionid, plate);
  120. format(VehiclePlate[sessionid], MAX_VEHICLE_PLATE, plate);
  121. VehicleSQLID[sessionid] = sqlid;
  122. VehicleOwnerSQLID[sessionid] = ownerid;
  123. VehicleModel[sessionid] = model;
  124. VehiclePrice[sessionid] = price;
  125. VehicleColOne[sessionid] = colOne;
  126. VehicleColTwo[sessionid] = colTwo;
  127. VehiclePosX[sessionid] = x;
  128. VehiclePosY[sessionid] = y;
  129. VehiclePosZ[sessionid] = z;
  130. VehiclePosA[sessionid] = a;
  131. VehicleWorld[sessionid] = world;
  132. VehicleInterior[sessionid] = interior;
  133. CallRemoteFunction("OnServerVehicleSpawned", "i", sessionid);
  134. SendAdminWarningF(playerid, "You spawned vehicle ID %d, SQL ID %d.", sessionid, sqlid);
  135. }
  136. else SendAdminWarningF(playerid, "That vehicle (SQL ID %d) could not be found in the database.", sqlid);
  137. }
  138. MySQL_TQueryInline(MySQL_GetHandle(), using inline OnVehicleFound, "SELECT * FROM vehicles WHERE id = %d", sqlid);
  139. return 1;
  140. }
  141. CMD:despawnvehicle(playerid, params[])
  142. {
  143. new sessionid;
  144. if(sscanf(params, "i", sessionid)) return SendSyntaxMessage(playerid, "/despawnveh (vehicleid)");
  145. if(!IsValidVehicle(sessionid)) return SendErrorMessage(playerid, "That vehicle is not spawned.");
  146. new world = GetVehicleVirtualWorld(sessionid);
  147. new Float:x, Float:y, Float:z, Float:a;
  148. GetVehiclePos(sessionid, x, y, z);
  149. GetVehicleZAngle(sessionid, a);
  150. new query[184];
  151. mysql_format(MySQL_GetHandle(), query, sizeof(query), "UPDATE vehicles SET pos_x = %f, pos_y = %f, pos_z = %f, pos_a = %f, world = %d WHERE id = %d", x, y, z, a, world, VehicleSQLID[sessionid]);
  152. mysql_tquery(MySQL_GetHandle(), query);
  153. VehicleSQLID[sessionid] = 0;
  154. VehicleOwnerSQLID[sessionid] = 0;
  155. DestroyVehicle(sessionid);
  156. SendAdminWarningF(playerid, "You despawned vehicle ID %d, SQL ID %d.", sessionid, VehicleSQLID[sessionid]);
  157. return 1;
  158. }
  159. stock bool:IsValidServerVehicle(vehicleid)
  160. {
  161. if(IsValidVehicle(vehicleid) && VehicleSQLID[vehicleid] != 0)
  162. {
  163. return true;
  164. }
  165. return false;
  166. }
  167. stock Vehicle_GetSQLID(vehicleid)
  168. {
  169. return VehicleSQLID[vehicleid];
  170. }
  171. stock Vehicle_SetOwnerSQLID(vehicleid, ownerid)
  172. {
  173. VehicleOwnerSQLID[vehicleid] = ownerid;
  174. new query[84];
  175. mysql_format(MySQL_GetHandle(), query, sizeof(query), "UPDATE vehicles SET owner_id = %d WHERE id = %d", ownerid, VehicleSQLID[vehicleid]);
  176. mysql_tquery(MySQL_GetHandle(), query);
  177. }
  178. stock Vehicle_GetOwnerSQLID(vehicleid)
  179. {
  180. return VehicleOwnerSQLID[vehicleid];
  181. }
  182. stock Vehicle_SetPrice(vehicleid, price)
  183. {
  184. VehiclePrice[vehicleid] = price;
  185. new query[84];
  186. mysql_format(MySQL_GetHandle(), query, sizeof(query), "UPDATE vehicles SET price = %d WHERE id = %d", price, VehicleSQLID[vehicleid]);
  187. mysql_tquery(MySQL_GetHandle(), query);
  188. }
  189. stock Vehicle_GetPrice(vehicleid)
  190. {
  191. return VehiclePrice[vehicleid];
  192. }
  193. stock Vehicle_SetColors(vehicleid, colOne, colTwo)
  194. {
  195. VehicleColOne[vehicleid] = colOne;
  196. VehicleColTwo[vehicleid] = colTwo;
  197. new query[84];
  198. mysql_format(MySQL_GetHandle(), query, sizeof(query), "UPDATE vehicles SET col_one = %d, col_two = %d WHERE id = %d", colOne, colTwo, VehicleSQLID[vehicleid]);
  199. mysql_tquery(MySQL_GetHandle(), query);
  200. }
  201. stock Vehicle_GetColors(vehicleid, &colOne, &colTwo)
  202. {
  203. colOne = VehicleColOne[vehicleid];
  204. colTwo = VehicleColTwo[vehicleid];
  205. }
  206. stock Vehicle_GetPosition(vehicleid, &Float:x, &Float:y, &Float:z, &Float:a)
  207. {
  208. x = VehiclePosX[vehicleid];
  209. y = VehiclePosY[vehicleid];
  210. z = VehiclePosZ[vehicleid];
  211. a = VehiclePosA[vehicleid];
  212. }
  213. stock Vehicle_GetWorld(vehicleid)
  214. {
  215. return VehicleWorld[vehicleid];
  216. }
  217. stock Vehicle_GetInterior(vehicleid)
  218. {
  219. return VehicleInterior[vehicleid];
  220. }