Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- OCDLib by Short Circuit
- For use by OCD people who want cool functions for Lua. :P
- CURRENT VERSION: 0.7.3
- CHANGELOG:
- 0.7.3:
- [+] OCDLib.isDefined(), a function that works much like defined(); in PHP.
- [~] Edited credits styling to add a bit more jazz.
- =================================================
- 0.7:
- [~] 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.
- [\] Note: If no count is specified, the old functionality will be used.
- =================================================
- 0.6:
- [+] Added dumpTable function, which dumps all the data in a single table in a nicely formatted way.
- [+] OCDLib test function (OCDLibPrivateFuncs.testLib())
- [+] OCDLib.version(), which prints the current version of OCDLib to STDOUT.
- =================================================
- 0.5.5.2:
- [+] Legacy script support (trueRandom function passes to betterRandom)
- =================================================
- 0.5.5:
- [+] New comments
- [~] Improved comments
- [~] errorDetection now exists inside of its own private library for use by OCDLib only.
- [@] Fixed credits so that (in theory) it will actually work.
- [~] Renamed trueRandom to betterRandom to reflect its psuedo-randomness.
- =================================================
- 0.5:
- [+] repeatPrint, a function that prints a string for the amount of times you tell it to.
- [\] Note: If no number is specified, repeatPrint will print the number five times by default.
- =================================================
- 0.4:
- [~] Code minimizations
- [~] Better formatting
- [+] New comments
- [~] Better comments
- [~] Now dumps 10 random numbers into the dumping grounds before nilling it instead of 5.
- =================================================
- 0.1:
- [=] Initial release
- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
- Changelog format:
- [+] -> Addition
- [~] -> Change of pre-existing thing
- [-] -> Removal of something
- [!] -> Known bug added
- [@] -> Bug removal
- [\] -> Comment on above change
- [=] -> Misc/uncategorized
- ]]--
- --Init the library
- OCDLib = {}
- --Init our private function library that will be used mainly for things like errorDetection
- --Note: This will be used for more later. Just an OCD thing to exclude it from the main lib.
- OCDLibPrivateFuncs = {}
- --Define the version we're currently using (for OCDLib.version())
- version = "0.7.3"
- --Pre-formatted error handler.
- function OCDLibPrivateFuncs.errorDetection( func )
- error( "[OCDLib]: OCDLib." .. func .. "() experienced an error. Please check your syntax." )
- end
- --Give us some space, will 'ya?
- function OCDLib.makeSomeRoom( spaces )
- --Checks if any specific number of spaces is needed.
- --(If not, return 2.)
- if spaces ~= nil then
- for i = 1, spaces do
- --\n also works... just don't feel like it. :P
- print( "" )
- end
- --Again, if no specific spaces are returned, use 2 as the default.
- elseif spaces == nil then
- for i = 1, 2 do
- print( "" )
- end
- --If something fails somehow... use our errorDetection function! :D
- else
- OCDLibPrivateFuncs.errorDetection( "makeSomeRoom" )
- end
- end
- --DOS/Windows like pause function.
- function OCDLib.pauseAndWait( textstr )
- --If you want, define the text to be printed here.
- if textstr ~= nil then
- print( textstr )
- elseif textstr == nil then
- --If no text is defined, default to this.
- print( "Press Enter to continue..." )
- --If something fails somehow... use our errorDetection function! :D
- else
- OCDLibPrivateFuncs.errorDetection( "pauseAndWait" )
- end
- --Actually wait for enter. :P
- io.read()
- end
- --Legacy support.
- function OCDLib.trueRandom( minpass, maxpass )
- return OCDLib.betterRandom( minpass, maxpass )
- end
- --Psuedo true randomness generator.
- function OCDLib.betterRandom( min, max, numberToGen )
- --Init the randomseed and throw away the first few numbers as they may not always be random.
- math.randomseed( os.time() )
- --Make a dumping ground for useless numbers.
- uselessDumpingGrounds = {}
- for i = 1,10 do
- --Insert the useless numbers into our dumping grounds.
- uselessDumpingGrounds[i] = math.random()
- end
- --Clear up the memory space used by our dumping grounds.
- uselessDumpingGrounds = nil
- if numberToGen == nil then
- if min and max ~= nil then
- random = math.random( min, max )
- --Check for anything that might cause errors...
- elseif min == nil then
- random = math.random( max )
- elseif max == nil then
- random = math.random( min, 10 )
- elseif min and max == nil then
- random = math.random( 10 )
- --If something fails somehow... use our errorDetection function! :D
- else
- OCDLibPrivateFuncs.errorDetection( "betterRandom" )
- end
- elseif numberToGen ~= nil then
- random = {}
- for i = 1, numberToGen do
- if min and max ~= nil then
- random[i] = math.random( min, max )
- elseif min == nil then
- random[i] = math.random( max )
- elseif max == nil then
- random[i] = math.random( min, 10 )
- elseif min and max == nil then
- random[i] = math.random( 10 )
- else
- OCDLibPrivateFuncs.errorDetection( "betterRandom" )
- end
- end
- else
- OCDLibPrivateFuncs.errorDetection( "betterRandom" )
- end
- return random
- end
- --Easy credits system.
- function OCDLib.credits( scriptname, creator, date, use )
- if scriptname ~= nil and creator ~= nil and date ~= nil and use ~= nil then
- print( "=---------------------------=" )
- print( "Script name: " .. scriptname )
- print( "Author: " .. creator )
- print( "Created on: " .. date )
- print( "Used for: " .. use )
- print( "=---------------------------=" )
- elseif use == nil or date == nil or creator == nil or scriptname == nil then
- error( "OCDLib.credits(): You must specify every argument! *Note- a \"\" will work." )
- else
- OCDLibPrivateFuncs.errorDetection( "credits" )
- end
- end
- --Print a string for the desired number of times
- function OCDLib.repeatPrint( printStr, numTimes )
- --Init i as a number used to count n' stuff
- i = 1
- if numTimes ~= nil then
- for i = 1, numTimes do
- print( printStr )
- end
- --Default to 5 if no number of times is specified
- elseif numTimes == nil then
- for i = 1, 5 do
- print( printStr )
- end
- else
- OCDLibPrivateFuncs.errorDetection( "repeatPrint" )
- end
- end
- --Dumps all the data in a single table
- function OCDLib.dumpTable( tab )
- for k, v in ipairs( tab ) do
- print( k .. ": " .. v )
- end
- end
- --If anyone wants, they may print the OCDLib version used in their script. This is not a requirement, but is nice. :P
- function OCDLib.version()
- print( "-- This script uses OCDLib version " .. version .. ", created by Short Circuit. --" )
- end
- --OCDLib test function
- --For use for debug only
- function OCDLibPrivateFuncs.testLib()
- print( "\nOCDLib.version()" )
- OCDLib.version()
- print( "\nOCDLib.dumpTable()" )
- tableToDump = { "pie", "cupcakes", "cake", "apples" }
- OCDLib.dumpTable( tableToDump )
- print( "\nOCDLib.makeSomeRoom()" )
- OCDLib.makeSomeRoom( 2 )
- print( "\nOCDLib.betterRandom() legacy support test" )
- rand = OCDLib.trueRandom( nil, 45 )
- print( rand )
- print( "\nOCDLib.betterRandom()" )
- rand2 = OCDLib.betterRandom( nil, 45 )
- print( rand2 )
- print( "\nOCDLib.credits()" )
- OCDLib.credits( "OCDLib Test",
- "Short Cirucit",
- "2/22/14",
- "Testing the OCDLib functions as they exist." )
- print( "\nOCDLib.repeatPrint()" )
- OCDLib.repeatPrint( "hai guys", 3 )
- print( "\nOCDLib.isDefined() (defined var)" )
- variable = 10
- print( OCDLib.isDefined( variable ) )
- print( "\nOCDLib.isDefined() (nil var)" )
- print( OCDLib.isDefined( nilVar ) )
- print( "\nOCDLib.pauseAndWait()" )
- OCDLib.pauseAndWait( "Press enter to complete the test..." )
- print( "\nOCDLibPrivateFuncs.errorDetection()" )
- OCDLibPrivateFuncs.errorDetection( "testLib" )
- end
- --Check if a variable is defined, much like PHP's defined().
- function OCDLib.isDefined( var )
- if var ~= nil then
- return true
- else
- return false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement