Plan to walk in lock map !

Other plugins for extending OpenKore's functionality. This forum is only for posting new plugins and commenting on existing plugins. For support, use the Support forum.

Moderator: Moderators

Message
Author
sofax222
Developers
Developers
Posts: 214
Joined: 24 Nov 2010, 03:08
Noob?: Yes

Plan to walk in lock map !

#1 Post by sofax222 »

I write a plugin for planning to walk in lock map.
The code is following:

Code: Select all

# planWalk by Snoopy

package planWalk;

use strict;
#use warnings;

use Time::HiRes qw(time);

use Globals;
use Utils;
use Misc;
use AI;
use Log qw(debug message warning error);
use Translation;
use encoding 'utf8';

our $nSpot = -1;;

Plugins::register('planWalk', 'Auto Plan to walk.', \&on_unload, \&on_reload);

my $hooks = Plugins::addHooks(['process_RandomWalk', \&do_planWalk]);

my $prefix = "planWalk_";

sub on_unload {
   Plugins::delHooks($hooks);
}

sub on_reload {
   message "planWalk plugin reloading\n";
   Plugins::delHooks($hooks);
}

sub do_planWalk {
   return if (!$config{autoPlanWalk} || $config{lockMap} ne $field->baseName);
#   message T("Do plan walking route to:\n");

   $nSpot++;
   $nSpot = 0 if (!(exists $config{$prefix.$nSpot}));
   my ($to_x, $to_y);
   $to_x = $config{$prefix.$nSpot."_x"};
   $to_y = $config{$prefix.$nSpot."_y"};
   if ($to_x eq "" || $to_y eq "") {
      error T("Invalid coordinates setting for planWalk (coordinates are unwalkable); planWalk disabled\n");
      $config{autoPlanWalk} = 0;
   } elsif ($field->isWalkable($to_x, $to_y)) {
      AI::clear(qw/move route mapRoute/);
      message TF("Do plan walking route to: %s: %s, %s\n", $field->descString(), $to_x, $to_y), "route";
      main::ai_route($field->baseName, $to_x, $to_y, attackOnRoute => 2, noSitAuto => 1);
   } else {
      error T("Invalid coordinates specified for planWalk (coordinates are unwalkable); planWalk disabled\n");
      $config{autoPlanWalk} = 0;
   }
}

1;


And, the setting for config.txt:

Code: Select all

autoPlanWalk 1

planWalk A {
   label myWalk_0
   x 365
   y 205
}

planWalk B {
   label myWalk_1
   x 360
   y 96
}

planWalk C {
   label myWalk_2
   x 332
   y 180
}

planWalk D {
   label myWalk_3
   x 365
   y 140
}

planWalk E {
   label myWalk_4
   x 332
   y 180
}

planWalk F {
   label myWalk_5
   x 365
   y 140
}

planWalk G {
   label myWalk_6
   x 360
   y 96
}


But, must add one line into the src/AI/CoreLogic.pm:
The line is:
Plugins::callHook('process_RandomWalk');
It is for hooking the "process_RandomWalk" event.

Code: Select all

##### RANDOM WALK #####
sub processRandomWalk {
   if (AI::isIdle && (AI::SlaveManager::isIdle()) && $config{route_randomWalk} && !$ai_v{sitAuto_forcedBySitCommand}
      && (!$field->isCity || $config{route_randomWalk_inTown})
      && length($field->{rawMap}) ) {
      my ($randX, $randY);
      my $i = 500;
      do {
         $randX = int(rand($field->width)) + 1;
         $randX = int($config{'lockMap_x'} - $config{'lockMap_randX'} + rand(2*$config{'lockMap_randX'}))+1 if ($config{'lockMap_x'} ne '' && $config{'lockMap_randX'} ne '');
         $randY = int(rand($field->height)) + 1;
         $randY = int($config{'lockMap_y'} - $config{'lockMap_randY'} + rand(2*$config{'lockMap_randY'}))+1 if ($config{'lockMap_y'} ne '' && $config{'lockMap_randY'} ne '');
      } while (--$i && !$field->isWalkable($randX, $randY));
      if (!$i) {
         error T("Invalid coordinates specified for randomWalk (coordinates are unwalkable); randomWalk disabled\n");
         $config{route_randomWalk} = 0;
      } else {
         message TF("Calculating random route to: %s: %s, %s\n", $field->descString(), $randX, $randY), "route";
         ai_route($field->baseName, $randX, $randY,
            maxRouteTime => $config{route_randomWalk_maxRouteTime},
            attackOnRoute => 2,
            noMapRoute => ($config{route_randomWalk} == 2 ? 1 : 0) );
      }
      Plugins::callHook('process_RandomWalk');  // the line for hooking the process_RandomWalk
   }
}

crowser
Noob
Noob
Posts: 8
Joined: 18 Feb 2009, 19:50
Noob?: No

Re: Plan to walk in lock map !

#2 Post by crowser »

is this the same as waypoint? Maybe i'll test it later.. However based on my exp when i use waypoints the bot doesnt go to storage to store loots.. i hope that this will be a solution..can i add additional coordinates though?

crowser
Noob
Noob
Posts: 8
Joined: 18 Feb 2009, 19:50
Noob?: No

Re: Plan to walk in lock map !

#3 Post by crowser »

works fine.. just had to re-configure config.txt to avoid the possibility of autostorage being disrupted by waypoint..

sofax222
Developers
Developers
Posts: 214
Joined: 24 Nov 2010, 03:08
Noob?: Yes

Re: Plan to walk in lock map !

#4 Post by sofax222 »

crowser wrote:works fine.. just had to re-configure config.txt to avoid the possibility of autostorage being disrupted by waypoint..


Actualy, I did this before I found out "WayPoint".
When I use the WayPoint, I think it is better than this.
But only one point, the WapPoint put the trigger on AI_Pre.
It means it always is triggered. And It is always in queue.

zCasanova
Noob
Noob
Posts: 19
Joined: 01 Jul 2009, 13:18
Noob?: No

Re: Plan to walk in lock map !

#5 Post by zCasanova »

crowser wrote:works fine.. just had to re-configure config.txt to avoid the possibility of autostorage being disrupted by waypoint..


What did you reconfigure?

crowser
Noob
Noob
Posts: 8
Joined: 18 Feb 2009, 19:50
Noob?: No

Re: Plan to walk in lock map !

#6 Post by crowser »

its easy.. i just had my bot respawn when it is about to go to storage.. since the waypoint is only activated on lock map the autostorage command will not be disrupted once he is out of the lockmap.. :D

sofax222
Developers
Developers
Posts: 214
Joined: 24 Nov 2010, 03:08
Noob?: Yes

Re: Plan to walk in lock map !

#7 Post by sofax222 »

There is a problem for running of WayPoint plugin.
That is, the "sit" console command will be blocked !
Because in running of WayPoint, there always is an ai task "waypoint" in the ai queue !

sofax222
Developers
Developers
Posts: 214
Joined: 24 Nov 2010, 03:08
Noob?: Yes

Re: Plan to walk in lock map !

#8 Post by sofax222 »

I did a major work (about continuous 10 hrs) last night for this thing "To plan walk".
I merge my planWalk.pl and the WayPoint and the macro.pl to make a new planLockMapWalk plugin.
As following:

Code: Select all

# planLockMapWalk by Snoopy Chen 2011/3/5

package planLockMapWalk;

use strict;

use Time::HiRes qw(time);

use Globals;
use Utils;
use Misc;
use AI;
use Log qw(debug message warning error);
use Translation;

my $nSpot = 0;
my $planMap = "";
my $cfID;
my $planwalk_file = "";
my @points = ();

Plugins::register('planLockMapWalk', 'Auto Plan to walk.', \&on_unload, \&on_reload);

my $hooks = Plugins::addHooks(
   ['configModify', \&on_configModify, undef],
   ['start3', \&on_start3, undef],
   ['postloadfiles', \&on_loadfiles, undef],
   ['pref_RandomWalk', \&pref_planWalk, undef],
   ['post_RandomWalk', \&post_planWalk, undef]
);

my $chooks = Commands::register(
   ['planwalk', "Plan Lock Map Walk plugin", \&commandHandler]
);

sub on_unload {
   Plugins::delHooks($hooks);
}

sub on_reload {
   message "planLockMapWalk plugin reloading\n";
   Plugins::delHooks($hooks);
   &on_start3
}

sub on_configModify {
   my (undef, $args) = @_;
   if ($args->{key} eq 'planWalk_file') {
      $planwalk_file = $args->{val};
      Settings::removeFile($cfID);
      $cfID = Settings::addControlFile($planwalk_file, loader => [ \&parsePlanWalk, undef]);
      Settings::loadByHandle($cfID);
   } elsif ($args->{key} eq 'lockMap') {
      if ($planMap ne $args->{val}) {
         $planMap = $args->{val};
         $nSpot = 0;
         Settings::loadByHandle($cfID);
      }
   }
}

sub on_start3 {
   &checkFile;
   Settings::removeFile($cfID) if ($cfID);
   $cfID = Settings::addControlFile($planwalk_file, loader => [ \&parsePlanWalk]);
   Settings::loadByHandle($cfID);
}

sub on_loadfiles {
   if ($char && ($planMap ne $config{lockMap} || $planwalk_file ne $config{planWalk_file})) {
      $planMap = $config{lockMap};
      $planwalk_file = (defined $config{planWalk_file})? $config{planWalk_file} : "planwalk.txt";
      Settings::removeFile($cfID) if ($cfID);
      $cfID = Settings::addControlFile($planwalk_file, loader => [ \&parsePlanWalk]);
      Settings::loadByHandle($cfID);
   }
}

# checks planwalk file
sub checkFile {
   $planMap = $config{lockMap};
   $planwalk_file = (defined $config{planWalk_file})? $config{planWalk_file} : "planwalk.txt";
}

# onFile(Re)load
sub parsePlanWalk {
   my $file = shift;

   my $flag1 = 0;
   @points = ();
   if (-e $file && $planMap) {
      open my $fp, "<:utf8", $file;
      while (<$fp>) {
         $. == 1 && s/^\x{FEFF}//; # utf bom
         s/(.*)[\s\t]+#.*$/$1/;   # remove last comments
         s/^\s*#.*$//;      # remove comments
         s/^\s*//;      # remove leading whitespaces
         s/\s*[\r\n]?$//g;   # remove trailing whitespaces and eol
         s/  +/ /g;      # trim down spaces - very cool for user's string data?
         next unless ($_);
         if (/^\[(.+)\]$/) {
            $flag1 = ($1 eq $planMap) ? 1 : 0;
         } elsif ($flag1) {
            if (/^(\d+):(\d+)$/) {
               push @points, {'xPos' => $1, 'yPos' => $2};
            }
         }
      }
   }
}

sub pref_planWalk {
   return if (!$config{autoPlanLockMapWalk} || $config{lockMap} ne $field->baseName);

   my (undef, $args) = @_;
   my $ret = 1;

   $nSpot = @points - 1 if ($nSpot >= @points);
   if ($nSpot >= 0) {
      my ($to_x, $to_y);
      $to_x = $points[$nSpot]->{xPos};
      $to_y = $points[$nSpot]->{yPos};

      if ($to_x eq "" || $to_y eq "") {
         error T("Empty coordinates setting for planLockMapWalk; planLockMapWalk disabled\n");
         $config{autoPlanLockMapWalk} = '0';
      } elsif ($field->isWalkable($to_x, $to_y)) {
         AI::clear(qw/move route mapRoute/);
         message TF("Do plan walking route to: %s: %s, %s\n", $field->descString(), $to_x, $to_y), "route";
         main::ai_route($field->baseName, $to_x, $to_y,
            maxRouteTime => $config{route_randomWalk_maxRouteTime},
            attackOnRoute => 2,
            noMapRoute => ($config{route_randomWalk} == 2 ? 1 : 0));
         $ret = 0;
      } else {
         error T("Invalid coordinates specified for planLockMapWalk (coordinates are unwalkable); planLockMapWalk disabled\n");
         $config{autoPlanLockMapWalk} = '0';
      }
      $nSpot++;
      $nSpot = 0 if ($nSpot >= @points);
   }
   $args->{return} = $ret;
}

