
/*
 * 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	"@(#)mine.c	1.7 :/usr/key/jojow/X/src/guts/SCCS/s.mine.c 3/19/91 10:15:55 "

/* mine.c - routines for handling mines */

#include "server.h"


mineblow(tp,mp,nx,ny)
	tank_t *tp;
	mine_t *mp;
	int nx, ny;
{
	mine_t *mr;
	tank_t *to;


	/* remove the mine */
	to = mp->m_own;
	to->t_minef--;

	/* kill it */
	if( Mvalid == mp )
		Mvalid = mp->m_next;
	else {
		for( mr = Mvalid ; mr->m_next != mp ; mr = mr->m_next );
		mr->m_next = mp->m_next;
	}

	field[nx][ny] = FSPACE;
	fpts[nx][ny] = (void *)NULL;

	mp->m_next = Mfree;
	Mfree = mp;
	
	explode(tp,mp->m_own,(tp && TANKSAFE(tp))?0:Minedmg,nx,ny);
}


arm_mine(tp)
	register tank_t *tp;
{
	/* can we afford it? */	
	if( (tp->t_energy < Minecost) || (tp->t_state & TMINE) )
		return;

	tp->t_energy -= Minecost;
	Totenergy += Minecost;

	tp->t_state |= TMINE;

	tp->t_minef++;
	Servstate |= SSCUDFRC;
}


drop_mine(tp,x,y)
	register tank_t *tp;
	int x,y;
{
	mine_t *mp;


	tp->t_state &= ~TMINE;

	mp = Mfree;
	Mfree = mp->m_next;

	mp->m_x = x;
	mp->m_y = y;
	mp->m_own = tp;
	field[x][y] = FMINE;
	fpts[x][y] = (void *)mp;

	mp->m_next = Mvalid;
	Mvalid = mp;

	Servstate |= SSCUDFRC;
}


rm_mines(tp)
	tank_t *tp;
{
	mine_t *mp, *mr, *mn;
	int nx, ny;
	

	/* remove all the mines belonging to tp */
	for( mp = Mvalid ; mp ; mp = mn ) {

		mn = mp->m_next;

		if( mp->m_own != tp )
			continue;

		/* kill it */
		if( Mvalid == mp )
			Mvalid = mp->m_next;
		else {
			for( mr = Mvalid ; mr->m_next != mp ; mr = mr->m_next );
			mr->m_next = mp->m_next;
		}

		mp->m_next = Mfree;
		Mfree = mp;

		/* remove the mine from the field */
		nx = mp->m_x;
		ny = mp->m_y;
		field[nx][ny] = FSPACE;
		fpts[nx][ny] = (void *)NULL;
	}
}


minedest()
{
	register mine_t *mp, *mr;


	/* Walk down the list and remove them */
	for( mp = Mvalid ; mp ; mp = mr ) {
		mr = mp->m_next;

		mineblow((tank_t *)NULL,mp,mp->m_x,mp->m_y);
	}
}

/* Lint Output
*/
