Advertisement
Mijyuoon

Moonscript lol

Apr 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.55 KB | None | 0 0
  1. --@name Code.Lock
  2. --@author Mijyuoon
  3.  
  4. --@include inc/sgos_sh
  5.  
  6. --@sharedscreen
  7. --@moonscript
  8.  
  9. require "inc/sgos"
  10. OS.Initialize!
  11.  
  12. import Scr, Gui, Ents from OS.Libs
  13.  
  14. --- Tab 1 (Code entry)
  15. TAB1_gui = CreateGUI
  16. __Name: "TabInput"
  17. Visible: false
  18. Dim: {
  19. x:64, y:134
  20. w:385, h:320 }
  21. State: 0
  22. AttN: 5
  23. Code: ""
  24.  
  25. --- Tab 2 (Setup)
  26. TAB2_gui = CreateGUI
  27. __Name: "TabSetup"
  28. Visible: false
  29. Dim: {
  30. x:64, y:134
  31. w:385, h:320 }
  32. Flags: 0
  33. Code: ""
  34.  
  35. --- Tab manager
  36. TABS_gui = CreateGUI
  37. __Name: "TabsBase"
  38. Visible: true
  39. Dim: {
  40. x:25, y:50
  41. w:462, h:435 }
  42. TabSel: 0
  43. StMsg: "$0"
  44.  
  45. TABS_gui\Register!
  46. TAB1_gui\Register!
  47. TAB2_gui\Register!
  48.  
  49. btninp = "123ABC456DEF7890>X"
  50. btnset = "123ABC456DEF7890<"
  51.  
  52. tabdef = {
  53. { w:140, t:"INPUT", g:TAB1_gui }
  54. { w:140, t:"SETUP", g:TAB2_gui }
  55. }
  56.  
  57. swhdef = {
  58. { w:120, t1:"SECURE", t2:"INPUT" }
  59. { w:130, t1:"DISABLE", t2:"LIMIT" }
  60. }
  61. _szx = -5
  62. for sw in *swhdef
  63. _szx += sw.w+5
  64. swhdef.sz = _szx/2
  65.  
  66. canTab = (num, ... using nil) ->
  67. if tab = tabdef[num].g
  68. if tab.CanTab
  69. return tab\CanTab ...
  70. return true
  71. false
  72.  
  73. getFlag = (num using nil) ->
  74. flgs = TAB2_gui\Get "Flags"
  75. (bit.band flgs, 2^(num-1)) != 0
  76.  
  77. if CLIENT then
  78. import rep from string
  79.  
  80. c_tb = Color 255,255,255
  81. c_tf = Color 80,180,180
  82. c_ub = Color 180,180,180
  83. c_uf = Color 40,100,100
  84. c_lb = Color 220,90,90
  85.  
  86. c_bn = Color 0,100,200
  87. c_bg = Color 40,255,40
  88. c_br = Color 255,40,40
  89. c_by = Color 255,180,40
  90.  
  91. c_wg = Color 50,180,50
  92. c_wr = Color 180,50,50
  93. c_wb = Color 10,80,180
  94.  
  95. f_th = Scr.createFont "OCR A Extended", 32, 700
  96. f_uh = Scr.createFont "OCR A Extended", 28, 400
  97. f_bt = Scr.createFont "OCR A Extended", 48, 700
  98. f_sw = Scr.createFont "OCR A Extended", 24, 700
  99.  
  100. icol = { c_tb, c_br, c_bg }
  101. imsg =
  102. "$L": {c: c_by, t:"SYS/ LOCKED"}
  103. "$A": {c: c_br, t:"NO SYS/ AUTH"}
  104. "$0": {c: c_tb, t:"CODE LOCK"}
  105.  
  106. drawRectBr = (x,y,w,h,fc,bc,bw=5 using nil) ->
  107. Scr.drawRect x, y, w, h, fc
  108. Scr.drawRectOL x, y, w, h, bc, bw
  109.  
  110. TABS_gui.OnRender = (using nil) =>
  111. {:x, :y, :w, :h} = @Dim
  112.  
  113. local ts1, ts2
  114. tpx, num = x+20, 1
  115. for tk in *tabdef
  116. ofs, c1, c2, fn = if num == @TabSel
  117. ts1, ts2 = tpx, tk.w
  118. 0, c_tf, c_tb, f_th
  119. elseif not canTab num
  120. 20, c_uf, c_lb, f_uh
  121. else
  122. 20, c_uf, c_ub, f_uh
  123. drawRectBr tpx, y+ofs, tk.w, 60-ofs, c1, c2
  124. Scr.drawText tpx+tk.w/2, y+29+ofs/2, tk.t, 1, 1, c2, fn
  125. tpx += tk.w+5; num += 1
  126.  
  127. if ts1
  128. drawRectBr x, y+55, w, h-55, c_tf, c_tb
  129. Scr.drawRect ts1+5, y+55, ts2-10, 5, c_tf
  130. else
  131. tph = (h-55)/5
  132. {t: txt, c: c1} = imsg[@StMsg]
  133. drawRectBr x, y+55, w, tph, c_uf, c_ub
  134. Scr.drawText x+w/2, y+55+tph/2, txt, 1, 1, c1, f_bt
  135.  
  136. com_tab12_keypad = (bdef, yf, bk using nil) =>
  137. {:x, :y, :w, :h} = @Dim
  138.  
  139. for iv = 1, 3 do for iu = 1, 6
  140. btxt = bdef[iu+6*(iv-1)]
  141. if btxt == "" then return
  142. bpx, bpy = x+66*(iu-1), y+yf+66*(iv-1)
  143. c1 = switch btxt
  144. when ">" then c_bg
  145. when "X","<" then c_br
  146. when "A","B","C" then c_by
  147. when "D","E","F" then c_by
  148. else c_bn
  149. bsz = (bk and btxt == bdef[-1]) and 120 or 54
  150. Scr.drawRect bpx, bpy, bsz, 54, c1
  151. Scr.drawText bpx+bsz/2, bpy+27, btxt, 1, 1, c_tb, f_bt
  152.  
  153. TAB1_gui.OnRender = (using nil) =>
  154. {:x, :y, :w, :h} = @Dim
  155. com_tab12_keypad @, btninp, 134, false
  156.  
  157. c1 = icol[@State+1] or c_tb
  158. txt = if msg = imsg[@Code]
  159. c1 = msg.c
  160. msg.t
  161. elseif getFlag 1
  162. "X"\rep #@Code
  163. else
  164. @Code
  165. drawRectBr x, y, w, 60, c_uf, c_tb, 6
  166. Scr.drawText x+w/2, y+30, txt, 1, 1, c1, f_bt
  167.  
  168. if getFlag 2
  169. drawRectBr x, y+76, w, 42, c_wr, c_tb, 4
  170. else
  171. perc, atxt = @AttN/5, tostring @AttN
  172. drawRectBr x, y+76, w-48, 42, c_wb, c_tb, 4
  173. Scr.drawRect x+8, y+84, (w-64)*perc, 26, c_tb
  174. drawRectBr x+w-42, y+76, 42, 42, c_uf, c_tb, 4
  175. Scr.drawText x+w-21, y+96, atxt, 1, 1, c_tb, f_th
  176.  
  177. TAB2_gui.OnRender = (using nil) =>
  178. {:x, :y, :w, :h} = @Dim
  179. com_tab12_keypad @, btnset, 74, true
  180.  
  181. pcode = getFlag(1) and ("X"\rep #@Code) or @Code
  182. drawRectBr x, y, w, 60, c_uf, c_tb, 6
  183. Scr.drawText x+w/2, y+30, pcode, 1, 1, c_tb, f_bt
  184.  
  185. bpx, num = x+w/2-swhdef.sz, 1
  186. for swh in *swhdef
  187. col = (bit.band(@Flags, num) > 0) and c_wg or c_wr
  188. drawRectBr bpx, y+274, swh.w, 56, col, c_tb
  189. Scr.drawText bpx+swh.w/2, y+301, swh.t1, 1, 2, c_tb, f_sw
  190. Scr.drawText bpx+swh.w/2, y+301, swh.t2, 1, 0, c_tb, f_sw
  191. bpx += swh.w+5; num *= 2
  192.  
  193. TAB2_gui.CanTab = (using nil) =>
  194. if TABS_gui\Get"StMsg" == "$A"
  195. return false
  196. Ents.owner! == Ents.player!
  197.  
  198. OS.OnRender = (using nil) ->
  199. Gui.dispatchRender!
  200.  
  201. OS.Render!
  202.  
  203. if SERVER
  204. import Time, Hook from OS.Libs
  205.  
  206. OS.WireInputs "n=Clear", "n=UnAuth"
  207. OS.WireOutputs "n=Gra", "n=Den"
  208.  
  209. inxy = (ck, x, y using nil) ->
  210. x <= ck and ck-y <= x
  211.  
  212. tabSelect = (ix using nil) ->
  213. TABS_gui\Set "TabSel", ix
  214. for i = 1, #tabdef
  215. tab = tabdef[i].g
  216. tab\Set "Visible", ix == i
  217.  
  218. setFailState = (sts, txt using nil) ->
  219. TABS_gui\Set "StMsg", txt
  220. cx, sts = if sts
  221. txt, 1
  222. else
  223. "", 0
  224. TAB1_gui\Set "Code", cx
  225. TAB1_gui\Set "State", sts
  226.  
  227. stat_noauth = false
  228. funcSetAuth = (sts using stat_noauth) ->
  229. stat_noauth = not sts
  230. if not sts
  231. if TABS_gui\Get"TabSel" > 1
  232. tabSelect 0
  233. setFailState true, "$A"
  234. elseif TAB1_gui\Get"AttN" < 1
  235. setFailState true, "$L"
  236. else
  237. setFailState false, "$0"
  238.  
  239. funcClearAtt = (using nil) ->
  240. attn = TAB1_gui\Get "AttN"
  241. TAB1_gui\Set "AttN", 5
  242. if not stat_noauth and attn == 0
  243. setFailState false, "$0"
  244.  
  245. codeCheck = (act using nil) ->
  246. cu = TAB1_gui\Get "Code"
  247. cs = TAB2_gui\Get "Code"
  248. if #cu < 1 then return
  249.  
  250. sts, wp = if cs == cu
  251. 2, "Gra"
  252. else
  253. 1, "Den"
  254. TAB1_gui\Set "State", sts
  255. if act then act (cs == cu)
  256. OS.WirePorts[wp] = 1
  257.  
  258. local attn
  259. if cs != cu and not getFlag 2
  260. attn = TAB1_gui\Get"AttN"-1
  261. TAB1_gui\Set "AttN", attn
  262. if attn < 1
  263. setFailState true, "$L"
  264.  
  265. Time.simple 0.5, ->
  266. if not attn or attn > 0
  267. setFailState false, "$0"
  268. OS.WirePorts[wp] = 0
  269.  
  270. TABS_gui.OnInput = (ply using nil) =>
  271. {:x, :y, :w, :h} = @Dim
  272. px, py = Scr.cursorPos ply
  273. if not (px and py) then return true
  274.  
  275. if inxy(py, y+25, 30) and inxy(px, x+20, w-40)
  276. tpx, num = x+20, 1
  277. for tk in *tabdef
  278. if inxy px, tpx+5, tk.w-10
  279. if not canTab num, ply then return false
  280. tabSelect (num != @TabSel) and num or 0
  281. return true
  282. tpx += tk.w+5; num += 1
  283.  
  284. com_tab12_keypad = (bdef, ply, yf, bk using nil) =>
  285. {:x, :y, :w, :h} = @Dim
  286. px, py = Scr.cursorPos ply
  287.  
  288. if inxy(px, x, w) and inxy(py, y+yf, 184)
  289. for iv = 1, 3 do for iu = 1, 6
  290. btxt = bdef[iu+6*(iv-1)]
  291. if btxt == "" then return nil
  292. bsz = (bk and btxt == bdef[-1]) and 120 or 54
  293. bpx, bpy = x+66*(iu-1), y+yf+66*(iv-1)
  294. if inxy(px, bpx, bsz) and inxy(py, bpy, 54)
  295. return btxt
  296. nil
  297.  
  298. TAB1_gui.OnInput = (ply using nil) =>
  299. {:x, :y, :w, :h} = @Dim
  300. px, py = Scr.cursorPos ply
  301. if not (px and py) then return true
  302.  
  303. if btxt = com_tab12_keypad @, btninp, ply, 134, false
  304. if @State > 0 then return true
  305. switch btxt
  306. when "X" then @Code = ""
  307. when ">" then codeCheck!
  308. else if #@Code < 12
  309. @Code ..= btxt
  310. return true
  311.  
  312. TAB2_gui.OnInput = (ply using nil) =>
  313. {:x, :y, :w, :h} = @Dim
  314. px, py = Scr.cursorPos ply
  315. if not (px and py) then return true
  316.  
  317. if btxt = com_tab12_keypad @, btnset, ply, 74, true
  318. if btxt == "<"
  319. @Code = @Code\sub 1, -2
  320. elseif #@Code < 12
  321. @Code ..= btxt
  322. return true
  323.  
  324. sww, swx = swhdef.sz*2, x+w/2-swhdef.sz
  325. if inxy(px, swx, sww) and inxy(py, y+279, 46)
  326. bpx, num = swx, 1
  327. for swh in *swhdef
  328. if inxy(px, bpx+5, swh.w-10)
  329. @Flags = bit.bxor @Flags, num
  330. return true
  331. bpx += swh.w+5; num *= 2
  332.  
  333. TAB2_gui.CanTab = (ply using nil) =>
  334. if stat_noauth then return false
  335. ply == Ents.owner!
  336.  
  337. local use_timeout
  338. Hook.add "think", "idle", (using use_timeout) ->
  339. if not use_timeout then return
  340. if Time.curtime! >= use_timeout
  341. use_timeout = nil
  342. tabSelect 0
  343.  
  344. OS.OnPlayerUse = (ply using use_timeout) ->
  345. Gui.dispatchInput ply
  346. use_timeout = Time.curtime! + 10
  347.  
  348. OS.OnTrigger_Clear = (val using nil) ->
  349. if val < 1 then return
  350. funcClearAtt!
  351.  
  352. OS.OnTrigger_UnAuth = (val using nil) ->
  353. funcSetAuth (val < 1)
  354.  
  355. export _ENT = Ents.self!
  356. _ENT.is_CodeLock = true
  357. _ENT.funcSetAuth = wrapContext funcSetAuth
  358. _ENT.funcClearAtt = wrapContext funcClearAtt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement