public override void Initialize(BotWorker worker) { base.Initialize(worker); _worker = worker; _worker.AddHandler(typeof(StateInfo), OnStateInfo); _botIndex = _worker.WorkerId; _random = new Random((int)DateTime.Now.Ticks); //_map = StateMap.Instance; //_map.InitializeStatePositions(-2, -2, 2, 2, .5, .2); var preferences = new PlayerPreferences(); preferences.Color = _botColorPrefs[_botIndex]; var message = new PreferencesMessage(); message.preferences = preferences; _worker.SendReliable(message); }
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(); } }