*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 committed 2021-02-10 23:40:28 -05:00
1 parent c05023ad20
commit f19796f82d
20 files changed
+391 -282

No files matched your search

File renamed without changes.
+30 -147
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 -- /init.lua
-- Modular Politics (modpol) for Minetest -- Modular Politics (modpol) for Minetest
@@ -9,156 +37,11 @@
dofile(minetest.get_modpath("modpol") .. "/modpol.lua") dofile(minetest.get_modpath("modpol") .. "/modpol.lua")
-- ===================================================================
-- Modular Politics functions
-- Overwriting default API functions with platform-specific ones
-- ===================================================================
-- =================================================================== -- ===================================================================
-- Function: modpol.list_users(org) -- Modular Politics Minetest Specific Code
-- 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
-- =================================================================== -- ===================================================================
-- Function: modpol.binary_poll_user(user, question) dofile(minetest.get_modpath("modpol") .. "/modpol_minetest/api.lua")
-- 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)
-- =================================================================== -- ===================================================================
-- End of file. -- End of file.
+23 -13
View File
@@ -6,8 +6,7 @@
-- Basic tables -- Basic tables
-- Main API table -- Main API table
modpol = { if not modpol then modpol = {} end
}
-- Table for modpol data -- Table for modpol data
modpol.orgs = { modpol.orgs = {
@@ -17,14 +16,21 @@ modpol.orgs = {
modpol.ledger = { modpol.ledger = {
} }
-- =================================================================== -- ===================================================================
-- Locate framework top-level directory. -- 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 -- returns a relative or absolute path for the framework top-level
-- directory without a trailing slash. -- 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) local str = debug.getinfo (2, "S").source:sub (2)
str = str:match ("(.*/)") or "." str = str:match ("(.*/)") or "."
str = str:gsub ("/$", "", 1) str = str:gsub ("/$", "", 1)
@@ -40,17 +46,23 @@ print (topdir)
-- Load dependencies -- Load dependencies
-- OldCoder utilities -- OldCoder utilities
dofile (topdir .. "/ocutil.lua") dofile (topdir .. "/util/ocutil/ocutil.lua")
-- =================================================================== -- ===================================================================
-- Persistent storage -- Persistent storage
-- must implement modpol.load_storage() and modpol.store_data() -- must implement modpol.load_storage() and modpol.store_data()
-- Select a storage method -- Select a storage method
-- -- preferably, declare this in the init.lua that calls modpol.lua This is the default.
-- Works with CLI: -- 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: -- 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 -- If available, load persistent storage into active tables
modpol.load_storage() modpol.load_storage()
@@ -58,11 +70,9 @@ modpol.load_storage()
-- =================================================================== -- ===================================================================
-- ModPol core features -- ModPol core features
dofile (topdir .. "/users.lua")
dofile (topdir .. "/orgs.lua") dofile (topdir .. "/modpol/api.lua")
dofile (topdir .. "/interactions.lua")
-- messaging functions
dofile (topdir .. "/processes.lua")
-- =================================================================== -- ===================================================================
-- Final checks -- Final checks
@@ -72,7 +82,7 @@ if (modpol.orgs["instance"] == nil) then
modpol.add_org("instance", modpol.list_users()) modpol.add_org("instance", modpol.list_users())
end end
ocutil.log ("modpol loaded") modpol.ocutil.log ("modpol loaded")
-- =================================================================== -- ===================================================================
-- End of file. -- End of file.
+15
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")
File renamed without changes.
+9 -5
View File
@@ -1,7 +1,7 @@
-- =================================================================== -- ===================================================================
-- /orgs.lua -- /orgs.lua
-- Org-related functions for Modular Politics -- Org-related functions for Modular Politics
-- Called by modpol.lua -- Called by modpol/modpol/api.lua
-- =================================================================== -- ===================================================================
-- Function: modpol.record -- Function: modpol.record
@@ -47,14 +47,14 @@ end
modpol.add_org = function (org_name, members) modpol.add_org = function (org_name, members)
local str local str
if ocutil.str_empty (org_name) then if modpol.ocutil.str_empty (org_name) then
return "Error: Org needs a name" return "Error: Org needs a name"
end end
ocutil.log ("Adding org " .. org_name) modpol.ocutil.log ("Adding org " .. org_name)
if modpol.orgs [org_name] ~= nil then if modpol.orgs [org_name] ~= nil then
str = "Error: Org " .. org_name .. " already exists" str = "Error: Org " .. org_name .. " already exists"
ocutil.log (str) modpol.ocutil.log (str)
return str return str
end end
@@ -82,7 +82,7 @@ modpol.list_orgs = function()
-- Process next "org" -- Process next "org"
-- Build string version of member list -- Build string version of member list
local memcat = org_data ["members"] local memcat = org_data ["members"]
if ocutil.str_empty (memcat) then if modpol.ocutil.str_empty (memcat) then
memcat = "(empty)" memcat = "(empty)"
else else
memcat = "(" .. table.concat (memcat, ", ") .. ")" memcat = "(" .. table.concat (memcat, ", ") .. ")"
@@ -119,12 +119,16 @@ end
modpol.add_member = function(org, member) modpol.add_member = function(org, member)
if (modpol.orgs[org] == nil) then if (modpol.orgs[org] == nil) then
return "Error: No such org" return "Error: No such org"
else
if modpol.is_member(org,member) then
return "Error: " .. member .. " is already a member of " .. org
else else
table.insert(modpol.orgs[org]["members"], member) table.insert(modpol.orgs[org]["members"], member)
local message = member .. " added to org " .. org local message = member .. " added to org " .. org
modpol.record(message, org) modpol.record(message, org)
return message return message
end end
end
end end
-- =================================================================== -- ===================================================================
@@ -1,5 +1,5 @@
-- =================================================================== -- ===================================================================
-- /prcesses.lua -- /processes.lua
-- Process-related functions for Modular Politics -- Process-related functions for Modular Politics
-- Called by modpol.lua -- Called by modpol.lua
File renamed without changes.
+40
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.
@@ -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,
})
+6
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)
@@ -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)
+21
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
+21 -21
View File
@@ -12,47 +12,47 @@ modpol.file_orgs = modpol.datadir .. "/orgs.dat"
os.execute ("mkdir -p " .. modpol.datadir) os.execute ("mkdir -p " .. modpol.datadir)
ocutil.setlogdir (modpol.datadir) modpol.ocutil.setlogdir (modpol.datadir)
ocutil.setlogname ("modpol.log") modpol.ocutil.setlogname ("modpol.log")
-- =================================================================== -- ===================================================================
-- Set up the Serpent Serializer functions. -- Set up the Serpent Serializer functions.
modpol.serpent = {} modpol.serpent = {}
dofile (modpol.topdir .. "/serpent/serpent.lua") dofile (modpol.topdir .. "/util/serpent/serpent.lua")
-- =================================================================== -- ===================================================================
-- This function stores "ledger" data to disk. -- This function stores "ledger" data to disk.
local store_ledger = function() 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)) modpol.serpent.dump (modpol.ledger))
if ok ~= true then if ok ~= true then
ocutil.fatal_error ("store_data: ledger") modpol.ocutil.fatal_error ("store_data: ledger")
end end
local nn = ocutil.table_length (modpol.ledger) local nn = modpol.ocutil.table_length (modpol.ledger)
local str = "entries" local str = "entries"
if nn == 1 then str = "entry" end 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 end
-- =================================================================== -- ===================================================================
-- This function stores "orgs" data to disk. -- This function stores "orgs" data to disk.
local store_orgs = function() 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)) modpol.serpent.dump (modpol.orgs))
if ok ~= true then if ok ~= true then
ocutil.fatal_error ("store_data: orgs") modpol.ocutil.fatal_error ("store_data: orgs")
end end
local nn = ocutil.table_length (modpol.orgs) local nn = modpol.ocutil.table_length (modpol.orgs)
local str = "entries" local str = "entries"
if nn == 1 then str = "entry" end if nn == 1 then str = "entry" end
ocutil.log (nn .. " orgs stored to disk") modpol.ocutil.log (nn .. " orgs stored to disk")
end end
-- =================================================================== -- ===================================================================
@@ -67,20 +67,20 @@ end
-- This function loads "ledger" data from disk. -- This function loads "ledger" data from disk.
local load_ledger = function() 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 if obj ~= nil then
local func, err = load (obj) local func, err = load (obj)
if err then if err then
ocutil.fatal_error ("load_data: ledger" ) modpol.ocutil.fatal_error ("load_data: ledger" )
end end
modpol.ledger = func() modpol.ledger = func()
local nn = ocutil.table_length (modpol.ledger) local nn = modpol.ocutil.table_length (modpol.ledger)
local str = "entries" local str = "entries"
if nn == 1 then str = "entry" end 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 else
ocutil.log ("No stored global ledger data found") modpol.ocutil.log ("No stored global ledger data found")
end end
end end
@@ -88,20 +88,20 @@ end
-- This function loads "orgs" data from disk. -- This function loads "orgs" data from disk.
local load_orgs = function() 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 if obj ~= nil then
local func, err = load (obj) local func, err = load (obj)
if err then if err then
ocutil.fatal_error ("load_data: orgs" ) modpol.ocutil.fatal_error ("load_data: orgs" )
end end
modpol.orgs = func() modpol.orgs = func()
local nn = ocutil.table_length (modpol.orgs) local nn = modpol.ocutil.table_length (modpol.orgs)
local str = "entries" local str = "entries"
if nn == 1 then str = "entry" end if nn == 1 then str = "entry" end
ocutil.log (nn .. " orgs loaded from disk") modpol.ocutil.log (nn .. " orgs loaded from disk")
else else
ocutil.log ("No stored orgs data found") modpol.ocutil.log ("No stored orgs data found")
end end
end end
@@ -4,8 +4,15 @@
-- See https://dev.minetest.net/StorageRef -- See https://dev.minetest.net/StorageRef
-- Loads content of stored orgs and ledger from mod_storage -- Loads content of stored orgs and ledger from mod_storage
--get modstorageref
local mod_storage = minetest.get_mod_storage()
modpol.load_storage = function() modpol.load_storage = function()
local mod_storage = minetest.get_mod_storage()
-- load orgs -- load orgs
local stored_orgs = minetest.deserialize(mod_storage:get_string("orgs")) local stored_orgs = minetest.deserialize(mod_storage:get_string("orgs"))
if (stored_orgs ~= nil) then if (stored_orgs ~= nil) then
@@ -20,6 +27,7 @@ end
-- Stores content of current orgs and ledger to mod_storage -- Stores content of current orgs and ledger to mod_storage
modpol.store_data = function() modpol.store_data = function()
-- write to storage -- write to storage
mod_storage:set_string("orgs", minetest.serialize(modpol.orgs)) mod_storage:set_string("orgs", minetest.serialize(modpol.orgs))
mod_storage:set_string("ledger", minetest.serialize(modpol.ledger)) mod_storage:set_string("ledger", minetest.serialize(modpol.ledger))
+94 -94
View File
@@ -1,7 +1,7 @@
-- =================================================================== -- ===================================================================
-- Overview. -- Overview.
-- This file is "top/ocutil.lua". -- This file is "top/modpol.ocutil.lua".
-- This file provides a collection of largely portable Lua utility -- This file provides a collection of largely portable Lua utility
-- functions. The collection is descended from one assembled by Old- -- 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 modpol.ocutil.log_console = true -- Flag: Copy log to console
ocutil.logdir = nil -- Absolute path for log-file direc- modpol.ocutil.logdir = nil -- Absolute path for log-file direc-
-- tory (or nil) -- tory (or nil)
-- =================================================================== -- ===================================================================
-- Function: ocutil.fixnil -- Function: modpol.ocutil.fixnil
-- Params: string s -- Params: string s
-- Outputs: -- 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)" if s == nil then s = "(nil)"
elseif s == "" then s = "(empty)" elseif s == "" then s = "(empty)"
end end
@@ -39,46 +39,46 @@ end
-- =================================================================== -- ===================================================================
-- Function: ocutil.setlogdir -- Function: modpol.ocutil.setlogdir
-- Params: string path -- Params: string path
-- Outputs: -- Outputs:
-- --
-- The input string should be the absolute path for a writable direc- -- 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. -- log files in that directory.
-- =================================================================== -- ===================================================================
ocutil.setlogdir = function (path) modpol.ocutil.setlogdir = function (path)
if path ~= nil and path ~= "" then if path ~= nil and path ~= "" then
ocutil.logdir = path modpol.ocutil.logdir = path
end end
end end
-- =================================================================== -- ===================================================================
-- Function: ocutil.setlogname -- Function: modpol.ocutil.setlogname
-- Params: string name -- Params: string name
-- Outputs: -- Outputs:
-- --
-- The input string should be a filename without a path component. -- 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. -- 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. -- directory.
-- =================================================================== -- ===================================================================
ocutil.setlogname = function (name) modpol.ocutil.setlogname = function (name)
if name ~= nil and name ~= "" then if name ~= nil and name ~= "" then
ocutil.logname = name modpol.ocutil.logname = name
end end
end end
-- =================================================================== -- ===================================================================
-- Function: ocutil.log -- Function: modpol.ocutil.log
-- Params: string s -- Params: string s
-- Outputs: -- Outputs:
-- --
@@ -87,26 +87,26 @@ end
-- =================================================================== -- ===================================================================
ocutil.log = function (s) modpol.ocutil.log = function (s)
s = ocutil.fixnil (s) s = modpol.ocutil.fixnil (s)
s = s:gsub ("\n$", "", 1) -- Remove trailing newline initially s = s:gsub ("\n$", "", 1) -- Remove trailing newline initially
if ocutil.log_console then if modpol.ocutil.log_console then
print (s) print (s)
end end
if ocutil.logdir ~= nil and if modpol.ocutil.logdir ~= nil and
ocutil.logname ~= nil then modpol.ocutil.logname ~= nil then
s = s .. "\n" -- Add trailing newline s = s .. "\n" -- Add trailing newline
local logpath = ocutil.logdir .. "/" .. ocutil.logname local logpath = modpol.ocutil.logdir .. "/" .. modpol.ocutil.logname
ocutil.file_append (logpath, s) modpol.ocutil.file_append (logpath, s)
end end
end end
-- =================================================================== -- ===================================================================
-- Function: ocutil.fatal_error -- Function: modpol.ocutil.fatal_error
-- Params: string s -- Params: string s
-- Outputs: -- Outputs:
-- --
@@ -115,15 +115,15 @@ end
-- =================================================================== -- ===================================================================
ocutil.fatal_error = function (s) modpol.ocutil.fatal_error = function (s)
ocutil.log ("Fatal Error: " .. s) modpol.ocutil.log ("Fatal Error: " .. s)
os.exit (1) os.exit (1)
return nil -- Shouldn't be reached return nil -- Shouldn't be reached
end end
-- =================================================================== -- ===================================================================
-- Function: ocutil.panic -- Function: modpol.ocutil.panic
-- Params: string s -- Params: string s
-- Outputs: -- Outputs:
-- --
@@ -132,15 +132,15 @@ end
-- =================================================================== -- ===================================================================
ocutil.panic = function (s) modpol.ocutil.panic = function (s)
ocutil.log ("Internal Error: " .. s) modpol.ocutil.log ("Internal Error: " .. s)
os.exit (1) os.exit (1)
return nil -- Shouldn't be reached return nil -- Shouldn't be reached
end end
-- =================================================================== -- ===================================================================
-- Function: ocutil.str_empty -- Function: modpol.ocutil.str_empty
-- Params: string x -- Params: string x
-- Outputs: -- Outputs:
-- --
@@ -149,7 +149,7 @@ end
-- =================================================================== -- ===================================================================
ocutil.str_empty = function (x) modpol.ocutil.str_empty = function (x)
if x == nil or x == "" then if x == nil or x == "" then
return true return true
else else
@@ -159,7 +159,7 @@ end
-- =================================================================== -- ===================================================================
-- Function: ocutil.str_nonempty -- Function: modpol.ocutil.str_nonempty
-- Params: string x -- Params: string x
-- Outputs: -- Outputs:
-- --
@@ -168,7 +168,7 @@ end
-- =================================================================== -- ===================================================================
ocutil.str_nonempty = function (x) modpol.ocutil.str_nonempty = function (x)
if x == nil or x == "" then if x == nil or x == "" then
return false return false
else else
@@ -178,7 +178,7 @@ end
-- =================================================================== -- ===================================================================
-- Function: ocutil.table_empty -- Function: modpol.ocutil.table_empty
-- Params: table tab -- Params: table tab
-- Outputs: -- Outputs:
-- --
@@ -187,7 +187,7 @@ end
-- =================================================================== -- ===================================================================
ocutil.table_empty = function (tab) modpol.ocutil.table_empty = function (tab)
local next = next local next = next
if next (tab) == nil then return true end if next (tab) == nil then return true end
return false return false
@@ -195,7 +195,7 @@ end
-- =================================================================== -- ===================================================================
-- Function: ocutil.table_nonempty -- Function: modpol.ocutil.table_nonempty
-- Params: table tab -- Params: table tab
-- Outputs: -- Outputs:
-- --
@@ -204,14 +204,14 @@ end
-- =================================================================== -- ===================================================================
ocutil.table_nonempty = function (tab) modpol.ocutil.table_nonempty = function (tab)
if ocutil.table_empty (tab) then return false end if modpol.ocutil.table_empty (tab) then return false end
return true return true
end end
-- =================================================================== -- ===================================================================
-- Function: ocutil.string_contains -- Function: modpol.ocutil.string_contains
-- Params: strings a, b -- Params: strings a, b
-- Outputs: -- 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 if string.match (a, b) then
return true return true
else else
@@ -230,7 +230,7 @@ end
-- =================================================================== -- ===================================================================
-- Function: ocutil.str_false -- Function: modpol.ocutil.str_false
-- Params: string x -- Params: string x
-- Outputs: -- Outputs:
-- --
@@ -240,7 +240,7 @@ end
-- =================================================================== -- ===================================================================
ocutil.str_false = function (x) modpol.ocutil.str_false = function (x)
if x == "false" or x == "no" or if x == "false" or x == "no" or
x == "off" or x == "0" or x == "off" or x == "0" or
x == false or x == 0 then x == false or x == 0 then
@@ -252,7 +252,7 @@ end
-- =================================================================== -- ===================================================================
-- Function: ocutil.str_true -- Function: modpol.ocutil.str_true
-- Params: string x -- Params: string x
-- Outputs: -- Outputs:
-- --
@@ -262,7 +262,7 @@ end
-- =================================================================== -- ===================================================================
ocutil.str_true = function (x) modpol.ocutil.str_true = function (x)
if x == "true" or x == "yes" or if x == "true" or x == "yes" or
x == "on" or x == "1" or x == "on" or x == "1" or
x == true or x == 1 then x == true or x == 1 then
@@ -274,7 +274,7 @@ end
-- =================================================================== -- ===================================================================
-- Function: ocutil.starts_with -- Function: modpol.ocutil.starts_with
-- Params: strings String, Start -- Params: strings String, Start
-- Outputs: -- 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 if string.sub (String, 1, string.len (Start)) == Start then
return true return true
else else
@@ -293,7 +293,7 @@ end
-- =================================================================== -- ===================================================================
-- Function: ocutil.not_starts_with -- Function: modpol.ocutil.not_starts_with
-- Params: strings String, Start -- Params: strings String, Start
-- Outputs: -- 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 if string.sub (String, 1, string.len (Start)) == Start then
return false return false
else else
@@ -312,7 +312,7 @@ end
-- =================================================================== -- ===================================================================
-- Function: ocutil.ends_with -- Function: modpol.ocutil.ends_with
-- Params: strings String, End -- Params: strings String, End
-- Outputs: -- 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, return End == '' or string.sub (String,
-string.len (End)) == End -string.len (End)) == End
end end
-- =================================================================== -- ===================================================================
-- Function: ocutil.not_ends_with -- Function: modpol.ocutil.not_ends_with
-- Params: strings String, End -- Params: strings String, End
-- Outputs: -- Outputs:
-- Returns false if the 1st string ends with the 2nd one -- Returns false if the 1st string ends with the 2nd one
@@ -338,8 +338,8 @@ end
-- =================================================================== -- ===================================================================
ocutil.not_ends_with = function (String, End) modpol.ocutil.not_ends_with = function (String, End)
if ocutil.ends_with (String, End) then if modpol.ocutil.ends_with (String, End) then
return false return false
else else
return true return true
@@ -348,33 +348,33 @@ end
-- =================================================================== -- ===================================================================
-- Function: ocutil.firstch -- Function: modpol.ocutil.firstch
-- Params: string str -- Params: string str
-- Outputs: -- Outputs:
-- Returns the 1st character of the string -- Returns the 1st character of the string
-- =================================================================== -- ===================================================================
ocutil.firstch = function (str) modpol.ocutil.firstch = function (str)
return string.sub (str, 1, 1) return string.sub (str, 1, 1)
end end
-- =================================================================== -- ===================================================================
-- Function: ocutil.first_to_upper -- Function: modpol.ocutil.first_to_upper
-- Params: string str -- Params: string str
-- Outputs: -- Outputs:
-- Returns the 1st character of the string in upper case -- 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)) return (str:gsub ("^%l", string.upper))
end end
-- =================================================================== -- ===================================================================
-- Function: ocutil.all_first_to_upper -- Function: modpol.ocutil.all_first_to_upper
-- Params: string str, flag cvtspace -- Params: string str, flag cvtspace
-- Outputs: -- 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", string.upper)
str = str:gsub ("[_ ]%l", str = str:gsub ("[_ ]%l",
function (a) return string.upper (a) end) function (a) return string.upper (a) end)
if ocutil.str_true (cvtspace) then if modpol.ocutil.str_true (cvtspace) then
str = str:gsub ("_", " ") str = str:gsub ("_", " ")
end end
@@ -400,7 +400,7 @@ end
-- =================================================================== -- ===================================================================
-- Function: ocutil.strtok -- Function: modpol.ocutil.strtok
-- Params: strings source, delimitch -- Params: strings source, delimitch
-- Outputs: -- Outputs:
-- --
@@ -413,7 +413,7 @@ end
-- =================================================================== -- ===================================================================
ocutil.strtok = function (source, delimitch) modpol.ocutil.strtok = function (source, delimitch)
if delimitch == nil or if delimitch == nil or
delimitch == "" then delimitch = " " end delimitch == "" then delimitch = " " end
@@ -432,7 +432,7 @@ end
-- =================================================================== -- ===================================================================
-- Function: ocutil.swap_key_value -- Function: modpol.ocutil.swap_key_value
-- Params: table itable -- Params: table itable
-- Outputs: -- Outputs:
-- Turns keys into values and vice versa and returns the resulting -- 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 if itable == nil then return nil end
local otable = {} local otable = {}
for key, value in pairs (itable) do otable [value] = key end 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 -- Params: table ktable
-- Outputs: -- Outputs:
-- --
@@ -476,7 +476,7 @@ end
-- print (ii .. ") " .. tabby [ii]) -- print (ii .. ") " .. tabby [ii])
-- end -- end
-- --
-- tabby = ocutil.ktable_to_vtable (tabby) -- tabby = modpol.ocutil.ktable_to_vtable (tabby)
-- --
-- print ("\nOutput table:") -- print ("\nOutput table:")
-- for ii, value in ipairs (tabby) do -- 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 = {} local vtable = {}
if ktable == nil then return vtable end 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 -- Params: table vtable, scalar set_to
-- Outputs: -- Outputs:
-- --
@@ -524,7 +524,7 @@ end
-- print (ii .. ") " .. tabby [ii]) -- print (ii .. ") " .. tabby [ii])
-- end -- end
-- --
-- tabby = ocutil.vtable_to_ktable (tabby, 42) -- tabby = modpol.ocutil.vtable_to_ktable (tabby, 42)
-- --
-- print ("\nOutput table:") -- print ("\nOutput table:")
-- for ii, value in pairs (tabby) do -- 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 = {} local ktable = {}
if vtable == nil then return ktable end if vtable == nil then return ktable end
@@ -548,7 +548,7 @@ end
-- =================================================================== -- ===================================================================
-- Function: ocutil.make_set -- Function: modpol.ocutil.make_set
-- Params: array harry -- Params: array harry
-- Outputs: -- Outputs:
-- --
@@ -559,12 +559,12 @@ end
-- Here's a complete code fragment which demonstrates operation of -- Here's a complete code fragment which demonstrates operation of
-- this function: -- 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 -- if color_set ["red"] ~= nil then print ("Supported color") end
-- =================================================================== -- ===================================================================
ocutil.make_set = function (list) modpol.ocutil.make_set = function (list)
local set = {} local set = {}
for _, l in ipairs (list) do set [l] = true end for _, l in ipairs (list) do set [l] = true end
return set return set
@@ -572,7 +572,7 @@ end
-- =================================================================== -- ===================================================================
-- Function: ocutil.pos_to_str -- Function: modpol.ocutil.pos_to_str
-- Params: position-table pos -- Params: position-table pos
-- Outputs: -- 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 return pos.x .. "," .. pos.y .. "," .. pos.z
end end
-- =================================================================== -- ===================================================================
-- Function: ocutil.table_length -- Function: modpol.ocutil.table_length
-- Params: table tabby -- Params: table tabby
-- Outputs: Returns number of entries in table -- 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 local count = 0
for _ in pairs (tabby) do count = count+1 end for _ in pairs (tabby) do count = count+1 end
return count return count
@@ -606,7 +606,7 @@ end
-- =================================================================== -- ===================================================================
-- Function: ocutil.clone_table -- Function: modpol.ocutil.clone_table
-- Params: table tabby -- Params: table tabby
-- Outputs: -- Outputs:
-- --
@@ -614,25 +614,25 @@ end
-- should work correctly for most cases, but special cases that re- -- should work correctly for most cases, but special cases that re-
-- quire additional work may exist. -- 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 if tabby == nil then return nil end
local copy = {} local copy = {}
for k, v in pairs (tabby) do 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 copy [k] = v
end end
return copy return copy
end 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 -- Params: string path
-- Outputs: -- 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 local file, err
file, err = io.open (path, "rb") file, err = io.open (path, "rb")
if err then return false end if err then return false end
@@ -652,7 +652,7 @@ end
-- =================================================================== -- ===================================================================
-- Function: ocutil.file_missing -- Function: modpol.ocutil.file_missing
-- Params: string path -- Params: string path
-- Outputs: -- Outputs:
-- --
@@ -662,14 +662,14 @@ end
-- =================================================================== -- ===================================================================
ocutil.file_missing = function (path) modpol.ocutil.file_missing = function (path)
if ocutil.file_exists (path) then return false end if modpol.ocutil.file_exists (path) then return false end
return true return true
end end
-- =================================================================== -- ===================================================================
-- Function: ocutil.file_read -- Function: modpol.ocutil.file_read
-- Params: string path -- Params: string path
-- Outputs: -- Outputs:
-- --
@@ -681,7 +681,7 @@ end
-- =================================================================== -- ===================================================================
ocutil.file_read = function (path) modpol.ocutil.file_read = function (path)
local file, err local file, err
file, err = io.open (path, "rb") file, err = io.open (path, "rb")
if err then return nil end if err then return nil end
@@ -692,7 +692,7 @@ end
-- =================================================================== -- ===================================================================
-- Function: ocutil.file_write -- Function: modpol.ocutil.file_write
-- Params: strings path, data -- Params: strings path, data
-- Outputs: -- Outputs:
-- --
@@ -707,7 +707,7 @@ end
-- =================================================================== -- ===================================================================
ocutil.file_write = function (path, data) modpol.ocutil.file_write = function (path, data)
local file, err local file, err
file, err = io.open (path, "wb") file, err = io.open (path, "wb")
if err then return false end if err then return false end
@@ -719,17 +719,17 @@ end
-- =================================================================== -- ===================================================================
-- Function: ocutil.file_append -- Function: modpol.ocutil.file_append
-- Params: strings path, data -- Params: strings path, data
-- Outputs: -- 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 -- difference: It appends to existing files as opposed to overwrites
-- them. -- them.
-- =================================================================== -- ===================================================================
ocutil.file_append = function (path, data) modpol.ocutil.file_append = function (path, data)
local file, err local file, err
file, err = io.open (path, "ab") file, err = io.open (path, "ab")
if err then return false end if err then return false end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.