Applied probability logic for pawns.

This commit is contained in:
Ashby Issac 2026-02-11 19:53:16 +05:30
parent 97cd17c0cf
commit da4b44957b
8 changed files with 164 additions and 45 deletions

View File

@ -1,6 +1,8 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using Random = UnityEngine.Random;
public class DiceView : MonoBehaviour, IBase public class DiceView : MonoBehaviour, IBase
{ {
@ -16,10 +18,14 @@ public class DiceView : MonoBehaviour, IBase
[SerializeField] private float sideForce = 0.18f; [SerializeField] private float sideForce = 0.18f;
[SerializeField] private float liftForce = 0.1f; [SerializeField] private float liftForce = 0.1f;
private List<int> probabilityValues = new List<int>() { 3, 4, 5 };
private Quaternion startRotation; private Quaternion startRotation;
private Action<int> onRollingComplete = null; private Action<int> onRollingComplete = null;
private IRollBase rollBase = null;
private bool hasNoActionOnRoll = false;
void Awake() private void Awake()
{ {
rb = GetComponent<Rigidbody>(); rb = GetComponent<Rigidbody>();
rb.useGravity = false; rb.useGravity = false;
@ -39,31 +45,31 @@ public class DiceView : MonoBehaviour, IBase
} }
} }
IEnumerator RollRoutine() private IEnumerator RollRoutine()
{ {
rolling = true; rolling = true;
// MICRO DELAY → breaks physics sync between dice // MICRO DELAY → breaks physics sync between dice
yield return new WaitForSeconds(UnityEngine.Random.Range(0.01f, 0.06f)); yield return new WaitForSeconds(Random.Range(0.01f, 0.06f));
rb.useGravity = true; rb.useGravity = true;
// PER-DICE FORCE MULTIPLIER // PER-DICE FORCE MULTIPLIER
float spinMultiplier = UnityEngine.Random.Range(0.8f, 1.25f); float spinMultiplier = Random.Range(0.8f, 1.25f);
// RANDOM TORQUE // RANDOM TORQUE
rb.AddTorque( rb.AddTorque(
UnityEngine.Random.Range(-baseSpinForce, baseSpinForce) * spinMultiplier, Random.Range(-baseSpinForce, baseSpinForce) * spinMultiplier,
UnityEngine.Random.Range(-baseSpinForce, baseSpinForce) * spinMultiplier, Random.Range(-baseSpinForce, baseSpinForce) * spinMultiplier,
UnityEngine.Random.Range(-baseSpinForce, baseSpinForce) * spinMultiplier, Random.Range(-baseSpinForce, baseSpinForce) * spinMultiplier,
ForceMode.Impulse ForceMode.Impulse
); );
// RANDOM SIDE FORCE // RANDOM SIDE FORCE
Vector3 sideDir = new Vector3( Vector3 sideDir = new Vector3(
UnityEngine.Random.Range(-1f, 1f), Random.Range(-1f, 1f),
0f, 0f,
UnityEngine.Random.Range(-1f, 1f) Random.Range(-1f, 1f)
).normalized; ).normalized;
rb.AddForce(sideDir * sideForce, ForceMode.Impulse); rb.AddForce(sideDir * sideForce, ForceMode.Impulse);
@ -73,28 +79,44 @@ public class DiceView : MonoBehaviour, IBase
yield return new WaitForSeconds(sideValueTime); yield return new WaitForSeconds(sideValueTime);
int value = GetDiceValue(); int rolledVal = 0;
if (hasNoActionOnRoll)
//TODO: Use the dice value as needed
Debug.Log($"Dice rolled: {value}");
ResetDice();
onRollingComplete?.Invoke(value);
}
int GetDiceValue()
{
foreach (DiceSide side in diceSides)
{ {
if (side.OnGround()) if (rollBase.SixRollCount == 0)
return side.sideValue; {
rollBase.UpdateMaxRollCount(probabilityValues[Random.Range(0, probabilityValues.Count)]);
}
rollBase.UpdateSixRollCount();
rolledVal = rollBase.SixRollCount == rollBase.MaxRollCount ?
Ludo_3D_Constants.Max_Dice_Rolls : GetDiceValue();
}
else
{
rolledVal = GetDiceValue();
} }
return 1; if (rolledVal == Ludo_3D_Constants.Max_Dice_Rolls)
ResetRollData();
ResetDice();
Debug.Log($"Dice rolled: {rolledVal}");
onRollingComplete?.Invoke(rolledVal);
}
private int GetDiceValue()
{
return Random.Range(1, Ludo_3D_Constants.Max_Dice_Rolls + 1);
}
public void ResetRollData()
{
if (rollBase != null) rollBase.ResetRollData();
} }
public void ResetDice() public void ResetDice()
{ {
rollBase = null;
rb.useGravity = false; rb.useGravity = false;
rb.velocity = Vector3.zero; rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero; rb.angularVelocity = Vector3.zero;
@ -108,4 +130,13 @@ public class DiceView : MonoBehaviour, IBase
transform.localRotation = startRotation; transform.localRotation = startRotation;
onRollingComplete = null; onRollingComplete = null;
} }
public void InitOnNoActionsAfterRoll(IRollBase rollBase, bool state)
{
hasNoActionOnRoll = state;
if (!state)
return;
this.rollBase = rollBase;
}
} }

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 05273ddf992ca4671b13f0b4b9dde85d
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,11 @@
public interface IRollBase
{
public int SixRollCount { get; }
public int MaxRollCount { get; }
void UpdateSixRollCount();
void UpdateMaxRollCount(int val);
void ResetRollData();
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4683cbbafe11541cf8a71cdd12df62ac
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -169,7 +169,7 @@ public class GameplayManager : MonoBehaviour, IBase, IBootLoader, IDataLoader
private void HandleDiceRollWithDelay() private void HandleDiceRollWithDelay()
{ {
Invoke(nameof(HandleDiceRoll), botDiceRollDelay); Invoke(nameof(RollDiceForBot), botDiceRollDelay);
} }
private void InitBotRuntimeData() private void InitBotRuntimeData()
@ -267,7 +267,7 @@ public class GameplayManager : MonoBehaviour, IBase, IBootLoader, IDataLoader
currentPlayerTurnTimer.Init(currentPlayerTurnMaxTime, onComplete: () => currentPlayerTurnTimer.Init(currentPlayerTurnMaxTime, onComplete: () =>
{ {
Debug.Log($"currentPlayerTurnTimer: HandleDiceViewForUser"); Debug.Log($"currentPlayerTurnTimer: HandleDiceViewForUser");
diceRollHandler.HandleDiceViewForUser(); RollDiceForUser();
}, inProgress: (remTime) => }, inProgress: (remTime) =>
{ {
uIManager.UpdatePlayerTurnText(currentPlayerTypeTurn, currentPlayerTurnMaxTime - (int)remTime); uIManager.UpdatePlayerTurnText(currentPlayerTypeTurn, currentPlayerTurnMaxTime - (int)remTime);
@ -349,7 +349,7 @@ public class GameplayManager : MonoBehaviour, IBase, IBootLoader, IDataLoader
public void OnDiceInteracted() public void OnDiceInteracted()
{ {
ResetCurrentPlayerTurnTimer(); ResetCurrentPlayerTurnTimer();
diceRollHandler.HandleDiceViewForUser(); RollDiceForUser();
} }
public void RollDiceForPlayer(int rolledVal) public void RollDiceForPlayer(int rolledVal)
@ -515,7 +515,7 @@ public class GameplayManager : MonoBehaviour, IBase, IBootLoader, IDataLoader
} }
if (CanRollDiceAgain) if (CanRollDiceAgain)
HandleDiceRoll(); RollDiceForBot();
else else
SwitchPlayer(); SwitchPlayer();
} }
@ -570,6 +570,8 @@ public class GameplayManager : MonoBehaviour, IBase, IBootLoader, IDataLoader
return possibleSteps > TilesManager.GetGeneralTilesLength() - 1; return possibleSteps > TilesManager.GetGeneralTilesLength() - 1;
} }
private bool HasNoPlayersTravelling() => Mathf.Abs(availPlayersToMove.Count - playerGameDatasDict[currentPlayerTypeTurn].totalPawnsInFinishingPath) < 1;
public void OnDiceRolled(int rolledVal) public void OnDiceRolled(int rolledVal)
{ {
SetCanRollDiceForUser(false); SetCanRollDiceForUser(false);
@ -578,9 +580,7 @@ public class GameplayManager : MonoBehaviour, IBase, IBootLoader, IDataLoader
Debug.Log($"Tile Index :: LUDO :: rolledVal: {rolledVal} :: {currentPlayerTypeTurn}"); Debug.Log($"Tile Index :: LUDO :: rolledVal: {rolledVal} :: {currentPlayerTypeTurn}");
diceRolledValue = rolledVal; diceRolledValue = rolledVal;
diceText.text = $"{diceRolledValue}"; diceText.text = $"{diceRolledValue}";
availPlayersToMove = new List<PlayerPawn>();
InitActivePlayers();
if (!CanRollDiceAgain) if (!CanRollDiceAgain)
{ {
SetDisplayCountForAllAvailPlayers(true); SetDisplayCountForAllAvailPlayers(true);
@ -596,9 +596,7 @@ public class GameplayManager : MonoBehaviour, IBase, IBootLoader, IDataLoader
if (botTypesInGame != null && !botTypesInGame.Contains(currentPlayerTypeTurn)) if (botTypesInGame != null && !botTypesInGame.Contains(currentPlayerTypeTurn))
{ {
var hasNoPlayersTraveling = Mathf.Abs(availPlayersToMove.Count - playerGameDatasDict[currentPlayerTypeTurn].totalPawnsInFinishingPath) < 1; if (availPlayersToMove.Count < 1 || HasNoPlayersTravelling())
Debug.Log($"availPlayersToMove.Count: {availPlayersToMove.Count}, hasNoPlayersTraveling: {hasNoPlayersTraveling}, totalPawnsInFinishingPath: {playerGameDatasDict[currentPlayerTypeTurn].totalPawnsInFinishingPath}");
if (availPlayersToMove.Count < 1 || hasNoPlayersTraveling)
{ {
if (playerGameDatasDict[currentPlayerTypeTurn].totalPawnsInHome > 0) if (playerGameDatasDict[currentPlayerTypeTurn].totalPawnsInHome > 0)
{ {
@ -707,6 +705,7 @@ public class GameplayManager : MonoBehaviour, IBase, IBootLoader, IDataLoader
private void InitActivePlayers() private void InitActivePlayers()
{ {
availPlayersToMove = new List<PlayerPawn>();
availPlayersToMove = playerGameDatasDict[currentPlayerTypeTurn].playerPawnsDict.Values.Select(pawn => pawn) availPlayersToMove = playerGameDatasDict[currentPlayerTypeTurn].playerPawnsDict.Values.Select(pawn => pawn)
.Where(pawn => pawn.GetPlayerState() == PlayerState.InSafeZone || .Where(pawn => pawn.GetPlayerState() == PlayerState.InSafeZone ||
pawn.GetPlayerState() == PlayerState.Moving || pawn.GetPlayerState() == PlayerState.Moving ||
@ -745,16 +744,48 @@ public class GameplayManager : MonoBehaviour, IBase, IBootLoader, IDataLoader
{ {
if (CanRollDiceAgain) if (CanRollDiceAgain)
{ {
HandleDiceRoll(); RollDiceForBot();
} }
} }
private void HandleDiceRoll() private void RollDiceForUser()
{
InitActivePlayers();
bool canUsePawnsFromHome = CanUsePawnsFromHome();
diceRollHandler.HandleDiceViewForUser(
hasNoActionOnRoll: canUsePawnsFromHome,
playerBase: canUsePawnsFromHome ? PlayerBaseHandler.GetPlayerBase(currentPlayerTypeTurn) : null);
}
private void RollDiceForBot()
{
InitActivePlayers();
HandleDiceRollForBot();
}
/***
* Summary:
* Scenario where the user/bot has no other move when some of the pawns/characters out of home
* are in finishing path and doesn't have a move from dice rolled value
*/
private bool CanUsePawnsFromHome()
{
return playerGameDatasDict[currentPlayerTypeTurn].totalPawnsInHome > 0 && HasNoPlayersTravelling();
}
private void HandleDiceRollForBot()
{ {
if (isDebugAITest) if (isDebugAITest)
diceRollHandler.HandleDiceViewForBot((rollVal) => RollDiceForBot(rollVal), diceValue == 0 ? UnityEngine.Random.Range(1, Ludo_3D_Constants.Max_Dice_Rolls + 1) : diceValue); diceRollHandler.HandleDiceViewForBot((rollVal) => RollDiceForBot(rollVal),
else diceValue == 0 ? UnityEngine.Random.Range(1, Ludo_3D_Constants.Max_Dice_Rolls + 1) : diceValue);
diceRollHandler.HandleDiceViewForBot((rollVal) => RollDiceForBot(rollVal)); else
{
bool canUsePawnsFromHome = CanUsePawnsFromHome();
diceRollHandler.HandleDiceViewForBot(
(rollVal) => RollDiceForBot(rollVal),
hasNoActionOnRoll: canUsePawnsFromHome,
playerBase: canUsePawnsFromHome ? PlayerBaseHandler.GetPlayerBase(currentPlayerTypeTurn) : null);
}
} }
public void OnPawnSelected(PlayerPawn selectedPawn) public void OnPawnSelected(PlayerPawn selectedPawn)
@ -1106,6 +1137,7 @@ public class GameplayManager : MonoBehaviour, IBase, IBootLoader, IDataLoader
// ShowUpdatedPlayerCountOnTile(playerPawn); // ShowUpdatedPlayerCountOnTile(playerPawn);
UpdatePlayerState(playerPawn, PlayerState.HasFinished); UpdatePlayerState(playerPawn, PlayerState.HasFinished);
playerGameDatasDict[currentPlayerTypeTurn].totalPawnsFinished++; playerGameDatasDict[currentPlayerTypeTurn].totalPawnsFinished++;
playerGameDatasDict[currentPlayerTypeTurn].totalPawnsInFinishingPath--; playerGameDatasDict[currentPlayerTypeTurn].totalPawnsInFinishingPath--;

View File

@ -7,12 +7,17 @@ public class BasePlacementData
public Transform placementTransform; public Transform placementTransform;
} }
public class PlayerBase : MonoBehaviour public class PlayerBase : MonoBehaviour, IRollBase
{ {
[SerializeField] private PlayerType playerType; [SerializeField] private PlayerType playerType;
[SerializeField] private BasePlacementData[] basePlacementDatas; [SerializeField] private BasePlacementData[] basePlacementDatas;
[SerializeField] private PlayerPawn[] playerPawns; [SerializeField] private PlayerPawn[] playerPawns;
[SerializeField] private GameObject playerBaseEffect; [SerializeField] private GameObject playerBaseEffect;
private int sixRollCount, maxRollCount;
public int SixRollCount => sixRollCount;
public int MaxRollCount => maxRollCount;
public bool IsBotBase public bool IsBotBase
{ {
@ -21,6 +26,22 @@ public class PlayerBase : MonoBehaviour
public PlayerType GetPlayerType() => playerType; public PlayerType GetPlayerType() => playerType;
public void UpdateSixRollCount()
{
sixRollCount++;
}
public void ResetRollData()
{
sixRollCount = 0;
maxRollCount = 0;
}
public void UpdateMaxRollCount(int val)
{
maxRollCount = val;
}
public void InitPlayerData() public void InitPlayerData()
{ {
for (int idx = 0; idx < basePlacementDatas.Length; idx++) for (int idx = 0; idx < basePlacementDatas.Length; idx++)

View File

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using System.Collections.Generic;
[System.Serializable] [System.Serializable]
public class PlayerBaseData public class PlayerBaseData

View File

@ -29,7 +29,7 @@ public class DiceRollHandler : MonoBehaviour
inputManager.SetDiceRollValue(rolledVal); inputManager.SetDiceRollValue(rolledVal);
} }
public void HandleDiceViewForUser() public void HandleDiceViewForUser(bool hasNoActionOnRoll = false, PlayerBase playerBase = null)
{ {
if (!inputManager.GameplayManager.CanRollDiceForUser) return; if (!inputManager.GameplayManager.CanRollDiceForUser) return;
@ -39,11 +39,17 @@ public class DiceRollHandler : MonoBehaviour
if (inputManager.GameplayManager.IsDebugPlayerTest) if (inputManager.GameplayManager.IsDebugPlayerTest)
OnUserDiceRollComplete(GetDiceTestVal()); OnUserDiceRollComplete(GetDiceTestVal());
else else
diceView.Roll(onComplete: (rolledVal) => OnUserDiceRollComplete(rolledVal), false); {
diceView.InitOnNoActionsAfterRoll(playerBase, hasNoActionOnRoll);
diceView.Roll(
(rolledVal) => OnUserDiceRollComplete(rolledVal),
false);
}
} }
public void HandleDiceViewForBot(Action<int> onComplete) public void HandleDiceViewForBot(Action<int> onComplete, bool hasNoActionOnRoll = false, PlayerBase playerBase = null)
{ {
diceView.InitOnNoActionsAfterRoll(playerBase, hasNoActionOnRoll);
diceView.Roll(onComplete: onComplete, true); diceView.Roll(onComplete: onComplete, true);
} }