Advertisement
Guest User

Untitled

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