Stary2001

Untitled

Feb 18th, 2013
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 KB | None | 0 0
  1. local args={...}
  2.  
  3.  
  4. local delay = 0.5
  5.  
  6. local grid={} -- stores cell states
  7. local gridActilocal args={...}
  8.  
  9.  
  10. local delay = 0.5
  11.  
  12. local grid={} -- stores cell states
  13. local gridActions={} -- used to store births and deaths
  14.  
  15. local aliveColor = colors.lime
  16. local deadColor = colors.black
  17.  
  18. local xSize,ySize = term.getSize()
  19.  
  20. local spaceOrX = term.isColor() and " " or "x"
  21. -- 0,dimSize+1 because we index
  22. -- using offsets xOff and yOff later
  23. -- which can be -1!
  24.  
  25. function sim(grid)
  26. for x=1,xSize do
  27. for y=1,ySize do
  28. local neighbors = 0
  29. for xOff = -1,1 do
  30. for yOff = -1,1 do
  31. if not (xOff == 0 and yOff == 0) and grid[x+xOff][y+yOff] then
  32. neighbors = neighbors + 1
  33. end
  34. end
  35. end
  36. if not grid[x][y] and neighbors == 3 then
  37. gridActions[x][y]="birth"
  38. elseif neighbors < 2 or neighbors > 3 then
  39. gridActions[x][y]="die"
  40. end
  41. end
  42. end
  43.  
  44. for x=1,xSize do
  45. for y=1,ySize do
  46. if gridActions[x][y]=="die" then
  47. grid[x][y]=false
  48. elseif gridActions[x][y]=="birth" then
  49. grid[x][y]=true
  50. end
  51. gridActions[x][y]=nil
  52. end
  53. end
  54.  
  55. return grid
  56. end
  57.  
  58. for i=0,xSize+1 do
  59. grid[i]={}
  60. gridActions[i]={}
  61. end
  62.  
  63. for i=0,xSize+1 do
  64. for j=0,ySize+1 do
  65. grid[i][j]=false
  66. end
  67. end
  68.  
  69. if args[1]=="monitor" then
  70. term.redirect(peripheral.wrap(args[2]))
  71. elseif args[1]== "load" then
  72. local name = fs.getName(args[2])
  73.  
  74. local ext = string.match(name,".-[^\\/]-%.?([^%.\\/]*)$")
  75.  
  76. local f = fs.open(shell.resolve(args[2]),"r")
  77. if ext == "cells" then
  78. local line
  79. line = f.readLine()
  80.  
  81. local patternYSize = 0
  82.  
  83. while line do
  84. if line:sub(1,1) ~= "!" then
  85. patternYSize = patternYSize + 1
  86. end
  87. line = f.readLine()
  88. end
  89.  
  90. local y = math.floor(ySize / 2) - math.floor(patternYSize / 2)
  91.  
  92. f.close()
  93. f = fs.open(shell.resolve(args[2]),"r")
  94. line = f.readLine()
  95.  
  96. while line do
  97. if line:sub(1,1) ~= "!" then
  98. for x=1,#line do
  99. grid[x][y] = line:sub(x,x) == "O"
  100. end
  101. y = y + 1
  102. end
  103. line = f.readLine()
  104. end
  105. elseif ext == "rle" then
  106. -- Not implemented, my implementation I had sucked
  107. -- kamal dongs
  108. elseif ext == "lif" then
  109. local line = f.readLine() -- header
  110. if line ~= "#Life 1.06" then
  111. error("Malformed Life 1.06 file!")
  112. end
  113. line = f.readLine()
  114. while line do
  115. local x,y = string.match(line,"([0-9%-]+) ([0-9%-]+)")
  116. print(x," ",y)
  117. x=tonumber(x)
  118. y=tonumber(y)
  119. print(x," ",y)
  120. grid[x][y] = true
  121. line = f.readLine()
  122. end
  123. end
  124. f.close()
  125. elseif args[1] == "get" then
  126. -- use http.get to load.. LATER
  127. end
  128.  
  129. term.setCursorBlink(false)
  130.  
  131. local paused = true
  132.  
  133. os.startTimer(delay) -- makes screen draw the first time
  134.  
  135. while true do
  136. local _,__,x,y = os.pullEvent()
  137.  
  138. if _ == "key" then
  139. if __ == 197 then -- pause key ( exit! )
  140. break
  141. end
  142.  
  143. paused = not paused
  144. elseif _ == "monitor_touch" or _ == "mouse_click" then
  145. grid[x][y] = not grid[x][y]
  146. end
  147.  
  148. if _ ~= "timer" then
  149. local ev
  150. while ev ~= "timer" do
  151. ev = os.pullEvent()
  152. end
  153. end
  154.  
  155. if not paused then
  156. grid=sim(grid)
  157. end
  158.  
  159. term.setBackgroundColor(deadColor)
  160. term.clear()
  161. term.setCursorPos(1,19)
  162. io.write(paused and "Paused" or " ")
  163.  
  164. for x=1,xSize do
  165. for y=1,ySize do
  166. term.setCursorPos(x,y)
  167. if grid[x][y] then
  168. term.setBackgroundColor(aliveColor)
  169. io.write(spaceOrX)
  170. term.setBackgroundColor(deadColor)
  171. end
  172. end
  173. end
  174.  
  175. os.startTimer(delay)
  176. end
  177.  
  178. term.restore()ons={} -- used to store births and deaths
  179.  
  180. local aliveColor = colors.lime
  181. local deadColor = colors.black
  182.  
  183. local xSize,ySize = term.getSize()
  184.  
  185. -- 0,dimSize+1 because we index
  186. -- using offsets xOff and yOff later
  187. -- which can be -1!
  188.  
  189. function sim(grid)
  190. for x=1,xSize do
  191. for y=1,ySize do
  192. local neighbors = 0
  193. for xOff = -1,1 do
  194. for yOff = -1,1 do
  195. if not (xOff == 0 and yOff == 0) and grid[x+xOff][y+yOff] then
  196. neighbors = neighbors + 1
  197. end
  198. end
  199. end
  200. if not grid[x][y] and neighbors == 3 then
  201. gridActions[x][y]="birth"
  202. elseif neighbors < 2 or neighbors > 3 then
  203. gridActions[x][y]="die"
  204. end
  205. end
  206. end
  207.  
  208. for x=1,xSize do
  209. for y=1,ySize do
  210. if gridActions[x][y]=="die" then
  211. grid[x][y]=false
  212. elseif gridActions[x][y]=="birth" then
  213. grid[x][y]=true
  214. end
  215. gridActions[x][y]=nil
  216. end
  217. end
  218.  
  219. return grid
  220. end
  221.  
  222. for i=0,xSize+1 do
  223. grid[i]={}
  224. gridActions[i]={}
  225. end
  226.  
  227. for i=0,xSize+1 do
  228. for j=0,ySize+1 do
  229. grid[i][j]=false
  230. end
  231. end
  232.  
  233. if args[1]=="monitor" then
  234. term.redirect(peripheral.wrap(args[2]))
  235. elseif args[1]== "load" then
  236. local name = fs.getName(args[2])
  237.  
  238. local ext = string.match(name,".-[^\\/]-%.?([^%.\\/]*)$")
  239.  
  240. local f = fs.open(shell.resolve(args[2]),"r")
  241. if ext == "cells" then
  242. local line
  243. line = f.readLine()
  244.  
  245. local patternYSize = 0
  246.  
  247. while line do
  248. if line:sub(1,1) ~= "!" then
  249. patternYSize = patternYSize + 1
  250. end
  251. line = f.readLine()
  252. end
  253.  
  254. local y = math.floor(ySize / 2) - math.floor(patternYSize / 2)
  255.  
  256. f.close()
  257. f = fs.open(shell.resolve(args[2]),"r")
  258. line = f.readLine()
  259.  
  260. while line do
  261. if line:sub(1,1) ~= "!" then
  262. for x=1,#line do
  263. grid[x][y] = line:sub(x,x) == "O"
  264. end
  265. y = y + 1
  266. end
  267. line = f.readLine()
  268. end
  269. elseif ext == "rle" then
  270. -- Not implemented, my implementation I had sucked
  271. -- kamal dongs
  272. elseif ext == "lif" then
  273. local line = f.readLine() -- header
  274. if line ~= "#Life 1.06" then
  275. error("Malformed Life 1.06 file!")
  276. end
  277. line = f.readLine()
  278. while line do
  279. local x,y = string.match(line,"([0-9%-]+) ([0-9%-]+)")
  280. print(x," ",y)
  281. x=tonumber(x)
  282. y=tonumber(y)
  283. print(x," ",y)
  284. grid[x][y] = true
  285. line = f.readLine()
  286. end
  287. end
  288. f.close()
  289. elseif args[1] == "get" then
  290. -- use http.get to load.. LATER
  291. end
  292.  
  293. term.clear()
  294. term.setCursorBlink(false)
  295.  
  296. local paused = true
  297.  
  298. os.startTimer(0.1) -- makes screen draw the first time
  299.  
  300. while true do
  301. local _,__,x,y = os.pullEvent()
  302.  
  303. if _ == "key" then
  304. if __ == 197 then -- pause key ( exit! )
  305. break
  306. end
  307.  
  308. paused = not paused
  309. elseif _ == "monitor_touch" or _ == "mouse_click" then
  310. grid[x][y] = not grid[x][y]
  311. end
  312.  
  313. if _ ~= "timer" then
  314. local ev
  315. while ev ~= "timer" do
  316. ev = os.pullEvent()
  317. end
  318. end
  319.  
  320. term.setBackgroundColor(deadColor)
  321. term.clear()
  322. term.setCursorPos(1,18)
  323. io.write(paused and "Paused" or " ")
  324.  
  325. if not paused then
  326. grid=sim(grid)
  327.  
  328. for x=1,xSize do
  329. for y=1,ySize do
  330. term.setCursorPos(x,y)
  331. if grid[x][y] then
  332. term.setBackgroundColor(aliveColor)
  333. io.write(" ")
  334. term.setBackgroundColor(deadColor)
  335. end
  336. end
  337. end
  338. end
  339.  
  340. os.startTimer(delay)
  341. end
  342.  
  343. term.restore()
Advertisement
Add Comment
Please, Sign In to add comment