Warcraft 3 documentation
vJASS & Zinc Documentation
For the latest documentation about how it works vJASS and Zinc language layers for Warcraft III, please follow these links:
Jasshelper documentation - Zinc documentation - WC3 Optimizer documentation

[System] ModeManager No New Posts Codes & Snippets

Started by
Magtheridon96

0 Members and 1 Guest are viewing this topic.

[System] ModeManager
on: July 09, 2012, 01:33:55 PM
Category: Execution, Units, Variables, System
Language: vJASS

Related Topics or Resources



by

This system allows to you to create game commands and modes.
You can assign a function callback for whenever this command/mode is used.

For a player to use a certain command/mode, you must authorize him.
This also allows you to give a certain player the ability to select a game mode.

You can also lock a player rendering him unable to use anything.
This makes the system faster, because then it won't have to go
through the loop for nothing.

Each mode has a boolean 'flag'.
This boolean can be used any way you desire.
For example, you could use it to indicate an activated mode.

This system allows several mode strings to be accepted in the same string.

Code: jass
  1. /*********************************************************
  2. *
  3. *   Mode Manager
  4. *   v6.0.0.0
  5. *   By Magtheridon96
  6. *
  7. *   - Allows you to create game modes and commands.
  8. *   - Max Modes/Commands: 682 (<9000)
  9. *       * If you require a higher mode cap, contact me.
  10. *
  11. *   Requires:
  12. *   ---------
  13. *
  14. *       - StringIndexer by Magtheridon96
  15. *
  16. *
  17. *   API:
  18. *   ----
  19. *
  20. *       - struct GameMode extends array
  21. *           - static boolean enabled
  22. *               - Enables/Disables System.
  23. *
  24. *           - method operator flag takes nothing returns boolean
  25. *           - method operator flag= takes boolean b returns nothing
  26. *               - For users to store a boolean in an instance of GameMode
  27. *
  28. *           - method authorize takes integer id returns nothing
  29. *           - method unauthorize takes integer id returns nothing
  30. *           - method isPlayerAuthorized takes integer id returns boolean
  31. *               - Used to manage Player Authorization for certain modes/commands.
  32. *
  33. *           - static method lockPlayer takes integer id returns nothing
  34. *           - static method unlockPlayer takes integer id returns nothing
  35. *           - static method isPlayerLocked takes integer id returns boolean
  36. *               - Used to manage Player Authentication. If a player is authorized
  37. *               for a few modes and you lock him, he will be rendered unable to use
  38. *               anything. When you unlock him, he will be able to use all of the
  39. *               modes you authorized him for.
  40. *
  41. *           - static method create takes string cd, boolean m, boolexpr b returns thistype
  42. *               - Creates a new mode/command.
  43. *
  44. *       - function CreateGameMode takes string modeString, code func returns GameMode
  45. *       - function CreateGameCommand takes string commandString, code func returns GameMode
  46. *
  47. *       - function AuthorizePlayerForGameModeById takes GameMode m, integer i returns nothing
  48. *       - function AuthorizePlayerForGameMode takes GameMode m, player p returns nothing
  49. *
  50. *       - function UnauthorizePlayerForGameModeById takes GameMode m, integer i returns nothing
  51. *       - function UnauthorizePlayerForGameMode takes GameMode m, player p returns nothing
  52. *
  53. *       - function IsPlayerAuthorizedForGameModeById takes GameMode m, integer i returns boolean
  54. *       - function IsPlayerAuthorizedForGameMode takes GameMode m, player p returns boolean
  55. *
  56. *       - function LockPlayerFromGameModesById takes integer i returns nothing
  57. *       - function LockPlayerFromGameModes takes player p returns nothing
  58. *
  59. *       - function UnlockPlayerFromGameModesById takes integer i returns nothing
  60. *       - function UnlockPlayerFromGameModes takes player p returns nothing
  61. *
  62. *       - function IsPlayerLockedFromGameModesById takes integer i returns boolean
  63. *       - function IsPlayerLockedFromGameModes takes player p returns boolean
  64. *
  65. *       - function SetGameModeData takes GameMode m, boolean b returns nothing
  66. *       - function GetGameModeData takes GameMode m returns boolean
  67. *
  68. *       - function EnableGameModes takes nothing returns nothing
  69. *       - function DisableGameModes takes nothing returns nothing
  70. *       - function GameModesEnabled takes nothing return boolean
  71. *
  72. *********************************************************/
  73. library ModeManager requires StringIndexer
  74.    
  75.     globals
  76.         // **** Configuration ****
  77.         private constant string  MODE_CHAR = "-"               // This character will be used to indicate a command/mode.
  78.         private constant integer MODE_CHAR_LENGTH = 1          // This is the length of the MODE_CHARACTER
  79.         private constant integer MODE_LENGTH = 2               // This is the length of each command/mode string
  80.         // **** End Configuration ****
  81.     endglobals
  82.    
  83.     struct GameMode extends array
  84.         private static trigger TRIGGER = CreateTrigger()
  85.         private static boolean e = true
  86.        
  87.         private static boolexpr array func
  88.         private static boolean array mode
  89.        
  90.         private static boolean array authorized
  91.         private static boolean array locked
  92.        
  93.         private static boolean array data
  94.         private static boolean array allocated
  95.        
  96.         static method enable takes nothing returns nothing
  97.             set e = true
  98.         endmethod
  99.        
  100.         static method disable takes nothing returns nothing
  101.             set e = false
  102.         endmethod
  103.        
  104.         static method operator enabled takes nothing returns boolean
  105.             return e
  106.         endmethod
  107.        
  108.         method operator flag takes nothing returns boolean
  109.             return data[this]
  110.         endmethod
  111.        
  112.         method operator flag= takes boolean b returns nothing
  113.             set data[this] = b
  114.         endmethod
  115.        
  116.         method authorize takes integer id returns nothing
  117.             debug if 0 > id or 15 < id then
  118.                 debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Attempted to authorize an invalid player!")
  119.                 debug return
  120.             debug endif
  121.            
  122.             set authorized[this * 12 + id] = true
  123.         endmethod
  124.        
  125.         method unauthorize takes integer id returns nothing
  126.             debug if 0 > id or 15 < id then
  127.                 debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Attempted to unauthorize an invalid player!")
  128.                 debug return
  129.             debug endif
  130.            
  131.             set authorized[this * 12 + id] = false
  132.         endmethod
  133.        
  134.         method isPlayerAuthorized takes integer id returns boolean
  135.             debug if 0 > id or 15 < id then
  136.                 debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Attempted to evaluate an invalid player!")
  137.                 debug return false
  138.             debug endif
  139.            
  140.             return authorized[this * 12 + id]
  141.         endmethod
  142.        
  143.         static method lockPlayer takes integer id returns nothing
  144.             debug if 0 > id or 15 < id then
  145.                 debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Attempted to lock a null player.")
  146.                 debug return
  147.             debug endif
  148.            
  149.             set locked[id] = true
  150.         endmethod
  151.        
  152.         static method unlockPlayer takes integer id returns nothing
  153.             debug if 0 > id or 15 < id then
  154.                 debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Attempted to unlock a null player.")
  155.                 debug return
  156.             debug endif
  157.            
  158.             set locked[id] = false
  159.         endmethod
  160.        
  161.         static method isPlayerLocked takes integer id returns boolean
  162.             debug if 0 > id or 15 < id then
  163.                 debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Attempted to evaluate a null player!")
  164.                 debug return false
  165.             debug endif
  166.            
  167.             return locked[id]
  168.         endmethod
  169.        
  170.         static method create takes string text, boolean isMode, code c returns thistype
  171.             local thistype this = IndexString(text)
  172.            
  173.             debug if allocated[this] then
  174.                 debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Warning: Attempted to overwrite mode data for an existing mode!")
  175.                 debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Registeration Terminated!")
  176.                 debug return 0
  177.             debug endif
  178.            
  179.             debug if MODE_LENGTH != StringLength(text) then
  180.                 debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Failed to register " + text + "!")
  181.                 debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Invalid String Length!")
  182.                 debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Registeration Terminated!")
  183.                 debug return 0
  184.             debug endif
  185.            
  186.             debug if c == null then
  187.                 debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Invalid boolexpr!")
  188.                 debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Registeration Terminated!")
  189.                 debug return 0
  190.             debug endif
  191.            
  192.             // Data storage
  193.             set mode[this] = isMode
  194.             set func[this] = Filter(c)
  195.            
  196.             // Safety
  197.             set allocated[this] = true
  198.            
  199.             return this
  200.         endmethod
  201.        
  202.         private static method filterSpace takes string s returns string
  203.             local string new = ""
  204.             local string tmp = ""
  205.             local integer index = 0
  206.             local integer length = StringLength(s)
  207.            
  208.             loop
  209.                 exitwhen index >= length
  210.                 set tmp = SubString(s, index, index + 1)
  211.                
  212.                 if " " != tmp then
  213.                     set new = new + tmp
  214.                 endif
  215.                
  216.                 set index = index + 1
  217.             endloop
  218.            
  219.             return new
  220.         endmethod
  221.        
  222.         private static method run takes nothing returns boolean
  223.             local thistype this
  224.             local integer index = MODE_CHAR_LENGTH
  225.             local integer id = GetPlayerId(GetTriggerPlayer())
  226.             local integer length
  227.             local string str
  228.            
  229.             // If you disabled the system, it won't run
  230.             if e and not locked[id] then
  231.            
  232.                 call TriggerClearConditions(TRIGGER)
  233.                
  234.                 // Remove all spaces from chat string
  235.                 set str = filterSpace(StringCase(GetEventPlayerChatString(), false))
  236.                 set length = StringLength(str)
  237.                
  238.                 // Loop through all the substrings in the chat string that have a length of MODE_LENGTH
  239.                 // We will start after the MODE_CHAR
  240.                 loop
  241.                     exitwhen index >= length
  242.                    
  243.                     // Get current instance through pointer Table
  244.                     set this = GetStringId(SubString(str, index, index + MODE_LENGTH))
  245.                    
  246.                     // Check if the instance is not null and
  247.                     // the player is authorized
  248.                     if authorized[this * 12 + id] and allocated[this] then
  249.                        
  250.                         // Check if it's a mode string
  251.                         if mode[this] then
  252.                             // Add function to trigger
  253.                             call TriggerAddCondition(TRIGGER, func[this])
  254.                         // Check if it's a command string and it's the first one encountered
  255.                         elseif not mode[this] and index == MODE_CHAR_LENGTH then
  256.                             // Add function to trigger
  257.                             call TriggerAddCondition(TRIGGER, func[this])
  258.                             exitwhen true
  259.                         endif
  260.                        
  261.                     endif
  262.                    
  263.                     // Increase index to get next string
  264.                     set index = index + MODE_LENGTH
  265.                 endloop
  266.            
  267.             endif
  268.            
  269.             // Run all functions
  270.             return TriggerEvaluate(TRIGGER)
  271.         endmethod
  272.        
  273.         private static method onInit takes nothing returns nothing
  274.             local trigger t = CreateTrigger()
  275.             local integer i = 11
  276.             local player p
  277.            
  278.             debug if MODE_CHAR_LENGTH != StringLength(MODE_CHAR) then
  279.                 debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: System Terminated!")
  280.                 debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Invalid integer (MODE_CHAR_LENGTH)")
  281.                 debug return
  282.             debug endif
  283.            
  284.             debug if 0 >= MODE_LENGTH then
  285.                 debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: System Terminated!")
  286.                 debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "[Mode Manager]Error: Invalid integer (MODE_LENGTH)")
  287.                 debug return
  288.             debug endif
  289.            
  290.             // Loop through active players
  291.             loop
  292.                 set p = Player(i)
  293.                 // If the player is available and happens to be a user (Non-computer)
  294.                     // Register the event for this player
  295.                     call TriggerRegisterPlayerChatEvent(t, p, MODE_CHAR, false)
  296.                 endif
  297.                
  298.                 exitwhen i == 0
  299.                 set i = i - 1
  300.             endloop
  301.            
  302.             call TriggerAddCondition(t, Condition(function thistype.run))
  303.            
  304.             set t = null
  305.             set p = null
  306.         endmethod
  307.     endstruct
  308.    
  309.     function CreateGameMode takes string modeString, code func returns GameMode
  310.         return GameMode.create(modeString, true, func)
  311.     endfunction
  312.    
  313.     function CreateGameCommand takes string commandString, boolean executeOnce, code func returns GameMode
  314.         return GameMode.create(commandString, false, func)
  315.     endfunction
  316.    
  317.     function AuthorizePlayerForGameModeById takes GameMode m, integer i returns nothing
  318.         call m.authorize(i)
  319.     endfunction
  320.    
  321.     function AuthorizePlayerForGameMode takes GameMode m, player p returns nothing
  322.         call m.authorize(GetPlayerId(p))
  323.     endfunction
  324.    
  325.     function UnauthorizePlayerForGameModeById takes GameMode m, integer i returns nothing
  326.         call m.unauthorize(i)
  327.     endfunction
  328.    
  329.     function UnauthorizePlayerForGameMode takes GameMode m, player p returns nothing
  330.         call m.unauthorize(GetPlayerId(p))
  331.     endfunction
  332.    
  333.     function IsPlayerAuthorizedForGameModeById takes GameMode m, integer i returns boolean
  334.         return m.isPlayerAuthorized(i)
  335.     endfunction
  336.    
  337.     function IsPlayerAuthorizedForGameMode takes GameMode m, player p returns boolean
  338.         return m.isPlayerAuthorized(GetPlayerId(p))
  339.     endfunction
  340.    
  341.     function LockPlayerFromGameModesById takes integer i returns nothing
  342.         call GameMode.lockPlayer(i)
  343.     endfunction
  344.    
  345.     function LockPlayerFromGameModes takes player p returns nothing
  346.         call GameMode.lockPlayer(GetPlayerId(p))
  347.     endfunction
  348.    
  349.     function UnlockPlayerFromGameModesById takes integer i returns nothing
  350.         call GameMode.unlockPlayer(i)
  351.     endfunction
  352.    
  353.     function UnlockPlayerFromGameModes takes player p returns nothing
  354.         call GameMode.unlockPlayer(GetPlayerId(p))
  355.     endfunction
  356.    
  357.     function IsPlayerLockedFromGameModesById takes integer i returns boolean
  358.         return GameMode.isPlayerLocked(i)
  359.     endfunction
  360.    
  361.     function IsPlayerLockedFromGameModes takes player p returns boolean
  362.         return GameMode.isPlayerLocked(GetPlayerId(p))
  363.     endfunction
  364.    
  365.     function SetGameModeData takes GameMode m, boolean b returns nothing
  366.         set m.flag = b
  367.     endfunction
  368.    
  369.     function GetGameModeData takes GameMode m returns boolean
  370.         return m.flag
  371.     endfunction
  372.    
  373.     function EnableGameModes takes nothing returns nothing
  374.         call GameMode.enable()
  375.     endfunction
  376.    
  377.     function DisableGameModes takes nothing returns nothing
  378.         call GameMode.disable()
  379.     endfunction
  380.    
  381.     function GameModesEnabled takes nothing returns boolean
  382.         return GameMode.enabled
  383.     endfunction
  384. endlibrary


