<commit_before>﻿using Bib3;
using Sanderling.Parse;
using BotEngine.Interface;
using System.Linq;
using System.Collections.Generic;
using System;
using Sanderling.Motor;
using BotEngine.Motor;
using Sanderling.ABot.Bot.Task;
using BotEngine.Common;

namespace Sanderling.ABot.Bot
{
	public class Bot
	{
		public BotStepInput StepLastInput { private set; get; }

		BotStepResult stepLastResult;

		int motionId;

		int stepIndex;

		FromProcessMeasurement<IMemoryMeasurement> memoryMeasurementAtTime;

		readonly Accumulator.MemoryMeasurementAccumulator MemoryMeasurementAccu = new Accumulator.MemoryMeasurementAccumulator();

		readonly IDictionary<Int64, int> MouseClickLastStepIndexFromUIElementId = new Dictionary<Int64, int>();

		readonly IDictionary<Accumulation.IShipUiModule, int> ToggleLastStepIndexFromModule = new Dictionary<Accumulation.IShipUiModule, int>();

		public Int64? MouseClickLastAgeStepCountFromUIElement(Interface.MemoryStruct.IUIElement uiElement)
		{
			if (null == uiElement)
				return null;

			var interactionLastStepIndex = MouseClickLastStepIndexFromUIElementId?.TryGetValueNullable(uiElement.Id);

			return stepIndex - interactionLastStepIndex;
		}

		public Int64? ToggleLastAgeStepCountFromModule(Accumulation.IShipUiModule module) =>
			stepIndex - ToggleLastStepIndexFromModule?.TryGetValueNullable(module);

		public BotStepResult Step(BotStepInput input)
		{
			StepLastInput = input;

			BotStepResult stepResult = null;

			var listTaskPath = new List<IBotTask[]>();

			try
			{
				memoryMeasurementAtTime = input?.FromProcessMemoryMeasurement?.MapValue(measurement => measurement?.Parse());

				var memoryMeasurement = memoryMeasurementAtTime?.Value;

				MemoryMeasurementAccu.Accumulate(memoryMeasurementAtTime);

				var sequenceTaskPath =
					((IBotTask)new BotTask { Component = SequenceRootTask() })?.EnumeratePathToNodeFromTreeDFirst(node => node?.Component)?.Where(path => null != path?.LastOrDefault());

				var sequenceTaskPathWithMotion = sequenceTaskPath?.Where(task => null != task?.LastOrDefault()?.Motion);

				listTaskPath.Add(sequenceTaskPathWithMotion?.FirstOrDefault());
			}
			finally
			{
				var listMotion = new List<MotionRecommendation>();

				foreach (var moduleToggle in listTaskPath.ConcatNullable().OfType<ModuleToggleTask>().Select(moduleToggleTask => moduleToggleTask?.module).WhereNotDefault())
					ToggleLastStepIndexFromModule[moduleToggle] = stepIndex;

				foreach (var taskPath in listTaskPath.EmptyIfNull())
				{
					var taskMotionParam = taskPath?.LastOrDefault()?.Motion;

					if (null == taskMotionParam)
						continue;

					listMotion.Add(new MotionRecommendation
					{
						Id = motionId++,
						MotionParam = taskMotionParam,
					});
				}

				var setMotionMOuseWaypointUIElement =
					listMotion
					?.Select(motion => motion?.MotionParam)
					?.Where(motionParam => 0 < motionParam?.MouseButton?.Count())
					?.Select(motionParam => motionParam?.MouseListWaypoint)
					?.ConcatNullable()?.Select(mouseWaypoint => mouseWaypoint?.UIElement)?.WhereNotDefault();

				foreach (var mouseWaypointUIElement in setMotionMOuseWaypointUIElement.EmptyIfNull())
					MouseClickLastStepIndexFromUIElementId[mouseWaypointUIElement.Id] = stepIndex;

				stepLastResult = stepResult = new BotStepResult
				{
					ListMotion = listMotion?.ToArrayIfNotEmpty(),
				};

				++stepIndex;
			}

			return stepResult;
		}

