14 years
edited 8 years
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.
Here's the Demo:
Contructive critisism will be accepted and is highly appreciated = )
Please give credits if used.
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.
1 /*********************************************************2 *3 * Mode Manager4 * v6.0.0.05 * By Magtheridon966 *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 Magtheridon9615 *16 *17 * API:18 * ----19 *20 * - struct GameMode extends array21 * - static boolean enabled22 * - Enables/Disables System.23 *24 * - method operator flag takes nothing returns boolean25 * - method operator flag= takes boolean b returns nothing26 * - For users to store a boolean in an instance of GameMode27 *28 * - method authorize takes integer id returns nothing29 * - method unauthorize takes integer id returns nothing30 * - method isPlayerAuthorized takes integer id returns boolean31 * - Used to manage Player Authorization for certain modes/commands.32 *33 * - static method lockPlayer takes integer id returns nothing34 * - static method unlockPlayer takes integer id returns nothing35 * - static method isPlayerLocked takes integer id returns boolean36 * - 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 thistype42 * - Creates a new mode/command.43 *44 * - function CreateGameMode takes string modeString, code func returns GameMode45 * - function CreateGameCommand takes string commandString, code func returns GameMode46 *47 * - function AuthorizePlayerForGameModeById takes GameMode m, integer i returns nothing48 * - function AuthorizePlayerForGameMode takes GameMode m, player p returns nothing49 *50 * - function UnauthorizePlayerForGameModeById takes GameMode m, integer i returns nothing51 * - function UnauthorizePlayerForGameMode takes GameMode m, player p returns nothing52 *53 * - function IsPlayerAuthorizedForGameModeById takes GameMode m, integer i returns boolean54 * - function IsPlayerAuthorizedForGameMode takes GameMode m, player p returns boolean55 *56 * - function LockPlayerFromGameModesById takes integer i returns nothing57 * - function LockPlayerFromGameModes takes player p returns nothing58 *59 * - function UnlockPlayerFromGameModesById takes integer i returns nothing60 * - function UnlockPlayerFromGameModes takes player p returns nothing61 *62 * - function IsPlayerLockedFromGameModesById takes integer i returns boolean63 * - function IsPlayerLockedFromGameModes takes player p returns boolean64 *65 * - function SetGameModeData takes GameMode m, boolean b returns nothing66 * - function GetGameModeData takes GameMode m returns boolean67 *68 * - function EnableGameModes takes nothing returns nothing69 * - function DisableGameModes takes nothing returns nothing70 * - function GameModesEnabled takes nothing return boolean71 *72 *********************************************************/73 library ModeManager requires StringIndexer
74
75 globals76 // **** 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 endglobals82
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 endmethod99
100 static method disable takes nothing returns nothing
101 set e = false
102 endmethod103
104 static method operator enabled takes nothing returns boolean
105 return e106 endmethod107
108 method operator flag takes nothing returns boolean
109 return data[this]
110 endmethod111
112 method operator flag= takes boolean b returns nothing
113 set data[this] = b
114 endmethod115
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 endmethod124
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 endmethod133
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 endmethod142
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 endmethod151
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 endmethod160
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 endmethod169
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 storage193 set mode[this] = isMode
194 set func[this] = Filter(c)
195
196 // Safety197 set allocated[this] = true
198
199 return this
200 endmethod201
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 loop209 exitwhen index >= length
210 set tmp = SubString(s, index, index + 1)
211
212 if " " != tmp then
213 set new = new + tmp
214 endif215
216 set index = index + 1
217 endloop218
219 return new220 endmethod221
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 run230 if e and not locked[id] then
231
232 call TriggerClearConditions(TRIGGER)
233
234 // Remove all spaces from chat string235 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_LENGTH239 // We will start after the MODE_CHAR240 loop241 exitwhen index >= length
242
243 // Get current instance through pointer Table244 set this = GetStringId(SubString(str, index, index + MODE_LENGTH))
245
246 // Check if the instance is not null and 247 // the player is authorized248 if authorized[this * 12 + id] and allocated[this] then
249
250 // Check if it's a mode string251 if mode[this] then
252 // Add function to trigger253 call TriggerAddCondition(TRIGGER, func[this])
254 // Check if it's a command string and it's the first one encountered255 elseif not mode[this] and index == MODE_CHAR_LENGTH then
256 // Add function to trigger257 call TriggerAddCondition(TRIGGER, func[this])
258 exitwhen true
259 endif260
261 endif262
263 // Increase index to get next string264 set index = index + MODE_LENGTH
265 endloop266
267 endif268
269 // Run all functions270 return TriggerEvaluate(TRIGGER)
271 endmethod272
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 players291 loop292 set p = Player(i)
293 // If the player is available and happens to be a user (Non-computer)294 if GetPlayerSlotState(p) == PLAYER_SLOT_STATE_PLAYING and GetPlayerController(p) == MAP_CONTROL_USER then
295 // Register the event for this player296 call TriggerRegisterPlayerChatEvent(t, p, MODE_CHAR, false)
297 endif298
299 exitwhen i == 0
300 set i = i - 1
301 endloop302
303 call TriggerAddCondition(t, Condition(function thistype.run))
304
305 set t = null
306 set p = null
307 endmethod308 endstruct309
310 function CreateGameMode takes string modeString, code func returns GameMode
311 return GameMode.create(modeString, true, func)
312 endfunction313
314 function CreateGameCommand takes string commandString, boolean executeOnce, code func returns GameMode
315 return GameMode.create(commandString, false, func)
316 endfunction317
318 function AuthorizePlayerForGameModeById takes GameMode m, integer i returns nothing
319 call m.authorize(i)
320 endfunction321
322 function AuthorizePlayerForGameMode takes GameMode m, player p returns nothing
323 call m.authorize(GetPlayerId(p))
324 endfunction325
326 function UnauthorizePlayerForGameModeById takes GameMode m, integer i returns nothing
327 call m.unauthorize(i)
328 endfunction329
330 function UnauthorizePlayerForGameMode takes GameMode m, player p returns nothing
331 call m.unauthorize(GetPlayerId(p))
332 endfunction333
334 function IsPlayerAuthorizedForGameModeById takes GameMode m, integer i returns boolean
335 return m.isPlayerAuthorized(i)
336 endfunction337
338 function IsPlayerAuthorizedForGameMode takes GameMode m, player p returns boolean
339 return m.isPlayerAuthorized(GetPlayerId(p))
340 endfunction341
342 function LockPlayerFromGameModesById takes integer i returns nothing
343 call GameMode.lockPlayer(i)
344 endfunction345
346 function LockPlayerFromGameModes takes player p returns nothing
347 call GameMode.lockPlayer(GetPlayerId(p))
348 endfunction349
350 function UnlockPlayerFromGameModesById takes integer i returns nothing
351 call GameMode.unlockPlayer(i)
352 endfunction353
354 function UnlockPlayerFromGameModes takes player p returns nothing
355 call GameMode.unlockPlayer(GetPlayerId(p))
356 endfunction357
358 function IsPlayerLockedFromGameModesById takes integer i returns boolean
359 return GameMode.isPlayerLocked(i)
360 endfunction361
362 function IsPlayerLockedFromGameModes takes player p returns boolean
363 return GameMode.isPlayerLocked(GetPlayerId(p))
364 endfunction365
366 function SetGameModeData takes GameMode m, boolean b returns nothing
367 set m.flag = b
368 endfunction369
370 function GetGameModeData takes GameMode m returns boolean
371 return m.flag
372 endfunction373
374 function EnableGameModes takes nothing returns nothing
375 call GameMode.enable()
376 endfunction377
378 function DisableGameModes takes nothing returns nothing
379 call GameMode.disable()
380 endfunction381
382 function GameModesEnabled takes nothing returns boolean
383 return GameMode.enabled
384 endfunction385 endlibrary
Here's the Demo:
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 endmethod12
13 // Good examples on how to stop mode collisions and avoiding14 // 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 endif20 endmethod21
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 endif27 endmethod28
29 static method ms takes nothing returns nothing
30 call print("COMMAND Movespeed")
31 endmethod32
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 endif38 endmethod39
40 static method ma takes nothing returns nothing
41 call print("COMMAND Myenemies")
42 endmethod43
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 endif49 endmethod50
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 endmethod58
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 endmethod89 endstruct
Contructive critisism will be accepted and is highly appreciated = )
Please give credits if used.