Here's the Demo:
Code: jass
  1. struct Demo extends array
  2.     private static GameMode Allpick
  3.     private static GameMode Allrandom
  4.     private static GameMode Onlymid
  5.     private static GameMode Fastrespawn
  6.     private static GameMode Movespeed
  7.     private static GameMode Myenemies
  8.    
  9.     static method print takes string s returns nothing
  10.         call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, s)
  11.     endmethod
  12.    
  13.     // Good examples on how to stop mode collisions and avoiding
  14.     // functions running more than once.
  15.     static method ap takes nothing returns nothing
  16.         if not Allpick.flag and not Allrandom.flag then
  17.             call print("MODE Allpick")
  18.             set Allpick.flag = true
  19.         endif
  20.     endmethod
  21.    
  22.     static method ar takes nothing returns nothing
  23.         if not Allpick.flag and not Allrandom.flag then
  24.             call print("MODE Allrandom")
  25.             set Allrandom.flag = true
  26.         endif
  27.     endmethod
  28.    
  29.     static method ms takes nothing returns nothing
  30.         call print("COMMAND Movespeed")
  31.     endmethod
  32.    
  33.     static method om takes nothing returns nothing
  34.         if not Onlymid.flag then
  35.             call print("MODE Onlymid")
  36.             set Onlymid.flag = true
  37.         endif
  38.     endmethod
  39.    
  40.     static method ma takes nothing returns nothing
  41.         call print("COMMAND Myenemies")
  42.     endmethod
  43.    
  44.     static method fr takes nothing returns nothing
  45.         if not Fastrespawn.flag then
  46.             call print("MODE Fastrespawn")
  47.             set Fastrespawn.flag = true
  48.         endif
  49.     endmethod
  50.    
  51.     private static method reset takes nothing returns boolean
  52.         set Allpick.flag = false
  53.         set Allrandom.flag = false
  54.         set Onlymid.flag = false
  55.         set Fastrespawn.flag = false
  56.         return false
  57.     endmethod
  58.    
  59.     private static method onInit takes nothing returns nothing
  60.         local trigger t = CreateTrigger()
  61.  
  62.         set Allpick = GameMode.create("ap", true, function thistype.ap)
  63.         set Onlymid = GameMode.create("om", true, function thistype.om)
  64.         set Allrandom = GameMode.create("ar", true, function thistype.ar)
  65.         set Fastrespawn = GameMode.create("fr", true, function thistype.fr)
  66.        
  67.         set Movespeed = GameMode.create("ms", false, function thistype.ms)
  68.         set Myenemies = GameMode.create("ma", false, function thistype.ma)
  69.        
  70.         call Allpick.authorize(0)
  71.         call Allrandom.authorize(0)
  72.         call Onlymid.authorize(0)
  73.         call Fastrespawn.authorize(0)
  74.        
  75.         call Movespeed.authorize(0)
  76.         call Myenemies.authorize(0)
  77.  
  78.         call print("[Mode Manager]Available Mode strings: ap, ar, om, fr")
  79.         call print("[Mode Manager]Available Command strings: ms, ma")
  80.         call print("[Mode Manager]To activate a mode, type \"-\" followed by a queue of mode strings")
  81.         call print("[Mode Manager]To run a command, type \"-\" followed by a single command string")
  82.         call print("[Mode Manager]You cannot activate the same mode twice. To reset a mode, type \"-reset\"")
  83.  
  84.         call TriggerRegisterPlayerChatEvent(t, Player(0), "-reset", true)
  85.         call TriggerAddCondition(t, Condition(function thistype.reset))
  86.  
  87.         set t = null
  88.     endmethod
  89. endstruct

Contructive critisism will be accepted and is highly appreciated = )
Please give credits if used.
« Last Edit: December 18, 2017, 06:22:58 AM by moyack »



 

Started by cohadar

Replies: 0
Views: 2044
Codes & Snippets

Started by ashujon

Replies: 0
Views: 2975
Warcraft III Spells and Systems

Started by azlier

Replies: 0
Views: 1789
Codes & Snippets

Started by Magtheridon96

Replies: 0
Views: 2025
Codes & Snippets

Started by moyack

Replies: 6
Views: 19000
Codes & Snippets
Vivir aprendiendo.co - A place for learning stuff, in Spanish   Chaos Realm - The world of Game modders and wc3 addicts   Diplo, a gaming community   Power of Corruption, an altered melee featuring Naga and Demon. Play it now!!!   WC3JASS.com - The JASS Vault + vJASS and Zinc   Jetcraft - A Starcraft II mod   WormTastic Clan (wTc)   Warcraft RESOURCES Reforged: Modelos, mapas, proyectos y mas...