Code: Select all
# refineAuto.pl
# 
# Automated Mass Refining
# version 0.0.1
# Openkore 2.0.0
package refineAuto;
use strict;
use Plugins;
use Globals;
use AI;
use Log;
use Actor::Item;
use Network::Send;
use Misc;
# Config keys used:
# refineAuto			enable/disable auto refining
# refineAuto_npc			refiner npc
# refineAuto_distance		distence to stand from npc
# refineAuto_maxRefine		maximum refining rate
# refineAuto_npc_steps		npc talking sequence when refining
# refineAuto_equip		equipment name that will be refined, without +
# refineAuto_useMagnifier	enable item identification with magnifier
# refineAuto_material		materials needed to refine
# refineAuto_minZenny		minimum amount of zenny needed to refine
# refineAuto_getAutoAdjust	automatically adjusts getAuto
# refineAuto_incremental	when incremental refining should start
# refineAuto_dcWhenDone		disconnect if resources are depleted
Plugins::register('autorefine', 'automatic refining', \&unload_callback);
my $plugin_hook = Plugins::addHook("AI_pre", \&autorefine);
sub autorefine {
	# Initiate autorefine, AI must be idle
	if ($config{'refineAuto'} && AI::isIdle) {
		AI::queue('refineAuto');
		Log::message "refineAuto initiated\n";
	} elsif (AI::is('refineAuto')) {
		my $args = AI::args;
		if (!$args->{npc}) {
			$args->{npc} = {};
			main::getNPCInfo($config{refineAuto_npc}, 
					 $args->{npc});
		}
		# Move to NPC if needed
		elsif (($field{name} ne $args->{npc}{map})
			|| ((Utils::distance($args->{npc}{pos}, $char->{pos_to})
			    > $config{refineAuto_distance}))) {
			    
			Log::message "Calculating refineAuto route to: " .
				     "$maps_lut{$args->{npc}{map}.'.rsw'}" .
				     "($args->{npc}{map}): " .
				     "$args->{npc}{pos}{x}, " .
				     "$args->{npc}{pos}{y}\n", "route";
			main::ai_route($args->{npc}{map}, $args->{npc}{pos}{x},
				       $args->{npc}{pos}{y},
				       distFromGoal => 
				       		$config{refineAuto_distance});
		} else {
		# We arrived at refiner
			
			# Check arguments for incremental refining
			# If refineAuto_incremental is invalid or not specified
			# Set refineAuto_incremental to maxRefine
			if (!($config{refineAuto_incremental} > 0
			      && $config{refineAuto_incremental}
			         < $config{refineAuto_maxRefine})) {
				 
				$config{refineAuto_incremental} = 
					$config{refineAuto_maxRefine};
			}
			
			# Equipped and ready to refine
			if ($args->{equip}) {
				refine_item($args);
			}
			
			# Check if we have all the requirements
			elsif (check_requirements($args)) {
				equip_item($args);
			}
		}
	}
}
sub refine_item {
	my $args = shift;
	Log::message "Trying to refine $args->{equip}{name}\n";
	if ($args->{refine} < $config{refineAuto_incremental}) {
		main::ai_talkNPC($args->{npc}{pos}{x}, $args->{npc}{pos}{y},
				 $config{refineAuto_npc_steps});
		Log::message "Refining $args->{equip}{name}\n";
		$args->{equip} = undef;
		$args->{refine} = undef;
	}
}
sub equip_item {
	my $args = shift;
	my $i;
	my $item;
	my $type;
	my $refine;
	$args->{equip} = undef;
	$args->{refine} = undef;
	for ($i = 0; $i < @{$char->{inventory}}; $i++) {
		$item = $char->{inventory}[$i];
		next unless $item && %{$item};
		$type = $item->{type};
		# Check if it is an equipment, refer to tables/itemtypes.txt
		if ($type == 4 || $type == 5 || $type == 11 || $type == 8
		 || $type == 9) {
		 	# Check if it is the right equipment
			if ($item->{name} =~ 
			    m/(\Q$config{refineAuto_equip}\E)/) {
				
				Log::message "Trying to equip $1\n";
				# Check its current refine rate
				$refine = $item->{name} =~ m/^\+(\d)/ ? $1 : 0;
				if ($refine < $config{refineAuto_incremental}) {
					identify_item($item);
					
					# If this doesn't work, just sendEquip
					$item->equip;
					$args->{equip} = $item;
					$args->{refine} = $refine;
					
					Log::message "Item equipped\n";
					return;
				}
			}
		}
	}
}
# Assumes proper configuration
sub identify_item {
	my $item = shift;
	
	# Check if the item is already Identified
	if ($item->{identified}) {
		return;
	}
}
sub check_requirements {
	my $args = shift;
	
	$args->{retry} = 0 unless defined $args->{retry};
	$args->{getPlus} = 0 unless defined $args->{getPlus};
	
	# Item shortage after 3 retries
	if ($args->{retry} > 3) {
		if ($config{refineAuto_incremental} > 0
		    && $config{refineAuto_incremental}
		       < $config{refineAuto_maxRefine}) {
		       $args->{retry} = 0;
		       $config{refineAuto_incremental} += 1;
		} else {
			done_refine();
		}
	}
	
	# Automatic getAuto adjustment, only done when storage auto failed
	if ($config{refineAuto_getAutoAdjust} == 1 && $args->{retry} > 1
	    && $args->{getPlus} < $config{refineAuto_incremental}) {
		# Look for getAuto index to adjust
		my $i = 0;
		my $done = 0;
		while ($config{"getAuto_$i"} ne "") {
			if ($config{"getAuto_$i"} =~ 
				m/(\Q$config{refineAuto_equip}\E)/) {
				
				my $prefix = ($args->{getPlus} == 0) ? 
					     "" : "+$args->{getPlus} ";
					
				$config{"getAuto_$i"} = $prefix .
						$config{refineAuto_equip};
				Log::message 'getAuto adjusted\n';
				$args->{getPlus} += 1;
				$args->{retry} = 0;
				$done = 1;
			}
			last if $done;
			$i++;
		}
	}
	
	# Check for config
	if ($config{refineAuto_equip}
	    && $config{refineAuto_material}
	    && $config{refineAuto_minZenny}) {
	    	my $material = Actor::Item::get($config{refineAuto_material}, 0, 1);
		my $equip = undef;
		my $want_magnifier = undef;
		my $plus;
		
		# Check for equipment, starting from +0 to max
		for ($plus = 0; 
		     $plus < $config{refineAuto_incremental}; $plus++) {
			my $prefix = ($plus == 0) ? "" : "+$plus ";
			$equip = Actor::Item::get($prefix .
					   $config{refineAuto_equip}, 0, 0);
			last if defined $equip;
		}
		
		if ($config{refineAuto_useMagnifier}) {
			if (!defined Actor::Item::get("Magnifier", 0, 1)) {
				$want_magnifier = 1;
			}
		} elsif ($char->{skills}{MC_IDENTIFY}{lv} != 1) {
			Log::message "No Identify skill, magnifier is needed";
			$config{refineAuto_useMagnifier} = 1;
			return 0;
		}
		
		if (defined $material
		    && defined $equip
		    && $char->{zenny} >= $config{refineAuto_minZenny}
		    && !$want_magnifier) {
		    	$args->{retry} = 0;
		    	return 1;
		} elsif ($char->{zenny} < $config{refineAuto_minZenny}) {
			done_refine();
		} elsif ($args->{retry} > 0 && !defined $material) {
			done_refine();
		} else {
			if ($config{refineAuto_visitStorage} != 0) {
				AI::queue("storageAuto");
			}
			$args->{retry} += 1;
		}
	} else {
		Log::message "Incomplete configuration for refineAuto\n";
	}
	return 0;   
}
sub done_refine {
	if ($config{refineAuto_dcWhenDone}) {
		Misc::quit();
	} else {
		AI::dequeue();
	}
}
sub unload_callback {
}
return 1;refineAuto 1
refineAuto_npc alberta_in 28 58
refineAuto_distance 5
refineAuto_maxRefine 7
refineAuto_npc_steps r5 r0 r0
refineAuto_equip Boots [1]
refineAuto_material Elunium
refineAuto_minZenny 2000
refineAuto_incremental 4
refineAuto_getAutoAdjust 0
refineAuto_dcWhenDone 0
refineAuto_visitStorage 0









