Scripting AI for pong

Non-platform specific questions.

Scripting AI for pong

Postby effix » Sat Mar 25, 2006 8:41 pm

Im trying to figure out this scripting thing.. because I want to create a small AI for my Pong game.. but i cant seem to get past the pseudo stage :D hehe... I cant seem to find a tutorial for scripting in GE C-script...
If I wanted to script something like this how would I do it?

Pseudo code:
HitProcentage=30 //Level dependant.. Level 1= low HitProcentage Level 9=high HitProcentage

if random number<HitProcentage
then move paddle to location near ball
else move paddle to some other location near the ball but not tuching it..

Does it make any sense?

TIA
Regards
Effix
Last edited by effix on Wed Mar 29, 2006 6:02 pm, edited 1 time in total.
User avatar
effix
 
Posts: 14
Joined: Sun Mar 19, 2006 7:37 pm
Location: Denmark
Score: 0 Give a positive score

Postby Fuzzy » Sun Mar 26, 2006 12:43 am

I have a little function that I use that may suit your purposes. I was going to save for later and use it in a demo or tutorial, but I think this is a good place to introduce it to the GE users.

go to your global code script editor, and type this in.

int Lint(int a, int z, double j) {return (int)(a*(1-j) + z*j);}

At the bottom, there is a blank beside name. Type in something descriptful, like Useful Functions. Hit add.

Now, I know it looks a little obfuscated, but its really simple, and you dont need to know about the guts of it. Here is how you use it. (you can only access it in the script editor, it doesnt appear in the event or action menus)

You feed it three numbers, two that are integers(no decimals), and one that is a floating point number(use decimal points). When you do this, the function will return a number which is partway between the first two numbers, using the third number as a distance. It rounds down to the nearest whole number.

Thats messy, I'll give you some examples.

Pretend your AI paddle is at x=100, and the ball is heading towards x=130. You want the paddle to head that way, and hopefully intercept the ball. The AI on this level is 70% effective, or 0.7.

In the place that your paddle does its thinking, you place this line:

TargetX=Lint(100,130,0.7);

TargetX is a variable that is used to store where the paddle is headed of course. TargetX should now be equal to 121, if all goes right, and you use your draw and movement functions(like moveto()) to move the paddle there.

The nice thing about this is it removes the need to think about whether or not you want to block the ball. The third number, in conjunction with the size of the paddle determines that.

The function also saves you the problem of figuring out whether you want to move left or right; It doesnt matter if you put Lint(130,100, 0.7) or Lint(100,130,0.7). THe only thing to watch for is that the third number is not greater than 1.0.

Please reply if any of this is unclear.

Oh, Lint stands for Linear Interpolation, if you wrere wondering.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Postby effix » Sun Mar 26, 2006 12:59 pm

Thanks ThreeFingerPete it seems to be working now :) now I just have to spice it up at little so it gets increasingly difficult. :) finally my game starts to look playable :)
But fear not my firends I will return with loads and loads of questions MUHAHAHAH!! :D
Last edited by effix on Sun Mar 26, 2006 6:41 pm, edited 1 time in total.
User avatar
effix
 
Posts: 14
Joined: Sun Mar 19, 2006 7:37 pm
Location: Denmark
Score: 0 Give a positive score

Postby Fuzzy » Sun Mar 26, 2006 4:02 pm

So that worked for that application? I always worry that I dont explain myself properly, or that I fail to understand peoples needs...
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Postby effix » Sun Mar 26, 2006 6:43 pm

Yes it worked.. but im trying to combine it with a randomness feature but i cant get it to work... :( guess I need som serious practice in scripting :)
User avatar
effix
 
Posts: 14
Joined: Sun Mar 19, 2006 7:37 pm
Location: Denmark
Score: 0 Give a positive score

Postby Fuzzy » Sun Mar 26, 2006 7:43 pm

Just use randomness in the floating point(third) number. Pick a random number from 1-10 divide it by 10, and use that. Better yet, divide by 100 and add that to a base number, such as 0.4.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Randomness.. :)

Postby effix » Wed Mar 29, 2006 1:18 pm

Yes Pete.. that would work... but its not that part of the code that I want to be random.
what I am trying to make is the following..
when the ball is on the CPU side of the level it uses the linear interpolation code you helped me with.. but when the ball is on the players side of the level the cpu_paddle moves more or less random... the idea is to make the CPU look a little more human.
Ive been trying to do it with "if else" but that didnt work.. so for now im trying to create a menu for my game. :)
User avatar
effix
 
Posts: 14
Joined: Sun Mar 19, 2006 7:37 pm
Location: Denmark
Score: 0 Give a positive score

Postby Fuzzy » Wed Mar 29, 2006 11:13 pm

eep! i never even thought of what the CPU would be doing as the ball heads away from it. Have you considered having the CPU taunt the player with text messages? You could read them from a file. THis would allow the player to customize that file.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

great idea...

Postby effix » Thu Mar 30, 2006 7:34 am

Thats a great idea :) Ill put it on my todo list.
User avatar
effix
 
Posts: 14
Joined: Sun Mar 19, 2006 7:37 pm
Location: Denmark
Score: 0 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest