Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.96 KB | None | 0 0
  1. local TermW,TermH = term.getSize()
  2.  
  3. local sLevelTitle
  4. local tScreen
  5. local oScreen
  6. local SizeW,SizeH
  7. local aExits
  8. local fExit
  9. local nSpeed
  10. local Speed
  11. local fSpeed
  12. local fSpeedS
  13. local bPaused
  14. local Tick
  15. local Blocks
  16. local XOrgin,YOrgin
  17.  
  18. local function reset()
  19. sLevelTitle = ""
  20. tScreen = {}
  21. oScreen = {}
  22. SizeW,SizeH = TermW,TermH
  23. aExits = 0
  24. fExit = "nop"
  25. nSpeed = 0.6
  26. Speed = nSpeed
  27. fSpeed = 0.2
  28. fSpeedS = false
  29. bPaused = false
  30. Tick = os.startTimer(Speed)
  31. Blocks = 0
  32. XOrgin,YOrgin = 1,1
  33.  
  34. term.setBackgroundColor(colors.black)
  35. term.setTextColor(colors.white)
  36. term.clear()
  37. end
  38.  
  39. local InterFace = {}
  40. InterFace.cExit = colors.red
  41. InterFace.cSpeedD = colors.white
  42. InterFace.cSpeedA = colors.red
  43. InterFace.cTitle = colors.red
  44.  
  45. local cG = colors.lightGray
  46. local cW = colors.gray
  47. local cS = colors.black
  48. local cR1 = colors.blue
  49. local cR2 = colors.red
  50. local cR3 = colors.green
  51. local cR4 = colors.yellow
  52.  
  53. local tArgs = { ... }
  54.  
  55. --Functions--
  56. local function printCentred( yc, stg )
  57. local xc = math.floor((TermW - string.len(stg)) / 2) + 1
  58. term.setCursorPos(xc,yc)
  59. term.write( stg )
  60. end
  61.  
  62. local function centerOrgin()
  63. XOrgin = math.floor((TermW/2)-(SizeW/2))
  64. YOrgin = math.floor((TermH/2)-(SizeH/2))
  65. end
  66.  
  67. local function reMap()
  68. tScreen = nil
  69. tScreen = {}
  70. for x=1,SizeW do
  71. tScreen[x] = {}
  72. for y=1,SizeH do
  73. tScreen[x][y] = { space = true, wall = false, ground = false, robot = "zz", start = "zz", exit = "zz" }
  74. end
  75. end
  76. end
  77.  
  78. local function tablecopy(t)
  79. local t2 = {}
  80. for k,v in pairs(t) do
  81. t2[k] = v
  82. end
  83. return t2
  84. end
  85.  
  86. local function buMap()
  87. oScreen = nil
  88. oScreen = {}
  89. for x=1,SizeW do
  90. oScreen[x] = {}
  91. for y=1,SizeH do
  92. oScreen[x][y] = tablecopy(tScreen[x][y])
  93. end
  94. end
  95. end
  96.  
  97. local function addRobot(x,y,side,color)
  98. local obj = tScreen[x][y]
  99. local data = side..color
  100. if obj.wall == nil and obj.robot == nil then
  101. tScreen[x][y].robot = data
  102. else
  103. obj.wall = nil
  104. obj.robot = "zz"
  105. tScreen[x][y].robot = data
  106. end
  107. end
  108.  
  109. local function addStart(x,y,side,color)
  110. local obj = tScreen[x][y]
  111. local data = side..color
  112. if obj.wall == nil and obj.space == nil then
  113. tScreen[x][y].start = data
  114. else
  115. obj.wall = nil
  116. obj.space = nil
  117. tScreen[x][y].start = data
  118. end
  119. aExits = aExits+1
  120. end
  121.  
  122. local function addGround(x,y)
  123. local obj = tScreen[x][y]
  124. if obj.space == nil and obj.exit == nil and obj.wall == nil and obj.robot == nil and obj.start == nil then
  125. tScreen[x][y].ground = true
  126. else
  127. obj.space = nil
  128. obj.exit = "zz"
  129. obj.wall = nil
  130. obj.robot = "zz"
  131. obj.start = "zz"
  132. tScreen[x][y].ground = true
  133. end
  134. end
  135.  
  136. local function addExit(x,y,cl)
  137. local obj = tScreen[x][y]
  138. if obj.space == nil and obj.ground == nil and obj.wall == nil and obj.robot == nil and obj.start == nil then
  139. tScreen[x][y].exit = cl
  140. else
  141. obj.space = nil
  142. obj.ground = nil
  143. obj.wall = nil
  144. obj.robot = "zz"
  145. obj.start = "zz"
  146. tScreen[x][y].exit = cl
  147. end
  148. end
  149.  
  150. local function addWall(x,y)
  151. local obj = tScreen[x][y]
  152. if obj == nil then
  153. return error("Here X"..x.." Y"..y)
  154. end
  155. if obj.space == nil and obj.exit == nil and obj.ground == nil and obj.robot == nil and obj.start == nil then
  156. tScreen[x][y].wall = true
  157. else
  158. obj.space = nil
  159. obj.exit = nil
  160. obj.ground = nil
  161. obj.robot = nil
  162. obj.start = nil
  163. tScreen[x][y].wall = true
  164. end
  165. end
  166.  
  167. local function loadLevel(nNum)
  168. sLevelTitle = "Level "..nNum
  169. if nNum == nil then return error("nNum == nil") end
  170. local sDir = fs.getDir( shell.getRunningProgram() )
  171. local sLevelD = sDir .. "/levels/" .. tostring(nNum)
  172. if not ( fs.exists(sLevelD) or fs.isDir(sLevelD) ) then return error("Level Not Exists : "..sLevelD) end
  173. fLevel = fs.open(sLevelD,"r")
  174. local Line = 0
  175. local wl = true
  176. Blocks = tonumber(string.sub(fLevel.readLine(),1,1))
  177. local xSize = string.len(fLevel.readLine())+2
  178. local Lines = 3
  179. while wl do
  180. local wLine = fLevel.readLine()
  181. if wLine == nil then
  182. fLevel.close()
  183. wl = false
  184. else
  185. xSize = math.max(string.len(wLine)+2,xSize)
  186. Lines = Lines + 1
  187. end
  188. end
  189. SizeW,SizeH = xSize,Lines
  190. reMap()
  191. fLevel = fs.open(sLevelD,"r")
  192. fLevel.readLine()
  193. for Line=2,Lines-1 do
  194. sLine = fLevel.readLine()
  195. local chars = string.len(sLine)
  196. for char = 1, chars do
  197. local el = string.sub(sLine,char,char)
  198. if el == "8" then
  199. addGround(char+1,Line)
  200. elseif el == "0" then
  201. addStart(char+1,Line,"a","a")
  202. elseif el == "1" then
  203. addStart(char+1,Line,"b","a")
  204. elseif el == "2" then
  205. addStart(char+1,Line,"c","a")
  206. elseif el == "3" then
  207. addStart(char+1,Line,"d","a")
  208. elseif el == "4" then
  209. addStart(char+1,Line,"a","b")
  210. elseif el == "5" then
  211. addStart(char+1,Line,"b","b")
  212. elseif el == "6" then
  213. addStart(char+1,Line,"c","b")
  214. elseif el == "9" then
  215. addStart(char+1,Line,"d","b")
  216. elseif el == "b" then
  217. addExit(char+1,Line,"a")
  218. elseif el == "e" then
  219. addExit(char+1,Line,"b")
  220. elseif el == "7" then
  221. addWall(char+1,Line)
  222. end
  223. end
  224. end
  225. fLevel.close()
  226. end
  227.  
  228. local function drawStars()
  229. --CCR Background By : RamiLego--
  230. local cStar,cStarG,crStar,crStarB = colors.lightGray,colors.gray,".","*"
  231. local DStar,BStar,nStar,gStar = 14,10,16,3
  232. local TermW,TermH = term.getSize()
  233.  
  234. term.clear()
  235. term.setCursorPos(1,1)
  236. for x=1,TermW do
  237. for y=1,TermH do
  238. local StarT = math.random(1,30)
  239. if StarT == DStar then
  240. term.setCursorPos(x,y)
  241. term.setTextColor(cStar)
  242. write(crStar)
  243. elseif StarT == BStar then
  244. term.setCursorPos(x,y)
  245. term.setTextColor(cStar)
  246. write(crStarB)
  247. elseif StarT == nStar then
  248. term.setCursorPos(x,y)
  249. term.setTextColor(cStarG)
  250. write(crStar)
  251. elseif StarT == gStar then
  252. term.setCursorPos(x,y)
  253. term.setTextColor(cStarG)
  254. write(crStarB)
  255. end
  256. end
  257. end
  258. end
  259.  
  260. local function drawMap()
  261. for x=1,SizeW do
  262. for y=1,SizeH do
  263.  
  264. local obj = tScreen[x][y]
  265. if obj.ground == true then
  266. paintutils.drawPixel(XOrgin+x,YOrgin+y+1,cG)
  267. end
  268. if obj.wall == true then
  269. paintutils.drawPixel(XOrgin+x,YOrgin+y+1,cW)
  270. end
  271.  
  272. local ex = tostring(tScreen[x][y].exit)
  273. if not(ex == "zz" or ex == "nil") then
  274. if ex == "a" then
  275. ex = cR1
  276. elseif ex == "b" then
  277. ex = cR2
  278. elseif ex == "c" then
  279. ex = cR3
  280. elseif ex == "d" then
  281. ex = cR4
  282. else
  283. return error("Exit Color Out")
  284. end
  285. term.setBackgroundColor(cG)
  286. term.setTextColor(ex)
  287. term.setCursorPos(XOrgin+x,YOrgin+y+1)
  288. print("X")
  289. end
  290.  
  291. local st = tostring(tScreen[x][y].start)
  292. if not(st == "zz" or st == "nil") then
  293. local Cr = string.sub(st,2,2)
  294. if Cr == "a" then
  295. Cr = cR1
  296. elseif Cr == "b" then
  297. Cr = cR2
  298. elseif Cr == "c" then
  299. Cr = cR3
  300. elseif Cr == "d" then
  301. Cr = cR4
  302. else
  303. return error("Start Color Out")
  304. end
  305.  
  306. term.setTextColor(Cr)
  307. term.setBackgroundColor(cG)
  308. term.setCursorPos(XOrgin+x,YOrgin+y+1)
  309.  
  310. sSide = string.sub(st,1,1)
  311. if sSide == "a" then
  312. print("^")
  313. elseif sSide == "b" then
  314. print(">")
  315. elseif sSide == "c" then
  316. print("v")
  317. elseif sSide == "d" then
  318. print("<")
  319. else
  320. print("@")
  321. end
  322. end
  323.  
  324. if obj.space == true then
  325. paintutils.drawPixel(XOrgin+x,YOrgin+y+1,cS)
  326. end
  327.  
  328. local rb = tostring(tScreen[x][y].robot)
  329. if not(rb == "zz" or rb == "nil") then
  330. local Cr = string.sub(rb,2,2)
  331. if Cr == "a" then
  332. Cr = cR1
  333. elseif Cr == "b" then
  334. Cr = cR2
  335. elseif Cr == "c" then
  336. Cr = cR3
  337. elseif Cr == "d" then
  338. Cr = cR4
  339. else
  340. Cr = colors.white
  341. end
  342. term.setBackgroundColor(Cr)
  343. term.setTextColor(colors.white)
  344. term.setCursorPos(XOrgin+x,YOrgin+y+1)
  345. sSide = string.sub(rb,1,1)
  346. if sSide == "a" then
  347. print("^")
  348. elseif sSide == "b" then
  349. print(">")
  350. elseif sSide == "c" then
  351. print("v")
  352. elseif sSide == "d" then
  353. print("<")
  354. else
  355. print("@")
  356. end
  357. end
  358. end
  359. end
  360. end
  361.  
  362. local function isBrick(x,y)
  363. local brb = tostring(tScreen[x][y].robot)
  364. local bobj = oScreen[x][y]
  365. if (brb == "zz" or brb == "nil") and not bobj.wall == true then
  366. return false
  367. else
  368. return true
  369. end
  370. end
  371.  
  372. local function gRender(sContext)
  373. if sContext == "start" then
  374. for x=1,SizeW do
  375. for y=1,SizeH do
  376. local st = tostring(tScreen[x][y].start)
  377. if not(st == "zz" or st == "nil") then
  378. local Cr = string.sub(st,2,2)
  379. local sSide = string.sub(st,1,1)
  380. addRobot(x,y,sSide,Cr)
  381. end
  382. end
  383. end
  384. elseif sContext == "tick" then
  385. buMap()
  386. for x=1,SizeW do
  387. for y=1,SizeH do
  388. local rb = tostring(oScreen[x][y].robot)
  389. if not(rb == "zz" or rb == "nil") then
  390. local Cr = string.sub(rb,2,2)
  391. local sSide = string.sub(rb,1,1)
  392. local sobj = oScreen[x][y]
  393. if sobj.space == true then
  394. tScreen[x][y].robot = "zz"
  395. if not sSide == "g" then
  396. addRobot(x,y,"g",Cr)
  397. end
  398. elseif sobj.exit == Cr then
  399. if sSide == "a" or sSide == "b" or sSide == "c" or sSide == "d" then
  400. tScreen[x][y].robot = "zz"
  401. addRobot(x,y,"g",Cr)
  402. aExits = aExits-1
  403. end
  404. elseif sSide == "a" then
  405. local obj = isBrick(x,y-1)
  406. tScreen[x][y].robot = "zz"
  407. if not obj == true then
  408. addRobot(x,y-1,sSide,Cr)
  409. else
  410. local obj2 = isBrick(x-1,y)
  411. local obj3 = isBrick(x+1,y)
  412. if not obj2 == true and not obj3 == true then
  413. if Cr == "a" then
  414. addRobot(x,y,"d",Cr)
  415. elseif Cr == "b" then
  416. addRobot(x,y,"b",Cr)
  417. end
  418. elseif obj == true and obj2 == true and obj3 == true then
  419. addRobot(x,y,"c",Cr)
  420. else
  421. if obj3 == true then
  422. addRobot(x,y,"d",Cr)
  423. elseif obj2 == true then
  424. addRobot(x,y,"b",Cr)
  425. end
  426. end
  427. end
  428. elseif sSide == "b" then
  429. local obj = isBrick(x+1,y)
  430. tScreen[x][y].robot = "zz"
  431. if not obj == true then
  432. addRobot(x+1,y,sSide,Cr)
  433. else
  434. local obj2 = isBrick(x,y-1)
  435. local obj3 = isBrick(x,y+1)
  436. if not obj2 == true and not obj3 == true then
  437. if Cr == "a" then
  438. addRobot(x,y,"a",Cr)
  439. elseif Cr == "b" then
  440. addRobot(x,y,"c",Cr)
  441. end
  442. elseif obj == true and obj2 == true and obj3 == true then
  443. addRobot(x,y,"d",Cr)
  444. else
  445. if obj3 == true then
  446. addRobot(x,y,"a",Cr)
  447. elseif obj2 == true then
  448. addRobot(x,y,"c",Cr)
  449. end
  450. end
  451. end
  452. elseif sSide == "c" then
  453. local obj = isBrick(x,y+1)
  454. tScreen[x][y].robot = "zz"
  455. if not obj == true then
  456. addRobot(x,y+1,sSide,Cr)
  457. else
  458. local obj2 = isBrick(x-1,y)
  459. local obj3 = isBrick(x+1,y)
  460. if not obj2 == true and not obj3 == true then
  461. if Cr == "a" then
  462. addRobot(x,y,"b",Cr)
  463. elseif Cr == "b" then
  464. addRobot(x,y,"d",Cr)
  465. end
  466. elseif obj == true and obj2 == true and obj3 == true then
  467. addRobot(x,y,"a",Cr)
  468. else
  469. if obj3 == true then
  470. addRobot(x,y,"d",Cr)
  471. elseif obj2 == true then
  472. addRobot(x,y,"b",Cr)
  473. end
  474. end
  475. end
  476. elseif sSide == "d" then
  477. local obj = isBrick(x-1,y)
  478. tScreen[x][y].robot = "zz"
  479. if not obj == true then
  480. addRobot(x-1,y,sSide,Cr)
  481. else
  482. local obj2 = isBrick(x,y-1)
  483. local obj3 = isBrick(x,y+1)
  484. if not obj2 == true and not obj3 == true then
  485. if Cr == "a" then
  486. addRobot(x,y,"c",Cr)
  487. elseif Cr == "b" then
  488. addRobot(x,y,"a",Cr)
  489. end
  490. elseif obj == true and obj2 == true and obj3 == true then
  491. addRobot(x,y,"b",Cr)
  492. else
  493. if obj3 == true then
  494. addRobot(x,y,"a",Cr)
  495. elseif obj2 == true then
  496. addRobot(x,y,"c",Cr)
  497. end
  498. end
  499. end
  500. else
  501. addRobot(x,y,sSide,"g")
  502. end
  503. end
  504. end
  505. end
  506. end
  507. end
  508.  
  509. function InterFace.drawBar()
  510. term.setBackgroundColor( colors.black )
  511. term.setTextColor( InterFace.cTitle )
  512. printCentred( 1, " "..sLevelTitle.." " )
  513.  
  514. term.setCursorPos(1,1)
  515. term.setBackgroundColor( cW )
  516. write( " " )
  517. term.setBackgroundColor( colors.black )
  518. write( " x "..tostring(Blocks).." " )
  519.  
  520. term.setCursorPos( TermW-8,TermH )
  521. term.setBackgroundColor( colors.black )
  522. term.setTextColour(InterFace.cSpeedD)
  523. write(" <<" )
  524. if bPaused then
  525. term.setTextColour(InterFace.cSpeedA)
  526. else
  527. term.setTextColour(InterFace.cSpeedD)
  528. end
  529. write(" ||")
  530. if fSpeedS then
  531. term.setTextColour(InterFace.cSpeedA)
  532. else
  533. term.setTextColour(InterFace.cSpeedD)
  534. end
  535. write(" >>")
  536.  
  537. term.setCursorPos( TermW-1, 1 )
  538. term.setBackgroundColor( colors.black )
  539. term.setTextColour( InterFace.cExit )
  540. write(" X")
  541. term.setBackgroundColor(colors.black)
  542. end
  543.  
  544. function InterFace.render()
  545. local id,p1,p2,p3 = os.pullEvent()
  546. if id == "mouse_click" then
  547. if p3 == 1 and p2 == TermW then
  548. return "end"
  549. elseif p3 == TermH and p2 >= TermW-7 and p2 <= TermW-6 then
  550. return "retry"
  551. elseif p3 == TermH and p2 >= TermW-4 and p2 <= TermW-3 then
  552. bPaused = not bPaused
  553. fSpeedS = false
  554. Speed = (bPaused and 0) or nSpeed
  555. if Speed > 0 then
  556. Tick = os.startTimer(Speed)
  557. else
  558. Tick = nil
  559. end
  560. InterFace.drawBar()
  561. elseif p3 == TermH and p2 >= TermW-1 then
  562. bPaused = false
  563. fSpeedS = not fSpeedS
  564. Speed = (fSpeedS and fSpeed) or nSpeed
  565. Tick = os.startTimer(Speed)
  566. InterFace.drawBar()
  567. elseif p3-1 < YOrgin+SizeH+1 and p3-1 > YOrgin and
  568. p2 < XOrgin+SizeW+1 and p2 > XOrgin then
  569. local eobj = tScreen[p2-XOrgin][p3-YOrgin-1]
  570. local erobj = tostring(tScreen[p2-XOrgin][p3-YOrgin-1].robot)
  571. if (erobj == "zz" or erobj == "nil") and not eobj.wall == true and not eobj.space == true and Blocks > 0 then
  572. addWall(p2-XOrgin,p3-YOrgin-1)
  573. Blocks = Blocks-1
  574. InterFace.drawBar()
  575. drawMap()
  576. end
  577. end
  578. elseif id == "timer" and p1 == Tick then
  579. gRender("tick")
  580. drawMap()
  581. if Speed > 0 then
  582. Tick = os.startTimer(Speed)
  583. else
  584. Tick = nil
  585. end
  586. end
  587. end
  588.  
  589. local function startG(LevelN)
  590. drawStars()
  591. loadLevel(LevelN)
  592. centerOrgin()
  593. local create = true
  594. drawMap()
  595. InterFace.drawBar()
  596. gRender("start")
  597. drawMap()
  598.  
  599. local NExit = true
  600. if aExits == 0 then
  601. NExit = false
  602. end
  603.  
  604. while true do
  605. local isExit = InterFace.render()
  606. if isExit == "end" then
  607. return nil
  608. elseif isExit == "retry" then
  609. return LevelN
  610. elseif fExit == "yes" then
  611. if fs.exists( fs.getDir( shell.getRunningProgram() ) .. "/levels/" .. tostring(LevelN + 1) ) then
  612. return LevelN + 1
  613. else
  614. return nil
  615. end
  616. end
  617. if aExits == 0 and NExit == true then
  618. fExit = "yes"
  619. end
  620. end
  621. end
  622.  
  623. local ok, err = true, nil
  624.  
  625. --Menu--
  626. local sStartLevel = tArgs[1]
  627. if ok and not sStartLevel then
  628. ok, err = pcall( function()
  629. term.setTextColor(colors.white)
  630. term.setBackgroundColor( colors.black )
  631. term.clear()
  632. drawStars()
  633. term.setTextColor( colors.red )
  634. printCentred( TermH/2 - 1, " REDIRECTION " )
  635. printCentred( TermH/2 - 0, " ComputerCraft Edition " )
  636. term.setTextColor( colors.yellow )
  637. printCentred( TermH/2 + 2, " Click to Begin " )
  638. os.pullEvent( "mouse_click" )
  639. end )
  640. end
  641.  
  642. --Game--
  643. if ok then
  644. ok,err = pcall( function()
  645. local nLevel
  646. if sStartLevel then
  647. nLevel = tonumber( sStartLevel )
  648. else
  649. nLevel = 1
  650. end
  651. while nLevel do
  652. reset()
  653. nLevel = startG(nLevel)
  654. end
  655. end )
  656. end
  657.  
  658. --Upsell screen--
  659. if ok then
  660. ok, err = pcall( function()
  661. term.setTextColor(colors.white)
  662. term.setBackgroundColor( colors.black )
  663. term.clear()
  664. drawStars()
  665. term.setTextColor( colors.red )
  666. if TermW >= 40 then
  667. printCentred( TermH/2 - 1, " Thank you for playing Redirection " )
  668. printCentred( TermH/2 - 0, " ComputerCraft Edition " )
  669. printCentred( TermH/2 + 2, " Check out the full game: " )
  670. term.setTextColor( colors.yellow )
  671. printCentred( TermH/2 + 3, " http://www.redirectiongame.com " )
  672. else
  673. printCentred( TermH/2 - 2, " Thank you for " )
  674. printCentred( TermH/2 - 1, " playing Redirection " )
  675. printCentred( TermH/2 - 0, " ComputerCraft Edition " )
  676. printCentred( TermH/2 + 2, " Check out the full game: " )
  677. term.setTextColor( colors.yellow )
  678. printCentred( TermH/2 + 3, " www.redirectiongame.com " )
  679. end
  680. parallel.waitForAll(
  681. function() sleep(2) end,
  682. function() os.pullEvent( "mouse_click" ) end
  683. )
  684. end )
  685. end
  686.  
  687. --Clear and exit--
  688. term.setCursorPos(1,1)
  689. term.setTextColor(colors.white)
  690. term.setBackgroundColor(colors.black)
  691. term.clear()
  692. if not ok then
  693. if err == "Terminated" then
  694. print( "Check out the full version of Redirection:" )
  695. print( "http://www.redirectiongame.com" )
  696. else
  697. printError( err )
  698. end
  699. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement