leapfrog.txt 9.3 KB

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