#!/usr/bin/perl
# Filename:	Dopey
# Author:	David Ljung Madison <DaveSource.com>
# See License:	http://MarginalHacks.com/License
# Description:	AI module for dAIve interface to dopewars
use strict;

##################################################
# Setup the variables
##################################################

# This is a guess - the server doesn't tell us that it's our last day  :(
my $NUM_TURNS	= 30;

##################################################
# Make decisions
##################################################

# Choose our name
# This needs to return a different name each time it's
# called, in case someone is already using the name we try
sub ChooseName {
  my ($game) = @_;

  return ++$game->{'Name'} if ($game->{'Name'});
  
  $game->{'Name'} = "Dopey";
}

sub DealDrugs {
  my ($game) = @_;

  my $turns_left = $NUM_TURNS - turn_num($game);

  # Since there is no cost to buying back a drug we just sold, we
  # can simplify the logic for deciding which drugs to sell and then
  # buy by selling all drugs, and then trying to buy drugs sorted according
  # to which are the best buy.  If we sell a drug we shouldn't have, then
  # arguably we should buy it back, otherwise our sort algorithm is broken.

  # See what's available, sell all drugs, choose some to buy
  my @wanted;
  for (my $drug=0; $drug<$game->{'NumDrugs'}; $drug++) {
    # Not available here
    next unless (drug_price($game,$drug));

    # We want drugs that are cheap by more than some percent
    my $percent = 10;
    $percent = 20 if ($turns_left == 2);	# Careful!
    $percent = 40 if ($turns_left == 1);	# Careful!
    my $profit = drug_ave($game,$drug) - drug_price($game,$drug);
    push(@wanted,$drug) if ($profit > drug_price($game,$drug)*$percent/100);

    # Sell everything, wanted or not (you can't always get what you want :)
    SellDrug($game,$drug,inv_drug($game,$drug)) if (inv_drug($game,$drug));
  }

  if (@wanted && $turns_left) {

    # Sort algorithm for which drugs to buy
    sub best_buy {
      my $a_profit = drug_ave($game,$a) - drug_price($game,$a);
      my $b_profit = drug_ave($game,$a) - drug_price($game,$a);
      $b_profit <=> $a_profit;
    }

    # Buy the more expensive drugs first to conserve space/$
    foreach my $drug ( sort best_buy @wanted ) {
      BuyDrug($game,$drug,undef);	# Buy all we can
    }
  }

  # Did we miss out on selling some drugs?
  if (!$turns_left) {
    for (my $drug=0; $drug<$game->{'NumDrugs'}; $drug++) {
      print "Damn:  Didn't sell ",inv_drug($game,$drug),
            " of ",drug_name($game,$drug)," (losing approx \$",
            (inv_drug($game,$drug)*drug_ave($game,$drug)),")\n"
        if (!drug_price($game,$drug) && inv_drug($game,$drug));
    }
  }
}

sub JetTo {
  my ($game) = @_;

  my $loc = location($game);

  # Time to see the loan shark?
  my $debt = inv_debt($game);
  my $shark = loan_shark($game);
  return $shark
    if (defined($shark) && $loc != $shark &&
        $debt && inv_cash($game)*1.2 > $debt);

  ($loc+1) % $#{$game->{'Locations'}};
}

# Amount to pay the loan shark
sub Pay_LoanShark {
  my ($game) = @_;

  my $debt = inv_debt($game);

  # As long as we have enough left over, pay the loan shark!
  return $debt if (inv_cash($game)*1.1 > $debt);
  return 0;
}

