private static int Offset(VSUserTransition transition)
        {
            int offset = 0;

            switch (transition)
            {
            case VSUserTransition.Rotate45Left:
                offset = 1;
                break;

            case VSUserTransition.Rotate45Right:
                offset = -1;
                break;

            case VSUserTransition.RotateRight:
                offset = -2;
                break;

            case VSUserTransition.RotateLeft:
                offset = 2;
                break;
            }

            return(offset);
        }
        /**
         * Warning: Don't use this to actually modify the state.
         * This method only used as a simulation with a reset afterwards.
         */
        private void UserCentricTransition(VSUserTransition transition, int playerNum)
        {
            switch (transition)
            {
            case VSUserTransition.Stay:
                break;

            case VSUserTransition.Focus:
                Focus(_userNumToIndex[playerNum] + _rotationState);
                break;

            case VSUserTransition.Unfocus:
                Defocus();
                break;

            case VSUserTransition.Defocus:
                Focus(_userNumToIndex[playerNum] + _rotationState == 0 ? 1 : 0);
                break;

            case VSUserTransition.Undefocus:
                Defocus();
                break;

            case VSUserTransition.RotateLeft:
            case VSUserTransition.SwitchLeft:
                RotateLeft();
                break;

            case VSUserTransition.RotateRight:
            case VSUserTransition.SwitchRight:
                RotateRight();
                break;

            case VSUserTransition.Rotate45Left:
                Rotate45Left();
                break;

            case VSUserTransition.Rotate45Right:
                Rotate45Right();
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(transition), transition, null);
            }
        }
