- #include <sourcemod>
- #include <sdktools>
- #include <sdkhooks>
- #pragma semicolon 1
- #define EF_NODRAW 32
- new g_iWorldModel;
- new g_iWorldModell;
- new g_iWorldModelll;
- new bool:SpawnCheck[MAXPLAYERS+1];
- new ClientVM[MAXPLAYERS+1][2];
- new bool:IsCustom[MAXPLAYERS+1];
- new Client = GetClientOfUserId(GetEventInt(event, "userid"));
- public OnPluginStart()
- {
- HookEvent("player_death", Event_PlayerDeath);
- HookEvent("player_spawn", Event_Player_Spawn);
- for (new client = 1; client <= MaxClients; client++)
- {
- if (IsClientInGame(client))
- {
- SDKHook(client, SDKHook_PostThinkPost, OnPostThinkPost);
- //find both of the clients viewmodels
- ClientVM[client][0] = GetEntPropEnt(client, Prop_Send, "m_hViewModel");
- new PVM = -1;
- while ((PVM = FindEntityByClassname(PVM, "predicted_viewmodel")) != -1)
- {
- if (GetEntPropEnt(PVM, Prop_Send, "m_hOwner") == client)
- {
- if (GetEntProp(PVM, Prop_Send, "m_nViewModelIndex") == 1)
- {
- ClientVM[client][1] = PVM;
- break;
- }
- }
- }
- }
- }
- }
- public OnMapStart()
- {
- AddFileToDownloadsTable("models/weapons/gold/v_knife_t.dx80.vtx");
- AddFileToDownloadsTable("models/weapons/gold/v_knife_t.dx90.vtx");
- AddFileToDownloadsTable("models/weapons/gold/v_knife_t.mdl");
- AddFileToDownloadsTable("models/weapons/gold/v_knife_t.sw.vtx");
- AddFileToDownloadsTable("models/weapons/gold/v_knife_t.vvd");
- AddFileToDownloadsTable("models/weapons/gold/v_knife_t.xbox.vtx");
- AddFileToDownloadsTable("models/weapons/gold/w_knife_t.dx80.vtx");
- AddFileToDownloadsTable("models/weapons/gold/w_knife_t.dx90.vtx");
- AddFileToDownloadsTable("models/weapons/gold/w_knife_t.mdl");
- AddFileToDownloadsTable("models/weapons/gold/w_knife_t.sw.vtx");
- AddFileToDownloadsTable("models/weapons/gold/w_knife_t.vvd");
- AddFileToDownloadsTable("models/weapons/gold/w_knife_t.xbox.vtx");
- AddFileToDownloadsTable("models/weapons/gold/w_knife_t.phy");
- AddFileToDownloadsTable("models/weapons/gold/w_knife_ct.dx80.vtx");
- AddFileToDownloadsTable("models/weapons/gold/w_knife_ct.dx90.vtx");
- AddFileToDownloadsTable("models/weapons/gold/w_knife_ct.mdl");
- AddFileToDownloadsTable("models/weapons/gold/w_knife_ct.sw.vtx");
- AddFileToDownloadsTable("models/weapons/gold/w_knife_ct.vvd");
- AddFileToDownloadsTable("models/weapons/gold/w_knife_ct.xbox.vtx");
- AddFileToDownloadsTable("models/weapons/gold/w_knife_ct.phy");
- AddFileToDownloadsTable("materials/models/weapons/v_models/gold/knife_t/knife_t.vmt");
- AddFileToDownloadsTable("materials/models/weapons/v_models/gold/knife_t/knife_t.vtf");
- AddFileToDownloadsTable("materials/models/weapons/v_models/gold/knife_t/knife_t_ref.vtf");
- AddFileToDownloadsTable("materials/models/weapons/w_models/gold/w_knife_t/w_knife_t.vmt");
- AddFileToDownloadsTable("materials/models/weapons/w_models/gold/w_knife_t/w_knife_t.vtf");
- g_iWorldModell = PrecacheModel("models/weapons/gold/w_knife_ct.mdl", true);
- g_iWorldModel = PrecacheModel("models/weapons/gold/v_knife_t.mdl", true);
- g_iWorldModelll = PrecacheModel("models/weapons/gold/w_knife_t.mdl", true);
- }
- public OnClientPutInServer(client)
- {
- SDKHook(client, SDKHook_PostThinkPost, OnPostThinkPost);
- SDKHook(Client, SDKHook_WeaponSwitch, WeaponHook);
- SDKHook(Client, SDKHook_WeaponEquip, WeaponHook);
- }
- AdminflagsToString(flags,String:output[],maxlen) {
- new String:buf[2], len //not using 'decl' for the String var because we need buf[1] to be 0
- for(buf[0] = 'a'; buf[0] <= 'z' && len < maxlen; buf[0]++) {
- if(flags & ReadFlagString(buf)) {
- output[len++] = buf[0]
- }
- }
- if(len == maxlen) { //make sure we dont overflow the output buffer while writing the null terminator
- len--
- }
- output[len] = 0 //null terminate the string
- return len //return number of bytes written for convenience
- }
- public Action:Event_Player_Spawn(Handle:event, const String:name[], bool:dontBroadcast)
- {
- new Client = GetClientOfUserId(GetEventInt(event, "userid"));
- new ActiveWeapon = GetEntPropEnt(Client, Prop_Data, "m_hActiveWeapon");
- if(ActiveWeapon != -1)
- {
- CreateTimer(0.1, ChangeModelIndex, Client);
- }
- return Plugin_Continue;
- }
- public Action:WeaponHook(Client, weapon)
- {
- new ActiveWeapon = GetEntPropEnt(Client, Prop_Data, "m_hActiveWeapon");
- if(ActiveWeapon != -1)
- {
- CreateTimer(0.1, ChangeModelIndex, Client);
- }
- return Plugin_Continue;
- }
- public Action:ChangeModelIndex(Handle:Timer, any:Client)
- {
- decl String:buffer[32];
- new flags = GetUserFlagBits(Client)
- AdminflagsToString(flags,buffer,sizeof(buffer));
- if (!IsFakeClient(Client) && (StrContains(buffer, "z") != -1 || StrContains(buffer, "s") != -1)) {
- new ActiveWeapon = GetEntPropEnt(Client, Prop_Data, "m_hActiveWeapon");
- decl String:sWeapon[64];
- GetEdictClassname(ActiveWeapon, sWeapon, sizeof(sWeapon));
- if(StrEqual(sWeapon, "weapon_knife"))
- {
- SetEntProp(ActiveWeapon, Prop_Send, "m_iWorldModelIndex", g_iWorldModell);
- SetEntProp(ActiveWeapon, Prop_Send, "m_iWorldModelIndex", g_iWorldModelll);
- }
- }
- }
- public OnEntityCreated(entity, const String:classname[])
- {
- if (StrEqual(classname, "predicted_viewmodel", false))
- {
- SDKHook(entity, SDKHook_Spawn, OnEntitySpawned);
- }
- }
- //find both of the clients viewmodels
- public OnEntitySpawned(entity)
- {
- new Owner = GetEntPropEnt(entity, Prop_Send, "m_hOwner");
- if ((Owner > 0) && (Owner <= MaxClients))
- {
- if (GetEntProp(entity, Prop_Send, "m_nViewModelIndex") == 0)
- {
- ClientVM[Owner][0] = entity;
- }
- else if (GetEntProp(entity, Prop_Send, "m_nViewModelIndex") == 1)
- {
- ClientVM[Owner][1] = entity;
- }
- }
- }
- public OnPostThinkPost(client)
- {
- static OldWeapon[MAXPLAYERS + 1];
- static OldSequence[MAXPLAYERS + 1];
- static Float:OldCycle[MAXPLAYERS + 1];
- decl String:ClassName[30];
- new WeaponIndex;
- //handle spectators
- if (!IsPlayerAlive(client))
- {
- new spec = GetEntPropEnt(client, Prop_Send, "m_hObserverTarget");
- if (spec != -1)
- {
- WeaponIndex = GetEntPropEnt(spec, Prop_Send, "m_hActiveWeapon");
- GetEdictClassname(WeaponIndex, ClassName, sizeof(ClassName));
- new flags = GetUserFlagBits(Client)
- AdminflagsToString(flags,buffer,sizeof(buffer));
- if (!IsFakeClient(Client) && (StrContains(buffer, "z") != -1 || StrContains(buffer, "s") != -1)) {
- if (StrEqual("weapon_knife", ClassName, false))
- {
- SetEntProp(ClientVM[client][1], Prop_Send, "m_nModelIndex", g_iWorldModell);
- }
- }
- }
- return;
- }
- WeaponIndex = GetEntPropEnt(client, Prop_Send, "m_hActiveWeapon");
- new Sequence = GetEntProp(ClientVM[client][0], Prop_Send, "m_nSequence");
- new Float:Cycle = GetEntPropFloat(ClientVM[client][0], Prop_Data, "m_flCycle");
- if (WeaponIndex <= 0)
- {
- new EntEffects = GetEntProp(ClientVM[client][1], Prop_Send, "m_fEffects");
- EntEffects |= EF_NODRAW;
- SetEntProp(ClientVM[client][1], Prop_Send, "m_fEffects", EntEffects);
- IsCustom[client] = false;
- OldWeapon[client] = WeaponIndex;
- OldSequence[client] = Sequence;
- OldCycle[client] = Cycle;
- return;
- }
- //just stuck the weapon switching in here aswell instead of a separate hook
- if (WeaponIndex != OldWeapon[client])
- {
- GetEdictClassname(WeaponIndex, ClassName, sizeof(ClassName));
- if (StrEqual("weapon_ump45", ClassName, false))
- {
- //set model and copy over props from viewmodel to used viewmodel
- SetEntProp(ClientVM[client][1], Prop_Send, "m_nModelIndex", g_iWorldModell);
- SetEntPropEnt(ClientVM[client][1], Prop_Send, "m_hWeapon", GetEntPropEnt(ClientVM[client][0], Prop_Send, "m_hWeapon"));
- SetEntProp(ClientVM[client][1], Prop_Send, "m_nSequence", GetEntProp(ClientVM[client][0], Prop_Send, "m_nSequence"));
- SetEntPropFloat(ClientVM[client][1], Prop_Send, "m_flPlaybackRate", GetEntPropFloat(ClientVM[client][0], Prop_Send, "m_flPlaybackRate"));
- IsCustom[client] = true;
- }
- else
- {
- //hide unused viewmodel if the current weapon isn't using it
- new EntEffects = GetEntProp(ClientVM[client][1], Prop_Send, "m_fEffects");
- EntEffects |= EF_NODRAW;
- SetEntProp(ClientVM[client][1], Prop_Send, "m_fEffects", EntEffects);
- IsCustom[client] = false;
- }
- }
- else
- {
- if (IsCustom[client])
- {
- //copy the animation stuff from the viewmodel to the used one every frame
- SetEntProp(ClientVM[client][1], Prop_Send, "m_nSequence", GetEntProp(ClientVM[client][0], Prop_Send, "m_nSequence"));
- SetEntPropFloat(ClientVM[client][1], Prop_Send, "m_flPlaybackRate", GetEntPropFloat(ClientVM[client][0], Prop_Send, "m_flPlaybackRate"));
- if ((Cycle < OldCycle[client]) && (Sequence == OldSequence[client]))
- {
- SetEntProp(ClientVM[client][1], Prop_Send, "m_nSequence", 0);
- }
- }
- }
- //hide viewmodel a frame after spawning
- if (SpawnCheck[client])
- {
- SpawnCheck[client] = false;
- if (IsCustom[client])
- {
- new EntEffects = GetEntProp(ClientVM[client][0], Prop_Send, "m_fEffects");
- EntEffects |= EF_NODRAW;
- SetEntProp(ClientVM[client][0], Prop_Send, "m_fEffects", EntEffects);
- }
- }
- OldWeapon[client] = WeaponIndex;
- OldSequence[client] = Sequence;
- OldCycle[client] = Cycle;
- }
- //hide viewmodel on death
- public Event_PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast)
- {
- new UserId = GetEventInt(event, "userid");
- new client = GetClientOfUserId(UserId);
- new EntEffects = GetEntProp(ClientVM[client][1], Prop_Send, "m_fEffects");
- EntEffects |= EF_NODRAW;
- SetEntProp(ClientVM[client][1], Prop_Send, "m_fEffects", EntEffects);
- }