sub post_planWalk {
}

sub commandHandler {
   message TF("The Plan Lock Map is : %s (Step: %d)\n", ($planMap) ? $planMap : "???", $nSpot - 1);
   message TF("The configuration file is : %s \n", ($planwalk_file) ? $planwalk_file : "???");
   for(my $i = 0 ; $i < @points ; $i++) {
      message TF("Walk Point(%d) : %s, %s\n", $i, $points[$i]->{xPos}, $points[$i]->{yPos});
   }
}

1;


But, it needs modifing the code "sub processRandomWalk" of src/AI/CoreLogic.pm.
I add two Plugins::callHook's : 'pref_RandomWalk' and 'post_RandomWalk'.
Acturly, I only use the 'pref_RandomWalk'. The new "sub processRandomWalk" is:

Code: Select all

##### RANDOM WALK #####
sub processRandomWalk {
   if (AI::isIdle && (AI::SlaveManager::isIdle()) && $config{route_randomWalk} && !$ai_v{sitAuto_forcedBySitCommand}
      && (!$field->isCity || $config{route_randomWalk_inTown})
      && length($field->{rawMap}) ) {

      my %hookArgs;
      $hookArgs{prefix} = "pref_RandomWalk";
      $hookArgs{return} = 1;
      Plugins::callHook('pref_RandomWalk', \%hookArgs);

      if ($hookArgs{return}) {
         my ($randX, $randY);
         my $i = 500;
         do {
            $randX = int(rand($field->width)) + 1;
            $randX = int($config{'lockMap_x'} - $config{'lockMap_randX'} + rand(2*$config{'lockMap_randX'}))+1 if ($config{'lockMap_x'} ne '' && $config{'lockMap_randX'} ne '');
            $randY = int(rand($field->height)) + 1;
            $randY = int($config{'lockMap_y'} - $config{'lockMap_randY'} + rand(2*$config{'lockMap_randY'}))+1 if ($config{'lockMap_y'} ne '' && $config{'lockMap_randY'} ne '');
         } while (--$i && !$field->isWalkable($randX, $randY));
         if (!$i) {
            error T("Invalid coordinates specified for randomWalk (coordinates are unwalkable); randomWalk disabled\n");
            $config{route_randomWalk} = 0;
         } else {
            message TF("Calculating random route to: %s: %s, %s\n", $field->descString(), $randX, $randY), "route";
            ai_route($field->baseName, $randX, $randY,
               maxRouteTime => $config{route_randomWalk_maxRouteTime},
               attackOnRoute => 2,
               noMapRoute => ($config{route_randomWalk} == 2 ? 1 : 0) );
         }
      }

      $hookArgs{prefix} = "post_RandomWalk";
      $hookArgs{return} = 1;
      Plugins::callHook('post_RandomWalk', \%hookArgs);
   }
}

zCasanova
Noob
Noob
Posts: 19
Joined: 01 Jul 2009, 13:18
Noob?: No

Re: Plan to walk in lock map !

#9 Post by zCasanova »

Kore won't run now, can't load the plugin.

sofax222
Developers
Developers
Posts: 214
Joined: 24 Nov 2010, 03:08
Noob?: Yes

Re: Plan to walk in lock map !

#10 Post by sofax222 »

zCasanova wrote:Kore won't run now, can't load the plugin.

?? This plugin is failed ?

Post Reply