y_areas.inc 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844
  1. /**--------------------------------------------------------------------------**\
  2. =============================
  3. Y Sever Includes - Areas Core
  4. =============================
  5. Description:
  6. Handles area checks for player location based code not involving CPs.
  7. Legal:
  8. Version: MPL 1.1
  9. The contents of this file are subject to the Mozilla Public License Version
  10. 1.1 (the "License"); you may not use this file except in compliance with
  11. the License. You may obtain a copy of the License at
  12. http://www.mozilla.org/MPL/
  13. Software distributed under the License is distributed on an "AS IS" basis,
  14. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  15. for the specific language governing rights and limitations under the
  16. License.
  17. The Original Code is the YSI AMX include.
  18. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  19. Portions created by the Initial Developer are Copyright (C) 2011
  20. the Initial Developer. All Rights Reserved.
  21. Contributors:
  22. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  23. Thanks:
  24. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  25. ZeeX - Very productive conversations.
  26. koolk - IsPlayerinAreaEx code.
  27. TheAlpha - Danish translation.
  28. breadfish - German translation.
  29. Fireburn - Dutch translation.
  30. yom - French translation.
  31. 50p - Polish translation.
  32. Zamaroht - Spanish translation.
  33. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  34. for me to strive to better.
  35. Pixels^ - Running XScripters where the idea was born.
  36. Matite - Pestering me to release it and using it.
  37. Very special thanks to:
  38. Thiadmer - PAWN, whose limits continue to amaze me!
  39. Kye/Kalcor - SA:MP.
  40. SA:MP Team past, present and future - SA:MP.
  41. Version:
  42. 0.3
  43. Changelog:
  44. 01/12/12:
  45. Added ovals and ovoids.
  46. Set cuboids to require only one slot in memory.
  47. Changed poly memory layout for vastly more efficient storage.
  48. 13/08/12:
  49. Added overlapping areas support.
  50. 01/01/12:
  51. Massive ground-up rewrite based on zones.
  52. 27/08/07:
  53. Made master compatible.
  54. 03/08/07:
  55. Updated timer system.
  56. 31/07/07:
  57. Made Area_GetPlayerArea safe.
  58. 08/05/07:
  59. First version.
  60. Functions:
  61. Public:
  62. Area_Loop - Main loop for checking who's where.
  63. Area_Remote - Does minor functions remotely.
  64. Area_AddRemote - Adds areas remotely.
  65. YSIM_Areas - Master system interface.
  66. Area_Broadcast - Recieves transmitted areas.
  67. Area_UpdateEmpty - Resets the empty array after master change.
  68. Core:
  69. Area_Area - Constructor.
  70. Area_CheckArea - Gets an area's type anx calls the relevant function.
  71. Area_OnPlayerConnect - Called when a player connects.
  72. Stock:
  73. Area_Delete - Deletes an area.
  74. Area_AddCube - Adds a cube.
  75. Area_AddBox - Adds a box.
  76. Area_AddCircle - Adds a circle.
  77. Area_AddSphere - Adds a sphere.
  78. Area_AddPoly - Adds a polygon.
  79. Area_GetPlayerArea - Gets the area a player is in.
  80. Area_SetPlayer - Sets wether a player can use this area.
  81. Area_SetAllPlayers - Sets wether all players can use this area.
  82. Area_SetAllWorlds - Sets wether all worlds have this are.
  83. Area_SetWorld - Sets wether this world has this area.
  84. Area_IsValid - Checks if an area is valid.
  85. Static:
  86. Area_IsInCircle - Checks if a player is in this circular area.
  87. Area_IsInSphere - Checks if a player is in this spherical area.
  88. Area_IsInPoly - Checks if a player is in this polygonal area.
  89. Area_IsInCube - Checks if a player is in this cubic area.
  90. Area_IsInBox - Checks if a player is in this rectangular area.
  91. Area_AddToUnused - Adds a area pointer to the unused list.
  92. Area_GetFreeSlot - Gets the next free area slot.
  93. Inline:
  94. Area_IsActive - Checks if an area is active.
  95. Area_GetEmptySlotCount - Gets the number of empty slots.
  96. Area_AddSlots - Removes slots from the unused count.
  97. API:
  98. -
  99. Callbacks:
  100. OnPlayerEnterArea - Called when a player enters an area.
  101. OnPlayerLeaveArea - Called when a player leaves an area.
  102. Definitions:
  103. MAX_AREAS - Max number of areas.
  104. NO_AREA - Fail return.
  105. AREA_NO_NEXT - Marker for end of a list.
  106. AREA_WORLDS - Number of worlds an area can be in.
  107. AREA_WORLD_COUNT - Number of cells required for a bit array of AREA_WORLDS.
  108. Enums:
  109. e_AREA_FLAGS - Flags for each area.
  110. E_AREA - Structure for part of an areas data.
  111. Macros:
  112. -
  113. Tags:
  114. -
  115. Variables:
  116. Global:
  117. -
  118. Static:
  119. YSI_g_sUnusedAreas - Pointer to the first unused area.
  120. YSI_g_sLastUnused - Pointer to the last unused area.
  121. YSI_g_sFreeCount - Number of unused slots.
  122. YSI_g_sAreas - Array of area data.
  123. YSI_g_sPlayerArea - Array of players' current areas.
  124. Commands:
  125. -
  126. Compile options:
  127. -
  128. Operators:
  129. -
  130. \**--------------------------------------------------------------------------**/
  131. #if defined _INC_y_areas
  132. #endinput
  133. #endif
  134. #define _INC_y_areas
  135. #include "..\YSI_Internal\y_version"
  136. #include "..\YSI_Data\y_bit"
  137. #include "..\YSI_Data\y_playerarray"
  138. #include "..\YSI_Data\y_iterate"
  139. #include "..\YSI_Core\y_debug"
  140. #include "..\YSI_Coding\y_inline"
  141. #include "..\YSI_Coding\y_remote"
  142. #if defined AREAS_USE_TIMER
  143. #include "..\YSI_Coding\y_timers"
  144. #endif
  145. #include "..\YSI_Internal\y_natives"
  146. #if !defined MAX_AREAS
  147. #define MAX_AREAS (1024)
  148. // Now supports as many as you want (pretty much), and in a VASTLY better way!
  149. #elseif MAX_AREAS >= 0x3FFFF
  150. #error This version does not support more than 262142 areas.
  151. #endif
  152. // Set commands as master 25.
  153. #define YSIM_U_DISABLE
  154. #define MASTER 62
  155. #include "..\YSI_Core\y_master"
  156. #define _GROUP_MAKE_NAME<%0...%1> %0Area%1
  157. #define _GROUP_MAKE_LIMIT MAX_AREAS
  158. #include "..\YSI_Players\y_groups\_funcs"
  159. #include "..\YSI_Coding\y_hooks"
  160. stock _YSI_HAS_y_areas(_do_not_call_me_ever_, _like_never_)
  161. {
  162. P:E("You called _YSI_HAS_y_areas! Bad, don't do that!", _do_not_call_me_ever_, _like_never_);
  163. assert(false);
  164. return 0;
  165. }
  166. #define NO_AREA (-1)
  167. //#define SINGLE_SLOT_AREA (e_AREA_FLAGS_NEXT)
  168. #define AREA_NO_NEXT (_:e_AREA_FLAGS_NEXT)
  169. // This will divide the world up in to 400 blocks, the fact that 400=400 is
  170. // PURE fluke. Setting this to 500 would result in 256 blocks:
  171. //
  172. // ((AREAS_ZONE_BOUNDS / AREAS_ZONE_SIZE) ** 2) * 4
  173. //
  174. #if !defined AREAS_ZONE_SIZE
  175. #define AREAS_ZONE_SIZE 400
  176. #endif
  177. #if !defined AREAS_ZONE_BOUNDS
  178. #define AREAS_ZONE_BOUNDS 4000
  179. #endif
  180. // You can of course have more, but if you do have more the extras will be dealt
  181. // with separately. I can see three happening sometimes, but not often. More
  182. // than that will probably be very rare, and less will be more common, so we can
  183. // use the third slot usually to store the "NO_AREA" terminator.
  184. #if !defined AREAS_MAX_OVERLAPS
  185. #define AREAS_MAX_OVERLAPS (3)
  186. #endif
  187. #define AREAS_ZONE_ARRAY (ceildiv(AREAS_ZONE_BOUNDS * 2, AREAS_ZONE_SIZE))
  188. #if !defined AREA_WORLDS
  189. #define AREA_WORLDS 256
  190. #endif
  191. #define AREAS_ZONE_PARTS (ceildiv(AREAS_ZONE_BOUNDS, AREAS_ZONE_SIZE))
  192. #define Area_MakeZone(%0) (floatround((%0) / AREAS_ZONE_SIZE.0, floatround_floor)) // - AREAS_ZONE_PARTS)
  193. #define _AREAS_ADD_TO_ZONE(%0,%1,%2,%3) YSI_g_sAreas[%2][E_AREA_FLAGS]=(YSI_g_sAreas[%2][E_AREA_FLAGS]&e_AREA_FLAGS_REST)|(e_AREA_FLAGS:%0&e_AREA_FLAGS_NEXT|e_AREA_FLAGS:(%3<<18)&e_AREA_FLAGS_ZONE),%0=%1
  194. //#define _AREAS_ZONES_O(%0) return e_AREA_ZONE_O_%0
  195. //#define _AREAS_ZONES_I(%0) return e_AREA_ZONE_I_%0
  196. //#define _AREAS_ZONES_X(%0) return e_AREA_ZONE_X_%0
  197. #define _AREAS_ZONES_O(%0) return _AREAS_ADD_TO_ZONE(YSI_g_sZoneO%0,area,last,e_AREA_ZONE_O_%0)
  198. #define _AREAS_ZONES_I(%0) return _AREAS_ADD_TO_ZONE(YSI_g_sZoneI%0,area,last,e_AREA_ZONE_I_%0)
  199. #define _AREAS_ZONES_X(%0) return _AREAS_ADD_TO_ZONE(YSI_g_sZoneX%0,area,last,e_AREA_ZONE_X_%0)
  200. #define _AREAS_ZONES_I_GRID zx=1// zx=Area_MakeZone(minx);zy=Area_MakeZone(miny);if(zx==Area_MakeZone(maxx)&&zy==Area_MakeZone(maxy))return _AREAS_MAKE_ZONE(zx,zy),_AREAS_ADD_TO_ZONE(YSI_g_sZones[zy],area,last,zy)
  201. #define _AREAS_MAKE_ZONE(%0,%1) %1+=((%0)+AREAS_ZONE_PARTS)*AREAS_ZONE_ARRAY+AREAS_ZONE_PARTS
  202. //#define _AREA_DO_ALL(%0) sCur=%0;while(sCur!=AREA_NO_NEXT)if(Area_CheckArea(playerid,sW,sCur,sX,sY,sZ))return Area_DoEnter(playerid,(YSI_g_sPlayerArea[playerid]=sCur))
  203. #define _AREA_DO_ALL(%0) sNext=%0;while((sCur=sNext)!=AREA_NO_NEXT)if(Area_CheckArea(playerid,sW,sCur,sX,sY,sZ,sNext,sAreas,sAlready))Area_DoEnter(playerid,sCur,in)
  204. /*#define _AREA_DO_ALL(%0)
  205. sCur = zone;
  206. while (sCur != AREA_NO_NEXT)
  207. if (Area_CheckArea(playerid, sW, sCur, sX, sY, sZ))
  208. return Area_DoEnter(playerid, (YSI_g_sPlayerArea[playerid] = sCur));
  209. */
  210. #define _AREAS_DO_REMOVE(%0,%1,%2) new __z=-1,__y=%0;while(__y!=%1)__z=__y,__y=_:(YSI_g_sAreas[__z][E_AREA_FLAGS] & e_AREA_FLAGS_NEXT);return ((__z==-1)?(%0=%2):(_:(YSI_g_sAreas[__z][E_AREA_FLAGS]=YSI_g_sAreas[__z][E_AREA_FLAGS]&e_AREA_FLAGS_I_NEXT|(e_AREA_FLAGS:%2&e_AREA_FLAGS_NEXT))))
  211. enum e_AREA_FLAGS
  212. {
  213. // These aren't used any more either, they get encoded in the "POS" array
  214. // (sort of) with polys.
  215. //e_AREA_FLAGS_NEXT = 0x2000 - 1 << 0, //0x00001FFF,
  216. e_AREA_FLAGS_NEXT = 0x0003FFFF,
  217. //e_AREA_FLAGS_ZONE = 1023 << 18, //0x0FFC0000,
  218. e_AREA_FLAGS_ZONE = 0x0FFC0000,
  219. e_AREA_FLAGS_I_NEXT = ~(e_AREA_FLAGS_NEXT),
  220. e_AREA_FLAGS_REST = ~(e_AREA_FLAGS_NEXT | e_AREA_FLAGS_ZONE),
  221. //e_AREA_FLAGS_TYPE = 0x38000000,
  222. e_AREA_FLAGS_SPHERE = 1 << 28, //0x08000000,
  223. e_AREA_FLAGS_POLY = 2 << 28, //0x10000000,
  224. e_AREA_FLAGS_CUBE = 3 << 28, //0x18000000,
  225. //e_AREA_FLAGS_BOX = 4 << 28, //0x20000000,
  226. e_AREA_FLAGS_OVOID = 4 << 28, //0x08000000,
  227. e_AREA_FLAGS_OVAL = 5 << 28, //0x28000000,
  228. // The reserved type is now in use as a poly area child.
  229. //e_AREA_FLAGS_RES_0 = 6 << 28, //0x28000000,
  230. e_AREA_FLAGS_CHILD = 6 << 28, //0x30000000,
  231. e_AREA_FLAGS_CIRCLE = 7 << 28, //0x30000000,
  232. // Type mask.
  233. e_AREA_FLAGS_UNUSED = 0b000 << 28, //0x00000000,
  234. e_AREA_FLAGS_TYPE = 0b111 << 28, //0x38000000,
  235. // The "USED" flag isn't really needed any more, it's encoded in the type.
  236. // It seems the "used" flag was never read anyway...
  237. e_AREA_FLAGS_ACTIVE = 1 << 31,
  238. //e_AREA_FLAGS_USED = 1 << 31
  239. // If this is true AND AREA_VERY_FAST is on, the bounds check is identical
  240. // to the sphere check, so only do one.
  241. //e_AREA_FLAGS_BOUNDS = 1 << 31
  242. //e_AREA_FLAGS_SPHERE = 1 << 31
  243. }
  244. // Go backwards, so these all have the upper bit set.
  245. enum (+= -1)
  246. {
  247. e_AREA_ZONE_I_N = -1,
  248. e_AREA_ZONE_I_NE, // -2
  249. e_AREA_ZONE_I_E, // -3
  250. e_AREA_ZONE_I_SE, // -4
  251. e_AREA_ZONE_I_S, // -5
  252. e_AREA_ZONE_I_SW, // -6
  253. e_AREA_ZONE_I_W, // -7
  254. e_AREA_ZONE_I_NW, // -8
  255. e_AREA_ZONE_I_, // -9
  256. // "Outer" zones. -4000 > x, y > 4000;
  257. e_AREA_ZONE_O_N, // -10
  258. e_AREA_ZONE_O_NE, // -11
  259. e_AREA_ZONE_O_E, // -12
  260. e_AREA_ZONE_O_SE, // -13
  261. e_AREA_ZONE_O_S, // -14
  262. e_AREA_ZONE_O_SW, // -15
  263. e_AREA_ZONE_O_W, // -16
  264. e_AREA_ZONE_O_NW, // -17
  265. //e_AREA_ZONE_O_, // Can't be in all of O, but none of I.
  266. // "Extra" zones. In a quadrant, but spanning the +-4000 boundary.
  267. e_AREA_ZONE_X_N, // -18
  268. e_AREA_ZONE_X_NE, // -19
  269. e_AREA_ZONE_X_E, // -20
  270. e_AREA_ZONE_X_SE, // -21
  271. e_AREA_ZONE_X_S, // -22
  272. e_AREA_ZONE_X_SW, // -23
  273. e_AREA_ZONE_X_W, // -24
  274. e_AREA_ZONE_X_NW, // -25
  275. // Extra zones. For when areas are too big for a quadrant.
  276. e_AREA_ZONE_X_, // -26
  277. e_AREA_ZONE_NONE = cellmin
  278. }
  279. // =============================================================================
  280. // =============================================================================
  281. // IMPORTANT NOTE: The order of elements in this enum is INCREDIBLY important.
  282. // Because this is three enums in one, they share some pieces of data that must
  283. // not get overridden.
  284. // =============================================================================
  285. // =============================================================================
  286. // This might just be the most complex enum I've ever made!
  287. enum E_AREA
  288. {
  289. //union
  290. //{
  291. // struct
  292. // {
  293. #if YSIM_HAS_MASTER
  294. E_AREA_MASTER,
  295. #endif
  296. // This has increased from 4 to 6, so we can store cubes and ovoids
  297. // in a single slot.
  298. Float:E_AREA_POS[6],
  299. PlayerArray:E_AREA_PLAYERS<MAX_PLAYERS>,
  300. // This MUST go between "E_AREA_PLAYERS" and "E_AREA_FLAGS" to
  301. // remain constant in subsequent unions.
  302. //#if defined AREA_VERY_FAST
  303. // Float:E_AREA_BOUNDING[4],
  304. //#endif
  305. // As must this.
  306. #if AREA_WORLDS > 0
  307. BitArray:E_AREA_WORLDS<AREA_WORLDS>,
  308. #endif
  309. // ALWAYS last (actually used by EVERY type).
  310. e_AREA_FLAGS:E_AREA_FLAGS,
  311. // }
  312. // // Start of poly data.
  313. // struct
  314. // {
  315. // Reset the enum counter to 0 (on the next item).
  316. _E_AREA_RESET_@1 = -1,
  317. // Now restart.
  318. #if YSIM_HAS_MASTER
  319. // Skip the master flag if it exists.
  320. _E_AREA_MASTER_@1,
  321. #endif
  322. // This is where polys differ from all others.
  323. E_AREA_POLY_COUNT,
  324. E_AREA_POLY_NEXT,
  325. //Float:E_AREA_POLY_POS[4],
  326. // This slot holds ONLY the polygon bounding data, it doesn't hold
  327. // any co-ordinates AT ALL.
  328. //Float:E_AREA_BOUNDING[4],
  329. Float:E_AREA_POLY_BOUND_X,
  330. Float:E_AREA_POLY_BOUND_Y,
  331. Float:E_AREA_POLY_BOUND_R,
  332. Float:E_AREA_POLY_BOUND_H,
  333. // Skip all the array and flag data, they stay the same.
  334. _E_AREA_ARRAYS_@1[E_AREA_FLAGS - E_AREA_PLAYERS],
  335. // Skip the flags.
  336. E_AREA_UNUSED_NEXT,
  337. // }
  338. // // Start of poly child data.
  339. // struct
  340. // {
  341. // Reset the enum counter to 0 (on the next item).
  342. _E_AREA_RESET_@2 = -1,
  343. // Now restart.
  344. #if YSIM_HAS_MASTER
  345. // Skip the master flag if it exists.
  346. _E_AREA_MASTER_@2,
  347. #endif
  348. // Store a link to the parent of this poly child.
  349. E_AREA_CHILD_PARENT,
  350. // And the next one in VERY rare cases (I hope).
  351. // THIS SHARES A SLOT WITH "E_AREA_POLY_NEXT", so we can always just
  352. // use that slot when iterating.
  353. #if _:(E_AREA_FLAGS - E_AREA_CHILD_PARENT) & 1
  354. // There are an ODD number of available slots. We can store an
  355. // extra pair iff we don't need to use "NEXT".
  356. //_E_AREA_SKIP_@2,
  357. E_AREA_CHILD_OPT_Y,
  358. #endif
  359. E_AREA_CHILD_OPT_X,
  360. // Just store a vast number of X/Y pairs in this child.
  361. Float:E_AREA_CHILD_ELEMS[_:E_AREA_FLAGS - _:E_AREA_CHILD_OPT_X - 1],
  362. // Skip the flags.
  363. _E_AREA_CHILD_ELEMS_END
  364. // }
  365. }
  366. #define CHILD_AREA_SLOTS ((_:_E_AREA_CHILD_ELEMS_END - _:E_AREA_CHILD_ELEMS))
  367. static stock
  368. YSI_g_sUnusedAreas = 0,
  369. //YSI_g_sLastUnused = MAX_AREAS - 1,
  370. YSI_g_sFreeCount = MAX_AREAS,
  371. YSI_g_sAreas[MAX_AREAS][E_AREA],
  372. YSI_g_sPlayerArea[MAX_PLAYERS][AREAS_MAX_OVERLAPS], // = {NO_AREA, ...},
  373. YSI_g_sHasCallbacks,
  374. YSI_g_sZones[AREAS_ZONE_ARRAY * AREAS_ZONE_ARRAY] = {AREA_NO_NEXT, ...},
  375. // "Inner" zones. -4000 <= x, y <= 4000; Z(x) != Z(y)
  376. YSI_g_sZoneIN = AREA_NO_NEXT, // -1
  377. YSI_g_sZoneINE = AREA_NO_NEXT, // -2
  378. YSI_g_sZoneIE = AREA_NO_NEXT, // -3
  379. YSI_g_sZoneISE = AREA_NO_NEXT, // -4
  380. YSI_g_sZoneIS = AREA_NO_NEXT, // -5
  381. YSI_g_sZoneISW = AREA_NO_NEXT, // -6
  382. YSI_g_sZoneIW = AREA_NO_NEXT, // -7
  383. YSI_g_sZoneINW = AREA_NO_NEXT, // -8
  384. // "Outer" zones. -4000 > x, y > 4000;
  385. YSI_g_sZoneON = AREA_NO_NEXT, // -9
  386. YSI_g_sZoneONE = AREA_NO_NEXT, // -10
  387. YSI_g_sZoneOE = AREA_NO_NEXT, // -11
  388. YSI_g_sZoneOSE = AREA_NO_NEXT, // -12
  389. YSI_g_sZoneOS = AREA_NO_NEXT, // -13
  390. YSI_g_sZoneOSW = AREA_NO_NEXT, // -14
  391. YSI_g_sZoneOW = AREA_NO_NEXT, // -15
  392. YSI_g_sZoneONW = AREA_NO_NEXT, // -16
  393. // "Extra" zones. In a quadrant, but spanning the +-4000 boundary.
  394. YSI_g_sZoneXN = AREA_NO_NEXT, // -17
  395. YSI_g_sZoneXNE = AREA_NO_NEXT, // -18
  396. YSI_g_sZoneXE = AREA_NO_NEXT, // -19
  397. YSI_g_sZoneXSE = AREA_NO_NEXT, // -20
  398. YSI_g_sZoneXS = AREA_NO_NEXT, // -21
  399. YSI_g_sZoneXSW = AREA_NO_NEXT, // -22
  400. YSI_g_sZoneXW = AREA_NO_NEXT, // -23
  401. YSI_g_sZoneXNW = AREA_NO_NEXT, // -24
  402. // Extra zones. For when areas are too big for a quadrant.
  403. YSI_g_sZoneI = AREA_NO_NEXT, // -25
  404. //YSI_g_sZoneO = AREA_NO_NEXT, // -26
  405. YSI_g_sZoneX = AREA_NO_NEXT; // -27
  406. forward Area_Loop();
  407. /**--------------------------------------------------------------------------**\
  408. <summary>OnScriptInit</summary>
  409. <returns>
  410. -
  411. </returns>
  412. <remarks>
  413. Sets up required variables. Note that this hooks "OnScriptInit",
  414. "OnGameModeInit" and "OnFilterScript". This is because ALL scripts need to
  415. initialise some things, and only the master needs to initialise others.
  416. </remarks>
  417. \**--------------------------------------------------------------------------**/
  418. mhook OnScriptInit()
  419. {
  420. for (new i = 0; i != MAX_AREAS - 1; ++i)
  421. {
  422. YSI_g_sAreas[i][E_AREA_UNUSED_NEXT] = (i + 1);
  423. }
  424. YSI_g_sAreas[MAX_AREAS - 1][E_AREA_UNUSED_NEXT] = AREA_NO_NEXT;
  425. //for (new i = 0; i != AREAS_ZONE_ARRAY * AREAS_ZONE_ARRAY; ++i)
  426. //{
  427. // YSI_g_sZones[i] = -1;
  428. //}
  429. return 1;
  430. }
  431. #if !defined FILTERSCRIPT
  432. hook OnGameModeInit()
  433. {
  434. if (!YSI_FILTERSCRIPT)
  435. {
  436. new
  437. buffer;
  438. YSI_g_sHasCallbacks = 0;
  439. YSI_g_sHasCallbacks |= funcidx("OnPlayerEnterArea") == -1 ? 0 : 1;
  440. YSI_g_sHasCallbacks |= funcidx("OnPlayerLeaveArea") == -1 ? 0 : 2;
  441. YSI_g_sHasCallbacks |= AMX_GetPublicEntry(0, buffer, "@yH_PlayerEnterArea") ? 4 : 0;
  442. YSI_g_sHasCallbacks |= AMX_GetPublicEntry(0, buffer, "@yH_PlayerLeaveArea") ? 8 : 0;
  443. }
  444. }
  445. #endif
  446. hook OnFilterScriptInit()
  447. {
  448. new
  449. buffer;
  450. YSI_g_sHasCallbacks = 0;
  451. YSI_g_sHasCallbacks |= funcidx("OnPlayerEnterArea") == -1 ? 0 : 1;
  452. YSI_g_sHasCallbacks |= funcidx("OnPlayerLeaveArea") == -1 ? 0 : 2;
  453. YSI_g_sHasCallbacks |= AMX_GetPublicEntry(0, buffer, "@yH_PlayerEnterArea") ? 4 : 0;
  454. YSI_g_sHasCallbacks |= AMX_GetPublicEntry(0, buffer, "@yH_PlayerLeaveArea") ? 8 : 0;
  455. }
  456. /**--------------------------------------------------------------------------**\
  457. <summary>Area_GetEmptySlotCount</summary>
  458. <returns>
  459. Number of unused area slots.
  460. </returns>
  461. <remarks>
  462. -
  463. </remarks>
  464. \**--------------------------------------------------------------------------**/
  465. #define Area_GetEmptySlotCount() \
  466. (YSI_g_sFreeCount)
  467. /**--------------------------------------------------------------------------**\
  468. <summary>Area_AddSlots</summary>
  469. <param name="num">Number of slots to add.</param>
  470. <returns>
  471. -
  472. </returns>
  473. <remarks>
  474. Actually removes slots from the unused count.
  475. </remarks>
  476. \**--------------------------------------------------------------------------**/
  477. #define Area_AddSlots(%1) \
  478. YSI_g_sFreeCount -= (%1)
  479. /**--------------------------------------------------------------------------**\
  480. <summary>Area_IsActive</summary>
  481. <param name="area">Area to check validity of</param>
  482. <returns>
  483. -
  484. </returns>
  485. <remarks>
  486. An area slot could be used but still invalid if it's not the first slot in
  487. an area set.
  488. </remarks>
  489. \**--------------------------------------------------------------------------**/
  490. #define Area_IsActive(%1) \
  491. ((%1) >= 0 && (%1) < MAX_AREAS && (YSI_g_sAreas[(%1)][E_AREA_FLAGS] & e_AREA_FLAGS_ACTIVE))
  492. /**--------------------------------------------------------------------------**\
  493. <summary>Area_Debug</summary>
  494. <param name="area">Area to print information about.</param>
  495. <returns>
  496. -
  497. </returns>
  498. <remarks>
  499. -
  500. </remarks>
  501. \**--------------------------------------------------------------------------**/
  502. foreign void:Area_Debug(area);
  503. global void:Area_Debug(area)
  504. {
  505. new
  506. e_AREA_FLAGS:flags = YSI_g_sAreas[area][E_AREA_FLAGS];
  507. if ((flags & e_AREA_FLAGS_ACTIVE))
  508. {
  509. switch (flags & e_AREA_FLAGS_TYPE)
  510. {
  511. case e_AREA_FLAGS_POLY:
  512. {
  513. new
  514. count = YSI_g_sAreas[area][E_AREA_POLY_COUNT];
  515. printf("Poly Area %d:", area);
  516. printf("\tPoints = %d", count);
  517. printf("\tNext = %d", YSI_g_sAreas[area][E_AREA_UNUSED_NEXT] & AREA_NO_NEXT);
  518. #if AREA_WORLDS > 0
  519. printf("\tWorlds = %s", Bit_Display(YSI_g_sAreas[area][E_AREA_WORLDS], bits<AREA_WORLDS>));
  520. #else
  521. printf("\tNo Worlds");
  522. #endif
  523. printf("\tPlayers = %s", Bit_Display(YSI_g_sAreas[area][E_AREA_PLAYERS][1], bits<MAX_PLAYERS>));
  524. printf("\tBounding Circle: %.2f, %.2f, %.2f, %.2f", YSI_g_sAreas[area][E_AREA_POLY_BOUND_X], YSI_g_sAreas[area][E_AREA_POLY_BOUND_Y], floatsqroot(YSI_g_sAreas[area][E_AREA_POLY_BOUND_R]), YSI_g_sAreas[area][E_AREA_POLY_BOUND_H]);
  525. new
  526. next = YSI_g_sAreas[area][E_AREA_POLY_NEXT];
  527. while (count > 0)
  528. {
  529. printf("\tPoints Set %d:", next);
  530. for (new i = 0, j = min(count, CHILD_AREA_SLOTS); i < j; i += 2)
  531. {
  532. printf("\t\t(%.2f, %.2f)", YSI_g_sAreas[next][E_AREA_CHILD_ELEMS][i], YSI_g_sAreas[next][E_AREA_CHILD_ELEMS][i + 1]);
  533. }
  534. next = YSI_g_sAreas[next][E_AREA_POLY_NEXT];
  535. count -= CHILD_AREA_SLOTS;
  536. }
  537. }
  538. }
  539. //return 1;
  540. }
  541. //return 0;
  542. }
  543. /**--------------------------------------------------------------------------**\
  544. <summary>Area_GetFreeSlot</summary>
  545. <returns>
  546. Next available slot.
  547. </returns>
  548. <remarks>
  549. Gets an empty slot, removes it from the unused list and returs a pointer.
  550. </remarks>
  551. \**--------------------------------------------------------------------------**/
  552. static stock Area_GetFreeSlot()
  553. {
  554. P:4("Area_GetFreeSlot called");
  555. if (YSI_g_sUnusedAreas == AREA_NO_NEXT) return NO_AREA;
  556. P:7("Area_GetFreeSlot: 1");
  557. new
  558. old = YSI_g_sUnusedAreas;
  559. YSI_g_sUnusedAreas = YSI_g_sAreas[old][E_AREA_UNUSED_NEXT];
  560. --YSI_g_sFreeCount;
  561. return old;
  562. }
  563. /**--------------------------------------------------------------------------**\
  564. <summary>Area_Delete</summary>
  565. <param name="area">Area to remove from the list.</param>
  566. <returns>
  567. -
  568. </returns>
  569. <remarks>
  570. You can only remove areas which are at the start of a list.
  571. </remarks>
  572. \**--------------------------------------------------------------------------**/
  573. foreign void:Area_Delete(area);
  574. global void:Area_Delete(area)
  575. {
  576. if (!Area_IsActive(area))
  577. {
  578. return; //0;
  579. }
  580. new
  581. e_AREA_FLAGS:flags = YSI_g_sAreas[area][E_AREA_FLAGS];
  582. switch (flags & e_AREA_FLAGS_TYPE)
  583. {
  584. case e_AREA_FLAGS_UNUSED:
  585. {
  586. return; //0;
  587. }
  588. case e_AREA_FLAGS_CHILD:
  589. {
  590. // This is just part of a poly, remove the whole thing.
  591. Area_Delete(YSI_g_sAreas[area][E_AREA_CHILD_PARENT]);
  592. }
  593. case e_AREA_FLAGS_POLY:
  594. {
  595. // Many blocks used - hard.
  596. new
  597. count = YSI_g_sAreas[area][E_AREA_POLY_COUNT],
  598. next = YSI_g_sAreas[area][E_AREA_POLY_NEXT],
  599. prev = YSI_g_sUnusedAreas;
  600. // All all of this poly area to the old area.
  601. while (count > 0)
  602. {
  603. // Increase the free count.
  604. ++YSI_g_sFreeCount;
  605. // Decrease the stored points.
  606. count -= CHILD_AREA_SLOTS;
  607. // Change the poly list to an unused list.
  608. YSI_g_sAreas[next][E_AREA_UNUSED_NEXT] = prev;
  609. prev = next;
  610. next = YSI_g_sAreas[next][E_AREA_POLY_NEXT];
  611. }
  612. // Remove the poly header.
  613. ++YSI_g_sFreeCount;
  614. YSI_g_sAreas[area][E_AREA_UNUSED_NEXT] = prev;
  615. YSI_g_sUnusedAreas = area;
  616. }
  617. default:
  618. {
  619. // Only one block used - easy.
  620. YSI_g_sAreas[area][E_AREA_UNUSED_NEXT] = YSI_g_sUnusedAreas;
  621. YSI_g_sUnusedAreas = area;
  622. ++YSI_g_sFreeCount;
  623. }
  624. }
  625. // Remove this one from the zone it was in.
  626. Area_DoRemove(_:(flags & e_AREA_FLAGS_ZONE), area, _:(flags & e_AREA_FLAGS_NEXT));
  627. //return 1;
  628. }
  629. /**--------------------------------------------------------------------------**\
  630. <summary>Area_DoRemove</summary>
  631. <param name="zone">The zone this area is in (right shifted 18).</param>
  632. <param name="start">The first of the allocated areas being removed.</param>
  633. <param name="end">The area in this zone after the last removed one.</param>
  634. <returns>
  635. -
  636. </returns>
  637. <remarks>
  638. Very tightly integrated with "Area_Delete", to the point where I could just
  639. make them one function if I wanted (but I won't).
  640. </remarks>
  641. \**--------------------------------------------------------------------------**/
  642. static stock Area_DoRemove(zone, area, next)
  643. {
  644. //printf("remove %d %d %d %d", zone << 18, area, next, (e_AREA_ZONE_I_ << 18 & _:e_AREA_FLAGS_ZONE));
  645. // Actually "zone << 18", but that's not important.
  646. switch (zone)
  647. {
  648. case (e_AREA_ZONE_I_N << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneIN, area, next);}
  649. case (e_AREA_ZONE_I_NE << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneINE, area, next);}
  650. case (e_AREA_ZONE_I_E << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneIE, area, next);}
  651. case (e_AREA_ZONE_I_SE << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneISE, area, next);}
  652. case (e_AREA_ZONE_I_S << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneIS, area, next);}
  653. case (e_AREA_ZONE_I_SW << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneISW, area, next);}
  654. case (e_AREA_ZONE_I_W << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneIW, area, next);}
  655. case (e_AREA_ZONE_I_NW << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneINW, area, next);}
  656. case (e_AREA_ZONE_I_ << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneI, area, next);}
  657. // "Outer" zones. -4000 > x, y > 4000;
  658. case (e_AREA_ZONE_O_N << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneON, area, next);}
  659. case (e_AREA_ZONE_O_NE << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneONE, area, next);}
  660. case (e_AREA_ZONE_O_E << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneOE, area, next);}
  661. case (e_AREA_ZONE_O_SE << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneOSE, area, next);}
  662. case (e_AREA_ZONE_O_S << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneOS, area, next);}
  663. case (e_AREA_ZONE_O_SW << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneOSW, area, next);}
  664. case (e_AREA_ZONE_O_W << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneOW, area, next);}
  665. case (e_AREA_ZONE_O_NW << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneONW, area, next);}
  666. //e_AREA_ZONE_O_, // Can't be in all of O, but none of I.
  667. // "Extra" zones. In a quadrant, but spanning the +-4000 boundary.
  668. case (e_AREA_ZONE_X_N << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneXN, area, next);}
  669. case (e_AREA_ZONE_X_NE << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneXNE, area, next);}
  670. case (e_AREA_ZONE_X_E << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneXE, area, next);}
  671. case (e_AREA_ZONE_X_SE << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneXSE, area, next);}
  672. case (e_AREA_ZONE_X_S << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneXS, area, next);}
  673. case (e_AREA_ZONE_X_SW << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneXSW, area, next);}
  674. case (e_AREA_ZONE_X_W << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneXW, area, next);}
  675. case (e_AREA_ZONE_X_NW << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneXNW, area, next);}
  676. // Extra zones. For when areas are too big for a quadrant.
  677. case (e_AREA_ZONE_X_ << 18 & _:e_AREA_FLAGS_ZONE): {_AREAS_DO_REMOVE(YSI_g_sZoneX, area, next);}
  678. default: {_AREAS_DO_REMOVE(YSI_g_sZones[zone >> 18], area, next);}
  679. }
  680. return 0;
  681. }
  682. /**--------------------------------------------------------------------------**\
  683. <summary>Area_GetZones</summary>
  684. <param name="Float:x">Start of an area.</param>
  685. <param name="Float:y">Start of an area.</param>
  686. <returns>
  687. All the zones this position overlaps.
  688. </returns>
  689. <remarks>
  690. The most zones you can be in at once is 9 - I_, I_S, I_E, I_SE, X_, X_S,
  691. X_E, X_SE, Z_ (or similar corners - NOT in O_ though).
  692. </remarks>
  693. \**--------------------------------------------------------------------------**/
  694. static stock Area_GetZones(Float:x, Float:y)
  695. {
  696. // Always end with e_AREA_ZONE_X_ as they will ALWAYS be in that one (that's
  697. // basically everywhere and means the area exists somewhere ever). Once
  698. // that zone is detected, stop next loop.
  699. static const
  700. scZones[16][9] =
  701. {
  702. {e_AREA_ZONE_O_N , e_AREA_ZONE_O_NE, e_AREA_ZONE_O_E , e_AREA_ZONE_X_N , e_AREA_ZONE_X_NE, e_AREA_ZONE_X_E , e_AREA_ZONE_X_ , e_AREA_ZONE_NONE, e_AREA_ZONE_NONE},
  703. {e_AREA_ZONE_O_NE, e_AREA_ZONE_O_E , e_AREA_ZONE_X_N , e_AREA_ZONE_X_NE, e_AREA_ZONE_X_E , e_AREA_ZONE_X_ , e_AREA_ZONE_NONE, e_AREA_ZONE_NONE, e_AREA_ZONE_NONE},
  704. {e_AREA_ZONE_O_SE, e_AREA_ZONE_O_E , e_AREA_ZONE_X_S , e_AREA_ZONE_X_SE, e_AREA_ZONE_X_E , e_AREA_ZONE_X_ , e_AREA_ZONE_NONE, e_AREA_ZONE_NONE, e_AREA_ZONE_NONE},
  705. {e_AREA_ZONE_O_S , e_AREA_ZONE_O_SE, e_AREA_ZONE_O_E , e_AREA_ZONE_X_S , e_AREA_ZONE_X_SE, e_AREA_ZONE_X_E , e_AREA_ZONE_X_ , e_AREA_ZONE_NONE, e_AREA_ZONE_NONE},
  706. {e_AREA_ZONE_O_N , e_AREA_ZONE_O_NE, e_AREA_ZONE_X_N , e_AREA_ZONE_X_NE, e_AREA_ZONE_X_E , e_AREA_ZONE_X_ , e_AREA_ZONE_NONE, e_AREA_ZONE_NONE, e_AREA_ZONE_NONE},
  707. {e_AREA_ZONE_NONE, e_AREA_ZONE_I_N , e_AREA_ZONE_I_NE, e_AREA_ZONE_I_E , e_AREA_ZONE_I_ , e_AREA_ZONE_X_N , e_AREA_ZONE_X_NE, e_AREA_ZONE_X_E , e_AREA_ZONE_X_ },
  708. {e_AREA_ZONE_NONE, e_AREA_ZONE_I_S , e_AREA_ZONE_I_SE, e_AREA_ZONE_I_E , e_AREA_ZONE_I_ , e_AREA_ZONE_X_S , e_AREA_ZONE_X_SE, e_AREA_ZONE_X_E , e_AREA_ZONE_X_ },
  709. {e_AREA_ZONE_O_S , e_AREA_ZONE_O_SE, e_AREA_ZONE_X_S , e_AREA_ZONE_X_SE, e_AREA_ZONE_X_E , e_AREA_ZONE_X_ , e_AREA_ZONE_NONE, e_AREA_ZONE_NONE, e_AREA_ZONE_NONE},
  710. {e_AREA_ZONE_O_N , e_AREA_ZONE_O_NW, e_AREA_ZONE_X_N , e_AREA_ZONE_X_NW, e_AREA_ZONE_X_W , e_AREA_ZONE_X_ , e_AREA_ZONE_NONE, e_AREA_ZONE_NONE, e_AREA_ZONE_NONE},
  711. {e_AREA_ZONE_NONE, e_AREA_ZONE_I_N , e_AREA_ZONE_I_NW, e_AREA_ZONE_I_W , e_AREA_ZONE_I_ , e_AREA_ZONE_X_N , e_AREA_ZONE_X_NW, e_AREA_ZONE_X_W , e_AREA_ZONE_X_ },
  712. {e_AREA_ZONE_NONE, e_AREA_ZONE_I_S , e_AREA_ZONE_I_SW, e_AREA_ZONE_I_W , e_AREA_ZONE_I_ , e_AREA_ZONE_X_S , e_AREA_ZONE_X_SW, e_AREA_ZONE_X_W , e_AREA_ZONE_X_ },
  713. {e_AREA_ZONE_O_S , e_AREA_ZONE_O_SW, e_AREA_ZONE_X_S , e_AREA_ZONE_X_SW, e_AREA_ZONE_X_W , e_AREA_ZONE_X_ , e_AREA_ZONE_NONE, e_AREA_ZONE_NONE, e_AREA_ZONE_NONE},
  714. {e_AREA_ZONE_O_N , e_AREA_ZONE_O_NW, e_AREA_ZONE_O_W , e_AREA_ZONE_X_N , e_AREA_ZONE_X_NW, e_AREA_ZONE_X_W , e_AREA_ZONE_X_ , e_AREA_ZONE_NONE, e_AREA_ZONE_NONE},
  715. {e_AREA_ZONE_O_NW, e_AREA_ZONE_O_W , e_AREA_ZONE_X_N , e_AREA_ZONE_X_NW, e_AREA_ZONE_X_W , e_AREA_ZONE_X_ , e_AREA_ZONE_NONE, e_AREA_ZONE_NONE, e_AREA_ZONE_NONE},
  716. {e_AREA_ZONE_O_SW, e_AREA_ZONE_O_W , e_AREA_ZONE_X_S , e_AREA_ZONE_X_SW, e_AREA_ZONE_X_W , e_AREA_ZONE_X_ , e_AREA_ZONE_NONE, e_AREA_ZONE_NONE, e_AREA_ZONE_NONE},
  717. {e_AREA_ZONE_O_S , e_AREA_ZONE_O_SW, e_AREA_ZONE_O_W , e_AREA_ZONE_X_S , e_AREA_ZONE_X_SW, e_AREA_ZONE_X_W , e_AREA_ZONE_X_ , e_AREA_ZONE_NONE, e_AREA_ZONE_NONE}
  718. };
  719. if (AREAS_ZONE_BOUNDS.0 <= x)
  720. {
  721. if (AREAS_ZONE_BOUNDS.0 <= y) return scZones[ 0];
  722. else if (0.0 <= y) return scZones[ 1];
  723. else if (-AREAS_ZONE_BOUNDS.0 <= y) return scZones[ 2];
  724. else return scZones[ 3];
  725. }
  726. else if (0.0 <= x)
  727. {
  728. if (AREAS_ZONE_BOUNDS.0 <= y) return scZones[ 4];
  729. else if (0.0 <= y) return scZones[ 5]; //, ret[0] = _AREAS_MAKE_ZONE(Area_MakeZone(x), Area_MakeZone(y)));
  730. else if (-AREAS_ZONE_BOUNDS.0 <= y) return scZones[ 6]; //, ret[0] = _AREAS_MAKE_ZONE(Area_MakeZone(x), Area_MakeZone(y)));
  731. else return scZones[ 7];
  732. }
  733. else if (-AREAS_ZONE_BOUNDS.0 <= x)
  734. {
  735. if (AREAS_ZONE_BOUNDS.0 <= y) return scZones[ 8];
  736. else if (0.0 <= y) return scZones[ 9]; //, ret[0] = _AREAS_MAKE_ZONE(Area_MakeZone(x), Area_MakeZone(y)));
  737. else if (-AREAS_ZONE_BOUNDS.0 <= y) return scZones[10]; //, ret[0] = _AREAS_MAKE_ZONE(Area_MakeZone(x), Area_MakeZone(y)));
  738. else return scZones[11];
  739. }
  740. else
  741. {
  742. if (AREAS_ZONE_BOUNDS.0 <= y) return scZones[12];
  743. else if (0.0 <= y) return scZones[13];
  744. else if (-AREAS_ZONE_BOUNDS.0 <= y) return scZones[14];
  745. else return scZones[15];
  746. }
  747. }
  748. /**--------------------------------------------------------------------------**\
  749. <summary>Area_DetermineZone</summary>
  750. <param name="Float:minx">Start of an area.</param>
  751. <param name="Float:miny">Start of an area.</param>
  752. <param name="Float:maxx">End of an area.</param>
  753. <param name="Float:maxy">End of an area.</param>
  754. <param name="area">The area to add to the determined zone.</param>
  755. <param name="last">The last slot that makes up the current zone.</param>
  756. <returns>
  757. -
  758. </returns>
  759. <remarks>
  760. Finds the smallest zone that this area will fit in completely. Note that
  761. due to a limitation in the code, any zones with a node touching the upper
  762. edge of a zone will spill in to the next zone too. Only lower zone edges
  763. are inclusive.
  764. </remarks>
  765. \**--------------------------------------------------------------------------**/
  766. static stock Area_DetermineZone(Float:minx, Float:miny, Float:maxx, Float:maxy, area, last)
  767. {
  768. new
  769. zx,
  770. zy;
  771. // This optimises based on the fact that (by definition) maxx can't be lower
  772. // than minx and maxy can't be lower than miny, meaning we can skip certain
  773. // checks in some cases.
  774. if (AREAS_ZONE_BOUNDS.0 <= minx) // Western edge.
  775. {
  776. if (0.0 <= miny) // Southern edge.
  777. {
  778. _AREAS_ZONES_O(NE);
  779. //YSI_g_sAreas[last][E_AREA_FLAGS]=(YSI_g_sAreas[last][E_AREA_FLAGS]&e_AREA_FLAGS_REST)|(e_AREA_FLAGS:YSI_g_sZoneXNE&e_AREA_FLAGS_NEXT|e_AREA_FLAGS(e_AREA_ZONE_X_NE<<18)&e_AREA_FLAGS_ZONE),YSI_g_sZoneXNE=area;
  780. //return _AREAS_ADD_TO_ZONE(YSI_g_sZoneXNE,area,last,e_AREA_ZONE_X_NE)
  781. }
  782. else // Southern edge.
  783. {
  784. if (0.0 <= maxy) // Northern edge.
  785. {
  786. _AREAS_ZONES_O(E);
  787. }
  788. else // Northern edge.
  789. {
  790. _AREAS_ZONES_O(SE);
  791. }
  792. }
  793. }
  794. else if (0.0 <= minx) // Western edge.
  795. {
  796. if (AREAS_ZONE_BOUNDS.0 <= miny) // Southern edge.
  797. {
  798. _AREAS_ZONES_O(NE);
  799. }
  800. else if (0.0 <= miny) // Southern edge.
  801. {
  802. if (AREAS_ZONE_BOUNDS.0 <= maxx) // Eastern edge.
  803. {
  804. _AREAS_ZONES_X(NE);
  805. }
  806. else // Eastern edge.
  807. {
  808. // Interesting case - y > 0, 0 <= x < 4000.
  809. if (AREAS_ZONE_BOUNDS.0 <= maxy) // Northern edge.
  810. {
  811. _AREAS_ZONES_X(NE);
  812. }
  813. else // Northern edge.
  814. {
  815. //_AREAS_ZONES_I_GRID;
  816. zx = Area_MakeZone(minx);
  817. zy = Area_MakeZone(miny);
  818. if (zx == Area_MakeZone(maxx) && zy == Area_MakeZone(maxy))
  819. {
  820. zy += ((zx + AREAS_ZONE_PARTS) * AREAS_ZONE_ARRAY) + AREAS_ZONE_PARTS;
  821. YSI_g_sAreas[last][E_AREA_FLAGS] =
  822. (YSI_g_sAreas[last][E_AREA_FLAGS] & e_AREA_FLAGS_REST) |
  823. (e_AREA_FLAGS:YSI_g_sZones[zy] & e_AREA_FLAGS_NEXT) |
  824. (e_AREA_FLAGS:(zy << 18) & e_AREA_FLAGS_ZONE);
  825. P:7("Area_DetermineZone: Added to North-Eastern square");
  826. return YSI_g_sZones[zy] = area;
  827. }
  828. _AREAS_ZONES_I(NE);
  829. }
  830. }
  831. }
  832. else if (-AREAS_ZONE_BOUNDS.0 <= miny) // Southern edge.
  833. {
  834. if (AREAS_ZONE_BOUNDS.0 <= maxx) // Eastern edge.
  835. {
  836. if (0.0 <= maxy) // Northern edge.
  837. {
  838. _AREAS_ZONES_X(E);
  839. }
  840. else // Northern edge.
  841. {
  842. _AREAS_ZONES_X(SE);
  843. }
  844. }
  845. else // Eastern edge.
  846. {
  847. if (AREAS_ZONE_BOUNDS.0 <= maxy) // Northern edge.
  848. {
  849. _AREAS_ZONES_X(E);
  850. }
  851. if (0.0 <= maxy) // Northern edge.
  852. {
  853. _AREAS_ZONES_I(E);
  854. }
  855. else // Northern edge.
  856. {
  857. zx = Area_MakeZone(minx);
  858. zy = Area_MakeZone(miny);
  859. if (zx == Area_MakeZone(maxx) && zy == Area_MakeZone(maxy))
  860. {
  861. zy += ((zx + AREAS_ZONE_PARTS) * AREAS_ZONE_ARRAY) + AREAS_ZONE_PARTS;
  862. YSI_g_sAreas[last][E_AREA_FLAGS] =
  863. (YSI_g_sAreas[last][E_AREA_FLAGS] & e_AREA_FLAGS_REST) |
  864. (e_AREA_FLAGS:YSI_g_sZones[zy] & e_AREA_FLAGS_NEXT) |
  865. (e_AREA_FLAGS:(zy << 18) & e_AREA_FLAGS_ZONE);
  866. return YSI_g_sZones[zy] = area;
  867. }
  868. _AREAS_ZONES_I(SE);
  869. }
  870. }
  871. }
  872. else // Southern edge.
  873. {
  874. if (0.0 <= maxy) // Northern edge.
  875. {
  876. _AREAS_ZONES_X(E);
  877. }
  878. if (-AREAS_ZONE_BOUNDS.0 <= maxy) // Northern edge.
  879. {
  880. _AREAS_ZONES_X(SE);
  881. }
  882. else // Northern edge.
  883. {
  884. _AREAS_ZONES_O(SE);
  885. }
  886. }
  887. }
  888. else if (-AREAS_ZONE_BOUNDS.0 <= minx) // Western edge.
  889. {
  890. if (AREAS_ZONE_BOUNDS.0 <= miny) // Southern edge.
  891. {
  892. if (0.0 <= maxx) // Eastern edge.
  893. {
  894. _AREAS_ZONES_O(N);
  895. }
  896. else // Eastern edge.
  897. {
  898. _AREAS_ZONES_O(NW);
  899. }
  900. }
  901. else if (0.0 <= miny) // Southern edge.
  902. {
  903. if (AREAS_ZONE_BOUNDS.0 <= maxx) // Eastern edge.
  904. {
  905. _AREAS_ZONES_X(N);
  906. }
  907. else if (0.0 <= maxx) // Eastern edge.
  908. {
  909. if (AREAS_ZONE_BOUNDS.0 <= maxy) // Northern edge.
  910. {
  911. _AREAS_ZONES_X(N);
  912. }
  913. else // Northern edge.
  914. {
  915. _AREAS_ZONES_I(N);
  916. }
  917. }
  918. else // Eastern edge.
  919. {
  920. if (AREAS_ZONE_BOUNDS.0 <= maxy) // Northern edge.
  921. {
  922. _AREAS_ZONES_X(NW);
  923. }
  924. else // Northern edge.
  925. {
  926. zx = Area_MakeZone(minx);
  927. zy = Area_MakeZone(miny);
  928. if (zx == Area_MakeZone(maxx) && zy == Area_MakeZone(maxy))
  929. {
  930. zy += ((zx + AREAS_ZONE_PARTS) * AREAS_ZONE_ARRAY) + AREAS_ZONE_PARTS;
  931. YSI_g_sAreas[last][E_AREA_FLAGS] =
  932. (YSI_g_sAreas[last][E_AREA_FLAGS] & e_AREA_FLAGS_REST) |
  933. (e_AREA_FLAGS:YSI_g_sZones[zy] & e_AREA_FLAGS_NEXT) |
  934. (e_AREA_FLAGS:(zy << 18) & e_AREA_FLAGS_ZONE);
  935. return YSI_g_sZones[zy] = area;
  936. }
  937. _AREAS_ZONES_I(NW);
  938. }
  939. }
  940. }
  941. else if (-AREAS_ZONE_BOUNDS.0 <= miny) // Southern edge.
  942. {
  943. if (AREAS_ZONE_BOUNDS.0 <= maxx) // Eastern edge.
  944. {
  945. if (0.0 <= maxy) // Northern edge.
  946. {
  947. _AREAS_ZONES_X();
  948. }
  949. else // Northern edge.
  950. {
  951. _AREAS_ZONES_X(S);
  952. }
  953. }
  954. else if (0.0 <= maxx) // Eastern edge.
  955. {
  956. if (AREAS_ZONE_BOUNDS.0 <= maxy) // Northern edge.
  957. {
  958. _AREAS_ZONES_X();
  959. }
  960. else if (0.0 <= maxy) // Northern edge.
  961. {
  962. _AREAS_ZONES_I();
  963. }
  964. else // Northern edge.
  965. {
  966. _AREAS_ZONES_I(S);
  967. }
  968. }
  969. else // Eastern edge.
  970. {
  971. if (AREAS_ZONE_BOUNDS.0 <= maxy) // Northern edge.
  972. {
  973. _AREAS_ZONES_X(W);
  974. }
  975. else if (0.0 <= maxy) // Northern edge.
  976. {
  977. _AREAS_ZONES_I(W);
  978. }
  979. else // Northern edge.
  980. {
  981. zx = Area_MakeZone(minx);
  982. zy = Area_MakeZone(miny);
  983. if (zx == Area_MakeZone(maxx) && zy == Area_MakeZone(maxy))
  984. {
  985. zy += ((zx + AREAS_ZONE_PARTS) * AREAS_ZONE_ARRAY) + AREAS_ZONE_PARTS;
  986. YSI_g_sAreas[last][E_AREA_FLAGS] =
  987. (YSI_g_sAreas[last][E_AREA_FLAGS] & e_AREA_FLAGS_REST) |
  988. (e_AREA_FLAGS:YSI_g_sZones[zy] & e_AREA_FLAGS_NEXT) |
  989. (e_AREA_FLAGS:(zy << 18) & e_AREA_FLAGS_ZONE);
  990. return YSI_g_sZones[zy] = area;
  991. }
  992. _AREAS_ZONES_I(SW);
  993. }
  994. }
  995. }
  996. else // Southern edge.
  997. {
  998. if (0.0 <= maxx) // Eastern edge.
  999. {
  1000. if (-AREAS_ZONE_BOUNDS.0 <= maxy) // Northern edge.
  1001. {
  1002. _AREAS_ZONES_X(S);
  1003. }
  1004. else // Northern edge.
  1005. {
  1006. _AREAS_ZONES_O(S);
  1007. }
  1008. }
  1009. else // Eastern edge.
  1010. {
  1011. if (-AREAS_ZONE_BOUNDS.0 <= maxy) // Northern edge.
  1012. {
  1013. _AREAS_ZONES_X(SW);
  1014. }
  1015. else // Northern edge.
  1016. {
  1017. _AREAS_ZONES_O(SW);
  1018. }
  1019. }
  1020. }
  1021. }
  1022. else // Western edge.
  1023. {
  1024. if (AREAS_ZONE_BOUNDS.0 <= miny) // Southern edge.
  1025. {
  1026. if (0.0 <= maxx) // Eastern edge.
  1027. {
  1028. _AREAS_ZONES_O(N);
  1029. }
  1030. else // Eastern edge.
  1031. {
  1032. // I missed this one and the compiler didn't complain about the
  1033. // path not returning - I think I confused it!
  1034. _AREAS_ZONES_O(NW);
  1035. }
  1036. }
  1037. else if (0.0 <= miny) // Southern edge.
  1038. {
  1039. if (0.0 <= maxx) // Eastern edge.
  1040. {
  1041. _AREAS_ZONES_X(N);
  1042. }
  1043. else if (-AREAS_ZONE_BOUNDS.0 <= maxx) // Eastern edge.
  1044. {
  1045. _AREAS_ZONES_X(NW);
  1046. }
  1047. else // Eastern edge.
  1048. {
  1049. _AREAS_ZONES_O(NW);
  1050. }
  1051. }
  1052. else if (-AREAS_ZONE_BOUNDS.0 <= miny) // Southern edge.
  1053. {
  1054. if (0.0 <= maxx) // Eastern edge.
  1055. {
  1056. if (0.0 <= maxy) // Northern edge.
  1057. {
  1058. _AREAS_ZONES_X();
  1059. }
  1060. else
  1061. {
  1062. _AREAS_ZONES_X(S);
  1063. }
  1064. }
  1065. else if (-AREAS_ZONE_BOUNDS.0 <= maxx) // Eastern edge.
  1066. {
  1067. if (0.0 <= maxy) // Northern edge.
  1068. {
  1069. _AREAS_ZONES_X(W);
  1070. }
  1071. else // Northern edge.
  1072. {
  1073. _AREAS_ZONES_X(SW);
  1074. }
  1075. }
  1076. else // Eastern edge.
  1077. {
  1078. if (0.0 <= maxy) // Northern edge.
  1079. {
  1080. _AREAS_ZONES_O(W);
  1081. }
  1082. else // Northern edge.
  1083. {
  1084. _AREAS_ZONES_O(SW);
  1085. }
  1086. }
  1087. }
  1088. else // Southern edge.
  1089. {
  1090. if (0.0 <= maxx) // Eastern edge.
  1091. {
  1092. if (0.0 <= maxy) // Northern edge.
  1093. {
  1094. _AREAS_ZONES_X();
  1095. }
  1096. else if (-AREAS_ZONE_BOUNDS.0 <= maxy) // Northern edge.
  1097. {
  1098. _AREAS_ZONES_X(S);
  1099. }
  1100. else // Northern edge.
  1101. {
  1102. _AREAS_ZONES_O(S);
  1103. }
  1104. }
  1105. else if (-AREAS_ZONE_BOUNDS.0 <= maxx) // Eastern edge.
  1106. {
  1107. if (0.0 <= maxy) // Northern edge.
  1108. {
  1109. _AREAS_ZONES_X(W);
  1110. }
  1111. else if (-AREAS_ZONE_BOUNDS.0 <= maxy) // Northern edge.
  1112. {
  1113. _AREAS_ZONES_X(SW);
  1114. }
  1115. else // Northern edge.
  1116. {
  1117. _AREAS_ZONES_O(SW);
  1118. }
  1119. }
  1120. else // Eastern edge.
  1121. {
  1122. if (0.0 <= maxy) // Northern edge.
  1123. {
  1124. _AREAS_ZONES_O(W);
  1125. }
  1126. else // Northern edge.
  1127. {
  1128. _AREAS_ZONES_O(SW);
  1129. }
  1130. }
  1131. }
  1132. }
  1133. }
  1134. /**--------------------------------------------------------------------------**\
  1135. <summary>Area_AddTo</summary>
  1136. <param name="area">Area to set for.</param>
  1137. <param name="set">Wether or not this area is usable in all worlds.</param>
  1138. <returns>
  1139. -
  1140. </returns>
  1141. <remarks>
  1142. -
  1143. </remarks>
  1144. \**--------------------------------------------------------------------------**/
  1145. /**--------------------------------------------------------------------------**\
  1146. <summary>Area_SetAllWorlds</summary>
  1147. <param name="area">Area to set for.</param>
  1148. <param name="set">Wether or not this area is usable in all worlds.</param>
  1149. <returns>
  1150. -
  1151. </returns>
  1152. <remarks>
  1153. -
  1154. </remarks>
  1155. \**--------------------------------------------------------------------------**/
  1156. foreign void:Area_SetAllWorlds(area, bool:set);
  1157. global void:Area_SetAllWorlds(area, bool:set)
  1158. {
  1159. #if AREA_WORLDS > 0
  1160. if (Area_IsActive(area))
  1161. {
  1162. Bit_SetAll(YSI_g_sAreas[area][E_AREA_WORLDS], set, bits<AREA_WORLDS>);
  1163. //return 1;
  1164. }
  1165. #else
  1166. #pragma unused area, set
  1167. #endif
  1168. //return 0;
  1169. }
  1170. /**--------------------------------------------------------------------------**\
  1171. <summary>Area_Add</summary>
  1172. <param name="type">Type of area.</param>
  1173. <param name="...">Points.</param>
  1174. <returns>
  1175. -
  1176. </returns>
  1177. <remarks>
  1178. Types:
  1179. CUBE, Cube
  1180. CUBOID, Cuboid
  1181. BOX, Box
  1182. CIRCLE, Circle
  1183. OVAL, Oval
  1184. OVOID, Ovoid
  1185. SPHERE, Sphere
  1186. POLY, Poly
  1187. </remarks>
  1188. \**--------------------------------------------------------------------------**/
  1189. #define Area_Add(%0,%1) Area_Add%0(%1)
  1190. #define Area_AddCUBE Area_AddCube
  1191. #define Area_AddCUBOID Area_AddCuboid
  1192. #define Area_AddBOX Area_AddBox
  1193. #define Area_AddCIRCLE Area_AddCircle
  1194. #define Area_AddOVAL Area_AddOval
  1195. #define Area_AddOVOID Area_AddOvoid
  1196. #define Area_AddSPHERE Area_AddSphere
  1197. #define Area_AddPOLY Area_AddPoly
  1198. /**--------------------------------------------------------------------------**\
  1199. <summary>Area_AddCube</summary>
  1200. <param name="Float:minx">Lowest X corner of box</param>
  1201. <param name="Float:miny">Lowest Y corner of box.</param>
  1202. <param name="Float:minx">Lowest Z corner of box.</param>
  1203. <param name="Float:maxx">Highest X corner of box.</param>
  1204. <param name="Float:maxy">Highest Y corner of box.</param>
  1205. <param name="Float:maxz">Highest Z corner of box.</param>
  1206. <returns>
  1207. Area slot or NO_AREA
  1208. </returns>
  1209. <remarks>
  1210. -
  1211. </remarks>
  1212. \**--------------------------------------------------------------------------**/
  1213. #define _AREA_MIN(%0,%1) if (%1 < %0) %0 ^= %1, %1 ^= %0, %0 ^= %1
  1214. @foreign Area_AddCuboid(Float:x0,Float:y0,Float:z0,Float:x1,Float:y1,Float:z1);
  1215. @global Area_AddCuboid(Float:x0,Float:y0,Float:z0,Float:x1,Float:y1,Float:z1)
  1216. {
  1217. new
  1218. slot = Area_GetFreeSlot();
  1219. if (slot == NO_AREA) return NO_AREA;
  1220. _AREA_MIN(x0, x1);
  1221. _AREA_MIN(y0, y1);
  1222. _AREA_MIN(z0, z1);
  1223. YSI_g_sAreas[slot][E_AREA_FLAGS] = e_AREA_FLAGS_CUBE | e_AREA_FLAGS_ACTIVE;
  1224. YSI_g_sAreas[slot][E_AREA_POS][0] = x0;
  1225. YSI_g_sAreas[slot][E_AREA_POS][1] = y0;
  1226. YSI_g_sAreas[slot][E_AREA_POS][2] = z0;
  1227. YSI_g_sAreas[slot][E_AREA_POS][3] = x1;
  1228. YSI_g_sAreas[slot][E_AREA_POS][4] = y1;
  1229. YSI_g_sAreas[slot][E_AREA_POS][5] = z1;
  1230. PA_FastInit(YSI_g_sAreas[slot][E_AREA_PLAYERS]);
  1231. NO_GROUPS(slot)
  1232. {
  1233. PA_Init(YSI_g_sAreas[slot][E_AREA_PLAYERS], true);
  1234. }
  1235. #if YSIM_HAS_MASTER
  1236. YSI_g_sAreas[slot][E_AREA_MASTER] = Master_Caller();
  1237. #endif
  1238. Area_SetAllWorlds(slot, true);
  1239. Area_DetermineZone(x0, y0, x1, y1, slot, slot);
  1240. /*#if defined AREA_VERY_FAST
  1241. new
  1242. rx = (x1 - z0) / 2.0,
  1243. ry = (y1 - y0) / 2.0,
  1244. rz = (z1 - z0) / 2.0;
  1245. YSI_g_sAreas[slot][E_AREA_BOUNDING][0] = x0 + rx;
  1246. YSI_g_sAreas[slot][E_AREA_BOUNDING][1] = y0 + ry;
  1247. YSI_g_sAreas[slot][E_AREA_BOUNDING][2] = z0 + rz;
  1248. if (rx > ry && rx > rz)
  1249. {
  1250. YSI_g_sAreas[slot][E_AREA_BOUNDING][3] = rx * rx;
  1251. }
  1252. else if (ry > rz)
  1253. {
  1254. YSI_g_sAreas[slot][E_AREA_BOUNDING][3] = ry * ry;
  1255. }
  1256. else
  1257. {
  1258. YSI_g_sAreas[slot][E_AREA_BOUNDING][3] = rz * rz;
  1259. }
  1260. #endif*/
  1261. return slot;
  1262. }
  1263. #undef _AREA_MIN
  1264. #define Area_AddCube Area_AddCuboid
  1265. /**--------------------------------------------------------------------------**\
  1266. <summary>Area_AddBox</summary>
  1267. <param name="Float:minx">Lowest X corner of box</param>
  1268. <param name="Float:miny">Lowest Y corner of box.</param>
  1269. <param name="Float:maxx">Highest X corner of box.</param>
  1270. <param name="Float:maxy">Highest Y corner of box.</param>
  1271. <returns>
  1272. Area slot or NO_AREA
  1273. </returns>
  1274. <remarks>
  1275. -
  1276. </remarks>
  1277. \**--------------------------------------------------------------------------**/
  1278. stock Area_AddBox(Float:minx, Float:miny, Float:maxx, Float:maxy)
  1279. {
  1280. return Area_AddCuboid(minx, miny, FLOAT_NEGATIVE_INFINITY, maxx, maxy, FLOAT_INFINITY);
  1281. }
  1282. /*
  1283. @foreign Area_AddBox(Float:minx, Float:miny, Float:maxx, Float:maxy);
  1284. @global Area_AddBox(Float:minx, Float:miny, Float:maxx, Float:maxy)
  1285. {
  1286. new
  1287. slot;
  1288. if (minx > maxx)
  1289. {
  1290. slot = _:minx;
  1291. minx = maxx;
  1292. maxx = Float:slot;
  1293. }
  1294. if (miny > maxy)
  1295. {
  1296. slot = _:miny;
  1297. miny = maxy;
  1298. maxy = Float:slot;
  1299. }
  1300. slot = Area_GetFreeSlot();
  1301. if (slot == NO_AREA) return NO_AREA;
  1302. YSI_g_sAreas[slot][E_AREA_FLAGS] = SINGLE_SLOT_AREA | e_AREA_FLAGS_BOX | e_AREA_FLAGS_USED | e_AREA_FLAGS_ACTIVE | e_AREA_FLAGS:(1 << 18);
  1303. YSI_g_sAreas[slot][E_AREA_POS][0] = minx;
  1304. YSI_g_sAreas[slot][E_AREA_POS][1] = miny;
  1305. YSI_g_sAreas[slot][E_AREA_POS][2] = maxx;
  1306. YSI_g_sAreas[slot][E_AREA_POS][3] = maxy;
  1307. PA_FastInit(YSI_g_sAreas[slot][E_AREA_PLAYERS]);
  1308. NO_GROUPS(slot)
  1309. {
  1310. PA_Init(YSI_g_sAreas[slot][E_AREA_PLAYERS], true);
  1311. }
  1312. #if YSIM_HAS_MASTER
  1313. YSI_g_sAreas[slot][E_AREA_MASTER] = Master_Caller();
  1314. #endif
  1315. Area_SetAllWorlds(slot, true);
  1316. Area_DetermineZone(minx, miny, maxx, maxy, slot, slot);
  1317. return slot;
  1318. }
  1319. */
  1320. /**--------------------------------------------------------------------------**\
  1321. <summary>Area_AddCircle</summary>
  1322. <param name="Float:x">X position of circle.</param>
  1323. <param name="Float:y">Y position of circle.</param>
  1324. <param name="Float:r">Radius of circle.</param>
  1325. <param name="Float:height">Ceiling of circle.</param>
  1326. <param name="Float:depth">Bottom of circle.</param>
  1327. <returns>
  1328. Area slot or NO_AREA
  1329. </returns>
  1330. <remarks>
  1331. Technically a cylinder, no lower bound (ceiling added cos there was a
  1332. spare slot in the 4 float design which may as well have been used).
  1333. </remarks>
  1334. \**--------------------------------------------------------------------------**/
  1335. stock Area_AddCircle(Float:x, Float:y, Float:r, Float:height = FLOAT_INFINITY)
  1336. {
  1337. return Area_AddOval(x, y, r, r, height, FLOAT_NEGATIVE_INFINITY);
  1338. }
  1339. /**--------------------------------------------------------------------------**\
  1340. <summary>Area_AddOval</summary>
  1341. <param name="Float:x">X position of circle.</param>
  1342. <param name="Float:y">Y position of circle.</param>
  1343. <param name="Float:rx">X Radius of oval.</param>
  1344. <param name="Float:ry">Y Radius of oval.</param>
  1345. <param name="Float:height">Ceiling of circle.</param>
  1346. <param name="Float:depth">Bottom of circle.</param>
  1347. <returns>
  1348. Area slot or NO_AREA
  1349. </returns>
  1350. <remarks>
  1351. Technically a cylinder, no lower bound (ceiling added cos there was a
  1352. spare slot in the 4 float design which may as well have been used).
  1353. </remarks>
  1354. \**--------------------------------------------------------------------------**/
  1355. @foreign Area_AddOval(Float:x,Float:y,Float:rx,Float:ry,Float:h,Float:d);
  1356. @global Area_AddOval(Float:x,Float:y,Float:rx,Float:ry,Float:h,Float:d)
  1357. {
  1358. new
  1359. slot = Area_GetFreeSlot();
  1360. if (slot == NO_AREA) return NO_AREA;
  1361. //printf("%f %f %f %f", x - r, y - r, x + r, y + r);
  1362. YSI_g_sAreas[slot][E_AREA_FLAGS] = e_AREA_FLAGS_ACTIVE;
  1363. YSI_g_sAreas[slot][E_AREA_POS][0] = x;
  1364. YSI_g_sAreas[slot][E_AREA_POS][1] = y;
  1365. YSI_g_sAreas[slot][E_AREA_POS][2] = rx * rx;
  1366. YSI_g_sAreas[slot][E_AREA_POS][3] = ry * ry;
  1367. YSI_g_sAreas[slot][E_AREA_POS][4] = h;
  1368. YSI_g_sAreas[slot][E_AREA_POS][5] = d;
  1369. PA_FastInit(YSI_g_sAreas[slot][E_AREA_PLAYERS]);
  1370. NO_GROUPS(slot)
  1371. {
  1372. PA_Init(YSI_g_sAreas[slot][E_AREA_PLAYERS], true);
  1373. }
  1374. #if YSIM_HAS_MASTER
  1375. YSI_g_sAreas[slot][E_AREA_MASTER] = Master_Caller();
  1376. #endif
  1377. Area_SetAllWorlds(slot, true);
  1378. Area_DetermineZone(x - rx, y - ry, x + rx, y + ry, slot, slot);
  1379. if (rx == ry && h == FLOAT_INFINITY && d == FLOAT_NEGATIVE_INFINITY)
  1380. {
  1381. YSI_g_sAreas[slot][E_AREA_FLAGS] |= e_AREA_FLAGS_CIRCLE;
  1382. }
  1383. else
  1384. {
  1385. YSI_g_sAreas[slot][E_AREA_FLAGS] |= e_AREA_FLAGS_OVAL;
  1386. }
  1387. // Bounding sphere. I'm not convinced this is needed on the simple shapes,
  1388. // I may only use it on polys and special-case
  1389. /*#if defined AREA_VERY_FAST
  1390. YSI_g_sAreas[slot][E_AREA_BOUNDING][0] = x;
  1391. YSI_g_sAreas[slot][E_AREA_BOUNDING][1] = y;
  1392. // Z radius of the bounding sphere (may be VERY huge).
  1393. new
  1394. rz = (h - d) / 2.0;
  1395. YSI_g_sAreas[slot][E_AREA_BOUNDING][2] = h - rz;
  1396. if (rx > ry && rx > rz)
  1397. {
  1398. YSI_g_sAreas[slot][E_AREA_BOUNDING][3] = rx * rx;
  1399. }
  1400. else if (ry > rz)
  1401. {
  1402. YSI_g_sAreas[slot][E_AREA_BOUNDING][3] = ry * ry;
  1403. }
  1404. else
  1405. {
  1406. YSI_g_sAreas[slot][E_AREA_BOUNDING][3] = rz * rz;
  1407. }
  1408. #endif*/
  1409. return slot;
  1410. }
  1411. /**--------------------------------------------------------------------------**\
  1412. <summary>Area_AddSphere</summary>
  1413. <param name="Float:x">X position of sphere.</param>
  1414. <param name="Float:y">Y position of sphere.</param>
  1415. <param name="Float:z">Z position of sphere.</param>
  1416. <param name="Float:r">Radius of sphere.</param>
  1417. <returns>
  1418. Area slot or NO_AREA
  1419. </returns>
  1420. <remarks>
  1421. -
  1422. </remarks>
  1423. \**--------------------------------------------------------------------------**/
  1424. stock Area_AddSphere(Float:x, Float:y, Float:z, Float:r)
  1425. {
  1426. return Area_AddOvoid(x, y, z, r, r, r);
  1427. }
  1428. /**--------------------------------------------------------------------------**\
  1429. <summary>Area_AddOvoid</summary>
  1430. <param name="Float:x">X position of sphere.</param>
  1431. <param name="Float:y">Y position of sphere.</param>
  1432. <param name="Float:z">Z position of sphere.</param>
  1433. <param name="Float:rx">X radius of ovoid.</param>
  1434. <param name="Float:ry">Y radius of ovoid.</param>
  1435. <param name="Float:rz">Z radius of ovoid.</param>
  1436. <returns>
  1437. Area slot or NO_AREA
  1438. </returns>
  1439. <remarks>
  1440. -
  1441. </remarks>
  1442. \**--------------------------------------------------------------------------**/
  1443. @foreign Area_AddOvoid(Float:x,Float:y,Float:z,Float:rx,Float:ry,Float:rz);
  1444. @global Area_AddOvoid(Float:x,Float:y,Float:z,Float:rx,Float:ry,Float:rz)
  1445. {
  1446. new
  1447. slot = Area_GetFreeSlot();
  1448. if (slot == NO_AREA) return NO_AREA;
  1449. YSI_g_sAreas[slot][E_AREA_FLAGS] = e_AREA_FLAGS_ACTIVE;
  1450. YSI_g_sAreas[slot][E_AREA_POS][0] = x;
  1451. YSI_g_sAreas[slot][E_AREA_POS][1] = y;
  1452. YSI_g_sAreas[slot][E_AREA_POS][2] = z;
  1453. YSI_g_sAreas[slot][E_AREA_POS][3] = rx * rx;
  1454. YSI_g_sAreas[slot][E_AREA_POS][4] = ry * ry;
  1455. YSI_g_sAreas[slot][E_AREA_POS][5] = rz * rz;
  1456. PA_FastInit(YSI_g_sAreas[slot][E_AREA_PLAYERS]);
  1457. NO_GROUPS(slot)
  1458. {
  1459. PA_Init(YSI_g_sAreas[slot][E_AREA_PLAYERS], true);
  1460. }
  1461. #if YSIM_HAS_MASTER
  1462. YSI_g_sAreas[slot][E_AREA_MASTER] = Master_Caller();
  1463. #endif
  1464. Area_SetAllWorlds(slot, true);
  1465. Area_DetermineZone(x - rx, y - ry, x + rx, y + ry, slot, slot);
  1466. // Create the bounding sphere for this zone. The player has to be inside
  1467. // the bounding sphere before they can be inside the oval. Of course, if
  1468. // the oval is also a sphere then these two checks are identical.
  1469. #if defined AREA_VERY_FAST
  1470. YSI_g_sAreas[slot][E_AREA_BOUNDING][0] = x;
  1471. YSI_g_sAreas[slot][E_AREA_BOUNDING][1] = y;
  1472. YSI_g_sAreas[slot][E_AREA_BOUNDING][2] = z;
  1473. if (rx > ry && rx > rz)
  1474. {
  1475. YSI_g_sAreas[slot][E_AREA_BOUNDING][3] = rx * rx;
  1476. }
  1477. else if (ry > rz)
  1478. {
  1479. YSI_g_sAreas[slot][E_AREA_BOUNDING][3] = ry * ry;
  1480. }
  1481. else
  1482. {
  1483. YSI_g_sAreas[slot][E_AREA_BOUNDING][3] = rz * rz;
  1484. }
  1485. if (rx == ry && rx == rz)
  1486. {
  1487. YSI_g_sAreas[slot][E_AREA_FLAGS] |= e_AREA_FLAGS_SPHERE;
  1488. }
  1489. else
  1490. #endif
  1491. {
  1492. // Create the zone bounds.
  1493. YSI_g_sAreas[slot][E_AREA_POS][0] = x;
  1494. YSI_g_sAreas[slot][E_AREA_POS][1] = y;
  1495. YSI_g_sAreas[slot][E_AREA_POS][2] = z;
  1496. YSI_g_sAreas[slot][E_AREA_POS][3] = rx * rx;
  1497. YSI_g_sAreas[slot][E_AREA_POS][4] = ry * ry;
  1498. YSI_g_sAreas[slot][E_AREA_POS][5] = rz * rz;
  1499. // Set this as not a sphere.
  1500. YSI_g_sAreas[slot][E_AREA_FLAGS] |= e_AREA_FLAGS_OVOID;
  1501. }
  1502. #pragma tabsize 4
  1503. return slot;
  1504. }
  1505. /**--------------------------------------------------------------------------**\
  1506. <summary>Area_AddPoly</summary>
  1507. <param name="Float:...">X/Ys of points</param>
  1508. <returns>
  1509. Area slot or NO_AREA
  1510. </returns>
  1511. <remarks>
  1512. Creates an irregular shape to detect players in. This is a 2d area made
  1513. up of a load of XY points. If an odd number of parameters is passed the
  1514. extra one is assumed to be a ceiling so you can ignore interiors or planes.
  1515. The height parameter goes first as it's easiest to check.
  1516. This is one of a few functions still written using the master system at a
  1517. low level (the other obvious one is "Class_Add").
  1518. </remarks>
  1519. \**--------------------------------------------------------------------------**/
  1520. #if YSIM_HAS_MASTER
  1521. #if _YSIM_IS_SERVER
  1522. forward Area_AddPoly(Float:x1, Float:y1, Float:x2, Float:y2, Float:x3, Float:y3, Float:...);
  1523. forward Area_AddPoly@(Float:points[], size, count);
  1524. stock Area_AddPoly(Float:x1, Float:y1, Float:x2, Float:y2, Float:x3, Float:y3, Float:...)
  1525. {
  1526. new
  1527. args = numargs();
  1528. if (args > 128)
  1529. {
  1530. return NO_AREA;
  1531. }
  1532. new
  1533. Float:points[128];
  1534. points[0] = x1;
  1535. points[1] = y1;
  1536. points[2] = x2;
  1537. points[3] = y2;
  1538. points[4] = x3;
  1539. points[5] = y3;
  1540. for (new i = 6; i != args; ++i)
  1541. {
  1542. points[i] = Float:getarg(i);
  1543. }
  1544. return Area_AddPoly@(points, sizeof (points), args);
  1545. }
  1546. public Area_AddPoly@(Float:points[], size, count)
  1547. #else
  1548. stock Area_AddPoly(Float:x1, Float:y1, Float:x2, Float:y2, Float:x3, Float:y3, Float:...) <>
  1549. {
  1550. new
  1551. args = numargs();
  1552. if (args > 128)
  1553. {
  1554. return NO_AREA;
  1555. }
  1556. new
  1557. Float:points[128];
  1558. points[0] = x1;
  1559. points[1] = y1;
  1560. points[2] = x2;
  1561. points[3] = y2;
  1562. points[4] = x3;
  1563. points[5] = y3;
  1564. for (new i = 6; i != args; ++i)
  1565. {
  1566. points[i] = Float:getarg(i);
  1567. }
  1568. new
  1569. p = getproperty(8, YSIM_CALLER);
  1570. setproperty(8, YSIM_CALLER, _@);
  1571. CallRemoteFunction("Area_AddPoly@", "aii", points, sizeof (points), args);
  1572. setproperty(8, YSIM_CALLER, p);
  1573. return getproperty(8, YSIM_RETURN);
  1574. }
  1575. forward Area_AddPoly@(Float:points[], size, count);
  1576. stock Area_AddPoly(Float:x1, Float:y1, Float:x2, Float:y2, Float:x3, Float:y3, Float:...) <_YCM : m>
  1577. {
  1578. #pragma unused x1, y1, x2, y2, x3, y3
  1579. return NO_AREA;
  1580. }
  1581. #if _YSIM_IS_CLIENT
  1582. static stock Area_AddPoly_(Float:x1, Float:y1, Float:x2, Float:y2, Float:x3, Float:y3, Float:...)
  1583. #else
  1584. #if _YSIM_IS_STUB
  1585. #error _YSIM_IS_STUB set in y_areas.
  1586. #else
  1587. public Area_AddPoly@(Float:points[], size, count) <>
  1588. {
  1589. return 0;
  1590. }
  1591. /*{
  1592. X@(_:@Zk:_YM@CP:%0(%1,,));
  1593. setproperty(8, YSIM_RETURN, n);
  1594. }*/
  1595. stock Area_AddPoly(Float:x1, Float:y1, Float:x2, Float:y2, Float:x3, Float:y3, Float:...) <_YCM : y>
  1596. {
  1597. new
  1598. args = numargs();
  1599. if (args > 128)
  1600. {
  1601. return NO_AREA;
  1602. }
  1603. new
  1604. Float:points[128];
  1605. points[0] = x1;
  1606. points[1] = y1;
  1607. points[2] = x2;
  1608. points[3] = y2;
  1609. points[4] = x3;
  1610. points[5] = y3;
  1611. for (new i = 6; i != args; ++i)
  1612. {
  1613. points[i] = Float:getarg(i);
  1614. }
  1615. return Area_AddPoly@(points, sizeof (points), args);
  1616. }
  1617. public Area_AddPoly@(Float:points[], size, count) <_YCM : y>
  1618. #endif
  1619. #endif
  1620. #endif
  1621. #else
  1622. stock Area_AddPoly(Float:x1, Float:y1, Float:x2, Float:y2, Float:x3, Float:y3, Float:...)
  1623. {
  1624. new
  1625. args = numargs();
  1626. if (args > 128)
  1627. {
  1628. return NO_AREA;
  1629. }
  1630. new
  1631. Float:points[128];
  1632. points[0] = x1;
  1633. points[1] = y1;
  1634. points[2] = x2;
  1635. points[3] = y2;
  1636. points[4] = x3;
  1637. points[5] = y3;
  1638. for (new i = 6; i != args; ++i)
  1639. {
  1640. points[i] = Float:getarg(i);
  1641. }
  1642. return Area_AddPoly_(points, sizeof (points), args);
  1643. }
  1644. static stock Area_AddPoly_(Float:points[], size, count)
  1645. #endif
  1646. {
  1647. // This is the main chunk of the code.
  1648. new
  1649. Float:height = Float:isodd(count);
  1650. count &= ~1;
  1651. new
  1652. reqSlots = ceildiv(count, CHILD_AREA_SLOTS) + 1;
  1653. if (Area_GetEmptySlotCount() < reqSlots) return NO_AREA;
  1654. if (_:height)
  1655. {
  1656. height = points[count];
  1657. }
  1658. else
  1659. {
  1660. height = FLOAT_INFINITY;
  1661. }
  1662. // Get the header. This stores meta-data about the poly area, and the
  1663. // bounding cylinder, it doesn't actually store any points.
  1664. new
  1665. header = Area_GetFreeSlot();
  1666. // Initialise the header. Area type, players, worlds, and count.
  1667. // This stores the number of pairs, not the number of co-ordinates.
  1668. YSI_g_sAreas[header][E_AREA_POLY_COUNT] = count;
  1669. YSI_g_sAreas[header][E_AREA_FLAGS] = e_AREA_FLAGS_POLY | e_AREA_FLAGS_ACTIVE;
  1670. PA_FastInit(YSI_g_sAreas[header][E_AREA_PLAYERS]);
  1671. NO_GROUPS(header)
  1672. {
  1673. PA_Init(YSI_g_sAreas[header][E_AREA_PLAYERS], true);
  1674. }
  1675. #if YSIM_HAS_MASTER
  1676. YSI_g_sAreas[header][E_AREA_MASTER] = Master_Caller();
  1677. #endif
  1678. Area_SetAllWorlds(header, true);
  1679. // Now loop through and store all the data points.
  1680. new
  1681. slot,
  1682. prev = header,
  1683. done = 0,
  1684. Float:cur,
  1685. Float:minx = FLOAT_INFINITY,
  1686. Float:miny = FLOAT_INFINITY,
  1687. Float:maxx = FLOAT_NEGATIVE_INFINITY,
  1688. Float:maxy = FLOAT_NEGATIVE_INFINITY;
  1689. while (done < count)
  1690. //while (done < count)
  1691. {
  1692. slot = Area_GetFreeSlot();
  1693. YSI_g_sAreas[slot][E_AREA_FLAGS] = e_AREA_FLAGS_ACTIVE | e_AREA_FLAGS_CHILD;
  1694. // Rely purely on counts, not ended lists. This is because the system
  1695. // MAY (if I get around to it) reuse this data in the last slot to
  1696. //YSI_g_sAreas[slot][E_AREA_POLY_NEXT] = NO_AREA;
  1697. YSI_g_sAreas[slot][E_AREA_CHILD_PARENT] = header;
  1698. YSI_g_sAreas[prev][E_AREA_POLY_NEXT] = slot;
  1699. #if YSIM_HAS_MASTER
  1700. YSI_g_sAreas[slot][E_AREA_MASTER] = Master_Caller();
  1701. #endif
  1702. // Now loop and put points in this slot.
  1703. size = min(count, done + CHILD_AREA_SLOTS);
  1704. for (new idx = 0; done < size; )
  1705. {
  1706. // X point.
  1707. cur = points[done++];
  1708. //printf("%d X: %0.2f", done, cur);
  1709. //printf("%04x%04x %04x%04x %.2f %d %d", _:minx >>> 16, _:minx & 0xFFFF, _:maxx >>> 16, _:maxx & 0xFFFF, cur, cur > maxx, cur < minx);
  1710. if (cur > maxx) maxx = cur;
  1711. if (cur < minx) minx = cur;
  1712. YSI_g_sAreas[slot][E_AREA_CHILD_ELEMS][idx++] = cur;
  1713. // Y point.
  1714. cur = points[done++];
  1715. //printf("%d Y: %0.2f", done, cur);
  1716. // Can be both on the first loop (WILL be both on the first loop).
  1717. if (cur > maxy) maxy = cur;
  1718. if (cur < miny) miny = cur;
  1719. YSI_g_sAreas[slot][E_AREA_CHILD_ELEMS][idx++] = cur;
  1720. }
  1721. prev = slot;
  1722. //count -= CHILD_AREA_SLOTS * 2;
  1723. }
  1724. // Now clean up.
  1725. #if YSIM_HAS_MASTER
  1726. setproperty(8, YSIM_RETURN, header);
  1727. #endif
  1728. Area_DetermineZone(minx, miny, maxx, maxy, header, header);
  1729. // Create the bounding cylinder so we can quickly determine if it is even
  1730. // at all possible for the player to be in this polygon.
  1731. // TODO: This doesn't YET create an accurate bounding cylinder as the
  1732. // diagonals of a square are longer than the width and height, and that fact
  1733. // isn't taken in to account by the radius. The question is, why are these
  1734. // sums returning -0.0 for 20.0-0.0?
  1735. // BIZZARE BUG FIX.
  1736. //printf("%f %f %f %f", minx, miny, maxx, maxy);
  1737. //printf("%f %f", maxx - minx, maxy - miny);
  1738. //printf("%04x%04x", _:minx >>> 16, _:minx & 0xFFFF);
  1739. //printf("%04x%04x", _:maxx >>> 16, _:maxx & 0xFFFF);
  1740. //if (maxx && minx) printf("1: %d", (maxx = (maxx - minx) / 2.0));
  1741. //else if (maxx) printf("2: %d", (maxx /= 2.0));
  1742. //else printf("3: %d", (maxx = minx / 2.0));
  1743. //if (maxx < 0.0) maxx = -maxx;
  1744. //if (maxy && miny) maxy = (maxy - miny) / 2.0;
  1745. //else if (maxy) maxy /= 2.0;
  1746. //else maxy = miny / 2.0;
  1747. //if (maxy < 0.0) maxy = -maxy;
  1748. //printf("%f %f %f %f", (maxx - minx) / 2.0, (maxy - miny) / 2.0, minx + (maxx - minx) / 2.0, miny + (maxy - miny) / 2.0);
  1749. maxx = (maxx - minx) / 2.0;
  1750. maxy = (maxy - miny) / 2.0;
  1751. //printf("%f %f %f %f", maxx, maxy, minx + maxx, miny + maxy);
  1752. minx += maxx;
  1753. miny += maxy;
  1754. YSI_g_sAreas[header][E_AREA_POLY_BOUND_X] = minx;
  1755. YSI_g_sAreas[header][E_AREA_POLY_BOUND_Y] = miny;
  1756. // Now find the radius. Note that it might actually make more sense to not
  1757. // use the dead centre but a point which minimises the R^2 distance, but
  1758. // that will require a lot more work.
  1759. slot = YSI_g_sAreas[header][E_AREA_POLY_NEXT];
  1760. count = YSI_g_sAreas[header][E_AREA_POLY_COUNT];
  1761. cur = 0.0;
  1762. while (count > 0)
  1763. {
  1764. for (new i = 0, j = min(count, CHILD_AREA_SLOTS); i < j; i += 2)
  1765. {
  1766. // Get the difference between the points.
  1767. maxx = YSI_g_sAreas[slot][E_AREA_CHILD_ELEMS][i] - minx;
  1768. maxy = YSI_g_sAreas[slot][E_AREA_CHILD_ELEMS][i + 1] - miny;
  1769. maxx = maxx * maxx + maxy * maxy;
  1770. if (maxx > cur) cur = maxx;
  1771. }
  1772. slot = YSI_g_sAreas[slot][E_AREA_POLY_NEXT];
  1773. count -= CHILD_AREA_SLOTS;
  1774. }
  1775. YSI_g_sAreas[header][E_AREA_POLY_BOUND_R] = cur;
  1776. YSI_g_sAreas[header][E_AREA_POLY_BOUND_H] = height;
  1777. return header;
  1778. }
  1779. /**--------------------------------------------------------------------------**\
  1780. <summary>Area_OnPlayerConnect</summary>
  1781. <param name="playerid">Player who connected</param>
  1782. <returns>
  1783. -
  1784. </returns>
  1785. <remarks>
  1786. -
  1787. </remarks>
  1788. \**--------------------------------------------------------------------------**/
  1789. mhook OnPlayerConnect(playerid)
  1790. {
  1791. for (new i = 0; i != AREAS_MAX_OVERLAPS; ++i)
  1792. {
  1793. YSI_g_sPlayerArea[playerid][i] = NO_AREA;
  1794. }
  1795. NO_GROUPS()
  1796. {
  1797. new
  1798. slot = Bit_Slot(playerid) + 1,
  1799. Bit:mask = Bit_Mask(playerid);
  1800. for (new i = 0; i != MAX_AREAS; ++i)
  1801. {
  1802. YSI_g_sAreas[i][E_AREA_PLAYERS][slot] |= mask;
  1803. }
  1804. }
  1805. return 1;
  1806. }
  1807. /**--------------------------------------------------------------------------**\
  1808. <summary>Area_loop</summary>
  1809. <param name="playerid">The player to check for.</param>
  1810. <returns>
  1811. -
  1812. </returns>
  1813. <remarks>
  1814. Main processing for the system. Takes one player and checks if they're in
  1815. some sort of range of somewhere.
  1816. </remarks>
  1817. \**--------------------------------------------------------------------------**/
  1818. #if defined AREAS_USE_TIMER
  1819. #error AREAS_USE_TIMER is currently unsupported sorry.
  1820. ptask Area_Loop[200](playerid)
  1821. {
  1822. new
  1823. Float:x,
  1824. Float:y,
  1825. Float:z,
  1826. area = YSI_g_sPlayerArea[playerid],
  1827. world = GetPlayerVirtualWorld(playerid);
  1828. GetPlayerPos(playerid, x, y, z);
  1829. if (area != NO_AREA)
  1830. {
  1831. if (Area_CheckArea(playerid, world, area, x, y, z)) continue;
  1832. YSI_g_sPlayerArea[playerid] = NO_AREA;
  1833. #if defined _YSI_GAMEMODE_PROPERTIES
  1834. if (!Property_OnPlayerLeaveArea(playerid, area))
  1835. #endif
  1836. CallRemoteFunction("OnPlayerLeaveArea", "ii", playerid, area);
  1837. }
  1838. for (area = 0; area < MAX_AREAS; area++)
  1839. {
  1840. if (Area_CheckArea(playerid, world, area, x, y, z))
  1841. {
  1842. YSI_g_sPlayerArea[playerid] = area;
  1843. CallRemoteFunction("OnPlayerEnterArea", "ii", playerid, area);
  1844. break;
  1845. }
  1846. }
  1847. }
  1848. #else
  1849. hook OnPlayerUpdate(playerid)
  1850. {
  1851. static
  1852. // Another hard-coded limit (max areas we can actually be in, more
  1853. // than the max that is globally stored, but still not infinate).
  1854. sAreas[16],
  1855. sZones[9],
  1856. Float:sX,
  1857. Float:sY,
  1858. Float:sZ,
  1859. sCur,
  1860. sNext,
  1861. sW,
  1862. sAlready;
  1863. new
  1864. in,
  1865. idx;
  1866. // Get all the zones for this player.
  1867. GetPlayerPos(playerid, sX, sY, sZ);
  1868. P:7("Areas_OnPlayerUpdate: pos = %f %f %f", sX, sY, sZ);
  1869. #if AREA_WORLDS > 0
  1870. sW = GetPlayerVirtualWorld(playerid);
  1871. #endif
  1872. for ( ; (sCur = _Area_GetPlayerAreas(playerid, idx)) != NO_AREA; )
  1873. {
  1874. // In this zone too.
  1875. if (Area_CheckArea(playerid, sW, sCur, sX, sY, sZ, sNext, sAreas, 0))
  1876. {
  1877. ++idx;
  1878. sAreas[in++] = sCur;
  1879. }
  1880. else
  1881. {
  1882. // No longer in the area they used to be in.
  1883. Area_DoLeave(playerid, sCur, idx);
  1884. // "Area_DoLeave" shifts all the slots, so you end up with not
  1885. // needing to change "idx" at all.
  1886. }
  1887. }
  1888. sAlready = in;
  1889. sZones = Area_GetZones(sX, sY);
  1890. for (idx = 0; ; )
  1891. {
  1892. P:7("Areas_OnPlayerUpdate: zone = %d", sZones[idx]);
  1893. switch (sZones[idx++])
  1894. {
  1895. case e_AREA_ZONE_I_N:
  1896. {
  1897. _AREA_DO_ALL(YSI_g_sZoneIN);
  1898. }
  1899. case e_AREA_ZONE_I_NE:
  1900. {
  1901. _AREA_DO_ALL(YSI_g_sZoneINE);
  1902. }
  1903. case e_AREA_ZONE_I_E:
  1904. {
  1905. _AREA_DO_ALL(YSI_g_sZoneIE);
  1906. }
  1907. case e_AREA_ZONE_I_SE:
  1908. {
  1909. _AREA_DO_ALL(YSI_g_sZoneISE);
  1910. }
  1911. case e_AREA_ZONE_I_S:
  1912. {
  1913. _AREA_DO_ALL(YSI_g_sZoneIS);
  1914. }
  1915. case e_AREA_ZONE_I_SW:
  1916. {
  1917. _AREA_DO_ALL(YSI_g_sZoneISW);
  1918. }
  1919. case e_AREA_ZONE_I_W:
  1920. {
  1921. _AREA_DO_ALL(YSI_g_sZoneIW);
  1922. }
  1923. case e_AREA_ZONE_I_NW:
  1924. {
  1925. _AREA_DO_ALL(YSI_g_sZoneINW);
  1926. }
  1927. case e_AREA_ZONE_I_:
  1928. {
  1929. _AREA_DO_ALL(YSI_g_sZoneI);
  1930. }
  1931. // "Outer" zones.
  1932. case e_AREA_ZONE_O_N:
  1933. {
  1934. _AREA_DO_ALL(YSI_g_sZoneON);
  1935. }
  1936. case e_AREA_ZONE_O_NE:
  1937. {
  1938. _AREA_DO_ALL(YSI_g_sZoneONE);
  1939. }
  1940. case e_AREA_ZONE_O_E:
  1941. {
  1942. _AREA_DO_ALL(YSI_g_sZoneOE);
  1943. }
  1944. case e_AREA_ZONE_O_SE:
  1945. {
  1946. _AREA_DO_ALL(YSI_g_sZoneOSE);
  1947. }
  1948. case e_AREA_ZONE_O_S:
  1949. {
  1950. _AREA_DO_ALL(YSI_g_sZoneOS);
  1951. }
  1952. case e_AREA_ZONE_O_SW:
  1953. {
  1954. _AREA_DO_ALL(YSI_g_sZoneOSW);
  1955. }
  1956. case e_AREA_ZONE_O_W:
  1957. {
  1958. _AREA_DO_ALL(YSI_g_sZoneOW);
  1959. }
  1960. case e_AREA_ZONE_O_NW:
  1961. {
  1962. _AREA_DO_ALL(YSI_g_sZoneONW);
  1963. }
  1964. //case e_AREA_ZONE_O_:
  1965. //{
  1966. // _AREA_DO_ALL(YSI_g_sZoneO);
  1967. //}
  1968. case e_AREA_ZONE_X_N:
  1969. {
  1970. _AREA_DO_ALL(YSI_g_sZoneXN);
  1971. }
  1972. case e_AREA_ZONE_X_NE:
  1973. {
  1974. _AREA_DO_ALL(YSI_g_sZoneXNE);
  1975. }
  1976. case e_AREA_ZONE_X_E:
  1977. {
  1978. _AREA_DO_ALL(YSI_g_sZoneXE);
  1979. }
  1980. case e_AREA_ZONE_X_SE:
  1981. {
  1982. _AREA_DO_ALL(YSI_g_sZoneXSE);
  1983. }
  1984. case e_AREA_ZONE_X_S:
  1985. {
  1986. _AREA_DO_ALL(YSI_g_sZoneXS);
  1987. }
  1988. case e_AREA_ZONE_X_SW:
  1989. {
  1990. _AREA_DO_ALL(YSI_g_sZoneXSW);
  1991. }
  1992. case e_AREA_ZONE_X_W:
  1993. {
  1994. _AREA_DO_ALL(YSI_g_sZoneXW);
  1995. }
  1996. case e_AREA_ZONE_X_NW:
  1997. {
  1998. _AREA_DO_ALL(YSI_g_sZoneXNW);
  1999. }
  2000. case e_AREA_ZONE_X_:
  2001. {
  2002. // Always the last zone in the list.
  2003. _AREA_DO_ALL(YSI_g_sZoneX);
  2004. break;
  2005. }
  2006. case e_AREA_ZONE_NONE:
  2007. {
  2008. // Specific zone (which).
  2009. _AREA_DO_ALL(YSI_g_sZones[((Area_MakeZone(sX) + AREAS_ZONE_PARTS) * AREAS_ZONE_ARRAY) + AREAS_ZONE_PARTS + Area_MakeZone(sY)]);
  2010. }
  2011. }
  2012. }
  2013. return 1;
  2014. }
  2015. #endif
  2016. /**--------------------------------------------------------------------------**\
  2017. <summary>Area_CheckArea</summary>
  2018. <param name="playerid">Player being checked for.</param>
  2019. <param name="world">VW the player is in.</param>
  2020. <param name="area">Area to check against.</param>
  2021. <param name="Float:x">X position to check.</param>
  2022. <param name="Float:y">Y position to check.</param>
  2023. <param name="Float:z">Z position to check.</param>
  2024. <returns>
  2025. -
  2026. </returns>
  2027. <remarks>
  2028. Checks if the given position is in the give area. All parameters are passed
  2029. to avoid calling functions over and over and over again. If the return is
  2030. not true, "area" is updated to the next one in the chain.
  2031. </remarks>
  2032. \**--------------------------------------------------------------------------**/
  2033. static stock Area_CheckArea(playerid, world, area, Float:x, Float:y, Float:z, &next, areas[16], already)
  2034. {
  2035. new
  2036. e_AREA_FLAGS:flags = YSI_g_sAreas[area][E_AREA_FLAGS];
  2037. next = YSI_g_sAreas[area][E_AREA_UNUSED_NEXT] & AREA_NO_NEXT;
  2038. // Are they already in this area?
  2039. while (already--)
  2040. {
  2041. if (areas[already] == area)
  2042. {
  2043. return 0;
  2044. }
  2045. }
  2046. if ((flags & e_AREA_FLAGS_ACTIVE))
  2047. {
  2048. P:7("Area_CheckArea: %d %d %d %f %f %f = %d", playerid, world, area, Float:x, Float:y, Float:z, _:(flags & e_AREA_FLAGS_TYPE) >>> 28);
  2049. switch (flags & e_AREA_FLAGS_TYPE)
  2050. {
  2051. case e_AREA_FLAGS_CIRCLE:
  2052. {
  2053. // In range of a sphere perfectly level with them.
  2054. if (!IsPlayerInRangeOfPoint(playerid, YSI_g_sAreas[area][E_AREA_POS][2], YSI_g_sAreas[area][E_AREA_POS][0], YSI_g_sAreas[area][E_AREA_POS][1], z))
  2055. {
  2056. return 0;
  2057. }
  2058. }
  2059. case e_AREA_FLAGS_OVAL:
  2060. {
  2061. if (!Area_IsInCircle(x, y, x, YSI_g_sAreas[area][E_AREA_POS]))
  2062. {
  2063. return 0;
  2064. }
  2065. }
  2066. case e_AREA_FLAGS_OVOID:
  2067. {
  2068. if (!Area_IsInOvoid(x, y, z, YSI_g_sAreas[area][E_AREA_POS]))
  2069. {
  2070. return 0;
  2071. }
  2072. }
  2073. case e_AREA_FLAGS_SPHERE:
  2074. {
  2075. if (!IsPlayerInRangeOfPoint(playerid, YSI_g_sAreas[area][E_AREA_POS][3], YSI_g_sAreas[area][E_AREA_POS][0], YSI_g_sAreas[area][E_AREA_POS][1], YSI_g_sAreas[area][E_AREA_POS][2]))
  2076. {
  2077. return 0;
  2078. }
  2079. }
  2080. case e_AREA_FLAGS_POLY:
  2081. {
  2082. if (!Area_IsInPoly(x, y, z, area))
  2083. {
  2084. return 0;
  2085. }
  2086. }
  2087. case e_AREA_FLAGS_CUBE:
  2088. {
  2089. if (!Area_IsInCube(x, y, z, YSI_g_sAreas[area][E_AREA_POS]))
  2090. {
  2091. return 0;
  2092. }
  2093. }
  2094. }
  2095. P:7("Area_CheckArea: MAYBE");
  2096. if (PA_Get(YSI_g_sAreas[area][E_AREA_PLAYERS], playerid))
  2097. {
  2098. #if AREA_WORLDS > 0
  2099. if (Bit_Get(YSI_g_sAreas[area][E_AREA_WORLDS], world))
  2100. {
  2101. return 1;
  2102. }
  2103. #else
  2104. #pragma unused world
  2105. return 1;
  2106. #endif
  2107. }
  2108. }
  2109. return 0;
  2110. }
  2111. /**--------------------------------------------------------------------------**\
  2112. <summary>Area_IsInCircle</summary>
  2113. <param name="Float:x">X position to check.</param>
  2114. <param name="Float:y">Y position to check.</param>
  2115. <param name="Float:z">Z position to check.</param>
  2116. <param name="Float:bounds[]">Data for the area position.</param>
  2117. <returns>
  2118. -
  2119. </returns>
  2120. <remarks>
  2121. Checks if a point is in a given circle.
  2122. </remarks>
  2123. \**--------------------------------------------------------------------------**/
  2124. static stock Area_IsInCircle(Float:x, Float:y, Float:z, Float:bounds[])
  2125. {
  2126. x -= bounds[0];
  2127. y -= bounds[1];
  2128. return (bounds[4] <= z <= bounds[5] && ((x * x) / bounds[2] + (y * y) / bounds[3]) < 1.0);
  2129. }
  2130. /**--------------------------------------------------------------------------**\
  2131. <summary>Area_IsInSphere</summary>
  2132. <param name="Float:x">X position to check.</param>
  2133. <param name="Float:y">Y position to check.</param>
  2134. <param name="Float:z">Z position to check.</param>
  2135. <param name="Float:bounds[]">Data for the area position.</param>
  2136. <returns>
  2137. -
  2138. </returns>
  2139. <remarks>
  2140. Checks if a point is in a given sphere.
  2141. </remarks>
  2142. \**--------------------------------------------------------------------------**/
  2143. /*static stock Area_IsInSphere(Float:x, Float:y, Float:z, Float:bounds[])
  2144. {
  2145. x -= bounds[0];
  2146. y -= bounds[1];
  2147. z -= bounds[2];
  2148. return (((x * x) + (y * y) + (z * z)) < bounds[3]);
  2149. }*/
  2150. /**--------------------------------------------------------------------------**\
  2151. <summary>Area_IsInOvoid</summary>
  2152. <param name="Float:x">X position to check.</param>
  2153. <param name="Float:y">Y position to check.</param>
  2154. <param name="Float:z">Z position to check.</param>
  2155. <param name="Float:bounds[]">Data for the area position.</param>
  2156. <returns>
  2157. -
  2158. </returns>
  2159. <remarks>
  2160. Checks if a point is in a given ovoid.
  2161. </remarks>
  2162. \**--------------------------------------------------------------------------**/
  2163. static stock Area_IsInOvoid(Float:x, Float:y, Float:z, Float:bounds[])
  2164. {
  2165. x -= bounds[0];
  2166. y -= bounds[1];
  2167. z -= bounds[2];
  2168. return (((x * x) / bounds[3] + (y * y) / bounds[4] + (z * z) / bounds[5]) < 1.0);
  2169. }
  2170. /**--------------------------------------------------------------------------**\
  2171. <summary>Area_IsInPoly</summary>
  2172. <param name="Float:x">X position to check.</param>
  2173. <param name="Float:y">Y position to check.</param>
  2174. <param name="Float:z">Z position to check.</param>
  2175. <param name="header">Pointer to the start of the polygon data in the array.</param>
  2176. <returns>
  2177. -
  2178. </returns>
  2179. <remarks>
  2180. Based on IsPlayerInAreaEx by koolk in the useful functions topic. The
  2181. passed pointer is the pointer to the first set of co-ordinates in a one-
  2182. way not looping linked list of points in the polygod as the data may be
  2183. spread throughout the areas array. This is as otherwise there may be
  2184. enough free spaces but not in one block.
  2185. The code first checks if there is a height component as it's the easiest
  2186. to check thus may save a load of pointless processing. If this passes it
  2187. then does the main loop. This loops till there are no points left to do
  2188. (monitored by decreasing count). When 2 points (four pieces of data) have
  2189. been checked the poiner for the data is moved on to the next group and the
  2190. checking continues.
  2191. For simplicity's sake (and thus speed's sake) the lower pointes from the
  2192. last check are saved amd used as the upper points for the next check to
  2193. avoid loads of repeated array accesses and saving the last array position.
  2194. </remarks>
  2195. \**--------------------------------------------------------------------------**/
  2196. /*static*/
  2197. stock Area_IsInPoly(Float:x, Float:y, Float:z, header)
  2198. {
  2199. // First check the bounding zone.
  2200. new
  2201. Float:bx = x - YSI_g_sAreas[header][E_AREA_POLY_BOUND_X],
  2202. Float:by = y - YSI_g_sAreas[header][E_AREA_POLY_BOUND_Y];
  2203. P:7("Area_IsInPoly: %f %f %f %d", x, y, z, header);
  2204. P:7("Area_IsInPoly: %f %f %f %d", YSI_g_sAreas[header][E_AREA_POLY_BOUND_H], (bx * bx + by * by),YSI_g_sAreas[header][E_AREA_POLY_BOUND_R], (z <= YSI_g_sAreas[header][E_AREA_POLY_BOUND_H] && (bx * bx + by * by) <= YSI_g_sAreas[header][E_AREA_POLY_BOUND_R]));
  2205. if (!(z <= YSI_g_sAreas[header][E_AREA_POLY_BOUND_H] && (bx * bx + by * by) <= YSI_g_sAreas[header][E_AREA_POLY_BOUND_R]))
  2206. {
  2207. return 0;
  2208. }
  2209. // Within the bounding cylinder, do the more complex checks.
  2210. new
  2211. count = YSI_g_sAreas[header][E_AREA_POLY_COUNT];
  2212. P:7("Area_IsInPoly: In the bounds: %d %d", count, CHILD_AREA_SLOTS);
  2213. if (count <= CHILD_AREA_SLOTS)
  2214. {
  2215. // Only uses one single child slot.
  2216. header = YSI_g_sAreas[header][E_AREA_POLY_NEXT];
  2217. new
  2218. lines,
  2219. done = 2,
  2220. Float:fx = YSI_g_sAreas[header][E_AREA_CHILD_ELEMS][0],
  2221. Float:fy = YSI_g_sAreas[header][E_AREA_CHILD_ELEMS][1],
  2222. Float:x0 = fx,
  2223. Float:y0 = fy,
  2224. Float:x1,
  2225. Float:y1;
  2226. while (done < count)
  2227. {
  2228. x1 = YSI_g_sAreas[header][E_AREA_CHILD_ELEMS][done++];
  2229. y1 = YSI_g_sAreas[header][E_AREA_CHILD_ELEMS][done++];
  2230. // Crosses this line.
  2231. //P:7("Area_IsInPoly: %f %f %f", y0, y, y1);
  2232. if (((y0 <= y <= y1) || (y1 <= y <= y0)) && ((x0 + ((y - y0) * (x1 - x0) / (y1 - y0))) <= x)) ++lines;
  2233. //P:7("Area_IsInPoly: %d %d %d %d %d", (y0 <= y <= y1), (y1 <= y <= y0), (y0 <= y <= y1) || (y1 <= y <= y0), (x0 + ((y - y0) * (x1 - x0) / (y1 - y0))) <= x, lines);
  2234. x0 = x1;
  2235. y0 = y1;
  2236. }
  2237. //P:7("Area_IsInPoly: %f %f %f", y0, y, fy);
  2238. if (((y0 <=y <= fy) || (fy <= y <= y0)) && (x0 + ((y - y0) * (fx - x0) / (fy - y0))) <= x) ++lines;
  2239. //P:7("Area_IsInPoly: %d %d %d %d %d", (y0 <= y <= fy), (fy <= y <= y0), (y0 <= y <= fy) || (fy <= y <= y0), (x0 + ((y - y0) * (fx - x0) / (fy - y0))) <= x, lines);
  2240. //P:7("Area_IsInPoly: Return %d", lines);
  2241. return isodd(lines);
  2242. }
  2243. else
  2244. {
  2245. // Has multiple children
  2246. header = YSI_g_sAreas[header][E_AREA_POLY_NEXT];
  2247. new
  2248. stp,
  2249. lines,
  2250. done = 2,
  2251. Float:fx = YSI_g_sAreas[header][E_AREA_CHILD_ELEMS][0],
  2252. Float:fy = YSI_g_sAreas[header][E_AREA_CHILD_ELEMS][1],
  2253. Float:x0 = fx,
  2254. Float:y0 = fy,
  2255. Float:x1,
  2256. Float:y1;
  2257. while (count > 0)
  2258. {
  2259. stp = min(count, CHILD_AREA_SLOTS);
  2260. while (done < stp)
  2261. {
  2262. x1 = YSI_g_sAreas[header][E_AREA_CHILD_ELEMS][done++];
  2263. y1 = YSI_g_sAreas[header][E_AREA_CHILD_ELEMS][done++];
  2264. // Crosses this line.
  2265. if (((y0 <= y <= y1) || (y1 <= y <= y0)) && (x0 + ((y - y0) * (x1 - x0) / (y1 - y0))) <= x) ++lines;
  2266. x0 = x1;
  2267. y0 = y1;
  2268. }
  2269. count -= CHILD_AREA_SLOTS;
  2270. done = 0;
  2271. header = YSI_g_sAreas[header][E_AREA_POLY_NEXT];
  2272. }
  2273. //x1 = YSI_g_sAreas[pointer][E_AREA_CHILD_ELEMS][0];
  2274. // Link the last and first points to complete the perimeter.
  2275. if (((y0 <=y <= fy) || (fy <= y <= y0)) && (x0 + ((y - y0) * (fx - x0) / (fy - y0))) <= x) ++lines;
  2276. //printf("Area_IsInPoly: Return %d", lines);
  2277. return isodd(lines);
  2278. }
  2279. }
  2280. /**--------------------------------------------------------------------------**\
  2281. <summary>Area_IsInCube</summary>
  2282. <param name="Float:x">X position to check.</param>
  2283. <param name="Float:y">Y position to check.</param>
  2284. <param name="Float:z">Z position to check.</param>
  2285. <param name="Float:lower[]">The lower corner of the cube.</param>
  2286. <param name="Float:upper[]">The upper corner of the cube.</param>
  2287. <returns>
  2288. -
  2289. </returns>
  2290. <remarks>
  2291. Checks if a point is in a given cube. This is another multi slot shape
  2292. but is much simpler than the poly as it's always 2 slots so we can easilly
  2293. get the data in one lump.
  2294. </remarks>
  2295. \**--------------------------------------------------------------------------**/
  2296. static stock Area_IsInCube(Float:x, Float:y, Float:z, Float:bounds[])
  2297. {
  2298. return (bounds[0] <= x <= bounds[3] && bounds[1] <= y <= bounds[4] && bounds[2] <= z <= bounds[5]);
  2299. }
  2300. /**--------------------------------------------------------------------------**\
  2301. <summary>Area_IsInBox</summary>
  2302. <param name="Float:x">X position to check.</param>
  2303. <param name="Float:y">Y position to check.</param>
  2304. <param name="Float:bounds[]">Data for the area position.</param>
  2305. <returns>
  2306. -
  2307. </returns>
  2308. <remarks>
  2309. Checks if a point is in a given box. There is no height check with this
  2310. one as any one area slot has 4 points which for this are upper and lower
  2311. x and y, adding a height check would make it require 2 slots and basically
  2312. make it a cube check.
  2313. </remarks>
  2314. \**--------------------------------------------------------------------------**/
  2315. /*static stock Area_IsInBox(Float:x, Float:y, Float:bounds[])
  2316. {
  2317. return (bounds[0] <= x <= bounds[2] && bounds[1] <= y <= bounds[3]);
  2318. }*/
  2319. /**--------------------------------------------------------------------------**\
  2320. <summary>Area_GetPlayerArea</summary>
  2321. <param name="playerid">Player to get area of.</param>
  2322. <returns>
  2323. The area a player is in or -1.
  2324. </returns>
  2325. <remarks>
  2326. Deprecated.
  2327. </remarks>
  2328. \**--------------------------------------------------------------------------**/
  2329. //foreign Area_GetPlayerArea(playerid);
  2330. #pragma deprecated Use Area_GetPlayerAreas
  2331. stock Area_GetPlayerArea(playerid)
  2332. {
  2333. if (VALID_PLAYERID(playerid))
  2334. {
  2335. return YSI_g_sPlayerArea[playerid][0];
  2336. }
  2337. return NO_AREA;
  2338. }
  2339. /**--------------------------------------------------------------------------**\
  2340. <summary>_Area_SetPlayerAreas</summary>
  2341. <param name="playerid">Player to set areas of.</param>
  2342. <param name="idx">Which slot to set.</param>
  2343. <param name="area">The area to store in that slot.</param>
  2344. <returns>
  2345. -
  2346. </returns>
  2347. <remarks>
  2348. Sets a player as in a slot.
  2349. </remarks>
  2350. \**--------------------------------------------------------------------------**/
  2351. static stock
  2352. _Area_SetPlayerAreas(playerid, idx, area)
  2353. {
  2354. if (idx < AREAS_MAX_OVERLAPS)
  2355. {
  2356. YSI_g_sPlayerArea[playerid][idx] = area;
  2357. }
  2358. else
  2359. {
  2360. // Use PVars to store extended areas.
  2361. static
  2362. sProp[12] = "y_areas_";
  2363. valstr(sProp[8], idx);
  2364. if (area == NO_AREA)
  2365. {
  2366. DeletePVar(playerid, sProp);
  2367. }
  2368. else
  2369. {
  2370. SetPVarInt(playerid, sProp, area);
  2371. }
  2372. }
  2373. }
  2374. /**--------------------------------------------------------------------------**\
  2375. <summary>
  2376. _Area_GetPlayerAreas
  2377. Area_GetPlayerAreas
  2378. </summary>
  2379. <param name="playerid">Player to get areas of.</param>
  2380. <param name="idx">Which area to get.</param>
  2381. <returns>
  2382. The area a player is in or -1.
  2383. </returns>
  2384. <remarks>
  2385. Replaces Area_GetPlayerArea for multiple areas. Includes an internal
  2386. version for tighter loops within this code to use once it is known that a
  2387. player exists, and without using the global function system.
  2388. </remarks>
  2389. \**--------------------------------------------------------------------------**/
  2390. static stock
  2391. _Area_GetPlayerAreas(playerid, idx)
  2392. {
  2393. if (idx < AREAS_MAX_OVERLAPS)
  2394. {
  2395. return YSI_g_sPlayerArea[playerid][idx];
  2396. }
  2397. else
  2398. {
  2399. // Use PVars to store extended areas.
  2400. static
  2401. sProp[12] = "y_areas_";
  2402. valstr(sProp[8], idx);
  2403. if (GetPVarType(playerid, sProp) == PLAYER_VARTYPE_INT)
  2404. {
  2405. return GetPVarInt(playerid, sProp);
  2406. }
  2407. }
  2408. return NO_AREA;
  2409. }
  2410. foreign Area_GetPlayerAreas(playerid, idx);
  2411. global Area_GetPlayerAreas(playerid, idx)
  2412. {
  2413. if (VALID_PLAYERID(playerid))
  2414. {
  2415. if (0 <= idx <= 999)
  2416. {
  2417. return _Area_GetPlayerAreas(playerid, idx);
  2418. }
  2419. }
  2420. return NO_AREA;
  2421. }
  2422. /**--------------------------------------------------------------------------**\
  2423. <summary>Area_SetPlayer</summary>
  2424. <param name="area">Area to set for.</param>
  2425. <param name="playerid">Player to set for.</param>
  2426. <param name="set">Wether or not the player can use the area.</param>
  2427. <returns>
  2428. -
  2429. </returns>
  2430. <remarks>
  2431. -
  2432. </remarks>
  2433. \**--------------------------------------------------------------------------**/
  2434. foreign void:Area_SetPlayer(area, playerid, bool:set);
  2435. global void:Area_SetPlayer(area, playerid, bool:set)
  2436. {
  2437. if (Area_IsActive(area))
  2438. {
  2439. PA_Set(YSI_g_sAreas[area][E_AREA_PLAYERS], playerid, set);
  2440. //return 1;
  2441. }
  2442. //return 0;
  2443. }
  2444. /**--------------------------------------------------------------------------**\
  2445. <summary>Area_GetPlayer</summary>
  2446. <param name="area">Area to get for.</param>
  2447. <param name="playerid">Player to get for.</param>
  2448. <returns>
  2449. -
  2450. </returns>
  2451. <remarks>
  2452. Can the player USE this area (not are they in it)?
  2453. </remarks>
  2454. \**--------------------------------------------------------------------------**/
  2455. foreign bool:Area_GetPlayer(area, playerid);
  2456. global bool:Area_GetPlayer(area, playerid)
  2457. {
  2458. if (Area_IsActive(area))
  2459. {
  2460. return PA_Get(YSI_g_sAreas[area][E_AREA_PLAYERS], playerid);
  2461. }
  2462. return false;
  2463. }
  2464. /**--------------------------------------------------------------------------**\
  2465. <summary>Area_SetWorld</summary>
  2466. <param name="area">Area to set for.</param>
  2467. <param name="world">World to set for.</param>
  2468. <param name="set">Wether or not the area is active in this world.</param>
  2469. <returns>
  2470. -
  2471. </returns>
  2472. <remarks>
  2473. -
  2474. </remarks>
  2475. \**--------------------------------------------------------------------------**/
  2476. foreign void:Area_SetWorld(area, world, bool:set);
  2477. global void:Area_SetWorld(area, world, bool:set)
  2478. {
  2479. #if AREA_WORLDS > 0
  2480. if (Area_IsActive(area))
  2481. {
  2482. Bit_Set(YSI_g_sAreas[area][E_AREA_WORLDS], world, set);
  2483. //return 1;
  2484. }
  2485. #else
  2486. #pragma unused area, world, set
  2487. #endif
  2488. //return 0;
  2489. }
  2490. /**--------------------------------------------------------------------------**\
  2491. <summary>Area_GetWorld</summary>
  2492. <param name="area">Area to set for.</param>
  2493. <param name="world">World to set for.</param>
  2494. <returns>
  2495. -
  2496. </returns>
  2497. <remarks>
  2498. -
  2499. </remarks>
  2500. \**--------------------------------------------------------------------------**/
  2501. foreign bool:Area_GetWorld(area, world);
  2502. global bool:Area_GetWorld(area, world)
  2503. {
  2504. #if AREA_WORLDS > 0
  2505. if (Area_IsActive(area))
  2506. {
  2507. return Bit_Get(YSI_g_sAreas[area][E_AREA_WORLDS], world);
  2508. }
  2509. return false;
  2510. #else
  2511. #pragma unused area, world
  2512. return true;
  2513. #endif
  2514. }
  2515. /**--------------------------------------------------------------------------**\
  2516. <summary>Area_SetAllPlayers</summary>
  2517. <param name="area">Area to set for.</param>
  2518. <param name="set">Wether or not all players can use this area.</param>
  2519. <returns>
  2520. -
  2521. </returns>
  2522. <remarks>
  2523. -
  2524. </remarks>
  2525. \**--------------------------------------------------------------------------**/
  2526. foreign void:Area_SetAllPlayers(area, bool:set);
  2527. global void:Area_SetAllPlayers(area, bool:set)
  2528. {
  2529. if (Area_IsActive(area))
  2530. {
  2531. PA_Init(YSI_g_sAreas[area][E_AREA_PLAYERS], set);
  2532. //return 1;
  2533. }
  2534. //return 0;
  2535. }
  2536. /**--------------------------------------------------------------------------**\
  2537. <summary>Area_IsValid</summary>
  2538. <param name="area">Area to check.</param>
  2539. <returns>
  2540. Is the passed area valid and active.
  2541. </returns>
  2542. <remarks>
  2543. -
  2544. </remarks>
  2545. \**--------------------------------------------------------------------------**/
  2546. foreign bool:Area_IsValid(area);
  2547. global bool:Area_IsValid(area)
  2548. {
  2549. return bool:Area_IsActive(area);
  2550. }
  2551. /**--------------------------------------------------------------------------**\
  2552. <summary>Area_IsEmpty</summary>
  2553. <param name="area">Area to check.</param>
  2554. <returns>
  2555. Is the passed area valid and empty.
  2556. </returns>
  2557. <remarks>
  2558. Currently very slow as it has to loop through multiple players! I may add
  2559. an entry counter to zones to make this O(1) instead of O(nm).
  2560. </remarks>
  2561. \**--------------------------------------------------------------------------**/
  2562. foreign bool:Area_IsEmpty(area);
  2563. global bool:Area_IsEmpty(area)
  2564. {
  2565. new
  2566. z;
  2567. if (Area_IsActive(area))
  2568. {
  2569. foreach (new playerid : Player)
  2570. {
  2571. for (new i = 0; ; ++i)
  2572. {
  2573. z = _Area_GetPlayerAreas(playerid, i);
  2574. if (z == NO_AREA)
  2575. {
  2576. break;
  2577. }
  2578. else if (z == area)
  2579. {
  2580. return false;
  2581. }
  2582. }
  2583. }
  2584. }
  2585. return true;
  2586. }
  2587. /**--------------------------------------------------------------------------**\
  2588. <summary>Area_SetCallback</summary>
  2589. <param name="area">Area to set for.</param>
  2590. <param name="callback:callback">Callback to use.</param>
  2591. <returns>
  2592. -
  2593. </returns>
  2594. <remarks>
  2595. Adds a y_inline style callback (publics included) to an area to be called
  2596. when a player enters it. This is NOT a remote function and instead records
  2597. the data locally to call functions in the correct script (or they'll just
  2598. end up crashing as you will be jumping to an arbitrary address in a script
  2599. that doesn't have sensible code at that address.
  2600. </remarks>
  2601. \**--------------------------------------------------------------------------**/
  2602. /**--------------------------------------------------------------------------**\
  2603. <summary>Area_DoEnter</summary>
  2604. <param name="playerid">The player that just entered the area.</param>
  2605. <param name="areaid">The area they entered.</param>
  2606. <returns>
  2607. -
  2608. </returns>
  2609. <remarks>
  2610. -
  2611. </remarks>
  2612. \**--------------------------------------------------------------------------**/
  2613. static stock Area_DoEnter(playerid, areaid, &idx)
  2614. {
  2615. // Save their area to the list of areas that they are currently in.
  2616. //YSI_g_sPlayerArea[playerid] = sCur;
  2617. _Area_SetPlayerAreas(playerid, idx++, areaid);
  2618. // Call the callback in all scripts.
  2619. CallRemoteFunction("OnPlayerEnterArea", "ii", playerid, areaid);
  2620. //broadcastfunc _Area_DoEnter(playerid, areaid);
  2621. }
  2622. /*remotefunc _Area_DoEnter(playerid, areaid)
  2623. {
  2624. if (YSI_g_sHasCallbacks & 1)
  2625. {
  2626. CallLocalFunction("OnPlayerEnterArea", "ii", playerid, areaid);
  2627. }
  2628. if (YSI_g_sHasCallbacks & 4)
  2629. {
  2630. //Hook_OnPlayerEnterArea(playerid, areaid);
  2631. }
  2632. return 1;
  2633. }*/
  2634. forward OnPlayerEnterArea(playerid, areaid);
  2635. /**--------------------------------------------------------------------------**\
  2636. <summary>Area_DoLeave</summary>
  2637. <param name="playerid">The player that just left the area.</param>
  2638. <param name="areaid">The area they left.</param>
  2639. <returns>
  2640. -
  2641. </returns>
  2642. <remarks>
  2643. -
  2644. </remarks>
  2645. \**--------------------------------------------------------------------------**/
  2646. //remotefunc Area_DoLeave(playerid, areaid)
  2647. static stock Area_DoLeave(playerid, areaid, idx)
  2648. {
  2649. // Remove this area from the list of areas.
  2650. new
  2651. cur;
  2652. // Shift up the zones.
  2653. while ((cur = _Area_GetPlayerAreas(playerid, idx + 1)) != NO_AREA)
  2654. {
  2655. _Area_SetPlayerAreas(playerid, idx++, cur);
  2656. }
  2657. _Area_SetPlayerAreas(playerid, idx, NO_AREA);
  2658. // Call the callback.
  2659. CallRemoteFunction("OnPlayerLeaveArea", "ii", playerid, areaid);
  2660. /*if (YSI_g_sHasCallbacks & 2)
  2661. {
  2662. CallLocalFunction("OnPlayerLeaveArea", "ii", playerid, areaid);
  2663. }
  2664. if (YSI_g_sHasCallbacks & 8)
  2665. {
  2666. //Hook_OnPlayerLeaveArea(playerid, areaid);
  2667. }*/
  2668. return 1;
  2669. }
  2670. forward OnPlayerLeaveArea(playerid, areaid);
  2671. #undef _GROUP_MAKE_LIMIT
  2672. #undef _GROUP_MAKE_NAME
  2673. #include "..\YSI_Core\y_master"