Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.17 KB | None | 0 0
  1. // ==UserScript==
  2. // @name botkeys
  3. // @version 0.1
  4. // @include http://*.jukejuice.com:*
  5. // @include http://*.newcompte.fr:*
  6. // @grant none
  7. // ==/UserScript==
  8.  
  9. MAP_VEL_PX = 100; // scale velocities by this to get pixels/second
  10.  
  11. function waitForId(fn) {
  12. // Don't execute the function until tagpro.playerId has been assigned.
  13. if (!tagpro || !tagpro.playerId) {
  14. return setTimeout(function() {
  15. waitForId(fn);
  16. }, 100);
  17. } else {
  18. // Only run the script if we are not spectating.
  19. if (!tagpro.spectator) {
  20. fn();
  21. }
  22. }
  23. }
  24.  
  25. function script() {
  26. var ENABLE = false;
  27. window.onkeydown = function(e) {ENABLE = (e && e.shiftKey);};
  28. window.onkeyup = function(e) {ENABLE = (e && e.shiftKey);};
  29.  
  30. var ctrl_period = 50;
  31. var counter = 0;
  32.  
  33. // Overriding this function to get a more accurate velocity of players.
  34. // Velocity is saved in player.vx and vy.
  35. Box2D.Dynamics.b2Body.prototype.GetLinearVelocity = function() {
  36. tagpro.players[this.player.id].vx = this.m_linearVelocity.x;
  37. tagpro.players[this.player.id].vy = this.m_linearVelocity.y;
  38. return this.m_linearVelocity;
  39. };
  40.  
  41. function create_controller(PIDconstants){
  42. var controller = {};
  43. controller.output = {};
  44. controller.output.threshold = 0.18;
  45. controller.output.set = function(u,me){
  46. this.horz(u.x,me);
  47. this.vert(u.y,me);
  48. };
  49.  
  50. controller.clear_botkeys = function(me) {
  51. me.botkeys.up = false;
  52. me.botkeys.down = false;
  53. me.botkeys.left = false;
  54. me.botkeys.right = false;
  55. }
  56.  
  57. controller.output.setkey = function(key, ispressed, me) {
  58. if (me[key] != ispressed) {
  59. tagpro.sendKeyPress(key, !ispressed);
  60. me[key] = ispressed;
  61. //me.botkeys[key] = ispressed;
  62. }
  63. }
  64.  
  65. controller.output.presskeys = function(val, keys, me) {
  66. if (val > this.threshold) {
  67. this.setkey(keys[0], true, me);
  68. this.setkey(keys[1], false, me);
  69. } else if (val < -this.threshold) {
  70. this.setkey(keys[1], true, me);
  71. this.setkey(keys[0], false, me);
  72. } else {
  73. this.setkey(keys[0], false, me);
  74. this.setkey(keys[1], false, me);
  75. }
  76. }
  77.  
  78. controller.output.horz = function(x,me){
  79. this.presskeys(x, ["right","left"], me);
  80. }
  81.  
  82. controller.output.vert = function(y,me){
  83. this.presskeys(y, ["up","down"], me);
  84. }
  85.  
  86. controller.execute = function(me,target){
  87. if (target) {
  88. var ctrl_vec = this.PID.get_ctrl_vec(me,target);
  89. this.output.set(ctrl_vec,me);
  90. }
  91. }
  92.  
  93. function create_PID(constants){
  94. var PID = {};
  95. PID.dims = {};
  96. PID.dims.list = ['x','y'];
  97. PID.dims.num = PID.dims.list.length;
  98. PID.constants = {};
  99. PID.constants.set_constants = function(constants){
  100. for (var key in constants){
  101. if (constants.hasOwnProperty(key)) {
  102. this[key] = constants[key];
  103. }
  104. }
  105. }
  106. PID.constants.set_constants(constants);
  107. PID.accum_error = {};
  108. PID.reset_accum_error = function(){
  109. PID.accum_error.pos = {'x':0,'y':0};
  110. PID.accum_error.vel = {'x':0,'y':0};
  111. };
  112. PID.reset_accum_error();
  113. PID.errors = {}; // gets set before its read
  114. PID.get_ctrl_vec = function(state_current, state_goal){
  115. this.errors = this.calc_error(state_current,state_goal);
  116. this.accumulate_error(this.errors);
  117. var u = {};
  118. for (var i = 0; i < this.dims.num; i++){
  119. var dim = this.dims.list[i];
  120. u[dim] = this.calc_P(dim) + this.calc_I(dim) + this.calc_D(dim);
  121. }
  122. return u;
  123. };
  124. PID.calc_P = function(axis){
  125. return this.constants.KP*this.errors.pos[axis];
  126. }
  127. PID.calc_I = function(axis){
  128. return this.constants.KI*this.accum_error.pos[axis];
  129. };
  130. PID.calc_D = function(axis){
  131. return this.constants.KD*this.errors.vel[axis];
  132. };
  133. PID.calc_error = function(state_current, state_goal){
  134. var errors = {};
  135. errors.pos = {};
  136. errors.vel = {};
  137. errors.pos.x = state_goal.x - state_current.x;
  138. errors.pos.y = state_current.y - state_goal.y;
  139. errors.vel.x = state_goal.vx - state_current.vx;
  140. errors.vel.y = state_current.vy - state_goal.vy;
  141. return errors;
  142. };
  143. PID.accumulate_error = function(errors){
  144. this.accum_error.pos.x += errors.pos.x;
  145. this.accum_error.pos.y += errors.pos.y;
  146. if (this.accum_error.pos.x > this.constants.KS) this.accum_error.pos.x = this.constants.KS;
  147. if (-this.accum_error.pos.x > this.constants.KS) this.accum_error.pos.x = -this.constants.KS;
  148. if (this.accum_error.pos.y > this.constants.KS) this.accum_error.pos.y = this.constants.KS;
  149. if (-this.accum_error.pos.y > this.constants.KS) this.accum_error.pos.y = -this.constants.KS;
  150. };
  151.  
  152. return PID;
  153. }
  154.  
  155. controller.PID = create_PID(PIDconstants);
  156. return controller;
  157. }
  158.  
  159. function create_decision_maker(me){
  160. var decision_maker = {};
  161. decision_maker.me = me;
  162.  
  163. decision_maker.find_player = function(criterion_fn){
  164. for (var key in tagpro.players) {
  165. if (tagpro.players.hasOwnProperty(key)) {
  166. // criterian_fn must take decision_maker and a player as arguments
  167. if (criterion_fn(this,tagpro.players[key])) {
  168. return tagpro.players[key];
  169. }
  170. }
  171. }
  172. return null;
  173. }
  174.  
  175. decision_maker.create_target = function(){
  176. var target = {};
  177. var opponent = this.find_player(function(dm,player){return (player.flag === me.team) || (player.flag === 3);});
  178.  
  179. if (opponent) {
  180. target = opponent;
  181. var p_thres = 30;
  182. var v_thres = 30;
  183. if (Math.abs(opponent.x - me.x) < p_thres && Math.abs(opponent.vx - me.vx) < v_thres) {
  184. target.vy = Math.sign(opponent.y - me.y) * 1000;
  185. }
  186. if (Math.abs(opponent.y - me.y) < p_thres && Math.abs(opponent.vy - me.vy) < v_thres) {
  187. target.vx = Math.sign(opponent.x - me.x) * 1000;
  188. }
  189. }
  190. else {
  191. target = me;
  192. }
  193.  
  194. return target;
  195. }
  196.  
  197. return decision_maker;
  198. }
  199.  
  200. function main() {
  201. var me = tagpro.players[tagpro.playerId];
  202. me.botkeys = {up: false, down: false, left: false, right: false};
  203. PID_constants = {};
  204. PID_constants.KP = 0.018;
  205. PID_constants.KI = 0.011;
  206. PID_constants.KD = 3.5;
  207. PID_constants.KS = 11;
  208.  
  209. var controller = create_controller(PID_constants);
  210. var decision_maker = create_decision_maker(me);
  211.  
  212. setInterval(function() {
  213. if (ENABLE) {
  214. // determine a target state
  215. var target = decision_maker.create_target();
  216. // execute the control to get to the target
  217. controller.execute(me,target);
  218. }
  219. else {
  220. controller.clear_botkeys(me);
  221. }
  222. }, ctrl_period);
  223. }
  224.  
  225. main();
  226. }
  227.  
  228. function startFunction() {
  229. var keys = {};
  230.  
  231. function addArrow(s,p) {
  232. var a = new PIXI.Graphics();
  233. s.addChild(a);
  234. a.beginFill(0x000000);
  235. a.lineStyle(1, 0x000000);
  236. a.drawCircle(p[0]*10 + 20, p[1]*10 + 20, 3);
  237. return a;
  238. }
  239.  
  240. function addArrows(id) {
  241. var s = tagpro.players[id].sprite;
  242. var k = {};
  243. k.down = addArrow(s, [0,1]);
  244. k.right = addArrow(s, [1,0]);
  245. k.up = addArrow(s, [0,-1]);
  246. k.left = addArrow(s, [-1,0]);
  247. keys[id] = k;
  248. }
  249.  
  250. addArrows(tagpro.playerId);
  251.  
  252. (function loop() {
  253. requestAnimationFrame(loop);
  254. update();
  255. })();
  256.  
  257. function update() {
  258. var id = tagpro.playerId;
  259. var p = tagpro.players[id];
  260. var dirs = ["up","down","left","right"];
  261. for (var i = 0; i < dirs.length; i++) {
  262. var d = dirs[i];
  263. keys[id][d].visible = p.botkeys[d];
  264. }
  265. }
  266. }
  267.  
  268. tagpro.ready(function() {
  269. waitForId(script);
  270. setTimeout(startFunction,1000);
  271. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement