haxball / haxball-issues

115 stars 43 forks source link

About room.onteamgoal #2027

Open SLNMX opened 1 year ago

SLNMX commented 1 year ago

Good afternoon everyone.

I have been trying to adapt a dodgeball map into the well known Wazar94 bot. I have found a predicament, I want to add a goal, when there's no people on one side of the field. Is there any possibility to do this?

thenorthstar commented 1 year ago

Good afternoon everyone.

I have been trying to adapt a dodgeball map into the well known Wazar94 bot. I have found a predicament, I want to add a goal, when there's no people on one side of the field. Is there any possibility to do this?

@SLNMX Yes, it's possible but don't need to use room.onTeamGoal about it. Let me explain how to do:

//Don't directly use this script without declaring the room variable above.

var collisionFlags = room.CollisionFlags;

function checkForTeamLengths() { //A function which must be in room.onGameTick should check if it is 1 or not.
    for (var i = 1; i <= 2; i++) {
        setLastPlayerAsScore(i);
    }
}

function getPlayersInTeam(teamID) { //Can be 0, 1 or 2. Here, 0 is unused.
    return room.getPlayerList().filter(p => p.team > 0 && p.team == teamID).length;
}

function resetPlayerCollisionFlags() {
    var players = room.getPlayerList().filter(p => room.getPlayerDiscProperties(room.getPlayerDiscProperties(p.id).cGroup & collisionFlags.score === 0));
    players.forEach(p => room.setPlayerDiscProperties(p.id).cGroup(collisionFlags.score | room.getPlayerDiscProperties(player.id).cGroup)); //This removes the "score" flag from the player without losing their initial (or previous) cGroup.
}

function setLastPlayerAsScore(teamID) {
    var players = room.getPlayerList();
    if (getPlayersInTeam(teamID) == 1) {
        var player = players.find(p => p.team == teamID);
        if (room.getPlayerDiscProperties(player.id) != null) { //Check if that player exists, for safe coding.
            if (room.getPlayerDiscProperties(player.id).cGroup & collisionFlags.score === 0) { //If the player doesn't have "score" in their cGroup
                room.setPlayerDiscProperties(player.id).cGroup(room.getPlayerDiscProperties(player.id).cGroup | collisionFlags.score); //This adds a "score" flag onto the player's cGroup property with saving their initial (or previous) cGroup.
            }
        }
    }
}

room.onGameTick = function () {
    checkForTeamLengths();
    //Another function(s) in room.onGameTick if exist(s).
}

room.onPositionsReset = function () {
    resetPlayerCollisionFlags();
    //Another function(s) in room.onPositionsReset if exist(s).
}

room.onTeamGoal = function (team) {
    var teams = ["Red", "Blue"];
    room.sendAnnouncement(`${teams[team - 1]} team wins the game! A new game will be started in a while.`);

    //The logic for starting a new game goes here...
}
SLNMX commented 1 year ago

Thanks a lot for your insight TheNorthstar. I have been trying to do it as you mentioned; right now just using a very rudimentary code which just includes the map (with the added features) and the code you have provided.

However, I have encountered that while it doesn't crash at any given time; it is not scoring a goal (just tried it in 1v1 and 2v2 situations). I have attached it. If you have any time to give it a look I will appreciate it. If not I do understand.

Thanks in advance. GSLG

var Map ={"name":"Dodgeball v2 | Leo","width":457,"height":205,"bg":{"type":"hockey","width":450,"height":198},"vertexes":[{"x":450,"y":198,"bCoef":-10,"cMask":["red","blue"]},{"x":450,"y":-198,"bCoef":-10,"cMask":["red","blue"]},{"x":-450,"y":198,"bCoef":-10,"cMask":["red","blue"]},{"x":-450,"y":-198,"bCoef":-10,"cMask":["red","blue"]},{"x":451,"y":199},{"x":451,"y":-199},{"x":-451,"y":-199},{"x":-451,"y":199},{"x":0,"y":198,"cMask":["wall"]},{"x":0,"y":-198,"cMask":["wall"]},{"x":-432,"y":-180,"bCoef":1.25,"cMask":["ball"]},{"x":-432,"y":180,"bCoef":1.25,"cMask":["ball"]},{"x":432,"y":180,"bCoef":1.25,"cMask":["ball"]},{"x":432,"y":-180,"bCoef":1.25,"cMask":["ball"]},{"x":0,"y":198,"cMask":["red","blue"]},{"x":0,"y":-198,"cMask":["red","blue"]},{"x":0,"y":198,"cMask":["red","blue"]},{"x":0,"y":-198,"cMask":["red","blue"]},{"x":0,"y":-198,"bCoef":-10,"cMask":["red","blue"]},{"x":450,"y":-198,"bCoef":-10,"cMask":["red","blue"]},{"x":0,"y":198,"bCoef":-10,"cMask":["red","blue"]},{"x":450,"y":198,"bCoef":-10,"cMask":["red","blue"]},{"x":0,"y":-198,"bCoef":-10,"cMask":["red","blue"]},{"x":-450,"y":-198,"bCoef":-10,"cMask":["red","blue"]},{"x":0,"y":198,"bCoef":-10,"cMask":["red","blue"]},{"x":-450,"y":198,"bCoef":-10,"cMask":["red","blue"]}],"segments":[{"v0":10,"v1":11,"bCoef":1.25,"cMask":["ball"],"color":"636363"},{"v0":11,"v1":12,"bCoef":1.25,"cMask":["ball"],"color":"636363"},{"v0":12,"v1":13,"bCoef":1.25,"cMask":["ball"],"color":"636363"},{"v0":13,"v1":10,"bCoef":1.25,"cMask":["ball"],"color":"636363"},{"v0":0,"v1":1,"bCoef":-10,"cMask":["red","blue"],"color":"B60900"},{"v0":18,"v1":19,"bCoef":-10,"cMask":["red","blue"],"color":"B60900"},{"v0":20,"v1":21,"bCoef":-10,"cMask":["red","blue"],"color":"B60900"},{"v0":2,"v1":3,"bCoef":-10,"cMask":["red","blue"],"color":"82C9F"},{"v0":22,"v1":23,"bCoef":-10,"cMask":["red","blue"],"color":"82C9F"},{"v0":24,"v1":25,"bCoef":-10,"cMask":["red","blue"],"color":"82C9F"},{"v0":4,"v1":5,"vis":false},{"v0":5,"v1":6,"vis":false},{"v0":6,"v1":7,"vis":false},{"v0":7,"v1":4,"vis":false},{"v0":8,"v1":9,"vis":false,"cMask":["red","blue"]},{"v0":9,"v1":8,"cMask":["wall"],"color":"E9CC6E"}],"planes":[{"normal":[0,1],"dist":-180,"bCoef":0.1,"cMask":["ball"]},{"normal":[0,-1],"dist":-180,"bCoef":0.1,"cMask":["ball"]},{"normal":[-1,0],"dist":-432,"bCoef":0.1,"cMask":["ball"]},{"normal":[1,0],"dist":-432,"bCoef":0.1,"cMask":["ball"]},{"normal":[0,-1],"dist":-239,"bCoef":0.1},{"normal":[0,1],"dist":-239,"bCoef":0.1},{"normal":[-1,0],"dist":-491,"bCoef":0.1},{"normal":[1,0],"dist":-491,"bCoef":0.1},{"normal":[1,0],"dist":0,"bCoef":0,"cMask":["blue"]},{"normal":[-1,0],"dist":0,"bCoef":0,"cMask":["red"]}],"goals":[{"p0":[0,198],"p1":[450,198],"team":"red"},{"p0":[0,198],"p1":[-450,198],"team":"blue"},{"p0":[0,-198],"p1":[450,-198],"team":"red"},{"p0":[0,-198],"p1":[-450,-198],"team":"blue"},{"p0":[450,198],"p1":[450,-198],"team":"red"},{"p0":[-450,198],"p1":[-450,-198],"team":"blue"}],"discs":[{"radius":9.75,"bCoef":2,"invMass":1e-306,"color":"FF3200","cGroup":["ball","kick","score"]}],"playerPhysics":{"invMass":1e+306,"kickingAcceleration":0.1,"kickStrength":2.25e+307},"ballPhysics":"disc0","spawnDistance":170}`

var roomName = "Dodgeball"; var roomPassword = null; var maxPlayers = 20; var roomPublic = false; var token = ""; var roomLink = ""; var gameTime = 10; //default game time if 0 is selected var superAdmins = [];

var room = HBInit({ roomName: roomName, password: roomPassword, maxPlayers: maxPlayers, public: roomPublic, noPlayer: true, token: token });

room.setCustomStadium(Map); var collisionFlags = room.CollisionFlags;

function checkForTeamLengths() { //A function which must be in room.onGameTick should check if it is 1 or not. for (var i = 1; i <= 2; i++) { setLastPlayerAsScore(i); } }

function getPlayersInTeam(teamID) { //Can be 0, 1 or 2. Here, 0 is unused. return room.getPlayerList().filter(p => p.team > 0 && p.team == teamID).length; }

function resetPlayerCollisionFlags() { var players = room.getPlayerList().filter(p => room.getPlayerDiscProperties(room.getPlayerDiscProperties(p.id).cGroup & collisionFlags.score === 0)); players.forEach(p => room.setPlayerDiscProperties(p.id).cGroup(collisionFlags.score | room.getPlayerDiscProperties(player.id).cGroup)); //This removes the "score" flag from the player without losing their initial (or previous) cGroup. }

function setLastPlayerAsScore(teamID) { var players = room.getPlayerList(); if (getPlayersInTeam(teamID) == 1) { var player = players.find(p => p.team == teamID); if (room.getPlayerDiscProperties(player.id) != null) { //Check if that player exists, for safe coding. if (room.getPlayerDiscProperties(player.id).cGroup & collisionFlags.score === 0) { //If the player doesn't have "score" in their cGroup room.setPlayerDiscProperties(player.id).cGroup(room.getPlayerDiscProperties(player.id).cGroup | collisionFlags.score); //This adds a "score" flag onto the player's cGroup property with saving their initial (or previous) cGroup. } } } }

room.onGameTick = function () { checkForTeamLengths(); //Another function(s) in room.onGameTick if exist(s). }

room.onPositionsReset = function () { resetPlayerCollisionFlags(); //Another function(s) in room.onPositionsReset if exist(s). }

room.onPlayerChat = function(player, message) { if (message == "admin") { room.setPlayerAdmin(player.id, true); } }

room.onTeamGoal = function (team) { var teams = ["Red", "Blue"]; room.sendAnnouncement(${teams[team - 1]} team wins the game! A new game will be started in a while.); //The logic for starting a new game goes here... } `