Ejemplo n.º 3
0
        public void OnStateInfo(IMessageBase baseMessage)
        {
            if (_sendAlive)
            {
                _sendAlive = false;
                //_sendVotingThread.Abort();
            }

            StateInfo info = (StateInfo)baseMessage;

            TransitionVoting voting = new TransitionVoting();

            voting.StateId = info.StateId;

            //Logger.Debug($"Receive at turn: {VirtualSpaceTime.CurrentTurn}, Millis: {VirtualSpaceTime.CurrentTimeInMillis}");

            //_map.GetPlayerStatus(info.SystemRotationState, info.SystemPlayerInFocusState, info.SystemPlayerNum,
            //    out Vector startPosition, out Polygon startPolygon);

            //if (info.SystemPlayerNum == 0)
            //{
            //    Logger.Debug($"=============");
            //    Logger.Debug($"{info.SystemPlayerNum} at {startPosition}");
            //}

            if (!VirtualSpaceTime.IsInitialized)
            {
                Logger.Warn("Current VS time is not initialized");
                Thread.Sleep(200);
            }

            for (int i = 0; i < info.PossibleTransitions.Count; i++)
            {
                VSUserTransition transition = info.PossibleTransitions[i];

                TransitionVote vote = new TransitionVote();

                //_map.GetPlayerStatus(info.SystemRotationState, info.SystemPlayerInFocusState, transition, info.SystemPlayerNum,
                //    out Vector endPosition, out Polygon endPolygon);

                //if (info.SystemPlayerNum == 0)
                //{
                //    Logger.Debug($"{info.SystemPlayerNum} with {transition} at {endPosition}");
                //    Logger.Debug($"{endPolygon.Points.ToPrintableString()}");
                //    Logger.Debug($"{info.TransitionEndPositions[i]}");
                //    Logger.Debug($"{info.TransitionEndAreas[i].Points.ToPrintableString()}");
                //}

                //var timeMultiplier = 100; // for debugging
                var timeMultiplier = 1000; // realistic

                //if (_botIndex == 0)
                //{
                //vote.PlanningTimeType = PlanningTimeType.RelativeExecution;
                vote.PlanningTimestampMs = new List <double>()
                {
                    1000, 2000
                };
                //} else if (_botIndex == 1)
                //{
                //    vote.PlanningTimeType = PlanningTimeType.Absolute;
                //    var nowMs = VirtualSpaceTime.CurrentTimeInMillis;
                //    vote.PlanningTimestampMs = new List<double>() { nowMs + (2 + 2 * _random.NextDouble()) * timeMultiplier, nowMs + 4 * timeMultiplier };
                //}
                //else
                //{
                //    vote.PlanningTimeType = PlanningTimeType.RelativeArrival;
                //    vote.PlanningTimestampMs = new List<double>() { (2 + _random.NextDouble() * .3) * timeMultiplier, (2 + _random.NextDouble() * .3) * timeMultiplier };
                //}

                vote.Transition        = transition;
                vote.ExecutionLengthMs = new List <double> {
                    1000, 2000
                };

                //for (int j = 0; j < vote.PlanningTimestampMs.Count; j++)
                //{
                //    if (vote.PlanningTimeType == PlanningTimeType.RelativeExecution || vote.PlanningTimeType == PlanningTimeType.RelativeArrival)
                //        vote.PlanningTimestampMs[j] *= timeMultiplier;
                //    vote.ExecutionLengthMs[j] *= timeMultiplier;
                //}

                //for (int po = 500; po < 5000; po += 500)
                //{
                //    for (int eo = 500; eo < 5000; eo += 500)
                //    {
                //        vote.PlanningTimestampMs.Add(po + _random.NextDouble() * 100);
                //        vote.ExecutionLengthMs.Add(eo + _random.NextDouble() * 100);
                //    }
                //}

                List <TimeCondition> timeConditions = new List <TimeCondition>();
                var prepTime        = new Variable(VariableTypes.PreperationTime);
                var execTime        = new Variable(VariableTypes.ExecutionTime);
                var arrivalTime     = new Variable(VariableTypes.ArrivalTime);
                var calculationTime = new Variable(VariableTypes.CalculationTime);

                var proxyPrep = new Variable(VariableTypes.Continuous);

                var now            = VirtualSpaceTime.CurrentTimeInMillis;
                var offset         = 1000;
                var multiple       = new Variable(VariableTypes.Integer);
                var intervalLength = 300;
                var tolerance      = 100;
                if (_botIndex % 2 == 0)
                {
                    timeConditions.Add(proxyPrep == now + offset + multiple * intervalLength);
                    timeConditions.Add(prepTime >= proxyPrep - tolerance);
                    timeConditions.Add(prepTime <= proxyPrep + tolerance);
                }
                else
                {
                    timeConditions.Add(prepTime >= now + _random.NextDouble() * 200);
                }
                timeConditions.Add(execTime >= 1200 + _random.NextDouble() * 100);
                timeConditions.Add(execTime <= 1400 + _random.NextDouble() * 100);

                vote.TimeConditions = timeConditions;

                if (transition == VSUserTransition.Defocus || (info.YourCurrentTransition == VSUserTransition.Defocus && transition == VSUserTransition.Stay))
                {
                    vote.Value = _random.Next(0, 25);
                }
                else
                {
                    switch (transition)
                    {
                    case VSUserTransition.Stay:
                        vote.Value = _random.Next(40, 90);
                        if (_botIndex == 0)
                        {
                            //vote.RequiredTransition = _random.NextDouble() < .5f;
                        }

                        break;

                    case VSUserTransition.SwitchLeft:
                    case VSUserTransition.SwitchRight:
                        vote.Value = 0;
                        break;

                    case VSUserTransition.RotateLeft:
                    case VSUserTransition.RotateRight:
                        vote.Value = _random.Next(50, 100);
                        break;

                    case VSUserTransition.Focus:
                        vote.Value = 0;
                        break;

                    case VSUserTransition.Rotate45Left:
                    case VSUserTransition.Rotate45Right:
                        vote.Value = _random.Next(60, 100);
                        break;

                    default:
                        vote.Value = _random.Next(50, 100);
                        break;
                    }
                }

                voting.Votes.Add(vote);
            }

            _voting = voting;

            if (_botIndex == 0)
            {
                // send once
                _worker.SendReliable(_voting);
                //}
                //else if (_botIndex == 1)
                //{
                //    // too hard
                //    // recalc + send in loop
                //    _worker.SendReliable(_voting);
            }
            else
            {
                // send in loop
                _sendAlive        = true;
                _sendVotingThread = new Thread(SendVoting);
                _sendVotingThread.Start();
            }
        }
        public int GetRotationalIndexFromUserNum(int userNum, VSUserTransition transition)
        {
            int offset = Offset(transition);

            return(GetRotationalIndexFromUserNum(userNum, offset));
        }