ArchUtils (and others)

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
ArchAngel075
Party member
Posts: 319
Joined: Mon Jun 24, 2013 5:16 am

ArchUtils (and others)

Post by ArchAngel075 »

So I have been using my own small concoction of utilities i wrote up when the need arised...
And such I decided to share them as i felt it perhaps will help others :awesome:

How to include ArchUtils into your projects :
simply use 'require' :

Code: Select all

ATils = require"ArchUtils"
So first is the functions exposed :

(the following was not written by me, but i include it regardless- credits go to the original writer...)

Code: Select all

ATils.deepcopy(orig)
--@orig is a table passed,
--Takes the @orig and returns a deepcopy of the table
(the following was written by me:)

Code: Select all

ATils.stiHelper.constructFromMap(STI_Map)
--[[
 @STI_Map is a map returned by the STI.new function from STI
 returns an array of all the tiles in the passed map @STI_Map for use in a-star library.
--]]
Note that the a-star library is available at the bottom of this post...

Code: Select all

ATils.stiHelper.getTileAt(STI_Map,x_table,y_var)
--[[
 @STI_Map is a map returned by the STI.new function from STI
 @x_table is one of two types :
  If a table, then the x,y value is pulled from the table from the first and second index or from the x_table.x and x_table.y index
  IE, the table can be {123,456} where x = 123 and y = 456 OR
  the table can be {x=123,y=456} where x = 123  and y = 456.
  
 IF x_table is a integer then x = x_table and y = @y_var (also a integer)
 Thus multiple formats of arguments are supported!

 RETURNS :
 the Tile at the coordinates x,y or nil if no table found...
--]]

Code: Select all

ATils.stiHelper.getTileProperties(STI_Map,tileset,tile_var)
--[[
 @STI_Map is a map returned by the STI.new function from STI
 @tileset is the tileset to use, if nil then uses the first/default tileset obtained from the map.
 @tile_var is one of two types :
  IF a table then it behaves like x_table, fetching the tile at the coordinates passed
  IF a tile from the STI_Map then uses that tile directly (NOTE this may be bugged, recommended to use coordinates for now)
 
 Returns the properties the tile at the coordinates/or/provided holds with relation to the passed/used tileset
 Returned as a table.
--]]

Code: Select all

ATils.getScaleForImage(image,xScale,yScale)
--[[
 @image is a love.image or love.imageData
 @xScale and @yScale is the scale that is wished to obtain

 Returns two values separately newW and newH - the scales to be used to draw at.

 What it does :
  Takes the image and scales provided and determins what scale must the image be drawn at to be xScale and yScale big,
  IE, a 64x64 image drawn in the size 32x32 will therefor look like :
  someImage (is the image, 64x64 to be drawn in 32x32)
  local scaleW,scaleH = ATils.getScaleForImage(someImage ,32,32)
  love.graphics.draw(someImage,1,1,0,scaleW,scaleH) -- draw the 64x64 image in the size 32x32

 Useful for tiles and resizing images to desired scale easily
--]]

Code: Select all

ATils.gTBL(t,index)
--[[
 @t a table to be printed out
 @index not important, but sets the index to use when spacing the printout. dont pass anything if unsure.

 Simply returns the table passed as a easy-ish to read string, I use it abusively to debug tables and databases  :crazy: 

 -Note the function does not assert if @t is a table before iteration, therefore it WILL error if @t is not a error. This is due to lazyness on my part...
--]]

Code: Select all

ATils.Len(t)
--[[
 @t is a table passed
 Returns the true length of any table passed as @t
  Useful for when #TABLE equates to 0 despite containing items.
 
 I found this useful when some strange things started happening to some tables i was operating on...
 --this does do type checking on t so passing a non-table value is safe.
--]]
(note the following is a large 'module' that works together...)
(refer to it as the Config or InI/ini/INI module)

Code: Select all

ATils.get_CFG(path,CFGNAME,section,param)
--[[
 @path is the path to the DIRECTORY of the config file.
 @CFGNAME is the config files name excluding the .ini extention
 @section is the ini section to use
 @param is the parameter to look up

 Returns the value of the parameter @param under the section @section as a string.
 If @section is nil then looks for a GLOBAL parameter
--]]

Code: Select all

ATils.add_CFG(path,CFGNAME,section,param,value)
--[[
 @path is the path to the DIRECTORY of the config file.
 @CFGNAME is the config files name excluding the .ini extention
 @section is the ini section to use
 @param is the parameter to use
 @value is the new value of the parameter

 attempts to add the section @section and then @param to the ini file,
 NOTE it does not overwrite existing parameters, use set_CFG for that
 NOTE if @section is nil then uses a GLOBAL parameter and does not create any sections
 NOTE if @param is nil and @section is not nil then only attempts to create the section @section

 Returns : nothing
--]]

Code: Select all

ATils.write_CFG(path,CFGNAME,CFG_Table)
--[[
 @path is the path to the DIRECTORY of the config file.
 @CFGNAME is the config files name excluding the .ini extention
 @CFG_Table is a table formatted correctly - a-bit hefty to explain how this table is formatted... 
Returns : nothing

 It is highly recommended that you stay clear of this function, it is used internally by add_CFG,
 If you want to set the value of parameters then use add_CFG instead! 
--]]

Code: Select all

ATils.load_CFG(path,CFGNAME)
--[[
 @path is the path to the DIRECTORY of the config file.
 @CFGNAME is the config files name excluding the .ini extention

 What it Does :
  reads a .ini file and returns it as a table where :
   All sections are tables comprised of its parameters.
   the non-section(GLOBAL) parameters are stored in a table whose key is '_'
 
 It is highly recommended that you stay clear of this function, it is used internally by the CFG utilities.

 If you wish to utilize this however, then i reccomend examining the format of the table returned with gTBL()
--]]

Code: Select all

ATils.set_CFG(path,CFGNAME,section,param,with)
--[[
 @path is the path to the DIRECTORY of the config file.
 @CFGNAME is the config files name excluding the .ini extention
 @section is the ini section to use
 @param is the parameter to use
 @value is the new value of the parameter

 attempts to set the section @section and then @param in the ini file,
 NOTE it DOES overwrite existing parameters, use add_CFG for non-overwriting
 NOTE if @section is nil then uses a GLOBAL parameter and does not set/create any sections
 NOTE if @param is nil and @section is not nil then only attempts to set the section @section

 Returns : nothing
--]]
NOTE that at the moment you need to hand create the config files, the utilities do not attempt to create them itself yet.
!!ALSO NOTE!! :
With regards to how the format of the .ini files are handled there is some small deviations from the standard :

All parameters must be devoid of white space between the '=' and the value after the '='
All parameters must be suffixed by an ';' character after the value,

An example of a .ini file :
--------------------------------------------------------------------------
notSectioned=true
isGlobalParam=true

[Core]
debug1=value1;
debug2=value2;

[NotCore]
something=something;
something2=nothing;

[UserData]
skinLocation=/UserData/Mods/SomeMod/resource/default.png;
PlayerName=AnotherNooblet;
---------------------------------------------------------
Notice that there is nothing like 'debug1 = something;' , whitespaces are evil....

It is a smallish set of deviations for a amazingly great feature i think :ehem:

Next is the a-star library i found laying around ... (all credits go to the original writer of the library)
I only produced the small function above to compress STI map tiles into a format the library can handle..
I wont explain how the library works, but it will link this > https://github.com/lattejed/a-star-lua/ ... a-star.lua

Downloads :
ArchUtils.lua
(7.91 KiB) Downloaded 55 times
a-star.lua
(5.05 KiB) Downloaded 55 times
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 60 guests