Advertisement
ZNZNCOOP

boom

Jan 21st, 2015
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.72 KB | None | 0 0
  1. trm=peripheral.wrap("monitor_14")
  2. trm.setTextScale(0.5)
  3. trm1=peripheral.wrap("monitor_15")
  4. trm1.setTextScale(2)
  5. cb=peripheral.wrap("command_1")
  6. fire="top"
  7. pay="right"
  8. start="68 73 207"
  9.  
  10. --// The elements of Basic graphical utility API (Written by: Bobby Lucero) were used
  11. --// http://www.computercraft.info/forums2/index.php?/topic/20827-computercraft-graphics-library/
  12.  
  13. function pixel(x,y)
  14.     trm.setCursorPos(x,y)
  15.     trm.write(" ")
  16. end
  17.  
  18. function line( startX, startY, endX, endY ) --Shamelessly borrowed from cc internal source
  19.     startX = math.floor(startX+0.5)
  20.     startY = math.floor(startY+0.5)
  21.     endX = math.floor(endX+0.5)
  22.     endY = math.floor(endY+0.5)
  23.  
  24.     if startX == endX and startY == endY then
  25.         pixel( startX, startY )
  26.         return
  27.     end
  28.  
  29.     local minX = math.min( startX, endX )
  30.     if minX == startX then
  31.         minY = startY
  32.         maxX = endX
  33.         maxY = endY
  34.     else
  35.         minY = endY
  36.         maxX = startX
  37.         maxY = startY
  38.     end
  39.  
  40.     local xDiff = maxX - minX
  41.     local yDiff = maxY - minY
  42.  
  43.     if xDiff > math.abs(yDiff) then
  44.         local y = minY
  45.         local dy = yDiff / xDiff
  46.         for x=minX,maxX do
  47.             pixel( x, math.floor( y + 0.5 ) )
  48.             y = y + dy
  49.         end
  50.     else
  51.         local x = minX
  52.         local dx = xDiff / yDiff
  53.         if maxY >= minY then
  54.             for y=minY,maxY do
  55.                 pixel( math.floor( x + 0.5 ), y )
  56.                 x = x + dx
  57.             end
  58.         else
  59.             for y=minY,maxY,-1 do
  60.                 pixel( math.floor( x + 0.5 ), y )
  61.                 x = x - dx
  62.             end
  63.         end
  64.     end
  65. end
  66.  
  67. function fillRect(x,y,w,h)
  68.   for i = y, y+h-1 do
  69.     trm.setCursorPos(x,i)
  70.     trm.write(string.rep(" ",w))
  71.   end
  72. end
  73.  
  74. stretch=1.4
  75. function drawArc(x, y, radius, angle1, angle2)
  76.     if angle1 == angle2 then return end
  77.     if angle1 > angle2 then angle1,angle2=angle2,angle1 end
  78.     local phi = angle1
  79.     local k=1.5
  80.     local x1,y1,x2,y2
  81.     x1 = (stretch*radius * math.cos(math.rad(phi))) + x
  82.     y1 = (radius * math.sin(math.rad(phi))) + y
  83.     phi=phi+2
  84.     while phi<angle2 do
  85.       x2 = (stretch*radius * math.cos(math.rad(phi))) + x
  86.       y2 = (radius * math.sin(math.rad(phi))) + y
  87.       line(x1,y1,x2,y2)
  88.       x1,y1=x2,y2
  89.       phi=phi+2
  90.     end
  91.     x2 = (stretch*radius * math.cos(math.rad(angle2))) + x
  92.     y2 = (radius * math.sin(math.rad(angle2))) + y
  93.     line(x1,y1,x2,y2)
  94. end
  95.  
  96. function makeArea(x,y,w,h)
  97.   local area={X=x,Y=y,W=w,H=h}
  98.   area.onMouse=function(clx,cly)
  99.     return clx>=area.X and clx<area.X+area.W and cly>=area.Y and cly<area.Y+area.H
  100.   end
  101.   area.show=function(level,col1,col2)
  102.     trm.setBackgroundColor(col1)
  103.     fillRect(area.X,area.Y,area.W,area.H)
  104.     trm.setBackgroundColor(col2)
  105.     local hy=math.floor(level*(area.H)+0.5)
  106.     fillRect(area.X,area.Y+area.H-hy,area.W,hy)
  107.   end
  108.   return area
  109. end
  110.  
  111. function tostr(v)
  112.   local s=tostring(v)
  113.   if not s:find("%.") then s=s..".0" end
  114.   return s
  115. end
  116.  
  117. function boom(ax,ay,e)
  118.   ay=math.rad(ay)
  119.   ax=math.rad(ax)
  120.   local v=math.sqrt(2*e)
  121.   local vy=tostr(v*math.sin(ay))
  122.   local vx=tostr(-v*math.cos(ay)*math.sin(ax))
  123.   local vz=tostr(v*math.cos(ay)*math.cos(ax))
  124.   cb.setCommand("playsound random.explode @a ~ ~ ~")
  125.   cb.runCommand()
  126. --  print("vx=",vx," vy=",vy," vz=",vz)
  127.   cb.setCommand("summon PrimedTnt "..start.." {Motion:["..vx..","..vy..","..vz.."], Fuse:127}")
  128.   cb.runCommand()
  129.   bang=bang-1
  130.   trm1.setCursorPos(1,1)
  131.   trm1.write(' '..bang..'  ')
  132. end
  133.  
  134. w,h=trm.getSize()
  135. cx=math.floor(w/2+0.5)
  136. cy=h
  137. r=h-2.2
  138. amax=45
  139. aymax=45
  140. pwmax=5
  141. ax=0
  142. ay=0
  143. pw=2
  144. bang=0
  145. trm1.setCursorPos(1,1)
  146. trm1.write(' '..bang..'  ')
  147.  
  148. function arrow()
  149.   line(cx,cy,cx+stretch*r*math.sin(math.rad(ax)),cy-r*math.cos(math.rad(ax)))
  150. end
  151.  
  152. trm.setBackgroundColor(colors.black)
  153. trm.clear()
  154. trm.setCursorBlink(false)
  155.  
  156. horizont=makeArea(7,1,45,cy,colors.black)
  157. trm.setBackgroundColor(colors.white)
  158. drawArc(cx,cy,cy-1,-90-amax,-90+amax)
  159. trm.setBackgroundColor(colors.red)
  160. arrow()
  161.  
  162. power=makeArea(1,1,6,cy,colors.yellow)
  163. power.show((pw-1)/(pwmax-1),colors.yellow,colors.orange)
  164.  
  165. vertikal=makeArea(w-5,1,6,h,colors.green)
  166. vertikal.show(ay/aymax,colors.green,colors.lime)
  167.  
  168. while true do
  169.   event,but,mx,my=os.pullEvent()
  170. --  print(event,' ',but,' ',mx,' ',my)
  171.   if event=="mouse_click" and but==1 then
  172.     clickx=mx
  173.     clicky=my
  174.   elseif event=="mouse_drag" and but==1 then
  175.     dragx=mx-clickx
  176.     dragy=my-clicky
  177.     clickx=mx
  178.     clicky=my
  179.     if power.onMouse(mx,my) then
  180.     end
  181.     if horizont.onMouse(mx,my) then
  182.       trm.setBackgroundColor(colors.black)
  183.       arrow()
  184.       ax=ax+dragx
  185.       if ax>amax then ax=amax end
  186.       if ax<-amax then ax=-amax end
  187.       trm.setBackgroundColor(colors.red)
  188.       arrow()
  189.     end
  190.     if vertikal.onMouse(mx,my) then
  191.       ay=ay-dragy
  192.       if ay>aymax then ay=aymax end
  193.       if ay<0 then ay=0 end
  194.       vertikal.show(ay/aymax,colors.green,colors.lime)
  195.     end
  196.     if power.onMouse(mx,my) then
  197.       pw=pw-dragy/10
  198.       if pw>pwmax then pw=pwmax end
  199.       if pw<1 then pw=1 end
  200.       power.show((pw-1)/(pwmax-1),colors.yellow,colors.orange)
  201.     end
  202.   elseif event=="monitor_touch" then
  203.     if onDrag then
  204.       os.queueEvent("mouse_drag",1,mx,my)
  205.     else
  206.       os.queueEvent("mouse_click",1,mx,my)
  207.     end
  208.     onDrag=true
  209.     clicktimer=os.startTimer(0.5)
  210.   elseif event=="timer" then
  211.     if but==clicktimer then
  212.       onDrag=false
  213.     elseif but==boomtimer then
  214.     end
  215.   elseif event=="redstone" then
  216.     if redstone.getInput(fire) then
  217.       boom(ax,ay,pw)
  218.     end
  219.     if redstone.getInput(pay) then
  220.       bang=bang+10
  221.       trm1.setCursorPos(1,1)
  222.       trm1.write(' '..bang..'  ')
  223.     end
  224.   end
  225. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement