Guest User

Untitled

a guest
Jan 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.48 KB | None | 0 0
  1. --TangentPaint V.0.5
  2. --Part one of the final product!
  3.  
  4. --VARIABLE SETUP--
  5. width,height=term.getSize()
  6. print(height) print(width)
  7.  
  8. colorNames=
  9. {
  10. "white",
  11. "orange",
  12. "magenta",
  13. "lightblue",
  14. "yellow",
  15. "lime",
  16. "pink",
  17. "gray",
  18. "lightgray",
  19. "cyan",
  20. "purple",
  21. "blue",
  22. "brown",
  23. "green",
  24. "red",
  25. "black"
  26. }
  27.  
  28. decHexTable={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}
  29.  
  30. canvas={}
  31. for i=1,height-1 do
  32. canvas[i]={}
  33. for j=1,width do
  34. canvas[i][j]=0
  35. end
  36. end
  37.  
  38. overlay={}
  39. for i=1,height do
  40. overlay[i]={}
  41. for j=1,width do
  42. overlay[i][j]=0
  43. end
  44. end
  45.  
  46. editorMatrix={}
  47. for i=1,height-4 do
  48. editorMatrix[i]={}
  49. for j=1,width-2 do
  50. editorMatrix[i][j]=" "
  51. end
  52. end
  53.  
  54. lastPosition={0,0}
  55. colorSelected=1
  56. alive=true
  57. needsave=false
  58. --FUNCTIONS--
  59. function drawTools()
  60. term.setBackgroundColor(colors.gray)
  61. term.setCursorPos(1,height)
  62. for i=1,width do io.write(" ") end
  63. term.setCursorPos(1,height)
  64. overlay={1,1,1,0,2,2,2,0,3,3,3,0,4,4,4}
  65. io.write("[L] [S] [M] [X]")
  66. term.setBackgroundColor(2^colorSelected-1)
  67. if colorSelected==1 then term.setTextColor(colors.black) end
  68. io.write(colorNames[colorSelected])
  69. term.setTextColor(colors.white)
  70. end
  71.  
  72.  
  73. function drawCanvas()
  74. for i=1,height-1 do
  75. for j=1,width do
  76. term.setBackgroundColor(2^canvas[i][j])
  77. io.write(" ")
  78. term.setBackgroundColor(2^0)
  79. end
  80. io.write("\n")
  81. end
  82. end
  83.  
  84. function paintLoad()
  85. term.setBackgroundColor(colors.gray)
  86. term.setCursorPos(1,height)
  87. for i=1,width do io.write(" ") end
  88. term.setCursorPos(1,height)
  89. loadName=io.read()
  90. loadData={}
  91. newCanvas={}
  92. ruleData={}
  93.  
  94. if fs.exists(loadName) then
  95. file = io.open(loadName, "r")
  96. line = file:read()
  97. while line do
  98. table.insert(loadData, line)
  99. line=file:read()
  100. end
  101. newCanvas[1]=loadData[1]
  102. height=loadData[2]+0
  103. width=loadData[3]+0
  104. for i=4,#loadData do
  105. ruleData[i-3]=loadData[i]
  106. print(ruleData[i-3])
  107. end
  108.  
  109. file:close()
  110.  
  111. for i=1,#ruleData do
  112. for j=1,string.len(ruleData[i]) do
  113. if ruleData[i]==nil then
  114. ruleData[i]=" "
  115. end
  116. editorMatrix[i][j]=string.sub(ruleData[i],j,j)
  117. end
  118. end
  119.  
  120. print(height)
  121. print(width)
  122.  
  123. print(string.len(newCanvas[1]))
  124. print(height)
  125. print(width)
  126.  
  127. for i=1,string.len(newCanvas[1]) do
  128. newCanvas[i]=string.sub(loadData[1],i,i)
  129. end
  130.  
  131. k=1
  132. for i=1,height-1 do
  133. for j=1,width do
  134. for decode=1,16 do if newCanvas[k]==decHexTable[decode] then canvas[i][j]=decode-1 end end
  135. k=k+1
  136. end
  137. end
  138. end
  139.  
  140.  
  141.  
  142.  
  143.  
  144. drawCanvas()
  145. drawTools()
  146. end
  147.  
  148. function paintSave()
  149. term.setBackgroundColor(colors.gray)
  150. term.setCursorPos(1,height)
  151. for i=1,width do io.write(" ") end
  152. term.setCursorPos(1,height)
  153. saveName=io.read()
  154.  
  155. local saveDir = saveName:sub(1, saveName:len() - fs.getName(saveName):len() )
  156. if not fs.exists(saveDir) then
  157. fs.makeDir( saveName )
  158. end
  159.  
  160. file = fs.open(saveName, "w")
  161. for i=1,height-1 do
  162. for j=1,width do
  163. index=canvas[i][j]+1
  164. file.write(decHexTable[index])
  165. end
  166. end
  167. file.write("\n"..height.."\n"..width)
  168.  
  169. for i=1,#editorMatrix do
  170. file.write("\n")
  171. for j=1,#editorMatrix[i] do
  172. file.write(editorMatrix[i][j])
  173. end
  174. end
  175. file.close()
  176. needsave=false
  177. end
  178.  
  179. function paintMenu()
  180. term.setBackgroundColor(colors.lightGray)
  181. term.setCursorPos(1,height)
  182. for i=1,width do io.write(" ") end
  183. term.setCursorPos(1,height)
  184. overlay={1,1,1,1,1,0,2,2,2,2,2,2,0,3,3,3,3,3,3,3}
  185. io.write("[TXT] [BACK] [START]")
  186.  
  187. clickedButton=menubarInteraction()
  188.  
  189. if clickedButton == 1 then
  190. textMenu()
  191. end
  192. if clickedButton == 2 then
  193. drawTools()
  194. end
  195. if clickedButton == 3 then
  196. CA_Menu()
  197. end
  198. end
  199.  
  200. function textMenu()
  201. term.setBackgroundColor(colors.gray)
  202. term.clear()
  203. term.setBackgroundColor(colors.orange)
  204. term.setCursorPos(1,1)
  205. for i=1,height do
  206. if (i==1) or (i==height) then
  207. for j=1,width do
  208. term.setCursorPos(j,i)
  209. io.write(" ")
  210. end
  211. else
  212. for k=1,width do
  213. if k==1 or k==width then
  214. term.setCursorPos(k,i)
  215. io.write(" ")
  216. end
  217. end
  218. end
  219. end
  220.  
  221. inEditor=true
  222. editorCursor={1,1}
  223. prevEditorCursor={1,1}
  224. term.setTextColor(colors.black)
  225. term.setBackgroundColor(colors.gray)
  226. term.setCursorPos(2,2) io.write("Extra save data editor. Use Ctrl to exit.")
  227. term.setCursorPos(1,3) term.setBackgroundColor(colors.orange) for i=1,width do io.write(" ") end term.setBackgroundColor(colors.gray)
  228.  
  229. function updateCursor()
  230. term.setCursorPos(prevEditorCursor[1]+1,prevEditorCursor[2]+3)
  231. term.setBackgroundColor(colors.gray)
  232. io.write(editorMatrix[prevEditorCursor[2]][prevEditorCursor[1]])
  233.  
  234. term.setBackgroundColor(colors.lightGray)
  235. term.setCursorPos(editorCursor[1]+1,editorCursor[2]+3)
  236. io.write(editorMatrix[editorCursor[2]][editorCursor[1]])
  237. term.setBackgroundColor(colors.gray)
  238. prevEditorCursor=editorCursor
  239. end
  240.  
  241. function updateEditor()
  242. for i=1,height-4 do
  243. for j=1,width-2 do
  244. term.setCursorPos(j+1,i+3)
  245. io.write(editorMatrix[i][j])
  246. end
  247. end
  248. updateCursor()
  249. end
  250. updateEditor()
  251. while inEditor do
  252. event,keyPressed=os.pullEvent()
  253. if event=="key" then
  254. if keyPressed==29 then inEditor=false end
  255. if keyPressed==keys.up then editorCursor[2]=editorCursor[2]-1 end
  256. if keyPressed==keys.down then editorCursor[2]=editorCursor[2]+1 end
  257. if keyPressed==keys.left then editorCursor[1]=editorCursor[1]-1 end
  258. if keyPressed==keys.right then editorCursor[1]=editorCursor[1]+1 end
  259. if editorCursor[1]<1 then editorCursor[1]=1 end
  260. if editorCursor[1]>(width-2) then editorCursor[1]=(width-2) end
  261. if editorCursor[2]<1 then editorCursor[2]=1 end
  262. if editorCursor[2]>(height-4) then editorCursor[2]=(height-4) end
  263.  
  264. end
  265.  
  266. if event=="char" then
  267. editorMatrix[editorCursor[2]][editorCursor[1]]=keyPressed
  268. if editorCursor[1]==(width-2) then
  269. prevEditorCursor=editorCursor
  270. editorCursor[1]=1
  271. else
  272. editorCursor[1]=editorCursor[1]+1
  273. end
  274. end
  275. updateEditor()
  276. end
  277. end
  278.  
  279. function paintExit()
  280. alive=false
  281. term.setBackgroundColor(colors.black)
  282. shell.run("clear")
  283. error()
  284. end
  285.  
  286. function screenInteract()
  287. if mouseY==height and mouseButton==1 then checkToolBar() return end
  288.  
  289. if mouseButton==2 then
  290. canvas[mouseY][mouseX]=0
  291. needSave=true
  292. end
  293.  
  294. if mouseButton==1 then
  295. canvas[mouseY][mouseX]=colorSelected-1
  296. needSave=true
  297. end
  298.  
  299. if mouseButton==3 then
  300. colorSelected=(canvas[mouseY][mouseX])+1
  301. end
  302.  
  303. if mouseButton==5 then
  304. colorSelected=colorSelected+1
  305. if colorSelected==17 then colorSelected=1 end
  306. end
  307.  
  308. if mouseButton==4 then
  309. colorSelected=colorSelected-1
  310. if colorSelected==0 then colorSelected=16 end
  311. end
  312. end
  313.  
  314. function checkToolBar()
  315. toolbarSelection=overlay[mouseX]
  316. if toolbarSelection==1 then paintLoad() end
  317. if toolbarSelection==2 then paintSave() end
  318. if toolbarSelection==3 then paintMenu() end
  319. if toolbarSelection==4 then paintExit() end
  320. end
  321.  
  322. function menubarInteraction()
  323. event,mouseX,mouseY,mouseButton=os.pullEvent("click")
  324. if mouseY==height and mouseButton==1 then
  325. return overlay[mouseX]
  326. else
  327. return 0
  328. end
  329. end
  330.  
  331. function CA_Menu()
  332. local blab="To return to edit mode, press Ctrl."
  333. local canvasHeight=#canvas
  334. local canvasWidth=#canvas[1]
  335. local moore={}
  336.  
  337. newSandbox={}
  338. for i=1,canvasHeight+2 do
  339. newSandbox[i]={}
  340. for j=1,canvasWidth+2 do
  341. newSandbox[i][j]=0
  342. end
  343. end
  344.  
  345.  
  346. sandbox={}
  347. for i=1,canvasHeight+2 do
  348. sandbox[i]={}
  349. for j=1,canvasWidth+2 do
  350. sandbox[i][j]=0
  351. end
  352. end
  353.  
  354. local x=2
  355. local y=2
  356. term.setCursorPos(1,1)
  357. term.clear()
  358. for i=1,canvasHeight do
  359. for j=1,canvasWidth do
  360. sandbox[y][x]=canvas[i][j]
  361. term.setTextColor(colors.black)
  362. term.setBackgroundColor(2^(sandbox[y][x]))
  363. io.write(" ")
  364. x=x+1
  365. end
  366. io.write("\n")
  367. x=2
  368. y=y+1
  369. end
  370.  
  371. function generate(index)
  372. term.setCursorPos(1,1)
  373. for i=2,(#sandbox)-1 do
  374. for j=2,(#sandbox[i])-1 do
  375. term.setTextColor(colors.black)
  376. term.setBackgroundColor(2^(sandbox[i][j]))
  377. io.write(" ")
  378. term.setBackgroundColor(2^(0))
  379. newSandbox[i][j]=checkRules(i,j)
  380. end
  381. io.write("\n")
  382. end
  383. sandbox=newSandbox
  384. --drawSandbox()
  385. end
  386.  
  387. function checkRules(indexa,indexb)
  388. local returnVar=0
  389. local neighbors=0
  390. local lookingFor=1
  391. moore={sandbox[indexa-1][indexb-1],sandbox[indexa][indexb-1],sandbox[indexa+1][indexb-1],sandbox[indexa+1][indexb],sandbox[indexa+1][indexb+1],sandbox[indexa][indexb+1],sandbox[indexa-1][indexb+1],sandbox[indexa-1][indexb],sandbox[indexa][indexb]}
  392. for k=1,8 do
  393. if moore[k]==lookingFor then
  394. neighbors=neighbors+1
  395. end
  396. end
  397.  
  398. if moore[9]==1 then returnVar=2 end
  399. if moore[9]==2 then returnVar=3 end
  400. if moore[9]==3 then returnVar=3 end
  401. if (neighbors == 2) and (moore[9] == 3) then returnVar=1 end
  402. if (neighbors == 1) and (moore[9] == 3) then returnVar=1 end
  403.  
  404. --if (neighbors<=1) and (moore[9] == 1 or moore[9] == 0) then returnVar=0 end
  405. --if (neighbors >= 2 and (moore[9]) == 1) then returnVar=1 end
  406. --if (neighbors == 3) and (moore[9] == 0) then returnVar=1 end
  407. --if (neighbors >= 4) and (moore[9] == 1) then returnVar=0 end
  408.  
  409. moore={0,0,0,0,0,0,0,0,0}
  410. return returnVar
  411. end
  412.  
  413. function drawSandbox()
  414. local x=2
  415. local y=2
  416. term.setCursorPos(1,1)
  417. term.clear()
  418. for i=1,height-1 do
  419. for j=1,width do
  420. term.setTextColor(colors.black)
  421. term.setBackgroundColor(2^(sandbox[y][x]))
  422. io.write(" ")
  423. x=x+1
  424. end
  425. io.write("\n")
  426. x=2
  427. y=y+1
  428. end
  429. end
  430.  
  431. term.setBackgroundColor(colors.white)
  432. term.setCursorPos(1,1)
  433. term.clear()
  434. term.setCursorPos(math.floor((width-string.len(blab))/2),math.floor(height/2))
  435. term.setTextColor(colors.black)
  436. term.setBackgroundColor(colors.gray)
  437. io.write(blab)
  438. event,keyPressed=os.pullEvent("key")
  439. term.clear()
  440. term.setBackgroundColor(colors.white)
  441. drawSandbox()
  442. inCA=true
  443. while inCA do
  444. event,keyPressed=os.pullEvent("key")
  445. if keyPressed==57 then generate(1) end
  446. if keyPressed==29 then inCA=false end
  447. if keyPressed==28 then
  448. term.setCursorPos(1,height-1)
  449. term.setTextColor(colors.black)
  450. term.setBackgroundColor(colors.gray)
  451. io.write("How many steps would you like to run for?")
  452. term.setCursorPos(1,height)
  453. local blab=io.read()
  454. term.setBackgroundColor(colors.white)
  455. for i=1,blab do
  456. generate(1)
  457. sleep(0.2)
  458. end
  459. end
  460. end
  461.  
  462. end
  463. --MAIN PROGRAM--
  464. while alive do
  465. drawCanvas()
  466. drawTools()
  467.  
  468. event,mouseX,mouseY,mouseButton=os.pullEvent("click")
  469. screenInteract()
  470. drawCanvas()
  471. end
Add Comment
Please, Sign In to add comment