// Attacking a hostile structure private void Attack() { List <Capturable> hostileCaps = gameBoard.capturables.Where(c => c.owner != player).ToList(); Capturable weakestCap = null; float smallestUnitCount = Mathf.Infinity; Capturable attackCap = null; float largestUnitCount = 0; foreach (Capturable hostileCap in hostileCaps) { if (hostileCap.unitCount < smallestUnitCount) { weakestCap = hostileCap; smallestUnitCount = hostileCap.unitCount; } } List <Capturable> attackCaps = gameBoard.capturables.Where(c => c.owner == player && !(c is Tower)).ToList(); foreach (Capturable cap in attackCaps) { if (cap.unitCount > largestUnitCount) { attackCap = cap; largestUnitCount = cap.unitCount; } } if (attackCap != null && weakestCap != null) { attackCap.BeginRaid(weakestCap); } }
// Update is called once per frame void Update() { if (currentActionCooldown <= 0) { if (Random.value < noActionChance) { List <Capturable> own = capturables.Where(c => c.owner == player).ToList(); if (own.Count > 0) { Capturable selected = own[Random.Range(0, own.Count)]; List <Capturable> targets = capturables.Where(c => c != selected && (c.owner != player || c.unitCount < c.unitCap)).ToList(); if (targets.Count > 0) { Capturable target = targets[Random.Range(0, targets.Count)]; selected.BeginRaid(target); } } } currentActionCooldown = actionCooldown; } else { currentActionCooldown -= Time.deltaTime; } }
public override void AgentAction(float[] vectorAction, string textAction) { if (currentActionCooldown <= 0) { base.AgentAction(vectorAction, textAction); Capturable selected = board.capturables[(int)vectorAction[0]]; Capturable target = board.capturables[(int)vectorAction[1]]; if (selected.owner == player) { if (selected.unitCount > 1 && target.owner != player) { //Encourage attacking AddReward(0.1f); } selected.BeginRaid(target); } int capturablesOwned = board.capturables.Count(c => c.owner == player); if (capturablesOwned > previousCapturables) { //Heavily encourage taking buildings. AddReward((capturablesOwned - previousCapturables) * 1.0f); } previousCapturables = capturablesOwned; //Check if we have won, or lost. if (board.capturables.All(c => c.owner == player) && !board.raids.Any(r => r.owner != player)) { //Super-heavily encourage winning. AddReward(100.0f); Done(); } else if (board.capturables.All(c => c.owner != player) && !board.raids.Any(r => r.owner == player)) { //Defeat Done(); } currentActionCooldown = actionCooldown; } currentActionCooldown -= Time.deltaTime; }
private void HandleRightClick() { if (selected != null) { if (Input.GetMouseButtonDown(1)) { var ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out RaycastHit rayHit)) { Capturable c = rayHit.collider.GetComponent <Capturable>(); if (c != null) { selected.BeginRaid(c); } } } } }
// Take over neutral buildings private void Expand() { List <Capturable> neutralCaps = gameBoard.capturables.Where(c => c.owner == null && !(c is Tower)).ToList(); List <Capturable> neutralTowers = gameBoard.capturables.Where(c => c.owner == null && c is Tower).ToList(); List <Capturable> ownCaps = gameBoard.capturables.Where(c => c.owner == player && !(c is Tower)).ToList(); float smallestDist = Mathf.Infinity; Capturable origin = null; Capturable destination = null; List <Capturable> sixtyPlusCaps = gameBoard.capturables.Where(c => c.owner == player && c.unitCount > 60).ToList(); if (neutralTowers.Count > 0 && sixtyPlusCaps.Count > 0) { neutralCaps = neutralTowers; ownCaps = sixtyPlusCaps; } if (neutralCaps.Count > 0) { foreach (var ownCap in ownCaps) { foreach (var neutralCap in neutralCaps) { float tempDist = Vector3.Distance(ownCap.transform.position, neutralCap.transform.position); if (tempDist < smallestDist) { smallestDist = tempDist; origin = ownCap; destination = neutralCap; } } } } if (origin != null && destination != null) { origin.BeginRaid(destination); } }
// Move units to a building to increase their unit count private void Reinforce() { List <Capturable> fullCaps = gameBoard.capturables.Where(c => c.owner == player && c.unitCount >= c.unitCap).ToList(); List <Capturable> noneFullCaps = gameBoard.capturables.Where(c => c.owner == player && c.unitCount < c.unitCap).ToList(); Capturable weakestCap = null; float smallestUnitCount = Mathf.Infinity; Capturable supportCap = null; float smallestDist = Mathf.Infinity; foreach (Capturable cap in noneFullCaps) { if (cap.unitCount < smallestUnitCount) { weakestCap = cap; smallestUnitCount = cap.unitCount; } } foreach (Capturable cap in fullCaps) { float tempDist = Mathf.Infinity; if (weakestCap != null) { tempDist = Vector3.Distance(weakestCap.transform.position, cap.transform.position); } if (tempDist < smallestDist) { smallestDist = tempDist; supportCap = cap; } } if (supportCap != null && weakestCap != null) { supportCap.BeginRaid(weakestCap); } }