*Reorganized code so that further expansion is possible in a very organized manner.

*modpol.add_member() -- added a check if the member is already a member of that organized

*storage -- fixed bug by moving storageref to top of file

*added on_joinplayer to add players to instance

*moved minetest specific code in init.lua to modpol_minetest, organized in folders.
All overrides in one folder, all chatcommands in another, the on_joinplayer in modpol_minetest/orgs/instance.lua
This commit is contained in:
MisterE123 2021-02-10 23:40:28 -05:00
parent c05023ad20
commit f19796f82d
20 changed files with 395 additions and 286 deletions

177
init.lua
View File

@ -1,3 +1,31 @@
modpol = {}
-- ===================================================================
--preoverrides: certain things must be predefined in the global table for them to have effect (or cmd line version defualts are used)
-- ===================================================================
-- ===================================================================
-- currently:
-- --get_script_dir: get modpol_minetest's version of directory
-- -- Persistent storage
-- -- -- must implement modpol.load_storage() and modpol.store_data()
-- -- -- defines the path for the lua file
-- get modpol_minetest's version of directory
modpol.get_script_dir = function()
return minetest.get_modpath("modpol")
end
-- TKTK: Implement minetest settingtypes for this... for now, the default is to use minetest mod storage.
-- However, the default for if modpol.lua is called from the cmd line is to use local storage
-- Any other implementation may with to make their own persistent storage file and declare it befoe calling modpol.lua in a similar manner
-- works with cmd line: "/storage/storage-local.lua", works with Minetest 5.0 and up: "/storage/storage-mod_storage.lua"
modpol.storage_file_path = minetest.get_modpath("modpol").."/storage/storage-mod_storage.lua"
-- ===================================================================
-- /init.lua
-- Modular Politics (modpol) for Minetest
@ -9,156 +37,11 @@
dofile(minetest.get_modpath("modpol") .. "/modpol.lua")
-- ===================================================================
-- Modular Politics functions
-- Overwriting default API functions with platform-specific ones
-- ===================================================================
-- ===================================================================
-- Function: modpol.list_users(org)
-- Overwrites function at /users.lua
-- Params:
-- if nil, lists instance members; if an org name, lists its members
-- Output: a table with names of players currently in the game
modpol.list_users = function(org)
local users = {}
if (org == nil) then -- no specified org; all players
for _,player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name()
table.insert(users,name)
end
else -- if an org is specified
if (modpol.orgs[org] ~= nil) then -- org exists
users = modpol.orgs[org]["members"]
end
end
return users
end
-- Modular Politics Minetest Specific Code
-- ===================================================================
-- Function: modpol.binary_poll_user(user, question)
-- Overwrites function at /interactions.lua
-- presents a yes/no/abstain poll to a user, returns answer
modpol.binary_poll_user = function(user, question)
-- set up formspec
local text = "Poll: " .. question
local formspec = {
"formspec_version[4]",
"size[5,3]",
"label[0.375,0.5;", minetest.formspec_escape(text), "]",
"button[1,1.5;1,0.8;yes;Yes]",
"button[2,1.5;1,0.8;no;No]",
"button[3,1.5;1,0.8;abstain;Abstain]"
--TKTK can we enable text wrapping?
}
local formspec_string = table.concat(formspec, "")
-- present to player
minetest.show_formspec(user, "modpol:binary_poll", formspec_string)
end
-- ===================================================================
-- Minetest commands
-- ===================================================================
local chat_table -- MT chat command definitions table
local regchat -- Chat-command registration function
regchat = minetest.register_chatcommand
-- ===================================================================
-- /addorg /add_org
-- This code defines a chat command which creates a new
-- "org". Presently, the command makes the user the sole member of the
-- "org".
chat_table = {
privs = {} ,
func = function (user, param)
local result = modpol.add_org (param, { user })
return true, result
end
}
regchat ("addorg" , chat_table)
regchat ("add_org" , chat_table)
-- ===================================================================
-- /listorg /listorgs /list_org /list_orgs
-- In Minetest mode, this code defines a chat command which lists the
-- existing "orgs".
-- The list shows one "org" per line in the following format:
-- org_name (member, member, ...)
chat_table = {
privs = {} ,
func = function (user, param)
return true, "Orgs:\n" .. modpol.list_orgs()
end
}
regchat ("listorg" , chat_table)
regchat ("listorgs" , chat_table)
regchat ("list_org" , chat_table)
regchat ("list_orgs" , chat_table)
-- ===================================================================
-- /listplayers
minetest.register_chatcommand(
"listplayers", {
privs = {},
func = function(user)
local result = table.concat(modpol.list_users(),", ")
return true, "All players: " .. result
end,
})
-- ===================================================================
-- /joinorg
minetest.register_chatcommand(
"joinorg", {
privs = {},
func = function(user, param)
local result = modpol.add_member(param, user)
return true, result
end,
})
-- ===================================================================
-- /pollself [question]
-- asks the user a question specified in param
minetest.register_chatcommand(
"pollself", {
privs = {},
func = function(user, param)
modpol.binary_poll_user(user, param)
return true, result
end,
})
-- ===================================================================
-- Minetest events
-- ===================================================================
-- ===================================================================
-- Receiving fields
minetest.register_on_player_receive_fields(function (player, formname, fields)
-- modpol:poll
if formname == "modpol:binary_poll" then
local pname = player:get_player_name()
local vote = nil
if fields.yes then vote = fields.yes
elseif fields.no then vote = fields.no
elseif fields.abstain then vote = fields.abstain
end
if vote then
minetest.chat_send_all(pname .. " voted " .. vote)
end
minetest.close_formspec(pname, formname)
return vote
else -- if the form is not a recognized name
return
end
end)
dofile(minetest.get_modpath("modpol") .. "/modpol_minetest/api.lua")
-- ===================================================================
-- End of file.

