Replace getpos() with get_pos()
[MineClone/MineClone2.git] / mods / ITEMS / mcl_maps / init.lua
blob300e1d35b141892c4f04dd4a4fd790ea6db86b5e
1 -- Turn empty map into filled map by rightclick
2 local make_filled_map = function(itemstack, placer, pointed_thing)
3 local new_map = ItemStack("mcl_maps:filled_map")
4 itemstack:take_item()
5 if itemstack:is_empty() then
6 return new_map
7 else
8 local inv = placer:get_inventory()
9 if inv:room_for_item("main", new_map) then
10 inv:add_item("main", new_map)
11 else
12 minetest.add_item(placer:get_pos(), new_map)
13 end
14 return itemstack
15 end
16 end
18 minetest.register_craftitem("mcl_maps:empty_map", {
19 description = "Empty Map",
20 _doc_items_longdesc = "Empty maps are not useful as maps, but they can be stacked and turned to maps which can be used.",
21 _doc_items_usagehelp = "Rightclick to start using the map (which can't be stacked anymore).",
22 inventory_image = "mcl_maps_map_empty.png",
23 groups = { not_in_creative_inventory = 1 },
24 on_place = make_filled_map,
25 on_secondary_use = make_filled_map,
26 stack_max = 64,
29 -- Enables minimap if carried in hotbar.
30 -- If this item is NOT in the hotbar, the minimap is unavailable
31 -- Note: This is not at all like Minecraft right now. Minetest's minimap is pretty overpowered, it
32 -- has a very greatly zoomed-out version and even a radar mode
33 minetest.register_craftitem("mcl_maps:filled_map", {
34 description = "Map",
35 _doc_items_longdesc = "Maps show your surroundings as you explore the world. They can even show you the world like a radar. MAGIC!\nNote: Maps are subject to change in future versions of MineClone 2.",
36 _doc_items_usagehelp = "Hold the map in any of the hotbar slots. This allows you to access the minimap by pressing the minimap key ([F9] by default).\nIn Creative Mode, you don't need this item; the minimap is always available.",
37 inventory_image = "mcl_maps_map_filled.png^(mcl_maps_map_filled_markings.png^[colorize:#000000)",
38 stack_max = 1,
41 minetest.register_craft({
42 output = "mcl_maps:filled_map",
43 recipe = {
44 { "mcl_core:paper", "mcl_core:paper", "mcl_core:paper" },
45 { "mcl_core:paper", "group:compass", "mcl_core:paper" },
46 { "mcl_core:paper", "mcl_core:paper", "mcl_core:paper" },
50 local function has_item_in_hotbar(player, item)
51 -- Requirement: player carries the tool in the hotbar
52 local inv = player:get_inventory()
53 local hotbar = player:hud_get_hotbar_itemcount()
54 for i=1, hotbar do
55 if inv:get_stack("main", i):get_name() == item then
56 return true
57 end
58 end
59 return false
60 end
62 -- Checks if player is still allowed to display the minimap
63 local function update_minimap(player)
64 if minetest.settings:get_bool("creative_mode") or has_item_in_hotbar(player, "mcl_maps:filled_map") then
65 player:hud_set_flags({minimap = true})
66 else
67 player:hud_set_flags({minimap = false})
68 end
69 end
71 minetest.register_on_joinplayer(function(player)
72 update_minimap(player)
73 end)
75 local updatetimer = 0
76 if not minetest.settings:get_bool("creative_mode") then
77 minetest.register_globalstep(function(dtime)
78 updatetimer = updatetimer + dtime
79 if updatetimer > 0.1 then
80 local players = minetest.get_connected_players()
81 for i=1, #players do
82 update_minimap(players[i])
83 end
84 updatetimer = updatetimer - dtime
85 end
86 end)
87 end