<commit_before>﻿using System.Runtime.Remoting.Channels;

namespace EasterFarm.GameLogic
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;

    using Contracts;
    using EasterFarm.Common;
    using EasterFarm.Models;
    using EasterFarm.Models.Contracts;
    using EasterFarm.Models.FarmObjects.Food;
    using EasterFarm.Models.FarmObjects.Animals;
    using EasterFarm.Models.FarmObjects.Byproducts;
    using EasterFarm.Models.MarketPlace;
    using EasterFarm.Models.Presents;

    public class Engine
    {
        private FarmManager farmManager;
        private Market market;
        private PresentFactory presentFactory;

        private readonly HashSet<GameObject> gameObjects;
        private readonly HashSet<FarmFood> farmFoods;
        private readonly HashSet<Livestock> livestocks;
        private readonly HashSet<Villain> villains;
        private readonly HashSet<Byproduct> byproducts;

        public Engine(IRenderer renderer, IUserKeyboardInput userInput)
        {
            this.Renderer = renderer;
            this.UserInput = userInput;
            this.gameObjects = new HashSet<GameObject>();
            this.farmFoods = new HashSet<FarmFood>();
            this.livestocks = new HashSet<Livestock>();
            this.villains = new HashSet<Villain>();
            this.byproducts = new HashSet<Byproduct>();
        }

        internal IRenderer Renderer { get; private set; }

        internal IUserKeyboardInput UserInput { get; private set; }

        public void AddGameObject(GameObject gameObject)
        {
            var farmFood = gameObject as FarmFood;
            if (farmFood != null)
            {
                this.farmFoods.Add(farmFood);
            }

            var livestock = gameObject as Livestock;
            if (livestock != null)
            {
                this.livestocks.Add(livestock);
            }

            var villain = gameObject as Villain;
            if (villain != null)
            {
                this.villains.Add(villain);
            }

            var byproduct = gameObject as Byproduct;
            if (byproduct != null)
            {
                this.byproducts.Add(byproduct);
            }

            this.gameObjects.Add(gameObject);
        }

        public void Start()
        {
            this.SetInitialGameObjects();

            while (true)
            {
                this.Renderer.RenderAll();

                Thread.Sleep(200);

                this.UserInput.ProcessInput();

                this.Renderer.ClearRenderer();

                foreach (var gameObject in gameObjects)
                {
                    this.Renderer.EnqueueForRendering(gameObject);
                }

                this.Seek(livestocks, typeof(FarmFood));
                this.Seek(villains, typeof(Livestock));

                this.Collide(livestocks, farmFoods);
                this.Collide(villains, livestocks);

                ClearCollections();
            }
        }

        private void ClearCollections()
        {
            farmFoods.RemoveWhere(ff => ff.IsDestroyed);
            livestocks.RemoveWhere(ls => ls.IsDestroyed);
            villains.RemoveWhere(v => v.IsDestroyed);
            byproducts.RemoveWhere(bp => bp.IsDestroyed);
            gameObjects.RemoveWhere(go => go.IsDestroyed);
        }

        private void Collide(IEnumerable<Animal> colliders, IEnumerable<GameObject> destroyables)
        {
            foreach (var destroyable in destroyables)
            {
                foreach (var collider in colliders)
                {
                    if (destroyable.TopLeft == collider.TopLeft)
                    {
                        destroyable.IsDestroyed = true;

                        //TODO: Think of a way to get the color of the destroyable.
                        Byproduct byproduct = collider.Produce((ByproductColor.Red));
                        if (byproduct != null)
                        {
                            this.AddGameObject(byproduct);
                        }
                    }
                }
            }
        }

        private void Seek(IEnumerable<Animal> chasers, Type targetType)
        {
            var map = this.CreateTopographicMap(targetType);
            foreach (var chaser in chasers)
            {
                chaser.Move(map);
            }
        }

        private int[,] CreateTopographicMap(Type gameObjectType)
        {
            int[,] map = new int[Constants.WorldRows, (int)(Constants.WorldCols * Constants.LeftRightScreenRatio) + 1];
            int mapHeight = map.GetLength(0);
            int mapWidth = map.GetLength(1);

            for (int row = 1; row < mapHeight - 1; row++)
            {
                for (int col = 1; col < mapWidth - 1; col++)
                {
                    map[row, col] = -1;
                }
            }

            for (int row = 0; row < mapHeight; row++)
            {
                map[row, 0] = int.MaxValue;
                map[row, mapWidth - 1] = int.MaxValue;
            }<commit_msg>GetByproductColor method in Engine<commit_after>﻿using System.Runtime.Remoting.Channels;

namespace EasterFarm.GameLogic
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;

    using Contracts;
    using EasterFarm.Common;
    using EasterFarm.Models;
    using EasterFarm.Models.Contracts;
    using EasterFarm.Models.FarmObjects.Food;
    using EasterFarm.Models.FarmObjects.Animals;
    using EasterFarm.Models.FarmObjects.Byproducts;
    using EasterFarm.Models.MarketPlace;
    using EasterFarm.Models.Presents;

    public class Engine
    {
        private FarmManager farmManager;
        private Market market;
        private PresentFactory presentFactory;

        private readonly HashSet<GameObject> gameObjects;
        private readonly HashSet<FarmFood> farmFoods;
        private readonly HashSet<Livestock> livestocks;
        private readonly HashSet<Villain> villains;
        private readonly HashSet<Byproduct> byproducts;

        public Engine(IRenderer renderer, IUserKeyboardInput userInput)
        {
            this.Renderer = renderer;
            this.UserInput = userInput;
            this.gameObjects = new HashSet<GameObject>();
            this.farmFoods = new HashSet<FarmFood>();
            this.livestocks = new HashSet<Livestock>();
            this.villains = new HashSet<Villain>();
            this.byproducts = new HashSet<Byproduct>();
        }

        internal IRenderer Renderer { get; private set; }

        internal IUserKeyboardInput UserInput { get; private set; }

        public void AddGameObject(GameObject gameObject)
        {
            var farmFood = gameObject as FarmFood;
            if (farmFood != null)
            {
                this.farmFoods.Add(farmFood);
            }

            var livestock = gameObject as Livestock;
            if (livestock != null)
            {
                this.livestocks.Add(livestock);
            }

            var villain = gameObject as Villain;
            if (villain != null)
            {
                this.villains.Add(villain);
            }

            var byproduct = gameObject as Byproduct;
            if (byproduct != null)
            {
                this.byproducts.Add(byproduct);
            }

            this.gameObjects.Add(gameObject);
        }

        public void Start()
        {
            this.SetInitialGameObjects();

            while (true)
            {
                this.Renderer.RenderAll();

                Thread.Sleep(200);

                this.UserInput.ProcessInput();

                this.Renderer.ClearRenderer();

                foreach (var gameObject in gameObjects)
                {
                    this.Renderer.EnqueueForRendering(gameObject);
                }

                this.Seek(livestocks, typeof(FarmFood));
                this.Seek(villains, typeof(Livestock));

                this.Collide(livestocks, farmFoods);
                this.Collide(villains, livestocks);

                ClearCollections();
            }
        }

        private void ClearCollections()
        {
            farmFoods.RemoveWhere(ff => ff.IsDestroyed);
            livestocks.RemoveWhere(ls => ls.IsDestroyed);
            villains.RemoveWhere(v => v.IsDestroyed);
            byproducts.RemoveWhere(bp => bp.IsDestroyed);
            gameObjects.RemoveWhere(go => go.IsDestroyed);
        }

        private void Collide(IEnumerable<Animal> colliders, IEnumerable<GameObject> destroyables)
        {
            foreach (var destroyable in destroyables)
            {
                foreach (var collider in colliders)
                {
                    if (destroyable.TopLeft == collider.TopLeft)
                    {
                        destroyable.IsDestroyed = true;

                        Byproduct byproduct = collider.Produce((GetByproductColor(destroyable)));
                        if (byproduct != null)
                        {
                            this.AddGameObject(byproduct);
                        }
                    }
                }
            }
        }

        private ByproductColor GetByproductColor(GameObject gameObject)
        {
            FarmFood farmFood = gameObject as FarmFood;
            if (farmFood != null)
            {
                return farmFood.GetColor();
            }

            return ByproductColor.None;
        }

        private void Seek(IEnumerable<Animal> chasers, Type targetType)
        {
            var map = this.CreateTopographicMap(targetType);
            foreach (var chaser in chasers)
            {
                chaser.Move(map);
            }
        }

        private int[,] CreateTopographicMap(Type gameObjectType)
        {
            int[,] map = new int[Constants.WorldRows, (int)(Constants.WorldCols * Constants.LeftRightScreenRatio) + 1];
            int mapHeight = map.GetLength(0);
            int mapWidth = map.GetLength(1);

            for (int row = 1; row < mapHeight - 1; row++)
            {
                for (int col = 1; col < mapWidth - 1; col++)
                {
                    map[row, col] = -1;
                }
            }

            for (int row = 0; row < mapHeight; row++)
            {
                map[row, 0] = int.MaxValue;
                map[row, mapWidth - 1] = int.MaxValue;
            }
{'fraction_non_alphanumeric': 0.08460156914642215, 'fraction_numerical': 0.0026467530012288494, 'mean_word_length': 1.521448999046711, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 39, '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': '6332454', 'n_tokens_mistral': 2882, 'n_tokens_neox': 2658, 'n_words': 575}