public ChemicalElement[] GetPrimariesOf(ChemicalElementEntity ent)
        {
            ChemicalToArrayData data = this.primaries.Find(x => x.element == ent.type);

            if (data.array != null)
            {
                return(data.array);
            }
            else
            {
                Debug.LogWarning($"WARNING : This element ({ent.type}) is not yet registered ! Check it out !");
                return(null);
            }
        }
        protected internal static ChemicalElementEntity MixTwoElement(ChemicalElementEntity a, ChemicalElementEntity b)
        {
            if (a.type == ChemicalElement.Void)
            {
                return(b);
            }
            else if (b.type == ChemicalElement.Void)
            {
                return(a);
            }

            if (InteractiveEngine.instance.HasMixOf(a, b))
            {
                string name = InteractiveEngine.instance.GetMixOf(a, b);
                if (name == null)
                {
                    return(null);
                }
                Type t = Type.GetType(name);
                return(Activator.CreateInstance(t, ChemicalElementMixEntity.STANDARD_PARAMS) as ChemicalElementEntity);
            }

            List <ChemicalElementMixEntity> candidates = null;
            ChemicalElementEntity           winner     = null;

            candidates = InteractiveEngine.instance.chemicalElementMixEntityPoolList;

            foreach (ChemicalElementMixEntity mix in ChemicalElementMixEntity.mixes)
            {
                if (mix.type != a.type && mix.type != b.type && mix.CouldBeMadeOf(a, b))
                {
                    candidates.Add(mix);
                }
            }

            if (candidates.Count > 0)
            {
                winner = candidates[0];
                for (int i = 1; i < candidates.Count; ++i)
                {
                    winner = GetWinnerBetween(winner, candidates[i]);
                }
            }

            candidates.Clear();

            InteractiveEngine.instance.SetMixOf(a, b, winner);

            return((winner != null) ? Spawn(winner) : null);
        }
        /* ---------------------------------------------------------------------------------------------*/
        /* ---------------------------------------------------------------------------------------------*/
        /* ---------------------------------------------------------------------------------------------*/
        /* ---------------------------------------- * OPERATOR -----------------------------------------*/
        /* ---------------------------------------------------------------------------------------------*/
        /* ---------------------------------------------------------------------------------------------*/
        /* ---------------------------------------------------------------------------------------------*/
        // ELEMENT * ELEMENT
        public static ChemicalElementEntity operator *(ChemicalElementEntity a, ChemicalElementEntity b)
        {
            if (a.type == b.type)
            {
                a.intensity = b.intensity = a.intensity + b.intensity;
                return(a);
            }
            ChemicalElementEntity m = ChemicalElementMixEntity.MixTwoElement(a, b);

            if (m != null)
            {
                return(m);
            }
            return(ChemicalElementEntity.GetWinnerBetween(a, b));
        }
        public bool IsWinningAgainst(ChemicalElementEntity main, ChemicalElementEntity other)
        {
            int couple             = (int)(main.type | other.type);
            IntToChemicalData data = this.winners.Find(x => x.couple == couple);

            if (data.couple > 0)
            {
                return(data.type == main.type);
            }
            else
            {
                Debug.LogWarning($"WARNING : This couple ({main} + {other}) is not yet registered ! Check it out !");
                return(false);
            }
        }
        private static void Reaction(InteractiveEntity main, ChemicalElementEntity element, PhysicalInteractionEntity interaction)
        {
            InteractiveStatus status;

            // Calculate reaction :
            // 1. Result between 'current physical state' and 'possible element'
            // For example : Frozen * Fire = Neutral; Water
            status = main.physical * element;

            // Update entity interactive status
            main.physical = status.state;
            main.chemical = status.element;

            // main manage its new status && the interaction with the unknown entity
            main.InteractWith(status, interaction);
        }
 /* ---------------------------------------------------------------------------------------------*/
 /* ---------------------------------------------------------------------------------------------*/
 /* ---------------------------------------------------------------------------------------------*/
 /* -------------------------------------- STATIC METHODS ---------------------------------------*/
 /* ---------------------------------------------------------------------------------------------*/
 /* ---------------------------------------------------------------------------------------------*/
 /* ---------------------------------------------------------------------------------------------*/
 private protected static ChemicalElementEntity GetWinnerBetween(ChemicalElementEntity a, ChemicalElementEntity b)
 {
     if (a.IsStrongAgainst(b))
     {
         return(a);
     }
     else if (b.IsStrongAgainst(a))
     {
         return(b);
     }
     else if (a.intensity != b.intensity)
     {
         return((a.intensity > b.intensity) ? a : b);
     }
     else
     {
         return(new Void());
     }
 }
        public void SetWinnerBetween(ChemicalElementEntity main, ChemicalElementEntity other, bool isMainWinning)
        {
            int couple = (int)(main.type | other.type);

            if (!this.winners.Exists(x => x.couple == couple))
            {
                if (isMainWinning)
                {
                    this.winners.Add(new IntToChemicalData(couple, main.type));
                }
                else
                {
                    this.winners.Add(new IntToChemicalData(couple, other.type));
                }
            }
            else
            {
                Debug.LogWarning($"WARNING : This couple ({main} + {other}) is already registered ! Check it out for optimization !");
            }
        }
        public void SetMixOf(ChemicalElementEntity a, ChemicalElementEntity b, ChemicalElementEntity ent)
        {
            int couple = (int)(a.type | b.type);

            if (!this.couples.Exists(x => x.couple == couple))
            {
                if (ent == null)
                {
                    this.couples.Add(new IntToChemicalData(couple, ChemicalElement.Void, true));
                }
                else
                {
                    this.couples.Add(new IntToChemicalData(couple, ent.type));
                }
            }
            else
            {
                Debug.LogWarning($"WARNING : This couple ({a} + {b}) is already registered ! Check it out for optimization !");
            }
        }
        public string GetMixOf(ChemicalElementEntity a, ChemicalElementEntity b)
        {
            int couple             = (int)(a.type | b.type);
            IntToChemicalData data = this.couples.Find(x => x.couple == couple);

            if (data.couple > 0)
            {
                if (data.empty)
                {
                    return(null);
                }
                this.stringBuilder.Clear();
                this.stringBuilder.Append("Interactive.Engine.").Append(data.type.ToString());
                return(this.stringBuilder.ToString());
            }
            else
            {
                Debug.LogWarning($"WARNING : This couple ({a} + {b}) is not yet registered ! Check it out !");
                return(voiddString);
            }
        }
        // does elements in 'a' and 'b' validate this primary element composition
        protected internal bool CouldBeMadeOf(ChemicalElementEntity a, ChemicalElementEntity b)
        {
            ChemicalElement[] a1 = this.interactiveEngine.GetPrimariesOf(a);
            ChemicalElement[] a2 = this.interactiveEngine.GetPrimariesOf(b);
            bool found;

            foreach (ChemicalElement e in this.interactiveEngine.GetPrimariesOf(this))
            {
                found = false;

                // search in first array
                foreach (ChemicalElement k in a1)
                {
                    if (e == k)
                    {
                        found = true;
                        break;
                    }
                }
                // if not found in first array then
                if (!found)
                {
                    // search in second array
                    foreach (ChemicalElement k in a2)
                    {
                        if (e == k)
                        {
                            found = true;
                            break;
                        }
                    }
                }
                // if not found in either one of them
                if (!found)
                {
                    return(false);
                }
            }
            return(true);
        }
        private bool IsStrongAgainst(ChemicalElementEntity other)
        {
            if (this.interactiveEngine.HasWinnerBetween(this, other))
            {
                return(this.interactiveEngine.IsWinningAgainst(this, other));
            }

            int mywin, hiswin;

            mywin = hiswin = 0;

            foreach (ChemicalElement w in this.interactiveEngine.GetPrimariesOf(this))
            {
                foreach (ChemicalElement e in this.interactiveEngine.GetPrimariesOf(other))
                {
                    if (w == e)
                    {
                        hiswin++;
                    }
                }
            }
            foreach (ChemicalElement w in this.interactiveEngine.GetPrimariesOf(other))
            {
                foreach (ChemicalElement e in this.interactiveEngine.GetPrimariesOf(this))
                {
                    if (w == e)
                    {
                        mywin++;
                    }
                }
            }

            bool isMainWinning = (mywin != hiswin && mywin > hiswin);

            this.interactiveEngine.SetWinnerBetween(this, other, isMainWinning);

            return(isMainWinning);
        }
 public bool HasPrimariesOf(ChemicalElementEntity ent) => this.primaries.Exists(x => x.element == ent.type);
        public bool HasWinnerBetween(ChemicalElementEntity main, ChemicalElementEntity other)
        {
            int couple = (int)(main.type | other.type);

            return(this.winners.Exists(x => x.couple == couple));
        }
        public void WarmUp()
        {
            this.ClearAll();

            Type t;

            Type[] types;

            ChemicalElementEntity     element;
            PhysicalInteractionEntity interaction;
            InteractiveStatus         status;

            ChemicalElementEntity[]  elements;
            ChemicalMaterialEntity[] materials;
            PhysicalStateEntity[]    states;

            object[] parameters;



            t     = typeof(ChemicalElementEntity);
            types = Assembly.GetAssembly(t).GetExportedTypes().Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(t)).ToArray();

            parameters = new object[] { 0f };

            elements = new ChemicalElementEntity[types.Length];
            for (int i = 0; i < elements.Length; ++i)
            {
                elements[i] = Activator.CreateInstance(types[i], parameters) as ChemicalElementEntity;
            }



            t     = typeof(ChemicalMaterialEntity);
            types = Assembly.GetAssembly(t).GetTypes().Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(t)).ToArray();

            materials = new ChemicalMaterialEntity[types.Length];
            for (int i = 0; i < materials.Length; ++i)
            {
                materials[i] = t.GetField(types[i].Name.ToLower()).GetValue(null) as ChemicalMaterialEntity;
            }


            t     = typeof(PhysicalStateEntity);
            types = Assembly.GetAssembly(t).GetTypes().Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(t)).ToArray();

            states = new PhysicalStateEntity[types.Length];
            for (int i = 0; i < states.Length; ++i)
            {
                states[i] = t.GetField(types[i].Name.ToLower()).GetValue(null) as PhysicalStateEntity;
            }



            float start = Time.realtimeSinceStartup;

            // ELEMENT * ELEMENT
            for (int i = 0; i < elements.Length; ++i)
            {
                for (int j = i; j < elements.Length; ++j)
                {
                    element = elements[i] * elements[j];
                }
            }

            // ELEMENT * MATERIAL
            for (int i = 0; i < elements.Length; ++i)
            {
                for (int j = 0; j < materials.Length; ++j)
                {
                    element = elements[i] * materials[j];
                }
            }

            // ELEMENT * STATE
            for (int i = 0; i < elements.Length; ++i)
            {
                for (int j = 0; j < states.Length; ++j)
                {
                    status = elements[i] * states[j];
                }
            }


            // STATE * STATE
            for (int i = 0; i < states.Length; ++i)
            {
                for (int j = i; j < states.Length; ++j)
                {
                    interaction = states[i] * states[j];
                }
            }

            Debug.Log($"Took {Time.realtimeSinceStartup - start} seconds");

            // INSPECTOR
            this.inspector_showDetails = new bool[this.primaries.Count];
        }
Beispiel #15
0
 protected void SetInteractiveState(ChemicalElementEntity e, ChemicalMaterialEntity m, PhysicalStateEntity p)
 {
     this.status.state   = p;
     this.status.element = e;
     this.material       = m;
 }
 public bool HasWeaknessesOf(ChemicalElementEntity ent)
 {
     // only does that
     return(this.weaknesses.Exists(x => x.element == ent.type));
 }
 public InteractiveStatus(PhysicalStateEntity s, ChemicalElementEntity e)
 {
     this.state   = s;
     this.element = e;
 }
 private static ChemicalElementEntity Spawn(ChemicalElementEntity e)
 {
     // only does that
     return(Activator.CreateInstance(e.GetType(), ChemicalElementMixEntity.STANDARD_PARAMS) as ChemicalElementEntity);
 }
 public void SetPrimariesOf(ChemicalElementEntity ent, ChemicalElement[] ps)
 {
     this.primaries.Add(new ChemicalToArrayData(ent.type, ps));
     this.primaries.Sort(CompareChemicalToArrayData);
 }
        public bool HasMixOf(ChemicalElementEntity a, ChemicalElementEntity b)
        {
            int couple = (int)(a.type | b.type);

            return(this.couples.Exists(x => x.couple == couple));
        }