Guest User

Untitled

a guest
Aug 27th, 2021
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.80 KB | None | 0 0
  1. // Checkpoint Manager
  2. // (c) Jo?o Pedro Lopes, All right's reserved
  3. //
  4. // Feel free to change any line above, as long as you post the 'fix' / 'patch' at the forum
  5. // and send a copy to [email protected]
  6.  
  7. /* natives
  8. native CreateCheckpoint(ownerid, chpid, Float:posX, Float:posY, Float:posZ, Float:size); // Creates a checkpoint
  9. native SetCheckpointInterior(chpid, interiorid); // Changes the checkpoint interior
  10. native SetCheckpointVirtualWorld(chpid, VirtualWorldID); // Changes the Checkpoint vWorld
  11. native ToggleCheckpointActive(chpid, bool:active); // Deactivates / Activates the checkpoint
  12. native ChangeCheckpointOwner(chpid, owner); // Change the checkpoint owner
  13. native RemoveCheckpoint(chpid); // Removes the specified checkpoint
  14. native StartCheckpointSeeking(); // Starts seeking for each individual
  15. native StopCheckpointSeeking(); // Stops the system
  16. native VerifyCheckpoint(playerid); // Place this at OnPlayerEnterCheckpoint
  17. */
  18.  
  19. // Function Forwards
  20. forward public OnCheckpointEnter(playerid, checkpointid);
  21.  
  22. #if defined _CHECKPOINT_MANAGER_INCLUDED
  23. #endinput
  24. #endif
  25.  
  26. #define _CHECKPOINT_MANAGER_INCLUDED
  27. #pragma library CheckpointManager
  28.  
  29. #include <a_samp>
  30.  
  31. #define MAX_CHECKPOINTS 300
  32. #define CHECKPOINT_SEEKER_DELAY 300
  33.  
  34. #define GLOBAL_OWNER_ID -1
  35.  
  36.  
  37. // CHECKPOINT ENUMERATION
  38. enum _checkpointEnum{
  39. _chp_populated, // Is this slot of the memory populated?
  40.  
  41. _chp_id, // The ID of the checkpoint
  42. _chp_owner, // The ID of the player who this checkpoint is visible too
  43.  
  44. Float:_chp_posX, // The X position of this checkpoint
  45. Float:_chp_posY, // The Y position of this checkpoint
  46. Float:_chp_posZ, // The Z position of this checkpoint
  47. Float:_chp_size, // The checkpoint size
  48. Float:_chp_viewDistance, // The checkpoint view distance
  49.  
  50. bool:_chp_active, // Is this checkpoint active?
  51.  
  52. _chp_interior_id, // The interior id of this checkpoint
  53. _chp_world_id // The world id of this checkpoint
  54. };
  55.  
  56. // DATA ARRAYS
  57. new _checkpoints[MAX_CHECKPOINTS][_checkpointEnum];
  58. new _p_VisibleCheckpoint[MAX_PLAYERS];
  59. new _chp_manager_timer_id;
  60.  
  61. // DATA VARIABLES
  62. new _totalCheckpoints;
  63.  
  64. // --------------------------------------------------------------------------------------------------------
  65. // Creates a new checkpoint with some initial data
  66. stock CreateCheckpoint(__ownerid, __chpid, Float:__posX, Float:__posY, Float:__posZ, Float:__size){
  67. // Max checkpoint reached?
  68. if(_totalCheckpoints == MAX_CHECKPOINTS) return 0;
  69.  
  70. // First checkpoint? Setting everything to unpopulated
  71. if(!_totalCheckpoints){
  72. for(new i; i < MAX_PLAYERS; i++) _p_VisibleCheckpoint[i] = -1;
  73. for(new i; i < MAX_CHECKPOINTS; i++){
  74. _checkpoints[i][_chp_populated] = false;
  75. }
  76.  
  77. // Sending the Initialization Info
  78. printf("[Checkpoint Manager : Version 0.1.1b] System Initialized...", __chpid);
  79. }
  80.  
  81. // Getting the first open slot
  82. new _slot;
  83. for(new i = 0; i < MAX_CHECKPOINTS; i++){
  84. if(!_checkpoints[i][_chp_populated]){
  85. _slot = i;
  86. break;
  87. }
  88. }
  89.  
  90. // Adding the new checkpoint
  91. _checkpoints[_slot][_chp_populated] = true;
  92. _checkpoints[_slot][_chp_id] = __chpid;
  93. _checkpoints[_slot][_chp_owner] = __ownerid;
  94. _checkpoints[_slot][_chp_posX] = __posX;
  95. _checkpoints[_slot][_chp_posY] = __posY;
  96. _checkpoints[_slot][_chp_posZ] = __posZ;
  97. _checkpoints[_slot][_chp_size] = __size;
  98. _checkpoints[_slot][_chp_viewDistance] = 4.0;
  99. _checkpoints[_slot][_chp_active] = true;
  100. _checkpoints[_slot][_chp_interior_id] = 0;
  101. _checkpoints[_slot][_chp_world_id] = 0;
  102.  
  103. printf("[Checkpoint Manager] Checkpoint created (%d) at slot %d", __chpid, _slot);
  104. printf("Checkpoint Position: { %f, %f, %f }", _checkpoints[_slot][_chp_posX], _checkpoints[_slot][_chp_posY], _checkpoints[_slot][_chp_posZ]);
  105.  
  106. _totalCheckpoints++;
  107. return 1;
  108. }
  109.  
  110. //---------------------------------------------------------------------------------------------
  111. stock SetCheckpointInterior(__chpid, __interiorid){
  112. new _slot = __ChpSlotByID(__chpid);
  113. if(_slot > -1){
  114. // Valid slot?
  115. _checkpoints[_slot][_chp_interior_id] = __interiorid;
  116. return 1;
  117. }
  118. return 0;
  119. }
  120.  
  121. //---------------------------------------------------------------------------------------------
  122. stock SetCheckpointVirtualWorld(__chpid, __virtual_world_id){
  123. new _slot = __ChpSlotByID(__chpid);
  124. if(_slot > -1){
  125. _checkpoints[_slot][_chp_world_id] = __virtual_world_id;
  126. return 1;
  127. }
  128. return 0;
  129. }
  130.  
  131. stock ToggleCheckpointActive(__chpid, bool:__active){
  132. new _slot = __ChpSlotByID(__chpid);
  133. if(_slot > -1){
  134. _checkpoints[_slot][_chp_active] = __active;
  135. return 1;
  136. }
  137. return 0;
  138. }
  139.  
  140. stock ChangeCheckpointOwner(__chpid, __owner){
  141. new _slot = __ChpSlotByID(__chpid);
  142. if(_slot > -1){
  143. _checkpoints[_slot][_chp_owner] = __owner;
  144. return 1;
  145. }
  146. return 0;
  147. }
  148.  
  149. stock RemoveCheckpoint(__chpid){
  150. new _slot = __ChpSlotByID(__chpid);
  151. if(_slot > -1){
  152. // Deleting the checkpoint
  153. _checkpoints[_slot][_chp_populated] = false;
  154. _checkpoints[_slot][_chp_id] = -1;
  155. _checkpoints[_slot][_chp_owner] = 255;
  156. _checkpoints[_slot][_chp_posX] = -1;
  157. _checkpoints[_slot][_chp_posY] = -1;
  158. _checkpoints[_slot][_chp_posZ] = -1;
  159. _checkpoints[_slot][_chp_size] = -1;
  160. _checkpoints[_slot][_chp_viewDistance] = -1;
  161. _checkpoints[_slot][_chp_active] = false;
  162. _checkpoints[_slot][_chp_interior_id] = -1;
  163. _checkpoints[_slot][_chp_world_id] = -1;
  164. _totalCheckpoints--;
  165. printf("\n[Checkpoint Manager] Checkpoint removed (ID: %d)", __chpid);
  166. return 1;
  167. }
  168. return 0;
  169. }
  170.  
  171. //---------------------------------------------------------------------------------------------
  172. // Gets the checkpoint slot by id
  173. stock __ChpSlotByID(__chpid){
  174. for(new i; i < MAX_CHECKPOINTS; i++){
  175. if(_checkpoints[i][_chp_id] == __chpid) return i;
  176. }
  177. return -1;
  178. }
  179.  
  180.  
  181. forward CheckpointSeeker();
  182. stock StartCheckpointSeeking(){
  183. _chp_manager_timer_id = SetTimer("CheckpointSeeker", CHECKPOINT_SEEKER_DELAY, 1);
  184. return 1;
  185. }
  186.  
  187. stock StopCheckpointSeeking(){
  188. KillTimer(_chp_manager_timer_id);
  189. return 1;
  190. }
  191.  
  192. public CheckpointSeeker(){
  193. new Float:__posX, Float:__posY, Float:__posZ;
  194. new __interior;
  195. new __virtualWorld;
  196. for(new i; i < MAX_PLAYERS; i++)
  197. {
  198. if(!IsPlayerConnected(i)) continue;
  199.  
  200. GetPlayerPos(i, Float:__posX, Float:__posY, Float:__posZ);
  201. // Is the player near a checkpoint?
  202. if(_p_VisibleCheckpoint[i] > -1)
  203. {
  204. // If the player is no longer near that point
  205. if(__posX < (_checkpoints[_p_VisibleCheckpoint[i]][_chp_posX] - _checkpoints[_p_VisibleCheckpoint[i]][_chp_viewDistance])
  206. || __posX > (_checkpoints[_p_VisibleCheckpoint[i]][_chp_posX] + _checkpoints[_p_VisibleCheckpoint[i]][_chp_viewDistance])
  207. || __posY < (_checkpoints[_p_VisibleCheckpoint[i]][_chp_posY] - _checkpoints[_p_VisibleCheckpoint[i]][_chp_viewDistance])
  208. || __posY > (_checkpoints[_p_VisibleCheckpoint[i]][_chp_posY] + _checkpoints[_p_VisibleCheckpoint[i]][_chp_viewDistance])
  209. || __posZ < (_checkpoints[_p_VisibleCheckpoint[i]][_chp_posZ] - _checkpoints[_p_VisibleCheckpoint[i]][_chp_viewDistance])
  210. || __posZ > (_checkpoints[_p_VisibleCheckpoint[i]][_chp_posZ] + _checkpoints[_p_VisibleCheckpoint[i]][_chp_viewDistance]))
  211. {
  212. DisablePlayerCheckpoint(i);
  213. _p_VisibleCheckpoint[i] = -1;
  214. }
  215. }
  216. else
  217. {
  218. // Getting the player Interior and virtual world
  219. __interior = GetPlayerInterior(i);
  220. __virtualWorld = GetPlayerVirtualWorld(i);
  221.  
  222. // Looking for a new checkpoint
  223. for(new j = 0; j < MAX_CHECKPOINTS; j++){
  224. if(!_checkpoints[j][_chp_populated]) continue;
  225. if((_checkpoints[j][_chp_owner] != i) && (_checkpoints[j][_chp_owner] != -1)) continue;
  226. if(_checkpoints[j][_chp_interior_id] != __interior) continue;
  227. if(_checkpoints[j][_chp_world_id] != __virtualWorld) continue;
  228.  
  229. if(__posX > (_checkpoints[j][_chp_posX] - _checkpoints[j][_chp_viewDistance])
  230. && __posX < (_checkpoints[j][_chp_posX] + _checkpoints[j][_chp_viewDistance])
  231. && __posY > (_checkpoints[j][_chp_posY] - _checkpoints[j][_chp_viewDistance])
  232. && __posY < (_checkpoints[j][_chp_posY] + _checkpoints[j][_chp_viewDistance])
  233. && __posZ > (_checkpoints[j][_chp_posZ] - _checkpoints[j][_chp_viewDistance])
  234. && __posZ < (_checkpoints[j][_chp_posZ] + _checkpoints[j][_chp_viewDistance])){
  235. SetPlayerCheckpoint(i, _checkpoints[j][_chp_posX], _checkpoints[j][_chp_posY], _checkpoints[j][_chp_posZ], _checkpoints[j][_chp_size]);
  236. _p_VisibleCheckpoint[i] = j;
  237. break;
  238. }
  239. }
  240. }
  241. }
  242. return 1;
  243. }
  244.  
  245. stock VerifyCheckpoint(__playerid){
  246. if(_p_VisibleCheckpoint[__playerid] >= 0){
  247. OnCheckpointEnter(__playerid, _checkpoints[_p_VisibleCheckpoint[__playerid]][_chp_id]);
  248. return 1;
  249. }
  250. return 0;
  251. }
  252. stock VerifyCheckpointe(__playerid){
  253. if(_p_VisibleCheckpoint[__playerid] >= 0){
  254. OnCheckpointEXIT(__playerid, _checkpoints[_p_VisibleCheckpoint[__playerid]][_chp_id]);
  255. return 1;
  256. }
  257. return 0;
  258. }
  259.  
Advertisement
Add Comment
Please, Sign In to add comment