Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ProcessAutoBuy] bulkBuying #2948

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions control/sys.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ skipPlugins_list

###### Miscellaneous ######
sendAnonymousStatisticReport 0
maxItemBuyIfNotDefined 1
89 changes: 40 additions & 49 deletions src/AI/CoreLogic.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 "") {
Expand All @@ -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'}) {
Expand Down Expand Up @@ -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"} != 0) ? 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
Mortimal marked this conversation as resolved.
Show resolved Hide resolved
$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);
}
}

Expand Down
26 changes: 26 additions & 0 deletions src/Misc.pm
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ our @EXPORT = (
# Npc buy and sell
qw/cancelNpcBuySell
completeNpcSell
checkItemBuyNeed
completeNpcBuy/,

# Char login
Expand Down Expand Up @@ -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
Mortimal marked this conversation as resolved.
Show resolved Hide resolved

# item needded if:
return 1 if (($config{"buyAuto_$i"."_minAmount"} ne ""
&& $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"} # the price is set...
Mortimal marked this conversation as resolved.
Show resolved Hide resolved
&& $zeny > $config{"buyAuto_$i"."_price"} # ...and we can buy at least one of that item
)
);
return 0;
}

sub completeNpcBuy {
my $items = shift;

Expand Down