// 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:
// Magnum.
//

#include "g_local.h"
#include "magnum.h"
#include "dualmagnum.h" //###

CLASS_DECLARATION( BulletWeapon, Magnum, "weapon_magnum" );

ResponseDef Magnum::Responses[] =
{
	{ &EV_Weapon_Shoot, ( Response )Magnum::Shoot },
	{ NULL, NULL }
};

Magnum::Magnum()
{
	SetModels( "magnum.def", "view_magnum.def" );
	SetAmmo( "Bullet10mm", 1, 100 );
	SetRank( 20, 20 );
	SetType( WEAPON_1HANDED );
	modelIndex( "10mm.def" );
	silenced = true;

//###
	ammosynced = false;

	// precaches for the dual magnum
	modelIndex( "dualmagnum_w.def" );
	modelIndex( "view_dualmagnum.def" );
//###
}

void Magnum::Shoot( Event *ev )
{
	NextAttack( 0.20 );
	FireBullets( 1, "10 10 10", 12, 24, DAMAGE_BULLET, MOD_MAGNUM, false );
}

//###
void Magnum::ReadyWeapon( void )
{
	str animname;

	if ( weaponstate != WEAPON_HOLSTERED )
	{
		return;
	}

	weaponstate = WEAPON_RAISING;

	AttachGun();

	if ( ( owner ) && ( owner->flags & FL_SILENCER ) && ( silenced ) )
		animname = "silready";
	else
	{
		if ( weaponmode == SECONDARY )
			animname = "secondaryready";
		else
			animname = "ready";

	}

	// added dual magnum ammo syncing
	if(!ammosynced)
	{
		Weapon *weapon;

		weapon = (Weapon *)owner->FindItem( "DualMagnum" );
		if(weapon)
		{
			int amount;

			amount = weapon->ClipAmmo() * 0.5;
			if(amount > ammo_clip_size)
				amount = ammo_clip_size;
			weapon->SetAmmoAmount( weapon->ClipAmmo() - amount );

			SetAmmoAmount( amount );

			ammosynced = true;
			((DualMagnum *)weapon)->ammosynced = false;
		}
	}

	if ( !HasAnim( animname.c_str() ) || ( deathmatch->value && ( ( int )dmflags->value & DF_FAST_WEAPONS ) ) )
	{
		ProcessEvent( EV_Weapon_DoneRaising );
		return;
	}

	RandomAnimate( animname.c_str(), EV_Weapon_DoneRaising );
}

void Magnum::SecondaryUse( Event *ev )
{
	// switch to the dual magnums
	owner->useWeapon( "DualMagnum" );
}
//###

qboolean Magnum::Drop( void )
{
//### drop magnums now so people can get dual magnums
/*
   // Don't leave magnums around
   if ( owner && owner->deadflag && deathmatch->value )
      {
      return false;
      }

   return BulletWeapon::Drop();
 */
	Weapon *weapon;
	Sentient *sent;

	ClassDef *cls;
	Item *item;

	if ( !owner )
	{
		return false;
	}

	if ( !IsDroppable() )
	{
		return false;
	}

	// check if player had two magnums
	weapon = (Weapon *)owner->FindItem( "DualMagnum" );

	if(!weapon) // only had one magnum, so drop it
	{
		return BulletWeapon::Drop();
	}

	// has two magnums

	sent = owner.Ptr();

	// get rid of secondary magnum
	sent->takeWeapon( "DualMagnum" );

	// drop this magnum
	BulletWeapon::Drop();

	// give the player a new magnum since he had two
	item = (Item *)sent->FindItem( "Magnum" );

	if(!weapon)
	{
		cls = getClass( "Magnum" );
		if(!cls)
		{
			// somethin's majorly f'ed to get here :P
			gi.dprintf( "No item named 'Magnum'\n" );
			return true;
		}

		item = (Item *)cls->newInstance();

		item->SetOwner( sent );

		item->ProcessPendingEvents();

		item->Set( 1 );
		sent->AddItem( item );
	}
	else
	{
		// for some reason the player still had a magnum
		gi.dprintf( "Player still had magnum after dropping it\n" );
	}

	return true;
//###
}
