Server Help

Bot Questions - Warping multiple times in a specific radius

Quan Chi2 - Mon Sep 12, 2005 9:32 pm
Post subject: Warping multiple times in a specific radius
How would I specify a certain number of tiles to warp randomly in on a command?

Like...



Just tell me how to specify the radius in this case, please..
I want to try to code the rest myself..

TY in advance...
Muskrat - Mon Sep 12, 2005 9:39 pm
Post subject:
I dont think you explained it very well, but if you want it to be warped somewhere randomly within that region, you need to come up with a way to check if a point is in it. For a square it would be much easier, just get a random x between x1 and x2, and likewise for y. Or, you could specify a center tile and add or subtract a random amount of tiles within 0 and the radius.
Quan Chi2 - Mon Sep 12, 2005 10:01 pm
Post subject:
I mean.. like... I do want to make it square.. So you're saying to get a random number? and then how would I specify the coords around the random number?

What command in other words.. I can get a random number, but how could I tell the bot what coords are around the number i specified? Is that a good enough explaination?
Mine GO BOOM - Mon Sep 12, 2005 10:28 pm
Post subject:
If you want to pick a random spot inside of a circle, you choose two random values. One value is anywhere between 0 and the radius of the circle. The next value is anything between 0 and 2*pi. Now, using trigonometry, convert those polar coordinates into rectangular. Then add those X and Y values to the center point of the circle.

To help you learn this, I'll leave out the math for converting polar to rectangular. Use google or a math book to work that one out.
Quan Chi2 - Mon Sep 12, 2005 10:47 pm
Post subject:
k ty.. lol i never took this before except basic trig tongue.gif
Quan Chi2 - Mon Sep 12, 2005 11:03 pm
Post subject:
jeeeeeeeeeeeeeeeeeeeesus this math is crazy.... this must be like advanced trig.. wtf loll... i'll get through it
Muskrat - Mon Sep 12, 2005 11:49 pm
Post subject:
I suggest going with a square for now which will be much easier until you learn the polar to rectangular coordinates...

Get two random numbers between your radius and the negative value of your radius, then just add one to the x-coord and one to the y-coord and you've got a random spot in the square.
SamHughes - Tue Sep 13, 2005 12:05 am
Post subject:
Mine GO BOOM wrote:
If you want to pick a random spot inside of a circle, you choose two random values. One value is anywhere between 0 and the radius of the circle. The next value is anything between 0 and 2*pi. Now, using trigonometry, convert those polar coordinates into rectangular. Then add those X and Y values to the center point of the circle.

To help you learn this, I'll leave out the math for converting polar to rectangular. Use google or a math book to work that one out.


This method isn't very evenly distributed. Provided your values in [0, r) and [0, 2*pi) are evenly distributed, a given area at distance x from the center will get twice as many 'darts' as an equally sized area at distance 2x from the center.

If an even distribution mattered (I actually like your distribution better, but I might as well mention...), I'd use rectangular coordinates where x ranges in [-r,r] and y ranges in [-r,r] and pick random coordinates until x*x + y*y < r*r.

MGB's distribution gives an average radius of r/2, with a denser distribution near the center point, while the even distribution gives an average radius of 2r/3.
Quan Chi2 - Tue Sep 13, 2005 7:32 am
Post subject:
Smart.
Bak - Tue Sep 13, 2005 9:42 am
Post subject:
no we're not. It's the pathagareon theorm (distance formula). r = sqrt(x^2 + y^2). All he did was sqaure both sides to speed up calculating it on the computer (r^2 = x^2 + y^2)
Quan Chi2 - Tue Sep 13, 2005 3:52 pm
Post subject:
lol i learned that! :d last year i didnt think that i would ever have to use it again :/
pfft lol
Quan Chi2 - Tue Sep 13, 2005 3:55 pm
Post subject:
k now what commands/functions am i using here? I have to get a radom number, that I know.. but what other commands am i using? I saw the antiwarp src on Distort's site.. and it said 'radius'.. will i have to use something like that? is radius a built in command?
Bak - Tue Sep 13, 2005 3:57 pm
Post subject:
no you'll have to use variables. Find a good c++ reference online or in book form.
Quan Chi2 - Tue Sep 13, 2005 8:59 pm
Post subject:
k icon_smile.gif
Quan Chi2 - Tue Sep 13, 2005 10:31 pm
Post subject:
I'll have to include the math header file right?
Bak - Tue Sep 13, 2005 11:26 pm
Post subject:
for what?
Quan Chi2 - Wed Sep 14, 2005 12:15 am
Post subject:
i dunno... im trying to figure out how to make exponents in the code lol.. im reading C++ for dummies biggrin.gif
SamHughes - Wed Sep 14, 2005 12:56 am
Post subject:
You don't need exponents to square a number.

