//-----------------------------------------------------------------------------
//
//  $Logfile:: /game/spidermine.h                                                $
// $Revision:: 2                                                              $
//   $Author:: Burgermac                                                      $
//     $Date:: 12/27/00 7:28 PM                                               $
//
// Copyright (C) 1998 by Ritual Entertainment, Inc.
// All rights reserved.
//
// This source is may not be distributed and/or modified without
// expressly written permission by Ritual Entertainment, Inc.
//
// DESCRIPTION:
// Spider Mines
//

#ifndef __SPIDERMINE_H__
#define __SPIDERMINE_H__

#include "g_local.h"
#include "item.h"
#include "weapon.h"
#include "specialfx.h"

class Detonator;

typedef SafePtr<Detonator> DetonatorPtr;
class Mine : public Projectile
{
private:
	DetonatorPtr detonator;
	qboolean detonate;
	qboolean sticky;

public:
	CLASS_PROTOTYPE( Mine );

	void                    Explode( Event* ev );
	void                    Setup( Entity* owner, Vector pos, Vector dir );
	void              SlideOrStick( Event* ev );
	void              CheckForTargets( Event* ev );
	void              Run( Event* ev );
	void              SetDetonator( Detonator* det );
	qboolean          IsOwner( Sentient* sent );
	virtual void Archive( Archiver& arc );
	virtual void Unarchive( Archiver& arc );
};


typedef SafePtr<Mine> MinePtr;


class Detonator : public Weapon
{
private:
public:
	Container<MinePtr>   mineList;
	int currentMine;

	CLASS_PROTOTYPE( Detonator );

	/*Detonator::*/ Detonator();
	virtual void            Shoot( Event* ev );
	virtual void            DoneFiring( Event* ev );
	void          AddMine( Mine* mine );
	void          RemoveMine( Mine* mine );
	void          CycleCamera( Event* ev );
	virtual qboolean      AutoChange( void );
	qboolean      IsDroppable( void );
	virtual void          Archive( Archiver& arc );
	virtual void          Unarchive( Archiver& arc );
};


inline void Mine::Archive( Archiver& arc )
{
	Projectile::Archive( arc );
	arc.WriteSafePointer( detonator );
	arc.WriteBoolean( detonate );
	arc.WriteBoolean( sticky );
}

inline void Mine::Unarchive( Archiver& arc )
{
	Projectile::Unarchive( arc );
	arc.ReadSafePointer( &detonator );
	detonate = arc.ReadBoolean();
	sticky = arc.ReadBoolean();
}

inline void Detonator::Archive( Archiver& arc )
{
	int i;
	int num;

	Weapon::Archive( arc );

	arc.WriteInteger( currentMine );

	num = mineList.NumObjects();
	arc.WriteInteger( num );
	for( i = 1; i <= num; i++ )
	{
		#if defined(__GNUC__) && (__GNUC__ < 3)
		/* gcc-2.95 bug: it errors otherwise... */
		arc.WriteSafePointer( mineList.ObjectAt( i ).Ptr() );
		#else
		arc.WriteSafePointer( mineList.ObjectAt( i ) );
		#endif
	}
}

inline void Detonator::Unarchive( Archiver& arc )
{
	int i;
	int num;

	mineList.FreeObjectList();

	Weapon::Unarchive( arc );

	currentMine = arc.ReadInteger();

	num = arc.ReadInteger();
	mineList.Resize( num );
	for( i = 1; i <= num; i++ )
	{
		arc.ReadSafePointer( mineList.AddressOfObjectAt( i ) );
	}
}

class SpiderMine : public Weapon
{
private:
	MinePtr currentMine;
	DetonatorPtr detonator;
public:
	CLASS_PROTOTYPE( SpiderMine );

	/*SpiderMine::*/ SpiderMine();
	virtual void        Shoot( Event* ev );
	virtual void        DoneFiring( Event* ev );
	virtual qboolean  ReadyToUse( void );
	void      ChangeToDetonator( Event* ev );
	void      SetDetonator( Detonator* det );
	virtual void      SetOwner( Sentient* sent );
	qboolean  IsDroppable( void );
	virtual void Archive( Archiver& arc );
	virtual void Unarchive( Archiver& arc );
};

inline void SpiderMine::Archive( Archiver& arc )
{
	Weapon::Archive( arc );
	arc.WriteSafePointer( currentMine );
	arc.WriteSafePointer( detonator );
}

inline void SpiderMine::Unarchive( Archiver& arc )
{
	Weapon::Unarchive( arc );
	arc.ReadSafePointer( &currentMine );
	arc.ReadSafePointer( &detonator );
}

#endif /* SpiderMine.h */
