Software dose AI play game just from HDMI signal (just seeing display)
proposed by Prof.Satoshi Nishimura image00 / Jichi Med Univ(JAPAN)

Specs: AI plays game in real time just using HDMI signals from playstation

E-mail: If interested (snishi-tky(a-mark)ninus.ocn.ne.jp ) image00

Back to top image00

PAC-man (AI plays with Montecarlo methods for parameter adjusts, without any learning)



RAIDEN, AI plays (without learnng)



RAIDEN, AI plays (without learnng)



RAIDEN, AI plays (without learnng)



		
            using OpenCvSharp;
            using System;
            using System.IO;
            
            using System.Drawing;
            using System.Windows.Forms;
            using MccDaq;
            using ErrorDefs;
            using DigitalIO;
            using AnalogIO;
            
            using System.Threading.Tasks;
            
            //using Counters;
            //using System.Collections;
            //using System.ComponentModel;
            //using System.Data;
            //using System.Diagnostics;
            
            
            namespace WindowsFormsApp2
            {
                public partial class Form1 : Form
                {
                    public int videoON = 0;            //  capture Camera found
                    public int controllerON = 0;      // communication relay board found
                    public int playON = 0;
                    public int conttimes = 0;
            
                    // for DQ5
                    public int fightcount = 0;
                    public int fightmode = 0;
                    public int restcount = 0;
                    public int restmode = 0;
                    public int enemycount = 0;
                    public int medmode = 0;
                    public int medtime = 0;
                    public int medcount = 0, medenemy = 0;
                    public int blackcheck = 0;
                    public int prevrest = 0;
                    public int prevfight = 0;
            
                    // for RAIDEN
                    public int prev_jikiX = 0;
                    public int prev_jikiY = 0;
                    public int prev_jikiX_A = 0;
                    public int prev_jikiY_A = 0;
                    public int prev_jikiX_B = 0;
                    public int prev_jikiY_B = 0;
                    public int prev_jikiX_C = 0;
                    public int prev_jikiY_C = 0;
                    public int prev_jikiX_D = 0;
                    public int prev_jikiY_D = 0;
                    public int offy = 20;
                    public int jikiX = 320;
                    public int jikiY = 200;
                    public int jikiX_A = 320;
                    public int jikiY_A = 200;
                    public int jikiX_B = 320;
                    public int jikiY_B = 200;
                    public int jikiX_C = 320;
                    public int jikiY_C = 200;
                    public int jikiX_D = 320;
                    public int jikiY_D = 200;
            
                    public int maxtime = 40;
                    public int prevtime = 40;
                    public int nowtime = 40;
                    public int oldellapsedtime = 40;
            
                    public int beforetime;
            
                    public int startflag = 0;
                    public int supportflag = 0;
                    public int conflag = 0;
                    public int x1 = 0, y1 = 0;  //previous move
            
                    public byte[] srcData = new byte [3*640*480];
                    public byte[] dstData = new byte[3 * 640 * 480];
                    public byte[] dstDataR = new byte[3 * 640 * 480];
                    public byte[] dstDataG = new byte[3 * 640 * 480];
                    public byte[] dstDataB = new byte[3 * 640 * 480];
                    public byte[] prevData0 = new byte[3 * 640 * 480];
                    public byte[] prevData1 = new byte[3 * 640 * 480];
                    public byte[] prevData2 = new byte[3 * 640 * 480];
            
                    public int[] diffsum=new int [100];
                    public int[] x_history = new int[100];
                    public int[] y_history = new int[100];
                    public int[] x_history_A = new int[100];
                    public int[] y_history_A = new int[100];
                    public int[] x_history_B = new int[100];
                    public int[] y_history_B = new int[100];
                    public int[] x_history_C = new int[100];
                    public int[] y_history_C = new int[100];
                    public int[] x_history_D = new int[100];
                    public int[] y_history_D = new int[100];
            
                    public int[] UD_history = new int[100];
                    public int[] RL_history = new int[100];
            
                    Bitmap canvasLEFT = new Bitmap(640, 480);
            
                    public int sl1=50;
                    public int sl2 = 50;
                    public int sl3 = 50;
                    public int sl4 = 50;
                    public int sl5 = 50;
                    public int sl6 = 20;
                    public int sl7 = 80;
            
                    //for raiden
                    public int[] prevX = new int[10];
                    public int[] prevY = new int[10];
                    public int[] targetX = new int[1000];
                    public int[] targetY = new int[1000];
                    public int[] targetX_A = new int[1000];
                    public int[] targetY_A = new int[1000];
                    public int[] targetX_B = new int[1000];
                    public int[] targetY_B = new int[1000];
                    public int[] targetX_C = new int[1000];
                    public int[] targetY_C = new int[1000];
                    public int[] targetX_D = new int[1000];
                    public int[] targetY_D = new int[1000];
                    public int[] targetX_E = new int[1000];
                    public int[] targetY_E = new int[1000];
                    public int[] targetX_F = new int[1000];
                    public int[] targetY_F = new int[1000];
                    public int[] targetNEARX = new int[1000];
                    public int[] targetNEARY = new int[1000];
            
                    public int[] filtjikiposX = new int[1000];
                    public int[] filtjikiposY = new int[1000];
                    public int[] filtjikiposX_A = new int[1000];
                    public int[] filtjikiposY_A = new int[1000];
                    public int[] filtjikiposX_B = new int[1000];
                    public int[] filtjikiposY_B = new int[1000];
                    public int[] filtjikiposX_C = new int[1000];
                    public int[] filtjikiposY_C = new int[1000];
                    public int[] filtjikiposX_D = new int[1000];
                    public int[] filtjikiposY_D = new int[1000];
                    public int[] filtjikiposX_E = new int[1000];
                    public int[] filtjikiposY_E = new int[1000];
                    public int[] filtjikiposX_F = new int[1000];
                    public int[] filtjikiposY_F = new int[1000];
                    public int[] filtjikiposNEARX = new int[1000];
                    public int[] filtjikiposNEARY = new int[1000];
            
                    public int[] tamaposX = new int[10000];
                    public int[] tamaposY = new int[10000];
                    public int[] filt_tamaposX = new int[10000];
                    public int[] filt_tamaposY = new int[10000];
                    public int[] tamaposX2 = new int[10000];
                    public int[] tamaposY2 = new int[10000];
                    public int[] filt_tamaposX2 = new int[10000];
                    public int[] filt_tamaposY2 = new int[10000];
            
                    public int[] controllog = new int[10000];
                    public int controllast = 0;
                    public int logcursor = 0;
            
                    public int ang1 = 0;
            
                    // DIRECt HDMI capturing OPEN CV
                    VideoCapture VideoCapture1;
                    Graphics Graphics1;
                    Mat Mat1;
            
                    // RelayBOARD JOYSTICK
                    int NumPorts, NumBits, FirstBit;
                    int PortType, ProgAbility;
                    string PortName;
                    MccDaq.DigitalPortType PortNum;
                    MccDaq.DigitalPortDirection Direction;
                    DigitalIO.clsDigitalIO DioProps = new DigitalIO.clsDigitalIO();
            
                    // Create a new MccBoard object for Board 0
                    MccDaq.MccBoard DaqBoard = new MccDaq.MccBoard(0);
                    MccDaq.Range Range;
                    private int ADResolution, NumAIChans;
                    private ushort[] ADData;        //  dimension an array to hold the input values
                    private IntPtr MemHandle = IntPtr.Zero;
                    AnalogIO.clsAnalogIO AIOProps = new AnalogIO.clsAnalogIO();
                    private MccDaq.MccBoard daqBoardon1 = null;
            
                    //////////////////////
                    ///MAIN ENTRY POINT OPEN//CLOSE
                    /////////////////////////
                    public Form1()
                    {
                        InitializeComponent();
                    }
            
                    private void Form1_Load(object sender, EventArgs e)    // INITIALIZE  //
                    {
            
            
                        for(int i = 0; i < 10000; i++)
                        {
                            controllog[i] = 0;
                        }
                        controllast = 0;
            
                        DateTime dt3 = DateTime.Now;
                        prevtime = (dt3.Hour * 60 + dt3.Minute) * 60 + dt3.Second;
                        nowtime = (dt3.Hour * 60 + dt3.Minute) * 60 + dt3.Second;
                        beforetime=((dt3.Minute*60)+dt3.Second)*1000 + dt3.Millisecond;
            
            
                        numericUpDown1.Minimum = 0;
                        numericUpDown1.Maximum = 5; 
                        numericUpDown1.Value = 1;        // camera
                        numericUpDown1.TextAlign = HorizontalAlignment.Right;
                        numericUpDown1.Increment = 1;
            
                        //////////// RAIDEN PARAMETERS ///////////////////
            
                        numericUpDown2.Minimum = 0;
                        numericUpDown2.Maximum = 10;
                        numericUpDown2.Value = 1;        // Z shift
                        numericUpDown2.TextAlign = HorizontalAlignment.Right;
                        numericUpDown2.Increment = 1;
            
                        numericUpDown3.Minimum = -5;
                        numericUpDown3.Maximum = 5;
                        numericUpDown3.Value = -1;        // X shift
                        numericUpDown3.TextAlign = HorizontalAlignment.Right;
                        numericUpDown3.Increment = 1;
            
                        numericUpDown4.Minimum = 0;
                        numericUpDown4.Maximum = 1000;
                        numericUpDown4.Value = 160;        // threshold into black
                        numericUpDown4.TextAlign = HorizontalAlignment.Right;
                        numericUpDown4.Increment = 2;
            
                        numericUpDown5.Minimum = 0;
                        numericUpDown5.Maximum = 10;
                        numericUpDown5.Value = 7;        // previous 
                        numericUpDown5.TextAlign = HorizontalAlignment.Right;
                        numericUpDown5.Increment = 1;
            
                        numericUpDown6.Minimum = 0;
                        numericUpDown6.Maximum = 20;
                        numericUpDown6.Value = 3;        // delay of joystick
                        numericUpDown6.TextAlign = HorizontalAlignment.Right;
                        numericUpDown6.Increment = 1;
            
                        numericUpDown8.Minimum = 0;
                        numericUpDown8.Maximum = 180;
                        numericUpDown8.Value = 23;        // Angle adujust
                        numericUpDown8.TextAlign = HorizontalAlignment.Right;
                        numericUpDown8.Increment = 1;
            
                        //////////// YOUR GAME? ///////////////////
            
                        numericUpDown7.Minimum = 1;
                        numericUpDown7.Maximum = 10;
                        numericUpDown7.Value = 1;        // delay of joystick
                        numericUpDown7.TextAlign = HorizontalAlignment.Right;
                        numericUpDown7.Increment = 1;
                        label38.Text = "YOUR GAME??";
            
                        hScrollBar1.Value = 80;
                        hScrollBar2.Value = 35;
                        hScrollBar3.Value = 66;
                        hScrollBar4.Value = 60;
                        hScrollBar5.Value = 65;
                        hScrollBar6.Value = 30;
                        hScrollBar7.Value = 80;
            
                        label46.Text = hScrollBar1.Value.ToString();
                        label47.Text = hScrollBar2.Value.ToString();
                        label48.Text = hScrollBar3.Value.ToString();
                        label49.Text = hScrollBar4.Value.ToString();
                        label50.Text = hScrollBar5.Value.ToString();
                        label55.Text = hScrollBar6.Value.ToString();
                        label57.Text = hScrollBar7.Value.ToString();
            
            
                        button17.BackColor = Color.LightGray;   //SUPPORTmode
                        button16.BackColor = Color.LightGray;   //AUTOPLAYmode
            
                        timer1.Enabled = false;
                        timer1.Interval = 5;                   // camera capturing interval
            
                        timer2.Enabled = false;
                        timer2.Interval = 1;
            
                        timer3.Enabled = false;
                        timer3.Interval = 100;
            
                        ///////////////////////////////////////////////////////////////////
                        //////////////// INIT /////////////////////////////////////////////
                        ///////////////////////////////////////////////////////////////////
                        for (int i = 0; i < 100; i++)
                        {
                            diffsum[i] = 0;
                            x_history[i] = 0;
                            y_history[i] = 0;
                            x_history_A[i] = 0;
                            y_history_A[i] = 0;
                            x_history_B[i] = 0;
                            y_history_B[i] = 0;
                            x_history_C[i] = 0;
                            y_history_C[i] = 0;
                            x_history_D[i] = 0;
                            y_history_D[i] = 0;
                            UD_history[i] = 0;
                            RL_history[i] = 0;
                        }
            
            
                        for (int i = 0; i < 10; i++)        // Reset previous jiki position
                        {
                            prevX[i] = 320;
                            prevY[i] = 240;
                        }
                        
                        for(int j = 0; j < 3 * 640 * 400; j++)   //Reset previous captured images to GRAY
                        {
                            prevData0[j] = 127;
                            prevData1[j] = 127;
                            prevData2[j] = 127;
                            srcData[j] = 127;
                        }
            
                    }
            
                    private void Form1_FormClosing(object sender, FormClosingEventArgs e)   //QUIT & close Cameras
                    {
                        //// END END ///////////////////////////////////
                        /// USE OPEN CV only for LeftVIEW (HDMI capture)
                        if (videoON != 0)
                        {
                            VideoCapture1.Dispose();
                            Graphics1.Dispose();
                        }
            
                        if (controllerON > 0)
                        {
                           
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);   //1,2,4,8
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                        }
                    }
            
                    //////////////////////
                    /// SYSTEMM / CAM & MCCDAQ
                    /////////////////////////
            
                    private void button1_Click(object sender, EventArgs e)    // LOAD CAPTURE HDMI Camera  //
                    {
                        ///////////////////////////////////////////////////////////////////
                        //////////////// CAMERA 1 LOAD /////////////////////////////////////////////
                        ///////////////////////////////////////////////////////////////////
                        if (numericUpDown1.Value == 0) { VideoCapture1 = new VideoCapture(0); }
                        if (numericUpDown1.Value == 1) { VideoCapture1 = new VideoCapture(1); }
                        if (numericUpDown1.Value == 2) { VideoCapture1 = new VideoCapture(2); }
                        if (numericUpDown1.Value == 3) { VideoCapture1 = new VideoCapture(3); }
                        if (numericUpDown1.Value == 4) { VideoCapture1 = new VideoCapture(4); }
                        if (numericUpDown1.Value == 5) { VideoCapture1 = new VideoCapture(5); }
                        if (!VideoCapture1.IsOpened())
                        {
                            MessageBox.Show("no camera");
                            videoON = 0;
                            timer1.Enabled = false;
                        }
                        else
                        {
                            Graphics1 = pictureBox1.CreateGraphics();
                            videoON++;
                            using (Mat1 = new Mat(0, 0, MatType.CV_8UC3))
                            {
                                VideoCapture1.Read(Mat1);
                                using (Bitmap bitmap1 = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(Mat1))
                                {
                                    label1.Text = Mat1.Cols.ToString();
                                    label2.Text = Mat1.Rows.ToString();
                                }
                            }
                            timer1.Enabled = true;
                        }
                    }
                    private void button2_Click(object sender, EventArgs e)/////////////SAVE BMP DATA for further analysis //////////////
                    {
                        //////////////SAVE BMP DATA for further analysis //////////////
                        if (videoON != 0)
                        {
                            using (Mat1 = new Mat(0, 0, MatType.CV_8UC3))
                            {
                                VideoCapture1.Read(Mat1);
                                using (Bitmap bitmap1 = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(Mat1))
                                {
                                    Random r1 = new System.Random();
                                    int filenum = r1.Next(0, 100);
                                    Graphics1.DrawImage(bitmap1, 0, 0, Mat1.Cols, Mat1.Rows);
                                    bitmap1.Save(@"D:\HDMIcapture\HDMIcap" + filenum.ToString("D2") + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                                }
                            }
                        }
                    }
            
                    public void InitUL()  /////////// INITIATE MCC DAQ SERVICE ///////////////////////////
                    {
                        ///////////// INITIATE MCC DAQ SERVICE ///////////////////////////
                        MccDaq.ErrorInfo ULStat;
                        //  Initiate error handling
                        //   activating error handling will trap errors like
                        //   bad channel numbers and non-configured conditions.
                        //   Parameters:
                        //     MccDaq.ErrorReporting.PrintAll :all warnings and errors encountered will be printed
                        //     MccDaq.ErrorHandling.StopAll   :if an error is encountered, the program will stop
            
                        clsErrorDefs.ReportError = MccDaq.ErrorReporting.PrintAll;
                        clsErrorDefs.HandleError = MccDaq.ErrorHandling.StopAll;
                        ULStat = MccDaq.MccService.ErrHandling
                            (ErrorReporting.PrintAll, ErrorHandling.StopAll);
                    }
                    private void button4_Click(object sender, EventArgs e)   // INIT communication MCC=DAQ board
                    {
                        //////////// INITIALIZE DAQ BOARD /////////
                        InitUL();
                        MccDaq.DaqDeviceManager.IgnoreInstaCal();
                        foreach (MccDaq.MccBoard daqBoard2 in comboBox1.Items)
                        {
                            MccDaq.DaqDeviceManager.ReleaseDaqDevice(daqBoard2);
                        }
                        MccDaq.DaqDeviceDescriptor[] inventory = MccDaq.DaqDeviceManager.GetDaqDeviceInventory(MccDaq.DaqDeviceInterface.Any);
                        int numDevDiscovered = inventory.Length;
                        comboBox1.Items.Clear();
                        label12.Text = numDevDiscovered + " DAQ Device(s) Discovered";
                        if (numDevDiscovered > 0)
                        {
                            for (int boardNum = 0; boardNum < numDevDiscovered; boardNum++)
                            {
                                try
                                {
                                    //        BoardNum			: board number to be assigned to the specified DAQ device
                                    //        DeviceDescriptor	: device descriptor of the DAQ device 
                                    MccDaq.MccBoard daqBoard2 = MccDaq.DaqDeviceManager.CreateDaqDevice(boardNum, inventory[boardNum]);
                                    comboBox1.Items.Add(daqBoard2);
                                }
                                catch (ULException ule)
                                {
                                    label12.Text = "Error occured: " + ule.Message;
                                }
                            }
                        }
                    }
                    private void button5_Click(object sender, EventArgs e)   // FIND communication MCC=DAQ board
                    {
                        ////////////// assign USB DAQ  board ///////
                        if (daqBoardon1 == null)
                        {
                            daqBoardon1 = (MccDaq.MccBoard)comboBox1.SelectedItem;
                            daqBoardon1.FlashLED();
                            label12.Text = " / " + daqBoardon1.ToString();
                            NumPorts = DioProps.FindPortsOfType(daqBoardon1, PortType, out ProgAbility, out PortNum, out NumBits, out FirstBit);
                            PortType = clsDigitalIO.PORTOUT;
                            Direction = MccDaq.DigitalPortDirection.DigitalOut;
                            daqBoardon1.DConfigPort(PortNum, Direction);
                            //determine if digital port exists, its capabilities, etc
                            NumPorts = DioProps.FindPortsOfType(daqBoardon1, PortType, out ProgAbility, out PortNum, out NumBits, out FirstBit);
                            PortType = clsDigitalIO.PORTOUT;
                            Direction = MccDaq.DigitalPortDirection.DigitalOut;
                            daqBoardon1.DConfigPort(PortNum, Direction);
                            if (NumPorts == 0)
                            {
                                label12.Text = "NO AD/DA board";
                            }
                            else
                            {
                                PortName = PortNum.ToString();
                                label12.Text = daqBoardon1.BoardNum.ToString() + " / " + daqBoardon1.ToString(); ;//
                            }
                            controllerON++;
                        }
                        daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                        daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);
            
                        /////////////////////////////////////// ANALOG IN ////////////////////////////////////
            
                        MccDaq.TriggerType DefaultTrig;
                        InitUL();
                        int NumPoints = 100;
                        int lowch;
                        // determine the number of analog channels and their capabilities
                        int ChannelType = clsAnalogIO.ANALOGINPUT;
                        NumAIChans = AIOProps.FindAnalogChansOfType(DaqBoard, ChannelType, out ADResolution, out Range, out lowch, out DefaultTrig);
                        if (NumAIChans == 0)
                        {
                            label31.Text = "Board " + DaqBoard.BoardNum.ToString() +
                                " does not have analog input channels.";
                        }
                        else
                        {
                            // Check the resolution of the A/D data and allocate memory accordingly
                            // set aside memory to hold 16-bit data
                            ADData = new ushort[NumPoints];
                            MemHandle = MccDaq.MccService.WinBufAllocEx(NumPoints);
                            label31.Text = "Board " + DaqBoard.BoardNum.ToString() + " AIN" + NumAIChans.ToString() + "Range " + Range.ToString() + ".";
                            label33.Text = "bit" + ADResolution.ToString();
                        }
                        MccDaq.ErrorInfo ULStat;
                        MccDaq.ScanOptions Options;
                        int Rate;
                        int Count;
                        Count = NumPoints;  //  total number of data points to collect
                        Rate = 100;
                        //  return data as 12-bit values (ignored for 16-bit boards)
                        Options = MccDaq.ScanOptions.ConvertData;
            
                        // READ READ READ READ READ READ READ ////////////////////////////////////////////
                        ULStat = DaqBoard.AInScan(0, 3, Count, ref Rate, Range, MemHandle, Options);
            
                        //  Transfer the data from the memory buffer set up by Windows to an array
                        ULStat = MccDaq.MccService.WinBufToArray(MemHandle, ADData, 0, 4);
            
                        label34.Text = ADData[0].ToString("D");
                        label35.Text = ADData[1].ToString("D");
                        label36.Text = ADData[2].ToString("D");
                        label37.Text = ADData[3].ToString("D");
                    }        
                    private void button19_Click(object sender, EventArgs e)    //Analog in CHannel check     
                    {
            
                        // AIN CHECK BOARD  //
                        MccDaq.TriggerType DefaultTrig;
                        //InitUL();
                        int NumPoints = 4;
                        int lowch;
            
                        // determine the number of analog channels and their capabilities
                        int ChannelType = clsAnalogIO.ANALOGINPUT;
                        NumAIChans = AIOProps.FindAnalogChansOfType(DaqBoard, ChannelType, out ADResolution, out Range, out lowch, out DefaultTrig);
                        ADData = new ushort[NumPoints];
                        MemHandle = MccDaq.MccService.WinBufAllocEx(NumPoints);
            
                        label31.Text = "Board " + DaqBoard.BoardNum.ToString() +
                                " AIN" + NumAIChans.ToString() + "Range " + Range.ToString() + ".";
                        label33.Text = "bit" + ADResolution.ToString();
            
                        MccDaq.ErrorInfo ULStat;
                        MccDaq.ScanOptions Options;
                        int Rate;
                        int Count;
                        Count = NumPoints;  //  total number oaf data points to collect
                        Rate = 100;           //  return data as 12-bit values (ignored for 16-bit boards)
                        Options = MccDaq.ScanOptions.ConvertData;
                        ULStat = DaqBoard.AInScan(0, 3, Count, ref Rate, Range, MemHandle, Options);
                        //  Transfer the data from the memory buffer set up by Windows to an array
                        ULStat = MccDaq.MccService.WinBufToArray(MemHandle, ADData, 0, 4);
            
                        label34.Text = ADData[0].ToString("D");
                        label35.Text = ADData[1].ToString("D");
                        label36.Text = ADData[2].ToString("D");
                        label37.Text = ADData[3].ToString("D");
                        label34.Refresh();
                        label35.Refresh();
                        label36.Refresh();
                        label37.Refresh();
                    }
                   
                    // timer2 for camera2 capture
                    private void timer2_Tick(object sender, EventArgs e)
                    {
                        timer2.Stop();
                        timer2.Enabled = false;
                        if (videoON != 0)                 ////////////////////HDMI CAM ON??? ////////////////////
                        {
                            using (Mat1 = new Mat(0, 0, MatType.CV_8UC3))               // for HDMI CAPTURE
                            {
            
            
                                VideoCapture1.Read(Mat1);
                                using (Bitmap bitmap1 = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(Mat1))
                                {
                                    Graphics g = Graphics.FromImage(canvasLEFT);
                                    Graphics1.DrawImage(bitmap1, 0, 0, Mat1.Cols, Mat1.Rows);
                                    g.DrawImage(bitmap1, 0, 0);
                                    g.Dispose();
                                }
                            }
                        }
            
                        timer2.Start();
                        timer2.Enabled = true;
                    }
                   
                    /////////////////////////
                    ///Button control
                    /////////////////////////
            
                    private void button10_Click(object sender, EventArgs e)  ///// START ///////
                    {
                        controllog[controllast] = 9;
                        controllast++;
            
                        ////////////////////////////// START /////////////////////////////////
                        if (controllerON > 0)
                        {
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 32);   //1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16analogue 32START
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                            button10.BackColor = Color.Red;
                            button10.Refresh();
                            waitsecone();
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);   //1,2,4,8
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                            button10.BackColor = Color.LightGray;
                            button10.Refresh();
                            waitsecone();
                        }
                    }
                    private void button11_Click(object sender, EventArgs e)   ///// /// SELECT /////////
                    {
                        controllog[controllast] = 10;
                        controllast++;
            
                        ////////////////////////////// SELECT /////////////////////////////////
                        if (controllerON > 0)
                        {
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 16);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16SELECT 32START
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                            button11.BackColor = Color.Red;
                            button11.Refresh();
                            waitsecone();
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);   //1,2,4,8
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                            button11.BackColor = Color.LightGray;
                            button11.Refresh();
                            waitsecone();
                        }
                    }
                    private void button6_Click(object sender, EventArgs e)  ///// UP ////////
                    {
                        controllog[controllast] = 1;
                        controllast++;
            
                        ////////////////////////////// UP /////////////////////////////////
                        if (controllerON > 0)
                        {
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 2);//1Right,2UP,4Left,8Down
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);
                            button6.BackColor = Color.Red;
                            button6.Refresh();
                            waitsecone();
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);   //1,2,4,8
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);
                            button6.BackColor = Color.LightGray;
                            button6.Refresh();
                            waitsecone();
                        }
                    }
                    private void button7_Click(object sender, EventArgs e)  //// LEFT //////////
                    {
                        controllog[controllast] = 2;
                        controllast++;
            
                        if (controllerON > 0)
                        {
                            ////////////////////////////// LEFT /////////////////////////////////
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 4);//1Right,2UP,4Left,8Down
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);
                            button7.BackColor = Color.Red;
                            button7.Refresh();
                            waitsecone();
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);   //1,2,4,8
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);
                            button7.BackColor = Color.LightGray;
                            waitsecone();
                            button7.Refresh();
                        }
                    }
                    private void button9_Click(object sender, EventArgs e)  ////// DOWN //////
                    {
                        controllog[controllast] = 3;
                        controllast++;
            
                        if (controllerON > 0)
                        {
                            ////////////////////////////// DOWN /////////////////////////////////
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 8);//1Right,2UP,4Left,8Down
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);
                            button9.BackColor = Color.Red;
                            button9.Refresh();
                            waitsecone();
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);   //1,2,4,8
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);
                            button9.BackColor = Color.LightGray;
                            button9.Refresh();
                            waitsecone();
                        }
                    }
                    private void button8_Click(object sender, EventArgs e)  ///// RIGHT ////////
                    {
                        controllog[controllast] = 4;
                        controllast++;
            
                        if (controllerON > 0)
                        {
                            ////////////////////////////// RIGHT /////////////////////////////////
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 1);//1Right,2UP,4Left,8Down
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);
                            button8.BackColor = Color.Red;
                            button8.Refresh();
                            waitsecone();
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);   //1,2,4,8
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);
                            button8.BackColor = Color.LightGray;
                            button8.Refresh();
                            waitsecone();
                        }
                    }
                    private void button12_Click(object sender, EventArgs e) //// BOMB //////////
                    {
                        controllog[controllast] = 5;
                        controllast++;
            
                        if (controllerON > 0)
                        {
                            ////////////////////////////// BOMB /////////////////////////////////
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 1);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16analogue 32START
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                            button12.BackColor = Color.Red;
                            button12.Refresh();
                            waitsecone();
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);   //1,2,4,8
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                            button12.BackColor = Color.LightGray;
                            button12.Refresh();
                            waitsecone();
                        }
                    }
                    private void button13_Click(object sender, EventArgs e)  ////// SINGLE SHOT //////
                    {
                        controllog[controllast] = 6;
                        controllast++;
            
                        if (controllerON > 0)
                        {
                            ////////////////////////////// SINGLE SHOT /////////////////////////////////
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 2);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16analogue 32START
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                            button13.BackColor = Color.Red;
                            button13.Refresh();
                            waitsecone();
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);   //1,2,4,8
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                            button13.BackColor = Color.LightGray;
                            button13.Refresh();
                            waitsecone();
                        }
                    }
                    private void button14_Click(object sender, EventArgs e) // BOMB /////
                    {
                        controllog[controllast] = 7;
                        controllast++;
            
                        if (controllerON > 0)
                        {
                            ////////////////////////////// BOMB /////////////////////////////////
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 8);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16analogue 
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                            button14.BackColor = Color.Red;
                            button14.Refresh();
                            waitsecone();
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);   //1,2,4,8
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                            button14.BackColor = Color.LightGray;
                            button14.Refresh();
                            waitsecone();
                        }
                    }
                    private void button15_Click(object sender, EventArgs e)  // RAPID SHOT ////// RAPID SHOT //////
            
                    {
                        controllog[controllast] = 8;
                        controllast++;
            
                        if (controllerON > 0)
                        {
                            ////////////////////////////// RAPID SHOT /////////////////////////////////
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 4);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16analogue 32START
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                            button15.BackColor = Color.Red;
                            button15.Refresh();
                            waitsecone();
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);   //1,2,4,8
                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                            button15.BackColor = Color.LightGray;
                            button15.Refresh();
                            waitsecone();
                        }
                    }
            
                    /////////////////////////
                    ///MAIN LOOP by timer1
                    /////////////////////////
                    private void timer1_Tick(object sender, EventArgs e)
                    {
                        DateTime dt5 = DateTime.Now;
                        int aftertime = 0;
                        aftertime = ((dt5.Minute * 60) + dt5.Second) * 1000 + dt5.Millisecond;
                        label59.Text=(aftertime - beforetime).ToString()+"ms/fr";
                        beforetime = aftertime;
            
            
                        //////////////// TIMER LOOP , MAIN LOOP by timer1/ ///////////////////////////////
                        timer1.Enabled = false;
                        timer1.Stop();
            
            
                        if (videoON != 0)                 ////////////////////HDMI CAM ON??? ////////////////////
                        {
                            using (Mat1 = new Mat(0, 0, MatType.CV_8UC3))               // for HDMI CAPTURE
                            {
                                VideoCapture1.Read(Mat1);
                                using (Bitmap bitmap1 = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(Mat1))
                                {
            
                                    /////////////////////////HDMI CAPTURING ///////////////////////////////////////////////
            
                                    //Random r1 = new System.Random();
                                    //int filenum = r1.Next(0, 100);
                                    //bitmap1.Save(@"D:\HDMIcapture\HDMIcap" + filenum.ToString("D2") + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            
                                    Graphics g = Graphics.FromImage(canvasLEFT);
                                    Graphics1.DrawImage(bitmap1, 0, 0, Mat1.Cols, Mat1.Rows);
                                    g.Clear(Color.LightGray);
            
                                    // EXCLUSIVE use image data for "bmpdata"//
                                    System.Drawing.Imaging.BitmapData bmpData = bitmap1.LockBits(new Rectangle(0, 0, bitmap1.Width, bitmap1.Height),
                                    System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap1.PixelFormat);
            
                                    System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, srcData, 0, srcData.Length);
                                    var stride = Math.Abs(bmpData.Stride);                                        // stride=1920
                                    var channel = Bitmap.GetPixelFormatSize(bitmap1.PixelFormat) / 8;             // ch=====3
                                    label3.Text = "Ch" + channel.ToString();
            
                                    int count_target = 0;                // find JIKI from all by GREEN
                                    int count_targetNEAR = 0;            // find JIKI from previous-near by GREEN
                                    int counttama = 0;                   // find YELLOWISH 
                                    int indexR, indexG, indexB;
                                    int count_target_A = 0;
                                    int count_target_B = 0;
                                    int count_target_C = 0;
                                    int count_target_D = 0;
                                    int count_target_E = 0;
                                    int count_target_F = 0;
            
                                    
                                    ///@@@@@@@@@@@@@@PAC PA PAC PAC
                                    if (numericUpDown7.Value == 2)
                                    {
            
                                        //     double nv5 = (double)numericUpDown5.Value;
                                        //     for (int pixelY = 0; pixelY < 480; pixelY++)  // pixel Y
                                        //     {
                                        //         for (int pixelX = 0; pixelX < 640; pixelX++) // pixel X * ch
                                        //         {
                                        //             indexR = (pixelX + pixelY * 640) * 3+2 ; //r
                                        //             indexG = (pixelX + pixelY * 640) * 3 + 1; //g
                                        //             indexB = (pixelX + pixelY * 640) * 3 ; //b
            
                                        // SLIDE previous image data for ONESTEP
                                        //             prevData2[indexR] = prevData1[indexR];  //  -2
                                        //             prevData2[indexG] = prevData1[indexG];
                                        //             prevData2[indexB] = prevData1[indexB];
                                        //             prevData1[indexR] = (byte)((double)(prevData0[indexR]) * nv5 * 0.1 + (double)(prevData1[indexR]) * (1 - nv5 * 0.1));  // -1
                                        //             prevData1[indexG] = (byte)((double)(prevData0[indexG]) * nv5 * 0.1 + (double)(prevData1[indexG]) * (1 - nv5 * 0.1));  // -1
                                        //             prevData1[indexB] = (byte)((double)(prevData0[indexB]) * nv5 * 0.1 + (double)(prevData1[indexB]) * (1 - nv5 * 0.1));  // -1
                                        //             prevData0[indexR] = srcData[indexR];    // now
                                        //             prevData0[indexG] = srcData[indexG];
                                        //             prevData0[indexB] = srcData[indexB];
                                        //         }
                                        //     }
            
                                        //     int offset = (int)(numericUpDown2.Value);
                                        //     int xx1 = x_history[(int)(numericUpDown6.Value)] * (int)(numericUpDown3.Value);
                                        //     int yy1 = y_history[(int)(numericUpDown6.Value)] * (int)(numericUpDown3.Value);
            
                                        double bor = 0.0;
                                        bor = Math.Sqrt((double)(numericUpDown4.Value));
                                        int chR = 0, chG = 0, chB = 0;
                                        int nowR = 0, nowG = 0, nowB = 0;
            
                                        // FIND YELLOW ME & Enemy from 1dot
                                        count_target = 0;
                                        count_targetNEAR = 0;
                                        count_target_A = 0;
                                        count_target_B = 0;
                                        count_target_C = 0;
                                        count_target_D = 0;
                                        count_target_E = 0;
                                        count_target_F = 0;
            
                                        for (int pixelY = 50; pixelY < (480 - 50); pixelY++)  // pixel Y
                                        {
                                            for (int pixelX = 140; pixelX < (640 - 140); pixelX++) // pixel X * ch
                                            {
                                                indexR = (pixelX + pixelY * 640) * 3 + 2; //r
                                                indexG = (pixelX + pixelY * 640) * 3 + 1; //g
                                                indexB = (pixelX + pixelY * 640) * 3 + 0; //b
                                                if (srcData[indexR] > 200 && srcData[indexG] > 200 && srcData[indexB] < 50) // 20 20 200
                                                {
                                                    if (count_target < 990)
                                                    {
                                                        targetX[count_target] = pixelX;
                                                        targetY[count_target] = pixelY;
                                                        count_target++;
                                                    }
                                                    if (count_targetNEAR < 990)
                                                    {
                                                        if (((prev_jikiX - pixelX) * (prev_jikiX - pixelX) + (prev_jikiY - pixelY) * (prev_jikiY - pixelY)) < 50)
                                                        {
                                                            targetNEARX[count_targetNEAR] = pixelX;
                                                            targetNEARY[count_targetNEAR] = pixelY;
                                                            count_targetNEAR++;
                                                        }
                                                    }
                                                }
            
                                                ////////////// FIND MONSTER in 1pixesl /////////////////////
                                                if (srcData[indexR] > 200 && srcData[indexR] < 220 && srcData[indexG] > 140 && srcData[indexG] < 160 && srcData[indexB] > 180 && srcData[indexB] < 200) // 20 20 200
                                                {
                                                    if (count_target_A < 990)
                                                    {
                                                        targetX_A[count_target_A] = pixelX;
                                                        targetY_A[count_target_A] = pixelY;
                                                        count_target_A++;
                                                    }
                                                }
                                                if (srcData[indexR] > 220 && srcData[indexG] < 15 && srcData[indexB] < 25) // 20 20 200
                                                {
                                                    if (count_target_B < 990)
                                                    {
                                                        targetX_B[count_target_B] = pixelX;
                                                        targetY_B[count_target_B] = pixelY;
                                                        count_target_B++;
                                                    }
                                                }
                                                if (srcData[indexR] > 210 && srcData[indexR] < 230 && srcData[indexG] > 130 && srcData[indexG] < 150 && srcData[indexB] > 70 && srcData[indexB] < 90) // 20 20 200
                                                {
                                                    if (count_target_C < 990)
                                                    {
                                                        targetX_C[count_target_C] = pixelX;
                                                        targetY_C[count_target_C] = pixelY;
                                                        count_target_C++;
                                                    }
                                                }
                                                if (srcData[indexR] > 5 && srcData[indexR] < 25 && srcData[indexG] > 220 && srcData[indexB] > 170 && srcData[indexB] < 190) // 20 20 200
                                                {
                                                    if (count_target_D < 990)
                                                    {
                                                        targetX_D[count_target_D] = pixelX;
                                                        targetY_D[count_target_D] = pixelY;
                                                        count_target_D++;
                                                    }
                                                }
                                                ////////////// FIND PANIC MONSTER  /////////////////////
                                                if (srcData[indexR] > 5 && srcData[indexR] < 30 && srcData[indexG] > 0 && srcData[indexG] < 15 && srcData[indexB] > 150 && srcData[indexB] < 170) // 20 20 200
                                                {
                                                    if (count_target_E < 990)
                                                    {
                                                        targetX_E[count_target_E] = pixelX;
                                                        targetY_E[count_target_E] = pixelY;
                                                        count_target_E++;
                                                    }
                                                }
                                                //////////////// find FOODy/////////////////////
                                                if (srcData[indexR] > 160 && srcData[indexR] < 200 && srcData[indexG] > 130 && srcData[indexG] < 160 && srcData[indexB] > 130 && srcData[indexB] < 160) // 20 20 200
                                                {
                                                    if (count_target_F < 990 && pixelY > 70)
                                                    {
                                                        targetX_F[count_target_F] = pixelX;
                                                        targetY_F[count_target_F] = pixelY;
                                                        count_target_F++;
                                                    }
                                                }
                                            }
                                        }
                                        /// MEMO monster colors //
                                        //A pink 210 150 190
                                        //B  230 0 15
                                        //C 220 140 81
                                        //D 13 240 180
                                        // FOOD210 140 140
                                        //KILL E  20 5 160
            
                                        // FILTER YELLOWME in FAR
                                        int filtjiki = 0;
                                        int countpix2 = 0;
                                        if (count_target != 0)
                                        {
                                            for (int counta = 0; counta < count_target; counta++)
                                            {
                                                countpix2 = 0;
                                                for (int xx = 0; xx < 5; xx++)
                                                {
                                                    for (int yy = 0; yy < 5; yy++)
                                                    {
                                                        indexR = (targetX[counta] + xx - 2 + (targetY[counta] + yy - 2) * 640) * 3 + 2; //r
                                                        indexG = (targetX[counta] + xx - 2 + (targetY[counta] + yy - 2) * 640) * 3 + 1; //g
                                                        indexB = (targetX[counta] + xx - 2 + (targetY[counta] + yy - 2) * 640) * 3 + 0; //b
                                                        if (srcData[indexR] > 200 && srcData[indexG] > 200 && srcData[indexB] < 50)  // 20 20 200
                                                        {
                                                            countpix2++;
                                                        }
                                                    }
                                                }
                                                if (countpix2 > 15 && filtjiki < 9000)
                                                {
                                                    filtjikiposX[filtjiki] = targetX[counta];
                                                    filtjikiposY[filtjiki] = targetY[counta];
                                                    filtjiki++;
                                                }
                                            }
                                        }
            
                                        // filter monsterA
                                        int filtjiki_A = 0;
                                        if (count_target_A != 0)
                                        {
                                            for (int counta = 0; counta < count_target_A; counta++)
                                            {
                                                countpix2 = 0;
                                                for (int xx = 0; xx < 5; xx++)
                                                {
                                                    for (int yy = 0; yy < 5; yy++)
                                                    {
                                                        indexR = (targetX_A[counta] + xx - 2 + (targetY_A[counta] + yy - 2) * 640) * 3 + 2; //r
                                                        indexG = (targetX_A[counta] + xx - 2 + (targetY_A[counta] + yy - 2) * 640) * 3 + 1; //g
                                                        indexB = (targetX_A[counta] + xx - 2 + (targetY_A[counta] + yy - 2) * 640) * 3 + 0; //b
                                                        if (srcData[indexR] > 200 && srcData[indexR] < 220 && srcData[indexG] > 140 && srcData[indexG] < 160 && srcData[indexB] > 180 && srcData[indexB] < 200) // 20 20 200
                                                        {
                                                            countpix2++;
                                                        }
                                                    }
                                                }
                                                if (countpix2 > 15 && filtjiki_A < 9000)
                                                {
                                                    filtjikiposX_A[filtjiki_A] = targetX_A[counta];
                                                    filtjikiposY_A[filtjiki_A] = targetY_A[counta];
                                                    filtjiki_A++;
                                                }
                                            }
                                        }
                                        // filter monsterB
                                        int filtjiki_B = 0;
                                        if (count_target_B != 0)
                                        {
                                            for (int counta = 0; counta < count_target_B; counta++)
                                            {
                                                countpix2 = 0;
                                                for (int xx = 0; xx < 5; xx++)
                                                {
                                                    for (int yy = 0; yy < 5; yy++)
                                                    {
                                                        indexR = (targetX_B[counta] + xx - 2 + (targetY_B[counta] + yy - 2) * 640) * 3 + 2; //r
                                                        indexG = (targetX_B[counta] + xx - 2 + (targetY_B[counta] + yy - 2) * 640) * 3 + 1; //g
                                                        indexB = (targetX_B[counta] + xx - 2 + (targetY_B[counta] + yy - 2) * 640) * 3 + 0; //b
                                                        if (srcData[indexR] > 220 && srcData[indexG] < 15 && srcData[indexB] < 25) // 20 20 200
                                                        {
                                                            countpix2++;
                                                        }
                                                    }
                                                }
                                                if (countpix2 > 15 && filtjiki_B < 9000)
                                                {
                                                    filtjikiposX_B[filtjiki_B] = targetX_B[counta];
                                                    filtjikiposY_B[filtjiki_B] = targetY_B[counta];
                                                    filtjiki_B++;
                                                }
                                            }
                                        }
                                        // filter monster C
                                        int filtjiki_C = 0;
                                        if (count_target_C != 0)
                                        {
                                            for (int counta = 0; counta < count_target_C; counta++)
                                            {
                                                countpix2 = 0;
                                                for (int xx = 0; xx < 5; xx++)
                                                {
                                                    for (int yy = 0; yy < 5; yy++)
                                                    {
                                                        indexR = (targetX_C[counta] + xx - 2 + (targetY_C[counta] + yy - 2) * 640) * 3 + 2; //r
                                                        indexG = (targetX_C[counta] + xx - 2 + (targetY_C[counta] + yy - 2) * 640) * 3 + 1; //g
                                                        indexB = (targetX_C[counta] + xx - 2 + (targetY_C[counta] + yy - 2) * 640) * 3 + 0; //b
                                                        if (srcData[indexR] > 210 && srcData[indexR] < 230 && srcData[indexG] > 130 && srcData[indexG] < 150 && srcData[indexB] > 70 && srcData[indexB] < 90) // 20 20 200
                                                        {
                                                            countpix2++;
                                                        }
                                                    }
                                                }
                                                if (countpix2 > 15 && filtjiki_C < 9000)
                                                {
                                                    filtjikiposX_C[filtjiki_C] = targetX_C[counta];
                                                    filtjikiposY_C[filtjiki_C] = targetY_C[counta];
                                                    filtjiki_C++;
                                                }
                                            }
                                        }
                                        // filter monster D
                                        int filtjiki_D = 0;
                                        if (count_target_D != 0)
                                        {
                                            for (int counta = 0; counta < count_target_D; counta++)
                                            {
                                                countpix2 = 0;
                                                for (int xx = 0; xx < 5; xx++)
                                                {
                                                    for (int yy = 0; yy < 5; yy++)
                                                    {
                                                        indexR = (targetX_D[counta] + xx - 2 + (targetY_D[counta] + yy - 2) * 640) * 3 + 2; //r
                                                        indexG = (targetX_D[counta] + xx - 2 + (targetY_D[counta] + yy - 2) * 640) * 3 + 1; //g
                                                        indexB = (targetX_D[counta] + xx - 2 + (targetY_D[counta] + yy - 2) * 640) * 3 + 0; //b
                                                        if (srcData[indexR] > 5 && srcData[indexR] < 25 && srcData[indexG] > 220 && srcData[indexB] > 170 && srcData[indexB] < 190) // 20 20 200
                                                        {
                                                            countpix2++;
                                                        }
                                                    }
                                                }
                                                if (countpix2 > 15 && filtjiki_D < 9000)
                                                {
                                                    filtjikiposX_D[filtjiki_D] = targetX_D[counta];
                                                    filtjikiposY_D[filtjiki_D] = targetY_D[counta];
                                                    filtjiki_D++;
                                                }
                                            }
                                        }
            
                                        // filter PANIC monster
                                        int filtjiki_E = 0;
                                        if (count_target_E != 0)
                                        {
                                            for (int counta = 0; counta < count_target_E; counta++)
                                            {
                                                countpix2 = 0;
                                                for (int xx = 0; xx < 5; xx++)
                                                {
                                                    for (int yy = 0; yy < 5; yy++)
                                                    {
                                                        indexR = (targetX_E[counta] + xx - 2 + (targetY_E[counta] + yy - 2) * 640) * 3 + 2; //r
                                                        indexG = (targetX_E[counta] + xx - 2 + (targetY_E[counta] + yy - 2) * 640) * 3 + 1; //g
                                                        indexB = (targetX_E[counta] + xx - 2 + (targetY_E[counta] + yy - 2) * 640) * 3 + 0; //b
                                                        if (srcData[indexR] > 200 && srcData[indexR] < 220 && srcData[indexG] > 140 && srcData[indexG] < 160 && srcData[indexB] > 180 && srcData[indexB] < 200) // 20 20 200
                                                        {
                                                            countpix2++;
                                                        }
                                                    }
                                                }
                                                if (countpix2 > 15 && filtjiki_E < 9000)
                                                {
                                                    filtjikiposX_E[filtjiki_E] = targetX_E[counta];
                                                    filtjikiposY_E[filtjiki_E] = targetY_E[counta];
                                                    filtjiki_E++;
                                                }
                                            }
                                        }
            
                                        // FILTER YELLOW ME in NEAR
                                        int filtjikinear = 0;
                                        int countpix3 = 0;
                                        if (count_targetNEAR != 0)
                                        {
                                            for (int counta = 0; counta < count_targetNEAR; counta++)
                                            {
                                                countpix3 = 0;
                                                for (int xx = 0; xx < 5; xx++)
                                                {
                                                    for (int yy = 0; yy < 5; yy++)
                                                    {
                                                        indexR = (targetNEARX[counta] + xx - 2 + (targetNEARY[counta] + yy - 2) * 640) * 3 + 2; //r
                                                        indexG = (targetNEARX[counta] + xx - 2 + (targetNEARY[counta] + yy - 2) * 640) * 3 + 1; //g
                                                        indexB = (targetNEARX[counta] + xx - 2 + (targetNEARY[counta] + yy - 2) * 640) * 3 + 0; //b
                                                        if (srcData[indexR] > 200 && srcData[indexG] > 200 && srcData[indexB] < 50) // 20 20 200
                                                        {
                                                            countpix3++;
                                                        }
                                                    }
                                                }
                                                if (countpix3 > 15 && filtjikinear < 9000)
                                                {
                                                    filtjikiposNEARX[filtjikinear] = targetNEARX[counta];
                                                    filtjikiposNEARY[filtjikinear] = targetNEARY[counta];
                                                    filtjikinear++;
                                                    // g.DrawRectangle(Pens.Green, targetNEARX[counta] , targetNEARY[counta] , 10, 10);
                                                }
                                            }
                                        }
            
            
                                        //find Yellwo me
                                        if (filtjikinear > 10)
                                        {
                                            int zerox = 0;
                                            int zeroy = 0;
            
                                            for (int count = 0; count < filtjikinear; count++)
                                            {
                                                zerox = filtjikiposNEARX[count] + zerox;
                                                zeroy = filtjikiposNEARY[count] + zeroy;
                                            }
                                            jikiX = zerox / filtjikinear;
                                            jikiY = zeroy / filtjikinear;
            
                                        }
                                        else if (filtjiki > 10)
                                        {
                                            int zerox2 = 0;
                                            int zeroy2 = 0;
            
                                            for (int count = 0; count < filtjiki; count++)
                                            {
                                                zerox2 = filtjikiposX[count] + zerox2;
                                                zeroy2 = filtjikiposY[count] + zeroy2;
                                            }
                                            jikiX = zerox2 / filtjiki;
                                            jikiY = zeroy2 / filtjiki;
                                        }
                                        else
                                        {
                                            jikiX = prev_jikiX;
                                            jikiY = prev_jikiY;
            
                                        }
            
                                        //find monsterA
                                        if (filtjiki_A > 10)
                                        {
                                            int zerox2 = 0;
                                            int zeroy2 = 0;
            
                                            for (int count = 0; count < filtjiki_A; count++)
                                            {
                                                zerox2 = filtjikiposX_A[count] + zerox2;
                                                zeroy2 = filtjikiposY_A[count] + zeroy2;
                                            }
                                            jikiX_A = zerox2 / filtjiki_A;
                                            jikiY_A = zeroy2 / filtjiki_A;
                                        }
                                        else
                                        {
                                            jikiX_A = 0;
                                            jikiY_A = 0;
            
                                        }
                                       
                                        // find monsterB
                                        if (filtjiki_B > 10)
                                        {
                                            int zerox2 = 0;
                                            int zeroy2 = 0;
            
                                            for (int count = 0; count < filtjiki_B; count++)
                                            {
                                                zerox2 = filtjikiposX_B[count] + zerox2;
                                                zeroy2 = filtjikiposY_B[count] + zeroy2;
                                            }
                                            jikiX_B = zerox2 / filtjiki_B;
                                            jikiY_B = zeroy2 / filtjiki_B;
                                        }
                                        else
                                        {
                                            jikiX_B = 0;
                                            jikiY_B = 0;
            
                                        }
            
                                        // find monsterC
                                        if (filtjiki_C > 10)
                                        {
                                            int zerox2 = 0;
                                            int zeroy2 = 0;
            
                                            for (int count = 0; count < filtjiki_C; count++)
                                            {
                                                zerox2 = filtjikiposX_C[count] + zerox2;
                                                zeroy2 = filtjikiposY_C[count] + zeroy2;
                                            }
                                            jikiX_C = zerox2 / filtjiki_C;
                                            jikiY_C = zeroy2 / filtjiki_C;
                                        }
                                        else
                                        {
                                            jikiX_C = 0;
                                            jikiY_C = 0;
            
                                        }
            
                                        // find monsterD
                                        if (filtjiki_D > 10)
                                        {
                                            int zerox2 = 0;
                                            int zeroy2 = 0;
            
                                            for (int count = 0; count < filtjiki_D; count++)
                                            {
                                                zerox2 = filtjikiposX_D[count] + zerox2;
                                                zeroy2 = filtjikiposY_D[count] + zeroy2;
                                            }
                                            jikiX_D = zerox2 / filtjiki_D;
                                            jikiY_D = zeroy2 / filtjiki_D;
                                        }
                                        else
                                        {
                                            jikiX_D = 0;
                                            jikiY_D = 0;
            
                                        }
                                        
                                        // check for restert message
                                        int pixr = 0, pixg = 0, pixb = 0;
                                        for (int xx = 120; xx < 123; xx++)
                                        {
                                            for (int yy = 380; yy < 383; yy++)
                                            {
                                                indexR = (xx + yy * 640) * 3 + 2; //r
                                                indexG = (xx + yy * 640) * 3 + 1; //g
                                                indexB = (xx + yy * 640) * 3 + 0; //b
                                                pixr = pixr + (int)srcData[indexR];
                                                pixg = pixg + (int)srcData[indexG];
                                                pixb = pixb + (int)srcData[indexB];
            
                                            }
                                        }
                                        pixr = pixr / 9;
                                        pixg = pixg / 9;
                                        pixb = pixb / 9;
            
                                        //       HITTING WALL??   
                                        int Lhit = 0, Rhit = 0, Uhit = 0, Dhit = 0;
                                        if (jikiX > 100 && jikiX < 540 && jikiY > 50 && jikiY < 430)
                                        {
                                            int countpi = 0;
                                            for (int xx = jikiX - 32; xx < (jikiX - 10); xx++)
                                            {
                                                for (int yy = jikiY - 5; yy < (jikiY + 5); yy++)
                                                {
                                                    indexR = (xx + yy * 640) * 3 + 2; //r
                                                    indexG = (xx + yy * 640) * 3 + 1; //g
                                                    indexB = (xx + yy * 640) * 3 + 0; //b
                                                    if (srcData[indexR] < 10 && srcData[indexG] < 10 && srcData[indexB] > 40 && srcData[indexB] < 160)
                                                    {
                                                        countpi++;
                                                    }
            
                                                }
            
                                            }
                                            if (countpi > 5)
                                            {
                                                Lhit = 1;
                                            }
                                        }
                                        if (jikiX > 100 && jikiX < 540 && jikiY > 50 && jikiY < 430)
                                        {
                                            int countpi = 0;
                                            for (int xx = jikiX + 10; xx < (jikiX + 32); xx++)
                                            {
                                                for (int yy = jikiY - 5; yy < (jikiY + 5); yy++)
                                                {
                                                    indexR = (xx + yy * 640) * 3 + 2; //r
                                                    indexG = (xx + yy * 640) * 3 + 1; //g
                                                    indexB = (xx + yy * 640) * 3 + 0; //b
                                                    if (srcData[indexR] < 10 && srcData[indexG] < 10 && srcData[indexB] > 40 && srcData[indexB] < 160)
                                                    {
                                                        countpi++;
                                                    }
            
                                                }
            
                                            }
                                            if (countpi > 5)
                                            {
                                                Rhit = 1;
                                            }
                                        }
                                        if (jikiX > 100 && jikiX < 540 && jikiY > 50 && jikiY < 430)
                                        {
                                            int countpi = 0;
                                            for (int xx = jikiX -5; xx < (jikiX+5); xx++)
                                            {
                                                for (int yy = jikiY + 10; yy < (jikiY + 32); yy++)
                                                {
                                                    indexR = (xx + yy * 640) * 3 + 2; //r
                                                    indexG = (xx + yy * 640) * 3 + 1; //g
                                                    indexB = (xx + yy * 640) * 3 + 0; //b
                                                    if (srcData[indexR] < 10 && srcData[indexG] < 10 && srcData[indexB] > 40 && srcData[indexB] < 160)
                                                    {
                                                        countpi++;
                                                    }
            
                                                }
            
                                            }
                                            if (countpi > 5)
                                            {
                                                Dhit = 1;
                                            }
                                        }
                                        if (jikiX > 100 && jikiX < 540 && jikiY > 50 && jikiY < 430)
                                        {
                                            int countpi = 0;
                                            for (int xx = jikiX - 5; xx < (jikiX + 5); xx++)
                                            {
                                                for (int yy = jikiY -32 ; yy < (jikiY -10); yy++)
                                                {
                                                    indexR = (xx + yy * 640) * 3 + 2; //r
                                                    indexG = (xx + yy * 640) * 3 + 1; //g
                                                    indexB = (xx + yy * 640) * 3 + 0; //b
                                                    if (srcData[indexR] < 10 && srcData[indexG] < 10 && srcData[indexB] > 40 && srcData[indexB] < 160)
                                                    {
                                                        countpi++;
                                                    }
            
                                                }
            
                                            }
                                            if (countpi > 5)
                                            {
                                                Uhit = 1;
                                            }
                                        }
            
            
            
                                        //  RESTART??  //
                                        if (pixb > 105)
                                        {
                                            startflag++;
                                            label21.Text = "restart" + startflag.ToString();
                                            label27.Text = "Continue : " + conttimes.ToString();
                                            if (startflag > 40)
                                            {
                                                if (playON != 0 && controllerON > 0)
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 1);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16SEL 32START
                                                    waitsecone();
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);   //1,2,4,8
                                                    label21.Text = "restart" + startflag.ToString();
                                                    conttimes++;
                                                    label27.Text = "Continue : " + conttimes.ToString();
                                                    startflag = 0;
            
            
                                                    DateTime dt4 = DateTime.Now;
                                                    nowtime = (dt4.Hour * 60 + dt4.Minute) * 60 + dt4.Second;
                                                    int newellapsedtime = (nowtime - prevtime);
                                                    if (newellapsedtime < 10) { newellapsedtime = 30; }
                                                    label51.Text = "new"+ newellapsedtime.ToString();
                                                    label52.Text = "old"+ oldellapsedtime.ToString();
                                                    prevtime = nowtime;
                                                    if (newellapsedtime > maxtime)
                                                    {
                                                        maxtime = newellapsedtime;
                                                        label53.Text = "max" + maxtime.ToString();
                                                    }
            
                                                    if (supportflag > 0)
                                                    {
                                                        if (newellapsedtime < oldellapsedtime)
                                                        {
                                                            hScrollBar1.Value = sl1;
                                                            hScrollBar2.Value = sl2;
                                                            hScrollBar3.Value = sl3;
                                                            hScrollBar4.Value = sl4;
                                                            hScrollBar5.Value = sl5;
                                                            hScrollBar6.Value = sl6;
                                                            hScrollBar7.Value = sl7;
                                                        }
                                                        else
                                                        {
                                                            sl1 = hScrollBar1.Value;
                                                            sl2 = hScrollBar2.Value;
                                                            sl3 = hScrollBar3.Value;
                                                            sl4 = hScrollBar4.Value;
                                                            sl5 = hScrollBar5.Value;
                                                            sl6 = hScrollBar6.Value;
                                                            sl7 = hScrollBar7.Value;
                                                        }
                                                        oldellapsedtime = newellapsedtime;
            
            
                                                        int sw = dt4.Millisecond % 14;
                                                        int ofs = 8;
                                                        int newsl1 = sl1, newsl2 = sl2, newsl3 = sl3, newsl4 = sl4, newsl5 = sl5, newsl6 = sl6, newsl7=sl7;
                                                        if (sw == 0 & sl1 > 20) { newsl1 = sl1 - ofs; }
                                                        if (sw == 1 & sl1 < 80) { newsl1 = sl1 + ofs; }
                                                        if (sw == 2 & sl2 > 20) { newsl2 = sl2 - ofs; }
                                                        if (sw == 3 & sl2 < 80) { newsl2 = sl2 + ofs; }
                                                        if (sw == 4 & sl3 > 20) { newsl3 = sl3 - ofs; }
                                                        if (sw == 5 & sl3 < 80) { newsl3 = sl3 + ofs; }
                                                        if (sw == 6 & sl4 > 20) { newsl4 = sl4 - ofs; }
                                                        if (sw == 7 & sl4 < 80) { newsl4 = sl4 + ofs; }
                                                        if (sw == 8 & sl5 > 20) { newsl5 = sl5 - ofs; }
                                                        if (sw == 9 & sl5 < 80) { newsl5 = sl5 + ofs; }
                                                        if (sw == 10 & sl6 > 6) { newsl6 = sl6 - 2; }
                                                        if (sw == 11 & sl6 < 180) { newsl6 = sl6 + 2; }
                                                        if (sw == 12 & sl7 > 20) { newsl7 = sl7 - ofs; }
                                                        if (sw == 13 & sl7 < 80) { newsl7 = sl7 + ofs; }
                                                        hScrollBar1.Value = newsl1;
                                                        hScrollBar2.Value = newsl2;
                                                        hScrollBar3.Value = newsl3;
                                                        hScrollBar4.Value = newsl4;
                                                        hScrollBar5.Value = newsl5;
                                                        hScrollBar6.Value = newsl6;
                                                        hScrollBar7.Value = newsl7;
                                                        label46.Text = newsl1.ToString();
                                                        label47.Text = newsl2.ToString();
                                                        label48.Text = newsl3.ToString();
                                                        label49.Text = newsl4.ToString();
                                                        label50.Text = newsl5.ToString();
                                                        label55.Text = newsl6.ToString();
                                                        label57.Text = newsl7.ToString();
                                                        timer1.Interval = newsl6;
                                                    }
                                                }
                                                startflag = 0;
            
            
            
                                            }
                                        }
                                        else
                                        {
                                            startflag--;
                                            if (startflag < 0)
                                            {
                                                startflag = 0;
                                            }
                                        }
            
            
                                        ///   back ground into BLACK  //!!
                                        for (int pixelY = 50; pixelY < (480 - 50); pixelY++)  // pixel Y
                                        {
                                            for (int pixelX = 140; pixelX < (640 - 140); pixelX++) // pixel X * ch
                                            {
                                                indexR = (pixelX + pixelY * 640) * 3 + 2; //r
                                                indexG = (pixelX + pixelY * 640) * 3 + 1; //r
                                                indexB = (pixelX + pixelY * 640) * 3 + 0; //r
                                                chB = prevData1[indexB] + prevData1[indexB - 3] + prevData1[indexB + 3] + prevData1[indexB + 1920] + prevData1[indexB - 1920];
                                                chG = prevData1[indexG] + prevData1[indexG - 3] + prevData1[indexG + 3] + prevData1[indexG + 1920] + prevData1[indexG - 1920];
                                                chR = prevData1[indexR] + prevData1[indexR - 3] + prevData1[indexR + 3] + prevData1[indexR + 1920] + prevData1[indexR - 1920];
                                                indexR = (pixelX + pixelY * 640) * 3 + 2; //r
                                                indexG = (pixelX + pixelY * 640) * 3 + 1; //g
                                                indexB = (pixelX + pixelY * 640) * 3 + 0; //b
            
                                                nowB = prevData0[indexB] + prevData0[indexB - 3] + prevData0[indexB + 3] + prevData0[indexB + 1920] + prevData0[indexB - 1920];
                                                nowG = prevData0[indexG] + prevData0[indexG - 3] + prevData0[indexG + 3] + prevData0[indexG + 1920] + prevData0[indexG - 1920];
                                                nowR = prevData0[indexR] + prevData0[indexR - 3] + prevData0[indexR + 3] + prevData0[indexR + 1920] + prevData0[indexR - 1920];
            
                                                if ((chB - nowB) * (chB - nowB) / (chB + 1) + (chG - nowG) * (chG - nowG) / (chG + 1) + (chR - nowR) * (chR - nowR) / (chR + 1) < bor * 100)
                                                {
                                                    srcData[indexR] = (byte)(srcData[indexR] / 2);
                                                    srcData[indexG] = (byte)(srcData[indexG] / 2);
                                                    srcData[indexB] = (byte)(srcData[indexB] / 2);
                                                }
                                                else
                                                {
                                                    //  srcData[indexR] = (byte)(srcData[indexR]- srcData[indexR] * nv50 / 30);
                                                    //   srcData[indexG] = (byte)(srcData[indexG]- srcData[indexG] *nv50 / 30);
                                                    //   srcData[indexB] = (byte)(srcData[indexB] - srcData[indexB]  * nv50 / 30);
                                                }
                                            }
                                        }
            
                                        //////////////// END OF LOCKBITS ///////////////
            
                                        System.Runtime.InteropServices.Marshal.Copy(srcData, 0, bmpData.Scan0, srcData.Length);
                                        bitmap1.UnlockBits(bmpData);
                                        g.DrawImage(bitmap1, 0, 0);
            
            
                                        //////////////////// message display ////////////////////
                                        label4.Text = "Jikix" + jikiX.ToString();
                                        label5.Text = "Jikiy" + jikiY.ToString();
                                        label6.Text = "targeFar" + count_target.ToString();
                                        label7.Text = "targetNear" + count_targetNEAR.ToString();
                                        //label14.Text = "tamapixels" + counttama.ToString();
                                        //label16.Text = "filtama" + filttama.ToString();
                                        label17.Text = "filtjikiNEAR" + filtjikinear.ToString();
                                        label18.Text = "filjikifar" + filtjiki.ToString();
                                        //label22.Text = "riskvalue" + atrisk.ToString();
                                        //label29.Text = "BGpixels" + counttama2.ToString();
                                        //label30.Text = "filtBG" + filttama2.ToString();
            
            
                                        // DISPOSE && UNLOCK for display
                                        pictureBox2.Image = canvasLEFT;
                                        //for (int counta = 0; counta < filtjikinear; counta++)
                                        //{
                                        //   g.DrawRectangle(Pens.Green, targetNEARX[counta] - 5, targetNEARY[counta] - 5, 10, 10);
                                        //}
            
                                        ///// shift old position ME & monsters
                                        // shift X1 Y1
                                        for (int i = 98; i >= 0; i--)
                                        {
                                            x_history_A[i + 1] = x_history_A[i];
                                            y_history_A[i + 1] = y_history_A[i];
                                        }
                                        x_history_A[0] = jikiX_A;
                                        y_history_A[0] = jikiY_A;
            
                                        for (int i = 98; i >= 0; i--)
                                        {
                                            x_history_B[i + 1] = x_history_B[i];
                                            y_history_B[i + 1] = y_history_B[i];
                                        }
                                        x_history_B[0] = jikiX_B;
                                        y_history_B[0] = jikiY_B;
            
                                        for (int i = 98; i >= 0; i--)
                                        {
                                            x_history_C[i + 1] = x_history_C[i];
                                            y_history_C[i + 1] = y_history_C[i];
                                        }
                                        x_history_C[0] = jikiX_C;
                                        y_history_C[0] = jikiY_C;
            
                                        for (int i = 98; i >= 0; i--)
                                        {
                                            x_history_D[i + 1] = x_history_D[i];
                                            y_history_D[i + 1] = y_history_D[i];
                                        }
                                        x_history_D[0] = jikiX_D;
                                        y_history_D[0] = jikiY_D;
            
                                        for (int i = 98; i >= 0; i--)
                                        {
                                            x_history[i + 1] = x_history[i];
                                            y_history[i + 1] = y_history[i];
                                        }
                                        x_history[0] = jikiX;
                                        y_history[0] = jikiY;
            
                                        //////////// are you stopping? 
            
                                        int dis = 0;
                                        int hisa = 10 * 50 / sl6+1, hisb = 20 * 50 / sl6+1;
                                        if (hisa > 80) { hisa = 80; }
                                        if (hisb > 80) { hisb = 80; }
            
                                        dis = dis + (x_history[0] - x_history[hisa]) * (x_history[0] - x_history[hisa]) + (y_history[0] - y_history[hisa]) * (y_history[0] - y_history[hisa]);
                                        dis = dis + (x_history[hisa] - x_history[hisb]) * (x_history[hisa] - x_history[hisb]) + (y_history[hisa] - y_history[hisb]) * (y_history[hisa] - y_history[hisb]);
                                        dis = 2400 - dis;
                                        if (dis < 0) { dis = 0; }
                                        dis = dis / 20;
                                        g.DrawRectangle(Pens.White, 10, 100, dis, 5);
            
                                        // FUTURE POSITION imagine
                                        int yosouAX = 0, yosouAY = 0, yosouBX = 0, yosouBY = 0, yosouCX = 0, yosouCY = 0, yosouDX = 0, yosouDY = 0;
                                        int hist = 3, hitsp = 80 / (sl6+10);
                                        if (hitsp == 0) { hitsp = 1; }
                                        yosouAX = jikiX_A - (x_history_A[hist] - jikiX_A) * hitsp;
                                        yosouAY = jikiY_A - (y_history_A[hist] - jikiY_A) * hitsp;
                                        yosouBX = jikiX_B - (x_history_B[hist] - jikiX_B) * hitsp;
                                        yosouBY = jikiY_B - (y_history_B[hist] - jikiY_B) * hitsp;
                                        yosouCX = jikiX_C - (x_history_C[hist] - jikiX_C) * hitsp;
                                        yosouCY = jikiY_C - (y_history_C[hist] - jikiY_C) * hitsp;
                                        yosouDX = jikiX_D - (x_history_D[hist] - jikiX_D) * hitsp;
                                        yosouDY = jikiY_D - (y_history_D[hist] - jikiY_D) * hitsp;
            
                                        prev_jikiX = jikiX;
                                        prev_jikiY = jikiY;
                                        prev_jikiX_A = jikiX_A;
                                        prev_jikiY_A = jikiY_A;
                                        prev_jikiX_B = jikiX_B;
                                        prev_jikiY_B = jikiY_B;
                                        prev_jikiX_C = jikiX_C;
                                        prev_jikiY_C = jikiY_C;
                                        prev_jikiX_D = jikiX_D;
                                        prev_jikiY_D = jikiY_D;
            
                                        //  DISTANCE??
                                        int dis_A = (jikiX - jikiX_A) * (jikiX - jikiX_A) + (jikiY - jikiY_A) * (jikiY - jikiY_A);
                                        int dis_B = (jikiX - jikiX_B) * (jikiX - jikiX_A) + (jikiY - jikiY_B) * (jikiY - jikiY_B);
                                        int dis_C = (jikiX - jikiX_C) * (jikiX - jikiX_A) + (jikiY - jikiY_C) * (jikiY - jikiY_C);
                                        int dis_D = (jikiX - jikiX_D) * (jikiX - jikiX_A) + (jikiY - jikiY_D) * (jikiY - jikiY_D);
                                        int yosoudis_A = (jikiX - yosouAX) * (jikiX - yosouAX) + (jikiY - yosouAY) * (jikiY - yosouAY);
                                        int yosoudis_B = (jikiX - yosouBX) * (jikiX - yosouBX) + (jikiY - yosouBY) * (jikiY - yosouBY);
                                        int yosoudis_C = (jikiX - yosouCX) * (jikiX - yosouCX) + (jikiY - yosouCY) * (jikiY - yosouCY);
                                        int yosoudis_D = (jikiX - yosouDX) * (jikiX - yosouDX) + (jikiY - yosouDY) * (jikiY - yosouDY);
                                        double conv_A = (double)dis_A;
                                        double conv_B = (double)dis_B;
                                        double conv_C = (double)dis_C;
                                        double conv_D = (double)dis_D;
                                        double yosouconv_A = (double)yosoudis_A;
                                        double yosouconv_B = (double)yosoudis_B;
                                        double yosouconv_C = (double)yosoudis_C;
                                        double yosouconv_D = (double)yosoudis_D;
            
                                        dis_A = (int)(Math.Sqrt(conv_A));
                                        dis_B = (int)(Math.Sqrt(conv_B));
                                        dis_C = (int)(Math.Sqrt(conv_C));
                                        dis_D = (int)(Math.Sqrt(conv_D));
            
                                        yosoudis_A = (int)(Math.Sqrt(yosouconv_A));
                                        yosoudis_B = (int)(Math.Sqrt(yosouconv_B));
                                        yosoudis_C = (int)(Math.Sqrt(yosouconv_C));
                                        yosoudis_D = (int)(Math.Sqrt(yosouconv_D));
                                        /////////////////////////////////////////////////////////
            
                                        /// minimum distance?
            
                                        int mindis = 0;
                                        mindis = dis_A;
                                        if (dis_B < mindis)
                                        {
                                            mindis = dis_B;
                                        }
                                        if (dis_C < mindis)
                                        {
                                            mindis = dis_C;
                                        }
                                        if (dis_D < mindis)
                                        {
                                            mindis = dis_D;
                                        }
                                        if (yosoudis_A < mindis)
                                        {
                                            mindis = yosoudis_A;
                                        }
                                        if (yosoudis_B < mindis)
                                        {
                                            mindis = yosoudis_B;
                                        }
                                        if (yosoudis_C < mindis)
                                        {
                                            mindis = yosoudis_C;
                                        }
                                        if (yosoudis_D < mindis)
                                        {
                                            mindis = yosoudis_D;
                                        }
            
                                        //food area  1=left 2=right 3=up 4=down
                                        int w1 = 10, w2 = 70;
                                        int area1 = 0, area2 = 0, area3 = 0, area4 = 0;
                                        for (int counta = 0; counta < count_target_F; counta++)
                                        {
                                            int zax, zay;
                                            zax = targetX_F[counta];
                                            zay = targetY_F[counta];
                                            g.DrawRectangle(Pens.Gray, zax - 1, zay - 1, 3, 3);
                                            if (zax > (jikiX - w1 - w2) && zay > (jikiY - w1) && zax < (jikiX - w1) && zay < (jikiY + w1)) { area1++; g.DrawRectangle(Pens.Red, zax, zay, 3, 3);}
                                            if (zax > (jikiX + w1) && zay > (jikiY - w1) && zax < (jikiX + w1 + w2) && zay < (jikiY + w1)) { area2++; g.DrawRectangle(Pens.Blue, zax, zay, 3, 3); }
                                            if (zax > (jikiX - w1) && zay > (jikiY - w1 - w2) && zax < (jikiX + w1) && zay < (jikiY - w1)) { area3++; g.DrawRectangle(Pens.Green, zax, zay, 3, 3); }
                                            if (zax > (jikiX - w1) && zay > (jikiY + w1) && zax < (jikiX + w1) && zay < (jikiY + w1 + w2)) { area4++; g.DrawRectangle(Pens.Orange, zax, zay, 3, 3); }
                                        }
                                        area1 = area1 * 5;
                                        area2 = area2 * 5;
                                        area3 = area3 * 5;
                                        area4 = area4 * 5;
            
                                        //  WHICH AREA IS FOODRICH?  1=left 2=right 3=up 4=down
                                        int maxarea = 10, areaflag = 0;
                                        if (maxarea < area1)
                                        {
                                            maxarea = area1;
                                            areaflag = 1;
                                        }
                                        if (maxarea < area2)
                                        {
                                            maxarea = area2;
                                            areaflag = 2;
                                        }
                                        if (maxarea < area3)
                                        {
                                            maxarea = area3;
                                            areaflag = 3;
                                        }
                                        if (maxarea < area4)
                                        {
                                            maxarea = area4;
                                            areaflag = 4;
                                        }
            
                                        if (areaflag == 1)
                                        {
                                            g.DrawRectangle(Pens.Red, jikiX - w1 - area1, jikiY - w1, area1, w1 * 2);
                                        }
                                        else
                                        {
                                            g.DrawRectangle(Pens.DarkGray, jikiX - w1 - area1, jikiY - w1, area1, w1 * 2);
                                        }
            
                                        if (areaflag == 2)
                                        {
                                            g.DrawRectangle(Pens.Red, jikiX + w1, jikiY - w1, area2, w1 * 2);
                                        }
                                        else
                                        {
                                            g.DrawRectangle(Pens.DarkGray, jikiX + w1, jikiY - w1, area2, w1 * 2);
                                        }
            
                                        if (areaflag == 3)
                                        {
                                            g.DrawRectangle(Pens.Red, jikiX - w1, jikiY - w1 - area3, w1 * 2, area3);
                                        }
                                        else
                                        {
                                            g.DrawRectangle(Pens.DarkGray, jikiX - w1, jikiY - w1 - area3, w1 * 2, area3);
                                        }
            
                                        if (areaflag == 4)
                                        {
                                            g.DrawRectangle(Pens.Red, jikiX - w1, jikiY + w1, w1 * 2, area4);
                                        }
                                        else
                                        {
                                            g.DrawRectangle(Pens.DarkGray, jikiX - w1, jikiY + w1, w1 * 2, area4);
                                        }
            
            
                                        //   g.DrawRectangle(Pens.DarkGray, jikiX-w1-area1,jikiY-w1,area1,w1*2);
                                        //   g.DrawRectangle(Pens.DarkGray, jikiX +w1, jikiY - w1, area2, w1 * 2);
                                        //   g.DrawRectangle(Pens.DarkGray, jikiX - w1 , jikiY - w1-area3, w1*2, area3);
                                        //   g.DrawRectangle(Pens.DarkGray, jikiX - w1, jikiY + w1, w1*2, area4);
            
                                        //g.DrawLine(Pens.Gray, jikiX-area1,jikiY,jikiX,jikiY);
                                        //g.DrawLine(Pens.Gray, jikiX + area2, jikiY, jikiX, jikiY);
                                        //g.DrawLine(Pens.Gray, jikiX, jikiY-area3, jikiX,jikiY);
                                        //g.DrawLine(Pens.Gray, jikiX, jikiY+area4, jikiX,jikiY);
            
            
                                        //Write JIKI 
                                        int thred = 80;  //threshold of NEAR
                                        int thred2 = 80;  //threshold of FUTURE
            
                                        if (jikiX >= 0 && jikiX < 640 && jikiY >= 0 && jikiY < 480)
                                        {
                                            g.DrawRectangle(Pens.White, jikiX - 15, jikiY - 15, 30, 30);
                                            g.DrawEllipse(Pens.White, jikiX - 25, jikiY - 25, 50, 50);
                                        }
            
                                        //write wall
                                        if (Lhit > 0)
                                        {
                                            g.DrawRectangle(Pens.Yellow, jikiX - 25, jikiY - 25, 3, 50);
                                        }
                                        if (Rhit > 0)
                                        {
                                            g.DrawRectangle(Pens.Yellow, jikiX + 25, jikiY - 25, 3, 50);
                                        }
                                        if (Uhit > 0)
                                        {
                                            g.DrawRectangle(Pens.Yellow, jikiX - 25, jikiY - 25, 50,3);
                                        }
                                        if (Dhit > 0)
                                        {
                                            g.DrawRectangle(Pens.Yellow, jikiX - 25, jikiY + 25, 50, 3);
                                        }
            
                                        //write monster
                                        if (jikiX_A >= 0 && jikiX_A < 640 && jikiY_A >= 0 && jikiY_A < 480)
                                        {
                                            if (dis_A < thred)
                                            {
                                                g.DrawEllipse(Pens.HotPink, jikiX_A - 25, jikiY_A - 25, 50, 50);
                                            }
                                            g.DrawRectangle(Pens.HotPink, jikiX_A - 15, jikiY_A - 15, 30, 30);
                                        }
                                        if (jikiX_B >= 0 && jikiX_B < 640 && jikiY_B >= 0 && jikiY_B < 480)
                                        {
                                            if (dis_B < thred)
                                            {
                                                g.DrawEllipse(Pens.Red, jikiX_B - 25, jikiY_B - 25, 50, 50);
                                            }
                                            g.DrawRectangle(Pens.Red, jikiX_B - 15, jikiY_B - 15, 30, 30);
                                        }
                                        if (jikiX_C >= 0 && jikiX_C < 640 && jikiY_C >= 0 && jikiY_C < 480)
                                        {
                                            if (dis_C < thred)
                                            {
                                                g.DrawEllipse(Pens.Orange, jikiX_C - 25, jikiY_C - 25, 50, 50);
                                            }
                                            g.DrawRectangle(Pens.Orange, jikiX_C - 15, jikiY_C - 15, 30, 30);
                                        }
                                        if (jikiX_D >= 0 && jikiX_D < 640 && jikiY_D >= 0 && jikiY_D < 480)
                                        {
                                            if (dis_D < thred)
                                            {
                                                g.DrawEllipse(Pens.Cyan, jikiX_D - 25, jikiY_D - 25, 50, 50);
                                            }
                                            g.DrawRectangle(Pens.Cyan, jikiX_D - 15, jikiY_D - 15, 30, 30);
                                        }
            
                                        g.DrawRectangle(Pens.HotPink, yosouAX - 5, yosouAY - 5, 10, 10);
                                        g.DrawRectangle(Pens.Red, yosouBX - 5, yosouBY - 5, 10, 10);
                                        g.DrawRectangle(Pens.Orange, yosouCX - 5, yosouCY - 5, 10, 10);
                                        g.DrawRectangle(Pens.Cyan, yosouDX - 5, yosouDY - 5, 10, 10);
            
                                        if (yosoudis_A < thred2)
                                        {
                                            g.DrawRectangle(Pens.HotPink, yosouAX - 15, yosouAY - 15, 30, 30);
                                        }
                                        if (yosoudis_B < thred2)
                                        {
                                            g.DrawRectangle(Pens.Red, yosouBX - 15, yosouBY - 15, 30, 30);
                                        }
                                        if (yosoudis_C < thred2)
                                        {
                                            g.DrawRectangle(Pens.Orange, yosouCX - 15, yosouCY - 15, 30, 30);
                                        }
                                        if (yosoudis_D < thred2)
                                        {
                                            g.DrawRectangle(Pens.Cyan, yosouDX - 15, yosouDY - 15, 30, 30);
                                        }
            
                                        conv_A = Math.Sqrt(conv_A);
                                        conv_B = Math.Sqrt(conv_B);
                                        conv_C = Math.Sqrt(conv_C);
                                        conv_D = Math.Sqrt(conv_D);
                                        yosouconv_A = Math.Sqrt(yosouconv_A);
                                        yosouconv_B = Math.Sqrt(yosouconv_B);
                                        yosouconv_C = Math.Sqrt(yosouconv_C);
                                        yosouconv_D = Math.Sqrt(yosouconv_D);
            
                                        dis_A = (int)(Math.Sqrt(conv_A * 10));
                                        dis_B = (int)(Math.Sqrt(conv_B * 10));
                                        dis_C = (int)(Math.Sqrt(conv_C * 10));
                                        dis_D = (int)(Math.Sqrt(conv_D * 10));
            
                                        //  make escape line
                                        double escX = 0.0;
                                        double escY = 0.0;
            
                                        if (jikiX != 0 && jikiY != 0)
                                        {
                                            if (jikiX_A != 0 && jikiY_A != 0 && dis_A < thred && dis_A != 0)
                                            {
                                                escX = escX + (jikiX - jikiX_A) / (dis_A / 100.0);
                                                escY = escY + (jikiY - jikiY_A) / (dis_A / 100.0);
                                            }
                                            if (jikiX_B != 0 && jikiY_B != 0 && dis_B < thred && dis_B != 0)
                                            {
                                                escX = escX + (jikiX - jikiX_B) / (dis_B / 100.0);
                                                escY = escY + (jikiY - jikiY_B) / (dis_B / 100.0);
                                            }
                                            if (jikiX_C != 0 && jikiY_C != 0 && dis_C < thred && dis_C != 0)
                                            {
                                                escX = escX + (jikiX - jikiX_C) / (dis_C / 100.0);
                                                escY = escY + (jikiY - jikiY_C) / (dis_C / 100.0);
                                            }
                                            if (jikiX_D != 0 && jikiY_D != 0 && dis_D < thred && dis_D != 0)
                                            {
                                                escX = escX + (jikiX - jikiX_D) / (dis_D / 100.0);
                                                escY = escY + (jikiY - jikiY_D) / (dis_D / 100.0);
                                            }
            
                                            if (jikiX_A != 0 && jikiY_A != 0 && yosoudis_A < thred2 && yosoudis_A != 0)
                                            {
                                                escX = escX + (jikiX - yosouAX) / (yosoudis_A / 100.0);
                                                escY = escY + (jikiY - yosouAY) / (yosoudis_A / 100.0);
                                            }
                                            if (jikiX_B != 0 && jikiY_B != 0 && yosoudis_B < thred2 && yosoudis_B != 0)
                                            {
                                                escX = escX + (jikiX - yosouBX) / (yosoudis_B / 100.0);
                                                escY = escY + (jikiY - yosouBY) / (yosoudis_B / 100.0);
                                            }
                                            if (jikiX_C != 0 && jikiY_C != 0 && yosoudis_C < thred2 && yosoudis_C != 0)
                                            {
                                                escX = escX + (jikiX - yosouCX) / (yosoudis_C / 100.0);
                                                escY = escY + (jikiY - yosouCY) / (yosoudis_C / 100.0);
                                            }
                                            if (jikiX_D != 0 && jikiY_D != 0 && yosoudis_D < thred2 && yosoudis_D != 0)
                                            {
                                                escX = escX + (jikiX - yosouDX) / (yosoudis_D / 100.0);
                                                escY = escY + (jikiY - yosouDY) / (yosoudis_D / 100.0);
                                            }
                                            escX = escX / 8;
                                            escY = escY / 8;
                                        }
            
                                        //////////////// are you in corner??
                                        ////////   1UL  2U   3 UR
                                        ///////    4L    5    6R
                                        ////////// 7DL  8D   9Dr
            
                                        int zoning = 0;
                                        int fuchix = 70;             //////  make escape space x
                                        int fuchiy = 70;             //////       escape space y
                                        if (jikiX < (150 + fuchix)) { zoning = zoning + 1; }
                                        if (jikiX > (149 + fuchix) && jikiX < (480 - fuchix)) { zoning = zoning + 2; }
                                        if (jikiX > (479 - fuchix)) { zoning = zoning + 3; }
                                        if (jikiY < (70 + fuchiy)) { zoning = zoning + 0; }
                                        if (jikiY > (69 + fuchiy) && jikiY < (440 - fuchiy)) { zoning = zoning + 3; }
                                        if (jikiY > (439 - fuchiy)) { zoning = zoning + 6; }
            
                                        if (zoning == 1) { g.DrawRectangle(Pens.DarkBlue, 150, 70, fuchix, fuchiy); }
                                        if (zoning == 2) { g.DrawRectangle(Pens.DarkBlue, 150 + fuchix, 70, 330 - fuchix * 2, fuchiy); }
                                        if (zoning == 3) { g.DrawRectangle(Pens.DarkBlue, 480 - fuchix, 70, fuchix, fuchiy); }
                                        if (zoning == 4) { g.DrawRectangle(Pens.DarkBlue, 150, 70 + fuchiy, fuchix, 370 - fuchiy * 2); }
                                        if (zoning == 5) { g.DrawRectangle(Pens.DarkBlue, 150 + fuchix, 70 + fuchiy, 330 - fuchix * 2, 370 - fuchiy * 2); }
                                        if (zoning == 6) { g.DrawRectangle(Pens.DarkBlue, 480 - fuchix, 70 + fuchiy, fuchix, 370 - fuchiy * 2); }
                                        if (zoning == 7) { g.DrawRectangle(Pens.DarkBlue, 150, 440 - fuchiy, fuchix, fuchiy); }
                                        if (zoning == 8) { g.DrawRectangle(Pens.DarkBlue, 150 + fuchix, 440 - fuchiy, 330 - fuchix * 2, fuchiy); }
                                        if (zoning == 9) { g.DrawRectangle(Pens.DarkBlue, 480 - fuchiy, 440 - fuchiy, fuchix, fuchiy); }
            
            
                                        ////////////////////// WANNA FOOD?                            
                                        int lev_UD = 0, lev_RL = 0;
                                        int ratio1 = 1;
                                        if (hScrollBar1.Value > 20)
                                        {
                                            if (areaflag == 1)
                                            {
                                                lev_RL = lev_RL - hScrollBar1.Value * ratio1;
                                            }
                                            if (areaflag == 2)
                                            {
                                                lev_RL = lev_RL + hScrollBar1.Value * ratio1;
                                            }
                                            if (areaflag == 3)
                                            {
                                                lev_UD = lev_UD - hScrollBar1.Value * ratio1;
                                            }
                                            if (areaflag == 4)
                                            {
                                                lev_UD = lev_UD + hScrollBar1.Value * ratio1;
                                            }
                                        }
            
                                        /////////////////////////  BACK TO CENTER??
                                        int ratio2 = 2;
                                        int ratio3 = 1;
                                        if (hScrollBar2.Value > 20)
                                        {
                                            if (zoning == 1)
                                            {
                                                lev_RL = lev_RL + hScrollBar2.Value * ratio2;
                                                lev_UD = lev_UD + hScrollBar2.Value * ratio2;
                                            }
                                            if (zoning == 3)
                                            {
                                                lev_RL = lev_RL - hScrollBar2.Value * ratio2;
                                                lev_UD = lev_UD + hScrollBar2.Value * ratio2;
                                            }
                                            if (zoning == 7)
                                            {
                                                lev_RL = lev_RL + hScrollBar2.Value * ratio2;
                                                lev_UD = lev_UD - hScrollBar2.Value * ratio2;
                                            }
                                            if (zoning == 9)
                                            {
                                                lev_RL = lev_RL - hScrollBar2.Value * ratio2;
                                                lev_UD = lev_UD - hScrollBar2.Value * ratio2;
                                            }
                                            if (zoning == 2)
                                            {
                                              //  lev_UD = lev_UD + hScrollBar2.Value * ratio3 / 3;
                                            }
                                            if (zoning == 4)
                                            {
                                              //  lev_RL = lev_RL + hScrollBar2.Value * ratio3 / 3;
                                            }
                                            if (zoning == 6)
                                            {
                                               // lev_RL = lev_RL - hScrollBar2.Value * ratio3 / 3;
                                            }
                                            if (zoning == 8)
                                            {
                                               // lev_UD = lev_UD - hScrollBar2.Value * ratio3 / 3;
                                            }
                                        }
            
            
                                        /////////////////////////  ESCAPEFROM ENEMY
                                        if (hScrollBar3.Value > 20)
                                        {
                                            int ratio4 = 1;
                                            if (mindis < 40)
                                            {
                                                ratio4 = 3;
                                            }
                                            else if (mindis < 60)
                                            {
                                                ratio4 = 2;
                                            }
            
                                            if (escX < 0)
                                            {
                                                lev_RL = lev_RL - hScrollBar3.Value * ratio4;
                                            }
                                            else
                                            {
                                                lev_RL = lev_RL + hScrollBar3.Value * ratio4;
                                            }
            
                                            if (escY < 0)
                                            {
                                                lev_UD = lev_UD - hScrollBar3.Value * ratio4;
                                            }
                                            else
                                            {
                                                lev_UD = lev_UD + hScrollBar3.Value * ratio4;
                                            }
                                        }
            
                                        ////////////////// KEEP ON RUNNING
                                        ///
                                        /// INCREASE RANDOMENSS
                                        /// 
                                        int ratio6 = 1;
                                        if (hScrollBar4.Value > 20)
                                        {
                                            if (dis > 30)
                                            {
                                                if (UD_history[10] < 0)
                                                {
                                                    lev_UD = lev_UD + hScrollBar4.Value * ratio6;
                                                }
                                                if (UD_history[10] > 0)
                                                {
                                                    lev_UD = lev_UD - hScrollBar4.Value * ratio6;
                                                }
                                                if (RL_history[10] < 0)
                                                {
                                                    lev_RL = lev_RL + hScrollBar4.Value * ratio6;
                                                }
                                                if (RL_history[10] > 0)
                                                {
                                                    lev_RL = lev_RL - hScrollBar4.Value * ratio6;
                                                }
                                            }
            
                                            if (dis > 90)
                                            {
                                                if (UD_history[15] < 0)
                                                {
                                                    lev_UD = lev_UD + hScrollBar4.Value * ratio6;
                                                }
                                                if (UD_history[15] > 0)
                                                {
                                                    lev_UD = lev_UD - hScrollBar4.Value * ratio6;
                                                }
                                                if (RL_history[15] < 0)
                                                {
                                                    lev_RL = lev_RL + hScrollBar4.Value * ratio6;
                                                }
                                                if (RL_history[15] > 0)
                                                {
                                                    lev_RL = lev_RL - hScrollBar4.Value * ratio6;
                                                }
                                            }
                                        }
            
                                      
            
            
                                        ////////////////// RANDOMNESS
                                        ///
                                        /// 
                                        ///
                                        int ratio5 = 1;
                                        DateTime dt2 = DateTime.Now;
                                        if (hScrollBar5.Value > 20)
                                        {
                                            if ((dt2.Millisecond % 1000) < 250)
                                            {
                                                lev_RL = lev_RL - hScrollBar5.Value * ratio5;
                                            }
                                            else if ((dt2.Millisecond % 1000) < 500)
                                            {
            
                                                lev_RL = lev_RL + hScrollBar5.Value * ratio5;
                                            }
                                            else if ((dt2.Millisecond % 1000) < 750)
                                            {
            
                                                lev_UD = lev_UD + hScrollBar5.Value * ratio5;
                                            }
                                            else
                                            {
                                                lev_UD = lev_UD - hScrollBar5.Value * ratio5;
                                            }
            
                                        }
            
                                        ///
                                        /// WALL HIT & go back
                                        /// 
                                        int ratio7 = 5;
                                        if (hScrollBar7.Value > 20)
                                        {
                                            if (Lhit > 0)
                                            {
                                                if (lev_RL < 0)
                                                {
                                                    lev_RL = lev_RL + hScrollBar7.Value * ratio7;
                                                }
                                            }
                                            if (Rhit > 0)
                                            {
                                                if (lev_RL > 0)
                                                {
                                                    lev_RL = lev_RL - hScrollBar7.Value * ratio7;
                                                }
                                            }
                                            if (Uhit > 0)
                                            {
                                                if (lev_UD < 0)
                                                {
                                                    lev_UD = lev_UD + hScrollBar7.Value * ratio7;
                                                }
                                            }
                                            if (Dhit > 0)
                                            {
                                                if (lev_UD > 0)
                                                {
                                                    lev_UD = lev_UD - hScrollBar7.Value * ratio7;
                                                }
                                            }
                                        }
            
                                        if (lev_UD * lev_UD > lev_RL * lev_RL * 8) { lev_RL = 0; }
                                        if (lev_RL * lev_RL > lev_UD * lev_UD * 8) { lev_UD = 0; }
            
                                        /////////////////// go through zone , as you go
                                        if(jikiY>228 && jikiY < 245)
                                        {
                                            if(jikiX < 220)
                                            { 
                                                lev_RL = 0;
                                            }
                                            if (jikiX > 440)
                                            {
                                                lev_RL = 0;
                                            }
                                        }
            
                                        //////////////////////MOVE JOY STICK////////////////////////
            
                                        short joyc = 0;
                                        if (lev_UD > 0)
                                        {
                                            joyc = (short)(joyc + 8);
                                        }
                                        if (lev_UD < 0)
                                        {
                                            joyc = (short)(joyc + 2);
                                        }
            
                                        if (lev_RL > 0)
                                        {
                                            joyc = (short)(joyc + 1);
                                        }
                                        if (lev_RL < 0)
                                        {
                                            joyc = (short)(joyc + 4);
                                        }
            
                                        DateTime dt3 = DateTime.Now;                  ///////////// FLUSHING JOY STICK
                                        if ((dt3.Millisecond % 200) < 20)
                                        {
                                            joyc = 0;
                                        }
            
                                        if (controllerON > 0)
                                        {
                                            if (playON != 0)
                                            {
                                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, joyc);//1Right,2UP,4Left,8Down
                                            }
                                        }
                                        for (int i = 98; i >= 0; i--)
                                        {
                                            UD_history[i + 1] = UD_history[i];
                                            RL_history[i + 1] = RL_history[i];
                                        }
                                        UD_history[0] = lev_UD;
                                        RL_history[0] = lev_RL;
            
                                        if (lev_UD > 0)
                                        {
                                            g.DrawRectangle(Pens.Orange, jikiX - 25, jikiY + 25, 50, 10);
                                        }
                                        if (lev_UD < 0)
                                        {
                                            g.DrawRectangle(Pens.Orange, jikiX - 25, jikiY - 35, 50, 10);
                                        }
                                        if (lev_RL < 0)
                                        {
                                            g.DrawRectangle(Pens.Orange, jikiX - 35, jikiY - 25, 10, 50);
                                        }
                                        if (lev_RL > 0)
                                        {
                                            g.DrawRectangle(Pens.Orange, jikiX + 25, jikiY - 25, 10, 50);
                                        }
            
                                        if (mindis < 150)
                                        {
                                            g.DrawLine(Pens.White, jikiX, jikiY, jikiX + (int)escX, jikiY + (int)escY);
                                        }
                                        g.Dispose();
                                    }
                       
                                    //@@@@@@@@@@@@@@@@@@@@@@ Galaxian
                                    if (numericUpDown7.Value == 3)
                                    {
            
                                        double nv5 = (double)numericUpDown5.Value;
                                       
                                        
            
                                           Parallel.For(0, 480, pixelY =>
                                                {
            
                                               for (int pixelX = 0; pixelX < 640; pixelX++) // pixel X * ch
                                                {
                                                        int indexR2, indexG2, indexB2;
                                                indexR2 = (pixelX + pixelY * 640) * 3 + 2; //r
                                                indexG2 = (pixelX + pixelY * 640) * 3 + 1; //g
                                                indexB2 = (pixelX + pixelY * 640) * 3 + 0; //b
            
                                                // SLIDE previous image data for ONESTEP
                                                prevData2[indexR2] = prevData1[indexR2];  //  -2
                                                prevData2[indexG2] = prevData1[indexG2];
                                                prevData2[indexB2] = prevData1[indexB2];
                                                prevData1[indexR2] = (byte)((double)(prevData0[indexR2]) * nv5 * 0.1 + (double)(prevData1[indexR2]) * (1 - nv5 * 0.1));  // -1
                                                prevData1[indexG2] = (byte)((double)(prevData0[indexG2]) * nv5 * 0.1 + (double)(prevData1[indexG2]) * (1 - nv5 * 0.1));  // -1
                                                prevData1[indexB2] = (byte)((double)(prevData0[indexB2]) * nv5 * 0.1 + (double)(prevData1[indexB2]) * (1 - nv5 * 0.1));  // -1
                                                prevData0[indexR2] = srcData[indexR2];    // now
                                                prevData0[indexG2] = srcData[indexG2];
                                                prevData0[indexB2] = srcData[indexB2];
                                                }
                                            });
            
            
                                        int offset = (int)(numericUpDown2.Value);
                                        int chR = 0, chG = 0, chB = 0;
                                        int nowR = 0, nowG = 0, nowB = 0;
                                        int xx1 = x_history[(int)(numericUpDown6.Value)] * (int)(numericUpDown3.Value);
                                        int yy1 = y_history[(int)(numericUpDown6.Value)] * (int)(numericUpDown3.Value);
                                        double bor = 0.0;
                                        bor = Math.Sqrt((double)(numericUpDown4.Value));
            
            
            
                                        // FIND GREEN JIKI from 1dot
                                        count_target = 0;
                                        count_targetNEAR = 0;
            
            
            
                                        Parallel.For(50, 480-9, pixelY =>
                                        {
            
                                            for (int pixelX = 30; pixelX < (640 - 30); pixelX++) // pixel X * ch
                                            {
                                                int indexR3, indexG3, indexB3;
                                                indexR3 = (pixelX + pixelY * 640) * 3 + 2; //r
                                                indexG3 = (pixelX + pixelY * 640) * 3 + 1; //g
                                                indexB3 = (pixelX + pixelY * 640) * 3 + 0; //b
                                                if (srcData[indexR3] < 40 && srcData[indexB3] < 60 && srcData[indexG3] > 245) // 20 20 200
                                                {
                                                    if (count_target < 990)
                                                    {
                                                        targetX[count_target] = pixelX;
                                                        targetY[count_target] = pixelY;
                                                        count_target++;
                                                    }
                                                    if (count_targetNEAR < 990)
                                                    {
                                                        if (((prev_jikiX - pixelX) * (prev_jikiX - pixelX) + (prev_jikiY - pixelY) * (prev_jikiY - pixelY)) < 300)
                                                        {
                                                            targetNEARX[count_targetNEAR] = pixelX;
                                                            targetNEARY[count_targetNEAR] = pixelY;
                                                            count_targetNEAR++;
                                                        }
                                                    }
                                                }
                                            }
                                        });
            
            
                                        // FILTER GREENjiki in FAR
                                        int filtjiki = 0;
                                        int countpix2 = 0;
            
                                        if (count_target != 0)
                                        {
                                            Parallel.For(0, count_target, counta =>
                                           // for (int counta = 0; counta < count_target; counta++)
                                            {
                                                int countpix22 = 0;
                                                for (int xx = 0; xx < 5; xx++)
                                                {
                                                    for (int yy = 0; yy < 8; yy++)
                                                    {
                                                        int indexR4, indexG4, indexB4;
                                                        indexR4 = (targetX[counta] + xx - 2 + (targetY[counta] + yy - 4) * 640) * 3 + 2; //r
                                                        indexG4 = (targetX[counta] + xx - 2 + (targetY[counta] + yy - 4) * 640) * 3 + 1; //g
                                                        indexB4 = (targetX[counta] + xx - 2 + (targetY[counta] + yy - 4) * 640) * 3 + 0; //b
                                                        if (srcData[indexR4] < 40 && srcData[indexB4] < 60 && srcData[indexG4] > 245)  // 20 20 200
                                                        {
                                                            countpix2++;
                                                        }
                                                    }
                                                }
                                                if (countpix22 > 5 && filtjiki < 9000)
                                                {
                                                    filtjikiposX[filtjiki] = targetX[counta];
                                                    filtjikiposY[filtjiki] = targetY[counta];
                                                    filtjiki++;
                                                }
                                            });
                                        }
            
            
            
                                        // FILTER GREENjiki in NEAR
                                        int filtjikinear = 0;
                                        int countpix3 = 0;
            
                                        if (count_targetNEAR != 0)
                                        {
                                           Parallel.For (0, count_targetNEAR,counta =>
                                            //for (int counta = 0; counta < count_targetNEAR; counta++)
                                            {
                                                int countpix33 = 0;
                                                for (int xx = 0; xx < 5; xx++)
                                                {
                                                    for (int yy = 0; yy < 8; yy++)
                                                    {
                                                        int indexR5, indexG5, indexB5;
                                                        indexR5 = (targetNEARX[counta] + xx - 2 + (targetNEARY[counta] + yy - 4) * 640) * 3 + 2; //r
                                                        indexG5 = (targetNEARX[counta] + xx - 2 + (targetNEARY[counta] + yy - 4) * 640) * 3 + 1; //g
                                                        indexB5 = (targetNEARX[counta] + xx - 2 + (targetNEARY[counta] + yy - 4) * 640) * 3 + 0; //b
                                                        if (srcData[indexR5] < 40 && srcData[indexB5] < 60 && srcData[indexG5] > 245) // 20 20 200
                                                        {
                                                            countpix33++;
                                                        }
                                                    }
                                                }
                                                if (countpix3 > 5 && filtjikinear < 9000)
                                                {
                                                    filtjikiposNEARX[filtjikinear] = targetNEARX[counta];
                                                    filtjikiposNEARY[filtjikinear] = targetNEARY[counta];
                                                    filtjikinear++;
                                                    g.DrawRectangle(Pens.Green, targetNEARX[counta] - 5, targetNEARY[counta] - 5, 10, 10);
                                                }
                                            });
            
                                        }
            
            
                                        //find JIKI from fil or non-filGREEN
                                        if (filtjikinear > 10)
                                        {
                                            int zerox = 0;
                                            int zeroy = 0;
            
                                            for (int count = 0; count < filtjikinear; count++)
                                            {
                                                zerox = filtjikiposNEARX[count] + zerox;
                                                zeroy = filtjikiposNEARY[count] + zeroy;
                                            }
                                            jikiX = zerox / filtjikinear;
                                            jikiY = zeroy / filtjikinear;
            
                                        }
                                        else if (filtjiki > 10)
                                        {
                                            int zerox2 = 0;
                                            int zeroy2 = 0;
            
                                            for (int count = 0; count < filtjiki; count++)
                                            {
                                                zerox2 = filtjikiposX[count] + zerox2;
                                                zeroy2 = filtjikiposY[count] + zeroy2;
                                            }
                                            jikiX = zerox2 / filtjiki;
                                            jikiY = zeroy2 / filtjiki;
                                        }
                                        else
                                        {
                                            jikiX = prev_jikiX;
                                            jikiY = prev_jikiY;
            
                                        }
            
                                        // FIND YELLOW TAMA pixels in 1dots
                                        counttama = 0;
                                        for (int pixelYY = 30; pixelYY < (480 - 12); pixelYY++)  // pixel Y
                                        {
                                            for (int pixelXX = 30; pixelXX < (640 - 30); pixelXX++) // pixel X * ch
                                            {
                                                indexR = (pixelXX + (pixelYY) * 640) * 3 + 2; //r
                                                indexG = (pixelXX + (pixelYY) * 640) * 3 + 1; //g
                                                indexB = (pixelXX + (pixelYY) * 640) * 3 + 0; //b
                                                if (srcData[indexR] > 230 && srcData[indexB] < 70 && srcData[indexG] > 120 && srcData[indexG] < 200)  //240 20 100 200
                                                {
                                                    if (counttama < 9000)
                                                    {
                                                        tamaposX[counttama] = pixelXX;
                                                        tamaposY[counttama] = pixelYY;
                                                        counttama++;
                                                    }
                                                }
                                            }
                                        }
            
            
                                        // FILTER TAMApos
                                        int filttama = 0;
                                        int countpix = 0;
                                        if (counttama != 0)
                                        {
                                            for (int counta = 0; counta < counttama; counta++)
                                            {
                                                countpix = 0;
                                                for (int xx = 0; xx < 10; xx++)
                                                {
                                                    for (int yy = 0; yy < 10; yy++)
                                                    {
                                                        indexR = (tamaposX[counta] + xx - 5 + (tamaposY[counta] + yy - 5) * bmpData.Width) * 3 + 2; //r
                                                        indexG = (tamaposX[counta] + xx - 5 + (tamaposY[counta] + yy - 5) * bmpData.Width) * 3 + 1; //g
                                                        indexB = (tamaposX[counta] + xx - 5 + (tamaposY[counta] + yy - 5) * bmpData.Width) * 3 + 0; //b
            
                                                        if (srcData[indexR] > 230 && srcData[indexB] < 70 && srcData[indexG] > 120 && srcData[indexG] < 200)  //240 20 100 200
                                                        {
                                                            countpix++;
                                                        }
                                                    }
                                                }
                                                if (countpix > 15)  //15
                                                {
                                                    if (filttama < 9000)
                                                    {
                                                        filt_tamaposX[filttama] = tamaposX[counta];
                                                        filt_tamaposY[filttama] = tamaposY[counta];
                                                        filttama++;
                                                    }
                                                }
                                            }
                                        }
            
            
                                        int nv = (int)(numericUpDown3.Value);
                                        if (nv < 0)
                                        {
                                            nv = -nv;
                                        }
            
            
                                        ///   back ground into BLACK  //!!
                                        ///   
            
            
                                        //  for (int pixelY = offset + nv * 5 + 5; pixelY < (480 - offset - 5 - nv * 5); pixelY++)  // pixel Y
            
            
                                        Parallel.For(offset + nv * 5 + 5, 480 - offset - 5 - nv * 5, pixelY =>
                                        //for (int counta = 0; counta < count_targetNEAR; counta++)
                                        {
            
                                            for (int pixelX = offset + nv * 5 + 5; pixelX < (640 - offset - 5 - nv * 5); pixelX++) // pixel X * ch
                                            {
                                                int indexR2, indexG2, indexB2;
                                                int chB2, chG2, chR2;
                                                indexR2 = (pixelX - xx1 + (pixelY - offset - yy1) * 640) * 3 + 2; //r
                                                indexG2 = (pixelX - xx1 + (pixelY - offset - yy1) * 640) * 3 + 1; //r
                                                indexB2 = (pixelX - xx1 + (pixelY - offset - yy1) * 640) * 3 + 0; //r
                                                chB2 = prevData1[indexB2] + prevData1[indexB2 - 3] + prevData1[indexB2 + 3] + prevData1[indexB2 + 1920] + prevData1[indexB2 - 1920];
                                                chG2 = prevData1[indexG2] + prevData1[indexG2 - 3] + prevData1[indexG2 + 3] + prevData1[indexG2 + 1920] + prevData1[indexG2 - 1920];
                                                chR2 = prevData1[indexR2] + prevData1[indexR2 - 3] + prevData1[indexR2 + 3] + prevData1[indexR2 + 1920] + prevData1[indexR2 - 1920];
                                                indexR2 = (pixelX + pixelY * 640) * 3 + 2; //r
                                                indexG2 = (pixelX + pixelY * 640) * 3 + 1; //g
                                                indexB2 = (pixelX + pixelY * 640) * 3 + 0; //b
            
                                                int nowB2, nowG2, nowR2;
                                                nowB2 = prevData0[indexB2] + prevData0[indexB2 - 3] + prevData0[indexB2 + 3] + prevData0[indexB2 + 1920] + prevData0[indexB2 - 1920];
                                                nowG2 = prevData0[indexG2] + prevData0[indexG2 - 3] + prevData0[indexG2 + 3] + prevData0[indexG2 + 1920] + prevData0[indexG2 - 1920];
                                                nowR2 = prevData0[indexR2] + prevData0[indexR2 - 3] + prevData0[indexR2 + 3] + prevData0[indexR2 + 1920] + prevData0[indexR2 - 1920];
            
                                                if ((chB2 - nowB2) * (chB2 - nowB2) / (chB2 + 1) + (chG2 - nowG2) * (chG2 - nowG2) / (chG2 + 1) + (chR2 - nowR2) * (chR2 - nowR2) / (chR2 + 1) < bor * 100)
                                                {
                                                    srcData[indexR2] = (byte)(srcData[indexR2] / 2);
                                                    srcData[indexG2] = (byte)(srcData[indexG2] / 2);
                                                    srcData[indexB2] = (byte)(srcData[indexB2] / 2);
                                                }
                                                else
                                                {
                                                        //  srcData[indexR] = (byte)(srcData[indexR]- srcData[indexR] * nv50 / 30);
                                                        //   srcData[indexG] = (byte)(srcData[indexG]- srcData[indexG] *nv50 / 30);
            
            
                                                }
                                            }
                                        });
            
            
                                        // BackGround enemy?? in yellow
                                        int counttama2 = 0;
                                       
                                        
                                        for (int pixelYY = 30; pixelYY < (jikiY - 40); pixelYY++)  // pixel Y
                                        
                                        {
                                            for (int pixelXX = 140; pixelXX < (640 - 140); pixelXX++) // pixel X * ch
                                            {
                                                indexR = (pixelXX + (pixelYY) * 640) * 3 + 2; //r
                                                indexG = (pixelXX + (pixelYY) * 640) * 3 + 1; //g
                                                indexB = (pixelXX + (pixelYY) * 640) * 3 + 0; //b
                                                if (srcData[indexR] > 100 && srcData[indexG] > 100)  //240 20 100 200
                                                {
                                                    if (counttama2 < 9000)
                                                    {
                                                        tamaposX2[counttama2] = pixelXX;
                                                        tamaposY2[counttama2] = pixelYY;
                                                        counttama2++;
                                                    }
                                                }
                                            }
                                        }
            
            
                                        // BackGround enemy?? in yellow
            
                                        for (int pixelYY = jikiY+40; pixelYY < 440; pixelYY++)  // pixel Y
                                        {
                                            for (int pixelXX = 140; pixelXX < (640 - 140); pixelXX++) // pixel X * ch
                                            {
                                                indexR = (pixelXX + (pixelYY) * 640) * 3 + 2; //r
                                                indexG = (pixelXX + (pixelYY) * 640) * 3 + 1; //g
                                                indexB = (pixelXX + (pixelYY) * 640) * 3 + 0; //b
                                                if (srcData[indexR] > 100 && srcData[indexG] > 100)  //240 20 100 200
                                                {
                                                    if (counttama2 < 9000)
                                                    {
                                                        tamaposX2[counttama2] = pixelXX;
                                                        tamaposY2[counttama2] = pixelYY;
                                                        counttama2++;
                                                    }
                                                }
                                            }
                                        }
            
            
            
                                        // FILTER TAMApos2 (ENEMY)
                                        int filttama2 = 0;
                                        int countpix5 = 0;
                                        int countpix6 = 0;
                                        if (counttama2 != 0)
                                        {
                                            for (int counta = 0; counta < counttama2; counta++)
                                            {
                                                countpix5 = 0;
                                                countpix6 = 0;
                                                for (int xx = 0; xx < 6; xx++)
                                                {
                                                    for (int yy = 0; yy < 6; yy++)
                                                    {
                                                        indexR = (tamaposX2[counta] + xx - 3 + (tamaposY2[counta] + yy - 3) * 640) * 3 + 2; //r
                                                        indexG = (tamaposX2[counta] + xx - 3 + (tamaposY2[counta] + yy - 3) * 640) * 3 + 1; //g
                                                        indexB = (tamaposX2[counta] + xx - 3 + (tamaposY2[counta] + yy - 3) * 640) * 3 + 0; //b
            
                                                        if (srcData[indexR] > 90 & srcData[indexG] > 90 && srcData[indexB]>90)  //240 20 100 200
                                                        {
                                                            countpix5++;
                                                        }
                                                        if (prevData1[indexR] > 140 & prevData1[indexG] > 140)  //240 20 100 200
                                                        {
                                                            countpix6++;
                                                        }
                                                    }
                                                }
                                                if (countpix5 > 15 && countpix6 > 18)  //15
                                                {
                                                    if (filttama2 < 9000)
                                                    {
                                                        filt_tamaposX2[filttama2] = tamaposX2[counta];
                                                        filt_tamaposY2[filttama2] = tamaposY2[counta];
                                                        filttama2++;
                                                    }
                                                }
                                            }
                                        }
            
            
                                        //////////////// END OF LOCKBITS ///////////////
            
                                        System.Runtime.InteropServices.Marshal.Copy(srcData, 0, bmpData.Scan0, srcData.Length);
                                        bitmap1.UnlockBits(bmpData);
            
                                        g.DrawImage(bitmap1, 0, 0);
            
            
                                        //  WRITE TAMA
                                        int dangertama = 0;
                                        int bibunX = 0;
                                        int bibunY = 0;
                                        int bombflag = 0;
                                        int atrisk = 0;
            
                                        for (int countb = 0; countb < filttama; countb++)
                                        {
                                            int dist = 0;
                                            dist = (filt_tamaposX[countb] - jikiX) * (filt_tamaposX[countb] - jikiX) + (filt_tamaposY[countb] - jikiY + offy + 10) * (filt_tamaposY[countb] - jikiY + offy + 10);
                                            if (dist > 25000)
                                            {
                                                g.DrawRectangle(Pens.White, filt_tamaposX[countb] - 5, filt_tamaposY[countb] - 5, 10, 10);
            
                                            }
                                            else
                                            {
                                                g.DrawRectangle(Pens.Red, filt_tamaposX[countb] - 5, filt_tamaposY[countb] - 5, 10, 10);
                                                dangertama++;
                                                g.DrawLine(Pens.Gray, filt_tamaposX[countb], filt_tamaposY[countb], jikiX, jikiY - offy);
                                                int distanceratio = 1;
                                                if (dist < 3500)
                                                {
                                                    distanceratio = 4;
                                                    bombflag++;
                                                    atrisk = atrisk + 3;
            
                                                }
                                                else if (dist < 9500)
                                                {
                                                    distanceratio = 3;
                                                    atrisk++;
                                                    atrisk++;
                                                }
                                                else if (dist < 13500)
                                                {
                                                    distanceratio = 2;
                                                    atrisk++;
                                                }
                                                else
                                                {
                                                    distanceratio = 1;
                                                }
            
                                                bibunX = bibunX + (jikiX - filt_tamaposX[countb]) * distanceratio;
                                                bibunY = bibunY + (jikiY - offy - filt_tamaposY[countb]) * distanceratio;
                                            }
                                        }
            
                                        for (int countb = 0; countb < filttama2; countb++)
                                        {
                                            int dist = 0;
                                            dist = (filt_tamaposX2[countb] - jikiX) * (filt_tamaposX2[countb] - jikiX) + (filt_tamaposY2[countb] - jikiY + offy + 10) * (filt_tamaposY2[countb] - jikiY + offy + 10);
                                            if (dist > 25000)
                                            {
                                                g.DrawRectangle(Pens.Yellow, filt_tamaposX2[countb] - 5, filt_tamaposY2[countb] - 5, 10, 10);
            
                                            }
                                            else
                                            {
                                                g.DrawRectangle(Pens.Orange, filt_tamaposX2[countb] - 5, filt_tamaposY2[countb] - 5, 10, 10);
                                                dangertama++;
                                                g.DrawLine(Pens.Gray, filt_tamaposX2[countb], filt_tamaposY2[countb], jikiX, jikiY - offy);
                                                int distanceratio = 1;
                                                if (dist < 3500)
                                                {
                                                    distanceratio = 6;
                                                    
            
                                                }
                                                else if (dist<5000)
                                                {
                                                    distanceratio = 4;
                                                    atrisk++;
                                                    atrisk++;
            
            
                                                }
                                                else if (dist < 9500)
                                                {
                                                    distanceratio = 3;
                                                    atrisk++;
                                                    atrisk++;
                                                }
                                                else if (dist < 13500)
                                                {
                                                    distanceratio = 2;
                                                    atrisk++;
                                                }
                                                else
                                                {
                                                    distanceratio = 1;
                                                }
            
                                                bibunX = bibunX + (jikiX - filt_tamaposX2[countb]) * distanceratio;
                                                bibunY = bibunY + (jikiY - offy - filt_tamaposY2[countb]) * distanceratio;
                                            }
                                        }
            
            
                                        // define bibun & risk
                                        if (dangertama != 0)
                                        {
                                            bibunX = bibunX / dangertama;
                                            bibunY = bibunY / dangertama;
                                            g.DrawLine(Pens.Red, jikiX + bibunX, jikiY - offy + bibunY, jikiX, jikiY - offy);
                                            g.DrawRectangle(Pens.Red, jikiX + bibunX, jikiY - offy + bibunY, 10, 10);
            
                                            if (atrisk > 20)
                                            {
                                                label19.Text = "risky";
                                                label19.BackColor = Color.Red;
                                            }
                                            else if (atrisk > 7)
                                            {
                                                label19.Text = "so so";
                                                label19.BackColor = Color.Green;
                                            }
                                            else
                                            {
                                                label19.Text = "safe";
                                                label19.BackColor = Color.LightGray;
                                            }
            
            
                                        }
                                        else
                                        {
                                            label19.Text = "safe";
                                            label19.BackColor = Color.LightGray;
                                        }
            
            
                                        // are you BOMB?
            
                                        if (controllerON > 0 && supportflag == 0 && playON > 0)
                                        {
                                            if (dangertama * atrisk > 4000)
                                            {
                                                bombflag++;
                                                if (bombflag > 10)
                                                {
                                                    label19.Text = "BOOOOMB!!!";
                                                    label19.BackColor = Color.Red;
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 12);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16analogue 32START
            
                                                    button13.BackColor = Color.Red;
            
                                                    button15.BackColor = Color.Red;
                                                    bombflag = 0;
                                                }
                                            }
                                            else
                                            {
                                                bombflag = 0;
                                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 4);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16analogue 32START
                                                button15.BackColor = Color.Red;
                                                button13.BackColor = Color.LightGray;
                                            }
            
                                        }
                                        // go back to center
                                        if (jikiX < 160 && supportflag == 0)
                                        {
                                            bibunX = +10;
                                            bibunY = 20;
                                            atrisk = 10;
                                        }
                                        if (jikiX > 480 && supportflag == 0)
                                        {
                                            bibunY = +10;
                                            bibunX = -20;
                                            atrisk = 10;
                                        }
                                        if (jikiY < 50 && supportflag == 0)
            
                                        {
                                            bibunY = +10;
                                            bibunX = 1;
                                            atrisk = 10;
                                        }
            
                                        if (jikiY > 430 && supportflag == 0)
                                        {
                                            bibunY = -10;
                                            bibunX = 1;
                                            atrisk = 10;
                                        }
                                        if (dangertama < 1 && atrisk < 2 && supportflag == 0)  // go back
                                        {
            
                                            bibunX = -(jikiX - 320);
                                            bibunY = -(jikiY - offy - 360);
            
                                            //    if ((bibunX * bibunX + bibunY * bibunY) < 800)
                                            //
                                            //  {
                                            //  atrisk = 0;
                                            // }
                                        }
            
            
                                        // calc angle
                                        double th = 0;
                                        th = Math.Atan2(-bibunY, +bibunX);
                                        th = th * 180 / Math.PI;
                                        if (supportflag == 0)
                                        {
                                            DateTime dt = DateTime.Now;
                                            if ((dt.Second % 8) < 4)
            
                                            {
                                                th = th + (int)numericUpDown8.Value;
                                            }
                                            else
                                            {
                                                th = th - (int)numericUpDown8.Value;
                                            }
                                        }
                                        if (th < 0)
                                        {
                                            th = th + 360;
                                        }
                                        if (th > 360)
                                        {
                                            th = th - 360;
                                        }
            
                                        label20.Text = "angl" + th.ToString("n1");
                                        // TELL ME risk when being supported
                                        if (supportflag == 1)
                                        {
                                            atrisk = atrisk * 4;      // adjust risk when being supported
                                            if (atrisk > 10)
                                            {
                                                pictureBox2.BackColor = Color.Blue;
            
                                            }
                                            else
                                            {
                                                pictureBox2.BackColor = Color.LightGray;
                                            }
                                        }
                                        else
                                        {
                                            atrisk = atrisk * 4;
                                        }
            
            
            
            
                                        // STICK operation for AUTOPLAY
                                        if (atrisk > 5 && playON != 0)
                                        {
                                            if (th < 75)
                                            {
                                                ang1 = 0;
                                                {
                                                    if (controllerON > 0)
                                                    {
                                                        daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 1);//1Right,2UP,4Left,8Down
                                                    }
                                                    button8.BackColor = Color.Red;
                                                    button8.Refresh();
                                                    button7.BackColor = Color.LightGray;
                                                    button6.BackColor = Color.LightGray;
                                                    button9.BackColor = Color.LightGray;
                                                    x1 = 3;
                                                    y1 = 0;
                                                }
                                            }
                                            else if (th < 85)
                                            {
                                                ang1 = 45;  //RIGHT UP
                                                {
                                                    if (controllerON > 0)
                                                    {
                                                        daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 3);//1Right,2UP,4Left,8Down
                                                    }
                                                    button6.BackColor = Color.Red;
                                                    button8.BackColor = Color.Red;
                                                    button8.Refresh();
                                                    button6.Refresh();
                                                    button7.BackColor = Color.LightGray;
                                                    button8.BackColor = Color.LightGray;
                                                    x1 = 2;
                                                    y1 = -2;
                                                }
                                            }
                                            else if (th < 95)
                                            {
                                                ang1 = 90; // UP
                                                if (controllerON > 0)
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 2);//1Right,2UP,4Left,8Down
                                                }
                                                button6.BackColor = Color.Red;
                                                button6.Refresh();
                                                button7.BackColor = Color.LightGray;
                                                button8.BackColor = Color.LightGray;
                                                button9.BackColor = Color.LightGray;
                                                x1 = 0;
                                                y1 = -3;
            
                                            }
                                            else if (th < 105)
                                            {
                                                ang1 = 135;
                                                if (controllerON > 0) // LEFTUP
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 6);//1Right,2UP,4Left,8Down
                                                }
                                                button6.BackColor = Color.Red;
                                                button7.BackColor = Color.Red;
                                                button6.Refresh();
                                                button7.Refresh();
                                                button8.BackColor = Color.LightGray;
                                                button9.BackColor = Color.LightGray;
                                                x1 = -2;
                                                y1 = -2;
                                            }
                                            else if (th < 255)
                                            {
                                                ang1 = 180;
                                                if (controllerON > 0) // LEFT
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 4);//1Right,2UP,4Left,8Down
                                                }
                                                button7.BackColor = Color.Red;
                                                button7.Refresh();
                                                button8.BackColor = Color.LightGray;
                                                button9.BackColor = Color.LightGray;
                                                button6.BackColor = Color.LightGray;
                                                x1 = -3;
                                                y1 = 0;
                                            }
                                            else if (th < 265)
                                            {
                                                ang1 = 225;
                                                if (controllerON > 0) // LEFT DOWN
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 12);//1Right,2UP,4Left,8Down
                                                }
                                                button7.BackColor = Color.Red;
                                                button9.BackColor = Color.Red;
                                                button7.Refresh();
                                                button9.Refresh();
                                                button8.BackColor = Color.LightGray;
                                                button6.BackColor = Color.LightGray;
                                                x1 = -2;
                                                y1 = 2;
                                            }
                                            else if (th < 275)
                                            {
                                                ang1 = 270;
                                                if (controllerON > 0) // DOWN
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 8);//1Right,2UP,4Left,8Down
                                                }
                                                button9.BackColor = Color.Red;
                                                button9.Refresh();
                                                button8.BackColor = Color.LightGray;
                                                button6.BackColor = Color.LightGray;
                                                button7.BackColor = Color.LightGray;
                                                y1 = 3;
                                                x1 = 0;
                                            }
                                            else if (th < 285)
                                            {
                                                ang1 = 315;
                                                if (controllerON > 0) // DOWN RIGHT
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 9);//1Right,2UP,4Left,8Down
                                                }
                                                button9.BackColor = Color.Red;
                                                button9.Refresh();
                                                button8.BackColor = Color.Red;
                                                button8.Refresh();
                                                button6.BackColor = Color.LightGray;
                                                button7.BackColor = Color.LightGray;
                                                x1 = 2;
                                                y1 = 2;
                                            }
                                            else
                                            {
                                                ang1 = 0;
                                                if (controllerON > 0)
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 1);//1Right,2UP,4Left,8Down
                                                }
                                                button8.BackColor = Color.Red;
                                                button8.Refresh();
                                                button8.Refresh();
                                                button7.BackColor = Color.LightGray;
                                                button6.BackColor = Color.LightGray;
                                                button9.BackColor = Color.LightGray;
                                                x1 = 3;
                                                y1 = 0;
                                            }
                                        }
            
                                        if (atrisk <= 10 && playON != 0 && supportflag == 0 && controllerON > 0)
                                        {
                                            if (jikiX > 180 && jikiX < 460 && jikiY > 40 && jikiY < 440)
                                            {
                                                DateTime dt2 = DateTime.Now;
                                                if ((dt2.Second % 2) < 1)
                                                {
                                                    if (jikiX < 420)
                                                    {
                                                        daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 1);//1Right,2UP,4Left,8Down
                                                        x1 = 3;
                                                        y1 = 0;
                                                    }
                                                }
                                                else
                                                {
                                                    if (jikiX > 220)
                                                    {
                                                        daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 4);//1Right,2UP,4Left,8Down
                                                        x1 = -3;
                                                        y1 = 0;
                                                    }
                                                }
                                            }
                                        }
            
            
                                        ////////////////////NEEDED????
                                        if (atrisk <= 3 && supportflag > 0)
                                        {
                                            if (controllerON > 0)
                                            {
                                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);//1Right,2UP,4Left,8Down
                                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);
                                            }
                                            button8.BackColor = Color.LightGray;
                                            button7.BackColor = Color.LightGray;
                                            button6.BackColor = Color.LightGray;
                                            button9.BackColor = Color.LightGray;
                                        }
            
                                        // are you moving?
            
                                        label21.Text = "restart" + startflag.ToString();
            
            
                                        // restart??
                                        if (jikiX > 270 && jikiX < 340 && jikiY > 454 && jikiY < 460)
                                        {
                                            if (((prev_jikiX - jikiX) * (prev_jikiX - jikiX)) < 25)
                                            {
                                                startflag++;
            
            
                                                if (startflag > 40 && startflag<43)
                                                {
                                                    if (playON != 0 && controllerON > 0)
                                                    {
                                                        daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 20);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16SEL 32START
            
                                                    }
                                                }
            
                                                if (startflag > 90)
                                                {
                                                    startflag = 0;
                                                    if (playON != 0)
                                                    {
                                                        button16.BackColor = Color.Red;
                                                        if (controllerON > 0)
                                                        {
                                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 4);   //1Right,2UP,4Left,8Down
                                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 1);   //1Right,2UP,4Left,8Down
                                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 4);
                                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 20);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16SEL 32START
                                                            for (int k = 0; k < 5000; k++)
                                                            {
                                                                for (int l = 0; l < 3; l++)
                                                                {
                                                                }
                                                            }
                                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);   //1,2,4,8
                                                            for (int k = 0; k < 5000; k++)
                                                            {
                                                                for (int l = 0; l < 3; l++)
                                                                {
                                                                }
                                                            }
                                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 20);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16SEL 32START
                                                            for (int k = 0; k < 5000; k++)
                                                            {
            
                                                            }
                                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);   //1,2,4,8
            
            
            
            
                                                            if (conflag > 0)
                                                            {
            
                                                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 20);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16SEL 32START
            
                                                                waitsecone();
            
                                                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);   //1Right,2UP,4Left,8Down
                                                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 36);   //1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16analogue 32START
                                                                button10.BackColor = Color.Red;
                                                                button10.Refresh();
                                                                waitsecone();
                                                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);   //1,2,4,8
                                                                button10.BackColor = Color.LightGray;
                                                                button10.Refresh();
            
                                                                conttimes++;
                                                                label27.Text = "Continue : " + conttimes.ToString();
            
                                                                //////////////////////////////
                                                               // if (numericUpDown8.Value < 89)
                                                               // {
                                                               //     numericUpDown8.Value++;
                                                               // }
                                                               // else
                                                               // {
                                                               //     numericUpDown8.Value = 0;
                                                               // }
            
            
            
                                                            }
            
                                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 36);   //1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16analogue 32START
                                                            button10.BackColor = Color.Red;
                                                            button10.Refresh();
                                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);   //1,2,4,8
                                                            button10.BackColor = Color.LightGray;
                                                            button10.Refresh();
                                                            //daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 1);   //1Right,2UP,4Left,8Down
                                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 4);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16analogue 32START
                                                            button15.BackColor = Color.Red;
                                                            button15.Refresh();
            
            
                                                        }
                                                    }
                                                    else
                                                    {
                                                        startflag = 0;
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                startflag--;
                                                if (startflag < 0) { startflag = 0; }
                                            }
                                            
                                        }
            
                                        
            
                                        prev_jikiX = jikiX;
                                        prev_jikiY = jikiY;
                                        label4.Text = "Jikix" + jikiX.ToString();
                                        label5.Text = "Jikiy" + jikiY.ToString();
                                        label6.Text = "jikixFar" + count_target.ToString();
                                        label7.Text = "jikixNear" + count_targetNEAR.ToString();
                                        label14.Text = "tamapixels" + counttama.ToString();
                                        label16.Text = "filtama" + filttama.ToString();
                                        label17.Text = "filtjikinum" + filtjikinear.ToString();
                                        label18.Text = "jikinum" + filtjiki.ToString();
                                        label22.Text = "riskvalue" + atrisk.ToString();
            
                                        label29.Text = "BGpixels" + counttama2.ToString();
                                        label30.Text = "filtBG" + filttama2.ToString();
            
            
            
            
                                        ///   back ground into BLACK  //!!
            
            
            
                                        Parallel.For(50, 480 - 50, pixelY =>
            
                                          // for (int pixelY = 50; pixelY < (480 - 50); pixelY++)  // pixel Y
                                          {
                                              for (int pixelX = 140; pixelX < (640 - 140); pixelX++) // pixel X * ch
                                            {
                                                  int indexR7, indexG7, indexB7;
                                                  indexR7 = (pixelX + pixelY * 640) * 3; //r
                                                indexG7 = (pixelX + pixelY * 640) * 3 + 1; //r
                                                indexB7 = (pixelX + pixelY * 640) * 3 + 2; //r
                                                  int chB7, chG7, chR7;
                                                  chB7 = prevData1[indexB7] + prevData1[indexB7 - 3] + prevData1[indexB7 + 3] + prevData1[indexB7 + 1920] + prevData1[indexB7 - 1920];
                                                  chG7 = prevData1[indexG7] + prevData1[indexG7 - 3] + prevData1[indexG7 + 3] + prevData1[indexG7 + 1920] + prevData1[indexG7 - 1920];
                                                  chR7 = prevData1[indexR7] + prevData1[indexR7 - 3] + prevData1[indexR7 + 3] + prevData1[indexR7 + 1920] + prevData1[indexR7 - 1920];
                                                  indexR7 = (pixelX + pixelY * 640) * 3; //r
                                                indexG7 = (pixelX + pixelY * 640) * 3 + 1; //g
                                                indexB7 = (pixelX + pixelY * 640) * 3 + 2; //b
            
                                                  int nowB7, nowG7, nowR7;
                                                  nowB7 = prevData0[indexB7] + prevData0[indexB7 - 3] + prevData0[indexB7 + 3] + prevData0[indexB7 + 1920] + prevData0[indexB7 - 1920];
                                                  nowG7 = prevData0[indexG7] + prevData0[indexG7 - 3] + prevData0[indexG7 + 3] + prevData0[indexG7 + 1920] + prevData0[indexG7 - 1920];
                                                  nowR7 = prevData0[indexR7] + prevData0[indexR7 - 3] + prevData0[indexR7 + 3] + prevData0[indexR7 + 1920] + prevData0[indexR7 - 1920];
            
                                                  if ((chB7 - nowB7) * (chB7 - nowB7) / (chB7 + 1) + (chG7 - nowG7) * (chG7 - nowG7) / (chG7 + 1) + (chR7 - nowR7) * (chR7 - nowR7) / (chR7 + 1) < bor * 100)
                                                  {
                                                      srcData[indexR7] = (byte)(srcData[indexR7] / 2);
                                                      srcData[indexG7] = (byte)(srcData[indexG7] / 2);
                                                      srcData[indexB7] = (byte)(srcData[indexB7] / 2);
                                                  }
                                                  else
                                                  {
                                                    //  srcData[indexR] = (byte)(srcData[indexR]- srcData[indexR] * nv50 / 30);
                                                    //   srcData[indexG] = (byte)(srcData[indexG]- srcData[indexG] *nv50 / 30);
                                                    //   srcData[indexB] = (byte)(srcData[indexB] - srcData[indexB]  * nv50 / 30);
                                                }
                                              }
                                          });
            
            
            
            
            
            
                                        prev_jikiX = jikiX;
                                        prev_jikiY = jikiY;
            
            
            
            
                                        // shift X1 Y1
                                        for (int i = 98; i >= 0; i--)
                                        {
                                            x_history[i + 1] = x_history[i];
                                            y_history[i + 1] = y_history[i];
                                        }
                                        x_history[0] = x1;
                                        y_history[0] = y1;
            
                                        // DISPOSE && UNLOCK for display
                                        pictureBox2.Image = canvasLEFT;
                                        for (int counta = 0; counta < filtjikinear; counta++)
                                        {
                                            g.DrawRectangle(Pens.Green, targetNEARX[counta] - 5, targetNEARY[counta] - 5, 10, 10);
                                        }
            
                                        //Write JIKI 
                                        if (jikiX >= 0 && jikiX < 640 && jikiY >= 0 && jikiY < 480)
                                        {
                                            g.DrawRectangle(Pens.Blue, jikiX - 15, jikiY - 15 - offy, 30, 30);
                                            g.DrawEllipse(Pens.Green, jikiX - 25, jikiY - 25 - offy, 50, 50);
            
                                        }
                                        //Write escapeline
                                        if (dangertama != 0)
                                        {
                                            g.DrawLine(Pens.Red, jikiX + bibunX, jikiY - offy + bibunY, jikiX, jikiY - offy);
                                            g.DrawRectangle(Pens.Red, jikiX + bibunX, jikiY - offy + bibunY, 10, 10);
                                        }
            
                                        if (filttama != 0)
                                        {
                                            for (int countb = 0; countb < filttama; countb++)
                                            {
                                                int dist = 0;
                                                dist = (filt_tamaposX[countb] - jikiX) * (filt_tamaposX[countb] - jikiX) + (filt_tamaposY[countb] - jikiY + offy + 10) * (filt_tamaposY[countb] - jikiY + offy + 10);
                                                if (dist > 25000)
                                                {
                                                    g.DrawRectangle(Pens.Green, filt_tamaposX[countb] - 5, filt_tamaposY[countb] - 5, 10, 10);
            
                                                }
                                                else
                                                {
                                                    g.DrawRectangle(Pens.Red, filt_tamaposX[countb] - 5, filt_tamaposY[countb] - 5, 10, 10);
                                                    g.DrawLine(Pens.Gray, filt_tamaposX[countb], filt_tamaposY[countb], jikiX, jikiY - offy);
                                                }
                                            }
                                        }
            
                                        if (filttama2 != 0)
                                        {
                                            for (int countb = 0; countb < filttama2; countb++)
                                            {
                                                int dist = 0;
                                                dist = (filt_tamaposX2[countb] - jikiX) * (filt_tamaposX2[countb] - jikiX) + (filt_tamaposY2[countb] - jikiY + offy + 10) * (filt_tamaposY2[countb] - jikiY + offy + 10);
                                                if (dist > 25000)
                                                {
                                                    g.DrawRectangle(Pens.Green, filt_tamaposX2[countb] - 5, filt_tamaposY2[countb] - 5, 10, 10);
            
                                                }
                                                else
                                                {
                                                    g.DrawRectangle(Pens.Yellow, filt_tamaposX2[countb] - 5, filt_tamaposY2[countb] - 5, 10, 10);
                                                    g.DrawLine(Pens.Orange, filt_tamaposX2[countb], filt_tamaposY2[countb], jikiX, jikiY - offy);
                                                }
                                            }
                                        }
            
            
            
                                        g.Dispose();
                                    }
                                    
                                    //@@@@@@@@@@@@@@@@@@@@@@ RAIDEN RAIDEN RAIDEN
                                    if (numericUpDown7.Value == 1)
                                    {
            
                                        double nv5 = (double)numericUpDown5.Value;
            
            
                                        for (int pixelY = 0; pixelY < 480; pixelY++)  // pixel Y
            
            
            
            
                                        {
                                            for (int pixelX = 0; pixelX < 640; pixelX++) // pixel X * ch
                                            {
                                                indexR = (pixelX + pixelY * 640) * 3 + 2; //r
                                                indexG = (pixelX + pixelY * 640) * 3 + 1; //g
                                                indexB = (pixelX + pixelY * 640) * 3 + 0; //b
            
                                                // SLIDE previous image data for ONESTEP
                                                prevData2[indexR] = prevData1[indexR];  //  -2
                                                prevData2[indexG] = prevData1[indexG];
                                                prevData2[indexB] = prevData1[indexB];
                                                prevData1[indexR] = (byte)((double)(prevData0[indexR]) * nv5 * 0.1 + (double)(prevData1[indexR]) * (1 - nv5 * 0.1));  // -1
                                                prevData1[indexG] = (byte)((double)(prevData0[indexG]) * nv5 * 0.1 + (double)(prevData1[indexG]) * (1 - nv5 * 0.1));  // -1
                                                prevData1[indexB] = (byte)((double)(prevData0[indexB]) * nv5 * 0.1 + (double)(prevData1[indexB]) * (1 - nv5 * 0.1));  // -1
                                                prevData0[indexR] = srcData[indexR];    // now
                                                prevData0[indexG] = srcData[indexG];
                                                prevData0[indexB] = srcData[indexB];
                                            }
                                        }
            
                                        int offset = (int)(numericUpDown2.Value);
                                        int chR = 0, chG = 0, chB = 0;
                                        int nowR = 0, nowG = 0, nowB = 0;
                                        int xx1 = x_history[(int)(numericUpDown6.Value)] * (int)(numericUpDown3.Value);
                                        int yy1 = y_history[(int)(numericUpDown6.Value)] * (int)(numericUpDown3.Value);
                                        double bor = 0.0;
                                        bor = Math.Sqrt((double)(numericUpDown4.Value));
            
            
            
                                        // FIND GREEN JIKI from 1dot
                                        count_target = 0;
                                        count_targetNEAR = 0;
            
            
            
                                        for (int pixelY = 50; pixelY < (480 - 9); pixelY++)  // pixel Y
                                        {
                                            for (int pixelX = 30; pixelX < (640 - 30); pixelX++) // pixel X * ch
                                            {
                                                indexR = (pixelX + pixelY * 640) * 3 + 2; //r
                                                indexG = (pixelX + pixelY * 640) * 3 + 1; //g
                                                indexB = (pixelX + pixelY * 640) * 3 + 0; //b
                                                if (srcData[indexR] < 40 && srcData[indexB] < 60 && srcData[indexG] > 245) // 20 20 200
                                                {
                                                    if (count_target < 990)
                                                    {
                                                        targetX[count_target] = pixelX;
                                                        targetY[count_target] = pixelY;
                                                        count_target++;
                                                    }
                                                    if (count_targetNEAR < 990)
                                                    {
                                                        if (((prev_jikiX - pixelX) * (prev_jikiX - pixelX) + (prev_jikiY - pixelY) * (prev_jikiY - pixelY)) < 300)
                                                        {
                                                            targetNEARX[count_targetNEAR] = pixelX;
                                                            targetNEARY[count_targetNEAR] = pixelY;
                                                            count_targetNEAR++;
                                                        }
                                                    }
                                                }
                                            }
                                        }
            
                                        // FILTER GREENjiki in FAR
                                        int filtjiki = 0;
                                        int countpix2 = 0;
            
                                        if (count_target != 0)
                                        {
                                            for (int counta = 0; counta < count_target; counta++)
                                            {
                                                countpix2 = 0;
                                                for (int xx = 0; xx < 5; xx++)
                                                {
                                                    for (int yy = 0; yy < 8; yy++)
                                                    {
                                                        indexR = (targetX[counta] + xx - 2 + (targetY[counta] + yy - 4) * 640) * 3 + 2; //r
                                                        indexG = (targetX[counta] + xx - 2 + (targetY[counta] + yy - 4) * 640) * 3 + 1; //g
                                                        indexB = (targetX[counta] + xx - 2 + (targetY[counta] + yy - 4) * 640) * 3 + 0; //b
                                                        if (srcData[indexR] < 40 && srcData[indexB] < 60 && srcData[indexG] > 245)  // 20 20 200
                                                        {
                                                            countpix2++;
                                                        }
                                                    }
                                                }
                                                if (countpix2 > 5 && filtjiki < 9000)
                                                {
                                                    filtjikiposX[filtjiki] = targetX[counta];
                                                    filtjikiposY[filtjiki] = targetY[counta];
                                                    filtjiki++;
                                                }
                                            }
                                        }
            
            
            
                                        // FILTER GREENjiki in NEAR
                                        int filtjikinear = 0;
                                        int countpix3 = 0;
            
                                        if (count_targetNEAR != 0)
                                        {
                                            for (int counta = 0; counta < count_targetNEAR; counta++)
                                            {
                                                countpix3 = 0;
                                                for (int xx = 0; xx < 5; xx++)
                                                {
                                                    for (int yy = 0; yy < 8; yy++)
                                                    {
                                                        indexR = (targetNEARX[counta] + xx - 2 + (targetNEARY[counta] + yy - 4) * 640) * 3 + 2; //r
                                                        indexG = (targetNEARX[counta] + xx - 2 + (targetNEARY[counta] + yy - 4) * 640) * 3 + 1; //g
                                                        indexB = (targetNEARX[counta] + xx - 2 + (targetNEARY[counta] + yy - 4) * 640) * 3 + 0; //b
                                                        if (srcData[indexR] < 40 && srcData[indexB] < 60 && srcData[indexG] > 245) // 20 20 200
                                                        {
                                                            countpix3++;
                                                        }
                                                    }
                                                }
                                                if (countpix3 > 5 && filtjikinear < 9000)
                                                {
                                                    filtjikiposNEARX[filtjikinear] = targetNEARX[counta];
                                                    filtjikiposNEARY[filtjikinear] = targetNEARY[counta];
                                                    filtjikinear++;
                                                    g.DrawRectangle(Pens.Green, targetNEARX[counta] - 5, targetNEARY[counta] - 5, 10, 10);
                                                }
                                            }
            
                                        }
            
            
                                        //find JIKI from fil or non-filGREEN
                                        if (filtjikinear > 10)
                                        {
                                            int zerox = 0;
                                            int zeroy = 0;
            
                                            for (int count = 0; count < filtjikinear; count++)
                                            {
                                                zerox = filtjikiposNEARX[count] + zerox;
                                                zeroy = filtjikiposNEARY[count] + zeroy;
                                            }
                                            jikiX = zerox / filtjikinear;
                                            jikiY = zeroy / filtjikinear;
            
                                        }
                                        else if (filtjiki > 10)
                                        {
                                            int zerox2 = 0;
                                            int zeroy2 = 0;
            
                                            for (int count = 0; count < filtjiki; count++)
                                            {
                                                zerox2 = filtjikiposX[count] + zerox2;
                                                zeroy2 = filtjikiposY[count] + zeroy2;
                                            }
                                            jikiX = zerox2 / filtjiki;
                                            jikiY = zeroy2 / filtjiki;
                                        }
                                        else
                                        {
                                            jikiX = prev_jikiX;
                                            jikiY = prev_jikiY;
            
                                        }
            
                                        // FIND YELLOW TAMA pixels in 1dots
                                        counttama = 0;
                                        for (int pixelYY = 30; pixelYY < (480 - 12); pixelYY++)  // pixel Y
                                        {
                                            for (int pixelXX = 30; pixelXX < (640 - 30); pixelXX++) // pixel X * ch
                                            {
                                                indexR = (pixelXX + (pixelYY) * 640) * 3 + 2; //r
                                                indexG = (pixelXX + (pixelYY) * 640) * 3 + 1; //g
                                                indexB = (pixelXX + (pixelYY) * 640) * 3 + 0; //b
                                                if (srcData[indexR] > 230 && srcData[indexB] < 70 && srcData[indexG] > 120 && srcData[indexG] < 200)  //240 20 100 200
                                                {
                                                    if (counttama < 9000)
                                                    {
                                                        tamaposX[counttama] = pixelXX;
                                                        tamaposY[counttama] = pixelYY;
                                                        counttama++;
                                                    }
                                                }
                                            }
                                        }
            
            
                                        // FILTER TAMApos
                                        int filttama = 0;
                                        int countpix = 0;
                                        if (counttama != 0)
                                        {
                                            for (int counta = 0; counta < counttama; counta++)
                                            {
                                                countpix = 0;
                                                for (int xx = 0; xx < 10; xx++)
                                                {
                                                    for (int yy = 0; yy < 10; yy++)
                                                    {
                                                        indexR = (tamaposX[counta] + xx - 5 + (tamaposY[counta] + yy - 5) * bmpData.Width) * 3 + 2; //r
                                                        indexG = (tamaposX[counta] + xx - 5 + (tamaposY[counta] + yy - 5) * bmpData.Width) * 3 + 1; //g
                                                        indexB = (tamaposX[counta] + xx - 5 + (tamaposY[counta] + yy - 5) * bmpData.Width) * 3 + 0; //b
            
                                                        if (srcData[indexR] > 230 && srcData[indexB] < 70 && srcData[indexG] > 120 && srcData[indexG] < 200)  //240 20 100 200
                                                        {
                                                            countpix++;
                                                        }
                                                    }
                                                }
                                                if (countpix > 15)  //15
                                                {
                                                    if (filttama < 9000)
                                                    {
                                                        filt_tamaposX[filttama] = tamaposX[counta];
                                                        filt_tamaposY[filttama] = tamaposY[counta];
                                                        filttama++;
                                                    }
                                                }
                                            }
                                        }
            
            
                                        int nv = (int)(numericUpDown3.Value);
                                        if (nv < 0)
                                        {
                                            nv = -nv;
                                        }
            
            
                                        ///   back ground into BLACK  //!!
                                        for (int pixelY = offset + nv * 5 + 5; pixelY < (480 - offset - 5 - nv * 5); pixelY++)  // pixel Y
                                        {
                                            for (int pixelX = offset + nv * 5 + 5; pixelX < (640 - offset - 5 - nv * 5); pixelX++) // pixel X * ch
                                            {
                                                indexR = (pixelX - xx1 + (pixelY - offset - yy1) * 640) * 3 + 2; //r
                                                indexG = (pixelX - xx1 + (pixelY - offset - yy1) * 640) * 3 + 1; //r
                                                indexB = (pixelX - xx1 + (pixelY - offset - yy1) * 640) * 3 + 0; //r
                                                chB = prevData1[indexB] + prevData1[indexB - 3] + prevData1[indexB + 3] + prevData1[indexB + 1920] + prevData1[indexB - 1920];
                                                chG = prevData1[indexG] + prevData1[indexG - 3] + prevData1[indexG + 3] + prevData1[indexG + 1920] + prevData1[indexG - 1920];
                                                chR = prevData1[indexR] + prevData1[indexR - 3] + prevData1[indexR + 3] + prevData1[indexR + 1920] + prevData1[indexR - 1920];
                                                indexR = (pixelX + pixelY * 640) * 3 + 2; //r
                                                indexG = (pixelX + pixelY * 640) * 3 + 1; //g
                                                indexB = (pixelX + pixelY * 640) * 3 + 0; //b
            
                                                nowB = prevData0[indexB] + prevData0[indexB - 3] + prevData0[indexB + 3] + prevData0[indexB + 1920] + prevData0[indexB - 1920];
                                                nowG = prevData0[indexG] + prevData0[indexG - 3] + prevData0[indexG + 3] + prevData0[indexG + 1920] + prevData0[indexG - 1920];
                                                nowR = prevData0[indexR] + prevData0[indexR - 3] + prevData0[indexR + 3] + prevData0[indexR + 1920] + prevData0[indexR - 1920];
            
                                                if ((chB - nowB) * (chB - nowB) / (chB + 1) + (chG - nowG) * (chG - nowG) / (chG + 1) + (chR - nowR) * (chR - nowR) / (chR + 1) < bor * 100)
                                                {
                                                    srcData[indexR] = (byte)(srcData[indexR] / 2);
                                                    srcData[indexG] = (byte)(srcData[indexG] / 2);
                                                    srcData[indexB] = (byte)(srcData[indexB] / 2);
                                                }
                                                else
                                                {
                                                    //  srcData[indexR] = (byte)(srcData[indexR]- srcData[indexR] * nv50 / 30);
                                                    //   srcData[indexG] = (byte)(srcData[indexG]- srcData[indexG] *nv50 / 30);
                                                    //   srcData[indexB] = (byte)(srcData[indexB] - srcData[indexB]  * nv50 / 30);
                                                }
                                            }
                                        }
            
                                        // BackGround enemy?? in yellow
                                        int counttama2 = 0;
                                        for (int pixelYY = 30; pixelYY < (jikiY - 40); pixelYY++)  // pixel Y
                                        {
                                            for (int pixelXX = 140; pixelXX < (640 - 140); pixelXX++) // pixel X * ch
                                            {
                                                indexR = (pixelXX + (pixelYY) * 640) * 3 + 2; //r
                                                indexG = (pixelXX + (pixelYY) * 640) * 3 + 1; //g
                                                indexB = (pixelXX + (pixelYY) * 640) * 3 + 0; //b
                                                if (srcData[indexR] > 100 && srcData[indexG] > 100)  //240 20 100 200
                                                {
                                                    if (counttama2 < 9000)
                                                    {
                                                        tamaposX2[counttama2] = pixelXX;
                                                        tamaposY2[counttama2] = pixelYY;
                                                        counttama2++;
                                                    }
                                                }
                                            }
                                        }
            
            
                                        // BackGround enemy?? in yellow
            
                                        for (int pixelYY = jikiY + 40; pixelYY < 440; pixelYY++)  // pixel Y
                                        {
                                            for (int pixelXX = 140; pixelXX < (640 - 140); pixelXX++) // pixel X * ch
                                            {
                                                indexR = (pixelXX + (pixelYY) * 640) * 3 + 2; //r
                                                indexG = (pixelXX + (pixelYY) * 640) * 3 + 1; //g
                                                indexB = (pixelXX + (pixelYY) * 640) * 3 + 0; //b
                                                if (srcData[indexR] > 100 && srcData[indexG] > 100)  //240 20 100 200
                                                {
                                                    if (counttama2 < 9000)
                                                    {
                                                        tamaposX2[counttama2] = pixelXX;
                                                        tamaposY2[counttama2] = pixelYY;
                                                        counttama2++;
                                                    }
                                                }
                                            }
                                        }
            
            
            
                                        // FILTER TAMApos2 (ENEMY)
                                        int filttama2 = 0;
                                        int countpix5 = 0;
                                        int countpix6 = 0;
                                        if (counttama2 != 0)
                                        {
                                            for (int counta = 0; counta < counttama2; counta++)
                                            {
                                                countpix5 = 0;
                                                countpix6 = 0;
                                                for (int xx = 0; xx < 6; xx++)
                                                {
                                                    for (int yy = 0; yy < 6; yy++)
                                                    {
                                                        indexR = (tamaposX2[counta] + xx - 3 + (tamaposY2[counta] + yy - 3) * 640) * 3 + 2; //r
                                                        indexG = (tamaposX2[counta] + xx - 3 + (tamaposY2[counta] + yy - 3) * 640) * 3 + 1; //g
                                                        indexB = (tamaposX2[counta] + xx - 3 + (tamaposY2[counta] + yy - 3) * 640) * 3 + 0; //b
            
                                                        if (srcData[indexR] > 90 & srcData[indexG] > 90 && srcData[indexB] > 90)  //240 20 100 200
                                                        {
                                                            countpix5++;
                                                        }
                                                        if (prevData1[indexR] > 140 & prevData1[indexG] > 140)  //240 20 100 200
                                                        {
                                                            countpix6++;
                                                        }
                                                    }
                                                }
                                                if (countpix5 > 15 && countpix6 > 18)  //15
                                                {
                                                    if (filttama2 < 9000)
                                                    {
                                                        filt_tamaposX2[filttama2] = tamaposX2[counta];
                                                        filt_tamaposY2[filttama2] = tamaposY2[counta];
                                                        filttama2++;
                                                    }
                                                }
                                            }
                                        }
            
            
                                        //////////////// END OF LOCKBITS ///////////////
            
                                        System.Runtime.InteropServices.Marshal.Copy(srcData, 0, bmpData.Scan0, srcData.Length);
                                        bitmap1.UnlockBits(bmpData);
            
                                        g.DrawImage(bitmap1, 0, 0);
            
            
                                        //  WRITE TAMA
                                        int dangertama = 0;
                                        int bibunX = 0;
                                        int bibunY = 0;
                                        int bombflag = 0;
                                        int atrisk = 0;
            
                                        for (int countb = 0; countb < filttama; countb++)
                                        {
                                            int dist = 0;
                                            dist = (filt_tamaposX[countb] - jikiX) * (filt_tamaposX[countb] - jikiX) + (filt_tamaposY[countb] - jikiY + offy + 10) * (filt_tamaposY[countb] - jikiY + offy + 10);
                                            if (dist > 25000)
                                            {
                                                g.DrawRectangle(Pens.White, filt_tamaposX[countb] - 5, filt_tamaposY[countb] - 5, 10, 10);
            
                                            }
                                            else
                                            {
                                                g.DrawRectangle(Pens.Red, filt_tamaposX[countb] - 5, filt_tamaposY[countb] - 5, 10, 10);
                                                dangertama++;
                                                g.DrawLine(Pens.Gray, filt_tamaposX[countb], filt_tamaposY[countb], jikiX, jikiY - offy);
                                                int distanceratio = 1;
                                                if (dist < 3500)
                                                {
                                                    distanceratio = 4;
                                                    bombflag++;
                                                    atrisk = atrisk + 3;
            
                                                }
                                                else if (dist < 9500)
                                                {
                                                    distanceratio = 3;
                                                    atrisk++;
                                                    atrisk++;
                                                }
                                                else if (dist < 13500)
                                                {
                                                    distanceratio = 2;
                                                    atrisk++;
                                                }
                                                else
                                                {
                                                    distanceratio = 1;
                                                }
            
                                                bibunX = bibunX + (jikiX - filt_tamaposX[countb]) * distanceratio;
                                                bibunY = bibunY + (jikiY - offy - filt_tamaposY[countb]) * distanceratio;
                                            }
                                        }
            
                                        for (int countb = 0; countb < filttama2; countb++)
                                        {
                                            int dist = 0;
                                            dist = (filt_tamaposX2[countb] - jikiX) * (filt_tamaposX2[countb] - jikiX) + (filt_tamaposY2[countb] - jikiY + offy + 10) * (filt_tamaposY2[countb] - jikiY + offy + 10);
                                            if (dist > 25000)
                                            {
                                                g.DrawRectangle(Pens.Yellow, filt_tamaposX2[countb] - 5, filt_tamaposY2[countb] - 5, 10, 10);
            
                                            }
                                            else
                                            {
                                                g.DrawRectangle(Pens.Orange, filt_tamaposX2[countb] - 5, filt_tamaposY2[countb] - 5, 10, 10);
                                                dangertama++;
                                                g.DrawLine(Pens.Gray, filt_tamaposX2[countb], filt_tamaposY2[countb], jikiX, jikiY - offy);
                                                int distanceratio = 1;
                                                if (dist < 3500)
                                                {
                                                    distanceratio = 6;
            
            
                                                }
                                                else if (dist < 5000)
                                                {
                                                    distanceratio = 4;
                                                    atrisk++;
                                                    atrisk++;
            
            
                                                }
                                                else if (dist < 9500)
                                                {
                                                    distanceratio = 3;
                                                    atrisk++;
                                                    atrisk++;
                                                }
                                                else if (dist < 13500)
                                                {
                                                    distanceratio = 2;
                                                    atrisk++;
                                                }
                                                else
                                                {
                                                    distanceratio = 1;
                                                }
            
                                                bibunX = bibunX + (jikiX - filt_tamaposX2[countb]) * distanceratio;
                                                bibunY = bibunY + (jikiY - offy - filt_tamaposY2[countb]) * distanceratio;
                                            }
                                        }
            
            
                                        // define bibun & risk
                                        if (dangertama != 0)
                                        {
                                            bibunX = bibunX / dangertama;
                                            bibunY = bibunY / dangertama;
                                            g.DrawLine(Pens.Red, jikiX + bibunX, jikiY - offy + bibunY, jikiX, jikiY - offy);
                                            g.DrawRectangle(Pens.Red, jikiX + bibunX, jikiY - offy + bibunY, 10, 10);
            
                                            if (atrisk > 20)
                                            {
                                                label19.Text = "risky";
                                                label19.BackColor = Color.Red;
                                            }
                                            else if (atrisk > 7)
                                            {
                                                label19.Text = "so so";
                                                label19.BackColor = Color.Green;
                                            }
                                            else
                                            {
                                                label19.Text = "safe";
                                                label19.BackColor = Color.LightGray;
                                            }
            
            
                                        }
                                        else
                                        {
                                            label19.Text = "safe";
                                            label19.BackColor = Color.LightGray;
                                        }
            
            
                                        // are you BOMB?
            
                                        if (controllerON > 0 && supportflag == 0 && playON > 0)
                                        {
                                            if (dangertama * atrisk > 4000)
                                            {
                                                bombflag++;
                                                if (bombflag > 10)
                                                {
                                                    label19.Text = "BOOOOMB!!!";
                                                    label19.BackColor = Color.Red;
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 12);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16analogue 32START
            
                                                    button13.BackColor = Color.Red;
            
                                                    button15.BackColor = Color.Red;
                                                    bombflag = 0;
                                                }
                                            }
                                            else
                                            {
                                                bombflag = 0;
                                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 4);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16analogue 32START
                                                button15.BackColor = Color.Red;
                                                button13.BackColor = Color.LightGray;
                                            }
            
                                        }
                                        // go back to center
                                        if (jikiX < 160 && supportflag == 0)
                                        {
                                            bibunX = +10;
                                            bibunY = 20;
                                            atrisk = 10;
                                        }
                                        if (jikiX > 480 && supportflag == 0)
                                        {
                                            bibunY = +10;
                                            bibunX = -20;
                                            atrisk = 10;
                                        }
                                        if (jikiY < 50 && supportflag == 0)
            
                                        {
                                            bibunY = +10;
                                            bibunX = 1;
                                            atrisk = 10;
                                        }
            
                                        if (jikiY > 430 && supportflag == 0)
                                        {
                                            bibunY = -10;
                                            bibunX = 1;
                                            atrisk = 10;
                                        }
                                        if (dangertama < 1 && atrisk < 2 && supportflag == 0)  // go back
                                        {
            
                                            bibunX = -(jikiX - 320);
                                            bibunY = -(jikiY - offy - 360);
            
                                            //    if ((bibunX * bibunX + bibunY * bibunY) < 800)
                                            //
                                            //  {
                                            //  atrisk = 0;
                                            // }
                                        }
            
            
                                        // calc angle
                                        double th = 0;
                                        th = Math.Atan2(-bibunY, +bibunX);
                                        th = th * 180 / Math.PI;
                                        if (supportflag == 0)
                                        {
                                            DateTime dt = DateTime.Now;
                                            if ((dt.Second % 8) < 4)
            
                                            {
                                                th = th + (int)numericUpDown8.Value;
                                            }
                                            else
                                            {
                                                th = th - (int)numericUpDown8.Value;
                                            }
                                        }
                                        if (th < 0)
                                        {
                                            th = th + 360;
                                        }
                                        if (th > 360)
                                        {
                                            th = th - 360;
                                        }
            
                                        label20.Text = "angl" + th.ToString("n1");
                                        // TELL ME risk when being supported
                                        if (supportflag == 1)
                                        {
                                            atrisk = atrisk * 4;      // adjust risk when being supported
                                            if (atrisk > 10)
                                            {
                                                pictureBox2.BackColor = Color.Blue;
            
                                            }
                                            else
                                            {
                                                pictureBox2.BackColor = Color.LightGray;
                                            }
                                        }
                                        else
                                        {
                                            atrisk = atrisk * 4;
                                        }
            
            
            
            
                                        // STICK operation for AUTOPLAY
                                        if (atrisk > 5 && playON != 0)
                                        {
                                            if (th < 75)
                                            {
                                                ang1 = 0;
                                                {
                                                    if (controllerON > 0)
                                                    {
                                                        daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 1);//1Right,2UP,4Left,8Down
                                                    }
                                                    button8.BackColor = Color.Red;
                                                    button8.Refresh();
                                                    button7.BackColor = Color.LightGray;
                                                    button6.BackColor = Color.LightGray;
                                                    button9.BackColor = Color.LightGray;
                                                    x1 = 3;
                                                    y1 = 0;
                                                }
                                            }
                                            else if (th < 85)
                                            {
                                                ang1 = 45;  //RIGHT UP
                                                {
                                                    if (controllerON > 0)
                                                    {
                                                        daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 3);//1Right,2UP,4Left,8Down
                                                    }
                                                    button6.BackColor = Color.Red;
                                                    button8.BackColor = Color.Red;
                                                    button8.Refresh();
                                                    button6.Refresh();
                                                    button7.BackColor = Color.LightGray;
                                                    button8.BackColor = Color.LightGray;
                                                    x1 = 2;
                                                    y1 = -2;
                                                }
                                            }
                                            else if (th < 95)
                                            {
                                                ang1 = 90; // UP
                                                if (controllerON > 0)
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 2);//1Right,2UP,4Left,8Down
                                                }
                                                button6.BackColor = Color.Red;
                                                button6.Refresh();
                                                button7.BackColor = Color.LightGray;
                                                button8.BackColor = Color.LightGray;
                                                button9.BackColor = Color.LightGray;
                                                x1 = 0;
                                                y1 = -3;
            
                                            }
                                            else if (th < 105)
                                            {
                                                ang1 = 135;
                                                if (controllerON > 0) // LEFTUP
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 6);//1Right,2UP,4Left,8Down
                                                }
                                                button6.BackColor = Color.Red;
                                                button7.BackColor = Color.Red;
                                                button6.Refresh();
                                                button7.Refresh();
                                                button8.BackColor = Color.LightGray;
                                                button9.BackColor = Color.LightGray;
                                                x1 = -2;
                                                y1 = -2;
                                            }
                                            else if (th < 255)
                                            {
                                                ang1 = 180;
                                                if (controllerON > 0) // LEFT
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 4);//1Right,2UP,4Left,8Down
                                                }
                                                button7.BackColor = Color.Red;
                                                button7.Refresh();
                                                button8.BackColor = Color.LightGray;
                                                button9.BackColor = Color.LightGray;
                                                button6.BackColor = Color.LightGray;
                                                x1 = -3;
                                                y1 = 0;
                                            }
                                            else if (th < 265)
                                            {
                                                ang1 = 225;
                                                if (controllerON > 0) // LEFT DOWN
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 12);//1Right,2UP,4Left,8Down
                                                }
                                                button7.BackColor = Color.Red;
                                                button9.BackColor = Color.Red;
                                                button7.Refresh();
                                                button9.Refresh();
                                                button8.BackColor = Color.LightGray;
                                                button6.BackColor = Color.LightGray;
                                                x1 = -2;
                                                y1 = 2;
                                            }
                                            else if (th < 275)
                                            {
                                                ang1 = 270;
                                                if (controllerON > 0) // DOWN
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 8);//1Right,2UP,4Left,8Down
                                                }
                                                button9.BackColor = Color.Red;
                                                button9.Refresh();
                                                button8.BackColor = Color.LightGray;
                                                button6.BackColor = Color.LightGray;
                                                button7.BackColor = Color.LightGray;
                                                y1 = 3;
                                                x1 = 0;
                                            }
                                            else if (th < 285)
                                            {
                                                ang1 = 315;
                                                if (controllerON > 0) // DOWN RIGHT
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 9);//1Right,2UP,4Left,8Down
                                                }
                                                button9.BackColor = Color.Red;
                                                button9.Refresh();
                                                button8.BackColor = Color.Red;
                                                button8.Refresh();
                                                button6.BackColor = Color.LightGray;
                                                button7.BackColor = Color.LightGray;
                                                x1 = 2;
                                                y1 = 2;
                                            }
                                            else
                                            {
                                                ang1 = 0;
                                                if (controllerON > 0)
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 1);//1Right,2UP,4Left,8Down
                                                }
                                                button8.BackColor = Color.Red;
                                                button8.Refresh();
                                                button8.Refresh();
                                                button7.BackColor = Color.LightGray;
                                                button6.BackColor = Color.LightGray;
                                                button9.BackColor = Color.LightGray;
                                                x1 = 3;
                                                y1 = 0;
                                            }
                                        }
            
                                        if (atrisk <= 10 && playON != 0 && supportflag == 0 && controllerON > 0)
                                        {
                                            if (jikiX > 180 && jikiX < 460 && jikiY > 40 && jikiY < 440)
                                            {
                                                DateTime dt2 = DateTime.Now;
                                                if ((dt2.Second % 2) < 1)
                                                {
                                                    if (jikiX < 420)
                                                    {
                                                        daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 1);//1Right,2UP,4Left,8Down
                                                        x1 = 3;
                                                        y1 = 0;
                                                    }
                                                }
                                                else
                                                {
                                                    if (jikiX > 220)
                                                    {
                                                        daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 4);//1Right,2UP,4Left,8Down
                                                        x1 = -3;
                                                        y1 = 0;
                                                    }
                                                }
                                            }
                                        }
            
            
                                        ////////////////////NEEDED????
                                        if (atrisk <= 3 && supportflag > 0)
                                        {
                                            if (controllerON > 0)
                                            {
                                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);//1Right,2UP,4Left,8Down
                                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);
                                            }
                                            button8.BackColor = Color.LightGray;
                                            button7.BackColor = Color.LightGray;
                                            button6.BackColor = Color.LightGray;
                                            button9.BackColor = Color.LightGray;
                                        }
            
                                        // are you moving?
            
                                        label21.Text = "restart" + startflag.ToString();
            
            
                                        // restart??
                                        if (jikiX > 270 && jikiX < 340 && jikiY > 454 && jikiY < 460)
                                        {
                                            if (((prev_jikiX - jikiX) * (prev_jikiX - jikiX)) < 25)
                                            {
                                                startflag++;
            
            
                                                if (startflag > 40 && startflag < 43)
                                                {
                                                    if (playON != 0 && controllerON > 0)
                                                    {
                                                        daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 20);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16SEL 32START
            
                                                    }
                                                }
            
                                                if (startflag > 90)
                                                {
                                                    startflag = 0;
                                                    if (playON != 0)
                                                    {
                                                        button16.BackColor = Color.Red;
                                                        if (controllerON > 0)
                                                        {
                                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 4);   //1Right,2UP,4Left,8Down
                                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 1);   //1Right,2UP,4Left,8Down
                                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 4);
                                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 20);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16SEL 32START
                                                            for (int k = 0; k < 5000; k++)
                                                            {
                                                                for (int l = 0; l < 3; l++)
                                                                {
                                                                }
                                                            }
                                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);   //1,2,4,8
                                                            for (int k = 0; k < 5000; k++)
                                                            {
                                                                for (int l = 0; l < 3; l++)
                                                                {
                                                                }
                                                            }
                                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 20);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16SEL 32START
                                                            for (int k = 0; k < 5000; k++)
                                                            {
            
                                                            }
                                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);   //1,2,4,8
            
            
            
            
                                                            if (conflag > 0)
                                                            {
            
                                                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 20);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16SEL 32START
            
                                                                waitsecone();
            
                                                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);   //1Right,2UP,4Left,8Down
                                                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 36);   //1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16analogue 32START
                                                                button10.BackColor = Color.Red;
                                                                button10.Refresh();
                                                                waitsecone();
                                                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);   //1,2,4,8
                                                                button10.BackColor = Color.LightGray;
                                                                button10.Refresh();
            
                                                                conttimes++;
                                                                label27.Text = "Continue : " + conttimes.ToString();
            
                                                                //////////////////////////////
                                                                // if (numericUpDown8.Value < 89)
                                                                // {
                                                                //     numericUpDown8.Value++;
                                                                // }
                                                                // else
                                                                // {
                                                                //     numericUpDown8.Value = 0;
                                                                // }
            
            
            
                                                            }
            
                                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 36);   //1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16analogue 32START
                                                            button10.BackColor = Color.Red;
                                                            button10.Refresh();
                                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);   //1,2,4,8
                                                            button10.BackColor = Color.LightGray;
                                                            button10.Refresh();
                                                            //daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 1);   //1Right,2UP,4Left,8Down
                                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 4);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16analogue 32START
                                                            button15.BackColor = Color.Red;
                                                            button15.Refresh();
            
            
                                                        }
                                                    }
                                                    else
                                                    {
                                                        startflag = 0;
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                startflag--;
                                                if (startflag < 0) { startflag = 0; }
                                            }
            
                                        }
            
            
            
                                        prev_jikiX = jikiX;
                                        prev_jikiY = jikiY;
                                        label4.Text = "Jikix" + jikiX.ToString();
                                        label5.Text = "Jikiy" + jikiY.ToString();
                                        label6.Text = "jikixFar" + count_target.ToString();
                                        label7.Text = "jikixNear" + count_targetNEAR.ToString();
                                        label14.Text = "tamapixels" + counttama.ToString();
                                        label16.Text = "filtama" + filttama.ToString();
                                        label17.Text = "filtjikinum" + filtjikinear.ToString();
                                        label18.Text = "jikinum" + filtjiki.ToString();
                                        label22.Text = "riskvalue" + atrisk.ToString();
            
                                        label29.Text = "BGpixels" + counttama2.ToString();
                                        label30.Text = "filtBG" + filttama2.ToString();
            
            
            
            
                                        ///   back ground into BLACK  //!!
                                        for (int pixelY = 50; pixelY < (480 - 50); pixelY++)  // pixel Y
                                        {
                                            for (int pixelX = 140; pixelX < (640 - 140); pixelX++) // pixel X * ch
                                            {
                                                indexR = (pixelX + pixelY * 640) * 3; //r
                                                indexG = (pixelX + pixelY * 640) * 3 + 1; //r
                                                indexB = (pixelX + pixelY * 640) * 3 + 2; //r
                                                chB = prevData1[indexB] + prevData1[indexB - 3] + prevData1[indexB + 3] + prevData1[indexB + 1920] + prevData1[indexB - 1920];
                                                chG = prevData1[indexG] + prevData1[indexG - 3] + prevData1[indexG + 3] + prevData1[indexG + 1920] + prevData1[indexG - 1920];
                                                chR = prevData1[indexR] + prevData1[indexR - 3] + prevData1[indexR + 3] + prevData1[indexR + 1920] + prevData1[indexR - 1920];
                                                indexR = (pixelX + pixelY * 640) * 3; //r
                                                indexG = (pixelX + pixelY * 640) * 3 + 1; //g
                                                indexB = (pixelX + pixelY * 640) * 3 + 2; //b
            
                                                nowB = prevData0[indexB] + prevData0[indexB - 3] + prevData0[indexB + 3] + prevData0[indexB + 1920] + prevData0[indexB - 1920];
                                                nowG = prevData0[indexG] + prevData0[indexG - 3] + prevData0[indexG + 3] + prevData0[indexG + 1920] + prevData0[indexG - 1920];
                                                nowR = prevData0[indexR] + prevData0[indexR - 3] + prevData0[indexR + 3] + prevData0[indexR + 1920] + prevData0[indexR - 1920];
            
                                                if ((chB - nowB) * (chB - nowB) / (chB + 1) + (chG - nowG) * (chG - nowG) / (chG + 1) + (chR - nowR) * (chR - nowR) / (chR + 1) < bor * 100)
                                                {
                                                    srcData[indexR] = (byte)(srcData[indexR] / 2);
                                                    srcData[indexG] = (byte)(srcData[indexG] / 2);
                                                    srcData[indexB] = (byte)(srcData[indexB] / 2);
                                                }
                                                else
                                                {
                                                    //  srcData[indexR] = (byte)(srcData[indexR]- srcData[indexR] * nv50 / 30);
                                                    //   srcData[indexG] = (byte)(srcData[indexG]- srcData[indexG] *nv50 / 30);
                                                    //   srcData[indexB] = (byte)(srcData[indexB] - srcData[indexB]  * nv50 / 30);
                                                }
                                            }
                                        }
            
            
            
            
            
            
                                        prev_jikiX = jikiX;
                                        prev_jikiY = jikiY;
            
            
            
            
                                        // shift X1 Y1
                                        for (int i = 98; i >= 0; i--)
                                        {
                                            x_history[i + 1] = x_history[i];
                                            y_history[i + 1] = y_history[i];
                                        }
                                        x_history[0] = x1;
                                        y_history[0] = y1;
            
                                        // DISPOSE && UNLOCK for display
                                        pictureBox2.Image = canvasLEFT;
                                        for (int counta = 0; counta < filtjikinear; counta++)
                                        {
                                            g.DrawRectangle(Pens.Green, targetNEARX[counta] - 5, targetNEARY[counta] - 5, 10, 10);
                                        }
            
                                        //Write JIKI 
                                        if (jikiX >= 0 && jikiX < 640 && jikiY >= 0 && jikiY < 480)
                                        {
                                            g.DrawRectangle(Pens.Blue, jikiX - 15, jikiY - 15 - offy, 30, 30);
                                            g.DrawEllipse(Pens.Green, jikiX - 25, jikiY - 25 - offy, 50, 50);
            
                                        }
                                        //Write escapeline
                                        if (dangertama != 0)
                                        {
                                            g.DrawLine(Pens.Red, jikiX + bibunX, jikiY - offy + bibunY, jikiX, jikiY - offy);
                                            g.DrawRectangle(Pens.Red, jikiX + bibunX, jikiY - offy + bibunY, 10, 10);
                                        }
            
                                        if (filttama != 0)
                                        {
                                            for (int countb = 0; countb < filttama; countb++)
                                            {
                                                int dist = 0;
                                                dist = (filt_tamaposX[countb] - jikiX) * (filt_tamaposX[countb] - jikiX) + (filt_tamaposY[countb] - jikiY + offy + 10) * (filt_tamaposY[countb] - jikiY + offy + 10);
                                                if (dist > 25000)
                                                {
                                                    g.DrawRectangle(Pens.Green, filt_tamaposX[countb] - 5, filt_tamaposY[countb] - 5, 10, 10);
            
                                                }
                                                else
                                                {
                                                    g.DrawRectangle(Pens.Red, filt_tamaposX[countb] - 5, filt_tamaposY[countb] - 5, 10, 10);
                                                    g.DrawLine(Pens.Gray, filt_tamaposX[countb], filt_tamaposY[countb], jikiX, jikiY - offy);
                                                }
                                            }
                                        }
            
                                        if (filttama2 != 0)
                                        {
                                            for (int countb = 0; countb < filttama2; countb++)
                                            {
                                                int dist = 0;
                                                dist = (filt_tamaposX2[countb] - jikiX) * (filt_tamaposX2[countb] - jikiX) + (filt_tamaposY2[countb] - jikiY + offy + 10) * (filt_tamaposY2[countb] - jikiY + offy + 10);
                                                if (dist > 25000)
                                                {
                                                    g.DrawRectangle(Pens.Green, filt_tamaposX2[countb] - 5, filt_tamaposY2[countb] - 5, 10, 10);
            
                                                }
                                                else
                                                {
                                                    g.DrawRectangle(Pens.Yellow, filt_tamaposX2[countb] - 5, filt_tamaposY2[countb] - 5, 10, 10);
                                                    g.DrawLine(Pens.Orange, filt_tamaposX2[countb], filt_tamaposY2[countb], jikiX, jikiY - offy);
                                                }
                                            }
                                        }
            
            
            
                                        g.Dispose();
                                    }
            
                                    ///@@@@@@@@@@@@@@ DQ5
                                    if (numericUpDown7.Value == 5)
                                    {
                                        //X=60,375,8, 81
            
                                        label62.Text = "fight:" + fightmode.ToString() + "; rest:" + restmode.ToString() + "; med:" + medmode.ToString();
            
            
            
                                        System.Runtime.InteropServices.Marshal.Copy(srcData, 0, bmpData.Scan0, srcData.Length);
                                        bitmap1.UnlockBits(bmpData);
                                        g.DrawImage(bitmap1, 0, 0);
            
                                        DateTime dt4 = DateTime.Now;
            
                                        nowtime = (dt4.Hour * 60 + dt4.Minute) * 60 + dt4.Second;
                                        int elltime = nowtime - prevtime;
                                        label61.Text = elltime.ToString() + "sec";
            
            
                                        if (enemycount % numericUpDown9.Value ==(numericUpDown9.Value-2) && restmode==1 && medenemy 100 && picG > 100 && picB > 100)
                                                    {
                                                        fightframe++;
                                                    }
                                                }
                                            }
            
                                            for (int pixelY = 0; pixelY < 400; pixelY++)  // pixel Y
                                            {
                                                for (int pixelX = 0; pixelX < 600; pixelX++) // pixel X * ch
                                                {
                                                    indexR = (pixelX + pixelY * 640) * 3 + 2; //r
                                                    indexG = (pixelX + pixelY * 640) * 3 + 1; //g
                                                    indexB = (pixelX + pixelY * 640) * 3 + 0; //b
                                                    int picR = srcData[indexR];
                                                    int picG = srcData[indexG];
                                                    int picB = srcData[indexB];
                                                    if (picR < 5 && picG < 5 && picB < 5)
                                                    {
                                                        blackframe++;
                                                    }
                                                }
                                            }
            
                                            for (int pixelY = 309; pixelY < 380; pixelY++)  // pixel Y
                                            {
                                                for (int pixelX = 574; pixelX < 580; pixelX++) // pixel X * ch
                                                {
                                                    indexR = (pixelX + pixelY * 640) * 3 + 2; //r
                                                    indexG = (pixelX + pixelY * 640) * 3 + 1; //g
                                                    indexB = (pixelX + pixelY * 640) * 3 + 0; //b
                                                    int picR = srcData[indexR];
                                                    int picG = srcData[indexG];
                                                    int picB = srcData[indexB];
                                                    if (picR > 100 && picG > 100 && picB > 100)
                                                    {
                                                        fightframe2++;
                                                    }
                                                }
                                            }
            
                                            if (blackframe > 200000)
                                            {
                                                blackcheck++;
                                            }
            
                                            if (blackcheck > 5)  //blackout
                                            {
                                                timer3.Enabled = false;
                                                timer4.Enabled = false;
                                                if (restmode == 1)
                                                {
                                                    restmode = 0;
                                                }
                                                if (fightmode == 1)
                                                {
                                                    fightmode = 0;
                                                }
                                            }
            
                                            if (fightframe > 70 && fightframe2 > 200 && restmode == 0 && fightmode == 0)   // enemy white check
                                            {
                                                fightcount++;
                                                if (fightcount > 10)
                                                {
                                                    fightcount = 10;
                                                }
                                            }
                                            else
                                            {
                                                fightcount--;
                                                if (fightcount < 0)
                                                {
                                                    fightcount = 0;
                                                }
                                            }
            
                                            if ((fightframe < 170 || fightframe2 < 200) && restmode == 0 && fightmode == 0)   // restcheck
                                            {
                                                restcount++;
                                                if (restcount > 100)
                                                {
                                                    restcount = 100;
                                                }
                                            }
                                            else
                                            {
                                                restcount--;
                                                if (restcount < 0)
                                                {
                                                    restcount = 0;
                                                }
                                            }
            
                                            if (fightcount > 9 && blackcheck>0 && prevfight==0)         //enemy encount
                                            {
                                                fightmode = 1;
                                                restmode = 0;
                                                prevfight = 1;
                                                prevrest = 0;
                                                blackcheck = 0;
                                                enemycount++;
                                                label60.Text = enemycount.ToString() + "enemies enc";
                                                timer4.Enabled = false;
                                                timer3.Enabled = true;
                                            }
            
                                            if (restcount > 99 && blackcheck > 0 && prevrest==0)  //into rest mode
                                            {
                                                restmode = 1;
                                                fightmode = 0;
                                                blackcheck = 0;
                                                prevrest = 1;
                                                prevfight = 0;
                                                timer3.Enabled = false;
                                                timer4.Enabled = true;
                                            }
            
            
            
                                            timer4.Interval = 3000;
                                            timer3.Interval = 300;
            
                                            if (restmode == 1)
                                            {
                                                g.DrawRectangle(Pens.Blue, 45, 355, 6, 75);
                                                g.DrawRectangle(Pens.Blue, 46, 356, 4, 73);
                                            }
            
                                            if (fightmode == 1)
                                            {
                                                g.DrawRectangle(Pens.Red, 345, 355, 6, 75);
                                                g.DrawRectangle(Pens.Red, 346, 356, 4, 73);
                                            }
            
                                            if (restmode == 1)
                                            {
                                                if (button7.BackColor == Color.Red)
                                                {
                                                    g.DrawRectangle(Pens.White, 11, 1, 5, 470);
                                                    g.DrawRectangle(Pens.White, 12, 1, 4, 470);
                                                    g.DrawRectangle(Pens.White, 13, 1, 3, 470);
                                                }
                                                else
                                                {
                                                    g.DrawRectangle(Pens.White, 631, 1, 5, 470);
                                                    g.DrawRectangle(Pens.White, 632, 1, 4, 470);
                                                    g.DrawRectangle(Pens.White, 633, 1, 3, 470);
                                                }
                                            }
                                            else
                                            {
                                                if (controllerON > 0)
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);   //1,2,4,8
                                                }
                                            }
            
                                            label66.Text = fightframe.ToString() + " " + fightframe2.ToString()+" " + blackframe.ToString();
                                            label67.Text = "fi"+fightcount.ToString() + " res" + restcount.ToString() + " blk" + blackcheck.ToString();
                                            label68.Text = "fiprev" + prevfight.ToString() + " resprev" + prevrest.ToString();
                                            if (blackcheck > 0)
                                            {
                                                label66.BackColor = Color.Red;
                                            }
                                            else
                                            {
                                                label66.BackColor = Color.LightGray;
                                            }
            
                                        
                                          
            
            
                                        }
            
                                        if (medmode > 0)
                                        {
            
                                            int timdif = nowtime - medtime;
                                            timer3.Enabled = false;
                                            timer4.Enabled = false;
                                            g.DrawRectangle(Pens.Yellow, 201, 1, 205, 470);
                                            g.DrawRectangle(Pens.Yellow, 202, 1, 204, 470);
                                            g.DrawRectangle(Pens.Yellow, 203, 1, 203, 470);
            
            
                                            label62.Text = timdif.ToString();
                                            if (controllerON > 0)
                                            {
                                                //  A, right, down, down, A, A, ,,,,A, 
                                                if (timdif == 1)
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 1);   //1,2,4,8
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                                                }
                                                else if (timdif == 3)
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 1);
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);
                                                }
                                                else if (timdif == 5)
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 8);
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);
                                                }
                                                else if (timdif == 7)
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 8);
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);
                                                }
                                                else if (timdif == 9)
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 1);   //1,2,4,8
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                                                }
                                                else if (timdif == 11)
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 1);   //1,2,4,8
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                                                }
                                                else if (timdif == 21)
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 1);   //1,2,4,8
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                                                }
                                                else
                                                {
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);
                                                }
                                               
                                            }
                                            if (timdif > 25)
                                            {
                                                medmode = 0;
                                                restmode = 1;
                                                timer4.Enabled = true;
                                            }
                                        }
            
            
                                        int lev_UD = 0, lev_RL = 0;
            
                                        //////////////////////MOVE JOY STICK////////////////////////
            
                                        short joyc = 0;
                                        if (lev_UD > 0)
                                        {
                                            joyc = (short)(joyc + 8);
                                        }
                                        if (lev_UD < 0)
                                        {
                                            joyc = (short)(joyc + 2);
                                        }
            
                                        if (lev_RL > 0)
                                        {
                                            joyc = (short)(joyc + 1);
                                        }
                                        if (lev_RL < 0)
                                        {
                                            joyc = (short)(joyc + 4);
                                        }
            
            
            
                                        if (controllerON > 0)
                                        {
                                            if (playON != 0)
                                            {
                                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, joyc);//1Right,2UP,4Left,8Down
                                            }
                                        }
            
            
            
                                        pictureBox2.Image = canvasLEFT;
                                        g.Dispose();
                                    }
            
                                }
            
                            }
            
            
                            timer1.Enabled = true;
            
                        timer1.Start();
            
                           
                        }
                    }
            
            
                    /// AUTO PLAY ON
                    private void button16_Click(object sender, EventArgs e) /// GAME SELECT ///////
                    {
            
                        ///// GAME SELECT ////////////////////
                        timer1.Enabled = false;
                        if (numericUpDown7.Value == 1)
                        {
                            label38.Text = "GAME RAIDEN";
                        }
                        else if (numericUpDown7.Value == 2)
                        {
                            label38.Text = "GAME PACMAN";
                        }
                        else if (numericUpDown7.Value == 3)
                        {
                            label38.Text = "GAME Galaxian";
                        }
                        else if (numericUpDown7.Value == 4)
                        {
                            label38.Text = "GAME FACE";
                        }
                        else if (numericUpDown7.Value == 5)
                        {
                            label38.Text = "GAME ????";
                        }
                        else 
                        {
                        }
            
                        if (playON == 0)
                        {
                            button16.BackColor = Color.Red;
                            if (controllerON > 0)
                            {
                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 4);  // 4RapidSHOT
                                button15.BackColor = Color.Red;
                                button15.Refresh();
                            }
                            playON++;
            
                            waitsecone();
                            playON = 1;
                        }
                        else
                        {
                            playON = 0;
                            button16.BackColor = Color.LightGray;
                            if (controllerON > 0)
                            {
                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);    // NULL
                                button6.BackColor = Color.LightGray;
                                button7.BackColor = Color.LightGray;
                                button8.BackColor = Color.LightGray;
                                button9.BackColor = Color.LightGray;
                                button10.BackColor = Color.LightGray;
                                button11.BackColor = Color.LightGray;
                                button12.BackColor = Color.LightGray;
                                button13.BackColor = Color.LightGray;
                                button14.BackColor = Color.LightGray;
                                button15.BackColor = Color.LightGray;
                                button10.Refresh();
                                button11.Refresh();
                                button15.Refresh();
                                waitsecone();
                            }
                        }
                        timer1.Enabled = true;
                    }
                    
                    ///SUPPORT MODE ON
                    private void button17_Click(object sender, EventArgs e)
                    {
                        ///////////// SUPPORT MODE //////////
                        if (supportflag == 0)
                        {
                            supportflag = 1;
                            button17.BackColor = Color.Red;
            
                        }
                        else
                        {
                            supportflag = 0;
                            button17.BackColor = Color.LightGray;
                        }
                    }
                    
                    ///SELECT GAME Type
                    private void numericUpDown7_ValueChanged(object sender, EventArgs e)
                    {
                        if (controllerON > 0)
                        {
                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);   //1,2,4,8
                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                            }
            
                            timer3.Enabled = false;
                        timer4.Enabled = false;
            
                        if (numericUpDown7.Value == 1)
                        {
                            label38.Text = "RAIDEN";
                            timer1.Interval = 1;
                            timer2.Enabled=false;
                            timer1.Enabled =true;
                        }
                        else if (numericUpDown7.Value == 2)
                        {
                            label38.Text = "PACman";
                            timer1.Interval = 30;
                            timer2.Interval = 5;
                            timer2.Enabled = true;
                            timer1.Enabled = true;
                        }
                        else if (numericUpDown7.Value == 3)
                        {
                            label38.Text = "GAME Galaxian";
                        }
                        else if (numericUpDown7.Value == 4)
                        {
                            label38.Text = "GAME FACE";
                        }
                        else if (numericUpDown7.Value == 5)
                        {
            
            
                            DateTime dt4 = DateTime.Now;
                            prevtime = (dt4.Hour * 60 + dt4.Minute) * 60 + dt4.Second;
                            nowtime = (dt4.Hour * 60 + dt4.Minute) * 60 + dt4.Second;
                            beforetime = ((dt4.Minute * 60) + dt4.Second) * 1000 + dt4.Millisecond;
            
                            label38.Text = "GAME DQ5";
                            blackcheck = 1;
                        }
                        else
                        {
                        }
            
                        sl1 = hScrollBar1.Value;
                        sl2 = hScrollBar2.Value;
                        sl3 = hScrollBar3.Value;
                        sl4 = hScrollBar4.Value;
                        sl5 = hScrollBar5.Value;
                        sl6 = hScrollBar6.Value;
                        sl7 = hScrollBar7.Value;
                        ///////////////////////////////////////////////////////////////////
                        //////////////// INIT  FOR PREVIOUS positions/////////////////////////////////////////////
                        ///////////////////////////////////////////////////////////////////
                        for (int i = 0; i < 100; i++)
                        {
                            diffsum[i] = 0;
                            x_history[i] = 0;
                            y_history[i] = 0;
                            x_history_A[i] = 0;
                            y_history_A[i] = 0;
                            x_history_B[i] = 0;
                            y_history_B[i] = 0;
                            x_history_C[i] = 0;
                            y_history_C[i] = 0;
                            x_history_D[i] = 0;
                            y_history_D[i] = 0;
                            UD_history[i] = 0;
                            RL_history[i] = 0;
                        }
                        for (int i = 0; i < 10; i++)        // Reset previous jiki position
                        {
                            prevX[i] = 320;
                            prevY[i] = 240;
                        }
            
                        for (int j = 0; j < 3 * 640 * 400; j++)   //Reset previous captured images to GRAY
                        {
                            prevData0[j] = 127;
                            prevData1[j] = 127;
                            prevData2[j] = 127;
                            srcData[j] = 127;
                        }
            
                        DateTime dt3 = DateTime.Now;
                        prevtime = (dt3.Hour * 60 + dt3.Minute) * 60 + dt3.Second;
                        nowtime = (dt3.Hour * 60 + dt3.Minute) * 60 + dt3.Second;
                    }
            
                    // AUTO CONTINUE MODE
                    private void button18_Click(object sender, EventArgs e)
                    {
                        
                        if (conflag == 1)
                        {
                            conflag = 0;
                            button18.BackColor = Color.LightGray;
                        }
                        else
                        {
                            conflag = 1;
                            button18.BackColor = Color.Red;
                        }
                    
                    }
                    
                    //////////
                    ////EVENTLOGGER
                    //////////
                    
                    ///EVENT LOG RESET && log save
                    private void button23_Click(object sender, EventArgs e)
                    {
                        string filepath = @"D:\joysticklog" +  ".txt";
                        for (int i = 0; i < 1000; i++)
                        {
                            filepath = @"D:\joysticklog"+i.ToString()+".txt";
                            if (File.Exists(filepath))
                            {
            
                            }
                            else
                            {
                                i = 30000;
                            }
                        }
            
            
                        StreamWriter sw = new System.IO.StreamWriter(filepath,
                        false,
                        System.Text.Encoding.GetEncoding("shift_jis"));
            
                       
                            sw.WriteLine(controllast);
                        for(int i = 0; i < controllast; i++)
                        {
                            sw.WriteLine( controllog[i]);
                        }
            
                        sw.Close();
            
            
            
                        for (int i = 0; i < 10000; i++)
                        {
                            controllog[i] = 0;
                        }
                        controllast = 0;
                        label69.Text = "saved:"+filepath;
                    }
            
                    // joy log read
                    private void button24_Click(object sender, EventArgs e)
                    {
                        string filepath = @"D:\joysticklog.txt";
                      
                            if (File.Exists(filepath))
                            {
                               
                                using (StreamReader sr = new StreamReader(filepath))
                                {
                                    controllast = Convert.ToInt32(sr.ReadLine());
                                    int i = 0;
                                    while (sr.Peek()>= 0)
                                    {
                                        controllog[i]=Convert.ToInt32( sr.ReadLine());
                                    i++;
                                    }
                                }
                            }
                 
            
                        label69.Text = controllast.ToString()+"logged ";
                        int controlstart = controllast - 49;
                        if (controlstart < 0)
                        {
                            controlstart = 0;
                        }
                        for (int i = controlstart; i < controllast + 1; i++)
                        {
                            if (controllog[i] < 10)
                            {
                                label69.Text = label69.Text + controllog[i].ToString();
                            }
                            if (controllog[i] == 10)
                            {
                                label69.Text = label69.Text + "A";
                            }
            
                        }
            
                    }
            
                    //logger play start
                    private void button25_Click(object sender, EventArgs e)
                    {
                        timer5.Enabled = true;
                        timer5.Interval = 250;
                        logcursor = 0;
                    }
            
                    //JOYSTICK log PLAY
                    private void timer5_Tick(object sender, EventArgs e)
                    {
                        if (logcursor < controllast * 2)
                        {
                            button25.Text = logcursor.ToString();
            
                            if (logcursor % 2 == 0)
                            {
                                if (controllerON > 0)
                                {
                                    button25.BackColor = Color.Red;
                                    {
            
                                        button6.BackColor = Color.LightGray;
                                        button7.BackColor = Color.LightGray;
                                        button9.BackColor = Color.LightGray;
                                        button8.BackColor = Color.LightGray;
                                        button12.BackColor = Color.LightGray;
                                        button13.BackColor = Color.LightGray;
                                        button14.BackColor = Color.LightGray;
                                        button15.BackColor = Color.LightGray;
                                        button10.BackColor = Color.LightGray;
                                        button11.BackColor = Color.LightGray;
            
                                        int joy = controllog[logcursor / 2];
                                        if (joy == 1)
                                        {
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 2);//1Right,2UP,4Left,8Down
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);
                                            button6.BackColor = Color.Red;
                                        }
                                        else if (joy == 2)
                                        {
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 4);//1Right,2UP,4Left,8Down
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);
                                            button7.BackColor = Color.Red;
                                        }
                                        else if (joy == 3)
                                        {
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 8);//1Right,2UP,4Left,8Down
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);
                                            button9.BackColor = Color.Red;
                                        }
                                        else if (joy == 4)
                                        {
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 1);//1Right,2UP,4Left,8Down
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);
                                            button8.BackColor = Color.Red;
                                        }
                                        else if (joy == 5)
                                        {
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 1);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16analogue 32START
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                                            button12.BackColor = Color.Red;
                                        }
                                        else if (joy == 6)
                                        {
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 2);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16analogue 32START
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                                            button13.BackColor = Color.Red;
                                        }
                                        else if (joy == 7)
                                        {
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 8);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16analogue 
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                                            button14.BackColor = Color.Red;
                                        }
                                        else if (joy == 8)
                                        {
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 4);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16analogue 32START
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                                            button15.BackColor = Color.Red;
                                        }
                                        else if (joy == 9)
                                        {
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 32);   //1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16analogue 32START
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                                            button10.BackColor = Color.Red;
                                        }
                                        else if (joy == 10)
                                        {
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 16);//1 BOMB 2 SingleSHot 4RapidSHOT 8BOMB 16SELECT 32START
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                                            button11.BackColor = Color.Red;
                                        }
                                        else
                                        {
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);   //1,2,4,8
                                            daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);   //1,2,4,8
                                        }
                                    }
                                }
                            }
                            else
                            {
                                button25.BackColor = Color.Blue;
                                if (controllerON > 0)
                                {
                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);   //1,2,4,8
                                    daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);   //1,2,4,8
                                }
                            }
            
                            if (controllerON > 0)
            
            
                                logcursor++;
                        }
                        else
                        {
                            button25.BackColor = Color.LightGray;
                            if (controllerON > 0)
                            {
                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);   //1,2,4,8
                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);   //1,2,4,8
                            }
                            timer5.Enabled = false;
                        }
            
                    }
                    //WAIT SEC ONE, wait loop
                    private void waitsecone()
                    {
                        timer1.Stop();
                        timer1.Enabled = false;
                        if (videoON != 0)                 ////////////////////HDMI CAM ON??? ////////////////////
                        {
                            using (Mat1 = new Mat(0, 0, MatType.CV_8UC3))               // for HDMI CAPTURE
                            {
            
                                int counti = 0;
            
                                int waittime2 = 0;
                                int nowtime2 = 0;
                                DateTime dt = DateTime.Now;
                                nowtime2 = ((dt.Minute*60+dt.Second)*1000)+dt.Millisecond;
                                waittime2 = nowtime2 + 1*250;
            
                                for (counti = 0; counti < 1;)
                                
                                { 
                                    DateTime dt3 = DateTime.Now;
                                    nowtime2 = ((dt3.Minute * 60 + dt3.Second) * 1000) + dt3.Millisecond;
                                    if (nowtime2 % 100 == 1)
                                    {
                                        VideoCapture1.Read(Mat1);
                                        using (Bitmap bitmap1 = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(Mat1))
                                        {
                                            Graphics g = Graphics.FromImage(canvasLEFT);
                                            Graphics1.DrawImage(bitmap1, 0, 0, Mat1.Cols, Mat1.Rows);
                                            g.DrawImage(bitmap1, 0, 0);
                                            g.Dispose();
                                        }
                                    }
            
                                    if (nowtime2 > waittime2)
                                    {
                                        counti++;
                                    }
                                    if (nowtime2 % 21 == 1)
                                    {
                                        button6.Text = (waittime2 - nowtime2).ToString();
                                        button6.Refresh();
                                    }
                                }
                                button6.Text = "UP";
                                button6.Refresh();
                            }
                        }
                        label69.Text = controllast.ToString() + "logged ";
                        int controlstart = controllast - 49;
                        if (controlstart < 0)
                        {
                            controlstart = 0;
                        }
                        for(int i = controlstart; i < controllast + 1; i++)
                        {
                            if (controllog[i] < 10)
                            {
                                label69.Text = label69.Text + controllog[i].ToString();
                            }
                            if (controllog[i] == 10)
                            {
                                label69.Text = label69.Text + "A";
                            }
            
                        }
            
                        timer1.Start();
                        timer1.Enabled = true;
                    }
            
            
                    ///  PACMAN Control
                    ///  
                    //HORIZONtal Scroll bars for PACMAN
                    private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
                    {
                        label46.Text = hScrollBar1.Value.ToString();
                        sl1 = hScrollBar1.Value;
                    }
                    private void hScrollBar2_Scroll(object sender, ScrollEventArgs e)
                    {
                        label47.Text = hScrollBar2.Value.ToString();
                        sl2 = hScrollBar2.Value;
                    }
                    private void hScrollBar3_Scroll(object sender, ScrollEventArgs e)
                    {
            
                        label48.Text = hScrollBar3.Value.ToString();
                        sl3 = hScrollBar3.Value;
                    }
                    private void hScrollBar4_Scroll(object sender, ScrollEventArgs e)
                    {
            
                        label49.Text = hScrollBar4.Value.ToString();
                        sl4 = hScrollBar4.Value;
                    }
                    private void hScrollBar5_Scroll(object sender, ScrollEventArgs e)
                    {
            
                        label50.Text = hScrollBar5.Value.ToString();
                        sl5 = hScrollBar5.Value;
                    }
                    private void hScrollBar6_Scroll(object sender, ScrollEventArgs e)
                    {
                        label55.Text = hScrollBar6.Value.ToString();
                        timer1.Interval = hScrollBar6.Value;
                        sl6= hScrollBar6.Value;
                    }
                    private void hScrollBar7_Scroll(object sender, ScrollEventArgs e)
                    {
                        label57.Text = hScrollBar7.Value.ToString();
                        sl7 = hScrollBar7.Value;
                    }
            
                    // refinement parameters for PACMAN
                    private void button20_Click(object sender, EventArgs e)
                    {
                        maxtime = 30;
                        label53.Text = "max reset";
                    }
            
            
                    // NO use
                    private void pictureBox1_Click(object sender, EventArgs e)
                    {
                    }
                    private void label27_Click(object sender, EventArgs e)
                    {
            
                    }
                    private void label1_Click(object sender, EventArgs e)
                    {
            
                    }
                    private void label15_Click(object sender, EventArgs e)
                    {
            
                    }
                    private void numericUpDown3_ValueChanged(object sender, EventArgs e)
                    {
            
                    }
                    private void label48_Click(object sender, EventArgs e)
                    {
                    }
                    private void label49_Click(object sender, EventArgs e)
                    {
                    }
                    private void label50_Click(object sender, EventArgs e)
                    {
                    }
            
                    /////////////////////////
                    //// DQ5 hit button, right & left at walking
                    private void timer3_Tick(object sender, EventArgs e)
                    {
            
                        if (button12.BackColor == Color.Red)
                        {
                            button12.BackColor = Color.LightGray;
                            button12.Refresh();
                            if (controllerON > 0)
                            {
                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 0);   //1,2,4,8
                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                            }
                        }
                        else
                        {
                            button12.BackColor = Color.Red;
                            button12.Refresh();
                            if (controllerON > 0)
                            {
                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortB, 1);   //1,2,4,8
                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 0);
                            }
                        }
                    }
                    //walking mode
                    private void button21_Click(object sender, EventArgs e)
                    {
                        timer3.Enabled = false;
                        timer4.Enabled = true;
                        timer4.Interval = 3000;
                        blackcheck = 0;
                        restmode = 1;
                        fightmode = 0;
                        prevrest = 0;
                        prevfight = 0;
                    }
                    //fight mode
                    private void button22_Click(object sender, EventArgs e)
                    {
                        timer4.Enabled = false;
                        timer3.Enabled = true;
                            timer3.Interval = 300;
                            blackcheck = 0;
                            restmode = 0;
                            fightmode = 1;
                        prevrest = 0;
                        prevfight = 0;
                    }
                    // walk mode R andL
                    private void timer4_Tick(object sender, EventArgs e)
                    {
            
                        if (button7.BackColor == Color.Red)
                        {
                            button7.BackColor = Color.LightGray;
                            button8.BackColor = Color.Red;
                            button12.Refresh();
                            if (controllerON > 0)
                            {
                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 1);   //1,2,4,8
                            }
                        }
                        else
                        {
                            button8.BackColor = Color.LightGray;
                            button7.BackColor = Color.Red;
                            if (controllerON > 0)
                            {
                                daqBoardon1.DOut(MccDaq.DigitalPortType.FirstPortA, 4);   //1,2,4,8
                            }
                        }
                    }
            
                    //NO USE
                    private void numericUpDown4_ValueChanged(object sender, EventArgs e)
                    {
            
                    }
                    private void button3_Click(object sender, EventArgs e)
                    {
                     
                    }
                    private void pictureBox3_Click(object sender, EventArgs e)
                    {
            
                    }
                    private void pictureBox2_Click(object sender, EventArgs e)
                    {
            
                    }
                    private void pictureBox4_Click(object sender, EventArgs e)
                    {
            
                    }
                }
            }