Difference between revisions of "Writing Advanced Modules In C"
From ASSS Wiki
m (fixed spacing, might replace strtok with strsplit later) |
(now its easier for the newbies :D) |
||
Line 53: | Line 53: | ||
// define macros for comparing strings that work for both linux and windows | // define macros for comparing strings that work for both linux and windows | ||
#ifdef WIN32 | #ifdef WIN32 | ||
− | + | //a case insensitive comparison that returns 0 if both are the same | |
− | + | #define istrcmp(x,y) (stricmp(x,y) == 0) | |
+ | //a case insensitive comparison that returns 0 if both are the same for the first N letters | ||
+ | #define istrcmpn(x,y,n) (strnicmp(x,y,n) == 0) | ||
#else | #else | ||
− | + | //a case insensitive comparison that returns 0 if both are the same | |
− | + | #define istrcmp(x,y) (strcasecmp(x,y) == 0) | |
+ | //a case insensitive comparison that returns 0 if both are the same for the first N letters | ||
+ | #define istrcmpn(x,y,n) (strncasecmp(x,y,n) == 0) | ||
#endif | #endif | ||
local void examplecommand(const char *command, const char *params, Player *p, const Target *t) | local void examplecommand(const char *command, const char *params, Player *p, const Target *t) | ||
{ | { | ||
− | + | chat->SendMessage(p,"Sentence: %s",params); | |
− | |||
− | while(word) // | + | char word[255]; //where strsplit will store each word |
+ | const char *tmp=NULL; //pointer that strsplit needs to work | ||
+ | while(strsplit(params," ,:",word,sizeof(word),&tmp)) //for each word, seperated by ' ' or ',' or ':' | ||
{ | { | ||
− | if( | + | chat->SendMessage(p,"Word: %s",word); //the word currently being reviewed |
+ | |||
+ | if(istrcmpn(word,"a=",2)) //remember, the N means it is checking only first 2 letters | ||
{ | { | ||
//do stuff if first 2 letters of word are 'a' then '=' | //do stuff if first 2 letters of word are 'a' then '=' | ||
Line 74: | Line 81: | ||
int check=atoi(word); //then read a number | int check=atoi(word); //then read a number | ||
} | } | ||
− | else if( | + | else if(istrcmpn(word,"bc=",3)) //3 this time |
{ | { | ||
//do stuff if first 3 letters of word are 'b' then 'c' then '=' | //do stuff if first 3 letters of word are 'b' then 'c' then '=' | ||
Line 81: | Line 88: | ||
int check=atoi(word); //then read a number | int check=atoi(word); //then read a number | ||
} | } | ||
− | else if( | + | else if(istrcmp(word,"-de")) //no N, it just checks the whole thing |
{ | { | ||
//do stuff if word is "-de" | //do stuff if word is "-de" | ||
} | } | ||
− | |||
} | } | ||
− | |||
− | |||
} | } | ||
</pre> | </pre> |
Revision as of 19:46, 7 January 2011
This tutorial explains how to write advanced modules in C. It is assumed you know how to code and are familiar with how the ASSS code works.
This tutorial is a continuation of Writing Modules In C.
Some useful references:
http://qnxcs.unomaha.edu/help/product/neutrino/lib_ref/summary.html
http://www.cplusplus.com/reference/
Contents
Passing Data To Timers
typedef struct ThisIsData { Player *p; int number; } ThisIsData; local int timerfunc(void *vp) //vp is void pointer, just an address that can point anywhere { ThisIsData *tid=(ThisIsData*)vp; //we know it points to our data if(tid->number == 10) { //if it worked anything in here will work too } int returnValue=0; //return 1 if you want timer to run again, or 0 if you want it to be removed if(!returnValue) afree(tid); //if zero, free the data we have previously allocated return returnValue; } anotherfunction() { ThisIsData *tid=amalloc(sizeof(ThisIsData)); //we must allocate memory because anything in this function is destroyed when it ends tid->number=10 //now set timer to activate in 1000 centiseconds, then repeat every 100. //we are also sending the address of the memory we just allocated. ml->SetTimer(timerfunc,1000,100,tid,0); }
Passing Multiple Arguments To Commands
Since the words are read one by one, then checked against the whole list, they can be in any order!
// define macros for comparing strings that work for both linux and windows #ifdef WIN32 //a case insensitive comparison that returns 0 if both are the same #define istrcmp(x,y) (stricmp(x,y) == 0) //a case insensitive comparison that returns 0 if both are the same for the first N letters #define istrcmpn(x,y,n) (strnicmp(x,y,n) == 0) #else //a case insensitive comparison that returns 0 if both are the same #define istrcmp(x,y) (strcasecmp(x,y) == 0) //a case insensitive comparison that returns 0 if both are the same for the first N letters #define istrcmpn(x,y,n) (strncasecmp(x,y,n) == 0) #endif local void examplecommand(const char *command, const char *params, Player *p, const Target *t) { chat->SendMessage(p,"Sentence: %s",params); char word[255]; //where strsplit will store each word const char *tmp=NULL; //pointer that strsplit needs to work while(strsplit(params," ,:",word,sizeof(word),&tmp)) //for each word, seperated by ' ' or ',' or ':' { chat->SendMessage(p,"Word: %s",word); //the word currently being reviewed if(istrcmpn(word,"a=",2)) //remember, the N means it is checking only first 2 letters { //do stuff if first 2 letters of word are 'a' then '=' word+=2; //like move start of word 2 letters forward int check=atoi(word); //then read a number } else if(istrcmpn(word,"bc=",3)) //3 this time { //do stuff if first 3 letters of word are 'b' then 'c' then '=' word+=3; //make sure you move it 3 letters and not 2 int check=atoi(word); //then read a number } else if(istrcmp(word,"-de")) //no N, it just checks the whole thing { //do stuff if word is "-de" } } }
Creating Callbacks
Write me!
//example code goes here
Creating Interfaces
Cover overwriting existing interfaces to replace old modules. Write me!
//example code goes here
Sending Packets To Players
Cover position packets, weapon packets, clientset stuff, etc. Write me!
//example code goes here
Using Advisors
Write me!
//example code goes here
Creating Advisors
Write me!
//example code goes here