1

I'm trying to make a random encounter with a monster based on %.

I got in my MySQL database a column called "monster_chancespawn" :

  • Monster 1 got 50%
  • Monster 2 got 30%
  • Monster 3 got 20%

What I want to do is to do a random MySQL SELECT in my "monster" DATABASE based on these %.

I already got this

    $monsterspawn=$db->prepare('SELECT * FROM monster');
    $monsterspawn->execute();
    $monsterspawn->CloseCursor();


    foreach ($monsterspawn as $infospawn)
      { 
      echo . $infospawn["monster_chanceloot"] . ;
      }

With this, it normally echo "50 30 20", but nothing really useful. What I want to do is :

  • Getting each monster's chances of loot.
  • Maybe doing something like "If rand is between 0 and 50, spawn Monster 1. If it is between 50 and 80, spawn Monster 3. If it is between 80 and 100, spawn Monster 3. BUT I want this to be flexible, and to change when I change the monster_chanceloot value

I could do something like this I guess

$randomspawn = rand(0, 100);
if ($randomspawn >= 0 && $randomspawn <= $infospawn["monster_chanceloot"])
{ $monstername = "Monster1" }

   elseif ($randomspawn >= $infospawn["monster_chanceloot"] && $randomspawn <= $infospawn["monster_chanceloot"](Monster 2's one))
    { $monstername = "Monster2" }

And same thing for Monster 3. The main problem would be that is does not let me change the number of monsters available, and the other problem is that I don't know how to get the monster_chanceloot of each monster outside of the foreach. And I don't want to make 30 MYSQL connection, so avoid using it each time.

Thanks !

EDIT : Here is a possibility given by "German Drulyk" :

I got Monster1, Monster2, Monster3. I duplicate Monster1 2times in my database and Monster2 1time.

I have now 3 Monster1, 2 Monster2 and 1 Monster3. So : 50% of chance to pick a Monster1, 33,3 for Monster 2 and 16,6 for Monster 3.

To get more accurate results, you can copy them to 100 to have 1 monster = 1%. To get a monster, simply do :

    $query=$db->prepare('SELECT * FROM MONSTERDDB ORDER BY RAND() LIMIT 1');
    $query->execute();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Lucas G
  • 71
  • 6
  • Will you be creating more than 100 monsters? What if certain monsters have the same exact chance within their type/tier? Imagine this scenario; 50% of the time it should be monster 1 or 2, 30% of the time it should be monster 3, and 20% of the time it should be monster 4 or 5 – MonkeyZeus Aug 10 '17 at 17:22
  • I see, I'll write up an answer – MonkeyZeus Aug 10 '17 at 17:28
  • (Deleted my last comment, sorry i didn't understood quite well). First, it will have 5 to 20 monsters, not a big number. For your example, normally nobody can have the same % in my database, but if it's the case then I just have then i don't really know because I limit this to 100%, meaning that if I put another monster, i will reduce the % of the other monsters equally. (50 to 40%, 30 to 24..) – Lucas G Aug 10 '17 at 17:31
  • Please see my answer. – MonkeyZeus Aug 10 '17 at 17:52
  • Hey, glad to see you liked my answer. I'm even happier to see that you were able to figure out a solution more elegant than mine! Good luck with your game! – MonkeyZeus Aug 11 '17 at 00:32

1 Answers1

0

IF you will never exceed 100 monsters in a zone then you need a table with 3 columns; zone_id, chance, monster_id

Per zone, insert 100 records and set the chance to 1 through 100 inclusively.

Per record, add a monster_id respective to it's common-ness; all 100 records per zone should have a monster_id

For example, zone1 needs 50 records with monster1, 30 records for monster2, and 20 records for monster3

In PHP, you can write your query like so

$sql = "select
          monster_id
        from
          table_name
        where
          zone_id = $zoneId and
          chance = ".rand(1, 100);

Additionally, I have not tried this but I guess native MySQL can achieve the same effect:

$sql = "select
          monster_id
        from
          table_name
        where
          zone_id = $zoneId and
          chance = FLOOR( 1 + RAND() * 100 )";
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • Thanks a lot, copies of the same monster was a good idea ! It's taking more place, but it's working well so far and as i don't have a lot of monsters i guess it's ok. Thanks for the idea ! – Lucas G Aug 10 '17 at 18:37