Naked Science Forum

Non Life Sciences => Chemistry => Topic started by: Anvilsmith on 10/08/2006 10:43:05

Title: How do I design a chemistry system for a simulation?
Post by: Anvilsmith on 10/08/2006 10:43:05
In my efforts to create a roguelike game, wherein the player takes the role of a planetary explorer, I've set out to make the game as random as I can - to the extent that even the chemistry system may be randomy generated on each game.

The algorythms I've come up with so far are fairly simple, and all rely to a great extent on real chemistry. First, the player may input the values which quantum numbers n, l and ml are supposed to take - by default, n can go as far as 5, l must be smaller than n, and ml must hold any positive value up to and including l (so instead of 3 different p orbitals, I get 2 per tier). A periodic table is created based on that; an equation for the weight of atoms may be randomly generated (the equation is A = 2 * Z by default), then equations for the energies of various bonds are created, as well as the number of bonds of each type that atoms may form (based on their intrinsinc properties), and whether two different kinds of bonds "share space" (that is, using one type of bond prohibits the use of the other).

The equations for the bonds' energies incorporate a series of parameters such as the number of missing electrons in the highest s orbital of one of the two interacting elements, or the difference between said elements' atomic numbers. Every parameter is multiplied with a constant. A typical equation would look like E = k1 * da^2 + k2 * db - k3 * dZ^(1/2). The energies would need to be too weak most of the time to actually achieve any effect; otherwise, I'd have to calculate dozens of factors whenever the player decided to pour something on solid ground. Also, I need to ensure that only a select few materials are eligible to combine into organic chemicals.

Note that bonds can be both physical and chemical; chemical bonds, in my game, are the ones that spawn new substances, whereas physical bonds determine the structures and phases of the substances mentioned. The only way to distinguish the two types of bonds is by the energy they contain - chemical bonds contain much higher energies than physical bonds.

Every type of bond has a specific energy, a maximum number of bonds, and a link to other types of bonds that lowers the available number of bonds of those types by 1, 2 or 3 when it forms (this is to simulate such things as free electron pairs being applied to hydrogen bonds or coordinative bonds, but not both). Before the start of the game, every pair of atoms in existence is paired to check for potential molecules (I'll probably have to oganize a more efficient algorithm, though), and then every new molecule is paired up with the previous atoms and molecules, until organic compounds are noted (but that's another issue). When two simple molecules are put together, the program checks the weakest bond of each and sees whether the molecules would form stronger bonds through a substitution reaction. Then it glances over the free energies of the molecules' fragments, determining how much energy (heat) will need to be delivered to the system before the  form. Alternatively, it looks for possible addition reactions. The total list of possible reactions, as well as their set of conditions, is then stored.

Unlike regular chemistry, there's no "ideal state" which every atom tries to achieve (i.e. filling out all its orbitals), and beyond which it will stop forming bonds. I might therefore have to include a value that decreases the overall energy of double, triple bonds, etc. until it drops beyond zero. All that works pretty well for me - in theory, to see whether molecule will form certain structures in a test tube, I just need to go through every type of molecule, and every type of bond, and see which ones have the easiest time forming said bonds (which bonds bear the maximum energy, in other words). The following questions arise, though:

-How do I determine latent energy and boiling points? I know they're related to the strength of intermolecular bonds, but... How, specifically? Are there equations to determine that? Also, if the bonds break when energy is transferred in, wouldn't even ~more~ energy be generated? Should I perhaps include temperature as a factor in all bond energies, lowering them dynamically? That would make in-game calculations too complex, though, and I doubt that's the way things work in real life.

-For that matter, how do I introduce temperature, pressure and volume among the game's chemical aspects? I ~could~ use enthalpy and entropy,  but I'm trying to shy away from the latter due to the difficulties of calculating it... I'm also not sure how I could determine the pressure of gases. What exactly makes a gas "push through" its counterparts at a given speed?

-How do I establish the ranges of intermolecular bond energies that gases and liquids can take? I'll probably set up specific types of intermolecular bonds for the various phases, but I'd like to have some idea of what values their constants should take...

My biggest problem is having a big base of reactions for players to draw upon, as well as for the planet's wildlife to apply inherently, without making the calculations too strenious for the processor to perform each turn. Since the map's made out of tiles, delimiting cubic areas, I can't exactly refer to expanding volumes, as particles "migrate" to nearby spaces in their process of diffusion (I've yet to work out a system that determines how far they go based on pressure, but then, I suck at both physics and chemistry).

I've been browsing through the internet for information, though sadly, my time is limited by a need to study for my transfer exams. If anyone's willing to help, or give suggestions, I'd greatly appreciate it.