public void FindEnemyUnitToAttackFrom()
        {
            //_log.Debug("FindEnemyUnitToAttackFrom()");
            for (var i = 0; i < _units.Count; i++)
            {
                if (_alliedEnemyMatrix.AreEnemies(_units[i].Nationality, NATIONALITY.USA) &&
                    _units[i].UnitHasAttackedThisTurn == false)
                {
                    var cells = _map.FindAdjacentCells(_units[i].Col, _units[i].Row, -1, _units[i].Range);                     //TODO: need to find all cells in firing range

                    //TODO: update algorithm to attack the lowest defense unit first (to remove from board and prevent multiple counter attacks per turn).
                    foreach (var cellCoordinates in cells)
                    {
                        var alliedUnit = _units.FindUnit(cellCoordinates.Col, cellCoordinates.Row);
                        if (alliedUnit != null && _alliedEnemyMatrix.AreEnemies(alliedUnit.Nationality, _units[i].Nationality))
                        {
                            //_log.Debug("FindEnemyUnitToAttackFrom() allied unit under attack=" + alliedUnit.UnitNumber);
                            AlliedUnitUnderAttack = alliedUnit;
                            EnemyUnitAttacking    = _units[i];
                            EnemyAttackFlash      = 0;
                            return;
                        }
                    }
                }
            }

            AlliedUnitUnderAttack = null;
            EnemyUnitAttacking    = null;
        }
        public void PlayerCheckForPossibleAttack(Unit unit, IAlliedEnemyMatrix alliedEnemyMatrix)
        {
            // check to see if this unit can attack an enemy unit (in range, unit has not attacked this turn, etc.)
            // if unit cannot attack, then mark as turn complete
            var cells = _terrainMap.FindAdjacentCells(unit.Col, unit.Row, unit.UnitType, unit.Range);

            foreach (var cell in cells)
            {
                var nearbyUnit = FindUnit(cell.Col, cell.Row);
                if (nearbyUnit != null && alliedEnemyMatrix.AreEnemies(nearbyUnit.Nationality, unit.Nationality))
                {
                    // this unit can attack a nearby enemy.
                    nearbyUnit.Sleep = false;
                    return;
                }
            }

            // no nearby enemies in range, end turn for this unit
            unit.UnitHasAttackedThisTurn = true;
        }
Exemple #3
0
        private void FindPath()
        {
            _iterations++;

            if (WayPoint.Count > 0)
            {
                //log.Debug("WayPoint.Count > 0");
                return;
            }

            if (_iterations > 500)
            {
                //log.Debug("Iterations > 50");
                return;
            }

            // pop the top item off the openlist and find surrounding cells
            var node = _openList.Pop();

            //log.DebugFormat("FindPath({0},{1})", node.X, node.Y);

            _closedList.Push(node);

            var surroundingCells = _terrainMap.FindAdjacentCells(node.X, node.Y, _unitType, 1);

            foreach (var mapCoordinates in surroundingCells)
            {
                // skip any nodes that are already in the closed list
                if (!_closedList.Contains(mapCoordinates.Col, mapCoordinates.Row))
                {
                    var distance = node.G;
                    if (_terrainMap[mapCoordinates.Col, mapCoordinates.Row].Roads > 0)
                    {
                        distance += 1;
                    }
                    else
                    {
                        distance += 3;
                    }

                    if (_openList.Contains(mapCoordinates.Col, mapCoordinates.Row))
                    {
                        // check to see if this path is shorter than the one on the open list, if so, then update it, otherwise skip
                        var tempNode = new AStarNode(node.X, node.Y, mapCoordinates.Col, mapCoordinates.Row, _endCol, _endRow, distance);

                        _openList.UpdateNodeIfBetter(tempNode);
                    }
                    else
                    {
                        _openList.Push(new AStarNode(node.X, node.Y, mapCoordinates.Col, mapCoordinates.Row, _endCol, _endRow, distance));
                    }
                }
            }

            var smallestNode = _openList.FindSmallestNode();

            if (smallestNode == null)
            {
                //log.Debug("smallestNode == null");
                return;
            }

            // check if this is the destination node
            if (smallestNode.X == _endCol && smallestNode.Y == _endRow)
            {
                // consolidate the actual path into the WayPoint list
                WayPoint.Add(new Offset(_endCol, _endRow));

                // walk back to the starting point
                var tempNode = _closedList.GetNode(smallestNode.Source.X, smallestNode.Source.Y);
                while (tempNode.X != _startCol || tempNode.Y != _startRow)
                {
                    WayPoint.Insert(0, new Offset(tempNode.X, tempNode.Y));
                    tempNode = _closedList.GetNode(tempNode.Source.X, tempNode.Source.Y);
                }

                // clear the open and closed lists
                _openList.Clear();
                _closedList.Clear();
                //log.Debug("success");
                return;
            }

            _openList.Push(smallestNode);
            FindPath();
        }