What would you need exponents for? Anyway, if they're double-precision approximations of numbers, you'd use the function pow; if they're integers, you could make your own function if you wanted.
Quan Chi2 - Wed Sep 14, 2005 7:30 am
Post subject:
k ty
Quan Chi2 - Wed Sep 14, 2005 5:45 pm
Post subject:
um...

int radius;
Sqrt (4*4) + (3 * 3);/*Pythagorean Theorem read from a website*/


i have a strong feeling that im doing this wrong :/
I don't mean to be annoying.. i've just never done this before :/ Im looking through references though.

I'll learn by trial and error I guess.. But am I doing it wrong?
Dr Brain - Wed Sep 14, 2005 5:46 pm
Post subject:
In a word, yes.
Quan Chi2 - Wed Sep 14, 2005 6:04 pm
Post subject:
yesss biggrin.gif wait.. noooo... Wait you mean its correct? or is it not?
Muskrat - Wed Sep 14, 2005 6:10 pm
Post subject:
Not.
Dr Brain - Wed Sep 14, 2005 7:01 pm
Post subject:
Quan Chi2 wrote:
yesss biggrin.gif wait.. noooo... Wait you mean its correct? or is it not?


You asked if you were doing it wrong. I said yes. Draw your own conclusion.
Quan Chi2 - Wed Sep 14, 2005 9:31 pm
Post subject:
lol k
Bak - Thu Sep 15, 2005 12:35 am
Post subject:
don't rely on a website to do math for you. You can learn it off a website and then apply it to your problem. That's how it's meant to be done anyways
Quan Chi2 - Thu Sep 15, 2005 4:15 pm
Post subject:
Yes, Im trying to do that. I know the math, but I'm having trouble putting it in the code... I have almost no idea how to do this besides making an equation.. I'm just trying to.
Mine GO BOOM - Thu Sep 15, 2005 10:40 pm
Post subject:
Code: Show/Hide
void PickRandomSpotInCircle(int CircleX, int CircleY, int Radius, int &XCoord, int &YCoord)
{
   double theta, r;

   r = (double)rand() / RAND_MAX * Radius;
   theta = (double)rand() / RAND_MAX * 6.28319; /* aka: 2pi */

   XCoord = (int)(r * cos(theta)) + CircleX;
   YCoord = (int)(r * sin(theta)) + CircleY;
}

Quan Chi2 - Thu Sep 15, 2005 11:03 pm
Post subject:
is that a built in function?
Quan Chi2 - Thu Sep 15, 2005 11:04 pm
Post subject:
ty for the help icon_smile.gif
Quan Chi2 - Fri Sep 16, 2005 3:07 pm
Post subject:
hey lol id you make that up yourself? looks cool lol seriously tongue.gif im looking up some things in that code to learn how it works hehe
Bak - Fri Sep 16, 2005 3:42 pm
Post subject:
(shakes head)

stop triple posting. And only post relevant things, not "cool"
Quan Chi2 - Fri Sep 16, 2005 3:49 pm
Post subject:
lol sorry









(oops i did it again :/ )
Quan Chi2 - Fri Sep 16, 2005 5:28 pm
Post subject:
um.. im supposed to fill in the coords and stuff right?
Muskrat - Fri Sep 16, 2005 5:47 pm
Post subject:
That would have been a perfect opportunity to use the edit button.
Mine GO BOOM - Fri Sep 16, 2005 11:10 pm
Post subject:
Quan Chi2 wrote:
hey lol id you make that up yourself? looks cool lol seriously :P im looking up some things in that code to learn how it works hehe

Yes. A simple google search on polar to rectangular returned the exact math you needed. The original idea is what I posted back in my first message in this thread.
Quan Chi2 wrote:
um.. im supposed to fill in the coords and stuff right?

You'll need fill in three values, CircleX, CircleY, and Radius. These should be known values of the circle you want to warp into, as that is what you mentioned in the very first post. The XCoord and YCoord doesn't need to be set to anything, as the function sets the return values there.

Example usage:
int x,y;
PickRandomSpotInCircle(512, 512, 10, x, y);
sendMessage(playerID, "*warpto %d %d", x, y);

The sendMessage is different for each bot core. I don't use MERV at all, thus I don't know if it supports formatting or you guys just use the string class everywhere.
Quan Chi2 - Fri Sep 16, 2005 11:25 pm
Post subject:
ok.. i see what you're saying. in the first post i just didnt understand. i had done a google search on polar to rectangular but i didnt understand the math.. not quite at that level yet tongue.gif Thank you very much for the help
Underlord - Sun Sep 18, 2005 9:12 pm
Post subject:
Sample Code to warp pilot within a box size area
Code: Show/Hide

#include "time.h"

// do   srand(time(NULL));   when the bot logins or just once at any point before rand(); is called

