fun_caves/chat.lua
2016-07-18 17:46:07 -05:00

279 lines
7.2 KiB
Lua

minetest.register_chatcommand("armor", {
params = "",
description = "Display your armor values",
privs = {},
func = function(player_name, param)
if not (player_name and type(player_name) == 'string' and player_name ~= '' and fun_caves.db.status) then
return
end
local player = minetest.get_player_by_name(player_name)
if not player then
return
end
local armor = player:get_armor_groups()
if armor then
minetest.chat_send_player(player_name, "Armor:")
for group, value in pairs(armor) do
minetest.chat_send_player(player_name, " "..group.." "..value)
end
if fun_caves.db.status[player_name].armor_elixir then
local armor_time = fun_caves.db.status[player_name].armor_elixir.remove
local game_time = minetest.get_gametime()
if not (armor_time and type(armor_time) == 'number' and game_time and type(game_time) == 'number') then
return
end
local min = math.floor(math.max(0, armor_time - game_time) / 60)
minetest.chat_send_player(player_name, "Your armor elixir will expire in "..min..' minutes.')
end
end
end,
})
minetest.register_chatcommand("setspawn", {
params = "",
description = "change your spawn position",
privs = {},
func = function(player_name, param)
if not (player_name and type(player_name) == 'string' and player_name ~= '') then
return
end
local player = minetest.get_player_by_name(player_name)
if not player then
return
end
local pos = player:getpos()
beds.spawn[player_name] = pos
minetest.chat_send_player(player_name, 'Your spawn position has been changed.')
end,
})
minetest.register_chatcommand("fixlight", {
params = "<radius>",
description = "attempt to fix light bugs",
privs = {},
func = function(player_name, param)
if not (player_name and type(player_name) == 'string' and player_name ~= '') then
return
end
local privs = minetest.check_player_privs(player_name, {server=true})
if not privs then
return
end
print('Fun Caves: '..player_name..' used the fixlight command')
local player = minetest.get_player_by_name(player_name)
if not player then
return
end
local pos = player:getpos()
if not pos then
return
end
pos = vector.round(pos)
local radius = tonumber(param) or 50
radius = math.floor(radius)
local minp = vector.subtract(pos, radius)
local maxp = vector.add(pos, radius)
local vm = minetest.get_voxel_manip(minp, maxp)
if not vm then
return
end
--vm:set_lighting({day = 0, night = 0}, minp, maxp)
vm:calc_lighting(minp, maxp)
vm:update_liquids()
vm:write_to_map()
vm:update_map()
end,
})
minetest.register_chatcommand("flatten", {
params = "<radius>",
description = "flatten terrain",
privs = {},
func = function(player_name, param)
if not (player_name and type(player_name) == 'string' and player_name ~= '') then
return
end
local privs = minetest.check_player_privs(player_name, {server=true})
if not privs then
return
end
print('Fun Caves: '..player_name..' used the flatten command')
local player = minetest.get_player_by_name(player_name)
if not player then
return
end
local pos = player:getpos()
if not pos then
return
end
pos = vector.round(pos)
local radius = tonumber(param) or 50
radius = math.floor(radius)
local minp = vector.subtract(pos, radius)
minp.y = minp.y - math.ceil(radius / 2)
local maxp = vector.add(pos, radius)
maxp.y = maxp.y + math.ceil(radius / 2)
local air = minetest.get_content_id('air')
local stone = minetest.get_content_id('default:stone')
local water = minetest.get_content_id('default:water_source')
local waters = {}
waters[minetest.get_content_id('default:water_source')] = true
waters[minetest.get_content_id('default:river_water_source')] = true
local stone_types = {'default:stone', 'default:desert_stone', 'default:sandstone', 'default:dirt', 'fun_caves:dirt', 'default:dirt_with_snow', 'default:dirt_with_grass', 'default:dirt_with_dry_grass', 'default:sand', 'default:desert_sand'}
local stones = {}
for i = 1, #stone_types do
stones[minetest.get_content_id(stone_types[i])] = true
end
local vm = minetest.get_voxel_manip(minp, maxp)
if not vm then
return
end
local emin, emax = vm:read_from_map(minp, maxp)
local area = VoxelArea:new({MinEdge = emin, MaxEdge = emax})
local data = vm:get_data()
local heightmap = {}
local height_avg = 0
local count = 0
local index = 1
for z = minp.z, maxp.z do
for x = minp.x, maxp.x do
local ivm = area:index(x, maxp.y, z)
for y = maxp.y, minp.y, -1 do
if stones[data[ivm]] then
if y >= maxp.y then
minetest.chat_send_player(player_name, 'Terrain altitude exceeds the given radius.')
return
end
heightmap[index] = y
height_avg = height_avg + heightmap[index]
count = count + 1
break
end
ivm = ivm - area.ystride
end
if not heightmap[index] then
minetest.chat_send_player(player_name, 'Terrain altitude exceeds the given radius.')
return
end
index = index + 1
end
end
height_avg = math.floor(height_avg / count + 0.5)
index = 1
for z = minp.z, maxp.z do
local dz = z - minp.z
for x = minp.x, maxp.x do
local dx = x - minp.x
local r = math.max(math.abs(radius - dx), math.abs(radius - dz)) / radius
local new_y = heightmap[index]
if r < 1 then
new_y = math.floor(heightmap[index] * r ^ 2 + height_avg * (1 - r ^ 2) + 0.5)
end
local delta = new_y - heightmap[index]
if delta > 0 then
local ivm = area:index(x, maxp.y, z)
for y = maxp.y - delta, new_y, -1 do
data[ivm] = data[ivm - area.ystride * delta]
ivm = ivm - area.ystride
end
elseif delta < 0 then
local ivm = area:index(x, new_y - 2, z)
for y = new_y - 2, maxp.y + delta do
data[ivm] = data[ivm - area.ystride * delta]
ivm = ivm + area.ystride
end
for y = maxp.y + delta + 1, maxp.y do
data[ivm] = air
ivm = ivm + area.ystride
end
end
local ivm = area:index(x, minp.y, z)
for y = minp.y, maxp.y do
if y <= 1 and data[ivm] == air then
data[ivm] = water
elseif y > 1 and waters[data[ivm]] then
data[ivm] = air
end
ivm = ivm + area.ystride
end
index = index + 1
end
end
vm:set_data(data)
vm:calc_lighting(minp, maxp)
vm:update_liquids()
vm:write_to_map()
vm:update_map()
end,
})
minetest.register_chatcommand("sleep", {
params = "",
description = "Sleep on the ground",
privs = {},
func = function(player_name, param)
local player = minetest.get_player_by_name(player_name)
if not (player and beds) then
return
end
if (beds.player and beds.player[player_name]) then
minetest.chat_send_player(player_name, 'You can\'t sleep.')
return
end
local pos = player:getpos()
if not pos then
return
end
pos = vector.round(pos)
local status, err = pcall(beds.on_rightclick, pos, player)
if status then
minetest.after(5, function()
local time = minetest.get_timeofday()
if not time or time < 0.23 or time > 0.3 then
return
end
local hp = player:get_hp()
if hp and type(hp) == 'number' then
player:set_hp(hp - 1)
end
minetest.chat_send_player(player_name, 'You\'d sleep better in a bed.')
end)
end
end,
})