voxel.txt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. event.load("item_air_click");
  2. event.load("left_click_air");
  3. event.load("block_break");
  4. default_state = block.newState("minecraft:stone");
  5. block_state = default_state;
  6. air_state = block.newState("minecraft:air");
  7. radius_per_player = map.new();
  8. state_per_player = map.new();
  9. msg("dev", "§bVoxel §rloaded.");
  10. @wait
  11. wait();
  12. if(!perm.has(player, "voxel")) {
  13. goto("wait");
  14. }
  15. if(event == "left_click_air" || event == "block_break") {
  16. state = air_state;
  17. last = false;
  18. } elseif(event == "item_air_click") {
  19. state = map.getOrDefault(state_per_player, player.getUuid(player), default_state);
  20. block_state = state;
  21. last = true;
  22. } else {
  23. goto("wait");
  24. }
  25. if(item.getType(living.getEquip(player, "hand")) == "km:plus_green") {
  26. goto("doshit");
  27. }
  28. if(item.getType(living.getEquip(player, "hand")) == "km:minus_green") {
  29. goto("dosmooth");
  30. }
  31. goto("wait");
  32. @doshit
  33. loc = entity.getLocation(player);
  34. world = loc.getWorld(loc);
  35. look = entity.getLook(player);
  36. loc.setX(loc, loc.getCoord(loc, "bx"));
  37. loc.setY(loc, loc.getCoord(loc, "by") + 1);
  38. loc.setZ(loc, loc.getCoord(loc, "bz"));
  39. list = list.new();
  40. radius = map.getOrDefault(radius_per_player, player.getUuid(player), 5);
  41. for(y = -radius; y <= radius; y += 0.5) {
  42. for(x = -radius; x <= radius; x += 0.5) {
  43. other_loc = loc.mod(loc, x, y, 0);
  44. if(loc.distance(loc, other_loc) < radius) {
  45. list.add(list, other_loc);
  46. }
  47. }
  48. }
  49. yaw = loc.getYaw(loc);
  50. pitch = loc.getPitch(loc);
  51. while(yaw < 0) {
  52. yaw += 360;
  53. }
  54. while(yaw >= 360) {
  55. yaw -= 360;
  56. }
  57. yaw = yaw * math.pi() / 180;
  58. pitch = pitch * math.pi() / 180;
  59. matrix = matrix.newRotationY(-yaw);
  60. matrix = matrix.mul(matrix, matrix.newRotationX(-pitch));
  61. list2 = list.new();
  62. vector = vector.new(0, 0, 0);
  63. iter = list.iterator(list);
  64. while(hasNext(iter)) {
  65. other_loc = next(iter);
  66. vector.set(vector, loc.getX(other_loc) - loc.getX(loc), loc.getY(other_loc) - loc.getY(loc), loc.getZ(other_loc) - loc.getZ(loc));
  67. matrix.mulVector(matrix, vector);
  68. loc.set(other_loc, loc.getX(loc) + vector.getX(vector), loc.getY(loc) + vector.getY(vector), loc.getZ(loc) + vector.getZ(vector));
  69. check = loc.trace(other_loc, look[0], look[1], look[2], 60, last);
  70. if(check) {
  71. list.add(list2, other_loc);
  72. }
  73. }
  74. list = list2;
  75. iter = list.iterator(list);
  76. while(hasNext(iter)) {
  77. block.setState(next(iter), state, true);
  78. }
  79. lowest_x = 5000000;
  80. lowest_y = 5000000;
  81. lowest_z = 5000000;
  82. highest_x = -5000000;
  83. highest_y = -5000000;
  84. highest_z = -5000000;
  85. iter = list.iterator(list);
  86. while(hasNext(iter)) {
  87. set_loc = next(iter);
  88. x = loc.getX(set_loc);
  89. y = loc.getY(set_loc);
  90. z = loc.getZ(set_loc);
  91. lowest_x = math.min(x, lowest_x);
  92. lowest_y = math.min(y, lowest_y);
  93. lowest_z = math.min(z, lowest_z);
  94. highest_x = math.max(x, highest_x);
  95. highest_y = math.max(y, highest_y);
  96. highest_z = math.max(z, highest_z);
  97. }
  98. lowest_x--;
  99. lowest_y--;
  100. lowest_z--;
  101. highest_x++;
  102. highest_y++;
  103. highest_z++;
  104. for(k = 0; k < 5; k++) {
  105. iter = loc.iterator(world, lowest_x, lowest_y, lowest_z, highest_x, highest_y, highest_z);
  106. while(hasNext(iter)) {
  107. smooth_loc = next(iter);
  108. //neighbours includes the original block
  109. neighbours = 19 - block.countAir(smooth_loc);
  110. if(neighbours <= 8) {
  111. block.setState(smooth_loc, air_state, true);
  112. } elseif(neighbours >= 12) {
  113. block.setState(smooth_loc, block_state, true);
  114. }
  115. }
  116. }
  117. goto("wait");
  118. @dosmooth
  119. loc = entity.getLocation(player);
  120. world = loc.getWorld(loc);
  121. look = entity.getLook(player);
  122. loc.setX(loc, loc.getCoord(loc, "bx"));
  123. loc.setY(loc, loc.getCoord(loc, "by") + 1);
  124. loc.setZ(loc, loc.getCoord(loc, "bz"));
  125. list = list.new();
  126. radius = map.getOrDefault(radius_per_player, player.getUuid(player), 5);
  127. for(y = -radius; y <= radius; y += 0.5) {
  128. for(x = -radius; x <= radius; x += 0.5) {
  129. other_loc = loc.mod(loc, x, y, 0);
  130. if(loc.distance(loc, other_loc) < radius) {
  131. list.add(list, other_loc);
  132. }
  133. }
  134. }
  135. yaw = loc.getYaw(loc);
  136. pitch = loc.getPitch(loc);
  137. while(yaw < 0) {
  138. yaw += 360;
  139. }
  140. while(yaw >= 360) {
  141. yaw -= 360;
  142. }
  143. yaw = yaw * math.pi() / 180;
  144. pitch = pitch * math.pi() / 180;
  145. matrix = matrix.newRotationY(-yaw);
  146. matrix = matrix.mul(matrix, matrix.newRotationX(-pitch));
  147. list2 = list.new();
  148. vector = vector.new(0, 0, 0);
  149. iter = list.iterator(list);
  150. while(hasNext(iter)) {
  151. other_loc = next(iter);
  152. vector.set(vector, loc.getX(other_loc) - loc.getX(loc), loc.getY(other_loc) - loc.getY(loc), loc.getZ(other_loc) - loc.getZ(loc));
  153. matrix.mulVector(matrix, vector);
  154. loc.set(other_loc, loc.getX(loc) + vector.getX(vector), loc.getY(loc) + vector.getY(vector), loc.getZ(loc) + vector.getZ(vector));
  155. check = loc.trace(other_loc, look[0], look[1], look[2], 60, last);
  156. if(check) {
  157. list.add(list2, other_loc);
  158. }
  159. }
  160. list = list2;
  161. lowest_x = 5000000;
  162. lowest_y = 5000000;
  163. lowest_z = 5000000;
  164. highest_x = -5000000;
  165. highest_y = -5000000;
  166. highest_z = -5000000;
  167. iter = list.iterator(list);
  168. while(hasNext(iter)) {
  169. set_loc = next(iter);
  170. x = loc.getX(set_loc);
  171. y = loc.getY(set_loc);
  172. z = loc.getZ(set_loc);
  173. lowest_x = math.min(x, lowest_x);
  174. lowest_y = math.min(y, lowest_y);
  175. lowest_z = math.min(z, lowest_z);
  176. highest_x = math.max(x, highest_x);
  177. highest_y = math.max(y, highest_y);
  178. highest_z = math.max(z, highest_z);
  179. }
  180. lowest_x--;
  181. lowest_y--;
  182. lowest_z--;
  183. highest_x++;
  184. highest_y++;
  185. highest_z++;
  186. for(k = 0; k < 5; k++) {
  187. iter = loc.iterator(world, lowest_x, lowest_y, lowest_z, highest_x, highest_y, highest_z);
  188. while(hasNext(iter)) {
  189. smooth_loc = next(iter);
  190. //neighbours includes the original block
  191. neighbours = 19 - block.countAir(smooth_loc);
  192. if(neighbours <= 9) {
  193. block.setState(smooth_loc, air_state, true);
  194. } elseif(neighbours >= 11) {
  195. block.setState(smooth_loc, block_state, true);
  196. }
  197. }
  198. }
  199. goto("wait");