|
|
(117 intermediate revisions by 9 users not shown) |
Line 1: |
Line 1: |
− | Here is the ever-popular MERVBot Tutorial by Underlord:
| + | 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]. |
| | | |
− | <br /><div align="center"><big><b><big>MERVBot Tutorial</big></b></big><br />
| + | ==Setting up a MERVBot (plugin)== |
− | <br />
| |
− | (Written by Underlord) | |
− | <hr width="500" size="1" /></div>
| |
| | | |
− | <font size="2"> </font><blockquote><font size="2">Setup a MERVBot bot and project in visual c++<br />
| + | [http://mervbot.com MERVBot download site] |
− | 0) <a href="#0">Setting up a MERVBot bot</a><br />
| + | |
− | <br />
| + | |
− | Command.cpp<br />
| + | ===Obtaining MERVBot=== |
− | 1)<a href="#1"> Player commands</a> | + | |
− | (!play, !squadA vs squadB)<br />
| + | * Download the [http://mervbot.com/files/MERVBot.rar latest build]. |
− | <br />
| + | * Unrar MERVBot.rar into a new folder. (example c:\program files\continuum\mervbot) |
− | Spawn.cpp<br />
| + | * Unzip src.zip into "src" subfolder of that new folder (example c:\program files\continuum\mervbot\src) |
− | 2) <a href="#2">Event descriptions</a> | + | |
− | (describe events in spawn.cpp)<br />
| + | ===Preparing to write a plugin=== |
− | 2) <a href="#2">Messaging </a> | + | |
− | ("*arena hi", ":player:*scorereset)<br />
| + | ''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 |
− | 3) <a href="#3">MervBot Timer</a> | + | |
− | (do this in 10 seconds)(countdown[n])<br />
| + | 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). |
− | 4) <a href="#4">Writing Functions</a> | + | |
− | (bool IsInCenter(Player *p))<br />
| + | ''File descriptions:'' |
− | <br />
| + | * spawn.h = declare/initialize globals |
− | Useful operations<br />
| + | * command.cpp = code for commands coming into bot (ie /!help, /!play, etc) |
− | 6) <a href="#6">Cycling players</a> | + | * spawn.cpp = code that interacts with bot spawns |
− | <br />
| + | |
− | 7) <a href="#7">Check if pilot is in safe zone</a> | + | ===Microsoft Visual c++=== |
− | <br />
| + | |
− | 8) <a href="#8">Random numbers</a> | + | <ol> |
− | <br />
| + | <li>Start Visual Studios 6.0. |
− | 9) <a href="#9">Time without using countdown[n]</a> | + | <li>Click the Drop Down Menu labeled "File" at the top left of your screen. |
− | <br />
| + | <li>Click "New". |
− | 10) <a href="#10">Storing data for pilots</a>
| + | <li>On the next screen that comes up, choose from the Project tab, then Win32 Dynamic-Link Library |
− | <br />
| + | <li>Select the "/src" folder as the base folder (example c:\program files\continuum\mervbot\src) |
− | 11) <a href="#11">Output data in messages</a>
| + | <li>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) |
− | <br />
| + | <li>Choose to create an "Empty DLL project". |
− | 12) <a href="#12">Input/Output to files</a>
| + | <li>Click "Finish". |
− | <br />
| + | <li>Click the Drop Down Menu labbled "Project". |
− | 13) <a href="#13">Programming commands</a>
| + | <li>Click "Add To Project Files" |
− | <br />
| + | <li>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) |
− | 14) <a href="#14">Useful player data</a><br />
| + | <li>Click the Drop Down Menu labelled "Build". |
− | 15) <a href="#15">Bot built in functions</a>
| + | <li>Click "Build (dll name)" - where (dll name) is "mybot" |
− | <br /><br />
| + | <li>Go into your "mybot" folder and look for a folder named "Debug" |
− | Example Code<br />
| + | (example c:\program files\continuum\mervbot\src\mybot\debug) |
− | </font><blockquote><font size="2">a) <a href="#15a">No antiwarp in center of map</a>
| + | <li>Your new DLL will be in that folder. (example mybot.dll) |
− | <br />
| + | <li>Copy mybot.dll to your base folder that has mervbot.exe in it (example c:\program files\continuum\mervbot) |
− | b) <a href="#15b">Setting freq size depending on how many pilots in game</a>
| + | </ol> |
− | <br />
| + | |
− | c) <a href="#15c">Tracking kills and announcing when pilot gets 10 kills in a row without dying </a> | + | |
− | <br />
| + | ===Run your bot dll=== |
− | d) <a href="#15d">Warp pilot to coord when they are in a certain region</a>
| + | |
− | <br />
| + | 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). |
− | e) <a href="#15e">Structures within structures</a>
| + | |
− | <br />
| + | <ol> |
− | f) <a href="#15f">Tracking flag data</a>
| + | <li>Edit spawns.txt. '''Read every word of spawns.txt to find out what needs to go in there.''' |
− | <br />
| + | |
− | g) <a href="#15g">Example way to do simple /!spam feature</a>
| + | <br> |
− | <br />
| + | ''Example:'' |
− | h) <a href="#15h">Example of implementing a simple stack to do "next in line for several 'boxes' at once"</a>
| + | <pre>2v2-Bot-League : botpw : 2v2a : 2v2league : staffpw</pre> |
− | <br />
| + | |
− | i) <a href="#15i">Example of reading any text from a .txt and printing it to pilot line by line</a>
| + | ''Note:'' The bot will attempt to create the name if it doesn't exist already. |
− | <br />
| + | |
− | j) <a href="#15j">Example of printing player stats grid</a>
| + | |
− | <br />
| + | <li>Edit MERVBot.ini |
− | k) <a href="#15k">Example of checking if any pilots are within a region</a>
| + | <br> |
− | <br />
| + | <br> |
− | l) <a href="#15l">Example of functions to get a pilot's struct id info from a name or *player info</a>
| + | <pre>[Login] |
− | <br />
| + | Zone=216.33.98.254:21000 // your zone IP:PORT available from zone.dat in Continuum dir |
− | m) <a href="#15m">Example of creating a logfile name using date and squad names</a>
| + | </pre> |
| | | |
− | <br />
| |
− | n) <a href="#15n">Sending messages to playing freqs or public and logging depending on status</a>
| |
− | <br />
| |
− | o) <a href="#15o">Example of reading in all player/freqs to struct data</a>
| |
− | <br />
| |
− | p) <a href="#15p">Example of finding MVP from struct data</a>
| |
− | <br />
| |
− | q) <a href="#15q">Print time stamp of event</a>
| |
− | <br />
| |
− | r) <a href="#15r">Simple way to track player bomb/bullet damage stats</a>
| |
− | <br />
| |
− | s) <a href="#15s">Simple way to print those stats</a><br />
| |
− | t) <a href="#15t">Make bot spectate specific coordinates</a>
| |
− | <br />
| |
− | </font></blockquote>
| |
− | </blockquote>
| |
− | <font size="2"> <br />
| |
− |
| |
− | </font><hr width="500" size="1" /><font size="2"><br />
| |
− | <br />
| |
− | <a name="0"></a>
| |
− | <b>[0] Setting up a MERVBot bot (plugin)</b><br />
| |
− |
| |
− | </font><blockquote><font size="2">MERVBot download site: <a href="http://catid.sscentral.com">http://catid.sscentral.com</a>
| |
− | <br />
| |
− | <br />
| |
− | MERVBot Forum: <a href="http://www.ssforum.net">http://www.ssforum.net</a>
| |
− | (Development - MervBot forum)<br />
| |
− | <br />
| |
− | 1) Download <a href="http://catid.sscentral.com/files/MERVBot.zip">MERVBot Build 37 (DLL 6.5)</a>
| |
− | <br />
| |
− | </font><blockquote><font size="2">Unzip MERVBot.zip into a new folder. (example c:program filescontinuummervbot)<br />
| |
− | Unzip src.zip into "/src" subfolder of that new folder (example c:program filescontinuummervbotsrc)<br />
| |
− | </font></blockquote>
| |
− | <font size="2">2) Download <a href="http://catid.sscentral.com/files/Tutorial.zip">DLL-plugin Tutorial</a>
| |
− | <br />
| |
− | </font><blockquote>
| |
− | <font size="2"> Unzip Tutorial.zip into "/tutorial" subfolder of that new folder. (example c:program filescontinuummervbotsrctutorial)<br />
| |
− | (note spawn.h, spawn.cpp, and command.cpp are here)<br />
| |
− | </font><blockquote><font size="2">File descriptions:<br />
| |
− | </font></blockquote>
| |
− | <font size="2"> </font><blockquote>
| |
− | <font size="2">spawn.h = declare/initialize globals<br />
| |
| | | |
| + | <li>Edit operators.txt. '''Read every word of operators.txt to find out what needs to go in there.'''<br /> |
| + | |
| + | ''Example:'' |
| + | <pre> |
| + | 4:my_name: |
| + | 4:another_sysop: |
| + | 3:other_person: |
| + | </pre> |
| | | |
| | | |
| + | <li>Make sure the bot is on vip.txt or has smod+ access, then run MERVBot.exe. |
| | | |
− | command.cpp = code for commands coming into bot (ie /!help, /!play, etc)<br /> | + | <li>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. |
| | | |
| + | </ol> |
| | | |
| + | ==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. |
| | | |
− | spawn.cpp = code for main part of bot<br />
| + | Example (makes bot reply to !test with "hi"): |
− | </font></blockquote>
| + | <pre> |
− | <font size="2"> </font></blockquote>
| + | void botInfo::gotCommand(Player *p, Command *c) { |
− | <font size="2"> </font><blockquote><font size="2">Note: if you only want to execute someone's premade
| + | switch (p->access) |
− | bot (.dll), skip to step 4 (Run your bot .dll), otherwise continue to learn
| + | { |
− | how to make your own bot<br />
| + | case OP_Moderator: |
− | </font></blockquote> | + | { |
− | <font size="2">3) Microsoft Visual c++<br />
| + | // handle moderator-operator commands here. |
− | </font><blockquote><font size="2">1) Start Visual Studios 6.0<br />
| + | } |
− | <br />
| + | 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 |
| + | } |
| + | } |
| + | </pre> |
| | | |
− | 2) Click the Drop Down Menu labbled "File" at the top left of your screen<br />
| + | ===How to have commands with numerical parameters=== |
− | <br />
| + | Example (!test #): |
| + | <pre> |
| + | if (c->check("test")) { // reads in test #, default to 1 if invalid number input |
| + | int temp = 1; |
| | | |
− | 3) Click "New"<br />
| + | if (isNumeric(c->final)) |
− | <br />
| + | temp = atoi(c->final); |
| + | </pre> |
| | | |
− | 4) On the next screen that comes up, choose from the Project tab, then Win32 Dynamic-Link Library<br />
| + | ===How to have player name as input=== |
− | <br />
| + | Example (!rank player): |
| + | <pre> |
| + | if (c->check("rank")) |
| + | { |
| + | String player_name = c->final; |
| | | |
− | 5) Select the "/src" folder as the base folder (example c:program filescontinuummervbotsrc)<br />
| + | if (player_name.IsEmpty()) // default name to self if invalid name |
− | <br />
| + | player_name = p->name; |
| + | </pre> |
| | | |
− | 6) Name your project "mybot". This will make a "mybot" subfolder in your "src" folder. Click OK<br />
| + | ===How to have multi-parameter input=== |
− | (example creates c:program filescontinuummervbotsrcmybot)<br />
| |
− | <br />
| |
| | | |
− | 7) Choose to create an "Empty DLL project"<br />
| + | Use the CRT function sscanf() to scan the string for the values. |
− | <br />
| |
| | | |
− | 8) Click "Finish"<br />
| + | Example (!squads squadA vs squadB ''or'' !squads teamA:squadA:teamB:squadB): |
− | <br />
| + | <pre> |
| + | else if (c->check("squads")) |
| + | { |
| + | char squadA[20], squadB[20]; |
| + | int teamA, teamB; |
| | | |
− | 9) Click the Drop Down Menu labbled "Project"<br />
| + | strncpy(squadA, "", 20); |
− | <br />
| + | strncpy(squadB, "", 20); |
| | | |
− | 10) Click "Add To Project Files"<br />
| + | int n_found; |
− | <br />
| |
− | 11) Copy only spawn.h, spawn.cpp, and command.cpp from the "tutorial" folder into the this new folder.<br />
| |
− | (example from c:program filescontinuummervbotsrctutorial to c:program filescontinuummervbotsrcmybot)<br />
| |
− | <br />
| |
− | 12) Click the Drop Down Menu labelled "Build"<br />
| |
− | <br />
| |
| | | |
− | 13) Click "Build (dll name)" - where the (dll name) is "mybot"<br />
| + | //Note: %[A-Za-z ] is equivalent to %s, but allows an internal space. |
− | <br />
| |
| | | |
− | 14) Go into your "mybot" folder and look for a folder named "Debug"<br />
| + | //scan the string for the two squads separated by " vs " |
− | (example c:program filescontinuummervbotsrcmybotdebug)<br /> | + | n_found = sscanf(c->final, "%[A-Za-z ] vs %[A-Za-z ]", squadA, squadB); |
− | <br />
| |
| | | |
− | 15) Your new DLL will be in that folder. (example mybot.dll)<br />
| + | //if that fails, scan the string for freqA:squadA:freqB:squadB |
− | <br />
| + | if (n_found < 2) |
− | 16) Copy mybot.dll to your base folder that has mervbot.exe in it (example c:program filescontinuummervbot)<br />
| + | sscanf(c->final, "%d:%[A-Za-z ]:%d:%[A-Za-z ]", &teamA, squadA, &teamB, squadB); |
− | </font></blockquote>
| + | } |
| + | </pre> |
| | | |
− | <font size="2"> 4) Run your bot dll:<br />
| + | ===Help Menu=== |
− | </font><blockquote><font size="2">To run your bot you need your .dll (mybot.dll), Commands.txt, MERVBot.exe,
| + | When a player sends !help to the bot, MERVBot calls botInfo::gotHelp() in each plugin loaded. |
− | MERVBot.ini, Operators.txt, Spawns.txt, subspace.bin, and zlib.dll all in
| + | <pre> |
− | one folder (example c:program filescontinuummervbot)<br />
| + | void botInfo::gotHelp(Player *p, Command *c) |
− | </font></blockquote>
| + | { |
− | <font size="2"> </font><blockquote>
| + | if (!*c->final) |
− | <font size="2"> Edit spawns.txt (only one line of text in file needed)<br />
| + | { |
| + | 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"); |
| + | </pre> |
| | | |
− |
| + | ==Event Calls== |
− | </font><blockquote><font size="2">bot_name : pw_for_bot_name : arena: dll_plugin : optional_staff_password<br />
| |
− | </font><blockquote>
| |
− | <font size="2">Example: 2v2-Bot-League : botpw: 2v2a: 2v2league : staffpw<br />
| |
− | <br />
| |
− | - You should create your bot name and pw using your Continuum client as you would create any new name. <br />
| |
− | <br />
| |
− | - There is no * in front of staffpw. <br />
| |
− | <br />
| |
− | - Bot needs to be on vip.txt or have moderator+ access to enter a zone.<br />
| |
| | | |
− | </font></blockquote>
| + | 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. |
− | <font size="2"> </font></blockquote>
| |
| | | |
− | <font size="2">Edit MERVBot.ini<br />
| + | 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(). |
− | </font><blockquote><font size="2">[Login]<br />
| |
− | Zone=216.33.98.254:21000 // make that your zone IP:PORT available from zone.dat in Continuum dir<br />
| |
− | </font></blockquote>
| |
| | | |
− | <font size="2"> | + | <pre>tell(makeFollowing(false));</pre> |
− | Edit operators.txt<br />
| |
− | </font><blockquote>
| |
− | <font size="2"> access_level : name :<br />
| |
| | | |
− |
| + | ==The Messaging System== |
− | </font><blockquote><font size="2">Example: <br />
| |
| | | |
− | 4:my_name:<br />
| + | Private message - void sendPrivate(Player *player, char *msg); |
| | | |
− | 4:another_sysop:<br />
| + | ''Examples:'' |
| | | |
− | 3:other_person:<br />
| + | <pre> |
| + | sendPrivate(p,"hi"); |
| | | |
− | </font></blockquote>
| + | String s="test"; |
− | <font size="2"> </font><blockquote><font size="2"> </font></blockquote>
| + | sendPrivate(p,s); |
| | | |
− | <font size="2"> </font></blockquote>
| + | String s="test"; |
| + | s += "ing"; |
| + | sendPrivate(p,s); |
| | | |
− | <font size="2"> Run MERVBot.exe<br />
| + | char captain1[20]; |
− | </font><blockquote>
| + | char captain2[20]; |
− | <font size="2"> Double click MERVBot.exe, the bot should now enter the zone<br />
| + | strncpy(captain1,"",20); |
− | </font></blockquote>
| + | strncpy(captain2,"",20); |
− | <font size="2"> </font></blockquote>
| + | sendPrivate(p,(String) captain1 + " and " + (String) captain2 + " are the captains."); |
− | <font size="2"> </font></blockquote>
| + | </pre> |
− | <font size="2"> </font><blockquote>
| |
− | <font size="2"> </font><blockquote><font size="2">Modify your MERVBot code<br />
| |
− | </font><blockquote><font size="2">You can open your MERVBot project by opening "mybot.dsw" file with visual c++<br />
| |
− | (example c:program filescontinuummervbotsrcmybotmybot.dsw)<br />
| |
− | <br />
| |
− | Edit the spawn.h, spawn.cpp, and command.cpp to create your bot, then build,
| |
− | copy your updated dll to your MERVBot.exe folder and then execute the bot.<br />
| |
− | <br />
| |
− | Use the tutorial to get ideas on how to implement certain types of features into the bot.<br />
| |
− | </font></blockquote>
| |
− | <font size="2"> </font></blockquote>
| |
− | <font size="2"> </font></blockquote>
| |
− | <font size="2"><br />
| |
− | <a name="1"></a>
| |
− | <b>[1] Player Commands - (command.cpp)</b><br />
| |
− | </font><blockquote>
| |
− | <font size="2"> </font><div align="left"><font size="2">This section describes how to implement player commands into MERVBot <br />
| |
− | (example: if you want /!test to make the bot respond with "hi")<br />
| |
− | <br />
| |
− | In command.cpp<br />
| |
− | </font><blockquote><font size="2">void botInfo::gotCommand(Player *p, Command *c) {<br />
| |
− | switch (p->access) {<br />
| |
− | case OP_Player:<br />
| |
− | {<br />
| |
− | if (c->check("test"))<br />
| |
− | {<br />
| |
− | sendPrivate(p,"hi");<br />
| |
− | }<br />
| |
− | }<br />
| |
− | </font></blockquote>
| |
− | <font size="2">You just put your player command where 'test' is, then what you want that command to do.<br />
| |
− | If you want the command to be for moderators you put it in the OP_Moderator section instead<br />
| |
− | </font><blockquote><font size="2">case OP_Moderator:<br />
| |
− | { // Moderator-level commands<br />
| |
− | }<br />
| |
− | </font></blockquote>
| |
− | <font size="2"> // Note: Moderators
| |
− | can do Player commands, SuperModerator can do Moderator and Player commands,
| |
− | etc<br />
| |
− | <br />
| |
− | How to have commands with numerical input - (ie !test 1)<br />
| |
− | </font><blockquote><font size="2"> if (c->check("test")) { // reads in test #, default to 1 if invalid number input<br />
| |
− | <br />
| |
− | int temp = 1;<br />
| |
− | <br />
| |
− | if (isNumeric(c->final))<br />
| |
− | temp = atoi(c->final);<br />
| |
− | </font></blockquote>
| |
− | <font size="2"> <br />
| |
− | How to have player name as input - (ie !rank player)<br />
| |
− | </font><blockquote><font size="2"> if (c->check("rank")) {<br />
| |
− | <br />
| |
− | String player_name = c->final;<br />
| |
− | <br />
| |
− | if (player_name.IsEmpty()) // default name to self if invalid name<br />
| |
− | player_name = p->name;<br />
| |
− | </font></blockquote>
| |
− | <font size="2"> <br />
| |
− | How to have complicated input - (ie !squads squadA vs squadB _OR_ !squads teamA:squadA:teamB:squadB)<br />
| |
− | <br />
| |
− | you parse the c->final message
| |
− | looking for identifiers, then divide the string to get the values<br />
| |
− | <br />
| |
− | else if (c->check("squads"))<br />
| |
− | {<br />
| |
− | strncpy(squadA, "", 20);<br />
| |
− | strncpy(squadB, "", 20);<br />
| |
− | <br />
| |
− | char s[256];<br />
| |
− | strncpy(s,c->final,255);<br />
| |
− | <br />
| |
− | int l= strlen(s) - 1;<br />
| |
− | <br />
| |
− | for (int i=0; i<l; i++)<br />
| |
− | {<br />
| |
− | // team1 vs team2<br />
| |
− |
| |
− | if ((s[i]==' ') && (s[i+1]=='v') && (s[i+2]=='s') &&
| |
− | (s[i+3]==' '))<br />
| |
− | {<br />
| |
− | s[i]=0;<br />
| |
− | strncpy(squadA, s,20);<br />
| |
− | s[i]=' ';<br />
| |
− |
| |
− | strncpy(squadB, &(s[i+4]),20);<br />
| |
− | break;<br />
| |
− | }<br />
| |
− | <br />
| |
− | // freq1:team1:freq2:team2<br />
| |
− | if (s[i] == ':')<br />
| |
− | {<br />
| |
− | char s2[256];<br />
| |
− |
| |
− | teamA = atoi(s);<br />
| |
− |
| |
− | strncpy(s2,&(s[i+1]),255);<br />
| |
− | <br />
| |
− |
| |
− | for (int j=0; j < strlen(s2); j++)<br />
| |
− |
| |
− | if (s2[j] == ':')<br />
| |
− |
| |
− | {<br />
| |
− |
| |
− | char s3[256];<br />
| |
− | <br />
| |
− |
| |
− | s2[j]=0;<br />
| |
− |
| |
− | strncpy(squadA,s2,20);<br />
| |
− |
| |
− | s2[j]=':';<br />
| |
− |
| |
− | <br />
| |
− |
| |
− | strncpy(s3,&(s2[j+1]),255);<br />
| |
− | <br />
| |
− |
| |
− | for (int k=0;
| |
− | k<strlen(s3); k++)<br />
| |
− |
| |
− |
| |
− | if (s3[k]==':')<br />
| |
− |
| |
− |
| |
− | {
| |
− | <br />
| |
− |
| |
− |
| |
− | teamB = atoi(s3);<br />
| |
− |
| |
− |
| |
− | strncpy(squadB,&(s3[k+1]),20);<br />
| |
− |
| |
− |
| |
− | break;<br />
| |
− |
| |
− |
| |
− | }<br />
| |
− |
| |
− | break;<br />
| |
− |
| |
− | }<br />
| |
− | break;<br />
| |
− | }<br />
| |
− | }<br />
| |
− | </font></div>
| |
− | <font size="2"> </font></blockquote>
| |
− | <font size="2"> </font><blockquote><font size="2">// Example of a help menu (!help) spawn.h<br />
| |
− | </font><blockquote><font size="2">void botInfo::gotHelp(Player *p, Command *c)<br />
| |
− | {<br />
| |
− | if (!*c->final)<br />
| |
− | {<br />
| |
− | sendPrivate(p, "4v4 Bot General Commands:");<br />
| |
− | sendPrivate(p, "------------------------");<br />
| |
− | sendPrivate(p, "!caps
| |
− | - get captain names");<br />
| |
− | sendPrivate(p, "!roster <squad> - get roster of a squad");<br />
| |
− | sendPrivate(p, "!schedule
| |
− | - get current schedule");<br /> sendPrivate(p,
| |
− | "!score
| |
− | - get current score");<br />
| |
− | <br />
| |
− | </font></blockquote>
| |
− | <font size="2"> </font></blockquote>
| |
− | <font size="2"><a name="2"></a>
| |
− | <b>[2] Event Call Descriptions - in spawn.cpp</b><br />
| |
− | </font><blockquote><font size="2">MERVBot is event based, so when making a bot you need to decide what will happen at certain events<br />
| |
− | Normal plugins need to consider what happens when bot enters arena, player enters arena, player leaves arena, <br />
| |
− | player events like kill, shipchange, teamchange, spec, move then any other relevent events to your bot<br />
| |
− | just worry about events that are relevent to the tasks your bot is doing<br />
| |
− | <br />
| |
− | // Timer<br />
| |
| | | |
− | case EVENT_Tick: - called 1x/sec, decrementing each countdown[n] by 1, use for time triggered events<br />
| + | Team message - void sendTeamPrivate(Uint16 team, char *msg);<br /> |
− | <br />
| + | <blockquote>Examples: <br /> |
| + | a) sendTeamPrivate(8025,"hi spec freq");<br /> |
| + | b) Uint16 test=0; sendTeamPrivate(test,"hi freq 0");<br /> |
| + | </blockquote> |
| | | |
− | // Arena Events<br /> | + | Public message - void sendPublic(char *msg);<br /> |
| + | <blockquote>Example: sendPublic("*arena " + (String) p->name + " is now a captain");<br /> |
| + | </blockquote> |
| + | Chat channel message - void sendChannel(char *msg);<br /> |
| + | <blockquote>Example: sendChannel("hi chat channel");<br /> |
| + | </blockquote> |
| + | Remote private message - void sendRemotePrivate(char *name, char *msg);<br /> |
| + | <blockquote>Example: sendRemotePrivate("Player01", "hi");<br /> |
| + | </blockquote> |
| | | |
− | case EVENT_ArenaEnter: - called when bot enters arena<br />
| + | 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 |
| | | |
− | case EVENT_ArenaSettings: - called when bot gets arena settings<br />
| + | ===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 |
| | | |
− | case EVENT_ArenaLeave: - called when bot exits arena<br />
| + | String s = "*arena "; |
| + | s += temp; |
| + | s += " pilots left in the game."; |
| | | |
− | case EVENT_ArenaListEntry: - called when bot gets an entry from the arena list (esc-a)<br />
| + | sendPublic(s); |
| + | </pre> |
| + | Or, |
| + | <pre> |
| + | //NOTE: this can be considered inefficient. |
| | | |
− | case EVENT_ArenaListEnd: - called when bot reads last entry of arena list (esc-a)<br />
| + | sendPublic("*arena " + (String)temp + " pilots left in the game"); |
− | <br />
| + | </pre> |
| | | |
− | // Flag Events<br /> | + | <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 |
| | | |
− | case EVENT_FlagGrab: - called when a pilot picks up a loose flag<br />
| + | char str[255]; |
| + | sendPublic("*arena--------------------------------------------------------------------------------"); |
| | | |
− | case EVENT_FlagDrop: - called when a pilot drops a flag (timer runs out)<br />
| + | sprintf(str, "*arena Squad: %-20s PTS FPTS K D DMG DEALT TAKEN F FK FLT", |
| + | freqs[freq].freqname |
| + | ); |
| | | |
− | case EVENT_FlagMove: - called when a flag moves<br />
| + | sendPublic(str); |
| | | |
− | case EVENT_FlagVictory: - called when a team wins flag game (gets all flags)<br />
| + | sendPublic("*arena--------------------------------------------------------------------------------"); |
| | | |
− | case EVENT_FlagReward: - called when theres a periodic flag reward given<br />
| + | // assuming existing freqs struct with data |
− | <br />
| + | for (pilot=freqs[freq].playercount-1; pilot>=0; pilot--) |
| + | { |
| + | // on freq squad so print stats |
| + | char outString[255]; |
| | | |
− | // Timed Game<br /> | + | 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); |
| + | } |
| | | |
− | case EVENT_TimedGameOver: - called when a timed game is over (like speed zone game)<br />
| + | // Notes: sprintf format = sprintf(output char string, spacing, variables) |
− | <br />
| + | // 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> |
| | | |
− | // Soccer<br />
| + | ==Time== |
| | | |
− | case EVENT_SoccerGoal: - called when a soccer goal is scored<br />
| + | 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. |
| | | |
− | case EVENT_BallMove: - called when a soccer ball moves<br />
| + | Setup number of timers and initialize in spawn.h: |
− | <br />
| + | <pre> |
| + | class botInfo |
| + | { |
| + | #define COUNTDOWNS 10 // how many countdowns you want |
| + | int countdown[COUNTDOWNS]; // this gives you 10 timers |
| | | |
− | // Receive file<br /> | + | // unrelated code |
| + | |
| + | public: |
| + | botInfo(CALL_HANDLE given) |
| + | { |
| + | countdown[0] = 0; |
| + | countdown[1] = 60; // 60 seconds |
| + | // |
| + | // initialize values |
| + | // |
| + | countdown[9] = 5*60; // 5 minutes |
| + | </pre> |
| + | Using timer functions in spawn.cpp: |
| + | <pre> |
| + | 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. |
| | | |
− | case EVENT_File: - called when a file is received (*getfile)<br />
| + | if (countdown[1] == 2) // when timer #1 hits two seconds |
− | <br />
| + | { |
| + | // 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 |
| + | } |
| + | </pre> |
| | | |
− | // Player events<br />
| + | 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. |
| | | |
− | case EVENT_PlayerEntering: - called when a pilot enters the arena<br />
| + | === Tracking time not using countdown[n] === |
| | | |
− | case EVENT_PlayerMove: - called when a pilot's ship moves<br />
| + | 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. |
| | | |
− | case EVENT_PlayerWeapon: - called when a pilot fires a weapon<br />
| + | Lucky for us, Windows provides a function called GetTickCount() that is a measurement of time (milliseconds) that we can use for such cases. |
− | case EVENT_WatchDamage: - called when
| |
− | a pilot takes damage (bot must have /*watchdamage on for them)<br /> | |
| | | |
− | case EVENT_PlayerDeath: - called when a pilot dies<br />
| + | So: |
| + | <pre> |
| + | int begin = GetTickCount(); |
| | | |
− | case EVENT_PlayerScore: - called when a pilot changes their score (?scorereset)<br />
| + | // do some code here. |
| | | |
− | case EVENT_PlayerPrize: - called when a pilot gets a prize (green)<br />
| + | int end = GetTickCount(); |
| + | |
| + | int delta = (end - begin) / 1000; // elapsed time converted to seconds from milliseconds |
| + | </pre> |
| | | |
− | case EVENT_PlayerShip: - called when a pilot ship changes<br />
| + | === Obtaining the current time === |
| | | |
− | case EVENT_PlayerSpec: - called when a pilot spectates<br />
| + | ''Requirements:'' Include <time.h>. |
| | | |
− | case EVENT_PlayerTeam: - called when a pilot changes freqs<br />
| + | 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> |
| | | |
− | case EVENT_PlayerLeaving: - called when a pilot exits arena (esc-q, ?go <arena>, or lagout)<br />
| + | ==Writing Functions== |
− | <br />
| + | <!-- begin of 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: |
| + | <pre> |
| + | //////// DLL "import" //////// |
| | | |
− | // Bot events<br /> | + | 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); |
| + | } |
| + | </pre> |
| | | |
− | case EVENT_SelfShipReset: - called when bot's ship gets reset<br />
| |
| | | |
− | case EVENT_SelfPrize: - called when bot gets a prize (green)<br />
| + | 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. |
| + | <pre> |
| + | //////// DLL "import" //////// |
| | | |
− | case EVENT_SelfUFO: - called when bot toggles UFO mode<br />
| + | bool botInfo::closeto(Player *p, int x, int y, int tolerance) |
| + | { |
| + | ... |
| + | } |
| + | </pre> |
| + | In '''spawn.h''', add your method's prototype without botInfo::, it will look |
| + | like this: |
| + | <pre> |
| + | //... |
| + | botInfo(CALL_HANDLE given) |
| + | { |
| + | // ... |
| + | } |
| + | bool closeto(Player *p, int x, int y, int tolerance); // Your function prototype. |
| | | |
− | case EVENT_PositionHook: - called when "Core is requesting the DLL to send a position packet"<br />
| + | void clear_objects(); //provided by Catid, and already exists. |
− | <br />
| + | void object_target(Player *p); //provided by Catid, and already exists. |
| + | // ... |
| + | }; |
| + | </pre> |
| + | If you're not familiar with prototypes, notice it is similar to that in your spawn.cpp, but without the botInfo::, and a trailing ;. |
| | | |
− | // Brick<br />
| + | ===Function notes=== |
| | | |
− | case EVENT_BrickDropped: - called when a pilot uses a brick<br />
| + | Remember that you can pass variables [http://www.cplusplus.com/doc/tutorial/tut2-3.html by reference]. If variables are passed by reference, any changes a function makes to the variables will remain after the function returns. |
− | <br />
| |
| | | |
− | // Objects<br /> | + | From time to time you will need to pass an array to a function. An example illustrating this is: |
| + | <pre> |
| + | int freqs[5]; // declare our example data. |
| | | |
− | case EVENT_ObjectToggled: - called when a .lvz object is toggled on/off<br />
| + | // call function - notice freqs and not freqs[5] or freqs[]. |
− | <br />
| + | my_function(freqs); //You're not passing the array itself, just a pointer to the array. |
| | | |
− | // Turrets<br /> | + | // function - notice freqs[] and not freqs[5] or freqs |
| + | void my_function(int freqs[]) {} //You're specifying that the freqs parameter is an array |
| + | </pre> |
| | | |
− | case EVENT_CreateTurret: - called when a pilot attaches to another pilot (f7)<br />
| + | ==Cycling through players== |
| | | |
− | case EVENT_DeleteTurret: - called when a pilot detaches from a turret<br />
| + | 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. |
− | <br />
| |
| | | |
− | // Messages<br /> | + | 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: |
| + | <pre> |
| + | _listnode <Player> *parse = playerlist->head; //set to first link of the player linked list |
| | | |
− | case EVENT_Chat: - called when bot gets a chat message<br />
| + | 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 |
| | | |
− | case EVENT_LocalCommand: - called when bot gets a player command from arena<br />
| + | // 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 |
| | | |
− | case EVENT_LocalHelp: - called when bot gets a player !help command<br />
| + | parse = parse->next; //set parse to the next link |
| + | } |
| + | </pre> |
| | | |
− | case EVENT_RemoteCommand: - called when bot gets a command from outside arena<br />
| + | 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: |
| + | <pre> |
| + | void handleCmdSetShip(enum Ship_Types ship); |
| + | </pre> |
| | | |
− | case EVENT_RemoteHelp: - called when bot gets a player !help command from outside arena<br />
| + | In spawn.cpp add: |
− | <br />
| + | <pre> |
| + | 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. |
| | | |
− | case EVENT_Init: - called when DLL is loaded<br />
| + | _listnode <Player> *parse = playerlist->head; |
| + | while (parse) |
| + | { |
| + | Player *p = parse->item; |
| | | |
− | case EVENT_Term: - called when DLL is unloaded<br />
| + | if ( p->ship != ship && p->ship != SHIP_Spectator ) |
− | </font></blockquote>
| + | sendPrivate(p, "*setship " + (String)ship); |
− | <font size="2"> <br />
| |
− | <a name="3"></a>
| |
− | <b>[3] Messaging - How to use the messaging system</b><br />
| |
− |
| |
− | </font><blockquote><font size="2">Private message - void sendPrivate(Player *player, char *msg);<br />
| |
− | </font><blockquote><font size="2">Examples:<br />
| |
− | a) sendPrivate(p,"hi");<br />
| |
− | b) String s="test"; sendPrivate(p,s);<br />
| |
− | c) String s="test"; s += "ing"; sendPrivate(p,s);<br />
| |
− | </font></blockquote>
| |
− | <font size="2"> </font><blockquote><font size="2">d) char captain1[20]; char captain2[20];<br />
| |
− | strncpy(captain1,"",20); strncpy(captain2,"",20);<br />
| |
− | sendPrivate(p,(String) captain1 + " and " + (String) captain2 + " are the captains.");<br />
| |
− | </font></blockquote>
| |
− | <font size="2"> </font><blockquote>
| |
− | <font size="2"> </font></blockquote>
| |
| | | |
− | <font size="2"> </font><blockquote><font size="2">Team message - void sendTeamPrivate(Uint16 team, char *msg);<br />
| + | parse = parse->next; |
− | </font><blockquote><font size="2">Examples: <br />
| + | } |
− | a) sendTeamPrivate(8025,"hi spec freq");<br />
| + | } |
− | b) Uint16 test=0; sendTeamPrivate(test,"hi freq 0");<br />
| + | </pre> |
− | </font></blockquote>
| |
− | <font size="2"> </font></blockquote>
| |
− | <font size="2"> </font><blockquote><font size="2">Public message - void sendPublic(char *msg);<br />
| |
− | </font><blockquote><font size="2">Example: sendPublic("*arena " + (String) p->name + " is now a captain");<br />
| |
− | </font></blockquote>
| |
− | <font size="2">Chat channel message - void sendChannel(char *msg);<br />
| |
− | </font><blockquote><font size="2">Example: sendChannel("hi chat channel");<br />
| |
− | </font></blockquote>
| |
− | <font size="2">Remote private message - void sendRemotePrivate(char *name, char *msg);<br />
| |
− | </font><blockquote><font size="2">Example: sendRemotePrivate("Player01", "hi");<br />
| |
− | </font></blockquote>
| |
− | <font size="2"> </font></blockquote>
| |
− | <font size="2"> </font><blockquote><font size="2">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<br />
| |
− | </font></blockquote>
| |
| | | |
− | <font size="2"> <br />
| + | To use, just call the function with the appropriate Ship_Type from the enum in clientprot.h: |
− | <a name="4"></a>
| + | <pre> |
− | <b>[4] Timer - How to use the timing function</b><br />
| + | handleCmdSetShip(SHIP_Warbird); |
− |
| + | </pre> |
− | </font><blockquote><font size="2">Setup number of timers and initialize:<br />
| |
− | </font><blockquote><font size="2">// spawn.h - specify how many timers<br />
| |
− | </font></blockquote>
| |
− | <font size="2"> </font><blockquote><font size="2">class botInfo<br />
| |
− | {<br />
| |
− | int countdown[10]; // this gives you 10 timers</font></blockquote>
| |
− | <font size="2"> </font><blockquote><font size="2"> // spawn.h - initialize timers<br />
| |
− | <br />
| |
− | public:<br />
| |
− | botInfo(CALL_HANDLE given)<br />
| |
− | {<br />
| |
− | countdown[0] = 0;<br />
| |
− | countdown[1] = 60; // 60 seconds<br />
| |
− | //<br />
| |
− | // initialize values<br />
| |
− | //<br />
| |
− | countdown[9] = 5*60; // 5 minutes<br />
| |
− | </font></blockquote>
| |
− | <font size="2"> Using timer functions:<br />
| |
− | </font><blockquote><font size="2"> // in spawn.cpp<br />
| |
− | <br />
| |
− | case EVENT_Tick:<br />
| |
− | {<br />
| |
− | for (int i = 0; i < 10; ++i)<br />
| |
− | --countdown[i];<br />
| |
− | <br />
| |
− | if (countdown[1] == 2) // when timer #1 hits two seconds<br />
| |
− | {<br />
| |
− | // do stuff here when timer #1 hits 2 seconds<br />
| |
− | // example: sendPublic("two seconds left, setting timer to 1 minute");<br />
| |
− | // example: countdown[1] = 60; // change timer #1 value<br />
| |
− | }<br />
| |
− | </font></blockquote>
| |
− | <font size="2"> </font><blockquote><font size="2"> Notes: - In the "for" loop, you want "i < 10" to be the number of timers from spawn.h<br />
| |
− | - The timer is in seconds. EVENT_Tick is called once per second.<br />
| |
− | - The timers decrease by one each second down, not stopping at 0.<br />
| |
− | <br />
| |
− | - You can change the value of the countdown[n]'s in other parts of the program<br />
| |
− | (ie. case EVENT_PlayerDeath: { countdown[1] = 45; // set timer #1 to 45 sec<br />
| |
− | everytime someone dies)<br />
| |
− | </font></blockquote>
| |
− | <font size="2"> </font></blockquote>
| |
| | | |
− | <font size="2"> <br />
| + | ==Random numbers== |
− | <a name="5"></a>
| |
− | <b>[5] Writing Functions</b><br />
| |
− | </font><blockquote><blockquote><font size="2">Write your function in spawn.cpp at top of file in the //////// DLL "import" //////// section
| |
− | </font><blockquote><font size="2"> //////// DLL "import" ////////<br />
| |
− | <br />
| |
| | | |
− | bool closeto(Player *p, int x, int y, int tolerance) // note uses abs() function declared elsewhere<br />
| + | ===Generating a random number=== |
− | {<br />
| |
− | // return if player
| |
− | p is in area of square with center x,y and radius = tolerance<br />
| |
| | | |
− | return (abs((p->tile.x) - x) <
| + | To use this method, these two includes must be used: |
− | tolerance) && (abs((p->tile.y) - y) < tolerance);<br />
| + | <pre> |
| + | #include "time.h" //provides time() function. |
| + | #include "stdlib.h" //provides srand() and rand() functions. |
| + | </pre> |
| | | |
− |
| + | Use: |
− | }<br />
| + | <pre> |
− | </font></blockquote><font size="2"> If
| + | srand(time(NULL)); // seed random number generator. |
− | you want your function to have access to data from the spawn.h botInfo section<br />
| |
− | </font><blockquote><font size="2">public:<br />
| |
− | botInfo(CALL_HANDLE given)<br />
| |
− | {<br />
| |
− | <br />
| |
− | // then you need to make your spawn.cpp function in the format:<br />
| |
− | <br />
| |
− | bool botInfo::closeto(Player *p, int x, int y, int tolerance)<br />
| |
− | {<br />
| |
− | // function here<br />
| |
− | }<br />
| |
− | <br />
| |
− | Include function in spawn.h at bottom of file with the other functions<br />
| |
− | </font><blockquote><font size="2"> public:<br />
| |
| | | |
− | botInfo(CALL_HANDLE given)<br />
| + | rand(); // randomize. |
| | | |
− | {<br />
| + | 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 |
| + | </pre> |
| | | |
− | }<br />
| + | ===Picking a random pilot=== |
− | <br />
| |
| | | |
− | bool closeto(Player *p, int x, int y, int tolerance); <br />
| + | ''Note:'' A required user-defined function, getInGame(), must be created for this example to work. |
− | // your new function. note: w/o botInfo keyword and with a ;<br />
| |
− | <br />
| |
| | | |
− | void clear_objects(); // functions already there<br />
| + | <pre> |
| + | int temp = GetTickCount() % getInGame(); // getInGame() = how many pilots in arena |
| | | |
− | void object_target(Player *p); // functions already there</font></blockquote>
| + | Player *rabbit = NULL; |
| | | |
− | <font size="2"> </font></blockquote> | + | _listnode <Player> *parse = playerlist->head; |
− | <font size="2"> </font></blockquote>
| + | while (parse) |
− | <font size="2"> </font><blockquote><font size="2">Pass values by reference
| + | { |
− | </font><blockquote><font size="2">bool botInfo::closeto(Player *p, int& x, int y, int tolerance) // note the &<br />
| + | Player *p = parse->item; |
− | <br />
| |
− | bool is_close = closeto(p,x,y,tolerance); // if x is changed in closeto() its saved here <br />
| |
− | </font></blockquote>
| |
− | <font size="2"> </font></blockquote>
| |
| | | |
− | <font size="2"> </font><blockquote><font size="2">Pass array as parameter<br />
| + | if (p->ship != SHIP_Spectator) // if player is not a spectator |
− | </font><blockquote><font size="2">int freqs[5];<br />
| + | if ( !(--temp) ) // and if we've hit the randomly-selected pilot |
− | <br />
| + | { |
− | my_function(freqs); // call function - notice freqs and not freqs[5] or freqs[]<br />
| + | rabbit = p; |
− | <br />
| + | break; |
− | void my_function(int freqs[]) {} // function - notice freqs[] and not freqs[5] or freqs<br />
| + | } |
− | </font></blockquote>
| + | parse = parse->next; |
− | <font size="2"> </font></blockquote>
| + | } |
− | <font size="2"><a name="6"></a>
| + | </pre> |
− | <b>[6] Cycling players- How to search through the players in the arena</b><br />
| |
− | </font><blockquote><font size="2"> _listnode <Player> *parse = playerlist->head;<br />
| |
− | <br />
| |
− | while (parse)<br />
| |
− | {<br />
| |
− | Player *p = parse->item;<br />
| |
− | <br />
| |
− | // do functionality here<br />
| |
− | // Example 1: sendPrivate(p,"*watchdamage"); // turns on all pilot's watchdamage<br />
| |
− | // Example 2: if (p->safety != 0)
| |
− | sendPrivate(p,"*spec"); // spec all pilots in safe zone<br />
| |
− | <br />
| |
− | parse = parse->next<br />
| |
− | }<br />
| |
− | </font></blockquote>
| |
− | <font size="2"> <br />
| |
− | <a name="7"></a><b>
| |
− | [7] Checking if pilot is in a safe zone</b><br />
| |
− | </font><blockquote><font size="2"> if (p->safety != 0) // pilot is in a safe zone<br />
| |
− | if (p->safety == 0) // pilot is NOT in a safe zone<br />
| |
− | <br />
| |
− | To detect a pilot entering a safe zone do:<br />
| |
− | <br />
| |
− | case EVENT_PlayerMove:<br />
| |
− | {<br />
| |
− | if (p->safety != 0)<br />
| |
− | </font></blockquote><font size="2"><br />
| |
− | <a name="8"></a>
| |
− | <b>[8] Random number </b><br />
| |
− | </font><blockquote><font size="2">Method 1 (completely random)<br />
| |
− | </font><blockquote><font size="2"> #include "time.h"<br />
| |
− | <br />
| |
− | srand(time(NULL));<br />
| |
− | rand();<br />
| |
− | int temp = (int) (51 * ((float)rand()/RAND_MAX)); // returns a random integer between 0 and 51<br />
| |
− | </font></blockquote>
| |
− | <font size="2"> <br />
| |
− | Method 2 (random pilot in arena)<br />
| |
− | </font><blockquote><font size="2"> #include <stdlib.h><br />
| |
− | <br />
| |
− | int temp = GetTickCount() % getIngame(); // getIngame() = how many pilots in arena<br />
| |
− | <br />
| |
− | _listnode <Player> *parse = playerlist->head;<br />
| |
− | while (parse)<br />
| |
− | {<br />
| |
− | Player *p = parse->item;<br />
| |
− | <br />
| |
− | if (p->ship != SHIP_Spectator){ // if not in spec<br />
| |
− | if (!(--temp) { // decrement temp, if its 0, make this pilot rabbit<br />
| |
− | rabbit = p;<br />
| |
− | break;<br />
| |
− | }}<br />
| |
− | parse = parse->next;<br />
| |
− | }<br />
| |
− | </font></blockquote>
| |
− | <font size="2"> </font></blockquote>
| |
− | <font size="2"> <br />
| |
− | <a name="9"></a>
| |
− | <b>[9] Tracking time not using countdown[n] </b><br />
| |
− | </font><blockquote><font size="2"> #include <stdlib.h><br />
| |
− | <br />
| |
− | int temp = GetTickCount(); // get time stamp<br />
| |
− | // later in program<br />
| |
− | temp = (GetTickCount() - temp)/1000; // how many seconds have passed<br />
| |
− | </font></blockquote>
| |
− | <font size="2"> <br />
| |
− | <a name="10"></a><b>
| |
− | [10] Storing data for pilots</b><br />
| |
− | </font><blockquote><font size="2">There are several ways to store data for pilots (ie tracking flagtime or kills in a period of time)<br />
| |
− | <br />
| |
− | 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<br />
| |
− | <br />
| |
− | 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<br />
| |
− | <br />
| |
− | 3) structs - use to track all pilots even if they leave, (advantage -
| |
− | easier to sort by freqs), have to specify bound of players<br />
| |
− | <br />
| |
− | note: 2 and 3 are similar in effect, mostly the difference is in how you are able to search through data<br />
| |
− | you need to decide which method of storing data is best for each bot depending on what it does<br />
| |
− | 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<br />
| |
− | <br />
| |
| | | |
− | // initialize values in spawn.h at very top<br />
| + | ==Storing data for pilots== |
− | <br />
| |
− | 1) Built in get/setTag method</font></blockquote><blockquote><blockquote><font size="2"> #define DMG_DEALT 0<br />
| |
− | #define DMG_TAKEN 1<br />
| |
− | <br />
| |
− | // in spawn.cpp initialize the values on arena-enter and player-enter<br />
| |
− | <br />
| |
− | case EVENT_ArenaEnter: {<br />
| |
− | <br />
| |
− | _listnode <Player> *parse = playerlist->head;<br />
| |
− | <br />
| |
− | while (parse) // do for all pilots in arena when bot enters<br />
| |
− | {<br />
| |
− | Player *p = parse->item; // get pilot<br />
| |
− | <br />
| |
− | set_tag(p, DMG_DEALT, 0); // initialize to 0<br />
| |
− | set_tag(p, DMG_TAKEN, 0);<br />
| |
− | <br />
| |
− | sendPrivate(p,"*watchdamage");
| |
− | // optionally turn on player *watchdamage<br />
| |
− | <br />
| |
− | parse = parse->next; // get next pilot<br />
| |
− | }<br />
| |
− | }<br />
| |
− | <br />
| |
− | case EVENT_PlayerEntering: {<br />
| |
− | <br />
| |
− | set_tag(p, DMG_DEALT, 0); // initialize to 0<br />
| |
− | set_tag(p, DMG_TAKEN, 0);<br />
| |
− | <br />
| |
− | sendPrivate(p,"*watchdamage");<br />
| |
− | <br />
| |
− | // then somewhere edit the tag values<br />
| |
− | <br />
| |
− | case EVENT_WatchDamage: {<br />
| |
− | <br />
| |
− | // sets tag for k (shooter) to be old value plus damage dealt currently<br />
| |
− | set_tag(k, DMG_BOMB_DEALT, get_tag(k, DMG_BOMB_DEALT) + damage);<br />
| |
− | <br />
| |
− | // how to retrieve the tag values<br />
| |
− | <br />
| |
− | // as a command in spawn.h<br />
| |
− | <br />
| |
− | else if (c->check("showstats")) {<br />
| |
− | <br />
| |
− | int temp = get_tag(p, DMG_TOTAL_DEALT);<br />
| |
− | <br />
| |
− | String s = "You've done ";<br />
| |
− | s += temp;<br />
| |
− | s += " damage so far!";<br />
| |
− | <br />
| |
− | sendPrivate(p,s);<br />
| |
− | <br />
| |
− | // kill tags when player leaves arena<br />
| |
− | <br />
| |
− | case EVENT_PlayerLeaving: {<br />
| |
− | <br />
| |
− | killTags(p);<br />
| |
− | </font></blockquote><font size="2">2) Modified permanent get/setTag method<br />
| |
− | </font><blockquote><font size="2">// same as get/setTag with some modifications to the tag code, then can use tags exactly as above<br />
| |
− | <br />
| |
− | // spawn.h, add char name[20]; into struct PlayerTag<br />
| |
− | </font><blockquote><font size="2">struct PlayerTag<br />
| |
− | {<br />
| |
− | Player *p;<br />
| |
− | char name[20];<br />
| |
− | int index;<br />
| |
− | int data;<br />
| |
− | };<br />
| |
− | </font></blockquote>
| |
− | <font size="2">// spawn.cpp, modifications<br />
| |
− | </font><blockquote><font size="2">case EVENT_PlayerLeaving:<br />
| |
− | {<br />
| |
− | Player *p = (Player*)event.p[0];<br />
| |
− | <br />
| |
− | // killTags(p); // remove so tag not deleted on arena exit<br />
| |
− | <br />
| |
− | </font></blockquote>
| |
− | <font size="2"> </font><blockquote><font size="2">int botInfo::get_tag(Player *p, int index)<br />
| |
− | {<br />
| |
− | _listnode <PlayerTag> *parse = taglist.head;<br />
| |
− | PlayerTag *tag;<br />
| |
− | <br />
| |
− | while (parse)<br />
| |
− | {<br />
| |
− | tag = parse->item;<br />
| |
− | <br />
| |
− | // if (tag->p == p)<br />
| |
− | if (strcmp(tag->name,p->name)==0) // now tracking by player name, not ID<br />
| |
− | if (tag->index == index)<br />
| |
− | return tag->data;<br />
| |
− | <br />
| |
− | parse = parse->next;<br />
| |
− | }<br />
| |
− | <br />
| |
− | return 0;<br />
| |
− | }<br />
| |
− | <br />
| |
− | void botInfo::set_tag(Player *p, int index, int data)<br />
| |
− | {<br />
| |
− | _listnode <PlayerTag> *parse = taglist.head;<br />
| |
− | PlayerTag *tag;<br />
| |
− | <br />
| |
− | while (parse)<br />
| |
− | {<br />
| |
− | tag = parse->item;<br />
| |
− | <br />
| |
− | //if (tag->p == p)<br />
| |
− | if (strcmp(tag->name,p->name)==0) // now tracking by player name, not ID<br />
| |
− | if (tag->index == index)<br />
| |
− | {<br />
| |
− | tag->data = data;<br />
| |
− | return;<br />
| |
− | }<br />
| |
− | parse = parse->next;<br />
| |
− | }<br />
| |
− | <br />
| |
− | tag = new PlayerTag;<br />
| |
− | // tag->p = p; // not tracking by ID anymore<br />
| |
− | strncpy(tag->name, p->name, 20); // tracking by player name<br />
| |
− | tag->index = index;<br />
| |
− | tag->data = data;<br />
| |
− | taglist.append(tag);<br />
| |
− | }</font></blockquote>
| |
− | <font size="2"> </font></blockquote>
| |
− | <font size="2">3) Using Struct's<br />
| |
− | </font><blockquote><font size="2">struct name <br />
| |
− | {
| |
− |
| |
− | <br />
| |
| | | |
− | // variables <br />
| + | 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. |
| | | |
− | };<br />
| + | # Built-in get/setTag: Tracks data until player leaves the arena, then automatically deletes data. |
− | <br />
| + | # Modified perm get/setTag: Tracks data until bot leaves arena, then automatically deletes data. (Advantage: easier to sort by player) |
− | // Example: (in spawn.h)<br />
| + | # Custom Structs: Tracks data until plugin deletes it. (Advantage: easier to sort by freqs) |
− | </font><blockquote><font size="2">class botInfo<br />
| |
− | {<br />
| |
− | <br />
| |
− | struct freqdata {<br />
| |
− | int kills;<br />
| |
− | int deaths; <br />
| |
− | };<br />
| |
− | <br />
| |
− | freqdata freqs[100]; // 100 of those structs<br />
| |
− | <br />
| |
− | // then access it in spawn.cpp using<br />
| |
− | freqs[56].kills = 1;<br />
| |
− | </font></blockquote>
| |
− | <font size="2"> </font></blockquote>
| |
− | <font size="2"> <br />
| |
− | </font></blockquote>
| |
− | <font size="2"> <a name="11"></a>
| |
− | <b>[11] Output of data/messages</b><br />
| |
− | <br />
| |
− | using normal Strings<br />
| |
− | <br />
| |
− | // does *arena X pilots left in the game.<br />
| |
− | <br />
| |
− | String s = "*arena ";<br />
| |
− | s += temp; // some variable (int)<br />
| |
− | s += " pilots left in the game.";<br />
| |
− | sendPublic(s)<br />
| |
− |
| |
− | </font><blockquote><font size="2">OR<br />
| |
− |
| |
− | </font></blockquote>
| |
− | <font size="2"> </font><blockquote><font size="2">sendPublic("*arena " + (String) temp + " pilots left in the game.");<br />
| |
− |
| |
− |
| |
− | </font></blockquote>
| |
| | | |
− | <font size="2"> <br />
| + | ''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. |
− | example using sprintf to align/space data<br />
| |
− | <br />
| |
− | // output data
| |
− | will be in this approximate format (not lined up perfectly because of html)<br />
| |
− | <br />
| |
− | //
| |
− | ----------------------------------------------------------------------------------------<br />
| |
− | // Squad: squadname
| |
− | PTS FPTS K D DMG DEALT
| |
− | TAKEN F FK FLT<br />
| |
− | // ----------------------------------------------------------------------------------------<br />
| |
− | //
| |
− | PlayerA
| |
− | 10000 500
| |
− | 116 101
| |
− | 9999 99999 10 150 980:55<br />
| |
− | // PlayerB
| |
− | 500
| |
− | 200 7 5
| |
− | 9999 99999 5
| |
− | 3 0:04<br />
| |
− | <br />
| |
− | char str[255];<br />
| |
− | sendPublic("*arena----------------------------------------------------------------------------------");<br />
| |
− | sprintf(str, "*arena
| |
− | Squad: %-20s PTS FPTS K
| |
− | D DMG DEALT TAKEN F FK FLT",freqs[freq].freqname);<br />
| |
− | sendPublic(str);<br />
| |
− | sendPublic("*arena----------------------------------------------------------------------------------");<br />
| |
− | <br />
| |
− | // assuming existing freqs struct with data<br />
| |
− | for (pilot=freqs[freq].playercount-1; pilot>=0; pilot--)<br />
| |
− | {<br />
| |
− | // on freq squad so print stats<br />
| |
− | char outString[255];<br />
| |
− | <br />
| |
− | sprintf(outString,
| |
− | "*arena %-20s %12d %8d %3d %3d %10d %6d %2d %3d %3d:%02d",freqs[freq].pilots[pilot].name,<br />
| |
− | freqs[freq].pilots[pilot].points,
| |
− | freqs[freq].pilots[pilot].flagpoints, freqs[freq].pilots[pilot].kills,<br />
| |
− | freqs[freq].pilots[pilot].deaths,
| |
− | freqs[freq].pilots[pilot].dmgdealt, freqs[freq].pilots[pilot].dmgtaken,freqs[freq].pilots[pilot].flags,<br />
| |
− |
| |
− | freqs[freq].pilots[pilot].flagkills, freqs[freq].pilots[pilot].flagtime
| |
− | /60, freqs[freq].pilots[pilot].flagtime %60);<br />
| |
− | <br />
| |
− | sendPublic(outString);<br />
| |
− | }<br />
| |
− | <br />
| |
− | // Notes: sprintf
| |
− | format = sprintf(output char string, spacing, variables)<br />
| |
− | // Notes: s = chars, d = integer, - = left align, right align default<br />
| |
− | // Notes: doing
| |
− | %02d = put 0 in front if not 2 digits, %3d:%02d makes 0:04 format<br />
| |
− | <br />
| |
− | <br />
| |
− | <a name="12"></a>
| |
− | <b>[12] Input/Output to files </b><br />
| |
− | </font><blockquote><font size="2">Input to file<br />
| |
− | </font><blockquote><font size="2">// example reading from duel.ini looking for line that starts with MaxBoxes= then taking the next char as value to store as <br />
| |
− | // MAX_BOXES (ie duel.ini = MaxBoxes=5)<br />
| |
− | #include <fstream><br />
| |
− | using namespace std;<br />
| |
− | <br />
| |
− | ifstream file("duel.ini");<br />
| |
− | char line[256];<br />
| |
− | <br />
| |
− | // read in MaxBoxes=X<br />
| |
− | while (file.getline(line, 256))<br />
| |
− | {<br />
| |
− | if (CMPSTART("MaxBoxes=", line))<br />
| |
− | {<br />
| |
− | MAX_BOXES = atoi(&(line[9]));<br />
| |
− | break;<br />
| |
− | }<br />
| |
− | }<br />
| |
− | </font></blockquote>
| |
− | <font size="2">Output to file<br />
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2">// normal char output<br />
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2">#include <fstream><br />
| |
| | | |
| + | ===Built-in get/setTag method=== |
| | | |
− | using namespace std;<br />
| + | Player tags simply tag a player with a number. Define the wanted values in '''spawn.h''' at the top: |
| + | <pre> |
| + | #define DMG_DEALT 0 |
| + | #define DMG_TAKEN 1 |
| + | </pre> |
| | | |
− | <br />
| + | In spawn.cpp, initialize the values on ArenaEnter and PlayerEnter: |
| + | <pre> |
| + | case EVENT_ArenaEnter: |
| + | { |
| + | // ... |
| | | |
− | ofstream file("duelleaguestat.inc", ios::app); // app = put all data at end of file<br />
| + | // do for all pilots in arena when bot enters |
| + | _listnode <Player> *parse = playerlist->head; |
| + | while (parse) |
| + | { |
| + | Player *p = parse->item; // get pilot |
| | | |
− | <br />
| + | set_tag(p, DMG_DEALT, 0); // initialize to 0 |
| + | set_tag(p, DMG_TAKEN, 0); |
| + | sendPrivate(p, "*watchdamage"); // optionally turn on player *watchdamage |
| | | |
− | file << squad1<< endl; // squad1 = char[20]<br />
| + | parse = parse->next; // get next pilot |
| + | } |
| + | } |
| + | </pre> |
| + | <pre> |
| + | case EVENT_PlayerEntering: |
| + | { |
| + | // ... |
| + | set_tag(p, DMG_DEALT, 0); // initialize to 0 |
| + | set_tag(p, DMG_TAKEN, 0); |
| + | sendPrivate(p,"*watchdamage"); |
| + | } |
| + | </pre> |
| | | |
− | file << " vs "<< endl;<br />
| + | Then somewhere edit the tag values: |
| + | <pre> |
| + | case EVENT_WatchDamage: |
| + | { |
| + | // sets tag for k (shooter) to be old value plus damage currently dealt |
| | | |
− | file << squad2<< endl; // squad2 = char[20]<br />
| + | int old_damage = get_tag(k, DMG_BOMB_DEALT); |
− |
| + | set_tag(k, DMG_BOMB_DEALT, old_damage + damage); |
− |
| + | } |
− | </font></blockquote>
| + | </pre> |
− | <font size="2">
| |
− | // how to output String's
| |
− | to file (key is converting String to (char*) to file write)<br />
| |
− |
| |
− |
| |
− | </font><blockquote>
| |
− | <font size="2">String str = freqs[freq].slotname[slot];<br /> | |
| | | |
− | str += ", Repels: " + (String)(int) t->repel;<br />
| + | The following demonstrates how to retrieve the tag values as a command in command.cpp: |
| + | <pre> |
| + | if (c->check("showstats")) |
| + | { |
| + | int temp = get_tag(p, DMG_TOTAL_DEALT); |
| | | |
− |
| + | String s = "You've done "; |
− |
| + | s += temp; |
− | <br />
| + | s += " damage so far!"; |
| | | |
− | outf << endl;<br />
| + | sendPrivate(p,s); |
| + | } |
| + | </pre> |
| | | |
− | outf << (char*) str;<br />
| + | ===Modified permanent get/setTag method=== |
− |
| |
− | </font></blockquote>
| |
| | | |
− | <font size="2"> <br />
| + | 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. |
− | // date and time stamp<br />
| |
− |
| |
− | </font><blockquote><font size="2">#include "time.h"<br />
| |
− | <br />
| |
− | char u[100];<br />
| |
− | time_t t=time(NULL);<br />
| |
− | tm *tmp = localtime(&t);<br />
| |
− | strftime(u,99,"%c",tmp);<br />
| |
− | sendPublic("Date and time: " + (String) u);<br />
| |
| | | |
− |
| |
− | </font></blockquote>
| |
− | <font size="2"> </font></blockquote>
| |
− | </blockquote>
| |
− | <font size="2"> </font><blockquote><font size="2">Example reading input from file using "GetPrivateProfileString" (from rampage plugin)<br />
| |
− | </font><blockquote><font size="2">format of rampage.ini<br />
| |
− | </font><blockquote><font size="2">7=is on a killing spree! (6:0)<br />10=is opening a can of whoop-ass! (9:0)<br />
| |
− | </font></blockquote>
| |
− | <font size="2">read input<br />
| |
− | </font><blockquote><font size="2">rampageini.h<br />
| |
− | </font><blockquote><font size="2">#pragma once<br />
| |
− | #ifndef RAMPAGEINI_H<br />
| |
− | #define RAMPAGEINI_H<br />
| |
− | <br />
| |
− | #define NUM_RANKS 10<br />
| |
− | #define BUFFER_LEN 256<br />
| |
− | <br />
| |
− | struct RampageSettings<br />
| |
− | {<br />
| |
− | char quotes[NUM_RANKS][BUFFER_LEN];<br />
| |
− | };<br />
| |
− | <br />
| |
− | void LoadSettings(RampageSettings &setts);<br />
| |
− | <br />
| |
− | #endif // RAMPAGEINI_H<br />
| |
− | </font></blockquote>
| |
− | <font size="2">rampageini.cpp<br />
| |
− | </font><blockquote><font size="2">#include "rampageini.h"<br />
| |
− | static char buffer[BUFFER_LEN];<br />
| |
− | static char path[BUFFER_LEN];<br />
| |
− | #include "../algorithms.h"<br />
| |
− | #define WIN32_LEAN_AND_MEAN<br />
| |
− | #include <windows.h><br />
| |
− |
| |
− | <br />
| |
− | char *rank_type[10] = {<br />
| |
− | "7",<br />
| |
− | "10",<br />
| |
− | };<br />
| |
− | <br />
| |
− | void LoadSettings(RampageSettings &setts)<br />
| |
− | {<br />
| |
− | GetCurrentDirectory(BUFFER_LEN - 64, path);<br />
| |
− | strcat(path, "\rampage.ini");<br />
| |
− |
| |
− | <br />
| |
− | for (int i = 0; i < NUM_RANKS; ++i)<br />
| |
− | {<br />
| |
− | GetPrivateProfileString("Comments",
| |
− | rank_type[i], "-ERROR-", setts.quotes[i], BUFFER_LEN, path);<br />
| |
− | }<br />
| |
− | }<br />
| |
− | </font></blockquote>
| |
− | <font size="2"> </font></blockquote><font size="2"> </font></blockquote><font size="2"> </font></blockquote>
| |
− | <font size="2"><a name="13"></a><b>
| |
− | [13] Simple programming commands</b><br />
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2">simple commands <br />
| |
− |
| |
− | </font><blockquote>
| |
− | <font size="2"> declare/initialize variables // example: int
| |
− | temp; int temp2 = 1; temp2 = 3; <br />
| |
− |
| |
− |
| |
− | // bool check = true; check = false;<br />
| |
− |
| |
− | <br />
| |
− | if (condition) {}
| |
− | // example: if (a > b) { a++; }<br />
| |
| | | |
− | <br />
| + | // spawn.h, add char name[20]; into struct PlayerTag |
| + | <pre> |
| + | struct PlayerTag |
| + | { |
| + | Player *p; |
| + | char name[20]; |
| + | int index; |
| + | int data; |
| + | }; |
| + | </pre> |
| | | |
− | if (condition) {} else {}
| + | In spawn.cpp: |
− | // example: if (b <= 0) { b--; } else { a++;
| + | <pre> |
− | }<br />
| + | case EVENT_PlayerLeaving: |
| + | { |
| + | Player *p = (Player*)event.p[0]; |
| | | |
− | <br />
| + | // killTags(p); // remove so tag not deleted on arena exit |
| | | |
− | while (condition) {}
| + | // ... |
− | // example: while (b > 0) { b--; }<br />
| + | </pre> |
| | | |
− | <br />
| + | Locate in spawn.cpp and modify accordingly: |
| + | <pre> |
| + | int botInfo::get_tag(Player *p, int index) |
| + | { |
| + | _listnode <PlayerTag> *parse = taglist.head; |
| + | PlayerTag *tag; |
| | | |
− | for (initialize; condition; increment){}
| + | while (parse) |
− | // example: for (a=1; a < 10; a++) { a = a + b; }<br />
| + | { |
− |
| + | tag = parse->item; |
− | </font></blockquote>
| |
| | | |
− | <font size="2"> array<br />
| + | // if (tag->p == p) |
− |
| + | if (strcmp(tag->name,p->name)==0) // now tracking by player name, not pointer |
− | </font><blockquote><font size="2">single dimension<br />
| + | if (tag->index == index) |
− |
| + | return tag->data; |
− | </font><blockquote><font size="2">int teams[100]; // create 100 hundred teams 0-99<br />
| |
− |
| |
− | <br />
| |
− | teams[50] = 1;<br />
| |
− |
| |
− | </font></blockquote>
| |
− | <font size="2">multi-dimensional<br />
| |
− |
| |
− | </font><blockquote><font size="2">int teams[100][50]; // multidimensional arry<br />
| |
− |
| |
− | <br />
| |
− | teams[99][49] = 2;<br />
| |
− |
| |
− | </font></blockquote>
| |
− | <font size="2">variable size <br />
| |
− |
| |
− | </font><blockquote><font size="2">String *list = new String[amount+1]; // string array with size amount (variable) + 1;<br />
| |
− |
| |
− | <br />
| |
− | list[amount-1] = "hi";<br />
| |
− |
| |
− | </font></blockquote><font size="2"> </font></blockquote>
| |
− | <font size="2">struct <br />
| |
− |
| |
− | </font><blockquote><font size="2"> struct name
| |
− | <br />
| |
| | | |
− | {
| + | parse = parse->next; |
− |
| + | } |
− | <br />
| + | return 0; |
| + | } |
| | | |
− | // variables
| + | void botInfo::set_tag(Player *p, int index, int data) |
− | <br />
| + | { |
| + | _listnode <PlayerTag> *parse = taglist.head; |
| + | PlayerTag *tag; |
| | | |
| + | while (parse) |
| + | { |
| + | tag = parse->item; |
| | | |
− | };<br />
| + | //if (tag->p == p) |
− |
| + | if (strcmp(tag->name,p->name)==0) // now tracking by player name, not pointer |
− | </font><blockquote><font size="2">// example:<br />
| + | if (tag->index == index) |
− | struct freqdata {
| + | { |
− | int kills; int deaths; };<br />
| + | tag->data = data; |
− |
| + | return; |
− | <br />
| + | } |
− | freqdata freqs[100]; // 100 of those structs<br />
| + | parse = parse->next; |
− |
| + | } |
− | <br />
| |
− | freqs[56].kills =
| |
− | 1; // access struct<br />
| |
− |
| |
− | </font></blockquote><font size="2"> </font></blockquote>
| |
| | | |
− | <font size="2"> switch <br />
| + | tag = new PlayerTag; |
− |
| + | // tag->p = p; // not tracking by pointer anymore |
− |
| + | strncpy(tag->name, p->name, 20); // tracking by player name |
− | </font><blockquote><font size="2">switch (variable)
| + | tag->index = index; |
− | <br />
| + | tag->data = data; |
| + | taglist.append(tag); |
| + | } |
| + | </pre> |
| | | |
− | {
| + | ===Using structs=== |
− |
| |
− | <br />
| |
| | | |
− | case n:
| + | In '''spawn.h''': |
− | <br />
| + | <pre> |
| + | class botInfo |
| + | { |
| + | struct freqdata |
| + | { |
| + | int kills, deaths; |
| + | }; |
| + | // ... |
| + | }; |
| + | </pre> |
| | | |
− | { }
| + | To make use of this structure, implement accordingly: |
− |
| + | <pre> |
− | <br />
| + | freqdata freqs[100]; // 100 of those structs |
| + | </pre> |
| + | Access the data in spawn.cpp using |
| + | <pre> |
| + | freqs[56].kills = 1; |
| + | </pre> |
| | | |
− | break;
| + | 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. |
− | <br />
| |
| | | |
− |
| + | ==Input/Output to files== |
− |
| |
− | <br />
| |
| | | |
− | case m:
| + | For reading and/or writing to files with C++ you must have the required include statement as follows: |
− | <br />
| + | <pre> |
| + | #include <fstream> |
| + | using namespace std; |
| + | </pre> |
| | | |
− | { }
| + | ===File stream input=== |
− |
| + | The following example will show you how to read a file, duel.ini, line by line. |
− | <br />
| |
| | | |
− | break;
| + | <pre>#include "stdlib.h" // for atoi()</pre> |
− | <br />
| |
| | | |
| + | <pre> |
| + | ifstream file("duel.ini"); |
| | | |
− | <br />
| + | 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; |
| + | } |
| + | } |
| | | |
− | default:<br />
| + | file.close(); |
| + | } |
| + | </pre> |
| | | |
| + | ===File stream output=== |
| + | The following code example will demonstrate how to append to a file, duelleaguestat.inc. |
| + | <pre> |
| + | ofstream file("duelleaguestat.inc", ios::app); // app = put all data at end of file |
| | | |
− | break;<br />
| + | 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(); |
| + | } |
| + | </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 |
| + | String str = freqs[freq].slotname[slot]; |
| + | str += ", Repels: " + (String)(int) t->repel; |
| + | file << endl; |
| + | file << (char*) str; |
| + | </pre> |
| | | |
− | }<br />
| + | ===Input with GetPrivateProfileString=== |
− |
| |
− | </font><blockquote><font size="2"> </font><blockquote><font size="2">// Example:<br />
| |
− | switch (p->ship) {<br />
| |
− |
| |
− | <br />
| |
− | case SHIP_Warbird: {<br />
| |
− | sendPrivate(p,"You're in a warbird");<br />
| |
− | } break;<br />
| |
− |
| |
− | <br />
| |
− | default:<br />
| |
− | break;</font></blockquote><font size="2"> </font></blockquote>
| |
| | | |
− | <font size="2"> </font></blockquote>
| + | 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. |
− | <font size="2"> </font></blockquote>
| |
| | | |
− | <font size="2"> <br /> | + | The file format for rampage.ini is like this: |
− | <b><a name="14"></a>
| + | <pre> |
− | [14] Useful Player data</b><br />
| + | 7=is on a killing spree! (6:0) |
− | </font><blockquote>
| + | 10=is opening a can of booya! (9:0) |
− | <font size="2">
| + | </pre> |
− | // useful data stored
| |
− | by MervBot about each player (player.h)<br />
| |
− |
| |
− | <br />
| |
| | | |
− | p->name = player name stored as char[20]<br />
| + | In '''rampageini.cpp''': |
− | p->squad = player squad stored as char[20]<br />
| + | <pre> |
− | p->ship = ship (0-7) enumerated as SHIP_Warbird, SHIP_Spectator, etc..<br />
| + | #include "rampageini.h" |
− | p->safety = if ship is in safety zone (boolean)<br />
| + | #define WIN32_LEAN_AND_MEAN |
− | p->bounty = player bounty<br />
| + | #include <windows.h> |
− | p->energy = player energy (have bot with *energy on to get accurate readings)<br />
| |
− | p->flagCount = how many flags player is holding<br />
| |
| | | |
− | p->team = player frequency<br />
| + | #define NUM_RANKS 10 |
− | p->(burst, repel, thor, brick, decoy, rocket, portal) = how many items of that type player has<br />
| + | #define BUFFER_LEN 256 |
− | p->(stealth, cloak, xradar, awarp, ufo, flash, safety, shields, supers) = if player has that item on (boolean)<br />
| |
| | | |
− | p->score.killPoints = player kill points<br />
| + | struct RampageSettings |
− | p->score.flagPoints = player flag points<br />
| + | { |
− | p->score.wins = player kills from f2<br />
| + | char quotes[NUM_RANKS][BUFFER_LEN]; |
− | p->score.losses = player deaths from f2<br />
| + | }; |
− | </font></blockquote>
| + | |
− | <font size="2"> <br />
| + | void LoadSettings(RampageSettings &setts); |
− | <br />
| |
− | <b><a name="15"></a> [15] Bot built in functions<br />
| |
− | </b>
| |
− | </font><blockquote><font size="2">// useful MervBot commands to control what the bot is doing<br />
| |
− | <br />
| |
− | // player.cpp<br />
| |
− | Player::move(Sint32 x, Sint32 y) // example me->move(512,512) - bot moves to coord 512 512<br />
| |
− | Player::clone(Player *p) // example me->clone(p) <br />
| |
− | <br />
| |
− | // dllcore.cpp (descriptions of functions in dllcore.h)<br />
| |
− | <br />
| |
− | BotEvent makeEcho (char *m);<br />
| |
− | BotEvent makeSay (int t, int s, int i, char *m);<br />
| |
− | <br />
| |
− | BotEvent makeShip (int s);<br />
| |
− | BotEvent makeTeam (int t);<br />
| |
− | BotEvent makeGrabFlag (int f);<br />
| |
− | BotEvent makeSendPosition (bool reliable);<br />
| |
− | BotEvent makeDropFlags ();<br />
| |
− | <br />
| |
− | BotEvent makeDeath (Player *p);<br />
| |
− | BotEvent makeAttach (Player *p);<br />
| |
− | BotEvent makeDetach ();<br />
| |
− | BotEvent makeFollowing (bool f);<br />
| |
− | BotEvent makeFlying (bool f);<br />
| |
− | BotEvent makeBanner (BYTE *b);<br />
| |
− | BotEvent makeDropBrick ();<br />
| |
− | BotEvent makeFireWeapon (void *weapon_info);<br />
| |
− | <br />
| |
− | BotEvent makeToggleObjects (Uint16 player, Uint16 *objects, int num_objects);<br />
| |
− | <br />
| |
− | BotEvent makeSpawnBot
| |
− | (char *name, char *password, char *staff, char *arena);<br />
| |
− | BotEvent makeChangeArena (char *name);<br />
| |
− | BotEvent makeChangeSettings (_linkedlist <String> *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 <arena>)<br />
| |
− | </font></blockquote>
| |
− | <font size="2"> <b><br />
| |
− | Example Code</b><br />
| |
− | <br />
| |
− | <a name="15a"></a>
| |
− | a) No antiwarp in center of map (take green away and warn)<br />
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2">// using function closeto() and abs()<br /></font></blockquote><font size="2"> </font><blockquote><font size="2">bool closeto(Player *p, int x, int y, int tolerance) {<br />
| |
− | return (abs((p->tile.x) - x) < tolerance) && (abs((p->tile.y) - y) < tolerance); }<br /><br />
| |
− | inline int abs(int n) {<br />
| |
− | if (n < 0) return -n;<br />
| |
− | else return n; }<br /><br />
| |
− | radius = 35; // global variable<br /></font></blockquote><font size="2"> </font><blockquote><font size="2">case EVENT_PlayerMove: {<br />
| |
− | Player *p = (Player*)event.p[0];<br /><br />
| |
− | // no anti in center<br />
| |
− | if ((p->ship != SHIP_Spectator) && (p->awarp)) {<br />
| |
− | if (closeto(p, 512, 512, radius)){<br />
| |
− | sendPrivate(p, "*prize #-20");<br />
| |
− |
| |
− | sendPrivate(p, "*warn Antiwarp is not allowed in center.");<br />
| |
− | }<br />
| |
− | }<br /></font></blockquote>
| |
− | <font size="2">
| |
− |
| |
− | <a name="15b"></a>
| |
| | | |
− | b) Setting freq size depending on how many pilots in game<br />
| + | static char path[BUFFER_LEN]; |
− |
| |
− |
| |
− | </font><blockquote><font size="2">case EVENT_Tick: {<br /></font><blockquote><font size="2">if (countdown[0] == 0) { // assuming countdown[0] initialized to > 0 in spawn.h, freqchange=0;<br /></font></blockquote>
| |
− | <font size="2">
| |
− | _listnode <Player> *parse = playerlist->head;<br />
| |
− | int count = 0;<br /><br />
| |
− | while (parse)<br />
| |
− | {<br />
| |
− | Player *p = parse->item;<br /><br />
| |
− | if (p->ship != SHIP_Spectator)<br />
| |
− | ++count;<br /><br />
| |
− | parse = parse->next;<br />
| |
− | }<br /><br />
| |
− | if ((count > 24) && (freqchange != 4))<br />
| |
− | {<br />
| |
− |
| |
− | sendPublic("?set team:maxperteam:4");<br />
| |
− | String s;<br />
| |
− | s = "Max freq size 4 (";<br />
| |
− | s += count;<br />
| |
− | s += " pilots in game)";<br />
| |
− | sendPublic(s);<br />
| |
− | freqchange = 4;<br />
| |
− | }<br /><br />
| |
− |
| |
− | if ((count < 25) && (count > 14) && (freqchange !=
| |
− | 3))<br />
| |
− | {<br />
| |
− |
| |
− | sendPublic("?set team:maxperteam:3");<br />
| |
− | String s;<br />
| |
− | s = "Max freq size 3 (";<br />
| |
− | s += count;<br />
| |
− | s += " pilots in game)";<br />
| |
− | sendPublic(s);<br />
| |
− | freqchange = 3;<br />
| |
− | }<br /></font><blockquote><font size="2">countdown[0] = 120;<br />
| |
− | }<br /></font></blockquote></blockquote><font size="2">
| |
− |
| |
− | <a name="15c"></a> c) Tracking kills and announcing when pilot gets 10 kills in a row without
| |
− | dying
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2">case EVENT_PlayerDeath: {<br /></font><blockquote><font size="2">// assuming tags are setup (see storing data section)<br />
| |
− | set_tag(p,KILLS, 0); // pilot died, reset to 0 kills in a row<br />
| |
− | set_tag(k, KILLS, get_tag(k, KILLS) + 1); // pilot killed someone, increment kills in a row by 1<br /><br />
| |
− | if (get_tag(k,KILLS) == 10) sendPublic("*arena (String) k->name + " has gotten 10 kills.");</font></blockquote></blockquote>
| |
− | <font size="2">
| |
− |
| |
− | <a name="15d"></a>
| |
| | | |
− | d) Warp pilot to coord when they are in a certain region<br />
| + | char *rank_type[NUM_RANKS] = { "7", "10" }; |
− |
| |
− |
| |
− | </font><blockquote><font size="2">// using functions closeto() and abs()<br /><br />
| |
− | bool closeto(Player *p, int x, int y, int tolerance) {<br />
| |
| | | |
− | return (abs((p->tile.x) - x) < tolerance) && (abs((p->tile.y) - y) < tolerance); }<br /><br />
| + | void LoadSettings(RampageSettings &setts) |
| + | { |
| + | GetCurrentDirectory(BUFFER_LEN - 64, path); |
| + | strcat(path, "\rampage.ini"); |
| | | |
− | inline int abs(int n) {<br />
| + | for (int i = 0; i < NUM_RANKS; ++i) |
| + | { |
| + | GetPrivateProfileString("Comments", rank_type[i], "-ERROR-", |
| + | setts.quotes[i], BUFFER_LEN, path); |
| + | } |
| + | } |
| + | </pre> |
| | | |
− | if (n < 0) return -n;<br />
| + | ==Player data== |
| | | |
− | else return n; }<br /><br />
| + | As stated earlier in the tutorial, MervBot stores useful player data internally as Player objects, see player.h for implementation details. |
− | case EVENT_PlayerMove: {<br /><br />
| |
− | if (closeto(p, 509, 509, 2)) { // if pilot within 2 of map coord 509,509<br />
| |
− | sendPrivate(p, "*warpto 509 504"); // warp to coord 509,504<br /></font></blockquote>
| |
− | <font size="2">
| |
− |
| |
− | <br />
| |
− |
| |
− | <a name="15e"></a>
| |
| | | |
− | e) Structures within structures (spawn.h botinfo)<br />
| + | * 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.. |
− | </font><blockquote><font size="2"> // Declare in spawn.h<br /></font><blockquote><font size="2"> struct playerstats<br />
| + | * p->safety = whether ship is in safety zone (boolean) |
− | {<br />
| + | * p->bounty = player bounty |
− | char name[20];<br /><br />
| + | * p->energy = player energy (have bot with *energy on to get accurate readings) |
− | int kills;<br />
| + | * p->flagCount = how many flags player is holding |
− | int deaths;<br />
| + | * p->team = player frequency |
− | Uint16 points;<br />
| + | * p->(burst, repel, thor, brick, decoy, rocket, portal) = how many items of that type player has |
− | Uint16 flagpoints;<br />
| + | * p->(stealth, cloak, xradar, awarp, ufo, flash, safety, shields, supers) = if player has that item on (boolean) |
− | int flagtime;<br />
| + | * p->score.killPoints = player kill points |
− | int cflagtime;<br />
| + | * p->score.flagPoints = player flag points |
− | int flags;<br />
| + | * p->score.wins = player kills from f2 |
− | int flagkills;<br /><br />
| + | * p->score.losses = player deaths from f2 |
− | int dmgdealt;<br />
| |
− | int dmgtaken;<br /><br />
| |
− | };<br /><br />
| |
− | struct freqdata<br />
| |
− | {<br />
| |
− | playerstats pilots[100];<br /><br />
| |
− | int freqpoints;<br />
| |
− | char freqname[20];<br />
| |
− | int freqflagpoints;<br />
| |
− | Uint16 freqteam;<br />
| |
− | int freqflagtime;<br /><br />
| |
− | int flags;<br />
| |
− | int kills;<br />
| |
− | int deaths;<br />
| |
− | int flagkills;<br /><br />
| |
− | int dmgdealt;<br />
| |
− | int dmgtaken;<br /><br />
| |
− | int playercount;<br />
| |
− | };<br /><br />
| |
− | freqdata freqs[100];<br /></font></blockquote>
| |
− | <font size="2">// Initialize in spawn.cpp<br /></font><blockquote><font size="2">void botInfo::Clear()<br />
| |
− | {<br />
| |
− | // initialize/clear struct data<br />
| |
− | for (int n=99; n>=0; n--)<br />
| |
− | {<br />
| |
− | freqs[n].freqteam=-1;<br />
| |
− | freqs[n].freqpoints=0;<br />
| |
− | freqs[n].freqflagpoints=0;<br />
| |
− | freqs[n].playercount=0;<br />
| |
− | freqs[n].flags=0;<br />
| |
− | freqs[n].kills=0;<br />
| |
− | freqs[n].deaths=0;<br />
| |
− | freqs[n].freqflagtime=0;<br />
| |
− | freqs[n].flagkills=0;<br />
| |
− | freqs[n].dmgdealt=0;<br />
| |
− | freqs[n].dmgtaken=0;<br />
| |
− | <br />
| |
− | for (int m = 99; m>=0; m--)<br />
| |
− | {<br />
| |
− | freqs[n].pilots[m].deaths=0;<br />
| |
− | freqs[n].pilots[m].kills=0;<br />
| |
− | freqs[n].pilots[m].points=0;<br />
| |
− | freqs[n].pilots[m].flagpoints=0;<br />
| |
− | freqs[n].pilots[m].flagtime=0;<br />
| |
− | freqs[n].pilots[m].cflagtime=0;<br />
| |
− | freqs[n].pilots[m].flags=0;<br />
| |
− | freqs[n].pilots[m].flagkills=0;<br />
| |
− | freqs[n].pilots[m].dmgdealt=0;<br />
| |
− | freqs[n].pilots[m].dmgtaken=0;<br />
| |
− | }<br />
| |
− | }<br />
| |
− | }<br /></font></blockquote></blockquote><font size="2"> </font><blockquote><font size="2">// Access data in spawn.cpp<br /></font><blockquote><font size="2">int freq = p->team;<br /><br />
| |
− | freqs[1].pilots[2].kills++;<br /><br />
| |
− | OR<br /><br />
| |
− | freqs[freq].deaths++;<br /></font></blockquote></blockquote><font size="2">
| |
− |
| |
− |
| |
− | <a name="15f"></a>
| |
| | | |
− | f) Tracking flag data<br />
| + | Just access the respective member of the Player class to check the player's property. |
− |
| |
− | </font><blockquote><font size="2">Example GetPilot() function (using structs from example e)<br /></font><blockquote><font size="2">bool botInfo::GetPilot(Player *p)<br />
| |
− | {<br />
| |
− | // get a pilots freq/pilot id from struct<br />
| |
− | for (freq=freqcount-1; freq>=0; freq--)<br />
| |
− | if (p->team == freqs[freq].freqteam)<br />
| |
− | for (pilot = freqs[freq].playercount-1; pilot>=0; pilot--)<br />
| |
− |
| |
− | if (strcmp(p->name,freqs[freq].pilots[pilot].name)==0)<br />
| |
− | return true;<br /><br />
| |
− | return false;<br />
| |
− | }<br /></font></blockquote></blockquote><font size="2"> </font><blockquote><font size="2">Example way to track flag data using above struct/functions<br /></font><blockquote><font size="2">case EVENT_FlagGrab:<br />
| |
− | {<br />
| |
− | if (GetPilot(p)) // function<br />
| |
− | {<br />
| |
− | freqs[freq].pilots[pilot].flags++;<br />
| |
− | freqs[freq].flags++;<br /><br />
| |
− |
| |
− | if (freqs[freq].pilots[pilot].flags < 2) // didnt have a flag before,
| |
− | first flag<br />
| |
− |
| |
− | freqs[freq].pilots[pilot].cflagtime = GetTickCount();
| |
− | // time stamp when picked up flag<br />
| |
− | }<br /></font></blockquote></blockquote>
| |
− | <font size="2"> </font><blockquote><font size="2">Example way to track flag data using built in get/set tag (from catid flagbot)<br />
| |
− |
| |
− |
| |
− | </font><blockquote>
| |
− | <font size="2">case EVENT_FlagGrab:<br />
| |
| | | |
| + | For example, in spawn.cpp, to check whether a player is in a safety zone: |
| + | <pre> |
| + | EVENT_PlayerMove: |
| + | { |
| + | Player *p = (Player*)event.p[0]; |
| | | |
− | {<br />
| + | if ( p->safety ) // player is in safe zone. |
| + | { |
| + | // do something. |
| + | } |
| | | |
| + | if ( !p->safety ) // player NOT in safe zone. |
| + | { |
| + | // do something. |
| + | } |
| + | } |
| + | </pre> |
| | | |
− | set_tag(p, TAG_STAT_FS, get_tag(p, TAG_STAT_FS) + 1);<br />
| + | === 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. |
| | | |
− | set_tag(p, TAG_FLAGTIMER, GetTickCount());<br />
| + | ''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> |
| + | // 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; |
| | | |
− | }<br />
| + | //convert search name to lowercase |
− |
| + | char nname[20], pname[20]; |
− |
| + | strncpy(nname, name, 20); |
− | </font></blockquote><font size="2"> </font></blockquote><font size="2"> </font><blockquote><font size="2">Get current flag times using struct format<br />
| + | tolower(nname); |
− |
| |
− |
| |
− | </font><blockquote><font size="2">void botInfo::SetFlagTimes()<br />
| |
− | {<br />
| |
− | // set current flagtime for pilots/freqs<br />
| |
− | _listnode <Player> *parse = playerlist->head;<br />
| |
− | <br />
| |
− | while (parse)<br />
| |
− | {<br />
| |
− | Player *p = parse->item;<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | if (GetPilot(p))<br />
| |
− | if (freqs[freq].pilots[pilot].flags > 0)<br />
| |
− | {<br />
| |
− | if (PilotOnSquad(p))<br />
| |
− |
| |
− | freqs[freq].freqflagtime += (GetTickCount() - freqs[freq].pilots[pilot].cflagtime)/1000;<br />
| |
− |
| |
− |
| |
− | <br />
| |
− |
| |
− | freqs[freq].pilots[pilot].flagtime += (GetTickCount() - freqs[freq].pilots[pilot].cflagtime)/1000;<br />
| |
− |
| |
− | freqs[freq].pilots[pilot].cflagtime = GetTickCount();<br />
| |
− | }<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | parse = parse->next;<br />
| |
− | }<br />
| |
− | }<br />
| |
− |
| |
− | </font></blockquote>
| |
− | <font size="2">// side note: case EVENT_FlagDrop: {} gets called anytime theres a teamkill<br />
| |
− |
| |
− |
| |
− | </font></blockquote><font size="2">
| |
− |
| |
− |
| |
− | <br />
| |
− |
| |
− |
| |
− | <a name="15g"></a>
| |
− | g) Example way to do simple /!spam feature (allowed 1x/60s)<br />
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2">declare and initialize variables in spawn.h<br />
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2">class botInfo<br />
| |
− | {<br />
| |
− | bool spamready;<br />
| |
− | int SPAM_TIME;<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | public:<br />
| |
− | botInfo(CALL_HANDLE given)<br />
| |
− | {<br />
| |
− | spamready = true;<br />
| |
− | SPAM_TIME = 60;<br />
| |
− |
| |
− |
| |
− | </font></blockquote>
| |
− | <font size="2">spawn.cpp - mark as spamready=true when 60 seconds up<br />
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2">case EVENT_Tick:<br />
| |
− | {<br />
| |
− | if (countdown[0] == 1) {<br />
| |
− | spamready = true; // ready to spam again<br />
| |
− | }<br />
| |
− |
| |
− |
| |
− | </font></blockquote><font size="2">
| |
− |
| |
− |
| |
− | <br />
| |
− | command.cpp - handle !spam command<br />
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2">case OP_Player:<br />
| |
− | { // Player-level commands<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | else if (c->check("spam"))<br />
| |
− | {<br />
| |
− |
| |
− | // zone announcement "Need pilots to duel in ?go arena -pilotname"<br />
| |
− | if (spamready == true)<br />
| |
− | {<br />
| |
− | String s;<br />
| |
− |
| |
− | s += "*zone Need pilots to duel in ?go ";<br />
| |
− | s += arena;<br />
| |
− | s += " - ";<br />
| |
− | s += p->name;<br />
| |
− | sendPublic(s);<br />
| |
− | <br />
| |
− | spamready=false;<br />
| |
− |
| |
− | countdown[0] = SPAM_TIME * 60; // next spam time limit<br />
| |
− | }<br />
| |
− | else if (countdown[0] < 0)<br />
| |
− | {<br />
| |
− |
| |
− | sendPrivate(p,"Spam ability disabled.");<br />
| |
− | }<br />
| |
− | else <br />
| |
− | {<br />
| |
− | String s;<br />
| |
− | s += SPAM_TIME;<br />
| |
− |
| |
− | s += " Minute timer between announcements. ";<br />
| |
− | s += countdown[0] / 60;<br />
| |
− | s += ":";<br />
| |
− | if (countdown[0] % 60 < 10)<br />
| |
− | s += "0";<br />
| |
− | s += countdown[0] % 60;<br />
| |
− |
| |
− | s += " minutes left before next spam allowed.";<br />
| |
− | sendPrivate(p, s);<br />
| |
− | }<br />
| |
− | }<br />
| |
− |
| |
− |
| |
− | </font></blockquote><font size="2"> </font></blockquote><font size="2">
| |
− |
| |
− |
| |
− | <a name="15h"></a>
| |
− | h) Example of implementing a simple stack to do "next in line for several 'boxes' at once"<br />
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2">//spawn.h declare variables<br />
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2">class botInfo<br />
| |
− | {<br />
| |
− | Player *next[99][99];<br />
| |
− | int MAX_NEXT;<br />
| |
− | int nextcount[99];<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | public:<br />
| |
− | botInfo(CALL_HANDLE given)<br />
| |
− | {<br />
| |
− | MAX_NEXT = 8; <br />
| |
− |
| |
− |
| |
− | </font></blockquote>
| |
− | <font size="2">// spawn.cpp MoveUp function<br />
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2">void botInfo::MoveUp(int pos, int box)<br />
| |
− | {<br />
| |
− | // moves up the next line for that box and decrement box's nextcount<br />
| |
− | if (nextcount[box] > 0)<br />
| |
− | nextcount[box]--;<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | for (pos = pos; pos < MAX_NEXT - 1; pos++)<br />
| |
− | {<br />
| |
− | next[box][pos] = next[box][pos + 1];<br />
| |
− | }<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | next[box][MAX_NEXT] = 0;<br />
| |
− | }<br />
| |
− |
| |
− | </font></blockquote>
| |
− | <font size="2"> </font></blockquote>
| |
− | <font size="2">
| |
− |
| |
− | <a name="15i"></a>
| |
− | i) Example of reading any text from a .txt and printing it to pilot line by line<br />
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2">#include <fstream><br />
| |
− | using namespace std;<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | case OP_Player:<br />
| |
− | {<br />
| |
− | if (c->check("schedule"))<br />
| |
− | {<br />
| |
− | // read in schedule from schedule.txt<br />
| |
− | ifstream file("schedule.txt");<br />
| |
− | char line[256];<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | while (file.getline(line, 256))<br />
| |
− | {<br />
| |
− | sendPrivate(p, line);<br />
| |
− | }<br />
| |
− | file.close();<br />
| |
− | } <br />
| |
− |
| |
− |
| |
− | </font></blockquote><font size="2">
| |
− |
| |
− |
| |
− | <a name="15j"></a>
| |
− | j) Example of printing player stats grid<br />
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2">// spawn.cpp (see "structures within structures" example for variable declarations, varibale freqcount = # of freqs)<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | void botInfo::DisplayPlayers()<br />
| |
− | {<br />
| |
− | // Display Match player/freq stats in this format (not aligned b/c of html but aligned in bot)<br />
| |
− | // ---------------------------------------------------<br />
| |
− | // Squad: squad_name_1
| |
− | K D TK DMG DEALT TAKEN<br />
| |
− | // ---------------------------------------------------<br />
| |
− | // Player_1
| |
− | 0 0 0 0
| |
− | 0<br /> // Player_2
| |
− | 0 0 0 0
| |
− | 0<br /> // TOTAL:
| |
− | 0 0 0 0
| |
− | 0<br />
| |
− | // ---------------------------------------------------<br />
| |
− | // Squad: squad_name_2
| |
− | K D TK DMG DEALT TAKEN<br />
| |
| | | |
− | // ---------------------------------------------------<br />
| + | while (parse) |
− | // Player_3
| + | { |
− | 0 0 0 0
| + | Player *p = parse->item; |
− | 0<br /> // Player_4
| |
− | 0 0 0 0
| |
− | 0<br /> // Player_5
| |
− | 0 0 0 0
| |
− | 0<br /> // TOTAL:
| |
− | 0 0 0 0
| |
− | 0<br />
| |
− | // ---------------------------------------------------<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | for (freq=freqcount-1; freq>=0; freq--)<br />
| |
− | {<br />
| |
− | char str[255];<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | sendFreqs("---------------------------------------------------");<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | sprintf(str, "Squad: %-20s K D TK DMG DEALT TAKEN", freqs[freq].freqname);<br />
| |
− | sendFreqs(str);<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | sendFreqs("---------------------------------------------------");<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | for (pilot=freqs[freq].playercount-1; pilot >= 0; pilot--)<br />
| |
− | {<br />
| |
− | sprintf(str, "%-20s
| |
− | %8d %2d %2d %9d %5d", freqs[freq].pilots[pilot].name, freqs[freq].pilots[pilot].kills,<br />
| |
− |
| |
− | freqs[freq].pilots[pilot].deaths, freqs[freq].pilots[pilot].teamkills,
| |
− | freqs[freq].pilots[pilot].dmgdealt,<br />
| |
− |
| |
− | freqs[freq].pilots[pilot].dmgtaken);<br />
| |
− | sendFreqs(str);<br />
| |
− | }<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | sprintf(str, "TOTAL:
| |
− | %2d %2d %2d %9d %5d", freqs[freq].kills, freqs[freq].deaths, <br />
| |
− | freqs[freq].teamkills, freqs[freq].dmgdealt, freqs[freq].dmgtaken);<br />
| |
− | sendFreqs(str);<br />
| |
− | }<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | sendFreqs("---------------------------------------------------");<br />
| |
− | }<br />
| |
− |
| |
− |
| |
− | </font></blockquote><font size="2">
| |
− |
| |
− |
| |
− | <a name="15k"></a> k) Example of checking if any pilots are
| |
− | within a region
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2">// see GetPilotName(name) function in other example, returns *player as TempPlayer from name<br />
| |
− | // closeto() function from several previous examples<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | bool botInfo::FreqAInBox()<br />
| |
− | {<br />
| |
− | // return true if teamA has a pilot in the box, otherwise false<br />
| |
− | for (int tempplayercount = freqs[0].playercount-1; tempplayercount >= 0; tempplayercount--)<br />
| |
− | if (GetPilotName(freqs[0].pilots[tempplayercount].name))<br />
| |
− | if (closeto(TempPlayer,
| |
− | coordX, coordY, 73) && (TempPlayer->ship != SHIP_Spectator))<br />
| |
− | return true;<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | return false;<br />
| |
− | }<br />
| |
− |
| |
− |
| |
− | </font></blockquote><font size="2">
| |
− |
| |
− |
| |
− | <a name="15l"></a>
| |
− | l) Example of functions to get a pilot's struct id info from a name or *player info<br />
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2">// see struct examples for variable info<br />
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− | // return struct freq/pilot id from *player info<br />
| |
− | bool botInfo::GetPilot(Player *p)<br />
| |
| | | |
− | {<br />
| + | // convert to lowercase to compare |
| + | strncpy(pname,p->name,20); |
| + | tolower(pname); |
| + | if (strcmp(pname,nname)==0) |
| + | return p; |
| + | |
| + | parse = parse->next; |
| + | } |
| | | |
− | // return freq, pilot of a player p<br />
| + | return NULL; //player not found |
| + | } |
| + | </pre> |
| | | |
− | for (freq=freqcount-1; freq>=0; freq--)<br />
| + | ==Bot built in functions== |
| | | |
− | if (p->team == freqs[freq].freqteam)<br />
| + | Here are some useful MervBot commands to control what the bot is doing. |
| | | |
− | for (pilot = freqs[freq].playercount-1; pilot>=0; pilot--)<br />
| + | Player.cpp: |
− |
| + | * Player::move(Sint32 x, Sint32 y) moves a player to the coordinates specified by x and y |
− | if (strcmp(p->name,freqs[freq].pilots[pilot].name)==0)<br />
| + | * Player::clone(Player *p) clones a player into a player class |
| | | |
− | return true;<br /> | + | Look in Commands.txt , command.cpp (core), or /!help to bot to see all bot external commands (example /!go <arena>). |
− |
| |
− |
| |
− |
| |
− | <br />
| |
| | | |
− | return false;<br />
| + | ''LVZ Object toggling commands in plugins are to go here.'' |
| | | |
− | }<br />
| + | [[Category:Guides]] |
− |
| |
− |
| |
− | <br />
| |
− | // return *player as TempPlayer info from p->name info<br />bool botInfo::GetPilotName(char *name)<br />
| |
− | {<br />
| |
− | // get pilot from a name, return as TempPlayer<br />
| |
− | _listnode <Player> *parse = playerlist->head;<br />
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− | while (parse)<br />
| |
− | {<br />
| |
− | Player *p = parse->item;<br />
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− | // convert both to lowercase to compare<br />
| |
− | char pname[20];
| |
− | <br />
| |
− | strncpy(pname,p->name,20);<br />
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− | char nname[20];
| |
− | <br />
| |
− | strncpy(nname,name,20);<br />
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− | tolower(pname);<br />
| |
− | tolower(nname);<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | if (strcmp(pname,nname)==0)<br />
| |
− | {<br />
| |
− | TempPlayer = p;<br />
| |
− | return true;<br />
| |
− | }<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | parse = parse->next;<br />
| |
− | }<br />
| |
− | return false;<br />
| |
− | }<br />
| |
− | <br />
| |
− | // note: better to implement these functions as passing values by reference instead of using global variables<br />
| |
− | // just easier to not have to be declaring different int freq, int pilot all the time<br />
| |
− |
| |
− |
| |
− |
| |
− | </font></blockquote><font size="2">
| |
− |
| |
− |
| |
− | <a name="15m"></a>
| |
− | m) Example of creating a logfile name using date and squad names<br />
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2"> // create log file name (squadA and squadB external char[20] variables)<br />
| |
− | char u[100];<br />
| |
− | time_t t=time(NULL);<br />
| |
− | tm *tmp = localtime(&t);<br />
| |
− | strftime(u,99,"%y",tmp);<br />
| |
− | logname = "c:\Program Files\Continuum\logs\";<br />
| |
− | logname += u;<br />
| |
− | logname += "y";<br />
| |
− | strftime(u,99,"%m",tmp);<br />
| |
− | logname += u;<br />
| |
− | logname += "m";<br />
| |
− | strftime(u,99,"%d",tmp);<br />
| |
− | logname += u;<br />
| |
− | logname += "d";<br />
| |
− | logname += squadA;<br />
| |
− | logname += " vs ";<br />
| |
− | logname += squadB;<br />
| |
− | strftime(u,99,"%I",tmp);<br />
| |
− | logname += u;<br />
| |
− | logname += "h";<br />
| |
− | strftime(u,99,"%M",tmp);<br />
| |
− | logname += u;<br />
| |
− | logname += "m";<br />
| |
− | logname += ".txt";<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | // example name created: 03y01m27dBLACKDRaGON vs Integral05h08m.txt<br />
| |
− | // format year, month, day, squadA vs squadB, hour, minute<br />
| |
− |
| |
− |
| |
− | </font></blockquote>
| |
− | <font size="2">
| |
− |
| |
− |
| |
− | <a name="15n"></a>
| |
− | n) Example of sending messages to playing freqs or public and logging depending on status<br />
| |
− |
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2">// teamA, teamB, logname global variables<br />
| |
− | void botInfo::sendFreqs(char *msg)<br />
| |
− | {<br />
| |
− | char *mmsg = "*arena";<br />
| |
− | String s = msg;<br />
| |
− | <br />
| |
− | if (teammsgs == false)<br />
| |
− | {<br />
| |
− | s.prepend("*arena ",7);<br />
| |
− | sendPublic(s);<br />
| |
− | }<br />
| |
− | else<br />
| |
− | {<br />
| |
− | sendTeamPrivate(8025,msg);<br />
| |
− | sendTeamPrivate(teamA,msg);<br />
| |
− | sendTeamPrivate(teamB,msg);<br />
| |
− | }<br />
| |
− | if (gameon == true)<br />
| |
− | {<br />
| |
− | ofstream outf(logname, ios::app);<br />
| |
− | outf << msg << endl;<br />
| |
− | outf.close();<br />
| |
− | }<br />
| |
− | }<br />
| |
− |
| |
− |
| |
− | </font></blockquote><font size="2">
| |
− |
| |
− |
| |
− | <a name="15o"></a>
| |
− | o) Example of reading in all player/freqs to struct data<br />
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2">// 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 />
| |
− | // read pilots into freq struct data from ingame and on playing freqs<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | _listnode <Player> *parse = playerlist->head;<br />
| |
− | <br />
| |
− | while (parse)<br />
| |
− | {<br />
| |
− | Player *p = parse->item;<br />
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− | if (p->ship != SHIP_Spectator)<br />
| |
− | if (closeto(p, coordX, coordY, 73))<br />
| |
− | {<br />
| |
− | // look for freq in struct<br />
| |
− | bool foundfreq=false;<br />
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− | freq=freqcount-1;<br />
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− | while ((freq>=0) && (foundfreq==false))<br />
| |
− | {<br />
| |
− |
| |
− | if (p->team == freqs[freq].freqteam)<br />
| |
− | {<br />
| |
− |
| |
− | foundfreq=true;<br />
| |
− |
| |
− | strncpy(freqs[freq].pilots[freqs[freq].playercount].name,
| |
− | p->name, 20);<br />
| |
− |
| |
− | freqs[freq].playercount++;
| |
− | <br />
| |
− | }<br />
| |
− | freq--;<br />
| |
− | }<br />
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− | // didnt find freq in struct so add new freq<br />
| |
− | if (foundfreq == false)<br />
| |
− | {<br />
| |
− | if (manualsquads == false)<br />
| |
− | {<br />
| |
− |
| |
− | strncpy(freqs[freqcount].freqname,
| |
− | p->squad, 20);<br />
| |
− |
| |
− |
| |
− | <br />
| |
− |
| |
− | if (freqcount == 0)<br />
| |
− | {<br />
| |
− |
| |
− | teamA = p->team;<br />
| |
− |
| |
− | strncpy(squadA,
| |
− | p->squad, 20);<br />
| |
− | }<br />
| |
− | else<br />
| |
− | {<br />
| |
− |
| |
− | teamB = p->team;<br />
| |
− |
| |
− | strncpy(squadB,
| |
− | p->squad, 20);<br />
| |
− | }<br />
| |
− | }<br />
| |
− | else<br />
| |
− | {<br />
| |
− |
| |
− | if (p->team == teamA)<br />
| |
− |
| |
− | strncpy(freqs[freqcount].freqname,squadA,20);<br />
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− |
| |
− | else if (p->team == teamB)<br />
| |
− |
| |
− | strncpy(freqs[freqcount].freqname,squadB,20);<br />
| |
− | }<br />
| |
− | <br />
| |
− |
| |
− | freqs[freqcount].freqteam = p->team;<br />
| |
− |
| |
− |
| |
− | <br />
| |
− |
| |
− | strncpy(freqs[freqcount].pilots[0].name, p->name, 20);<br />
| |
− |
| |
− |
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− | freqs[freqcount].playercount++;<br />
| |
− | freqcount++;<br />
| |
− | }<br />
| |
− | }<br />
| |
− | parse = parse->next;<br />
| |
− | }<br />
| |
− | }<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | // to get freqs in a game where there are only two teams<br />
| |
− | void botInfo::GetFreqs()<br />
| |
− | {<br />
| |
− | // read pilots into freq struct data from ingame and on playing freqs<br />
| |
− | _listnode <Player> *parse = playerlist->head;<br />
| |
− | <br />
| |
− | while (parse)<br />
| |
− | {<br />
| |
− | Player *p = parse->item;<br />
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− | if ((p->ship != SHIP_Spectator)
| |
− | && ((p->team == teamA) || (p->team == teamB)))<br />
| |
− | {<br />
| |
− | // freq 100, team A<br />
| |
− | // set freq<br />
| |
− | freq = 0;<br />
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− | if (p->team == teamB)<br />
| |
− | freq = 1;<br />
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− | // number of pilots on freq counted so far, starts 0<br />
| |
− | pilot = freqs[freq].playercount;<br />
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− | // pilot name<br />
| |
− | strncpy(freqs[freq].pilots[pilot].name, p->name, 20);<br />
| |
− | // time stamp for playing time<br />
| |
− | freqs[freq].pilots[pilot].cplaying_time = GetTickCount();<br />
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− | // slot name<br />
| |
− | if (freqs[freq].playercount < NUMBER_PILOTS)<br />
| |
− |
| |
− | strncpy(freqs[freq].slotname[pilot], p->name, 20);<br />
| |
− | <br />
| |
− | // increment freq player count<br />
| |
− | freqs[freq].playercount++;<br />
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− | // if freq not already have name, give it player squad name<br />
| |
− | if ((manualsquads == false) && (strlen(p->squad) > 0))<br />
| |
− | strncpy(freqs[freq].freqname, p->squad, 20);<br />
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− | // set player ship<br />
| |
− | freqs[freq].pilots[pilot].ship = p->ship + 1;<br />
| |
− | }<br />
| |
− | parse = parse->next;<br />
| |
− | }<br />
| |
− | }<br />
| |
− |
| |
− |
| |
− | </font></blockquote><font size="2">
| |
− |
| |
− |
| |
− | <a name="15p"></a>
| |
− | p) Example of finding MVP from struct data (2*kills - deaths formula)<br />
| |
− |
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2"> int highest=-20;<br />
| |
− | int mvp=0;<br />
| |
− |
| |
− |
| |
− | <br />
| |
− | for (pilot = freqs[mvpteam].playercount-1; pilot >=0; pilot--)<br />
| |
− | {<br />
| |
− | if (((freqs[mvpteam].pilots[pilot].kills
| |
− | * 2) - freqs[mvpteam].pilots[pilot].deaths) > highest)<br />
| |
− | {<br />
| |
− | mvp = pilot;<br />
| |
− | highest = (freqs[mvpteam].pilots[pilot].kills
| |
− | * 2) - freqs[mvpteam].pilots[pilot].deaths;<br />
| |
− | }<br />
| |
− | }<br />
| |
− |
| |
− |
| |
− | </font></blockquote><font size="2">
| |
− |
| |
− |
| |
− | <a name="15q"></a>
| |
− | q) Print time stamp of event<br />
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2">#include "time.h"<br />
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− | char u[100];<br />
| |
− | time_t t=time(NULL);<br />
| |
− | tm *tmp = localtime(&t);<br />
| |
− | strftime(u,99,"%c",tmp);<br />
| |
− | sendPublic("Current date and time: " + (String) u);<br />
| |
− |
| |
− |
| |
− | </font></blockquote><font size="2">
| |
− |
| |
− |
| |
− | <a name="15r"></a>
| |
− | r) Simple way to track player bomb/bullet damage stats<br />
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2">// spawn.cpp (see data section for how to setup set_tag)<br />
| |
− | // see clientprot.h for weapon information<br />
| |
− | <br />
| |
− | case EVENT_WatchDamage:<br />
| |
− | {<br />
| |
− | if (PLAYING) // if tracking stats<br />
| |
− | {<br />
| |
− |
| |
− | if ((wi.type == PROJ_PBomb) && (p->name != k->name))<br />
| |
− | {<br />
| |
− |
| |
− | set_tag(k, DMG_BOMB_DEALT, get_tag(k, DMG_BOMB_DEALT)
| |
− | + damage);<br />
| |
− |
| |
− | set_tag(k, DMG_TOTAL_DEALT, get_tag(k, DMG_TOTAL_DEALT)
| |
− | + damage);<br />
| |
− |
| |
− | set_tag(p, DMG_BOMB_TAKEN, get_tag(p, DMG_BOMB_TAKEN)
| |
− | + damage);<br />
| |
− |
| |
− | set_tag(p, DMG_TOTAL_TAKEN, get_tag(p, DMG_TOTAL_TAKEN)
| |
− | + damage);<br />
| |
− | }<br />
| |
− | else if (wi.type == PROJ_BBullet)<br />
| |
− | {<br />
| |
− |
| |
− | set_tag(k, DMG_BULLET_DEALT, get_tag(k, DMG_BULLET_DEALT)
| |
− | + damage);<br />
| |
− |
| |
− | set_tag(k, DMG_TOTAL_DEALT, get_tag(k, DMG_TOTAL_DEALT)
| |
− | + damage);<br />
| |
− |
| |
− | set_tag(p, DMG_BULLET_TAKEN, get_tag(k, DMG_BULLET_TAKEN)
| |
− | + damage);<br />
| |
− |
| |
− | set_tag(p, DMG_TOTAL_TAKEN, get_tag(k, DMG_TOTAL_TAKEN)
| |
− | + damage);<br />
| |
− | }<br />
| |
− | }<br />
| |
− |
| |
− |
| |
− |
| |
− | </font></blockquote><font size="2">
| |
− |
| |
− |
| |
− | <a name="15s"></a>
| |
− | s) Simple way to print those stats<br />
| |
− |
| |
− |
| |
− | </font><blockquote><font size="2">case OP_Moderator:<br />
| |
− | {<br />
| |
− | if (c->check("showstats"))<br />
| |
− | {<br />
| |
− | sendPublic("Showing stats:");<br />
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− |
| |
− | _listnode <Player> *parse = playerlist->head;<br />
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− | while (parse) <br />
| |
− | {<br />
| |
− | Player *p = parse->item;<br />
| |
− |
| |
− |
| |
− |
| |
− | <br />
| |
− |
| |
− | if (get_tag(p, DMG_TOTAL_DEALT) > 0)<br />
| |
− | {<br />
| |
− | char str[256];<br />
| |
− |
| |
− | sprintf(str, "(%-20s Dmg Dealt: Total
| |
− | %0004d, Bomb %0004d, Bullet %0004d Dmg TAKEN: Total %0004d, Bomb %0004d,
| |
− | Bullet %0004d)", <br />
| |
− |
| |
− | p->name, get_tag(p,DMG_TOTAL_DEALT),
| |
− | get_tag(p,DMG_BOMB_DEALT), get_tag(p,DMG_BULLET_DEALT), <br />
| |
− |
| |
− | get_tag(p,DMG_TOTAL_TAKEN), get_tag(p,DMG_BOMB_TAKEN),
| |
− | get_tag(p,DMG_BULLET_TAKEN));<br />
| |
− |
| |
− | sendPublic(str);<br />
| |
− | }<br />
| |
− | parse = parse->next;<br />
| |
− | }<br />
| |
− | }<br />
| |
− |
| |
− |
| |
− |
| |
− | </font></blockquote><font size="2"><a name="15t"></a>
| |
− | t) Make bot spectate specific coordinates<br />
| |
− | </font><blockquote><font size="2">// make bot spectate the coord 512,600<br />
| |
− | // possible use - capturing weapon packets in a specific region<br />
| |
− | <br />
| |
− | tell(makeFollowing(false));<br />
| |
− | tell(makeFlying(true));<br />
| |
− | me->move(512 * 16, 600 * 16);<br />
| |
− | tell(makeSendPosition(true));<br />
| |
− | </font></blockquote>
| |
− | </blockquote></blockquote>
| |
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.
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).
This section describes how to implement player commands into your plugin. Commands are sent to the botInfo::gotCommand function in command.cpp.
Use the CRT function sscanf() to scan the string for the values.
When a player sends !help to the bot, MERVBot calls botInfo::gotHelp() in each plugin loaded.
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().
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
An example of using normal strings to output data/messages.
An example using sprintf to align/space data, where output data will be in this approximate format.
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.
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.
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.
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:
If you're not familiar with prototypes, notice it is similar to that in your spawn.cpp, but without the botInfo::, and a trailing ;.
From time to time you will need to pass an array to a function. An example illustrating this is:
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:
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:
To use, just call the function with the appropriate Ship_Type from the enum in clientprot.h:
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.
Player tags simply tag a player with a number. Define the wanted values in spawn.h at the top:
The following demonstrates how to retrieve the tag values as a command in command.cpp:
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.
For reading and/or writing to files with C++ you must have the required include statement as follows:
The following example will show you how to read a file, duel.ini, line by line.
The following code example will demonstrate how to append to a file, duelleaguestat.inc.
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.
As stated earlier in the tutorial, MervBot stores useful player data internally as Player objects, see player.h for implementation details.
Just access the respective member of the Player class to check the player's property.
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.
Here are some useful MervBot commands to control what the bot is doing.
Look in Commands.txt , command.cpp (core), or /!help to bot to see all bot external commands (example /!go <arena>).