Thread: [TF2] Pets
View Single Post
arthurdead
Senior Member
Join Date: Jul 2013
Old 01-13-2014 , 12:15   Re: [TF2] Pets
Reply With Quote #18

Quote:
Originally Posted by abrandnewday View Post
So I unloaded and reloaded it, now both commands don't do anything whatsoever.

No errors in console, no errors in logs.
But It Works Fine Here

Are you sure you got your code right ?

PHP Code:
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>
#include <tf2>
#include <tf2_stocks>
#include <pets>
#include <smlib>
#pragma semicolon 1

stock AttachParticle(entString:particleType[], String:attachBone[], Float:addPos[3]=NULL_VECTORFloat:addAngle[3]=NULL_VECTOR)
{
    new 
particle CreateEntityByName("info_particle_system"); 
    if (
IsValidEdict(particle)) 
    { 
        new 
String:tName[32]; 
        
GetEntPropString(entProp_Data"m_iName"tNamesizeof(tName)); 
        
DispatchKeyValue(particle"targetname""tf2particle"); 
        
DispatchKeyValue(particle"parentname"tName); 
        
DispatchKeyValue(particle"effect_name"particleType); 
        
DispatchSpawn(particle); 
        
SetVariantString("!activator"); 
        
AcceptEntityInput(particle"SetParent"entent0); 
        
SetVariantString(attachBone);
        
AcceptEntityInput(particle"SetParentAttachment"particleparticle0); 
        
ActivateEntity(particle); 

        
TeleportEntity(particleaddPosaddAngleNULL_VECTOR); 
        
AcceptEntityInput(particle"start"); 
    } 
    return 
particle
}

stock AttachModel(entString:Modelname[], String:attachBone[], Float:addPos[3]=NULL_VECTORFloat:addAngle[3]=NULL_VECTOR)
{
    new 
model CreateEntityByName("prop_dynamic_override"); 
    if (
IsValidEdict(model)) 
    {
        new 
String:tName[32]; 
        
GetEntPropString(entProp_Data"m_iName"tNamesizeof(tName)); 
        
DispatchKeyValue(model"targetname""tf2modelattach"); 
        
DispatchKeyValue(model"model"Modelname); 
        
DispatchKeyValue(model"parentname"tName); 
        
SetVariantString("!activator"); 
        
AcceptEntityInput(model"SetParent"entent0); 
        
SetVariantString(attachBone);
        
AcceptEntityInput(model"SetParentAttachment"modelmodel0); 
        
DispatchSpawn(model); 
        
SetEntityModel(modelModelname);
        
TeleportEntity(modeladdPosaddAngleNULL_VECTOR); 
    } 
    return 
model
}

stock SafeKillTimer(&Handle:timer)
{
    if(
timer != INVALID_HANDLE)
    {
        
KillTimer(timer);
        
timer INVALID_HANDLE;
    }
}

stock CreateParticle(String:particle[], Float:pos[3])
{
    new 
tblidx FindStringTable("ParticleEffectNames");
    new 
String:tmp[256];
    new 
count GetStringTableNumStrings(tblidx);
    new 
stridx INVALID_STRING_INDEX;
    for(new 
i=0i<counti++)
    {
        
ReadStringTable(tblidxitmpsizeof(tmp));
        if(
StrEqual(tmpparticlefalse))
        {
            
stridx i;
            break;
        }
    }
    for(new 
i=1i<=GetMaxClients(); i++)
    {
        if(!
IsValidEntity(i)) continue;
        if(!
IsClientInGame(i)) continue;
        
TE_Start("TFParticleEffect");
        
TE_WriteFloat("m_vecOrigin[0]"pos[0]);
        
TE_WriteFloat("m_vecOrigin[1]"pos[1]);
        
TE_WriteFloat("m_vecOrigin[2]"pos[2]);
        
TE_WriteNum("m_iParticleSystemIndex"stridx);
        
TE_SendToClient(i0.0);
    }
}

new 
pet[MAXPLAYERS+1];
new 
petType[MAXPLAYERS+1];
new 
petState[MAXPLAYERS+1];
new 
Handle:soundTimer[MAXPLAYERS+1] = INVALID_HANDLE;
new 
Float:g_pos[3];
new 
bool:inColldown[MAXPLAYERS+1];

public 
OnPluginStart()
{
    
RegConsoleCmd("givepet"Command_SpawnPet);
    
RegConsoleCmd("killpet"Command_KillPet);
    
HookEvent("player_death"Event_DeathEventHookMode_Post);
    
HookEvent("player_spawn"Event_SpawnEventHookMode_Post);
}

public 
OnMapStart()
{
    for(new 
i=1i<sizeof(petInfo); i++)
    {
        
PrecacheModel(petInfo[i][iModel]);
        
PrecacheModel(petInfo[i][iHat]);
        
PrecacheModel(petInfo[i][iWep]);
        
PrecacheSound(petInfo[i][iSoundGeneric]);
        
PrecacheSound(petInfo[i][iSoundJump]);
        
PrecacheSound(petInfo[i][iSoundTaunt]);
        
PrecacheSound(petInfo[i][iSoundSpawn]);
        
PrecacheSound(petInfo[i][iSoundDuck]);
        
PrecacheSound(petInfo[i][iSoundAttack]);
        
PrecacheSound(petInfo[i][iSoundStun]);
        
PrecacheSound(petInfo[i][iSoundFoot0]);
        
PrecacheSound(petInfo[i][iSoundFoot1]);
        
PrecacheSound(petInfo[i][iSoundFoot2]);
    }
}

public 
Action:Event_Spawn(Handle:event, const String:name[], bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event"userid"));
    if(
petType[client] != 0)
    {
        
SpawnPet(client);
    }
}

public 
Action:Event_Death(Handle:event, const String:name[], bool:dontBroadcast)
{
    new 
victim GetClientOfUserId(GetEventInt(event"userid"));
    
KillPet(victim);
}

public 
OnPluginEnd()
{
    for (new 
1<=MaxClientsi++)
    {
        
KillPet(i);
    }
}

public 
Action:Command_KillPet(clientargs)
{
    new 
String:target[PLATFORM_MAX_PATH];
    
GetCmdArg(1targetPLATFORM_MAX_PATH);
    
decl String:target_name[MAX_TARGET_LENGTH];
    
decl target_list[MAXPLAYERS], target_count;
    new 
bool:tn_is_ml;
    if ((
target_count ProcessTargetString(
    
target,
    
client,
    
target_list,
    
MAXPLAYERS,
    
COMMAND_FILTER_ALIVE,
    
target_name,
    
sizeof(target_name),
    
tn_is_ml)) <= 0)
    {
        
ReplyToTargetError(clienttarget_count);
        return 
Plugin_Handled;
    }
    for (new 
0target_counti++)
    {
        
KillPet(target_list[i]);
    }
    return 
Plugin_Handled;
}

public 
Action:Command_SpawnPet(clientargs)
{
    new 
String:target[PLATFORM_MAX_PATH];
    new 
String:petindex[PLATFORM_MAX_PATH];
    
GetCmdArg(1targetPLATFORM_MAX_PATH);
    
GetCmdArg(2petindexPLATFORM_MAX_PATH);
    
decl String:target_name[MAX_TARGET_LENGTH];
    
decl target_list[MAXPLAYERS], target_count;
    new 
bool:tn_is_ml;
    if ((
target_count ProcessTargetString(
    
target,
    
client,
    
target_list,
    
MAXPLAYERS,
    
COMMAND_FILTER_ALIVE,
    
target_name,
    
sizeof(target_name),
    
tn_is_ml)) <= 0)
    {
        
ReplyToTargetError(clienttarget_count);
        return 
Plugin_Handled;
    }
    for (new 
0target_counti++)
    {
        new 
strint StringToInt(petindex10);
        if(
strint == 0)
        {
            
ReplyToCommand(client"[SM] Invalid Pet Index");
        }
        else
        {
            
KillPet(target_list[i]);
            
petType[target_list[i]] = strint;
            
SpawnPet(target_list[i]);
        }
    }
    return 
Plugin_Handled;
}

