#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>
#include <shop>
#define PLUGIN_VERSION "1.0-simple"
static ConVar g_cvEnabled;
static ConVar g_cvCoinModel;
static ConVar g_cvCoinLifetime;
static ConVar g_cvCoinCreditsMin;
static ConVar g_cvCoinCreditsMax;
static ConVar g_cvPickupRange;
static const char COIN_TARGETNAME[] = "coin_pickup";
public Plugin myinfo =
{
name = "[CoinDrop] Simple",
author = "Cursor и 7RG",
description = "Простые монетки",
version = PLUGIN_VERSION,
url = "Maincs.ru"
};
public void OnPluginStart()
{
g_cvEnabled = CreateConVar("sm_coin_enable", "1", "Включить монетки");
g_cvCoinModel = CreateConVar("sm_coin_model", "models/coins/coin.mdl", "Модель монетки");
g_cvCoinLifetime = CreateConVar("sm_coin_lifetime", "30.0", "Время жизни монетки");
g_cvCoinCreditsMin = CreateConVar("sm_coin_credits_min", "1", "Минимум кредитов за подбор");
g_cvCoinCreditsMax = CreateConVar("sm_coin_credits_max", "10", "Максимум кредитов за подбор");
g_cvPickupRange = CreateConVar("sm_coin_range", "150.0", "Радиус подбора");
AutoExecConfig(true, "coin_simple");
HookEvent("player_death", Event_PlayerDeath, EventHookMode_Post);
HookEvent("round_start", Event_RoundStart, EventHookMode_Post);
}
public void OnMapStart()
{
char model[256];
g_cvCoinModel.GetString(model, sizeof(model));
if (model[0] && FileExists(model, true))
{
PrecacheModel(model, true);
AddFileToDownloadsTable(model);
}
PrecacheSound("items/gift_drop.wav", true);
AddFileToDownloadsTable("sound/items/gift_drop.wav");
}
public void Event_RoundStart(Event event, const char[] name, bool dontBroadcast)
{
int ent = -1;
while ((ent = FindEntityByClassname(ent, "prop_dynamic_override")) != -1)
{
char targetname[64];
GetEntPropString(ent, Prop_Data, "m_iName", targetname, sizeof(targetname));
if (StrEqual(targetname, COIN_TARGETNAME))
{
AcceptEntityInput(ent, "Kill");
}
}
}
public void Event_PlayerDeath(Event event, const char[] name, bool dontBroadcast)
{
if (!g_cvEnabled.BoolValue)
return;
int victim = GetClientOfUserId(event.GetInt("userid"));
int attacker = GetClientOfUserId(event.GetInt("attacker"));
if (victim < 1 || victim > MaxClients || !IsClientInGame(victim))
return;
if (attacker < 1 || attacker > MaxClients || !IsClientInGame(attacker))
return;
if (victim == attacker)
return;
float origin[3];
GetClientAbsOrigin(victim, origin);
origin[2] += 3.0;
CreateCoin(origin);
}
void CreateCoin(float origin[3])
{
int ent = CreateEntityByName("prop_dynamic_override");
if (ent == -1)
return;
char model[256];
g_cvCoinModel.GetString(model, sizeof(model));
DispatchKeyValue(ent, "model", model);
DispatchKeyValue(ent, "targetname", COIN_TARGETNAME);
DispatchKeyValue(ent, "solid", "0");
DispatchKeyValue(ent, "spawnflags", "0");
if (DispatchSpawn(ent))
{
TeleportEntity(ent, origin, NULL_VECTOR, NULL_VECTOR);
SetEntProp(ent, Prop_Send, "m_usSolidFlags", 8);
SetEntProp(ent, Prop_Send, "m_CollisionGroup", 1);
SetEntProp(ent, Prop_Data, "m_nSolidType", 2);
float mins[3] = {-40.0, -40.0, -20.0};
float maxs[3] = {40.0, 40.0, 20.0};
SetEntPropVector(ent, Prop_Send, "m_vecMins", mins);
SetEntPropVector(ent, Prop_Send, "m_vecMaxs", maxs);
float lifetime = g_cvCoinLifetime.FloatValue;
CreateTimer(lifetime, Timer_RemoveCoin, EntIndexToEntRef(ent), TIMER_FLAG_NO_MAPCHANGE);
SDKHook(ent, SDKHook_Touch, Hook_CoinTouch);
CreateTimer(0.05, Timer_RotateCoin, EntIndexToEntRef(ent), TIMER_REPEAT);
}
}
public void Hook_CoinTouch(int entity, int client)
{
if (client > 0 && client <= MaxClients && IsClientInGame(client) && IsPlayerAlive(client) && !IsFakeClient(client))
{
float entPos[3], clientPos[3];
GetEntPropVector(entity, Prop_Data, "m_vecOrigin", entPos);
GetClientAbsOrigin(client, clientPos);
float distance = GetVectorDistance(clientPos, entPos);
float range = g_cvPickupRange.FloatValue;
if (distance <= range)
{
int minCredits = g_cvCoinCreditsMin.IntValue;
int maxCredits = g_cvCoinCreditsMax.IntValue;
int credits = minCredits + GetURandomInt() % (maxCredits - minCredits + 1);
if (credits > 0)
{
int given = Shop_GiveClientCredits(client, credits, CREDITS_BY_NATIVE);
if (given > 0)
{
PrintToChat(client, " \x01+\x04%d \x01монета", given);
EmitAmbientSound("items/gift_drop.wav", entPos, entity, SNDLEVEL_NORMAL);
AcceptEntityInput(entity, "Kill");
}
}
}
}
}
public Action Timer_RotateCoin(Handle timer, int ref)
{
int ent = EntRefToEntIndex(ref);
if (ent == INVALID_ENT_REFERENCE || !IsValidEntity(ent))
{
return Plugin_Stop;
}
float angles[3];
GetEntPropVector(ent, Prop_Data, "m_angRotation", angles);
angles[1] += 10.0;
if (angles[1] >= 360.0) angles[1] = 0.0;
TeleportEntity(ent, NULL_VECTOR, angles, NULL_VECTOR);
return Plugin_Continue;
}
public Action Timer_RemoveCoin(Handle timer, int ref)
{
int ent = EntRefToEntIndex(ref);
if (ent != INVALID_ENT_REFERENCE && IsValidEntity(ent))
{
AcceptEntityInput(ent, "Kill");
}
return Plugin_Stop;
}