Rename painting item
[MineClone/MineClone2.git] / mods / ENTITIES / mcl_paintings / init.lua
blobddd5677470d07b9e0b11d772237fc09ee97061c4
1 -- TODO: Move all textures to mcl_paintings when finished
3 -- Intllib
4 local MP = minetest.get_modpath(minetest.get_current_modname())
5 local S, NS = dofile(MP .. "/intllib.lua")
7 minetest.register_craftitem("mcl_paintings:painting", {
8 description = S("Painting"),
9 _doc_items_longdesc = S("Paintings are decorations which can be placed on walls. THIS ITEM IS INCOMPLETE."),
10 wield_image = "gemalde_node.png",
11 inventory_image = "gemalde_node.png",
12 on_place = function(itemstack, placer, pointed_thing)
13 if pointed_thing.type ~= "node" then
14 return
15 end
17 local under = pointed_thing.under
18 local above = pointed_thing.above
20 -- Use pointed node's on_rightclick function first, if present
21 local node_under = minetest.get_node(under)
22 if placer and not placer:get_player_control().sneak then
23 if minetest.registered_nodes[node_under.name] and minetest.registered_nodes[node_under.name].on_rightclick then
24 return minetest.registered_nodes[node_under.name].on_rightclick(under, node_under, placer, itemstack) or itemstack
25 end
26 end
28 -- Can only be placed on side
29 if under.y ~= above.y then
30 return itemstack
31 end
32 -- Can only be placed on solid nodes
33 if minetest.get_item_group(node_under.name, "solid") == 0 then
34 return itemstack
35 end
37 -- Spawn painting and rotate
38 local painting = minetest.add_entity(above, "mcl_paintings:painting")
39 local yaw = minetest.dir_to_yaw(vector.direction(under, above))
40 painting:set_yaw(yaw)
42 if not minetest.settings:get_bool("creative_mode") then
43 itemstack:take_item()
44 end
45 return itemstack
46 end,
49 -- List of painting IDs, indexed by size.
50 -- Outer index: Width in node lengths
51 -- Inner index: Height in node lengths
52 local paintings = {
53 [1] = {
54 [1] = { 1, 2, 3, 4, 5, 6, 7 }, -- 1×1
55 [2] = { 8, 9, 10, 11, 12 }, -- 1×2
57 [2] = {
58 [1] = { 13, 14}, -- 2×1
59 [2] = { 15, 16, 17, 18, 19, 20 }, -- 2×2
61 [3] = {
62 [4] = { 25, 26 }, -- 3×4
64 [4] = {
65 [2] = { 21 }, -- 4×2
66 [4] = { 22, 23, 24 }, -- 4×4
70 -- Returns a random painting ID for the given size.
71 -- x: Width in node lenghts
72 -- y: Height in node lengths
73 local function select_painting(x, y)
74 if paintings[x] then
75 local pool = paintings[x][y]
76 if paintings[x][y] then
77 local p = math.random(1, #pool)
78 return p
79 end
80 end
81 return nil
82 end
84 -- Returns the texture table for the given painting ID
85 local get_textures = function(painting_id)
86 return {
87 "gemalde_bg.png",
88 "gemalde_bg.png",
89 "gemalde_bg.png",
90 "gemalde_bg.png",
91 "gemalde_"..tostring(painting_id)..".png",
92 "gemalde_bg.png"
94 end
96 -- Painting entitty.
97 -- Can be killed.
98 -- Breaks and drops as item if punched.
99 --
100 minetest.register_entity("mcl_paintings:painting", {
101 physical = false,
102 collide_with_objects = true,
103 hp_max = 1,
104 -- TODO: Fix visual
105 visual = "cube",
106 visual_size = { x=1, y=1 },
107 textures = get_textures(1),
109 _painting = nil, -- Holds the current painting ID. Initially nil for random painting
111 get_staticdata = function(self)
112 local out = { _painting = self._painting }
113 return minetest.serialize(out)
114 end,
115 on_activate = function(self, staticdata)
116 if staticdata and staticdata ~= "" then
117 local inp = minetest.deserialize(staticdata)
118 self._painting = inp._painting
120 -- Initial spawn. Select random painting
121 if not self._painting then
122 self._painting = select_painting(1, 1)
124 self.object:set_properties({textures = get_textures(self._painting)})
125 end,
126 on_punch = function(self, puncher)
127 if not puncher or not puncher:is_player() or self._removed then
128 return
130 -- Drop painting as item on ground
131 if not minetest.settings:get_bool("creative_mode") then
132 minetest.add_item(self.object:getpos(), "mcl_paintings:painting")
134 self._removed = true
135 self.object:remove()
139 --[[
140 -- TODO: Add crafting when this mod works better
141 if minetest.get_modpath("mcl_core") then
142 minetest.register_craft({
143 output = "mcl_paintings:painting",
144 recipe = {
145 {"mcl_core:stick", "mcl_core:stick", "mcl_core:stick"},
146 {"mcl_core:stick", "group:wool", "mcl_core:stick"},
147 {"mcl_core:stick", "mcl_core:stick", "mcl_core:stick"},