// Draw the property inside the given rect
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        // Using BeginProperty / EndProperty on the parent property means that
        // prefab override logic works on the entire property.
        EditorGUI.BeginProperty(position, label, property);

        var valueProp = property.FindPropertyRelative("value");
        var nameProp  = property.FindPropertyRelative("resource").FindPropertyRelative("m_name");

        if (!Mathc.ArrayContains(ref GameManagerEditor.availableResources, nameProp.stringValue, out lastIndex))
        {
            lastIndex = 0;
        }

        // Draw label
        position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);

        // Don't make child fields be indented
        var indent = EditorGUI.indentLevel;

        EditorGUI.indentLevel = 0;

        // Calculate rects
        var nameRect = new Rect(position.x + 2, position.y, 135, position.height);          // min = x+2		|| max = x+137
        var amount   = new Rect(nameRect.xMax + 7, position.y, 135, position.height);       // min = prevMax+7	|| max = x+279

        lastIndex            = EditorGUI.Popup(nameRect, lastIndex, GameManagerEditor.availableResources);
        nameProp.stringValue = GameManagerEditor.availableResources[lastIndex];
        valueProp.floatValue = EditorGUI.FloatField(amount, valueProp.floatValue);

        // Set indent back to what it was
        EditorGUI.indentLevel = indent;

        EditorGUI.EndProperty();
    }
    public bool AddNodeToLayer(int layer, bool randomWeights, float weightVal = 0.0f, float biasVal = 0.0f)
    {
        if (!Mathc.ValueIsBetween(layer, -1, layerCount, true))
        {
            return(false);
        }

        Neuron newNode = new Neuron(nodeCount++, randomWeights ? Random.value * 2 - 1 : biasVal);

        nodeLayers[layer].Add(newNode);

        if (layer > 0)
        {
            foreach (var node in nodeLayers[layer - 1])
            {
                node.ConnectNode(newNode, randomWeights ? Random.value * 2 - 1 : weightVal);
            }
        }
        if (layer < layerCount - 1)
        {
            foreach (var node in nodeLayers[layer + 1])
            {
                newNode.ConnectNode(node, randomWeights ? Random.value * 2 - 1 : weightVal);
            }
        }
        return(true);
    }
Пример #3
0
 public NodeController GetRandomOwnedNode()
 {
     if (ownedNodes.Count < 1)
     {
         return(null);
     }
     return(Mathc.GetRandomValueFromDict(ref ownedNodes));
 }
 public void DrawConnections(Gradient relationGradient)
 {
     foreach (var axon in axons)
     {
         Gizmos.color = relationGradient.Evaluate(Mathc.NormalizeBetween(axon.weight, -1, 1));
         Gizmos.DrawLine(pos, axon.receivingNode.pos);
     }
 }
 public NodeController GetNodeFromIndex(int index)
 {
     if (Mathc.ValueIsBetween(index, -1, nodes.Count, true))
     {
         return(nodes[index]);
     }
     return(null);
 }
Пример #6
0
 public NodeController GetRandomBorderNode()
 {
     if (m_borderNodes.Count < 1)
     {
         return(null);
     }
     return(Mathc.GetRandomValueFromDict(ref m_borderNodes));
 }
    public NodeBuildingCont(NodeController pNode)
    {
        parentNode = pNode;

        var types = Mathc.GetEnumValues <BUILDING_TYPE>();

        foreach (var t in types)
        {
            buildingTypeCount.Add(t, 0);
        }
    }
        public static void Save(Window window, Type viewModelType)
        {
            var filename = Path.Combine(ApplicationData.Current.LocalFolder.FullName, viewModelType.FullName.GetHash() + "_Navigator");

            var dictionary = new KeyRawValueDictionary();

            dictionary.Add("Width", Mathc.Clamp(PersistentWindowProperties.GetWidth(window), window.MinWidth, window.MaxWidth));
            dictionary.Add("Height", Mathc.Clamp(PersistentWindowProperties.GetHeight(window), window.MinHeight, window.MaxHeight));
            dictionary.Add("Top", window.Top);
            dictionary.Add("Left", window.Left);
            dictionary.Add("WindowState", (int)window.WindowState);

            File.WriteAllBytes(filename, dictionary.Serialize());
        }
    public bool RemoveLayer(int index, bool replaceConnections)
    {
        if (!Mathc.ValueIsBetween(index, 0, layerCount - 1, true))
        {
            return(false);
        }

        var currLayer = nodeLayers[index];
        var prevLayer = nodeLayers[index - 1];
        var nextLayer = nodeLayers[index + 1];

        if (prevLayer.Count != currLayer.Count)
        {
            replaceConnections = true;
        }

        if (!replaceConnections)
        {
            for (int i = 0; i < currLayer.Count; ++i)
            {
                var n0 = currLayer[i];
                var n1 = prevLayer[i];
                n1.RemoveAllConnections();
                foreach (var n2 in nextLayer)
                {
                    n0.ReplaceConnection(n2, n1, false);
                }
            }
        }
        else
        {
            foreach (var n0 in prevLayer)
            {
                n0.RemoveAllConnections();
                foreach (var n1 in nextLayer)
                {
                    n0.ConnectNode(n1, Random.value * 2 - 1);
                }
            }
        }

        nodeLayers.RemoveAt(index);

        return(true);
    }
Пример #10
0
    public string GetConnectionSummary()
    {
        StringBuilder msg = new StringBuilder($"\n {tileNum}[{layer}]: ");

        for (int i = 0; i < 6; ++i)
        {
            if (connections[i] == null)
            {
                msg.Append($"[{i}, null] ");
                continue;
            }

            Vector2 nPos = connections[i].pos - pos;
            float   ang  = Mathf.Atan2(nPos.y, nPos.x);
            ang = Mathc.AnglePiToAngle2Pi(ang) * Mathf.Rad2Deg;
            msg.Append($"[{i}, {ang.ToString("F1")}, {nPos.ToString("F2")}] ");
        }

        return(msg.ToString());
    }
Пример #11
0
    /// <summary>  Returns the closest point to "pos" that is connected to "index"   </summary>
    int GetClosestConnectedPoint(int index, Vector2 pos)
    {
        int   cIndex = 0;
        float dist   = 0;

        foreach (var link in graphPoints[index].connections)
        {
            if (graphPoints[link.index].isBlocked)
            {
                continue;
            }
            float comp = Mathc.SqrDist2D(PointPos(link.index), pos);
            if (comp < dist)
            {
                dist   = comp;
                cIndex = link.index;
            }
        }

        return(cIndex);
    }
Пример #12
0
    // Misc Public Graph Data
    #region

    public int GetClosestPointTo(Vector2 pos)
    {
        int   cIndex = 0;
        float dist   = float.MaxValue;

        foreach (var point in graphPoints)
        {
            if (point.isBlocked)
            {
                continue;
            }
            float comp = Mathc.SqrDist2D(point.position, pos);
            if (comp < dist)
            {
                dist   = comp;
                cIndex = graphPoints.IndexOf(point);
            }
        }

        return(cIndex);
    }
    void InitializeBuildings()
    {
        buildingList         = new ReadOnlyCollection <BuildingData>(m_buildingList);
        buildingDict         = new ReadOnlyDictionary <int, BuildingData>(m_buildingDict);
        maxBuildingTypesDict = new ReadOnlyDictionary <BUILDING_TYPE, int>(m_maxBuildingTypesDict);

        foreach (var t in Mathc.GetEnumValues <BUILDING_TYPE>())
        {
            m_BuildingsByType.Add(t, new List <BuildingData>());
        }

        foreach (var bData in m_buildingList)
        {
            bData.Init();
            m_buildingDict.Add(bData.key, bData);
            m_BuildingsByType[bData.buildingType].Add(bData);
        }
        foreach (var t2 in m_maxBuildingTypesList)
        {
            m_maxBuildingTypesDict.Add(t2.type, t2.max);
        }
    }
Пример #14
0
        public static void Load(Window window, Type viewModelType)
        {
            PersistentWindowProperties.SetHeight(window, Mathc.Clamp(window.ActualHeight, window.MinHeight, window.MaxHeight));
            PersistentWindowProperties.SetWidth(window, Mathc.Clamp(window.ActualWidth, window.MinWidth, window.MaxWidth));

            var filename = Path.Combine(ApplicationData.Current.LocalFolder.FullName, viewModelType.FullName.GetHash() + "_Navigator");

            if (!File.Exists(filename))
            {
                return;
            }

            var dictionary = new KeyRawValueDictionary();

            dictionary.Deserialize(File.ReadAllBytes(filename));

            window.WindowStartupLocation = WindowStartupLocation.Manual;
            window.Left        = dictionary["Left"].ToDouble();
            window.Top         = dictionary["Top"].ToDouble();
            window.Height      = Math.Max(dictionary["Height"].ToDouble(), 1);
            window.Width       = Math.Max(dictionary["Width"].ToDouble(), 1);
            window.WindowState = (WindowState)dictionary["WindowState"].ToInteger();
        }
    public bool RemoveNodeFromLayer(int layerIndex)
    {
        if (!Mathc.ValueIsBetween(layerIndex, -1, layerCount, true))
        {
            return(false);
        }

        var layer  = nodeLayers[layerIndex];
        var neuron = layer[layer.Count - 1];

        if (layerIndex > 0)
        {
            foreach (var n in nodeLayers[layerIndex - 1])
            {
                n.RemoveConnection(neuron);
            }
        }
        neuron.RemoveAllConnections();

        layer.RemoveAt(layer.Count - 1);
        nodeCount--;

        return(true);
    }
Пример #16
0
        private void ChangeValue()
        {
            var value = Mathc.ValueOf(this.AssociatedObject.Value, this.AssociatedObject.Maximum, this.mainGrid.ActualWidth);

            ProgressBarProperties.SetValue(this.AssociatedObject, value);
        }
Пример #17
0
        /// <summary>
        /// Checks the password's strength
        /// </summary>
        /// <param name="password">The password to check</param>
        /// <returns>Returns <see cref="PasswordScore"/> rating</returns>
        public static PasswordScore GetPasswordScore(string password)
        {
            // Origin: http://social.msdn.microsoft.com/Forums/vstudio/en-US/5e3f27d2-49af-410a-85a2-3c47e3f77fb1/how-to-check-for-password-strength
            if (string.IsNullOrEmpty(password))
            {
                return(PasswordScore.Blank);
            }

            int score = 1;

            if (password.Length < 4)
            {
                return(PasswordScore.VeryWeak);
            }

            if (password.Length >= 8)
            {
                score++;
            }

            if (password.Length >= 12)
            {
                score++;
            }

            // If 90% of the password are numbers then lower the score
            if (Mathc.ValueOf(Regex.Match(password, @"[0-9]+(\.[0-9][0-9]?)?").Length, password.Length, 100) > 90)
            {
                score--;
            }
            // At least 4 of the chars should be numbers
            else if (Regex.Match(password, @"[0-9]+(\.[0-9][0-9]?)?").Length > 4)
            {
                score++;
            }

            // If 99% of the password are letters then lower the score
            if (Mathc.ValueOf(Regex.Match(password, @"^(?=.*[a-z])(?=.*[A-Z]).+$").Length, password.Length, 100) == 99)
            {
                score--;
            }
            else if (Regex.IsMatch(password, @"^(?=.*[a-z])(?=.*[A-Z]).+$"))
            {
                score++;
            }

            if (Regex.IsMatch(password, @"[!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]"))
            {
                score++;
            }

            if (string.Equals(password, "password", StringComparison.CurrentCultureIgnoreCase) ||
                string.Equals(password, "p4ssw0rd", StringComparison.CurrentCultureIgnoreCase) ||
                string.Equals(password, "p455w0rd", StringComparison.CurrentCultureIgnoreCase))
            {
                score = 1;
            }

            if (password.Distinct(new DynamicEqualityComparer <char>((a, b) => a == b)).Count() == 1)
            {
                score = score - 2;
            }

            return((PasswordScore)Mathc.Clamp(score, 1, 5));
        }
