1
0

hooks.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. _ _ _
  3. | | | | | |
  4. | |__| | ___ ___ | | _____
  5. | __ |/ _ \ / _ \| |/ / __|
  6. | | | | (_) | (_) | <\__ \
  7. |_| |_|\___/ \___/|_|\_\___/
  8. This file contains hooks for callbacks
  9. that the player owned vehicle system needs.
  10. I encourage hooking. It is preprocessing.
  11. It doesn't slow down execution time in-game!
  12. */
  13. /*=========================================================
  14. OnPlayerSecondSync -
  15. contains: /breakin - detect if player left vehicle
  16. ===========================================================
  17. */
  18. OnVehiclePlayerSecondSync( playerid ) {
  19. new Float:vpos[3];
  20. if( LockPickingCar[playerid] == -1 ) {
  21. return 1;
  22. }
  23. new player = LockPickingCar[playerid];
  24. new slot = GetPlayerSpawnedVehicleSlot( player );
  25. if( slot < 0 ) return 1;
  26. #define veh(%1) PlayerVehicles[player][slot][pv%1]
  27. GetVehiclePos( veh(ID), vpos[0], vpos[1], vpos[2] );
  28. if( !IsPlayerInRangeOfPoint( playerid, 3.0, vpos[0], vpos[1], vpos[2] ) ) {
  29. vehiclePickCount[playerid] = 0;
  30. LockPickingCar[playerid] = -1;
  31. HidePlayerProgressBar( playerid, workingProgress[playerid] );
  32. ClearAnimations( playerid );
  33. SendClientMessage( playerid, COLOR_GREY, "Your attempt to breakin has failed as you have walked away." );
  34. return 1;
  35. }
  36. #undef veh
  37. return 1;
  38. }
  39. /*=========================================================
  40. OnPlayerKeyStateChange -
  41. contains: /breakin; tapping Y rapidly.
  42. ===========================================================
  43. */
  44. #define PRESSING(%0,%1) \
  45. (%0 & (%1))
  46. public OnPlayerKeyStateChange( playerid, newkeys, oldkeys ) {
  47. #if defined hook_OnPlayerKeyStateChange
  48. hook_OnPlayerKeyStateChange( playerid, newkeys, oldkeys );
  49. #endif
  50. if( PRESSING( newkeys, KEY_YES ) ) {
  51. cmd_lock( playerid, "\0" );
  52. }
  53. //The user must tap "Y" 100 times to unlock the car.
  54. if( PRESSING(newkeys, KEY_YES) ) {
  55. if( LockPickingCar[playerid] == -1 ) {
  56. return 1;
  57. }
  58. vehiclePickCount[playerid] += 1;
  59. SetPlayerProgressBarValue( playerid, workingProgress[playerid], float( vehiclePickCount[playerid] ) );
  60. new player = LockPickingCar[playerid];
  61. #define veh(%1) PlayerVehicles[player][playerSpawnedVehicle[player]][pv%1]
  62. //Check if timer is up, successful breakin
  63. if( vehiclePickCount[playerid] == 100 ) {
  64. displayCenterHUDInfo( playerid, "You have broken the vehicle's lock.", 4 );
  65. veh(locked) = 0;
  66. SetVehicleLockState( veh(ID), veh(locked) ); //unlock the car
  67. vehiclePickCount[playerid] = 0;
  68. LockPickingCar[playerid] = -1;
  69. HidePlayerProgressBar( playerid, workingProgress[playerid] );
  70. ClearAnimations( playerid );
  71. ApplyAnimationEx( playerid, "CARRY", "crry_prtial", 4.0, 0, 0, 0, 0, 0 );
  72. }
  73. #undef veh
  74. }
  75. return 1;
  76. }
  77. #if defined _ALS_OnPlayerKeyStateChange
  78. #undef OnPlayerKeyStateChange
  79. #else
  80. #define _ALS_OnPlayerKeyStateChange
  81. #endif
  82. #define OnPlayerKeyStateChange hook_OnPlayerKeyStateChange
  83. #if defined hook_OnPlayerKeyStateChange
  84. forward hook_OnPlayerKeyStateChange( playerid, newkeys, oldkeys );
  85. #endif
  86. /*=========================================================
  87. OnVehicleDeath -
  88. contains: vehicle insurance
  89. ===========================================================
  90. */
  91. public OnVehicleDeath( vehicleid, killerid ) {
  92. for( new i; i <= GetPlayerPoolSize(); i++ ) {
  93. new slot = GetPlayerSpawnedVehicleSlot( i );
  94. if( slot < 0 ) continue;
  95. #define veh(%0) PlayerVehicles[i][slot][pv%0]
  96. if( veh(ID) == vehicleid ) {
  97. if( !veh(insurance) ) {
  98. TrunkInfo[vehicleid][cTrunkOpened] = 0;
  99. TrunkInfo[vehicleid][cGun1] = 0;
  100. TrunkInfo[vehicleid][cGun2] = 0;
  101. TrunkInfo[vehicleid][cCannabis] = 0;
  102. TrunkInfo[vehicleid][cCocaine] = 0;
  103. TrunkInfo[vehicleid][cArmor] = 0.0;
  104. break;
  105. }
  106. //Set vehicle to destroy -> don't respawn, let the player do it.
  107. destroyPlayerVehicle( i, slot );
  108. //spawnPlayerVehicle( i, slot );
  109. }
  110. #undef veh
  111. }
  112. //printf("exiting /vehicles/hooks: OnVehicleDeath( %d, %d ), entering SARP_current callback", vehicleid, killerid );
  113. #if defined hook_OnVehicleDeath
  114. hook_OnVehicleDeath( vehicleid );
  115. #endif
  116. return 1;
  117. }
  118. #if defined _ALS_OnVehicleDeath
  119. #undef OnVehicleDeath
  120. #else
  121. #define _ALS_OnVehicleDeath
  122. #endif
  123. #define OnVehicleDeath hook_OnVehicleDeath
  124. #if defined hook_OnVehicleDeath
  125. forward hook_OnVehicleDeath( vehicleid );
  126. #endif
  127. /*=========================================================
  128. OnVehicleRespawn
  129. fixes seat position sync issues with SAMP; just cancel respawn in last pos,
  130. change pos to parked position
  131. ===========================================================
  132. */
  133. public OnVehicleSpawn( vehicleid ) {
  134. #if defined veh_OnVehicleSpawn
  135. veh_OnVehicleSpawn( vehicleid );
  136. #endif
  137. //unlock the car by default
  138. SetVehicleLockState( vehicleid, 0 );
  139. for( new i; i <= GetPlayerPoolSize(); i++ ) {
  140. new slot = GetPlayerSpawnedVehicleSlot( i );
  141. if( slot < 0 ) continue;
  142. #define veh(%0) PlayerVehicles[i][slot][pv%0]
  143. if( veh(ID) == vehicleid ) {
  144. //Set vehicle to respawn in last parked position.
  145. destroyPlayerVehicle( i, slot );
  146. spawnPlayerVehicle( i, slot );
  147. }
  148. #undef veh
  149. }
  150. //printf("exiting /vehicles/hooks: OnVehicleSpawn( %d )", vehicleid );
  151. return 1;
  152. }
  153. #if defined _ALS_OnVehicleSpawn
  154. #undef OnVehicleSpawn
  155. #else
  156. #define _ALS_OnVehicleSpawn
  157. #endif
  158. #define OnVehicleSpawn veh_OnVehicleSpawn
  159. #if defined veh_OnVehicleSpawn
  160. forward veh_OnVehicleSpawn( vehicleid );
  161. #endif
  162. /*=========================================================
  163. OnPlayerDisconnect -
  164. contains: Reset all player vars
  165. ===========================================================
  166. */
  167. public OnPlayerDisconnect( playerid, reason ) {
  168. SQLSave( playerid );
  169. //destroy the player's vehicles
  170. new slot = GetPlayerSpawnedVehicleSlot( playerid );
  171. if( slot > -1 ) {
  172. PlayerInfo[playerid][lastCarID] = PlayerVehicles[playerid][slot][pvSQLID];
  173. destroyPlayerVehicle( playerid, slot );
  174. } else {
  175. PlayerInfo[playerid][lastCarID] = -1;
  176. }
  177. #if defined hook_OnPlayerDisconnect
  178. hook_OnPlayerDisconnect( playerid, reason );
  179. #endif
  180. //reset slots, don't rid the vehicle...
  181. for( new s; s < PlayerInfo[playerid][pMaxCarSlots]; s++ ) {
  182. for( new i; pvehicleDatav:i < pvehicleDatav; i++ ) {
  183. PlayerVehicles[playerid][s][pvehicleDatav:i] = 0;
  184. }
  185. }
  186. //sellcar info
  187. playeridcarOffered[playerid] = -1;
  188. carOfferedSlot[playerid] = -1;
  189. carOfferedPrice[playerid] = -1;
  190. spareKeys[playerid] = -1;
  191. spareKeysSlot[playerid] = -1;
  192. // /breakin time remaining
  193. vehiclePickCount[playerid] = 0;
  194. //player is upgrading vehicle index x
  195. vupgrading[playerid] = -1;
  196. //spawn timer
  197. vSpawnTimer[playerid] = 0;
  198. DestroyPlayerProgressBar( playerid, workingProgress[playerid] );
  199. printf("exiting /vehicles/hooks: OnPlayerDisconnect( %d, %d )", playerid, reason );
  200. return 1;
  201. }
  202. #if defined _ALS_OnPlayerDisconnect
  203. #undef OnPlayerDisconnect
  204. #else
  205. #define _ALS_OnPlayerDisconnect
  206. #endif
  207. #define OnPlayerDisconnect hook_OnPlayerDisconnect
  208. #if defined hook_OnPlayerDisconnect
  209. forward hook_OnPlayerDisconnect( playerid );
  210. #endif
  211. /*=========================================================
  212. OnDialogResponse
  213. contains: /vupgrade
  214. ===========================================================
  215. */
  216. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) {
  217. #if defined hook_OnDialogResponse
  218. hook_OnDialogResponse( playerid, dialogid, response, listitem, inputtext );
  219. #endif
  220. //** Upgrade menu, on response: take the money, proceed the upgrade. **
  221. if( dialogid == DIALOG_VEHICLES_UP2 ) {
  222. if( !response ) return 1;
  223. new upslot = vupgrading[playerid];
  224. //No car slot is selected to be upgraded.
  225. if( upslot < 0 ) return 1;
  226. #define veh(%1) PlayerVehicles[playerid][upslot][pv%1]
  227. if( veh(SQLID) < 1 ) {
  228. return 1;
  229. }
  230. if( !IsValidVehicle( veh(ID) ) ) {
  231. return SendClientMessage( playerid, COLOR_GREY, "You have no vehicle spawned to upgrade, try using /v spawn." );
  232. }
  233. switch( listitem ) {
  234. //GPS
  235. case 0: {
  236. if( veh(gps) ) {
  237. return SendClientMessage( playerid, COLOR_GREY, "This vehicle already has a GPS." );
  238. }
  239. if( PlayerInfo[playerid][pCash] < 350000 ) {
  240. return SendClientMessage( playerid, COLOR_GREY, "You cannot afford this." );
  241. }
  242. GiveMoney( playerid, -350000 );
  243. veh(gps) = 1;
  244. vupgrading[playerid] = -1;
  245. SendClientMessage( playerid, COLOR_WHITE, "Congratulations on your purchase! You can use /trackcar to locate your vehicle." );
  246. }
  247. //Alarm
  248. case 1: {
  249. if( veh(alarm) ) {
  250. return SendClientMessage( playerid, COLOR_GREY, "This vehicle already has an alarm." );
  251. }
  252. if( PlayerInfo[playerid][pCash] < 200000 ) {
  253. return SendClientMessage( playerid, COLOR_GREY, "You cannot afford this." );
  254. }
  255. GiveMoney( playerid, -200000 );
  256. veh(alarm) = 1;
  257. vupgrading[playerid] = -1;
  258. SendClientMessage( playerid, COLOR_WHITE, "Congratulations on your purchase! Your vehicle now has an alarm." );
  259. }
  260. //Insurance
  261. case 2: {
  262. if( veh(insurance) ) {
  263. return SendClientMessage( playerid, COLOR_GREY, "This vehicle is already insured." );
  264. }
  265. if( PlayerInfo[playerid][pCash] < 250000 ) {
  266. return SendClientMessage( playerid, COLOR_GREY, "You cannot afford this." );
  267. }
  268. GiveMoney( playerid, -250000 );
  269. veh(insurance) = 1;
  270. vupgrading[playerid] = -1;
  271. SendClientMessage( playerid, COLOR_WHITE, "Congratulations on your purchase! Your vehicle is now insured." );
  272. }
  273. //Spare keys
  274. case 3: {
  275. if( veh(sparekey) ) {
  276. return SendClientMessage( playerid, COLOR_GREY, "This vehicle already has a set of spare keys." );
  277. }
  278. if( PlayerInfo[playerid][pCash] < 250000 ) {
  279. return SendClientMessage( playerid, COLOR_GREY, "You cannot afford this." );
  280. }
  281. GiveMoney( playerid, -250000 );
  282. veh(sparekey) = 1;
  283. vupgrading[playerid] = -1;
  284. SendClientMessage( playerid, COLOR_WHITE, "Congratulations on your purchase! You now have a set of spare keys for your vehicle (/v givekeys).");
  285. }
  286. //Custom vehicle plate
  287. case 4: {
  288. if( PlayerInfo[playerid][pCash] < 100000 ) {
  289. return SendClientMessage( playerid, COLOR_GREY, "You cannot afford this." );
  290. }
  291. ShowPlayerDialog( playerid, DIALOG_CUSTOM_PLATE, DIALOG_STYLE_INPUT, "Custom Vehicle Plate", "Enter a new custom license plate for your vehicle.", "Purchase", "Cancel" );
  292. }
  293. case 5: {
  294. if( PlayerInfo[playerid][pMaxCarSlots] == MAX_VEHICLE_SLOTS ) {
  295. return SendClientMessage( playerid, COLOR_GREY, "You cannot buy anymore vehicle slots." );
  296. }
  297. if( PlayerInfo[playerid][pCash] < 15000000 ) {
  298. return SendClientMessage( playerid, COLOR_GREY, "You cannot afford this." );
  299. }
  300. GiveMoney( playerid, -15000000 );
  301. PlayerInfo[playerid][pMaxCarSlots]++;
  302. SendClientMessage( playerid, COLOR_WHITE, "Congratulations on your purchase! You can now purchase an extra vehicle." );
  303. }
  304. }
  305. #undef veh
  306. return 1;
  307. }
  308. //** Upgrading vehicle slot's: custom license plate: MSGBOX. **
  309. if( dialogid == DIALOG_CUSTOM_PLATE ) {
  310. if( !response ) return 1;
  311. if( vupgrading[playerid] < 0 ) {
  312. return 1;
  313. }
  314. #define veh(%1) PlayerVehicles[playerid][vupgrading[playerid]][pv%1]
  315. if( PlayerInfo[playerid][pCash] < 100000 ) {
  316. return SendClientMessage( playerid, COLOR_GREY, "You cannot afford this." );
  317. }
  318. //Acceptable characters: A-Z, a-z, numbers 0-9 max length 8
  319. if( !regex_match( inputtext, "^[A-Za-z0-9]{0,}" ) || strlen( inputtext ) > 8 ) {
  320. return SendClientMessage( playerid, COLOR_GREY, "You can only enter characters A-Z and/or numbers 0-9, and a maximum of 8 characters.");
  321. }
  322. GiveMoney( playerid, -100000 );
  323. format( veh(plate), 9, "%s", inputtext );
  324. SetVehicleNumberPlate( veh(ID), veh(plate) );
  325. #undef veh
  326. vupgrading[playerid] = -1;
  327. SendClientMessage( playerid, COLOR_LIGHTBLUE, "Congratulations on your purchase! Your vehicle now has a custom number plate." );
  328. return 1;
  329. }
  330. return 1;
  331. }
  332. #if defined _ALS_OnDialogResponse
  333. #undef OnDialogResponse
  334. #else
  335. #define _ALS_OnDialogResponse
  336. #endif
  337. #define OnDialogResponse hook_OnDialogResponse
  338. #if defined hook_OnDialogResponse
  339. forward hook_OnDialogResponse( playerid );
  340. #endif
  341. public OnVehiclePaintjob( playerid, vehicleid, paintjobid ) {
  342. new pvid = GetPlayerSpawnedVehicleSlot( playerid );
  343. if( pvid > -1 ) {
  344. if( PlayerVehicles[playerid][pvid][pvID] == vehicleid ) {
  345. PlayerVehicles[playerid][pvid][pvpaintjob] = paintjobid;
  346. }
  347. }
  348. return 1;
  349. }
  350. public OnVehicleRespray(playerid, vehicleid, color1, color2)
  351. {
  352. new slot = GetPlayerSpawnedVehicleSlot( playerid );
  353. if( slot < 0 ) return 1;
  354. if( PlayerVehicles[playerid][slot][pvID] == vehicleid ) {
  355. PlayerVehicles[playerid][slot][pvcolor1] = color1;
  356. PlayerVehicles[playerid][slot][pvcolor2] = color2;
  357. }
  358. return 1;
  359. }