|
|
Little help with simple ai
#459929
06/12/16 13:20
06/12/16 13:20
|
Joined: Jul 2001
Posts: 4,801 netherlands
Realspawn
OP

Expert
|
OP

Expert
Joined: Jul 2001
Posts: 4,801
netherlands
|
Could someone help me with a simple enemy ai problem. I created this game based on pong on a 1 player vs pc the bat moves on the same y as the ball. But now it will never miss the ball so that seems a little unfair. How to make it react a bit slower so it can actually miss the ball to Here is what I have so far and it works however now It is unbeatable the clamp is needed cause the computer player must not leave the player area
action player2ai()
{
set(my,SHADOW | METAL);
player02 = me;
c_setminmax(me);
ent_create("spark2+16.tga", vector(player02.x,player02.y,player02.z+10), sprite_played);
while (!bbal) {wait (1);}
while(health2 >=1 )
{
blast_rocketpl22();
my.y = bbal.y;
my.y = clamp(my.y,-344,344);
///// my.y += 1 * (bbal.y - bbal.y);
wait(1);
}
ent_remove(astron);
ent_remove(bbal);
ent_remove(bringer);
set(play1_pan,SHOW);
ent_create("explo2+16.tga", vector(player02.x,player02.y,player02.z+10), sprite_played);
wait(1);
ent_remove(me);
game_over();
}
i know i know should have paid more attention at math back at school  Credits in game for him who helps me out 
Last edited by Realspawn; 06/12/16 13:21.
|
|
|
Re: Little help with simple ai
[Re: Realspawn]
#459933
06/12/16 16:52
06/12/16 16:52
|
Joined: Dec 2011
Posts: 1,823 Netherlands
Reconnoiter
Serious User
|
Serious User
Joined: Dec 2011
Posts: 1,823
Netherlands
|
A bit more info about your current AI (e.g. a video) would help.  From what I understand, your problem now is that the AI always blocks player balls 100% succesfull. You could solve this by letting the AI calculate the move time to intercept a player ball. And than give it a chance to decrease or increase this value as if the AI made a mistake during its calculation. (so the AI goes to slow to or beyond the point where the player ball would hit) Another trick would be to give it a small chance the AI go suddenly to fast or to slow as if its fingers slipped of the mouse or such.
|
|
|
Re: Little help with simple ai
[Re: Realspawn]
#459936
06/12/16 18:22
06/12/16 18:22
|
Joined: Oct 2011
Posts: 1,082 Germany
Ch40zzC0d3r
Serious User
|
Serious User
Joined: Oct 2011
Posts: 1,082
Germany
|
I wrote a pong game in javascript some weeks ago, this is what I did:
/* An AI controller */
var AIController = function(paddle, ball) {
this.speed = paddle.MAX_SPEED;
this.paddle = paddle;
this.ball = ball;
this.lastTick = 0;
this.calcCnt = 0;
this.ballSign = -1;
if(this.paddle.isRightPaddle)
this.ballSign = 1;
};
AIController.prototype.play = function() {
if(pongGame.getOptions().difficulty == 0) {
this.REACT_TIME = 320;
this.ACT_TIME = 4;
this.MOVE_VEL = 6;
this.SPREAD = 1;
} else if(pongGame.getOptions().difficulty == 1) {
this.REACT_TIME = 250;
this.ACT_TIME = 8;
this.MOVE_VEL = 8;
this.SPREAD = 1;
} else if(pongGame.getOptions().difficulty == 2) {
this.REACT_TIME = 150;
this.ACT_TIME = 12;
this.MOVE_VEL = 10;
this.SPREAD = 1;
} else {
this.REACT_TIME = 80;
this.ACT_TIME = 20;
this.MOVE_VEL = 14;
this.SPREAD = 0;
}
this.paddle.isCPU = 1;
this.paddle.speed = this.speed;
if(Math.sign(this.ball.xSpeed) == this.ballSign) {
if(this.calcCnt < this.ACT_TIME) {
var deltaX;
if(this.SPREAD)
deltaX = Math.abs((this.paddle.xPos) - this.ball.xPos) * Math.random()*2 - 1;
else
deltaX = Math.abs((this.paddle.xPos) - this.ball.xPos);
var deltaY = (this.paddle.yPos + this.paddle.paddleHeight / 2 + deltaX / (this.paddle.gameWidth / this.MOVE_VEL)) - this.ball.yPos;
if(deltaY > 0) {
this.paddle.yPos -= Tools.clamp(this.paddle.speed * Math.abs(deltaY) / (this.paddle.gameWidth / this.MOVE_VEL), 0, this.speed);
this.paddle.startMovingUp();
} else if(deltaY < 0) {
this.paddle.yPos += Tools.clamp(this.paddle.speed * Math.abs(deltaY) / (this.paddle.gameWidth / this.MOVE_VEL), 0, this.speed);
this.paddle.startMovingDown();
}
this.paddle.yPos = Tools.clamp(this.paddle.yPos, 0, this.paddle.gameHeight - this.paddle.paddleHeight);
this.calcCnt++;
} else {
var ticks = new Date().getTime();
if(ticks - this.lastTick > this.REACT_TIME)
{
this.lastTick = ticks;
} else {
this.calcCnt = 0;
}
}
} else {
this.calcCnt = this.ACT_TIME;
}
}
|
|
|
Re: Little help with simple ai
[Re: Dooley]
#459940
06/12/16 18:51
06/12/16 18:51
|
Joined: May 2005
Posts: 867 Chicago, IL
Dooley
User
|
User
Joined: May 2005
Posts: 867
Chicago, IL
|
Something like this: if(my.y < ball.y) { my.y += 10 * time_step;//not sure what speed to put here } else if(my.y > ball.y) { my.y -= 10 * time_step; } EDIT: This will probably cause the bat to jitter up and down a bit, so you could add a little gap, like: if(my.y < ball.y - 1) if(my.y > ball.y + 1) You're making me want to write a pong game 
Last edited by Dooley; 06/12/16 18:56.
|
|
|
Re: Little help with simple ai
[Re: Realspawn]
#459943
06/12/16 19:18
06/12/16 19:18
|
Joined: May 2005
Posts: 867 Chicago, IL
Dooley
User
|
User
Joined: May 2005
Posts: 867
Chicago, IL
|
Awesome, glad to help! I tried some multiplayer stuff a while ago. Look up Locoweeds Multiplayer Tutorial. I forgot if it was Lite-C or C-Script... And if you need a beta-tester, I'm your guy 
|
|
|
|