Making 2 elements out of 1

  • tommig
    8th Jun 2011 Member 0 Permalink
    I am restarting my mod, and would like to make an element that decays into two different elements at once, so that the original element becomes 2 different ones, but I can't work out the correct code! Anyone got something that works?
    Thanks
    Tommig
  • MasterMind555
    8th Jun 2011 Member 0 Permalink
    @tommig (View Post)
    ..I'm not an expert but common sense tells me that at the line where it would generates one element, you make it generates another one?
  • tommig
    8th Jun 2011 Member 0 Permalink
    I've tried the create part code, and getting it to change type at the same time, but it didn't work, so I deleted the failed code
  • randalserrano
    8th Jun 2011 Member 0 Permalink
    what compiler are you using?
  • jacksonmj
    8th Jun 2011 Developer 0 Permalink
    Assuming you want to keep the total number of particles the same, just pick one of the two decay products at random. For example:
    if (1>rand()%2)
    create_part(i, x, y, PT_DUST);
    else
    create_part(i, x, y, PT_WATR);

    will make half of it turn into dust and half into water.

    If you want to make each particle turn into two new particles, that's slightly more complicated and depends on what the elements are.
  • Disaster
    8th Jun 2011 Member 0 Permalink
    @randalserrano (View Post)
    No difference.

    @tommig (View Post)

    if ((rand()%1+1)>1){
    parts[i].type = Element1;
    }else{
    parts[i].type = Element2;
    }

    EDIT: Jacksonmj beat me to it.
  • tommig
    8th Jun 2011 Member 0 Permalink
    @jacksonmj (View Post)
    I need, preferablly, to turn 1 into 2, I'm using my H2O2 element, I want to turn it into water and O2
  • pilojo
    8th Jun 2011 Member 0 Permalink
    @tommig (View Post)
    So, exactly what jacksonmj put. It will make it turn into both.
  • tommig
    8th Jun 2011 Member 0 Permalink
    no, I need 1 part of H2O2 to turn into 1 part water and 1 part O2, that turns 2 parts H2O2 into 1 water and 1 O2
  • jacksonmj
    8th Jun 2011 Developer 0 Permalink
    @tommig (View Post)
    I can't see any way to do that without putting water and O2 in the same place. And it's a bad idea to put two particles in the same place (unless they're allowed to overlap, like neutrons and photons), because it causes big problems with movement. I'd recommend against it in this case.

    Normally, you create a new particle using create_part(-1,x,y,type), but create_part won't allow a new particle in the same place as an existing one. So if you want to see how buggy it makes movement, try the code below. Note how the liquid volume 'shrinks' - some of the liquid particles end up overlapping.
    // turn the current particle into water
    create_part(i,x,y,PT_WATR);
    if (pfree!=-1)
    {
    // allocate a new particle (normally done by create_part)
    r = pfree;
    pfree = parts[r].life;
    // make the new particle into O2
    create_part(r,x,y,PT_O2);
    }