AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [CS:GO] Custom Viewmodel (https://forums.alliedmods.net/showthread.php?t=273885)

CareFully 10-28-2015 15:08

[CS:GO] Custom Viewmodel
 
1 Attachment(s)
PHP Code:

#include <sourcemod>
#include <sdkhooks> 
#include <sdktools>

new g_PVMid[MAXPLAYERS]; // Predicted ViewModel ID's
new g_iSMGModel;    // Custom ViewModel index

public OnPluginStart()
{
    
HookEvent("player_spawn"Event_PlayerSpawn);
}

public 
OnConfigsExecuted()
{
    
g_iSMGModel PrecacheModel("models/weapons/paintball/v_pbgunt.mdl"); // Custom model
}

public 
OnClientPostAdminCheck(client){
    
SDKHook(clientSDKHook_WeaponSwitchPostOnClientWeaponSwitchPost);    
}

public 
OnClientWeaponSwitchPost(clientwpnid)
{
    
    
decl String:szWpn[64];
    
GetEntityClassname(wpnid,szWpn,sizeof(szWpn));
    
    if(
StrEqual(szWpn"weapon_mac10")){
        
SetEntProp(wpnidProp_Send"m_nModelIndex"0);
        
SetEntProp(g_PVMid[client], Prop_Send"m_nModelIndex"g_iSMGModel);
    }
}

public 
Event_PlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event"userid"));
    
g_PVMid[client] = Weapon_GetViewModelIndex(client, -1);
    


// Thanks to gubka for these 2 functions below.

// Get model index and prevent server from crash
Weapon_GetViewModelIndex(clientsIndex)
{
    while ((
sIndex FindEntityByClassname2(sIndex"predicted_viewmodel")) != -1)
    {
        new 
Owner GetEntPropEnt(sIndexProp_Send"m_hOwner");
        
        if (
Owner != client)
            continue;
        
        return 
sIndex;
    }
    return -
1;
}
// Get entity name
FindEntityByClassname2(sStartEntString:szClassname[])
{
    while (
sStartEnt > -&& !IsValidEntity(sStartEnt)) sStartEnt--;
    return 
FindEntityByClassname(sStartEntszClassname);


I've tinkered around with cs:go trying to get custom viewmodels working without flickering or turning off prediction and after hours and hours I think i finally got something that seems to work.

It might work with other mods aswell, but i haven't tested it.

The custom model must have animations in the same order as the original model, oherwise it's going to play wrong animations.

EDIT: I added a custom model to attachments to test the code. It's a paintball gun converted from cs 1.6 to cs:go. Lighting of the model might be a bit glicthy :).

Franc1sco 10-29-2015 11:37

Re: [CS:GO] Custom Viewmodel
 
I will test this. If this code works well then will be very usefull and more optimized that this code,

Thanks and good job :)

Vinnice 10-30-2015 06:54

Re: [CS:GO] Custom Viewmodel
 
Good job! It's working very well.
But, here's the question: Where can somebody get this kind of custom models?

CareFully 10-30-2015 11:22

Re: [CS:GO] Custom Viewmodel
 
Yea thats a little problem indeed. Well all source viewmodels should work, but you'll have to probably decompile it and change the animations order. Other way is to convert goldsrc models but thats big hassle to do :)

And one thing more. The game tries to put weapon skins to the custom models aswell, so here's my fix for that:
PHP Code:

