示例#1
0
        /// <summary>
        /// Handle the collision of two molecules
        /// </summary>
        /// <param name="m1">molecule 1</param>
        /// <param name="m2">molecule 2</param>
        public void Collision(Molecule m1, Molecule m2)
        {
            // check if ReactionPrep of both molecules is not null and if the ReactionPrep equals
            if ((m1.ReactionPrep != null) && (m1.ReactionPrep == m2.ReactionPrep))
            {
                // both molecules have the same ReactionPrep assigned and therefore
                // belong to the same reaction

                ReactionPrep reactionPrep = m1.ReactionPrep;

                // make the molecules ready for reacting
                reactionPrep.Ready(m1);
                reactionPrep.Ready(m2);
            }
        }
示例#2
0
        /// <summary>
        /// Initiates a reaction.
        /// </summary>
        /// <returns><c>true</c>, if the reaction was initiated, <c>false</c> otherwise.</returns>
        /// <param name="reaction">Reaction.</param>
        /// <param name="queueIfNotPossible">If set to <c>true</c>, the reaction is queued and performed later if the reaction is not possible at the moment.</param>
        public bool InitiateReaction(ReactionType reaction, bool queueIfNotPossible)
        {
            CUE cue = CUE.GetInstance();

            ReactionPrep reactionPrep = new ReactionPrep(reaction);

            bool moleculesFound;

            // Select molecules

            // Prefer selected molecule if suitable
            if (
                selectedMolecule != null && selectedReaction != null &&                 // molecule and reaction must be selected
                selectedReaction == reaction &&                                         // selected reaction must match current reaction
                selectedMolecule.ReactionPrep == null                                   // selected molecule must not be already involved in a reaction
                )
            {
                moleculesFound = cue.Molecules.FindMolecuelsForReaction(reactionPrep, selectedMolecule);
            }
            else
            {
                // select random molecules
                moleculesFound = cue.Molecules.FindMolecuelsForReaction(reactionPrep);
            }

            if (moleculesFound)
            {
                // Ready reactions for 0 to 1 reagents because Collision is never called for these molecules

                if (reactionPrep.MoleculeCount == 0)
                {
                    reactionPrep.Ready(null);
                }
                if (reactionPrep.MoleculeCount == 1)
                {
                    reactionPrep.Ready(reactionPrep.Molecules[0]);                     // necessary if only one reagent in reaction
                }

                return(true);
            }
            else
            {
                // not sufficient molecules for reaction

                reactionPrep.Release();                 // remove ReactonPrep from Molecules

                if (queueIfNotPossible)
                {
                    // --> queue Reaction for later

                    ShortKeyDict <ReactionType, int> .Entry entry;
                    if (!openReactions.Find(reaction, out entry))
                    {
                        entry = openReactions.Set(reaction, 0);
                    }

                    entry.Value++;
                }

                return(false);
            }
        }