Tutorial: Add Vote-Map to most any mod

Discuss programming in the QuakeC language.
Post Reply
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Tutorial: Add Vote-Map to most any mod

Post by Baker »

This work is my documentation of a hard work of a project done by Bam in late 2007. I attempted to post this here as QuakeC tutorial last year, but I wasn't phyiscally around a lot and had some questions about the formatting and due to frequent lack of internet availability, I never got around to doing it. (Original thread for reference: link)

Anyway, I think Inside3D should serve as the central repository of all QuakeC information:

Foreword

Most of the popular mods have some form of map voting, but the burden upon a QuakeC coder to implement a map voting system is high and usually a foreign and rather technical and painful process for someone who typically is writing a mod for fun and doesn't want to be bogged down with figuring out "administrative code".

Instructions:

1. Add bindings.qc and vote.qc to your QuakeC source folder.
Download: bindings.qc
Download: vote.qc
You will need to edit vote.qc if the mod or the server you intend to use this on is running something other than the standard Quake maps. The changes necessary are pretty obvious.
Just keep in mind that you will need to change line 241 to expand/contract the impulse range if the number of voteable maps change:
Line 241 of vote.qc will need changed if voteable maps are customized

if (self.impulse >= 1 && self.impulse <= 38 )
2. In progs.src, you need to add vote.qc and bindings.qc
Add highlighted lines to your progs.src

../progs.dat

defs.qc
settings.qc // Add if this does not exist
func.qc
subs.qc
fight.qc
ai.qc
combat.qc
items.qc
vote.qc // Must be before weapons.qc
weapons.qc
world.qc
bindings.qc // Must be before client.qc
client.qc
player.qc
monsters.qc
doors.qc
buttons.qc
triggers.qc
plats.qc
misc.qc
3. Add this to the end of defs.qc

These are new variables and constants to support the vote-map functionality.
Add to end of defs.qc

//BAM ADD
.float pflag, vflag;
.float vdelay, vcount;
.float primary_impulse_hack;
string request_mapname;

//==============
// client.pflag
//==============
// client.pflag is used for random things to remember
// about a player, and its value is saved across levels

float POQ_NEW_CLIENT = 1;
float POQ_BINDINGS_SENT = 2;
float POQ_VOTE_IDENTIFY = 4;

//==============
// client.vflag
//==============
// client.vflag is used for voting

float POQ_VOTE_YES = 1;
float POQ_VOTE_NO = 2;
4. Create or add this to settings.qc

Adjust the impulses as appropriate for your mod. Your mod might already use these impulses or may prefer to change them. Impulses can be from 1 to 255, but generally the lower impulses are for weapons and such.
Add to end of settings.qc, if settings.qc does not exist then create it.

float VOTE_PRIMARY_IMPULSE = 97;
float VOTE_YES_IMPULSE = 98;
float VOTE_NO_IMPULSE = 99;
float VOTE_PRINT_DELAY = 10;
float VOTE_PRINT_COUNT = 5;
5. Add this to the end of func.qc

To support the new functions.
Add to end of func.qc

//BAM ADD
void () vote_init_consider;
void () vote_aliases;
void (string s, void () func) vote_init;
void (float print) vote_print;
void (string mode_name, void () func) vote_spawn;
6. Open Weapons.qc and modify as follows

a. Find void() ImpulseCommands and highlighted text to an appropriate place in the if statements block.
Add highlighted lines to ImpulseCommands in weapons.qc

void() ImpulseCommands =
{

if (self.impulse >= 1 && self.impulse <= 8 )
W_ChangeWeapon ();

if (self.impulse == 9)
CheatCommand ();
if (self.impulse == 10)
CycleWeaponCommand ();
if (self.impulse == 11)
ServerflagsCommand ();
if (self.impulse == 12)
CycleWeaponReverseCommand ();
if (self.impulse == VOTE_YES_IMPULSE || self.impulse == VOTE_NO_IMPULSE)
vote_impulse ();
if (self.impulse == VOTE_PRIMARY_IMPULSE)
{
self.pflag = self.pflag | POQ_VOTE_IDENTIFY;
self.primary_impulse_hack = time + 0.3;
return;
}


if (self.impulse == 255)
QuadCheat ();

self.impulse = 0;
};
b. Find void() W_WeaponFrame and insert highlighted text.

This is where the impulses that control map voting are checked.
Add Highlighted code as follows to W_WeaponFrame in weapons.qc

