minetest-mods/kmchat/init.lua

208 lines
6.9 KiB
Lua
Raw Normal View History

2013-08-22 10:55:58 -07:00
-- kmchat - a simple local chat mod for minetest
2014-02-06 10:25:29 -08:00
-- Copyright (C) 2014 hunterdelyx1, vegasd, sullome (Konungstvo Midgard)
2013-08-22 10:55:58 -07:00
--
-- This file is part of KMRP minetest-mods
--
-- KMRP minetest-mods is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- KMRP minetest-mods is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with KMRP minetest-mods. If not, see <http://www.gnu.org/licenses/>.
--
-------------------------------------------------------------------------------
--
-- Features:
-- * Local chat
-- * Comfortable whisper and shout w/o commands
2014-02-07 08:24:14 -08:00
-- * Colorful chat
2013-08-22 10:55:58 -07:00
-- * Local and global OOC-chat
-- * GM-prefixes
2013-08-22 12:19:11 -07:00
-- * Dices
2013-08-22 10:55:58 -07:00
2013-08-22 10:24:54 -07:00
-- config zone {{{
2014-02-07 08:24:14 -08:00
formats = {
-- ["MATCH"] = {"FORMAT" RANGE COLOR PRIV}, --
2014-02-07 10:19:48 -08:00
["_(.+)"] = {"%s (OOC): (( %s ))", 18, 0x9966AA, nil },
["%(%((.+)%)%)"] = {"%s (OOC): (( %s ))", 18, 0x9966AA, nil },
2014-02-09 04:39:16 -08:00
["!(.+)"] = {"%s (shouts): %s", 68, 0xFFFFFF, nil },
2014-02-07 10:19:48 -08:00
["=(.+)"] = {"%s (whispers): %s", 3, 0xE0EEE0, nil },
["*(.+)"] = {"* %s %s", 18, 0xFFFF00, nil },
["#(.+)"] = {"*** %s: %s ***", 18, 0xFFFF00, "gm"},
["?(.+)"] = {"%s (OOC): %s ***", 31000, 0x20EEDD, nil },
2014-02-07 08:24:14 -08:00
}
2014-02-07 10:19:48 -08:00
DEFAULT_FORMAT = "%s: %s"
DEFAULT_RANGE = 18
DEFAULT_COLOR = 0xEEF3EE
DICE_COLOR = 0xFFFF00
GMSPY_COLOR = 0x666666
2014-02-07 08:24:14 -08:00
GM_PREFIX = "[GM] "
2014-02-05 01:38:13 -08:00
MESSAGES_ON_SCREEN = 10
2014-02-05 02:20:04 -08:00
MAX_LENGTH = 100
2014-02-05 01:38:13 -08:00
LEFT_INDENT = 0.01
TOP_INDENT = 0.92
FONT_WIDTH = 12
FONT_HEIGHT = 28
2014-10-29 19:37:03 -07:00
fudge_levels = {"-","terrible--","terrible-","terrible", "poor", "mediocre", "fair", "good", "great", "superb", "legendary", "legendary+", "legendary++","like Allah"}
2013-08-22 10:24:54 -07:00
-- config zone }}}
2014-02-06 15:00:10 -08:00
firsthud = nil
2014-02-05 01:38:13 -08:00
2014-02-07 08:35:41 -08:00
function addMessage(player, new_text, new_color)
2014-02-05 01:59:51 -08:00
local temp_text
local temp_color
2014-02-06 09:31:48 -08:00
local hud
2014-02-05 03:07:54 -08:00
for id = firsthud, (firsthud+MESSAGES_ON_SCREEN-1) do
2014-02-06 09:31:48 -08:00
hud = player:hud_get(id)
if hud.name == "chat" then
temp_text = hud.text
temp_color = hud.number
player:hud_change(id, "number", new_color)
player:hud_change(id, "text", new_text)
new_text = temp_text
new_color = temp_color
end
2014-02-05 01:38:13 -08:00
end
end
2014-02-07 08:35:41 -08:00
function sendMessage(player, message, color)
local splitter
while message:len() > MAX_LENGTH do
splitter = string.find (message, " ", MAX_LENGTH)
if splitter == nil then
splitter = MAX_LENGTH
end
addMessage(player, message:sub(0,splitter), color)
message = message:sub(splitter+1)
end
addMessage(player, message, color)
end
2014-02-05 01:38:13 -08:00
minetest.register_on_joinplayer(function(player)
2014-02-06 09:31:48 -08:00
minetest.after(2, function(player)
for i = 1, MESSAGES_ON_SCREEN do
2014-02-05 01:38:13 -08:00
local hud_id = player:hud_add({
hud_elem_type = "text",
2014-02-06 09:31:48 -08:00
text = "",
2014-02-05 01:38:13 -08:00
position = {x = LEFT_INDENT, y = TOP_INDENT},
name = "chat",
scale = {x=500, y=50},
number = 0xFFFFFF,
item = 0,
direction = 0,
alignment = {x=1, y=0},
offset = {x=0, y=-i*FONT_HEIGHT}
})
2014-02-06 15:00:10 -08:00
if not firsthud then
2014-02-05 03:07:54 -08:00
firsthud = hud_id
end
2014-02-05 01:38:13 -08:00
end
end, player)
end)
2013-08-22 11:09:37 -07:00
minetest.register_privilege("gm", "Gives accses to reading all messages in the chat")
2013-08-22 10:24:54 -07:00
minetest.register_on_chat_message(function(name, message)
2014-02-07 10:19:48 -08:00
fmt = DEFAULT_FORMAT
range = DEFAULT_RANGE
color = DEFAULT_COLOR
2014-02-05 01:38:13 -08:00
pl = minetest.get_player_by_name(name)
pls = minetest.get_connected_players()
2014-02-07 08:24:14 -08:00
-- formats (see config zone)
for m, f in pairs(formats) do
submes = string.match(message, m)
if submes then
2014-02-07 10:19:48 -08:00
if not f[4] then -- if PRIV==nil
2014-02-07 08:24:14 -08:00
fmt = f[1]
range = f[2]
color = f[3]
break
elseif minetest.check_player_privs(name, {[f[4]]=true}) then
fmt = f[1]
range = f[2]
color = f[3]
break
end
end
end
2013-08-22 10:24:54 -07:00
2014-02-07 08:24:14 -08:00
-- dices
2014-10-29 19:37:03 -07:00
dice = string.match(message, "^d(%d+).*")
2014-02-07 08:24:14 -08:00
if dice=="4" or dice=="6" or dice=="8" or dice=="10" or dice=="12" or dice=="20" then
2014-02-07 10:19:48 -08:00
fmt = "*** %s rolls d"..dice.." and the result is %s ***"
color = DICE_COLOR
2014-02-07 08:24:14 -08:00
submes = math.random(dice)
end
2014-10-29 19:37:03 -07:00
--Temporary solution for 4dF
fudge_dice_tmp = string.match(message, "^4dF (.*)$")
if fudge_dice_tmp~=nil then
for key, val in pairs(fudge_levels) do
fudge_level = string.match(fudge_dice_tmp, "^("..val..".*)")
fudge_level_key = key
if fudge_level~=nil then
diff = 0
signs = ""
for i = 1, 4 do
rand = math.random(3)
if rand == 1 then
diff=diff+1
signs = signs.."+"
elseif rand == 2 then
diff=diff-1
signs = signs.."-"
else
signs = signs.."="
end
end
fmt = "*** %s rolls 4df ("..signs..") from "..fudge_level.." and the result is %s ***"
color = DICE_COLOR
fudge_level_key = fudge_level_key+diff
if fudge_level_key<1 then
fudge_level_key = 1
elseif fudge_level_key>#fudge_levels then
fudge_level_key = #fudge_levels
end
submes = fudge_levels[fudge_level_key]
break
end
end
end
2014-02-07 08:24:14 -08:00
if not submes then
2014-02-05 01:38:13 -08:00
submes = message
end
2013-08-22 10:24:54 -07:00
2014-02-05 01:38:13 -08:00
-- GM's prefix
2014-02-07 08:24:14 -08:00
if minetest.check_player_privs(name, {["gm"]=true,}) then
2014-02-07 10:19:48 -08:00
name = GM_PREFIX .. name
2014-02-05 01:38:13 -08:00
end
2013-08-22 10:55:58 -07:00
2014-02-05 01:38:13 -08:00
senderpos = pl:getpos()
for i = 1, #pls do
recieverpos = pls[i]:getpos()
if math.sqrt((senderpos.x-recieverpos.x)^2 + (senderpos.y-recieverpos.y)^2 + (senderpos.z-recieverpos.z)^2) < range then
2014-02-07 10:19:48 -08:00
sendMessage(pls[i], string.format(fmt, name, submes), color)
2014-02-05 01:38:13 -08:00
elseif minetest.check_player_privs(pls[i]:get_player_name(), {gm=true}) then
2014-02-07 10:19:48 -08:00
sendMessage(pls[i], string.format(fmt, name, submes), GMSPY_COLOR)
2014-02-05 01:38:13 -08:00
end
2014-10-29 19:37:03 -07:00
print(string.format(fmt, name, submes))
2013-08-22 10:24:54 -07:00
end
2014-02-05 01:38:13 -08:00
return true
end)