diff --git a/control/sys.txt b/control/sys.txt index c9e3ee91de..6a7b3419c0 100644 --- a/control/sys.txt +++ b/control/sys.txt @@ -58,3 +58,4 @@ skipPlugins_list ###### Miscellaneous ###### sendAnonymousStatisticReport 0 +maxItemBuyIfNotDefined 1 \ No newline at end of file diff --git a/src/AI/CoreLogic.pm b/src/AI/CoreLogic.pm index 4c968d2512..e452e8da41 100644 --- a/src/AI/CoreLogic.pm +++ b/src/AI/CoreLogic.pm @@ -1801,26 +1801,14 @@ sub processAutoSell { #####AUTO BUY##### sub processAutoBuy { +#Here we check if there any need of initiating autobuy sequence my $needitem; if ((AI::action eq "" || AI::action eq "route" || AI::action eq "follow") && timeOut($timeout{'ai_buyAuto'}) && $char->inventory->isReady()) { undef $ai_v{'temp'}{'found'}; for(my $i = 0; exists $config{"buyAuto_$i"}; $i++) { - next if (!$config{"buyAuto_$i"} || !$config{"buyAuto_$i"."_npc"} || $config{"buyAuto_${i}_disabled"}); - my $amount; - if ($config{"buyAuto_$i"} =~ /^\d{3,}$/) { - $amount = $char->inventory->sumByNameID($config{"buyAuto_$i"}); - } - else { - $amount = $char->inventory->sumByName($config{"buyAuto_$i"}); - } - if ( - $config{"buyAuto_$i"."_minAmount"} ne "" && - $config{"buyAuto_$i"."_maxAmount"} ne "" && - (checkSelfCondition("buyAuto_$i")) && - $amount <= $config{"buyAuto_$i"."_minAmount"} && - $amount < $config{"buyAuto_$i"."_maxAmount"} - ) { + #Begining to break logic here. Replacing checks for new Misc.pm function - checkItemBuyNeed. + if (checkItemBuyNeed($i,$char->{zeny})) { $ai_v{'temp'}{'found'} = 1; my $bai = $config{"buyAuto_$i"}; if ($needitem eq "") { @@ -1839,7 +1827,7 @@ sub processAutoBuy { } $timeout{'ai_buyAuto'}{'time'} = time; } - +#Here we check if we finished autobuy sequence if (AI::action eq "buyAuto" && AI::args->{'done'}) { if (exists AI::args->{'error'}) { @@ -2033,53 +2021,56 @@ sub processAutoBuy { } else { return unless (timeOut($args->{'recv_buyList_time'}, $timeout{ai_buyAuto_wait_before_buy}{timeout})); } - + #We did all we need here + return if !$args->{lastIndex}; + #Creating bulkBuyList my @buyList; - my $item; - if ($config{"buyAuto_".$args->{lastIndex}} =~ /^\d{3,}$/) { - $item = $storeList->getByNameID( $config{"buyAuto_".$args->{lastIndex}} ); - $args->{'nameID'} = $config{"buyAuto_".$args->{lastIndex}} if (defined $item); - } - else { - $item = $storeList->getByName( $config{"buyAuto_".$args->{lastIndex}} ); - $args->{'nameID'} = $item->{nameID} if (defined $item); - } - - if (!exists $args->{'nameID'}) { - $args->{index_failed}{$args->{lastIndex}} = 1; - error "buyAuto index ".$args->{lastIndex}." (".$config{"buyAuto_".$args->{lastIndex}}.") failed, item doesn't exist in npc sell list.\n", "npc"; - - } else { - my $maxbuy = ($config{"buyAuto_".$args->{lastIndex}."_price"}) ? int($char->{zeny}/$config{"buyAuto_$args->{index}"."_price"}) : 30000; # we assume we can buy 30000, when price of the item is set to 0 or undef - my $needbuy = $config{"buyAuto_".$args->{lastIndex}."_maxAmount"}; + my $zenyleft = $char->{zeny}; + #Filter all items in buyList to determine those sold by current npc + for(my $i = 0; exists $config{"buyAuto_$i"}; $i++){ + next if (($config{"buyAuto_".$args->{lastIndex}."_npc"} ne $config{"buyAuto_".$i."_npc"}) + || (($i != $args->{lastIndex}) && !checkItemBuyNeed($i,$zenyleft))); + $item = $storeList->getByName( $config{"buyAuto_".$i} ); + if (defined $item){ + $args->{'nameID'} = $item->{nameID}; + } + if (!exists $args->{'nameID'}){ + $args->{index_failed}{$i} = 1; + error "buyAuto index ".$i." (".$config{"buyAuto_".$i}.") failed, item doesn't exist in npc sell list.\n", "npc"; + }else{ + my $maxbuy = ($config{"buyAuto_".$i."_price"} ne "") ? int($zenyleft/$config{"buyAuto_".$i."_price"}) : $::sys{'maxItemBuyIfNotDefined'}; # we assume we can buy 1 as default from sys.txt, when price of the item is set to 0 + my $inv_amount = $char->inventory->sumByNameID($args->{'nameID'}); - $needbuy -= $inv_amount; + my $needbuy = $config{"buyAuto_".$i."_maxAmount"} - $inv_amount; my $buy_amount = ($maxbuy > $needbuy) ? $needbuy : $maxbuy; my $batchSize = $config{"buyAuto_".$args->{lastIndex}."_batchSize"}; - - if ($batchSize && $batchSize < $buy_amount) { - - while ($buy_amount > 0) { - my $amount = ($buy_amount > $batchSize) ? $batchSize : $buy_amount; + #Batch size is important + if ($batchSize && $batchSize < $buy_amount){ + while ($buy_amount > 0){ + my $amount = ($buy_amount > $batchSize) ? $batchSize : $buy_amount; + my %buy = ( + itemID => $args->{'nameID'}, + amount => $amount + ); + push(@buyList, \%buy); #push batched + $buy_amount -= $amount; + #We must understand if there is enough zeny to do all bulkBuing + $zenyleft -= $amount * $config{"buyAuto_".$i."_price"}; + } + }else{ my %buy = ( itemID => $args->{'nameID'}, - amount => $amount + amount => $buy_amount #Replace to adapt ); - push(@buyList, \%buy); - $buy_amount -= $amount; + push(@buyList, \%buy); # push not batched + $zenyleft -= $buy_amount * $config{"buyAuto_".$i."_price"}; } - } else { - my %buy = ( - itemID => $args->{'nameID'}, - amount => $buy_amount - ); - push(@buyList, \%buy); } } diff --git a/src/Misc.pm b/src/Misc.pm index eae442e646..0993aa86c6 100644 --- a/src/Misc.pm +++ b/src/Misc.pm @@ -217,6 +217,7 @@ our @EXPORT = ( # Npc buy and sell qw/cancelNpcBuySell completeNpcSell + checkItemBuyNeed completeNpcBuy/, # Char login @@ -5148,6 +5149,31 @@ sub completeNpcSell { } } +sub checkItemBuyNeed{ + my $i = shift; + my $zeny = shift; + #skip block if: + return 0 if (!$config{"buyAuto_$i"} # this block is non existant + || !$config{"buyAuto_$i"."_npc"} # npc is not set + || $config{"buyAuto_${i}_disabled"} # block is not disabled + ); + + my $amount = $char->inventory->sumByName($config{"buyAuto_$i"});# getting items amount + + # item needded if: + return 1 if (($config{"buyAuto_$i"."_minAmount"} eq "" + || $amount <= $config{"buyAuto_$i"."_minAmount"}) # minAmount is set and we got less than minAmount + && $config{"buyAuto_$i"."_maxAmount"} ne "" # maxamount is set + && (checkSelfCondition("buyAuto_$i")) # selfConditions are met + + && $amount < $config{"buyAuto_$i"."_maxAmount"} # we got less than maxAmount + && ($config{"buyAuto_$i"."_price"} eq "" # the price is set... + || $zeny > $config{"buyAuto_$i"."_price"} # ...and we can buy at least one of that item + ) + ); + return 0; +} + sub completeNpcBuy { my $items = shift;