public 
SpawnPet(client)
{
    
decl Float:pos[3];
    
GetClientAbsOrigin(clientpos);
    
OffsetLocation(pos);
    if(!(
pet[client] = CreateEntityByName("prop_dynamic_override")))
    {
        return;
    }
    
SetEntPropEnt(pet[client], Prop_Send"m_hOwnerEntity"client);
    new 
String:clientname[PLATFORM_MAX_PATH];
    
GetClientName(clientclientnamePLATFORM_MAX_PATH);
    new 
String:PetName[PLATFORM_MAX_PATH];
    
Format(PetNamePLATFORM_MAX_PATH"%s Pet"clientname);
    
DispatchKeyValue(pet[client], "targetname"PetName);
    
DispatchKeyValue(pet[client], "model"petInfo[petType[client]][iModel]);
    
DispatchKeyValue(pet[client], "DefaultAnim"petInfo[petType[client]][iAnimIdle]);
    
SetVariantString(petInfo[petType[client]][iAnimIdle]);
    
AcceptEntityInput(pet[client], "SetDefaultAnimation");
    
SetVariantInt(petInfo[petType[client]][iSkin]);
    
AcceptEntityInput(pet[client], "skin");
    
SetVariantInt(petInfo[petType[client]][iBodyGroup]);
    
AcceptEntityInput(pet[client], "SetBodyGroup");
    
SetVariantInt(2);
    
AcceptEntityInput(pet[client], "solid");
    
DispatchKeyValueFloat(pet[client], "modelscale"petInfo[petType[client]][iModelSize]);
    
AttachModel(pet[client], petInfo[petType[client]][iHat], petInfo[petType[client]][iHatBone]);
    
AttachModel(pet[client], petInfo[petType[client]][iWep], petInfo[petType[client]][iWepBone]);
    
AttachParticle(pet[client], petInfo[petType[client]][iParticle], petInfo[petType[client]][iParBone]);
    
DispatchSpawn(pet[client]);
    
TeleportEntity(pet[client], posNULL_VECTORNULL_VECTOR);
    
SetPetState(clientSTATE_SPAWNING);
    
CreateParticle(petInfo[petType[client]][iSpawnParticle], pos);
    
EmitAmbientSound(petInfo[petType[client]][iSoundGeneric], pospet[client]);
    
EmitAmbientSound(petInfo[petType[client]][iSoundSpawn], pos);
    
SafeKillTimer(soundTimer[client]);
    
soundTimer[client] = CreateTimer(10.0Timer_GenericSoundclient);
    
SDKHook(clientSDKHook_PreThinkPetThink);
}

public 
PetThink(client)
{
    if(!
IsValidEntity(pet[client]))
    {
        
SDKUnhook(clientSDKHook_PreThinkPetThink);
        return;
    }
    
decl Float:pos[3], Float:ang[3], Float:clientPos[3], Float:clientAng[3];
    
GetEntPropVector(pet[client], Prop_Data"m_vecOrigin"pos);
    
GetEntPropVector(pet[client], Prop_Data"m_angRotation"ang);
    
GetClientAbsOrigin(clientclientPos);
    
GetClientAbsAngles(clientclientAng);
    new 
Float:dist GetVectorDistance(clientPospos);
    new 
Float:distX clientPos[0] - pos[0];
    new 
Float:distY clientPos[1] - pos[1];
    new 
Float:speed = (dist 64.0) / 54;
    
Math_Clamp(speed, -4.04.0);
    if(
FloatAbs(speed) < 0.3)
        
speed *= 0.1;
    if(
dist 1024.0)
    {
        
decl Float:posTmp[3];
        
GetClientAbsOrigin(clientposTmp);
        
OffsetLocation(posTmp);
        
TeleportEntity(pet[client], posTmpNULL_VECTORNULL_VECTOR);
        
GetEntPropVector(pet[client], Prop_Data"m_vecOrigin"pos);
    }
    if(
pos[0] < clientPos[0])    pos[0] += speed;
    if(
pos[0] > clientPos[0])    pos[0] -= speed;
    if(
pos[1] < clientPos[1])    pos[1] += speed;
    if(
pos[1] > clientPos[1])    pos[1] -= speed;
    switch(
petInfo[petType[client]][iHeight])
    {
        case 
HEIGHT_GROUNDpos[2] = clientPos[2];
        case 
HEIGHT_HOVERpos[2] = clientPos[2] + 32.0;
        case 
HEIGHT_FLYpos[2] = clientPos[2] + 96.0;
    }
    if(!(
GetEntityFlags(client) & FL_ONGROUND))
        
SetPetState(clientSTATE_JUMPING);
    else if(
FloatAbs(speed) > 0.2)
        
SetPetState(clientSTATE_WALKING);
    else if(
TF2_IsPlayerInCondition(clientTFCond_Taunting))
        
SetPetState(clientSTATE_TAUNTING);
    else if(
GetClientButtons(client) & IN_DUCK)
        
SetPetState(clientSTATE_DUCKING);
    else if(
GetClientButtons(client) & IN_ATTACK)
        
SetPetState(clientSTATE_ATTACKING);
    else if(
TF2_IsPlayerInCondition(clientTFCond_Dazed))
        
SetPetState(clientSTATE_STUN);
    else
        
SetPetState(clientSTATE_IDLE);
    
ang[1] = (ArcTangent2(distYdistX) * 180) / 3.14;
    
TeleportEntity(pet[client], posangNULL_VECTOR);
    if(
petState[client] == STATE_ATTACKING)
    {
        
TeleportEntity(pet[client], NULL_VECTORclientAngNULL_VECTOR);
    }
    if(
petState[client] == STATE_ATTACKING && inColldown[client] == false)
    {
        
CreateProjectile(client);
        
inColldown[client] = true;
        
CreateTimer(3.0PetColldownclient);
    }
    if(
petState[client] == STATE_WALKING && inColldown[client] == false)
    {
        new 
footstep GetRandomInt(02);
        switch(
footstep)
        {
            case 
0EmitAmbientSound(petInfo[petType[client]][iSoundFoot0], pospet[client], ___petInfo[petType[client]][iPitch]);
            case 
1EmitAmbientSound(petInfo[petType[client]][iSoundFoot1], pospet[client], ___petInfo[petType[client]][iPitch]);
            case 
2EmitAmbientSound(petInfo[petType[client]][iSoundFoot2], pospet[client], ___petInfo[petType[client]][iPitch]);
        }
        
inColldown[client] = true;
        
CreateTimer(0.5PetColldownclient);
    }
}

public 
Action:PetColldown(Handle:timerany:client)
{
    
inColldown[client] = false;
}

public 
CreateProjectile(client)
{
    if(!
SetTeleportEndPoint(client))
    {
        
PrintToChat(client"[SM] Could not find spawn point.");
    }
    new 
Float:vAngles[3];
    new 
Float:vPosition[3];
    new 
Float:ang[3];
    
GetClientEyeAngles(clientang);
    
GetEntPropVector(pet[client], Prop_Data"m_angRotation"vAngles);
    
GetEntPropVector(pet[client], Prop_Data"m_vecOrigin"vPosition);
    new 
iTeam GetClientTeam(client);
    new 
iProject CreateEntityByName(petInfo[petType[client]][iProjectile]);
    
decl Float:vVelocity[3];
    
decl Float:vBuffer[3];
    
GetAngleVectors(vAnglesvBufferNULL_VECTORNULL_VECTOR);
    
vVelocity[0] = vBuffer[0]*1100.0;
    
vVelocity[1] = vBuffer[1]*1100.0;
    
vVelocity[2] = vBuffer[2]*1100.0;
    
SetEntPropEnt(iProjectProp_Send"m_hOwnerEntity"client);
    
SetEntProp(iProjectProp_Send"m_bCritical"100);
    
SetEntProp(iProjectProp_Send"m_iTeamNum"iTeam1);
    
SetEntProp(iProjectProp_Send"m_nSkin", (iTeam-2));
    
vPosition[2] += 50.0;
    
TeleportEntity(iProjectvPositionvAnglesNULL_VECTOR);
    
SetVariantInt(iTeam);
    
AcceptEntityInput(iProject"TeamNum", -1, -10);
    
SetVariantInt(iTeam);
    
AcceptEntityInput(iProject"SetTeam", -1, -10); 
    
DispatchSpawn(iProject);
    
TeleportEntity(iProjectNULL_VECTORangvVelocity);
}

public 
SetTeleportEndPoint(client)
{
    
decl Float:vAngles[3];
    
decl Float:vOrigin[3];
    
decl Float:vBuffer[3];
    
decl Float:vStart[3];
    
decl Float:Distance;
    
GetClientEyePosition(client,vOrigin);
    
GetClientEyeAngles(clientvAngles);
    new 
Handle:trace TR_TraceRayFilterEx(vOriginvAnglesMASK_SHOTRayType_InfiniteTraceEntityFilterPlayer);
    if(
TR_DidHit(trace))
    {        
            
TR_GetEndPosition(vStarttrace);
        
GetVectorDistance(vOriginvStartfalse);
        
Distance = -35.0;
            
GetAngleVectors(vAnglesvBufferNULL_VECTORNULL_VECTOR);
        
g_pos[0] = vStart[0] + (vBuffer[0]*Distance);
        
g_pos[1] = vStart[1] + (vBuffer[1]*Distance);
        
g_pos[2] = vStart[2] + (vBuffer[2]*Distance);
    }
    else
    {
        
CloseHandle(trace);
        return 
false;
    }
    
CloseHandle(trace);
    return 
true;
}

