Advertisement
Guest User

y_commands

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