Skip to content

Commit

Permalink
Give AI players a budget to spend in auctions
Browse files Browse the repository at this point in the history
  • Loading branch information
toberge committed Oct 14, 2024
1 parent 59b4d65 commit 5fa0e00
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions Assets/Scripts/Auction/BiddingAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ public class BiddingAI : BiddingPlayer
private int platformsEvaluated = 0;
private bool shouldEvaluate = true;

void Start()
private int budget;
private int availableChips;

private PlayerIdentity identity;

private void Start()
{
GetComponent<PlayerIK>().RightHandIKTarget = signTarget;
agent.updateRotation = false;
Expand All @@ -36,9 +41,12 @@ void Start()

public void SetIdentity(PlayerIdentity identity)
{
this.identity = identity;
chipText.text = "<sprite name=\"chip\">" + identity.Chips.ToString();
identity.onChipChange += UpdateChipStatus;
chipText.color = playerManager.identity.HasMaxChips ? Color.red : Color.black;
availableChips = identity.Chips;
budget = DetermineBudget(availableChips);
}

private IEnumerator WaitAndEvaluate()
Expand All @@ -58,7 +66,7 @@ private void EvaluatePlatformStates(BiddingPlatform platform)

var isNotActive = !platform.IsActive;
var isTooExpensive = platform.chips >= playerManager.identity.Chips;
if (isNotActive || isTooExpensive)
if (isNotActive || isTooExpensive || !IsWithinBudget(platform.chips))
priorities[platform] = -1;
}

Expand All @@ -72,7 +80,9 @@ private void ChooseDestination()
return;

// AI should yield
if (priorities[currentDestination] == -1)
var spentChips = availableChips - identity.Chips;
var isOverBudget = spentChips >= budget;
if (priorities[currentDestination] == -1 || isOverBudget)
{
agent.SetDestination(AuctionDriver.Singleton.YieldPosition);
return;
Expand Down Expand Up @@ -117,6 +127,39 @@ private void EvaluateItem(BiddingPlatform platform)
shouldEvaluate = false;
}

private int DetermineBudget(int availableChips)
{
// Irrelevant for non-chip gamemodes
var isFirstToXChips = MatchRules.Current.MatchWinCondition is { WinCondition: MatchWinConditionType.Chips, StopCondition: MatchStopConditionType.FirstToX };
if (!isFirstToXChips)
return availableChips;

var numBodiesBought = identity.Bodies.Count - 1;
var numBarrelsBought = identity.Barrels.Count - 1;
var numExtensionsBought = identity.Extensions.Count;
var numPartsOfCompleteSet = Mathf.Min(1, numBodiesBought) + Mathf.Min(1, numBarrelsBought) + Mathf.Min(1, numExtensionsBought);

var budgetFactor = 1f;
if (numPartsOfCompleteSet > 3)
budgetFactor = .15f;
else if (numPartsOfCompleteSet > 2)
budgetFactor = .2f;
else if (numPartsOfCompleteSet > 1)
budgetFactor = .75f;

var budget = Mathf.FloorToInt(budgetFactor * availableChips);
Debug.Log($"{identity.ToColorString()} has {numPartsOfCompleteSet} parts and will spend {budget}/{availableChips}");
return budget;

}

private bool IsWithinBudget(int currentBid)
{
var spentChips = availableChips - identity.Chips;
var spentChipsIfBought = currentBid + 1 + spentChips;
return spentChipsIfBought <= budget;
}

private void AnimateBid()
{
if (LeanTween.isTweening(signMesh.gameObject) || !currentPlatform)
Expand All @@ -132,6 +175,9 @@ private void OnBiddingPlatformChange(BiddingPlatform platform)
if (!platform || !currentDestination || platform != currentDestination || platform.LeadingBidder == playerManager.id || platform != playerManager.SelectedBiddingPlatform)
return;

if (!IsWithinBudget(platform.chips))
return;

AnimateBid();
currentDestination.PlaceBid(playerManager.identity);
currentDestination = null;
Expand Down

0 comments on commit 5fa0e00

Please sign in to comment.