Advertisement
TraerAlone

Untitled

Jul 26th, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local cmp = require('component')
  2. local event = require('event')
  3. local serialization = require('serialization')
  4. local gpu=cmp.gpu
  5. local filesystem=require('filesystem')
  6. local term =require('term')
  7. local A,B = gpu.getResolution()
  8. local oldBac=gpu.getBackground()
  9. local oldFore = gpu.getForeground()
  10. --local testFile='../maps/'..MapsName
  11. local testFile='../test'
  12. local unicode = require('unicode')
  13. local shell=require('shell')
  14. term.clear()
  15.  
  16.  
  17.  
  18. function colorSelector(txt)
  19.   buttonsSelector={
  20.   {53,20,1,1}, --<settings>
  21.   {54,20,1,1},
  22.   {55,20,1,1},
  23.   {56,20,1,1},
  24.   {57,20,1,1},
  25.   {58,20,1,1},
  26.   {53,22,1,1},
  27.   {54,22,1,1},
  28.   {55,22,1,1},
  29.   {56,22,1,1},
  30.   {57,22,1,1},
  31.   {58,22,1,1},
  32.   {64,21,20,3} --</settings>
  33. }
  34.   local colorPos1 = 0
  35.   local colorPos2 = 0
  36.   local colorPos3 = 0
  37.   local colorPos4 = 0
  38.   local colorPos5 = 0
  39.   local colorPos6 = 0
  40.   local function F16(n)
  41.     N=nil
  42.     if n > 9 then
  43.     if n==10 then
  44.       N = 'A'
  45.     elseif n == 11 then
  46.       N = 'B'
  47.     elseif n == 12 then
  48.       N = 'C'
  49.     elseif n == 13 then
  50.       N = 'D'
  51.     elseif n == 14 then
  52.       N = 'E'
  53.     elseif n == 15 then
  54.       N = 'F'
  55.     else N = n
  56.     end
  57.   end
  58.   if N == nil then N=n end
  59.     return N
  60.   end
  61.   drawField(30,15,60,11,' ',0x505050,0x505050)
  62.   drawClound('┌','┐','└','┘','│','─',30,15,60,11,0x505050,0xFFFFFF)
  63.   drawText(40,17,40,txt,0x505050,0xFFFFFF)
  64.   gpu.setBackground(0)
  65.   gpu.set(52,21,'0x')
  66.   gpu.set(54,21,'000000')
  67.   drawField(47,21,2,1,' ',0,0x505050)
  68.   drawClound('┌','┐','└','┘','│','─',46,20,4,3,0x505050,0xFFFFFF)
  69.   gpu.setBackground(0x505050)
  70.   gpu.setForeground(0xFFAA00)
  71.   gpu.set(54,20,'▲▲▲▲▲▲')
  72.   gpu.set(54,22,'▼▼▼▼▼▼')
  73.   gpu.setForeground(0xFFFFFF)
  74.   gpu.setBackground(0x898989)
  75.   gpu.fill(64,20,20,3,' ')
  76.   gpu.set(73,21,'OK')
  77.   gpu.setBackground(0x505050)
  78.   gpu.setBackground(oldBac)
  79.   newColor=0
  80. while true do
  81.   local key,posSettingMenu,clickMouse,name = getRawEvent(buttonsSelector)
  82.   if key == 'touch' then
  83.   if posSettingMenu > 0 and posSettingMenu < 13 then
  84.     if posSettingMenu == 1 then
  85.       colorPos1 = colorPos1+1
  86.     elseif posSettingMenu == 2 then
  87.       colorPos2 = colorPos2+1
  88.     elseif posSettingMenu == 3 then
  89.       colorPos3 = colorPos3 + 1
  90.     elseif posSettingMenu == 4 then
  91.       colorPos4 = colorPos4 + 1
  92.     elseif posSettingMenu == 5 then
  93.       colorPos5 = colorPos5 + 1
  94.     elseif posSettingMenu == 6 then
  95.       colorPos6 = colorPos6 + 1
  96.     elseif posSettingMenu == 7 then
  97.       colorPos1 = colorPos1-1
  98.     elseif posSettingMenu == 8 then
  99.       colorPos2 = colorPos2-1
  100.     elseif posSettingMenu == 9 then
  101.       colorPos3 = colorPos3 - 1
  102.     elseif posSettingMenu == 10 then
  103.       colorPos4 = colorPos4 - 1
  104.     elseif posSettingMenu == 11 then
  105.       colorPos5 = colorPos5 - 1
  106.     elseif posSettingMenu == 12 then
  107.       colorPos6 = colorPos6 - 1
  108.     end
  109.     if colorPos1 > 15 then
  110.       colorPos1 = 15
  111.     elseif colorPos2 > 15 then
  112.       colorPos2 = 15
  113.     elseif colorPos3 > 15 then
  114.       colorPos3 = 15
  115.     elseif colorPos4 > 15 then
  116.       colorPos4 = 15
  117.     elseif colorPos5 > 15 then
  118.      colorPos5 = 15
  119.     elseif colorPos6 > 15 then
  120.       colorPos6 = 15
  121.     end
  122.     if colorPos1 < 0 then
  123.       colorPos1 = 0
  124.     elseif colorPos2 < 0 then
  125.       colorPos2 = 0
  126.     elseif colorPos3 < 0 then
  127.       colorPos3 = 0
  128.     elseif colorPos4 < 0 then
  129.       colorPos4 = 0
  130.     elseif colorPos5 < 0 then
  131.       colorPos5 = 0
  132.     elseif colorPos6 < 0 then
  133.       colorPos6 = 0
  134.     end
  135.     local F1=F16(colorPos1)
  136.     local F2=F16(colorPos2)
  137.     local F3=F16(colorPos3)
  138.     local F4=F16(colorPos4)
  139.     local F5=F16(colorPos5)
  140.     local F6=F16(colorPos6)
  141.     gpu.setBackground(0)
  142.     gpu.set(54,21,tostring(F1..F2..F3..F4..F5..F6))
  143.     newColor=tonumber('0x'..F1..F2..F3..F4..F5..F6)
  144.     drawField(47,21,2,1,' ',newColor,0x505050)
  145.     drawClound('┌','┐','└','┘','│','─',40,46,4,3,0x505050,0xFFFFFF)
  146.     gpu.setBackground(0x505050)
  147.   gpu.setBackground(oldBac)
  148.     end
  149.   if posSettingMenu == 13 then
  150.    gpu.setBackground(oldBac)
  151.   term.clear()
  152.  -- map=loadTbl(testFile)
  153.     break
  154.   end
  155.   end
  156.   end
  157.   gpu.setBackground(oldBac)
  158.   return newColor
  159. end
  160.  
  161. local function drawButtonAddMenu(arr,i)
  162.     drawField(arr[i][1],arr[i][2],arr[i][3],1,'▄',arr[i][4],arr[i][5])
  163.     drawField(arr[i][1],arr[i][2]+1,arr[i][3],1,' ',arr[i][5],arr[i][5])
  164.     drawField(arr[i][1],arr[i][2]+2,arr[i][3],1,'▄',arr[i][5],arr[i][4])
  165. end
  166.  
  167. function getRawEvent(arr)
  168.   local N = nil
  169.   local key,_,x,y,clickMouse,name=event.pull()
  170.   for i=1,#arr do
  171.     if x > arr[i][1]-1 and x < arr[i][1]+arr[i][3]+1 and y > arr[i][2]-1 and y < arr[i][2]+arr[i][4]+1 then
  172.       N=i
  173.     break
  174.   end
  175. end
  176.   if N == nil then
  177.     N = 0
  178.   end
  179.   return key,N,clickMouse,name  
  180. end
  181.  
  182. function drawText(x,y,leght,txt,col_back,col_fore)
  183.   local oldB=gpu.getBackground()
  184.   local oldF=gpu.getForeground()
  185.   local txt = tostring(txt)
  186.   gpu.setBackground(col_back)
  187.   gpu.setForeground(col_fore)
  188.   if unicode.len(txt) > leght then
  189.     txt=unicode.sub(txt,0,leght)
  190.   end
  191.   gpu.set(x+math.floor((leght-unicode.len(txt))/2),y,txt)
  192.   gpu.setBackground(oldBac)
  193.   gpu.setForeground(oldF)
  194. end
  195.  
  196. function drawClound(s,s2,s3,s4,s5,s6,x,y,w,h,c1,c2)  
  197.   oldFore=gpu.getForeground()
  198.   oldBack=gpu.getBackground()
  199.   gpu.setBackground(c1)
  200.   gpu.setForeground(c2)
  201.   gpu.set(x,y,s)
  202.   gpu.fill(x+1,y,w-1,1,s6)
  203.   gpu.set(x,y+h-1,s3)
  204.   gpu.fill(x,y+1,1,h-2,s5)
  205.   gpu.fill(x+w-1,y+1,1,h-2,s5)
  206.   gpu.set(x+w-1,y,s2)
  207.   gpu.fill(x+1,y+h-1,w-2,1,s6)
  208.   gpu.set(x+w-1,y+h-1,s4)
  209.   gpu.setBackground(oldBac)
  210.   gpu.setForeground(oldFore)
  211. end
  212.  
  213. function drawField(x,y,w,h,s,c1,c2)
  214.   oldBack=gpu.getBackground()
  215.   oldFore=gpu.getForeground()
  216.   gpu.setBackground(c1)
  217.   gpu.setForeground(c2)
  218.   gpu.fill(x,y,w,h,s)
  219.   gpu.setBackground(oldBac)
  220.   gpu.setForeground(oldFore)
  221. end
  222.  
  223. function getxy()
  224.   local key,_,x,y,clickMouse,name=event.pull()
  225.   return key,x,y,clickMouse
  226. end
  227. local function saveTbl(tbl, fl)
  228.   file = io.open(fl, 'w')
  229.   file:write(serialization.serialize(tbl))
  230.   file:close()
  231. end
  232. local function loadTbl(fl)
  233.   file = io.open(fl, 'r')
  234.   if not file then
  235.     file = io.open(fl, 'w')
  236.     file:write('{}')
  237.     return {} else
  238.     return serialization.unserialize(file:read('*a'))
  239.   end
  240.   file:close()
  241. end
  242.  
  243. -------
  244. map=loadTbl(testFile)
  245. -------
  246.  
  247. function checkBrick(arr,x,y)
  248.   N=nil
  249.   for i=1,#arr do
  250.     if x > arr[i][1]-1 and x < arr[i][1]+arr[i][3]+1 and y > arr[i][2]-1 and y < arr[i][2]+arr[i][4]+1 then
  251.       N=i
  252.       break
  253.     end
  254.   end
  255.   if N == nil then
  256.     N=0
  257.   end
  258.   return N
  259. end
  260.  
  261. function drawAllBrick()
  262. map=loadTbl(testFile)
  263. for i=1,#map do
  264. drawBrick(map,i)
  265. end
  266. end
  267.  
  268. function drawBrick(arr,N)
  269.   gpu.setBackground(arr[N][5])
  270.   gpu.fill(arr[N][1],arr[N][2],arr[N][3],arr[N][4],' ')
  271.   gpu.setBackground(oldBac)
  272. end
  273. map=loadTbl(testFile)
  274. drawAllBrick()
  275. arrBonus={'empty','double_damage','freeze_speed_ball','double_ball'}
  276. function drawObjectSettings(N)
  277. if map[N] ~= nil then
  278. durability=map[N][6]
  279. brickColor=map[N][5]
  280. bonus=1
  281. buttonsSetting={
  282. {32,6,6,0x505050,0}, -- dur field
  283. {17,9,34,0x505050,0}, -- bonus field
  284. {25,6,5,0x505050,0xFFAA00}, -- - dur
  285. {40,6,5,0x505050,0xFFAA00}, -- + dur
  286. {30,14,24,0x505050,0xAA0000}, -- del
  287. {30,17,24,0x505050,123123}, -- OK
  288. }
  289. buttonsFrameBrickSettings={
  290. {17,9,34,3}, -- bonus field
  291. {25,6,5,3}, -- - dur
  292. {40,6,5,3}, -- + dur
  293. {30,14,24,3}, -- del
  294. {30,17,24,3}, -- OK
  295. {10,14,14,7} -- color selector
  296. }
  297. function drawObjSettings(N)
  298. drawField(3,3,60,20,' ',0x505050,0x505050)
  299. drawClound('┌','┐','└','┘','│','─',3,3,60,20,0x505050,0xFFFFFF)
  300. drawText(4,3,58,'[Свойства кирпича]',0x505050,0xFFFFFF)
  301. for i=1,6 do
  302. drawButtonAddMenu(buttonsSetting,i)
  303. end
  304. drawText(17,10,34,arrBonus[bonus],0,0xFFFFFF)
  305. drawText(32,7,6,durability,0,0xFFFFFF)
  306. gpu.setBackground(0x505050)
  307. gpu.setForeground(0xFFFFFF)
  308. gpu.set(10,7,'Прочность: ')
  309. gpu.set(10,10,'Бонус: ')
  310. drawText(30,15,24,'Удалить кирпич',0xAA0000,0xFFFFFF)
  311. drawText(30,18,24,'Принять',123123,0xFFFFFF)
  312. gpu.setBackground(0xFFAA00)
  313. gpu.set(27,7,'-')
  314. gpu.set(42,7,'+')
  315. gpu.setBackground(0x505050)
  316. drawClound('┌','┐','└','┘','│','─',10,14,14,7,0x505050,0xFFFFFF)
  317. drawField(11,15,12,5,' ',brickColor,0x505050)
  318. drawText(10,14,14,'[Цвет]',0x505050,0xFFFFFF)
  319. gpu.setBackground(oldBac)
  320. end
  321. drawObjSettings(N)
  322.   while true do  
  323.     local key,buttonInBrickFrame,clickMouse,name = getRawEvent(buttonsFrameBrickSettings)
  324.     if key == 'touch' then
  325.       if buttonInBrickFrame==1 then
  326.       bonus=bonus+1
  327.       if bonus>#arrBonus then
  328.         bonus=bonus-#arrBonus
  329.       end
  330.       drawField(17,10,34,1,' ',0,0x505050)
  331.       drawText(17,10,34,arrBonus[bonus],0,0xFFFFFF)
  332.       elseif buttonInBrickFrame==2 then
  333.       durability=durability-1
  334.     if durability < 0 then
  335.       durability=0
  336.     end
  337.     drawField(32,7,6,1,' ',0,0x505050)
  338.     drawText(32,7,6,durability,0,0xFFFFFF)
  339.     elseif buttonInBrickFrame==3 then
  340.       durability=durability+1
  341.     if durability > 9999 then
  342.       durability=9999
  343.     end
  344.     drawField(32,7,6,1,' ',0,0x505050)
  345.     drawText(32,7,6,durability,0,0xFFFFFF)
  346.     elseif buttonInBrickFrame==4 then
  347.     fakeMap={}
  348.     k=0
  349.       for i=1,#map do
  350.       if i~=N then
  351.       k=k+1
  352.         fakeMap[k]={map[i][1],map[i][2],map[i][3],map[i][4],map[i][5],map[i][6],map[i][7]}
  353.       end
  354.     end
  355.     shell.execute('del '..testFile)
  356.     saveTbl(fakeMap,testFile)
  357.     map=loadTbl(testFile)
  358.     gpu.setBackground(oldBac)
  359.     term.clear()
  360.     drawAllBrick()
  361.    break
  362.       elseif buttonInBrickFrame==5 then
  363.         map[N]={map[N][1],map[N][2],map[N][3],map[N][4],brickColor,durability,arrBonus[bonus]}
  364.     saveTbl(map,testFile)
  365.     gpu.setBackground(oldBac)
  366.     term.clear()
  367.     map=loadTbl(testFile)
  368.     for i=1,#map do
  369.       drawBrick(map,i)
  370.     end
  371.     break
  372.     elseif buttonInBrickFrame==6 then
  373.       brickColor=colorSelector('Выберете цвет для данного кирпича:')
  374.       drawObjSettings(N)
  375.     end
  376.     end
  377.   end
  378.   end
  379. end
  380.  
  381. while true do
  382.     drawAllBrick()
  383.     local key,x,y,mouseClick=getxy()
  384.     if key == 'touch' then
  385.     click=nil
  386.       if #map~=nil then
  387.         click=checkBrick(map,x,y)
  388.       end
  389.       if click~=nil and click > 0 and #map>0 then
  390.         drawObjectSettings(click)
  391.       else
  392.       gpu.setForeground(0xFFFFFF)
  393.     if mouseClick==0 then
  394.       gpu.setForeground(0xAA0000)
  395.       gpu.set(x,y,'•')
  396.       gpu.setForeground(0xFFFFFF)
  397.     if FirstPosition ~= nil then
  398.       gpu.setForeground(0xAA0000)
  399.       gpu.set(FirstPosition[1],FirstPosition[2],'•')
  400.     end  
  401.     if FirstPosition~=nil and FirstPosition[1] ~= x and FirstPosition[2] ~= y then
  402.         if FirstPosition ~= nil then
  403.           gpu.set(FirstPosition[1],FirstPosition[2],' ')
  404.         end
  405.     end
  406.       FirstPosition={x,y}
  407.     elseif mouseClick==1 then
  408.       gpu.setForeground(0x00AA00)
  409.       gpu.set(x,y,'•')
  410.       gpu.setForeground(0xFFFFFF)
  411.    if SecondPosition~= nil then
  412.       gpu.setForeground(0x00AA00)
  413.       gpu.set(SecondPosition[1],SecondPosition[2],'•')
  414.     end
  415.     if SecondPosition~=nil and SecondPosition[1]~=x and SecondPosition[2] ~= y then
  416.         if SecondPosition ~= nil then
  417.           gpu.set(SecondPosition[1],SecondPosition[2],' ')
  418.         end
  419.     end
  420.       SecondPosition={x,y}
  421.     end
  422.   end
  423.   elseif key == 'key_up' then
  424.     if FirstPosition~=nil and SecondPosition ~= nil then
  425.     if y == 28 then
  426.       leght=FirstPosition[1]-SecondPosition[1]
  427.       weight=FirstPosition[2]-SecondPosition[2]
  428.       if leght < 0 then
  429.         leght=leght*(-1)
  430.       end
  431.       if weight < 0 then
  432.         weight=weight*(-1)
  433.       end
  434.       if FirstPosition[1] > SecondPosition[1] then
  435.         mapX=SecondPosition[1]
  436.       else
  437.         mapX=FirstPosition[1]
  438.       end
  439.       if FirstPosition[2] > SecondPosition[2] then
  440.         mapY=SecondPosition[2]
  441.       else
  442.         mapY=FirstPosition[2]
  443.       end
  444.       map[#map+1] = {mapX,mapY,leght+1,weight+1,math.random(100000,16000000),1,'empty'}
  445.     shell.execute('del '..testFile)
  446.       saveTbl(map,testFile)
  447.       FirstPosition=nil
  448.       SecondPosition=nil
  449.     end  
  450. end
  451.   end
  452.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement