commit fd5299a847558dcab473042e14dffe45ef5653d4 Author: A. Demant Date: Fri Nov 23 06:11:45 2018 +0100 initial commit with basic functions diff --git a/README.md b/README.md new file mode 100644 index 0000000..7aa9dd7 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +Basic functions +short api: +function has_value(tab,val) + check if val is inside list tab + +function import_csv(infile,def) + read configuration from infile + def.as_numeric: all values not stated in col_num, col_tab or with name "name" are interpreted as numeric + def.seperator: character to use as field delimiter + def.col_num: turn this elements to numbers + def.groups_num: put this elements as numbers into matrix groups + +function parse_tree(mat,ind,val) + split name of "ind" and store as nested matrix inside mat. + Example: armor_fleshy_1 will be stored in armor={fleshy={[1]=val}} diff --git a/functions.lua b/functions.lua new file mode 100644 index 0000000..714a25a --- /dev/null +++ b/functions.lua @@ -0,0 +1,99 @@ + +basic_functions.has_value = function(tab, val) +-- test if val is in tab + for index, value in ipairs(tab) do + if value == val then + return true + end + end + return false +end + +local has_value=basic_functions.has_value + +-- read table "infile" where in "def" is defined, which cols are numbers and which belongs to a group +-- def.as_numeric: all values not stated in col_num, col_tab or with name "name" are interpreted as numeric +-- def.seperator: character to use as field delimiter +-- def.col_num: turn this elements to numbers +-- def.groups_num: put this elements as numbers into matrix groups +basic_functions.import_csv = function(infile,def) + local file = io.open(infile, "r") + local outdata = {} + -- reading header with column names + local splitchar="," + if def.seperator then + splitchar=def.seperator + end + local as_numeric=false + if def.as_numeric then + as_numeric = true + end + local header = file:read():gsub("\r",""):split(splitchar,true) + -- read each line, split in separat fields and stores in array + -- by header the value is stored as numeric, in the group environment or as text + for line in file:lines() do + local attribs = line:gsub("\r",""):split(splitchar,true) + local nrow={groups={}} + for i,d in ipairs(attribs) do + + if d ~= "" then + local th=header[i] + local dsaved = false + if def.col_num then + if has_value(def.col_num,th) then + nrow[th] = tonumber(d) + dsaved = true + end + end + if def.groups_num then + if has_value(def.groups_num,th) then + nrow.groups[th]=tonumber(d) + dsaved = true + end + end + if th == "name" then + nrow[th] = d + else + if not dsaved then + if as_numeric then + nrow[th] = tonumber(d) + else + nrow[th]=d + end + end + end + end + end + if nrow.name then + outdata[nrow.name] = nrow + else + outdata[#outdata+1] = nrow + end + end + file:close() + + return outdata +end + +-- split name of value (ind) by "_" and store content in nested matrix +-- inside "mat" +basic_functions.parse_tree=function(mat,ind,val) + if string.find(ind,"_") == nil then + mat[ind] = {} + mat[ind] = tonumber(val) + else + local ind_split=string.split(ind,"_") + local first=ind_split[1] + local second=string.split(ind,"_")[2] + if #ind_split > 2 then + for n=3,#ind_split do + second = second.."_"..ind_split[n] + end + end + if mat[first] == nil then + mat[first]={} + end + mat[first]=minerdream.parse_tree(mat[first],second,val) + end + return(mat) +end diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..95a903f --- /dev/null +++ b/init.lua @@ -0,0 +1,5 @@ +basic_functions = {} +basic_functions.path = minetest.get_modpath("minerdream") +basic_functions.modname = minetest.get_current_modname() + +dofile(basic_functions.path .. "/functions.lua") diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..fb00fe8 --- /dev/null +++ b/mod.conf @@ -0,0 +1,7 @@ +name = basic_functions +title = Basic functions +author = ademant +description = Basic functions I need in a few mods +depends = default +license = MIT +version = 1.0.0