using UnityEngine;
///
/// Holds agent stats and provides utility methods
/// Each stat is 1-100, total pool is (number of stats * 100) / 10 = 40 for 4 stats
///
public class AgentStats
{
public int Strength { get; private set; }
public int Speed { get; private set; }
public int Magic { get; private set; }
public int Dexterity { get; private set; }
private const int STAT_TOTAL_POOL = 40; // (4 stats * 100) / 10
private const int NUM_STATS = 4;
private const int MAX_STAT_VALUE = 100;
private const int MIN_STAT_VALUE = 1;
public AgentStats()
{
GenerateRandomStats();
}
///
/// Generates random stats by creating 4 random values and shuffling them
/// This ensures all stats get a fair distribution
///
private void GenerateRandomStats()
{
int[] stats = new int[NUM_STATS];
// Generate 4 random values between 1 and 100
for (int i = 0; i < NUM_STATS; i++)
{
stats[i] = Random.Range(MIN_STAT_VALUE, MAX_STAT_VALUE + 1);
}
// Shuffle the stats array to randomly assign values
for (int i = NUM_STATS - 1; i > 0; i--)
{
int randomIndex = Random.Range(0, i + 1);
// Swap
int temp = stats[i];
stats[i] = stats[randomIndex];
stats[randomIndex] = temp;
}
Strength = stats[0];
Speed = stats[1];
Magic = stats[2];
Dexterity = stats[3];
// Normalize to fit within pool
NormalizeStats();
}
///
/// Normalizes stats to roughly fit within the STAT_TOTAL_POOL
///
private void NormalizeStats()
{
int total = Strength + Speed + Magic + Dexterity;
if (total > STAT_TOTAL_POOL)
{
float scale = (float)STAT_TOTAL_POOL / total;
Strength = Mathf.Max(MIN_STAT_VALUE, (int)(Strength * scale));
Speed = Mathf.Max(MIN_STAT_VALUE, (int)(Speed * scale));
Magic = Mathf.Max(MIN_STAT_VALUE, (int)(Magic * scale));
Dexterity = Mathf.Max(MIN_STAT_VALUE, (int)(Dexterity * scale));
}
}
public int GetTotalStats()
{
return Strength + Speed + Magic + Dexterity;
}
public override string ToString()
{
return $"STR: {Strength} | SPD: {Speed} | MAG: {Magic} | DEX: {Dexterity} | Total: {GetTotalStats()}";
}
}