1
0
Fork 0
MineClone2/mods/ITEMS/mcl_compass/init.lua

90 lines
2.8 KiB
Lua
Raw Normal View History

2017-02-20 20:14:10 +00:00
mcl_compass = {}
2015-06-29 17:55:56 +00:00
local default_spawn_settings = minetest.setting_get("static_spawnpoint")
minetest.register_globalstep(function(dtime)
local players = minetest.get_connected_players()
for i,player in ipairs(players) do
local function has_compass(player)
for _,stack in ipairs(player:get_inventory():get_list("main")) do
if minetest.get_item_group(stack:get_name(), "compass") ~= 0 then
return true
end
end
return false
end
if has_compass(player) then
local spawn = {x=0,y=0,z=0}
local s = minetest.setting_get("static_spawnpoint")
if s then
local numbers = string.split(s, ",")
spawn.x = tonumber(numbers[1])
spawn.y = tonumber(numbers[2])
spawn.z = tonumber(numbers[3])
if type(spawn.x) ~= "number" and type(spawn.y) ~= "number" and type(spawn.z) ~= "number" then
spawn = {x=0,y=0,z=0}
end
end
2017-01-09 02:56:45 +00:00
local pos = player:getpos()
local dir = player:get_look_horizontal()
2015-06-29 17:55:56 +00:00
local angle_north = math.deg(math.atan2(spawn.x - pos.x, spawn.z - pos.z))
if angle_north < 0 then angle_north = angle_north + 360 end
local angle_dir = -math.deg(dir)
2017-01-09 01:03:09 +00:00
local angle_relative = (angle_north - angle_dir + 180) % 360
local compass_image = math.floor((angle_relative/11.25) + 0.5)%32
2015-06-29 17:55:56 +00:00
for j,stack in ipairs(player:get_inventory():get_list("main")) do
if minetest.get_item_group(stack:get_name(), "compass") ~= 0 and
minetest.get_item_group(stack:get_name(), "compass")-1 ~= compass_image then
local count = stack:get_count()
player:get_inventory():set_stack("main", j, ItemStack("mcl_compass:"..compass_image.." "..count))
2015-06-29 17:55:56 +00:00
end
end
end
end
end)
2017-01-09 01:03:09 +00:00
local images = {}
for frame=0,31 do
2017-01-09 02:05:03 +00:00
table.insert(images, "mcl_compass_compass.png^[verticalframe:32:"..frame)
2017-01-09 01:03:09 +00:00
end
2015-06-29 17:55:56 +00:00
2017-01-09 02:26:00 +00:00
local stereotype_frame = 18
2015-06-29 17:55:56 +00:00
for i,img in ipairs(images) do
local inv = 1
2017-01-09 02:26:00 +00:00
if i == stereotype_frame then
2015-06-29 17:55:56 +00:00
inv = 0
end
local doc, longdesc, usagehelp
doc = i == stereotype_frame
if doc then
longdesc = "Compasses are tools which point to the world origin (X=0, Z=0) or the spawn point in the Overworld."
end
minetest.register_craftitem("mcl_compass:"..(i-1), {
2015-06-29 17:55:56 +00:00
description = "Compass",
_doc_items_create_entry = doc,
_doc_items_longdesc = longdesc,
_doc_items_usagehelp = usagehelp,
2015-06-29 17:55:56 +00:00
inventory_image = img,
wield_image = img,
stack_max = 64,
groups = {not_in_creative_inventory=inv, compass=i, tool=1}
2015-06-29 17:55:56 +00:00
})
end
minetest.register_craft({
2017-01-09 02:26:00 +00:00
output = 'mcl_compass:'..stereotype_frame,
2015-06-29 17:55:56 +00:00
recipe = {
2017-02-11 20:14:40 +00:00
{'', 'mcl_core:iron_ingot', ''},
{'mcl_core:iron_ingot', 'mesecons:redstone', 'mcl_core:iron_ingot'},
{'', 'mcl_core:iron_ingot', ''}
2015-06-29 17:55:56 +00:00
}
2017-01-04 04:04:13 +00:00
})
2017-01-09 02:26:00 +00:00
minetest.register_alias("mcl_compass:compass", "mcl_compass:"..stereotype_frame)
2017-02-20 20:14:10 +00:00
-- Export stereotype item for other mods to use
mcl_compass.stereotype = "mcl_compass:"..tostring(stereotype_frame)