 |
Server Help Community forums for Subgame, ASSS, and bots
|
Author |
Message |
Mine GO BOOM Hunch Hunch What What

Age:41 Gender: Joined: Aug 01 2002 Posts: 3615 Location: Las Vegas Offline
|
Posted: Sun Oct 23, 2005 10:37 pm Post subject: |
 |
|
|
|
#include <stdlib.h>
#include <time.h>
#include <math.h>
/* Call this somewhere */
srand(time(NULL));
/* Later on (Angle is in radians) */
int X, Y;
double Angle, Mag;
Angle = (rand()/RAND_MAX)*6.283185307;
Mag = sqrt(((float)rand())/(float)RAND_MAX)*radius;
X = (int)(cos(Angle) * Mag);
Y = (int)(sin(Angle) * Mag); |
Thats all well and good, but you leave out one big problem with this: rounding. Cast typing does not handle rounding, it just throws away everything after the decimal point. Thus 0.9 and -0.9 both typecast to 0.
Lets go with this example. I created a C program that will pick one million random points inside of a circle with a radius of 50 pixels. Add up a counter for whenever a hit occurs at these points, and set an output image's alpha value to the percent chance of this point being picked. So the blackest part is the highest chance of being picked.
Zoom in a big, and you'll see that the X=0 and Y=0 lines both are very strong. At (0,0), it is at its strongest, almost 5 times as more likely than any non X=0/Y=0 point.
Now lets change it so instead of typecasting to int, use this rounding code:
int round(double n)
{
if (n >= 0.0)
return (int)(n + 0.5);
else
return (int)(n - 0.5);
} |
Do another random one million test points inside a 50 radius circle, and we'll get this:
So you'll have to add a bit more layer to be more correct, though the sqrt function helps move it away from the center. All of this is due to C's fast way of converting from floating to integers, and may not be required for all languages. To be complete, here is the non-sqrt method's output:
If these images all appear as black squares on your computer, IE has a problem with handling alpha tranparency with png images. Firefox/Opera/etc will handle the images correctly, or you could load them into any image editor.
typecast-big.png - 14.42 KB
File downloaded or viewed 8 time(s)
non-sqrt.png - 2.79 KB
File downloaded or viewed 10 time(s)
rounding.png - 10.98 KB
File downloaded or viewed 9 time(s)
typecast.png - 8.54 KB
File downloaded or viewed 9 time(s)
|
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: Sun Oct 23, 2005 11:05 pm Post subject: |
 |
|
|
|
That's all well and good mgb, but that's not the problem he's complaining about. _________________ Hyperspace Owner
Smong> so long as 99% deaths feel lame it will always be hyperspace to me
|
|
Back to top |
|
 |
Maverick

Age:40 Gender: Joined: Feb 26 2005 Posts: 1521 Location: The Netherlands Offline
|
Posted: Mon Oct 24, 2005 4:54 am Post subject: |
 |
|
|
|
Fixed code that should work:
case OP_Player:
{ // Player-level commands
if (c->check("about"))
{
sendPrivate(p, "I am a plain vanilla flavored bot. Yup, just as plain and useless as can be.");
}
if (c->check("confuse"))
{
srand(time(NULL));
rand();
int X, Y;
double Angle, Mag;
Angle = (rand()/RAND_MAX)*6.283185307;
Mag = sqrt(((float)rand())/(float)RAND_MAX)*10;
X = (int)(cos(Angle) * Mag);
Y = (int)(sin(Angle) * Mag);
{
sendPrivate(p, "*warpto "+String(X)+" "+String(Y));
sendPrivate(p, "You have dodged your opponents weapons! Now go for the kill!");
}
}
}
}
}
|
The player is warped to a coordinate in a circle of 10 tiles, however not from its original position. Use X = X + p->tile.x; Y = Y + p->tile.y; | to warp the player to a random location from the player's original location.
Also notice that you must use rand(); once after using srand(); . _________________
|
|
Back to top |
|
 |
Mine GO BOOM Hunch Hunch What What

Age:41 Gender: Joined: Aug 01 2002 Posts: 3615 Location: Las Vegas Offline
|
Posted: Mon Oct 24, 2005 11:23 am Post subject: |
 |
|
|
|
Maverick wrote: | Fixed code that should work:
...
Also notice that you must use rand(); once after using srand(); |
No, bad. Don't keep reseeding srand, you should only need to do that once at the start of your program. And didn't you read anything I wrote up in my fancy post either? Typecasting to int is bad for this, do some rounding function like the one I have listed above.
|
|
Back to top |
|
 |
Maverick

Age:40 Gender: Joined: Feb 26 2005 Posts: 1521 Location: The Netherlands Offline
|
Posted: Mon Oct 24, 2005 11:43 am Post subject: |
 |
|
|
|
Mine GO BOOM wrote: |
No, bad. Don't keep reseeding srand, you should only need to do that once at the start of your program. | Thanks, didn't know that
Mine GO BOOM wrote: | And didn't you read anything I wrote up in my fancy post either? Typecasting to int is bad for this, do some rounding function like the one I have listed above. | I think I totally missed there was a page #3 to the thread when I was viewing the last unread message (on page #2).
Sowwy
Nice post there, MGB, thanks.
|
|
Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
Software by php BB © php BB Group Server Load: 26 page(s) served in previous 5 minutes.
|