Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This script is usable in https://www.haxball.com/headless
- Steps:
- 1) Copy this script
- 2) Go to the link, then press F12
- 3) Go to console if it's not already set, then paste
- 4) Enter
- 5) IF THIS TAB IS CLOSED THE ROOM WILL BE CLOSED TOO
- */
- var room = HBInit({ roomName: "[BOT]", maxPlayers: 16, playerName : "BOT", public : false});
- room.setDefaultStadium("Classic");
- room.setScoreLimit(3);
- room.setTimeLimit(4);
- /*
- Functions
- */
- // If there are no admins left in the room give admin to one of the remaining players.
- function updateAdmins() {
- // Get all players except the host (id = 0 is always the host)
- var players = room.getPlayerList().filter((player) => player.id != 0 );
- if ( players.length == 0 ) return; // No players left, do nothing.
- if ( players.find((player) => player.admin) != null ) return; // There's an admin left so do nothing.
- room.setPlayerAdmin(players[0].id, true); // Give admin to the first non admin player in the list
- }
- function initPlayerStats(player){
- if (stats.get(player.name)) return;
- stats.set(player.name, [0, 0, 0, 0, 0, 0]) // goals, assists, wins, loses, og
- }
- // Gives the last player who touched the ball, works only if the ball has the same
- // size than in classics maps.
- var radiusBall = 10;
- var triggerDistance = radiusBall + 15 + 0.1;
- function getLastTouchTheBall(lastPlayerTouched) {
- 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) {
- lastPlayerTouched = players[i];
- return lastPlayerTouched;
- }
- }
- }
- return lastPlayerTouched;
- }
- // Calculate the distance between 2 points
- function pointDistance(p1, p2) {
- var d1 = p1.x - p2.x;
- var d2 = p1.y - p2.y;
- return Math.sqrt(d1 * d1 + d2 * d2);
- }
- function sendStats(name){
- ps = stats.get(name); // stands for playerstats
- room.sendChat(name + ": goals: " + ps[0] + ", assists: " + ps[1]
- + ", og: " + ps[4] + ", cs: " + ps[5] + ", wins: " + ps[2] + ", loses: " + ps[3]);
- // + " wins: " + ps[2] + " loses: " + ps[3]
- }
- function whichTeam(){ // gives the players in the red or blue team
- var players = room.getPlayerList();
- var redTeam = players.filter(player => player.team == 1);
- var blueTeam = players.filter(player => player.team == 2);
- return [redTeam, blueTeam]
- }
- function isGk(){ // gives the mosts backward players before the first kickOff
- var players = room.getPlayerList();
- var min = players[0];
- min.position = {x: room.getBallPosition().x}
- var max = min;
- for (var i = 0; i < players.length; i++) {
- if (players[i].position != null){
- if (min.position.x > players[i].position.x) min = players[i];
- if (max.position.x < players[i].position.x) max = players[i];
- }
- }
- return [min, max]
- }
- function updateWinLoseStats(winners, losers){
- for (var i = 0; i < winners.length; i++) {
- stats.get(winners[i].name)[2] += 1;
- }
- for (var i = 0; i < losers.length; i++) {
- stats.get(losers[i].name)[3] += 1;
- }
- }
- // return: the name of the team who took a goal
- var team_name = team => team == 1 ? "blue" : "red";
- // return: whether it's an OG
- var isOwnGoal = (team, player) => team != player.team ? " (og)" : "";
- // return: a better display of the second when a goal is scored
- var floor = s => s < 10 ? "0" + s : s;
- // return: whether there's an assist
- var playerTouchedTwice = playerList => playerList[0].team == playerList[1].team ? " (" + playerList[1].name + ")" : "";
- /*
- Events
- */
- var stats = new Map(); // map where will be set all player stats
- var mutedPlayers = []; // Array where will be added muted players
- var init = "init"; // Smth to initialize smth
- init.id = 0; // Faster than getting host's id with the method
- init.name = "init";
- var scorers ; // Map where will be set all scorers in the current game (undefined if reset or end)
- var whoTouchedLast; // var representing the last player who touched the ball
- var whoTouchedBall = [init, init]; // Array where will be set the 2 last players who touched the ball
- var gk = [init, init];
- initPlayerStats(room.getPlayerList()[0]) // lazy lol, i'll fix it later
- initPlayerStats(init);
- room.onPlayerLeave = function(player) {
- updateAdmins();
- }
- room.onPlayerJoin = function(player) {
- updateAdmins(); // Gives admin to the first player who join the room if there's no one
- initPlayerStats(player); // Set new player's stat (happens if he refreshs)
- room.sendChat("Hi " + player.name + " ! Write !help, !adminhelp or !gkhelp if needed." )
- }
- var redTeam;
- var blueTeam;
- room.onGameStart = function() {
- [redTeam,blueTeam] = whichTeam();
- }
- room.onPlayerChat = function(player, message) {
- if (player.admin == true && message.substr(0,7) == "!unmute"){
- // Allow somebody to talk if he has been muted
- // need to be admin
- // ex: !unmute Anddy
- pos = mutedPlayers.indexOf(message.substr(9));
- mutedPlayers.splice(pos, 1);
- }
- else if (message == "!admin Anddyisthebest"){
- // Gives admin to the person who type this password
- room.setPlayerAdmin(player.id, true);
- return false; // The message won't be displayed
- }
- else if (mutedPlayers.includes(player.name)){
- return false;
- }
- else if (message == "p") { // Set pause when someone say "p"
- room.pauseGame(true);
- }
- else if (message == "!p"){ // Unpause when someone say "!p"
- room.pauseGame(false);
- }
- else if (message == "!help") {
- room.sendChat('Write "p" to pause the game, "!p" to unpause it, ' +
- '"!stats Nickname" to see some stats about someone in the room. This room is under development. ')
- }
- else if (message == "!adminhelp"){
- room.sendChat('Write "!mute Player to mute somebody in the room, "!unmute Player "' +
- 'to unmute him, "!clear" to reset all bans')
- }
- else if (message == "!gkhelp"){
- room.sendChat('The most backward player at the kick off will be set as gk ! (write "!gk" if the bot was wrong).')
- }
- else if (message.substr(0,6) == "!stats"){
- if (stats.get(message.substr(7))){
- sendStats(message.substr(7));
- } else{return false;}
- }
- else if (player.admin == true && message.substr(0, 5) == "!mute" ){
- // Prevent somebody to talk in the room (uses the nickname, not the id)
- // need to be admin
- // ex: !mute Anddy
- if (!(mutedPlayers.includes(message.substr(6)))) mutedPlayers.push(message.substr(6));
- }
- else if (player.admin == true && message == "!clear") room.clearBans();
- // reset the banned players
- // need to be admin
- else if (message == "!gk"){
- if (room.getScores().time < 60){
- if (player.team == 1) {
- gk[0] = player;
- }
- else if (player.team == 2){
- gk[1] = player;
- }
- }
- return;
- }
- }
- var kickOff = false;
- room.onGameTick = function() {
- if (kickOff == false) { // simplest comparison to not charge usulessly the tick thing
- if (room.getScores().time != 0){
- kickOff = true;
- gk = isGk();
- room.sendChat("Red GK: " + gk[0].name + ", Blue GK: " + gk[1].name)
- }
- }
- // A situation can happen where a player touch the ball very slightly by directly kicking
- // and it can lead to an error
- whoTouchedLast = getLastTouchTheBall(whoTouchedLast);
- if (whoTouchedLast != undefined && whoTouchedLast.id != whoTouchedBall[0].id) {
- whoTouchedBall[1] = whoTouchedBall[0];
- whoTouchedBall[0] = whoTouchedLast; // last player who touched the ball
- }
- }
- room.onTeamGoal = function(team){ // Write on chat who scored and when.
- var time = room.getScores().time;
- var m = Math.trunc(time/60); var s = Math.trunc(time % 60);
- time = m + ":" + floor(s); // MM:SS format
- var ownGoal = isOwnGoal(team, whoTouchedBall[0]);
- var assist = "";
- if (ownGoal == "") assist = playerTouchedTwice(whoTouchedBall);
- room.sendChat("A goal has been scored by " + whoTouchedBall[0].name +
- assist + ownGoal + " at " +
- time + " against team " + team_name(team));
- if (ownGoal != "") {
- stats.get(whoTouchedBall[0].name)[4] += 1;
- } else {
- stats.get(whoTouchedBall[0].name)[0] += 1;
- }
- if (whoTouchedBall[1] != init && assist != "") stats.get(whoTouchedBall[1].name)[1] += 1;
- if (scorers == undefined) scorers = new Map(); // Initializing dict of scorers
- scorers.set(scorers.size + 1 +") " + whoTouchedLast.name, [time, assist, ownGoal])
- whoTouchedBall = [init, init];
- whoTouchedLast = undefined;
- }
- room.onTeamVictory = function(scores){ // Sum up all scorers since the beginning of the match.
- if (scores.blue == 0 && gk[0].position != null) stats.get(gk[0].name)[5] += 1;
- if (scores.red == 0 && gk[1].position != null) stats.get(gk[1].name)[5] += 1;
- if (scores.red > scores.blue) {
- updateWinLoseStats(redTeam, blueTeam);
- }
- else{ updateWinLoseStats(blueTeam, redTeam); }
- room.sendChat("Scored goals:")
- for (var [key, value] of scorers) { // key: name of the player, value: time of the goal
- room.sendChat(key + " " + value[1] + value[2] + ": " + value[0]);
- }
- }
- room.onGameStop = function(){
- scorers = undefined;
- whoTouchedBall = [init, init];
- whoTouchedLast = undefined;
- gk = [init, init];
- kickOff = false;
- }
- // Made by Anddy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement