Table hockey AI collab

Work together on Algodoo projects here.

Table hockey AI collab

Postby Kilinich » Sun Nov 29, 2009 9:55 pm

Hi, ppl!

Honestly, I newer like collab forum because I can do everything by myself and didn't like to share unfinished scenes. But now I change my mind for very interesting and potentially one of the best phun/algodo game.

It's a table hockey game (like Stiga's but simplified a bit).
I've make a table, players, controls, puck and all you need is make an AI.

Player 1 is a human controlled (by default, but it's easy to change!).

Player 2 is AI controlled.
You have 5 positions of "players" and 10 variables:

scene.my.pl2pos#len - distance of player from middle of table.
scene.my.pl2pos#ang - angle of his stick (radians, 0 is horizontal, to the right).
# - number of player from 1 to 5

plus
scene.my.pl2pos0len - position of goalkeeper.

The puck have a coordinates of scene.my.k1pos (not implemented yet)

Here it is an unfinished table (will be finalized till next weekend I believe):
Rating: rated 4.7
Filesize: 388.38 kB
Comments: 1
Ratings: 4
download


So! Your task is to write a functions scene.my.pl2pos#len, scene.my.pl2pos#ang to control the AI player and defeat a human player (or another AI, we can make it real!).
Who's with me (can write a complete AI for table hockey)? More AI - more fun to play!

p.s. Soon I'll give more info like standards dimensions and helpful function...
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Table hockey AI collab

Postby gradyfitz » Mon Nov 30, 2009 1:57 pm

I'm up for this :D, here's a code I wrote which I think should work when everything is plugged in.
Code: Select all
//Table Hockey AI
//      Collaboration by Kilinich

/* /// Code Objective \\\
/Grady\
-- Create an AI capable of varying levels of difficulty, able to move and rotate paddles depending on the movement of the puck,
-- Allow, if required, a switch between AI and Human players on both player 1 and/or 2.

/// Pseudocode \\\
// Level 1 \\
If the puck is in the area, switch to paddle nearby and move to meet puck at x axis.
If the puck is within hitting radius, spin paddle.
Difficulty settings-
Time between switches.
Strength of paddle.

// Level 2 \\
If the puck is in the Top Left, Spinner 2 activates, Top Right,  Spinner 4 activates, Bottom Left, Spinner 1 activates, Bottom Middle, Spinner 3 activates, Bottom Right, Spinner 5 activates. Calculate length to puck.
If the puck is within hitting radius of calculated position, spin paddle.

The AI may only switch  from one paddle to another every set amount of time, determined by the difficulty at which the AI plays.
The AI rotates the paddle modularly of the time multiplied by a factor determined by the difficulty at which the AI plays, by a full rotation, subtracting half a rotation.

/// Difficulty Levels \\\
Presets can be configured, these will set the Difficulty settings to give the AI different levels of "ability", easy, medium, hard and such.
*/

// / Global Variables \ \\
Scene.my.PuckPosition = [1,1]; // Hypothetical number, later to be replaced by puck beacon.

// / Difficulty Settings \ \\
// AI 1 \\
Scene.my.AI1Preset = "Custom"; // The difficulty of AI to be displayed.
Scene.my.AI1MinSwitchTime = 0; // The time an AI must wait before switching paddle control.
Scene.my.AI1PaddleStrength = 1; // The factor of strength that an AI uses a paddle for.
Scene.my.AI1Engaged = false; // Whether control of user 1 is by AI (true) or by Human (false).
Scene.my.AI1MaxSeekSpeed = 1; // The Speed at which the AI maximally allowed to seek to the position of the puck (for when AI must switch to another paddle which happens to be further away from the puck than desired).
Scene.my.AI1RotationalRadius = 0.9; // AI will engage spinning motion when the puck is within this radius.

// AI 2 \\
Scene.my.AI2Preset = "Custom"; // The difficulty of AI to be displayed.
Scene.my.AI2MinSwitchTime = 0; // The time an AI must wait before switching paddle control.
Scene.my.AI2PaddleStrength = 1; // The factor of strength that an AI uses a paddle for.
Scene.my.AI2Engaged = true; // Whether the control of user 2 is by AI (true) or by Human (false).
Scene.my.AI2MaxSeekSpeed = 1; // The Speed at which the AI maximally allowed to seek to the position of the puck (for when AI must switch to another paddle which happens to be further away from the puck than desired).
Scene.my.AI2RotationalRadius = 0.9; // AI will engage spinning motion when the puck is within this radius.

// / AI Rules \ \\
// AI 1 \\
Scene.my.AI1Area = [[[-5,8],[-2,0.5]],[[-5,0.5],[0,-7.5]],[[-2,7.5],[2,0.5]],[[0,-0.5],[5,-8]],[[2,7.5],[5,0.5]]]; // Areas within which certain paddles are activated p1,p2,p3,p4,p5, top-left co-ord & Bottom-right co-ord
Scene.my.AI1Control = 1; // The paddle AI 1 is currently controlling (used when switching).
Scene.my.AI1LastSwitch = 0; // The time the AI last switched paddles.
Scene.my.AI1CommandStructure = {
x := Scene.my.PuckPosition;
y := Scene.my.AI1Area;
x(0) < y(0)(0)(0) && x(1) < y(0)(0)(1) && x(0) > y(0)(1)(0) && x(1) > y(0)(1)(1) ? {Scene.my.AI1Switch(1)} : {};
x(0) < y(1)(0)(0) && x(1) < y(1)(0)(1) && x(0) > y(1)(1)(0) && x(1) > y(1)(1)(1) ? {Scene.my.AI1Switch(2)} : {};
x(0) < y(2)(0)(0) && x(1) < y(2)(0)(1) && x(0) > y(2)(1)(0) && x(1) > y(2)(1)(1) ? {Scene.my.AI1Switch(3)} : {};
x(0) < y(3)(0)(0) && x(1) < y(3)(0)(1) && x(0) > y(3)(1)(0) && x(1) > y(3)(1)(1) ? {Scene.my.AI1Switch(4)} : {};
x(0) < y(4)(0)(0) && x(1) < y(4)(0)(1) && x(0) > y(4)(1)(0) && x(1) > y(4)(1)(1) ? {Scene.my.AI1Switch(5)} : {};
Scene.my.AI1MoveUpdate;
Scene.my.AI1RotationUpdate}; // This calculates whether to switch paddles, and to update the movement.
Scene.my.AI1ZeroYPosition = [1,-1,1,-1,1]; // The Y position at 0 length, this helps determine how long the length should be.
Scene.my.AI1XPosition = [-2,-1,0,1,2]; // The X positions of each position, this helps determine whether to rotate or not.
Scene.my.AI1MoveUpdate = {x := Scene.my.AI1ZeroYPosition; y := (Scene.my.AI1Control - 1); z := eval("Scene.my.pl1pos" + Scene.my.AI1Control + "len") ;x(y) < 0 ? {eval("Scene.my.pl1pos" + Scene.my.AI1Control + "len = " + (((z + Scene.my.PuckPosition(1))^2)^0.5 > Scene.my.AI1MaxSeekSpeed ? Scene.my.PuckPosition(1) : (Scene.my.PuckPosition(1) > z ? {z + Scene.my.AI1MaxSeekSpeed} : {z - Scene.my.AI1MaxSeekSpeed}) ))} : {}}; // Calculates the linear y axis movement to be made.
Scene.my.AI1RotationUpdate = {v := Scene.my.AI1XPosition(Scene.my.AI1Control - 1);w := Scene.my.PuckPosition;x := eval("Scene.my.pl1pos" + Scene.my.AI1Control + "len"); y := Scene.my.AI1Control; z := Scene.my.AI1RotationalRadius; w(0) >= (v - z) && w(0) <= (v + z) && w(1) >= (x - z) && w(1) <= (x + z) ? {eval("Scene.my.pl1pos" + Scene.my.AI1Control + "ang = {(Sim.time * Scene.my.AI1PaddleStrength) % (Math.pi * 2) - Math.pi}")} : {eval("Scene.my.pl1pos" + Scene.my.AI1Control + "ang = 0")}}; // Calculates the anglular movement to be made if within radius.
Scene.my.AI1Switch = (x)=>{x != Scene.my.AI1Control && (Scene.my.AI1LastSwitch + Scene.my.AI1MinSwitchTime) < Sim.time ? {Scene.my.AI1Control = x} : {}}; // Switches from current paddle to paddle (x) if possible.

// AI 2 \\
Scene.my.AI2Area = [[[-5,-0.5],[-2,-7.5]],[[-5,8],[0,0.5]],[[-2,-0.5],[2,-7.5]],[[0,7.5],[5,0.5]],[[2,-0.5],[5,-8]]]; // Areas within which certain paddles are activated p1,p2,p3,p4,p5, top-left co-ord & Bottom-right co-ord
Scene.my.AI2Control = 1; // The paddle AI 2 is currently controlling (used when switching).
Scene.my.AI2LastSwitch = 0; // The time the AI last switched paddles.
Scene.my.AI2CommandStructure = {
x := Scene.my.PuckPosition;
y := Scene.my.AI2Area;
x(0) < y(0)(0)(0) && x(1) < y(0)(0)(1) && x(0) > y(0)(1)(0) && x(1) > y(0)(1)(1) ? {Scene.my.AI2Switch(1)} : {};
x(0) < y(1)(0)(0) && x(1) < y(1)(0)(1) && x(0) > y(1)(1)(0) && x(1) > y(1)(1)(1) ? {Scene.my.AI2Switch(2)} : {};
x(0) < y(2)(0)(0) && x(1) < y(2)(0)(1) && x(0) > y(2)(1)(0) && x(1) > y(2)(1)(1) ? {Scene.my.AI2Switch(3)} : {};
x(0) < y(3)(0)(0) && x(1) < y(3)(0)(1) && x(0) > y(3)(1)(0) && x(1) > y(3)(1)(1) ? {Scene.my.AI2Switch(4)} : {};
x(0) < y(4)(0)(0) && x(1) < y(4)(0)(1) && x(0) > y(4)(1)(0) && x(1) > y(4)(1)(1) ? {Scene.my.AI2Switch(5)} : {};
Scene.my.AI2MoveUpdate;
Scene.my.AI2RotationUpdate}; // This calculates whether to switch paddles, and to update the movement.
Scene.my.AI2ZeroYPosition = [-7,1,-6.5,1,-7]; // The Y position at 0 length, this helps determine how long the length should be.
Scene.my.AI2XPosition = [-2,-1,0,1,2]; // The X positions of each position, this helps determine whether to rotate or not.
Scene.my.AI2MoveUpdate = {x := Scene.my.AI2ZeroYPosition; y := (Scene.my.AI2Control - 1); z := eval("Scene.my.pl2pos" + Scene.my.AI2Control + "len") ;x(y) < 0 ? {eval("Scene.my.pl2pos" + Scene.my.AI2Control + "len = " + (((z + Scene.my.PuckPosition(1))^2)^0.5 > Scene.my.AI2MaxSeekSpeed ? Scene.my.PuckPosition(1) : (Scene.my.PuckPosition(1) > z ? {z + Scene.my.AI2MaxSeekSpeed} : {z - Scene.my.AI2MaxSeekSpeed}) ))} : {}}; // Calculates the linear y axis movement to be made.
Scene.my.AI2RotationUpdate = {v := Scene.my.AI2XPosition(Scene.my.AI2Control - 1);w := Scene.my.PuckPosition;x := eval("Scene.my.pl2pos" + Scene.my.AI2Control + "len"); y := Scene.my.AI2Control; z := Scene.my.AI2RotationalRadius; w(0) >= (v - z) && w(0) <= (v + z) && w(1) >= (x - z) && w(1) <= (x + z) ? {eval("Scene.my.pl2pos" + Scene.my.AI2Control + "ang = {(Sim.time * Scene.my.AI2PaddleStrength) % (Math.pi * 2) - Math.pi}")} : {eval("Scene.my.pl2pos" + Scene.my.AI2Control + "ang = 0")}}; // Calculates the anglular movement to be made if within radius.
Scene.my.AI2Switch = (x)=>{x != Scene.my.AI2Control && (Scene.my.AI2LastSwitch + Scene.my.AI2MinSwitchTime) < Sim.time ? {Scene.my.AI2Control = x} : {}}; // Switches from current paddle to paddle (x) if possible.


Hopefully this is what you want :D, if not, just say.

EDIT: Just to let you know, I didn't factor in the goal keeper, so, that may have to be added later. Shouldn't be too hard :D. But this should be a start.
Mechanisms: 18 Mechanisms.
Thyme: Tutorial - Variables/Commands List.
Thymechanic
gradyfitz
 
Posts: 174
Joined: Tue Sep 01, 2009 8:33 am
Location: Victoria, Australia

Re: Table hockey AI collab

Postby savask » Mon Nov 30, 2009 3:43 pm

Can I join too?
User avatar
savask
 
Posts: 162
Joined: Mon Oct 12, 2009 2:53 pm
Location: Russia, Siberia

Re: Table hockey AI collab

Postby Kilinich » Mon Nov 30, 2009 4:13 pm

savask wrote:Can I join too?

Why not!? Just think how you AI will work and write a script!
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia


Return to Collab forum

Who is online

Users browsing this forum: No registered users and 9 guests

cron