View File

@ -6,8 +6,7 @@
-- Basic tables
-- Main API table
modpol = {
}
if not modpol then modpol = {} end
-- Table for modpol data
modpol.orgs = {
@ -17,14 +16,21 @@ modpol.orgs = {
modpol.ledger = {
}
-- ===================================================================
-- Locate framework top-level directory.
-- This function is intended for use under Linux and/or UNIX only. It
-- This function is intended for use under Linux and/or UNIX only.
-- It
-- returns a relative or absolute path for the framework top-level
-- directory without a trailing slash.
local get_script_dir = function()
-- if your application has a different method of getting the script directory,
-- then feel free to overwrite this in an init.lua or other such file by first
-- defining the modpol table and then defining the get_script_dir() function
local get_script_dir = modpol.get_script_dir or function()
local str = debug.getinfo (2, "S").source:sub (2)
str = str:match ("(.*/)") or "."
str = str:gsub ("/$", "", 1)
@ -40,17 +46,23 @@ print (topdir)
-- Load dependencies
-- OldCoder utilities
dofile (topdir .. "/ocutil.lua")
dofile (topdir .. "/util/ocutil/ocutil.lua")
-- ===================================================================
-- Persistent storage
-- must implement modpol.load_storage() and modpol.store_data()
-- Select a storage method
-- -- preferably, declare this in the init.lua that calls modpol.lua This is the default.
-- Works with CLI:
dofile (topdir .. "/storage-local.lua")
modpol.storage_file_path = modpol.storage_file_path or topdir .. "/storage/storage-local.lua"
-- Works with Minetest 5:
-- dofile (topdir .. "/storage-mod_storage.lua")
--modpol.storage_file_path = modpol.storage_file_path or topdir .. "/storage/storage-mod_storage.lua")
--execute the storage file
dofile (modpol.storage_file_path)
-- If available, load persistent storage into active tables
modpol.load_storage()
@ -58,11 +70,9 @@ modpol.load_storage()
-- ===================================================================
-- ModPol core features
dofile (topdir .. "/users.lua")
dofile (topdir .. "/orgs.lua")
dofile (topdir .. "/interactions.lua")
-- messaging functions
dofile (topdir .. "/processes.lua")
dofile (topdir .. "/modpol/api.lua")
-- ===================================================================
-- Final checks
@ -72,7 +82,7 @@ if (modpol.orgs["instance"] == nil) then
modpol.add_org("instance", modpol.list_users())
end
ocutil.log ("modpol loaded")
modpol.ocutil.log ("modpol loaded")
-- ===================================================================
-- End of file.

15
modpol/api.lua Normal file
View File

@ -0,0 +1,15 @@
--call all files in this directory
local localdir = modpol.get_script_dir() .. "/modpol"
--Users
dofile (localdir .. "/users/users.lua")
--orgs
dofile (localdir .. "/orgs/orgs.lua")
--interactions
dofile (localdir .. "/interactions/interactions.lua")
-- messaging functions
dofile (localdir .. "/processes/processes.lua")

View File

@ -1,7 +1,7 @@
-- ===================================================================
-- /orgs.lua
-- Org-related functions for Modular Politics
-- Called by modpol.lua
-- Called by modpol/modpol/api.lua
-- ===================================================================
-- Function: modpol.record
@ -47,14 +47,14 @@ end
modpol.add_org = function (org_name, members)
local str
if ocutil.str_empty (org_name) then
if modpol.ocutil.str_empty (org_name) then
return "Error: Org needs a name"
end
ocutil.log ("Adding org " .. org_name)
modpol.ocutil.log ("Adding org " .. org_name)
if modpol.orgs [org_name] ~= nil then
str = "Error: Org " .. org_name .. " already exists"
ocutil.log (str)
modpol.ocutil.log (str)
return str
end
@ -82,7 +82,7 @@ modpol.list_orgs = function()
-- Process next "org"
-- Build string version of member list
local memcat = org_data ["members"]
if ocutil.str_empty (memcat) then
if modpol.ocutil.str_empty (memcat) then
memcat = "(empty)"
else
memcat = "(" .. table.concat (memcat, ", ") .. ")"
@ -120,10 +120,14 @@ modpol.add_member = function(org, member)
if (modpol.orgs[org] == nil) then
return "Error: No such org"
else
table.insert(modpol.orgs[org]["members"], member)
local message = member .. " added to org " .. org
modpol.record(message, org)
return message
if modpol.is_member(org,member) then
return "Error: " .. member .. " is already a member of " .. org
else
table.insert(modpol.orgs[org]["members"], member)
local message = member .. " added to org " .. org
modpol.record(message, org)
return message
end
end
end

View File

@ -1,5 +1,5 @@
-- ===================================================================
-- /prcesses.lua
-- /processes.lua
-- Process-related functions for Modular Politics
-- Called by modpol.lua

40
modpol_minetest/api.lua Normal file
View File

@ -0,0 +1,40 @@
--call all files in this directory
-- ===================================================================
-- Minetest Specific Overrides of modpol functions
-- ===================================================================
local localdir = modpol.get_script_dir() .. "/modpol_minetest"
--Users
dofile (localdir .. "/overrides/users/users.lua")
--orgs
--dofile (localdir .. "/overrides/orgs/orgs.lua")
--interactions
dofile (localdir .. "/overrides/interactions/interactions.lua")
-- messaging functions
-- dofile (localdir .. "/overrides/processes/processes.lua")
-- ===================================================================
-- Minetest Chatcommands
-- ===================================================================
dofile (localdir .. "/chatcommands/chatcommands.lua")
-- ===================================================================
-- Minetest Specific code
-- ===================================================================
-- orgs
-- ===================
dofile (localdir .. "/orgs/instance.lua") --add players to the instance when they join.

View File

@ -0,0 +1,78 @@
-- ===================================================================
-- Minetest commands
-- ===================================================================
local chat_table -- MT chat command definitions table
local regchat -- Chat-command registration function
regchat = minetest.register_chatcommand
-- ===================================================================
-- /addorg /add_org
-- This code defines a chat command which creates a new
-- "org". Presently, the command makes the user the sole member of the
-- "org".
chat_table = {
privs = {} ,
func = function (user, param)
local result = modpol.add_org (param, { user })
return true, result
end
}
regchat ("addorg" , chat_table)
regchat ("add_org" , chat_table)
-- ===================================================================
-- /listorg /listorgs /list_org /list_orgs
-- In Minetest mode, this code defines a chat command which lists the
-- existing "orgs".
-- The list shows one "org" per line in the following format:
-- org_name (member, member, ...)
chat_table = {
privs = {} ,
func = function (user, param)
return true, "Orgs:\n" .. modpol.list_orgs()
end
}
regchat ("listorg" , chat_table)
regchat ("listorgs" , chat_table)
regchat ("list_org" , chat_table)
regchat ("list_orgs" , chat_table)
-- ===================================================================
-- /listplayers
minetest.register_chatcommand(
"listplayers", {
privs = {},
func = function(user)
local result = table.concat(modpol.list_users(),", ")
return true, "All players: " .. result
end,
})
-- ===================================================================
-- /joinorg
minetest.register_chatcommand(
"joinorg", {
privs = {},
func = function(user, param)
local result = modpol.add_member(param, user)
return true, result
end,
})
-- ===================================================================
-- /pollself [question]
-- asks the user a question specified in param
minetest.register_chatcommand(
"pollself", {
privs = {},
func = function(user, param)
modpol.binary_poll_user(user, param)
return true, result
end,
})

View File

@ -0,0 +1,6 @@
--add members to the instance, if they are not already there.
minetest.register_on_joinplayer(function(player)
local p_name = player:get_player_name()
modpol.add_member("instance", p_name)
end)

View File

@ -0,0 +1,44 @@
-- ===================================================================
-- Function: modpol.binary_poll_user(user, question)
-- Overwrites function at /interactions.lua
-- presents a yes/no/abstain poll to a user, returns answer
modpol.binary_poll_user = function(user, question)
-- set up formspec
local text = "Poll: " .. question
local formspec = {
"formspec_version[4]",
"size[5,3]",
"label[0.375,0.5;", minetest.formspec_escape(text), "]",
"button[1,1.5;1,0.8;yes;Yes]",
"button[2,1.5;1,0.8;no;No]",
"button[3,1.5;1,0.8;abstain;Abstain]"
--TKTK can we enable text wrapping?
--TKTK we could use scroll boxes to contain the text
}
local formspec_string = table.concat(formspec, "")
-- present to player
minetest.show_formspec(user, "modpol:binary_poll", formspec_string)
end
--what to do
minetest.register_on_player_receive_fields(function (player, formname, fields)
-- modpol:poll
if formname == "modpol:binary_poll" then
local pname = player:get_player_name()
local vote = nil
if fields.yes then vote = fields.yes
elseif fields.no then vote = fields.no
elseif fields.abstain then vote = fields.abstain
end
if vote then
minetest.chat_send_all(pname .. " voted " .. vote)
--TKTK : we should send the message to all in that org, definately not to all players
end
minetest.close_formspec(pname, formname)
return vote
else -- if the form is not a recognized name
return
end
end)

View File

@ -0,0 +1,21 @@
-- ===================================================================
-- Function: modpol.list_users(org)
-- Overwrites function at /users.lua
-- Params:
-- if nil, lists instance members; if an org name, lists its members
-- Output: a table with names of players currently in the game
modpol.list_users = function(org)
local users = {}
if (org == nil) then -- no specified org; all players
for _,player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name()
table.insert(users,name)
end
else -- if an org is specified
if (modpol.orgs[org] ~= nil) then -- org exists
users = modpol.orgs[org]["members"]
end
end
return users
end

View File

@ -12,47 +12,47 @@ modpol.file_orgs = modpol.datadir .. "/orgs.dat"
os.execute ("mkdir -p " .. modpol.datadir)
ocutil.setlogdir (modpol.datadir)
ocutil.setlogname ("modpol.log")
modpol.ocutil.setlogdir (modpol.datadir)
modpol.ocutil.setlogname ("modpol.log")
-- ===================================================================
-- Set up the Serpent Serializer functions.
modpol.serpent = {}
dofile (modpol.topdir .. "/serpent/serpent.lua")
dofile (modpol.topdir .. "/util/serpent/serpent.lua")
-- ===================================================================
-- This function stores "ledger" data to disk.
local store_ledger = function()
local ok = ocutil.file_write (modpol.file_ledger,
local ok = modpol.ocutil.file_write (modpol.file_ledger,
modpol.serpent.dump (modpol.ledger))
if ok ~= true then
ocutil.fatal_error ("store_data: ledger")
modpol.ocutil.fatal_error ("store_data: ledger")
end
local nn = ocutil.table_length (modpol.ledger)
local nn = modpol.ocutil.table_length (modpol.ledger)
local str = "entries"
if nn == 1 then str = "entry" end
ocutil.log (nn .. " global ledger entries stored to disk")
modpol.ocutil.log (nn .. " global ledger entries stored to disk")
end
-- ===================================================================
-- This function stores "orgs" data to disk.
local store_orgs = function()
local ok = ocutil.file_write (modpol.file_orgs,
local ok = modpol.ocutil.file_write (modpol.file_orgs,
modpol.serpent.dump (modpol.orgs))
if ok ~= true then
ocutil.fatal_error ("store_data: orgs")
modpol.ocutil.fatal_error ("store_data: orgs")
end
local nn = ocutil.table_length (modpol.orgs)
local nn = modpol.ocutil.table_length (modpol.orgs)
local str = "entries"
if nn == 1 then str = "entry" end
ocutil.log (nn .. " orgs stored to disk")
modpol.ocutil.log (nn .. " orgs stored to disk")
end
-- ===================================================================
@ -67,20 +67,20 @@ end
-- This function loads "ledger" data from disk.
local load_ledger = function()
local obj = ocutil.file_read (modpol.file_ledger )
local obj = modpol.ocutil.file_read (modpol.file_ledger )
if obj ~= nil then
local func, err = load (obj)
if err then
ocutil.fatal_error ("load_data: ledger" )
modpol.ocutil.fatal_error ("load_data: ledger" )
end
modpol.ledger = func()
local nn = ocutil.table_length (modpol.ledger)
local nn = modpol.ocutil.table_length (modpol.ledger)
local str = "entries"
if nn == 1 then str = "entry" end
ocutil.log (nn .. " global ledger entries loaded from disk")
modpol.ocutil.log (nn .. " global ledger entries loaded from disk")
else
ocutil.log ("No stored global ledger data found")
modpol.ocutil.log ("No stored global ledger data found")
end
end
@ -88,20 +88,20 @@ end
-- This function loads "orgs" data from disk.
local load_orgs = function()
local obj = ocutil.file_read (modpol.file_orgs )
local obj = modpol.ocutil.file_read (modpol.file_orgs )
if obj ~= nil then
local func, err = load (obj)
if err then
ocutil.fatal_error ("load_data: orgs" )
modpol.ocutil.fatal_error ("load_data: orgs" )
end
modpol.orgs = func()
local nn = ocutil.table_length (modpol.orgs)
local nn = modpol.ocutil.table_length (modpol.orgs)
local str = "entries"
if nn == 1 then str = "entry" end
ocutil.log (nn .. " orgs loaded from disk")
modpol.ocutil.log (nn .. " orgs loaded from disk")
else
ocutil.log ("No stored orgs data found")
modpol.ocutil.log ("No stored orgs data found")
end
end

View File

@ -4,8 +4,15 @@
-- See https://dev.minetest.net/StorageRef
-- Loads content of stored orgs and ledger from mod_storage
--get modstorageref
local mod_storage = minetest.get_mod_storage()
modpol.load_storage = function()
local mod_storage = minetest.get_mod_storage()
-- load orgs
local stored_orgs = minetest.deserialize(mod_storage:get_string("orgs"))
if (stored_orgs ~= nil) then
@ -20,6 +27,7 @@ end
-- Stores content of current orgs and ledger to mod_storage
modpol.store_data = function()
-- write to storage
mod_storage:set_string("orgs", minetest.serialize(modpol.orgs))
mod_storage:set_string("ledger", minetest.serialize(modpol.ledger))

View File

@ -1,7 +1,7 @@
-- ===================================================================
-- Overview.
-- This file is "top/ocutil.lua".
-- This file is "top/modpol.ocutil.lua".
-- This file provides a collection of largely portable Lua utility
-- functions. The collection is descended from one assembled by Old-
@ -9,15 +9,15 @@
-- ===================================================================
ocutil = {}
modpol.ocutil = {}
ocutil.log_console = true -- Flag: Copy log to console
ocutil.logdir = nil -- Absolute path for log-file direc-
modpol.ocutil.log_console = true -- Flag: Copy log to console
modpol.ocutil.logdir = nil -- Absolute path for log-file direc-
-- tory (or nil)
-- ===================================================================
-- Function: ocutil.fixnil
-- Function: modpol.ocutil.fixnil
-- Params: string s
-- Outputs:
--
@ -30,7 +30,7 @@ ocutil.logdir = nil -- Absolute path for log-file direc-
-- ===================================================================
ocutil.fixnil = function (s)
modpol.ocutil.fixnil = function (s)
if s == nil then s = "(nil)"
elseif s == "" then s = "(empty)"
end
@ -39,46 +39,46 @@ end
-- ===================================================================
-- Function: ocutil.setlogdir
-- Function: modpol.ocutil.setlogdir
-- Params: string path
-- Outputs:
--
-- The input string should be the absolute path for a writable direc-
-- tory that already exists. This function tells "ocutil.log" to put
-- tory that already exists. This function tells "modpol.ocutil.log" to put
-- log files in that directory.
-- ===================================================================
ocutil.setlogdir = function (path)
modpol.ocutil.setlogdir = function (path)
if path ~= nil and path ~= "" then
ocutil.logdir = path
modpol.ocutil.logdir = path
end
end
-- ===================================================================
-- Function: ocutil.setlogname
-- Function: modpol.ocutil.setlogname
-- Params: string name
-- Outputs:
--
-- The input string should be a filename without a path component.
-- This function tells "ocutil.log" to use the specified filename for
-- This function tells "modpol.ocutil.log" to use the specified filename for
-- the main log file.
--
-- "ocutil.setlogdir" must be called separately to set the log-file
-- "modpol.ocutil.setlogdir" must be called separately to set the log-file
-- directory.
-- ===================================================================
ocutil.setlogname = function (name)
modpol.ocutil.setlogname = function (name)
if name ~= nil and name ~= "" then
ocutil.logname = name
modpol.ocutil.logname = name
end
end
-- ===================================================================
-- Function: ocutil.log
-- Function: modpol.ocutil.log
-- Params: string s
-- Outputs:
--
@ -87,26 +87,26 @@ end
-- ===================================================================
ocutil.log = function (s)
s = ocutil.fixnil (s)
modpol.ocutil.log = function (s)
s = modpol.ocutil.fixnil (s)
s = s:gsub ("\n$", "", 1) -- Remove trailing newline initially
if ocutil.log_console then
if modpol.ocutil.log_console then
print (s)
end
if ocutil.logdir ~= nil and
ocutil.logname ~= nil then
if modpol.ocutil.logdir ~= nil and
modpol.ocutil.logname ~= nil then
s = s .. "\n" -- Add trailing newline
local logpath = ocutil.logdir .. "/" .. ocutil.logname
ocutil.file_append (logpath, s)
local logpath = modpol.ocutil.logdir .. "/" .. modpol.ocutil.logname
modpol.ocutil.file_append (logpath, s)
end
end
-- ===================================================================
-- Function: ocutil.fatal_error
-- Function: modpol.ocutil.fatal_error
-- Params: string s
-- Outputs:
--
@ -115,15 +115,15 @@ end
-- ===================================================================
ocutil.fatal_error = function (s)
ocutil.log ("Fatal Error: " .. s)
modpol.ocutil.fatal_error = function (s)
modpol.ocutil.log ("Fatal Error: " .. s)
os.exit (1)
return nil -- Shouldn't be reached
end
-- ===================================================================
-- Function: ocutil.panic
-- Function: modpol.ocutil.panic
-- Params: string s
-- Outputs:
--
@ -132,15 +132,15 @@ end
-- ===================================================================
ocutil.panic = function (s)
ocutil.log ("Internal Error: " .. s)
modpol.ocutil.panic = function (s)
modpol.ocutil.log ("Internal Error: " .. s)
os.exit (1)
return nil -- Shouldn't be reached
end
-- ===================================================================
-- Function: ocutil.str_empty
-- Function: modpol.ocutil.str_empty
-- Params: string x
-- Outputs:
--
@ -149,7 +149,7 @@ end
-- ===================================================================
ocutil.str_empty = function (x)
modpol.ocutil.str_empty = function (x)
if x == nil or x == "" then
return true
else
@ -159,7 +159,7 @@ end
-- ===================================================================
-- Function: ocutil.str_nonempty
-- Function: modpol.ocutil.str_nonempty
-- Params: string x
-- Outputs:
--
@ -168,7 +168,7 @@ end
-- ===================================================================
ocutil.str_nonempty = function (x)
modpol.ocutil.str_nonempty = function (x)
if x == nil or x == "" then
return false
else
@ -178,7 +178,7 @@ end
-- ===================================================================
-- Function: ocutil.table_empty
-- Function: modpol.ocutil.table_empty
-- Params: table tab
-- Outputs:
--
@ -187,7 +187,7 @@ end
-- ===================================================================
ocutil.table_empty = function (tab)
modpol.ocutil.table_empty = function (tab)
local next = next
if next (tab) == nil then return true end
return false
@ -195,7 +195,7 @@ end
-- ===================================================================
-- Function: ocutil.table_nonempty
-- Function: modpol.ocutil.table_nonempty
-- Params: table tab
-- Outputs:
--
@ -204,14 +204,14 @@ end
-- ===================================================================
ocutil.table_nonempty = function (tab)
if ocutil.table_empty (tab) then return false end
modpol.ocutil.table_nonempty = function (tab)
if modpol.ocutil.table_empty (tab) then return false end
return true
end
-- ===================================================================
-- Function: ocutil.string_contains
-- Function: modpol.ocutil.string_contains
-- Params: strings a, b
-- Outputs:
--
@ -220,7 +220,7 @@ end
-- ===================================================================
ocutil.str_contains = function (a, b)
modpol.ocutil.str_contains = function (a, b)
if string.match (a, b) then
return true
else
@ -230,7 +230,7 @@ end
-- ===================================================================
-- Function: ocutil.str_false
-- Function: modpol.ocutil.str_false
-- Params: string x
-- Outputs:
--
@ -240,7 +240,7 @@ end
-- ===================================================================
ocutil.str_false = function (x)
modpol.ocutil.str_false = function (x)
if x == "false" or x == "no" or
x == "off" or x == "0" or
x == false or x == 0 then
@ -252,7 +252,7 @@ end
-- ===================================================================
-- Function: ocutil.str_true
-- Function: modpol.ocutil.str_true
-- Params: string x
-- Outputs:
--
@ -262,7 +262,7 @@ end
-- ===================================================================
ocutil.str_true = function (x)
modpol.ocutil.str_true = function (x)
if x == "true" or x == "yes" or
x == "on" or x == "1" or
x == true or x == 1 then
@ -274,7 +274,7 @@ end
-- ===================================================================
-- Function: ocutil.starts_with
-- Function: modpol.ocutil.starts_with
-- Params: strings String, Start
-- Outputs:
--
@ -283,7 +283,7 @@ end
-- ===================================================================
ocutil.starts_with = function (String, Start)
modpol.ocutil.starts_with = function (String, Start)
if string.sub (String, 1, string.len (Start)) == Start then
return true
else
@ -293,7 +293,7 @@ end
-- ===================================================================
-- Function: ocutil.not_starts_with
-- Function: modpol.ocutil.not_starts_with
-- Params: strings String, Start
-- Outputs:
--
@ -302,7 +302,7 @@ end
-- ===================================================================
ocutil.not_starts_with = function (String, Start)
modpol.ocutil.not_starts_with = function (String, Start)
if string.sub (String, 1, string.len (Start)) == Start then
return false
else
@ -312,7 +312,7 @@ end
-- ===================================================================
-- Function: ocutil.ends_with
-- Function: modpol.ocutil.ends_with
-- Params: strings String, End
-- Outputs:
--
@ -322,14 +322,14 @@ end
-- ===================================================================
ocutil.ends_with = function (String, End)
modpol.ocutil.ends_with = function (String, End)
return End == '' or string.sub (String,
-string.len (End)) == End
end
-- ===================================================================
-- Function: ocutil.not_ends_with
-- Function: modpol.ocutil.not_ends_with
-- Params: strings String, End
-- Outputs:
-- Returns false if the 1st string ends with the 2nd one
@ -338,8 +338,8 @@ end
-- ===================================================================
ocutil.not_ends_with = function (String, End)
if ocutil.ends_with (String, End) then
modpol.ocutil.not_ends_with = function (String, End)
if modpol.ocutil.ends_with (String, End) then
return false
else
return true
@ -348,33 +348,33 @@ end
-- ===================================================================
-- Function: ocutil.firstch
-- Function: modpol.ocutil.firstch
-- Params: string str
-- Outputs:
-- Returns the 1st character of the string
-- ===================================================================
ocutil.firstch = function (str)
modpol.ocutil.firstch = function (str)
return string.sub (str, 1, 1)
end
-- ===================================================================
-- Function: ocutil.first_to_upper
-- Function: modpol.ocutil.first_to_upper
-- Params: string str
-- Outputs:
-- Returns the 1st character of the string in upper case
-- ===================================================================
ocutil.first_to_upper = function (str)
modpol.ocutil.first_to_upper = function (str)
return (str:gsub ("^%l", string.upper))
end
-- ===================================================================
-- Function: ocutil.all_first_to_upper
-- Function: modpol.ocutil.all_first_to_upper
-- Params: string str, flag cvtspace
-- Outputs:
--
@ -385,13 +385,13 @@ end
-- ===================================================================
ocutil.all_first_to_upper = function (str, cvtspace)
modpol.ocutil.all_first_to_upper = function (str, cvtspace)
str = str:gsub ("^%l", string.upper)
str = str:gsub ("[_ ]%l",
function (a) return string.upper (a) end)
if ocutil.str_true (cvtspace) then
if modpol.ocutil.str_true (cvtspace) then
str = str:gsub ("_", " ")
end
@ -400,7 +400,7 @@ end
-- ===================================================================
-- Function: ocutil.strtok
-- Function: modpol.ocutil.strtok
-- Params: strings source, delimitch
-- Outputs:
--
@ -413,7 +413,7 @@ end
-- ===================================================================
ocutil.strtok = function (source, delimitch)
modpol.ocutil.strtok = function (source, delimitch)
if delimitch == nil or
delimitch == "" then delimitch = " " end
@ -432,7 +432,7 @@ end
-- ===================================================================
-- Function: ocutil.swap_key_value
-- Function: modpol.ocutil.swap_key_value
-- Params: table itable
-- Outputs:
-- Turns keys into values and vice versa and returns the resulting
@ -440,7 +440,7 @@ end
-- ===================================================================
ocutil.swap_key_value = function (itable)
modpol.ocutil.swap_key_value = function (itable)
if itable == nil then return nil end
local otable = {}
for key, value in pairs (itable) do otable [value] = key end
@ -449,7 +449,7 @@ end
-- ===================================================================
-- Function: ocutil.ktable_to_vtable
-- Function: modpol.ocutil.ktable_to_vtable
-- Params: table ktable
-- Outputs:
--
@ -476,7 +476,7 @@ end
-- print (ii .. ") " .. tabby [ii])
-- end
--
-- tabby = ocutil.ktable_to_vtable (tabby)
-- tabby = modpol.ocutil.ktable_to_vtable (tabby)
--
-- print ("\nOutput table:")
-- for ii, value in ipairs (tabby) do
@ -485,7 +485,7 @@ end
-- ===================================================================
ocutil.ktable_to_vtable = function (ktable)
modpol.ocutil.ktable_to_vtable = function (ktable)
local vtable = {}
if ktable == nil then return vtable end
@ -499,7 +499,7 @@ end
-- ===================================================================
-- Function: ocutil.vtable_to_ktable
-- Function: modpol.ocutil.vtable_to_ktable
-- Params: table vtable, scalar set_to
-- Outputs:
--
@ -524,7 +524,7 @@ end
-- print (ii .. ") " .. tabby [ii])
-- end
--
-- tabby = ocutil.vtable_to_ktable (tabby, 42)
-- tabby = modpol.ocutil.vtable_to_ktable (tabby, 42)
--
-- print ("\nOutput table:")
-- for ii, value in pairs (tabby) do
@ -533,7 +533,7 @@ end
-- ===================================================================
ocutil.vtable_to_ktable = function (vtable, set_to)
modpol.ocutil.vtable_to_ktable = function (vtable, set_to)
local ktable = {}
if vtable == nil then return ktable end
@ -548,7 +548,7 @@ end
-- ===================================================================
-- Function: ocutil.make_set
-- Function: modpol.ocutil.make_set
-- Params: array harry
-- Outputs:
--
@ -559,12 +559,12 @@ end
-- Here's a complete code fragment which demonstrates operation of
-- this function:
--
-- local color_set = ocutil.make_set { "red", "green", "blue" }
-- local color_set = modpol.ocutil.make_set { "red", "green", "blue" }
-- if color_set ["red"] ~= nil then print ("Supported color") end
-- ===================================================================
ocutil.make_set = function (list)
modpol.ocutil.make_set = function (list)
local set = {}
for _, l in ipairs (list) do set [l] = true end
return set
@ -572,7 +572,7 @@ end
-- ===================================================================
-- Function: ocutil.pos_to_str
-- Function: modpol.ocutil.pos_to_str
-- Params: position-table pos
-- Outputs:
--
@ -583,13 +583,13 @@ end
-- ===================================================================
ocutil.pos_to_str = function (pos)
modpol.ocutil.pos_to_str = function (pos)
return pos.x .. "," .. pos.y .. "," .. pos.z
end
-- ===================================================================
-- Function: ocutil.table_length
-- Function: modpol.ocutil.table_length
-- Params: table tabby
-- Outputs: Returns number of entries in table
--
@ -598,7 +598,7 @@ end
-- ===================================================================
ocutil.table_length = function (tabby)
modpol.ocutil.table_length = function (tabby)
local count = 0
for _ in pairs (tabby) do count = count+1 end
return count
@ -606,7 +606,7 @@ end
-- ===================================================================
-- Function: ocutil.clone_table
-- Function: modpol.ocutil.clone_table
-- Params: table tabby
-- Outputs:
--
@ -614,25 +614,25 @@ end
-- should work correctly for most cases, but special cases that re-
-- quire additional work may exist.
--
-- This function may also be called as "ocutil.table_clone".
-- This function may also be called as "modpol.ocutil.table_clone".
-- ===================================================================
ocutil.clone_table = function (tabby)
modpol.ocutil.clone_table = function (tabby)
if tabby == nil then return nil end
local copy = {}
for k, v in pairs (tabby) do
if type (v) == 'table' then v = ocutil.clone_table (v) end
if type (v) == 'table' then v = modpol.ocutil.clone_table (v) end
copy [k] = v
end
return copy
end
ocutil.table_clone = ocutil.clone_table
modpol.ocutil.table_clone = modpol.ocutil.clone_table
-- ===================================================================
-- Function: ocutil.file_exists
-- Function: modpol.ocutil.file_exists
-- Params: string path
-- Outputs:
--
@ -642,7 +642,7 @@ ocutil.table_clone = ocutil.clone_table
-- ===================================================================
ocutil.file_exists = function (path)
modpol.ocutil.file_exists = function (path)
local file, err
file, err = io.open (path, "rb")
if err then return false end
@ -652,7 +652,7 @@ end
-- ===================================================================
-- Function: ocutil.file_missing
-- Function: modpol.ocutil.file_missing
-- Params: string path
-- Outputs:
--
@ -662,14 +662,14 @@ end
-- ===================================================================
ocutil.file_missing = function (path)
if ocutil.file_exists (path) then return false end
modpol.ocutil.file_missing = function (path)
if modpol.ocutil.file_exists (path) then return false end
return true
end
-- ===================================================================
-- Function: ocutil.file_read
-- Function: modpol.ocutil.file_read
-- Params: string path
-- Outputs:
--
@ -681,7 +681,7 @@ end
-- ===================================================================
ocutil.file_read = function (path)
modpol.ocutil.file_read = function (path)
local file, err
file, err = io.open (path, "rb")
if err then return nil end
@ -692,7 +692,7 @@ end
-- ===================================================================
-- Function: ocutil.file_write
-- Function: modpol.ocutil.file_write
-- Params: strings path, data
-- Outputs:
--
@ -707,7 +707,7 @@ end
-- ===================================================================
ocutil.file_write = function (path, data)
modpol.ocutil.file_write = function (path, data)
local file, err
file, err = io.open (path, "wb")
if err then return false end
@ -719,17 +719,17 @@ end
-- ===================================================================
-- Function: ocutil.file_append
-- Function: modpol.ocutil.file_append
-- Params: strings path, data
-- Outputs:
--
-- This function is identical to "ocutil.file_write" except for one
-- This function is identical to "modpol.ocutil.file_write" except for one
-- difference: It appends to existing files as opposed to overwrites
-- them.
-- ===================================================================
ocutil.file_append = function (path, data)
modpol.ocutil.file_append = function (path, data)
local file, err
file, err = io.open (path, "ab")
if err then return false end