haxball / haxball-issues

114 stars 42 forks source link

ball #1344

Open korpro opened 3 years ago

korpro commented 3 years ago

Hello is it possible to set ball boost after player hold it for few time or in custom area? For example when you pass ball before 3 s speed of ball is normal, after you keep ball for 3s ball is much faster like shot not pass. Or when you hold the ball and reach area which gives ball speed boost to shot BR

thenorthstar commented 3 years ago

Hello is it possible to set ball boost after player hold it for few time or in custom area? For example when you pass ball before 3 s speed of ball is normal, after you keep ball for 3s ball is much faster like shot not pass. Or when you hold the ball and reach area which gives ball speed boost to shot BR

@korpro I'm not completely sure but I believe that you can use it with inside a timeout (For example, you can start a timeout when player touches the ball in a custom area). By the way, 1 second maybe enough to boost the ball.

korpro commented 3 years ago

Hello is it possible to set ball boost after player hold it for few time or in custom area? For example when you pass ball before 3 s speed of ball is normal, after you keep ball for 3s ball is much faster like shot not pass. Or when you hold the ball and reach area which gives ball speed boost to shot BR

@korpro I'm not completely sure but I believe that you can use it with inside a timeout (For example, you can start a timeout when player touches the ball in a custom area). By the way, 1 second maybe enough to boost the ball.

THANKS for answer, but how use tiemout to set ball boost?

thenorthstar commented 3 years ago

Hello is it possible to set ball boost after player hold it for few time or in custom area? For example when you pass ball before 3 s speed of ball is normal, after you keep ball for 3s ball is much faster like shot not pass. Or when you hold the ball and reach area which gives ball speed boost to shot BR

@korpro I'm not completely sure but I believe that you can use it with inside a timeout (For example, you can start a timeout when player touches the ball in a custom area). By the way, 1 second maybe enough to boost the ball.

THANKS for answer, but how use tiemout to set ball boost?

@korpro I have made an example of this ball boost. Frankly there was no need to use timeout. Anyway, let's jump to the code:

var powerActive = false; //You can kick the ball faster when this is true.
var PowerCoefficient = 3; //Original ball kick speed would be multiplied by this number when power shot is activated.
var TimeOut = 180; //This means 3 seconds.
var TimePlayerBallTouch = 0; //The time indicator that increases as player touched to the ball

var assistingTouch = "";
var lastPlayerTouched = "";
var lastTeamTouched = 0;
var previousPlayerTouched;
var radiusBall = 10; //Classic map puck radius, you can change it with respect to the ball radius of your map.
var radiusPlayer = 15; //The original player radius, you can change it with respect to the player radius of your map.
var triggerDistance = radiusBall + radiusPlayer + 0.01; //Player ball distance tolerance. You can increase it for less sensitivity.

var room = HBInit({roomName:"TEST",playerName:"",noPlayer:true,public:true,maxPlayers:12});

function getLastTouchTheBall(){
    var ballPosition = room.getBallPosition();
    var players = room.getPlayerList();
    for(var i=0; i<players.length; i++){
        if(players[i].position != null){
            var distanceToBall = pointDistance(players[i].position,ballPosition);
            if(distanceToBall < triggerDistance){
                if(lastPlayerTouched.id != players[i].id){
                    if(lastTeamTouched==players[i].team){
                        assistingTouch = lastPlayerTouched;
                    }
            else{
            assistingTouch = "";
            }
                }
                lastTeamTouched = players[i].team;
                previousPlayerTouched == lastPlayerTouched;
                lastPlayerTouched = players[i];
            }
        }
    }
    return lastPlayerTouched;
}

function pointDistance(p1,p2){
    var d1 = p1.x-p2.x;
    var d2 = p1.y-p2.y;
    return Math.sqrt(d1*d1 + d2*d2);
}

function CheckPowerShot(){
    if(pointDistance(room.getPlayerDiscProperties(lastPlayerTouched.id),room.getDiscProperties(0)) < triggerDistance){
    TimePlayerBallTouch++;

    if(TimePlayerBallTouch == TimeOut){
        room.sendAnnouncement("✅ Power shot activated!",null,0x00FF00,"italic",2); //Power shot is activated when the player touches to the ball for 3 seconds long.
    }
    if(TimePlayerBallTouch >= TimeOut){
        if(powerActive == false){
        powerActive = true;
        }
    }
    }
    else{
    if(TimePlayerBallTouch != 0){ //Touch timer is reset when the contact between player and ball is interrupted.
        TimePlayerBallTouch = 0;
        //room.sendAnnouncement("🚫 Power shot inactivated!",null,0xFF0000,"italic",2); //You can remove this to prevent bot to spam too much.
    }
    }
}

room.onPlayerBallKick = function(player){ //Ball speed is multiplied by the speed coefficient when power shot is active.
    if(powerActive == true){
    room.setDiscProperties(0,{xspeed:PowerCoefficient * room.getDiscProperties(0).xspeed,yspeed:PowerCoefficient * room.getDiscProperties(0).yspeed});
    powerActive = false;
    }
}

room.onGameStop = function(byPlayer){ //This is important to avoid from gametick errors.
    lastPlayerTouched = "";
}

room.onGameTick = function(){
    if(room.getPlayerList().filter(p => p.team != 0).length > 0 && lastPlayerTouched != ""){ //This is also important to avoid from gametick errors.
    CheckPowerShot();
    }

    getLastTouchTheBall(); //You have to use this in here to get the last toucher.
}

And the following is how this code's output looks like: https://thehax.pl/forum/powtorki.php?nagranie=e64972a8f1d2a27e167ca84e7da1609d

I wish this code could be helpful for you and who want to use it in their bots.

korpro commented 3 years ago

stradi you are awesome, big boss, thank you :)

korpro commented 3 years ago

Dear @thenorthstar, do you think its possible to do same thing but power shot depend on area you are in? E.g. power shot is available not after few seconds but when player is in penalty area?

thenorthstar commented 3 years ago

Dear @thenorthstar, do you think its possible to do same thing but power shot depend on area you are in? E.g. power shot is available not after few seconds but when player is in penalty area?

@korpro Yes, also you can set it depend on the direction of player (that is, angle between the player's move direction and goal). For example, Turkish auto-power RS bot rooms use a similar script. But making it is a bit hard and requires some angular measurements as an addition to coding stuff.