actor_robbery.inc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. [DECLARATIONS]
  3. */
  4. #if !defined _samp_included
  5. #error Please include a_samp before actor_robbery.
  6. #endif
  7. #if !defined KEY_AIM
  8. #define KEY_AIM 128
  9. #endif
  10. #if !defined PreloadAnimLib
  11. #define PreloadAnimLib(%1,%2) ApplyAnimation(%1,%2,"null",0.0,0,0,0,0,0)
  12. #endif
  13. #define MAX_ROBBERY_ACTORS (50)
  14. #define TYPE_SUCCESS (0)
  15. #define TYPE_FAILED (1)
  16. #define TYPE_UNFINISHED (2)
  17. #define MIN_MONEY_ROB 500
  18. #define MAX_MONEY_ROB 10000
  19. #define ROBBERY_WAIT_TIME (5)
  20. enum E_ACTOR_ROBBERY_DATA
  21. {
  22. actor_skin,
  23. Float:actor_x,
  24. Float:actor_y,
  25. Float:actor_z,
  26. Float:actor_ang,
  27. actor_vw,
  28. money_min,
  29. money_max,
  30. bool:actor_created,
  31. actor_robbedRecently
  32. }
  33. static
  34. robbery_data[MAX_ROBBERY_ACTORS][E_ACTOR_ROBBERY_DATA],
  35. i_actor = 0;
  36. forward RunActorAnimationSequence(playerid, actorid, animation_pattern);
  37. forward OnPlayerStartRobbery(playerid, actorid, bool:robbed_recently);
  38. forward OnPlayerFinishRobbery(playerid, actorid, robbedmoney, type);
  39. forward OnPlayerRequestRobbery(playerid, actorid);
  40. /*
  41. Function:
  42. CreateActorRobbery
  43. Info:
  44. Creates the robbery actor according to the position set.
  45. Param:
  46. * skinid -> Skin ID of the robbery actor
  47. * Float:x -> Coordinate X of the robbery actor
  48. * Float:y -> Coordinate X of the robbery actor
  49. * Float:z -> Coordinate X of the robbery actor
  50. * Float:ang -> Facing angle of the robbery actor
  51. * actor_vwid -> virtualid of the robbery actor
  52. * r_moneymin -> Minimum money to be robbed from the robbery actor
  53. * r_moneymax -> Maximum money to be robbed from the robbery actor
  54. */
  55. #pragma deprecated GetActorRobberyData
  56. stock GetActorRobberyData(actorid, &skinid, &Float:x, &Float:y, &Float:z, &Float:ang, &actor_vwid, &r_moneymin, &r_moneymax) {
  57. #pragma unused actorid, skinid, x, y, z, ang, actor_vwid, r_moneymin, r_moneymax
  58. }
  59. #pragma deprecated Please use new function name Robbery_CreateActor.
  60. stock CreateActorRobbery(skinid, Float:x, Float:y, Float:z, Float:ang, actor_vwid = 0, r_moneymin = MIN_MONEY_ROB, r_moneymax = MAX_MONEY_ROB) {
  61. #pragma unused skinid, x, y, z, ang, actor_vwid, r_moneymin, r_moneymax
  62. return -1;
  63. }
  64. stock Robbery_CreateActor(skinid, Float:x, Float:y, Float:z, Float:ang, actor_vwid = 0, r_moneymin = MIN_MONEY_ROB, r_moneymax = MAX_MONEY_ROB) {
  65. new actorid = GetActorFreeID();
  66. if(actorid == -1) {
  67. print("ERROR: Robbery_CreateActor - MAX_ROBBERY_ACTOR reached, increase the limit size.");
  68. return -1;
  69. }
  70. CreateActor(skinid, x, y, z, ang);
  71. SetActorVirtualWorld(actorid, actor_vwid);
  72. robbery_data[actorid][actor_created] = true;
  73. robbery_data[actorid][actor_skin] = skinid;
  74. robbery_data[actorid][actor_x] = x;
  75. robbery_data[actorid][actor_y] = y;
  76. robbery_data[actorid][actor_z] = z;
  77. robbery_data[actorid][actor_ang] = ang;
  78. robbery_data[actorid][actor_vw] = actor_vwid;
  79. robbery_data[actorid][money_min] = r_moneymin;
  80. robbery_data[actorid][money_max] = r_moneymax;
  81. return (i_actor ++);
  82. }
  83. /*
  84. Function:
  85. Robbery_GetActorData
  86. Info:
  87. Retrieves the actor data
  88. Param:
  89. * actorid -> ID of robbery actor you want to retrieve data from.
  90. * &skinid -> Skin ID of the robbery actor
  91. * &Float:x -> Coordinate X of the robbery actor
  92. * &Float:y -> Coordinate X of the robbery actor
  93. * &Float:z -> Coordinate X of the robbery actor
  94. * &Float:ang -> Facing angle of the robbery actor
  95. * &actor_vwid -> virtualid of the robbery actor
  96. * &r_moneymin -> Minimum money to be robbed from the robbery actor
  97. * &r_moneymax -> Maximum money to be robbed from the robbery actor
  98. */
  99. stock Robbery_GetActorData(actorid, &skinid, &Float:x, &Float:y, &Float:z, &Float:ang, &actor_vwid, &r_moneymin, &r_moneymax) {
  100. if(actorid == INVALID_ACTOR_ID) {
  101. print("ERROR: GetActorRobberyData - Invalid actorid input.");
  102. return 1;
  103. }
  104. skinid = robbery_data[actorid][actor_skin];
  105. x = robbery_data[actorid][actor_x];
  106. y = robbery_data[actorid][actor_y];
  107. z = robbery_data[actorid][actor_z] ;
  108. ang = robbery_data[actorid][actor_ang] ;
  109. actor_vwid = robbery_data[actorid][actor_vw];
  110. r_moneymin = robbery_data[actorid][money_min];
  111. r_moneymax = robbery_data[actorid][money_max];
  112. return 1;
  113. }
  114. /*
  115. Function:
  116. GetActorFreeID
  117. Info:
  118. Retrieves the unused ID of an actor.
  119. Param:
  120. None
  121. */
  122. static stock GetActorFreeID()
  123. {
  124. for(new i = 0; i < MAX_ROBBERY_ACTORS; i++) {
  125. if(!robbery_data[i][actor_created]) {
  126. return i;
  127. }
  128. }
  129. return -1;
  130. }
  131. /*
  132. Robbery IMPL
  133. */
  134. public RunActorAnimationSequence(playerid, actorid, animation_pattern) {
  135. switch(animation_pattern) {
  136. case 0: {
  137. ClearActorAnimations(actorid);
  138. ApplyActorAnimation(actorid, "SHOP", "SHP_Rob_HandsUp", 4.1, 0, 1, 1, 1, 0);
  139. SetTimerEx("RunActorAnimationSequence", 1000 * ROBBERY_WAIT_TIME, false, "iii", playerid, actorid, 1);
  140. new Float:x, Float:y, Float:z;
  141. GetPlayerPos(playerid, x, y, z);
  142. for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++) {
  143. if(!IsPlayerConnected(i)) {
  144. continue;
  145. }
  146. PlayerPlaySound(i, 3401, x, y, z);
  147. }
  148. }
  149. case 1: {
  150. if(!IsPlayerInRangeOfPoint(playerid, 10.0, robbery_data[actorid][actor_x], robbery_data[actorid][actor_y], robbery_data[actorid][actor_z])) {
  151. CallLocalFunction("OnPlayerFinishRobbery", "iiii", playerid, actorid, 0, TYPE_UNFINISHED);
  152. }
  153. else {
  154. ClearActorAnimations(actorid);
  155. ApplyActorAnimation(actorid, "SHOP", "SHP_Rob_GiveCash", 4.1, 0, 1, 1, 1, 0);
  156. SetTimerEx("RunActorAnimationSequence", 1000 * ROBBERY_WAIT_TIME, false, "iii", playerid, actorid, 2);
  157. }
  158. }
  159. case 2: {
  160. ClearActorAnimations(actorid);
  161. ApplyActorAnimation(actorid, "PED", "DUCK_cower", 4.1, 1, 1, 1, 1, 1);
  162. SetTimerEx("RunActorAnimationSequence", 1000 * 60 * ROBBERY_WAIT_TIME, false, "iii", playerid, actorid, 3);
  163. new robberyChance = random(100);
  164. if(robberyChance > 40) {
  165. CallLocalFunction("OnPlayerFinishRobbery", "iiii", playerid, actorid, (random(robbery_data[actorid][money_max] - robbery_data[actorid][money_min]) + robbery_data[actorid][money_min]), TYPE_SUCCESS);
  166. }
  167. else {
  168. CallLocalFunction("OnPlayerFinishRobbery", "iiii", playerid, actorid, 0, TYPE_FAILED);
  169. }
  170. }
  171. case 3: {
  172. ClearActorAnimations(actorid);
  173. PlayerPlaySound(playerid, 0, 0.0, 0.0, 0.0);
  174. }
  175. }
  176. return 1;
  177. }
  178. public OnPlayerConnect(playerid)
  179. {
  180. EnablePlayerCameraTarget(playerid, 1);
  181. PreloadAnimLib(playerid, "PED");
  182. PreloadAnimLib(playerid, "SHOP");
  183. #if defined actorrob_OnPlayerConnect
  184. return actorrob_OnPlayerConnect(playerid);
  185. #else
  186. return 1;
  187. #endif
  188. }
  189. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  190. {
  191. if((newkeys & KEY_AIM) == KEY_AIM && GetPlayerState(playerid) == PLAYER_STATE_ONFOOT)
  192. {
  193. switch(GetPlayerWeapon(playerid))
  194. {
  195. case 22 .. 33:
  196. {
  197. new actorid = GetPlayerCameraTargetActor(playerid);
  198. if(actorid == INVALID_ACTOR_ID) {
  199. return 1;
  200. }
  201. if(!robbery_data[actorid][actor_created]) {
  202. // this fixes the issue with normal create actors.
  203. // there was a bug that you aim at an actor and rob it even though it wasn't created by actor_robbery.
  204. return 1;
  205. }
  206. if(!OnPlayerRequestRobbery(playerid, actorid))
  207. {
  208. return 1;
  209. }
  210. if(gettime() - robbery_data[actorid][actor_robbedRecently] < 60 * ROBBERY_WAIT_TIME) {
  211. return CallLocalFunction("OnPlayerStartRobbery", "iii", playerid, actorid, true);
  212. }
  213. robbery_data[actorid][actor_robbedRecently] = gettime();
  214. RunActorAnimationSequence(playerid, actorid, 0);
  215. CallLocalFunction("OnPlayerStartRobbery", "iii", playerid, actorid, false);
  216. }
  217. }
  218. }
  219. #if defined actorrob_OnPlayerKeyStateChange
  220. return actorrob_OnPlayerKeyStateChange(playerid, newkeys, oldkeys);
  221. #else
  222. return 1;
  223. #endif
  224. }
  225. /* OnPlayerRequestRobbery
  226. Usage: return 0 to disallow the robbery.
  227. See test.pwn for example.
  228. */
  229. public OnPlayerRequestRobbery(playerid, actorid)
  230. {
  231. #if defined actorrob_OnPlayerRequestRobbery
  232. return actorrob_OnPlayerRequestRobbery(playerid, actorid);
  233. #else
  234. return 1; // Default version always wants to returns >1 to allow robbery
  235. #endif
  236. }
  237. //OnPlayerKeyStateChange hook directives
  238. #if defined _ALS_OnPlayerKeyStateChange
  239. #undef OnPlayerKeyStateChange
  240. #else
  241. #define _ALS_OnPlayerKeyState
  242. #endif
  243. #define OnPlayerKeyStateChange actorrob_OnPlayerKeyStateChange
  244. #if defined actorrob_OnPlayerKeyStateChange
  245. forward actorrob_OnPlayerKeyStateChange(playerid, newkeys, oldkeys);
  246. #endif
  247. //OnPlayerConnect directives
  248. #if defined _ALS_OnPlayerConnect
  249. #undef OnPlayerConnect
  250. #else
  251. #define _ALS_OnPlayerConnect
  252. #endif
  253. #define OnPlayerConnect actorrob_OnPlayerConnect
  254. #if defined actorrob_OnPlayerConnect
  255. forward actorrob_OnPlayerConnect(playerid);
  256. #endif
  257. //OnPlayerRequestRobbery directives
  258. #if defined _ALS_OnPlayerRequestRobbery
  259. #undef OnPlayerRequestRobbery
  260. #else
  261. #define _ALS_OnPlayerRequestRobbery
  262. #endif
  263. #define OnPlayerRequestRobbery actorrob_OnPlayerRequestRobbery
  264. #if defined actorrob_OnPlayerRequestRobbery
  265. forward actorrob_OnPlayerRequestRobbery(playerid);
  266. #endif