public OnEntityCreated(entid){
    
decl String:szWpn[32];
    
GetEntityClassname(entidszWpnsizeof(szWpn));
    if(
StrEqual(szWpn"weapon_mac10"){
        
SDKHook(entidSDKHook_SpawnOnEntitySpawn);
    }
}
public 
OnEntitySpawn(entid){
    
SetEntProp(entid,Prop_Send,"m_iItemIDLow",0);
    
SetEntProp(entid,Prop_Send,"m_iItemIDHigh",0);


I hope valve is not gonna be mad for blocking skins :)

gubka 11-01-2015 08:28

Re: [CS:GO] Custom Viewmodel
 
Good job , I will check it now , because yesterday I wrote new code for changing models with high ping , but method look better , thanks a million, I will use it in my mod

gubka 11-01-2015 09:13

Re: [CS:GO] Custom Viewmodel
 
It still flippings on custom models, my way better

Quote:

public void ThinkOnThinkPost(int iPlayer)
{
if(!IsPlayerExist(iPlayer))
{
return;
}

if(gZombie[iPlayer])
{
// Check current weapon
char iWeapon[128];
int iItem = GetEntPropEnt(iPlayer, Prop_Send, "m_hActiveWeapon");

GetEdictClassname(iItem, iWeapon, sizeof(iWeapon));

// If weapon is a knife, then allow pickup.
if (StrEqual(iWeapon, "weapon_knife"))
{
int iButton = GetClientButtons(iPlayer);

// Prevent from flipping on CS:GO
if(iButton & IN_ATTACK || iButton & IN_ATTACK2)
SetEntPropFloat(iPlayer, Prop_Data, "m_flNextAttack", 0.0);
else
SetEntPropFloat(iPlayer, Prop_Data, "m_flNextAttack", 999.0);

// Show custom model
Weapon_SetViewModelIndex(iPlayer, "models/zombie/normal_f/hand/hand_zombie_normal_f.mdl");
}
}
}


/ Set ViewModel and check model index
void Weapon_SetViewModelIndex(int sClient, char[] pModel)
{
// Precache model , this is important
int sIndex = PrecacheModel(pModel);

if (sIndex < 1)
{
LogError("Unable to precache model '%s'", pModel);
return;
}

// Get index of model for changing it
pViewModelIndex[sClient] = Weapon_GetViewModelIndex(sClient, -1);

if (pViewModelIndex[sClient] < 1)
{
LogError("Unable to get a viewmodel index");
return;
}

// Create new vars
int iCurrentWeapon[MAXPLAYERS+1];
int iOldSequence[MAXPLAYERS+1];
float iOldCycle[MAXPLAYERS+1];

// Update some offsets
int iActiveWeapon = GetEntPropEnt(sClient, Prop_Send, "m_hActiveWeapon");
int iSequence = GetEntProp(pViewModelIndex[sClient], Prop_Send, "m_nSequence");
float iCycle = GetEntPropFloat(pViewModelIndex[sClient], Prop_Data, "m_flCycle");

// If valid entity , change view_model for client
if (IsValidEdict(iActiveWeapon))
{
if (iActiveWeapon != iCurrentWeapon[sClient])
{
if (iCurrentWeapon[sClient])
{
ShowViewModel(pViewModelIndex[sClient], false);
}

iCurrentWeapon[sClient] = 0;

ShowViewModel(pViewModelIndex[sClient], true);

SetEntProp(pViewModelIndex[sClient], Prop_Send, "m_nModelIndex", sIndex);
ChangeEdictState(pViewModelIndex[sClient], FindDataMapOffs(pViewModelIndex[sClient], "m_nModelIndex"));

iCurrentWeapon[sClient] = iActiveWeapon;
}
}

// If this is current weapon, check cycles and sequenses
if (iCurrentWeapon[sClient])
{
SetEntProp(pViewModelIndex[sClient], Prop_Send, "m_nSequence", GetEntProp(pViewModelIndex[sClient], Prop_Send, "m_nSequence"));
SetEntPropFloat(pViewModelIndex[sClient], Prop_Send, "m_flPlaybackRate", GetEntPropFloat(pViewModelIndex[sClient], Prop_Send, "m_flPlaybackRate"));

if ((iCycle < iOldCycle[sClient]) && (iSequence == iOldSequence[sClient]))
{
SetEntProp(pViewModelIndex[sClient], Prop_Send, "m_nSequence", 0);
}
}

// Update old sequences
iOldSequence[sClient] = iSequence;
iOldCycle[sClient] = iCycle;
}

// Check and change effect of weapon entity
void ShowViewModel(int iViewModel, bool bShow)
{
int iFlags = GetEntProp(iViewModel, Prop_Send, "m_fEffects");

SetEntProp(iViewModel, Prop_Send, "m_fEffects", bShow ? iFlags & ~EF_NODRAW : iFlags | EF_NODRAW);
}

// Get entity name
int FindEntityByClassname2(int sStartEnt, char[] szClassname)
{
while (sStartEnt > -1 && !IsValidEntity(sStartEnt)) sStartEnt--;
return FindEntityByClassname(sStartEnt, szClassname);
}

// Get model index and prevent server from crash
int Weapon_GetViewModelIndex(int sClient, int sIndex)
{
while ((sIndex = FindEntityByClassname2(sIndex, "predicted_viewmodel")) != -1)
{
int Owner = GetEntPropEnt(sIndex, Prop_Send, "m_hOwner");
int ClientWeapon = GetEntPropEnt(sClient, Prop_Send, "m_hActiveWeapon");
int Weapon = GetEntPropEnt(sIndex, Prop_Send, "m_hWeapon");

if (Owner != sClient)
continue;

if (ClientWeapon != Weapon)
continue;

return sIndex;
}
return -1;
}

CareFully 11-01-2015 10:28

Re: [CS:GO] Custom Viewmodel
 
For me, my way only flickers once and thats when you switch to the weapon.

Your way it flickers each shot for me.

gubka 11-01-2015 14:43

Re: [CS:GO] Custom Viewmodel
 
I tryied you way, to change knife to zombie claws, and they filckers every sec

Starbish 11-02-2015 19:27

Re: [CS:GO] Custom Viewmodel
 
Quote:

Originally Posted by gubka (Post 2358932)
I tryied you way, to change knife to zombie claws, and they filckers every sec

Isn't it already existed as a standalone plugin?

CareFully 11-03-2015 11:03

Re: [CS:GO] Custom Viewmodel
 
Does the zombie claws model have animations or is it using offical csgo animations?


All times are GMT -4. The time now is 10:55.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.