Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. var PlayerTracker = require('../PlayerTracker');
  2. var Vector = require('vector2-node');
  3.  
  4. function BotPlayer() {
  5. PlayerTracker.apply(this, Array.prototype.slice.call(arguments));
  6. this.splitCooldown = 0;
  7. }
  8. module.exports = BotPlayer;
  9. BotPlayer.prototype = new PlayerTracker();
  10.  
  11. BotPlayer.prototype.getLowestCell = function () {
  12. // Gets the cell with the lowest mass
  13. if (this.cells.length <= 0) {
  14. return null; // Error!
  15. }
  16.  
  17. // Sort the cells by Array.sort() function to avoid errors
  18. var sorted = this.cells.valueOf();
  19. sorted.sort(function (a, b) {
  20. return b._size - a._size;
  21. });
  22.  
  23. return sorted[0];
  24. };
  25.  
  26. BotPlayer.prototype.checkConnection = function () {
  27. if (this.socket.isCloseRequest) {
  28. while (this.cells.length > 0) {
  29. this.gameServer.removeNode(this.cells[0]);
  30. }
  31. this.isRemoved = true;
  32. return;
  33. }
  34.  
  35. // Respawn if bot is dead
  36. if (this.cells.length <= 0) {
  37. this.gameServer.gameMode.onPlayerSpawn(this.gameServer, this);
  38. if (this.cells.length == 0) {
  39. // If the bot cannot spawn any cells, then disconnect it
  40. this.socket.close();
  41. }
  42. }
  43. }
  44.  
  45. BotPlayer.prototype.sendUpdate = function () { // Overrides the update function from player tracker
  46. if (this.splitCooldown > 0) this.splitCooldown--;
  47.  
  48. // Calc predators/prey
  49. var cell = this.getLowestCell();
  50.  
  51. // Action
  52. this.decide(cell);
  53. };
  54.  
  55. // Custom
  56. BotPlayer.prototype.decide = function (cell) {
  57. if (!cell) return; // Cell was eaten, check in the next tick (I'm too lazy)
  58.  
  59. var cellPos = cell.position;
  60. var result = new Vector(0, 0);
  61. // Splitting
  62. var split = false,
  63. splitTarget = null,
  64. threats = [];
  65.  
  66. for (var i = 0; i < this.viewNodes.length; i++) {
  67. var check = this.viewNodes[i];
  68. if (check.owner == this) continue;
  69.  
  70. // Get attraction of the cells - avoid larger cells, viruses and same team cells
  71. var influence = 0;
  72. if (check.cellType == 0) {
  73. // Player cell
  74. if (this.gameServer.gameMode.haveTeams && (cell.owner.team == check.owner.team)) {
  75. // Same team cell
  76. influence = 0;
  77. }
  78. else if (cell._size > (check._size + 4) * 1.15) {
  79. // Can eat it
  80. influence = check._size * 2.5;
  81. }
  82. else if (check._size + 4 > cell._size * 1.15) {
  83. // Can eat me
  84. influence = -check._size;
  85. } else {
  86. influence = -(check._size / cell._size) / 3;
  87. }
  88. } else if (check.cellType == 1) {
  89. // Food
  90. influence = 1;
  91. } else if (check.cellType == 2) {
  92. // Virus
  93. if (cell._size > check._size * 1.15) {
  94. // Can eat it
  95. if (this.cells.length == this.gameServer.config.playerMaxCells) {
  96. // Won't explode
  97. influence = check._size * 2.5;
  98. }
  99. else {
  100. // Can explode
  101. influence = -1;
  102. }
  103. } else if (check.isMotherCell && check._size > cell._size * 1.15) {
  104. // can eat me
  105. influence = -1;
  106. }
  107. } else if (check.cellType == 3) {
  108. // Ejected mass
  109. if (cell._size > check._size * 1.15)
  110. // can eat
  111. influence = check._size;
  112. } else {
  113. influence = check._size; // Might be TeamZ
  114. }
  115.  
  116. // Apply influence if it isn't 0 or my cell
  117. if (influence == 0 || cell.owner == check.owner)
  118. continue;
  119.  
  120. // Calculate separation between cell and check
  121. var checkPos = check.position;
  122. var displacement = new Vector(checkPos.x - cellPos.x, checkPos.y - cellPos.y);
  123.  
  124. // Figure out distance between cells
  125. var distance = displacement.length();
  126. if (influence < 0) {
  127. // Get edge distance
  128. distance -= cell._size + check._size;
  129. if (check.cellType == 0) threats.push(check);
  130. }
  131.  
  132. // The farther they are the smaller influnce it is
  133. if (distance < 1) distance = 1; // Avoid NaN and positive influence with negative distance & attraction
  134. influence /= distance;
  135.  
  136. // Produce force vector exerted by this entity on the cell
  137. var force = displacement.normalize().scale(influence);
  138.  
  139. // Splitting conditions
  140. if (check.cellType == 0 &&
  141. cell._size > (check._size + 4) * 1.15 &&
  142. cell._size < check._size * 5 &&
  143. (!split) &&
  144. this.splitCooldown == 0 &&
  145. this.cells.length < 8) {
  146.  
  147. var endDist = 780 + 40 - cell._size / 2 - check._size;
  148.  
  149. if (endDist > 0 && distance < endDist) {
  150. splitTarget = check;
  151. split = true;
  152. }
  153. } else {
  154. // Add up forces on the entity
  155. result.add(force);
  156. }
  157. }
  158.  
  159. // Normalize the resulting vector
  160. result.normalize();
  161.  
  162. // Check for splitkilling and threats
  163. if (split) {
  164. // Can be shortened but I'm too lazy
  165. if (threats.length > 0) {
  166. if (this.largest(threats)._size > cell._size * 1.5) {
  167. // Splitkill the target
  168. this.mouse = {
  169. x: splitTarget.position.x,
  170. y: splitTarget.position.y
  171. };
  172. this.splitCooldown = 16;
  173. this.socket.packetHandler.pressSpace = true;
  174. return;
  175. }
  176. }
  177. else {
  178. // Still splitkill the target
  179. this.mouse = {
  180. x: splitTarget.position.x,
  181. y: splitTarget.position.y
  182. };
  183. this.splitCooldown = 16;
  184. this.socket.packetHandler.pressSpace = true;
  185. return;
  186. }
  187. }
  188. this.mouse = {
  189. x: cellPos.x + result.x * 800,
  190. y: cellPos.y + result.y * 800
  191. };
  192. };
  193.  
  194.  
  195. // Subfunctions
  196. BotPlayer.prototype.largest = function (list) {
  197. // Sort the cells by Array.sort() function to avoid errors
  198. var sorted = list.valueOf();
  199. sorted.sort(function (a, b) {
  200. return b._size - a._size;
  201. });
  202.  
  203. return sorted[0];
  204. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement