portal_gun.txt 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. event.load("block_click");
  2. event.load("player_move");
  3. Reach = 25;
  4. Portal_Surface_Mat = material.get("IRON_BLOCK");
  5. Portal_Mat = material.get("GLOW_LICHEN");
  6. air_mat = material.get("AIR");
  7. left_portal_particle = particle.get("ELECTRIC_SPARK");
  8. right_portal_particle = particle.get("GLOW");
  9. Existing_Portals = map.new(); // right/left, id of player_move
  10. Portal_Locs = map.new(); // right/left, locs as array
  11. portals = map.new(); // id, portal.create(in_mid_loc, out_mid_loc, facing_in, facing_out));
  12. Portal_directions = map.new(); // right/left, direction as string
  13. @wait
  14. wait();
  15. if(event == "block_click"){
  16. if(player.getName(player) != "SirTerence7" && player.getName(player) != "Mareeeen"){
  17. goto("wait");
  18. }
  19. if(action == "PHYSICAL"){
  20. goto("wait");
  21. }
  22. player_hand = living.getHand(player);
  23. if(!item.hasName(player_hand)){
  24. goto("wait");
  25. }
  26. item_name = string.text(item.getName(player_hand));
  27. if(item_name != "Portal Gun"){
  28. goto("wait");
  29. }
  30. loc = entity.getLocation(player);
  31. look = entity.getLook(player);
  32. if(player.isSneaking(player)){
  33. shoot_loc = loc.mod(loc, 0, 1.25, 0);
  34. }else{
  35. shoot_loc = loc.mod(loc, 0, 1.55, 0);
  36. }
  37. portal.shoot(action, shoot_loc, look);
  38. cancel = true;
  39. goto("wait");
  40. }
  41. if(event == "player_move"){
  42. if(!map.contains(portals, id)){
  43. goto("wait");
  44. }
  45. portal.enter(player, id);
  46. }
  47. goto("wait");
  48. function portal.shoot(action, loc, look){
  49. if(action == "LEFT_CLICK_AIR" || action == "LEFT_CLICK_BLOCK"){
  50. portal_to_place = "left";
  51. portal_particle = $left_portal_particle;
  52. }elseif(action == "RIGHT_CLICK_AIR" || action == "RIGHT_CLICK_BLOCK"){
  53. portal_to_place = "right";
  54. portal_particle = $right_portal_particle;
  55. }else{
  56. return;
  57. }
  58. Yaw = loc.getYaw(loc);
  59. for(a = 0; a < $Reach * 3; a++){
  60. new_loc = loc.mod(loc, a*look[0]/3, a*look[1]/3, a*look[2]/3);
  61. new_block = block.get(new_loc);
  62. new_mat = block.getType(new_block);
  63. particle.spawn(new_loc, portal_particle, 1, 0, 0, 0, 0);
  64. if(material.isSolid(new_mat)){
  65. if(new_mat == $Portal_Surface_Mat){
  66. portal.place(new_loc, loc.mod(new_loc, -look[0]/3, -look[1]/3, -look[2]/3), portal_to_place, Yaw);
  67. break;
  68. }else{
  69. break;
  70. }
  71. }
  72. }
  73. return;
  74. }
  75. function portal.place(to_loc, from_loc, portal_to_place, yaw){
  76. block_loc = block.getLocation(block.get(to_loc));
  77. direction = portal.getDirection(block_loc, from_loc);
  78. if(direction == "EAST" || direction == "WEST" || direction == "SOUTH" || direction == "NORTH"){
  79. mid_Y = loc.getY(block_loc) + 0.5;
  80. to_Y = loc.getY(from_loc);
  81. if(mid_Y - to_Y < 0){
  82. block2_loc = loc.mod(block_loc, 0, 1, 0);
  83. }else{
  84. block2_loc = loc.mod(block_loc, 0, -1, 0);
  85. }
  86. }else{
  87. yaw = yaw + 180;
  88. if((yaw > 60 && yaw < 120) || (yaw > 240 && yaw < 300)){
  89. mid_X = loc.getX(block_loc) + 0.5;
  90. to_X = loc.getX(from_loc);
  91. if(mid_X - to_X < 0){
  92. block2_loc = loc.mod(block_loc, 1, 0, 0);
  93. }else{
  94. block2_loc = loc.mod(block_loc, -1, 0, 0);
  95. }
  96. }else{
  97. mid_Z = loc.getZ(block_loc) + 0.5;
  98. to_Z = loc.getZ(from_loc);
  99. if(mid_Z - to_Z < 0){
  100. block2_loc = loc.mod(block_loc, 0, 0, 1);
  101. }else{
  102. block2_loc = loc.mod(block_loc, 0, 0, -1);
  103. }
  104. }
  105. }
  106. if(block.getType(block.get(block2_loc)) != $Portal_Surface_Mat){
  107. return;
  108. }
  109. to_surface_array = direction_array(direction);
  110. portal_loc1 = loc.mod(block_loc, to_surface_array[0], to_surface_array[1], to_surface_array[2]);
  111. portal_loc2 = loc.mod(block2_loc, to_surface_array[0], to_surface_array[1], to_surface_array[2]);
  112. portal_block1 = block.get(portal_loc1);
  113. portal_block2 = block.get(portal_loc2);
  114. portal_1_mat = block.getType(portal_block1);
  115. portal_2_mat = block.getType(portal_block2);
  116. if(portal_1_mat != $air_mat){
  117. if(portal_1_mat != $Portal_Mat){
  118. return;
  119. }
  120. Other_Portal_Locs_Array = map.get($Portal_Locs, portal.getOther(portal_to_place));
  121. if(Other_Portal_Locs_Array != null){
  122. if(Other_Portal_Locs_Array[0] == portal_loc1 || Other_Portal_Locs_Array[1] == portal_loc1){
  123. return;
  124. }
  125. }
  126. }
  127. if(portal_2_mat != $air_mat){
  128. if(portal_2_mat != $Portal_Mat){
  129. return;
  130. }
  131. Other_Portal_Locs_Array = map.get($Portal_Locs, portal.getOther(portal_to_place));
  132. if(Other_Portal_Locs_Array != null){
  133. if(Other_Portal_Locs_Array[0] == portal_loc2 || Other_Portal_Locs_Array[1] == portal_loc2){
  134. return;
  135. }
  136. }
  137. }
  138. if(map.contains($Portal_Locs, portal_to_place)){
  139. Portal_array = map.get($Portal_Locs, portal_to_place);
  140. block.setMaterial(block.get(Portal_array[0]), $air_mat);
  141. block.setMaterial(block.get(Portal_array[1]), $air_mat);
  142. }
  143. block.setMaterial(portal_block1, $Portal_Mat);
  144. block.setMaterial(portal_block2, $Portal_Mat);
  145. //block.setDirectionalFace(portal_block1, direction);
  146. //block.setDirectionalFace(portal_block2, direction);
  147. if(map.contains($Existing_Portals, portal_to_place)){
  148. id_to_remove = map.get($Existing_Portals, portal_to_place);
  149. event.removeMoveData(id_to_remove);
  150. }
  151. in_mid_loc = loc.getMid(portal_loc1, portal_loc2);
  152. new_id = event.addMoveData(loc.mod(in_mid_loc, -0.25, -0.25, -0.25), loc.mod(in_mid_loc, 0.25, 0.25, 0.25), 5, -1, $player);
  153. map.add($Existing_Portals, portal_to_place, new_id);
  154. Portal_array = array.new(2);
  155. Portal_array[0] = portal_loc1;
  156. Portal_array[1] = portal_loc2;
  157. map.add($Portal_Locs, portal_to_place, Portal_array);
  158. map.add($Portal_directions, portal_to_place, direction);
  159. if(map.getSize($Existing_Portals) > 1){
  160. map.clear($portals);// id, portal.create(in_mid_loc, out_mid_loc, facing_in, facing_out));
  161. other_portal = portal.getOther(portal_to_place);
  162. Other_Portal_array = map.get($Portal_Locs, other_portal);
  163. out_mid_loc = loc.getMid(Other_Portal_array[0], Other_Portal_array[1]);
  164. oth_id = map.get($Existing_Portals, other_portal);
  165. map.add($portals, new_id, portal.create(out_mid_loc, in_mid_loc, direction, map.get($Portal_directions, other_portal)));
  166. map.add($portals, oth_id, portal.create(in_mid_loc, out_mid_loc, map.get($Portal_directions, other_portal), direction));
  167. }
  168. }
  169. function portal.getDirection(block_loc, from_loc){
  170. X = loc.getBlockX(from_loc) - loc.getX(block_loc);
  171. Y = loc.getBlockY(from_loc) - loc.getY(block_loc);
  172. Z = loc.getBlockZ(from_loc) - loc.getZ(block_loc);
  173. if(X == -1){
  174. return("EAST");
  175. }elseif(X == 1){
  176. return("WEST");
  177. }elseif(Y == 1){
  178. return("DOWN");
  179. }elseif(Y == -1){
  180. return("UP");
  181. }elseif(Z == -1){
  182. return("SOUTH");
  183. }elseif(Z == 1){
  184. return("NORTH");
  185. }
  186. }
  187. function direction_array(direction){
  188. array = array.new(3);
  189. if(direction == "UP"){
  190. array[0] = 0;
  191. array[1] = -1;
  192. array[2] = 0;
  193. }elseif(direction == "DOWN"){
  194. array[0] = 0;
  195. array[1] = 1;
  196. array[2] = 0;
  197. }elseif(direction == "EAST"){
  198. array[0] = -1;
  199. array[1] = 0;
  200. array[2] = 0;
  201. }elseif(direction == "WEST"){
  202. array[0] = 1;
  203. array[1] = 0;
  204. array[2] = 0;
  205. }elseif(direction == "SOUTH"){
  206. array[0] = 0;
  207. array[1] = 0;
  208. array[2] = -1;
  209. }elseif(direction == "NORTH"){
  210. array[0] = 0;
  211. array[1] = 0;
  212. array[2] = 1;
  213. }
  214. return(array);
  215. }
  216. function portal.getOther(Portal){
  217. if(Portal == "left"){
  218. return "right";
  219. }else{
  220. return "left";
  221. }
  222. }
  223. function portal.create(to_loc, mid_loc, Direction_in, Direction_out){
  224. array = array.new(4);
  225. array[0] = Direction_in;//east // south
  226. array[1] = Direction_out;//east -> +X, west -> -X, south -> +Z, north -> -Z
  227. array[2] = to_loc;
  228. array[3] = mid_loc;
  229. return array;
  230. }
  231. /*
  232. portal.enter muss umstrukturiert werden zur Inkulsion von UP und DOWN -> get und setlook verwenden
  233. */
  234. function portal.enter(player, id){
  235. array = map.get($portals, id);
  236. Direction_in = array[0];
  237. Direction_out = array[1];
  238. to_loc = array[2];
  239. mid_loc = array[3];
  240. player_loc = entity.getLocation(player);
  241. player_yaw = loc.getYaw(player_loc);
  242. if(Direction_in == "EAST" || Direction_in == "WEST"){
  243. player_X = loc.getX(player_loc);
  244. mid_X = loc.getX(mid_loc);
  245. dif = mid_X - player_X;
  246. rel_yaw = player_yaw+90;
  247. }
  248. elseif(Direction_in == "SOUTH" || Direction_in == "NORTH"){
  249. player_Z = loc.getZ(player_loc);
  250. mid_Z = loc.getZ(mid_loc);
  251. dif = mid_Z - player_Z;
  252. rel_yaw = player_yaw;
  253. }
  254. if(Direction_out == "EAST"){
  255. tp_loc = loc.mod(to_loc, 2*dif, 0, 0);
  256. rel_yaw += -90;
  257. }
  258. elseif(Direction_out == "SOUTH"){
  259. tp_loc = loc.mod(to_loc, 0, 0, 2*dif);
  260. }
  261. elseif(Direction_out == "WEST"){
  262. tp_loc = loc.mod(to_loc, -2*dif, 0, 0);
  263. rel_yaw += 90;
  264. }
  265. elseif(Direction_out == "NORTH"){
  266. tp_loc = loc.mod(to_loc, 0, 0, -2*dif);
  267. rel_yaw += 180;
  268. }
  269. if(rel_yaw > 180){
  270. rel_yaw -= 360;
  271. }elseif(rel_yaw <= -180){
  272. rel_yaw += 360;
  273. }
  274. loc.setYaw(tp_loc, rel_yaw);
  275. loc.setPitch(tp_loc, loc.getPitch(player_loc));
  276. entity.teleport(player, tp_loc);
  277. }
  278. function loc.getMid(loc1, loc2){
  279. loc1_world = loc.getWorld(loc1);
  280. if(loc1_world != loc.getWorld(loc2)){
  281. return null;
  282. }
  283. return loc.new(loc1_world, (loc.getX(loc1) + loc.getX(loc2))/2 + 0.5, (loc.getY(loc1) + loc.getY(loc2))/2 + 0.5, (loc.getZ(loc1) + loc.getZ(loc2))/2 + 0.5);
  284. }