Advertisement
rithrin

madness

Apr 26th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. --Define variables
  2. local colourList = {}
  3. local coloursUsed = {}
  4. local coloursUnused = {}
  5. local dir = 1
  6. local x = 1
  7. local y = 1
  8. local w = 1
  9. local h = 1
  10. local grid = {}
  11. local sequential = true
  12. local step = 1
  13.  
  14. --Miscellaneous helper functions to simplify code later
  15. --Generate either -1 or 1
  16. local function randomSign()
  17. local k = math.random(2)
  18. return (k * 2 - 3)
  19. end
  20. --Turn integers from 1 to 16 into their colour vaues without needing to perform exponents.
  21. local function colourSwitch(c)
  22. if c == 1 then return 1
  23. elseif c == 2 then return 2
  24. elseif c == 3 then return 4
  25. elseif c == 4 then return 8
  26. elseif c == 5 then return 16
  27. elseif c == 6 then return 32
  28. elseif c == 7 then return 64
  29. elseif c == 8 then return 128
  30. elseif c == 9 then return 256
  31. elseif c == 10 then return 512
  32. elseif c == 11 then return 1024
  33. elseif c == 12 then return 2048
  34. elseif c == 13 then return 4096
  35. elseif c == 14 then return 8192
  36. elseif c == 15 then return 16384
  37. elseif c == 16 then return 32768
  38. end
  39. end
  40.  
  41. --Helper functions for creating a ruleset.
  42.  
  43. local function initColourLists()
  44. for i=1,16 do colourList[i] = {} end
  45. coloursUsed = {16}
  46. coloursUnused = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
  47. end
  48. local function chooseColours()
  49. local n = math.random(15)
  50. local choicesLeft = table.getn(coloursUnused)
  51. local choice = 1
  52.  
  53. --Choose colours randomly from the list of unchosen colours
  54. for i=1,n do
  55. choice = math.random(choicesLeft)
  56. coloursUsed[i+1] = table.remove(coloursUnused, choice)
  57. choicesLeft = choicesLeft - 1
  58. end
  59.  
  60. --Set up colourList linkage
  61. for i=1,n do
  62. colourList[coloursUsed[i]][2] = coloursUsed[i+1]
  63. end
  64. colourList[coloursUsed[n]][2] = coloursUsed[1]
  65. end
  66. local function chooseDirections()
  67. local n = table.getn(coloursUsed)
  68. local total = 0
  69. local s = 0
  70.  
  71. --Randomise the direction for all colours used.
  72. for i, c in ipairs(coloursUsed) do
  73. s = randomSign()
  74. colourList[c][1] = s
  75. total = total + s
  76. end
  77.  
  78. --If all colours turn in the same direction, switch the direction of one used colour.
  79. if math.abs(total) == n then
  80. colourList[coloursUsed[1]][1] = - colourList[coloursUsed[1]][1]
  81. end
  82. end
  83.  
  84. --Function to create the play-space and position the ant/
  85. local function initGrid()
  86. for i=1,w do
  87. grid[i] = {}
  88. for j=1,h do
  89. grid[i][j] = 16
  90. end
  91. end
  92.  
  93. x = math.ceil(w/2)
  94. y = math.ceil(h/2)
  95. dir = math.random(4)
  96. end
  97.  
  98. --Step function for ant movement. Returns true until the ant leaves the grid.
  99. local function move()
  100. --Move the ant
  101. if dir == 1 then x = x + 1
  102. elseif dir == 2 then y = y + 1
  103. elseif dir == 3 then x = x - 1
  104. elseif dir == 4 then y = y - 1
  105. end
  106.  
  107. --If out of bounds, return false to end the program
  108. if x<1 or x>w or y<1 or y>h then
  109. return false
  110. end
  111.  
  112. --Change colour of new position and direction
  113. local c = grid[x][y]
  114. grid[x][y] = colourList[c][2]
  115. dir = dir + colourList[c][1]
  116.  
  117. --Correct directions outside of possible values
  118. if dir == 0 then dir = 4
  119. elseif dir == 5 then dir = 1
  120. end
  121.  
  122. return true
  123. end
  124.  
  125. --Function to generate a ruleset and play it out.
  126. function play(monitorSide, sequential, step)
  127. local m = peripheral.wrap(monitorSide)
  128. if not m.isColor() then error("Advanced monitor required.") end
  129. m.setTextScale(0.5)
  130. w, h = m.getSize()
  131. if sequential ~= false then sequential = true end
  132. step = tonumber(step) or 1
  133.  
  134. --randomise background colour
  135. --standard can be set by replacing math.random(16) with an integer from 1 to 16
  136. m.setBackgroundColour(colourSwitch(math.random(16)))
  137. m.clear()
  138.  
  139. --initialize variables and choose generator ruleset
  140. initColourLists()
  141. initGrid()
  142. chooseColours()
  143. chooseDirections()
  144. local i = 0
  145.  
  146. --play out the ruleset
  147. while move() do
  148. m.setCursorPos(x, y)
  149. m.setBackgroundColour(colourSwitch(grid[x][y]))
  150. m.write(" ")
  151. i = i + 1
  152.  
  153. --leave breaks so that the program yields and to allow stopping by right-click
  154. if sequential and i == step then
  155. i = 0
  156. os.startTimer(0.1)
  157. local e = os.pullEvent()
  158. if e == 'monitor_touch' then return true end
  159. end
  160. end
  161. end
  162.  
  163.  
  164. --The part of the program that runs it if not used as an API
  165. local tArgs = {...}
  166. local side = tArgs[1] or "top"
  167. if tArgs[2] == 'false' then sequential = false end
  168. if tArgs[3] ~= nil then step = tonumber(tArgs[3]) end
  169.  
  170. while true do
  171. play(side, sequential, step)
  172. os.pullEvent('monitor_touch')
  173. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement