Advertisement
Guest User

TF2 Attachments Fix

a guest
Jun 27th, 2010
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.26 KB | None | 0 0
  1. /**************************************************************************
  2.  *                                                                        *
  3.  *                        Attachment Workaround                           *
  4.  *                       Author: Mecha the Slag                           *
  5.  *                           Version: 1.0.0                               *
  6.  *                                                                        *
  7.  **************************************************************************/
  8.  
  9. new Handle:attachments_array = INVALID_HANDLE;
  10.  
  11. /**
  12.  * Attaches a child to a parent.
  13.  *
  14.  * @param child         Child entity.
  15.  * @param parent        Parent entity.
  16.  * @param variant       Attachment point. Empty for none.
  17.  * @return              True if able to create attachment. False if fail.
  18.  *  
  19.  * On error/Errors:     If unable to create attachment.
  20.  */
  21. stock CAttach(child, parent, const String:variant[]) {
  22.     if (attachments_array == INVALID_HANDLE) attachments_array = CreateArray(2);
  23.     new link = CGetLink(child);
  24.     if (link == -1) link = CAddLink(child);
  25.     if (link == -1) {
  26.         decl String:Classname[128];
  27.         if (GetEdictClassname(child, Classname, sizeof(Classname))) ThrowError("Unable to create link for entity %s", Classname);
  28.         else ThrowError("Unable to create link for unknown entity");
  29.         return false;
  30.     }
  31.    
  32.     new String:name[16];
  33.     Format(name, sizeof(name), "target%i", parent);
  34.     DispatchKeyValue(parent, "targetname", name);
  35.  
  36.     new String:name2[32];
  37.     GetEntPropString(parent, Prop_Data, "m_iName", name2, sizeof(name2));
  38.     DispatchKeyValue(link, "parentname", name2);
  39.    
  40.    
  41.     SetVariantString(name2);
  42.     AcceptEntityInput(link, "SetParent", link, link, 0);
  43.     if (!StrEqual(variant, "")) {
  44.         SetVariantString("flag");
  45.         AcceptEntityInput(link, "SetParentAttachment", link, link, 0);
  46.     }
  47.     return true;
  48. }
  49.  
  50. /**
  51.  * Detaches an entity from all its parents. Should be called before the entity is killed.
  52.  *
  53.  * @param ent           Entity to detach.
  54.  * @return              True if an attachment is destroyed. False if no attachment.
  55.  *  
  56.  * On error/Errors:     Never.
  57.  */
  58. stock CDetach(ent) {
  59.     if (attachments_array == INVALID_HANDLE) attachments_array = CreateArray(2);
  60.    
  61.     new link = CGetLink(ent);
  62.     if (link != -1) {
  63.         AcceptEntityInput(link, "kill");
  64.         for (new i = 0; i < GetArraySize(attachments_array); i++) {
  65.             new ent2 = GetArrayCell(attachments_array, i);
  66.             if (ent == ent2) RemoveFromArray(attachments_array, i);
  67.         }
  68.        
  69.         return true;
  70.     }
  71.     return false;
  72. }
  73.  
  74. stock CGetLink(ent) {
  75.     for (new i = 0; i < GetArraySize(attachments_array); i++) {
  76.         new ent2 = GetArrayCell(attachments_array, i);
  77.         if (ent == ent2) return (GetArrayCell(attachments_array, i, 1));
  78.     }
  79.     return -1;
  80. }
  81.  
  82. stock CAddLink(ent) {
  83.     new String:name_ent[16];
  84.     Format(name_ent, sizeof(name_ent), "target%i", ent);
  85.     DispatchKeyValue(ent, "targetname", name_ent);
  86.  
  87.     new link = CreateEntityByName("env_sprite");
  88.     if (IsValidEntity(link)) {
  89.         new Float:Origin[3];
  90.         new Float:Rotation[3];
  91.         GetEntPropVector(ent, Prop_Send, "m_vecOrigin", Origin);
  92.         GetEntPropVector(ent, Prop_Send, "m_angRotation", Rotation);
  93.    
  94.         new String:name_link[16];
  95.         Format(name_link, sizeof(name_link), "target%i", link);
  96.         DispatchKeyValue(link, "targetname", name_link);
  97.        
  98.         DispatchKeyValue(link, "classname", "env_sprite");
  99.         DispatchKeyValue(link, "spawnflags", "1");
  100.         DispatchKeyValue(link, "scale", "0");
  101.         DispatchKeyValue(link, "rendermode", "0");
  102.         DispatchKeyValue(link, "rendercolor", "255 255 255");
  103.        
  104.         DispatchKeyValue(link, "model", "materials/sprites/minimap_icons/voiceicon.vmt");
  105.         DispatchSpawn(link);
  106.         TeleportEntity(link, Origin, Rotation, NULL_VECTOR);
  107.        
  108.         SetVariantString(name_link);
  109.         AcceptEntityInput(ent, "SetParent", ent, ent, 0);
  110.        
  111.         new index = PushArrayCell(attachments_array, ent);
  112.         SetArrayCell(attachments_array, index, link, 1);
  113.         return link;
  114.     }
  115.     return -1;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement