FoxForeach.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /****************************************************************************************************
  2. * *
  3. * )( FoX Foreach )( *
  4. * *
  5. * Copyright © 2017 Abyss Morgan. All rights reserved. *
  6. * *
  7. * Download: https://github.com/AbyssMorgan/SA-MP/tree/master/include/SAM *
  8. * Publication: http://forum.sa-mp.com/showthread.php?t=611175 *
  9. * Website: http://8.ct8.pl *
  10. * *
  11. * Plugins: None *
  12. * Modules: None *
  13. * *
  14. * File Version: 1.2.1 *
  15. * SA:MP Version: 0.3.7 (REQUIRE) *
  16. * *
  17. * Functions: *
  18. * FoxForeach(variable,tag); //Tags: Player, Bot, Character *
  19. * *
  20. * Player Functions: *
  21. * CountFoxPlayer(); *
  22. * FoxForeachRandomPlayer(); *
  23. * bool:IsPlayerInFoxPool(playerid); *
  24. * *
  25. * Bot Functions: *
  26. * CountFoxBot(); *
  27. * FoxForeachRandomBot(); *
  28. * bool:IsBotInFoxPool(playerid); *
  29. * *
  30. * Character Functions: *
  31. * CountFoxCharacter(); *
  32. * FoxForeachRandomCharacter(); *
  33. * bool:IsCharacterInFoxPool(playerid); *
  34. * *
  35. * Extended functions: *
  36. * foX(variable,beginning,end); *
  37. * PoolForeach(variable); *
  38. * *
  39. * Auto functions: *
  40. * FoxForeachInit(); *
  41. * bool:FoxAddPlayer(playerid); *
  42. * bool:FoxRemovePlayer(playerid); *
  43. * bool:FoxAddBot(playerid); *
  44. * bool:FoxRemoveBot(playerid); *
  45. * bool:FoxAddCharacter(playerid); *
  46. * bool:FoxRemoveCharacter(playerid); *
  47. * *
  48. ****************************************************************************************************/
  49. #if defined _3D_Tryg
  50. #error [ADM] Please include FoxForeach before 3DTryg, for increase loop speed.
  51. #endif
  52. #if defined _FoX_Foreach
  53. #endinput
  54. #endif
  55. #define _FoX_Foreach
  56. #if (!defined GetPlayerPoolSize || !defined GetSVarInt)
  57. #error [ADM] This include requires SA:MP version 0.3.7 (github.com/AbyssMorgan/SA-MP/blob/master/samp/include)
  58. #endif
  59. #define FoxForeach(%0,%1) for(new %0 = Fox%1[0], i_%0 = 0; i_%0 <= Fox%1Upp; i_%0++, %0 = Fox%1[i_%0])
  60. #define CountFoxPlayer() (FoxPlayerUpp+1)
  61. #define CountFoxBot() (FoxBotUpp+1)
  62. #define CountFoxCharacter() (FoxCharacterUpp+1)
  63. #define foX(%1,%2,%3) for(new %1 = %2; %1 <= %3; %1++)
  64. #if !defined PoolForeach
  65. #define PoolForeach(%1) for(new %1 = 0, p_%1 = GetPlayerPoolSize(); %1 <= p_%1; %1++)
  66. #endif
  67. new FoxPlayer[MAX_PLAYERS],
  68. FoxBot[MAX_PLAYERS],
  69. FoxCharacter[MAX_PLAYERS],
  70. FoxPlayerUpp = -1,
  71. FoxBotUpp = -1,
  72. FoxCharacterUpp = -1;
  73. stock bool:IsPlayerInFoxPool(playerid){
  74. for(new i = 0; i <= FoxPlayerUpp; i++){
  75. if(FoxPlayer[i] == playerid){
  76. return true;
  77. }
  78. }
  79. return false;
  80. }
  81. stock bool:FoxAddPlayer(playerid){
  82. if(IsPlayerInFoxPool(playerid)) return false;
  83. FoxPlayerUpp++;
  84. FoxPlayer[FoxPlayerUpp] = playerid;
  85. return true;
  86. }
  87. stock FoxForeachRandomPlayer(){
  88. if(FoxPlayerUpp == -1) return 0;
  89. new playerid = FoxPlayer[random(FoxPlayerUpp+1)];
  90. if(IsPlayerConnected(playerid)) return playerid;
  91. return 0;
  92. }
  93. stock bool:FoxRemovePlayer(playerid){
  94. if(!IsPlayerInFoxPool(playerid)) return false;
  95. for(new i = 0; i <= FoxPlayerUpp; i++){
  96. if(FoxPlayer[i] == playerid){
  97. if(FoxPlayerUpp == 0){
  98. FoxPlayer[i] = 0;
  99. } else if(i == FoxPlayerUpp){
  100. FoxPlayer[i] = 0;
  101. } else {
  102. FoxPlayer[i] = FoxPlayer[FoxPlayerUpp];
  103. FoxPlayer[FoxPlayerUpp] = 0;
  104. }
  105. FoxPlayerUpp--;
  106. break;
  107. }
  108. }
  109. return true;
  110. }
  111. stock bool:IsBotInFoxPool(playerid){
  112. for(new i = 0; i <= FoxBotUpp; i++){
  113. if(FoxBot[i] == playerid){
  114. return true;
  115. }
  116. }
  117. return false;
  118. }
  119. stock bool:FoxAddBot(playerid){
  120. if(IsBotInFoxPool(playerid)) return false;
  121. FoxBotUpp++;
  122. FoxBot[FoxBotUpp] = playerid;
  123. return true;
  124. }
  125. stock FoxForeachRandomBot(){
  126. if(FoxBotUpp == -1) return 0;
  127. new playerid = FoxBot[random(FoxBotUpp+1)];
  128. if(IsBotConnected(playerid)) return playerid;
  129. return 0;
  130. }
  131. stock bool:FoxRemoveBot(playerid){
  132. if(!IsBotInFoxPool(playerid)) return false;
  133. for(new i = 0; i <= FoxBotUpp; i++){
  134. if(FoxBot[i] == playerid){
  135. if(FoxBotUpp == 0){
  136. FoxBot[i] = 0;
  137. } else if(i == FoxBotUpp){
  138. FoxBot[i] = 0;
  139. } else {
  140. FoxBot[i] = FoxBot[FoxBotUpp];
  141. FoxBot[FoxBotUpp] = 0;
  142. }
  143. FoxBotUpp--;
  144. break;
  145. }
  146. }
  147. return true;
  148. }
  149. stock bool:IsCharacterInFoxPool(playerid){
  150. for(new i = 0; i <= FoxCharacterUpp; i++){
  151. if(FoxCharacter[i] == playerid){
  152. return true;
  153. }
  154. }
  155. return false;
  156. }
  157. stock bool:FoxAddCharacter(playerid){
  158. if(IsCharacterInFoxPool(playerid)) return false;
  159. FoxCharacterUpp++;
  160. FoxCharacter[FoxCharacterUpp] = playerid;
  161. return true;
  162. }
  163. stock FoxForeachRandomCharacter(){
  164. if(FoxCharacterUpp == -1) return 0;
  165. new playerid = FoxCharacter[random(FoxCharacterUpp+1)];
  166. if(IsCharacterConnected(playerid)) return playerid;
  167. return 0;
  168. }
  169. stock bool:FoxRemoveCharacter(playerid){
  170. if(!IsCharacterInFoxPool(playerid)) return false;
  171. for(new i = 0; i <= FoxCharacterUpp; i++){
  172. if(FoxCharacter[i] == playerid){
  173. if(FoxCharacterUpp == 0){
  174. FoxCharacter[i] = 0;
  175. } else if(i == FoxCharacterUpp){
  176. FoxCharacter[i] = 0;
  177. } else {
  178. FoxCharacter[i] = FoxCharacter[FoxCharacterUpp];
  179. FoxCharacter[FoxCharacterUpp] = 0;
  180. }
  181. FoxCharacterUpp--;
  182. break;
  183. }
  184. }
  185. return true;
  186. }
  187. stock FoxForeachInit(){
  188. for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++){
  189. if(IsPlayerConnected(i)){
  190. if(IsPlayerNPC(i)){
  191. FoxAddBot(i);
  192. } else {
  193. FoxAddPlayer(i);
  194. }
  195. FoxAddCharacter(i);
  196. }
  197. }
  198. return 1;
  199. }
  200. new bool:CRC_FoxForeachInit = true;
  201. //Hook: OnFilterScriptInit
  202. public OnFilterScriptInit(){
  203. if(CRC_FoxForeachInit){
  204. CRC_FoxForeachInit = false;
  205. FoxForeachInit();
  206. }
  207. #if defined FoX_OnFilterScriptInit
  208. FoX_OnFilterScriptInit();
  209. #endif
  210. return 1;
  211. }
  212. #if defined _ALS_OnFilterScriptInit
  213. #undef OnFilterScriptInit
  214. #else
  215. #define _ALS_OnFilterScriptInit
  216. #endif
  217. #define OnFilterScriptInit FoX_OnFilterScriptInit
  218. #if defined FoX_OnFilterScriptInit
  219. forward FoX_OnFilterScriptInit();
  220. #endif
  221. //Hook: OnGameModeInit
  222. public OnGameModeInit(){
  223. if(CRC_FoxForeachInit){
  224. CRC_FoxForeachInit = false;
  225. FoxForeachInit();
  226. }
  227. #if defined FoX_OnGameModeInit
  228. FoX_OnGameModeInit();
  229. #endif
  230. return 1;
  231. }
  232. #if defined _ALS_OnGameModeInit
  233. #undef OnGameModeInit
  234. #else
  235. #define _ALS_OnGameModeInit
  236. #endif
  237. #define OnGameModeInit FoX_OnGameModeInit
  238. #if defined FoX_OnGameModeInit
  239. forward FoX_OnGameModeInit();
  240. #endif
  241. //Hook: OnPlayerDisconnect
  242. public OnPlayerDisconnect(playerid,reason){
  243. if(IsPlayerNPC(playerid)){
  244. FoxRemoveBot(playerid);
  245. } else {
  246. FoxRemovePlayer(playerid);
  247. }
  248. FoxRemoveCharacter(playerid);
  249. #if defined FoX_OnPlayerDisconnect
  250. FoX_OnPlayerDisconnect(playerid,reason);
  251. #endif
  252. return 1;
  253. }
  254. #if defined _ALS_OnPlayerDisconnect
  255. #undef OnPlayerDisconnect
  256. #else
  257. #define _ALS_OnPlayerDisconnect
  258. #endif
  259. #define OnPlayerDisconnect FoX_OnPlayerDisconnect
  260. #if defined FoX_OnPlayerDisconnect
  261. forward FoX_OnPlayerDisconnect(playerid,reason);
  262. #endif
  263. //Hook: OnPlayerConnect
  264. public OnPlayerConnect(playerid){
  265. if(IsPlayerNPC(playerid)){
  266. FoxAddBot(playerid);
  267. } else {
  268. FoxAddPlayer(playerid);
  269. }
  270. FoxAddCharacter(playerid);
  271. #if defined FoX_OnPlayerConnect
  272. FoX_OnPlayerConnect(playerid);
  273. #endif
  274. return 1;
  275. }
  276. #if defined _ALS_OnPlayerConnect
  277. #undef OnPlayerConnect
  278. #else
  279. #define _ALS_OnPlayerConnect
  280. #endif
  281. #define OnPlayerConnect FoX_OnPlayerConnect
  282. #if defined FoX_OnPlayerConnect
  283. forward FoX_OnPlayerConnect(playerid);
  284. #endif
  285. //EOF