snake_core.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. sign.started(gamesignloc);
  2. rankingtable = "snakeranks";
  3. game_short = "snake";
  4. maxplayers = 1;
  5. gamename = "§aSnake";
  6. air_material = material.get("AIR");
  7. head_material = material.get("RED_TERRACOTTA");
  8. body_material = material.get("GREEN_TERRACOTTA");
  9. second_body_material = material.get("LIME_TERRACOTTA");
  10. tail_material = material.get("CYAN_TERRACOTTA");
  11. food_material = material.get("RED_WOOL");
  12. snake_has_eaten = false;
  13. this_record = 0;
  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. turn_lore = list.new();
  29. list.add(turn_lore, text.new("Turns the Snake in your"));
  30. list.add(turn_lore, text.new("main look direction"));
  31. item_turn = item.new(material.get("arrow"),1);
  32. item.setName(item_turn, text.new("turn"));
  33. item.setLore(item_turn, turn_lore);
  34. possible_names_map = list.new();
  35. list.add(possible_names_map, "down");
  36. list.add(possible_names_map, "up");
  37. list.add(possible_names_map, "north");
  38. list.add(possible_names_map, "south");
  39. list.add(possible_names_map, "west");
  40. list.add(possible_names_map, "east");
  41. list.add(possible_names_map, "reset");
  42. list.add(possible_names_map, "turn");
  43. //events
  44. event.load("block_click");
  45. event.load("entity_damage");
  46. event.load("player_quit");
  47. event.load("player_giveup");
  48. event.load("minigame_join");
  49. event.load("block_break");
  50. //
  51. reset = false;
  52. difficulty = "hard";
  53. loop_times = map.new();
  54. map.add(loop_times, "trivial", 120);
  55. map.add(loop_times, "very easy", 80);
  56. map.add(loop_times, "easy", 60);
  57. map.add(loop_times, "medium", 40);
  58. map.add(loop_times, "hard", 20);
  59. map.add(loop_times, "very hard", 15);
  60. map.add(loop_times, "impossible", 10);
  61. loop_time = map.get(loop_times, difficulty);
  62. current_direction = array.new(3);
  63. current_direction[0] = 0;
  64. current_direction[1] = 1;
  65. current_direction[2] = 0;
  66. default_direction = array.new(3);
  67. default_direction[0] = 0;
  68. default_direction[1] = 1;
  69. default_direction[2] = 0;
  70. started = false;
  71. //
  72. goto("checkgame");
  73. @Start
  74. head_loc = loc.mod(head_start_loc, 0, 0, 0);
  75. snake_parts = map.new();
  76. middle_parts = list.new();
  77. die = false;
  78. current_direction = default_direction;
  79. snake.spawn();
  80. if(reset){
  81. snake.reset();
  82. reset = false;
  83. if(!truereset){
  84. truereset = true;
  85. sgoto(loop_time + 5,"loop2");
  86. }
  87. }else{
  88. truereset = false;
  89. sgoto(loop_time,"loop3");
  90. }
  91. @checkgame
  92. wait();
  93. if(event == "entity_damage") {
  94. if(!isPlayer(entity)) {
  95. goto("checkgame");
  96. }
  97. player = entity;
  98. }
  99. if(!player.hasMinigameId(player, script_id)) {
  100. goto("checkgame");
  101. }
  102. if(event == "minigame_join"){
  103. player.clearInventory(player);
  104. entity.teleport(player, Start_Pos);
  105. player_inv = player.getInv(player);
  106. player_had_fly = player.hasFly(player);
  107. player.setFly(player, true);
  108. inv.setItem(player_inv, 0, item_down);
  109. inv.setItem(player_inv, 1, item_up);
  110. inv.setItem(player_inv, 2, item_north);
  111. inv.setItem(player_inv, 3, item_south);
  112. inv.setItem(player_inv, 4, item_west);
  113. inv.setItem(player_inv, 5, item_east);
  114. inv.setItem(player_inv, 7, item_turn);
  115. inv.setItem(player_inv, 8, item_reset);
  116. box_iterator = loc.iterator(loc.getWorld(Edge_loc1), loc.getX(Edge_loc1),loc.getY(Edge_loc1),loc.getZ(Edge_loc1),loc.getX(Edge_loc2),loc.getY(Edge_loc2),loc.getZ(Edge_loc2));
  117. while(hasnext(box_iterator)){
  118. block.setMaterial(block.get(next(box_iterator)), air_material);
  119. }
  120. possible_food_locations = snake.food_create(Edge_loc1, Edge_loc2, 0, 0, 0);
  121. food_list_Size = list.getSize(possible_food_locations);
  122. for(a = 0; a < map.get(loop_times, difficulty); a = a + 5){
  123. snake.food();
  124. }
  125. goto("Start");
  126. }
  127. if(event == "block_break"){
  128. cancel = true;
  129. goto("checkgame");
  130. }
  131. if(event == "entity_damage") {
  132. cancel = true;
  133. goto("checkgame");
  134. }
  135. if(event == "block_click"){
  136. if(!started){
  137. goto("checkgame");
  138. }
  139. if(action == "PHYSICAL"){
  140. goto("checkgame");
  141. }
  142. player_hand = living.getHand(player);
  143. if(!item.hasName(player_hand)){
  144. goto("checkgame");
  145. }
  146. if(reset){
  147. goto("checkgame");
  148. }
  149. item_name = string.text(item.getName(player_hand));
  150. if(item_name == "reset"){
  151. started = false;
  152. snake.reset();
  153. reset = true;
  154. goto("Start");
  155. }elseif(item_name == "turn"){
  156. snake.setDirection(entity.getMainLookDirection(entity.getLook(player)));
  157. }else{
  158. snake.setDirection(item_name);
  159. }
  160. cancel = true;
  161. }
  162. if(event == "player_quit"){
  163. game_stop(null);
  164. }
  165. if(event == "player_giveup"){
  166. game_stop("You have left the game");
  167. }
  168. goto("checkgame");
  169. @loop2
  170. truereset = false;
  171. @loop3
  172. started = true;
  173. @loop
  174. if(!truereset && !reset){
  175. die = snake.head_move();
  176. if(die){
  177. snake.die();
  178. goto("checkgame");
  179. }
  180. if(snake_has_eaten){
  181. snake_has_eaten = false;
  182. }else{
  183. snake.tail_move();
  184. }
  185. sgoto(loop_time, "loop");
  186. }
  187. goto("checkgame");
  188. function snake.setDirection(item_name){
  189. if(item_name == "down"){
  190. $current_direction[0] = 0;
  191. $current_direction[1] = -1;
  192. $current_direction[2] = 0;
  193. }elseif(item_name == "up"){
  194. $current_direction[0] = 0;
  195. $current_direction[1] = 1;
  196. $current_direction[2] = 0;
  197. }elseif(item_name == "north"){
  198. $current_direction[0] = 0;
  199. $current_direction[1] = 0;
  200. $current_direction[2] = -1;
  201. }elseif(item_name == "east"){
  202. $current_direction[0] = 1;
  203. $current_direction[1] = 0;
  204. $current_direction[2] = 0;
  205. }elseif(item_name == "south"){
  206. $current_direction[0] = 0;
  207. $current_direction[1] = 0;
  208. $current_direction[2] = 1;
  209. }elseif(item_name == "west"){
  210. $current_direction[0] = -1;
  211. $current_direction[1] = 0;
  212. $current_direction[2] = 0;
  213. }
  214. }
  215. function snake.head_move(){
  216. save_direction = array.new(3);
  217. save_direction[0] = $current_direction[0];
  218. save_direction[1] = $current_direction[1];
  219. save_direction[2] = $current_direction[2];
  220. map.add($snake_parts, loc.mod($head_loc, 0, 0, 0), save_direction);
  221. new_body_loc = loc.mod($head_loc, save_direction[0], save_direction[1], save_direction[2]);
  222. new_body_block = block.get(new_body_loc);
  223. new_head_loc = loc.mod($head_loc, 2*save_direction[0], 2*save_direction[1], 2*save_direction[2]);
  224. new_head_block = block.get(new_head_loc);
  225. new_head_block_type = block.getType(new_head_block);
  226. if(block.getType(new_body_block) != $air_material){
  227. return true;
  228. }
  229. if(new_head_block_type != $air_material){
  230. if(new_head_block_type == $food_material){
  231. $snake_has_eaten = true;
  232. }
  233. else{
  234. return true;
  235. }
  236. }
  237. list.add($middle_parts, new_body_block);
  238. block.setMaterial(block.get($head_loc), $body_material, false);
  239. block.setMaterial(new_body_block, $second_body_material, false);
  240. block.setMaterial(new_head_block, $head_material, false);
  241. loc.add($head_loc, 2*save_direction[0], 2*save_direction[1], 2*save_direction[2]);
  242. if($snake_has_eaten){
  243. snake.food();
  244. }
  245. return false;
  246. }
  247. function snake.tail_move(){
  248. save_direction = map.getOrDefault($snake_parts, $tail_loc, $default_direction);
  249. map.remove($snake_parts, $tail_loc);
  250. mid_loc = loc.mod($tail_loc, save_direction[0], save_direction[1], save_direction[2]);
  251. mid_block = block.get(mid_loc);
  252. block.setMaterial(block.get($tail_loc), $air_material, false);
  253. block.setMaterial(mid_block, $air_material, false);
  254. list.remove($middle_parts, mid_block);
  255. $tail_loc = loc.mod(mid_loc, save_direction[0], save_direction[1], save_direction[2]);
  256. block.setMaterial(block.get($tail_loc), $tail_material, false);
  257. }
  258. function snake.die(){
  259. Snake_Size = map.getSize($snake_parts);
  260. if(Snake_Size > $this_record){
  261. $this_record = Snake_Size;
  262. }
  263. if(Snake_Size >= 999){
  264. minigame.speakAll($gamename, string.concat("Your snake died at the size of ", string.number(Snake_Size), ", this is the max length."));
  265. }else{
  266. minigame.speakAll($gamename, string.concat("Your snake died at the size of ", string.number(Snake_Size), "."));
  267. }
  268. player_id = player.getId($player);
  269. playedgames = minigame.getPlayed(player_id, $game_short) + 1;
  270. minigame.setPlayed(player_id, $game_short, playedgames);
  271. }
  272. function snake.reset(){
  273. map_iterator = map.iterator($snake_parts);
  274. while(hasnext(map_iterator)){
  275. block.setMaterial(block.get(map.getKey(next(map_iterator))), $air_material);
  276. }
  277. list_iterator = iterator($middle_parts);
  278. while(hasnext(list_iterator)){
  279. block.setMaterial(next(list_iterator), $air_material);
  280. }
  281. block.setMaterial(block.get($head_loc), $air_material);
  282. block.setMaterial(block.get($tail_loc), $air_material);
  283. map.clear($snake_parts);
  284. list.clear($middle_parts);
  285. }
  286. function snake.food(){
  287. temp_block = list.getIndex($possible_food_locations, math.random(0,$food_list_Size-1));
  288. a = 0;
  289. while(block.getType(temp_block) != $air_material && a < $food_list_Size){
  290. temp_block = list.getIndex($possible_food_locations, math.random(0,$food_list_Size-1));
  291. a++;
  292. }
  293. if(a < $food_list_Size){
  294. block.setMaterial(temp_block, $food_material, false);
  295. }
  296. }
  297. function snake.food_create(loc1, loc2, X, Y, Z){//X,Y,Z 0/1 if uneven
  298. possible_food_locations = list.new();
  299. x1 = loc.getX(loc1);
  300. y1 = loc.getY(loc1);
  301. z1 = loc.getZ(loc1);
  302. x2 = loc.getX(loc2);
  303. y2 = loc.getY(loc2);
  304. z2 = loc.getZ(loc2);
  305. box_iterator = loc.iterator(loc.getWorld(loc1), x1,y1,z1,x2,y2,z2);
  306. while(hasnext(box_iterator)){
  307. temp_block = block.get(next(box_iterator));
  308. temp_loc = block.getLocation(temp_block);
  309. 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){
  310. list.add(possible_food_locations, temp_block);
  311. }
  312. }
  313. return possible_food_locations;
  314. }
  315. function snake.spawn(){
  316. $tail_loc = loc.mod($head_loc, -2*$current_direction[0], -2*$current_direction[1], -2*$current_direction[2]);
  317. block.setMaterial(block.get($head_loc), $head_material, false);
  318. block.setMaterial(block.get(loc.mod($head_loc, -$current_direction[0], -$current_direction[1], -$current_direction[2])), $second_body_material, false);
  319. block.setMaterial(block.get($tail_loc), $tail_material, false);
  320. map.add($snake_parts, $tail_loc, $current_direction);
  321. }
  322. function entity.getMainLookDirection(look_array){
  323. X = look_array[0];
  324. Y = look_array[1];
  325. Z = look_array[2];
  326. X_abs = math.abs(X);
  327. Y_abs = math.abs(Y);
  328. Z_abs = math.abs(Z);
  329. looks_map = map.new();
  330. map.add(looks_map, X_abs, 0);
  331. map.add(looks_map, Y_abs, 1);
  332. map.add(looks_map, Z_abs, 2);
  333. main_direction = map.get(looks_map, math.max(math.max(X_abs, Y_abs), Z_abs)); //0=X,1=Y,2=Z
  334. if(main_direction == 0){
  335. if(X < 0){
  336. return "west";
  337. }else{
  338. return "east";
  339. }
  340. }elseif(main_direction == 1){
  341. if(Y < 0){
  342. return "down";
  343. }else{
  344. return "up";
  345. }
  346. }elseif(main_direction == 2){
  347. if(Z < 0){
  348. return "north";
  349. }else{
  350. return "south";
  351. }
  352. }
  353. }
  354. function game_stop(message){
  355. Snake_Size = map.getSize($snake_parts);
  356. if($this_record < Snake_Size){
  357. $this_record = Snake_Size;
  358. }
  359. player_id = player.getId($player);
  360. last_record = ranking.getPoints($rankingtable, player_id);
  361. if(last_record < $this_record){
  362. record = $this_record;
  363. diff = $this_record - last_record;
  364. }else{
  365. record = last_record;
  366. }
  367. ranking.setPoints($rankingtable, player_id, record);
  368. playedgames = minigame.getPlayed(player_id, $game_short) + 1;
  369. minigame.setPlayed(player_id, $game_short, playedgames);
  370. player_list = minigame.getPlayers(script_id);
  371. if(message != null){
  372. msg.prefix($player, $gamename, message);
  373. minigame.statsHeader($player, $gamename, "§e");
  374. minigame.statsLine($player, "§e", "Size", string.number($this_record));
  375. minigame.statsLine($player, "§e", "Your record", string.number(last_record));
  376. if(diff != null){
  377. msg.string($player, string.concat(" §e- §rBeat own record by §e", string.number(diff)));
  378. }
  379. minigame.statsLine($player, "§e", "Played games", string.number(playedgames));
  380. }
  381. script = script.getFromId($script_id);
  382. minigame.kickPlayer(script, $player);
  383. player.setFly($player, $player_had_fly);
  384. minigame.term(script, $gamesignloc);
  385. term();
  386. }