123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- sign.started(gamesignloc);
- rankingtable = "snakeranks";
- game_short = "snake";
- maxplayers = 1;
- gamename = "§aSnake";
- air_material = material.get("AIR");
- head_material = material.get("RED_TERRACOTTA");
- body_material = material.get("GREEN_TERRACOTTA");
- second_body_material = material.get("LIME_TERRACOTTA");
- tail_material = material.get("CYAN_TERRACOTTA");
- food_material = material.get("RED_WOOL");
- snake_has_eaten = false;
- this_record = 0;
- item_down = item.new(material.get("purple_concrete"),1);
- item.setName(item_down, text.new("down"));
- item_up = item.new(material.get("yellow_concrete"),1);
- item.setName(item_up, text.new("up"));
- item_north = item.new(material.get("pink_concrete"),1);
- item.setName(item_north, text.new("north"));
- item_south = item.new(material.get("magenta_concrete"),1);
- item.setName(item_south, text.new("south"));
- item_west = item.new(material.get("red_concrete"),1);
- item.setName(item_west, text.new("west"));
- item_east = item.new(material.get("blue_concrete"),1);
- item.setName(item_east, text.new("east"));
- item_reset = item.new(material.get("stone"),1);
- item.setName(item_reset, text.new("reset"));
- turn_lore = list.new();
- list.add(turn_lore, text.new("Turns the Snake in your"));
- list.add(turn_lore, text.new("main look direction"));
- item_turn = item.new(material.get("arrow"),1);
- item.setName(item_turn, text.new("turn"));
- item.setLore(item_turn, turn_lore);
- possible_names_map = list.new();
- list.add(possible_names_map, "down");
- list.add(possible_names_map, "up");
- list.add(possible_names_map, "north");
- list.add(possible_names_map, "south");
- list.add(possible_names_map, "west");
- list.add(possible_names_map, "east");
- list.add(possible_names_map, "reset");
- list.add(possible_names_map, "turn");
- //events
- event.load("block_click");
- event.load("entity_damage");
- event.load("player_quit");
- event.load("player_giveup");
- event.load("minigame_join");
- event.load("block_break");
- //
- reset = false;
- difficulty = "hard";
- loop_times = map.new();
- map.add(loop_times, "trivial", 120);
- map.add(loop_times, "very easy", 80);
- map.add(loop_times, "easy", 60);
- map.add(loop_times, "medium", 40);
- map.add(loop_times, "hard", 20);
- map.add(loop_times, "very hard", 15);
- map.add(loop_times, "impossible", 10);
- loop_time = map.get(loop_times, difficulty);
- current_direction = array.new(3);
- current_direction[0] = 0;
- current_direction[1] = 1;
- current_direction[2] = 0;
- default_direction = array.new(3);
- default_direction[0] = 0;
- default_direction[1] = 1;
- default_direction[2] = 0;
- started = false;
- //
- goto("checkgame");
- @Start
- head_loc = loc.mod(head_start_loc, 0, 0, 0);
- snake_parts = map.new();
- middle_parts = list.new();
- die = false;
- current_direction = default_direction;
- snake.spawn();
- if(reset){
- snake.reset();
- reset = false;
- if(!truereset){
- truereset = true;
- sgoto(loop_time + 5,"loop2");
- }
- }else{
- truereset = false;
- sgoto(loop_time,"loop3");
- }
- @checkgame
- wait();
- if(event == "entity_damage") {
- if(!isPlayer(entity)) {
- goto("checkgame");
- }
- player = entity;
- }
- if(!player.hasMinigameId(player, script_id)) {
- goto("checkgame");
- }
- if(event == "minigame_join"){
- player.clearInventory(player);
- entity.teleport(player, Start_Pos);
- player_inv = player.getInv(player);
- player_had_fly = player.hasFly(player);
- player.setFly(player, true);
-
- inv.setItem(player_inv, 0, item_down);
- inv.setItem(player_inv, 1, item_up);
- inv.setItem(player_inv, 2, item_north);
- inv.setItem(player_inv, 3, item_south);
- inv.setItem(player_inv, 4, item_west);
- inv.setItem(player_inv, 5, item_east);
- inv.setItem(player_inv, 7, item_turn);
- inv.setItem(player_inv, 8, item_reset);
-
- 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));
- while(hasnext(box_iterator)){
- block.setMaterial(block.get(next(box_iterator)), air_material);
- }
-
- possible_food_locations = snake.food_create(Edge_loc1, Edge_loc2, 0, 0, 0);
- food_list_Size = list.getSize(possible_food_locations);
-
- for(a = 0; a < map.get(loop_times, difficulty); a = a + 5){
- snake.food();
- }
- goto("Start");
- }
- if(event == "block_break"){
- cancel = true;
- goto("checkgame");
- }
- if(event == "entity_damage") {
- cancel = true;
- goto("checkgame");
- }
- if(event == "block_click"){
- if(!started){
- goto("checkgame");
- }
- if(action == "PHYSICAL"){
- goto("checkgame");
- }
- player_hand = living.getHand(player);
- if(!item.hasName(player_hand)){
- goto("checkgame");
- }
- if(reset){
- goto("checkgame");
- }
- item_name = string.text(item.getName(player_hand));
- if(item_name == "reset"){
- started = false;
- snake.reset();
- reset = true;
- goto("Start");
- }elseif(item_name == "turn"){
- snake.setDirection(entity.getMainLookDirection(entity.getLook(player)));
- }else{
- snake.setDirection(item_name);
- }
- cancel = true;
- }
- if(event == "player_quit"){
- game_stop(null);
- }
- if(event == "player_giveup"){
- game_stop("You have left the game");
- }
- goto("checkgame");
- @loop2
- truereset = false;
- @loop3
- started = true;
- @loop
- if(!truereset && !reset){
- die = snake.head_move();
- if(die){
- snake.die();
- goto("checkgame");
- }
- if(snake_has_eaten){
- snake_has_eaten = false;
- }else{
- snake.tail_move();
- }
- sgoto(loop_time, "loop");
- }
- goto("checkgame");
- function snake.setDirection(item_name){
- if(item_name == "down"){
- $current_direction[0] = 0;
- $current_direction[1] = -1;
- $current_direction[2] = 0;
- }elseif(item_name == "up"){
- $current_direction[0] = 0;
- $current_direction[1] = 1;
- $current_direction[2] = 0;
- }elseif(item_name == "north"){
- $current_direction[0] = 0;
- $current_direction[1] = 0;
- $current_direction[2] = -1;
- }elseif(item_name == "east"){
- $current_direction[0] = 1;
- $current_direction[1] = 0;
- $current_direction[2] = 0;
- }elseif(item_name == "south"){
- $current_direction[0] = 0;
- $current_direction[1] = 0;
- $current_direction[2] = 1;
- }elseif(item_name == "west"){
- $current_direction[0] = -1;
- $current_direction[1] = 0;
- $current_direction[2] = 0;
- }
- }
- function snake.head_move(){
- save_direction = array.new(3);
- save_direction[0] = $current_direction[0];
- save_direction[1] = $current_direction[1];
- save_direction[2] = $current_direction[2];
- map.add($snake_parts, loc.mod($head_loc, 0, 0, 0), save_direction);
-
- new_body_loc = loc.mod($head_loc, save_direction[0], save_direction[1], save_direction[2]);
- new_body_block = block.get(new_body_loc);
-
- new_head_loc = loc.mod($head_loc, 2*save_direction[0], 2*save_direction[1], 2*save_direction[2]);
- new_head_block = block.get(new_head_loc);
- new_head_block_type = block.getType(new_head_block);
- if(block.getType(new_body_block) != $air_material){
- return true;
- }
- if(new_head_block_type != $air_material){
- if(new_head_block_type == $food_material){
- $snake_has_eaten = true;
- }
- else{
- return true;
- }
- }
- list.add($middle_parts, new_body_block);
-
- block.setMaterial(block.get($head_loc), $body_material, false);
- block.setMaterial(new_body_block, $second_body_material, false);
- block.setMaterial(new_head_block, $head_material, false);
-
- loc.add($head_loc, 2*save_direction[0], 2*save_direction[1], 2*save_direction[2]);
- if($snake_has_eaten){
- snake.food();
- }
- return false;
- }
- function snake.tail_move(){
- save_direction = map.getOrDefault($snake_parts, $tail_loc, $default_direction);
- map.remove($snake_parts, $tail_loc);
-
- mid_loc = loc.mod($tail_loc, save_direction[0], save_direction[1], save_direction[2]);
-
- mid_block = block.get(mid_loc);
-
- block.setMaterial(block.get($tail_loc), $air_material, false);
- block.setMaterial(mid_block, $air_material, false);
-
- list.remove($middle_parts, mid_block);
-
- $tail_loc = loc.mod(mid_loc, save_direction[0], save_direction[1], save_direction[2]);
- block.setMaterial(block.get($tail_loc), $tail_material, false);
- }
- function snake.die(){
- Snake_Size = map.getSize($snake_parts);
- if(Snake_Size > $this_record){
- $this_record = Snake_Size;
- }
- if(Snake_Size >= 999){
- minigame.speakAll($gamename, string.concat("Your snake died at the size of ", string.number(Snake_Size), ", this is the max length."));
- }else{
- minigame.speakAll($gamename, string.concat("Your snake died at the size of ", string.number(Snake_Size), "."));
- }
-
- player_id = player.getId($player);
- playedgames = minigame.getPlayed(player_id, $game_short) + 1;
- minigame.setPlayed(player_id, $game_short, playedgames);
- }
- function snake.reset(){
- map_iterator = map.iterator($snake_parts);
- while(hasnext(map_iterator)){
- block.setMaterial(block.get(map.getKey(next(map_iterator))), $air_material);
- }
- list_iterator = iterator($middle_parts);
- while(hasnext(list_iterator)){
- block.setMaterial(next(list_iterator), $air_material);
- }
- block.setMaterial(block.get($head_loc), $air_material);
- block.setMaterial(block.get($tail_loc), $air_material);
- map.clear($snake_parts);
- list.clear($middle_parts);
- }
- function snake.food(){
- temp_block = list.getIndex($possible_food_locations, math.random(0,$food_list_Size-1));
- a = 0;
- while(block.getType(temp_block) != $air_material && a < $food_list_Size){
- temp_block = list.getIndex($possible_food_locations, math.random(0,$food_list_Size-1));
- a++;
- }
- if(a < $food_list_Size){
- block.setMaterial(temp_block, $food_material, false);
- }
- }
- function snake.food_create(loc1, loc2, X, Y, Z){//X,Y,Z 0/1 if uneven
- possible_food_locations = list.new();
- x1 = loc.getX(loc1);
- y1 = loc.getY(loc1);
- z1 = loc.getZ(loc1);
- x2 = loc.getX(loc2);
- y2 = loc.getY(loc2);
- z2 = loc.getZ(loc2);
- box_iterator = loc.iterator(loc.getWorld(loc1), x1,y1,z1,x2,y2,z2);
- while(hasnext(box_iterator)){
- temp_block = block.get(next(box_iterator));
- temp_loc = block.getLocation(temp_block);
- 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){
- list.add(possible_food_locations, temp_block);
- }
- }
- return possible_food_locations;
- }
- function snake.spawn(){
- $tail_loc = loc.mod($head_loc, -2*$current_direction[0], -2*$current_direction[1], -2*$current_direction[2]);
- block.setMaterial(block.get($head_loc), $head_material, false);
- block.setMaterial(block.get(loc.mod($head_loc, -$current_direction[0], -$current_direction[1], -$current_direction[2])), $second_body_material, false);
- block.setMaterial(block.get($tail_loc), $tail_material, false);
-
- map.add($snake_parts, $tail_loc, $current_direction);
- }
- function entity.getMainLookDirection(look_array){
- X = look_array[0];
- Y = look_array[1];
- Z = look_array[2];
- X_abs = math.abs(X);
- Y_abs = math.abs(Y);
- Z_abs = math.abs(Z);
-
- looks_map = map.new();
- map.add(looks_map, X_abs, 0);
- map.add(looks_map, Y_abs, 1);
- map.add(looks_map, Z_abs, 2);
-
- main_direction = map.get(looks_map, math.max(math.max(X_abs, Y_abs), Z_abs)); //0=X,1=Y,2=Z
-
- if(main_direction == 0){
- if(X < 0){
- return "west";
- }else{
- return "east";
- }
- }elseif(main_direction == 1){
- if(Y < 0){
- return "down";
- }else{
- return "up";
- }
- }elseif(main_direction == 2){
- if(Z < 0){
- return "north";
- }else{
- return "south";
- }
- }
- }
- function game_stop(message){
- Snake_Size = map.getSize($snake_parts);
- if($this_record < Snake_Size){
- $this_record = Snake_Size;
- }
-
- player_id = player.getId($player);
- last_record = ranking.getPoints($rankingtable, player_id);
- if(last_record < $this_record){
- record = $this_record;
- diff = $this_record - last_record;
- }else{
- record = last_record;
- }
- ranking.setPoints($rankingtable, player_id, record);
-
- playedgames = minigame.getPlayed(player_id, $game_short) + 1;
- minigame.setPlayed(player_id, $game_short, playedgames);
-
- player_list = minigame.getPlayers(script_id);
- if(message != null){
- msg.prefix($player, $gamename, message);
-
- minigame.statsHeader($player, $gamename, "§e");
- minigame.statsLine($player, "§e", "Size", string.number($this_record));
- minigame.statsLine($player, "§e", "Your record", string.number(last_record));
- if(diff != null){
-
- msg.string($player, string.concat(" §e- §rBeat own record by §e", string.number(diff)));
- }
- minigame.statsLine($player, "§e", "Played games", string.number(playedgames));
- }
- script = script.getFromId($script_id);
- minigame.kickPlayer(script, $player);
- player.setFly($player, $player_had_fly);
- minigame.term(script, $gamesignloc);
- term();
- }
|