Randomizing monster spawns [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Moocat
Forum Contributor
Posts: 143
Joined: Wed Oct 12, 2005 9:28 am
Location: USA

Randomizing monster spawns [SOLVED]

Post by Moocat »

When randomizing my monster spawns, I pull a list of monsters with chances for them to spawn (which add up to 100%). For some reason I'm just running into a brain block as to how to pull them out. Here is an example:

Red Slime = 5% chance to spawn
Small Snake = 20% chance to spawn
Rat = 25% chance to spawn
Green Slime = 50% chance to spawn

And for the life of me I just can't think of how to randomize which one will spawn :p
Last edited by Moocat on Wed Nov 16, 2005 1:56 pm, edited 1 time in total.
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post by trukfixer »

put em in an associative array and use array_rand perhaps?
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Re: Randomizing monster spawns

Post by Charles256 »

Moocat wrote:When randomizing my monster spawns, I pull a list of monsters with chances for them to spawn (which add up to 100%). For some reason I'm just running into a brain block as to how to pull them out. Here is an example:

Red Slime = 5% chance to spawn
Small Snake = 20% chance to spawn
Rat = 25% chance to spawn
Green Slime = 50% chance to spawn

And for the life of me I just can't think of how to randomize which one will spawn :p
red = 1
small=2222
rat=33333
green=4444444444
store those numbers in an array..
pick a random number from the array...
maybe? I could be being retarded too..
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Post by jabbaonthedais »

The only thing I can think of is shuffling numbers 1-20, and have each of those numbers assigned to a monster. Each number would represent 5%, so Red Slime would be stored to one number, where Green Slime would be stored to 10 variables, like $monster[1] and $monster[2]. There is probably a much better way to do this, but this would work.
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Re: Randomizing monster spawns

Post by jabbaonthedais »

Charles256 wrote: red = 1
small=2222
rat=33333
green=4444444444
store those numbers in an array..
pick a random number from the array...
maybe? I could be being retarded too..
Thats a creative way to do it, and should work great.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

it's the only way I've ever seen:-D seem to remember this being asked in regard to songs one time....
Dm7
Forum Commoner
Posts: 67
Joined: Sat Oct 08, 2005 9:16 pm
Location: USA

Post by Dm7 »

Code: Select all

<?php
//creates a random number... 1 to 100 (as in 1% to 100%)
$rand = rand(1, 100);

// checks if it's 5 or less (creating 5% of it)
if ($rand <= 5) 
{
	echo "Congrats, you got a Red Slime!";
} 
// checks if there's 25 or less and is greater than 5 (creating 20% of it)
elseif ($rand > 5 && $rand <= 25)
{
	echo "Congrats, you got a Small Snake!";
}
// checks if there's 50 or less and is greater than 25 (creating 25% of it)
elseif ($rand > 25 && $rand <= 50)
{
	echo "Congrats, you got a Rat!";
}
// since the rest is 50%, then else statement is used
else
{
	echo "Congrats, you got a Green Slime!";
}
?>
It has been tested... that's how I would do it.. maybe there's a better way to do it. :) But from what I can see, it seems to work just fine.
User avatar
Moocat
Forum Contributor
Posts: 143
Joined: Wed Oct 12, 2005 9:28 am
Location: USA

Post by Moocat »

Alright, let's see if I got this down right:

Code: Select all

srand();
//SQL grab real monster array here
$monster_array = array("6:25", "7:20", "5:5");
$number_monster = count($monster_array);
//Subtract one because arrays start with 0
$number_monster--;
//Loop through all unique monsters, there will be no duplicates so no worries
while($number_monster >= 0) {
	//Split monster stats into monster ids and the percent chance to spawn
	$monster_stats = explode(":", $monster_array[$number_monster]);
	$monster_id = $monster_stats[0];
	$percent = $monster_stats[1];
	//Assign the same number of monster ids to the array as the percent number (will add to 100 later on, I know it's only 50 now)
	while($percent > 0) {
		$monster_count[] = $monster_id;
		$percent--;
	}
	$number_monster--;
}
//Set the monster id

$monster_spawned = array_rand($monster_count);
$monster_spawned = $monster_count[$monster_spawned];

//SQL select monster stuff goes here

echo "Oh no, you've encountered a ", $monster_spawned;
This appears to work :) Posted here just to let you smart people check my work over. Also, I thought you didn't need srand() for seeding anymore? I needed it and I have PHP 4.3, otherwise it just returned 6 over and over :/
Post Reply