leapfrog.txt 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. event.load("block_break");
  2. event.load("block_place");
  3. event.load("block_click");
  4. event.load("player_join");
  5. event.load("player_giveup");
  6. event.load("player_logout");
  7. rankingtable = "leapfrogranks";
  8. game_short = "lf";
  9. sign.started(gamesignloc);
  10. gamename = "§9Leapfrog";
  11. points = 0;
  12. @wait
  13. wait();
  14. if(player.hasMinigameId(player, script_id)) {
  15. ignoreGoto(event);
  16. }
  17. goto("wait");
  18. @player_join
  19. player_record = ranking.getPoints(rankingtable, player);
  20. display.reset(player);
  21. display.add(player, 0, gamename);
  22. display.add(player, 1, "Points: §e", text.number(points));
  23. display.add(player, 2, "Your Record: §e", text.number(player_record));
  24. stacks.setActive(player, false);
  25. living.setHealth(player, 20);
  26. player.setHunger(player, 20);
  27. player.setSaturation(player, 5);
  28. entity.teleport(player, loc.mod(middleloc, 0.5, 0, 0.5));
  29. initPlayField();
  30. resetPlayField();
  31. msg.prefix(player, gamename, "Click on a torch and jump over another torch to let it disappear. Try to get as many points (jumps) as possible. If the last torch ends up in the middle, you get an extra point.");
  32. goto("wait");
  33. @block_break
  34. @block_place
  35. cancel = true;
  36. goto("wait");
  37. @block_click
  38. if(hand == "OFF_HAND" || action == "left") {
  39. goto("wait");
  40. }
  41. if(!isInPlayField(block_loc)) {
  42. goto("wait");
  43. }
  44. //Click token
  45. if(loc.getY(block_loc) == y_bottom + 1) {
  46. if(pos1 != null) {
  47. block.set(loc.mod(pos1, 0, -2, 0), torch);
  48. }
  49. pos1 = block_loc;
  50. block.set(loc.mod(pos1, 0, -2, 0), "minecraft:air");
  51. goto("wait");
  52. }
  53. //Jump with token
  54. if(loc.getY(block_loc) == y_bottom) {
  55. pos2 = block_loc;
  56. if(pos1 == null) {
  57. msg.prefix(player, gamename, "Select a torch first.");
  58. goto("wait");
  59. }
  60. //Are pos1 and pos2 on same line
  61. x1 = loc.getX(pos1);
  62. z1 = loc.getZ(pos1);
  63. x2 = loc.getX(pos2);
  64. z2 = loc.getZ(pos2);
  65. if(x1 == x2) {
  66. if(!distanceCheck(pos1, pos2)) {
  67. msgUnlegitMove(player);
  68. goto("wait");
  69. }
  70. if(fieldHasToken(pos2)) {
  71. msgUnlegitMove(player);
  72. goto("wait");
  73. }
  74. if(z2 > z1) {
  75. middlepos = loc.new(gamesworld, x1, y_bottom + 1, z1 + 1);
  76. } else {
  77. middlepos = loc.new(gamesworld, x1, y_bottom + 1, z2 + 1);
  78. }
  79. if(!hasToken(middlepos)) {
  80. msgUnlegitMove(player);
  81. goto("wait");
  82. }
  83. doMove(player, pos1, pos2, middlepos);
  84. goto("wait");
  85. }
  86. if(z1 == z2) {
  87. if(!distanceCheck(pos1, pos2)) {
  88. msgUnlegitMove(player);
  89. goto("wait");
  90. }
  91. if(fieldHasToken(pos2)) {
  92. msgUnlegitMove(player);
  93. goto("wait");
  94. }
  95. if(x2 > x1) {
  96. middlepos = loc.new(gamesworld, x1 + 1, y_bottom + 1, z1);
  97. } else {
  98. middlepos = loc.new(gamesworld, x2 + 1, y_bottom + 1, z1);
  99. }
  100. if(!hasToken(middlepos)) {
  101. msgUnlegitMove(player);
  102. goto("wait");
  103. }
  104. doMove(player, pos1, pos2, middlepos);
  105. goto("wait");
  106. }
  107. msgUnlegitMove(player);
  108. }
  109. goto("wait");
  110. @player_giveup
  111. @player_logout
  112. money.addBoost(player, math.round(points / 4));
  113. script = script.getFromId(script_id);
  114. minigame.kickPlayer(script, player);
  115. minigame.term(script, gamesignloc);
  116. term();
  117. function msgUnlegitMove(player) {
  118. msg.prefix(player, $gamename, "Unlegit move. Jump over a torch to a free field.");
  119. }
  120. function distanceCheck(pos1, pos2) { //returns true if the distance between the token and the selected field is 2
  121. return loc.distance(pos1, loc.mod(pos2, 0, 1, 0)) == 2;
  122. }
  123. function fieldHasToken(location) { //returns true if above a field is a token
  124. return !block.isAir(loc.mod(location, 0, 1, 0));
  125. }
  126. function hasToken(location) { //returns true if there is a token
  127. return !block.isAir(location);
  128. }
  129. function doMove(player, pos1, pos2, middlepos) {
  130. block.set(pos1, "minecraft:air");
  131. block.set(loc.mod(pos2, 0, 1, 0), $torch);
  132. block.set(middlepos, "minecraft:air");
  133. block.set(loc.mod(pos2, 0, -1, 0), $torch);
  134. $points++;
  135. $pos1 = null;
  136. if(isGameOver()) {
  137. msg.prefix(player, $gamename, "Gameover. No more moves possible.");
  138. msg.prefix(player, $gamename, "Study the field to get better.");
  139. msg.prefix(player, $gamename, "Use /leave to return to lobby.");
  140. event.unload("block_click");
  141. //extra
  142. if(block.getType($middleloc) == $torch && $points == 43) {
  143. $points++;
  144. msg.prefix(player, $gamename, "Extra point for the last torch ending up in the middle.");
  145. }
  146. showStats(player, $points);
  147. }
  148. display.add(player, 1, "Points: §e", text.number($points));
  149. player.setHunger(player, 20);
  150. player.setSaturation(player, 5);
  151. }
  152. function isGameOver() {
  153. //left to right
  154. for(i = 0; i < list.getSize($left_start_locs); i++) {
  155. //init a counter and history vars
  156. counter = 0;
  157. hist_2 = 1;
  158. hist_1 = 1;
  159. loc = list.getIndex($left_start_locs, i);
  160. loc = loc.mod(loc, 0, 0, 0);
  161. end_loc = list.getIndex($right_ends_locs, i);
  162. while(loc != end_loc) {
  163. //raise counter if current location is torch
  164. if(block.getType(loc) == $torch) {
  165. counter++;
  166. hist_0 = 1;
  167. } else {
  168. //if there is no torch and the counter has 2 or more torches the game is not over, else reset counter
  169. hist_0 = 0;
  170. if(counter >= 2) {
  171. return false;
  172. }
  173. counter = 0;
  174. }
  175. //if counter found 2 torches and the field before these 2 torches is free, game is not over
  176. if(counter == 2) {
  177. if(hist_2 == 0) {
  178. return false;
  179. }
  180. }
  181. hist_2 = hist_1;
  182. hist_1 = hist_0;
  183. loc.addX(loc, 1);
  184. }
  185. }
  186. //up to down
  187. for(i = 0; i < list.getSize($upper_start_locs); i++) {
  188. counter = 0;
  189. hist_2 = 1;
  190. hist_1 = 1;
  191. loc = list.getIndex($upper_start_locs, i);
  192. loc = loc.mod(loc, 0, 0, 0);
  193. end_loc = list.getIndex($down_ends_locs, i);
  194. while(loc != end_loc) {
  195. if(block.getType(loc) == $torch) {
  196. counter++;
  197. hist_0 = 1;
  198. } else {
  199. hist_0 = 0;
  200. if(counter >= 2) {
  201. return false;
  202. }
  203. counter = 0;
  204. }
  205. if(counter == 2) {
  206. if(hist_2 == 0) {
  207. return false;
  208. }
  209. }
  210. hist_2 = hist_1;
  211. hist_1 = hist_0;
  212. loc.addZ(loc, 1);
  213. }
  214. }
  215. return true;
  216. }
  217. function showStats(player, points) {
  218. minigame.addPlayed(player, $game_short, 1);
  219. minigame.statsHeader(player, $gamename, "§e");
  220. minigame.statsLine(player, "§e", "Points", text.number(points));
  221. if($player_record < points) {
  222. ranking.setPoints($rankingtable, player, points);
  223. minigame.statsLine(player, "§e", "Beat own record by", text.number(points - $player_record));
  224. $player_record = points;
  225. display.add(player, 2, "Your Record: §e", text.number($player_record));
  226. }
  227. minigame.statsLine(player, "§e", "Record", text.number($player_record));
  228. }
  229. function isInPlayField(location) {
  230. if(loc.isBetween(location, $edge1, loc.mod($edge2, 0, -1, 0))) {
  231. return true;
  232. }
  233. if(loc.isBetween(location, $edge3, loc.mod($edge4, 0, -1, 0))) {
  234. return true;
  235. }
  236. return false;
  237. }
  238. function initPlayField() {
  239. $left_start_locs = list.new();
  240. list.add($left_start_locs, loc.mod($lu_corner, 3, 0, 0));
  241. list.add($left_start_locs, loc.mod($lu_corner, 3, 0, 1));
  242. list.add($left_start_locs, loc.mod($lu_corner, 3, 0, 2));
  243. list.add($left_start_locs, loc.mod($lu_corner, 0, 0, 3));
  244. list.add($left_start_locs, loc.mod($lu_corner, 0, 0, 4));
  245. list.add($left_start_locs, loc.mod($lu_corner, 0, 0, 5));
  246. list.add($left_start_locs, loc.mod($lu_corner, 3, 0, 6));
  247. list.add($left_start_locs, loc.mod($lu_corner, 3, 0, 7));
  248. list.add($left_start_locs, loc.mod($lu_corner, 3, 0, 8));
  249. $right_ends_locs = list.new();
  250. list.add($right_ends_locs, loc.mod($lu_corner, 6, 0, 0));
  251. list.add($right_ends_locs, loc.mod($lu_corner, 6, 0, 1));
  252. list.add($right_ends_locs, loc.mod($lu_corner, 6, 0, 2));
  253. list.add($right_ends_locs, loc.mod($lu_corner, 9, 0, 3));
  254. list.add($right_ends_locs, loc.mod($lu_corner, 9, 0, 4));
  255. list.add($right_ends_locs, loc.mod($lu_corner, 9, 0, 5));
  256. list.add($right_ends_locs, loc.mod($lu_corner, 6, 0, 6));
  257. list.add($right_ends_locs, loc.mod($lu_corner, 6, 0, 7));
  258. list.add($right_ends_locs, loc.mod($lu_corner, 6, 0, 8));
  259. $upper_start_locs = list.new();
  260. list.add($upper_start_locs, loc.mod($lu_corner, 0, 0, 3));
  261. list.add($upper_start_locs, loc.mod($lu_corner, 1, 0, 3));
  262. list.add($upper_start_locs, loc.mod($lu_corner, 2, 0, 3));
  263. list.add($upper_start_locs, loc.mod($lu_corner, 3, 0, 0));
  264. list.add($upper_start_locs, loc.mod($lu_corner, 4, 0, 0));
  265. list.add($upper_start_locs, loc.mod($lu_corner, 5, 0, 0));
  266. list.add($upper_start_locs, loc.mod($lu_corner, 6, 0, 3));
  267. list.add($upper_start_locs, loc.mod($lu_corner, 7, 0, 3));
  268. list.add($upper_start_locs, loc.mod($lu_corner, 8, 0, 3));
  269. $down_ends_locs = list.new();
  270. list.add($down_ends_locs, loc.mod($lu_corner, 0, 0, 6));
  271. list.add($down_ends_locs, loc.mod($lu_corner, 1, 0, 6));
  272. list.add($down_ends_locs, loc.mod($lu_corner, 2, 0, 6));
  273. list.add($down_ends_locs, loc.mod($lu_corner, 3, 0, 9));
  274. list.add($down_ends_locs, loc.mod($lu_corner, 4, 0, 9));
  275. list.add($down_ends_locs, loc.mod($lu_corner, 5, 0, 9));
  276. list.add($down_ends_locs, loc.mod($lu_corner, 6, 0, 6));
  277. list.add($down_ends_locs, loc.mod($lu_corner, 7, 0, 6));
  278. list.add($down_ends_locs, loc.mod($lu_corner, 8, 0, 6));
  279. }
  280. function resetPlayField() {
  281. for(i = 0; i < list.getSize($left_start_locs); i++) {
  282. loc = list.getIndex($left_start_locs, i);
  283. loc = loc.mod(loc, 0, 0, 0);
  284. end_loc = list.getIndex($right_ends_locs, i);
  285. while(loc != end_loc) {
  286. block.set(loc, $torch);
  287. block.set(loc.mod(loc, 0, -2, 0), $torch);
  288. loc.addX(loc, 1);
  289. }
  290. }
  291. block.set($middleloc, "minecraft:air");
  292. }