#include <sourcemod>
#include <sdktools>
#pragma semicolon 1

new g_iActiveWeapon = -1,
	g_iAccount = -1;

new g_iDropMoney;

#define MODEL		"models/props/cs_assault/money.mdl"

public Plugin:myinfo =
{
	name = "Drop Money",
	author = "R1KO",
	version = "1.0"
};

public OnPluginStart()
{
	new Handle:hCvar;
	HookConVarChange((hCvar = CreateConVar("sm_drop_money", "1000", "Сколько денег выбрасывать")), OnDropMoneyChange);
	g_iDropMoney = GetConVarInt(hCvar);
	CloseHandle(hCvar);

	g_iAccount = GetSendPropOffset("CCSPlayer", "m_iAccount");
	g_iActiveWeapon = GetSendPropOffset("CAI_BaseNPC", "m_hActiveWeapon");

	AddCommandListener(DropCommand, "drop");
}

GetSendPropOffset(const String:sNetClass[], const String:sPropertyName[])
{
	new iOffset = FindSendPropOffs(sNetClass, sPropertyName);
	if (iOffset == -1) SetFailState("Fatal Error: Unable to find offset: \"%s::%s\"", sNetClass, sPropertyName);

	return iOffset;
}

public OnDropMoneyChange(Handle:hCvar, const String:oldValue[], const String:newValue[]) g_iDropMoney = GetConVarInt(hCvar);

public OnConfigsExecuted() PrecacheModel(MODEL, true);

public Action:DropCommand(iClient, const String:command[], args)
{
	if(IsClientInGame(iClient) && IsPlayerAlive(iClient))
	{
		new iWeapon = GetEntDataEnt2(iClient, g_iActiveWeapon);

		if (iWeapon > 0 && IsValidEntity(iWeapon))
		{
			decl String:sWeapon[64];
			GetEntityClassname(iWeapon, sWeapon, sizeof(sWeapon));

			if(strcmp(sWeapon[7], "knife", false) == 0)
			{
				new iMoney = GetEntData(iClient, g_iAccount, 4);
				if(iMoney >= g_iDropMoney)
				{
					DropMoney(iClient);
					SetEntData(iClient, g_iAccount, iMoney - g_iDropMoney, 4, true);
				}
			}
		}
	}
}

stock DropMoney(iClient)
{
	static Float:fPos[3], Float:fAng[3], Float:fVel[3], Float:fPVel[3];
	GetClientEyePosition(iClient, fPos);
	fPos[2] -= 20.0;

	new iEntity = CreateEntityByName("prop_physics");
	if (iEntity != -1)
	{
		decl String:sTargetName[32];
		FormatEx(sTargetName, sizeof(sTargetName) - 1, "drop_money_%d", iEntity);
		DispatchKeyValue(iEntity, "targetname", sTargetName);
		DispatchKeyValue(iEntity, "classname", "drop_money");
		DispatchKeyValue(iEntity, "model", MODEL);
		DispatchKeyValue(iEntity, "spawnflags", "256");
		if (DispatchSpawn(iEntity))
		{
			SetEntProp(iEntity, Prop_Send, "m_usSolidFlags", 8);
			SetEntProp(iEntity, Prop_Send, "m_nSolidType", 6);
			SetEntProp(iEntity, Prop_Send, "m_CollisionGroup", 11);
			
			GetClientEyeAngles(iClient, fAng);
			GetAngleVectors(fAng, fVel, NULL_VECTOR, NULL_VECTOR);
			ScaleVector(fVel, 300.0);

			GetEntPropVector(iClient, Prop_Data, "m_vecVelocity", fPVel);
			AddVectors(fVel, fPVel, fVel);

			TeleportEntity(iEntity, fPos, fAng, fVel);

			HookSingleEntityOutput(iEntity, "OnPlayerUse", OnMoneyUse, false);
		}
	}
}

public OnMoneyUse(const String:output[], iEntity, activator, Float:delay)
{
	new iClient = GetActivator(iEntity);
	if(iClient > 0)
	{
		new iMoney = GetEntData(iClient, g_iAccount, 4)+g_iDropMoney;
		if(iMoney <= 16000)
		{
			SetEntData(iClient, g_iAccount, iMoney, 4, true);
			AcceptEntityInput(iEntity, "Kill");
			UnhookSingleEntityOutput(iEntity, "OnPlayerUse", OnMoneyUse);
		}
	}
}

stock GetActivator(const &iEntity)
{
	for (new i = 1; i <= MaxClients; i++)
	{
		if (IsClientInGame(i) && IsPlayerAlive(i) && GetClientButtons(i) & IN_USE)
		{
			if (GetClientAimTarget(i, false) == iEntity) return i;
		}
	}

	return -1;
}