Skip to content

Commit

Permalink
Prevent bidding AIs from spamming bids
Browse files Browse the repository at this point in the history
  • Loading branch information
toberge committed Jun 8, 2024
1 parent 16a2e89 commit ea70c08
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
38 changes: 26 additions & 12 deletions Assets/Scripts/Auction/BiddingAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class BiddingAI : BiddingPlayer
[SerializeField]
private Vector3 platformDestinationOffset = Vector3.back * 2;

private int platformsEvaluated = 0;
private bool shouldEvaluate = true;

void Start()
Expand All @@ -40,28 +41,41 @@ public void SetIdentity(PlayerIdentity identity)
private IEnumerator WaitAndEvaluate()
{
foreach (BiddingPlatform platform in AuctionDriver.Singleton.BiddingPlatforms)
if (platform.IsActive)
EvaluatePlatformStates(platform);
EvaluatePlatformStates(platform);
ChooseDestination();
yield return new WaitForSeconds(2);
StartCoroutine(WaitAndEvaluate());
}

private void EvaluatePlatformStates(BiddingPlatform platform)
{
if (platform.LeadingBidder == playerManager.id)
var isAlreadyInTheLead = platform.IsActive && platform.LeadingBidder == playerManager.id;
if (isAlreadyInTheLead)
return;

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

private void ChooseDestination()
{
currentDestination = priorities.ToList()
.OrderByDescending(x => x.Value)
.Select(x => x.Key).First();

if (currentDestination)
agent.SetDestination(currentDestination.transform.position + platformDestinationOffset);
if (!currentDestination)
return;

var isAlreadyInTheLead = currentDestination.LeadingBidder == playerManager.id;
if (isAlreadyInTheLead)
return;

agent.SetDestination(currentDestination.transform.position + platformDestinationOffset);

if (currentDestination == playerManager.SelectedBiddingPlatform)
var isAlreadyAtThisPlatform = currentDestination == playerManager.SelectedBiddingPlatform;
if (isAlreadyAtThisPlatform)
OnBiddingPlatformChange(currentDestination);
}

Expand All @@ -83,11 +97,11 @@ private void EvaluateItem(BiddingPlatform platform)
.Sum((augment) => augment.KillCount);
break;
}
priorities.Add(platform, priority);
// Move towards a position that does not obscure the cost text (!)
agent.SetDestination(platform.transform.position + platformDestinationOffset);
// (re)set the priority (reassignment in case we have multiple bidding rounds)
priorities[platform] = priority;

if (!shouldEvaluate)
platformsEvaluated++;
if (platformsEvaluated < 3 || !shouldEvaluate)
return;
StartCoroutine(WaitAndEvaluate());
shouldEvaluate = false;
Expand All @@ -105,7 +119,7 @@ private void AnimateBid()

private void OnBiddingPlatformChange(BiddingPlatform platform)
{
if (!platform || !currentDestination || platform != currentDestination || platform.LeadingBidder == playerManager.id)
if (!platform || !currentDestination || platform != currentDestination || platform.LeadingBidder == playerManager.id || platform != playerManager.SelectedBiddingPlatform)
return;

AnimateBid();
Expand Down
1 change: 0 additions & 1 deletion Assets/Scripts/Auction/BiddingPlatform.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Mirror;
using Org.BouncyCastle.Tls;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
Expand Down

0 comments on commit ea70c08

Please sign in to comment.