void() W_WeaponFrame =
{
if (time < self.attack_finished)
return;

if (self.impulse)
{
//BAM: Primary/Secondary vote impulse handling
if (!self.pflag & POQ_VOTE_IDENTIFY)
ImpulseCommands ();
else
{
// BAM: If a player uses the primary impulse
// (impulse <VOTE_PRIMARY_IMPULSE>) it goes into an endless loop.
if (self.primary_impulse_hack < time)
{
self.pflag = self.pflag - (self.pflag & POQ_VOTE_IDENTIFY);
self.impulse = 0;
}
else
vote_init_consider ();
}
}


// check for attack
if (self.button0)
{
SuperDamageSound ();
W_Attack ();
}
};
7. Finally open client.qc for the following adjustments

You may need to adjust this for some mods. This is where is bindings are sent to the client.

The process works like this: a client connects and it will need the vote aliases sent to it and .pflag is keeps track of whether or not the aliases have been sent yet or not. This needs to preserve across map changes because clients only need the aliases sent once.

If a client connects, it automatically needs them. If a client disconnects, this must be reset and the voting state of the client needs reset as well (.vflag).
Near the top, preferably right before void() SetChangeParms add this function in client.qc

void ()
SetNewParms2 =
{
parm1 = IT_SHOTGUN | IT_AXE;
parm2 = 100;
parm3 = 0;
parm4 = 25;
parm5 = 0;
parm6 = 0;
parm7 = 0;
parm8 = 1;
parm9 = 0;
parm10 = self.pflag;
};
Now in SetChangeParms, add the highlighted code

void ()
SetChangeParms =
{
if (self.health <= 0)
{
SetNewParms2 ();
return;
}

//remove items
self.items = self.items - (self.items &
(IT_KEY1 | IT_KEY2 | IT_INVISIBILITY | IT_INVULNERABILITY | IT_SUIT | IT_QUAD) );

//cap super health
if (self.health > 100)
self.health = 100;
if (self.health < 50)
self.health = 50;
parm1 = self.items;
parm2 = self.health;
parm3 = self.armorvalue;
if (self.ammo_shells < 25)
parm4 = 25;
else
parm4 = self.ammo_shells;
parm5 = self.ammo_nails;
parm6 = self.ammo_rockets;
parm7 = self.ammo_cells;
parm8 = self.weapon;
parm9 = self.armortype * 100;
parm10 = self.pflag;
};
Add this single highlighted line to SetNewParms in client.qc

void ()
SetNewParms =
{
parm1 = IT_SHOTGUN | IT_AXE;
parm2 = 100;
parm3 = 0;
parm4 = 25;
parm5 = 0;
parm6 = 0;
parm7 = 0;
parm8 = 1;
parm9 = 0;
parm10 = POQ_NEW_CLIENT; // New client! They will need aliases sent!
};
Add/change highlighted lines in DecodeLevelParms in client.qc

void ()
DecodeLevelParms =
{
if (serverflags)
{
if (world.model == "maps/start.bsp")
SetNewParms2 (); // This WAS SetNewParms(); but we are changing!
}

self.items = parm1;
self.health = parm2;
self.armorvalue = parm3;
self.ammo_shells = parm4;
self.ammo_nails = parm5;
self.ammo_rockets = parm6;
self.ammo_cells = parm7;
self.weapon = parm8;
self.armortype = parm9 * 0.01;
self.pflag = parm10; // Need to preserve state of whether aliases have been sent
};
Change highlighted line in respawn in client.qc

void ()
respawn =
{
if (coop)
{
// make a copy of the dead body for appearances sake
CopyToBodyQue (self);
// get the spawn parms as they were at level start
setspawnparms (self);
// respawn
PutClientInServer ();
}
else if (deathmatch)
{
// make a copy of the dead body for appearances sake
CopyToBodyQue (self);
// set default spawn parms
SetNewParms2 (); // Was SetNewParms();
// respawn
PutClientInServer ();
}
else
{ // restart the entire server
localcmd ("restart\n");
}
};
Change code in ClientConnect in client.qc to mirror this

void ()
ClientConnect =
{
// a client connecting during an intermission can cause problems
if (intermission_running)
ExitIntermission ();

if (parm10 & POQ_NEW_CLIENT) // this is a new client
{
bprint (self.netname); // Note these 2 lines have been moved
bprint (" entered the game\n"); // to below the intermission check to ensure aliases are sent


player_bindings_begin (); // send aliases
parm10 = parm10 - POQ_NEW_CLIENT;
}

};
Finally ...
Add to end of ClientDisconnect in client.qc

void ()
ClientDisconnect =
{
if (gameover)
return;
// if the level end trigger has been activated, just return
// since they aren't *really* leaving

// let everyone else know
bprint (self.netname);
bprint (" left the game with ");
bprint (ftos(self.frags));
bprint (" frags\n");
sound (self, CHAN_BODY, "player/tornoff2.wav", 1, ATTN_NONE);
set_suicide_frame ();
self.pflag = 0; // Reset "alias sent" status
self.vflag = 0; // Reset voting state (yes/no) status

};
ceriux
Posts: 2230
Joined: Sat Sep 06, 2008 3:30 pm
Location: Indiana, USA

Post by ceriux »

good tutorials :D wish there was more like it, it explains more... so not only can u take ur time to read through the qc, but you also get to see what certain parts do exactly. do you think you could add a weapon changing and adding tutorial like it? maybe with some kind of moc weapon thats easy to edit?
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Post by Baker »

ceriux wrote:good tutorials :D wish there was more like it, it explains more... so not only can u take ur time to read through the qc, but you also get to see what certain parts do exactly. do you think you could add a weapon changing and adding tutorial like it? maybe with some kind of moc weapon thats easy to edit?
I wrote this tutorial based on someone else's work. It was sort of a cooperative effort.

I'm great at documentation but suck at QuakeC. I don't get to use QuakeC very often because my time is spent modifying engine stuff.

As a result, I spend most of my time writing engine stuff so I never get a lot of time to get serious about QuakeC.

Sorry. :(

[Things would be different if I could replicate myself via fission like blue-green algae do, but alas my efforts have so far been unsuccessful. :D But I haven't entirely given up on the idea.]
ceriux
Posts: 2230
Joined: Sat Sep 06, 2008 3:30 pm
Location: Indiana, USA

Post by ceriux »

lol would be cool. once i get good at QC im going to try out engine coding, but i think i should learn the easyier of the two before i move on to the next.
Lardarse
Posts: 266
Joined: Sat Nov 05, 2005 1:58 pm
Location: Bristol, UK

Post by Lardarse »

Nice tutorial. There's some mismatched tags near the end, though. Remember: last in, first out.
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Post by Baker »

Lardarse wrote:Nice tutorial. There's some mismatched tags near the end, though. Remember: last in, first out.
LA, could you explain in more detail ....
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Post by Baker »

Note for future:

This code would be even better if it read maxplayers value and had at least an option to bypass all this if == 1 (aka single player or someone just walking around a map even if deathmatch isn't 0).

Vote-map is pointless when run locally and the above method is somewhat inappropriate for someone playing single player.

/Maybe create a mini-utility or javascripty page to easily generate the vote.qc code based on a map list.
Electro
Posts: 312
Joined: Wed Dec 29, 2004 11:25 pm
Location: Brisbane, Australia
Contact:

Post by Electro »

Shouldn't check the maxplayers for that really, should just check the number of players actually in the game. So it'd work in dm if they're the only person on the server.
Benjamin Darling
http://www.bendarling.net/

Reflex - In development competitive arena fps combining modern tech with the speed, precision and freedom of 90's shooters.
http://www.reflexfps.net/
Lardarse
Posts: 266
Joined: Sat Nov 05, 2005 1:58 pm
Location: Bristol, UK

Post by Lardarse »

Baker wrote:
Lardarse wrote:Nice tutorial. There's some mismatched tags near the end, though. Remember: last in, first out.
LA, could you explain in more detail ....
In the Change code in ClientConnect in client.qc to mirror this section.
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

Post by Spike »

Just for reference, a common feature of QuakeWorld engines is that they forget any aliases that were sent to them during a map.
This started with fuhquake, I believe (maybe zquake?), but can be found in all popular clients. Well, ezquake and other fuhquake derivatives, and FTE.

Basically the following assumption (quoted from the initial post) is not true in the case of quakeworld: 'This needs to preserve across map changes because clients only need the aliases sent once.'

The first 'needs' is an exaggeration in any engine. :P


Oh... And fix the red armour precision bug some time. :P
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Post by Baker »

Spike wrote:Just for reference, a common feature of QuakeWorld engines is that they forget any aliases that were sent to them during a map.
This started with fuhquake, I believe (maybe zquake?), but can be found in all popular clients. Well, ezquake and other fuhquake derivatives, and FTE.
Is that the "clear all server aliases on disconnect" feature?

So that was added to ZQuake and not a feature of original Quakeworld then?

I think I noticed that a year ago. It sounds like a pretty solid idea of a client-side feature (avoids accumulated aliases that do who knows what if connecting from server to server).
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

Post by Spike »

Yeah, its important if you support saving all aliases into configs, or at least the tracking is.
I think it was added with fuhquake. Its certainly not a base feature, but its pretty much base in QW nowadays.
Worth a mention in your tutorial.
Post Reply