Advertisement
Admigo

Extended Actor Functions

Apr 2nd, 2016
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 9.79 KB | None | 0 0
  1. /*
  2.  *  Extended actor functions by Emmet
  3.  *
  4.  *  Features:
  5.  *    Many useful functions for more actor control
  6.  *    Automatically preloads animations for an actor
  7.  *    Damage is automatically deducted
  8.  *    Attach 3D labels to an actor
  9.  *    Lots more!
  10.  *
  11.  *  Creation date: May 8, 2015
  12. */
  13.  
  14. // Resynchronize an actor.
  15. forward ResyncActor(actorid);
  16. // Respawns an actor.
  17. forward RespawnActor(actorid);
  18. // Checks if the actor is dead.
  19. forward IsActorDead(actorid);
  20. // Sets an actor's respawn time (after death).
  21. forward SetActorRespawnTime(actorid, time);
  22. // Checks if a player is in range of an actor.
  23. forward IsPlayerInRangeOfActor(playerid, actorid, Float:radius = 5.0);
  24. // Attaches a 3D text label to an actor.
  25. forward Text3D:Attach3DTextLabelToActor(actorid, text[], color, Float:fOffsetX, Float:fOffsetY, Float:fOffsetZ, Float:distance = 10.0, worldid = 0, testlos = 0);
  26.  
  27. // Called when an actor dies.
  28. forward OnActorDeath(actorid, killerid, reason);
  29. // Called when an actor spawns.
  30. forward OnActorSpawn(actorid);
  31. // Called when a player aims at an actor.
  32. forward OnPlayerTargetActor(playerid, newtarget, oldtarget);
  33.  
  34. // List of animation libraries used for preloading.
  35. static s_AnimationLibraries[][] = {
  36.     !"AIRPORT",    !"ATTRACTORS",   !"BAR",         !"BASEBALL",
  37.     !"BD_FIRE",    !"BEACH",        !"BENCHPRESS",  !"BF_INJECTION",
  38.     !"BIKED",      !"BIKEH",        !"BIKELEAP",    !"BIKES",
  39.     !"BIKEV",      !"BIKE_DBZ",     !"BMX",         !"BOMBER",
  40.     !"BOX",        !"BSKTBALL",     !"BUDDY",       !"BUS",
  41.     !"CAMERA",     !"CAR",          !"CARRY",       !"CAR_CHAT",
  42.     !"CASINO",     !"CHAINSAW",     !"CHOPPA",      !"CLOTHES",
  43.     !"COACH",      !"COLT45",       !"COP_AMBIENT", !"COP_DVBYZ",
  44.     !"CRACK",      !"CRIB",         !"DAM_JUMP",     !"DANCING",
  45.     !"DEALER",     !"DILDO",        !"DODGE",        !"DOZER",
  46.     !"DRIVEBYS",   !"FAT",          !"FIGHT_B",      !"FIGHT_C",
  47.     !"FIGHT_D",    !"FIGHT_E",      !"FINALE",       !"FINALE2",
  48.     !"FLAME",      !"FLOWERS",      !"FOOD",         !"FREEWEIGHTS",
  49.     !"GANGS",      !"GHANDS",       !"GHETTO_DB",    !"GOGGLES",
  50.     !"GRAFFITI",   !"GRAVEYARD",    !"GRENADE",      !"GYMNASIUM",
  51.     !"HAIRCUTS",   !"HEIST9",       !"INT_HOUSE",    !"INT_OFFICE",
  52.     !"INT_SHOP",   !"JST_BUISNESS", !"KART",         !"KISSING",
  53.     !"KNIFE",      !"LAPDAN1",      !"LAPDAN2",      !"LAPDAN3",
  54.     !"LOWRIDER",   !"MD_CHASE",     !"MD_END",       !"MEDIC",
  55.     !"MISC",       !"MTB",          !"MUSCULAR",     !"NEVADA",
  56.     !"ON_LOOKERS", !"OTB",          !"PARACHUTE",    !"PARK",
  57.     !"PAULNMAC",   !"PED",          !"PLAYER_DVBYS", !"PLAYIDLES",
  58.     !"POLICE",     !"POOL",         !"POOR",         !"PYTHON",
  59.     !"QUAD",       !"QUAD_DBZ",     !"RAPPING",      !"RIFLE",
  60.     !"RIOT",       !"ROB_BANK",     !"ROCKET",       !"RUSTLER",
  61.     !"RYDER",      !"SCRATCHING",   !"SHAMAL",       !"SHOP",
  62.     !"SHOTGUN",    !"SILENCED",     !"SKATE",        !"SMOKING",
  63.     !"SNIPER",     !"SPRAYCAN",     !"STRIP",        !"SUNBATHE",
  64.     !"SWAT",       !"SWEET",        !"SWIM",         !"SWORD",
  65.     !"TANK",       !"TATTOOS",      !"TEC",          !"TRAIN",
  66.     !"TRUCK",      !"UZI",          !"VAN",          !"VENDING",
  67.     !"VORTEX",     !"WAYFARER",     !"WEAPONS",      !"WUZI",
  68.     !"WOP",        !"GFUNK",        !"RUNNINGMAN"
  69. };
  70.  
  71. static s_ActorRespawnTimer[MAX_ACTORS] = {-1, ...};
  72. static s_TargetActor[MAX_PLAYERS] = {INVALID_ACTOR_ID, ...};
  73. static s_3DTextActorID[MAX_3DTEXT_GLOBAL] = {INVALID_ACTOR_ID, ...};
  74.  
  75. stock static PreloadActorAnimations(actorid)
  76. {
  77.     for(new i = 0; i < sizeof(s_AnimationLibraries); i ++)
  78.     {
  79.         ApplyActorAnimation(actorid, s_AnimationLibraries[i], "null", 0.0, 0, 0, 0, 0, 0);
  80.     }
  81. }
  82.  
  83. stock ResyncActor(actorid)
  84. {
  85.     if(IsValidActor(actorid))
  86.     {
  87.         new
  88.             Float:x,
  89.             Float:y,
  90.             Float:z,
  91.             worldid = GetActorVirtualWorld(actorid);
  92.            
  93.         // Get the actor's position.
  94.         GetActorPos(actorid, x, y, z);
  95.         // Set their position and world to some arbitrary values.
  96.         SetActorPos(actorid, 1000.0, -2000.0, 500.0);
  97.         SetActorVirtualWorld(actorid, random(cellmax));
  98.         // We need a timer to defer the actions. Doing it right away doesn't seem to work.
  99.         SetTimerEx("RestoreActor", 850, 0, "iifff", actorid, worldid, x, y, z);
  100.         return 1;
  101.     }
  102.     return 0;
  103. }
  104.  
  105. forward RestoreActor(actorid, worldid, Float:x, Float:y, Float:z);
  106. public RestoreActor(actorid, worldid, Float:x, Float:y, Float:z)
  107. {
  108.     SetActorVirtualWorld(actorid, worldid);
  109.     SetActorPos(actorid, x, y, z);
  110. }
  111.  
  112. // That's right, a public function. See "SetActorRespawnTime" for details.
  113. forward RespawnActor(actorid);
  114. public RespawnActor(actorid)
  115. {
  116.     if(IsValidActor(actorid))
  117.     {
  118.         // Reset the timer variable if set.
  119.         s_ActorRespawnTimer[actorid] = -1;
  120.         // Restore the actor's health.
  121.         SetActorHealth(actorid, 100.0);
  122.         // Clear the death animation.
  123.         ClearActorAnimations(actorid);
  124.         // Resync the actor.
  125.         ResyncActor(actorid);
  126.         // Call "OnActorSpawn".
  127.         CallLocalFunction("OnActorSpawn", "i", actorid);
  128.         return 1;
  129.     }
  130.     return 0;
  131. }
  132.  
  133. stock IsActorDead(actorid)
  134. {
  135.     if(IsValidActor(actorid))
  136.     {
  137.         new
  138.             Float:health;
  139.    
  140.         // Get the actor's health.
  141.         GetActorHealth(actorid, health);
  142.         return (health <= 0.0);
  143.     }
  144.     return 0;
  145. }
  146.  
  147. stock SetActorRespawnTime(actorid, time)
  148. {
  149.     if(IsValidActor(actorid))
  150.     {
  151.         if(s_ActorRespawnTimer[actorid] != -1)
  152.         {
  153.             // Kill any previously created timer.
  154.             KillTimer(s_ActorRespawnTimer[actorid]);
  155.         }
  156.         s_ActorRespawnTimer[actorid] = SetTimerEx("RespawnActor", time, 0, "i", actorid);
  157.         return 1;
  158.     }
  159.     return 0;
  160. }
  161.  
  162. stock IsPlayerInRangeOfActor(playerid, actorid, Float:radius = 5.0)
  163. {
  164.     new
  165.         Float:x,
  166.         Float:y,
  167.         Float:z;
  168.  
  169.     if (GetActorPos(actorid, x, y, z))
  170.     {
  171.         return IsPlayerInRangeOfPoint(playerid, radius, x, y, z) && GetPlayerVirtualWorld(playerid) == GetActorVirtualWorld(actorid);
  172.     }
  173.     return 0;
  174. }
  175.  
  176. stock Text3D:Attach3DTextLabelToActor(actorid, text[], color, Float:fOffsetX, Float:fOffsetY, Float:fOffsetZ, Float:distance = 10.0, worldid = 0, testlos = 0)
  177. {
  178.     new Text3D:textid = Text3D:INVALID_3DTEXT_ID;
  179.  
  180.     if(IsValidActor(actorid))
  181.     {
  182.         new
  183.             Float:x,
  184.             Float:y,
  185.             Float:z;
  186.  
  187.         GetActorPos(actorid, x, y, z);
  188.  
  189.         textid = Create3DTextLabel(text, color, x + fOffsetX, y + fOffsetY, (z + 0.8) + fOffsetZ, distance, worldid, testlos);
  190.        
  191.         if(textid != Text3D:INVALID_3DTEXT_ID)
  192.         {
  193.             s_3DTextActorID[_:textid] = actorid;
  194.         }
  195.     }
  196.     return textid;
  197. }
  198.  
  199. stock AC_CreateActor(modelid, Float:x, Float:y, Float:z, Float:rotation)
  200. {
  201.     new
  202.         actorid = INVALID_ACTOR_ID;
  203.  
  204.     if(0 <= modelid <= 311)
  205.     {
  206.         actorid = CreateActor(modelid, x, y, z, rotation);
  207.  
  208.         if (actorid != INVALID_ACTOR_ID)
  209.         {
  210.             // Reset the timer variable.
  211.             s_ActorRespawnTimer[actorid] = -1;
  212.             // Preload their animations.
  213.             PreloadActorAnimations(actorid);
  214.             // Let them know that they've spawned.
  215.             CallLocalFunction("OnActorSpawn", "i", actorid);
  216.         }
  217.     }
  218.     return actorid;
  219. }
  220.  
  221. stock AC_DestroyActor(actorid)
  222. {
  223.     if(IsValidActor(actorid))
  224.     {
  225.         for(new i = 0; i < MAX_3DTEXT_GLOBAL; i ++)
  226.         {
  227.             if(s_3DTextActorID[i] == actorid)
  228.             {
  229.                 // Destroy the 3D text label.
  230.                 Delete3DTextLabel(Text3D:i);
  231.                 // Reset the variable.
  232.                 s_3DTextActorID[i] = INVALID_ACTOR_ID;
  233.             }
  234.         }
  235.         return DestroyActor(actorid);
  236.     }
  237.     return 0;
  238. }
  239.  
  240. stock AC_Delete3DTextLabel(Text3D:textid)
  241. {
  242.     if(textid != Text3D:INVALID_3DTEXT_ID)
  243.     {
  244.         // Reset the variable.
  245.         s_3DTextActorID[_:textid] = INVALID_ACTOR_ID;
  246.         return Delete3DTextLabel(textid);
  247.     }
  248.     return 0;
  249. }
  250.  
  251. public OnPlayerUpdate(playerid)
  252. {
  253.     new target_actor = GetPlayerTargetActor(playerid);
  254.    
  255.     if (s_TargetActor[playerid] != target_actor)
  256.     {
  257.         CallLocalFunction("OnPlayerTargetActor", "iii", playerid, target_actor, s_TargetActor[playerid]);
  258.    
  259.         s_TargetActor[playerid] = target_actor;
  260.     }
  261.     #if defined AC_OnPlayerUpdate
  262.         return AC_OnPlayerUpdate(playerid);
  263.     #else
  264.         return 1;
  265.     #endif
  266. }
  267.  
  268. public OnPlayerGiveDamageActor(playerid, damaged_actorid, Float:amount, weaponid, bodypart)
  269. {
  270.     // Sanity checks for safety. Players can easily manipulate this data.
  271.     if(!IsValidActor(damaged_actorid) || (!(0 <= amount <= 500.0)) || (!(0 <= weaponid <= 42)))
  272.     {
  273.         return 0;
  274.     }
  275.     else if(!IsActorInvulnerable(damaged_actorid))
  276.     {
  277.         new
  278.             Float:health;
  279.            
  280.         // Deduct the damage from the actor, since it has to be done manually.
  281.         GetActorHealth(damaged_actorid, health);       
  282.         SetActorHealth(damaged_actorid, health - amount);
  283.        
  284.         if(health - amount < 1.0)
  285.         {
  286.             // There's a bug where animations aren't cleared when an actor dies.
  287.             ClearActorAnimations(damaged_actorid);
  288.             // Call the function to let them know they're dead.
  289.             CallLocalFunction("OnActorDeath", "iii", damaged_actorid, playerid, weaponid);
  290.         }
  291.     }
  292.     #if defined AC_OnPlayerGiveDamageActor
  293.         return AC_OnPlayerGiveDamageActor(playerid, damaged_actorid, amount, weaponid, bodypart);
  294.     #else
  295.         return 1;
  296.     #endif
  297. }
  298.  
  299. #if defined _ALS_CreateActor
  300.     #undef CreateActor
  301. #else
  302.     #define _ALS_CreateActor
  303. #endif
  304.  
  305. #define CreateActor AC_CreateActor
  306.  
  307. #if defined _ALS_DestroyActor
  308.     #undef DestroyActor
  309. #else
  310.     #define _ALS_DestroyActor
  311. #endif
  312.  
  313. #define DestroyActor AC_DestroyActor
  314.  
  315. #if defined _ALS_Delete3DTextLabel
  316.     #undef Delete3DTextLabel
  317. #else
  318.     #define _ALS_Delete3DTextLabel
  319. #endif
  320.  
  321. #define Delete3DTextLabel AC_Delete3DTextLabel
  322.  
  323. #if defined _ALS_OnPlayerUpdate
  324.     #undef OnPlayerUpdate
  325. #else
  326.     #define _ALS_OnPlayerUpdate
  327. #endif
  328.  
  329. #define OnPlayerUpdate AC_OnPlayerUpdate
  330.  
  331. #if defined AC_OnPlayerUpdate
  332.     forward AC_OnPlayerUpdate(playerid);
  333. #endif
  334.  
  335. #if defined _ALS_OnPlayerGiveDamageActor
  336.     #undef OnPlayerGiveDamageActor
  337. #else
  338.     #define _ALS_OnPlayerGiveDamageActor
  339. #endif
  340.  
  341. #define OnPlayerGiveDamageActor AC_OnPlayerGiveDamageActor
  342.  
  343. #if defined AC_OnPlayerGiveDamageActor
  344.     forward AC_OnPlayerGiveDamageActor(playerid, damaged_actorid, Float:amount, weaponid, bodypart);
  345. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement