Advertisement
Short_Circuit

OCDLib Version 0.7.3

Jan 25th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.03 KB | None | 0 0
  1. --[[
  2.  
  3. OCDLib by Short Circuit
  4. For use by OCD people who want cool functions for Lua. :P
  5.  
  6.  
  7.  
  8. CURRENT VERSION: 0.7.3
  9.  
  10.  
  11.  
  12. CHANGELOG:
  13.  
  14.     0.7.3:
  15.         [+] OCDLib.isDefined(), a function that works much like defined(); in PHP.
  16.         [~] Edited credits styling to add a bit more jazz.
  17.  
  18. =================================================
  19.  
  20.     0.7:
  21.         [~] betterRandom now supports an optional multi-number generation mode. Specify a third argument (a number value), and it will return a table containing the numbers you requested.
  22.         [\] Note: If no count is specified, the old functionality will be used.
  23.  
  24. =================================================
  25.  
  26.     0.6:
  27.         [+] Added dumpTable function, which dumps all the data in a single table in a nicely formatted way.
  28.         [+] OCDLib test function (OCDLibPrivateFuncs.testLib())
  29.         [+] OCDLib.version(), which prints the current version of OCDLib to STDOUT.
  30.  
  31. =================================================
  32.  
  33.     0.5.5.2:
  34.         [+] Legacy script support (trueRandom function passes to betterRandom)
  35.  
  36. =================================================
  37.  
  38.     0.5.5:
  39.         [+] New comments
  40.         [~] Improved comments
  41.         [~] errorDetection now exists inside of its own private library for use by OCDLib only.
  42.         [@] Fixed credits so that (in theory) it will actually work.
  43.         [~] Renamed trueRandom to betterRandom to reflect its psuedo-randomness.
  44.  
  45. =================================================
  46.  
  47.     0.5:
  48.         [+] repeatPrint, a function that prints a string for the amount of times you tell it to.
  49.         [\] Note: If no number is specified, repeatPrint will print the number five times by default.
  50.  
  51. =================================================
  52.  
  53.     0.4:
  54.         [~] Code minimizations
  55.         [~] Better formatting
  56.         [+] New comments
  57.         [~] Better comments
  58.         [~] Now dumps 10 random numbers into the dumping grounds before nilling it instead of 5.
  59.  
  60. =================================================
  61.  
  62.     0.1:
  63.         [=] Initial release
  64.  
  65. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  66.  
  67.     Changelog format:
  68.         [+] -> Addition
  69.         [~] -> Change of pre-existing thing
  70.         [-] -> Removal of something
  71.         [!] -> Known bug added
  72.         [@] -> Bug removal
  73.         [\] -> Comment on above change
  74.         [=] -> Misc/uncategorized
  75.  
  76. ]]--
  77.  
  78.  
  79. --Init the library
  80. OCDLib = {}
  81.  
  82. --Init our private function library that will be used mainly for things like errorDetection
  83. --Note: This will be used for more later. Just an OCD thing to exclude it from the main lib.
  84. OCDLibPrivateFuncs = {}
  85.  
  86. --Define the version we're currently using (for OCDLib.version())
  87. version = "0.7.3"
  88.  
  89. --Pre-formatted error handler.
  90. function OCDLibPrivateFuncs.errorDetection( func )
  91.     error( "[OCDLib]: OCDLib." .. func .. "() experienced an error. Please check your syntax." )
  92. end
  93.  
  94. --Give us some space, will 'ya?
  95. function OCDLib.makeSomeRoom( spaces )
  96.     --Checks if any specific number of spaces is needed.
  97.     --(If not, return 2.)
  98.     if spaces ~= nil then
  99.         for i = 1, spaces do
  100.             --\n also works... just don't feel like it. :P
  101.             print( "" )
  102.         end
  103.     --Again, if no specific spaces are returned, use 2 as the default.
  104.     elseif spaces == nil then
  105.         for i = 1, 2 do
  106.             print( "" )
  107.         end
  108.     --If something fails somehow... use our errorDetection function! :D
  109.     else
  110.         OCDLibPrivateFuncs.errorDetection( "makeSomeRoom" )
  111.     end
  112. end
  113.  
  114. --DOS/Windows like pause function.
  115. function OCDLib.pauseAndWait( textstr )
  116.     --If you want, define the text to be printed here.
  117.     if textstr ~= nil then
  118.         print( textstr )
  119.     elseif textstr == nil then
  120.         --If no text is defined, default to this.
  121.         print( "Press Enter to continue..." )
  122.     --If something fails somehow... use our errorDetection function! :D
  123.     else
  124.         OCDLibPrivateFuncs.errorDetection( "pauseAndWait" )
  125.     end
  126.     --Actually wait for enter. :P
  127.     io.read()
  128. end
  129.  
  130. --Legacy support.
  131. function OCDLib.trueRandom( minpass, maxpass )
  132.     return OCDLib.betterRandom( minpass, maxpass )
  133. end
  134.  
  135. --Psuedo true randomness generator.
  136. function OCDLib.betterRandom( min, max, numberToGen )
  137.     --Init the randomseed and throw away the first few numbers as they may not always be random.
  138.     math.randomseed( os.time() )
  139.     --Make a dumping ground for useless numbers.
  140.     uselessDumpingGrounds = {}
  141.     for i = 1,10 do
  142.         --Insert the useless numbers into our dumping grounds.
  143.         uselessDumpingGrounds[i] = math.random()
  144.     end
  145.     --Clear up the memory space used by our dumping grounds.
  146.     uselessDumpingGrounds = nil
  147.     if numberToGen == nil then
  148.         if min and max ~= nil then
  149.             random = math.random( min, max )
  150.         --Check for anything that might cause errors...
  151.         elseif min == nil then
  152.             random = math.random( max )
  153.         elseif max == nil then
  154.             random = math.random( min, 10 )
  155.         elseif min and max == nil then
  156.             random = math.random( 10 )
  157.         --If something fails somehow... use our errorDetection function! :D
  158.         else
  159.             OCDLibPrivateFuncs.errorDetection( "betterRandom" )
  160.         end
  161.     elseif numberToGen ~= nil then
  162.         random = {}
  163.         for i = 1, numberToGen do
  164.             if min and max ~= nil then
  165.                 random[i] = math.random( min, max )
  166.             elseif min == nil then
  167.                 random[i] = math.random( max )
  168.             elseif max == nil then
  169.                 random[i] = math.random( min, 10 )
  170.             elseif min and max == nil then
  171.                 random[i] = math.random( 10 )
  172.             else
  173.                 OCDLibPrivateFuncs.errorDetection( "betterRandom" )
  174.             end
  175.         end
  176.     else
  177.         OCDLibPrivateFuncs.errorDetection( "betterRandom" )
  178.     end
  179.     return random
  180. end
  181.  
  182. --Easy credits system.
  183. function OCDLib.credits( scriptname, creator, date, use )
  184.     if scriptname ~= nil and creator ~= nil and date ~= nil and use ~= nil then
  185.         print( "=---------------------------=" )
  186.         print( "Script name: " .. scriptname )
  187.         print( "Author: " .. creator )
  188.         print( "Created on: " .. date )
  189.         print( "Used for: " .. use )
  190.         print( "=---------------------------=" )
  191.     elseif use == nil or date == nil or creator == nil or scriptname == nil then
  192.         error( "OCDLib.credits(): You must specify every argument! *Note- a \"\" will work." )
  193.     else
  194.         OCDLibPrivateFuncs.errorDetection( "credits" )
  195.     end
  196. end
  197.  
  198. --Print a string for the desired number of times
  199. function OCDLib.repeatPrint( printStr, numTimes )
  200.     --Init i as a number used to count n' stuff
  201.     i = 1
  202.     if numTimes ~= nil then
  203.         for i = 1, numTimes do
  204.             print( printStr )
  205.         end
  206.     --Default to 5 if no number of times is specified
  207.     elseif numTimes == nil then
  208.         for i = 1, 5 do
  209.             print( printStr )
  210.         end
  211.     else
  212.         OCDLibPrivateFuncs.errorDetection( "repeatPrint" )
  213.     end
  214. end
  215.  
  216. --Dumps all the data in a single table
  217. function OCDLib.dumpTable( tab )
  218.     for k, v in ipairs( tab ) do
  219.         print( k  .. ": " .. v )
  220.     end
  221. end
  222.  
  223. --If anyone wants, they may print the OCDLib version used in their script. This is not a requirement, but is nice. :P
  224. function OCDLib.version()
  225.     print( "-- This script uses OCDLib version " .. version .. ", created by Short Circuit. --" )
  226. end
  227.  
  228. --OCDLib test function
  229. --For use for debug only
  230. function OCDLibPrivateFuncs.testLib()
  231.     print( "\nOCDLib.version()" )
  232.     OCDLib.version()
  233.     print( "\nOCDLib.dumpTable()" )
  234.     tableToDump = { "pie", "cupcakes", "cake", "apples" }
  235.     OCDLib.dumpTable( tableToDump )
  236.     print( "\nOCDLib.makeSomeRoom()" )
  237.     OCDLib.makeSomeRoom( 2 )
  238.     print( "\nOCDLib.betterRandom() legacy support test" )
  239.     rand = OCDLib.trueRandom( nil, 45 )
  240.     print( rand )
  241.     print( "\nOCDLib.betterRandom()" )
  242.     rand2 = OCDLib.betterRandom( nil, 45 )
  243.     print( rand2 )
  244.     print( "\nOCDLib.credits()" )
  245.     OCDLib.credits( "OCDLib Test",
  246.         "Short Cirucit",
  247.         "2/22/14",
  248.         "Testing the OCDLib functions as they exist." )
  249.     print( "\nOCDLib.repeatPrint()" )
  250.     OCDLib.repeatPrint( "hai guys", 3 )
  251.     print( "\nOCDLib.isDefined() (defined var)" )
  252.     variable = 10
  253.     print( OCDLib.isDefined( variable ) )
  254.     print( "\nOCDLib.isDefined() (nil var)" )
  255.     print( OCDLib.isDefined( nilVar ) )
  256.     print( "\nOCDLib.pauseAndWait()" )
  257.     OCDLib.pauseAndWait( "Press enter to complete the test..." )
  258.     print( "\nOCDLibPrivateFuncs.errorDetection()" )
  259.     OCDLibPrivateFuncs.errorDetection( "testLib" )
  260. end
  261.  
  262. --Check if a variable is defined, much like PHP's defined().
  263. function OCDLib.isDefined( var )
  264.     if var ~= nil then
  265.         return true
  266.     else
  267.         return false
  268.     end
  269. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement