public override void StateTick() { pState = state; if (nextStateCheckTime < Time.time) // Don't change state every tick { ai.aggression = AirCraftAI.AIAggression.FollowInRange; // usually, allow chasing after enemies for (int i = 0; i < carriers.Count; i++) { if (carriers[i] && carriers[i].GetHealth()[0] < carriers[i].GetMaxHealth()[0] * 0.3f) { // if base under attack -> defend if (AirCraftAI.getEnemyCountInRange(carriers[i].transform.position, 45f, craft.faction) > 0) { if ((carriers[i].transform.position - craft.transform.position).sqrMagnitude > 1500f) { ai.aggression = AirCraftAI.AIAggression.KeepMoving; // Get to the home base asap primaryTarget = carriers[i]; } else { primaryTarget = null; // TODO: find the most dangerous unit in the area, set as primary target } state = BattleState.Defend; } else { state = BattleState.Collect; } break; } } // if population is nearly capped, attack if (shellcore.GetTotalCommandLimit() < shellcore.GetUnitsCommanding().Count + 1) // TODO: OR if enemy base is weak { state = BattleState.Attack; } else if (shellcore.GetPower() >= 300) { state = BattleState.Fortify; primaryTarget = null; } // if there's no need for more population space, try to create turrets to protect owned outposts and stations else { if (state != BattleState.Collect) { collectTarget = null; } if (shellcore.GetTractorTarget() != null && shellcore.GetTractorTarget().GetComponent <Turret>() != null && shellcore.GetHealth()[0] > shellcore.GetMaxHealth()[0] * 0.1f) { state = BattleState.Attack; } else { state = BattleState.Collect; } } nextStateCheckTime = Time.time + 1f; } if (pState != state) { //Debug.LogFormat("Faction {0} Shellcore changed state to: {1}", craft.faction, state); } }
public override void Init() { carriers = new List <Entity>(); harvesterTurrets = new Dictionary <EnergyRock, Turret>(); Entity[] targetEntities = BattleZoneManager.getTargets(); if (targetEntities == null) { Debug.LogError("Battle zone target list not initialized"); ai.setMode(AirCraftAI.AIMode.Inactive); return; } for (int i = 0; i < targetEntities.Length; i++) { if (targetEntities[i] is ICarrier) { if (targetEntities[i].faction == craft.faction) { carriers.Add(targetEntities[i]); } } } if (craft is ShellCore shellCore) { shellcore = shellCore; } else { Debug.LogError("Battle zone AI should only be used by shellcores!"); } foreach (IOwnable ownable in shellcore.GetUnitsCommanding()) { if (ownable is Turret turret && turret.entityName == "Harvester Turret") { foreach (var rock in AIData.energyRocks) { if (!harvesterTurrets.ContainsKey(rock) && Vector3.SqrMagnitude(rock.transform.position - turret.transform.position) <= 200) { harvesterTurrets.Add(rock, turret); break; } } } } nextSearchTime = Time.time; nextStateCheckTime = Time.time; for (int i = 0; i < AIData.vendors.Count; i++) { int rockCount = 0; for (int j = 0; j < AIData.energyRocks.Count; j++) { if ((AIData.energyRocks[j].transform.position - AIData.vendors[i].transform.position).sqrMagnitude < 100) { rockCount++; } } AITargets.Add(new AITarget(AIData.vendors[i], rockCount + 1f)); } for (int i = 0; i < carriers.Count; i++) { AITargets.Add(new AITarget(carriers[i], 100f)); } }