|
|
Line 283: |
Line 283: |
| //Shuffle reaction order | | //Shuffle reaction order |
| shuffle_array(sim.reac_order); | | shuffle_array(sim.reac_order); |
- |
| |
- | for(var i= 0 ; i < sim.reac_order.length ; ++i)
| |
- | {
| |
- | var usedmols= new Array();
| |
- |
| |
- | var reac= sim.reactions[sim.reac_order[i]];
| |
- | var proba= reac.probability;
| |
- | for(var j= 0 ; j < reac.inputs.length ; ++j)
| |
- | {
| |
- | var inid= reac.inputs[j];
| |
- | if(usedmols[inid] == undefined)
| |
- | usedmols[inid]= 1;
| |
- | else
| |
- | usedmols[inid] ++;
| |
- | proba *= sim.molecules[inid].quantity;
| |
- | }
| |
- | for(var j= 0 ; j < reac.outputs.length ; ++j)
| |
- | {
| |
- | var outid= reac.outputs[j];
| |
- | if(usedmols[outid] != undefined)
| |
- | usedmols[outid]--;
| |
- | }
| |
- | proba /= Math.pow(sim.volume, reac.inputs.length);
| |
- |
| |
- | var entier= Math.floor(proba);
| |
- | var frac= proba - entier;
| |
- | for(var elt in usedmols)
| |
- | {
| |
- | if( entier * usedmols[elt] >= sim.molecules[elt].quantity )
| |
- | {
| |
- | var tmpe= Math.floor(sim.molecules[elt].quantity / usedmols[elt]);
| |
- | if(tmpe < entier)
| |
- | entier= tmpe;
| |
- | frac= 0;
| |
- | }
| |
- | }
| |
- |
| |
- | for(var j= 0 ; j < reac.inputs.length ; ++j)
| |
- | sim.molecules[reac.inputs[j]].quantity -= entier;
| |
- | for(var j= 0 ; j < reac.outputs.length ; ++j)
| |
- | sim.molecules[reac.outputs[j]].quantity += entier;
| |
- |
| |
- | bool lastone= false;
| |
- | if(frac > 0)
| |
- | lastone= (Math.random() < frac);
| |
- | if(lastone)
| |
- | {
| |
- | for(var j= 0 ; j < reac.inputs.length ; ++j)
| |
- | sim.molecules[reac.inputs[j]].quantity --;
| |
- | for(var j= 0 ; j < reac.outputs.length ; ++j)
| |
- | sim.molecules[reac.outputs[j]].quantity ++;
| |
- | }
| |
- | }
| |
| | | |
| for(var i= 0 ; i < sim.molecules.length ; ++i) | | for(var i= 0 ; i < sim.molecules.length ; ++i) |