laser.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. event.load("block_place");
  2. event.load("block_break");
  3. observer_mat = material.get("OBSERVER");
  4. dispenser_mat = material.get("DISPENSER");
  5. dropper_mat = material.get("DROPPER");
  6. glass_mat = material.get("GLASS");
  7. water_mat = material.get("WATER");
  8. glass_pane_mat = material.get("GLASS_PANE");
  9. air_mat = material.get("AIR");
  10. end_part = particle.get("REVERSE_PORTAL");
  11. green_part = particle.get("VILLAGER_HAPPY");
  12. laser_particle = end_part;
  13. max_length = 30; //amount of blocks a laser travels from last redirection
  14. max_redirections = 20;
  15. blocks_map = map.new();
  16. laser_list = list.new();
  17. sgoto(10, "loop");
  18. receiver_var = false;
  19. @wait
  20. wait();
  21. player_name = player.getName(player);
  22. if(player_name != "SirTerence7"){
  23. goto("wait");
  24. }
  25. if(event == "block_place"){
  26. if(block.getType(block) == observer_mat){
  27. directional_string = block.getDirectionalFace(block);
  28. directional_vector = block.getFacingVector(directional_string, true);
  29. map.add(blocks_map, block, directional_vector);
  30. }
  31. }
  32. if(event == "block_break"){
  33. if(block.getType(block) == observer_mat){
  34. if(map.contains(blocks_map, block)){
  35. map.remove(blocks_map, block);
  36. }
  37. }
  38. }
  39. goto("wait");
  40. @loop
  41. iterator = map.iterator(blocks_map);
  42. while(hasnext(iterator)){
  43. laser_locs_list = list.new();
  44. receiver_var = false;
  45. iterator_element = next(iterator);
  46. loop_block = map.getKey(iterator_element);
  47. loop_direction = map.getValue(iterator_element);
  48. redirect_count = 0;
  49. @laser
  50. loop_loc = block.getLocation(loop_block);
  51. for(a = 1; a < max_length; a++){
  52. new_loc = loc.mod(loop_loc, a*loop_direction[0], a*loop_direction[1], a*loop_direction[2]);
  53. new_block = block.get(new_loc);
  54. new_mat = block.getType(new_block);
  55. if(new_mat == dispenser_mat){
  56. if(redirect_count < max_redirections){
  57. loop_block = new_block;
  58. old_direction = loop_direction;
  59. loop_direction = block.getFacingVector(block.getDirectionalFace(new_block), false);
  60. list.add(laser_locs_list, loc.mod(new_loc, 0.5 - old_direction[0]/3, 0.5 - old_direction[1]/3, 0.5 - old_direction[2]/3));
  61. list.add(laser_locs_list, loc.mod(new_loc, 0.5, 0.5, 0.5));
  62. list.add(laser_locs_list, loc.mod(new_loc, 0.5 + loop_direction[0]/3, 0.5 + loop_direction[1]/3, 0.5 + loop_direction[2]/3));
  63. if(laser.ismirrored(old_direction, loop_direction)){
  64. break;
  65. }
  66. redirect_count++;
  67. goto("laser");
  68. }else{
  69. break;
  70. }
  71. }elseif(new_mat == dropper_mat){
  72. old_direction = loop_direction;
  73. loop_direction = block.getFacingVector(block.getDirectionalFace(new_block), false);
  74. if(laser.ismirrored(old_direction, loop_direction)){
  75. receiver_var = true;
  76. break;
  77. }
  78. break;
  79. }
  80. elseif(new_mat == air_mat || new_mat == glass_mat || new_mat == glass_pane_mat || new_mat == water_mat || !material.isSolid(new_mat)){
  81. list.add(laser_locs_list, loc.mod(new_loc, 0.5 - loop_direction[0]/3, 0.5 - loop_direction[1]/3, 0.5 - loop_direction[2]/3));
  82. list.add(laser_locs_list, loc.mod(new_loc, 0.5, 0.5, 0.5));
  83. list.add(laser_locs_list, loc.mod(new_loc, 0.5 + loop_direction[0]/3, 0.5 + loop_direction[1]/3, 0.5 + loop_direction[2]/3));
  84. }
  85. else{
  86. list.add(laser_locs_list, loc.mod(new_loc, 0.5 - loop_direction[0]/3, 0.5 - loop_direction[1]/3, 0.5 - loop_direction[2]/3));
  87. break;
  88. }
  89. }
  90. laser_array = array.new(2);
  91. laser_array[0] = laser_locs_list;
  92. laser_array[1] = receiver_var;
  93. list.add(laser_list, laser_array);
  94. }
  95. iterator = iterator(laser_list);
  96. while(hasnext(iterator)){
  97. temp_laser_array = next(iterator);
  98. if(temp_laser_array[1]){//doshit: true if receiver is powered by laser
  99. laser_particle = green_part;
  100. }else{
  101. laser_particle = end_part;
  102. }
  103. iterator2 = iterator(temp_laser_array[0]);
  104. while(hasnext(iterator2)){
  105. particle.spawn(next(iterator2), laser_particle, 1, 0, 0, 0, 0);
  106. }
  107. list.clear(temp_laser_array[0]);
  108. }
  109. list.clear(laser_list);
  110. sgoto(10, "loop");
  111. goto("wait");
  112. function block.getFacingVector(direction, inverted){
  113. Vector = array.new(3);
  114. if(direction == "DOWN"){
  115. Vector[0] = 0;
  116. Vector[1] = -1;
  117. Vector[2] = 0;
  118. }elseif(direction == "UP"){
  119. Vector[0] = 0;
  120. Vector[1] = 1;
  121. Vector[2] = 0;
  122. }elseif(direction == "NORTH"){
  123. Vector[0] = 0;
  124. Vector[1] = 0;
  125. Vector[2] = -1;
  126. }elseif(direction == "SOUTH"){
  127. Vector[0] = 0;
  128. Vector[1] = 0;
  129. Vector[2] = 1;
  130. }elseif(direction == "WEST"){
  131. Vector[0] = -1;
  132. Vector[1] = 0;
  133. Vector[2] = 0;
  134. }elseif(direction == "EAST"){
  135. Vector[0] = 1;
  136. Vector[1] = 0;
  137. Vector[2] = 0;
  138. }
  139. if(inverted){
  140. Vector[0] = -1 * Vector[0];
  141. Vector[1] = -1 * Vector[1];
  142. Vector[2] = -1 * Vector[2];
  143. }
  144. return Vector;
  145. }
  146. function laser.ismirrored(old_direction, loop_direction){
  147. new_vector = array.new(3);
  148. new_vector[0] = old_direction[0] + loop_direction[0];
  149. new_vector[1] = old_direction[1] + loop_direction[1];
  150. new_vector[2] = old_direction[2] + loop_direction[2];
  151. if(new_vector[0] == 0 && new_vector[1] == 0 && new_vector[2] == 0){
  152. return true;
  153. }
  154. return false;
  155. }