Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
Warping multiple times in a specific radius
Goto page Previous  1, 2, 3
 
Post new topic   Reply to topic Printable version
 View previous topic  Buy.ini Post :: Post Football source up for grabs....  View next topic  
Author Message
Mine GO BOOM
Hunch Hunch
What What
Hunch Hunch<br>What What


Age:41
Gender:Gender:Male
Joined: Aug 01 2002
Posts: 3615
Location: Las Vegas
Offline

PostPosted: Sun Oct 23, 2005 10:37 pm    Post subject: Reply to topic Reply with quote

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.




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
View users profile Send private message Add User to Ignore List Send email
Dr Brain
Flip-flopping like a wind surfer


Age:39
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Sun Oct 23, 2005 11:05 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Maverick
broken record


Age:40
Gender:Gender:Male
Joined: Feb 26 2005
Posts: 1521
Location: The Netherlands
Offline

PostPosted: Mon Oct 24, 2005 4:54 am    Post subject: Reply to topic Reply with quote

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(); .
_________________
Nickname: Maverick (I changed my name!)
TWCore developer | Subspace statistics
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Mine GO BOOM
Hunch Hunch
What What
Hunch Hunch<br>What What


Age:41
Gender:Gender:Male
Joined: Aug 01 2002
Posts: 3615
Location: Las Vegas
Offline

PostPosted: Mon Oct 24, 2005 11:23 am    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Send email
Maverick
broken record


Age:40
Gender:Gender:Male
Joined: Feb 26 2005
Posts: 1521
Location: The Netherlands
Offline

PostPosted: Mon Oct 24, 2005 11:43 am    Post subject: Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Bot Questions All times are GMT - 5 Hours
Goto page Previous  1, 2, 3
Page 3 of 3

 
Jump to:  
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
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 26 page(s) served in previous 5 minutes.

phpBB Created this page in 0.542859 seconds : 31 queries executed (79.7%): GZIP compression disabled