Openkore.com

OpenKore Forums
It is currently 22 Sep 2018, 21:31

All times are UTC - 5 hours [ DST ]





Post new topic Reply to topic  [ 34 posts ]  Go to page 1, 2, 3, 4  Next
Author Message
 Post subject: Plan to walk in lock map !
PostPosted: 27 Jan 2011, 05:38 
Offline
Developers
Developers

Joined: 24 Nov 2010, 03:08
Posts: 214
I write a plugin for planning to walk in lock map.
The code is following:
Code:
# 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:
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:
##### 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
   }
}


Top
 Profile  
 
 Post subject: Re: Plan to walk in lock map !
PostPosted: 05 Feb 2011, 00:37 
Offline
Noob
Noob

Joined: 18 Feb 2009, 19:50
Posts: 8
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?


Top
 Profile  
 
 Post subject: Re: Plan to walk in lock map !
PostPosted: 06 Feb 2011, 09:10 
Offline
Noob
Noob

Joined: 18 Feb 2009, 19:50
Posts: 8
works fine.. just had to re-configure config.txt to avoid the possibility of autostorage being disrupted by waypoint..


Top
 Profile  
 
 Post subject: Re: Plan to walk in lock map !
PostPosted: 07 Feb 2011, 19:58 
Offline
Developers
Developers

Joined: 24 Nov 2010, 03:08
Posts: 214
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.


Top
 Profile  
 
 Post subject: Re: Plan to walk in lock map !
PostPosted: 22 Feb 2011, 09:37 
Offline
Noob
Noob

Joined: 01 Jul 2009, 13:18
Posts: 19
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?


Top
 Profile  
 
 Post subject: Re: Plan to walk in lock map !
PostPosted: 28 Feb 2011, 20:42 
Offline
Noob
Noob

Joined: 18 Feb 2009, 19:50
Posts: 8
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


Top
 Profile  
 
 Post subject: Re: Plan to walk in lock map !
PostPosted: 04 Mar 2011, 06:11 
Offline
Developers
Developers

Joined: 24 Nov 2010, 03:08
Posts: 214
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 !


Top
 Profile  
 
 Post subject: Re: Plan to walk in lock map !
PostPosted: 05 Mar 2011, 23:44 
Offline
Developers
Developers

Joined: 24 Nov 2010, 03:08
Posts: 214
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:
# 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:
##### 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);
   }
}


Top
 Profile  
 
 Post subject: Re: Plan to walk in lock map !
PostPosted: 07 Mar 2011, 04:51 
Offline
Noob
Noob

Joined: 01 Jul 2009, 13:18
Posts: 19
Kore won't run now, can't load the plugin.


Top
 Profile  
 
 Post subject: Re: Plan to walk in lock map !
PostPosted: 07 Mar 2011, 12:53 
Offline
Developers
Developers

Joined: 24 Nov 2010, 03:08
Posts: 214
zCasanova wrote:
Kore won't run now, can't load the plugin.

?? This plugin is failed ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 34 posts ]  Go to page 1, 2, 3, 4  Next

All times are UTC - 5 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group