diff --git a/chatcommands.lua b/chatcommands.lua index a8acefa..40b66f8 100644 --- a/chatcommands.lua +++ b/chatcommands.lua @@ -1,10 +1,10 @@ ------------------------------------------------------------------------------- --- factions Mod by Sapier +-- factionsmod Mod by Sapier -- -- License WTFPL -- --! @file chatcommnd.lua ---! @brief factions chat interface +--! @brief factionsmod chat interface --! @copyright Sapier --! @author Sapier --! @date 2013-05-08 @@ -12,18 +12,18 @@ -- Contact sapier a t gmx net ------------------------------------------------------------------------------- ---! @class factions_chat +--! @class factionsmod_chat --! @brief chat interface class -factions_chat = {} +factionsmod_chat = {} ------------------------------------------------------------------------------- -- name: init() -- --! @brief initialize chat interface ---! @memberof factions_chat +--! @memberof factionsmod_chat --! @public ------------------------------------------------------------------------------- -function factions_chat.init() +function factionsmod_chat.init() minetest.register_privilege("faction_user", { @@ -34,26 +34,26 @@ function factions_chat.init() minetest.register_privilege("faction_admin", { - description = "this user is allowed to create or delete factions", + description = "this user is allowed to create or delete factionsmod", give_to_singleplayer = true, } ) - minetest.register_chatcommand("factions", + minetest.register_chatcommand("factionsmod", { params = " .. ", description = "faction administration functions", privs = { interact=true }, - func = factions_chat.cmdhandler, + func = factionsmod_chat.cmdhandler, } ) minetest.register_chatcommand("af", { params = "text", - description = "send message to all factions", + description = "send message to all factionsmod", privs = { faction_user=true }, - func = factions_chat.allfactions_chathandler, + func = factionsmod_chat.allfactionsmod_chathandler, } ) @@ -62,7 +62,7 @@ function factions_chat.init() params = " text", description = "send message to a specific faction", privs = { faction_user=true }, - func = factions_chat.chathandler, + func = factionsmod_chat.chathandler, } ) end @@ -71,13 +71,13 @@ end -- name: cmdhandler(playername,parameter) -- --! @brief chat command handler ---! @memberof factions_chat +--! @memberof factionsmod_chat --! @private -- --! @param playername name --! @param parameter data supplied to command ------------------------------------------------------------------------------- -function factions_chat.cmdhandler(playername,parameter) +function factionsmod_chat.cmdhandler(playername,parameter) local player = minetest.env:get_player_by_name(playername) local params = parameter:split(" ") @@ -87,11 +87,11 @@ function factions_chat.cmdhandler(playername,parameter) if parameter == nil or parameter == "" then - local playerfactions = factions.get_factions(player) + local playerfactionsmod = factionsmod.get_factionsmod(player) - local tosend = "Factions: " .. playername .. " factions:" - for i,v in ipairs(playerfactions) do - if i ~= #playerfactions then + local tosend = "factionsmod: " .. playername .. " factionsmod:" + for i,v in ipairs(playerfactionsmod) do + if i ~= #playerfactionsmod then tosend = tosend .. " " .. v .. "," else tosend = tosend .. " " .. v @@ -102,17 +102,17 @@ function factions_chat.cmdhandler(playername,parameter) end if cmd == "claim" then - local playerfactions = factions.get_factions(player) + local playerfactionsmod = factionsmod.get_factionsmod(player) return end if cmd == "unclaim" then return end - --list all known factions + --list all known factionsmod if cmd == "list" then - local list = factions.get_faction_list() - local tosend = "Factions: current available factions:" + local list = factionsmod.get_faction_list() + local tosend = "factionsmod: current available factionsmod:" for i,v in ipairs(list) do if i ~= #list then @@ -125,9 +125,9 @@ function factions_chat.cmdhandler(playername,parameter) return end - --show factions mod version + --show factionsmod mod version if cmd == "version" then - minetest.chat_send_player(playername, "Factions: version " .. factions_version , false) + minetest.chat_send_player(playername, "factionsmod: version " .. factionsmod_version , false) return end @@ -135,8 +135,8 @@ function factions_chat.cmdhandler(playername,parameter) if cmd == "info" then if params[2] ~= nil then minetest.chat_send_player(playername, - "Factions: " .. params[2] .. ": " .. - factions.get_description(params[2]), false) + "factionsmod: " .. params[2] .. ": " .. + factionsmod.get_description(params[2]), false) return end end @@ -146,19 +146,19 @@ function factions_chat.cmdhandler(playername,parameter) if params[3] ~= nil then local toremove = minetest.env:get_player_by_name(params[3]) --allowed if faction_admin, admin of faction or player itself - if factions.is_admin(params[2],playername) and + if factionsmod.is_admin(params[2],playername) and toremove ~= nil then - factions.member_remove(params[2],toremove) + factionsmod.member_remove(params[2],toremove) minetest.chat_send_player(playername, - "Factions: " .. params[3] .. " has been removed from " + "factionsmod: " .. params[3] .. " has been removed from " .. params[2], false) return end else - factions.member_remove(params[2],player) + factionsmod.member_remove(params[2],player) minetest.chat_send_player(playername, - "Factions: You have left " .. params[2], false) + "factionsmod: You have left " .. params[2], false) return end end @@ -169,20 +169,20 @@ function factions_chat.cmdhandler(playername,parameter) --create new faction if cmd == "create" then if params[2] ~= nil then - if factions.add_faction(params[2]) then + if factionsmod.add_faction(params[2]) then minetest.chat_send_player(playername, - "Factions: created faction " .. params[2], + "factionsmod: created faction " .. params[2], false) - if factions.member_add(params[2],minetest.env:get_player_by_name(playername)) then + if factionsmod.member_add(params[2],minetest.env:get_player_by_name(playername)) then minetest.chat_send_player(playername, - "Factions: " .. playername .. " joined faction " .. + "factionsmod: " .. playername .. " joined faction " .. params[2], false) end return else minetest.chat_send_player(playername, - "Factions: FAILED to created faction " .. params[2], + "factionsmod: FAILED to created faction " .. params[2], false) return end @@ -193,23 +193,23 @@ function factions_chat.cmdhandler(playername,parameter) if cmd == "join" then if params[2] ~= nil then --check for invitation - if factions.is_invited(params[2],playername) then - if factions.member_add(params[2],player) then + if factionsmod.is_invited(params[2],playername) then + if factionsmod.member_add(params[2],player) then minetest.chat_send_player(playername, - "Factions: joined faction " .. + "factionsmod: joined faction " .. params[2], false) return else minetest.chat_send_player(playername, - "Factions: FAILED to join faction " .. + "factionsmod: FAILED to join faction " .. params[2], false) return end else minetest.chat_send_player(playername, - "Factions: you are not allowed to join " .. params[2], + "factionsmod: you are not allowed to join " .. params[2], false) return end @@ -219,18 +219,18 @@ function factions_chat.cmdhandler(playername,parameter) --all following commands require at least two parameters if params[2] ~= nil then if minetest.check_player_privs(playername,{ faction_admin=true }) or - factions.is_admin(params[2],playername) then + factionsmod.is_admin(params[2],playername) then --delete faction if cmd == "delete" then - if factions.delete_faction(params[2]) then + if factionsmod.delete_faction(params[2]) then minetest.chat_send_player(playername, - "Factions: deleted faction " .. params[2], + "factionsmod: deleted faction " .. params[2], false) return else minetest.chat_send_player(playername, - "Factions: FAILED to deleted faction " .. params[2], + "factionsmod: FAILED to deleted faction " .. params[2], false) return end @@ -245,14 +245,14 @@ function factions_chat.cmdhandler(playername,parameter) value = true end - if factions.set_free(params[2],value) then + if factionsmod.set_free(params[2],value) then minetest.chat_send_player(playername, - "Factions: free to join for " .. params[2] .. + "factionsmod: free to join for " .. params[2] .. " has been set to " .. params[3], false) else minetest.chat_send_player(playername, - "Factions: FAILED to set free to join for " .. + "factionsmod: FAILED to set free to join for " .. params[2], false) end @@ -269,14 +269,14 @@ function factions_chat.cmdhandler(playername,parameter) value = true end - if factions.set_admin(params[2],params[3],value) then + if factionsmod.set_admin(params[2],params[3],value) then minetest.chat_send_player(playername, - "Factions: adminstate of " .. params[3] .. + "factionsmod: adminstate of " .. params[3] .. " has been set to " .. params[4], false) else minetest.chat_send_player(playername, - "Factions: FAILED to set admin privileges for " .. + "factionsmod: FAILED to set admin privileges for " .. params[3], false) end @@ -292,15 +292,15 @@ function factions_chat.cmdhandler(playername,parameter) for i=4, #params, 1 do desc = desc .. " " .. params[i] end - if factions.set_description(params[2],desc) then + if factionsmod.set_description(params[2],desc) then minetest.chat_send_player(playername, - "Factions: updated description of faction " .. + "factionsmod: updated description of faction " .. params[2], false) return else minetest.chat_send_player(playername, - "Factions: FAILED to update description of faction " .. + "factionsmod: FAILED to update description of faction " .. params[2], false) return @@ -310,19 +310,19 @@ function factions_chat.cmdhandler(playername,parameter) if cmd == "invite" and params[2] ~= nil and params[3] ~= nil then - if factions.member_invite(params[2],params[3]) then + if factionsmod.member_invite(params[2],params[3]) then minetest.chat_send_player(params[3], - "Factions: " .. params[3] .. + "factionsmod: " .. params[3] .. " you have been invited to join faction " .. params[2], false) minetest.chat_send_player(playername, - "Factions: " .. params[3] .. + "factionsmod: " .. params[3] .. " has been invited to join faction " .. params[2], false) return else minetest.chat_send_player(playername, - "Factions: FAILED to invite " .. params[3] .. + "factionsmod: FAILED to invite " .. params[3] .. " to join faction " .. params[2], false) return @@ -331,28 +331,28 @@ function factions_chat.cmdhandler(playername,parameter) end end - factions_chat.show_help(playername) + factionsmod_chat.show_help(playername) end ------------------------------------------------------------------------------- --- name: allfactions_chathandler(playername,parameter) +-- name: allfactionsmod_chathandler(playername,parameter) -- --! @brief chat handler ---! @memberof factions_chat +--! @memberof factionsmod_chat --! @private -- --! @param playername name --! @param parameter data supplied to command ------------------------------------------------------------------------------- -function factions_chat.allfactions_chathandler(playername,parameter) +function factionsmod_chat.allfactionsmod_chathandler(playername,parameter) local player = minetest.env:get_player_by_name(playername) if player ~= nil then local recipients = {} - for faction,value in pairs(factions.get_factions(player)) do - for name,value in pairs(factions.dynamic_data.membertable[faction]) do + for faction,value in pairs(factionsmod.get_factionsmod(player)) do + for name,value in pairs(factionsmod.dynamic_data.membertable[faction]) do local object_to_check = mientest.env:get_player_by_name(name) if object_to_check ~= nil then @@ -368,20 +368,20 @@ function factions_chat.allfactions_chathandler(playername,parameter) end return end - factions_chat.show_help(playername) + factionsmod_chat.show_help(playername) end ------------------------------------------------------------------------------- -- name: chathandler(playername,parameter) -- --! @brief chat handler ---! @memberof factions_chat +--! @memberof factionsmod_chat --! @private -- --! @param playername name --! @param parameter data supplied to command ------------------------------------------------------------------------------- -function factions_chat.chathandler(playername,parameter) +function factionsmod_chat.chathandler(playername,parameter) local player = minetest.env:get_player_by_name(playername) @@ -396,17 +396,17 @@ function factions_chat.chathandler(playername,parameter) local valid_faction = false - for faction,value in pairs(factions.get_factions(player)) do + for faction,value in pairs(factionsmod.get_factionsmod(player)) do if target_faction == faction then valid_faction = true end end if faction ~= nil and valid_faction and - factions.dynamic_data.membertable[faction] ~= nil then - for name,value in pairs(factions.dynamic_data.membertable[faction]) do + factionsmod.dynamic_data.membertable[faction] ~= nil then + for name,value in pairs(factionsmod.dynamic_data.membertable[faction]) do local object_to_check = mientest.env:get_player_by_name(name) - factions_chat.show_help(playername) + factionsmod_chat.show_help(playername) if object_to_check ~= nil and name ~= playername then minetest.chat_send_player(name,playername ..": " .. text,false) @@ -414,44 +414,44 @@ function factions_chat.chathandler(playername,parameter) end else minetest.chat_send_player(playername, - "Factions: you're not a member of " .. dump(faction),false) + "factionsmod: you're not a member of " .. dump(faction),false) end return end - factions_chat.show_help(playername) + factionsmod_chat.show_help(playername) end ------------------------------------------------------------------------------- -- name: show_help(playername,parameter) -- --! @brief send help message to player ---! @memberof factions_chat +--! @memberof factionsmod_chat --! @private -- --! @param playername name ------------------------------------------------------------------------------- -function factions_chat.show_help(playername) +function factionsmod_chat.show_help(playername) local MSG = function(text) minetest.chat_send_player(playername,text,false) end - MSG("Factions mod") + MSG("factionsmod mod") MSG("Usage:") MSG("\tUser commands:") - MSG("\t\t/factions -> info on your current factions") - MSG("\t\t/factions info -> show description of faction") - MSG("\t\t/factions list -> show list of factions") - MSG("\t\t/factions leave -> leave specified faction") - MSG("\t\t/factions join -> join specified faction") - MSG("\t\t/factions version -> show version number of mod") + MSG("\t\t/factionsmod -> info on your current factionsmod") + MSG("\t\t/factionsmod info -> show description of faction") + MSG("\t\t/factionsmod list -> show list of factionsmod") + MSG("\t\t/factionsmod leave -> leave specified faction") + MSG("\t\t/factionsmod join -> join specified faction") + MSG("\t\t/factionsmod version -> show version number of mod") MSG("\tAdmin commands:") - MSG("\t\t/factions create -> create a new faction") - MSG("\t\t/factions delete -> delete a faction faction") - MSG("\t\t/factions leave -> remove player from faction") - MSG("\t\t/factions invite -> invite player to faction") - MSG("\t\t/factions set_free -> set faction free to join") - MSG("\t\t/factions admin -> make player admin of faction") - MSG("\t\t/factions description -> set description for faction") + MSG("\t\t/factionsmod create -> create a new faction") + MSG("\t\t/factionsmod delete -> delete a faction faction") + MSG("\t\t/factionsmod leave -> remove player from faction") + MSG("\t\t/factionsmod invite -> invite player to faction") + MSG("\t\t/factionsmod set_free -> set faction free to join") + MSG("\t\t/factionsmod admin -> make player admin of faction") + MSG("\t\t/factionsmod description -> set description for faction") end \ No newline at end of file diff --git a/factionsmod.lua b/factionsmod.lua new file mode 100644 index 0000000..3d4a70f --- /dev/null +++ b/factionsmod.lua @@ -0,0 +1,793 @@ +------------------------------------------------------------------------------- +-- factionsmod Mod by Sapier +-- +-- License WTFPL +-- +--! @file factionsmod.lua +--! @brief factionsmod core file containing datastorage +--! @copyright Sapier +--! @author Sapier +--! @date 2013-05-08 +-- +-- Contact sapier a t gmx net +------------------------------------------------------------------------------- + +--read some basic information +local factionsmod_worldid = minetest.get_worldpath() + +--! @class factionsmod +--! @brief main class for factionsmod +factionsmod = {} + +--! @brief runtime data +factionsmod.data = {} +factionsmod.data.factionsmod = {} +factionsmod.data.objects = {} +factionsmod.dynamic_data = {} +factionsmod.dynamic_data.membertable = {} + +factionsmod.print = function(text) + print("factionsmod: " .. dump(text)) +end + +factionsmod.dbg_lvl1 = function() end --factionsmod.print -- errors +factionsmod.dbg_lvl2 = function() end --factionsmod.print -- non cyclic trace +factionsmod.dbg_lvl3 = function() end --factionsmod.print -- cyclic trace + +------------------------------------------------------------------------------- +-- name: add_faction(name) +-- +--! @brief add a faction +--! @memberof factionsmod +--! @public +-- +--! @param name of faction to add +--! +--! @return true/false (succesfully added faction or not) +------------------------------------------------------------------------------- +function factionsmod.add_faction(name) + + if factionsmod.data.factionsmod[name] == nil then + factionsmod.data.factionsmod[name] = {} + factionsmod.data.factionsmod[name].reputation = {} + factionsmod.data.factionsmod[name].base_reputation = {} + factionsmod.data.factionsmod[name].adminlist = {} + factionsmod.data.factionsmod[name].invitations = {} + factionsmod.data.factionsmod[name].owner = "" + + factionsmod.dynamic_data.membertable[name] = {} + + factionsmod.save() + + return true + end + + return false +end + +------------------------------------------------------------------------------- +-- name: set_base_reputation(faction1,faction2,value) +-- +--! @brief set base reputation between two factionsmod +--! @memberof factionsmod +--! @public +-- +--! @param faction1 first faction +--! @param faction2 second faction +--! @param value value to use +--! +--! @return true/false (succesfully added faction or not) +------------------------------------------------------------------------------- +function factionsmod.set_base_reputation(faction1,faction2,value) + + if factionsmod.data.factionsmod[faction1] ~= nil and + factionsmod.data.factionsmod[faction2] ~= nil then + + factionsmod.data.factionsmod[faction1].base_reputation[faction2] = value + factionsmod.data.factionsmod[faction2].base_reputation[faction1] = value + factionsmod.save() + return true + end + return false +end + +------------------------------------------------------------------------------- +-- name: get_base_reputation(faction1,faction2) +-- +--! @brief get base reputation between two factionsmod +--! @memberof factionsmod +--! @public +-- +--! @param faction1 first faction +--! @param faction2 second faction +--! +--! @return reputation/0 if none set +------------------------------------------------------------------------------- +function factionsmod.get_base_reputation(faction1,faction2) + factionsmod.dbg_lvl3("get_base_reputation: " .. faction1 .. "<-->" .. faction2) + if factionsmod.data.factionsmod[faction1] ~= nil and + factionsmod.data.factionsmod[faction2] ~= nil then + if factionsmod.data.factionsmod[faction1].base_reputation[faction2] ~= nil then + return factionsmod.data.factionsmod[faction1].base_reputation[faction2] + end + end + return 0 +end + +------------------------------------------------------------------------------- +-- name: set_description(name,description) +-- +--! @brief set description for a faction +--! @memberof factionsmod +--! @public +-- +--! @param name of faction +--! @param description text describing a faction +--! +--! @return true/false (succesfully set description) +------------------------------------------------------------------------------- +function factionsmod.set_description(name,description) + + if factionsmod.data.factionsmod[name] ~= nil then + factionsmod.data.factionsmod[name].description = description + factionsmod.save() + return true + end + return false +end + +------------------------------------------------------------------------------- +-- name: get_description(name) +-- +--! @brief get description for a faction +--! @memberof factionsmod +--! @public +-- +--! @param name of faction +--! +--! @return description or "" +------------------------------------------------------------------------------- +function factionsmod.get_description(name) + + if factionsmod.data.factionsmod[name] ~= nil and + factionsmod.data.factionsmod[name].description ~= nil then + return factionsmod.data.factionsmod[name].description + end + return "" +end + +------------------------------------------------------------------------------- +-- name: exists(name) +-- +--! @brief check if a faction exists +--! @memberof factionsmod +--! @public +--! @param name name to check +--! +--! @return true/false +------------------------------------------------------------------------------- +function factionsmod.exists(name) + + for key,value in pairs(factionsmod.data.factionsmod) do + if key == name then + return true + end + end + + return false +end + +------------------------------------------------------------------------------- +-- name: get_faction_list() +-- +--! @brief get list of factionsmod +--! @memberof factionsmod +--! @public +--! +--! @return list of factionsmod +------------------------------------------------------------------------------- +function factionsmod.get_faction_list() + + local retval = {} + + for key,value in pairs(factionsmod.data.factionsmod) do + table.insert(retval,key) + end + + return retval +end + +------------------------------------------------------------------------------- +-- name: delete_faction(name) +-- +--! @brief delete a faction +--! @memberof factionsmod +--! @public +-- +--! @param name of faction to delete +--! +--! @return true/false (succesfully added faction or not) +------------------------------------------------------------------------------- +function factionsmod.delete_faction(name) + + factionsmod.data.factionsmod[name] = nil + + factionsmod.save() + + if factionsmod.data.factionsmod[name] == nil then + return true + end + + return false +end + +------------------------------------------------------------------------------- +-- name: member_add(name,object) +-- +--! @brief add an entity or player to a faction +--! @memberof factionsmod +--! @public +-- +--! @param name of faction to add object to +--! @param object to add to faction +--! +--! @return true/false (succesfully added faction or not) +------------------------------------------------------------------------------- +function factionsmod.claim() +end +function factionsmod.unclaim() +end +function factionsmod.member_add(name, object) + local new_entry = {} + new_entry.factionsmod = {} + + if object.object ~= nil then + object = object.object + end + if next(factionsmod.get_factionsmod(object)) ~= nil then + for k,v in pairs(factionsmod.get_factionsmod(object)) do + if k ~= nil then + factionsmod.member_remove(k,object) + end + end + end + if not factionsmod.exists(name) then + print("Unable to add to NON existant faction >" .. name .. "<") + return false + end + + new_entry.name,new_entry.temporary = factionsmod.get_name(object) + + factionsmod.dbg_lvl2("Adding name=" .. dump(new_entry.name) .. " to faction: " .. name ) + + if new_entry.name ~= nil then + if factionsmod.data.objects[new_entry.name] == nil then + factionsmod.data.objects[new_entry.name] = new_entry + end + + if factionsmod.data.objects[new_entry.name].factionsmod[name] == nil then + factionsmod.data.objects[new_entry.name].factionsmod[name] = true + factionsmod.dynamic_data.membertable[name][new_entry.name] = true + factionsmod.data.factionsmod[name].invitations[new_entry.name] = nil + if factionsmod.data.factionsmod[name].owner == "" then + factionsmod.data.factionsmod[name].owner = object:get_player_name() + factionsmod.set_admin(name,object:get_player_name(), true) + end + factionsmod.save() + return true + end + end + + --return false if no valid object or already member + return false +end + +------------------------------------------------------------------------------- +-- name: member_invite(name,playername) +-- +--! @brief invite a player for joining a faction +--! @memberof factionsmod +--! @public +-- +--! @param name of faction to add object to +--! @param name of player to invite +--! +--! @return true/false (succesfully added invitation or not) +------------------------------------------------------------------------------- +function factionsmod.member_invite(name, playername) + + if factionsmod.data.factionsmod[name] ~= nil and + factionsmod.data.factionsmod[name].invitations[playername] == nil then + factionsmod.data.factionsmod[name].invitations[playername] = true + factionsmod.save() + return true + end + + --return false if not a valid faction or player already invited + return false +end + +------------------------------------------------------------------------------- +-- name: member_remove(name,object) +-- +--! @brief remove an entity or player to a faction +--! @memberof factionsmod +--! @public +-- +--! @param name of faction to add object to +--! @param object to add to faction +--! +--! @return true/false (succesfully added faction or not) +------------------------------------------------------------------------------- +function factionsmod.member_remove(name,object) + + local id,type = factionsmod.get_name(object) + + factionsmod.dbg_lvl2("removing name=" .. dump(id) .. " to faction: " .. name ) + + if id ~= nil and + factionsmod.data.objects[id] ~= nil and + factionsmod.data.objects[id].factionsmod[name] ~= nil then + factionsmod.data.objects[id].factionsmod[name] = nil + factionsmod.dynamic_data.membertable[name][id] = nil + if factionsmod.data.factionsmod[name].owner == object:get_player_name() then + factionsmod.delete_faction(name) + end + factionsmod.save() + return true + end + + if id ~= nil and + factionsmod.data.factionsmod[name].invitations[id] ~= nil then + factionsmod.data.factionsmod[name].invitations[id] = nil + factionsmod.save() + return true + end + + return false +end + +------------------------------------------------------------------------------- +-- name: set_admin(name,playername,value) +-- +--! @brief set admin priviles for a playername +--! @memberof factionsmod +--! @public +-- +--! @param name of faction to add object to +--! @param playername to change rights +--! @param value true/false has or has not admin privileges +--! +--! @return true/false (succesfully changed privileges) +------------------------------------------------------------------------------- +function factionsmod.set_admin(name,playername,value) + mobf_assert_backtrace(type(playername) == "string") + if factionsmod.data.factionsmod[name] ~= nil then + if value then + factionsmod.data.factionsmod[name].adminlist[playername] = true + factionsmod.save() + return true + else + factionsmod.data.factionsmod[name].adminlist[playername] = nil + factionsmod.save() + return true + end + else + print("factionsmod: no faction >" .. name .. "< found") + end + + return false +end + +------------------------------------------------------------------------------- +-- name: set_free(name,value) +-- +--! @brief set faction to be joinable by everyone +--! @memberof factionsmod +--! @public +-- +--! @param name of faction to add object to +--! @param value true/false has or has not admin privileges +--! +--! @return true/false (succesfully added faction or not) +------------------------------------------------------------------------------- +function factionsmod.set_free(name,value) + + if factionsmod.data.factionsmod[name] ~= nil then + if value then + if factionsmod.data.factionsmod[name].open == nil then + factionsmod.data.factionsmod[name].open = true + factionsmod.save() + return true + else + return false + end + else + if factionsmod.data.factionsmod[name].open == nil then + return false + else + factionsmod.data.factionsmod[name].open = nil + factionsmod.save() + return true + end + end + end + + return false +end + +------------------------------------------------------------------------------- +-- name: is_free(name) +-- +--! @brief check if a fraction is free to join +--! @memberof factionsmod +--! @public +-- +--! @param name of faction to add object to +-- +--! @return true/false (free or not) +------------------------------------------------------------------------------- +function factionsmod.is_free(name) + if factionsmod.data.factionsmod[name] ~= nil and + factionsmod.data.factionsmod[name].open then + return true + end + + return false +end + +------------------------------------------------------------------------------- +-- name: is_admin(name,playername) +-- +--! @brief read admin privilege of player +--! @memberof factionsmod +--! @public +-- +--! @param name of faction to check rights +--! @param playername to change rights +--! +--! @return true/false (succesfully added faction or not) +------------------------------------------------------------------------------- +function factionsmod.is_admin(name,playername) + + if factionsmod.data.factionsmod[name] ~= nil and + factionsmod.data.factionsmod[name].adminlist[playername] == true then + return true + end + + return false +end + +------------------------------------------------------------------------------- +-- name: is_invited(name,playername) +-- +--! @brief read invitation status of player +--! @memberof factionsmod +--! @public +-- +--! @param name of faction to check for invitation +--! @param playername to change rights +--! +--! @return true/false (succesfully added faction or not) +------------------------------------------------------------------------------- +function factionsmod.is_invited(name,playername) + + if factionsmod.data.factionsmod[name] ~= nil and + ( factionsmod.data.factionsmod[name].invitations[playername] == true or + factionsmod.data.factionsmod[name].open == true) then + return true + end + + return false +end + +------------------------------------------------------------------------------- +-- name: get_factionsmod(object) +-- +--! @brief get list of factionsmod for an object +--! @memberof factionsmod +--! @public +-- +--! @param object to get list for +--! +--! @return list of factionsmod +------------------------------------------------------------------------------- +function factionsmod.get_factionsmod(object) + + local id,type = factionsmod.get_name(object) + + local retval = {} + if id ~= nil and + factionsmod.data.objects[id] ~= nil then + for key,value in pairs(factionsmod.data.objects[id].factionsmod) do + table.insert(retval,key) + end + end + + return retval +end + +------------------------------------------------------------------------------- +-- name: is_member(name,object) +-- +--! @brief check if object is member of name +--! @memberof factionsmod +--! @public +-- +--! @param name of faction to check +--! @param object to check +--! +--! @return true/false +------------------------------------------------------------------------------- +function factionsmod.is_member(name,object) + + local retval = false + + local id,type = factionsmod.get_name(object) + + if id ~= nil and + factionsmod.data.objects[id] ~= nil then + for key,value in pairs(factionsmod.data.objects[id].factionsmod) do + if key == name then + retval = true + break + end + end + end + + return retval +end + + +------------------------------------------------------------------------------- +-- name: get_reputation(name,object) +-- +--! @brief get reputation of an object +--! @memberof factionsmod +--! @public +-- +--! @param name name of faction to check for reputation +--! @param object object to get reputation for +--! +--! @return number value -100 to 100 0 being neutral, -100 beeing enemy 100 friend +------------------------------------------------------------------------------- +function factionsmod.get_reputation(name,object) + + local id,type = factionsmod.get_name(object) + + factionsmod.dbg_lvl3("get_reputation: " .. name .. "<-->" .. dump(id)) + + if id ~= nil and + factionsmod.data.factionsmod[name] ~= nil then + + factionsmod.dbg_lvl3("get_reputation: object reputation: " .. dump(factionsmod.data.factionsmod[name].reputation[id])) + + if factionsmod.data.factionsmod[name].reputation[id] == nil then + factionsmod.data.factionsmod[name].reputation[id] + = factionsmod.calc_base_reputation(name,object) + end + + return factionsmod.data.factionsmod[name].reputation[id] + else + factionsmod.dbg_lvl3("get_reputation: didn't find any factionsmod for: " .. name) + end + + return 0 +end + +------------------------------------------------------------------------------- +-- name: modify_reputation(name,object,delta) +-- +--! @brief modify reputation of an object for a faction +--! @memberof factionsmod +--! @public +-- +--! @param name name of faction to modify reputation +--! @param object object to change reputation +--! @param delta value to change reputation +--! +--! @return true/false +------------------------------------------------------------------------------- +function factionsmod.modify_reputation(name,object,delta) + + local id,type = factionsmod.get_name(object) + + if factionsmod.data.factionsmod[name] ~= nil then + if factionsmod.data.factionsmod[name].reputation[id] == nil then + factionsmod.data.factionsmod[name].reputation[id] + = factionsmod.calc_base_reputation(name,object) + end + + factionsmod.data.factionsmod[name].reputation[id] + = factionsmod.data.factionsmod[name].reputation[id] + delta + factionsmod.save() + return true + end + + return false +end + +------------------------------------------------------------------------------- +-- name: get_name(object) +-- +--! @brief get textual name of object +--! @memberof factionsmod +--! @private +-- +--! @param object fetch name for this +--! +--! @return name or nil,is temporary element +------------------------------------------------------------------------------- +function factionsmod.get_name(object) + if object == nil then + return nil,true + end + + if object.object ~= nil then + object = object.object + end + + if object:is_player() then + return object:get_player_name(),false + else + local luaentity = object:get_luaentity() + + if luaentity ~= nil then + return tostring(luaentity),true + end + end + + return nil,true +end + +------------------------------------------------------------------------------- +-- name: calc_base_reputation(name,object) +-- +--! @brief calculate initial reputation of object within a faction +--! @memberof factionsmod +--! @private +-- +--! @param name name of faction +--! @param object calc reputation for this +--! +--! @return reputation value +------------------------------------------------------------------------------- +function factionsmod.calc_base_reputation(name,object) + + --calculate initial reputation based uppon all groups + local object_factionsmod = factionsmod.get_factionsmod(object) + local rep_value = 0 + + factionsmod.dbg_lvl3("calc_base_reputation: " .. name .. " <--> " .. tostring(object)) + + if object_factionsmod ~= nil then + factionsmod.dbg_lvl3("calc_base_reputation: " .. tostring(object) .. " is in " .. #object_factionsmod .. " factionsmod") + for k,v in pairs(object_factionsmod) do + if factionsmod.data.factionsmod[v] == nil then + print("factionsmod: warning object is member of faction " .. v .. " which doesn't exist") + else + factionsmod.dbg_lvl3("calc_base_reputation: " .. name .. " <--> " .. v .. " rep=" .. dump(factionsmod.data.factionsmod[v].base_reputation[name])) + if factionsmod.data.factionsmod[v].base_reputation[name] ~= nil then + rep_value = + rep_value + factionsmod.data.factionsmod[v].base_reputation[name] + end + end + end + + rep_value = rep_value / #object_factionsmod + end + + return rep_value +end + +------------------------------------------------------------------------------- +-- name: save() +-- +--! @brief save data to file +--! @memberof factionsmod +--! @private +------------------------------------------------------------------------------- +function factionsmod.save() + + --saving is done much more often than reading data to avoid delay + --due to figuring out which data to save and which is temporary only + --all data is saved here + --this implies data needs to be cleant up on load + + local file,error = io.open(factionsmod_worldid .. "/" .. "factionsmod.conf","w") + + if file ~= nil then + file:write(minetest.serialize(factionsmod.data)) + file:close() + else + minetest.log("error","MOD factionsmod: unable to save factionsmod world specific data!: " .. error) + end + +end + +------------------------------------------------------------------------------- +-- name: load() +-- +--! @brief load data from file +--! @memberof factionsmod +--! @private +-- +--! @return true/false +------------------------------------------------------------------------------- +function factionsmod.load() + local file,error = io.open(factionsmod_worldid .. "/" .. "factionsmod.conf","r") + + if file ~= nil then + local raw_data = file:read("*a") + file:close() + + if raw_data ~= nil and + raw_data ~= "" then + + local raw_table = minetest.deserialize(raw_data) + + + --read object data + local temp_objects = {} + + if raw_table.objects ~= nil then + for key,value in pairs(raw_table.objects) do + + if value.temporary == false then + factionsmod.data.objects[key] = value + else + temp_objects[key] = true + end + end + end + + if raw_table.factionsmod ~= nil then + for key,value in pairs(raw_table.factionsmod) do + factionsmod.data.factionsmod[key] = {} + factionsmod.data.factionsmod[key].base_reputation = value.base_reputation + factionsmod.data.factionsmod[key].adminlist = value.adminlist + factionsmod.data.factionsmod[key].open = value.open + factionsmod.data.factionsmod[key].invitations = value.invitations + + factionsmod.data.factionsmod[key].reputation = {} + for repkey,repvalue in pairs(value.reputation) do + if temp_objects[repkey] == nil then + factionsmod.data.factionsmod[key].reputation[repkey] = repvalue + end + end + + factionsmod.dynamic_data.membertable[key] = {} + end + end + + --populate dynamic faction member table + for id,object in pairs(factionsmod.data.objects) do + for name,value in pairs(factionsmod.data.objects[id].factionsmod) do + if value then + factionsmod.dynamic_data.membertable[name][id] = true + end + end + end + end + else + local file,error = io.open(factionsmod_worldid .. "/" .. "factionsmod.conf","w") + + if file ~= nil then + file:close() + else + minetest.log("error","MOD factionsmod: unable to save factionsmod world specific data!: " .. error) + end + end + + --create special faction players + --factionsmod.add_faction("players") + + --autojoin players to faction players + minetest.register_on_joinplayer( + function(player) + if player:is_player() then + --factionsmod.member_add("players",player) + end + end + ) +end \ No newline at end of file diff --git a/init.lua b/init.lua index a5003b4..a809f5a 100644 --- a/init.lua +++ b/init.lua @@ -1,10 +1,10 @@ ------------------------------------------------------------------------------- --- factions Mod by Sapier +-- factionsmod Mod by Sapier -- -- License WTFPL -- --! @file init.lua ---! @brief factions mod to be used by other mods +--! @brief factionsmod mod to be used by other mods --! @copyright Sapier --! @author Sapier --! @date 2013-05-08 @@ -12,18 +12,18 @@ -- Contact sapier a t gmx net ------------------------------------------------------------------------------- -local factions_version = "0.1.6" +local factionsmod_version = "0.1.6" -core.log("action", "MOD: factions (by sapier) loading ...") +core.log("action", "MOD: factionsmod (by sapier) loading ...") --!path of mod -factions_modpath = minetest.get_modpath("factions") +factionsmod_modpath = minetest.get_modpath("factionsmod") -dofile (factions_modpath .. "/factions.lua") -dofile (factions_modpath .. "/chatcommands.lua") +dofile (factionsmod_modpath .. "/factionsmod.lua") +dofile (factionsmod_modpath .. "/chatcommands.lua") -factions.load() -factions_chat.init() +factionsmod.load() +factionsmod_chat.init() -core.log("action","MOD: factions (by sapier) " .. factions_version .. " loaded.") +core.log("action","MOD: factionsmod (by sapier) " .. factionsmod_version .. " loaded.")