y_areas.inc 83 KB

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