Пример #18
0
    // Graph Navigation.
    #region

    void NavigateBetweenAstar(int indexA, int indexB)
    {
        int   nextIndex   = indexB;
        var   q           = new List <int>();
        int   cIndex      = 0;
        bool  foundTarget = false;
        float dist        = float.MaxValue;

        startIndex = indexA;
        finalIndex = indexB;

        if (startIndex == finalIndex)
        {
            return;
        }

        ClearPointNavData();
        graphPoints[indexA].navData           = new GraphPoint.NavData(indexA, 0.0f);
        graphPoints[indexB].navData.wasTarget = true;

        foreach (var link in graphPoints[indexA].connections)
        {
            float comp = Mathc.SqrDist2D(PointPos(link.index), PointPos(indexB));
            if (comp < dist)
            {
                dist   = comp;
                cIndex = graphPoints[indexA].connections.IndexOf(link);
            }
            else
            {
                graphPoints[link.index].navData.evaluated = true;
            }
        }

        graphPoints[indexA].navData.evaluated = true;
        dist   = graphPoints[indexA].connections[cIndex].dist;
        cIndex = graphPoints[indexA].connections[cIndex].index;
        graphPoints[cIndex].navData = new GraphPoint.NavData(indexA, dist);
        q.Add(cIndex);

        while (q.Count > 0)
        {
            int curIndex = q[0];

            if (curIndex == indexB)
            {
                foundTarget = true;
            }
            else if (graphPoints[curIndex].navData.evaluated)
            {
                q.Remove(curIndex);
                continue;
            }

            cIndex = 0;
            dist   = float.MaxValue;
            foreach (var link in graphPoints[curIndex].connections)
            {
                if (graphPoints[link.index].navData.evaluated)
                {
                    continue;
                }

                float comp = Mathc.SqrDist2D(PointPos(link.index), PointPos(indexB));
                if (comp < dist)
                {
                    dist   = comp;
                    cIndex = graphPoints[curIndex].connections.IndexOf(link);
                }
                else
                {
                    graphPoints[link.index].navData.evaluated = true;
                }
            }

            dist   = graphPoints[curIndex].connections[cIndex].dist + graphPoints[curIndex].navData.tDist;
            cIndex = graphPoints[curIndex].connections[cIndex].index;

            if (dist < graphPoints[cIndex].navData.tDist)
            {
                graphPoints[cIndex].navData = new GraphPoint.NavData(curIndex, dist);
            }

            if (!foundTarget)
            {
                q.Add(cIndex);
            }

            graphPoints[curIndex].navData.evaluated = true;
            q.Remove(curIndex);
        }

        // Record path.
        q.Clear();
        q.Add(nextIndex);
        for (int a = 0; a < graphPoints.Count && nextIndex != indexA; a++)
        {
            nextIndex = graphPoints[nextIndex].navData.pIndex;
            q.Add(nextIndex);
        }


        navPath.Clear();
        for (int a = 0; a < q.Count; a++)
        {
            navPath.Add(q[q.Count - (a + 1)]);
        }
    }
 public NodeController GetRandomConnection()
 {
     return(Mathc.GetRandomValueFromDict(ref connections));
 }
    /// <summary>  </summary>
    /// <param name="index"></param>
    /// <param name="numNodes"></param>
    /// <param name="replaceAxonWeights">
    /// If you do not want to change the inputs going into this layer, set to false.
    /// NOTE: If the layer does not contain the same number of nodes as the one before it, THIS OPTION WILL DO NOTHING!!
    /// </param>
    /// <param name="randomWeights"></param>
    /// <param name="weightVal"></param>
    /// <param name="biasVal"></param>
    /// <returns></returns>
    public bool AddLayer(int index, int numNodes, bool replaceAxonWeights, bool randomWeights, float weightVal = 1.0f, float biasVal = 0.0f)
    {
        if (!Mathc.ValueIsBetween(index, 0, layerCount, true))
        {
            return(false);
        }

        // If the added layer and previous layer do not have the same number of nodes, copying connections can't be done.
        //Debug.Log($"{numNodes} != {nodeLayers[index-1].Count}");
        if (numNodes != nodeLayers[index - 1].Count)
        {
            replaceAxonWeights = true;
        }

        NeuronListWrapper newLayer = new List <Neuron>(numNodes);

        // Create and insert newLayer
        for (int i = 0; i < numNodes; ++i)
        {
            newLayer.Add(new Neuron(nodeCount++, randomWeights ? Random.value * 2 - 1 : biasVal));
        }
        nodeLayers.Insert(index, newLayer);

        var prevLayer = nodeLayers[index - 1];
        var nextLayer = nodeLayers[index + 1];

        // If possible, copy prevLayer->nextLayer connections to corresponding newLayer->nextLayer connections
        if (!replaceAxonWeights)
        {
            //Debug.Log("Copying connections");
            for (int i = 0; i < prevLayer.Count; ++i)
            {
                var n0 = prevLayer[i];
                var n1 = newLayer[i];
                foreach (var n2 in nextLayer)
                {
                    n0.ReplaceConnection(n2, n1, false);
                }
            }
        }
        // If not copying, remove prevLayer's connections, and then connect newLayer to nextLayer.
        else
        {
            //Debug.Log("Replacing connections");
            foreach (var n0 in prevLayer)
            {
                n0.RemoveAllConnections();
            }
            foreach (var n0 in newLayer)
            {
                foreach (var n1 in nextLayer)
                {
                    n0.ConnectNode(n1, randomWeights ? Random.value * 2 - 1 : weightVal);
                }
            }
        }


        // Connect prevLayer to newLayer
        foreach (var n0 in prevLayer)
        {
            foreach (var n1 in newLayer)
            {
                n0.ConnectNode(n1, randomWeights ? Random.value * 2 - 1 : weightVal);
            }
        }

        return(true);
    }