metalstache
2013-08-04 d5c55415014e53ba0eaeb48729ec7a2b27ff8a3b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
-- Minetest: builtin/item_entity.lua
 
function minetest.spawn_item(pos, item)
    -- Take item in any format
    local stack = ItemStack(item)
    local obj = minetest.env:add_entity(pos, "__builtin:item")
    obj:get_luaentity():set_item(stack:to_string())
    return obj
end
 
minetest.register_entity(":__builtin:item", {
    initial_properties = {
        hp_max = 1,
        physical = true,
        collisionbox = {-0.17,-0.17,-0.17, 0.17,0.17,0.17},
        visual = "sprite",
        visual_size = {x=0.5, y=0.5},
        textures = {""},
        spritediv = {x=1, y=1},
        initial_sprite_basepos = {x=0, y=0},
        is_visible = false,
    },
    
    itemstring = '',
    physical_state = true,
    timer = 0,
    
    set_item = function(self, itemstring)
        self.itemstring = itemstring
        local stack = ItemStack(itemstring)
        local itemtable = stack:to_table()
        local itemname = nil
        if itemtable then
            itemname = stack:to_table().name
        end
        local item_texture = nil
        local item_type = ""
        if minetest.registered_items[itemname] then
            item_texture = minetest.registered_items[itemname].inventory_image
            item_type = minetest.registered_items[itemname].type
        end
        prop = {
            is_visible = true,
            visual = "sprite",
            textures = {"unknown_item.png"}
        }
        if item_texture and item_texture ~= "" then
            prop.visual = "sprite"
            prop.textures = {item_texture}
            prop.visual_size = {x=0.50, y=0.50}
        else
            prop.visual = "wielditem"
            prop.textures = {itemname}
            prop.visual_size = {x=0.20, y=0.20}
            prop.automatic_rotate = math.pi * 0.25
        end
        self.object:set_properties(prop)
    end,
 
    get_staticdata = function(self)
        --return self.itemstring
        return minetest.serialize({
            itemstring = self.itemstring,
            always_collect = self.always_collect,
        })
    end,
 
    on_activate = function(self, staticdata)
        if string.sub(staticdata, 1, string.len("return")) == "return" then
            local data = minetest.deserialize(staticdata)
            if data and type(data) == "table" then
                self.itemstring = data.itemstring
                self.always_collect = data.always_collect
            end
        else
            self.itemstring = staticdata
        end
        self.object:set_armor_groups({immortal=1})
        self.object:setvelocity({x=0, y=2, z=0})
        self.object:setacceleration({x=0, y=-10, z=0})
        self:set_item(self.itemstring)
    end,
 
    on_step = function(self, dtime)
        self.timer = self.timer + dtime
        if (self.timer > 300) then
            self.object:remove()
        end
        local p = self.object:getpos()
        p.y = p.y - 0.3
        local nn = minetest.env:get_node(p).name
        -- If node is not registered or node is walkably solid and resting on nodebox
        local v = self.object:getvelocity()
        if not minetest.registered_nodes[nn] or minetest.registered_nodes[nn].walkable and v.y == 0 then
            if self.physical_state then
                self.object:setvelocity({x=0,y=0,z=0})
                self.object:setacceleration({x=0, y=0, z=0})
                self.physical_state = false
                self.object:set_properties({
                    physical = false
                })
            end
        else
            if not self.physical_state then
                self.object:setvelocity({x=0,y=0,z=0})
                self.object:setacceleration({x=0, y=-10, z=0})
                self.physical_state = true
                self.object:set_properties({
                    physical = true
                })
            end
        end
    end,
 
    on_punch = function(self, hitter)
        if self.itemstring ~= '' then
            local left = hitter:get_inventory():add_item("main", self.itemstring)
            if not left:is_empty() then
                self.itemstring = left:to_string()
                return
            end
        end
        self.object:remove()
    end,
})
print("ITEM ENTITY LOADED")