		IEnumerable<IBotTask> SequenceRootTask()
		{
			yield return this.EnsureIsActive(MemoryMeasurementAccu?.ShipUiModule?.Where(module => module?.TooltipLast?.Value?.IsHardener ?? false));

			var moduleUnknown = MemoryMeasurementAccu?.ShipUiModule?.FirstOrDefault(module => null == module?.TooltipLast?.Value);

			yield return new BotTask { Motion = moduleUnknown?.MouseMove() };

			yield return new BotTask { Component = CombatSequenceTask() };
		}

		IEnumerable<IBotTask> CombatSequenceTask()
		{
			var memoryMeasurement = memoryMeasurementAtTime?.Value;

			var listOverviewEntryToAttack =
				memoryMeasurement?.WindowOverview?.FirstOrDefault()?.ListView?.Entry?.Where(entry => entry?.MainIcon?.Color?.IsRed() ?? false)
				?.OrderBy(entry => entry?.DistanceMax ?? int.MaxValue)
				?.ToArray();

			var targetSelected =
				memoryMeasurement?.Target?.FirstOrDefault(target => target?.IsSelected ?? false);

			var shouldAttackTarget =
				listOverviewEntryToAttack?.Any(entry => entry?.MeActiveTarget ?? false) ?? false;

			var setModuleWeapon =
				MemoryMeasurementAccu?.ShipUiModule?.Where(module => module?.TooltipLast?.Value?.IsWeapon ?? false);

			if (null != targetSelected)
				if (shouldAttackTarget)
					yield return this.EnsureIsActive(setModuleWeapon);
<commit_msg>In this case, combat is not possible.<commit_after>﻿using Bib3;
using Sanderling.Parse;
using BotEngine.Interface;
using System.Linq;
using System.Collections.Generic;
using System;
using Sanderling.Motor;
using BotEngine.Motor;
using Sanderling.ABot.Bot.Task;
using BotEngine.Common;

namespace Sanderling.ABot.Bot
{
	public class Bot
	{
		public BotStepInput StepLastInput { private set; get; }

		BotStepResult stepLastResult;

		int motionId;

		int stepIndex;

		FromProcessMeasurement<IMemoryMeasurement> memoryMeasurementAtTime;

		readonly Accumulator.MemoryMeasurementAccumulator MemoryMeasurementAccu = new Accumulator.MemoryMeasurementAccumulator();

		readonly IDictionary<Int64, int> MouseClickLastStepIndexFromUIElementId = new Dictionary<Int64, int>();

		readonly IDictionary<Accumulation.IShipUiModule, int> ToggleLastStepIndexFromModule = new Dictionary<Accumulation.IShipUiModule, int>();

		public Int64? MouseClickLastAgeStepCountFromUIElement(Interface.MemoryStruct.IUIElement uiElement)
		{
			if (null == uiElement)
				return null;

			var interactionLastStepIndex = MouseClickLastStepIndexFromUIElementId?.TryGetValueNullable(uiElement.Id);

			return stepIndex - interactionLastStepIndex;
		}

		public Int64? ToggleLastAgeStepCountFromModule(Accumulation.IShipUiModule module) =>
			stepIndex - ToggleLastStepIndexFromModule?.TryGetValueNullable(module);

		public BotStepResult Step(BotStepInput input)
		{
			StepLastInput = input;

			BotStepResult stepResult = null;

			var listTaskPath = new List<IBotTask[]>();

			try
			{
				memoryMeasurementAtTime = input?.FromProcessMemoryMeasurement?.MapValue(measurement => measurement?.Parse());

				var memoryMeasurement = memoryMeasurementAtTime?.Value;

				MemoryMeasurementAccu.Accumulate(memoryMeasurementAtTime);

				var sequenceTaskPath =
					((IBotTask)new BotTask { Component = SequenceRootTask() })?.EnumeratePathToNodeFromTreeDFirst(node => node?.Component)?.Where(path => null != path?.LastOrDefault());

				var sequenceTaskPathWithMotion = sequenceTaskPath?.Where(task => null != task?.LastOrDefault()?.Motion);

				listTaskPath.Add(sequenceTaskPathWithMotion?.FirstOrDefault());
			}
			finally
			{
				var listMotion = new List<MotionRecommendation>();

				foreach (var moduleToggle in listTaskPath.ConcatNullable().OfType<ModuleToggleTask>().Select(moduleToggleTask => moduleToggleTask?.module).WhereNotDefault())
					ToggleLastStepIndexFromModule[moduleToggle] = stepIndex;

				foreach (var taskPath in listTaskPath.EmptyIfNull())
				{
					var taskMotionParam = taskPath?.LastOrDefault()?.Motion;

					if (null == taskMotionParam)
						continue;

					listMotion.Add(new MotionRecommendation
					{
						Id = motionId++,
						MotionParam = taskMotionParam,
					});
				}

				var setMotionMOuseWaypointUIElement =
					listMotion
					?.Select(motion => motion?.MotionParam)
					?.Where(motionParam => 0 < motionParam?.MouseButton?.Count())
					?.Select(motionParam => motionParam?.MouseListWaypoint)
					?.ConcatNullable()?.Select(mouseWaypoint => mouseWaypoint?.UIElement)?.WhereNotDefault();

				foreach (var mouseWaypointUIElement in setMotionMOuseWaypointUIElement.EmptyIfNull())
					MouseClickLastStepIndexFromUIElementId[mouseWaypointUIElement.Id] = stepIndex;

				stepLastResult = stepResult = new BotStepResult
				{
					ListMotion = listMotion?.ToArrayIfNotEmpty(),
				};

				++stepIndex;
			}

			return stepResult;
		}

		IEnumerable<IBotTask> SequenceRootTask()
		{
			yield return this.EnsureIsActive(MemoryMeasurementAccu?.ShipUiModule?.Where(module => module?.TooltipLast?.Value?.IsHardener ?? false));

			var moduleUnknown = MemoryMeasurementAccu?.ShipUiModule?.FirstOrDefault(module => null == module?.TooltipLast?.Value);

			yield return new BotTask { Motion = moduleUnknown?.MouseMove() };

			yield return new BotTask { Component = CombatSequenceTask() };
		}

		IEnumerable<IBotTask> CombatSequenceTask()
		{
			var memoryMeasurement = memoryMeasurementAtTime?.Value;

			var currentManeuverType = memoryMeasurement?.ShipUi?.Indication?.ManeuverType;

			if (ShipManeuverTypeEnum.Warp == currentManeuverType ||
				ShipManeuverTypeEnum.Jump == currentManeuverType)
				yield break;

			var listOverviewEntryToAttack =
				memoryMeasurement?.WindowOverview?.FirstOrDefault()?.ListView?.Entry?.Where(entry => entry?.MainIcon?.Color?.IsRed() ?? false)
				?.OrderBy(entry => entry?.DistanceMax ?? int.MaxValue)
				?.ToArray();

			var targetSelected =
				memoryMeasurement?.Target?.FirstOrDefault(target => target?.IsSelected ?? false);

			var shouldAttackTarget =
				listOverviewEntryToAttack?.Any(entry => entry?.MeActiveTarget ?? false) ?? false;

			var setModuleWeapon =
				MemoryMeasurementAccu?.ShipUiModule?.Where(module => module?.TooltipLast?.Value?.IsWeapon ?? false);

			if (null != targetSelected)
				if (shouldAttackTarget)
					yield return this.EnsureIsActive(setModuleWeapon);

{'fraction_non_alphanumeric': 0.10481028405556707, 'fraction_numerical': 0.0020733982998133943, 'mean_word_length': 5.094125078963993, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1531186', 'n_tokens_mistral': 3344, 'n_tokens_neox': 2725, 'n_words': 490}