Beispiel #1
0
 public uint GetScore(IEnumerable <GameObjectState> gameObjects,
                      GameObjectExtended self,
                      byte currentLane)
 {
     return(_priority
            + GetDistanceScore(currentLane)
            + GetTrafficScore(gameObjects, self)
            + GetVelocityScore());
 }
Beispiel #2
0
        private uint GetTrafficScore(IEnumerable <GameObjectState> gameObjects,
                                     GameObjectExtended self)
        {
            var closestObject = GetClosestObjectInFront(gameObjects, self);

            if (closestObject == null)
            {
                return(0);
            }
            var closest = new GameObjectExtended(closestObject);
            var score   = Math.Min(500, Math.Exp(self.GetDistance(closest) - 50)); // get exp

            score = Math.Max(0, 500 - score);                                      // reverse
            //score = score / 10;
            return((uint)score);
        }
Beispiel #3
0
        public PlayerAction Update(GameState gameState)
        {
            /* TODO:
             * always overtake roadblocks
             */
            float accelerationY = 1;
            bool  left = false, right = false;

            var selfObject = gameState
                             .GameObjectStates
                             .Single(o => o.Id == _playerId);
            var self = new GameObjectExtended(selfObject);

            var allObstacleObjects = gameState
                                     .GameObjectStates
                                     .Where(g => g.GameObjectType == GameObjectType.Car ||
                                            g.GameObjectType == GameObjectType.Roadblock ||
                                            g.GameObjectType == GameObjectType.Pedestrian ||
                                            g.GameObjectType == GameObjectType.BusStop)
                                     .ToList();

            //var nearbyLanes = _lanes
            //    .Where(lane => Math.Abs(_currentLaneId - lane.Id) <= 1);
            var laneScores = _lanes
                             .Select(lane => new
            {
                Key   = lane.Id,
                Value = lane.GetScore(allObstacleObjects, self, _currentLaneId)
            })
                             .ToList();
            var bestLaneScore = laneScores.Min(l => l.Value);
            var bestLaneId    = laneScores.First(l => l.Value == bestLaneScore).Key;

            Debug.Write($"{bestLaneId} ");
            Debug.Write($"{_lanes[0].GetScore(allObstacleObjects, self, _currentLaneId)} ");
            Debug.Write($"{_lanes[1].GetScore(allObstacleObjects, self, _currentLaneId)} ");
            Debug.Write($"{_lanes[2].GetScore(allObstacleObjects, self, _currentLaneId)} ");
            Debug.Write($"{_lanes[3].GetScore(allObstacleObjects, self, _currentLaneId)} ");
            Debug.WriteLine("");
            if (!_isChangingLanes && bestLaneId != _currentLaneId)
            {
                _isChangingLanes = true;
                _currentLaneId   = bestLaneId;
            }
            var desiredX = (_currentLaneId * 3) - _laneOffset + GameConstants.LaneWidth / 2;

            //var otherInFront = _lanes[_currentLaneId].GetClosestObjectInFront(allObstacleObjects, self);
            //if (otherInFront != null)
            //{
            //    var otherSpeed = obstacleInFront.VY;
            //    var otherDistanceToStop = CalculateDistanceToStop(otherSpeed, obstacleInFront.MaximumDeceleration);
            //    var selfDistanceToStop = CalculateDistanceToStop(self.VY, self.MaximumDeceleration);

            //    var selfCenterY = (self.BoundingBox.Top + self.BoundingBox.Bottom) / 2;
            //    var otherCenterY = (obstacleInFront.BoundingBox.Top + obstacleInFront.BoundingBox.Bottom) / 2;

            //    var distanceBetweenCars = Math.Abs(selfCenterY - otherCenterY) - self.BoundingBox.Height / 2 -
            //                              obstacleInFront.BoundingBox.Height / 2;
            //    const float plusSafeDistance = 1;
            //foreach (var lane in _lanePriority)
            //{
            //    var dist = obstaclesInFront[lane].GetDistance(self);
            //    if (dist > 50)
            //    {
            //        _currentLaneId = lane;
            //        break;
            //    }
            //}
            //if (otherInFront.GetDistance(self) < self.DistanceToStop - otherInFront.DistanceToStop + plusSafeDistance &&
            //    self.VY > 0)
            //{
            //    accelerationY = -1;
            //}
            //}

            var centerX = (self.BoundingBox.Left + self.BoundingBox.Right) / 2;

            if (Math.Abs(desiredX - centerX) > 0.2)
            {
                if (desiredX < centerX)
                {
                    left = true;
                }
                else
                {
                    right = true;
                }
            }
            else
            {
                _isChangingLanes = false;
            }

            return(new PlayerAction()
            {
                MoveLeft = left,
                MoveRight = right,
                Acceleration = accelerationY
            });
        }
Beispiel #4
0
        public float GetDistance(GameObjectExtended other)
        {
            var distanceBetweenCenters = Math.Abs(CenterY - other.CenterY);

            return(distanceBetweenCenters - EdgeOffsetY - other.EdgeOffsetY);
        }