Fixed calculation of surrounded tiles. Fixed some AI bugs.

This commit is contained in:
AlexMamontow 2021-08-09 22:16:41 +03:00
parent 6dfd44119d
commit 701d8c9fce
5 changed files with 497 additions and 38 deletions

File diff suppressed because it is too large Load Diff

View File

@ -10,6 +10,7 @@ public class AI_Input : MonoBehaviour
public float agressiveTime = 5f; public float agressiveTime = 5f;
public float attackTime = 2f; public float attackTime = 2f;
public float updateBehaviourIn = 1f;
public Action OnTouchDown, OnTouchUp; public Action OnTouchDown, OnTouchUp;
public Action OnCurrentPathFinished, OnAttack; public Action OnCurrentPathFinished, OnAttack;
public List<PlayerState> enemies; public List<PlayerState> enemies;
@ -30,7 +31,7 @@ public class AI_Input : MonoBehaviour
_actionManager.OnActionSuccess += BackToPatrol; _actionManager.OnActionSuccess += BackToPatrol;
_actionManager.OnActionStart += OnActionStart; _actionManager.OnActionStart += OnActionStart;
_tileMovement.OnFinishMovement += CheckState; //_tileMovement.OnFinishMovement += CheckState;
_tileMovement.OnStartMovement += StopJoystick; _tileMovement.OnStartMovement += StopJoystick;
_playerState.OnInitializied += StartPatrolBehaviour; _playerState.OnInitializied += StartPatrolBehaviour;
@ -39,6 +40,11 @@ public class AI_Input : MonoBehaviour
SetEnemies(); SetEnemies();
} }
private void Start()
{
InvokeRepeating("CheckState", UnityEngine.Random.Range(0.5f, 1f), updateBehaviourIn); //to make not the same start
}
private void OnActionStart(ActionType arg1, CharacterState arg2) private void OnActionStart(ActionType arg1, CharacterState arg2)
{ {
_currentEnemy = null; _currentEnemy = null;
@ -91,12 +97,12 @@ public class AI_Input : MonoBehaviour
MoveTo(_currentFollowingPath[1]); MoveTo(_currentFollowingPath[1]);
} }
private void CheckState(ActionType newType, CharacterState newState) private void CheckState(/*ActionType newType, CharacterState newState*/)
{ {
foreach (PlayerState enemy in enemies) foreach (PlayerState enemy in enemies)
{ {
Debug.Log("Check near enemy"); Debug.Log("Check near enemy");
if (Vector3.Distance(enemy.transform.position, transform.position) <= TileManagment.tileOffset*1.1) if (Vector3.Distance(enemy.transform.position, transform.position) <= TileManagment.tileOffset*1.1f)
{ {
botState = BotState.Attack; botState = BotState.Attack;
_currentEnemy = enemy; _currentEnemy = enemy;
@ -124,6 +130,7 @@ public class AI_Input : MonoBehaviour
} }
SetBehaviour(botState); SetBehaviour(botState);
} }
private void SetBehaviour(BotState state) private void SetBehaviour(BotState state)
@ -144,7 +151,7 @@ public class AI_Input : MonoBehaviour
private void AttackEnemy(PlayerState currentEnemy) private void AttackEnemy(PlayerState currentEnemy)
{ {
//Debug.Log("attacking"); Debug.Log("attacking");
leftInput = Vector2.zero; leftInput = Vector2.zero;
_currentFollowingPath.Clear(); _currentFollowingPath.Clear();
//_actionManager.AttackEnemyOnTile(currentEnemy.currentTile); //_actionManager.AttackEnemyOnTile(currentEnemy.currentTile);
@ -219,12 +226,14 @@ public class AI_Input : MonoBehaviour
private IEnumerator TryToAttack(float attackCoolDown) private IEnumerator TryToAttack(float attackCoolDown)
{ {
while (_currentEnemy) while (_currentEnemy && Vector3.Distance(_currentEnemy.transform.position, transform.position) <= TileManagment.tileOffset * 1.1f)
{ {
//Debug.Log("try attack"); //Debug.Log("try attack");
_actionManager.AttackEnemyOnTile(_currentEnemy.currentTile); _actionManager.AttackEnemyOnTile(_currentEnemy.currentTile);
yield return new WaitForSeconds(attackCoolDown); yield return new WaitForSeconds(attackCoolDown);
} }
BackToPatrol();
StopAllCoroutines();
} }
public enum BotState public enum BotState

View File

@ -59,7 +59,7 @@ public class CaptureController : MonoBehaviour
//Debug.Log("Try to capture " + tile.name); //Debug.Log("Try to capture " + tile.name);
if(_ownerIndex != tile.tileOwnerIndex) if(_ownerIndex != tile.tileOwnerIndex)
{ {
if (tile.whoCanEasyGetTile != _ownerIndex) if (!tile.easyCaptureFor.Contains(_ownerIndex))
{ {
if (tile.tileOwnerIndex == TileOwner.Neutral) if (tile.tileOwnerIndex == TileOwner.Neutral)
{ {

View File

@ -14,6 +14,8 @@ public class TileInfo : MonoBehaviour
public TileOwner tileOwnerIndex = TileOwner.Neutral; //recieved by TileManager on game start public TileOwner tileOwnerIndex = TileOwner.Neutral; //recieved by TileManager on game start
public TileOwner whoCanEasyGetTile = TileOwner.Neutral; public TileOwner whoCanEasyGetTile = TileOwner.Neutral;
public List<TileOwner> easyCaptureFor = new List<TileOwner>();
public List<TileOwner> checkedFor = new List<TileOwner>();
public bool isBorderTile = false; public bool isBorderTile = false;

View File

@ -11,8 +11,10 @@ public class TileManagment : MonoBehaviour
public static List<List<TileInfo>> charTiles = new List<List<TileInfo>>(); public static List<List<TileInfo>> charTiles = new List<List<TileInfo>>();
public static PlayerState[] players;
public List<Material> tileMaterials; public List<Material> tileMaterials;
//public List<TileInfo> pathTiles = new List<TileInfo>();
public static float tileOffset; public static float tileOffset;
@ -36,6 +38,9 @@ public class TileManagment : MonoBehaviour
basicDirections = GetBasicDirections(BASIC_DIRECTIONS); basicDirections = GetBasicDirections(BASIC_DIRECTIONS);
players = FindObjectsOfType<PlayerState>();
Debug.Log(players.Length);
} }
private void InitCharTiles() private void InitCharTiles()
@ -76,9 +81,8 @@ public class TileManagment : MonoBehaviour
charTiles[(int)ownerIndex].Add(tile); charTiles[(int)ownerIndex].Add(tile);
charTiles[(int)oldOwner].Remove(tile); charTiles[(int)oldOwner].Remove(tile);
//Debug.Log(GetOtherTiles(tile).Count);
CheckSurroundedTiles(levelTiles, ownerIndex, tile); CheckSurroundedTiles(levelTiles, ownerIndex, tile);
//Debug.Log("Captured " + tile.name);
} }
public static void AssignBuildingToTile(TileInfo tile, GameObject building) public static void AssignBuildingToTile(TileInfo tile, GameObject building)
@ -197,11 +201,11 @@ public class TileManagment : MonoBehaviour
return tempArr; return tempArr;
} }
public static void CheckSurroundedTiles(List<TileInfo> tiles, TileOwner ownerIndex, TileInfo startTile) public static void CheckSurroundedTiles(List<TileInfo> tiles, TileOwner ownerIndex, TileInfo capTile)
{ {
List<TileInfo> firstAdjacentTiles = GetOtherTiles(startTile, ownerIndex); //List<TileInfo> firstAdjacentTiles = GetOtherTiles(startTile, ownerIndex);
List<TileInfo> firstAllAdjacentTiles = GetAllAdjacentTiles(startTile); //List<TileInfo> firstAllAdjacentTiles = GetAllAdjacentTiles(startTile);
List<TileOwner> differentOwners = new List<TileOwner>(); //List<TileOwner> differentOwners = new List<TileOwner>();
/*foreach (TileInfo tile in firstAllAdjacentTiles) /*foreach (TileInfo tile in firstAllAdjacentTiles)
{ {
if (!differentOwners.Contains(tile.tileOwnerIndex) && tile.tileOwnerIndex != TileOwner.Neutral) if (!differentOwners.Contains(tile.tileOwnerIndex) && tile.tileOwnerIndex != TileOwner.Neutral)
@ -223,13 +227,94 @@ public class TileManagment : MonoBehaviour
} }
}*/ }*/
foreach (TileInfo tile in firstAdjacentTiles) /*foreach (TileInfo tile in firstAdjacentTiles)
{ {
SetSurroundedTiles(tiles, ownerIndex, tile); SetSurroundedTiles(tiles, ownerIndex, tile);
}*/
List<TileOwner> checkingOwners = new List<TileOwner>();
checkingOwners.Add(capTile.tileOwnerIndex);
checkingOwners.Add(ownerIndex);
foreach (TileInfo tile in levelTiles)
{
/*tile.checkedFor.Clear();
tile.easyCaptureFor.Clear();*/
foreach (TileOwner owner in checkingOwners)
{
tile.checkedFor.Remove(owner);
tile.easyCaptureFor.Remove(owner);
}
}
foreach (TileInfo tile in levelTiles)
{
if (!tile.isBorderTile)
{
/*foreach (var player in players)
{
if ((!tile.checkedFor.Contains(player.ownerIndex)) && (tile.tileOwnerIndex!= player.ownerIndex))
{
CheckIfSurroundedByOwner(tiles, player.ownerIndex, tile);
}
}*/
foreach (TileOwner owner in checkingOwners)
{
if ((!tile.checkedFor.Contains(owner)) && (tile.tileOwnerIndex != owner))
{
CheckIfSurroundedByOwner(tiles, owner, tile);
}
}
}
} }
} }
public static void SetSurroundedTiles(List<TileInfo> tiles, TileOwner ownerIndex, TileInfo startTile)
public static void CheckIfSurroundedByOwner(List<TileInfo> tiles, TileOwner ownerIndex, TileInfo startTile)
{
List<TileInfo> connectedTiles = new List<TileInfo>();
var q = new Queue<TileInfo>(tiles.Count);
q.Enqueue(startTile);
int iterations = 0;
while (q.Count > 0)
{
var tile = q.Dequeue();
if (q.Count > tiles.Count)
{
throw new Exception("The algorithm is probably looping. Queue size: " + q.Count);
}
if (tile.isBorderTile) //we are in a wrong area
{
connectedTiles.Clear();
return;
}
if (connectedTiles.Contains(tile))
{
continue;
}
connectedTiles.Add(tile);
tile.checkedFor.Add(ownerIndex);
//Debug.Log("Checked");
var adjacentTiles = GetOtherTiles(tile, ownerIndex);
foreach (TileInfo newTile in adjacentTiles)
{
q.Enqueue(newTile);
}
iterations++;
}
foreach (TileInfo tile in connectedTiles)
{
tile.easyCaptureFor.Add(ownerIndex);
}
}
/* public static void SetSurroundedTiles(List<TileInfo> tiles, TileOwner ownerIndex, TileInfo startTile)
{ {
List<TileInfo> surroundedTiles = new List<TileInfo>(); List<TileInfo> surroundedTiles = new List<TileInfo>();
var q = new Queue<TileInfo>(tiles.Count); var q = new Queue<TileInfo>(tiles.Count);
@ -239,7 +324,6 @@ public class TileManagment : MonoBehaviour
while (q.Count > 0) while (q.Count > 0)
{ {
var tile = q.Dequeue(); var tile = q.Dequeue();
//Debug.Log("current tile " + tile.name);
if (q.Count > tiles.Count) if (q.Count > tiles.Count)
{ {
@ -248,12 +332,6 @@ public class TileManagment : MonoBehaviour
if (tile.isBorderTile) //we are in a wrong area if (tile.isBorderTile) //we are in a wrong area
{ {
//Debug.Log("FindBorder");
/*foreach (TileInfo t in surroundedTiles)
{
//t.whoCanEasyGetTile = ownerIndex;
t.whoCanEasyGetTile = TileOwner.Neutral;
}*/
surroundedTiles.Clear(); surroundedTiles.Clear();
return; return;
} }
@ -264,11 +342,9 @@ public class TileManagment : MonoBehaviour
} }
surroundedTiles.Add(tile); surroundedTiles.Add(tile);
/*if(tile.whoCanEasyGetTile != TileOwner.Neutral)
tile.whoCanEasyGetTile = TileOwner.Neutral;*/
var adjacentTiles = GetOtherTiles(tile, ownerIndex); var adjacentTiles = GetOtherTiles(tile, ownerIndex);
//Debug.Log("second tiles "+ adjacentTiles.Count);
foreach (TileInfo newTile in adjacentTiles) foreach (TileInfo newTile in adjacentTiles)
{ {
q.Enqueue(newTile); q.Enqueue(newTile);
@ -284,5 +360,5 @@ public class TileManagment : MonoBehaviour
tile.whoCanEasyGetTile = ownerIndex; tile.whoCanEasyGetTile = ownerIndex;
} }
Debug.Log("Surrounded " + surroundedTiles.Count); Debug.Log("Surrounded " + surroundedTiles.Count);
} }*/
} }