Quake DeveLS - Auto Bindings

Author: Kieren Johnstone a.k.a. -X'BuG-
Difficulty:
Easy

In this tutorial, I'll be showing you how to create auto bindings. This means, when a client connects to a server with your mod on, they'll automatically get some bindings sent to them.

·  Please that this tutorial assumes you have downloaded the q_devels.c functions, which always help out !!

·  You should also have the 3.20 source code.

So, lets get on with this...create a new file to hold our functions called p_autobind.c.
Now, paste the following code into it:

 
/*
=================
p_autobind.c
 
Functions for automatic binds
and/or aliases on client
connection.
=================
*/
 
#include "q_devels.h"   // QDeveLS functions
#include "g_local.h"    // Standard include header
#include "p_autobind.h" // AutoBind header
 
void CreateBind (edict_t *ent,char *key, char *command)
{
        // CreateBind binds the key "key" to the command "command" for the
        // client *ent;
        // * If required, add \" quote characters around commands and/or
        //   keys
 
        stuffcmd ("bind ",ent);
        stuffcmd (key,ent);
        stuffcmd (" ",ent);
        stuffcmd (command,ent);
        stuffcmd ("\n",ent);
}
 
 
void CreateAlias (edict_t *ent,char *name, char *commands)
{
        // CreateAlias - similar to "alias  ".  Sends to
        // client *ent;
 
        stuffcmd ("alias ",ent);
        stuffcmd (name,ent);
        stuffcmd (" ",ent);
        stuffcmd (commands,ent);
        stuffcmd ("\n",ent);
}

And then, you make the header file, p_autobind.h. Put this text into it to provide prototypes for the command functions we just added:

 
/*
=================
p_autobind.h
 
Header for autobind additions
to Quake 2
=================
*/
 
void CreateBind (edict_t *ent,char *key, char *command);
void CreateAlias (edict_t *ent,char *name, char *commands);

Then add these files to your Quake 2 project, and open up p_client.c. At the top of this file, add a

#include "p_autobind.h"

statement.
Go to the function ClientBeginDeathmatch - this is called in each game just before a client is about to spawn in a server.
Somewhere after the line containing PutClientInServer, add the following code:

 
        // display information message
        gi.cprintf(ent,PRINT_MEDIUM,"Binding default keys....\n");
 
        // create a bind
        CreateBind(ent,"mouse1","+attack");
        gi.cprintf(ent,PRINT_MEDIUM,"Left mouse button  = fire\n");
 
        // create another bind
        CreateBind(ent,"mouse2","+movedown");
        gi.cprintf(ent,PRINT_MEDIUM,"Right mouse button = crouch\n");
 
        // create yet another bind (see scanner tutorial)
        CreateBind(ent,"mouse3","scanner");
        gi.cprintf(ent,PRINT_MEDIUM,"Middle button      = toggle scanner\n");

and add whatever binds and/or aliases you want to do!!
That's it, I hope you enjoyed coding it. If you didn't, e-mail me.


Tutorial by Kieren Johnstone, author of forthcoming mod CLOT.

This site, and all content and graphics displayed on it,
are ©opyrighted to the Quake DeveLS team. All rights received.
Got a suggestion? Comment? Question? Hate mail? Send it to us!
Oh yeah, this site is best viewed in 16 Bit or higher, with the resolution on 800*600.
Thanks to Planet Quake for there great help and support with hosting.
Best viewed with Netscape 4 or IE 5