y_commands.inc 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723
  1. /*----------------------------------------------------------------------------*\
  2. ================================
  3. Y Sever Includes - Commands Core
  4. ================================
  5. Description:
  6. Runs commands registered with the system and calls the required functions.
  7. Also handles alternate names and prefixes. Based very loosely on dcmd.
  8. Legal:
  9. Version: MPL 1.1
  10. The contents of this file are subject to the Mozilla Public License Version
  11. 1.1 (the "License"); you may not use this file except in compliance with
  12. the License. You may obtain a copy of the License at
  13. http://www.mozilla.org/MPL/
  14. Software distributed under the License is distributed on an "AS IS" basis,
  15. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  16. for the specific language governing rights and limitations under the
  17. License.
  18. The Original Code is the YSI commands include.
  19. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  20. Portions created by the Initial Developer are Copyright (C) 2011
  21. the Initial Developer. All Rights Reserved.
  22. Contributors:
  23. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  24. Thanks:
  25. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  26. ZeeX - Very productive conversations.
  27. koolk - IsPlayerinAreaEx code.
  28. TheAlpha - Danish translation.
  29. breadfish - German translation.
  30. Fireburn - Dutch translation.
  31. yom - French translation.
  32. 50p - Polish translation.
  33. Zamaroht - Spanish translation.
  34. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  35. for me to strive to better.
  36. Pixels^ - Running XScripters where the idea was born.
  37. Matite - Pestering me to release it and using it.
  38. Very special thanks to:
  39. Thiadmer - PAWN, whose limits continue to amaze me!
  40. Kye/Kalcor - SA:MP.
  41. SA:MP Team past, present and future - SA:MP.
  42. Version:
  43. 0.1.4
  44. Changelog:
  45. 20/10/10:
  46. Fixed a bug with insensitive commands - my fault for not testing.
  47. 06/01/08:
  48. Improved master and /help support.
  49. 04/01/08:
  50. Fixed bad element in Command_SetDeniedReturn.
  51. 12/08/07:
  52. Added master support.
  53. 24/06/07:
  54. Modifed a few functions to use Bit_GetBit for speed.
  55. 04/05/07:
  56. Completed command use support.
  57. Added invalid character protection.
  58. 02/05/07:
  59. Added YSI_ prefix to all globals.
  60. 14/04/07:
  61. Updated header documentation with more than changelog/functions.
  62. Added function name requesting.
  63. 13/04/07:
  64. Added function documentation.
  65. Added wrapped functions for e_COMM_FLAG values missing them.
  66. Added header function list.
  67. 12/04/07:
  68. Added command removal.
  69. 11/04/07:
  70. Changed system slightly to handle names and alt names separately. Still
  71. need a better way of ignoring names when alt names are used.
  72. 10/04/07:
  73. First version.
  74. Functions:
  75. Public:
  76. Command_Add - Adds a command to the array for processing.
  77. Command_Remove - Removes a command.
  78. Command_Name - Gets the name of a command in a property.
  79. Core:
  80. Command_Process - Called from OnPlayerCommandText to process entered commands.
  81. Command_Parse - Sorts added commands into a binary tree.
  82. Command_Hash - Hashes a word for command hashing.
  83. Command_ProcRem - Processes a help command in the master script.
  84. Stock:
  85. Command_SetDisconnectReturn - Sets the return value for unconnected players.
  86. Command_UseShortCuts - Toggles use of per-player command shortcuts.
  87. Command_SetDeniedReturn - Sets the return value for denied use commands.
  88. Command_UseDeniedMessage - Toggles the use of an error message for denied.
  89. Command_SetIllegalReturn - Sets the return value for illegal characters.
  90. Command_UseAltNames - Toggles the use of ini defined alternate names.
  91. Command_UsePrefix - Toggles the use of a global prefix.
  92. Command_UseSpace - Toggles the use of a space between prefix and command.
  93. Command_SetAltName - Sets the alternate name of a function.
  94. Command_SetPrefix - Sets the pfexix to be typed.
  95. Comamnd_SetPlayerUse - Sets wether or not a player can use a command.
  96. Comamnd_SetPlayerUseByID - Sets wether or not a player can use a command.
  97. Command_FindByName - Finds a command in a possibly sorted list.
  98. Static:
  99. Command_FindFast - Finds a function by binary search of function name.
  100. Command_FindAlt - Finds a function by binary search of alternate name.
  101. Command_SetSorted - Marks the binary tree as sorted.
  102. Command_FindSlow - Finds a function by linear search of function name.
  103. Command_Find - Finds a function from entered text.
  104. Command_Prefix - Checks the entered prefix.
  105. Command_ReturnName - Returns the name of a function.
  106. Command_GetPlayerCount - Gets the number of commands a player can use.
  107. Inline:
  108. Command_Command - Not used, constructor.
  109. Command_IsCleared - Checks a player is cleared to use a command.
  110. Command_DisconnectReturn - Gets the return value for disconnected players.
  111. Command_UsingShortCuts - Checks if short cuts are being used.
  112. Command_DeniedReturn - Gets the return value for prohibited commands.
  113. Command_IllegalReturn - Gets the return value for invalid characters.
  114. Command_DeniedMessage - Checks if a level error message should be sent.
  115. Command_IsSorted - Checks if the binary tree has been initialised.
  116. Command_UsingAltNames - Checks if alt names are being used.
  117. Command_UsingPrefix - Checks if the prefix is being used.
  118. Command_UsingSpace - Checks if the space option is being used.
  119. Command_CallFunction - Calls the required function.
  120. ycmd - Adds a command to the system.
  121. API:
  122. -
  123. Callbacks:
  124. -
  125. Definitions:
  126. MAX_COMMAND_LENGTH - The maximum length of a command string.
  127. COMMAND_NOT_FOUND - Indicates that a searched for string is not a function.
  128. Enums:
  129. e_COMM_FLAG - Bit mappings for command options.
  130. E_COMMANDS - Structure of the array holding the string data.
  131. Macros:
  132. Command_(%1) - Forwards and declares a standard command for calling.
  133. ycmd(%1) - Adds a command to the array (wrapper for Command_Add).
  134. Tags:
  135. e_COMM_FLAG - Flag type.
  136. Variables:
  137. Global:
  138. -
  139. Static:
  140. YSI_g_sCommands - Holds all the textual data of the commands.
  141. YSI_g_sSearchTree - Tree of hashes for function names.
  142. YSI_g_sAltTree - Tree of hashes for alternate names.
  143. YSI_g_sPrefix - The command prefix.
  144. YSI_g_sPrefixLength - Length of the prefix.
  145. YSI_g_sCommandIndex - Pointer to the next free index in the function array.
  146. YSI_g_sAltCount - The number of commands with altnames.
  147. YSI_g_sCommandFlags - Bit array of command options.
  148. Commands:
  149. commands - Lists all commands available to you.
  150. Compile options:
  151. COMMAND_SENSITIVE - Make commands case sensitive.
  152. COMMAND_ACCURATE - Can use '@' in command names.
  153. MAX_COMMANDS - The maximum number of commands which can be used.
  154. \*----------------------------------------------------------------------------*/
  155. #if defined _INC_y_text
  156. #error y_commands must be included before y_text
  157. #endif
  158. #include "internal\y_version"
  159. #if !defined MAX_COMMANDS
  160. #define MAX_COMMANDS (1024)
  161. #endif
  162. #define _GROUP_MAKE_NAME_CMD<%0...%1> %0Command%1
  163. #define _GROUP_MAKE_LIMIT_CMD MAX_COMMANDS
  164. #define YSIM_U_DISABLE
  165. #include "y_master"
  166. #include "y_groups"
  167. #include "y_debug"
  168. #include "y_hooks"
  169. #include "y_bintree"
  170. #include "y_amx"
  171. #include "y_playerarray"
  172. #include "y_utils"
  173. #include "internal\y_natives"
  174. // Define the CMD: macro. This uses the prefix "@yC_" as it's 32 bits so allows
  175. // for fast command string searching in the publics table and makes the commands
  176. // public without the need for the public keyword.
  177. /*#if YSIM_HAS_MASTER
  178. #if _YSIM_IS_CLIENT
  179. #define YCMD:%1(%2) static stock @_yC%1(%2)
  180. #else
  181. #if _YSIM_IS_SERVER
  182. #define YCMD:%1(%2) @yC_%1(a,b[],c);@yC_%1(a,b[],c)U@(8,YSIM_RETURN,@_yC%1(a,b,c));static @_yC%1(%2)
  183. #else
  184. #define YCMD:%1(%2) @yC_%1(a,b[],c);@yC_%1(a,b[],c)<>{}@yC_%1(a,b[],c)<_YCM:y>U@(8,YSIM_RETURN,@_yC%1(a,b,c));static @_yC%1(%2)
  185. #endif
  186. #endif
  187. #else
  188. #define YCMD:%1(%2) @yC_%1(%2);@yC_%1(%2)
  189. #endif*/
  190. //#define YCMD:%0(%1) RC:%0(%1)
  191. #define _YCMD_0:_YCMD_1:_YCMD_2:%0(%1[]%2) RC:%0(%1[]%2)
  192. #define _YCMD_1:_YCMD_2:%0, Command_GetID(#%0),
  193. #define _YCMD_2:%0) Command_GetID(#%0))
  194. #define YCMD: _YCMD_0:_YCMD_1:_YCMD_2:
  195. // ZCMD compatibility.
  196. #define CMD:%0(%1) RC:%0(%1,_h_e_l_p_)if(_h_e_l_p_)return 0;else
  197. #define COMMAND CMD
  198. // This is opposite AMX_FastString as that's in C, not packed, order.
  199. #define Command_FastString(%1,%2,%3,%4) \
  200. (((%1)<<24)|((%2)<<16)|((%3)<<8)|((%4)<<0))
  201. #define MAX_COMMAND_LENGTH (32)
  202. #define COMMAND_NOT_FOUND (-1)
  203. #if defined COMMAND_SENSITIVE
  204. #define TOLOWER(%0) (%0)
  205. #define COMMAND_END_CMP (0)
  206. #else
  207. //#if defined COMMAND_ACCURATE
  208. // #define TOLOWER(%0) ((%0) | 0x20)
  209. // #define COMMAND_END_CMP (0x20)
  210. //#else
  211. #define TOLOWER(%0) tolower(%0)
  212. #define COMMAND_END_CMP (0)
  213. //#endif
  214. #endif
  215. // Don't forget the AMX_FastString below if you change this.
  216. #define COMMAND_FUNCTION_PREFIX (Command_FastString('@', 'y', 'C', '_'))
  217. // Reset both of these when you remove a command.
  218. #define _Command_IsValid(%0) ((Command_GetPointer(%0)==(%0))||(YSI_g_sCommands[(%0)][E_COMMANDS_MASTERS]==-1))
  219. #define Command_IsValid(%0) ((0<=(%0)<MAX_COMMANDS)&&_Command_IsValid(%0))
  220. enum e_COMM_FLAG (<<= 1)
  221. {
  222. e_COMM_FLAG_PROVIDER = 0x000000FF,
  223. e_COMM_FLAG_SORTED = 0x00000100,
  224. e_COMM_FLAG_PERMISSION_WARN,
  225. e_COMM_FLAG_USE_ALTNAMES,
  226. e_COMM_FLAG_PERMISSION_RET,
  227. e_COMM_FLAG_USE_PREFIX,
  228. e_COMM_FLAG_USE_SPACE,
  229. e_COMM_FLAG_USE_SHORTCUTS,
  230. e_COMM_FLAG_DISCONNECT,
  231. e_COMM_FLAG_ILLEGAL,
  232. e_COMM_FLAG_COLLISION,
  233. e_COMM_FLAG_UNKNOWN,
  234. e_COMM_FLAG_MULPRO,
  235. // Can't easilly add more flags now...
  236. e_COMM_FLAG_OPCP = 0x07C00000, // 0b00000111110..0
  237. e_COMM_FLAG_OPCP_ADD = 0x00400000, // Low bit of above flag.
  238. e_COMM_FLAG_OPCR = 0xF8000000, // 0b11111000000..0
  239. e_COMM_FLAG_OPCR_ADD = 0x08000000 // Low bit of above flag.
  240. }
  241. enum E_COMMANDS
  242. {
  243. E_COMMANDS_FUNCTION[MAX_COMMAND_LENGTH char],
  244. PlayerArray:E_COMMANDS_PLAYERS<MAX_PLAYERS>,
  245. #if YSIM_HAS_MASTER
  246. E_COMMANDS_MASTERS,
  247. #endif
  248. E_COMMANDS_FUNC_POINTER
  249. //E_COMMANDS_AMX_ADDRESS,
  250. }
  251. static stock
  252. #if YSIM_HAS_MASTER
  253. YSI_g_sPlayerProvider[MAX_PLAYERS char] = {-1, ...},
  254. #endif
  255. YSI_g_sCommands[MAX_COMMANDS][E_COMMANDS],
  256. BinaryTree:YSI_g_sSearchTree<MAX_COMMANDS>,
  257. e_COMM_FLAG:YSI_g_sCommandFlags = e_COMM_FLAG:0xFF,
  258. YSI_g_sCommandIndex,
  259. YSI_g_sCommandCount;
  260. //YSI_g_sCommandDialog[MAX_PLAYERS] = {-1, ...},
  261. //YSI_g_sMaster23,
  262. /*Group:Command_InitialiseFromGroups(x)
  263. {
  264. //Bit_SetAll(YSI_g_sCommands[x][E_COMMANDS_PLAYERS], true, bits<MAX_PLAYERS>);
  265. PA_Init(YSI_g_sCommands[x][E_COMMANDS_PLAYERS], true, bits<MAX_PLAYERS>);
  266. }*/
  267. /*----------------------------------------------------------------------------*\
  268. Function:
  269. Command_Name
  270. Params:
  271. index - Index of the command to operate on.
  272. Return:
  273. -
  274. Notes:
  275. Gets the name of a function from the array.
  276. \*----------------------------------------------------------------------------*/
  277. #define Command_Name(%1) \
  278. (YSI_g_sCommands[(%1)][E_COMMANDS_FUNCTION][1])
  279. /*----------------------------------------------------------------------------*\
  280. Function:
  281. Command_GetFuncName
  282. Params:
  283. index - Index of the command to operate on.
  284. Return:
  285. -
  286. Notes:
  287. Gets the full function name for a slot - note that this may not be the right
  288. function if this slot points to another one.
  289. \*----------------------------------------------------------------------------*/
  290. #define Command_GetFuncName(%1) \
  291. (YSI_g_sCommands[(%1)][E_COMMANDS_FUNCTION])
  292. /*----------------------------------------------------------------------------*\
  293. Function:
  294. Command_GetPointer
  295. Params:
  296. index - Index of the command to operate on.
  297. Return:
  298. -
  299. Notes:
  300. Gets the pointer for a function from the array.
  301. \*----------------------------------------------------------------------------*/
  302. #if YSIM_HAS_MASTER
  303. #define Command_GetPointer(%1) \
  304. (YSI_g_sCommands[(%1)][E_COMMANDS_FUNC_POINTER]&0x00FFFFFF)
  305. #else
  306. #define Command_GetPointer(%1) \
  307. (YSI_g_sCommands[(%1)][E_COMMANDS_FUNC_POINTER])
  308. #endif
  309. /*----------------------------------------------------------------------------*\
  310. Function:
  311. Command_GetProvider
  312. Params:
  313. index - Index of the command to operate on.
  314. Return:
  315. -
  316. Notes:
  317. Gets the unique script in which this version of the command is.
  318. \*----------------------------------------------------------------------------*/
  319. #if YSIM_HAS_MASTER
  320. #define Command_Provider(%1) \
  321. (YSI_g_sCommands[(%1)][E_COMMANDS_FUNC_POINTER]>>>24)
  322. #endif
  323. /*----------------------------------------------------------------------------*\
  324. Function:
  325. Command_GetProvider
  326. Params:
  327. index - Index of the command to operate on.
  328. Return:
  329. -
  330. Notes:
  331. Gets the unique script in which this version of the command is.
  332. \*----------------------------------------------------------------------------*/
  333. #if YSIM_HAS_MASTER
  334. #define Command_DefaultProvider() \
  335. (YSI_g_sCommandFlags & e_COMM_FLAG_PROVIDER)
  336. #endif
  337. /*----------------------------------------------------------------------------*\
  338. Function:
  339. Command_GetFunction
  340. Params:
  341. index - Index of the command to operate on.
  342. Return:
  343. -
  344. Notes:
  345. Gets the real function for this slot.
  346. \*----------------------------------------------------------------------------*/
  347. #define Command_GetFunction(%1) \
  348. (Command_GetFuncName(Command_GetPointer((%1))))
  349. /*----------------------------------------------------------------------------*\
  350. Function:
  351. Command_CheckPlayer
  352. Params:
  353. index - Index of the command to operate on.
  354. playerid - The player to check for.
  355. Return:
  356. -
  357. Notes:
  358. Gets wether a player can use a command.
  359. \*----------------------------------------------------------------------------*/
  360. #define Command_CheckPlayer(%1,%2) \
  361. (YSI_g_sCommands[(%1)][E_COMMANDS_PLAYERS][Bit_Slot(%2)+1]&Bit_Mask(%2))
  362. //(PA=([E_COMMANDS_PLAYERS], (%2)))
  363. //(Bit_Get(YSI_g_sCommands[(%1)][E_COMMANDS_PLAYERS], (%2)))
  364. /*----------------------------------------------------------------------------*\
  365. Function:
  366. Command_DeniedReturn
  367. Params:
  368. -
  369. Return:
  370. e_COMM_FLAG_PERMISSION_RET
  371. Notes:
  372. -
  373. \*----------------------------------------------------------------------------*/
  374. #define Command_DeniedReturn() \
  375. (YSI_g_sCommandFlags & e_COMM_FLAG_PERMISSION_RET)
  376. /*----------------------------------------------------------------------------*\
  377. Function:
  378. Command_SetDeniedReturn
  379. Params:
  380. -
  381. Return:
  382. -
  383. Notes:
  384. -
  385. \*----------------------------------------------------------------------------*/
  386. foreign Command_SetDeniedReturn(bool:set);
  387. global Command_SetDeniedReturn(bool:set)
  388. {
  389. P:2("Command_SetDeniedReturn called: %i", bool:set);
  390. if (set)
  391. {
  392. YSI_g_sCommandFlags |= e_COMM_FLAG_PERMISSION_RET;
  393. }
  394. else
  395. {
  396. YSI_g_sCommandFlags &= ~e_COMM_FLAG_PERMISSION_RET;
  397. }
  398. return 1;
  399. }
  400. /*----------------------------------------------------------------------------*\
  401. Function:
  402. Command_SetProvider
  403. Params:
  404. p - Script.
  405. Return:
  406. -
  407. Notes:
  408. Set the current provider to use for all commands being added. Basically
  409. allows us to have the same command name in different scripts and have them
  410. do different things, with different players targetting different ones.
  411. \*----------------------------------------------------------------------------*/
  412. #if YSIM_HAS_MASTER
  413. foreign Command_SetProvider(p);
  414. global Command_SetProvider(p)
  415. {
  416. P:2("Command_SetProvider called: %i", p);
  417. YSI_g_sCommandFlags = (YSI_g_sCommandFlags & ~e_COMM_FLAG_PROVIDER) | (e_COMM_FLAG:p & e_COMM_FLAG_PROVIDER) | e_COMM_FLAG_MULPRO;
  418. return 1;
  419. }
  420. foreign Command_GetProvider();
  421. global Command_GetProvider()
  422. {
  423. P:2("Command_GetProvider called");
  424. /*YSI_g_sCommandFlags = (YSI_g_sCommandFlags & ~e_COMM_FLAG_PROVIDER) | (e_COMM_FLAG:p & e_COMM_FLAG_PROVIDER)
  425. if (YSI_g_sCommandFlags & e_COMM_FLAG_MULPRO)
  426. {
  427. return _:(YSI_g_sCommandFlags & (YSI_g_sCommandFlags & ~e_COMM_FLAG_PROVIDER));
  428. }
  429. else
  430. {
  431. }*/
  432. // Poorely named, gets the current provider.
  433. return _:Command_DefaultProvider();
  434. }
  435. foreign Command_SetPlayerProvider(playerid,p);
  436. global Command_SetPlayerProvider(playerid,p)
  437. {
  438. P:2("Command_SetPlayerProvider called: %i, %i", playerid, p);
  439. if (0 <= playerid < MAX_PLAYERS)
  440. {
  441. YSI_g_sPlayerProvider{playerid} = p & 0xFF;
  442. }
  443. return 1;
  444. }
  445. foreign Command_GetPlayerProvider(playerid);
  446. global Command_GetPlayerProvider(playerid)
  447. {
  448. P:2("Command_GetPlayerProvider called: %i", playerid);
  449. return YSI_g_sPlayerProvider{playerid};
  450. }
  451. #endif
  452. /*----------------------------------------------------------------------------*\
  453. Function:
  454. Command_GetDeniedReturn
  455. Params:
  456. -
  457. Return:
  458. -
  459. Notes:
  460. -
  461. \*----------------------------------------------------------------------------*/
  462. foreign bool:Command_GetDeniedReturn();
  463. global bool:Command_GetDeniedReturn()
  464. {
  465. P:2("bool:Command_GetDeniedReturn called");
  466. return bool:Command_DeniedReturn();
  467. }
  468. /*----------------------------------------------------------------------------*\
  469. Function:
  470. Command_HasCollisions
  471. Params:
  472. -
  473. Return:
  474. e_COMM_FLAG_COLLISION
  475. Notes:
  476. -
  477. \*----------------------------------------------------------------------------*/
  478. #define Command_HasCollisions() \
  479. (YSI_g_sCommandFlags & e_COMM_FLAG_COLLISION)
  480. /*----------------------------------------------------------------------------*\
  481. Function:
  482. Command_HasCollisions
  483. Params:
  484. -
  485. Return:
  486. e_COMM_FLAG_MULPRO
  487. Notes:
  488. -
  489. \*----------------------------------------------------------------------------*/
  490. #if YSIM_HAS_MASTER
  491. #define Command_HasMultipleProviders() \
  492. (YSI_g_sCommandFlags & e_COMM_FLAG_MULPRO)
  493. #endif
  494. /*----------------------------------------------------------------------------*\
  495. Function:
  496. Command_SetCollisions
  497. Params:
  498. -
  499. Return:
  500. -
  501. Notes:
  502. -
  503. \*----------------------------------------------------------------------------*/
  504. #define Command_SetCollisions() \
  505. YSI_g_sCommandFlags |= e_COMM_FLAG_COLLISION
  506. /*----------------------------------------------------------------------------*\
  507. Function:
  508. Command_IllegalReturn
  509. Params:
  510. -
  511. Return:
  512. e_COMM_FLAG_ILLEGAL
  513. Notes:
  514. -
  515. \*----------------------------------------------------------------------------*/
  516. #define Command_IllegalReturn() \
  517. (YSI_g_sCommandFlags & e_COMM_FLAG_ILLEGAL)
  518. /*----------------------------------------------------------------------------*\
  519. Function:
  520. Command_SetIllegalReturn
  521. Params:
  522. -
  523. Return:
  524. -
  525. Notes:
  526. -
  527. \*----------------------------------------------------------------------------*/
  528. foreign Command_SetIllegalReturn(bool:set);
  529. global Command_SetIllegalReturn(bool:set)
  530. {
  531. P:2("Command_SetIllegalReturn called: %i", bool:set);
  532. if (set)
  533. {
  534. YSI_g_sCommandFlags |= e_COMM_FLAG_ILLEGAL;
  535. }
  536. else
  537. {
  538. YSI_g_sCommandFlags &= ~e_COMM_FLAG_ILLEGAL;
  539. }
  540. return 1;
  541. }
  542. /*----------------------------------------------------------------------------*\
  543. Function:
  544. Command_GetIllegalReturn
  545. Params:
  546. -
  547. Return:
  548. -
  549. Notes:
  550. -
  551. \*----------------------------------------------------------------------------*/
  552. foreign bool:Command_GetIllegalReturn();
  553. global bool:Command_GetIllegalReturn()
  554. {
  555. P:2("bool:Command_GetIllegalReturn called");
  556. return bool:Command_IllegalReturn();
  557. }
  558. /*----------------------------------------------------------------------------*\
  559. Function:
  560. Command_IllegalReturn
  561. Params:
  562. -
  563. Return:
  564. e_COMM_FLAG_ILLEGAL
  565. Notes:
  566. -
  567. \*----------------------------------------------------------------------------*/
  568. #define Command_UnknownReturn() \
  569. (YSI_g_sCommandFlags & e_COMM_FLAG_UNKNOWN)
  570. /*----------------------------------------------------------------------------*\
  571. Function:
  572. Command_SetIllegalReturn
  573. Params:
  574. -
  575. Return:
  576. -
  577. Notes:
  578. -
  579. \*----------------------------------------------------------------------------*/
  580. foreign Command_SetUnknownReturn(bool:set);
  581. global Command_SetUnknownReturn(bool:set)
  582. {
  583. P:2("Command_SetUnknownReturn called: %i", bool:set);
  584. if (set)
  585. {
  586. YSI_g_sCommandFlags |= e_COMM_FLAG_UNKNOWN;
  587. }
  588. else
  589. {
  590. YSI_g_sCommandFlags &= ~e_COMM_FLAG_UNKNOWN;
  591. }
  592. return 1;
  593. }
  594. /*----------------------------------------------------------------------------*\
  595. Function:
  596. Command_GetIllegalReturn
  597. Params:
  598. -
  599. Return:
  600. -
  601. Notes:
  602. -
  603. \*----------------------------------------------------------------------------*/
  604. foreign bool:Command_GetUnknownReturn();
  605. global bool:Command_GetUnknownReturn()
  606. {
  607. P:2("bool:Command_GetUnknownReturn called");
  608. return bool:Command_UnknownReturn();
  609. }
  610. /*----------------------------------------------------------------------------*\
  611. Function:
  612. Command_DisconnectReturn
  613. Params:
  614. -
  615. Return:
  616. e_COMM_FLAG_DISCONNECT
  617. Notes:
  618. -
  619. \*----------------------------------------------------------------------------*/
  620. #define Command_DisconnectReturn() \
  621. (YSI_g_sCommandFlags & e_COMM_FLAG_DISCONNECT)
  622. /*----------------------------------------------------------------------------*\
  623. Function:
  624. Command_SetDisconnectReturn
  625. Params:
  626. -
  627. Return:
  628. -
  629. Notes:
  630. -
  631. \*----------------------------------------------------------------------------*/
  632. foreign Command_SetDisconnectReturn(bool:set);
  633. global Command_SetDisconnectReturn(bool:set)
  634. {
  635. P:2("Command_SetDisconnectReturn called: %i", bool:set);
  636. if (set)
  637. {
  638. YSI_g_sCommandFlags |= e_COMM_FLAG_DISCONNECT;
  639. }
  640. else
  641. {
  642. YSI_g_sCommandFlags &= ~e_COMM_FLAG_DISCONNECT;
  643. }
  644. return 1;
  645. }
  646. /*----------------------------------------------------------------------------*\
  647. Function:
  648. Command_GetDisconnectReturn
  649. Params:
  650. -
  651. Return:
  652. -
  653. Notes:
  654. -
  655. \*----------------------------------------------------------------------------*/
  656. foreign bool:Command_GetDisconnectReturn();
  657. global bool:Command_GetDisconnectReturn()
  658. {
  659. P:2("bool:Command_GetDisconnectReturn called");
  660. return bool:Command_DisconnectReturn();
  661. }
  662. /*----------------------------------------------------------------------------*\
  663. Function:
  664. Command_DeniedDisplay
  665. Params:
  666. -
  667. Return:
  668. e_COMM_FLAG_PERMISSION_WARN
  669. Notes:
  670. -
  671. \*----------------------------------------------------------------------------*/
  672. #define Command_DeniedDisplay() \
  673. (YSI_g_sCommandFlags & e_COMM_FLAG_PERMISSION_WARN)
  674. /*----------------------------------------------------------------------------*\
  675. Function:
  676. Command_SetDeniedDisplay
  677. Params:
  678. -
  679. Return:
  680. -
  681. Notes:
  682. -
  683. \*----------------------------------------------------------------------------*/
  684. foreign Command_SetDeniedDisplay(bool:set);
  685. global Command_SetDeniedDisplay(bool:set)
  686. {
  687. P:2("Command_SetDeniedDisplay called: %i", bool:set);
  688. if (set)
  689. {
  690. YSI_g_sCommandFlags |= e_COMM_FLAG_PERMISSION_WARN;
  691. }
  692. else
  693. {
  694. YSI_g_sCommandFlags &= ~e_COMM_FLAG_PERMISSION_WARN;
  695. }
  696. return 1;
  697. }
  698. /*----------------------------------------------------------------------------*\
  699. Function:
  700. Command_GetDeniedDisplay
  701. Params:
  702. -
  703. Return:
  704. -
  705. Notes:
  706. -
  707. \*----------------------------------------------------------------------------*/
  708. foreign bool:Command_GetDeniedDisplay();
  709. global bool:Command_GetDeniedDisplay()
  710. {
  711. P:2("bool:Command_GetDeniedDisplay called");
  712. return bool:Command_DeniedDisplay();
  713. }
  714. /*----------------------------------------------------------------------------*\
  715. Function:
  716. Command_GetNameInt
  717. Params:
  718. f - Command to get the name of.
  719. Return:
  720. -
  721. Notes:
  722. -
  723. \*----------------------------------------------------------------------------*/
  724. /*RF:Command_GetNameInt[i](f)
  725. {
  726. P:2("Command_GetNameInt called: %i", f);
  727. if (f >= 0 && f < YSI_g_sCommandIndex)
  728. {
  729. setproperty(8, "", YSIM_STRING, Command_Name(f));
  730. return 1;
  731. }
  732. return 0;
  733. }*/
  734. /*----------------------------------------------------------------------------*\
  735. Function:
  736. Command_GetName
  737. Params:
  738. funcid - Command to get the name of.
  739. Return:
  740. -
  741. Notes:
  742. native Command_GetName(funcid);
  743. \*----------------------------------------------------------------------------*/
  744. /*stock Command_GetName(funcid)
  745. {
  746. P:3("Command_GetName called: %i", funcid);
  747. new
  748. buffer[32] = "";
  749. if (Command_GetNameInt(funcid))
  750. {
  751. getproperty(8, "", YSIM_STRING, buffer);
  752. strunpack(buffer, buffer);
  753. }
  754. return buffer;
  755. }*/
  756. /*----------------------------------------------------------------------------*\
  757. Function:
  758. Command_IsSorted
  759. Params:
  760. -
  761. Return:
  762. e_COMM_FLAG_SORTED
  763. Notes:
  764. -
  765. \*----------------------------------------------------------------------------*/
  766. #define Command_IsSorted() \
  767. (YSI_g_sCommandFlags & e_COMM_FLAG_SORTED)
  768. /*----------------------------------------------------------------------------*\
  769. Function:
  770. Command_Generate
  771. Params:
  772. -
  773. Return:
  774. -
  775. Notes:
  776. -
  777. \*----------------------------------------------------------------------------*/
  778. foreign Command_Generate();
  779. global Command_Generate()
  780. {
  781. P:2("Command_Generate called");
  782. P:2("Command_Generate called");
  783. if (!Command_IsSorted())
  784. {
  785. P:2("Command_Generate: Count = %d", YSI_g_sCommandCount);
  786. new
  787. data[MAX_COMMANDS][E_BINTREE_INPUT];
  788. // This is only called once, so we know YSI_g_sCommandCount will be
  789. // accurate WRT the locations of commands.
  790. for (new i = 0; i != YSI_g_sCommandCount; ++i)
  791. {
  792. data[i][E_BINTREE_INPUT_POINTER] = i;
  793. new
  794. hash = Command_PackHash(Command_Name(i));
  795. // Check for an existing command with this hash.
  796. if (!Command_HasCollisions())
  797. {
  798. for (new j = 0; j != i; ++j)
  799. {
  800. if (hash == data[j][E_BINTREE_INPUT_VALUE])
  801. {
  802. Command_SetCollisions();
  803. break;
  804. }
  805. }
  806. }
  807. C:3(else printf("Command_Generate: Hash = %d", hash););
  808. P:2("Command_Generate: Hash = %d", hash);
  809. data[i][E_BINTREE_INPUT_VALUE] = hash;
  810. }
  811. // WHY THE HECK did I change "Bintree_Generate" to "Bintree_Fill"? That
  812. // skips the whole sorting code before adding things to the binary tree!
  813. if (YSI_g_sCommandCount)
  814. {
  815. //Bintree_Fill(YSI_g_sSearchTree, data, YSI_g_sCommandCount);
  816. Bintree_Generate(YSI_g_sSearchTree, data, YSI_g_sCommandCount);
  817. //Bintree_Fill(YSI_g_sSearchTree, data, YSI_g_sCommandCount);
  818. }
  819. P:4("Command_Generate: %d %d %d %d %d", YSI_g_sSearchTree[0][E_BINTREE_TREE_VALUE], YSI_g_sSearchTree[0][E_BINTREE_TREE_LEFT], YSI_g_sSearchTree[0][E_BINTREE_TREE_RIGHT], YSI_g_sSearchTree[0][E_BINTREE_TREE_PARENT], YSI_g_sSearchTree[0][E_BINTREE_TREE_POINTER]);
  820. // Set sorted to true.
  821. YSI_g_sCommandFlags |= e_COMM_FLAG_SORTED;
  822. }
  823. return 1;
  824. }
  825. foreign Command_IncOPCR();
  826. global Command_IncOPCR()
  827. {
  828. P:2("Command_IncOPCR called");
  829. YSI_g_sCommandFlags += e_COMM_FLAG_OPCR_ADD;
  830. return 1;
  831. }
  832. foreign Command_DecOPCR();
  833. global Command_DecOPCR()
  834. {
  835. P:2("Command_DecOPCR called");
  836. YSI_g_sCommandFlags -= e_COMM_FLAG_OPCR_ADD;
  837. return 1;
  838. }
  839. foreign Command_IncOPCP();
  840. global Command_IncOPCP()
  841. {
  842. P:2("Command_IncOPCP called");
  843. YSI_g_sCommandFlags += e_COMM_FLAG_OPCP_ADD;
  844. return 1;
  845. }
  846. foreign Command_DecOPCP();
  847. global Command_DecOPCP()
  848. {
  849. P:2("Command_DecOPCP called");
  850. YSI_g_sCommandFlags -= e_COMM_FLAG_OPCP_ADD;
  851. return 1;
  852. }
  853. /*----------------------------------------------------------------------------*\
  854. Function:
  855. OnScriptInit
  856. Params:
  857. -
  858. Return:
  859. -
  860. Notes:
  861. -
  862. \*----------------------------------------------------------------------------*/
  863. hook OnScriptInit()
  864. {
  865. // Set the default provider as any script.
  866. //YSI_g_sCommandFlags = 0xFF;
  867. P:1("Command_OnScriptInit called");
  868. // Initialise the tree.
  869. #if YSIM_NOT_CLIENT
  870. Bintree_Reset(YSI_g_sSearchTree);
  871. //YSI_g_sMaster23 = getproperty(8, "x@");
  872. #endif
  873. // Make a list of unused commands.
  874. for (new i = 0; i != MAX_COMMANDS - 1; ++i)
  875. {
  876. YSI_g_sCommands[i][E_COMMANDS_FUNC_POINTER] = i + 1;
  877. }
  878. YSI_g_sCommands[MAX_COMMANDS - 1][E_COMMANDS_FUNC_POINTER] = -1;
  879. // Loop through all the possible commands. Note that this may need to add
  880. // commands to the remote command system if this is not the master system.
  881. // The way the master system is designed means that we will know if we are
  882. // master or not by the time this function is called.
  883. new
  884. buffer[32],
  885. idx;
  886. // This is the only place where AMX_FastString is used instead of
  887. // Commands_FastString as the strings in the AMX are not the same as packed
  888. // strings - they are in different memory orders.
  889. while ((idx = AMX_GetPublicNamePrefix(idx, buffer, _A<@yC_>)))
  890. {
  891. Command_Add(buffer, _@);
  892. //Command_Add(unpack(buffer), _@);
  893. //Command_Add(unpack(buffer), _@);
  894. P:2("Command_OnScriptInit: Adding %s", unpack(buffer));
  895. }
  896. Command_Generate();
  897. // Now that all commands have been added to the array, sort it.
  898. // Now call the next constructor.
  899. if (funcidx("OnPlayerCommandPerformed") != -1)
  900. {
  901. Command_IncOPCP();
  902. }
  903. if (funcidx("OnPlayerCommandReceived") != -1)
  904. {
  905. Command_IncOPCR();
  906. }
  907. }
  908. // Forwards for initial command callbacks.
  909. forward OnPlayerCommandReceived(playerid, cmdtext[]);
  910. forward OnPlayerCommandPerformed(playerid, cmdtext[], success);
  911. hook OnScriptExit()
  912. {
  913. P:1("Commands_OnScriptExit called");
  914. if (funcidx("OnPlayerCommandPerformed") != -1)
  915. {
  916. Command_DecOPCP();
  917. }
  918. if (funcidx("OnPlayerCommandReceived") != -1)
  919. {
  920. Command_DecOPCR();
  921. }
  922. return 1;
  923. }
  924. /*----------------------------------------------------------------------------*\
  925. Function:
  926. OnScriptClose
  927. Params:
  928. script - ID of the closing script.
  929. Return:
  930. -
  931. Notes:
  932. Called when a script under the control of the master system ends.
  933. \*----------------------------------------------------------------------------*/
  934. #if YSIM_HAS_MASTER && YSIM_NOT_CLIENT
  935. #if _YSIM_IS_CLOUD
  936. public OnScriptClose(script) <>
  937. {
  938. CallLocalFunction("Command_OnScriptClose", "i", script);
  939. }
  940. public OnScriptClose(script) <_YCM:y>
  941. #elseif _YSIM_IS_STUB
  942. #error y_commands called with _YSIM_IS_STUB.
  943. #else
  944. public OnScriptClose(script)
  945. #endif
  946. {
  947. new
  948. mask = ~(1 << script);
  949. for (new i = 0; i != MAX_COMMANDS; ++i)
  950. {
  951. if (Command_GetPointer(i) == i && (YSI_g_sCommands[i][E_COMMANDS_MASTERS] &= mask) == 0)
  952. {
  953. for (new j = 0; j != MAX_COMMANDS; ++j)
  954. {
  955. // Remove all alternate names.
  956. if (Command_GetPointer(j) == i)
  957. {
  958. YSI_g_sCommands[j][E_COMMANDS_FUNC_POINTER] = YSI_g_sCommandIndex;
  959. YSI_g_sCommandIndex = j;
  960. YSI_g_sCommands[j][E_COMMANDS_MASTERS] = 0;
  961. //Bintree_Delete(YSI_g_sSearchTree, j, YSI_g_sCommandCount);
  962. Command_RemoveFromBintree(j);
  963. --YSI_g_sCommandCount;
  964. //Bintree_Add(YSI_g_sSearchTree, YSI_g_sCommandIndex, hash, YSI_g_sCommandIndex);
  965. }
  966. }
  967. }
  968. /*new
  969. p = i;
  970. while (YSI_g_sCommands[p][E_COMMANDS_MASTERS] == -1)
  971. {
  972. p = Command_GetPointer(p);
  973. }
  974. if (YSI_g_sCommands[p][E_COMMANDS_FUNC_POINTER] == p && (YSI_g_sCommands[p][E_COMMANDS_MASTERS] &= mask) == 0)
  975. {
  976. // Remove all alternate versions of commands - done backwards by
  977. // an alternate command checking its parent. Can also handle
  978. // alternate versions of alternate commands (note this is the
  979. // only code that can currently).
  980. YSI_g_sCommands[i][E_COMMANDS_MASTERS] = 0;
  981. YSI_g_sCommands[i][E_COMMANDS_FUNC_POINTER] = YSI_g_sCommandIndex;
  982. YSI_g_sCommandIndex = i;
  983. // Mark it as invalid in this instance.
  984. YSI_g_sCommands[i][E_COMMANDS_MASTERS] = 0;
  985. // Reduce the number of commands.
  986. --YSI_g_sCommandCount;
  987. }*/
  988. }
  989. //YSI_g_sMaster23 = getproperty(8, "x@");
  990. // I can add better removal code later. Done ^
  991. CallLocalFunction("Command_OnScriptClose", "i", script);
  992. }
  993. // Don't need ALS here as we did it supporting it at the start.
  994. #undef OnScriptClose
  995. #define OnScriptClose Command_OnScriptClose
  996. forward Command_OnScriptClose(script);
  997. #endif
  998. /*----------------------------------------------------------------------------*\
  999. Function:
  1000. OnPlayerConnect
  1001. Params:
  1002. -
  1003. Return:
  1004. -
  1005. Notes:
  1006. -
  1007. \*----------------------------------------------------------------------------*/
  1008. // TODO: Rewrite!
  1009. //RA:Command_OnPlayerConnect(playerid)
  1010. #if YSIM_NOT_CLIENT
  1011. mhook OnPlayerConnect(playerid)
  1012. {
  1013. //#if YSIM_NOT_CLIENT
  1014. //YSI_g_sCommandDialog[playerid] = -1;
  1015. #if YSIM_HAS_MASTER
  1016. YSI_g_sPlayerProvider{playerid} = 0xFF;
  1017. #endif
  1018. NO_GROUPS()
  1019. {
  1020. new
  1021. slot = Bit_Slot(playerid) + 1,
  1022. Bit:mask = Bit_Mask(playerid); //Bit:(1 << (playerid & (cellbits - 1)));
  1023. for (new i = 0; i != MAX_COMMANDS; ++i)
  1024. {
  1025. YSI_g_sCommands[i][E_COMMANDS_PLAYERS][slot] |= mask;
  1026. //PA+(YSI_g_sCommands[i][E_COMMANDS_PLAYERS], mask);
  1027. }
  1028. }
  1029. //#endif
  1030. // Groups will ALWAYS be called after this function - so this can reset
  1031. // player permissions however it likes with the group system then being
  1032. // able to override anything set in here.
  1033. return 1;
  1034. //ALS_CALL<PlayerConnect, i>(playerid)
  1035. }
  1036. #endif
  1037. /*----------------------------------------------------------------------------*\
  1038. Function:
  1039. Command_
  1040. Params:
  1041. command - Command to declare.
  1042. Return:
  1043. -
  1044. Notes:
  1045. Deprecated!
  1046. \*----------------------------------------------------------------------------*/
  1047. #define Command_(%1) \
  1048. CMD:%1(playerid,params[],help)
  1049. /*----------------------------------------------------------------------------*\
  1050. Function:
  1051. ycmd
  1052. Params:
  1053. command[] - Command to register.
  1054. Return:
  1055. -
  1056. Notes:
  1057. Deprecated!
  1058. \*----------------------------------------------------------------------------*/
  1059. #define ycmd(%1);
  1060. /*----------------------------------------------------------------------------*\
  1061. Function:
  1062. Command_FindFast
  1063. Params:
  1064. data[] - Function name to find.
  1065. value - Hash of function name.
  1066. Return:
  1067. Position in functions array or COMMAND_NOT_FOUND.
  1068. Notes:
  1069. -
  1070. \*----------------------------------------------------------------------------*/
  1071. #if YSIM_HAS_MASTER
  1072. static stock Command_FindFast(data[], value, provider = 0xFF)
  1073. #else
  1074. static stock Command_FindFast(data[], value)
  1075. #endif
  1076. {
  1077. new
  1078. leaf,
  1079. pointer;
  1080. #if YSIM_HAS_MASTER
  1081. P:4("Command_FindFast called: \"%s\", %i, %i", data, value, provider);
  1082. provider <<= 24;
  1083. P:5("Command_FindFast: Searching for %s", data);
  1084. if (Command_HasMultipleProviders())
  1085. {
  1086. /*if (provider == 0xFF)
  1087. {
  1088. while ((pointer = Bintree_FindValue(YSI_g_sSearchTree, value, leaf)) != BINTREE_NOT_FOUND)
  1089. {
  1090. //new
  1091. // p = YSI_g_sCommands[pointer][E_COMMANDS_FUNC_POINTER] & 0xFF000000;
  1092. //P:7("Command_FindFast: providers %d %d %d", Command_HasMultipleProviders(), (p & 0xFF000000 != 0xFF000000), (p >>> 24 != provider));
  1093. // Any script will do.
  1094. //if ((YSI_g_sCommands[pointer][E_COMMANDS_FUNC_POINTER] & 0xFF000000) != 0xFF000000) continue;
  1095. //if (p != 0xFF000000 && p != provider) continue;
  1096. // Don't do an strcmp if there are no collisions.
  1097. P:7("Command_FindFast: collisions %d", !Command_HasCollisions());
  1098. if (!Command_HasCollisions()) return pointer;
  1099. //if (!strcmp(YSI_g_sCommands[i][E_COMMANDS_FUNCTION][1], funcname) &&
  1100. P:7("Command_FindFast: strcmp %d", !strcmp(Command_Name(pointer), data));
  1101. if (!strcmp(Command_Name(pointer), data)) return pointer;
  1102. //if (!strcmp(Command_Name(pointer), data) && (Command_GetProvider(pointer) == 0xFF || Command_GetProvider(pointer) == provider)) return pointer;
  1103. }
  1104. }
  1105. else
  1106. {*/
  1107. while ((pointer = Bintree_FindValue(YSI_g_sSearchTree, value, leaf)) != BINTREE_NOT_FOUND)
  1108. {
  1109. //new
  1110. // p = YSI_g_sCommands[pointer][E_COMMANDS_FUNC_POINTER] & 0xFF000000;
  1111. //P:7("Command_FindFast: providers %d %d %d", Command_HasMultipleProviders(), (p & 0xFF000000 != 0xFF000000), (p >>> 24 != provider));
  1112. //if (p != provider && p != 0xFF000000) continue;
  1113. // Only one provider will do.
  1114. if ((YSI_g_sCommands[pointer][E_COMMANDS_FUNC_POINTER] & 0xFF000000) != provider) continue;
  1115. // Don't do an strcmp if there are no collisions.
  1116. P:7("Command_FindFast: collisions %d", Command_HasCollisions());
  1117. if (!Command_HasCollisions()) return pointer;
  1118. //if (!strcmp(YSI_g_sCommands[i][E_COMMANDS_FUNCTION][1], funcname) &&
  1119. P:7("Command_FindFast: strcmp %d", !strcmp(Command_Name(pointer), data));
  1120. if (!strcmp(Command_Name(pointer), data)) return pointer;
  1121. //if (!strcmp(Command_Name(pointer), data) && (Command_GetProvider(pointer) == 0xFF || Command_GetProvider(pointer) == provider)) return pointer;
  1122. }
  1123. //}
  1124. }
  1125. else
  1126. {
  1127. #endif
  1128. while ((pointer = Bintree_FindValue(YSI_g_sSearchTree, value, leaf)) != BINTREE_NOT_FOUND)
  1129. {
  1130. //new
  1131. // p = YSI_g_sCommands[pointer][E_COMMANDS_FUNC_POINTER] & 0xFF000000;
  1132. //P:7("Command_FindFast: providers %d %d %d", Command_HasMultipleProviders(), (p & 0xFF000000 != 0xFF000000), (p >>> 24 != provider));
  1133. //if (Command_HasMultipleProviders() && (p != 0xFF000000) && (p != provider)) continue;
  1134. // Don't do an strcmp if there are no collisions.
  1135. P:7("Command_FindFast: collisions %d", Command_HasCollisions());
  1136. if (!Command_HasCollisions()) return pointer;
  1137. //if (!strcmp(YSI_g_sCommands[i][E_COMMANDS_FUNCTION][1], funcname) &&
  1138. P:7("Command_FindFast: strcmp %d", !strcmp(Command_Name(pointer), data));
  1139. if (!strcmp(Command_Name(pointer), data)) return pointer;
  1140. //if (!strcmp(Command_Name(pointer), data) && (Command_GetProvider(pointer) == 0xFF || Command_GetProvider(pointer) == provider)) return pointer;
  1141. }
  1142. #if YSIM_HAS_MASTER
  1143. }
  1144. #endif
  1145. P:5("Command_FindFast: Not found");
  1146. return COMMAND_NOT_FOUND;
  1147. }
  1148. static stock Command_FindFastStrict(data[], value, provider)
  1149. {
  1150. #if YSIM_HAS_MASTER
  1151. P:4("Command_FindFastStrict called: \"%s\", %i, %i", data, value, provider);
  1152. #else
  1153. #pragma unused provider
  1154. #endif
  1155. new
  1156. leaf,
  1157. pointer;
  1158. P:5("Command_FindFast: Searching for %s", data);
  1159. while ((pointer = Bintree_FindValue(YSI_g_sSearchTree, value, leaf)) != BINTREE_NOT_FOUND)
  1160. {
  1161. #if YSIM_HAS_MASTER
  1162. if (Command_HasMultipleProviders() && (YSI_g_sCommands[pointer][E_COMMANDS_FUNC_POINTER] >>> 24 != provider)) continue;
  1163. #endif
  1164. // Don't do an strcmp if there are no collisions.
  1165. if (!Command_HasCollisions()) return pointer;
  1166. if (!strcmp(Command_Name(pointer), data)) return pointer;
  1167. }
  1168. P:5("Command_FindFast: Not found");
  1169. return COMMAND_NOT_FOUND;
  1170. }
  1171. /*----------------------------------------------------------------------------*\
  1172. Function:
  1173. Command_FindSlow
  1174. Params:
  1175. funcname[] - Function to find.
  1176. Return:
  1177. -
  1178. Notes:
  1179. Searches through the array for function linearly - used to set altnames
  1180. before the data has been sorted.
  1181. \*----------------------------------------------------------------------------*/
  1182. #if YSIM_HAS_MASTER
  1183. static stock Command_FindSlow(funcname[], provider = 0xFF)
  1184. #else
  1185. static stock Command_FindSlow(funcname[])
  1186. #endif
  1187. {
  1188. #if YSIM_HAS_MASTER
  1189. P:4("Command_FindSlow called: \"%s\", %i", funcname, provider);
  1190. #endif
  1191. for (new i = 0; i != MAX_COMMANDS; ++i)
  1192. {
  1193. #if YSIM_HAS_MASTER
  1194. new
  1195. p = YSI_g_sCommands[i][E_COMMANDS_FUNC_POINTER];
  1196. if (((p & 0x00FFFFFF == i) || (YSI_g_sCommands[i][E_COMMANDS_MASTERS] == -1)) &&
  1197. (!Command_HasMultipleProviders() || (p & 0xFF000000 == 0xFF000000) || (p >>> 24 == provider)) &&
  1198. !strcmp(Command_Name(i), funcname))
  1199. #else
  1200. if (YSI_g_sCommands[i][E_COMMANDS_FUNC_POINTER] == i && !strcmp(Command_Name(i), funcname))
  1201. #endif
  1202. {
  1203. return i;
  1204. }
  1205. }
  1206. return COMMAND_NOT_FOUND;
  1207. }
  1208. /*----------------------------------------------------------------------------*\
  1209. Function:
  1210. Command_FindSlowStrict
  1211. Params:
  1212. funcname[] - Function to find.
  1213. Return:
  1214. -
  1215. Notes:
  1216. Searches through the array for function linearly - used to set altnames
  1217. before the data has been sorted.
  1218. \*----------------------------------------------------------------------------*/
  1219. static stock Command_FindSlowStrict(funcname[], provider)
  1220. {
  1221. #if YSIM_HAS_MASTER
  1222. P:4("Command_FindSlowStrict called: \"%s\", %i", funcname, provider);
  1223. #else
  1224. #pragma unused provider
  1225. #endif
  1226. P:2("Command_FindSlowStrict: Searching for %s", unpack(funcname));
  1227. for (new i = 0; i != MAX_COMMANDS; ++i)
  1228. {
  1229. #if YSIM_HAS_MASTER
  1230. new
  1231. p = YSI_g_sCommands[i][E_COMMANDS_FUNC_POINTER];
  1232. P:6("Command_FindSlowStrict: %s %08x %d %d", unpack(funcname), Command_Name(i), p & 0xFFFFFF, i);
  1233. // This needs additional checks that the item is valid.
  1234. if (((p & 0x00FFFFFF == i) || (YSI_g_sCommands[i][E_COMMANDS_MASTERS] == -1)) &&
  1235. (!Command_HasMultipleProviders() || (p >>> 24 == provider)) &&
  1236. !strcmp(Command_Name(i), funcname))
  1237. #else
  1238. if (YSI_g_sCommands[i][E_COMMANDS_FUNC_POINTER] == i && !strcmp(Command_Name(i), funcname))
  1239. #endif
  1240. {
  1241. return i;
  1242. }
  1243. }
  1244. return COMMAND_NOT_FOUND;
  1245. }
  1246. /*----------------------------------------------------------------------------*\
  1247. Function:
  1248. Command_AddHash
  1249. Params:
  1250. command[] - Command text to hash.
  1251. dest[] - Array to copy to.
  1252. idx - Point to start copying from.
  1253. Return:
  1254. hash value.
  1255. Notes:
  1256. Hashes a string and copies it to a destination at the same time.
  1257. \*----------------------------------------------------------------------------*/
  1258. static stock Command_AddHash(command[], dest[], idx)
  1259. {
  1260. P:4("Command_AddHash called: \"%s\", %i, %i", command, dest, idx);
  1261. // Skip the function name prefix.
  1262. new
  1263. hash = -1,
  1264. ch,
  1265. dx = 1,
  1266. end = idx + 28;
  1267. // Copy and hash at the same time.
  1268. do
  1269. {
  1270. /*ch = TOLOWER(command[idx++]);
  1271. // Always NULL terminate.
  1272. if ((dest[dx] = ch << 24) == COMMAND_END_CMP << 24)
  1273. {
  1274. // Fixes a bug with commands multiples of 4 chars long.
  1275. dest[dx] = 0;
  1276. break;
  1277. }*/
  1278. ch = TOLOWER(command[idx++]);
  1279. if (ch == COMMAND_END_CMP) break;
  1280. dest[dx] = ch << 24;
  1281. hash = hash * 33 + ch; //Command_ToUpper(ch);
  1282. ch = TOLOWER(command[idx++]);
  1283. if (ch == COMMAND_END_CMP) break;
  1284. dest[dx] |= ch << 16;
  1285. hash = hash * 33 + ch; //Command_ToUpper(ch);
  1286. ch = TOLOWER(command[idx++]);
  1287. if (ch == COMMAND_END_CMP) break;
  1288. dest[dx] |= ch << 8;
  1289. hash = hash * 33 + ch; //Command_ToUpper(ch);
  1290. ch = TOLOWER(command[idx++]);
  1291. if (ch == COMMAND_END_CMP) break;
  1292. dest[dx] |= ch << 0;
  1293. hash = hash * 33 + ch; //Command_ToUpper(ch);
  1294. ++dx;
  1295. }
  1296. while (idx < end);
  1297. return hash;
  1298. }
  1299. /*----------------------------------------------------------------------------*\
  1300. Function:
  1301. Command_AddHashPacked
  1302. Params:
  1303. command[] - Packed command text to hash.
  1304. dest[] - Array to copy to.
  1305. idx - Point to start copying from.
  1306. Return:
  1307. hash value.
  1308. Notes:
  1309. Hashes a string and copies it to a destination at the same time.
  1310. \*----------------------------------------------------------------------------*/
  1311. static stock Command_AddHashPacked(command[], dest[], idx)
  1312. {
  1313. P:4("Command_AddHashPacked called: \"%s\", %i, %i", command, dest, idx);
  1314. // Skip the function name prefix.
  1315. new
  1316. hash = -1,
  1317. ch,
  1318. dx = 1,
  1319. end = idx + 28;
  1320. // Copy and hash at the same time.
  1321. do
  1322. {
  1323. /*ch = TOLOWER(command[idx++]);
  1324. // Always NULL terminate.
  1325. if ((dest[dx] = ch << 24) == COMMAND_END_CMP << 24)
  1326. {
  1327. // Fixes a bug with commands multiples of 4 chars long.
  1328. dest[dx] = 0;
  1329. break;
  1330. }*/
  1331. ch = TOLOWER(command{idx++});
  1332. if (ch == COMMAND_END_CMP) break;
  1333. dest[dx] = ch << 24;
  1334. hash = hash * 33 + ch; //Command_ToUpper(ch);
  1335. ch = TOLOWER(command{idx++});
  1336. if (ch == COMMAND_END_CMP) break;
  1337. dest[dx] |= ch << 16;
  1338. hash = hash * 33 + ch; //Command_ToUpper(ch);
  1339. ch = TOLOWER(command{idx++});
  1340. if (ch == COMMAND_END_CMP) break;
  1341. dest[dx] |= ch << 8;
  1342. hash = hash * 33 + ch; //Command_ToUpper(ch);
  1343. ch = TOLOWER(command{idx++});
  1344. if (ch == COMMAND_END_CMP) break;
  1345. dest[dx] |= ch << 0;
  1346. hash = hash * 33 + ch; //Command_ToUpper(ch);
  1347. ++dx;
  1348. }
  1349. while (idx < end);
  1350. return hash;
  1351. }
  1352. /*----------------------------------------------------------------------------*\
  1353. Function:
  1354. Command_FastHash
  1355. Params:
  1356. command[] - Command text to hash.
  1357. Return:
  1358. hash value.
  1359. Notes:
  1360. Just hashes the passed string.
  1361. \*----------------------------------------------------------------------------*/
  1362. static stock Command_FastHash(command[])
  1363. {
  1364. P:4("Command_FastHash called: \"%s\"", command);
  1365. new
  1366. index = 0,
  1367. hash = -1,
  1368. ch;
  1369. while ((ch = command[index++])) hash = hash * 33 + TOLOWER(ch);
  1370. return hash;
  1371. }
  1372. /*----------------------------------------------------------------------------*\
  1373. Function:
  1374. Command_PackHash
  1375. Params:
  1376. command[] - Command text to hash.
  1377. Return:
  1378. hash value.
  1379. Notes:
  1380. Hashes packed strings.
  1381. \*----------------------------------------------------------------------------*/
  1382. static stock Command_PackHash(command[])
  1383. {
  1384. P:4("Command_PackHash called: \"%s\"", command);
  1385. new
  1386. index = 0,
  1387. hash = -1,
  1388. ch;
  1389. while ((ch = command[index++]))
  1390. {
  1391. P:4("Commands_PackHash: ch = 0x%04x%04x", ch >>> 16, ch & 0xFFFF);
  1392. if (ch & 0xFF000000)
  1393. {
  1394. hash = hash * 33 + TOLOWER(ch >>> 24);
  1395. P:5("Command_PackHash: Hash1 = %d", hash);
  1396. }
  1397. else
  1398. {
  1399. break;
  1400. }
  1401. if (ch & 0x00FF0000)
  1402. {
  1403. hash = hash * 33 + TOLOWER(ch >> 16 & 0xFF);
  1404. P:5("Command_PackHash: Hash2 = %d", hash);
  1405. }
  1406. else
  1407. {
  1408. break;
  1409. }
  1410. if (ch & 0x0000FF00)
  1411. {
  1412. hash = hash * 33 + TOLOWER(ch >> 8 & 0xFF);
  1413. P:5("Command_PackHash: Hash3 = %d", hash);
  1414. }
  1415. else
  1416. {
  1417. break;
  1418. }
  1419. if (ch & 0x000000FF)
  1420. {
  1421. hash = hash * 33 + TOLOWER(ch & 0xFF);
  1422. P:5("Command_PackHash: Hash4 = %d", hash);
  1423. }
  1424. else
  1425. {
  1426. break;
  1427. }
  1428. }
  1429. return hash;
  1430. }
  1431. /*----------------------------------------------------------------------------*\
  1432. Function:
  1433. Command_Hash
  1434. Params:
  1435. command[] - Command text to hash.
  1436. &index - Start point and variable to store end point to.
  1437. &length - Length of the hashed word.
  1438. Return:
  1439. hash value.
  1440. Notes:
  1441. Hashes a string using space delimiters and returns information such as the
  1442. length of the string hased and the start point of the next word.
  1443. \*----------------------------------------------------------------------------*/
  1444. static stock Command_Hash(command[], &index, &length)
  1445. {
  1446. P:4("Command_Hash called: \"%s\", %i, %i", command, index, length);
  1447. new
  1448. hash = -1,
  1449. ch;
  1450. length = index;
  1451. while ((ch = command[index++]) > ' ') hash = hash * 33 + TOLOWER(ch);
  1452. length = index - length - 1;
  1453. while (ch)
  1454. {
  1455. if (ch > ' ')
  1456. {
  1457. break;
  1458. }
  1459. ch = command[index++];
  1460. }
  1461. --index;
  1462. return hash;
  1463. }
  1464. /*----------------------------------------------------------------------------*\
  1465. Function:
  1466. Command_Find
  1467. Params:
  1468. function[] - Function name to find.
  1469. Return:
  1470. Position in functions array or COMMAND_NOT_FOUND.
  1471. Notes:
  1472. Used by API functions to avoid repeated sorting checks.
  1473. \*----------------------------------------------------------------------------*/
  1474. #if YSIM_HAS_MASTER
  1475. static stock Command_Find(function[], provider = -1)
  1476. #else
  1477. static stock Command_Find(function[])
  1478. #endif
  1479. {
  1480. #if YSIM_HAS_MASTER
  1481. P:4("Command_Find called: \"%s\", %i", function, provider);
  1482. if (provider == -1)
  1483. {
  1484. provider = _:Command_DefaultProvider();
  1485. // Find the ID of the command.
  1486. if (Command_IsSorted())
  1487. {
  1488. return Command_FindFast(function, Command_FastHash(function), provider);
  1489. }
  1490. else
  1491. {
  1492. return Command_FindSlow(function, provider);
  1493. }
  1494. }
  1495. else
  1496. {
  1497. provider &= 0xFF;
  1498. // Find the ID of the command.
  1499. if (Command_IsSorted())
  1500. {
  1501. return Command_FindFastStrict(function, Command_FastHash(function), provider);
  1502. }
  1503. else
  1504. {
  1505. return Command_FindSlowStrict(function, provider);
  1506. }
  1507. }
  1508. #else
  1509. if (Command_IsSorted())
  1510. {
  1511. return Command_FindFast(function, Command_FastHash(function));
  1512. }
  1513. else
  1514. {
  1515. return Command_FindSlow(function);
  1516. }
  1517. #endif
  1518. }
  1519. /*----------------------------------------------------------------------------*\
  1520. Function:
  1521. Command_GetID
  1522. Params:
  1523. function[] - Function name to find.
  1524. Return:
  1525. The ID of the passed function.
  1526. Notes:
  1527. -
  1528. native Command_GetID(function[])
  1529. \*----------------------------------------------------------------------------*/
  1530. foreign Command_GetID(string:function[]);
  1531. global Command_GetID(string:function[])
  1532. {
  1533. P:2("Command_GetID called: \"%s\"", function);
  1534. return Command_Find(function);
  1535. }
  1536. /*----------------------------------------------------------------------------*\
  1537. Function:
  1538. Command_SetPlayer
  1539. Params:
  1540. command - Command to set for.
  1541. playerid - Player to set.
  1542. bool:set - Wether or not this player can use this command.
  1543. Return:
  1544. -
  1545. Notes:
  1546. -
  1547. native bool:Command_SetPlayer(command, playerid, bool:set);
  1548. \*----------------------------------------------------------------------------*/
  1549. foreign Command_SetPlayer(c,p,bool:s);
  1550. global Command_SetPlayer(c,p,bool:s)
  1551. {
  1552. P:2("Command_SetPlayer called: %i, %i, %i", c, p, s);
  1553. // if (c < 0 || c >= YSI_g_sCommandIndex)
  1554. if (0 <= c < MAX_COMMANDS)
  1555. {
  1556. //Bit_Set(YSI_g_sCommands[c][E_COMMANDS_PLAYERS], p, s, bits<MAX_PLAYERS>);
  1557. //if (s) PA+(YSI_g_sCommands[c][E_COMMANDS_PLAYERS], p);
  1558. //else PA-(YSI_g_sCommands[c][E_COMMANDS_PLAYERS], p);
  1559. PA_Set(YSI_g_sCommands[c][E_COMMANDS_PLAYERS], p, s);
  1560. // Not in range,
  1561. // return false;
  1562. }
  1563. return 1;
  1564. // return s;
  1565. }
  1566. /*----------------------------------------------------------------------------*\
  1567. Function:
  1568. Command_SetPlayerNamed
  1569. Params:
  1570. funcname[] - Command to set for.
  1571. playerid - Player to set.
  1572. set - Wether or not this player can use this command.
  1573. Return:
  1574. -
  1575. Notes:
  1576. Like Command_SetPlayer but for a function name.
  1577. native bool:Command_SetPlayerNamed(funcname[], playerid, bool:set);
  1578. \*----------------------------------------------------------------------------*/
  1579. foreign Command_SetPlayerNamed(string:f[],p,bool:s);
  1580. global Command_SetPlayerNamed(string:f[],p,bool:s)
  1581. {
  1582. P:2("Command_SetPlayerNamed called: \"%s\", %i, %i", f, p, s);
  1583. Command_SetPlayer(Command_Find(f), p, s);
  1584. return 1;
  1585. }
  1586. /*----------------------------------------------------------------------------*\
  1587. Function:
  1588. Command_GetPlayer
  1589. Params:
  1590. command - Command to get for.
  1591. playerid - Player to get.
  1592. Return:
  1593. Wether this player can use this command.
  1594. Notes:
  1595. -
  1596. native bool:Command_GetPlayer(command, playerid);
  1597. \*----------------------------------------------------------------------------*/
  1598. foreign bool:Command_GetPlayer(command, playerid);
  1599. global bool:Command_GetPlayer(command, playerid)
  1600. {
  1601. P:2("bool:Command_GetPlayer called: %i, %i", command, playerid);
  1602. if (0 <= command < MAX_COMMANDS)
  1603. {
  1604. return bool:Command_CheckPlayer(command, playerid);
  1605. }
  1606. // Not in range,
  1607. return false;
  1608. }
  1609. /*----------------------------------------------------------------------------*\
  1610. Function:
  1611. Command_GetPlayerNamed
  1612. Params:
  1613. funcname[] - Command to get for.
  1614. playerid - Player to get.
  1615. Return:
  1616. -
  1617. Notes:
  1618. Like Command_GetPlayer but for a function name.
  1619. native bool:Command_GetPlayerNamed(funcname[], playerid);
  1620. \*----------------------------------------------------------------------------*/
  1621. foreign bool:Command_GetPlayerNamed(string:func[], playerid);
  1622. global bool:Command_GetPlayerNamed(string:func[], playerid)
  1623. {
  1624. P:2("bool:Command_GetPlayerNamed called: \"%s\", %i", func, playerid);
  1625. return Command_GetPlayer(Command_Find(func), playerid);
  1626. }
  1627. /*----------------------------------------------------------------------------*\
  1628. Function:
  1629. Command_Remove
  1630. Params:
  1631. func - The slot of the command to remove.
  1632. Return:
  1633. -
  1634. Notes:
  1635. native Command_Remove(func);
  1636. \*----------------------------------------------------------------------------*/
  1637. static stock Command_RemoveFromBintree(func)
  1638. {
  1639. P:4("Command_RemoveFromBintree called: %i", func);
  1640. if (!Command_IsSorted()) return;
  1641. // This function has to find the right index in the binary tree, as that's
  1642. // not in the same order at all.
  1643. new
  1644. leaf,
  1645. hash = Command_PackHash(Command_Name(func));
  1646. // Find where in the binary tree this is referenced from.
  1647. while (Bintree_FindValue(YSI_g_sSearchTree, hash, _, leaf) != BINTREE_NOT_FOUND)
  1648. {
  1649. if (YSI_g_sSearchTree[leaf][E_BINTREE_TREE_POINTER] == func)
  1650. {
  1651. P:2("Command_RemoveFromBintree: Delete branch");
  1652. Bintree_Delete(YSI_g_sSearchTree, leaf, YSI_g_sCommandCount);
  1653. return;
  1654. }
  1655. }
  1656. }
  1657. foreign Command_Remove(func);
  1658. global Command_Remove(func)
  1659. {
  1660. P:2("Command_Remove called: %i", func);
  1661. if (0 <= func < MAX_COMMANDS)
  1662. {
  1663. if (Command_GetPointer(func) == func)
  1664. {
  1665. for (new i = 0; i != MAX_COMMANDS; ++i)
  1666. {
  1667. // Remove all alternate names.
  1668. if (Command_GetPointer(i) == func)
  1669. {
  1670. // Add this to the list of unused functions.
  1671. YSI_g_sCommands[i][E_COMMANDS_FUNC_POINTER] = YSI_g_sCommandIndex;
  1672. YSI_g_sCommandIndex = i;
  1673. // Mark it as invalid in this instance.
  1674. #if YSIM_HAS_MASTER
  1675. YSI_g_sCommands[i][E_COMMANDS_MASTERS] = 0;
  1676. #endif
  1677. // Reduce the number of commands.
  1678. //Bintree_Delete(YSI_g_sSearchTree, i, YSI_g_sCommandCount);
  1679. Command_RemoveFromBintree(i);
  1680. --YSI_g_sCommandCount;
  1681. }
  1682. }
  1683. }
  1684. // Remove a single alternate name.
  1685. YSI_g_sCommands[func][E_COMMANDS_FUNC_POINTER] = YSI_g_sCommandIndex;
  1686. YSI_g_sCommandIndex = func;
  1687. #if YSIM_HAS_MASTER
  1688. YSI_g_sCommands[func][E_COMMANDS_MASTERS] = 0;
  1689. #endif
  1690. //Bintree_Delete(YSI_g_sSearchTree, func, YSI_g_sCommandCount);
  1691. Command_RemoveFromBintree(func);
  1692. --YSI_g_sCommandCount;
  1693. return 1;
  1694. }
  1695. return 0;
  1696. }
  1697. foreign Command_RemoveNamed(string:func[]);
  1698. global Command_RemoveNamed(string:func[])
  1699. {
  1700. P:2("Command_RemoveNamed called: \"%s\"", func);
  1701. return Command_Remove(Command_Find(func));
  1702. }
  1703. /*----------------------------------------------------------------------------*\
  1704. Function:
  1705. Command_Add
  1706. Params:
  1707. funcname[] - The function to add to the array.
  1708. script - The script ID with the function.
  1709. Return:
  1710. -
  1711. Notes:
  1712. If the list of commands have already been sorted into the binary tree the
  1713. new commands will be appended, otherwise they will just be added to the
  1714. array.
  1715. native Command_Add(funcname[], script);
  1716. \*----------------------------------------------------------------------------*/
  1717. foreign Command_Add(string:f[],s);
  1718. global Command_Add(string:f[],s)
  1719. {
  1720. #if !YSIM_HAS_MASTER
  1721. #pragma unused s
  1722. #endif
  1723. P:2("Command_Add called: \"%s\", %i", f, s);
  1724. if (YSI_g_sCommandCount < MAX_COMMANDS && YSI_g_sCommandIndex != -1)
  1725. {
  1726. #if !YSIM_HAS_MASTER
  1727. static
  1728. provider = -1;
  1729. #endif
  1730. new
  1731. #if YSIM_HAS_MASTER
  1732. provider = _:Command_DefaultProvider(),
  1733. #endif
  1734. //hash = Command_AddHashPacked(f, YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION], 4),
  1735. // Because of the way the master system works, packed strings are
  1736. // unpacked when they are passed remotely, but not when they are
  1737. // passed locally. The unpacking is done by "CallRemoteFunction",
  1738. // not anything we outselves do...
  1739. offset = f[0] == '@' ? 4 : 1,
  1740. hash = f[0] == '@' ?
  1741. Command_AddHash(f, YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION], 4) :
  1742. Command_AddHashPacked(f, YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION], 4);
  1743. //printf("%s", unpack(YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION][1]));
  1744. if (Command_IsSorted())
  1745. {
  1746. new
  1747. idx = Command_FindFastStrict(f[offset], hash, provider);
  1748. if (idx != COMMAND_NOT_FOUND)
  1749. {
  1750. P:4("Command_Add: Command exists");
  1751. #if YSIM_HAS_MASTER
  1752. YSI_g_sCommands[idx][E_COMMANDS_MASTERS] |= 1 << s;
  1753. #endif
  1754. return 0;
  1755. }
  1756. if (!Command_HasCollisions())
  1757. {
  1758. // Check for an existing command with this hash.
  1759. if (Bintree_FindValue(YSI_g_sSearchTree, hash) != BINTREE_NOT_FOUND)
  1760. {
  1761. Command_SetCollisions();
  1762. }
  1763. }
  1764. // Command doesn't exist already - good!
  1765. Bintree_Add(YSI_g_sSearchTree, YSI_g_sCommandIndex, hash, YSI_g_sCommandIndex);
  1766. }
  1767. else
  1768. {
  1769. new
  1770. idx = Command_FindSlowStrict(f[offset], provider);
  1771. if (idx != COMMAND_NOT_FOUND)
  1772. {
  1773. P:4("Command_Add: Command exists");
  1774. #if YSIM_HAS_MASTER
  1775. YSI_g_sCommands[idx][E_COMMANDS_MASTERS] |= 1 << s;
  1776. #endif
  1777. return 0;
  1778. }
  1779. }
  1780. YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION][0] = COMMAND_FUNCTION_PREFIX;
  1781. //Bit_SetAll(YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_PLAYERS], true, bits<MAX_PLAYERS>);
  1782. //Command_InitialiseFromGroups(YSI_g_sCommandIndex);
  1783. PA_FastInit(YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_PLAYERS]);
  1784. NO_GROUPS(YSI_g_sCommandIndex)
  1785. {
  1786. PA_Init(YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_PLAYERS], true);
  1787. }
  1788. // Swap these.
  1789. #if YSIM_HAS_MASTER
  1790. YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_MASTERS] = 1 << s;
  1791. #endif
  1792. P:2("Command_Add: Command added in %d (%d)", YSI_g_sCommandIndex, hash);
  1793. hash = YSI_g_sCommandIndex;
  1794. // Set this command as usable in any script.
  1795. P:4("Command_Add: %08x%08x%08x%08x", YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION][0], YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION][1], YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION][2], YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION][3]);
  1796. YSI_g_sCommandIndex = YSI_g_sCommands[hash][E_COMMANDS_FUNC_POINTER];
  1797. #if YSIM_HAS_MASTER
  1798. YSI_g_sCommands[hash][E_COMMANDS_FUNC_POINTER] = hash | (provider << 24);
  1799. #else
  1800. YSI_g_sCommands[hash][E_COMMANDS_FUNC_POINTER] = hash;
  1801. #endif
  1802. /*#if YSIM_HAS_MASTER
  1803. if (s == _@)
  1804. #endif
  1805. {
  1806. new
  1807. addr = funcidx(f) * 8 + AMX_HEADER_PUBLICS;
  1808. #emit LREF.S.pri addr
  1809. #emit STOR.S.pri addr
  1810. YSI_g_sCommands[hash][E_COMMANDS_AMX_ADDRESS] = addr;
  1811. }
  1812. #pragma tabsize 4*/
  1813. // Now some complex debug rubbish.
  1814. C:4(new str[32];strpack(str, f);printf("Command_Add: %08x%08x%08x%08x", str[0], str[1], str[2], str[3]););
  1815. ++YSI_g_sCommandCount;
  1816. }
  1817. return 1;
  1818. /*else
  1819. {
  1820. // Not all hope is lost - check if this command name already exists.
  1821. if (Command_IsSorted())
  1822. {
  1823. new
  1824. pos = Command_FindFast(funcname[4], Command_FastHash(funcname[4]));
  1825. if (pos != COMMAND_NOT_FOUND)
  1826. {
  1827. // Found it already in the array.
  1828. return pos;
  1829. }
  1830. }
  1831. else
  1832. {
  1833. new
  1834. pos = Command_FindSlow(funcname[4]);
  1835. if (pos != COMMAND_NOT_FOUND)
  1836. {
  1837. // Command already exists.
  1838. return pos;
  1839. }
  1840. }
  1841. }
  1842. return COMMAND_NOT_FOUND;*/
  1843. }
  1844. /*----------------------------------------------------------------------------*\
  1845. Function:
  1846. Command_AddAlt
  1847. Params:
  1848. funcidx - The function this is an alternate to.
  1849. altname[] - The new name.
  1850. Return:
  1851. -
  1852. Notes:
  1853. If the list of commands have already been sorted into the binary tree the
  1854. new commands will be appended, otherwise they will just be added to the
  1855. array.
  1856. native Command_AddAlt(funcidx, altname[]);
  1857. \*----------------------------------------------------------------------------*/
  1858. foreign Command_AddAlt(oidx, string:altname[]);
  1859. global Command_AddAlt(oidx, string:altname[])
  1860. {
  1861. P:2("Command_AddAlt called: %i, \"%s\"", oidx, altname);
  1862. if (!Command_IsValid(oidx))
  1863. {
  1864. return COMMAND_NOT_FOUND;
  1865. }
  1866. #if YSIM_HAS_MASTER
  1867. new
  1868. provider = _:Command_DefaultProvider();
  1869. #else
  1870. static
  1871. provider = -1;
  1872. #endif
  1873. if (YSI_g_sCommandCount < MAX_COMMANDS && YSI_g_sCommandIndex != -1)
  1874. {
  1875. new
  1876. hash = Command_AddHash(altname, YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION], 0);
  1877. if (Command_IsSorted())
  1878. {
  1879. // Find the function this mirrors.
  1880. //oidx = Command_FindFast(function, Command_FastHash(function));
  1881. new
  1882. pos = Command_FindFastStrict(altname, hash, provider);
  1883. if (pos != COMMAND_NOT_FOUND)
  1884. {
  1885. if (Command_GetPointer(pos) != oidx)
  1886. {
  1887. // Same altname, different function.
  1888. return COMMAND_NOT_FOUND;
  1889. }
  1890. return pos;
  1891. }
  1892. if (!Command_HasCollisions())
  1893. {
  1894. // Check for an existing command with this hash.
  1895. //if (Bintree_FindValue(YSI_g_sSearchTree, hash) != BINTREE_NOT_FOUND)
  1896. //{
  1897. // Command_SetCollisions();
  1898. //}
  1899. new
  1900. leaf;
  1901. while ((pos = Bintree_FindValue(YSI_g_sSearchTree, hash, leaf)) != BINTREE_NOT_FOUND)
  1902. {
  1903. // Don't have collisions if the providers are different.
  1904. if ((YSI_g_sCommands[pos][E_COMMANDS_FUNC_POINTER] >>> 24) != provider) continue;
  1905. Command_SetCollisions();
  1906. }
  1907. }
  1908. // Command doesn't exist already - good!
  1909. Bintree_Add(YSI_g_sSearchTree, YSI_g_sCommandIndex, hash, YSI_g_sCommandIndex);
  1910. }
  1911. else
  1912. {
  1913. new
  1914. pos = Command_FindSlowStrict(altname, provider);
  1915. if (pos != COMMAND_NOT_FOUND)
  1916. {
  1917. if (Command_GetPointer(pos) != oidx)
  1918. {
  1919. // Same altname, different function.
  1920. return COMMAND_NOT_FOUND;
  1921. }
  1922. // Command already exists.
  1923. return pos;
  1924. }
  1925. }
  1926. YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION][0] = COMMAND_FUNCTION_PREFIX;
  1927. //Bit_SetAll(YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_PLAYERS], true, bits<MAX_PLAYERS>);
  1928. //Command_InitialiseFromGroups(YSI_g_sCommandIndex);
  1929. PA_FastInit(YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_PLAYERS]);
  1930. NO_GROUPS(YSI_g_sCommandIndex)
  1931. {
  1932. PA_Init(YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_PLAYERS], true);
  1933. }
  1934. ++YSI_g_sCommandCount;
  1935. #if YSIM_HAS_MASTER
  1936. // This doesn't have real masters.
  1937. YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_MASTERS] = -1;
  1938. #endif
  1939. P:2("Command_AddAlt: Command added in %d as %d", YSI_g_sCommandIndex, oidx);
  1940. hash = YSI_g_sCommandIndex;
  1941. YSI_g_sCommandIndex = YSI_g_sCommands[hash][E_COMMANDS_FUNC_POINTER];
  1942. #if YSIM_HAS_MASTER
  1943. YSI_g_sCommands[hash][E_COMMANDS_FUNC_POINTER] = oidx | (provider << 24);
  1944. #else
  1945. YSI_g_sCommands[hash][E_COMMANDS_FUNC_POINTER] = oidx;
  1946. #endif
  1947. return hash;
  1948. }
  1949. else
  1950. {
  1951. // Not all hope is lost - check if this command name already exists.
  1952. new
  1953. pos;
  1954. if (Command_IsSorted())
  1955. {
  1956. pos = Command_FindFastStrict(altname, Command_FastHash(altname), provider);
  1957. }
  1958. else
  1959. {
  1960. pos = Command_FindSlowStrict(altname, provider);
  1961. }
  1962. if (pos != COMMAND_NOT_FOUND)
  1963. {
  1964. // Found it already in the array. Check if the element it points to
  1965. // has the correct name so we know this is the correct pair, not
  1966. // just the correct single element. I.e. check that this is the
  1967. // correct original/altname combo, not the right altname on a
  1968. // different original name.
  1969. if (oidx == Command_GetPointer(pos))
  1970. {
  1971. return pos;
  1972. }
  1973. }
  1974. }
  1975. return COMMAND_NOT_FOUND;
  1976. }
  1977. /*----------------------------------------------------------------------------*\
  1978. Function:
  1979. Command_AddAltNamed
  1980. Params:
  1981. function[] - The function this is an alternate to.
  1982. altname[] - The new name.
  1983. Return:
  1984. -
  1985. Notes:
  1986. Add an alternate command for an existing command.
  1987. native Command_AddAltNamed(function[], altname[]);
  1988. \*----------------------------------------------------------------------------*/
  1989. foreign Command_AddAltNamed(string:function[], string:altname[]);
  1990. global Command_AddAltNamed(string:function[], string:altname[])
  1991. {
  1992. P:2("Command_AddAltNamed called: \"%s\", \"%s\"", function, altname);
  1993. return Command_AddAlt(Command_Find(function), altname);
  1994. }
  1995. /*----------------------------------------------------------------------------*\
  1996. Function:
  1997. Command_Debug
  1998. Params:
  1999. -
  2000. Return:
  2001. -
  2002. Notes:
  2003. Print some random information about commands if _DEBUG is set.
  2004. \*----------------------------------------------------------------------------*/
  2005. stock Command_Debug()
  2006. {
  2007. P:3("Command_Debug called: %i");
  2008. #if _DEBUG > 0
  2009. printf("Command_Debug: Start");
  2010. for (new i = 0; i != MAX_COMMANDS; ++i)
  2011. {
  2012. if (Command_IsValid(i))
  2013. {
  2014. printf("Command_Debug: Loop start %d", i);
  2015. new buffer[MAX_COMMAND_LENGTH];
  2016. strunpack(buffer, Command_Name(i));
  2017. printf("%08x%08x%08x", YSI_g_sCommands[i][E_COMMANDS_FUNCTION][0], YSI_g_sCommands[i][E_COMMANDS_FUNCTION][1], YSI_g_sCommands[i][E_COMMANDS_FUNCTION][2]);
  2018. new pointer = Command_GetPointer(i);
  2019. printf("Command %d:", i);
  2020. printf("\t%s", buffer);
  2021. printf("\t%d", pointer);
  2022. printf("\t%d %d %d", YSI_g_sSearchTree[i][E_BINTREE_TREE_LEFT], YSI_g_sSearchTree[i][E_BINTREE_TREE_RIGHT], YSI_g_sSearchTree[i][E_BINTREE_TREE_VALUE]);
  2023. CallLocalFunction(Command_GetFunction(Command_GetPointer(i)), "isi", 0, "hi", 0);
  2024. printf("Command_Debug: Loop end");
  2025. }
  2026. }
  2027. printf("Command_Debug: End");
  2028. #endif
  2029. }
  2030. /*----------------------------------------------------------------------------*\
  2031. Function:
  2032. OnPlayerCommandText
  2033. Params:
  2034. playerid - Player who entered the command.
  2035. cmdtext[] - Text entered.
  2036. Return:
  2037. true - success or hidden fail, false - fail.
  2038. Notes:
  2039. Calls the Command_Process function if this is not a client.
  2040. \*----------------------------------------------------------------------------*/
  2041. #if YSIM_NOT_CLIENT
  2042. mhook OnPlayerCommandText(playerid, cmdtext[])
  2043. {
  2044. P:1("Commands_OnPlayerCommandText");
  2045. if (YSI_g_sCommandFlags & e_COMM_FLAG_OPCR)
  2046. {
  2047. switch (CallRemoteFunction("OnPlayerCommandReceived", "is", playerid, cmdtext))
  2048. {
  2049. case 0:
  2050. // Handle normally, as in ZCMD.
  2051. return 1;
  2052. case -1:
  2053. // Allow them to stop processing but return 0.
  2054. return 0;
  2055. // Do nothing on 1.
  2056. }
  2057. }
  2058. if (YSI_g_sCommandFlags & e_COMM_FLAG_OPCP)
  2059. {
  2060. new
  2061. ret = Command_Process(playerid, cmdtext, 0);
  2062. switch (CallRemoteFunction("OnPlayerCommandPerformed", "isi", playerid, cmdtext, ret))
  2063. {
  2064. case 0:
  2065. return 0;
  2066. case 1:
  2067. return 1;
  2068. //default:
  2069. // Return the original return on -1.
  2070. }
  2071. return ret;
  2072. }
  2073. else
  2074. {
  2075. /*switch (Command_Process(playerid, cmdtext, 0))
  2076. {
  2077. case 1:
  2078. // Found the command, processed.
  2079. return 1;
  2080. case -1:
  2081. // Found the command, but want to return 0 anyway.
  2082. return 0;
  2083. // Do nothing on 0.
  2084. }*/
  2085. return Command_Process(playerid, cmdtext, 0);
  2086. }
  2087. // This can never actually be reached!
  2088. //return 0;
  2089. }
  2090. #endif
  2091. /*----------------------------------------------------------------------------*\
  2092. Function:
  2093. Command_ReProcess
  2094. Params:
  2095. playerid - Player who entered the command.
  2096. cmdtext[] - Text entered.
  2097. help - Called from the help commmand or OnPlayerCommandText.
  2098. Return:
  2099. true - success or hidden fail, false - fail.
  2100. Notes:
  2101. -
  2102. \*----------------------------------------------------------------------------*/
  2103. foreign Command_ReProcess(p,string:c[],h);
  2104. global Command_ReProcess(p,string:c[],h)
  2105. {
  2106. P:2("Command_ReProcess called: %i, \"%s\", %i", p, c, h);
  2107. return Command_Process(p,c,h);
  2108. }
  2109. /*----------------------------------------------------------------------------*\
  2110. Function:
  2111. Command_Process
  2112. Params:
  2113. playerid - Player who entered the command.
  2114. cmdtext[] - Text entered.
  2115. help - Called from the help commmand or OnPlayerCommandText.
  2116. Return:
  2117. true - success or hidden fail, false - fail.
  2118. Notes:
  2119. -
  2120. \*----------------------------------------------------------------------------*/
  2121. #define Command_CallR(%1,%2) \
  2122. CallRemoteFunction(Command_GetFuncName((%1)), "isii", playerid, %2, help, script)
  2123. #define Command_CallL(%1,%2) \
  2124. CallLocalFunction(Command_GetFuncName((%1)), "isi", playerid, %2, help)
  2125. static stock Command_Process(playerid, cmdtext[], help)
  2126. {
  2127. P:4("Command_Process FS called: %i, \"%s\", %i", playerid, cmdtext, help);
  2128. // Support for very old problems!
  2129. // TODO: Add back.
  2130. P:2("Command_Process called: %d %s", playerid, cmdtext);
  2131. /*#if !_DEBUG && !defined _YSI_SPECIAL_DEBUG
  2132. // For testing purposes.
  2133. if (!IsPlayerConnected(playerid))
  2134. {
  2135. return Command_DisconnectReturn();
  2136. }
  2137. #endif*/
  2138. P:4("Command_Process: Connected");
  2139. new
  2140. idx,
  2141. prelen = help ? 0 : 1,
  2142. index = prelen;
  2143. /*// Shortcuts.
  2144. if (cmdtext[2] <= ' ')
  2145. {
  2146. // Get a player's shortcut information for this letter.
  2147. }
  2148. else*/
  2149. {
  2150. // No more faffing about with random alternate code - it's all done in
  2151. // one nice function instead of having to handle both separately.
  2152. new
  2153. length,
  2154. hash = Command_Hash(cmdtext, index, length);
  2155. P:2("Command_Process: Hash = %d, Length = %d", hash, length);
  2156. // NOTE: No prefix support here.
  2157. cmdtext[length + prelen] = '\0';
  2158. #if YSIM_HAS_MASTER
  2159. length = YSI_g_sPlayerProvider{playerid};
  2160. P:2("Command_Process: Provider: %d", length);
  2161. idx = Command_FindFast(cmdtext[prelen], hash, length);
  2162. if (idx == COMMAND_NOT_FOUND && length != 0xFF)
  2163. {
  2164. // Check default commands if no specialised commands were found.
  2165. idx = Command_FindFast(cmdtext[prelen], hash);
  2166. }
  2167. #else
  2168. idx = Command_FindFast(cmdtext[prelen], hash);
  2169. #endif
  2170. // TODO: Replace!
  2171. //idx = Command_FindSlow(cmdtext[prelen]);//, hash);
  2172. }
  2173. P:2("Command_Process: Index = %d", idx);
  2174. if (idx != COMMAND_NOT_FOUND)
  2175. {
  2176. // Get the master data for the underlying command, not the possibly
  2177. // changed name.
  2178. new
  2179. pointer = Command_GetPointer(idx);
  2180. P:4("Command_Process: Found %d, %d", idx, pointer);
  2181. // Found a command with this name - check the permissions.
  2182. //if (Bit_Get(YSI_g_sCommands[idx][E_COMMANDS_PLAYERS], playerid))
  2183. if (Command_CheckPlayer(idx, playerid))
  2184. {
  2185. P:4("Command_Process: Allowed");
  2186. // Allowed to use the command, get the real function. Note that
  2187. // this may well be the same as "idx", but we loose no time.
  2188. #if YSIM_HAS_MASTER
  2189. P:4("Command_Process: %08x%08x%08x%08x", YSI_g_sCommands[idx][E_COMMANDS_FUNCTION][0], YSI_g_sCommands[idx][E_COMMANDS_FUNCTION][1], YSI_g_sCommands[idx][E_COMMANDS_FUNCTION][2], YSI_g_sCommands[idx][E_COMMANDS_FUNCTION][3]);
  2190. //if (master & 1 << YSI_g_sMaster23)
  2191. //{
  2192. // master = YSI_g_sMaster23;
  2193. //}
  2194. //else
  2195. //{
  2196. new
  2197. script = Command_Provider(idx);
  2198. if (script == 0xFF)
  2199. {
  2200. script = YSI_g_sCommands[pointer][E_COMMANDS_MASTERS];
  2201. if (!script)
  2202. {
  2203. // No scripts can serve the code.
  2204. return Command_UnknownReturn();
  2205. }
  2206. // Find the lowest set bit. We use this to broadcastfunc the
  2207. // command. If it uses MASTER 23, this will select only
  2208. // one script to use. If it doesn't use MASTER 23, this is
  2209. // ignored as a parameter and _YCM is used instead.
  2210. static const
  2211. scDeBruijn[] =
  2212. {
  2213. 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
  2214. 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
  2215. };
  2216. // http://supertech.csail.mit.edu/papers/debruijn.pdf
  2217. script = scDeBruijn[((script & -script) * 0x077CB531) >>> 27];
  2218. }
  2219. //}
  2220. P:2("Command_Process: script = %d", script);
  2221. //if (master == _@)
  2222. //{
  2223. /*#endif
  2224. // This script has it!
  2225. addr = YSI_g_sCommands[pointer][E_COMMANDS_AMX_ADDRESS];
  2226. P:4("Command_Process: %04x%04x", addr >>> 16, addr & 0xFFFF);
  2227. // Make sure all the "#emits" are together as they don't
  2228. // seem to be affected by pre-processor directives.
  2229. #emit PUSH.S master
  2230. #emit PUSH.S help
  2231. #emit LOAD.S.alt cmdtext
  2232. #emit LOAD.S.pri index
  2233. #emit SMUL.C 4
  2234. #emit ADD
  2235. #emit PUSH.pri
  2236. #emit PUSH.S playerid
  2237. #emit PUSH.C 16
  2238. #emit LCTRL 6
  2239. #emit ADD.C 28
  2240. #emit PUSH.pri
  2241. #emit LOAD.S.pri addr
  2242. #emit SCTRL 6
  2243. #emit STOR.S.pri addr
  2244. #if YSIM_HAS_MASTER*/
  2245. //}
  2246. //else
  2247. //{
  2248. #pragma tabsize 4
  2249. P:2("Command_Process: call %s (%d, %d)", unpack(Command_GetFuncName(pointer)), pointer, script);
  2250. if (cmdtext[index])
  2251. {
  2252. // Call it!
  2253. Command_CallR(pointer, cmdtext[index]);
  2254. }
  2255. else
  2256. {
  2257. Command_CallR(pointer, NULL);
  2258. }
  2259. #pragma tabsize 4
  2260. //}
  2261. // Get the real return.
  2262. P:1("Command_Process: return: %d", getproperty(8, YSIM_RETURN));
  2263. return getproperty(8, YSIM_RETURN);
  2264. #else
  2265. if (cmdtext[index])
  2266. {
  2267. // Call it!
  2268. return Command_CallL(pointer, cmdtext[index]);
  2269. }
  2270. else
  2271. {
  2272. return Command_CallL(pointer, NULL);
  2273. }
  2274. //return addr;
  2275. #endif
  2276. }
  2277. else
  2278. {
  2279. return Command_DeniedReturn();
  2280. }
  2281. }
  2282. P:5("Command_Process: Not found");
  2283. return Command_UnknownReturn();
  2284. }
  2285. /*----------------------------------------------------------------------------*\
  2286. Function:
  2287. Command_GetName
  2288. Params:
  2289. funcid - Command to get the name of.
  2290. Return:
  2291. -
  2292. Notes:
  2293. native Command_GetName(funcid);
  2294. \*----------------------------------------------------------------------------*/
  2295. foreign string:Command_GetName(funcid);
  2296. global string:Command_GetName(funcid)
  2297. {
  2298. P:2("Command_GetName called: %i", funcid);
  2299. new
  2300. buffer[YSI_MAX_STRING] = "";
  2301. if (Command_IsValid(funcid))
  2302. {
  2303. strunpack(buffer, Command_Name(funcid));
  2304. }
  2305. return buffer;
  2306. }
  2307. /*----------------------------------------------------------------------------*\
  2308. Function:
  2309. Command_GetDisplay
  2310. Params:
  2311. f - Command to get the real name of.
  2312. p - Player to get the name for.
  2313. Return:
  2314. The name of a command for a single player.
  2315. Notes:
  2316. -
  2317. native Command_GetDisplay(funcid, playerid);
  2318. \*----------------------------------------------------------------------------*/
  2319. foreign string:Command_GetDisplay(funcid, playerid);
  2320. global string:Command_GetDisplay(funcid, playerid)
  2321. {
  2322. P:2("Command_GetDisplay called: %i, %i", funcid, playerid);
  2323. new
  2324. buffer[YSI_MAX_STRING] = "";
  2325. if (Command_IsValid(funcid))
  2326. {
  2327. // Don't recalculate this every loop.
  2328. new
  2329. slot = Bit_Slot(playerid) + 1,
  2330. Bit:mask = Bit_Mask(playerid);
  2331. // Check if they can use the original version.
  2332. if (YSI_g_sCommands[funcid][E_COMMANDS_PLAYERS][slot] & mask)
  2333. {
  2334. //setproperty(8, "", YSIM_STRING, Command_Name(f));
  2335. strunpack(buffer, Command_Name(funcid));
  2336. return buffer;
  2337. //return 1;
  2338. }
  2339. // Search for a command pointing to that command which the player can use.
  2340. for (new i = 0; i != MAX_COMMANDS; ++i)
  2341. {
  2342. if (Command_GetPointer(i) == funcid && (YSI_g_sCommands[i][E_COMMANDS_PLAYERS][slot] & mask))
  2343. {
  2344. //setproperty(8, "", YSIM_STRING, Command_Name(i));
  2345. strunpack(buffer, Command_Name(i));
  2346. return buffer;
  2347. }
  2348. }
  2349. }
  2350. return buffer;
  2351. }
  2352. /*----------------------------------------------------------------------------*\
  2353. Function:
  2354. Command_GetDisplayNamed
  2355. Params:
  2356. f[] - Command to get the real name of.
  2357. p - Player to get the name for.
  2358. Return:
  2359. The name of a named function for one player.
  2360. Notes:
  2361. Remote function call for Command_GetDisplayNameNamed - avoids needing to
  2362. expose users to the master system's odd way of returning strings. This is
  2363. the only part I've not yet fixed up to be nice and hidden.
  2364. native string:Command_GetDisplayNamed(string:funcid[], playerid);
  2365. \*----------------------------------------------------------------------------*/
  2366. foreign string:Command_GetDisplayNamed(string:func[], playerid);
  2367. global string:Command_GetDisplayNamed(string:func[], playerid)
  2368. {
  2369. P:1("Command_GetDisplayNamed called: \"%s\", %i", func, playerid);
  2370. new
  2371. pointer = Command_Find(func),
  2372. buffer[YSI_MAX_STRING] = "";
  2373. if (pointer != COMMAND_NOT_FOUND)
  2374. {
  2375. // Don't recalculate this every loop.
  2376. new
  2377. slot = Bit_Slot(playerid) + 1, //playerid >>> CELLSHIFT) + 1,
  2378. Bit:mask = Bit_Mask(playerid); //Bit:(1 << (playerid & (cellbits - 1)));
  2379. // Check if they can use the original version.
  2380. if (YSI_g_sCommands[pointer][E_COMMANDS_PLAYERS][slot] & mask)
  2381. {
  2382. //setproperty(8, "", YSIM_STRING, Command_Name(pointer));
  2383. strunpack(buffer, Command_Name(pointer));
  2384. return buffer;
  2385. }
  2386. // Search for a command pointing to that command which the player can use.
  2387. for (new i = 0; i != MAX_COMMANDS; ++i)
  2388. {
  2389. if (Command_GetPointer(i) == pointer && (YSI_g_sCommands[i][E_COMMANDS_PLAYERS][slot] & mask))
  2390. {
  2391. //setproperty(8, "", YSIM_STRING, Command_Name(i));
  2392. strunpack(buffer, Command_Name(i));
  2393. return buffer;
  2394. }
  2395. }
  2396. }
  2397. return buffer;
  2398. }
  2399. /*----------------------------------------------------------------------------*\
  2400. Function:
  2401. Command_GetPlayerCommandCount
  2402. Params:
  2403. playerid - Player to count for.
  2404. Return:
  2405. -
  2406. Notes:
  2407. Gets the number of comamnds this player can use.
  2408. native Command_GetPlayerCommandCount(playerid);
  2409. \*----------------------------------------------------------------------------*/
  2410. foreign Command_GetPlayerCommandCount(playerid);
  2411. global Command_GetPlayerCommandCount(playerid)
  2412. {
  2413. P:2("Command_GetPlayerCommandCount called: %i", playerid);
  2414. new
  2415. slot = Bit_Slot(playerid) + 1,
  2416. Bit:mask = Bit_Mask(playerid),
  2417. count = 0;
  2418. for (new i = 0; i != MAX_COMMANDS; ++i)
  2419. {
  2420. if (_Command_IsValid(i) && YSI_g_sCommands[i][E_COMMANDS_PLAYERS][slot] & mask)
  2421. {
  2422. ++count;
  2423. }
  2424. }
  2425. return count;
  2426. }
  2427. /*----------------------------------------------------------------------------*\
  2428. Function:
  2429. Command_GetNext
  2430. Params:
  2431. index - Index of the next command for this player.
  2432. playerid - Player to get the name for.
  2433. Return:
  2434. The name of a command for a single player.
  2435. Notes:
  2436. -
  2437. native Command_GetNext(index, playerid);
  2438. \*----------------------------------------------------------------------------*/
  2439. foreign string:Command_GetNext(index, playerid);
  2440. global string:Command_GetNext(index, playerid)
  2441. {
  2442. P:2("Command_GetNext called: %i, %i", index, playerid);
  2443. new
  2444. buffer[YSI_MAX_STRING] = "";
  2445. if (0 <= index < MAX_COMMANDS)
  2446. {
  2447. // Don't recalculate this every loop.
  2448. new
  2449. slot = Bit_Slot(playerid) + 1,
  2450. Bit:mask = Bit_Mask(playerid);
  2451. for (new i = 0; i != MAX_COMMANDS; ++i)
  2452. {
  2453. if (_Command_IsValid(i) && YSI_g_sCommands[i][E_COMMANDS_PLAYERS][slot] & mask)
  2454. {
  2455. // Skip already displayed ones.
  2456. if (index)
  2457. {
  2458. --index;
  2459. }
  2460. else
  2461. {
  2462. strunpack(buffer, Command_Name(i));
  2463. return buffer;
  2464. }
  2465. }
  2466. }
  2467. }
  2468. return buffer;
  2469. }
  2470. //#tryinclude <YSI\y_groups>
  2471. //#undef _YCM
  2472. // This is to allow callback chaining. When the user includes y_groups and
  2473. // other libraries, the function names will be reset to their custom ones after
  2474. // every inclusion, however if you then include another YSI library you need to
  2475. // revert to the previous library names to get the chaining to work. However I
  2476. // think this is completely pointless now thanks to "y_hooks".
  2477. //#define YSI_SET_LAST_GROUP 25
  2478. #include "internal\y_grouprevert"
  2479. // So we can use custom syntax on these two functions and allow command names to
  2480. // be sort of used...
  2481. //#define Group_SetCommand(%0,YCMD:%2,%3) Group_SetCommand(%0,Command_GetID(#%2),%3)
  2482. //#define Group_SetGlobalCommand(YCMD:%2,%3) Group_SetGlobalCommand(Command_GetID(#%2),%3)