Difference between revisions of "MERVBot Tutorial"

From ASSS Wiki
Jump to: navigation, search
(Input to file: forgot header info)
m (fixed tutorial link)
 
(55 intermediate revisions by 9 users not shown)
Line 1: Line 1:
This tutorial is based on the ever-popular MERVBot Tutorial by Underlord. It has since been updated to reflect new changes with MervBot.
+
This tutorial is based on the ever-popular MERVBot Tutorial by Underlord. It has since been updated to reflect new changes with MervBot. To see examples of how to use this instruction, see [[MERVBot Example Code]].
  
 
This tutorial also assumes that you have a basic knowledge of C++. If you don't, check out cplusplus.com's great [http://www.cplusplus.com/doc documentation].
 
This tutorial also assumes that you have a basic knowledge of C++. If you don't, check out cplusplus.com's great [http://www.cplusplus.com/doc documentation].
Line 5: Line 5:
 
==Setting up a MERVBot (plugin)==
 
==Setting up a MERVBot (plugin)==
  
[http://catid.sscentral.com/ MERVBot download site]
+
[http://mervbot.com MERVBot download site]
 
 
[http://www.ssforum.net/ MERVBot Forum] (Development - MervBot forum)
 
  
  
 
===Obtaining MERVBot===
 
===Obtaining MERVBot===
  
* Download the [http://catid.sscentral.com/files/MERVBot.zip latest build].
+
* Download the [http://mervbot.com/files/MERVBot.rar latest build].
* Unzip MERVBot.zip into a new folder. (example c:\program files\continuum\mervbot)
+
* Unrar MERVBot.rar into a new folder. (example c:\program files\continuum\mervbot)
 
* Unzip src.zip into "src" subfolder of that new folder (example c:\program files\continuum\mervbot\src)
 
* Unzip src.zip into "src" subfolder of that new folder (example c:\program files\continuum\mervbot\src)
 
  
 
===Preparing to write a plugin===
 
===Preparing to write a plugin===
Line 21: Line 18:
 
''Note:'' if you only want to execute someone's premade plugin (.dll), skip to [[MERVBot Tutorial#Run your bot dll|step 4]], otherwise continue to learn how to make your own bot
 
''Note:'' if you only want to execute someone's premade plugin (.dll), skip to [[MERVBot Tutorial#Run your bot dll|step 4]], otherwise continue to learn how to make your own bot
  
Download [http://catid.sscentral.com/files/Tutorial.zip DLL-plugin Tutorial] and unzip Tutorial.zip (containing spawn.h, spawn.cpp, and command.cpp) into a "tutorial" subfolder of that new folder. (example c:\program files\continuum\mervbot\src\tutorial).
+
Download [http://www.mervbot.com/files/Tutorial.rar DLL-plugin Tutorial] and unzip Tutorial.zip (containing spawn.h, spawn.cpp, and command.cpp) into a "tutorial" subfolder of that new folder. (example c:\program files\continuum\mervbot\src\tutorial).
  
 
''File descriptions:''
 
''File descriptions:''
Line 27: Line 24:
 
* command.cpp = code for commands coming into bot (ie /!help, /!play, etc)
 
* command.cpp = code for commands coming into bot (ie /!help, /!play, etc)
 
* spawn.cpp = code that interacts with bot spawns
 
* spawn.cpp = code that interacts with bot spawns
 
  
 
===Microsoft Visual c++===
 
===Microsoft Visual c++===
Line 54: Line 50:
 
===Run your bot dll===
 
===Run your bot dll===
  
To run your bot you need your DLL (mybot.dll), Commands.txt, MERVBot.exe, MERVBot.ini, Operators.txt, Spawns.txt, subspace.bin, and zlib.dll all in one folder (example c:\program files\continuum\mervbot).
+
To run your bot you need your DLL (mybot.dll), Commands.txt, MERVBot.exe, MERVBot.ini, Operators.txt, Spawns.txt, and zlib.dll all in one folder (example c:\program files\continuum\mervbot).
  
 
<ol>
 
<ol>
Line 70: Line 66:
 
<br>
 
<br>
 
<pre>[Login]
 
<pre>[Login]
Zone=216.33.98.254:21000 // make that your zone IP:PORT available from zone.dat in Continuum dir
+
Zone=216.33.98.254:21000 // your zone IP:PORT available from zone.dat in Continuum dir
 
</pre>
 
</pre>
  
Line 180: Line 176:
 
MERVBot is event based, so when making a bot you need to decide what will happen at certain events. Normal plugins need to consider what happens when bot enters arena, player enters arena, player leaves arena, player events like kill, shipchange, teamchange, spec, move then any other relevant events to your bot. Just worry about events that are relevant to the tasks your bot is doing.
 
MERVBot is event based, so when making a bot you need to decide what will happen at certain events. Normal plugins need to consider what happens when bot enters arena, player enters arena, player leaves arena, player events like kill, shipchange, teamchange, spec, move then any other relevant events to your bot. Just worry about events that are relevant to the tasks your bot is doing.
  
MERVBot sends events to botInfo::gotEvent() in spawn.cpp. Each supported event is already present and categorized in gotEvent(), along with the paramters that MERVBot sends with the event.  
+
MERVBot sends events to botInfo::gotEvent() in spawn.cpp. Each supported event is already present and categorized in gotEvent(), along with the paramters that MERVBot sends with the event. When a plugin wants the bot to do something, it sends tell(event) to the bot.
 +
 
 +
See dllcore.h for a list of current events and their descriptions. Dllcore.h also contains functions (like makeFollowing) to make events to send back to the bot via tell().
  
See dllcore.h for a list of current events and their descriptions.
+
<pre>tell(makeFollowing(false));</pre>
  
==Messaging - How to use the messaging system==
+
==The Messaging System==
  
 
Private message - void sendPrivate(Player *player, char *msg);
 
Private message - void sendPrivate(Player *player, char *msg);
Line 225: Line 223:
 
Note: to have bot print several lines of text fast it needs sysop in the
 
Note: to have bot print several lines of text fast it needs sysop in the
 
arena (sysop in arena bot first spawns to also) otherwise it'll print slow to avoid being
 
arena (sysop in arena bot first spawns to also) otherwise it'll print slow to avoid being
kicked for spam<br />
+
kicked for spam
 +
 
 +
===Output of data in messages===
 +
<p>An example of using normal strings to output data/messages.</p>
 +
<pre>
 +
// does *arena X pilots left in game
 +
// NOTE: variable temp needs to be defined with some value
  
==Timer - How to use the timing function==
+
String s = "*arena ";
 +
      s += temp;
 +
      s += " pilots left in the game.";
 +
 
 +
sendPublic(s);
 +
</pre>
 +
Or,
 +
<pre>
 +
//NOTE: this can be considered inefficient.
 +
 
 +
sendPublic("*arena " + (String)temp + " pilots left in the game");
 +
</pre>
 +
 
 +
<p>An example using sprintf to align/space data, where output data will be in this approximate format.</p>
 +
<pre>
 +
// output data will be in this approximate format (not lined up perfectly because of html)
 +
// --------------------------------------------------------------------------------------
 +
// Squad: squadname      PTS    FPTS    K    D  DMG DEALT TAKEN  F  FK    FLT
 +
// --------------------------------------------------------------------------------------
 +
// PlayerA              10000      500  116  101      9999 99999  10 150 980:55
 +
// PlayerB                500      200    7    5      9999 99999  5  3  0:04
 +
 
 +
char str[255];
 +
sendPublic("*arena--------------------------------------------------------------------------------");
 +
 
 +
sprintf(str, "*arena Squad: %-20s  PTS    FPTS  K  D  DMG DEALT  TAKEN  F  FK  FLT",
 +
        freqs[freq].freqname
 +
        );
 +
 
 +
sendPublic(str);
 +
 
 +
sendPublic("*arena--------------------------------------------------------------------------------");
 +
 
 +
            // assuming existing freqs struct with data
 +
            for (pilot=freqs[freq].playercount-1; pilot>=0; pilot--)
 +
            {
 +
                // on freq squad so print stats
 +
                char outString[255];
 +
 
 +
                sprintf(outString, "*arena %-20s %12d %8d %3d %3d %10d %6d %2d %3d %3d:%02d",
 +
                      freqs[freq].pilots[pilot].name,
 +
                      freqs[freq].pilots[pilot].points,
 +
                      freqs[freq].pilots[pilot].flagpoints,
 +
                      freqs[freq].pilots[pilot].kills,
 +
                      freqs[freq].pilots[pilot].deaths,
 +
                      freqs[freq].pilots[pilot].dmgdealt,
 +
                      freqs[freq].pilots[pilot].dmgtaken,
 +
                      freqs[freq].pilots[pilot].flags,
 +
                      freqs[freq].pilots[pilot].flagkills,
 +
                      freqs[freq].pilots[pilot].flagtime /60,
 +
                      freqs[freq].pilots[pilot].flagtime %60
 +
                      );
 +
               
 +
                sendPublic(outString);
 +
            }
 +
 
 +
            // Notes: sprintf format = sprintf(output char string, spacing, variables)
 +
            // Notes: s = chars, d = integer, - = left align, right align default
 +
            // Notes: doing %02d = put 0 in front if not 2 digits, %3d:%02d makes 0:04 format
 +
</pre>
 +
 
 +
==Time==
  
 
Each time MERVBot sends an EVENT_Tick to a plugin (once a second), the default handler code decrements each value in an array of countdowns. You can modify the number of countdowns and add code to occur at a specific value for one of the countdowns.
 
Each time MERVBot sends an EVENT_Tick to a plugin (once a second), the default handler code decrements each value in an array of countdowns. You can modify the number of countdowns and add code to occur at a specific value for one of the countdowns.
Line 266: Line 331:
  
 
You can then have events (such as EVENT_PlayerDeath) change the value of a countdown to make the bot do something a set time after an event occurs.
 
You can then have events (such as EVENT_PlayerDeath) change the value of a countdown to make the bot do something a set time after an event occurs.
 +
 +
=== Tracking time not using countdown[n] ===
 +
 +
This is a solution to a common problem of determining the amount of time it takes for something to occur. Using basic math, we record a start-time B, and an end-time E, both in the unit of seconds, we calculate the time elapsed by E-B.
 +
 +
Lucky for us, Windows provides a function called GetTickCount() that is a measurement of time (milliseconds) that we can use for such cases.
 +
 +
So:
 +
<pre>
 +
int begin = GetTickCount();
 +
 +
// do some code here.
 +
 +
int end = GetTickCount();
 +
 
 +
int delta = (end - begin) / 1000;  // elapsed time converted to seconds from milliseconds
 +
</pre>
 +
 +
=== Obtaining the current time ===
 +
 +
''Requirements:'' Include <time.h>.
 +
 +
Use:
 +
<pre>
 +
char u[100];
 +
time_t t=time(NULL);
 +
tm *tmp = localtime(&t);
 +
strftime(u,99,"%c",tmp);
 +
sendPublic("Current date and time: " + u);
 +
</pre>
  
 
==Writing Functions==
 
==Writing Functions==
Line 374: Line 469:
 
  handleCmdSetShip(SHIP_Warbird);
 
  handleCmdSetShip(SHIP_Warbird);
 
</pre>
 
</pre>
 
==Checking if pilot is in a safe zone==
 
<p>MervBot keeps track of whether a player is in safe or not by accessing a member of the Player class.<br />
 
* if (p->safety != 0)  // pilot is in a safe zone<br />
 
* if (p->safety == 0)  // pilot is NOT in a safe zone<br /></p>
 
<p>An example goes as follows, in spawn.cpp:
 
<pre>
 
...
 
EVENT_PlayerMove:
 
{
 
  Player *p = (Player*)event.p[0];
 
 
  if ( p->safety ) // player is in safe zone.
 
    {
 
        // do something.
 
    }
 
 
  if ( !p->safety ) // player NOT in safe zone.
 
    {
 
        // do something.
 
    }
 
}
 
...
 
</pre>
 
</p>
 
  
 
==Random numbers==
 
==Random numbers==
Line 445: Line 515:
 
</pre>
 
</pre>
  
==Tracking time not using countdown[n]==
+
==Storing data for pilots==
  
This is a solution to a common problem of determining the amount of time it takes for something to occur. Using basic math, we record a start-time B, and an end-time E, both in the unit of seconds, we calculate the time elapsed by E-B.
+
There are several ways to store data for pilots (ie tracking flagtime or kills in a period of time). Note that these methods are all purely internal to the bot, and don't effect anything beyond the plugin in any way.
  
Lucky for us, Windows provides a function called GetTickCount() that is a measurement of time (milliseconds) that we can use for such cases.
+
# Built-in get/setTag: Tracks data until player leaves the arena, then automatically deletes data.
 +
# Modified perm get/setTag: Tracks data until bot leaves arena, then automatically deletes data. (Advantage: easier to sort by player)
 +
# Custom Structs: Tracks data until plugin deletes it. (Advantage: easier to sort by freqs)
  
So:
+
''Note:'' 2 and 3 are similar in effect, mostly the difference is in how you are able to search through data you need to decide which method of storing data is best for each bot depending on what it does.
<pre>
 
int begin = GetTickCount();
 
  
// do some code here.
+
===Built-in get/setTag method===
  
int end = GetTickCount();
+
Player tags simply tag a player with a number. Define the wanted values in '''spawn.h''' at the top:
 
+
<pre>
int delta = (end - begin) / 1000;  // elapsed time converted to seconds from milliseconds
+
#define DMG_DEALT        0
 +
#define DMG_TAKEN        1
 
</pre>
 
</pre>
  
==Storing data for pilots==
+
In spawn.cpp, initialize the values on ArenaEnter and PlayerEnter:
<p>There are several ways to store data for pilots (ie tracking flagtime or kills in a period of time)</p>
 
 
 
<p>1) get/setTag - use if you only want to track data until pilot leaves arena then its erased<br />
 
built in tags track by an ID that is reset when pilot leaves/enters arena, so loses track of data once they leave arena</p>
 
 
 
<p>2) modified perm get/setTag - use if you want to track all pilots even if they leave (advantage - easier to sort by player)<br />also can track near unlimited amount of pilots</p>
 
 
 
<p>3) structs - use to track all pilots even if they leave, (advantage -
 
easier to sort by freqs), have to specify bound of players</p>
 
 
 
<p>note: 2 and 3 are similar in effect, mostly the difference is in how you are able to search through data you need to decide which method of storing data is best for each bot depending on what it does beware using modified perm get/setTag if bot is in an arena for long periods of time, data is not reset so the linkedlist could get huge</p>
 
<br />
 
<p>Initialize these following values in '''spawn.h''' at the very top:<br />
 
1.) '''Built in get/setTag method'''<br />
 
<pre>
 
#define DMG_DEALT        0
 
#define DMG_TAKEN        1
 
</pre>
 
// in spawn.cpp initialize the values on arena-enter and player-enter<br />
 
 
<pre>
 
<pre>
 
case EVENT_ArenaEnter:
 
case EVENT_ArenaEnter:
  {
+
{
  ...
+
// ...
  
    _listnode <Player> *parse = playerlist->head;
+
// do for all pilots in arena when bot enters
 +
_listnode <Player> *parse = playerlist->head;
 +
while (parse)
 +
{
 +
  Player *p = parse->item;  // get pilot
  
    while (parse) // do for all pilots in arena when bot enters
+
  set_tag(p, DMG_DEALT, 0); // initialize to 0
    {
+
  set_tag(p, DMG_TAKEN, 0);
      Player *p = parse->item;  // get pilot
+
  sendPrivate(p, "*watchdamage");  // optionally turn on player *watchdamage
  
      set_tag(p, DMG_DEALT, 0); // initialize to 0
+
  parse = parse->next;  // get next pilot
 
+
}
      set_tag(p, DMG_TAKEN, 0);
+
}
 
 
      sendPrivate(p,"*watchdamage");  // optionally turn on player *watchdamage
 
 
 
      parse = parse->next;  // get next pilot
 
    }
 
  }
 
 
</pre>
 
</pre>
 
<pre>
 
<pre>
 
case EVENT_PlayerEntering:
 
case EVENT_PlayerEntering:
  {
+
{
    set_tag(p, DMG_DEALT, 0); // initialize to 0
+
// ...
+
set_tag(p, DMG_DEALT, 0); // initialize to 0
    set_tag(p, DMG_TAKEN, 0);
+
set_tag(p, DMG_TAKEN, 0);
 
+
sendPrivate(p,"*watchdamage");
    sendPrivate(p,"*watchdamage");
+
}
  }
 
 
</pre>
 
</pre>
// then somewhere edit the tag values
+
 
 +
Then somewhere edit the tag values:
 
<pre>
 
<pre>
 
case EVENT_WatchDamage:
 
case EVENT_WatchDamage:
  {
+
{
      // sets tag for k (shooter) to be old value plus damage dealt currently
+
// sets tag for k (shooter) to be old value plus damage currently dealt
      set_tag(k, DMG_BOMB_DEALT, get_tag(k, DMG_BOMB_DEALT) + damage);
+
 
  }
+
int old_damage = get_tag(k, DMG_BOMB_DEALT);
 +
set_tag(k, DMG_BOMB_DEALT, old_damage + damage);
 +
}
 
</pre>
 
</pre>
The following demonstrates how to retrieve the tag values as a command in spawn.h.
+
 
 +
The following demonstrates how to retrieve the tag values as a command in command.cpp:
 
<pre>
 
<pre>
if (c->check("showstats")) {
+
if (c->check("showstats"))
            int temp = get_tag(p, DMG_TOTAL_DEALT);
+
{
 +
int temp = get_tag(p, DMG_TOTAL_DEALT);
  
            String s = "You've done ";
+
String s = "You've done ";
            s += temp;
+
s += temp;
            s += " damage so far!";
+
s += " damage so far!";
  
            sendPrivate(p,s);
+
sendPrivate(p,s);
 
  }
 
  }
 
</pre>
 
</pre>
Destroy the tags when the player leaves arena.
+
 
<pre>
+
===Modified permanent get/setTag method===
case EVENT_PlayerLeaving:
+
 
  {
+
This method is the same as get/setTag with some modifications to the tag code to retain them after the player leaves. Beware of using this method if bot is in an arena for long periods of time, linkedlist could get huge.
    killTags(p);
+
 
  }
 
</pre>
 
</p>
 
<p>2.) '''Modified permanent get/setTag method'''<br />
 
// same as get/setTag with some modifications to the tag code, then can use tags exactly as above
 
  
 
// spawn.h, add char name[20]; into struct PlayerTag
 
// spawn.h, add char name[20]; into struct PlayerTag
Line 548: Line 597:
 
struct PlayerTag
 
struct PlayerTag
 
{
 
{
    Player *p;
+
Player *p;
    char name[20];
+
char name[20];
    int index;
+
int index;
    int data;
+
int data;
 
};
 
};
 
</pre>
 
</pre>
 +
 
In spawn.cpp:
 
In spawn.cpp:
 
<pre>
 
<pre>
    case EVENT_PlayerLeaving:
+
case EVENT_PlayerLeaving:
    {
+
{
        Player *p = (Player*)event.p[0];
+
    Player *p = (Player*)event.p[0];
  
        // killTags(p);  // remove so tag not deleted on arena exit
+
    // killTags(p);  // remove so tag not deleted on arena exit
  
    ...
+
// ...
 
</pre>
 
</pre>
 +
 
Locate in spawn.cpp and modify accordingly:
 
Locate in spawn.cpp and modify accordingly:
 
<pre>
 
<pre>
    int botInfo::get_tag(Player *p, int index)
+
int botInfo::get_tag(Player *p, int index)
 +
{
 +
    _listnode <PlayerTag> *parse = taglist.head;
 +
    PlayerTag *tag;
 +
 
 +
    while (parse)
 
     {
 
     {
        _listnode <PlayerTag> *parse = taglist.head;
+
      tag = parse->item;
        PlayerTag *tag;
 
 
 
        while (parse)
 
        {
 
          tag = parse->item;
 
  
          // if (tag->p == p)
+
      // if (tag->p == p)
          if (strcmp(tag->name,p->name)==0)  // now tracking by player name, not ID
+
      if (strcmp(tag->name,p->name)==0)  // now tracking by player name, not pointer
          if (tag->index == index)
+
      if (tag->index == index)
            return tag->data;
+
        return tag->data;
  
          parse = parse->next;
+
      parse = parse->next;
        }
 
        return 0;
 
 
     }
 
     }
</pre>
+
     return 0;
<pre>
+
}
    void botInfo::set_tag(Player *p, int index, int data)
 
     {
 
        _listnode <PlayerTag> *parse = taglist.head;
 
        PlayerTag *tag;
 
  
        while (parse)
+
void botInfo::set_tag(Player *p, int index, int data)
        {
+
{
          tag = parse->item;
+
    _listnode <PlayerTag> *parse = taglist.head;
 +
    PlayerTag *tag;
  
          //if (tag->p == p)
+
    while (parse)
          if (strcmp(tag->name,p->name)==0) // now tracking by player name, not ID
+
    {
          if (tag->index == index)
+
      tag = parse->item;
          {
 
            tag->data = data;
 
            return;
 
          }
 
          parse = parse->next;
 
        }
 
  
        tag = new PlayerTag;
+
      //if (tag->p == p)
        // tag->p = p; // not tracking by ID anymore
+
      if (strcmp(tag->name,p->name)==0) // now tracking by player name, not pointer
        strncpy(tag->name, p->name, 20); // tracking by player name
+
      if (tag->index == index)
        tag->index = index;
+
      {
 
         tag->data = data;
 
         tag->data = data;
         taglist.append(tag);
+
         return;
 +
      }
 +
      parse = parse->next;
 
     }
 
     }
 +
 +
    tag = new PlayerTag;
 +
    // tag->p = p; // not tracking by pointer anymore
 +
    strncpy(tag->name, p->name, 20); // tracking by player name
 +
    tag->index = index;
 +
    tag->data = data;
 +
    taglist.append(tag);
 +
}
 
</pre>
 
</pre>
</p>
+
 
<p>3.) '''Using structs, implement in spawn.h:'''<br />
+
===Using structs===
 +
 
 +
In '''spawn.h''':
 
<pre>
 
<pre>
 
class botInfo
 
class botInfo
 
{
 
{
struct freqdata  
+
struct freqdata  
  {
+
{
  int kills;
+
int kills, deaths;
  int deaths;
+
};
  };
+
// ...
...
 
 
};
 
};
 
</pre>
 
</pre>
 +
 
To make use of this structure, implement accordingly:
 
To make use of this structure, implement accordingly:
<pre>freqdata freqs[100]; // 100 of those structs</pre>
 
Access the data in spawn.cpp using
 
<pre>freqs[56].kills = 1;</pre>
 
</p>
 
 
==Output of data/messages==
 
<p>An example of using normal strings to output data/messages.</p>
 
 
<pre>
 
<pre>
// does *arena X pilots left in game
+
freqdata freqs[100]; // 100 of those structs
// NOTE: variable temp needs to be defined with some value
 
 
 
String s = "*arena ";
 
      s += temp;
 
      s += " pilots left in the game.";
 
 
 
sendPublic(s);
 
 
</pre>
 
</pre>
Or,
+
Access the data in spawn.cpp using
 
<pre>
 
<pre>
//NOTE: this can be considered inefficient.
+
freqs[56].kills = 1;
 
 
sendPublic("*arena " + (String)temp + " pilots left in the game");
 
 
</pre>
 
</pre>
  
<p>An example using sprintf to align/space data, where output data will be in this approximate format.</p>
+
See CPlusPlus.com's [http://www.cplusplus.com/doc/tutorial/tut3-5.html Structures] tutorial for a more comprehensive guide. Note that, as shown on the bottom of the page, you can have structures within structures. Thus, for example, you could have a structure for each freq with a structure for each player nested within them.
<pre>
 
// output data will be in this approximate format (not lined up perfectly because of html)
 
// --------------------------------------------------------------------------------------
 
// Squad: squadname      PTS    FPTS    K    D  DMG DEALT TAKEN  F  FK    FLT
 
// --------------------------------------------------------------------------------------
 
// PlayerA              10000      500  116  101      9999 99999  10 150 980:55
 
// PlayerB                500      200    7    5       9999 99999  5  3  0:04
 
 
 
char str[255];
 
sendPublic("*arena--------------------------------------------------------------------------------");
 
  
sprintf(str, "*arena Squad: %-20s  PTS    FPTS  K  D  DMG DEALT  TAKEN  F  FK  FLT",
+
==Input/Output to files==
        freqs[freq].freqname
 
        );
 
  
sendPublic(str);
+
For reading and/or writing to files with C++ you must have the required include statement as follows:
 
+
<pre>
sendPublic("*arena--------------------------------------------------------------------------------");
+
#include <fstream>
 
+
using namespace std;
            // assuming existing freqs struct with data
 
            for (pilot=freqs[freq].playercount-1; pilot>=0; pilot--)
 
            {
 
                // on freq squad so print stats
 
                char outString[255];
 
 
 
                sprintf(outString, "*arena %-20s %12d %8d %3d %3d %10d %6d %2d %3d %3d:%02d",
 
                      freqs[freq].pilots[pilot].name,
 
                      freqs[freq].pilots[pilot].points,
 
                      freqs[freq].pilots[pilot].flagpoints,
 
                      freqs[freq].pilots[pilot].kills,
 
                      freqs[freq].pilots[pilot].deaths,
 
                      freqs[freq].pilots[pilot].dmgdealt,
 
                      freqs[freq].pilots[pilot].dmgtaken,
 
                      freqs[freq].pilots[pilot].flags,
 
                      freqs[freq].pilots[pilot].flagkills,
 
                      freqs[freq].pilots[pilot].flagtime /60,
 
                      freqs[freq].pilots[pilot].flagtime %60
 
                      );
 
               
 
                sendPublic(outString);
 
            }
 
 
 
            // Notes: sprintf format = sprintf(output char string, spacing, variables)
 
            // Notes: s = chars, d = integer, - = left align, right align default
 
            // Notes: doing %02d = put 0 in front if not 2 digits, %3d:%02d makes 0:04 format
 
 
</pre>
 
</pre>
  
==Input/Output to files==
+
===File stream input===
For reading and/or writing to files with C++ you must have the required include
 
 
 
statement as follows:
 
#include <fstream>
 
using namespace std; // not a good practice, but works.
 
 
 
'''Note''': neither example shows how to properly handle possible
 
 
 
error-exceptions.
 
 
 
===Input to file===
 
 
The following example will show you how to read a file, duel.ini, line by line.
 
The following example will show you how to read a file, duel.ini, line by line.
  
#include "stdlib.h" // for atoi()
+
<pre>#include "stdlib.h" // for atoi()</pre>
  
 
<pre>
 
<pre>
 
   ifstream file("duel.ini");
 
   ifstream file("duel.ini");
  char line[256];
 
  
   // read in MaxBoxes=X
+
   if (!file.good()) // if there was an error opening the file
  while (file.getline(line, 256))
+
    sendPublic("*arena Error opening file for reading"); // or add your own error handler
  {
+
  else
 +
  {
 +
    char line[256];
 +
 
 +
    // read in MaxBoxes=X
 +
    while (file.getline(line, 256))
 +
    {
 
      
 
      
    if (CMPSTART("MaxBoxes=", line)) //Does the line begin with MaxBoxes= ?
+
      if (CMPSTART("MaxBoxes=", line)) //Does the line begin with MaxBoxes= ?
    {
+
      {
      MAX_BOXES = atoi(&(line[9]));  //If so, read the value into an integer, using atio.
+
        MAX_BOXES = atoi(&(line[9]));  //If so, read the value into an integer, using atoi.
      break;
+
        break;
    }
+
      }
  }
+
    }
  
  file.close();
+
    file.close();
 +
  }
 
</pre>
 
</pre>
  
===Output to file===
+
===File stream output===
The following code example will demonstrate how to append to a file,  
+
The following code example will demonstrate how to append to a file, duelleaguestat.inc.
 
+
<pre>
duelleaguestat.inc.
 
 
 
 
  ofstream file("duelleaguestat.inc", ios::app);  // app = put all data at end of file
 
  ofstream file("duelleaguestat.inc", ios::app);  // app = put all data at end of file
file << squad1<< endl;  // squad1 = char[20]
 
file << " vs "<< endl;
 
file << squad2<< endl;  // squad2 = char[20]
 
file.close();
 
  
Similarly, you are able to write an output of a String to a file, which is
+
if (!file.good()) // if there was an error opening the file
 
+
  sendPublic("*arena Error opening file.");
similar to the previous example but rather do this:
+
else
 +
{
 +
  file << squad1<< endl;  // squad1 = char[20]
 +
  file << " vs "<< endl;
 +
  file << squad2<< endl;  // squad2 = char[20]
 +
  file.close();
 +
}
 +
</pre>
  
 +
Similarly, you are able to write an output of a String to a file:
 +
<pre>
 
  // key is converting String to (char*) to file write
 
  // key is converting String to (char*) to file write
 
  String str = freqs[freq].slotname[slot];
 
  String str = freqs[freq].slotname[slot];
Line 753: Line 749:
 
  file << endl;
 
  file << endl;
 
  file << (char*) str;
 
  file << (char*) str;
 +
</pre>
  
===Practical I/O Example===
+
===Input with GetPrivateProfileString===
This next example will show how to read input from a file using
 
  
"GetPrivateProfileString"; based on the rampage plugin.
+
GetPrivateProfileString(), a function provided by Windows for reading INI files, will automatically find an INI key (like "MaxBoxes=") in a file for you. See the [http://msdn.microsoft.com/library MSDN Library] for help on this function. This next example will show how to read input using GetPrivateProfileString() based on the rampage plugin.
  
 
The file format for rampage.ini is like this:
 
The file format for rampage.ini is like this:
 +
<pre>
 
  7=is on a killing spree! (6:0)
 
  7=is on a killing spree! (6:0)
 
  10=is opening a can of booya! (9:0)
 
  10=is opening a can of booya! (9:0)
 +
</pre>
  
Now to read input data,we begin by creating a '''rampageini.h''':
+
In '''rampageini.cpp''':
 
<pre>
 
<pre>
#pragma once
+
#include "rampageini.h"
 +
#define WIN32_LEAN_AND_MEAN
 +
#include <windows.h>
  
#ifndef RAMPAGEINI_H
+
#define NUM_RANKS 10
 +
#define BUFFER_LEN 256
  
#define RAMPAGEINI_H
+
struct RampageSettings
#define NUM_RANKS 10
+
{
#define BUFFER_LEN 256
+
char quotes[NUM_RANKS][BUFFER_LEN];
 
+
};
struct RampageSettings
 
  {
 
  char quotes[NUM_RANKS][BUFFER_LEN];
 
  };
 
 
   
 
   
void LoadSettings(RampageSettings &setts);
+
void LoadSettings(RampageSettings &setts);
  
#endif
 
</pre>
 
In '''rampageini.cpp''':
 
<pre>
 
#include "rampageini.h"
 
static char buffer[BUFFER_LEN];
 
 
static char path[BUFFER_LEN];
 
static char path[BUFFER_LEN];
#include "../algorithms.h"
 
#define WIN32_LEAN_AND_MEAN
 
#include <windows.h>
 
  
char *rank_type[10] = {
+
char *rank_type[NUM_RANKS] = { "7", "10" };
        "7",
 
        "10",
 
};
 
  
void LoadSettings(RampageSettings &setts)
+
void LoadSettings(RampageSettings &setts)
  {
+
{
    GetCurrentDirectory(BUFFER_LEN - 64, path);
+
GetCurrentDirectory(BUFFER_LEN - 64, path);
    strcat(path, "\rampage.ini");
+
strcat(path, "\rampage.ini");
  
    for (int i = 0; i < NUM_RANKS; ++i)
+
for (int i = 0; i < NUM_RANKS; ++i)
      {
+
{
        GetPrivateProfileString("Comments", rank_type[i], "-ERROR-",
+
GetPrivateProfileString("Comments", rank_type[i], "-ERROR-",
                                setts.quotes[i], BUFFER_LEN, path);
+
setts.quotes[i], BUFFER_LEN, path);
      }
+
}
  }
+
}
 
</pre>
 
</pre>
  
==Useful Player data==
+
==Player data==
<p>As stated earlier in the tutorial, MervBot stores useful player data internally as Player objects, see player.h for implementation details.</p>
+
 
<p>
+
As stated earlier in the tutorial, MervBot stores useful player data internally as Player objects, see player.h for implementation details.
* p->name = player name stored as char[20]
+
 
** '''Note:''' SubSpace protocol allows for usernames to be 19+ in length, do not rely on this for player-name comparisions.
+
* p->name = player name stored as char[20] (''Note:'' SubSpace protocol allows for usernames to be 19+ in length, do not rely on this for player-name comparisions.)
 
* p->squad = player squad stored as char[20]
 
* p->squad = player squad stored as char[20]
 
* p->ship = ship (0-9) enumerated as SHIP_Warbird, SHIP_Spectator, etc..
 
* p->ship = ship (0-9) enumerated as SHIP_Warbird, SHIP_Spectator, etc..
* p->safety = if ship is in safety zone (boolean)
+
* p->safety = whether ship is in safety zone (boolean)
 
* p->bounty = player bounty
 
* p->bounty = player bounty
 
* p->energy = player energy (have bot with *energy on to get accurate readings)
 
* p->energy = player energy (have bot with *energy on to get accurate readings)
Line 827: Line 812:
 
* p->score.wins = player kills from f2
 
* p->score.wins = player kills from f2
 
* p->score.losses = player deaths from f2
 
* p->score.losses = player deaths from f2
</p>
 
  
==Bot built in functions==
+
Just access the respective member of the Player class to check the player's property.
<blockquote>// useful MervBot commands to control what the bot is doing<br />
 
                                                                                                                            <br />
 
// player.cpp<br />
 
Player::move(Sint32 x, Sint32 y) &nbsp;// example &nbsp;me-&gt;move(512,512) - bot moves to coord 512 512<br />
 
Player::clone(Player *p) // example &nbsp;me-&gt;clone(p) <br />
 
                                                                                                                            <br />
 
// dllcore.cpp (descriptions of functions in dllcore.h)<br />
 
                                                                                                                            <br />
 
BotEvent makeEcho&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (char *m);<br />
 
BotEvent makeSay&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (int t, int s, int i, char *m);<br />
 
                                                                                                                            <br />
 
BotEvent makeShip&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (int s);<br />
 
BotEvent makeTeam&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (int t);<br />
 
BotEvent makeGrabFlag&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (int f);<br />
 
BotEvent makeSendPosition&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (bool reliable);<br />
 
BotEvent makeDropFlags&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ();<br />
 
                                                                                                                            <br />
 
BotEvent makeDeath&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (Player *p);<br />
 
BotEvent makeAttach&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (Player *p);<br />
 
BotEvent makeDetach&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ();<br />
 
BotEvent makeFollowing&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (bool f);<br />
 
BotEvent makeFlying&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (bool f);<br />
 
BotEvent makeBanner&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (BYTE *b);<br />
 
BotEvent makeDropBrick&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ();<br />
 
BotEvent makeFireWeapon&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (void *weapon_info);<br />
 
                                                                                                                            <br />
 
BotEvent makeToggleObjects&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (Uint16 player, Uint16 *objects, int num_objects);<br />
 
                                                                                                                            <br />
 
BotEvent makeSpawnBot&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
(char *name, char *password, char *staff, char *arena);<br />
 
BotEvent makeChangeArena&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (char *name);<br />
 
BotEvent makeChangeSettings&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (_linkedlist &lt;String&gt; *settings);<br />
 
                                                                                                                            <br />
 
// example how to use them:<br />
 
                                                                                                                            <br />
 
tell(makeFollowing(false));<br />
 
                                                      <br />
 
// look in Commands.txt , command.cpp (core), or /!help to bot to see all bot external commands (example /!go &lt;arena&gt;)<br />
 
                                                      </blockquote>
 
  
==Example Code==
+
For example, in spawn.cpp, to check whether a player is in a safety zone:
<!-- Example Code Sections have specific    -->
 
<!-- internal commenting system That        -->
 
<!-- go like this:                          -->
 
<!-- .equals.x4 <TITLE> .equals.x4          -->
 
<!-- <!-- EXAMPLE L: BEGIN -->              -->
 
<!-- CODE / ETC HERE.                        -->
 
<!-- <br /><!-- EXAMPLE L: END  -->        -->
 
==== No antiwarp in center of the map ====
 
<!-- EXAMPLE A: BEGIN -->
 
<p>'''Example A''': No antiwarp in center of map. Warn the player, and revoke the prize.</p>
 
<p>In order for this code to work correctly, the bot must have smod+ privilages.</p>
 
<p>Lets first implement two functions which we will need to accomplish this task:
 
 
<pre>
 
<pre>
bool closeto(Player *p, int x, int y, int tolerance) {
+
EVENT_PlayerMove:
    return (abs((p->tile.x) - x) < tolerance) && (abs((p->tile.y) - y) < tolerance); }
 
 
 
inline int abs(int n) {
 
    if (n < 0)    return -n;
 
    else        return n; }
 
</pre>
 
 
 
We should define the radius of antiwarp checking, this can be done several ways, for sake of simplicity, here is a quick-plop-in for '''spawn.h''':
 
<pre>
 
class botInfo
 
 
{
 
{
bool CONNECTION_DENIED;
 
        ...
 
        // Put bot data here
 
        int radius;
 
 
public:
 
botInfo(CALL_HANDLE given)
 
{
 
        ...
 
        // Put initial values here
 
        radius = 35;
 
        ...
 
</pre>
 
Locate and add into '''spawn.cpp''' accordingly:
 
<pre>
 
case EVENT_PlayerMove:
 
{
 
 
    Player *p = (Player*)event.p[0];
 
 
    // no anti in center
 
    if ((p->ship != SHIP_Spectator) && (p->awarp)) {
 
        if (closeto(p, 512, 512, radius)){
 
            sendPrivate(p, "*prize #-20");
 
            sendPrivate(p, "*warn Antiwarp is not allowed in center.");
 
        }
 
    }
 
  ...
 
</pre>
 
Just as a word of caution, players may at times be flooded with *prize #-20, and *warn statements under certain conditions.
 
</p>
 
<br /><!-- EXAMPLE A: END  -->
 
==== Setting freq size depending on how many pilots in game ====
 
<!-- EXAMPLE B: BEGIN -->
 
<p>'''Example B''': Setting freq size depending on how many pilots in game.</p>
 
<p>In order for this code to work correctly, the bot must have Sysop or Arena-Owner privilages.</p>
 
<p>In '''spawn.cpp''' (Note: this source code has assumptions, please review comments before implementing.):
 
<pre>
 
case EVENT_Tick:
 
  {
 
  ...
 
 
  // NOTE: assuming countdown[0] initialized to > 0  in spawn.h, freqchange=0;
 
  if (countdown[0] == 0)
 
  {
 
        _listnode <Player> *parse = playerlist->head;
 
        int count = 0;
 
 
        while (parse)
 
        {
 
            Player *p = parse->item;
 
 
            if (p->ship != SHIP_Spectator)
 
            ++count;
 
 
            parse = parse->next;
 
        }
 
 
        if ((count > 24) && (freqchange != 4))
 
        {
 
            sendPublic("?set team:maxperteam:4"); //Sysop command to modify arena config.
 
            String s;
 
            s = "Max freq size 4  (";
 
            s += count;
 
            s += " pilots in game)";
 
            sendPublic(s);
 
            freqchange = 4;
 
        }
 
 
        if ((count < 25) && (count > 14) && (freqchange != 3))
 
        {
 
            sendPublic("?set team:maxperteam:3"); //Sysop command to modify arena config.
 
            String s;
 
            s = "Max freq size 3  (";
 
            s += count;
 
            s += " pilots in game)";
 
            sendPublic(s);
 
            freqchange = 3;
 
        }
 
 
    countdown[0] = 120; // reset timer to 120 seconds.
 
    }
 
</pre></p><br /><!-- EXAMPLE B: END  -->
 
==== Tracking kills and announcing when pilot gets 10 kills in a row without dying ====
 
<!-- EXAMPLE C: BEGIN -->
 
<p>'''Example C''': Tracking kills and announcing when pilot gets 10 kills in a row without
 
dying.</p>
 
<p>In order for this code to work correctly, the bot must have smod+ privilages.</p>
 
<p>Locate EVENT_PlayerDeath in '''spawn.cpp''' (see note.):
 
<pre>
 
EVENT_PlayerDeath:
 
{
 
    Player *p = (Player*)event.p[0],
 
          *k = (Player*)event.p[1];
 
    Uint16 bounty = (Uint16)(Uint32)event.p[2];
 
    Uint16 flags = (Uint16)event.p[3];
 
 
    // NOTE: assuming tags are setup (see storing data section).
 
    set_tag(p, KILLS, 0);  // pilot died, reset to 0 kills in a row
 
    set_tag(k, KILLS, get_tag(k, KILLS) + 1);  // pilot killed someone, increment kills in a row by 1
 
 
    if (get_tag(k, KILLS) == 10)
 
        sendPublic("*arena (String) k->name + " has gotten 10 kills.");
 
...
 
  }
 
</pre></p>
 
<br /><!-- EXAMPLE C: END  -->
 
==== Warp pilot to coord when they are in a certain region ====
 
<!-- EXAMPLE D: BEGIN -->
 
<p>'''Example D''': Warp pilot to coord when they are in a certain region.</p>
 
<p>In order for this code to work correctly, the bot must have smod+ privilages.</p>
 
<p>Lets first implement two functions which we will need to accomplish this task:
 
<pre>
 
bool closeto(Player *p, int x, int y, int tolerance) {
 
    return (abs((p->tile.x) - x) < tolerance) && (abs((p->tile.y) - y) < tolerance); }
 
 
inline int abs(int n) {
 
    if (n < 0)    return -n;
 
    else        return n; }
 
</pre>
 
</p>
 
<p>In '''spawn.cpp''', EVENT_PlayerMove:
 
<pre>
 
case EVENT_PlayerMove:
 
  {
 
 
   Player *p = (Player*)event.p[0];
 
   Player *p = (Player*)event.p[0];
  
   if (closeto(p, 509, 509, 2)) // if pilot within 2 of map coord 509,509
+
   if ( p->safety ) // player is in safe zone.
      {
+
    {
        sendPrivate(p, "*warpto 509 504");  // warp to coord 509,504
+
        // do something.
      }
+
    }
  ...
 
</pre></p>
 
<br /><!-- EXAMPLE D: END  -->
 
==== Structures within structures ====
 
<!-- EXAMPLE E: START  -->
 
<p>'''Example E''': Structures within structures (spawn.h botinfo).</p>
 
<p>Implement the following in the spawn.h:
 
<pre>
 
struct playerstats
 
{
 
  char name[20];
 
  
   int kills;
+
   if ( !p->safety ) // player NOT in safe zone.
  int deaths;
+
    {
  Uint16 points;
+
         // do something.
  Uint16 flagpoints;
+
    }
  int flagtime;
 
  int cflagtime;
 
  int flags;
 
  int flagkills;
 
 
 
  int dmgdealt;
 
  int dmgtaken;
 
};
 
 
 
struct freqdata
 
{
 
  playerstats pilots[100];
 
 
 
  int freqpoints;
 
  char freqname[20];
 
  int freqflagpoints;
 
  Uint16 freqteam;
 
  int freqflagtime;
 
 
 
  int flags;
 
  int kills;
 
  int deaths;
 
  int flagkills;
 
 
 
  int dmgdealt;
 
  int dmgtaken;
 
 
 
  int playercount;
 
};
 
 
 
//... (large jump down in spawn.h)
 
 
 
// Put bot data here <- locate and add after:
 
freqdata freqs[100];
 
 
 
//... <- perhaps some other variables.. scroll down past the next }
 
}
 
 
void Clear(); // A user-defined function. Add this.
 
 
 
void clear_objects(); //already exists, provided by Catid.
 
void object_target(Player *p); //already exists, provided by Catid.
 
 
 
// ...  spawn.h continues.
 
</pre>
 
</p>
 
<p>Implement the following in '''spawn.cpp''':
 
<pre>
 
void botInfo::Clear()
 
{
 
  // initialize/clear struct data
 
  for (int n=99; n>=0; n--)
 
  {
 
    freqs[n].freqteam=-1;
 
    freqs[n].freqpoints=0;
 
    freqs[n].freqflagpoints=0;
 
    freqs[n].playercount=0;
 
    freqs[n].flags=0;
 
    freqs[n].kills=0;
 
    freqs[n].deaths=0;
 
    freqs[n].freqflagtime=0;
 
    freqs[n].flagkills=0;
 
    freqs[n].dmgdealt=0;
 
    freqs[n].dmgtaken=0;
 
 
 
    for (int m = 99; m>=0; m--)
 
    {
 
      freqs[n].pilots[m].deaths=0;
 
      freqs[n].pilots[m].kills=0;
 
      freqs[n].pilots[m].points=0;
 
      freqs[n].pilots[m].flagpoints=0;
 
      freqs[n].pilots[m].flagtime=0;
 
      freqs[n].pilots[m].cflagtime=0;
 
      freqs[n].pilots[m].flags=0;
 
      freqs[n].pilots[m].flagkills=0;
 
      freqs[n].pilots[m].dmgdealt=0;
 
      freqs[n].pilots[m].dmgtaken=0;
 
    }
 
}
 
}
 
</pre></p>
 
<p>                                                               
 
To access private data:
 
<pre>freqs[1].pilots[2].kills++;</pre>
 
-OR-
 
<pre>int freq = p->team;
 
freqs[freq].deaths++;</pre></p>
 
<br /><!-- EXAMPLE E: END  -->
 
==== Tracking flag data ====
 
<!-- EXAMPLE F: START  -->
 
<p>'''Example F''': Tracking flag data.</p>
 
f) Tracking flag data<br />                                         
 
                                                                       
 
<blockquote>Example GetPilot() function &nbsp;(using structs from example e)<br /><blockquote>bool botInfo::GetPilot(Player *p)<br />
 
{<br />
 
&nbsp;&nbsp;&nbsp; // get a pilots freq/pilot id from struct<br />
 
&nbsp;&nbsp;&nbsp; for (freq=freqcount-1; freq&gt;=0; freq--)<br />
 
if (p-&gt;team == freqs[freq].freqteam)<br />
 
&nbsp;&nbsp;&nbsp; for (pilot = freqs[freq].playercount-1; pilot&gt;=0; pilot--)<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
if (strcmp(p-&gt;name,freqs[freq].pilots[pilot].name)==0)<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return true;<br /><br />
 
&nbsp;&nbsp;&nbsp; return false;<br />
 
}<br />
 
</blockquote></blockquote>
 
&nbsp;
 
<blockquote>Example way to track flag data using above struct/functions<br /><blockquote>
 
case EVENT_FlagGrab:<br />
 
{<br />
 
&nbsp;&nbsp;&nbsp; if (GetPilot(p)) &nbsp;// function<br />
 
&nbsp;&nbsp;&nbsp; {<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; freqs[freq].pilots[pilot].flags++;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; freqs[freq].flags++;<br /><br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
if (freqs[freq].pilots[pilot].flags &lt; 2) // didnt have a flag before,
 
first flag<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
&nbsp;&nbsp;&nbsp; freqs[freq].pilots[pilot].cflagtime = GetTickCount();
 
&nbsp;// time stamp when picked up flag<br />
 
&nbsp;&nbsp;&nbsp; }<br /></blockquote></blockquote>
 
&nbsp;<blockquote>Example way to track flag data using built in get/set tag (from catid flagbot)<br />
 
                                                                       
 
                                                                       
 
                                              <blockquote>
 
case EVENT_FlagGrab:<br />
 
 
 
 
 
{<br />
 
 
 
 
 
&nbsp;&nbsp;&nbsp; set_tag(p, TAG_STAT_FS, get_tag(p, TAG_STAT_FS) + 1);<br />
 
 
 
 
 
&nbsp;&nbsp;&nbsp; set_tag(p, TAG_FLAGTIMER, GetTickCount());<br />
 
 
 
 
 
}<br />                           
 
                                                                       
 
                                                                       
 
                    </blockquote>&nbsp;</blockquote>&nbsp;<blockquote>Get current flag times using struct format<br />
 
                                                                       
 
                                                                       
 
                                                  <blockquote>void botInfo::SetFlagTimes()<br />
 
{<br />
 
&nbsp;&nbsp;&nbsp; // set current flagtime for pilots/freqs<br />
 
&nbsp;&nbsp;&nbsp; _listnode &lt;Player&gt; *parse = playerlist-&gt;head;<br />
 
&nbsp;&nbsp;&nbsp; <br />
 
&nbsp;&nbsp;&nbsp; while (parse)<br />
 
&nbsp;&nbsp;&nbsp; {<br />
 
Player *p = parse-&gt;item;<br /> 
 
                                                                       
 
                                                                       
 
                                                  <br />
 
if (GetPilot(p))<br />
 
&nbsp;&nbsp;&nbsp; if (freqs[freq].pilots[pilot].flags &gt; 0)<br />
 
&nbsp;&nbsp;&nbsp; {<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (PilotOnSquad(p))<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
&nbsp;&nbsp;&nbsp; freqs[freq].freqflagtime += (GetTickCount() - freqs[freq].pilots[pilot].cflagtime)/1000;<br />
 
                                                                       
 
                                                                       
 
                                                    <br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
freqs[freq].pilots[pilot].flagtime += (GetTickCount() - freqs[freq].pilots[pilot].cflagtime)/1000;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
freqs[freq].pilots[pilot].cflagtime = GetTickCount();<br />
 
&nbsp;&nbsp;&nbsp; }<br />       
 
                                                                       
 
                                                                       
 
                                          <br />
 
parse = parse-&gt;next;<br />
 
&nbsp;&nbsp;&nbsp; }<br />
 
}<br />                                                                 
 
                                                                       
 
                                                          </blockquote>
 
// side note: &nbsp;case EVENT_FlagDrop: {} gets called anytime theres a teamkill<br />
 
                                                                       
 
                                                                       
 
                                                  </blockquote>                                                               
 
<br /><!-- EXAMPLE F: END  -->
 
==== Example way to do simple /!spam feature (allowed 1x/60s) ====
 
<!-- EXAMPLE G: START  -->
 
<p>'''Example G''': Example way to do simple /!spam feature (allowed 1x/60s).</p>
 
g) Example way to do simple /!spam feature (allowed 1x/60s)<br />   
 
                                                                       
 
                                                                       
 
                                            <blockquote>declare and initialize variables in spawn.h<br />
 
                                                                       
 
                                                                       
 
                                                    <blockquote>class botInfo<br />
 
{<br />
 
bool spamready;<br />
 
int SPAM_TIME;<br />                                                   
 
                                                                       
 
                                                                       
 
<br />
 
public:<br />
 
&nbsp;&nbsp;&nbsp; botInfo(CALL_HANDLE given)<br />
 
&nbsp;&nbsp;&nbsp; {<br />
 
&nbsp;&nbsp;&nbsp; spamready = true;<br />
 
&nbsp;&nbsp;&nbsp; SPAM_TIME = 60;<br />                               
 
                                                                       
 
                                                                       
 
                    </blockquote>
 
spawn.cpp - mark as spamready=true when 60 seconds up<br />         
 
                                                                       
 
                                                                       
 
                                          <blockquote>case EVENT_Tick:<br />
 
{<br />
 
&nbsp;&nbsp;&nbsp; if (countdown[0] == 1) &nbsp;{<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; spamready = true; &nbsp;// ready to spam again<br />
 
&nbsp;&nbsp;&nbsp; }<br />       
 
                                                                       
 
                                                                       
 
                                              </blockquote>           
 
                                                                       
 
                                                                       
 
                                            <br />
 
command.cpp - handle !spam command<br />                             
 
                                                                       
 
                                                                       
 
                          <blockquote>case OP_Player:<br />
 
{&nbsp;&nbsp;&nbsp; // Player-level commands<br />                     
 
                                                                       
 
                                                                       
 
                                  <br />
 
else if (c-&gt;check(&quot;spam&quot;))<br />
 
&nbsp;&nbsp;&nbsp; {<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
// zone announcement &quot;Need pilots to duel in ?go arena -pilotname&quot;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (spamready == true)<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; String s;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
&nbsp;&nbsp;&nbsp; s += &quot;*zone Need pilots to duel in ?go &quot;;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; s += arena;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; s += &quot; - &quot;;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; s += p-&gt;name;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sendPublic(s);<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; spamready=false;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
&nbsp;&nbsp;&nbsp; countdown[0] = SPAM_TIME * 60; // next spam time limit<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else if (countdown[0] &lt; 0)<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
&nbsp;&nbsp;&nbsp; sendPrivate(p,&quot;Spam ability disabled.&quot;);<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else <br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; String s;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; s += SPAM_TIME;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
&nbsp;&nbsp;&nbsp; s += &quot; Minute timer between announcements. &quot;;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; s += countdown[0] / 60;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; s += &quot;:&quot;;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (countdown[0] % 60 &lt; 10)<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; s += &quot;0&quot;;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; s += countdown[0] % 60;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
&nbsp;&nbsp;&nbsp; s += &quot; minutes left before next spam allowed.&quot;;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sendPrivate(p, s);<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br />
 
&nbsp;&nbsp;&nbsp; }<br />          
 
                                                                       
 
                                                                       
 
                                                </blockquote>&nbsp;</blockquote>         
 
<br /><!-- EXAMPLE G: END    -->
 
==== Implementing a simple stack to do "next in line for several 'boxes' at once" ====
 
<!-- EXAMPLE H: START  -->
 
<p>'''Example H''': Example of implementing a simple stack to do "next in line for several 'boxes' at once".</p>
 
<p>In '''spawn.h''' declare the following variables and structs:
 
<pre>
 
class botInfo
 
{
 
  //...
 
 
 
  // Put bot data here  <- locate and add after
 
  Player *next[99][99];
 
  int MAX_NEXT;
 
  int nextcount[99];
 
 
 
public:
 
    botInfo(CALL_HANDLE given)
 
    {
 
    //...
 
 
 
    // Put initial values here <- locate and add after
 
    MAX_NEXT = 8;
 
 
 
//... spawn.h continues on.
 
</pre>
 
In '''spawn.cpp''' implement (remember to add the function prototype to spawn.h as well.):
 
<pre>
 
void botInfo::MoveUp(int pos, int box)
 
{
 
    // moves up the next line for that box and decrement box's nextcount
 
    if (nextcount[box] > 0)
 
        nextcount[box]--;
 
 
 
    for (pos = pos; pos < MAX_NEXT - 1; pos++)
 
    {
 
        next[box][pos] = next[box][pos + 1];
 
    }
 
 
 
    next[box][MAX_NEXT] = 0;
 
 
}
 
}
 
</pre>
 
</pre>
</p>
 
<br /><!-- EXAMPLE H: END    -->
 
  
==== Example of reading any text from a .txt and printing it to pilot line by line ====
+
=== Obtaining a player pointer using name comparison ===
<!-- EXAMPLE I: START  -->
 
<p>'''Example I''': Example of reading any text from a .txt and printing it to pilot line by line.</p>
 
<p>Required include:
 
<pre>#include <fstream>
 
using namespace std;  // bad coding practice, but for ease of use, we'll use it.
 
</pre>
 
Example of use, in command.cpp:
 
<pre>
 
case OP_Player:
 
{
 
    //...
 
 
 
    if (c->check("staff"))
 
    {
 
        // read in data line by line from the file staff.txt (max length 255)
 
        ifstream file("staff.txt");
 
        char line[256];
 
  
        while (file.getline(line, 256))
+
Since Player pointers are internal to MERVBot, it is necessary to find a way of obtaining a Player pointer from the identifying information given by the game. One of the simpler ways is just to compare the names after converting to lowercase.
        {
 
            sendPrivate(p, line);
 
        }
 
        file.close();
 
    }
 
  
    //... command.cpp continues.
+
''Note'': Using pilot names as vital comparisions should be used with caution. See [http://cypherjf.sscentral.com/articles/bots-as-clients/ Bot-Issues] by CypherJF.
</pre>
 
</p>
 
<br /><!-- EXAMPLE I: END    -->
 
  
==== Example of printing player stats grid ====
 
<!-- EXAMPLE J: START  -->
 
<p>'''Example J''': Example of printing player stats grid.</p>
 
<p>'''Note''': This example code relies on previously discussed material. Please see the section "structures within structures" example for variable declarations, varibale freqcount = # of freqs.</p>
 
<p>'''Note''': User defined function ''sendFreqs'' is required.</p>
 
<p>In '''spawn.cpp''':
 
 
<pre>
 
<pre>
void botInfo::DisplayPlayers()
+
// return Player* info (or NULL if not found) from p->name info
 +
Player * botInfo::GetPilot(char *name)
 
{
 
{
    // Display Match player/freq stats in this format
+
// get pilot from a name, return as TempPlayer
    // ---------------------------------------------------
+
_listnode <Player> *parse = playerlist->head;
    // Squad: squad_name_1            K  D TK DMG DEALT TAKEN
 
    // ---------------------------------------------------
 
    // Player_1                      0  0  0        0    0
 
    // Player_2                      0  0  0        0    0
 
    // TOTAL:                        0  0  0        0    0
 
    // ---------------------------------------------------
 
    // Squad: squad_name_2            K  D TK DMG DEALT TAKEN
 
    // ---------------------------------------------------
 
    // Player_3                      0  0  0        0    0
 
    // Player_4                      0  0  0        0    0
 
    // Player_5                      0  0  0        0    0
 
    // TOTAL:                        0  0  0        0    0
 
    // ---------------------------------------------------
 
  
    for (freq=freqcount-1; freq>=0; freq--)
+
//convert search name to lowercase
    {
+
char nname[20], pname[20];
      char str[255];
+
strncpy(nname, name, 20);
 +
tolower(nname);
  
      sendFreqs("---------------------------------------------------");
+
while (parse)
 +
{
 +
Player *p = parse->item;
  
      sprintf(str, "Squad: %-20s K  D TK DMG DEALT TAKEN", freqs[freq].freqname);
+
// convert to lowercase to compare
 
+
strncpy(pname,p->name,20);
      sendFreqs(str);
+
tolower(pname);
 
+
if (strcmp(pname,nname)==0)
      sendFreqs("---------------------------------------------------");
+
return p;
 +
 +
parse = parse->next;
 +
}
  
      for (pilot=freqs[freq].playercount-1; pilot >= 0; pilot--)
+
return NULL; //player not found
      {
 
        sprintf(str, "%-20s %8d %2d %2d %9d %5d",
 
                freqs[freq].pilots[pilot].name,
 
                freqs[freq].pilots[pilot].kills,
 
                freqs[freq].pilots[pilot].deaths,
 
                freqs[freq].pilots[pilot].teamkills,
 
                freqs[freq].pilots[pilot].dmgdealt,
 
                freqs[freq].pilots[pilot].dmgtaken
 
              );
 
        sendFreqs(str);
 
      }
 
 
 
      sprintf(str, "TOTAL:                    %2d %2d %2d %9d %5d",
 
              freqs[freq].kills, freqs[freq].deaths,
 
              freqs[freq].teamkills,
 
              freqs[freq].dmgdealt,
 
              freqs[freq].dmgtaken
 
            );
 
      sendFreqs(str);
 
    }
 
 
 
    sendFreqs("---------------------------------------------------");
 
 
}
 
}
 
</pre>
 
</pre>
</p>
 
<br /><!-- EXAMPLE J: END    -->
 
  
==== Checking if any pilots are within a region ====
+
==Bot built in functions==
<!-- EXAMPLE K: START    -->
 
<p>'''Example K''': Example of checking if any pilots are within a region.</p>
 
<p>'''Note''': User-defined function ''GetPilotName'' is missing, and needs to be implemented.See prior examples.</p>
 
<p>'''Note''': User-defined function ''closeto'' is missing, and needs to be implemented. See prior examples.</p>
 
<p>In spawn.cpp ('''Note''': you will also need to add the function prototype to spawn.h accordingly):
 
<pre>
 
bool botInfo::FreqAInBox()
 
{
 
    // return true if teamA has a pilot in the box, otherwise false
 
    for (int tempplayercount = freqs[0].playercount-1; tempplayercount >= 0; tempplayercount--)
 
      if (GetPilotName(freqs[0].pilots[tempplayercount].name))
 
        if (closeto(TempPlayer, coordX, coordY, 73) && (TempPlayer->ship != SHIP_Spectator))
 
            return true;
 
 
 
    return false;
 
}
 
</pre>
 
</p>
 
<br /><!-- EXAMPLE K: END      -->
 
 
 
==== Functions to get a pilot's struct id info from a name or *player info ====
 
<!-- EXAMPLE L: START    -->
 
<p>'''Example L''': Example of functions to get a pilot's struct id info from a name or *player info.</p>
 
<p>'''Note''': Required structures needed for this example to work. See structure examples for variable information.</p>
 
<p>'''Note''': Remember to implement any function into the spawn.h accordingly.</p>
 
<p>'''Note''': Using pilot names as vital comparisions should be used with caution. See [http://cypherjf.sscentral.com/articles/botsasclients.html Bot-Issues] by CypherJF.</p>
 
<p>'''Note''' from Underlord: It is "better to implement these functions as passing values by reference instead of using global variables... [it is] just easier to not have to be declaring different int freq, int pilot all the time."</p>
 
<p>In spawn.cpp:
 
<pre>
 
// return struct freq/pilot id from *player info
 
bool botInfo::GetPilot(Player *p)
 
{
 
    // return freq, pilot of a player p
 
    for (freq=freqcount-1; freq>=0; freq--)
 
    if (p->team == freqs[freq].freqteam)
 
        for (pilot = freqs[freq].playercount-1; pilot>=0; pilot--)
 
            if (strcmp(p->name,freqs[freq].pilots[pilot].name)==0)
 
              return true;
 
 
 
    return false;
 
}
 
</pre>
 
<pre>
 
// return *player as TempPlayer info from p->name info
 
bool botInfo::GetPilotName(char *name)
 
{
 
    // get pilot from a name, return as TempPlayer
 
    _listnode <Player> *parse = playerlist->head;
 
 
 
    while (parse)
 
    {
 
        Player *p = parse->item;
 
 
 
        // convert both to lowercase to compare
 
        char pname[20];         
 
        strncpy(pname,p->name,20);
 
 
 
        char nname[20];         
 
        strncpy(nname,name,20);
 
 
 
        tolower(pname);
 
        tolower(nname);
 
 
 
        if (strcmp(pname,nname)==0)
 
        {
 
          TempPlayer = p;
 
          return true;
 
        }
 
       
 
        parse = parse->next;
 
    }
 
    return false;
 
}
 
</pre>
 
</p>
 
<br /><!-- EXAMPLE L: END      -->
 
 
 
==== Creating a logfile name using date and squad names ====
 
<!-- EXAMPLE M: START    -->
 
<p>'''Example M''': Example of creating a logfile name using date and squad names.</p>
 
<p>Example Output: 03y01m27dBLACKDRaGON vs Integral05h08m.txt.</p>
 
<p>Format: year, month, day, squadA vs squadB, hour, minute.</p>
 
<p>'''Note''': Assuming you define the following variables: squadA (String), squadB (String).</p>
 
<p>
 
<pre>
 
    // create log file name (squadA and squadB external char[20] variables)
 
    char u[100];
 
    time_t t=time(NULL);
 
    tm *tmp = localtime(&t);
 
    strftime(u,99,"%y",tmp);
 
    logname = u;
 
    logname += "y";
 
    strftime(u,99,"%m",tmp);
 
    logname += u;
 
    logname += "m";
 
    strftime(u,99,"%d",tmp);
 
    logname += u;
 
    logname += "d";
 
    logname += squadA;
 
    logname += " vs ";
 
    logname += squadB;
 
    strftime(u,99,"%I",tmp);
 
    logname += u;
 
    logname += "h";
 
    strftime(u,99,"%M",tmp);
 
    logname += u;
 
    logname += "m";
 
    logname += ".txt";
 
</pre>
 
</p>
 
<br /><!-- EXAMPLE M: END      -->
 
 
 
==== Sending messages to playing freqs or public and logging depending on status ====
 
<!-- EXAMPLE N: START    -->
 
<p>'''Example N''': Example of sending messages to playing freqs or public and logging depending on status.</p>
 
<p>'''Note''': Assumes you have the following variables declared: teamA (String), teamB (String), logname (String).</p>
 
<p>'''Note''': Remember to implement the function prototypes into spawn.h</p>
 
<p>Required include statements:
 
<pre>
 
#include <fstream>
 
using namespace std;  // bad coding practice, but it works.
 
</pre>
 
</p>
 
<p>In spawn.cpp:
 
<pre>
 
// teamA, teamB, logname global variables
 
void botInfo::sendFreqs(char *msg)
 
{
 
  char *mmsg = "*arena";
 
  String s = msg;
 
 
 
  if (teammsgs == false)
 
  {
 
    s.prepend("*arena ",7);
 
    sendPublic(s);
 
  }
 
  else
 
  {
 
    sendTeamPrivate(8025,msg);
 
    sendTeamPrivate(teamA,msg);
 
    sendTeamPrivate(teamB,msg);
 
  }
 
  if (gameon == true)
 
  {
 
    ofstream outf(logname, ios::app);
 
    outf << msg << endl;
 
    outf.close();
 
  }
 
}
 
</pre>
 
</p>
 
<br /><!-- EXAMPLE N: END      -->
 
  
==== Reading in all player/freqs to struct data ====
+
Here are some useful MervBot commands to control what the bot is doing.
<!-- EXAMPLE O: START    -->
 
<p>'''Example O''': Example of reading in all player/freqs to struct data.</p>
 
o) Example of reading in all player/freqs to struct data<br />       
 
                                                                       
 
                                                                       
 
                                                                    <blockquote>// see structures within structures example for freqs[] declaration<br />
 
                                                                       
 
                                                                       
 
                                                                       
 
      <br />
 
// to get freqs in a game where there are several freqs<br />
 
void botInfo::GetFreqs()<br />
 
{<br />
 
&nbsp;&nbsp;&nbsp; // read pilots into freq struct data from ingame and on playing freqs<br />                     
 
                                                                       
 
                                                                       
 
                                                        <br />
 
&nbsp;&nbsp;&nbsp; _listnode &lt;Player&gt; *parse = playerlist-&gt;head;<br />
 
&nbsp;&nbsp;&nbsp; <br />
 
&nbsp;&nbsp;&nbsp; while (parse)<br />
 
&nbsp;&nbsp;&nbsp; {<br />
 
Player *p = parse-&gt;item;<br /> 
 
                                                                       
 
                                                                       
 
                                                                       
 
  <br />
 
if (p-&gt;ship != SHIP_Spectator)<br />
 
&nbsp;&nbsp;&nbsp; if (closeto(p, coordX, coordY, 73))<br />
 
&nbsp;&nbsp;&nbsp; {<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // look for freq in struct<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; bool foundfreq=false;<br />
 
                                                                       
 
                                                                       
 
                                                                       
 
      <br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; freq=freqcount-1;<br />
 
                                                                       
 
                                                                       
 
                                                                       
 
      <br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while ((freq&gt;=0) &amp;&amp; (foundfreq==false))<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
&nbsp;&nbsp;&nbsp; if (p-&gt;team == freqs[freq].freqteam)<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
foundfreq=true;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
strncpy(freqs[freq].pilots[freqs[freq].playercount].name,
 
p-&gt;name, 20);<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
freqs[freq].playercount++;&nbsp;&nbsp;&nbsp;
 
<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; freq--;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br />
 
                                                                       
 
                                                                       
 
                                                                       
 
      <br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // didnt find freq in struct so add new freq<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (foundfreq == false)<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (manualsquads == false)<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
strncpy(freqs[freqcount].freqname,
 
p-&gt;squad, 20);<br />                                                 
 
                                                                       
 
                                                                       
 
                            <br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
if (freqcount == 0)<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
&nbsp;&nbsp;&nbsp; teamA = p-&gt;team;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
&nbsp;&nbsp;&nbsp; strncpy(squadA,
 
p-&gt;squad, 20);<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
&nbsp;&nbsp;&nbsp; teamB = p-&gt;team;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
&nbsp;&nbsp;&nbsp; strncpy(squadB,
 
p-&gt;squad, 20);<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
if (p-&gt;team == teamA)<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
&nbsp;&nbsp;&nbsp; strncpy(freqs[freqcount].freqname,squadA,20);<br />
 
                                                                       
 
                                                                       
 
                                                                       
 
      <br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
else if (p-&gt;team == teamB)<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
&nbsp;&nbsp;&nbsp; strncpy(freqs[freqcount].freqname,squadB,20);<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
&nbsp;&nbsp;&nbsp; freqs[freqcount].freqteam = p-&gt;team;<br />       
 
                                                                       
 
                                                                       
 
                                                                    <br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
&nbsp;&nbsp;&nbsp; strncpy(freqs[freqcount].pilots[0].name, p-&gt;name, 20);<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
                                                                       
 
                                                                       
 
                                                                       
 
    <br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; freqs[freqcount].playercount++;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; freqcount++;<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br />
 
&nbsp;&nbsp;&nbsp; }<br />
 
parse = parse-&gt;next;<br />
 
&nbsp;&nbsp;&nbsp; }<br />
 
}<br />                                                                 
 
                                                                       
 
                                                                       
 
                                                    <br />
 
// to get freqs in a game where there are only two teams<br />
 
void botInfo::GetFreqs()<br />
 
{<br />
 
&nbsp;&nbsp;&nbsp; // read pilots into freq struct data from ingame and on playing freqs<br />
 
&nbsp;&nbsp;&nbsp; _listnode &lt;Player&gt; *parse = playerlist-&gt;head;<br />
 
&nbsp;&nbsp;&nbsp; <br />
 
&nbsp;&nbsp;&nbsp; while (parse)<br />
 
&nbsp;&nbsp;&nbsp; {<br />
 
Player *p = parse-&gt;item;<br /> 
 
                                                                       
 
                                                                       
 
                                                                       
 
                                          <br />
 
if ((p-&gt;ship != SHIP_Spectator)
 
&amp;&amp; ((p-&gt;team == teamA) || (p-&gt;team == teamB)))<br />
 
{<br />
 
&nbsp;&nbsp;&nbsp; // freq 100, team A<br />
 
&nbsp;&nbsp;&nbsp; // set freq<br />
 
&nbsp;&nbsp;&nbsp; freq = 0;<br />
 
                                                                       
 
                                                                       
 
                                                                       
 
                                            <br />
 
&nbsp;&nbsp;&nbsp; if (p-&gt;team == teamB)<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; freq = 1;<br />
 
                                                                       
 
                                                                       
 
                                                                       
 
                                              <br />
 
&nbsp;&nbsp;&nbsp; // number of pilots on freq counted so far, starts 0<br />
 
&nbsp;&nbsp;&nbsp; pilot = freqs[freq].playercount;<br />
 
                                                                       
 
                                                                       
 
                                                                       
 
                                              <br />
 
&nbsp;&nbsp;&nbsp; // pilot name<br />
 
&nbsp;&nbsp;&nbsp; strncpy(freqs[freq].pilots[pilot].name, p-&gt;name, 20);<br />
 
&nbsp;&nbsp;&nbsp; // time stamp for playing time<br />
 
&nbsp;&nbsp;&nbsp; freqs[freq].pilots[pilot].cplaying_time = GetTickCount();<br />
 
                                                                       
 
                                                                       
 
                                                                       
 
                                              <br />
 
&nbsp;&nbsp;&nbsp; // slot name<br />
 
&nbsp;&nbsp;&nbsp; if (freqs[freq].playercount &lt; NUMBER_PILOTS)<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 
strncpy(freqs[freq].slotname[pilot], p-&gt;name, 20);<br />
 
<br />
 
&nbsp;&nbsp;&nbsp; // increment freq player count<br />
 
&nbsp;&nbsp;&nbsp; freqs[freq].playercount++;<br />
 
                                                                       
 
                                                                       
 
                                                                       
 
                                              <br />
 
&nbsp;&nbsp;&nbsp; // if freq not already have name, give it player squad name<br />
 
&nbsp;&nbsp;&nbsp; if ((manualsquads == false) &amp;&amp; (strlen(p-&gt;squad) &gt; 0))<br />
 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; strncpy(freqs[freq].freqname, p-&gt;squad, 20);<br />
 
                                                                       
 
                                                                       
 
                                                                       
 
                                              <br />
 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // set player ship<br />
 
&nbsp;&nbsp;&nbsp; freqs[freq].pilots[pilot].ship = p-&gt;ship + 1;<br />
 
}<br />
 
parse = parse-&gt;next;<br />
 
&nbsp;&nbsp;&nbsp; }<br />
 
}<br />                                                       
 
                                                                       
 
                                                                       
 
                      </blockquote>                                   
 
<br /><!-- EXAMPLE O: END      -->
 
==== Finding MVP from struct data (2*kills - deaths formula) ====
 
<!-- EXAMPLE P: BEGIN -->
 
<p>'''Example P''': Example of finding MVP from struct data (2*kills - deaths formula).</p>
 
<p>
 
<pre>
 
int highest = -20;
 
int mvp = 0;
 
  
for (pilot = freqs[mvpteam].playercount-1; pilot >=0; pilot--)
+
Player.cpp:
  {
+
* Player::move(Sint32 x, Sint32 y) moves a player to the coordinates specified by x and y
    if (((freqs[mvpteam].pilots[pilot].kills * 2) - freqs[mvpteam].pilots[pilot].deaths) > highest)
+
* Player::clone(Player *p) clones a player into a player class
      {
 
        mvp = pilot;
 
        highest = (freqs[mvpteam].pilots[pilot].kills * 2) - freqs[mvpteam].pilots[pilot].deaths;
 
      }
 
  }
 
</pre>
 
</p>
 
<br /><!-- EXAMPLE P: END  -->
 
==== Print time stamp of event ====
 
<!-- EXAMPLE Q: BEGIN -->
 
<p>'''Example Q''': Print time stamp of event.</p>
 
<p><pre>#include "time.h" // required include</pre>Use:
 
<pre>
 
char u[100];
 
time_t t=time(NULL);
 
tm *tmp = localtime(&t);
 
strftime(u,99,"%c",tmp);
 
sendPublic("Current date and time: " + (String) u);
 
</pre></p>
 
<br /><!-- EXAMPLE Q: END  -->
 
==== Simple way to track player bomb/bullet damage stats ====
 
<!-- EXAMPLE R: BEGIN -->
 
<p>'''Example R''': Simple way to track player bomb/bullet damage stats.</p>
 
<p>'''Note''': see data section for how to setup set_tag.</p>
 
<p>'''Note''': see clientprot.h for weapon information.</p>
 
<p>In '''spawn.cpp''':
 
<pre>
 
case EVENT_WatchDamage:
 
{
 
    if (PLAYING)  // if tracking stats
 
    {
 
        if ((wi.type == PROJ_PBomb) && (p->name != k->name))
 
        {
 
            set_tag(k, DMG_BOMB_DEALT, get_tag(k, DMG_BOMB_DEALT) + damage);
 
            set_tag(k, DMG_TOTAL_DEALT, get_tag(k, DMG_TOTAL_DEALT) + damage);
 
            set_tag(p, DMG_BOMB_TAKEN, get_tag(p, DMG_BOMB_TAKEN) + damage);
 
            set_tag(p, DMG_TOTAL_TAKEN, get_tag(p, DMG_TOTAL_TAKEN) + damage);
 
        }
 
        else if (wi.type == PROJ_BBullet)
 
        {
 
            set_tag(k, DMG_BULLET_DEALT, get_tag(k, DMG_BULLET_DEALT) + damage);
 
            set_tag(k, DMG_TOTAL_DEALT, get_tag(k, DMG_TOTAL_DEALT) + damage);
 
            set_tag(p, DMG_BULLET_TAKEN, get_tag(k, DMG_BULLET_TAKEN) + damage);
 
            set_tag(p, DMG_TOTAL_TAKEN, get_tag(k, DMG_TOTAL_TAKEN) + damage);
 
        }
 
    }
 
</pre></p><br />
 
<!-- EXAMPLE R: END  -->
 
==== Simple way to print those stats ====
 
<!-- EXAMPLE S: START -->
 
<p>'''Example S''': Simple way to print those stats.</p>
 
<p><pre>
 
case OP_Moderator:
 
{
 
  if (c->check("showstats"))
 
    {
 
      sendPublic("Showing stats:");
 
     
 
      _listnode <Player> *parse = playerlist->head;           
 
 
 
      while (parse)
 
{
 
  Player *p = parse->item;
 
   
 
          if (get_tag(p, DMG_TOTAL_DEALT) > 0)
 
  {
 
    char str[256];
 
    sprintf(str, "(%-20s Dmg Dealt: Total %0004d, Bomb %0004d, Bullet %0004d  Dmg TAKEN: Total %0004d, Bomb %0004d, Bullet %0004d)",
 
    p->name,
 
            get_tag(p,DMG_TOTAL_DEALT),
 
            get_tag(p,DMG_BOMB_DEALT),
 
            get_tag(p,DMG_BULLET_DEALT),
 
    get_tag(p,DMG_TOTAL_TAKEN),
 
            get_tag(p,DMG_BOMB_TAKEN),
 
            get_tag(p,DMG_BULLET_TAKEN)
 
            );
 
  
    sendPublic(str);
+
Look in Commands.txt , command.cpp (core), or /!help to bot to see all bot external commands (example /!go &lt;arena&gt;).
    }
 
  parse = parse->next;
 
}
 
    }
 
}
 
</pre></p><br />
 
<!-- EXAMPLE S: END  -->
 
  
==== Make bot spectate specific coordinates ====
+
''LVZ Object toggling commands in plugins are to go here.''
<!-- EXAMPLE T: START  -->
 
<p>'''Example T''': Make bot spectate specific coordinates.</p>
 
<p>
 
<pre>
 
// make bot spectate the coord 512,600
 
// possible use - capturing weapon packets in a specific region
 
  
tell(makeFollowing(false));
+
[[Category:Guides]]
tell(makeFlying(true));
 
me->move(512 * 16, 600 * 16);
 
tell(makeSendPosition(true));
 
</pre></p>
 
<br /><!-- EXAMPLE T: END  -->
 

Latest revision as of 05:38, 3 August 2009

This tutorial is based on the ever-popular MERVBot Tutorial by Underlord. It has since been updated to reflect new changes with MervBot. To see examples of how to use this instruction, see MERVBot Example Code.

This tutorial also assumes that you have a basic knowledge of C++. If you don't, check out cplusplus.com's great documentation.

Setting up a MERVBot (plugin)

MERVBot download site


Obtaining MERVBot

  • Download the latest build.
  • Unrar MERVBot.rar into a new folder. (example c:\program files\continuum\mervbot)
  • Unzip src.zip into "src" subfolder of that new folder (example c:\program files\continuum\mervbot\src)

Preparing to write a plugin

Note: if you only want to execute someone's premade plugin (.dll), skip to step 4, otherwise continue to learn how to make your own bot

Download DLL-plugin Tutorial and unzip Tutorial.zip (containing spawn.h, spawn.cpp, and command.cpp) into a "tutorial" subfolder of that new folder. (example c:\program files\continuum\mervbot\src\tutorial).

File descriptions:

  • spawn.h = declare/initialize globals
  • command.cpp = code for commands coming into bot (ie /!help, /!play, etc)
  • spawn.cpp = code that interacts with bot spawns

Microsoft Visual c++

  1. Start Visual Studios 6.0.
  2. Click the Drop Down Menu labeled "File" at the top left of your screen.
  3. Click "New".
  4. On the next screen that comes up, choose from the Project tab, then Win32 Dynamic-Link Library
  5. Select the "/src" folder as the base folder (example c:\program files\continuum\mervbot\src)
  6. Name your project "mybot". This will make a "mybot" subfolder in your "src" folder. Click OK. (example creates c:\program files\continuum\mervbot\src\mybot)
  7. Choose to create an "Empty DLL project".
  8. Click "Finish".
  9. Click the Drop Down Menu labbled "Project".
  10. Click "Add To Project Files"
  11. Copy only spawn.h, spawn.cpp, and command.cpp from the "tutorial" folder into the this new folder. (example from c:\program files\continuum\mervbot\src\tutorial to c:\program files\continuum\mervbot\src\mybot)
  12. Click the Drop Down Menu labelled "Build".
  13. Click "Build (dll name)" - where (dll name) is "mybot"
  14. Go into your "mybot" folder and look for a folder named "Debug" (example c:\program files\continuum\mervbot\src\mybot\debug)
  15. Your new DLL will be in that folder. (example mybot.dll)
  16. Copy mybot.dll to your base folder that has mervbot.exe in it (example c:\program files\continuum\mervbot)


Run your bot dll

To run your bot you need your DLL (mybot.dll), Commands.txt, MERVBot.exe, MERVBot.ini, Operators.txt, Spawns.txt, and zlib.dll all in one folder (example c:\program files\continuum\mervbot).

  1. Edit spawns.txt. Read every word of spawns.txt to find out what needs to go in there.
    Example:
    2v2-Bot-League : botpw : 2v2a : 2v2league : staffpw

    Note: The bot will attempt to create the name if it doesn't exist already.


  2. Edit MERVBot.ini

    [Login]
    Zone=216.33.98.254:21000	// your zone IP:PORT available from zone.dat in Continuum dir
    


  3. Edit operators.txt. Read every word of operators.txt to find out what needs to go in there.
    Example:
    4:my_name:
    4:another_sysop:
    3:other_person:
    


  4. Make sure the bot is on vip.txt or has smod+ access, then run MERVBot.exe.
  5. You can now edit your plugin code by opening "mybot.dsw" (example c:\program files\continuum\mervbot\src\mybot\mybot.dsw) in Microsoft Visual C++. Edit the spawn.h, spawn.cpp, and command.cpp to create your plugin, then build, copy your updated DLL to your MERVBot.exe folder and then execute the bot. Use the tutorial to get ideas on how to implement certain types of features into the bot.

Player Commands - (command.cpp)

This section describes how to implement player commands into your plugin. Commands are sent to the botInfo::gotCommand function in command.cpp.

Example (makes bot reply to !test with "hi"):

void botInfo::gotCommand(Player *p, Command *c) {
	switch (p->access)
	{
        case OP_Moderator:
                {
                     // handle moderator-operator commands here.
                }
	case OP_Player: //appropriate staff rank here.
		{
			if (c->check("test")) //replace "test" with whatever command you want
			{
				//put your command code here
				sendPrivate(p,"hi"); //example
			}
		}

How to have commands with numerical parameters

Example (!test #):

	if (c->check("test")) { // reads in test #, default to 1 if invalid number input
		int temp = 1;

		if (isNumeric(c->final))
			temp = atoi(c->final);

How to have player name as input

Example (!rank player):

	if (c->check("rank"))
	{
		String player_name = c->final;

		if (player_name.IsEmpty()) // default name to self if invalid name
			player_name = p->name;

How to have multi-parameter input

Use the CRT function sscanf() to scan the string for the values.

Example (!squads squadA vs squadB or !squads teamA:squadA:teamB:squadB):

else if (c->check("squads"))
{
	char squadA[20], squadB[20];
	int teamA, teamB;

	strncpy(squadA, "", 20);
	strncpy(squadB, "", 20);

	int n_found;

	//Note: %[A-Za-z ] is equivalent to %s, but allows an internal space.

	//scan the string for the two squads separated by " vs "
	n_found = sscanf(c->final, "%[A-Za-z ] vs %[A-Za-z ]", squadA, squadB);

	//if that fails, scan the string for freqA:squadA:freqB:squadB
	if (n_found < 2)
		sscanf(c->final, "%d:%[A-Za-z ]:%d:%[A-Za-z ]", &teamA, squadA, &teamB, squadB);
}

Help Menu

When a player sends !help to the bot, MERVBot calls botInfo::gotHelp() in each plugin loaded.

void botInfo::gotHelp(Player *p, Command *c)
{
	if (!*c->final)
	{
	sendPrivate(p, "4v4 Bot General Commands:");
	sendPrivate(p, "------------------------");
	sendPrivate(p, "!caps - get captain names");
	sendPrivate(p, "!roster <squad> - get roster of a squad");
	sendPrivate(p, "!schedule- get current schedule");
	sendPrivate(p, "!score - get current score");

Event Calls

MERVBot is event based, so when making a bot you need to decide what will happen at certain events. Normal plugins need to consider what happens when bot enters arena, player enters arena, player leaves arena, player events like kill, shipchange, teamchange, spec, move then any other relevant events to your bot. Just worry about events that are relevant to the tasks your bot is doing.

MERVBot sends events to botInfo::gotEvent() in spawn.cpp. Each supported event is already present and categorized in gotEvent(), along with the paramters that MERVBot sends with the event. When a plugin wants the bot to do something, it sends tell(event) to the bot.

See dllcore.h for a list of current events and their descriptions. Dllcore.h also contains functions (like makeFollowing) to make events to send back to the bot via tell().

tell(makeFollowing(false));

The Messaging System

Private message - void sendPrivate(Player *player, char *msg);

Examples:

sendPrivate(p,"hi");

String s="test";
sendPrivate(p,s);

String s="test";
s += "ing";
sendPrivate(p,s);

char captain1[20];
char captain2[20];
strncpy(captain1,"",20);
strncpy(captain2,"",20);
sendPrivate(p,(String) captain1 + " and " + (String) captain2 + " are the captains.");

Team message - void sendTeamPrivate(Uint16 team, char *msg);

Examples:

a) sendTeamPrivate(8025,"hi spec freq");
b) Uint16 test=0; sendTeamPrivate(test,"hi freq 0");

Public message - void sendPublic(char *msg);

Example: sendPublic("*arena " + (String) p->name + " is now a captain");

Chat channel message - void sendChannel(char *msg);

Example: sendChannel("hi chat channel");

Remote private message - void sendRemotePrivate(char *name, char *msg);

Example: sendRemotePrivate("Player01", "hi");

Note: to have bot print several lines of text fast it needs sysop in the arena (sysop in arena bot first spawns to also) otherwise it'll print slow to avoid being kicked for spam

Output of data in messages

An example of using normal strings to output data/messages.

 // does *arena X pilots left in game
 // NOTE: variable temp needs to be defined with some value

 String s = "*arena ";
       s += temp;
       s += " pilots left in the game.";

 sendPublic(s);

Or,

 //NOTE: this can be considered inefficient.

 sendPublic("*arena " + (String)temp + " pilots left in the game");

An example using sprintf to align/space data, where output data will be in this approximate format.

// output data will be in this approximate format (not lined up perfectly because of html)
// --------------------------------------------------------------------------------------
// Squad: squadname       PTS     FPTS    K    D  DMG DEALT TAKEN   F  FK    FLT
// --------------------------------------------------------------------------------------
// PlayerA              10000      500  116  101       9999 99999  10 150 980:55
// PlayerB                500      200    7    5       9999 99999   5   3   0:04

char str[255];
sendPublic("*arena--------------------------------------------------------------------------------");

sprintf(str, "*arena Squad: %-20s   PTS     FPTS   K   D  DMG DEALT  TAKEN  F  FK  FLT",
         freqs[freq].freqname
        );

sendPublic(str);

sendPublic("*arena--------------------------------------------------------------------------------");

            // assuming existing freqs struct with data
            for (pilot=freqs[freq].playercount-1; pilot>=0; pilot--)
            {
                // on freq squad so print stats
                char outString[255];

                sprintf(outString, "*arena %-20s %12d %8d %3d %3d %10d %6d %2d %3d %3d:%02d",
                       freqs[freq].pilots[pilot].name,
                       freqs[freq].pilots[pilot].points,
                       freqs[freq].pilots[pilot].flagpoints,
                       freqs[freq].pilots[pilot].kills,
                       freqs[freq].pilots[pilot].deaths,
                       freqs[freq].pilots[pilot].dmgdealt,
                       freqs[freq].pilots[pilot].dmgtaken,
                       freqs[freq].pilots[pilot].flags,
                       freqs[freq].pilots[pilot].flagkills,
                       freqs[freq].pilots[pilot].flagtime /60,
                       freqs[freq].pilots[pilot].flagtime %60
                       );
                
                sendPublic(outString);
            }

            // Notes: sprintf format = sprintf(output char string, spacing, variables)
            // Notes: s = chars, d = integer, - = left align, right align default
            // Notes: doing %02d = put 0 in front if not 2 digits, %3d:%02d makes 0:04 format

Time

Each time MERVBot sends an EVENT_Tick to a plugin (once a second), the default handler code decrements each value in an array of countdowns. You can modify the number of countdowns and add code to occur at a specific value for one of the countdowns.

Setup number of timers and initialize in spawn.h:

class botInfo
{
	#define COUNTDOWNS 10 		// how many countdowns you want
	int countdown[COUNTDOWNS];	// this gives you 10 timers

// unrelated code
 
	public:
	botInfo(CALL_HANDLE given)
	{
	countdown[0] = 0;
	countdown[1] = 60; // 60 seconds
	//
	// initialize values
	//
	countdown[9] = 5*60; // 5 minutes

Using timer functions in spawn.cpp:

case EVENT_Tick:
{
	for (int i = 0; i < COUNTDOWNS; ++i) //cycles through each countdown you have
		--countdown[i]; //note that countdowns will continue decrementing past 0.

	if (countdown[1] == 2) // when timer #1 hits two seconds
	{
	// do stuff here when timer #1 hits 2 seconds
	// example: sendPublic("two seconds left, setting timer to 1 minute");
	// example: countdown[1] = 60; // change timer #1 value
	}

You can then have events (such as EVENT_PlayerDeath) change the value of a countdown to make the bot do something a set time after an event occurs.

Tracking time not using countdown[n]

This is a solution to a common problem of determining the amount of time it takes for something to occur. Using basic math, we record a start-time B, and an end-time E, both in the unit of seconds, we calculate the time elapsed by E-B.

Lucky for us, Windows provides a function called GetTickCount() that is a measurement of time (milliseconds) that we can use for such cases.

So:

	int begin = GetTickCount();

	// do some code here.

	int end = GetTickCount();
  
	int delta = (end - begin) / 1000;  // elapsed time converted to seconds from milliseconds

Obtaining the current time

Requirements: Include <time.h>.

Use:

 char u[100];
 time_t t=time(NULL);
 tm *tmp = localtime(&t);
 strftime(u,99,"%c",tmp);
 sendPublic("Current date and time: " + u);

Writing Functions

For this example, we will take the function called closeto, which determines if a player exists in an specific radius around a point. Now to apply this function to a MervBot plugin, you need to write it into the spawn.cpp - at the top of the file in the //////// DLL "import" //////// setion, as below:

//////// DLL "import" ////////

bool closeto(Player *p, int x, int y, int tolerance) 
{
	// Requires the function abs() to be declared elsewhere.
	// Return if player p is in area of square with center x, y
	//   and radius = tolerance
	return (abs((p->tile.x) - x) - tolerance) && (abs((p->tile.y) - y) - tolerance);
}


If you want your function to have access to the data from spawn.h botInfo class, you make the function apart of it. To do this, we add the botInfo:: infront of the function name, in spawn.cpp.

//////// DLL "import" ////////

bool botInfo::closeto(Player *p, int x, int y, int tolerance) 
    {
	 ...
    }

In spawn.h, add your method's prototype without botInfo::, it will look like this:

//...
botInfo(CALL_HANDLE given)
 {
//  ...
 } 
  bool closeto(Player *p, int x, int y, int tolerance); // Your function prototype.

  void clear_objects(); //provided by Catid, and already exists.
  void object_target(Player *p); //provided by Catid, and already exists.
//  ...
};

If you're not familiar with prototypes, notice it is similar to that in your spawn.cpp, but without the botInfo::, and a trailing ;.

Function notes

Remember that you can pass variables by reference. If variables are passed by reference, any changes a function makes to the variables will remain after the function returns.

From time to time you will need to pass an array to a function. An example illustrating this is:

	int freqs[5]; // declare our example data.

	// call function - notice freqs and not freqs[5] or freqs[].
	my_function(freqs); //You're not passing the array itself, just a pointer to the array.

	// function - notice freqs[] and not freqs[5] or freqs
	void my_function(int freqs[]) {} //You're specifying that the freqs parameter is an array

Cycling through players

MERVBot stores player-related data in a linked list. A linked list is a datatype that stores its data in a series of structures linked to each other, hence the name.

To search through the players in the arena, just start at the first link, then continue through all the following links until you reach the end:

_listnode <Player> *parse = playerlist->head;	//set to first link of the player linked list

while (parse)	//parse will be NULL when we reach the last link
{
	Player *p = parse->item;	//item is the actual data stored in the link

	// do functionality here
	// Example 1: sendPrivate(p,"*watchdamage"); // turns on all pilot's watchdamage
	// Example 2: if (p->safety != 0) sendPrivate(p,"*spec"); // spec all pilots in safe zone

	parse = parse->next;	//set parse to the next link
}

For example, assuming our bot has smod+ privilages, the following code will set all non-spectator players to a specific ship. First begin by adding the following function prototype to the spawn.h in the botInfo class:

 void handleCmdSetShip(enum Ship_Types ship);

In spawn.cpp add:

void botInfo::handleCmdSetShip(enum Ship_Types ship)
{
	//Note that the parameter ship is of the Ship_Types enum,
	//so its value is hopefully restricted to the proper types.

	_listnode <Player> *parse = playerlist->head;
	while (parse)
	{
		Player *p = parse->item;

		if ( p->ship != ship && p->ship != SHIP_Spectator )
				sendPrivate(p, "*setship " + (String)ship);

		parse = parse->next;
	}
}

To use, just call the function with the appropriate Ship_Type from the enum in clientprot.h:

 handleCmdSetShip(SHIP_Warbird);

Random numbers

Generating a random number

To use this method, these two includes must be used:

#include "time.h"    //provides time() function.
#include "stdlib.h"  //provides srand() and rand() functions.

Use:

    srand(time(NULL)); // seed random number generator.

    rand(); // randomize.

    int temp = (int) (51 * ((float)rand()/RAND_MAX));
       // the above line returns a random integer between 0 and 51.
       // Note: RAND_MAX is a global constant defined in stdlib.h

Picking a random pilot

Note: A required user-defined function, getInGame(), must be created for this example to work.

int temp = GetTickCount() % getInGame();  // getInGame() = how many pilots in arena

Player *rabbit = NULL;

_listnode <Player> *parse = playerlist->head;
while (parse)
{
	Player *p = parse->item;

	if (p->ship != SHIP_Spectator) // if player is not a spectator
	if ( !(--temp) ) // and if we've hit the randomly-selected pilot
	{
		rabbit = p;
		break;
	}
	parse = parse->next;
}

Storing data for pilots

There are several ways to store data for pilots (ie tracking flagtime or kills in a period of time). Note that these methods are all purely internal to the bot, and don't effect anything beyond the plugin in any way.

  1. Built-in get/setTag: Tracks data until player leaves the arena, then automatically deletes data.
  2. Modified perm get/setTag: Tracks data until bot leaves arena, then automatically deletes data. (Advantage: easier to sort by player)
  3. Custom Structs: Tracks data until plugin deletes it. (Advantage: easier to sort by freqs)

Note: 2 and 3 are similar in effect, mostly the difference is in how you are able to search through data you need to decide which method of storing data is best for each bot depending on what it does.

Built-in get/setTag method

Player tags simply tag a player with a number. Define the wanted values in spawn.h at the top:

#define DMG_DEALT        0
#define DMG_TAKEN        1

In spawn.cpp, initialize the values on ArenaEnter and PlayerEnter:

case EVENT_ArenaEnter:
{
	// ...

	// do for all pilots in arena when bot enters
	_listnode <Player> *parse = playerlist->head;
	while (parse)
	{
	   Player *p = parse->item;  // get pilot

	   set_tag(p, DMG_DEALT, 0); // initialize to 0
	   set_tag(p, DMG_TAKEN, 0);
	   sendPrivate(p, "*watchdamage");  // optionally turn on player *watchdamage

	   parse = parse->next;  // get next pilot
	}
}
case EVENT_PlayerEntering:
{
//	...
	set_tag(p, DMG_DEALT, 0); // initialize to 0
	set_tag(p, DMG_TAKEN, 0);
	sendPrivate(p,"*watchdamage");
}

Then somewhere edit the tag values:

case EVENT_WatchDamage:
{
	// sets tag for k (shooter) to be old value plus damage currently dealt

	int old_damage = get_tag(k, DMG_BOMB_DEALT);
	set_tag(k, DMG_BOMB_DEALT, old_damage + damage);
}

The following demonstrates how to retrieve the tag values as a command in command.cpp:

if (c->check("showstats"))
{
	int temp = get_tag(p, DMG_TOTAL_DEALT);

	String s = "You've done ";
	s += temp;
	s += " damage so far!";

	sendPrivate(p,s);
 }

Modified permanent get/setTag method

This method is the same as get/setTag with some modifications to the tag code to retain them after the player leaves. Beware of using this method if bot is in an arena for long periods of time, linkedlist could get huge.


// spawn.h, add char name[20]; into struct PlayerTag

struct PlayerTag
{
	Player *p;
	char name[20];
	int index;
	int data;
};

In spawn.cpp:

	case EVENT_PlayerLeaving:
	{
	    Player *p = (Player*)event.p[0];

	    // killTags(p);  // remove so tag not deleted on arena exit

//	...

Locate in spawn.cpp and modify accordingly:

int botInfo::get_tag(Player *p, int index)
{
    _listnode <PlayerTag> *parse = taglist.head;
    PlayerTag *tag;

    while (parse)
    {
      tag = parse->item;

      // if (tag->p == p)
      if (strcmp(tag->name,p->name)==0)  // now tracking by player name, not pointer
      if (tag->index == index)
        return tag->data;

      parse = parse->next;
    }
    return 0;
}

void botInfo::set_tag(Player *p, int index, int data)
{
    _listnode <PlayerTag> *parse = taglist.head;
    PlayerTag *tag;

    while (parse)
    {
      tag = parse->item;

      //if (tag->p == p)
      if (strcmp(tag->name,p->name)==0) // now tracking by player name, not pointer
      if (tag->index == index)
      {
        tag->data = data;
        return;
      }
      parse = parse->next;
    }

    tag = new PlayerTag;
    // tag->p = p; // not tracking by pointer anymore
    strncpy(tag->name, p->name, 20); // tracking by player name
    tag->index = index;
    tag->data = data;
    taglist.append(tag);
}

Using structs

In spawn.h:

class botInfo
{
	struct freqdata 
	{
		int kills, deaths;
	};
// ...
};

To make use of this structure, implement accordingly:

	freqdata freqs[100]; // 100 of those structs

Access the data in spawn.cpp using

freqs[56].kills = 1;

See CPlusPlus.com's Structures tutorial for a more comprehensive guide. Note that, as shown on the bottom of the page, you can have structures within structures. Thus, for example, you could have a structure for each freq with a structure for each player nested within them.

Input/Output to files

For reading and/or writing to files with C++ you must have the required include statement as follows:

#include <fstream>
using namespace std;

File stream input

The following example will show you how to read a file, duel.ini, line by line.

#include "stdlib.h" // for atoi()
  ifstream file("duel.ini");

  if (!file.good()) // if there was an error opening the file
    sendPublic("*arena Error opening file for reading"); // or add your own error handler
  else
  {
    char line[256];

    // read in MaxBoxes=X
    while (file.getline(line, 256))
    {
     
      if (CMPSTART("MaxBoxes=", line)) //Does the line begin with MaxBoxes= ?
      {
        MAX_BOXES = atoi(&(line[9]));  //If so, read the value into an integer, using atoi.
        break;
      }
    }

    file.close();
  }

File stream output

The following code example will demonstrate how to append to a file, duelleaguestat.inc.

 ofstream file("duelleaguestat.inc", ios::app);   // app = put all data at end of file

 if (!file.good()) // if there was an error opening the file
   sendPublic("*arena Error opening file.");
 else
 {
   file << squad1<< endl;  // squad1 = char[20]
   file << " vs "<< endl;
   file << squad2<< endl;  // squad2 = char[20]
   file.close();
 }

Similarly, you are able to write an output of a String to a file:

 // key is converting String to (char*) to file write
 String str = freqs[freq].slotname[slot];
 str += ", Repels: " + (String)(int) t->repel;
 file << endl;
 file << (char*) str;

Input with GetPrivateProfileString

GetPrivateProfileString(), a function provided by Windows for reading INI files, will automatically find an INI key (like "MaxBoxes=") in a file for you. See the MSDN Library for help on this function. This next example will show how to read input using GetPrivateProfileString() based on the rampage plugin.

The file format for rampage.ini is like this:

 7=is on a killing spree! (6:0)
 10=is opening a can of booya! (9:0)

In rampageini.cpp:

#include "rampageini.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#define NUM_RANKS 10
#define BUFFER_LEN 256

struct RampageSettings
{
	char quotes[NUM_RANKS][BUFFER_LEN];
};
 
void LoadSettings(RampageSettings &setts);

static char path[BUFFER_LEN];

char *rank_type[NUM_RANKS] = { "7", "10" };

void LoadSettings(RampageSettings &setts)
{
	GetCurrentDirectory(BUFFER_LEN - 64, path);
	strcat(path, "\rampage.ini");

	for (int i = 0; i < NUM_RANKS; ++i)
	{
		GetPrivateProfileString("Comments", rank_type[i], "-ERROR-",
					setts.quotes[i], BUFFER_LEN, path);
	}
}

Player data

As stated earlier in the tutorial, MervBot stores useful player data internally as Player objects, see player.h for implementation details.

  • p->name = player name stored as char[20] (Note: SubSpace protocol allows for usernames to be 19+ in length, do not rely on this for player-name comparisions.)
  • p->squad = player squad stored as char[20]
  • p->ship = ship (0-9) enumerated as SHIP_Warbird, SHIP_Spectator, etc..
  • p->safety = whether ship is in safety zone (boolean)
  • p->bounty = player bounty
  • p->energy = player energy (have bot with *energy on to get accurate readings)
  • p->flagCount = how many flags player is holding
  • p->team = player frequency
  • p->(burst, repel, thor, brick, decoy, rocket, portal) = how many items of that type player has
  • p->(stealth, cloak, xradar, awarp, ufo, flash, safety, shields, supers) = if player has that item on (boolean)
  • p->score.killPoints = player kill points
  • p->score.flagPoints = player flag points
  • p->score.wins = player kills from f2
  • p->score.losses = player deaths from f2

Just access the respective member of the Player class to check the player's property.

For example, in spawn.cpp, to check whether a player is in a safety zone:

EVENT_PlayerMove:
{
   Player *p = (Player*)event.p[0];

   if ( p->safety ) // player is in safe zone.
     {
        // do something.
     }

   if ( !p->safety ) // player NOT in safe zone.
     {
        // do something.
     }
}

Obtaining a player pointer using name comparison

Since Player pointers are internal to MERVBot, it is necessary to find a way of obtaining a Player pointer from the identifying information given by the game. One of the simpler ways is just to compare the names after converting to lowercase.

Note: Using pilot names as vital comparisions should be used with caution. See Bot-Issues by CypherJF.

// return Player* info (or NULL if not found) from p->name info
Player * botInfo::GetPilot(char *name)
{
	// get pilot from a name, return as TempPlayer
	_listnode <Player> *parse = playerlist->head;

	//convert search name to lowercase
	char nname[20], pname[20];
	strncpy(nname, name, 20);
	tolower(nname);

	while (parse)
	{
		Player *p = parse->item;

		// convert to lowercase to compare
		strncpy(pname,p->name,20);
		tolower(pname);
		if (strcmp(pname,nname)==0)
			return p;
		
		parse = parse->next;
	}

	return NULL;	//player not found
}

Bot built in functions

Here are some useful MervBot commands to control what the bot is doing.

Player.cpp:

  • Player::move(Sint32 x, Sint32 y) moves a player to the coordinates specified by x and y
  • Player::clone(Player *p) clones a player into a player class

Look in Commands.txt , command.cpp (core), or /!help to bot to see all bot external commands (example /!go <arena>).

LVZ Object toggling commands in plugins are to go here.