polygons.inc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //by Seregamil
  2. /*
  3. Function's:
  4. CreatePolygon( Float: area[ ], all_points = sizeof area )
  5. DestroyPolygon( polygonid )
  6. PolygonFlashForAll( polygonid, color = 0xFF0000AA )
  7. PolygonFlashForPlayer( playerid, polygonid, color = 0xFF0000AA )
  8. PolygonStopFlashForAll( polygonid )
  9. PolygonStopFlashForPlayer( playerid, polygonid )
  10. PolygonShowForAll( polygonid, color = 0xFFFFFFAA )
  11. PolygonShowForPlayer( playerid, polygonid, color = 0xFFFFFFAA )
  12. PolygonHideForAll( playerid, polygonid )
  13. PolygonHideForPlayer( playerid, polygonid )
  14. CountGangZones()
  15. Callback's:
  16. OnPlayerEnterPolygon( playerid, polygonid )
  17. OnPlayerLeavePolygon( playerid, polygonid )
  18. */
  19. #define MAX_POLYGONS 20
  20. #if !defined MAX_GANGZONES
  21. #define MAX_GANGZONES 1024
  22. #endif
  23. #if !defined INVALID_AREA_ID
  24. #define INVALID_AREA_ID -1
  25. #endif
  26. new gangZones[ MAX_POLYGONS * 2 ] = { INVALID_AREA_ID, ... };// firts & last
  27. #if defined STREAMER_TYPE_AREA
  28. new polygons[ MAX_POLYGONS ] = { MAX_POLYGONS + 1, ... };
  29. #endif
  30. new totalGangZones = 0;
  31. forward OnPlayerEnterPolygon( playerid, polygonid );
  32. forward OnPlayerLeavePolygon( playerid, polygonid );
  33. stock plgs_GangZoneCreate(Float:minx, Float:miny, Float:maxx, Float:maxy){
  34. totalGangZones++;
  35. return GangZoneCreate(minx, miny, maxx, maxy);
  36. }
  37. stock plgs_GangZoneDestroy(zone){
  38. totalGangZones--;
  39. return GangZoneDestroy(zone);
  40. }
  41. #if defined _ALS_GangZoneCreate
  42. #undef GangZoneCreate
  43. #else
  44. #define _ALS_GangZoneCreate
  45. #endif
  46. #define GangZoneCreate plgs_GangZoneCreate
  47. #if defined _ALS_GangZoneDestroy
  48. #undef GangZoneDestroy
  49. #else
  50. #define _ALS_GangZoneDestroy
  51. #endif
  52. #define GangZoneDestroy plgs_GangZoneDestroy
  53. stock CreatePolygon( Float: area[ ], all_points = sizeof area ){
  54. if( CountGangZones() >= MAX_GANGZONES )
  55. return MAX_POLYGONS + 1;
  56. new i = -1;
  57. new g_zones = sizeof gangZones;
  58. while( ++i < g_zones )
  59. if( gangZones[ i ] == INVALID_AREA_ID )
  60. break;
  61. if( i == g_zones )
  62. return MAX_POLYGONS + 1;
  63. new
  64. Float: start_x, Float: start_y,
  65. Float: end_x, Float: end_y,
  66. Float: draw_x, Float: draw_y,
  67. distance, slot = i;
  68. for( new points = -1, j = all_points ; ++points < j ; ){
  69. start_x = area[ points ];
  70. start_y = area[ points + 1 ];
  71. end_x = area[ points + 2 ];
  72. end_y = area[ ++points + 2 ];
  73. if( points + 1 == j )
  74. {
  75. end_x = area[ 0 ];
  76. end_y = area[ 1 ];
  77. }
  78. if( start_x == end_x || start_y == end_y ){
  79. gangZones[ slot ] = GangZoneCreate( start_x, start_y, end_x + ( start_x == end_x ? 5.0 : 0.0 ), end_y + ( start_x == end_x ? 0.0 : 5.0 ) );
  80. slot = i + 1;
  81. continue;
  82. }
  83. distance = floatround( VectorSize( end_x - start_x, end_y - start_y, 0.0 ) ) / 4;
  84. draw_x = floatdiv( end_x - start_x, float( distance ));
  85. draw_y = floatdiv( end_y - start_y, float( distance ));
  86. while( distance-- ) {
  87. start_x += draw_x;
  88. start_y += draw_y;
  89. if( gangZones[ slot ] != INVALID_AREA_ID && slot == i )
  90. slot = i + 1;
  91. gangZones[ slot ] = GangZoneCreate( start_x, start_y, start_x + 5.0, start_y + 5.0 );
  92. if( CountGangZones() >= MAX_GANGZONES )
  93. break;
  94. }
  95. if( CountGangZones() >= MAX_GANGZONES )
  96. break;
  97. }
  98. if( CountGangZones() >= MAX_GANGZONES ){
  99. DestroyPolygon( i / 2 + 1 );
  100. printf("Полигон %i был удален с сервера, т.к. достигнуто максимальное количество GZ ", i / 2 + 1);
  101. return MAX_POLYGONS + 1;
  102. }
  103. #if defined STREAMER_TYPE_AREA
  104. polygons[ i / 2 + 1 ] = CreateDynamicPolygon( area, -FLOAT_INFINITY, FLOAT_INFINITY, all_points );
  105. #endif
  106. return i / 2 + 1;
  107. }
  108. stock PolygonFlashForAll( polygonid, color = 0xFF0000AA ){
  109. if( polygonid == 0 ) return false;
  110. new slotid = polygonid * 2 - 2;
  111. if( gangZones[ slotid ] == INVALID_AREA_ID )
  112. return false;
  113. for( new i = gangZones[ slotid ], j = gangZones[ slotid + 1 ] + 1; i != j; i++ )
  114. GangZoneFlashForAll( i, color );
  115. return true;
  116. }
  117. stock PolygonFlashForPlayer( playerid, polygonid, color = 0xFF0000AA ){
  118. if( polygonid == 0 ) return false;
  119. new slotid = polygonid * 2 - 2;
  120. if( gangZones[ slotid ] == INVALID_AREA_ID )
  121. return false;
  122. for( new i = gangZones[ slotid ], j = gangZones[ slotid + 1 ] + 1; i != j; i++ )
  123. GangZoneFlashForPlayer( playerid, i, color );
  124. return true;
  125. }
  126. stock PolygonStopFlashForAll( polygonid ){
  127. if( polygonid == 0 ) return false;
  128. new slotid = polygonid * 2 - 2;
  129. if( gangZones[ slotid ] == INVALID_AREA_ID )
  130. return false;
  131. for( new i = gangZones[ slotid ], j = gangZones[ slotid + 1 ] + 1; i != j; i++ )
  132. GangZoneStopFlashForAll( i );
  133. return true;
  134. }
  135. stock PolygonStopFlashForPlayer( playerid, polygonid ){
  136. if( polygonid == 0 ) return false;
  137. new slotid = polygonid * 2 - 2;
  138. if( gangZones[ slotid ] == INVALID_AREA_ID )
  139. return false;
  140. for( new i = gangZones[ slotid ], j = gangZones[ slotid + 1 ] + 1; i != j; i++ )
  141. GangZoneStopFlashForPlayer( playerid, i );
  142. return true;
  143. }
  144. stock DestroyPolygon( polygonid ){
  145. if( polygonid == 0 ) return false;
  146. new slotid = polygonid * 2 - 2;
  147. if( gangZones[ slotid ] == INVALID_AREA_ID )
  148. return false;
  149. for( new i = gangZones[ slotid ], j = gangZones[ slotid + 1 ] + 1; i != j; i++ )
  150. GangZoneDestroy( i );
  151. gangZones[ slotid ] = INVALID_AREA_ID;
  152. gangZones[ slotid + 1 ] = INVALID_AREA_ID;
  153. #if defined STREAMER_TYPE_AREA
  154. DestroyDynamicArea( polygons[ slotid ] );
  155. #endif
  156. return true;
  157. }
  158. stock PolygonShowForAll( polygonid, color = 0xFFFFFFAA ){
  159. if( polygonid == 0 ) return false;
  160. new slotid = polygonid * 2 - 2;
  161. if( gangZones[ slotid ] == INVALID_AREA_ID )
  162. return false;
  163. for( new i = gangZones[ slotid ], j = gangZones[ slotid + 1 ] + 1; i != j; i++ )
  164. GangZoneShowForAll( i, color );
  165. return true;
  166. }
  167. stock PolygonShowForPlayer( playerid, polygonid, color = 0xFFFFFFAA ){
  168. if( polygonid == 0 ) return false;
  169. new slotid = polygonid * 2 - 2;
  170. if( gangZones[ slotid ] == INVALID_AREA_ID )
  171. return false;
  172. for( new i = gangZones[ slotid ], j = gangZones[ slotid + 1 ] + 1; i != j; i++ )
  173. GangZoneShowForPlayer( playerid, i, color );
  174. return true;
  175. }
  176. stock PolygonHideForAll( polygonid ){
  177. if( polygonid == 0 ) return false;
  178. new slotid = polygonid * 2 - 2;
  179. if( gangZones[ slotid ] == INVALID_AREA_ID )
  180. return false;
  181. for( new i = gangZones[ slotid ], j = gangZones[ slotid + 1 ] + 1; i != j; i++ )
  182. GangZoneHideForAll( i );
  183. return true;
  184. }
  185. stock PolygonHideForPlayer( playerid, polygonid ){
  186. if( polygonid == 0 ) return false;
  187. new slotid = polygonid * 2 - 2;
  188. if( gangZones[ slotid ] == INVALID_AREA_ID )
  189. return false;
  190. for( new i = gangZones[ slotid ], j = gangZones[ slotid + 1 ] + 1; i != j; i++ )
  191. GangZoneHideForPlayer( playerid, i );
  192. return true;
  193. }
  194. stock CountGangZones()
  195. return totalGangZones;
  196. #if defined STREAMER_TYPE_AREA
  197. public OnPlayerEnterDynamicArea( playerid, areaid ){
  198. for( new i = 1; i != MAX_POLYGONS + 1; i++){
  199. if( polygons[ i ] == areaid ){
  200. CallLocalFunction("OnPlayerEnterPolygon", "ii", playerid, i );
  201. break;
  202. }
  203. }
  204. #if defined plgs_OnPlayerEnterDynamicArea
  205. return plgs_OnPlayerEnterDynamicArea( playerid, areaid );
  206. #endif
  207. }
  208. #if defined _ALS_OnPlayerEnterDynamicArea
  209. #undef OnPlayerEnterDynamicArea
  210. #else
  211. #define _ALS_OnPlayerEnterDynamicArea
  212. #endif
  213. #define OnPlayerEnterDynamicArea plgs_OnPlayerEnterDynamicArea
  214. #if defined plgs_OnPlayerEnterDynamicArea
  215. forward plgs_OnPlayerEnterDynamicArea( playerid, areaid );
  216. #endif
  217. public OnPlayerLeaveDynamicArea( playerid, areaid ){
  218. for( new i = 1; i != MAX_POLYGONS + 1; i++){
  219. if( polygons[ i ] == areaid ){
  220. CallLocalFunction("OnPlayerLeavePolygon", "ii", playerid, i );
  221. break;
  222. }
  223. }
  224. #if defined plgs_OnPlayerLeaveDynamicArea
  225. return plgs_OnPlayerLeaveDynamicArea( playerid, areaid );
  226. #endif
  227. }
  228. #if defined _ALS_OnPlayerLeaveDynamicArea
  229. #undef OnPlayerLeaveDynamicArea
  230. #else
  231. #define _ALS_OnPlayerLeaveDynamicArea
  232. #endif
  233. #define OnPlayerLeaveDynamicArea plgs_OnPlayerLeaveDynamicArea
  234. #if defined plgs_OnPlayerLeaveDynamicArea
  235. forward plgs_OnPlayerLeaveDynamicArea( playerid, areaid );
  236. #endif
  237. #endif