Advertisement
Guest User

ltbge 1.0

a guest
Sep 21st, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.72 KB | None | 0 0
  1. --
  2. -- ltbge: a Lua text-based game engine
  3. -- version 1.0
  4. -- by ntnick
  5. --
  6.  
  7. -- Bonus function!
  8. -- this function is by Konlab and theOriginalBIT.
  9. function string.seperate(str, separator)
  10.   local t = {}
  11.   for s in str:gmatch("[^"..separator.."]+") do
  12.         t[#t+1] = s
  13.   end
  14.   return t
  15. end
  16. -------------------------------------------------
  17. if(not string.seperate) then
  18.     error("ltbge: couldn't find string.seperate.",1)
  19. end
  20.  
  21.  
  22. ltbge = {
  23.     class = {
  24.         seaol = "001";
  25.     };
  26.     core = {
  27.         version = 1.0;
  28.         seaol = "002";
  29.     };
  30.     ascii = {
  31.         seaol = "003";
  32.     };
  33.     files = {
  34.         seaol = "004";
  35.     };
  36.     time = {
  37.         seaol = "005";
  38.     };
  39.     colours = {
  40.         bash = {
  41.             -- Taken from the Arch Wiki.
  42.             txtblk='\\e[0;30m';
  43.             txtred='\\e[0;31m';
  44.             txtgrn='\\e[0;32m';
  45.             txtylw='\\e[0;33m';
  46.             txtblu='\\e[0;34m';
  47.             txtpur='\\e[0;35m';
  48.             txtcyn='\\e[0;36m';
  49.             txtwht='\\e[0;37m';
  50.             bldblk='\\e[1;30m';
  51.             bldred='\\e[1;31m';
  52.             bldgrn='\\e[1;32m';
  53.             bldylw='\\e[1;33m';
  54.             bldblu='\\e[1;34m';
  55.             bldpur='\\e[1;35m';
  56.             bldcyn='\\e[1;36m';
  57.             bldwht='\\e[1;37m';
  58.             unkblk='\\e[4;30m';
  59.             undred='\\e[4;31m';
  60.             undgrn='\\e[4;32m';
  61.             undylw='\\e[4;33m';
  62.             undblu='\\e[4;34m';
  63.             undpur='\\e[4;35m';
  64.             undcyn='\\e[4;36m';
  65.             undwht='\\e[4;37m';
  66.             bakblk='\\e[40m';
  67.             bakred='\\e[41m';
  68.             bakgrn='\\e[42m';
  69.             bakylw='\\e[43m';
  70.             bakblu='\\e[44m';
  71.             bakpur='\\e[45m';
  72.             bakcyn='\\e[46m';
  73.             bakwht='\\e[47m';
  74.             txtrst='\\e[0m';
  75.         };
  76.         seaol = "006";
  77.     };
  78. }
  79.  
  80. -- Key:
  81. -- <String className> = required variable
  82. -- [Function callback] = not required variable
  83.  
  84. -- ltbge.class.extend( <String className>, <Table o>, [Function callback] )
  85. -- Extend a class.
  86. -- For example:
  87. -- ltbge.class.extend("Hello",{
  88. --  sayHello = function() print("Hello, world!") end;
  89. -- })
  90. -- and later on, you can do this:
  91. -- ltbge.Hello.sayHello()
  92. --
  93. -- If you want to use callbacks:
  94. -- ltbge.class.extend("Hello",{
  95. --  sayHello = function() print("Hello, world!") end;
  96. -- }, function() print("ltbge.Hello created") end)
  97.  
  98. function ltbge.class.extend(className,o,...)
  99.     if(type(className) ~= "string") then
  100.         error("ltbge.class.extend: expected string, not "..type(className),1)
  101.     end
  102.     if(type(o) ~= "table") then
  103.         error("ltbge.class.extend: expected table, not "..type(className),1)
  104.     end
  105.     for i=1,#ltbge do
  106.         if(type(ltbge[i]) == "table") then
  107.             if(not ltbge[i].seaol) then
  108.                 ltbge[className] = o
  109.                 break
  110.             end
  111.         end
  112.     end
  113.     if(arg[1]) then
  114.         if(type(arg[1]) ~= "function") then
  115.             error("ltbge.class.extend: expected function, not "..type(arg[1]),1)
  116.         end
  117.         (arg[1])()
  118.     end
  119. end
  120.  
  121. -- ltbge.class.del( <String className>, [Function callback] )
  122. -- Delete a class.
  123. -- For example:
  124. -- ltbge.class.extend("mo",{hello = "Hi"})
  125. -- ltbge.class.del("mo")
  126. --
  127. -- If you want to use callbacks:
  128. -- ltbge.class.extend("mo",{hello = "Hi"},function() print("Hi") end)
  129. -- ltbge.class.del("mo",function() print("Hello, world!") end)
  130.  
  131. function ltbge.class.del(className,...)
  132.     if(type(className) ~= "string") then
  133.         error("ltbge.class.del: expected string, not "..type(className),1)
  134.     end
  135.     if(ltbge[className]) then
  136.         if(not ltbge[className].seaol) then
  137.             ltbge[className] = nil
  138.         end
  139.     end
  140.     if(arg[1]) then
  141.         if(type(arg[1]) ~= "function") then
  142.             error("ltbge.class.del: expected function, not "..type(arg[1]),1)
  143.         end
  144.         (arg[1])()
  145.     end
  146. end
  147.  
  148. -- ltbge.ascii.getAsciiFromFile( <String file>, [Function callback] )
  149. -- Get ASCII art from a file.
  150. -- For example:
  151. -- local myAscii = ltbge.ascii.getAsciiFromFile("resetti.txt")
  152. -- for i=1, #myAscii do
  153. --  print(myAscii[i])
  154. -- end
  155. --
  156. -- If you want to use callbacks:
  157. -- local myAscii = ltbge.ascii.getAsciiFromFile("resetti.txt",function() print("Loaded Resetti.txt") end)
  158. -- for i=1, #myAscii do
  159. --  print(myAscii[i])
  160. -- end
  161.  
  162. function ltbge.ascii.getAsciiFromFile(file,...)
  163.     if(type(file) ~= "string") then
  164.         error("ltbge.ascii.getAsciiFromFile: expected string, not "..type(file),1)
  165.     end
  166.     local f = io.open(file,"rb")
  167.     if(f == nil) then
  168.         error("ltbge.ascii.getAsciiFromFile: file "..file.." does not exist",1)
  169.     end
  170.     if(arg[1]) then
  171.         if(type(arg[1]) ~= "function") then
  172.             error("ltbge.ascii.getAsciiFromFile: expected function, not "..type(arg[1]),1)
  173.         end
  174.         (arg[1])()
  175.     end
  176.     return string.seperate(f,"\n")
  177. end
  178.  
  179. -- ltbge.files.writeTo( <String file>, <String content>, [Function callback] )
  180. -- Write data to a file.
  181. -- For example:
  182. -- ltbge.files.writeTo("resetti.txt","NO RESETTING!")
  183. --
  184. -- If you want to use callbacks:
  185. -- ltbge.files.writeTo("resetti.txt","NO RESETTING!",function()
  186. --  print("Wrote to resetti.txt")
  187. -- end)
  188.  
  189. function ltbge.files.writeTo(file,content,...)
  190.     if(type(file) ~= "string") then
  191.         error("ltbge.files.writeTo: expected string, not "..type(file),1)
  192.     end
  193.     if(type(content) ~= "string" and type(content) ~= "number") then
  194.         error("ltbge.files.writeTo: expected string/number, not "..type(content),1)
  195.     end
  196.     local f = io.open(file,"w")
  197.     f:write(content)
  198.     f:close()
  199.     if(arg[1]) then
  200.         if(type(arg[1]) ~= "function") then
  201.             error("ltbge.files.writeTo: expected function, not "..type(arg[1]),1)
  202.         end
  203.         (arg[1])()
  204.     end
  205. end
  206.  
  207. -- ltbge.time.getTime([Function callback])
  208. -- Returns the time.
  209. -- For example:
  210. -- local time = ltbge.time.getTime()
  211. -- print(time.hour..":"..time.min)
  212. --
  213. -- If you want to use callbacks:
  214. -- local n = ltbge.time.getTime(function()
  215. --  print("Time obtained")
  216. -- end)
  217. -- print(n.hour..":"..n.min)
  218.  
  219. function ltbge.time.getTime(...)
  220.     local o = os.date()
  221.     local f = string.seperate(o," ")
  222.     local n = string.seperate(f[2],":")
  223.     local g = {hour = n[2], min = n[3]}
  224.     if(arg[1]) then
  225.         if(type(arg[1]) ~= "function") then
  226.             error("ltbge.time.getTime: expected function, not "..type(arg[1]),1)
  227.         end
  228.         (arg[1])()
  229.     end
  230.     return g
  231. end
  232.  
  233. -- ltbge.time.get24HourTime([Function callback])
  234. -- Returns 24-hour time.
  235. -- For example:
  236. -- local time = ltbge.time.get24HourTime()
  237. -- print("24-hour time: "..time.hour..":"..time.min)
  238. --
  239. -- If you want to use callbacks:
  240. -- local n = ltbge.time.get24HourTime(function()
  241. --  print("24-hour time obtained")
  242. -- end)
  243.  
  244. function ltbge.time.get24HourTime(...)
  245.     local o = os.date()
  246.     local f = string.seperate(o," ")
  247.     local n = string.seperate(f[2],":")
  248.     -- To get 24-hour time:
  249.     -- Take the hour of the current time and multiply it by 2.
  250.     -- Example: 12:45:65 would be 24:45:65.
  251.     --
  252.     -- To convert 24-hour time to regular time:
  253.     -- Take the hour of the current time and divide it by 2.
  254.     -- Example: 24:45:65 would be 12:45:65.
  255.     local g = {hour = n[2]*2, min = n[3]}
  256.     if(arg[1]) then
  257.         if(type(arg[1]) ~= "function") then
  258.             error("ltbge.time.get24HourTime: expected function, not "..type(arg[1]),1)
  259.         end
  260.         (arg[1])()
  261.     end
  262.     return g
  263. end
  264.  
  265. -- That's it.
  266. -- Use ltbge responsibly.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement