Page 1 of 1

random create

PostPosted: Tue Jul 18, 2006 9:18 pm
by Zehper48
how do i make if player collides with the enemy there is a 10% chance that coin will be created

PostPosted: Tue Jul 18, 2006 9:43 pm
by Game A Gogo
use on destroy of the actor
Code: Select all
int coinC;
coinC=rand(10);
if(coinC ==1)
{
createactor("Coin", 0, 0, "non
_relative");

note: the create actor function may not be the good things in it, just replace it whit the real create action.

PostPosted: Wed Jul 19, 2006 12:15 am
by DilloDude
a good idea to use round(rand(10)) rather than just rand(10) or else the chance will be very slim

PostPosted: Wed Jul 19, 2006 7:45 am
by DilloDude
I'm currently working on a function for creating random items. It has a lot of parameters where you put the chance of each item you want to be able to create. It then chooses a number and finds which item to create. I have tested one sample by doing the arithmetic with a calculator, and it got the desird results, but I haven't tested in an actual game. You can modify it for any number of items.
Code: Select all
void LeaveItem(int th, int fl, int ar, int br, int sl, int gl, int rb, int hp, int na)
{
    int total = th + fl + ar + br + sl + gl + rb + hp + na;
    double THC;
    double FLC;
    double ARC;
    double BRC;
    double SLC;
    double GLC;
    double RBC;
    double HPC;
    if (total != 0)
    {
        double itm = rand(100);

        THC = (round(th) / total) * 100;
        FLC = ((round(fl) / total) * 100) + THC;
        ARC = ((round(ar) / total) * 100) + FLC;
        BRC = ((round(br) / total) * 100) + ARC;
        SLC = ((round(sl) / total) * 100) + BRC;
        GLC = ((round(gl) / total) * 100) + SLC;
        RBC = ((round(rb) / total) * 100) + GLC;
        HPC = ((round(hp) / total) * 100) + RBC; 
   
        if (itm <= THC){CreateActor("TH");}
        else if (itm <= FLC){CreateActor("FL");}
        else if (itm <= ARC){CreateActor("AR");}
        else if (itm <= BRC){CreateActor("BR");}
        else if (itm <= SLC){CreateActor("SL");}
        else if (itm <= GLC){CreateActor("GL");}
        else if (itm <= RBC){CreateActor("RB");}
        else if (itm <= HPC){CreateActor("HP");}
    }
}

the last parameter (na) is the chance that nothing will be created. Note that the Create Actor parts are not complete; you will need to complete these (possibly adding script for random location). For your example, if you wanted to create an item GL (short for GoLd coin), with a 90% chance that nothing will be created and a 10% chance that it will be a GL, put:
Code: Select all
LeaveItem(0, 0, 0, 0, 0, 1, 0, 0, 9);

But you can remove the parameters you don't need so you can only create the items you want. If na is not the last parameter, you will need an else.
If modifying it seems a bit complicated, just post the types of items you want and I can do the modification for you.