QuakeZone
MOD Coding Tutorials
Quake II Coding Tutorials!

Introduction
Code
Notes
Exercises
Quake II Coding Tutorials

Regenerative Healthbox

Introduction Introduction

This tutorial shows you how to add a Lox-style regenerative healthbox to your mod. When you pick up a health item, you have a small chance of it being a healthbox. When it is, your health quickly ramps up to 400, and when you take damage this value is quickly restored.

Code Code


g_local.h

At the end of the client_persistant_t struct, add the following line:

	qboolean	health_box;			// PJT - healthbox	

g_items.c

Add the following lines as indicated into function Pickup_Health:

	if (!(ent->style & HEALTH_IGNORE_MAX))
		if (other->health >= other->max_health)
			return false;

	// PJT - health box
	if (!other->client)
		return false;
	if (other->client->pers.health_box)
		return false;
	if (random() < 0.10)	// change to 0.5 for testing/debugging
	{
		gi.cprintf (other, PRINT_HIGH, "Regeneration healthbox picked up\n");
		other->max_health = 400;
		other->client->pers.health_box = true;
		if (!(ent->spawnflags & DROPPED_ITEM) && (deathmatch->value))
			SetRespawn (ent, 30);
		return true;
	}
	// PJT	

p_client.c

Add the following code as indicated into function ClientThink:

	level.current_entity = ent;
	client = ent->client;

	// PJT - health box
	if ((!ent->deadflag) && (client->pers.health_box) && (ent->health < ent->max_health))
		ent->health += 1;
	// PJT	


Notes Notes to code


g_local.h
  • a flag in the client structure to indicate ownership of the healthbox.
  • note that this is in the persistant part of the client structure, and a healthbox will be retained across game loads

g_items.c
  • this code gives the player a healthbox at random when picking up health (0.1 chance)
  • a client-only (i.e. not a monster) entity can pick up a healthbox, because the healthbox flag is stored in the client structure
  • if player already has a healthbox, he cannot pick up health
  • when a healthbox is picked up, following happens:
    • a message is sent to player
    • maximum health is set to 400
    • healthbox flag is set
    • item re-spawn is set if necessary, and return true to incidate successful pick up of a health item

p_client.c
  • the ClientThink routine processes a client every Quake frame
  • if a player is not dead, has healthbox and health is less than maximum, increments player health - this quickly restores player health to 400 when damage taken
Exercises Exercises

  • replace the healthbox switch in the client structure as an item that is given to the player, so the healthbox can be seen in the inventory


Page last updated 25th October 2000