public 
bool:TraceEntityFilterPlayer(entitycontentsMask)
{
    return 
entity MaxClients;
}

public 
Action:Timer_GenericSound(Handle:timerany:client)
{
    if(
pet[client] == || IsValidEntity(pet[client]) == false)
    {
        
SafeKillTimer(timer);
    }
    
decl Float:pos[3];
    
GetEntPropVector(pet[client], Prop_Data"m_vecOrigin"pos);
    
EmitAmbientSound(petInfo[petType[client]][iSoundGeneric], pospet[client], ___petInfo[petType[client]][iPitch]);
    
soundTimer[client] = CreateTimer(GetRandomFloat(20.060.0), Timer_GenericSoundclient);
}

public 
SetPetState(clientstatus)
{
    
decl Float:pos[3];
    
GetEntPropVector(pet[client], Prop_Data"m_vecOrigin"pos);
    if(
petState[client] == status)
    {
        return;
    }
    switch(
status)
    {
        case 
STATE_IDLESetPetAnim(clientpetInfo[petType[client]][iAnimIdle]);
        case 
STATE_WALKINGSetPetAnim(clientpetInfo[petType[client]][iAnimWalk]);
        case 
STATE_JUMPING:
        {
            
SetPetAnim(clientpetInfo[petType[client]][iAnimJump]);
            
EmitAmbientSound(petInfo[petType[client]][iSoundJump], pospet[client], ___petInfo[petType[client]][iPitch]);
        }
        case 
STATE_TAUNTING:
        {
            
SetPetAnim(clientpetInfo[petType[client]][iAnimTaunt]);
            
EmitAmbientSound(petInfo[petType[client]][iSoundTaunt], pospet[client], ___petInfo[petType[client]][iPitch]);
        }
        case 
STATE_SPAWNING:
        {
            
SetPetAnim(clientpetInfo[petType[client]][iAnimSpawn]);
            
EmitAmbientSound(petInfo[petType[client]][iSoundSpawn], pospet[client], ___petInfo[petType[client]][iPitch]);
        }
        case 
STATE_DUCKING:
        {
            
SetPetAnim(clientpetInfo[petType[client]][iAnimDuck]);
            
EmitAmbientSound(petInfo[petType[client]][iSoundDuck], pospet[client], ___petInfo[petType[client]][iPitch]);
        }
        case 
STATE_ATTACKING:
        {
            
SetPetAnim(clientpetInfo[petType[client]][iAnimAttack]);
            
EmitAmbientSound(petInfo[petType[client]][iSoundAttack], pospet[client], ___petInfo[petType[client]][iPitch]);
        }
        case 
STATE_STUN:
        {
            
SetPetAnim(clientpetInfo[petType[client]][iAnimStun]);
            
EmitAmbientSound(petInfo[petType[client]][iSoundStun], pospet[client], ___petInfo[petType[client]][iPitch]);
        }
    }
    
petState[client] = status;
}

public 
SetPetAnim(client, const String:anim[])
{
    
SetVariantString(anim);
    
AcceptEntityInput(pet[client], "SetAnimation");
}

public 
OffsetLocation(Float:pos[3])
{
    
pos[0] += GetRandomFloat(-128.0128.0);
    
pos[1] += GetRandomFloat(-128.0128.0);
}


public 
KillPet(client)
{
    if(
pet[client] == 0)
    {
        return;
    }
    
decl Float:pos[3];
    
GetEntPropVector(pet[client], Prop_Data"m_vecOrigin"pos);
    
SDKUnhook(clientSDKHook_PreThinkPetThink);
    
AcceptEntityInput(pet[client], "Kill");
    
CreateParticle(petInfo[petType[client]][iDeathParticle], pos);
    
pet[client] = 0;

arthurdead is offline