Advertisement
Henrybk

keeppreserveskilltest

Apr 26th, 2017
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 8.88 KB | None | 0 0
  1. # =======================
  2. # keepPreserveSkill v0.9.7 beta
  3. # =======================
  4. # This plugin is licensed under the GNU GPL
  5. # Created by Henrybk
  6. #
  7. # What it does: tries to keep your copied skill from being lost
  8. #
  9. #
  10. # Example (put in config.txt):
  11. #  
  12. #   Config example:
  13. #   keepPreserveSkill_on 1 (boolean active/inactive)
  14. #   keepPreserveSkill_handle MG_COLDBOLT (handle of skill to be kept)
  15. #   keepPreserveSkill_timeout 450 (amount of seconds after we used preserve in which we will start to try to use it again while walking, taking items, routing, etc)
  16. #   keepPreserveSkill_timeoutCritical 550 (same as above, but after this time we will try to use it even while attacking)
  17. #
  18. #
  19. # Extras: The plugin will make the character try to teleport if there is a monster on screen and you don't have preserve activated, this can make you keep teleport forever on very mobbed maps like juperus.
  20.  
  21. package keepPreserveSkill;
  22.  
  23. use Plugins;
  24. use Globals;
  25. use Log qw(message warning error debug);
  26. use File::Spec;
  27. use JSON::Tiny qw(from_json to_json);
  28. use AI;
  29. use Misc;
  30. use Network;
  31. use Network::Send;
  32. use Utils;
  33. use Commands;
  34. use Actor;
  35.  
  36. use constant {
  37.     INACTIVE => 0,
  38.     ACTIVE => 1
  39. };
  40.  
  41. Plugins::register('keepPreserveSkill', 'Tries to not get the preserved skill lost', , \&on_unload, \&on_unload);
  42.  
  43. my $base_hooks = Plugins::addHooks(
  44.     ['start3',        \&on_start3],
  45.     ['postloadfiles', \&checkConfig],
  46.     ['configModify',  \&on_configModify]
  47. );
  48.  
  49. our $folder = $Plugins::current_plugin_folder;
  50.  
  51. my $status = INACTIVE;
  52.  
  53. my $plugin_name = "keepPreserveSkill";
  54.  
  55. our %mobs;
  56. my $wait_for_skills_packet_hooks = undef;
  57. my $last_preserve_use_time;
  58. my $keeping_hooks;
  59. my $keep_skill;
  60. my $preserve_skill;
  61.  
  62. sub on_unload {
  63.    Plugins::delHook($base_hooks);
  64.    changeStatus(INACTIVE);
  65.    message "[$plugin_name] Plugin unloading or reloading.\n", 'success';
  66. }
  67.  
  68. sub on_start3 {
  69.     $preserve_skill = new Skill(handle => 'ST_PRESERVE');
  70.     my $file = File::Spec->catdir($folder,'mobs_info.json');
  71.     %mobs = %{loadFile($file)};
  72.     if (!%mobs || scalar keys %mobs == 0) {
  73.         error "[$plugin_name] Could not load mobs info due to a file loading problem.\n".
  74.               "[$plugin_name] File which was not loaded: $file.\n";
  75.         return;
  76.     }
  77.     Log::message( sprintf "[%s] Found %d mobs.\n", $plugin_name, scalar keys %mobs );
  78. }
  79.  
  80. sub loadFile {
  81.     my $file = shift;
  82.  
  83.     return unless (open FILE, "<:utf8", $file);
  84.     my @lines = <FILE>;
  85.     close(FILE);
  86.     chomp @lines;
  87.     my $jsonString = join('',@lines);
  88.  
  89.     my %converted = %{from_json($jsonString, { utf8  => 1 } )};
  90.  
  91.     return \%converted;
  92. }
  93.  
  94. sub checkConfig {
  95.     if (validate_settings()) {
  96.         changeStatus(ACTIVE);
  97.     } else {
  98.         changeStatus(INACTIVE);
  99.     }
  100. }
  101.  
  102. sub on_configModify {
  103.     my (undef, $args) = @_;
  104.     return unless ($args->{key} eq 'keepPreserveSkill_on' || $args->{key} eq 'keepPreserveSkill_handle' || $args->{key} eq 'keepPreserveSkill_timeout' || $args->{key} eq 'keepPreserveSkill_timeoutCritical');
  105.     if (validate_settings($args->{key}, $args->{val})) {
  106.         changeStatus(ACTIVE);
  107.     } else {
  108.         changeStatus(INACTIVE);
  109.     }
  110. }
  111.  
  112. sub validate_settings {
  113.     my ($key, $val) = @_;
  114.    
  115.     my $on_off;
  116.     my $handle;
  117.     my $timeout;
  118.     my $timeoutCritical;
  119.     if (!defined $key) {
  120.         $on_off = $config{keepPreserveSkill_on};
  121.         $handle = $config{keepPreserveSkill_handle};
  122.         $timeout = $config{keepPreserveSkill_timeout};
  123.         $timeoutCritical = $config{keepPreserveSkill_timeoutCritical};
  124.     } else {
  125.         $on_off =           ($key eq 'keepPreserveSkill_on'              ?   $val : $config{keepPreserveSkill_on});
  126.         $handle =           ($key eq 'keepPreserveSkill_handle'          ?   $val : $config{keepPreserveSkill_handle});
  127.         $timeout =          ($key eq 'keepPreserveSkill_timeout'         ?   $val : $config{keepPreserveSkill_timeout});
  128.         $timeoutCritical =  ($key eq 'keepPreserveSkill_timeoutCritical' ?   $val : $config{keepPreserveSkill_timeoutCritical});
  129.     }
  130.    
  131.     my $error = 0;
  132.     if (!defined $on_off || !defined $handle || !defined $timeout || !defined $timeoutCritical) {
  133.         message "[$plugin_name] There are config keys not defined.\n","system";
  134.         $error = 1;
  135.        
  136.     } elsif ($on_off !~ /^[01]$/) {
  137.         message "[$plugin_name] Value of key 'keepPreserveSkill_on' must be 0 or 1.\n","system";
  138.         $error = 1;
  139.        
  140.     } elsif ($timeout !~ /^\d+$/) {
  141.         message "[$plugin_name] Value of key 'keepPreserveSkill_timeout' must be a number.\n","system";
  142.         $error = 1;
  143.        
  144.     } elsif ($timeoutCritical !~ /^\d+$/) {
  145.         message "[$plugin_name] Value of key 'keepPreserveSkill_timeoutCritical' must be a number.\n","system";
  146.         $error = 1;
  147.     }
  148.    
  149.     if ($error == 1) {
  150.         configModify('keepPreserveSkill_on', 0) if (defined $on_off && $on_off != 0);
  151.         return 0;
  152.     }
  153.    
  154.     return 0 unless ($on_off == 1);
  155.    
  156.     $keep_skill = new Skill(handle => $handle);
  157.    
  158.     if ($char && $net && $net->getState() == Network::IN_GAME) {
  159.         unless (check_skills()) {
  160.             configModify('keepPreserveSkill_on', 0);
  161.             return 0;
  162.         }
  163.        
  164.     } else {
  165.         if (!defined $wait_for_skills_packet_hooks) {
  166.             $wait_for_skills_packet_hooks = Plugins::addHooks(
  167.                 ['packet/skills_list',  \&on_skills_packet]
  168.             );
  169.         }
  170.         return 0;
  171.     }
  172.    
  173.     return 1;
  174. }
  175.  
  176. sub on_skills_packet {
  177.     if (check_skills()) {
  178.         changeStatus(ACTIVE);
  179.     } else {
  180.         configModify('keepPreserveSkill_on', 0);
  181.         changeStatus(INACTIVE);
  182.     }
  183.     Plugins::delHook($wait_for_skills_packet_hooks);
  184.     undef $wait_for_skills_packet_hooks;
  185. }
  186.  
  187. sub check_skills {
  188.     if (!$char->getSkillLevel($preserve_skill)) {
  189.         message "[$plugin_name] You don't have the skill Preserve\n","system";
  190.         return 0;
  191.        
  192.     } elsif (!$char->getSkillLevel($keep_skill)) {
  193.         message "[$plugin_name] You don't have the skill you want to keep: ".$keep_skill->getName."\n","system";
  194.         return 0;
  195.     }
  196.    
  197.     return 1;
  198. }
  199.  
  200. sub changeStatus {
  201.     my $new_status = shift;
  202.    
  203.     return if ($new_status == $status);
  204.    
  205.     if ($new_status == INACTIVE) {
  206.         Plugins::delHook($keeping_hooks);
  207.         debug "[$plugin_name] Plugin stage changed to 'INACTIVE'\n", "$plugin_name", 1;
  208.        
  209.     } elsif ($new_status == ACTIVE) {
  210.         $keeping_hooks = Plugins::addHooks(
  211.             ['AI_pre',\&on_AI_pre, undef],
  212.             ['Actor::setStatus::change',\&on_statusChange, undef],
  213.             ['packet/skill_update',\&on_skills_update, undef],
  214.             ['packet/skills_list',\&on_skills_update, undef],
  215.             ['packet/skill_add',\&on_skills_update, undef],
  216.             ['packet/skill_delete',\&on_skills_update, undef]
  217.         );
  218.         debug "[$plugin_name] Plugin stage changed to 'ACTIVE'\n", "$plugin_name", 1;
  219.     }
  220.    
  221.     $status = $new_status;
  222. }
  223.  
  224. ######
  225.  
  226. sub on_statusChange {
  227.     my (undef, $args) = @_;
  228.     if ($args->{handle} eq 'EFST_PRESERVE' && $args->{actor_type}->isa('Actor::You') && $args->{flag} == 1) {
  229.         message "[$plugin_name] Preserve was used, reseting timer\n","system";
  230.         $last_preserve_use_time = time;
  231.     }
  232. }
  233.  
  234. sub on_skills_update {
  235.     unless (check_skills()) {
  236.         error "[$plugin_name] You lost the skill you want to keep or the preserve skill. Deactivating plugin.\n.";
  237.         configModify('keepPreserveSkill_on', 0);
  238.         changeStatus(INACTIVE);
  239.     }
  240. }
  241.  
  242. sub on_AI_pre {
  243.     return if (!$char || !$net || $net->getState() != Network::IN_GAME);
  244.     return if ($char->{muted});
  245.     return if ($char->{casting});
  246.     return if ($char->statusActive('EFST_POSTDELAY'));
  247.    
  248.     if ($char->statusActive('EFST_PRESERVE')) {
  249.         return unless (timeOut($config{keepPreserveSkill_timeout}, $last_preserve_use_time));
  250.         my $timeout_reuse = ($last_preserve_use_time + $config{keepPreserveSkill_timeout} - time);
  251.         if (AI::isIdle || AI::is(qw(mapRoute follow sitAuto take sitting clientSuspend move route items_take items_gather))) {
  252.             message "[$plugin_name] Using non-critical preserve with ".$timeout_reuse." seconds left on counter\n","system";
  253.             Commands::run("ss 475 1");
  254.             return;
  255.         }
  256.        
  257.         return unless (timeOut($config{keepPreserveSkill_timeoutCritical}, $last_preserve_use_time));
  258.         $timeout_reuse = ($last_preserve_use_time + $config{keepPreserveSkill_timeoutCritical} - time);
  259.         if (AI::is(qw(attack))) {
  260.             message "[$plugin_name] Using critical preserve with ".$timeout_reuse." seconds left on counter\n","system";
  261.             Commands::run("ss 475 1");
  262.             return;
  263.         }
  264.        
  265.     } else {
  266.         my $teleport = 0;
  267.         if (ai_getAggressives()) {
  268.             message "[$plugin_name] A monster is attacking us, teleporting to not lose skill\n","system";
  269.             $teleport = 1;
  270.            
  271.         } elsif (scalar @{$monstersList->getItems()} > 0) {
  272.             foreach my $mob (@{$monstersList->getItems()}) {
  273.                 my $id = $mob->{nameID};
  274.                 my $mob_info = $mobs{$id};
  275.                 next unless ($mob_info->{is_aggressive} == 1);
  276.                 message "[$plugin_name] Aggressive monster near, teleporting to not lose skill\n","system";
  277.                 $teleport = 1;
  278.                 last;
  279.             }
  280.         }
  281.         if ($teleport == 1) {
  282.             if (main::useTeleport(1)) {
  283.                 message "[$plugin_name] Teleport sent.\n", "info";
  284.             } else {
  285.                 message "[$plugin_name] Cannot use teleport.\n", "info";
  286.             }
  287.         } else {
  288.             message "[$plugin_name] Using preserve skill\n","system";
  289.             Commands::run("ss 475 1");
  290.         }
  291.     }
  292. }
  293.  
  294. return 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement