AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   wolle_gravity (https://forums.alliedmods.net/showthread.php?t=22758)

Wolle 01-03-2006 09:55

wolle_gravity
 
2 Attachment(s)
Hi everyone.
I wrote this small plugin to easily control the gravity setting on my server.
It's nothing new but I hope others also have a use for it.

I also welcome constructive criticism. I'm particularly looking for performance holes. Let me know if I could have written something different to make the code execute faster.

This plugin allows admins of access level A(m) to set a server's gravity through a chat command (/grav).
This command takes 2 arguments. The amount of gravity and the time in minutes for its duration.
So if you want to set a server's gravity to let's say 200 for 5 minutes simply type "/grav 200 5" without the quotes.
To reset the gravity type "/grav normal".
Enjoy!

EDIT
I just discovered that "sv_gravity" also excepts negative values.
I wasn't aware of that and therefore updated the code to dissallow negativ e numbers.
The updated code is below.
The 2 attachments are also updated.

I'm also thinking about allowing floating point numbers for both gravity and time.
Gravity doesn't really need that but it ecepts it so what the heck but it's nice for the time because it then allows to set the time to seconds instead of just minutes.
I might add that later.

Code:
/* AMXX Mod script. * * (c) Copyright 2005, Wolle * This file is provided as is (no warranties) * * Allows admins to easily control gravity * * Usage: "command amountofgravity timeinminutes" * Example: To set the gravity to 200 for a duration of 5 minutes just say or team_say: "/grav 200 5" * To reset the gravity to normal level use: "/grav normal" * * Enjoy!! * Contact: <a href="mailto:[email protected]">[email protected]</a> */ #include <amxmodx> #include <fun> new PLUGIN[]="Gravity Control"; new AUTHOR[]="Wolle"; new VERSION[]="1.00"; new g_iDuration = 0; new bool:g_bTimesUp = false; #define MAX_ARGUMENTS 3 #define MAX_ARGUMENT_LENGTH 32 public plugin_init() {     register_plugin( PLUGIN, VERSION, AUTHOR );     register_clcmd ( "say", "GravityControl" );     register_clcmd ( "say_team", "GravityControl" ); } public Timer() {     if( g_iDuration > 0 )     {         g_iDuration--;                 if( g_iDuration == 0 )         {             g_bTimesUp = true;         }         else if( g_iDuration <= 5 )         {             set_hudmessage( 230, 130, 30, 0.03, 0.6, 2, 0.02, 0.6, 0.01, 0.3, 0 );             show_hudmessage( 0, "Normal gravity in: %d", g_iDuration );         }                 if( g_bTimesUp )         {             set_cvar_num( "sv_gravity" , 800 );             g_bTimesUp = false;                         set_hudmessage( 230, 130, 30, 0.03, 0.6, 2, 0.02, 5.0, 0.01, 0.3, 1 );             show_hudmessage( 0, "Gravity has been restored." );         }     }     else if( g_iDuration < 0 )     {         g_iDuration = 0;     } } public GravityControl( id ) {     if( !id )     {         return PLUGIN_CONTINUE;     }         if( get_user_flags( id ) & ADMIN_LEVEL_A )     {         new i, j, len, len2, arg1, arg2;         new said[192], cmd[10], arguments[MAX_ARGUMENTS][MAX_ARGUMENT_LENGTH];                 read_argv( 0, cmd, 9 );         read_args( said, 191 );         remove_quotes( said );         len = strlen( said );                 for( i = 0 ; i < len ; i++ )         {             if ( said[i] == ' ' )             {                 j++;             }             else             {                 if( containi( arguments[0], "/grav" ) != -1 )                 {                     if( j > 2 )                     {                         set_hudmessage( 230, 130, 30, 0.03, 0.6, 2, 0.02, 5.0, 0.01, 0.3, 1 );                         show_hudmessage( 0, "Too many arguments!!^nUsage: /grav <gravity> <time>" );                                                 return PLUGIN_CONTINUE;                     }                 }                                 len2 = strlen( arguments[j] );                 add( arguments[j], len2+1, said[i] );             }         }                 if( containi( arguments[0], "/grav" ) != -1 )         {             arg1 = str_to_num( arguments[1] );             arg2 = str_to_num( arguments[2] );                         if( arg1 != 0 && arg2 != 0 || containi( arguments[1], "normal" ) != -1 )             {                 new user_name[64];                 get_user_name ( id, user_name, 63 );                                 if( containi( arguments[1], "normal" ) != -1 )                 {                     set_cvar_num( "sv_gravity" , 800 );                     g_iDuration = 0;                     remove_task( 1, 0 );                                         set_hudmessage( 230, 130, 30, 0.03, 0.6, 2, 0.02, 5.0, 0.01, 0.3, 0 );                     show_hudmessage( 0, "%s set the gravity back to normal", user_name );                                         return PLUGIN_CONTINUE;                 }                 else if( arg1 < 0 || arg2 < 0 )                 {                     set_hudmessage( 230, 130, 30, 0.03, 0.6, 2, 0.02, 5.0, 0.01, 0.3, 0 );                     show_hudmessage( 0, "No negative numbers allowed!" );                                         return PLUGIN_CONTINUE;                 }                 else if( arg2 * 60 > get_timeleft())                 {                     set_hudmessage( 230, 130, 30, 0.03, 0.6, 2, 0.02, 5.0, 0.01, 0.3, 1 );                     show_hudmessage( id, "Theres not enough time left.^nSet the duration to a lower value!" );                 }                 else                 {                     g_iDuration = arg2*60;                     set_task( 1.0, "Timer", 1, "", 0, "a", g_iDuration );                     set_cvar_num( "sv_gravity" , arg1 );                                         set_hudmessage( 230, 130, 30, 0.03, 0.6, 2, 0.02, 5.0, 0.01, 0.3, 0 );                     show_hudmessage( 0, "%s set the gravity to %d for %d minutes", user_name, arg1, arg2 );                 }             }             else             {                 set_hudmessage( 230, 130, 30, 0.03, 0.6, 2, 0.02, 5.0, 0.01, 0.3, 0 );                 show_hudmessage( id, "Invalid Arguments!!^nUsage: /grav <gravity> <time>" );             }         }         engclient_cmd ( id ,cmd ,said );     }     return PLUGIN_CONTINUE; }

Charr 01-03-2006 13:01

Everything looks good, but I'm not sure if the method you used to read the args would work.

Also, this wouldn't be the right forum for this. This really should go in New Submissions.

Wolle 01-03-2006 13:42

Darn my bad.
Maybe a moderator or an administrator can move this thread then?
Thanks!

Ohh and sorry for the inconvenience.

Jordan 01-03-2006 17:55

1) You don't need to post the .amxx, it gets put in automatically if you post the .sma

2) You need to edit the client commands - for example: the client command is just say, so if I type anything, like "hi" the gravity will be changed. Make the commands "say /gravity 200" or w/e, you get my point :)

3) This is kind of nitpicky, but you can make this script a smaller:

Code:
/* AMXX Mod script. * * (c) Copyright 2005, Wolle * This file is provided as is (no warranties) * * Allows admins to easily control gravity *   * Usage: "command amountofgravity timeinminutes" * Example: To set the gravity to 200 for a duration of 5 minutes just say or team_say: "/grav 200 5" * * Enjoy!! * Contact: <a href="mailto:[email protected]">[email protected]</a> */ #include <amxmodx> #include <fun> #define MAX_ARGUMENTS 3 #define MAX_ARGUMENT_LENGTH 32 new g_iDuration = 0; new bool:g_bTimesUp = false; public plugin_init() {     register_plugin("Gravity Control", "1.00", "Wolle");     register_clcmd ("say", "GravityControl");     register_clcmd ("say_team", "GravityControl"); } public Timer() {     if(g_iDuration > 0)     {         g_iDuration--;                   if(g_iDuration == 0)         {             g_bTimesUp = true;         }         else if(g_iDuration <= 5)         {             set_hudmessage(230, 130, 30, 0.03, 0.6, 2, 0.02, 0.6, 0.01, 0.3, 0);             show_hudmessage(0, "Normal gravity in: %d", g_iDuration);         }                   if(g_bTimesUp)         {             set_cvar_num("sv_gravity", 800);             g_bTimesUp = false;                           set_hudmessage(230, 130, 30, 0.03, 0.6, 2, 0.02, 5.0, 0.01, 0.3, 1);             show_hudmessage(0, "Gravity has been restored.");         }     }     else if(g_iDuration < 0)     {         g_iDuration = 0;     } } public GravityControl(id) {     if(!id)     {         return PLUGIN_CONTINUE;     }           if(get_user_flags(id) && ADMIN_LEVEL_A)     {         new i, j, len, len2, arg1, arg2;         new said[192], cmd[10], arguments[MAX_ARGUMENTS][MAX_ARGUMENT_LENGTH];                   read_argv(0, cmd, 9);         read_args(said, 191);         remove_quotes(said);         len = strlen(said);                   for(i = 0 ; i < len ; i++)         {             if (said[i] == ' ')             {                 j++;             }             else             {                 if(containi(arguments[0], "/grav") != -1)                 {                     if(j > 2)                     {                         set_hudmessage(230, 130, 30, 0.03, 0.6, 2, 0.02, 5.0, 0.01, 0.3, 1);                         show_hudmessage(0, "Too many arguments!!^nUsage: /grav <gravity> <time>");                         return PLUGIN_CONTINUE;                     }                 }                                   len2 = strlen(arguments[j]);                 add(arguments[j], len2+1, said[i]);             }         }                   if( containi(arguments[0], "/grav") != -1)         {             arg1 = str_to_num(arguments[1]);               arg2 = str_to_num(arguments[2]);                           if(arg1 != 0 && arg2 != 0 || containi(arguments[1], "normal") != -1)             {                 new user_name[64];                 get_user_name (id, user_name, 63);                                   if(containi(arguments[1], "normal") != -1)                 {                     set_cvar_num("sv_gravity" , 800);                     g_iDuration = 0;                     remove_task(1, 0);                                           set_hudmessage(230, 130, 30, 0.03, 0.6, 2, 0.02, 5.0, 0.01, 0.3, 0);                     show_hudmessage(0, "%s set the gravity back to normal", user_name);                                           return PLUGIN_CONTINUE;                 }                 else if(arg2 * 60 > get_timeleft())                 {                     set_hudmessage(230, 130, 30, 0.03, 0.6, 2, 0.02, 5.0, 0.01, 0.3, 1);                     show_hudmessage(id, "Theres not enough time left.^nSet the duration to a lower value!");                 }                 else                 {                     g_iDuration = arg2*60;                     set_task(1.0, "Timer", 1, "", 0, "a", g_iDuration);                     set_cvar_num("sv_gravity" , arg1);                                           set_hudmessage(230, 130, 30, 0.03, 0.6, 2, 0.02, 5.0, 0.01, 0.3, 0);                     show_hudmessage(0, "%s set the gravity to %d for %d minutes", user_name, arg1, arg2);                 }             }             else             {                 set_hudmessage(230, 130, 30, 0.03, 0.6, 2, 0.02, 5.0, 0.01, 0.3, 0);                 show_hudmessage(id, "Invalid Arguments!!^nUsage: /grav <gravity> <time>");             }         }         engclient_cmd (id, cmd, said);     }     return PLUGIN_CONTINUE; }

