diff --git a/mods/trading/.gitrepo b/mods/trading/.gitrepo new file mode 100644 index 0000000..608489b --- /dev/null +++ b/mods/trading/.gitrepo @@ -0,0 +1,11 @@ +; DO NOT EDIT (unless you know what you are doing) +; +; This subdirectory is a git "subrepo", and this file is maintained by the +; git-subrepo command. See https://github.com/git-commands/git-subrepo#readme +; +[subrepo] + remote = https://github.com/GunshipPenguin/trading.git + branch = master + commit = c64b543b058dc168d08bd21e49a385f0542567d8 + parent = b41aa2ba3e87fc78a1d13fcbefa62e8dfa120202 + cmdver = 0.3.1 diff --git a/mods/trading/COPYING b/mods/trading/COPYING new file mode 100644 index 0000000..0e259d4 --- /dev/null +++ b/mods/trading/COPYING @@ -0,0 +1,121 @@ +Creative Commons Legal Code + +CC0 1.0 Universal + + CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE + LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN + ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS + INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES + REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS + PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM + THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED + HEREUNDER. + +Statement of Purpose + +The laws of most jurisdictions throughout the world automatically confer +exclusive Copyright and Related Rights (defined below) upon the creator +and subsequent owner(s) (each and all, an "owner") of an original work of +authorship and/or a database (each, a "Work"). + +Certain owners wish to permanently relinquish those rights to a Work for +the purpose of contributing to a commons of creative, cultural and +scientific works ("Commons") that the public can reliably and without fear +of later claims of infringement build upon, modify, incorporate in other +works, reuse and redistribute as freely as possible in any form whatsoever +and for any purposes, including without limitation commercial purposes. +These owners may contribute to the Commons to promote the ideal of a free +culture and the further production of creative, cultural and scientific +works, or to gain reputation or greater distribution for their Work in +part through the use and efforts of others. + +For these and/or other purposes and motivations, and without any +expectation of additional consideration or compensation, the person +associating CC0 with a Work (the "Affirmer"), to the extent that he or she +is an owner of Copyright and Related Rights in the Work, voluntarily +elects to apply CC0 to the Work and publicly distribute the Work under its +terms, with knowledge of his or her Copyright and Related Rights in the +Work and the meaning and intended legal effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be +protected by copyright and related or neighboring rights ("Copyright and +Related Rights"). Copyright and Related Rights include, but are not +limited to, the following: + + i. the right to reproduce, adapt, distribute, perform, display, + communicate, and translate a Work; + ii. moral rights retained by the original author(s) and/or performer(s); +iii. publicity and privacy rights pertaining to a person's image or + likeness depicted in a Work; + iv. rights protecting against unfair competition in regards to a Work, + subject to the limitations in paragraph 4(a), below; + v. rights protecting the extraction, dissemination, use and reuse of data + in a Work; + vi. database rights (such as those arising under Directive 96/9/EC of the + European Parliament and of the Council of 11 March 1996 on the legal + protection of databases, and under any national implementation + thereof, including any amended or successor version of such + directive); and +vii. other similar, equivalent or corresponding rights throughout the + world based on applicable law or treaty, and any national + implementations thereof. + +2. Waiver. To the greatest extent permitted by, but not in contravention +of, applicable law, Affirmer hereby overtly, fully, permanently, +irrevocably and unconditionally waives, abandons, and surrenders all of +Affirmer's Copyright and Related Rights and associated claims and causes +of action, whether now known or unknown (including existing as well as +future claims and causes of action), in the Work (i) in all territories +worldwide, (ii) for the maximum duration provided by applicable law or +treaty (including future time extensions), (iii) in any current or future +medium and for any number of copies, and (iv) for any purpose whatsoever, +including without limitation commercial, advertising or promotional +purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each +member of the public at large and to the detriment of Affirmer's heirs and +successors, fully intending that such Waiver shall not be subject to +revocation, rescission, cancellation, termination, or any other legal or +equitable action to disrupt the quiet enjoyment of the Work by the public +as contemplated by Affirmer's express Statement of Purpose. + +3. Public License Fallback. Should any part of the Waiver for any reason +be judged legally invalid or ineffective under applicable law, then the +Waiver shall be preserved to the maximum extent permitted taking into +account Affirmer's express Statement of Purpose. In addition, to the +extent the Waiver is so judged Affirmer hereby grants to each affected +person a royalty-free, non transferable, non sublicensable, non exclusive, +irrevocable and unconditional license to exercise Affirmer's Copyright and +Related Rights in the Work (i) in all territories worldwide, (ii) for the +maximum duration provided by applicable law or treaty (including future +time extensions), (iii) in any current or future medium and for any number +of copies, and (iv) for any purpose whatsoever, including without +limitation commercial, advertising or promotional purposes (the +"License"). The License shall be deemed effective as of the date CC0 was +applied by Affirmer to the Work. Should any part of the License for any +reason be judged legally invalid or ineffective under applicable law, such +partial invalidity or ineffectiveness shall not invalidate the remainder +of the License, and in such case Affirmer hereby affirms that he or she +will not (i) exercise any of his or her remaining Copyright and Related +Rights in the Work or (ii) assert any associated claims and causes of +action with respect to the Work, in either case contrary to Affirmer's +express Statement of Purpose. + +4. Limitations and Disclaimers. + + a. No trademark or patent rights held by Affirmer are waived, abandoned, + surrendered, licensed or otherwise affected by this document. + b. Affirmer offers the Work as-is and makes no representations or + warranties of any kind concerning the Work, express, implied, + statutory or otherwise, including without limitation warranties of + title, merchantability, fitness for a particular purpose, non + infringement, or the absence of latent or other defects, accuracy, or + the present or absence of errors, whether or not discoverable, all to + the greatest extent permissible under applicable law. + c. Affirmer disclaims responsibility for clearing rights of other persons + that may apply to the Work or any use thereof, including without + limitation any person's Copyright and Related Rights in the Work. + Further, Affirmer disclaims responsibility for obtaining any necessary + consents, permissions or other rights required for any use of the + Work. + d. Affirmer understands and acknowledges that Creative Commons is not a + party to this document and has no duty or obligation with respect to + this CC0 or use of the Work. diff --git a/mods/trading/README.md b/mods/trading/README.md new file mode 100644 index 0000000..e0d75e3 --- /dev/null +++ b/mods/trading/README.md @@ -0,0 +1,24 @@ +# Trading + +A Minetest mod by GunshipPenguin + +Allows players to trade items with each other via the use of the /trade and +/accepttrade commands. + +License: CC0 (See COPYING file) + +## Usage + +Players must have the "trade" privilege in order to request trades. + +To request a trade use: + +`/trade ` + +To accept a trade offer from a specific player, use: + +`/accepttrade ` + +## Configuration + +See settings.lua for configuration. diff --git a/mods/trading/init.lua b/mods/trading/init.lua new file mode 100644 index 0000000..7b4316f --- /dev/null +++ b/mods/trading/init.lua @@ -0,0 +1,381 @@ +--[[ +Trading mod for Minetest by GunshipPenguin + +To the extent possible under law, the author(s) have dedicated all copyright and +related and neighboring rights to this software to the public domain worldwide. This +software is distributed without any warranty. +--]] +dofile(minetest.get_modpath("trading") .. "/settings.lua") + +local available_invs = {} +local all_invs = {} + +local pending_trades = {} + +minetest.register_privilege("trade", + "Player can request to trade with other players using the /trade command") + +local trade_formspec = +"size[9,9.5;]" .. +"label[0,0;You are offering]" .. +"label[5,0;OTHER_PLAYER is offering]" .. +"button[7,4.5;2,1;make_trade_button;Make Trade]" .. +"label[0,5;Your Inventory]" .. +"list[current_player;main;0.5,5.5;8,4;]" .. +"list[detached:LEFT_INV;main;0,0.5;4,4;]" .. +"list[detached:RIGHT_INV;main;5,0.5;4,4]" + +local cancel_formspec = +"size[5,2;]" .. +"label[0,0;Other player canceled trade]" .. +"button_exit[1.5,1;2,1;exit_button;OK]" + +local trade_complete_formspec = +"size[5,2;]" .. +"label[0,0;Trade Complete]" .. +"button_exit[1.5,1;2,1;exit_button;OK]" + +-- Returns the active trade involving the specified player +local get_active_trade_involving_player = function(player_name) + for _,trade in ipairs(pending_trades) do + if trade.requester == player_name or trade.accepter == player_name then + if trade.active then + return trade + end + end + end + return nil +end + +-- Returns the trade with the specified requester and accepter +local get_trade = function(requester, accepter) + for _,trade in ipairs(pending_trades) do + if trade.requester == requester and trade.accepter == accepter then + return trade + end + end + return nil +end + +-- Return the inventories in the specified trade to the list of available inventories +local free_trade_inventories = function(trade) + local inv_list = {trade.requester_trade_inv, trade.accepter_trade_inv} + for _,inv_name in ipairs(inv_list) do + if all_invs[inv_name] then + available_invs[#available_invs+1] = inv_name + else + minetest.log("error", + "Warning, trading tried to free a trade inventory that does not exist") + end + end + return; +end + +-- Remove all trades from the pending_trades table involving the specified player +local remove_trades_involving_player = function(player_name) + for index,trade in ipairs(pending_trades) do + if trade.requester == player_name or trade.accepter == player_name then + free_trade_inventories(trade) + table.remove(pending_trades, index) + end + end + return; +end + +-- Remove the trade from the pending_trades table with the specified requester and accepter +local remove_trade = function(requester, accepter) + for index,trade in ipairs(pending_trades) do + if trade.requester == requester and trade.accepter == accepter then + free_trade_inventories(trade) + table.remove(pending_trades, index) + return; + end + end + return; +end + +-- Returns the name of a detached trade inventory for use during a trade +local create_trade_inventory = function(player_name, trade) + if #available_invs < 1 then + return false + end + + local inv_name = available_invs[1] + local inv_ref = minetest.create_detached_inventory(inv_name, { + allow_move = function(inv, from_list, from_index, to_list, to_index, count, player) + if player:get_player_name() == player_name then + return 99 + else + return 0 + end + end, + + allow_put = function(inv, listname, index, stack, player) + if player:get_player_name() == player_name then + return 99 + else + return 0 + end + end, + + allow_take = function(inv, listname, index, stack, player) + if player:get_player_name() == player_name then + return 99 + else + return 0 + end + end, + + on_move = function(inv, from_list, from_index, to_list, to_index, count, player) + if trade.requester == player:get_player_name() then + trade.accepter_ready = false + else + trade.requester_ready = false + end + end, + + on_put = function(inv, listname, index, stack, player) + if trade.requester == player:get_player_name() then + trade.accepter_ready = false + else + trade.requester_ready = false + end + end, + + on_take = function(inv, listname, index, stack, player) + if trade.requester == player:get_player_name() then + trade.accepter_ready = false + else + trade.requester_ready = false + end + end, + }) + inv_ref:set_size("main", 16) + inv_ref:set_list("main", {}) + table.remove(available_invs, 1) + return inv_name +end + +local Trade = {} +Trade.__index = Trade + +-- Creates a new trade +function Trade:new(requester, accepter) + return setmetatable({ + requester=requester, + accepter=accepter, + requester_trade_inv=nil, + accepter_trade_inv=nil, + requester_ready=false, + accepter_ready = false, + active=false}, Trade) +end + +-- Obtain 2 trade inventories for the trade, show the trade formspec to both the accepter +-- and the requester, and mark this trade as active +function Trade:start() + self.accepter_trade_inv = create_trade_inventory(self.accepter, self) + self.requester_trade_inv = create_trade_inventory(self.requester, self) + + local requester_formspec = string.gsub(trade_formspec, + "LEFT_INV", self.requester_trade_inv) + requester_formspec = string.gsub(requester_formspec, + "RIGHT_INV", self.accepter_trade_inv) + requester_formspec = string.gsub(requester_formspec, + "OTHER_PLAYER", self.accepter) + + local accepter_formspec = string.gsub(trade_formspec, + "LEFT_INV", self.accepter_trade_inv) + accepter_formspec = string.gsub(accepter_formspec, + "RIGHT_INV", self.requester_trade_inv) + accepter_formspec = string.gsub(accepter_formspec, + "OTHER_PLAYER", self.requester) + + minetest.show_formspec(self.requester, + "trading:trade_formspec", requester_formspec) + minetest.show_formspec(self.accepter, + "trading:trade_formspec", accepter_formspec) + + self.active = true + return +end + +function Trade:set_player_ready(player_name) + if self.requester == player_name then + self.requester_ready = true + elseif self.accepter == player_name then + self.accepter_ready = true + end + return +end + +function Trade:set_player_not_ready(player_name) + if self.requester == player_name then + self.requester_ready = false + elseif self.accepter == playername then + self.accepter_ready = false + end + return +end + +-- Cancel the trade, give the requesters offered items back and the accepter's offered items back +function Trade:cancel() + -- Give requester items back to requester + local requester_inv = minetest.get_inventory({type="player", name=self.requester}) + local requester_trade_inv = minetest.get_inventory( + {type="detached", name=self.requester_trade_inv}) + for _,itemstack in pairs(requester_trade_inv:get_list("main")) do + requester_inv:add_item("main", itemstack:to_string()) + end + requester_trade_inv:set_list("main", {}) + + -- Give accepter items back to accepter + local accepter_inv = minetest.get_inventory({type="player", name=self.accepter}) + local accepter_trade_inv = minetest.get_inventory( + {type="detached", name=self.accepter_trade_inv}) + for _,itemstack in pairs(accepter_trade_inv:get_list("main")) do + accepter_inv:add_item("main", itemstack:to_string()) + end + accepter_trade_inv:set_list("main", {}) +end + +-- Finalize the trade, give the requester's offered items to the accepter and vica versa +function Trade:finalize() + -- Give requester items to accepter + local accepter_inv = minetest.get_inventory({type="player", name=self.accepter}) + local requester_trade_inv = minetest.get_inventory( + {type="detached", name=self.requester_trade_inv}) + for _,itemstack in pairs(requester_trade_inv:get_list("main")) do + accepter_inv:add_item("main", itemstack:to_string()) + end + requester_trade_inv:set_list("main", {}) + + -- Give accepter items to requester + local requester_inv = minetest.get_inventory({type="player", name=self.requester}) + local accepter_trade_inv = minetest.get_inventory( + {type="detached", name=self.accepter_trade_inv}) + for _,itemstack in pairs(accepter_trade_inv:get_list("main")) do + requester_inv:add_item("main", itemstack:to_string()) + end + accepter_trade_inv:set_list("main", {}) +end + +minetest.register_on_player_receive_fields(function(player, formname, fields) + if fields.quit == "true" and formname == "trading:trade_formspec" then + local trade = get_active_trade_involving_player(player:get_player_name()) + if player:get_player_name() == trade.requester then + minetest.show_formspec(trade.accepter, + "trading:cancel_trade", cancel_formspec) + else + minetest.show_formspec(trade.requester, + "trading:cancel_trade", cancel_formspec) + end + remove_trade(trade.requester, trade.accepter) + trade:cancel() + elseif fields.make_trade_button then + local trade = get_active_trade_involving_player(player:get_player_name()) + trade:set_player_ready(player:get_player_name()) + if trade.requester_ready and trade.accepter_ready then + trade:finalize() + remove_trade(trade.requester, trade.accepter) + minetest.show_formspec(trade.requester, + "trading:trade_completed", trade_complete_formspec) + minetest.show_formspec(trade.accepter, + "trading:trade_completed", trade_complete_formspec) + end + end +end) + +-- Cancel any active and inactive trades a player has when he leaves +minetest.register_on_leaveplayer(function(player) + local active_trade = get_active_trade_involving_player(player:get_player_name()) + if active_trade then + if player:get_player_name() == active_trade.requester then + minetest.show_formspec(active_trade.accepter, + "trading:cancel_trade", cancel_formspec) + else + minetest.show_formspec(active_trade.requester, + "trading:cancel_trade", cancel_formspec) + end + remove_trade(active_trade.requester, active_trade.accepter) + active_trade:cancel() + end + + remove_trades_involving_player(player:get_player_name()) +end) + +-- Create more trade inventories when players join in order to accommodate more trades +minetest.register_on_joinplayer(function(player) + local curr_num_invs = 0 + for _,_ in pairs(all_invs) do + curr_num_invs = curr_num_invs + 1 + end + local invs_to_add = (#minetest.get_connected_players()) - (curr_num_invs) + + for i=curr_num_invs+1,curr_num_invs+invs_to_add do + available_invs[#available_invs+1] = "trade_inv_" .. tostring(i) + all_invs["trade_inv_" .. tostring(i)] = true + end +end) + +minetest.register_chatcommand("trade", { + description="Request to trade with a player", + params = "", + func = function(player_name, param) + if not minetest.check_player_privs(player_name, {trade=true}) then + return false, "You do not have the trade privilege" + end + + if player_name == param then + return false, "You cannot start a trade with yourself" + end + + local requested_player = minetest.get_player_by_name(param) + if not requested_player then + return false, "Requested player not found" + end + + if get_trade(player_name, param) then + return false, "You have already have a pending trade request with " .. param + end + + local trade = Trade:new(player_name, param) + pending_trades[#pending_trades+1] = trade + minetest.chat_send_player(param, player_name .. + " has requested to trade with you, use /accepttrade " + .. player_name .. " to accept") + return true, "Trade request sent" + end +}) + +minetest.register_chatcommand("accepttrade", { + description="Accept a trade request from a player", + params = "", + func = function(player_name, param) + local requested_player = minetest.get_player_by_name(param) + + if not requested_player then + return false, "Requested player not found" + end + + if TRADE_DISTANCE ~= -1 then + accepter_pos = minetest.get_player_by_name(player_name):getpos() + requester_pos = minetest.get_player_by_name(param):getpos() + local dist = math.sqrt(math.pow(requester_pos.x - accepter_pos.x, 2) + + math.pow(requester_pos.y - accepter_pos.y, 2) + + math.pow(requester_pos.z - accepter_pos.z, 2)) + + if dist > TRADE_DISTANCE then + return false, "You are too far away from " .. param .. " to trade, move closer" + end + end + + for _,trade in ipairs(pending_trades) do + if trade.requester == param and trade.accepter == player_name then + trade:start() + return true + end + end + return false, "Requested player did not request a trade with you" + end +}) diff --git a/mods/trading/settings.lua b/mods/trading/settings.lua new file mode 100644 index 0000000..a8fe459 --- /dev/null +++ b/mods/trading/settings.lua @@ -0,0 +1,2 @@ +-- Maximum distance (in nodes) that players can be seperated by and still trade (-1 to disable) +TRADE_DISTANCE = -1