Module Fields


    'Размеры гиперколонки
    Public X_C As Integer = Main.X_GC
    Public Y_C As Integer = Main.Y_GC


    ' Суммарное поле
    Public SumFields(,) As Double

    ' Радиус распространения полевой активности
    Dim RLAM As Integer = X_C / 2

    ' шаблон распределения активности
    Dim NLocalAM As Integer
    Dim LocalAM(,)


    ' Параметр распространения активности. Дисперсия
    Dim Sigma2LAM As Single

    Public Sub Field_ini()


        'Sigma2LAM = (RLAM / 4) ^ 2

        NLocalAM = 2 * RLAM + 1

        ReDim LocalAM(NLocalAM - 1, NLocalAM - 1)

        ReDim SumFields(X_C - 1, Y_C - 1)



        ' Создание шаблона распрстранения активности

        Dim dx, dy As Single

        For ix = 0 To NLocalAM - 1
            For iy = 0 To NLocalAM - 1

                dx = Math.Abs(ix - RLAM)
                dy = Math.Abs(iy - RLAM)

                'LocalAM(ix, iy) = Math.Exp(-1 / (2 * Sigma2LAM) * (dx ^ 2 + dy ^ 2))

                LocalAM(ix, iy) = 1 / (dx ^ 2 + dy ^ 2 + 1)

            Next
        Next

    End Sub


    ' Формирование поля активности
    Public Sub Set_fields_1()

        Dim ix, iy As Integer

        reset_fields()

        ' Формирование центра "пинвела"

        ix = (X_C - 1) / 2
        iy = (Y_C - 1) / 2

        Add_fields_1(ix, iy, 0.2)

        Draw_Fields()

        For ix = 0 To X_C - 1
            For iy = 0 To Y_C - 1

                If Main.GCol(ix, iy).A Then

                    Add_fields_1(ix, iy, 1.0)


                End If

            Next
        Next

    End Sub

    ' Добавление полей активности
    Private Sub Add_fields_1(ix As Integer, iy As Integer, A As Double)

        Dim x, y As Integer

        With Main.GCol(ix, iy)

            For ix1 = 0 To NLocalAM - 1
                For iy1 = 0 To NLocalAM - 1

                    x = ix + ix1 - RLAM
                    y = iy + iy1 - RLAM

                    If x >= 0 And y >= 0 And x < X_C And y < Y_C Then

                        SumFields(x, y) += LocalAM(ix1, iy1) * A
                    End If

                Next
            Next


        End With

    End Sub

    ' Сброс поля

    Public Sub reset_fields()

        For ix = 0 To X_C - 1
            For iy = 0 To Y_C - 1

                SumFields(ix, iy) = 0

            Next

        Next
    End Sub

    ' Поиск свободной колонки с максимальным полем
    Public Sub Field_max(ByRef F As Boolean, ByRef maxx As Integer, ByRef maxy As Integer)

        Dim max As Double

        max = -1

        For ix = 0 To X_C - 1
            For iy = 0 To Y_C - 1

                If SumFields(ix, iy) > max And (Not Main.GCol(ix, iy).status) Then

                    max = SumFields(ix, iy)
                    maxx = ix
                    maxy = iy

                End If

            Next
        Next

        F = max >= 0

    End Sub

End Module
