
/*
 * Copyright (c) 1991    Jon Wesener 
 * Gaming Software
 *
 * Permission to use, copy, modify, and distribute this
 * software and its documentation for any purpose and without
 * fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting
 * documentation.  No representations are made about the
 * suitability of this software for any purpose.  It is
 * provided "as is" without express or implied warranty.
 *
 */

#ident	"@(#)pow.c	1.6 :/usr/key/jojow/X/src/guts/SCCS/s.pow.c 3/19/91 10:15:59 "

/* pow.c - routines for handling power pellets */

#include "server.h"

#define ENERGYSTORM		200

powblow(tp,pp,x,y)
	tank_t *tp;
	pow_t *pp;
	int x, y;
{
	if( tp->t_energy < Tmaxenergy )
		tp->t_energy += pp->p_val;
	else
		Totenergy += pp->p_val;

	/* remove it from the list */
	if( Pvalid == pp )
		Pvalid = pp->p_next;
	else {
		pow_t *pr;

		for( pr = Pvalid ; pr->p_next != pp ; pr = pr->p_next );
		pr->p_next = pp->p_next;
	}

	pp->p_next = Pfree;
	Pfree = pp;

	field[x][y] = FSPACE;
	fpts[x][y] = NULL;
}


#define VALCNT	10
static int powposvals[VALCNT] = {1,10,10,10,20,20,50,50,75,100};

checkpows()
{
	pow_t *pp;
	int x, y, pv;


	if( Totenergy < ENERGYSTORM ) 
		return;

	while( Totenergy >= 0 ) {
		if( !(pp = Pfree) )
			break;

		Pfree = pp->p_next;

		do {
			x = random() % MAZE_XL;
			y = random() % MAZE_YL;
		} while( field[x][y] != FSPACE );

		field[x][y] = FPOW;
		fpts[x][y] = (void *)pp;
		pp->p_x = x;
		pp->p_y = y;
		pv = random() % VALCNT;
		pp->p_val = powposvals[pv];
		Totenergy -= powposvals[pv];

		pp->p_next = Pvalid;
		Pvalid = pp;
	}
}

powdest(tp)
	tank_t *tp;
{
	register pow_t *pp, *pr = NULL;


	/* Walk down the list and remove them */
	for( pr = pp = Pvalid ; pp ; pp = pp->p_next ) {
		pr = pp;
		tp->t_energy += pp->p_val;
		field[pp->p_x][pp->p_y] = FSPACE;
		fpts[pp->p_x][pp->p_y] = NULL;
	}
		
	/* move the entire list */
	if( Pvalid ) {
		pr->p_next = Pfree;
		Pfree = Pvalid;
		Pvalid = NULL;
	}
}

/* Lint Output
*/
