1

Topic: [TUTORIAL] Coding: How to add commands to Teeworlds

Hello everybody! Hope you're doing fine.
This tutorial is for adding and making commands.

Note that this tutorial has been made and tested in Teeworlds 0.6.4!
If you haven't compiled your Teeworlds yet, see the Teeworlds help.

Let's start coding now!

Modify gamecontext.cpp under src/game/server directory.
Open gamecontext.h, too.

In gamecontext.h, press Ctrl + F and search this:

// voting

After the code

void AbortVoteKickOnDisconnect(int ClientID);

add the following code:

void ChatCmds(const char *pMsg, int ClientID);

Save gamecontext.h and now go to gamecontext.cpp.

Add this code:

if(!strncmp(pMsg->m_pMessage,"/",1))
   {
       ChatCmds(pMsg->m_pMessage, pPlayer->GetCID());
       return;
   }

else
    SendChat(ClientID,Team,pMsg->m_pMessage); // The player simply sends a message

In the place of the following code:

pPlayer->m_pLastChat = Server()->Tick();
SendChat(ClientID, Team, pMsg->m_pMessage);

Commands aren't added yet, we'll need to create a new file. Create a file at src/game/server. Change the file name to chatcmds.cpp, or any other name you want.

We'll add some simple-useful commands.
Add the following code on chatcmds.cpp file:

#include "gamecontext.h"
#include "entities/character.h"
#include "player.h"

#include <stdio.h>
#include <stdlib.h>

#include <string.h>

void CGameContext::ChatCmds(const char *pMsg, int ClientID)
{

 pMsg ++; // no slash

if(!str_comp_nocase(pMsg, "cmdlist"))
{

SendChatTarget(ClientID, "-----------Command list-----------");
SendChatTarget(ClientID, "/info - Information about the mod/server");
SendChatTarget(ClientID, "---------------------------------------");

}

If you're gonna add more commands, you probably want to add them to the cmdlist command.
Example:

if(!str_comp_nocase(pMsg, "cmdlist"))
{

SendChatTarget(ClientID, "-----------Command list-----------");
SendChatTarget(ClientID, "/info - Information about the mod/server");
SendChatTarget(ClientID, "/rules - Shows server rules");
SendChatTarget(ClientID, "/help - Help for new players");
SendChatTarget(ClientID, "---------------------------------------");

}

Indeed, you obviously need to add the rules command and the help command, otherwise, those commands won't work.
See some examples of how to add those commands:

if(!str_comp_nocase(pMsg, "info"))
{

SendChatTarget(ClientID, "------------Information about the mod/server--------------");
SendChatTarget(ClientID, "Add information about the mod/server!");

}

Example of the rules command:

if(!str_comp_nocase(pMsg, "rules"))
{

SendChatTarget(ClientID, "Don't insult people! Don't be a traitor!");
SendChatTarget(ClientID, "Don't share accounts! Do not use bots!");
SendChatTarget(ClientID, "Don't cheat, exploit, or anything related about cheating!");
SendChatTarget(ClientID, "Do not bother or troll people!");

}

We'll add a check for non-existing commands.
After all the commands you added, add the following code:

else
    {
        char aBuf[256];
        str_format(aBuf, sizeof(aBuf), "No such command: '/%s'", pMsg); // Specifically tell the user what command
                                                                                                       // they type and if it's an invalid command
        SendChatTarget(ClientID, "-------------------------------");
        SendChatTarget(ClientID, aBuf);
        SendChatTarget(ClientID, "Type /cmdlist for more info.");
        SendChatTarget(ClientID, "-------------------------------");
        dbg_msg("command", "wrong command: /%s", pMsg); // Send debug message
        
    }

When having more than one command, at the beginning, the first command should have an if.
However, the second command shall have an else if at the beginning instead.

For example:

// First command
if(!str_comp_nocase(pMsg, "hello"))
{

SendChatTarget(ClientID, "Hello my dear friends!");

}

// Second command
else if(!str_comp_nocase(pMsg, "bye"))
{

SendChatTarget(ClientID, "Bye my dear friends!");

}
This is how chatcmds.cpp should look like now:
#include "gamecontext.h"
#include "entities/character.h"
#include "player.h"

#include <stdio.h>
#include <stdlib.h>

#include <string.h>

void CGameContext::ChatCmds(const char *pMsg, int ClientID)
{

 pMsg ++; // no slash

if(!str_comp_nocase(pMsg, "cmdlist"))
{

SendChatTarget(ClientID, "-----------Command list-----------");
SendChatTarget(ClientID, "/info - Information about the mod/server");
SendChatTarget(ClientID, "/rules - Shows server rules");
SendChatTarget(ClientID, "/help - Help for new players");
SendChatTarget(ClientID, "---------------------------------------");

}

else if(!str_comp_nocase(pMsg, "info"))
{

SendChatTarget(ClientID, "------------Information about the server/mod--------------");
SendChatTarget(ClientID, "Add information about the mod/server!");

}

else if(!str_comp_nocase(pMsg, "rules"))
{

SendChatTarget(ClientID, "Don't insult people! Don't be a traitor!");
SendChatTarget(ClientID, "Don't share accounts! Do not use bots!");
SendChatTarget(ClientID, "Don't cheat, exploit, or anything related about cheating!");
SendChatTarget(ClientID, "Do not bother or troll people!");

}

else
    {
        char aBuf[256];
        str_format(aBuf, sizeof(aBuf), "No such command: '/%s'", pMsg); // Specifically tell the user what command
                                                                                                       // they type and if it's an invalid command
        SendChatTarget(ClientID, "-------------------------------");
        SendChatTarget(ClientID, aBuf);
        SendChatTarget(ClientID, "Type /cmdlist for more info.");
        SendChatTarget(ClientID, "-------------------------------");
        dbg_msg("command", "Wrong command: /%s", pMsg); // Send debug message
        
    }
}

Compile the source code to get your commands!
Try to type on chat /cmdlist and it'll show you the list of commands!

If you had troubles understanding please tell me and I'll help you with your doubts. smile

Old Teeworlds ex-gamer | Teeworlds ex-tutorial maker

2

Re: [TUTORIAL] Coding: How to add commands to Teeworlds

I guess the best way in tw 0.6.x is to use the console system for chat commands.

3

Re: [TUTORIAL] Coding: How to add commands to Teeworlds

Console system? You mean the RCON? This is for all users, also not everybody knows how to use the console, it's easier to type in the chat to use commands. smile

Old Teeworlds ex-gamer | Teeworlds ex-tutorial maker