Also, if you want you can put the { on lines like this:

if(random != random) {

And it'll save you a line :P Yeah but that was just my nitpickiness for the day... overall, gj! :)

Wolle 01-03-2006 19:23

Well, Thanks! and I'm not getting you.
I tested this plugin on my server for a few days first and my admins seem to be quiet happy with how it works.
I don't see how a simple say or team_say "hi" could change a server's gravity setting. Can anyone, who might have tested it, confirm that?

The way it works....it, first of all, ignores none admin clients. It only gets in action if the first phrase includes "/grav". It then checks what else is there, is it too much or is it not what we want. Only then if everything is there it goes ahead, sets the task and executes the command.

Ohh and with the {}, that's how I first learned and how I got used to it.
So, I do that by purpose. :wink:

Jordan 01-03-2006 20:03

Ahhhh, I see, my fault - I didn't notice that line :D

GJ :)

XxAvalanchexX 01-03-2006 21:03

Quote:

Originally Posted by satanwoj
1) You don't need to post the .amxx, it gets put in automatically if you post the .sma

It gets put in automatically? What?

Quote:

Originally Posted by satanwoj
2) You need to edit the client commands - for example: the client command is just say, so if I type anything, like "hi" the gravity will be changed. Make the commands "say /gravity 200" or w/e, you get my point :)

No, he doesn't. He registers the entire say command and reads off the arguments so that it is flexible, otherwise he'd have to register a command for every single combination.

Quote:

Originally Posted by satanwoj
3) This is kind of nitpicky, but you can make this script a smaller

At the cost of readability.

Quote:

Originally Posted by satanwoj
Also, if you want you can put the { on lines like this:

if(random != random) {

And it'll save you a line

Why save lines? The compiler ignores whitespace. Double-spacing your code and single-spacing your code produces the same .amxx. His style slightly increases readability.

It's just his particular coding standard. Everyone has a different style, there's no reason for him to change his because it's not like yours.

Jordan 01-03-2006 21:50

Ohhhh - if you don't post a .sma in the new plugin submission section it doesn't add that... I see.

And I didn't know the compiler ignored whitespace - I assumed it counted it as a character and therefore made the script bigger in size... Ignore my comments :x


All times are GMT -4. The time now is 16:00.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.