Game Rates , Spawn ,fire ,drop ,etc

The Partridge Family were neither partridges nor a family. Discuss.
egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Game Rates , Spawn ,fire ,drop ,etc

Post by egizz983 » September 14th, 2016, 11:11 am

Hello wanna ask you how to make a rates of game Spawn ,fire ,drop ,etc or what is the best way to do so , how would you do . i got my own idea but i am not sure if its a good way to do this .
So how do i thinking of doing it , is example 100% spawn rate of something , so i would generate a random number between 1-100 and if number is <= spawn rate then i would spawn it , i think this would work only for a spawn and drop rates for the thing you dont want to do continuously . i mean fire rate should be something different cuz you always want to shoot ins only based on how often do you want to do that . fire rate could be done something like : firerate = 50 , -- each frame and when its 0 then fire and reset fire rate . this is my ideas so far on how to make a rates . but i am not sure if this is the best ways to do

DeitusPrime
Posts: 97
Joined: June 9th, 2014, 11:14 pm

Re: Game Rates , Spawn ,fire ,drop ,etc

Post by DeitusPrime » September 14th, 2016, 12:58 pm

Look into the function rand()...however, for a more modern approach.
In the new beginner series, chili has brought these new c++ features to our attention in tutorial/episode 8.

Code: Select all

#include <random>

std::random_device rd; //setup a randomizer var
std::mt19937 rng(rd()); //pass it to a generator 

//limit range of the random field
std::uniform_int_distribution<int> xRange(10,740);  //screen width (10 pixels from edge)
std::uniform_int_distribution<int> yRange(10, 530); //screen height

//usage (randomizes position of poo1 on the screen)
	poo1X = xRange(rng);
	poo1Y = yRange(rng);
reloading those poo vars will randomize them every time...

In your case, make a variable called fireRate:

std::uniform_int_distribution<int> fireRate(48,53);

say avg. bullet-fire rate is 50, you could have it drop below or slightly higher.

get crazy, add stuff like:

Code: Select all

#include <chrono>
#define MANUAL_SEED 5  // any number

	typedef std::chrono::high_resolution_clock myclock;
	myclock::time_point beginning = myclock::now();

	myclock::duration d = myclock::now() - beginning;
	unsigned SEED = d.count();

	std::random_device rd;
	std::mt19937 rng(rd() * SEED+ MANUAL_SEED );
I'm interested in these random engines myself, so share the results. ;)
Choose not to go to destruction with a taco-sauce that is bland...
Go to construction with a taco-sauce that is flavorful!!
Mwa ha ha!!

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Game Rates , Spawn ,fire ,drop ,etc

Post by albinopapa » September 14th, 2016, 6:56 pm

DeitusPrime wrote:Look into the function rand()...however, for a more modern approach.
In the new beginner series, chili has brought these new c++ features to our attention in tutorial/episode 8.

Code: Select all

#include <random>

std::random_device rd; //setup a randomizer var
std::mt19937 rng(rd()); //pass it to a generator 

//limit range of the random field
std::uniform_int_distribution<int> xRange(10,740);  //screen width (10 pixels from edge)
std::uniform_int_distribution<int> yRange(10, 530); //screen height

//usage (randomizes position of poo1 on the screen)
	poo1X = xRange(rng);
	poo1Y = yRange(rng);
reloading those poo vars will randomize them every time...

In your case, make a variable called fireRate:

std::uniform_int_distribution<int> fireRate(48,53);

say avg. bullet-fire rate is 50, you could have it drop below or slightly higher.

get crazy, add stuff like:

Code: Select all

#include <chrono>
#define MANUAL_SEED 5  // any number

	typedef std::chrono::high_resolution_clock myclock;
	myclock::time_point beginning = myclock::now();

	myclock::duration d = myclock::now() - beginning;
	unsigned SEED = d.count();

	std::random_device rd;
	std::mt19937 rng(rd() * SEED+ MANUAL_SEED );
I'm interested in these random engines myself, so share the results. ;)
I would do the C++ random stuff that DeitusPrime suggests for the spawn rate, but for the firerate, you'd probably want a constant time right? So instead of counting frames, have a fire rate of 500 milliseconds, or since screen refresh is capped at 60 and you were thinking 50 frames between, have it capped at 50/60 * 1000 milliseconds (833 milliseconds). Use the chrono stuff that DeitusPrime posted to create a timer, keep track of the time that has passed since last shot, when elapsed time greater than or equal to firerate, reset the timer and fire your bullets.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Game Rates , Spawn ,fire ,drop ,etc

Post by chili » September 16th, 2016, 5:29 am

Like DeitusPrime says, you can use rand(), but the best option these days is agreed to be mt19973 together with a distribution object.

For things like rates, or for example firing angle skew, I recommend using a normal distribution instead of a uniform one. It tends to give more natural results.
Chili

egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Re: Game Rates , Spawn ,fire ,drop ,etc

Post by egizz983 » September 18th, 2016, 3:41 pm

Okay so i started making a meteor spawn rates but face a problem at the start the problem is with a random function its gives me an error "invalid template argument for normal_distribution"

Code: Select all

std::random_device rnum;
			std::mt19937 rnd(rnum());
			std::normal_distribution<int> mlvl(1, 100);
			if (level < 3) {
				meteorlvl = 1;
			}
			if (level >= 3 && level < 8) {
				if (mlvl(rnd) <= (60-(level*1.5f))) {
					meteorlvl = 1;
				}
				else {
					meteorlvl = 2;
				}
			}
			if (level >= 8 && level < 13) {
                              ///123
			}
			if (level >= 13) {
                             //1234
			}

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Game Rates , Spawn ,fire ,drop ,etc

Post by LuisR14 » September 18th, 2016, 5:24 pm

yea you can't use int as a type for that class, it'd have to be either float, double or long double (or a type typedef'd as one of those), you can leave it at <> tho (which just then uses double as default)
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Re: Game Rates , Spawn ,fire ,drop ,etc

Post by egizz983 » September 18th, 2016, 5:42 pm

LuisR14 wrote:yea you can't use int as a type for that class, it'd have to be either float, double or long double (or a type typedef'd as one of those), you can leave it at <> tho (which just then uses double as default)
but that would effect a random value i mean if i would make 1-100 then there would be 100 random numbers but it i do 1.0-100.0 then there would be like 1-1000 numbers or even more i am not good at math right ?unless i would make like 0.1-10.0 i think that would make same result as 1-100

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Game Rates , Spawn ,fire ,drop ,etc

Post by LuisR14 » September 18th, 2016, 8:01 pm

don't think you're using it correctly, (not sure really, would need input from guys who have used it more than me heh, ... and what's your language also? .. xD)
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Game Rates , Spawn ,fire ,drop ,etc

Post by albinopapa » September 18th, 2016, 10:21 pm

I think the way it works, is the first param is like it's target value and the second param is the range from target value.

So if you did 1.0,100.0; then the range would be from -99.0 to 101.0. Though, since it's a deviation and not a range, then some will be more some will be less than the min/max values I posted. Just means that if you were to add all the results up, and average them together, you'd get close the the mean...the first param.

So to use it, you'd want to do something like

Code: Select all

	std::random_device rnum;
	std::mt19937 rnd( rnum() );
	std::normal_distribution<float> mlvl( 0.f, 100.f );

	int count = 0;
	const int maxCount = 100;

	int randArray[ maxCount ];

	while( count < maxCount )
	{
		auto rndNumber = abs(mlvl( rnd ));
		if( rndNumber < 100.f )
		{
			randArray[ count ] = static_cast<int>( rndNumber );
			cout << randArray[ count ] << endl;
			++count;
		}
	}
Of course to find maxCount, you'd have to figure out ahead of time how many random numbers you'd want if you used this method. Instead of creating the array, just generate the number and if it is between 0 and 100, then do your stuff like normal, if not skip spawning.

Here's what I got:

Code: Select all

27
43
37
48
8
0
7
7
43
57
8
74
80
14
69
13
96
62
89
6
17
31
68
41
72
37
20
65
15
40
23
24
28
10
10
6
61
55
2
93
47
31
5
85
93
36
1
22
24
94
56
19
97
31
9
33
49
45
56
71
23
45
12
32
84
19
89
79
3
98
34
48
56
20
78
10
52
71
27
54
54
78
87
22
56
6
63
72
87
36
64
44
86
84
68
26
37
59
98
7
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Game Rates , Spawn ,fire ,drop ,etc

Post by chili » September 19th, 2016, 1:11 am

Yeah, for the normal distribution you give the mean (the average value generated) and the standard deviation (the amount that any typical value tends to deviate from the mean). Yeah, it has to generate a floating point type, sorry for not mentioning that :)
Chili

Post Reply