Advertisement
nonogamer9

teest

Oct 17th, 2024 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.53 KB | None | 0 0
  1. -- Custom Bit Library for ComputerCraft
  2. local bit = {}
  3.  
  4. function bit.band(a, b)
  5. local result = 0
  6. local bitval = 1
  7. while a > 0 and b > 0 do
  8. if a % 2 == 1 and b % 2 == 1 then result = result + bitval end
  9. bitval = bitval * 2
  10. a = math.floor(a / 2)
  11. b = math.floor(b / 2)
  12. end
  13. return result
  14. end
  15.  
  16. function bit.bor(a, b)
  17. local result = 0
  18. local bitval = 1
  19. while a > 0 or b > 0 do
  20. if a % 2 == 1 or b % 2 == 1 then result = result + bitval end
  21. bitval = bitval * 2
  22. a = math.floor(a / 2)
  23. b = math.floor(b / 2)
  24. end
  25. return result
  26. end
  27.  
  28. function bit.bxor(a, b)
  29. local result = 0
  30. local bitval = 1
  31. while a > 0 or b > 0 do
  32. if a % 2 ~= b % 2 then result = result + bitval end
  33. bitval = bitval * 2
  34. a = math.floor(a / 2)
  35. b = math.floor(b / 2)
  36. end
  37. return result
  38. end
  39.  
  40. function bit.bnot(n)
  41. return (-n) - 1
  42. end
  43.  
  44. function bit.blshift(n, bits)
  45. return n * (2 ^ bits)
  46. end
  47.  
  48. function bit.brshift(n, bits)
  49. return math.floor(n / (2 ^ bits))
  50. end
  51.  
  52. function bit.blogic_rshift(n, bits)
  53. if n >= 0 then
  54. return bit.brshift(n, bits)
  55. else
  56. return bit.brshift((n + 0x100000000), bits)
  57. end
  58. end
  59.  
  60. -- Z-machine implementation
  61. local memory = {}
  62. local pc = 1
  63. local stack = {}
  64. local sp = 1
  65. local locals = {}
  66. local globals = {}
  67. local version = 0
  68.  
  69. -- Initialize memory
  70. for i = 1, 65536 do
  71. memory[i] = 0
  72. end
  73.  
  74. -- Helper functions
  75. local function readByte(addr)
  76. return memory[addr]
  77. end
  78.  
  79. local function readWord(addr)
  80. return (memory[addr] * 256) + memory[addr + 1]
  81. end
  82.  
  83. local function writeByte(addr, value)
  84. memory[addr] = value % 256
  85. end
  86.  
  87. local function writeWord(addr, value)
  88. memory[addr] = math.floor(value / 256)
  89. memory[addr + 1] = value % 256
  90. end
  91.  
  92. local function pushStack(value)
  93. stack[sp] = value
  94. sp = sp + 1
  95. end
  96.  
  97. local function popStack()
  98. sp = sp - 1
  99. return stack[sp]
  100. end
  101.  
  102. -- ZSCII decoding (simplified)
  103. local function decodeZSCII(addr)
  104. local result = ""
  105. local i = addr
  106. while true do
  107. local word = readWord(i)
  108. for j = 1, 3 do
  109. local char = bit.band(bit.brshift(word, (3-j)*5), 0x1F)
  110. if char ~= 0 then
  111. result = result .. string.char(char + 96)
  112. end
  113. end
  114. if bit.band(word, 0x8000) ~= 0 then break end
  115. i = i + 2
  116. end
  117. return result
  118. end
  119.  
  120. -- Operand types
  121. local OP_LARGE = 0
  122. local OP_SMALL = 1
  123. local OP_VAR = 2
  124. local OP_OMITTED = 3
  125.  
  126. -- Fetch and decode operands
  127. local function fetchOperands(opcode)
  128. local operandTypes = {}
  129. local operands = {}
  130.  
  131. if opcode < 0x80 then -- 2OP
  132. operandTypes[1] = bit.band(bit.brshift(opcode, 6), 0x03)
  133. operandTypes[2] = bit.band(bit.brshift(opcode, 4), 0x03)
  134. elseif opcode < 0xB0 then -- 1OP
  135. operandTypes[1] = bit.band(bit.brshift(opcode, 4), 0x03)
  136. elseif opcode < 0xC0 then -- 0OP
  137. -- No operands
  138. else -- VAR
  139. local specifier = readByte(pc)
  140. pc = pc + 1
  141. for i = 1, 4 do
  142. local type = bit.band(bit.brshift(specifier, (4-i)*2), 0x03)
  143. if type ~= OP_OMITTED then
  144. table.insert(operandTypes, type)
  145. end
  146. end
  147. end
  148.  
  149. for _, type in ipairs(operandTypes) do
  150. if type == OP_LARGE then
  151. table.insert(operands, readWord(pc))
  152. pc = pc + 2
  153. elseif type == OP_SMALL then
  154. table.insert(operands, readByte(pc))
  155. pc = pc + 1
  156. elseif type == OP_VAR then
  157. local var = readByte(pc)
  158. pc = pc + 1
  159. if var == 0 then
  160. table.insert(operands, popStack())
  161. elseif var < 16 then
  162. table.insert(operands, locals[var])
  163. else
  164. table.insert(operands, globals[var - 16])
  165. end
  166. end
  167. end
  168.  
  169. return operands
  170. end
  171.  
  172. -- Opcode implementations
  173. local opcodes = {
  174. [0x00] = function() return 1 end, -- RTRUE
  175. [0x01] = function() return 0 end, -- RFALSE
  176. [0x02] = function(value) return value end, -- PRINT
  177. [0x03] = function(value) print(value) return 1 end, -- PRINT_RET
  178. [0x04] = function() end, -- NOP
  179. [0x05] = function(a) -- SAVE
  180. local result = saveGame("z_save.dat")
  181. if version <= 3 then
  182. pc = a
  183. return result and 1 or 0
  184. else
  185. return result and 1 or 0
  186. end
  187. end,
  188. [0x06] = function(a) -- RESTORE
  189. local result = restoreGame("z_save.dat")
  190. if version <= 3 then
  191. pc = a
  192. return result and 2 or 0
  193. else
  194. return result and 2 or 0
  195. end
  196. end,
  197. [0x07] = function() pc = 1 end, -- RESTART
  198. [0x08] = function() return popStack() end, -- RET_POPPED
  199. [0x09] = function() popStack() end, -- POP
  200. [0x0A] = function() error("QUIT instruction executed") end, -- QUIT
  201. [0x0B] = function() print() end, -- NEW_LINE
  202. [0x0C] = function(a) return a end, -- SHOW_STATUS
  203. [0x0D] = function(a) return a end, -- VERIFY
  204. [0x0E] = function() -- EXTENDED
  205. local opcode = readByte(pc)
  206. pc = pc + 1
  207. -- Implement extended opcodes here
  208. end,
  209. [0x0F] = function(a) -- PIRACY
  210. if version <= 3 then pc = a end
  211. return 0
  212. end,
  213. -- Add more opcodes here
  214. [0x80] = function(a, b) if a == b then return 1 else return 0 end end, -- JE
  215. [0x81] = function(a, b) if a ~= b then return 1 else return 0 end end, -- JL
  216. [0x82] = function(a, b) if a < b then return 1 else return 0 end end, -- JG
  217. [0x83] = function(a, b) if a >= b then return 1 else return 0 end end, -- DEC_CHK
  218. [0x84] = function(a, b) if a <= b then return 1 else return 0 end end, -- INC_CHK
  219. [0x85] = function(a, b) if a > b then return 1 else return 0 end end, -- JIN
  220. [0x86] = function(a, b) return math.random(a, b) end, -- TEST
  221. [0x87] = function(a, b) return bit.bor(a, b) end, -- OR
  222. [0x88] = function(a, b) return bit.band(a, b) end, -- AND
  223. [0x89] = function(a, b) if bit.band(a, b) ~= 0 then return 1 else return 0 end end, -- TEST_ATTR
  224. -- Add more opcodes as needed
  225. }
  226.  
  227. -- Main execution loop
  228. local function execute()
  229. while true do
  230. local opcode = readByte(pc)
  231. pc = pc + 1
  232. local operands = fetchOperands(opcode)
  233. if opcode == 0x0E then -- EXTENDED opcode
  234. opcode = readByte(pc)
  235. pc = pc + 1
  236. operands = fetchOperands(opcode)
  237. -- Handle extended opcodes here
  238. end
  239. if opcodes[opcode] then
  240. local result = opcodes[opcode](table.unpack(operands))
  241. if result ~= nil then
  242. return result
  243. end
  244. else
  245. error("Unknown opcode: " .. opcode)
  246. end
  247. end
  248. end
  249.  
  250. -- Load game file
  251. local function loadGame(filename)
  252. local file = fs.open(filename, "rb")
  253. if not file then
  254. error("Could not open file: " .. filename)
  255. end
  256. local content = file.readAll()
  257. file.close()
  258.  
  259. for i = 1, #content do
  260. memory[i] = string.byte(content, i)
  261. end
  262.  
  263. version = memory[0]
  264. pc = readWord(0x06)
  265. end
  266.  
  267. -- Save game state
  268. local function saveGame(filename)
  269. local file = fs.open(filename, "w")
  270. if not file then
  271. return false
  272. end
  273.  
  274. file.write(textutils.serialize(memory))
  275. file.write(textutils.serialize(stack))
  276.  
  277. local state = {
  278. pc = pc,
  279. sp = sp,
  280. locals = locals,
  281. globals = globals,
  282. version = version
  283. }
  284. file.write(textutils.serialize(state))
  285.  
  286. file.close()
  287. return true
  288. end
  289.  
  290. -- Restore game state
  291. local function restoreGame(filename)
  292. local file = fs.open(filename, "r")
  293. if not file then
  294. return false
  295. end
  296.  
  297. memory = textutils.unserialize(file.readLine())
  298. stack = textutils.unserialize(file.readLine())
  299.  
  300. local state = textutils.unserialize(file.readLine())
  301. pc = state.pc
  302. sp = state.sp
  303. locals = state.locals
  304. globals = state.globals
  305. version = state.version
  306.  
  307. file.close()
  308. return true
  309. end
  310.  
  311. -- Download Zork 3
  312. local function downloadZork3()
  313. local url = "https://eblong.com/infocom/gamefiles/zork3-r16-s830410.z3"
  314. local response = http.get(url)
  315. if response then
  316. local content = response.readAll()
  317. response.close()
  318.  
  319. local file = fs.open("zork3.z3", "wb")
  320. file.write(content)
  321. file.close()
  322. return true
  323. else
  324. return false
  325. end
  326. end
  327.  
  328. -- GUI
  329. local function drawGUI()
  330. term.clear()
  331. term.setCursorPos(1, 1)
  332. term.write("Advanced Z-machine for ComputerCraft")
  333. term.setCursorPos(1, 3)
  334. term.write("1. Download Zork 3")
  335. term.setCursorPos(1, 4)
  336. term.write("2. Play Zork 3")
  337. term.setCursorPos(1, 5)
  338. term.write("3. Exit")
  339. end
  340.  
  341. -- Main function
  342. local function main()
  343. while true do
  344. drawGUI()
  345. term.setCursorPos(1, 7)
  346. term.write("Enter your choice: ")
  347. local choice = read()
  348.  
  349. if choice == "1" then
  350. term.setCursorPos(1, 9)
  351. term.write("Downloading Zork 3...")
  352. if downloadZork3() then
  353. term.setCursorPos(1, 10)
  354. term.write("Download complete!")
  355. else
  356. term.setCursorPos(1, 10)
  357. term.write("Download failed.")
  358. end
  359. os.sleep(2)
  360. elseif choice == "2" then
  361. if fs.exists("zork3.z3") then
  362. term.clear()
  363. term.setCursorPos(1, 1)
  364. loadGame("zork3.z3")
  365. local result = execute()
  366. term.setCursorPos(1, 23)
  367. term.write("Game finished with result: " .. result)
  368. term.setCursorPos(1, 24)
  369. term.write("Press any key to continue...")
  370. os.pullEvent("key")
  371. else
  372. term.setCursorPos(1, 9)
  373. term.write("Zork 3 not found. Please download it first.")
  374. os.sleep(2)
  375. end
  376. elseif choice == "3" then
  377. term.clear()
  378. term.setCursorPos(1, 1)
  379. term.write("Goodbye!")
  380. break
  381. else
  382. term.setCursorPos(1, 9)
  383. term.write("Invalid choice. Please try again.")
  384. os.sleep(2)
  385. end
  386. end
  387. end
  388.  
  389. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement