y_sparsearray_entry.inc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #if defined _INC_y_sparsearray
  2. #endinput
  3. #endif
  4. #define _INC_y_sparsearray
  5. /*
  6. Legal:
  7. Version: MPL 1.1
  8. The contents of this file are subject to the Mozilla Public License Version
  9. 1.1 the "License"; you may not use this file except in compliance with
  10. the License. You may obtain a copy of the License at
  11. http://www.mozilla.org/MPL/
  12. Software distributed under the License is distributed on an "AS IS" basis,
  13. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. for the specific language governing rights and limitations under the
  15. License.
  16. The Original Code is the YSI framework.
  17. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  18. Portions created by the Initial Developer are Copyright C 2011
  19. the Initial Developer. All Rights Reserved.
  20. Contributors:
  21. Y_Less
  22. koolk
  23. JoeBullet/Google63
  24. g_aSlice/Slice
  25. Misiur
  26. samphunter
  27. tianmeta
  28. maddinat0r
  29. spacemud
  30. Crayder
  31. Dayvison
  32. Ahmad45123
  33. Zeex
  34. irinel1996
  35. Yiin-
  36. Chaprnks
  37. Konstantinos
  38. Masterchen09
  39. Southclaws
  40. PatchwerkQWER
  41. m0k1
  42. paulommu
  43. udan111
  44. Thanks:
  45. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  46. ZeeX - Very productive conversations.
  47. koolk - IsPlayerinAreaEx code.
  48. TheAlpha - Danish translation.
  49. breadfish - German translation.
  50. Fireburn - Dutch translation.
  51. yom - French translation.
  52. 50p - Polish translation.
  53. Zamaroht - Spanish translation.
  54. Los - Portuguese translation.
  55. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes for
  56. me to strive to better.
  57. Pixels^ - Running XScripters where the idea was born.
  58. Matite - Pestering me to release it and using it.
  59. Very special thanks to:
  60. Thiadmer - PAWN, whose limits continue to amaze me!
  61. Kye/Kalcor - SA:MP.
  62. SA:MP Team past, present and future - SA:MP.
  63. Optional plugins:
  64. Gamer_Z - GPS.
  65. Incognito - Streamer.
  66. Me - sscanf2, fixes2, Whirlpool.
  67. */
  68. #include "..\..\YSI_Core\y_utils"
  69. #define SparseArray:%0<> %0[]=#%0
  70. #if defined GLOBAL_VARTYPE_INT
  71. #define SparseArray_MakeName(%0,%1,%2) format((%0), sizeof (%0), "SparseArray_%d.%s", @_, (%1))
  72. #define SparseArray_Exists(%0,%1,%2) (GetGVarType(%0, %2) == GLOBAL_VARTYPE_INT)
  73. #define SparseArray_DoWrite(%0,%1,%2,%8) (SetGVarInt(%0, %8, %2))
  74. #define SparseArray_DoRead(%0,%1,%2) (GetGVarInt(%0, %2))
  75. #define SparseArray_DoDelete(%0,%1,%2) (DeleteGVar(%0, %2))
  76. #elseif defined SERVER_VARTYPE_INT
  77. #define SparseArray_MakeName(%0,%1,%2) format((%0), sizeof (%0), "SparseArray_%d.%s[%d]", @_, (%1), (%2))
  78. #define SparseArray_Exists(%0,%1,%2) (GetSVarType(%0) == SERVER_VARTYPE_INT)
  79. #define SparseArray_DoWrite(%0,%1,%2,%8) (SetSVarInt(%0, %8))
  80. #define SparseArray_DoRead(%0,%1,%2) (GetSVarInt(%0))
  81. #define SparseArray_DoDelete(%0,%1,%2) (DeleteSVar(%0))
  82. #else
  83. #define SPARSE_ARRAY_BASE_ID (0x795F7361)
  84. #define SparseArray_MakeName(%0,%1,%2) format((%0), sizeof (%0), "%s[%d]", (%1), (%2))
  85. #define SparseArray_Exists(%0,%1,%2) (existproperty(SPARSE_ARRAY_BASE_ID + @_, %0))
  86. #define SparseArray_DoWrite(%0,%1,%2,%8) (setproperty(SPARSE_ARRAY_BASE_ID + @_, %0, %8))
  87. #define SparseArray_DoRead(%0,%1,%2) (getproperty(SPARSE_ARRAY_BASE_ID + @_, %0))
  88. #define SparseArray_DoDelete(%0,%1,%2) (deleteproperty(SPARSE_ARRAY_BASE_ID + @_, %0))
  89. // TODO: Restore properties on mode restart. Really this is something that
  90. // should be in fixes.inc (but I'm not sure how to do it in a generic way
  91. // without the ability to a) query properties and b) allocate arbitrary
  92. // memory, so really I'm not sure how to go about it...) (a) can actually
  93. // be solved with more properties - make a property index system, (b) still
  94. // not...
  95. #endif
  96. /*-------------------------------------------------------------------------*//**
  97. * <param name="name">The sparse array.</param>
  98. * <param name="index">The index in the array.</param>
  99. * <param name="dest">Where to write the value.</param>
  100. * <returns>
  101. * Was the value found?
  102. * </returns>
  103. *//*------------------------------------------------------------------------**/
  104. stock Sparse_TryGet(const name[], index, &dest)
  105. {
  106. new id[64];
  107. SparseArray_MakeName(id, name, index);
  108. if (SparseArray_Exists(id, name, index))
  109. {
  110. dest = SparseArray_DoRead(id, name, index);
  111. return true;
  112. }
  113. return false;
  114. }
  115. /*-------------------------------------------------------------------------*//**
  116. * <param name="name">The sparse array.</param>
  117. * <param name="index">The index in the array.</param>
  118. * <returns>
  119. * The value at this index (or <c>cellmin</c>).
  120. * </returns>
  121. *//*------------------------------------------------------------------------**/
  122. stock Sparse_Get(const name[], index)
  123. {
  124. new id[64];
  125. SparseArray_MakeName(id, name, index);
  126. if (SparseArray_Exists(id, name, index))
  127. return SparseArray_DoRead(id, name, index);
  128. return cellmin;
  129. }
  130. /*-------------------------------------------------------------------------*//**
  131. * <param name="name">The sparse array.</param>
  132. * <param name="index">The index in the array.</param>
  133. * <returns>
  134. * Does this index exist?
  135. * </returns>
  136. *//*------------------------------------------------------------------------**/
  137. stock bool:Sparse_Contains(const name[], index)
  138. {
  139. new id[64];
  140. SparseArray_MakeName(id, name, index);
  141. return SparseArray_Exists(id, name, index);
  142. }
  143. /*-------------------------------------------------------------------------*//**
  144. * <param name="name">The sparse array.</param>
  145. * <param name="index">The index in the array.</param>
  146. * <remarks>
  147. * Remove the value from the array.
  148. * </remarks>
  149. *//*------------------------------------------------------------------------**/
  150. stock Sparse_UnSet(const name[], index)
  151. {
  152. new id[64];
  153. SparseArray_MakeName(id, name, index);
  154. SparseArray_DoDelete(id, name, index);
  155. }
  156. /*-------------------------------------------------------------------------*//**
  157. * <param name="name">The sparse array.</param>
  158. * <param name="index">The index in the array.</param>
  159. * <param name="value">The data to write.</param>
  160. * <remarks>
  161. * Add the value to the array.
  162. * </remarks>
  163. *//*------------------------------------------------------------------------**/
  164. stock Sparse_Set(const name[], index, value)
  165. {
  166. new id[64];
  167. SparseArray_MakeName(id, name, index);
  168. SparseArray_DoWrite(id, name, index, value);
  169. }
  170. /*-------------------------------------------------------------------------*//**
  171. * <param name="name">The sparse array.</param>
  172. * <param name="index">The index in the array.</param>
  173. * <param name="value">The destination variable.</param>
  174. * <param name="nv">The new value.</param>
  175. * <returns>
  176. * Did the value exist?
  177. * </returns>
  178. * <remarks>
  179. * Get the value and remove it from the array.
  180. * </remarks>
  181. *//*------------------------------------------------------------------------**/
  182. stock bool:Sparse_Exchange(const name[], index, &value, nv = cellmin)
  183. {
  184. new id[64];
  185. SparseArray_MakeName(id, name, index);
  186. if (SparseArray_Exists(id, name, index))
  187. {
  188. value = SparseArray_DoRead(id, name, index);
  189. if (nv == cellmin)
  190. SparseArray_DoDelete(id, name, index);
  191. else
  192. SparseArray_DoWrite(id, name, index, nv);
  193. return true;
  194. }
  195. else if (nv != cellmin)
  196. SparseArray_DoWrite(id, name, index, nv);
  197. return false;
  198. }
  199. #if defined YSI_TESTS
  200. #include "..\y_iterate"
  201. #include "..\y_bit"
  202. #include "y_sparsearray_tests"
  203. #endif