PDA

View Full Version : [Include] [INC]gcmd v0.4.1(2009/07/18)


yezizhu
10/05/2009, 08:55 AM
[gcmd]

Description:
gcmd is a command system:
Own its individual command syntax
Support mutli-named command(gcmd("command1","typeme_a","typeme_b","typeme_c"))
Support "dcmd" syntax
Support individual teleport command
It also similar with dcmd,so you can easily know how to use gcmd.
Version:0.4.1

Start to use gcmd
Firstly,you should DO:

//First add include <gcs_gcmd> on the top of script.
#include <gcs_gcmd>
//then add gcmd_struction() when install
public OnGameModeInit(){
gcmd_struction();//struction
return true;
}
//or
public OnFilterScriptInit(){
gcmd_struction();
return true;
}
//And then add gcmd_process() in OnPlayerCommandText(playerid,cmdtext[])
public OnPlayerCommandText(playerid,cmdtext[]){
gcmd_tele_insert();//struction for tele system
gcmd_process();//process for main system
return false;
}

Now,struct finished.

1 command = 1 teleportion
gcmd individual teleport command.

public OnPlayerCommandText(playerid,cmdtext[]){
gcmd_tele_insert();//struction
//gcmd_tele(command[],Float:x,Float:y,Float:z,Float:angel,Interior,igno reVec,remindText[]);
/**
ignoreVec = true --> teleport player without vehicle
ignoreVec = false --> teleport player within vehicle(if has)
*/
gcmd_tele("helloworld",1836.75,-1837.99,26.04,180.0,0,true,"Hellow,world!");//type "/helloworld" you will be teleported
return false;
}

It is easy and fast.

Translate dcmd to gcmd easily
You can easily translate dcmd to gcmd without function alomost any code edit now^^
Note:
It has less effeicent than using gcmd function.
Is time to translate dcmd&sscanf to gcmd^^.

//Firstly,change dcmd to gcmd_create
//eg:
public OnPlayerCommandText(playerid,cmdtext[]){
dcmd(kill,4,cmdtext);
return false;
}
//edited to:
public OnGameModeInit(){// OR OnFilterScriptInit()
gcmd_create("kill","kill");
return true;
}
//Secondly,change dcmd_functioname(playerid,params[]) to gcmd(functionname)
//eg:
dcmd_kill(playerid,params[])
//edited to:
gcmd(kill)

Now start to compiler,will get no error and works find!

Use gcmd individual syntax:
If you feel something strange,don't worry,you will get it.
[/PAWN]
Create kill command
add gcmd_create("Command_Kill","kill","k"); in OnGameModeInit();
Then:

gcmd(Command_Kill){
SetPlayerHealth(player(),0);
return true;
}


Create kick command
add gcmd_create("Command_Kick","kick","kickoff"); in OnGameModeInit();
Then:

gcmd(Command_Kick){
if(param("u"){//chexk if is playerid with connection
new
tmpstr[128];
if(paramn(2)){//check no.2 param if is null
format(tmpstr,sizeof tmpstr,"[Server]%d has been kicked by %d(reason:unknown).",parami(1),player());
}else{
format(tmpstr,sizeof tmpstr,"[Server]%d has been kicked by %d(reason:%s).",parami(1),player(),params(2));
}
kick(parami(1));
SendClientMessageToAll(0xFF8080FF,tmpstr);
}else{
SendClientMessage(player(),0xFF8080FF,"[Server]Unconnect/exist playerid");
}
return true;
}

Create ban command
add gcmd_create("Command_Ban","ban","b"); in OnGameModeInit();
Then:

gcmd(Command_Ban){
if(param("u"){//chexk if is playerid with connection
new
tmpstr[128];
if(paramn(2)){//check no.2 param if is null
format(tmpstr,sizeof tmpstr,"[Server]%d has been banned by %d(reason:unknown).",parami(1),player());
}else{
format(tmpstr,sizeof tmpstr,"[Server]%d has been banned by %d(reason:%s).",parami(1),player(),params(2));
}
kick(parami(1));
SendClientMessageToAll(0xFF8080FF,tmpstr);
}else{
SendClientMessage(player(),0xFF8080FF,"[Server]Unconnect/exist playerid");
}
return true;
}

Now,is time for some complex command.
Here's the explainsion:
cheatCommand:
text:
/cheat,/c
usage:
active cheat code,or active admin command
type "/cheat code health human(or god) 0" will change ur health
type "/cheat code armour human(or god) 0" will change ur armour
type "/cheat code gun minigun(or unkown) 0" will give u weapon
type "/cheat code admin active 911" will active adminCommand
adminCommand:
text:
/admin,/a
usage:
ban/unban/create/destroy cheatCommand
type "/admin 911 key 0" will destroy cheatCommand
type "/admin 911 key 1" will create cheatCommand
type "/admin 911 key 255" will ban cheatCommand for all
type "/admin 911 key 256" will unban cheatCommand for all
Add

gcmd_create("cheatCommand","cheat","c");
gcmd_create("adminCommand","admin","a");
gcmd_create("killCommand","kill","k");
gcmd_setPlayerAbleUse(INVALID_PLAYER_ID,"adminCommand",false);//INVALID_PLAYER_ID means 0-199
in OnGameModeInit();
Then:

#define send(%1) \
SendClientMessage(player(),0xFFFFFFAA,(%1))

gcmd(cheatCommand){
if(param("sssf","code","","")){//check if is same string, and no.2,no.3 params if are string,and no.4 param if is floatric
if(paramsame(2,"health")){//no.2 param if is "health"
if(paramsame(3,"human")){//no.3 param if is "human"
SetPlayerHealth(player(),90);
send("Im human");
}else if(paramsame(3,"god")){//no.3 param if is "god"
SetPlayerHealth(player(),99999);
send("Im god...");
}
}else if(paramsame(2,"armour")){//no.2 param if is "armour"
if(paramsame(3,"human")){//no.3 param if is "human"
SetPlayerArmour(player(),90);
send("I have armour");
}else if(paramsame(3,"human")){//no.3 param if is "god"
SetPlayerArmour(player(),99999);
send("AI's here...");
}
}else if(paramsame(2,"gun")){//no.2 param if is "gun"
if(paramsame(3,"minigun"))//no.3 param if is "minigun"
GivePlayerWeapon(player(),38,9997);
if(paramsame(3,"unknown"))//no.3 param if is "unknown"
GivePlayerWeapon(player(),37,9997);
}
send("cheat code active");
}else if(param("ssi","admin","active")){//if is same string + no.3 param if is a numberic
if(paramsame(3,"911")){//no.3 param if is "911"
new
tmpstr[128],
retstr[10][128];
gcmd_getCommand("adminCommand",retstr);//create command
format(tmpstr,sizeof tmpstr,"security code correct,you can now use %s %s %s %s %s %s %s %s %s now",retstr[0],retstr[1],retstr[2],retstr[3],retstr[4],retstr[5],retstr[6],retstr[7],retstr[8],retstr[9]);
send(tmpstr);
gcmd_setPlayerAbleUse(player(),"adminCommand",true);
}else{
send("security code error");
}
}
return true;
}

gcmd(adminCommand){
if(paramn(1)){
send("please input security code");
return true;
}
if(param("isi","911","key")){//check if is same string,and no.3 param if is numberic
if(paramsame(3,"0")){//if no.3 param is 0
gcmd_delete("cheatCommand");
send("[Admin]Destroy cheatCommand");
}else if(paramsame(3,"1")){//if no.3 param is 1
gcmd_create("cheatCommand","cheat","c");
send("[Admin]Create cheatCommand");
}else if(paramsame(3,"255")){//if no.3 param is 255
if(gcmd_setPlayerAbleUse(parami(3),"cheatCommand",false)){
send("[Admin]No one can use cheat command now");
//because "parami(3)" is 255(INVALID_PLAYER_ID),so no one can use this command now
}else{
send("[Admin]cheat command not exist");
}
}else if(paramsame(3,"256")){//if no.3 param is 256
if(gcmd_setPlayerAbleUse(INVALID_PLAYER_ID,"cheatCommand",true)){
send("[Admin]Everyone can use cheat command now");
//INVALID_PLAYER_ID means ban this command for anyone
}else{
send("[Admin]cheat command doesnt exist");
}
}
}
return true;
}


Time analysing:
loop 1000 times
gcmd main : 10329 10328 10359 10359 10360
gcmd tele : 3547 3563 3594 3562 3578
dcmd : 4766 4734 4719 4750 4750
test code.
#define TEST_TYPE (1)//-- 1=gcmd_main,2=gcmd_tele,3=dcmd


#include <a_samp>

#if (TEST_TYPE == 1 || TEST_TYPE == 2)
#include <gcs/gcs_gcmd>
#endif
main(){
#if TEST_TYPE == 1
print "Time test:gcmd_main"
#elseif TEST_TYPE == 2
print "Time test:gcmd_tele"
#elseif TEST_TYPE == 3
print "Time test:dcmd "
#else
#error "Please set TEST_TYPE"
#endif
}


#if TEST_TYPE == 3
#define dcmd(%1,%2,%3) if ((strcmp((%3)[1], #%1, true, (%2)) == 0) && ((((%3)[(%2) + 1] == 0) && (dcmd_%1(playerid, "")))||(((%3)[(%2) + 1] == 32) && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1
#endif

public OnGameModeInit(){
#if (TEST_TYPE == 1 || TEST_TYPE == 2)
gcmd_struction();
#endif

#if TEST_TYPE == 1
for(new i;i<1000;i++){
gcmd_create("tele001","t-tele001");
gcmd_create("test2","t-test");
gcmd_create("complexcommand");
gcmd_create("itypecomplexcommand","t_itypecomplexcommand");
gcmd_create("testcommand","t-testcommand");
gcmd_create("tele001","t-tele001");
gcmd_create("test2","t-test");
gcmd_create("complexcommand");
gcmd_create("itypecomplexcommand","t_itypecomplexcommand");
gcmd_create("testcommand","t-testcommand");
}// = 10K commands
#endif

timetest_struction();
SetTimer("timetest_start",1500,false);
}

forward timetest_start();

timetest_struction(){
OnPlayerCommandText(0,"/");
}

public timetest_start(){
print "start"
for(new t;t<5;t++){
new
tick1,tick2;
tick1 = GetTickCount();
for(new i;i<1000;i++){
OnPlayerCommandText(0,"/01 23 45 67 89 01 23 45 67 89");///itypecomplexcommand2
}
tick2 = GetTickCount();
printf("tick:%d",tick2-tick1);
}
print "end"
}


#if TEST_TYPE == 3
dcmd_tele001(playerid,params[]){
return true;
}
dcmd_test2(playerid,params[]){
return true;
}
dcmd_complexcommand(playerid,params[]){
return true;
}
dcmd_itypecomplexcommand(playerid,params[]){
return true;
}
dcmd_testcommand(playerid,params[]){
return true;
}
#endif

public OnPlayerCommandText(playerid,cmdtext[]){
#if TEST_TYPE == 1
gcmd_process();
#endif

#if TEST_TYPE == 2
gcmd_tele_insert();
for(new i;i<1000;i++){
gcmd_tele("tele001",125,320,5,random(360),0,true,"Welcome to tele001");
gcmd_tele("test2",125,320,5,random(360),0,true,"Welcome to tele001");
gcmd_tele("complexcommand",125,320,5,random(360),0,true,"Welcome to tele001");
gcmd_tele("itypecomplexcommand",125,320,5,random(360),0,true,"Welcome to tele001");
gcmd_tele("testcommand",125,320,5,random(360),0,true,"Welcome to tele001");
gcmd_tele("tele001",125,320,5,random(360),0,true,"Welcome to tele001");
gcmd_tele("test2",125,320,5,random(360),0,true,"Welcome to tele001");
gcmd_tele("complexcommand",125,320,5,random(360),0,true,"Welcome to tele001");
gcmd_tele("itypecomplexcommand",125,320,5,random(360),0,true,"Welcome to tele001");
gcmd_tele("testcommand",125,320,5,random(360),0,true,"Welcome to tele001");
}// = 10K commands
#endif

#if TEST_TYPE == 3
for(new i;i<1000;i++){
dcmd(tele001,7,cmdtext);
dcmd(test2,5,cmdtext);
dcmd(complexcommand,14,cmdtext);
dcmd(itypecomplexcommand,19,cmdtext);
dcmd(testcommand,11,cmdtext);
dcmd(tele001,7,cmdtext);
dcmd(test2,5,cmdtext);
dcmd(complexcommand,14,cmdtext);
dcmd(itypecomplexcommand,19,cmdtext);
dcmd(testcommand,11,cmdtext);
}// = 10K commands
#endif

return false;
}


Download:
gcmd v0.4:
gcs_gcmd.inc (http://pawn.pastebin.com/f29e30f2e)
example:
example.pwn (http://pawn.pastebin.com/fe8427fc)

Bug(s):
1.Thouth it can easily translate dcmd to gcmd(see reply#3),
it should replace all "return value;" to "return;" in the command function,
or you will get warnning 209.
In all,it does't matter
-

Other credits:
Nero_3D - 10 ----- SortArray (http://forum.sa-mp.com/index.php?topic=61574.480)
Y_less ----- sscanf (http://forum.sa-mp.com/index.php?topic=61574.msg557915;topicseen#msg55791 5)

If you connot understand how to use gcmd,or any bug you find,
please contact us[mail,pmsg,or post].I'll try my best to help you (English my bad) :mrgreen:

r:GCS Team

Lewwy
10/05/2009, 09:04 AM
Ahh indentation! :o

shindo
10/05/2009, 09:48 AM
so good. ;)

yezizhu
10/05/2009, 09:50 AM
Edited

shindo
10/05/2009, 09:53 AM
i have a question,how does it work? :?:

yezizhu
10/05/2009, 10:08 AM
Edited.

Weirdosport
10/05/2009, 02:20 PM
This looks really unclear, and I don't see it being better than DCMD and SSCANF...

yezizhu
10/05/2009, 02:44 PM
This looks really unclear, and I don't see it being better than DCMD and SSCANF...


OK...Here's easy use

public OnGamemodeInit(){
gcmd("testCommand","test","t");
return true;
}
public OnPlayerCommandText(playerid,cmdtext[]){
return true;
}
//Player() - return the playerid
//params(pos) - return param (split by whitespace)
Command(testCommand){
if(!nullparam(1)) printf "player:%d,param:%s" ,Player(),params(1)
else printf "player:%d",Player()
return true;
}

yezizhu
11/05/2009, 10:41 AM
Update

@TheShadow@
11/05/2009, 12:18 PM
hm its not so difficult to use dcmd^^ Why should we use yours? Is it faster or something?

yezizhu
11/05/2009, 04:19 PM
hm its not so difficult to use dcmd^^ Why should we use yours? Is it faster or something?

As I know,dcmd use strcmp to loop server commands,this is the main reason that affects efficiency.
In gcmd,it will check only two integer values first,that is far more faster than strcmp.

And while using gcmd,it offers some functions(some are marcos) to suit ur work.
eg:
paramsame(ppos,str[]); --> to check if is complete same string or not
paramlist(param1,param2...,param10); --> to check if is same with params
If you look the example 2,you will get more information about gcmd.(I'll try to make example 2 clearer,my fault.)
gcmd also easy for making one command with different types.(gcmd("testCommand","a","b","c","d","e"))
Whats more,it does support create/destroy and disallow/allow player to use any command.

If you have more suggestions,please advice tehn to me.^^

G_ROW_Chez
13/05/2009, 10:37 AM
as i know dcmd uses sscanf also...
it's fuckn simple an you don't need to make this idiotic funcs... your gcmd some kind of useless... need to understand and also debug
dcmd+sscanf and 4 minutes for make the command at all

yezizhu
13/05/2009, 01:09 PM
Chez ]
as i know dcmd uses sscanf also...
it's fuckn simple an you don't need to make this idiotic funcs... your gcmd some kind of useless... need to understand and also debug
dcmd+sscanf and 4 minutes for make the command at all


Thx for ur advice,i've found gcmd0.1's is very usless and inconvenientt for such command "/test <playerid>",which I was inadvertent on designing gcmd.
I'll update this issue,as far as better than d+s^^

yezizhu
29/05/2009, 08:24 AM
updated.

G_ROW_Chez
30/05/2009, 11:09 AM
your code is awesome! i need two days to understand it =)
Good job, i'm wait for anozer update

yezizhu
30/05/2009, 02:44 PM
Chez ]
your code is awesome! i need two days to understand it =)
Good job, i'm wait for anozer update

Thx^^,
in my opion, some code still unclear,and something still usless.That's why still 0.x version.
Im looking fowrad to getting commentary ,for taking more convenient,efficient for users^^

yezizhu
06/06/2009, 06:51 AM
Updated.
Add some examples in first post,and upgrade gcmd from 0.2.1 to 0.2.2

yezizhu
06/06/2009, 01:08 PM
Updated.
Easy for transalting dcmd to gcmd now(see Reply #3).
And upgrade gcmd from 0.2.2 to 0.2.3

yezizhu
07/06/2009, 02:42 PM
Updated.
1.It now use less memory and less time
2.Highly support for translating dcmd to gcmd now.(Only 1 issue)
3.No user-operation changed.
4.Upgrade gcmd from 0.2.4 to 0.3.0.

mysan
07/06/2009, 07:13 PM
Excellent work ! So function allows one command to do just short and full version? :o

yezizhu
08/06/2009, 01:07 PM
Updated.
1.It can make tele command in 1 line.
2.Fix some issues.
3.No user-operation changed.
4.Upgrade gcmd from 0.3.0 to 0.4.0

yezizhu
08/06/2009, 01:08 PM
Excellent work ! So function allows one command to do just short and full version? :o


Not only full&short version,they're allowed what you like them to be

yezizhu
18/07/2009, 02:58 PM
Updated.
1.Fix a bug that doesn't work for some ids.
2.Remove some debug info.