Advertisement
Guest User

Untitled

a guest
May 12th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.55 KB | None | 0 0
  1. var mineflayer = require('mineflayer');
  2. var Vec3 = require('vec3').Vec3;
  3.  
  4. var bot = mineflayer.createBot({
  5. host: process.argv[2],
  6. port: parseInt(process.argv[3]),
  7. username: process.argv[4] ? process.argv[4] : "echo",
  8. password: process.argv[5],
  9. });
  10.  
  11. // 改変に伴い必要な変数のみ以下に抽出
  12. var player_of_interest = "noone";
  13. var dst_player_of_interest = 9999;
  14. var chasing_user = "noone";
  15. var last_interact_time = new Date();
  16. var users = {};
  17. var command_timer = 0;
  18.  
  19. // テンプレ (起動用)
  20. bot.on('message', function(username, message) {
  21. if (username === bot.username) return;
  22. });
  23.  
  24. bot.on('connect', function() {
  25. console.log("connected");
  26. bot.last_pos = false;
  27. });
  28.  
  29. bot.on('death', function() {
  30. console.log("dead");
  31. bot.clearControlStates();
  32. });
  33.  
  34. // Health Recovery (ヘルス一定以下で手持ちの食料を食べる)
  35. bot.on('food', function() {
  36. iconsole.log("health = " + bot.health.toString() +
  37. ", food = " + bot.food.toString());
  38. if(bot.health <=16 && bot.food < 19)
  39. {
  40. eat.start_eat(bot);
  41. }
  42. else {
  43. eat.end_eat(bot);
  44. }
  45. });
  46.  
  47. // look at neaby player (最寄りのエンティティを注視)
  48. bot.on('entityMoved', function(entity) {
  49. if(entity.type === "mob")
  50. {
  51. //fight
  52. var dst = (bot.entity.position.distanceTo(entity.position));
  53. if (entity === bot.fighting_enemy){
  54. bot.dst_fighting_enemy = dst;
  55. if (dst >= 10){
  56. bot.fighting_enemy = false;
  57. }
  58. }
  59. if(dst < 10){
  60. console.log(entity);
  61. }
  62. if(entity.mobType == "Zombie")
  63. {
  64. bot.fighting_enemy = entity;
  65. //jump
  66. var mypos = new Vec3(bot.entity.position.x,
  67. bot.entity.position.y,
  68. bot.entity.position.z
  69. );
  70. var look_point = mypos.plus(
  71. pyr2vec(bot.entity.pitch, bot.entity.yaw, 0.8)
  72. );
  73. var dp = (mypos.subtract(entity.position));
  74. var pyr = vec2pitch_yaw_radus(dp);
  75. if (Math.abs(pyr.yaw - bot.entity.yaw) > 0.05 ||
  76. Math.abs(pyr.pitch - bot.entity.pitch) > 0.05){
  77. //console.log("dyaw = " + Math.abs(pyr.yaw - bot.entity.yaw));
  78. //console.log("yaw = " + pyr.yaw);
  79. //console.log("pitch = " + pyr.pitch);
  80. //bot.look(pyr.yaw, pyr.pitch, false, false);
  81. bot.look(pyr.yaw, 0, false, false);
  82. }
  83. //console.log("look = " + pyr2vec(bot.entity.pitch, bot.entity.yaw, 0.8));
  84. var block_in = bot.blockAt(mypos);
  85. var block_front = bot.blockAt(look_point);
  86. var block_front_up = bot.blockAt(look_point.add(new Vec3 (0, 1, 0)));
  87. if (dst < 3)
  88. {
  89. //bot.setControlState("swing", false);
  90. if(bot.last_pos && checkRaechable(bot.last_pos, entity))
  91. {
  92. bot.attack(entity);
  93. //bot.setControlState("swing", true);
  94. }
  95. bot.setControlState("forward", false);
  96. bot.setControlState("back", true);
  97. }
  98. else
  99. {
  100. //bot.setControlState("swing", false);
  101. if (dst < 10){
  102. bot.setControlState("forward", true);
  103. bot.setControlState("back", false);
  104. }
  105. else{
  106. bot.setControlState("forward", false);
  107. bot.setControlState("back", false);
  108. //bot.setControlState("swing", false);
  109. }
  110. }
  111. }
  112. }
  113. else if(entity.type === "player"){
  114. users[entity.username] = entity;
  115. //console.log("entity moved");
  116. //console.log(entity);
  117. dst = (bot.entity.position.distanceTo(entity.position));
  118. //console.log("d = " + dst);
  119. if(player_of_interest === entity.username && dst > 2){
  120. player_of_interst = "noone";
  121. dst_player_of_interest = 999;
  122. }
  123.  
  124. //being pushed
  125. if(dst < 0.8){
  126. var mypos = new Vec3(bot.entity.position.x,
  127. 0,
  128. bot.entity.position.z
  129. );
  130. var yourpos = new Vec3(entity.position.x,
  131. 0,
  132. entity.position.z
  133. );
  134. mypos.subtract(yourpos);
  135. mypos = mypos.scaled(60);
  136. //console.log("pushed");
  137. //console.log(mypos);
  138. bot.entity.velocity.add(mypos);
  139. }
  140.  
  141. if(dst < 2){
  142. var do_interact = false;
  143. if(player_of_interest === entity.username){
  144. dst_player_of_interest = dst;
  145. do_interact = true;
  146. }
  147. else if(dst < dst_player_of_interest){
  148. player_of_interest = entity.username;
  149. dst_player_of_interest = dst;
  150. do_interact = true;
  151. }
  152.  
  153. //if(false){
  154. if(chasing_user ==="noone" && do_interact == true){
  155. //avoid too frequent motion
  156. var now = new Date();
  157. var dt = now - last_interact_time;
  158. //console.log("dt = " + dt);
  159. if(dt > 200){
  160. var mypos = new Vec3(bot.entity.position.x,
  161. bot.entity.position.y,
  162. bot.entity.position.z
  163. );
  164. dp = (mypos.subtract(entity.position));
  165. pyr = vec2pitch_yaw_radus(dp);
  166. if (Math.abs(pyr.yaw - bot.entity.yaw) > 0.05 ||
  167. Math.abs(pyr.pitch - bot.entity.pitch) > 0.05){
  168. //console.log("dyaw = " + Math.abs(pyr.yaw - bot.entity.yaw));
  169. bot.look(pyr.yaw, pyr.pitch, false, false);
  170. //bot.look(pyr.yaw, 0, false, false);
  171. }
  172. //bot.lookAt(entity.position, false, false);
  173. //mimic sneaking
  174. if (entity.metadata["0"] === 2){
  175. bot.setControlState("sneak", true);
  176. if(command_timer){
  177. clearTimeout(command_timer);
  178. }
  179. command_timer = setTimeout(function(){
  180. bot.clearControlStates();
  181. }, 500);
  182. }
  183. else{
  184. //bot.setControlState("sneak", false);
  185. }
  186. last_interact_time = now;
  187. }
  188. }
  189. }
  190. }
  191. });
  192.  
  193. function once_three_seconds(){
  194. var entity = null;
  195. //console.log("chasing_user = " + chasing_user);
  196. //console.log(users);
  197.  
  198. //chase player
  199. if( entity = users[chasing_user]){
  200. //console.log("chasing_user found");
  201. var mypos = new Vec3(bot.entity.position.x,
  202. bot.entity.position.y,
  203. bot.entity.position.z
  204. );
  205. var dp = (mypos.subtract(entity.position));
  206. var pyr = vec2pitch_yaw_radus(dp);
  207. if (Math.abs(pyr.yaw - bot.entity.yaw) > 0.05 ||
  208. Math.abs(pyr.pitch - bot.entity.pitch) > 0.05){
  209. //console.log("dyaw = " + Math.abs(pyr.yaw - bot.entity.yaw));
  210. //console.log("yaw = " + pyr.yaw);
  211. //console.log("pitch = " + pyr.pitch);
  212. //bot.look(pyr.yaw, pyr.pitch, false, false);
  213. bot.look(pyr.yaw, 0, false, false);
  214. }
  215.  
  216. var dst = (bot.entity.position.distanceTo(entity.position));
  217. if(dst >= 2.0){
  218. //bot.setControlState("swing", false);
  219. bot.setControlState("forward", true);
  220. bot.setControlState("back", false);
  221.  
  222. //jump
  223. var mypos = new Vec3(bot.entity.position.x,
  224. bot.entity.position.y,
  225. bot.entity.position.z
  226. );
  227. var look_point = mypos.plus(
  228. pyr2vec(bot.entity.pitch, bot.entity.yaw, 0.8)
  229. );
  230. //console.log("look = " + pyr2vec(bot.entity.pitch, bot.entity.yaw, 0.8));
  231. var block_in = bot.blockAt(mypos);
  232. var block_front = bot.blockAt(look_point);
  233. var block_front_up = bot.blockAt(look_point.add(new Vec3 (0, 1, 0)));
  234. //console.log(block);
  235. if((block_front && block_front.boundingBox === "block" ||
  236. block_front && block_front.boundingBox === "glass_pane" ||
  237. block_front && block_front.boundingBox === "cauldon" ) &&
  238. block_front_up && block_front_up.boundingBox === "empty"){ //climbing
  239. bot.setControlState("jump", true);
  240. }
  241. else if(block_in && block_in.boundingBox === "cauldon") // go out of cauldons
  242. {
  243. bot.setControlState("jump", true);
  244. }
  245. else{
  246. bot.setControlState("jump", false);
  247. }
  248. }
  249. else{
  250. bot.clearControlStates();
  251. //bot.setControlState("swing", true);
  252. }
  253.  
  254. //bot.lookAt(entity.position, false, false);
  255. //mimic sneaking
  256. if (entity.metadata["0"] === 2){
  257. bot.setControlState("sneak", true);
  258. if(command_timer){
  259. clearTimeout(command_timer);
  260. }
  261. command_timer = setTimeout(function(){
  262. bot.clearControlStates();
  263. }, 500);
  264. }
  265. else{
  266. //bot.setControlState("sneak", false);
  267. }
  268. }
  269. if(chasing_user != "noone")
  270. setTimeout(once_three_seconds, 100, []);
  271. }
  272.  
  273. function vec2pitch_yaw_radus(v){
  274. if(v.x != 0.0){
  275. yaw = Math.atan2(v.x, v.z);
  276. }
  277. else{
  278. yaw = v.z >= 0 ? Math.PI / 2: -Math.PI / 2;
  279. }
  280. const groundDistance = Math.sqrt(v.x * v.x + v.z * v.z)
  281. pitch = Math.atan2(-v.y, groundDistance)
  282. radius = v.distanceTo(new Vec3(0, 0, 0))
  283. return {"pitch": pitch, "yaw": yaw, "radius": radius};
  284. }
  285.  
  286. bot.on('entitySwingArm', function(entity) {
  287. if(entity.type === "player"){
  288. //console.log("entity swinged arm");
  289. //console.log(entity);
  290. dst = (bot.entity.position.distanceTo(entity.position));
  291. //if(0){
  292. if(dst < 4){
  293. //calcurate the attack hit or not
  294. var mypos = new Vec3(bot.entity.position.x,
  295. bot.entity.position.y,
  296. bot.entity.position.z
  297. );
  298. var yourpos = new Vec3(entity.position.x,
  299. entity.position.y,
  300. entity.position.z
  301. );
  302. var lookat = pyr2vec(entity.pitch, entity.yaw, dst);
  303. var d = mypos.distanceTo(lookat.add(yourpos));
  304. if (d < 0.25){
  305. //console.log("hit");
  306. if(chasing_user === entity.username){
  307. chasing_user = "noone";
  308. bot.clearControlStates();
  309. }
  310. else{
  311. chasing_user = entity.username;
  312. setTimeout(once_three_seconds, 200, []);
  313. bot.clearControlStates();
  314. }
  315. }
  316. }
  317. }
  318. });
  319.  
  320. function pyr2vec(p, y, r){
  321. return new Vec3(
  322. -r * Math.cos(p) * Math.sin(y),
  323. r * Math.sin(p),
  324. -r * Math.cos(p) * Math.cos(y)
  325. );
  326. }
  327.  
  328. function vec2pitch_yaw_radus(v){
  329. if(v.x != 0.0){
  330. yaw = Math.atan2(v.x, v.z);
  331. }
  332. else{
  333. yaw = v.z >= 0 ? Math.PI / 2: -Math.PI / 2;
  334. }
  335. const groundDistance = Math.sqrt(v.x * v.x + v.z * v.z)
  336. pitch = Math.atan2(-v.y, groundDistance)
  337. radius = v.distanceTo(new Vec3(0, 0, 0))
  338. return {"pitch": pitch, "yaw": yaw, "radius": radius};
  339. }
  340.  
  341. function checkRaechable(me, you)
  342. {
  343. var mypos = new Vec3(me.position.x,
  344. me.position.y,
  345. me.position.z
  346. );
  347. var yourpos = new Vec3(you.position.x,
  348. you.position.y,
  349. you.position.z
  350. );
  351. var dst = (mypos.distanceTo(yourpos));
  352. var lookat = pyr2vec(me.pitch, me.yaw, dst);
  353. var d = yourpos.distanceTo(lookat.plus(mypos));
  354. return d <= 0.3;
  355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement