<commit_before>﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using Driftr.Properties;

namespace Driftr
{
    public partial class Driftr : Form
    {
        private const float screenScale = 3.0f;
        private Graphics _graphics;
        private Bitmap _backbuffer;
        private Size _bufferSize;
        private readonly GameTimer _timer = new GameTimer();

        private readonly bool[] _left = new bool[2];
        private readonly bool[] _right = new bool[2];
        private readonly bool[] _up = new bool[2];
        private readonly bool[] _down = new bool[2];

        private readonly float[] _steerings = new float[2]; // -1.0 is left, 0 is center,  1.0 is right.
        private readonly float[] _throttles = new float[2]; // 0 is coasting, 1 is full throttle.
        private readonly float[] _brakes = new float[2]; // 0 is no breaks, 1 is full breaks.

        private readonly Vehicle[] _vehicles = { new Vehicle(), new Vehicle() };

        public Driftr()
        {
            InitializeComponent();
            Application.Idle += Application_Idle;
            screen.Paint += screen_Paint;
            screen.MouseUp += screen_MouseUp;
            KeyUp += Driftr_KeyUp;
            KeyDown += Driftr_KeyDown;

            Init(screen.Size);
            pictureBox1.Parent = screen;
            pictureBox2.Parent = screen;

            // brandstof
            InitTimer();
            pictureBox1.Image = Resources.dashboard_5_red;
            pictureBox2.Image = Resources.dashboard_5_yellow;
        }

        void screen_MouseUp(object sender, MouseEventArgs e)
        {
            //Debug.WriteLine("Mouse: X={0}, Y={1}", e.X, e.Y);
            //
            //var p = ((Bitmap)screen.BackgroundImage).GetPixel(e.X, e.Y);
            //Debug.WriteLine(p);
            //
            //var pos = VehicleRelativePosition(0);
            //Debug.WriteLine("Vehicle: X={0}, Y={1}", (int)pos.X, (int)pos.Y);
            //
            //var p2 = ((Bitmap)screen.BackgroundImage).GetPixel((int)pos.X, (int)pos.Y);
            //Debug.WriteLine(p2);
            //
            //Debug.WriteLine("=======");
        }

        private void Init(Size size)
        {
            //screen.BackgroundImage = Resources.MapBackground;
            //
            //screen.SizeMode = PictureBoxSizeMode.Normal;
            //screen.BackgroundImageLayout = ImageLayout.None;
            //screen.Image = null;

            _bufferSize = size;
            _backbuffer = new Bitmap(_bufferSize.Width, _bufferSize.Height);
            _graphics = Graphics.FromImage(_backbuffer);
            
            _timer.GetETime();

            _vehicles[0].Setup(new Vector(3, 8) / 2.0f, 5, Resources.CarRed);
            _vehicles[0].SetLocation(new Vector(-117.5f, 3.5f), 0);

            _vehicles[1].Setup(new Vector(3, 8) / 2.0f, 5, Resources.CarYellow);
            _vehicles[1].SetLocation(new Vector(-103.0f, 3.5f), 0);
        }

        private void Render(Graphics g)
        {
            _graphics.Clear(Color.Transparent);
            _graphics.ResetTransform();
            _graphics.ScaleTransform(screenScale, -screenScale);
            _graphics.TranslateTransform(
                _bufferSize.Width / 2.0f / screenScale,
                -_bufferSize.Height / 2.0f / screenScale);

            DrawScreen();

            g.DrawImage(
                _backbuffer,
                new Rectangle(0, 0, _bufferSize.Width, _bufferSize.Height),
                0,
                0,
                _bufferSize.Width,
                _bufferSize.Height,
                GraphicsUnit.Pixel);
        }

        private void DrawScreen()
        {
            _vehicles[0].Draw(_graphics, _bufferSize);
            _vehicles[1].Draw(_graphics, _bufferSize);
            speedLabelRed.Text = Convert.ToString(Math.Round(_vehicles[0].DisplaySpeed));
            speedLabelYellow.Text = Convert.ToString(Math.Round(_vehicles[1].DisplaySpeed));

            // todo: figure out if the position is correct.
            //var pos = VehicleRelativePosition(0);
            //Debug.WriteLine("X: {0}, Y: {1}", (int)pos.X, (int)pos.Y);
            //Color p = ((Bitmap)screen.BackgroundImage).GetPixel((int)pos.X, (int)pos.Y);
            //Debug.WriteLine(p);

            //Debug.WriteLine(VehicleRelativePosition(0));
        }

        private Vector VehicleRelativePosition(int vehicle)
        {
            return _vehicles[vehicle].RelativePosition(_bufferSize.Width, _bufferSize.Height, screenScale);
        }

        private void DoFrame()
        {
            float etime = _timer.GetETime();

            ProcessInput();

            // Apply vehicle controls.
            for (int i = 0; i < _vehicles.Length; i++)
            {
                _vehicles[i].SetSteering(_steerings[i]);
                _vehicles[i].SetThrottle(_throttles[i]);
                _vehicles[i].SetBrakes(_brakes[i]);

                _vehicles[i].Update(etime);<commit_msg>Start with checkpoints.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Driftr.Properties;

namespace Driftr
{
    public partial class Driftr : Form
    {
        private const float screenScale = 3.0f;
        private Graphics _graphics;
        private Bitmap _backbuffer;
        private Size _bufferSize;
        private readonly GameTimer _timer = new GameTimer();

        private readonly bool[] _left = new bool[2];
        private readonly bool[] _right = new bool[2];
        private readonly bool[] _up = new bool[2];
        private readonly bool[] _down = new bool[2];

        private readonly float[] _steerings = new float[2]; // -1.0 is left, 0 is center,  1.0 is right.
        private readonly float[] _throttles = new float[2]; // 0 is coasting, 1 is full throttle.
        private readonly float[] _brakes = new float[2]; // 0 is no breaks, 1 is full breaks.

        private readonly Vehicle[] _vehicles = { new Vehicle(), new Vehicle() };
        private readonly Dictionary<Vehicle, bool[]> _vehicleCheckpoints = new Dictionary<Vehicle, bool[]>();

        private readonly List<Color> _checkpoints = new List<Color>
            {
                Color.FromArgb(255, 244, 0),
                Color.FromArgb(0, 162, 232),
                Color.FromArgb(191, 126, 90),
                Color.FromArgb(255, 127, 39),
                Color.FromArgb(239, 28, 36),
                Color.FromArgb(163, 73, 164),
                Color.FromArgb(255, 186, 214)
            };

        public Driftr()
        {
            InitializeComponent();
            Application.Idle += Application_Idle;
            screen.Paint += screen_Paint;
            screen.MouseUp += screen_MouseUp;
            KeyUp += Driftr_KeyUp;
            KeyDown += Driftr_KeyDown;

            foreach (Vehicle v in _vehicles)
            {
                _vehicleCheckpoints.Add(v, new bool[8]);
            }

            Init(screen.Size);
            pictureBox1.Parent = screen;
            pictureBox2.Parent = screen;

            // brandstof
            InitTimer();
            pictureBox1.Image = Resources.dashboard_5_red;
            pictureBox2.Image = Resources.dashboard_5_yellow;
        }

        void screen_MouseUp(object sender, MouseEventArgs e)
        {
            //Debug.WriteLine("Mouse: X={0}, Y={1}", e.X, e.Y);
            //
            //var p = ((Bitmap)screen.BackgroundImage).GetPixel(e.X, e.Y);
            //Debug.WriteLine(p);
            //
            //var pos = VehicleRelativePosition(0);
            //Debug.WriteLine("Vehicle: X={0}, Y={1}", (int)pos.X, (int)pos.Y);
            //
            //var p2 = ((Bitmap)screen.BackgroundImage).GetPixel((int)pos.X, (int)pos.Y);
            //Debug.WriteLine(p2);
            //
            //Debug.WriteLine("=======");
        }

        private void Init(Size size)
        {
            //screen.BackgroundImage = Resources.MapBackground;
            //
            //screen.SizeMode = PictureBoxSizeMode.Normal;
            //screen.BackgroundImageLayout = ImageLayout.None;
            //screen.Image = null;

            _bufferSize = size;
            _backbuffer = new Bitmap(_bufferSize.Width, _bufferSize.Height);
            _graphics = Graphics.FromImage(_backbuffer);
            
            _timer.GetETime();

            _vehicles[0].Setup(new Vector(3, 8) / 2.0f, 5, Resources.CarRed);
            _vehicles[0].SetLocation(new Vector(-117.5f, 3.5f), 0);

            _vehicles[1].Setup(new Vector(3, 8) / 2.0f, 5, Resources.CarYellow);
            _vehicles[1].SetLocation(new Vector(-103.0f, 3.5f), 0);
        }

        private void Render(Graphics g)
        {
            _graphics.Clear(Color.Transparent);
            _graphics.ResetTransform();
            _graphics.ScaleTransform(screenScale, -screenScale);
            _graphics.TranslateTransform(
                _bufferSize.Width / 2.0f / screenScale,
                -_bufferSize.Height / 2.0f / screenScale);

            DrawScreen();

            g.DrawImage(
                _backbuffer,
                new Rectangle(0, 0, _bufferSize.Width, _bufferSize.Height),
                0,
                0,
                _bufferSize.Width,
                _bufferSize.Height,
                GraphicsUnit.Pixel);
        }

        private void DrawScreen()
        {
            _vehicles[0].Draw(_graphics, _bufferSize);
            _vehicles[1].Draw(_graphics, _bufferSize);
            speedLabelRed.Text = Convert.ToString(Math.Round(_vehicles[0].DisplaySpeed));
            speedLabelYellow.Text = Convert.ToString(Math.Round(_vehicles[1].DisplaySpeed));

            //ProcessCheckpoints();
        }

        private void ProcessCheckpoints()
        {
            var background = (Bitmap)screen.BackgroundImage;
            for (int i = 0; i < _vehicles.Length; i++)
            {
                var v = _vehicles[i];
                var pos = VehicleRelativePosition(i);

                var c = background.GetPixel((int)pos.X, (int)pos.Y);
                if (_checkpoints.Any(x => x == c))
                {
                    int index = _checkpoints.IndexOf(c);

                    // Check if previous checkpoint was hit.
                    int previous = index - 1;
                    if (previous > 0 && _vehicleCheckpoints[v][previous])
                    {
                        _vehicleCheckpoints[v][index] = true;
                    }
                }
            }
        }

        private Vector VehicleRelativePosition(int vehicle)
        {
            return _vehicles[vehicle].RelativePosition(_bufferSize.Width, _bufferSize.Height, screenScale);
        }

        private void DoFrame()
        {
            float etime = _timer.GetETime();

            ProcessInput();

            // Apply vehicle controls.
            for (int i = 0; i < _vehicles.Length; i++)
            {
                _vehicles[i].SetSteering(_steerings[i]);
                _vehicles[i].SetThrottle(_throttles[i]);
                _vehicles[i].SetBrakes(_brakes[i]);

                _vehicles[i].Update(etime);
{'fraction_non_alphanumeric': 0.11572753209700427, 'fraction_numerical': 0.018366619115549217, 'mean_word_length': 1.7265435099659698, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, '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': '6303638', 'n_tokens_mistral': 3249, 'n_tokens_neox': 3040, 'n_words': 662}