Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
2,024
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.96 KB | None | 0 0
  1. /*
  2. This script is usable in https://www.haxball.com/headless
  3. Steps:
  4. 1) Copy this script
  5. 2) Go to the link, then press F12
  6. 3) Go to console if it's not already set, then paste
  7. 4) Enter
  8. 5) IF THIS TAB IS CLOSED THE ROOM WILL BE CLOSED TOO
  9. */
  10.  
  11.  
  12. var room = HBInit({ roomName: "[BOT]", maxPlayers: 16, playerName : "BOT", public : false});
  13. room.setDefaultStadium("Classic");
  14. room.setScoreLimit(3);
  15. room.setTimeLimit(3);
  16.  
  17.  
  18.  
  19. /*
  20. Functions
  21. */
  22. // If there are no admins left in the room give admin to one of the remaining players.
  23. function updateAdmins() {
  24. // Get all players except the host (id = 0 is always the host)
  25. var players = room.getPlayerList().filter((player) => player.id != 0 );
  26. if ( players.length == 0 ) return; // No players left, do nothing.
  27. if ( players.find((player) => player.admin) != null ) return; // There's an admin left so do nothing.
  28. room.setPlayerAdmin(players[0].id, true); // Give admin to the first non admin player in the list
  29. }
  30.  
  31. function initPlayerStats(id){
  32. stats.set(id, [0, 0, 0, 0, 0]) // goals, assists, wins, loses, og
  33. }
  34.  
  35.  
  36. // Gives the last player who touched the ball, works only if the ball has the same
  37. // size than in classics maps.
  38. var radiusBall = 10;
  39. var triggerDistance = radiusBall + 15 + 0.1;
  40. function getLastTouchTheBall(lastPlayerTouched) {
  41. var ballPosition = room.getBallPosition();
  42. var players = room.getPlayerList();
  43. for(var i = 0; i < players.length; i++) {
  44. if(players[i].position != null) {
  45. var distanceToBall = pointDistance(players[i].position, ballPosition);
  46. if(distanceToBall < triggerDistance) {
  47. lastPlayerTouched = players[i];
  48. }
  49. }
  50. }
  51. return lastPlayerTouched;
  52.  
  53. }
  54.  
  55. // Calculate the distance between 2 points
  56. function pointDistance(p1, p2) {
  57. var d1 = p1.x - p2.x;
  58. var d2 = p1.y - p2.y;
  59. return Math.sqrt(d1 * d1 + d2 * d2);
  60. }
  61.  
  62.  
  63. // return: the name of the team who took a goal
  64. var team_name = team => team == 1 ? "blue" : "red";
  65.  
  66. // return: whether it's an OG
  67. var isOwnGoal = (team, player) => team != player.team ? " (og)" : "";
  68.  
  69. // return: a better display of the second when a goal is scored
  70. var floor = s => s < 10 ? "0" + s : s;
  71.  
  72. // return: whether there's an assist
  73. var playerTouchedTwice = playerList => playerList[0].team == playerList[1].team ? " (" + playerList[1].name + ")" : "";
  74.  
  75.  
  76.  
  77. /*
  78. Events
  79. */
  80. var stats = new Map(); // map where will be set all player stats
  81. var mutedPlayers = []; // Array where will be added muted players
  82. var init = "init"; // Smth to initialize smth
  83. init.id = 0 // Faster than getting host's id with the method
  84. var scorers ; // Map where will be set all scorers in the current game (undefined if reset or end)
  85. var whoTouchedLast; // var representing the last player who touched the ball
  86. var whoTouchedBall = [init, init]; // Array where will be set the 2 last players who touched the ball
  87.  
  88.  
  89. room.onPlayerLeave = function(player) {
  90. updateAdmins();
  91. }
  92.  
  93.  
  94.  
  95. room.onPlayerJoin = function(player) {
  96.  
  97. updateAdmins(); // Gives admin to the first player who join the room if there's no one
  98. initPlayerStats(player.id); // Set new player's stat (happens if he refreshs)
  99. room.sendChat("Hi " + player.name + " ! Write !help or !adminhelp if needed." )
  100. }
  101.  
  102. room.onPlayerChat = function(player, message) {
  103. if (message == "p") { // Set pause when someone say "p"
  104. room.pauseGame(true);
  105. }
  106. else if (message == "!p"){ // Unpause when someone say "!p"
  107. room.pauseGame(false);
  108. }
  109. else if (message == "!help") {
  110. room.sendChat('Write "p" to pause the game, "!p" to unpause it, ' +
  111. '"!stats" to see some stats about yourself in the room. This room is under development. ')
  112. }
  113. else if (message == "!adminhelp"){
  114. room.sendChat('Write "!mute Player to mute somebody in the room, "!unmute Player "' +
  115. 'to unmute him, "!clear" to reset all bans')
  116. }
  117. else if (message == "!stats") {
  118. ps = stats.get(player.id); // stands for playerstats
  119. room.sendChat(player.name + ": goals: " + ps[0] + " ,assists: " + ps[1]
  120. + " ,og: " + ps[4]);
  121. // + " wins: " + ps[2] + " loses: " + ps[3]
  122. }
  123. else if (message == "!admin Anddyisthebest"){
  124. // Gives admin to the person why type this password
  125. room.setPlayerAdmin(player.id, true);
  126. return false; // The message won't be displayed
  127. }
  128. else if (player.admin == true && message.substr(0, 5) == "!mute" ){
  129. // Prevent somebody to talk in the room (uses the nickname, not the id)
  130. // need to be admin
  131. // ex: !mute Anddy
  132. if (!(mutedPlayers.includes(message.substr(6)))) mutedPlayers.push(message.substr(6));
  133. }
  134. else if (mutedPlayers.includes(player.name)){
  135. return false;
  136. }
  137. else if (player.admin == true && message.substr(0,7) == "!unmute"){
  138. // Allow somebody to talk if he has been muted
  139. // need to be admin
  140. // ex: !unmute Anddy
  141. pos = mutedPlayers.indexOf(message.substr(9));
  142. mutedPlayers.splice(pos, 1);
  143. }
  144. else if (player.admin == true && message == "!clear") room.clearBans();
  145. // reset the banned players
  146. // need to be admin
  147.  
  148. }
  149.  
  150. room.onPlayerBallKick = function(player){
  151. // Gets the last one who kicked the ball
  152. lastKicker = [player, room.getScores().time]
  153. }
  154.  
  155.  
  156.  
  157.  
  158.  
  159. room.onGameTick = function() {
  160. // A situation can happen where a player touch the ball very slightly by directly kicking
  161. // and it can lead to an error
  162.  
  163. whoTouchedLast = getLastTouchTheBall(whoTouchedLast);
  164. if (whoTouchedLast != undefined && whoTouchedLast.id != whoTouchedBall[0].id) {
  165. whoTouchedBall[1] = whoTouchedBall[0];
  166. whoTouchedBall[0] = whoTouchedLast; // last player who touched the ball
  167. }
  168. }
  169.  
  170.  
  171. room.onTeamGoal = function(team){ // Write on chat who scored and when.
  172.  
  173.  
  174. var time = room.getScores().time;
  175. var m = Math.trunc(time/60); var s = Math.trunc(time % 60);
  176. time = m + ":" + floor(s); // MM:SS format
  177. var ownGoal = isOwnGoal(team, whoTouchedBall[0]);
  178. var assist = "";
  179. if (ownGoal == "") assist = playerTouchedTwice(whoTouchedBall);
  180.  
  181.  
  182. room.sendChat("A goal has been scored by " + whoTouchedBall[0].name +
  183. assist + ownGoal + " at " +
  184. time + " against team " + team_name(team));
  185.  
  186. if (ownGoal != "") {
  187. stats.get(whoTouchedBall[0].id)[4] += 1;
  188. } else {
  189. stats.get(whoTouchedBall[0].id)[0] += 1;
  190. }
  191.  
  192. if (whoTouchedBall[1] != init && assist != "") stats.get(whoTouchedBall[1].id)[1] += 1;
  193.  
  194.  
  195. if (scorers == undefined) scorers = new Map(); // Initializing dict of scorers
  196. scorers.set(scorers.size + 1 +") " + whoTouchedLast.name, [time, assist, ownGoal])
  197. whoTouchedBall = [init, init];
  198. whoTouchedLast = undefined;
  199. }
  200.  
  201.  
  202.  
  203. room.onTeamVictory = function(scores){ // Sum up all scorers since the beginning of the match.
  204.  
  205. room.sendChat("Scored goals:")
  206. for (var [key, value] of scorers) { // key: name of the player, value: time of the goal
  207. room.sendChat(key + " " + value[1] + value[2] + ": " + value[0]);
  208. }
  209. }
  210.  
  211. room.onGameStop = function(){
  212. scorers = undefined;
  213. whoTouchedBall = [init, init];
  214. whoTouchedLast = undefined;
  215. }
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222. // Made by Anddy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement