示例#1
0
        /// <summary>
        /// Registers a molecule to the MoleculeManager
        /// All molecules have to call this method once. The molecule added
        /// must not have a ReactionPrep assigned.
        /// </summary>
        /// <param name="molecule">Molecule to add, must not be null</param>
        public void Add(Molecule molecule)
        {
            if (molecule == null)
            {
                throw new System.ArgumentException("molecule must not be null");
            }

            // try to find a entry of the species
            ShortKeyDict <MoleculeSpecies, MoleculeSet> .Entry entry;
            if (!collection.Find(molecule.Species, out entry))
            {
                // species not found -> create a new entry
                entry = collection.Set(molecule.Species, new MoleculeSet(molecule.Species));
            }

            // add the molecule to the free list.
            entry.Value.Free.Add(molecule);
        }
示例#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);
            }
        }