Is there a way to spawn enemies randomly?

kimduck

Member
I have created 10 different enemies (Mob1 to Mob10). Currently, calling 'spawn Mob1' only spawns that specific enemy. How can I make it so that one of these 10 enemies is selected and spawned randomly?
 
I have created 10 different enemies (Mob1 to Mob10). Currently, calling 'spawn Mob1' only spawns that specific enemy. How can I make it so that one of these 10 enemies is selected and spawned randomly?
I suggest using spawn scripts for random enemies. I have some examples in the SOR2X, I suggest taking a look.
The concept is, I spawn a fake enemy and this enemy has an onspawn script that randomly spawn a real enemy through a spawn script.

Here's a basic example.

Code:
void enemyX2(void name1, void name2, float dx, float dy, float dz)
{//Spawns random enemy next to caller (2 ENTITIES, GALSIA/DONOVAN OR SLUM/VICE)
    void self = getlocalvar("self");
    void vName;
    void vSpawn;
    int dir = getentityproperty(self,"direction");
    int iR;

    //RANDOM NAME
    iR = rand()%50+50;

    if(iR >= 0 && iR < 50){
        vName = name1;
    }
    else
    if(iR >= 50 && iR <= 100){
        vName = name2;
    }
    
    clearspawnentry();
    setspawnentry("name", vName);

    vSpawn = spawn();
    bindentity(vSpawn, self, dx, dz, dy, dir);

    return vSpawn;
}
 
How can I make it so that one of these 10 enemies is selected and spawned randomly?

There are many ways to make random enemy spawner. One way is to make something like this:
Code:
name        RandEnemy
type        none
lifespan    2
animationscript    data/scripts/scripts.c
load         Andore
load         Brett
load        Case
load         Daniel
load         Dingo
load         Dragon
load         Fish
load         Henry
load        J 
load         Suke
load        Wrestler



anim    idle
@script
    if(frame == 1){
      void self = getlocalvar("self");
      int r = rand()%5+5; //genera numero da 0 a 10

      //if(r > 0){
        if(r == 0){
          changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW1"));
        } else if(r == 1){
          changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW2"));
        } else if(r == 2){
          changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW3"));
        } else if(r == 3){
          changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW4"));
        } else if(r == 4){
          changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW5"));
        } else if(r == 5){
          changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW6"));
        } else if(r == 6){
          changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW7"));
        } else if(r == 7){
          changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW8"));
        } else if(r == 8){
          changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW9"));
        } else if(r == 9){
          changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW10"));
        } else if(r == 10){
          changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW11"));
        } 
      //}
    }
@end_script
    delay    1
    offset    8 14
    frame    data/chars/misc/empty.gif
    frame    data/chars/misc/empty.gif
    delay    200
    frame    data/chars/misc/empty.gif

anim    follow1
    delay    1
    offset    8 14
    frame    data/chars/misc/empty.gif
    delay    200
    @cmd    spawn01 "Andore" 0 0 0
    frame    data/chars/misc/empty.gif

anim    follow2
    delay    1
    offset    8 14
    frame    data/chars/misc/empty.gif
    delay    200
    @cmd    spawn01 "Brett" 0 0 0
    frame    data/chars/misc/empty.gif
    
anim    follow3
    delay    1
    offset    8 14
    frame    data/chars/misc/empty.gif
    delay    200
    @cmd    spawn01 "Case" 0 0 0
    frame    data/chars/misc/empty.gif
    
anim    follow4
    delay    1
    offset    8 14
    frame    data/chars/misc/empty.gif
    delay    200
    @cmd    spawn01 "Daniel" 0 0 0
    frame    data/chars/misc/empty.gif
    
anim    follow5
    delay    1
    offset    8 14
    frame    data/chars/misc/empty.gif
    delay    200
    @cmd    spawn01 "Dingo" 0 0 0
    frame    data/chars/misc/empty.gif
    
anim    follow6
    delay    1
    offset    8 14
    frame    data/chars/misc/empty.gif
    delay    200
    @cmd    spawn01 "Dragon" 0 0 0
    frame    data/chars/misc/empty.gif
    
anim    follow7
    delay    1
    offset    8 14
    frame    data/chars/misc/empty.gif
    delay    200
    @cmd    spawn01 "Fish" 0 0 0
    frame    data/chars/misc/empty.gif
    
anim    follow8
    delay    1
    offset    8 14
    frame    data/chars/misc/empty.gif
    delay    200
    @cmd    spawn01 "Henry" 0 0 0
    frame    data/chars/misc/empty.gif
    
anim    follow9
    delay    1
    offset    8 14
    frame    data/chars/misc/empty.gif
    delay    200
    @cmd    spawn01 "J" 0 0 0
    frame    data/chars/misc/empty.gif
    
anim    follow10
    delay    1
    offset    8 14
    frame    data/chars/misc/empty.gif
    delay    200
    @cmd    spawn01 "Suke" 0 0 0
    frame    data/chars/misc/empty.gif
    
anim    follow11
    delay    1
    offset    8 14
    frame    data/chars/misc/empty.gif
    delay    200
    @cmd    spawn01 "Wrestler" 0 0 0
    frame    data/chars/misc/empty.gif

You could rename enemy names in both load and spawn01 function to suit your need.
 
Back
Top Bottom