void botInfo::PutPilotInGame(Player *p)
{
   // warp pilot into game, 100 square area from coordinate 450,450
   rand();

   int coordx = 450;
   int coordy = 450;

   coordx = (int) (100 * ((float)rand()/RAND_MAX)) + 450;
   coordy = (int) (100 * ((float)rand()/RAND_MAX)) + 450;

   // warpto
   sendPrivate(p,"*warpto " + (String) coordx + " " + (String) coordy);
}

Bak - Sun Sep 18, 2005 10:16 pm
Post subject:
Code: Show/Hide
100 * ((float)rand()/RAND_MAX)

is equivilent to
Code: Show/Hide
rand() % 100


but I think mod is faster than division and multiplication.
Mine GO BOOM - Sun Sep 18, 2005 10:48 pm
Post subject:
rand() is not a truly random number. The high-order bits are requested to be more random than the lower bits by the official standard. For the most part, most system's rand() is good enough that you can just mod the number you want, but to get a more random number, you should use the high-order bits, which involves using the methods Underlord and I both posted.

Doing a quick test on the standard deviation of rand() on different amounts with different sample sets shows that both methods on Windows 2000's stdlib work almost identical. On other system's implementation of rand, it maybe different.

And in the case of speed, the bot is usually not trying to be real-time, and spends most of its time idling. So in this case, its better to error on the side of more accurate than speed. And in the case where you are using the string class, the speed between these methods is the least of your worries.
Underlord - Wed Sep 21, 2005 4:48 am
Post subject:
Quote:
Do NOT use

y = rand() % M;

as this focuses on the lower bits of rand(). For linear congruential random number generators, which rand() often is, the lower bytes are much less random than the higher bytes. In fact the lowest bit cycles between 0 and 1. Thus rand() may cycle between even and odd (try it out). Note rand() does not have to be a linear congruential random number generator. It's perfectly permissible for it to be something better which does not have this problem.

Bak - Wed Sep 21, 2005 11:47 pm
Post subject:
learn something new every day...

This actually came up a day after reading MGB's responce. I made a c program for a microcontroller and it was supposed to chose a random color for a bi-color LED, red or green. However, it chose red 95% of the time. Thinking about what mgb said, I changed
Code: Show/Hide
rand() % 2

to
Code: Show/Hide
(rand() % 10000) / 5000

and it finally started picking green a lot more. Funny how this doesn't come up in years of programming until the day after someone tells it to you. Another option I was considering was to load an array with like 5 red and 5 green values, then doing a random shuffle on the data, so for any 10 tries you'd get 5 reds and 5 greens, but the order would be random.
Dr Brain - Thu Sep 22, 2005 10:39 am
Post subject:
On all of the uCs I've programmed there hasn't been a rand() so I've always had to write my own psudeo-random number generator. Usually just a shift and xor does the trick.
Quan Chi2 - Sat Sep 24, 2005 12:43 am
Post subject:
why did Underlord include time.h?
Dr Brain - Sat Sep 24, 2005 12:51 am
Post subject:
Probably because he used a function in time.h. Just a guess...
Quan Chi2 - Sat Sep 24, 2005 1:17 am
Post subject:
lol.. funny
Bak - Sat Sep 24, 2005 4:11 am
Post subject:
Code: Show/Hide
// do   srand(time(NULL));   when the bot logins or just once at any point before rand(); is called


time() is defined in time.h
Maverick - Sun Oct 23, 2005 10:30 am
Post subject:
I would like to add to this topic that I have found a topic in another forum where this exact topic is discussed. Also MGB's code is discussed there.

http://gamedev.net/community/forums/topic.asp?topic_id=72305

It comes down to that MGB's code doesn't completely work (picks points to the center too much) and the following code does work:

(too bad the picture in the thread is gone)

Code: Show/Hide
#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);

Quan Chi2 - Sun Oct 23, 2005 8:02 pm
Post subject:
I tried..


Code: Show/Hide

   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));

            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);
            {
               String s;
               s += "*warpto";
               s += " X Y";
               sendPrivate(p, s.msg);
               sendPrivate(p, "You have dodged your opponents weapons!  Now go for the kill!");
            }
         }
      }
   }
}


I think I made a good attempt though icon_smile.gif

It does the message but it doesnt do the warp.. anything wrong?
Dr Brain - Sun Oct 23, 2005 8:41 pm
Post subject:
Yes, your bot will say exactly (no replacement) "*warpto X Y" The server doesn't know how to warp a player there.
Mine GO BOOM - Sun Oct 23, 2005 10:37 pm
Post subject:
Code: Show/Hide
#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:
Code: Show/Hide
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.
Dr Brain - 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.
Maverick - Mon Oct 24, 2005 4:54 am
Post subject:
Fixed code that should work:

Code: Show/Hide

   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
Code: Show/Hide
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(); .
Mine GO BOOM - 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.
Maverick - 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 icon_smile.gif
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.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group