Fixed calculation of surrounded tiles. Fixed some AI bugs.
This commit is contained in:
parent
6dfd44119d
commit
701d8c9fce
File diff suppressed because it is too large
Load Diff
@ -10,6 +10,7 @@ public class AI_Input : MonoBehaviour
|
||||
|
||||
public float agressiveTime = 5f;
|
||||
public float attackTime = 2f;
|
||||
public float updateBehaviourIn = 1f;
|
||||
public Action OnTouchDown, OnTouchUp;
|
||||
public Action OnCurrentPathFinished, OnAttack;
|
||||
public List<PlayerState> enemies;
|
||||
@ -30,7 +31,7 @@ public class AI_Input : MonoBehaviour
|
||||
_actionManager.OnActionSuccess += BackToPatrol;
|
||||
_actionManager.OnActionStart += OnActionStart;
|
||||
|
||||
_tileMovement.OnFinishMovement += CheckState;
|
||||
//_tileMovement.OnFinishMovement += CheckState;
|
||||
_tileMovement.OnStartMovement += StopJoystick;
|
||||
_playerState.OnInitializied += StartPatrolBehaviour;
|
||||
|
||||
@ -39,6 +40,11 @@ public class AI_Input : MonoBehaviour
|
||||
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)
|
||||
{
|
||||
_currentEnemy = null;
|
||||
@ -91,12 +97,12 @@ public class AI_Input : MonoBehaviour
|
||||
MoveTo(_currentFollowingPath[1]);
|
||||
}
|
||||
|
||||
private void CheckState(ActionType newType, CharacterState newState)
|
||||
private void CheckState(/*ActionType newType, CharacterState newState*/)
|
||||
{
|
||||
foreach (PlayerState enemy in enemies)
|
||||
{
|
||||
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;
|
||||
_currentEnemy = enemy;
|
||||
@ -124,6 +130,7 @@ public class AI_Input : MonoBehaviour
|
||||
}
|
||||
|
||||
SetBehaviour(botState);
|
||||
|
||||
}
|
||||
|
||||
private void SetBehaviour(BotState state)
|
||||
@ -144,7 +151,7 @@ public class AI_Input : MonoBehaviour
|
||||
|
||||
private void AttackEnemy(PlayerState currentEnemy)
|
||||
{
|
||||
//Debug.Log("attacking");
|
||||
Debug.Log("attacking");
|
||||
leftInput = Vector2.zero;
|
||||
_currentFollowingPath.Clear();
|
||||
//_actionManager.AttackEnemyOnTile(currentEnemy.currentTile);
|
||||
@ -219,12 +226,14 @@ public class AI_Input : MonoBehaviour
|
||||
|
||||
private IEnumerator TryToAttack(float attackCoolDown)
|
||||
{
|
||||
while (_currentEnemy)
|
||||
while (_currentEnemy && Vector3.Distance(_currentEnemy.transform.position, transform.position) <= TileManagment.tileOffset * 1.1f)
|
||||
{
|
||||
//Debug.Log("try attack");
|
||||
_actionManager.AttackEnemyOnTile(_currentEnemy.currentTile);
|
||||
yield return new WaitForSeconds(attackCoolDown);
|
||||
}
|
||||
BackToPatrol();
|
||||
StopAllCoroutines();
|
||||
}
|
||||
|
||||
public enum BotState
|
||||
|
@ -59,7 +59,7 @@ public class CaptureController : MonoBehaviour
|
||||
//Debug.Log("Try to capture " + tile.name);
|
||||
if(_ownerIndex != tile.tileOwnerIndex)
|
||||
{
|
||||
if (tile.whoCanEasyGetTile != _ownerIndex)
|
||||
if (!tile.easyCaptureFor.Contains(_ownerIndex))
|
||||
{
|
||||
if (tile.tileOwnerIndex == TileOwner.Neutral)
|
||||
{
|
||||
|
@ -14,6 +14,8 @@ public class TileInfo : MonoBehaviour
|
||||
|
||||
public TileOwner tileOwnerIndex = TileOwner.Neutral; //recieved by TileManager on game start
|
||||
public TileOwner whoCanEasyGetTile = TileOwner.Neutral;
|
||||
public List<TileOwner> easyCaptureFor = new List<TileOwner>();
|
||||
public List<TileOwner> checkedFor = new List<TileOwner>();
|
||||
|
||||
|
||||
public bool isBorderTile = false;
|
||||
|
@ -11,8 +11,10 @@ public class TileManagment : MonoBehaviour
|
||||
|
||||
public static List<List<TileInfo>> charTiles = new List<List<TileInfo>>();
|
||||
|
||||
public static PlayerState[] players;
|
||||
|
||||
public List<Material> tileMaterials;
|
||||
//public List<TileInfo> pathTiles = new List<TileInfo>();
|
||||
|
||||
|
||||
public static float tileOffset;
|
||||
|
||||
@ -36,6 +38,9 @@ public class TileManagment : MonoBehaviour
|
||||
|
||||
basicDirections = GetBasicDirections(BASIC_DIRECTIONS);
|
||||
|
||||
players = FindObjectsOfType<PlayerState>();
|
||||
Debug.Log(players.Length);
|
||||
|
||||
}
|
||||
|
||||
private void InitCharTiles()
|
||||
@ -76,9 +81,8 @@ public class TileManagment : MonoBehaviour
|
||||
charTiles[(int)ownerIndex].Add(tile);
|
||||
charTiles[(int)oldOwner].Remove(tile);
|
||||
|
||||
//Debug.Log(GetOtherTiles(tile).Count);
|
||||
CheckSurroundedTiles(levelTiles, ownerIndex, tile);
|
||||
//Debug.Log("Captured " + tile.name);
|
||||
|
||||
}
|
||||
|
||||
public static void AssignBuildingToTile(TileInfo tile, GameObject building)
|
||||
@ -197,11 +201,11 @@ public class TileManagment : MonoBehaviour
|
||||
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> firstAllAdjacentTiles = GetAllAdjacentTiles(startTile);
|
||||
List<TileOwner> differentOwners = new List<TileOwner>();
|
||||
//List<TileInfo> firstAdjacentTiles = GetOtherTiles(startTile, ownerIndex);
|
||||
//List<TileInfo> firstAllAdjacentTiles = GetAllAdjacentTiles(startTile);
|
||||
//List<TileOwner> differentOwners = new List<TileOwner>();
|
||||
/*foreach (TileInfo tile in firstAllAdjacentTiles)
|
||||
{
|
||||
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);
|
||||
}*/
|
||||
|
||||
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>();
|
||||
var q = new Queue<TileInfo>(tiles.Count);
|
||||
@ -239,7 +324,6 @@ public class TileManagment : MonoBehaviour
|
||||
while (q.Count > 0)
|
||||
{
|
||||
var tile = q.Dequeue();
|
||||
//Debug.Log("current tile " + tile.name);
|
||||
|
||||
if (q.Count > tiles.Count)
|
||||
{
|
||||
@ -248,12 +332,6 @@ public class TileManagment : MonoBehaviour
|
||||
|
||||
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();
|
||||
return;
|
||||
}
|
||||
@ -264,11 +342,9 @@ public class TileManagment : MonoBehaviour
|
||||
}
|
||||
|
||||
surroundedTiles.Add(tile);
|
||||
/*if(tile.whoCanEasyGetTile != TileOwner.Neutral)
|
||||
tile.whoCanEasyGetTile = TileOwner.Neutral;*/
|
||||
|
||||
var adjacentTiles = GetOtherTiles(tile, ownerIndex);
|
||||
//Debug.Log("second tiles "+ adjacentTiles.Count);
|
||||
|
||||
foreach (TileInfo newTile in adjacentTiles)
|
||||
{
|
||||
q.Enqueue(newTile);
|
||||
@ -284,5 +360,5 @@ public class TileManagment : MonoBehaviour
|
||||
tile.whoCanEasyGetTile = ownerIndex;
|
||||
}
|
||||
Debug.Log("Surrounded " + surroundedTiles.Count);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user