snake.txt 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. //coords
  2. /*
  3. snake head places blocks saved as key in map with the value of its facing-direction[x,y,z (out of which only one is +- 1)]
  4. snake tail destroys blocks and moves in their stored direction, or waits 1 turn if food has been eaten
  5. */
  6. world = world.get("creative");
  7. air_material = material.get("AIR");
  8. head_material = material.get("RED_TERRACOTTA");
  9. body_material = material.get("GREEN_TERRACOTTA");
  10. second_body_material = material.get("LIME_TERRACOTTA");
  11. tail_material = material.get("CYAN_TERRACOTTA");
  12. food_material = material.get("RED_WOOL");
  13. snake_has_eaten = false;
  14. item_down = item.new(material.get("purple_concrete"),1);
  15. item.setName(item_down, text.new("down"));
  16. item_up = item.new(material.get("yellow_concrete"),1);
  17. item.setName(item_up, text.new("up"));
  18. item_north = item.new(material.get("pink_concrete"),1);
  19. item.setName(item_north, text.new("north"));
  20. item_south = item.new(material.get("magenta_concrete"),1);
  21. item.setName(item_south, text.new("south"));
  22. item_west = item.new(material.get("red_concrete"),1);
  23. item.setName(item_west, text.new("west"));
  24. item_east = item.new(material.get("blue_concrete"),1);
  25. item.setName(item_east, text.new("east"));
  26. item_reset = item.new(material.get("stone"),1);
  27. item.setName(item_reset, text.new("reset"));
  28. item_turn = item.new(material.get("arrow"),1);
  29. item.setName(item_turn, text.new("turn"));
  30. possible_names_map = list.new();
  31. list.add(possible_names_map, "down");
  32. list.add(possible_names_map, "up");
  33. list.add(possible_names_map, "north");
  34. list.add(possible_names_map, "south");
  35. list.add(possible_names_map, "west");
  36. list.add(possible_names_map, "east");
  37. list.add(possible_names_map, "reset");
  38. list.add(possible_names_map, "turn");
  39. //"88822148-d046-4345-a64a-da6bbfb684ca" Mareeeen
  40. //"6cc9f8c7-9dfd-44f4-a3f2-af30054411a8" SirTerence7
  41. //"e41b5335-3c74-46e9-a6c5-dafc6334a477" marvinius
  42. uuid = "6cc9f8c7-9dfd-44f4-a3f2-af30054411a8";
  43. player = player.get(uuid);
  44. player_inv = player.getInv(player);
  45. inv.setItem(player_inv, 0, item_down);
  46. inv.setItem(player_inv, 1, item_up);
  47. inv.setItem(player_inv, 2, item_north);
  48. inv.setItem(player_inv, 3, item_south);
  49. inv.setItem(player_inv, 4, item_west);
  50. inv.setItem(player_inv, 5, item_east);
  51. inv.setItem(player_inv, 7, item_turn);
  52. inv.setItem(player_inv, 8, item_reset);
  53. Edge_loc1 = loc.new(world, 3068, 52, 2946);
  54. Edge_loc2 = loc.new(world, 3086, 68, 2964);
  55. possible_food_locations = snake_food_create(Edge_loc1, Edge_loc2, 0, 0, 0);
  56. food_list_Size = list.getSize(possible_food_locations);
  57. event.load("block_click");
  58. reset = false;
  59. difficulty = "hard";
  60. loop_times = map.new();
  61. map.add(loop_times, "trivial", 120);
  62. map.add(loop_times, "very easy", 80);
  63. map.add(loop_times, "easy", 60);
  64. map.add(loop_times, "medium", 40);
  65. map.add(loop_times, "hard", 20);
  66. map.add(loop_times, "very hard", 15);
  67. map.add(loop_times, "impossible", 10);
  68. loop_time = map.get(loop_times, difficulty);
  69. for(a = 0; a < map.get(loop_times, difficulty); a = a + 5){
  70. snake.food();
  71. }
  72. sgoto(40,"loop");
  73. @Start
  74. $head_loc = loc.new(world, 3074, 58, 2958);
  75. snake_parts = map.new();
  76. middle_parts = list.new();
  77. die = false;
  78. current_direction = array.new(3);
  79. current_direction[0] = 0;
  80. current_direction[1] = 1;
  81. current_direction[2] = 0;
  82. if(reset){
  83. snake.reset();
  84. reset = false;
  85. }
  86. snake.spawn();
  87. @checkgame
  88. wait();
  89. if(event == "block_click"){
  90. if(player.getName(player) != "SirTerence7" && player.getName(player) != "Mareeeen"){
  91. goto("checkgame");
  92. }
  93. if(action == "PHYSICAL"){
  94. goto("checkgame");
  95. }
  96. player_hand = living.getHand(player);
  97. if(!item.hasName(player_hand)){
  98. goto("checkgame");
  99. }
  100. item_name = string.text(item.getName(player_hand));
  101. if(item_name == "reset"){
  102. snake.reset();
  103. reset = true;
  104. goto("Start");
  105. }elseif(item_name == "turn"){
  106. snake.setDirection(entity.getMainLookDirection(entity.getLook(player)));
  107. }else{
  108. snake.setDirection(item_name);
  109. }
  110. cancel = true;
  111. }
  112. goto("checkgame");
  113. @loop
  114. die = snake.head_move();
  115. if(die){
  116. snake.die();
  117. goto("checkgame");
  118. }
  119. if(snake_has_eaten){
  120. snake_has_eaten = false;
  121. }else{
  122. snake.tail_move();
  123. }
  124. sgoto(loop_time, "loop");
  125. goto("checkgame");
  126. function snake.setDirection(item_name){
  127. if(item_name == "down"){
  128. $current_direction[0] = 0;
  129. $current_direction[1] = -1;
  130. $current_direction[2] = 0;
  131. }elseif(item_name == "up"){
  132. $current_direction[0] = 0;
  133. $current_direction[1] = 1;
  134. $current_direction[2] = 0;
  135. }elseif(item_name == "north"){
  136. $current_direction[0] = 0;
  137. $current_direction[1] = 0;
  138. $current_direction[2] = -1;
  139. }elseif(item_name == "east"){
  140. $current_direction[0] = 1;
  141. $current_direction[1] = 0;
  142. $current_direction[2] = 0;
  143. }elseif(item_name == "south"){
  144. $current_direction[0] = 0;
  145. $current_direction[1] = 0;
  146. $current_direction[2] = 1;
  147. }elseif(item_name == "west"){
  148. $current_direction[0] = -1;
  149. $current_direction[1] = 0;
  150. $current_direction[2] = 0;
  151. }
  152. }
  153. function snake.head_move(){
  154. save_direction = array.new(3);
  155. save_direction[0] = $current_direction[0];
  156. save_direction[1] = $current_direction[1];
  157. save_direction[2] = $current_direction[2];
  158. new_body_loc = loc.mod($head_loc, save_direction[0], save_direction[1], save_direction[2]);
  159. new_body_block = block.get(new_body_loc);
  160. new_head_loc = loc.mod($head_loc, 2*save_direction[0], 2*save_direction[1], 2*save_direction[2]);
  161. new_head_block = block.get(new_head_loc);
  162. new_head_block_type = block.getType(new_head_block);
  163. if(block.getType(new_body_block) != $air_material){
  164. return true;
  165. }
  166. if(new_head_block_type != $air_material){
  167. if(new_head_block_type == $food_material){
  168. $snake_has_eaten = true;
  169. }
  170. else{
  171. return true;
  172. }
  173. }
  174. map.add($snake_parts, loc.mod($head_loc, 0, 0, 0), save_direction);
  175. list.add($middle_parts, new_body_block);
  176. block.setMaterial(block.get($head_loc), $body_material, false);
  177. block.setMaterial(new_body_block, $second_body_material, false);
  178. block.setMaterial(new_head_block, $head_material, false);
  179. loc.add($head_loc, 2*save_direction[0], 2*save_direction[1], 2*save_direction[2]);
  180. if($snake_has_eaten){
  181. snake.food();
  182. }
  183. return false;
  184. }
  185. function snake.tail_move(){
  186. save_direction = map.get($snake_parts, $tail_loc);
  187. map.remove($snake_parts, $tail_loc);
  188. mid_loc = loc.mod($tail_loc, save_direction[0], save_direction[1], save_direction[2]);
  189. mid_block = block.get(mid_loc);
  190. block.setMaterial(block.get($tail_loc), $air_material, false);
  191. block.setMaterial(mid_block, $air_material, false);
  192. list.remove($middle_parts, mid_block);
  193. $tail_loc = loc.mod(mid_loc, save_direction[0], save_direction[1], save_direction[2]);
  194. block.setMaterial(block.get($tail_loc), $tail_material, false);
  195. }
  196. function snake.die(){
  197. Snake_Size = map.getSize($snake_parts);
  198. if(Snake_Size >= $food_list_Size - 3){
  199. msg("dev", text.new(""));
  200. }
  201. msg("dev", text.new(Snake_Size));
  202. }
  203. function snake.reset(){
  204. map_iterator = map.iterator($snake_parts);
  205. while(hasnext(map_iterator)){
  206. block.setMaterial(block.get(map.getKey(next(map_iterator))), $air_material);
  207. }
  208. list_iterator = iterator($middle_parts);
  209. while(hasnext(list_iterator)){
  210. block.setMaterial(next(list_iterator), $air_material);
  211. }
  212. block.setMaterial(block.get($head_loc), $air_material);
  213. block.setMaterial(block.get($tail_loc), $air_material);
  214. map.clear($snake_parts);
  215. list.clear($middle_parts);
  216. }
  217. function snake.food(){
  218. temp_block = list.getIndex($possible_food_locations, math.random(0,$food_list_Size-1));
  219. a = 0;
  220. while(block.getType(temp_block) != $air_material && a < $food_list_Size){
  221. temp_block = list.getIndex($possible_food_locations, math.random(0,$food_list_Size-1));
  222. a++;
  223. }
  224. if(a < $food_list_Size){
  225. block.setMaterial(temp_block, $food_material, false);
  226. }
  227. }
  228. function snake_food_create(loc1, loc2, X, Y, Z){//X,Y,Z 0/1 if uneven
  229. possible_food_locations = list.new();
  230. x1 = loc.getX(loc1);
  231. y1 = loc.getY(loc1);
  232. z1 = loc.getZ(loc1);
  233. x2 = loc.getX(loc2);
  234. y2 = loc.getY(loc2);
  235. z2 = loc.getZ(loc2);
  236. box_iterator = loc.iterator(loc.getWorld(loc1), x1,y1,z1,x2,y2,z2);
  237. while(hasnext(box_iterator)){
  238. temp_block = block.get(next(box_iterator));
  239. temp_loc = block.getLocation(temp_block);
  240. if(loc.getX(temp_loc)%2 == X && loc.getY(temp_loc)%2 == Y && loc.getZ(temp_loc)%2 == Z && block.getType(temp_block) == $air_material){
  241. list.add(possible_food_locations, temp_block);
  242. }
  243. }
  244. return possible_food_locations;
  245. }
  246. function snake.spawn(){
  247. block.setMaterial(block.get($head_loc), $head_material, false);
  248. block.setMaterial(block.get(loc.mod($head_loc, -$current_direction[0], -$current_direction[1], -$current_direction[2])), $body_material, false);
  249. $tail_loc = loc.mod($head_loc, -2*$current_direction[0], -2*$current_direction[1], -2*$current_direction[2]);
  250. block.setMaterial(block.get($tail_loc), $tail_material, false);
  251. map.add($snake_parts, $tail_loc, $current_direction);
  252. }
  253. function entity.getMainLookDirection(look_array){
  254. X = look_array[0];
  255. Y = look_array[1];
  256. Z = look_array[2];
  257. X_abs = math.abs(X);
  258. Y_abs = math.abs(Y);
  259. Z_abs = math.abs(Z);
  260. looks_map = map.new();
  261. map.add(looks_map, X_abs, 0);
  262. map.add(looks_map, Y_abs, 1);
  263. map.add(looks_map, Z_abs, 2);
  264. main_direction = map.get(looks_map, math.max(math.max(X_abs, Y_abs), Z_abs)); //0=X,1=Y,2=Z
  265. if(main_direction == 0){
  266. if(X < 0){
  267. return "west";
  268. }else{
  269. return "east";
  270. }
  271. }elseif(main_direction == 1){
  272. if(Y < 0){
  273. return "down";
  274. }else{
  275. return "up";
  276. }
  277. }elseif(main_direction == 2){
  278. if(Z < 0){
  279. return "north";
  280. }else{
  281. return "south";
  282. }
  283. }
  284. }