[Half-Life AMXX] / include / amxmisc.inc Repository:
ViewVC logotype

Annotation of /include/amxmisc.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 17 - (view) (download)

1 : ian 1 /* AMX Mod X misc.
2 :     *
3 :     * by the AMX Mod X Development Team
4 :     * originally developed by OLO
5 :     *
6 :     * This file is provided as is (no warranties).
7 :     */
8 :    
9 :     #if defined _amxmisc_included
10 :     #endinput
11 :     #endif
12 :     #define _amxmisc_included
13 :    
14 :     #if !defined _amxmodx_included
15 :     #if defined AMXMOD_BCOMPAT
16 :     #include <amxmod>
17 :     #else
18 :     #include <amxmodx>
19 :     #endif
20 :     #endif
21 :    
22 :     #if defined AMXMOD_BCOMPAT
23 :     #if defined _translator_included
24 :     #define SIMPLE_T(%1) _T(%1)
25 :     #else
26 :     #define SIMPLE_T(%1) %1
27 :     #endif
28 :     #endif
29 :    
30 :     stock is_user_admin(id)
31 :     {
32 : ian 17 new __flags=get_user_flags(id);
33 :     return (__flags>0 && !(__flags&ADMIN_USER));
34 : ian 1 }
35 :    
36 : ian 17 stock cmd_access(id, level, cid, num, bool:accesssilent = false)
37 : ian 1 {
38 :     new has_access = 0;
39 :     if ( id==(is_dedicated_server()?0:1) )
40 :     {
41 :     has_access = 1;
42 :     }
43 :     else if ( level==ADMIN_ADMIN )
44 :     {
45 :     if ( is_user_admin(id) )
46 :     {
47 :     has_access = 1;
48 :     }
49 :     }
50 :     else if ( get_user_flags(id) & level )
51 :     {
52 :     has_access = 1;
53 :     }
54 :     else if (level == ADMIN_ALL)
55 :     {
56 :     has_access = 1;
57 :     }
58 :    
59 :     if ( has_access==0 )
60 :     {
61 : ian 17 if (!accesssilent)
62 :     {
63 : ian 1 #if defined AMXMOD_BCOMPAT
64 : ian 17 console_print(id, SIMPLE_T("You have no access to that command."));
65 : ian 1 #else
66 : ian 17 console_print(id,"%L",id,"NO_ACC_COM");
67 : ian 1 #endif
68 : ian 17 }
69 : ian 1 return 0;
70 :     }
71 :     if (read_argc() < num)
72 :     {
73 :     new hcmd[32], hinfo[128], hflag;
74 :     get_concmd(cid,hcmd,31,hflag,hinfo,127,level);
75 :     #if defined AMXMOD_BCOMPAT
76 :     console_print(id, SIMPLE_T("Usage: %s %s"), hcmd, SIMPLE_T(hinfo));
77 :     #else
78 :     console_print(id,"%L: %s %s",id,"USAGE",hcmd,hinfo);
79 :     #endif
80 :     return 0;
81 :     }
82 :    
83 :     return 1;
84 :     }
85 :    
86 :     stock access(id,level)
87 :     {
88 : ian 17 if (level==ADMIN_ADMIN)
89 : ian 1 {
90 :     return is_user_admin(id);
91 :     }
92 : ian 17 else if (level==ADMIN_ALL)
93 :     {
94 :     return 1;
95 :     }
96 : ian 1
97 :     return (get_user_flags(id) & level);
98 :     }
99 :    
100 :     /* Flags:
101 :     * 1 - obey immunity
102 :     * 2 - allow yourself
103 :     * 4 - must be alive
104 :     * 8 - can't be bot */
105 : ian 17 #define CMDTARGET_OBEY_IMMUNITY (1<<0)
106 :     #define CMDTARGET_ALLOW_SELF (1<<1)
107 :     #define CMDTARGET_ONLY_ALIVE (1<<2)
108 :     #define CMDTARGET_NO_BOTS (1<<3)
109 :     stock cmd_target(id,const arg[],flags = CMDTARGET_OBEY_IMMUNITY)
110 : ian 1 {
111 :     new player = find_player("bl",arg);
112 :     if (player)
113 :     {
114 :     if ( player != find_player("blj",arg) )
115 :     {
116 :     #if defined AMXMOD_BCOMPAT
117 :     console_print(id, SIMPLE_T("There are more clients matching to your argument"));
118 :     #else
119 :     console_print(id,"%L",id,"MORE_CL_MATCHT");
120 :     #endif
121 :     return 0;
122 :     }
123 :     }
124 :     else if ( ( player = find_player("c",arg) )==0 && arg[0]=='#' && arg[1] )
125 :     {
126 :     player = find_player("k",str_to_num(arg[1]));
127 :     }
128 :     if (!player)
129 :     {
130 :     #if defined AMXMOD_BCOMPAT
131 :     console_print(id, SIMPLE_T("Client with that name or userid not found"));
132 :     #else
133 :     console_print(id,"%L",id,"CL_NOT_FOUND");
134 :     #endif
135 :     return 0;
136 :     }
137 : ian 17 if (flags & CMDTARGET_OBEY_IMMUNITY)
138 : ian 1 {
139 : ian 17 if ((get_user_flags(player) & ADMIN_IMMUNITY) &&
140 :     ((flags & CMDTARGET_ALLOW_SELF) ? (id != player) : true) )
141 : ian 1 {
142 :     new imname[32];
143 :     get_user_name(player,imname,31);
144 :     #if defined AMXMOD_BCOMPAT
145 :     console_print(id, SIMPLE_T("Client ^"%s^" has immunity"), imname);
146 :     #else
147 :     console_print(id,"%L",id,"CLIENT_IMM",imname);
148 :     #endif
149 :     return 0;
150 :     }
151 :     }
152 : ian 17 if (flags & CMDTARGET_ONLY_ALIVE)
153 : ian 1 {
154 :     if (!is_user_alive(player))
155 :     {
156 :     new imname[32];
157 :     get_user_name(player,imname,31);
158 :     #if defined AMXMOD_BCOMPAT
159 :     console_print(id, SIMPLE_T("That action can't be performed on dead client ^"%s^""), imname);
160 :     #else
161 :     console_print(id,"%L",id,"CANT_PERF_DEAD",imname);
162 :     #endif
163 :     return 0;
164 :     }
165 :     }
166 : ian 17 if (flags & CMDTARGET_NO_BOTS)
167 : ian 1 {
168 :     if (is_user_bot(player))
169 :     {
170 :     new imname[32];
171 :     get_user_name(player,imname,31);
172 :     #if defined AMXMOD_BCOMPAT
173 :     console_print(id, SIMPLE_T("That action can't be performed on bot ^"%s^""), imname);
174 :     #else
175 :     console_print(id,"%L",id,"CANT_PERF_BOT",imname);
176 :     #endif
177 :     return 0;
178 :     }
179 :     }
180 :     return player;
181 :     }
182 :    
183 : ian 17 /**
184 :     * Standard method to show activity to clients connected to the server.
185 :     * This depends on the amx_show_activity cvar. See documentation for more details.
186 :     *
187 :     * @param id The user id of the person doing the action.
188 :     * @param name The name of the person doing the action.
189 :     * @param fmt The format string to display. Do not put the "ADMIN:" prefix in this.
190 :     */
191 :     stock show_activity( id, const name[], const fmt[], any:... )
192 : ian 1 {
193 : ian 17 static __amx_show_activity;
194 :     if (__amx_show_activity == 0)
195 :     {
196 :     __amx_show_activity = get_cvar_pointer("amx_show_activity");
197 :    
198 :     // if still not found, then register the cvar as a dummy
199 :     if (__amx_show_activity == 0)
200 :     {
201 :     __amx_show_activity = register_cvar("amx_show_activity", "2");
202 :     }
203 :     }
204 :     #if defined AMXMOD_BCOMPAT
205 : ian 1 new buffer[128];
206 :     format_args( buffer , 127 , 2 );
207 : ian 17 #else
208 :     new prefix[10];
209 :     if (is_user_admin(id))
210 : ian 1 {
211 : ian 17 copy(prefix, charsmax(prefix), "ADMIN");
212 :     }
213 :     else
214 :     {
215 :     copy(prefix, charsmax(prefix), "PLAYER");
216 :     }
217 :     new buffer[512];
218 :     vformat(buffer, charsmax(buffer), fmt, 3);
219 :     #endif
220 :     switch(get_pcvar_num(__amx_show_activity))
221 :     {
222 : ian 1 #if defined AMXMOD_BCOMPAT
223 : ian 17 case 2: // show name to all
224 :     {
225 : ian 1 client_print(0, print_chat, "%s %s: %s", is_user_admin(id) ? SIMPLE_T("ADMIN") : SIMPLE_T("PLAYER"), name, buffer);
226 : ian 17 }
227 :     case 1: // hide name to all
228 :     {
229 : ian 1 client_print(0, print_chat, "%s: %s", is_user_admin(id) ? SIMPLE_T("ADMIN") : SIMPLE_T("PLAYER"), buffer);
230 : ian 17 }
231 : ian 1 #else
232 : ian 17 case 5: // hide name only to admins, show nothing to normal users
233 :     {
234 :     new __maxclients=get_maxplayers();
235 :    
236 :    
237 :     for (new i=1; i<__maxclients; i++)
238 :     {
239 :     if (is_user_connected(i))
240 :     {
241 :     if (is_user_admin(i))
242 :     {
243 :     client_print(i, print_chat, "%L: %s", i, prefix, buffer);
244 :     }
245 :     }
246 :     }
247 :     }
248 :     case 4: // show name only to admins, show nothing to normal users
249 :     {
250 :     new __maxclients=get_maxplayers();
251 :    
252 :     for (new i=1; i<__maxclients; i++)
253 :     {
254 :     if (is_user_connected(i))
255 :     {
256 :     if (is_user_admin(i))
257 :     {
258 :     client_print(i, print_chat, "%L %s: %s", i, prefix, name, buffer);
259 :     }
260 :     }
261 :     }
262 :     }
263 :     case 3: // show name only to admins, hide name from normal users
264 :     {
265 :     new __maxclients=get_maxplayers();
266 :    
267 :     for (new i=1; i<__maxclients; i++)
268 :     {
269 :     if (is_user_connected(i))
270 :     {
271 :     if (is_user_admin(i))
272 :     {
273 :     client_print(i, print_chat, "%L %s: %s", i, prefix, name, buffer);
274 :     }
275 :     else
276 :     {
277 :     client_print(i, print_chat, "%L: %s", i, prefix, buffer);
278 :     }
279 :     }
280 :     }
281 :     }
282 :     case 2: // show name to all
283 :     {
284 :     client_print(0, print_chat, "%L %s: %s", LANG_PLAYER, prefix , name , buffer );
285 :     }
286 :     case 1: // hide name to all
287 :     {
288 :     client_print(0, print_chat, "%L: %s", LANG_PLAYER, prefix, buffer );
289 :     }
290 : ian 1 #endif
291 :     }
292 :     }
293 :    
294 : ian 17 /**
295 :     * Standard method to show activity to one single client.
296 :     * This is useful for messages that get pieced together by many language keys.
297 :     * This depends on the amx_show_activity cvar. See documentation for more details.
298 :     *
299 :     * @param idtarget The user id of the person to display to. 0 is invalid.
300 :     * @param idadmin The user id of the person doing the action.
301 :     * @param name The name of the person doing the action.
302 :     * @param fmt The format string to display. Do not put the "ADMIN:" prefix in this.
303 :     */
304 :     stock show_activity_id(idtarget, idadmin, const name[], const fmt[], any:...)
305 :     {
306 :     if (idtarget == 0 ||
307 :     !is_user_connected(idtarget) )
308 :     {
309 :     return;
310 :     }
311 :    
312 :     static __amx_show_activity;
313 :     if (__amx_show_activity == 0)
314 :     {
315 :     __amx_show_activity = get_cvar_pointer("amx_show_activity");
316 :    
317 :     // if still not found, then register the cvar as a dummy
318 :     if (__amx_show_activity == 0)
319 :     {
320 :     __amx_show_activity = register_cvar("amx_show_activity", "2");
321 :     }
322 :     }
323 :    
324 :     static prefix[10];
325 :     if (is_user_admin(idadmin))
326 :     {
327 :     copy(prefix, charsmax(prefix), "ADMIN");
328 :     }
329 :     else
330 :     {
331 :     copy(prefix, charsmax(prefix), "PLAYER");
332 :     }
333 :    
334 :     static buffer[512];
335 :     vformat(buffer, charsmax(buffer), fmt, 5);
336 :    
337 :    
338 :     switch(get_pcvar_num(__amx_show_activity))
339 :     {
340 :     case 5: // hide name only to admins, show nothing to normal users
341 :     {
342 :     if ( is_user_admin(idtarget) )
343 :     {
344 :     client_print(idtarget, print_chat, "%L: %s", idtarget, prefix, buffer);
345 :     }
346 :     }
347 :     case 4: // show name only to admins, show nothing to normal users
348 :     {
349 :     if ( is_user_admin(idtarget) )
350 :     {
351 :     client_print(idtarget, print_chat, "%L %s: %s", idtarget, prefix, name, buffer);
352 :     }
353 :     }
354 :     case 3: // show name only to admins, hide name from normal users
355 :     {
356 :     if ( is_user_admin(idtarget) )
357 :     {
358 :     client_print(idtarget, print_chat, "%L %s: %s", idtarget, prefix, name, buffer);
359 :     }
360 :     else
361 :     {
362 :     client_print(idtarget, print_chat, "%L: %s", idtarget, prefix, buffer);
363 :     }
364 :     }
365 :     case 2: // show name to all
366 :     {
367 :     client_print(idtarget, print_chat, "%L %s: %s", idtarget, prefix, name, buffer);
368 :     }
369 :     case 1: // hide name to all
370 :     {
371 :     client_print(idtarget, print_chat, "%L: %s", idtarget, prefix, buffer);
372 :     }
373 :     }
374 :     }
375 :     /**
376 :     * Standard method to show activity to one single client with normal language keys.
377 :     * These keys need to be in the format of standard AMXX keys:
378 :     * eg: ADMIN_KICK_1 = ADMIN: kick %s
379 :     * ADMIN_KICK_2 = ADMIN %s: kick %s
380 :     * This depends on the amx_show_activity cvar. See documentation for more details.
381 :     *
382 :     * @param KeyWithoutName The language key that does not have the name field.
383 :     * @param KeyWithName The language key that does have the name field.
384 :     * @param __AdminName The name of the person doing the action.
385 :     * @extra Pass any extra format arguments for the language key in the variable arguments list.
386 :     */
387 :     stock show_activity_key(const KeyWithoutName[], const KeyWithName[], const ___AdminName[], any:...)
388 :     {
389 :     // The variable gets used via vformat, but the compiler doesn't know that, so it still cries.
390 :     #pragma unused ___AdminName
391 :     static __amx_show_activity;
392 :     if (__amx_show_activity == 0)
393 :     {
394 :     __amx_show_activity = get_cvar_pointer("amx_show_activity");
395 :    
396 :     // if still not found, then register the cvar as a dummy
397 :     if (__amx_show_activity == 0)
398 :     {
399 :     __amx_show_activity = register_cvar("amx_show_activity", "2");
400 :     }
401 :     }
402 :    
403 :     new buffer[512];
404 :     new keyfmt[256];
405 :     new i;
406 :    
407 :     new __maxclients=get_maxplayers();
408 :    
409 :     switch( get_pcvar_num(__amx_show_activity) )
410 :     {
411 :     case 5: // hide name to admins, display nothing to normal players
412 :     while (i++ < __maxclients)
413 :     {
414 :     if ( is_user_connected(i) )
415 :     {
416 :     if ( is_user_admin(i) )
417 :     {
418 :     LookupLangKey(keyfmt, charsmax(keyfmt), KeyWithoutName, i);
419 :    
420 :     // skip the "adminname" argument if not showing name
421 :     vformat(buffer, charsmax(buffer), keyfmt, 4);
422 :     client_print(i, print_chat, "%s", buffer);
423 :     }
424 :     }
425 :     }
426 :     case 4: // show name only to admins, display nothing to normal players
427 :     while (i++ < __maxclients)
428 :     {
429 :     if ( is_user_connected(i) )
430 :     {
431 :     if ( is_user_admin(i) )
432 :     {
433 :     LookupLangKey(keyfmt, charsmax(keyfmt), KeyWithName, i);
434 :     vformat(buffer, charsmax(buffer), keyfmt, 3);
435 :     client_print(i, print_chat, "%s", buffer);
436 :     }
437 :     }
438 :     }
439 :     case 3: // show name only to admins, hide name from normal users
440 :     while (i++ < __maxclients)
441 :     {
442 :     if ( is_user_connected(i) )
443 :     {
444 :     if ( is_user_admin(i) )
445 :     {
446 :     LookupLangKey(keyfmt, charsmax(keyfmt), KeyWithName, i);
447 :     vformat(buffer, charsmax(buffer), keyfmt, 3);
448 :     }
449 :     else
450 :     {
451 :     LookupLangKey(keyfmt, charsmax(keyfmt), KeyWithoutName, i);
452 :    
453 :     // skip the "adminname" argument if not showing name
454 :     vformat(buffer, charsmax(buffer), keyfmt, 4);
455 :     }
456 :     client_print(i, print_chat, "%s", buffer);
457 :     }
458 :     }
459 :     case 2: // show name to all users
460 :     while (i++ < __maxclients)
461 :     {
462 :     if ( is_user_connected(i) )
463 :     {
464 :     LookupLangKey(keyfmt, charsmax(keyfmt), KeyWithName, i);
465 :     vformat(buffer, charsmax(buffer), keyfmt, 3);
466 :     client_print(i, print_chat, "%s", buffer);
467 :     }
468 :     }
469 :     case 1: // hide name from all users
470 :     while (i++ < __maxclients)
471 :     {
472 :     if ( is_user_connected(i) )
473 :     {
474 :     LookupLangKey(keyfmt, charsmax(keyfmt), KeyWithoutName, i);
475 :    
476 :     // skip the "adminname" argument if not showing name
477 :     vformat(buffer, charsmax(buffer), keyfmt, 4);
478 :     client_print(i, print_chat, "%s", buffer);
479 :     }
480 :     }
481 :    
482 :     }
483 :     }
484 :    
485 : ian 1 stock colored_menus()
486 :     {
487 :     new mod_name[32];
488 :     get_modname(mod_name,31);
489 :    
490 :     return ( equal(mod_name,"cstrike") || equal(mod_name,"czero") || equal(mod_name,"dod") );
491 :     }
492 :    
493 :     stock cstrike_running()
494 :     {
495 :     new mod_name[32];
496 :     get_modname(mod_name,31);
497 :    
498 :     return ( equal(mod_name,"cstrike") || equal(mod_name,"czero") || equal(mod_name,"csv15") || equal(mod_name,"cs13") );
499 :     }
500 :    
501 :     stock is_running(const mod[])
502 :     {
503 :     new mod_name[32];
504 :     get_modname(mod_name,31);
505 :    
506 :     return equal(mod_name,mod);
507 :     }
508 :    
509 :     stock get_basedir(name[],len)
510 :     {
511 :     return get_localinfo("amxx_basedir",name,len);
512 :     }
513 :    
514 :     stock get_configsdir(name[],len)
515 :     {
516 :     return get_localinfo("amxx_configsdir",name,len);
517 :     }
518 :    
519 :     stock get_datadir(name[],len)
520 :     {
521 :     return get_localinfo("amxx_datadir",name,len);
522 :     }
523 :    
524 :     stock register_menu(const title[],keys,const function[],outside=0)
525 :     {
526 :     register_menucmd(register_menuid(title,outside),keys,function);
527 :     }
528 :    
529 :     /* Backwards Compatibility
530 :     * don't use it! */
531 :     stock get_customdir(name[],len)
532 :     {
533 :     return get_localinfo("amxx_configsdir",name,len);
534 :     }
535 :    
536 :     /* Add a menu item to Menus Front-End plugin ("amxmodmenu"):
537 :     * MENU_TEXT: Text that will be shown for this item in menu
538 :     * MENU_CMD: Command that should be executed to start menu
539 :     * MENU_ACCESS: Access required for menu
540 :     * MENU_PLUGIN: The exact case-insensitive name of plugin holding the menu command
541 :     */
542 :     stock AddMenuItem(const MENU_TEXT[], const MENU_CMD[], const MENU_ACCESS, const MENU_PLUGIN[])
543 :     {
544 :     AddMenuItem_call(MENU_TEXT, MENU_CMD, MENU_ACCESS, MENU_PLUGIN, false);
545 :     }
546 :     /* Just like above, but add menu item to "amx_menu", that should also be accessible by non-admins.
547 :     */
548 :     stock AddClientMenuItem(const MENU_TEXT[], const MENU_CMD[], const MENU_ACCESS, const MENU_PLUGIN[])
549 :     {
550 :     AddMenuItem_call(MENU_TEXT, MENU_CMD, MENU_ACCESS, MENU_PLUGIN, true);
551 :     }
552 :    
553 :     // Internal function used by above stocks.
554 :     stock AddMenuItem_call(const MENU_TEXT[], const MENU_CMD[], const MENU_ACCESS, const MENU_PLUGIN[], const bool:ADD_TO_CLIENT_MENU)
555 :     {
556 :     new pluginid = is_plugin_loaded("Menus Front-End");
557 :     if (pluginid == -1) {
558 :     log_amx("Can't add menu item ^"%s^" from plugin ^"%s^" to menu set because the Menus Front-End plugin itself is not loaded!", MENU_TEXT, MENU_PLUGIN);
559 :     return; // Menus Front-End doesn't exist, return.
560 :     }
561 :    
562 :     new filename[64], b[1];
563 :     get_plugin(pluginid, filename, 63, b, 0, b, 0, b, 0, b, 0);
564 :    
565 :     new status = callfunc_begin(ADD_TO_CLIENT_MENU ? "AddClientMenu" : "AddMenu", filename);
566 :     new bool:failed = true;
567 :     switch (status)
568 :     {
569 :     case 1: failed = false;
570 :     case 0: log_amx("Run time error! (AddMenuItem_call failed)");
571 :     case -2: log_amx("Function not found! (AddMenuItem_call failed)");
572 :     case -1: log_amx("Plugin not found! (AddMenuItem_call failed)");
573 :     }
574 :     if (failed)
575 :     {
576 :     return;
577 :     }
578 :     // Item text
579 :     callfunc_push_str(MENU_TEXT);
580 :     // Cmd
581 :     callfunc_push_str(MENU_CMD);
582 :     // Access
583 :     callfunc_push_int(MENU_ACCESS);
584 :     // Menu exists in this plugin
585 :     callfunc_push_str(MENU_PLUGIN);
586 :    
587 :     callfunc_end();
588 :     }
589 :    
590 :    
591 :     stock constraint_offset(low, high, seed, offset)
592 :     {
593 :     new numElements = high - low + 1;
594 :     offset += seed - low;
595 :    
596 :     if (offset >= 0)
597 :     {
598 :     return low + (offset % numElements);
599 :     }
600 :     else
601 :     {
602 :     return high - (abs(offset) % numElements) + 1;
603 :     }
604 :    
605 :     return 0; // Makes the compiler happy -_-
606 :     }
607 : ian 17
608 :     /* Returns true if the user has ANY of the provided flags
609 :     * false if they have none
610 :     */
611 :     stock has_flag(id, const flags[])
612 :     {
613 :     return (get_user_flags(id) & read_flags(flags));
614 :     }
615 :     /* Returns true if the user has ALL of the provided flags
616 :     * false otherwise
617 :     */
618 :     stock has_all_flags(id, const flags[])
619 :     {
620 :     new FlagsNumber=read_flags(flags);
621 :     return ((get_user_flags(id) & FlagsNumber)==FlagsNumber);
622 :     }

Contact
ViewVC Help
Powered by ViewVC 1.0.4