RealBadAngel
2013-02-03 fd26a83fcf953fd955dca3967205304c30e77b60
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
minetest.register_craftitem("technic:injector", {
    description = "Injector",
    stack_max = 99,
})
 
minetest.register_node("technic:injector", {
    description = "Injector",
    tiles = {"technic_iron_chest_top.png", "technic_iron_chest_top.png", "technic_iron_chest_side.png",
        "technic_iron_chest_side.png", "technic_iron_chest_side.png", "technic_iron_chest_front.png"},
    paramtype2 = "facedir",
    groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
    legacy_facedir_simple = true,
    sounds = default.node_sound_wood_defaults(),
    on_construct = function(pos)
        local meta = minetest.env:get_meta(pos)
        meta:set_string("formspec",
                "invsize[9,9;]"..
                "list[current_name;main;0,2;8,2;]"..
                "list[current_player;main;0,5;8,4;]")
        meta:set_string("infotext", "Injector")
        local inv = meta:get_inventory()
        inv:set_size("main", 8*4)
    end,
    can_dig = function(pos,player)
        local meta = minetest.env:get_meta(pos);
        local inv = meta:get_inventory()
        return inv:is_empty("main")
    end,
    on_punch = function (pos, node, puncher)
    local meta = minetest.env:get_meta(pos);
    local inv = meta:get_inventory()
    for _,stack in ipairs(inv:get_list("main")) do
        if stack:get_name() ~="" then 
            inv:remove_item("main",stack)
            item1=tube_item({x=pos.x+.5,y=pos.y,z=pos.z},stack)
            return
            end
    end
end,
})
 
 
function tube_item(pos, item)
    -- Take item in any format
    local stack = ItemStack(item)
    local obj = minetest.env:add_entity(pos, "technic:tubed_item")
    obj:get_luaentity():set_item(stack:to_string())
    obj:get_luaentity().start_pos = {x=pos.x,y=pos.y,z=pos.z}
    obj:setacceleration({x=0, y=0, z=0})
    pos.x=pos.x+1
    local meta = minetest.env:get_meta(pos)
    if meta:get_int("tubelike")==1 then obj:setvelocity({x=1, y=0, z=0}) return obj end
    pos.x=pos.x-2
    meta = minetest.env:get_meta(pos)
    if meta:get_int("tubelike")==1 then obj:setvelocity({x=-1, y=0, z=0}) return obj end
    pos.x=pos.x+1
    pos.z=pos.z+1
    meta = minetest.env:get_meta(pos)
    if meta:get_int("tubelike")==1 then obj:setvelocity({x=0, y=0, z=1}) return obj end
    pos.z=pos.z-2
    meta = minetest.env:get_meta(pos)
    if meta:get_int("tubelike")==1 then obj:setvelocity({x=0, y=0, z=-1}) return obj end
    pos.z=pos.z+1
    pos.y=pos.y+1
    meta = minetest.env:get_meta(pos)
    if meta:get_int("tubelike")==1 then obj:setvelocity({x=0, y=1, z=0}) return obj end
    pos.y=pos.y-2
    meta = minetest.env:get_meta(pos)
    if meta:get_int("tubelike")==1 then obj:setvelocity({x=0, y=-2, z=0}) return obj end
    pos.y=pos.y+1
    return obj
end
 
minetest.register_entity("technic:tubed_item", {
    initial_properties = {
        hp_max = 1,
        physical = false,
        collisionbox = {0,0,0,0,0,0},
        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,
        start_pos={}
    },
    
    itemstring = '',
    physical_state = false,
 
    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.3, y=0.3}
        else
            prop.visual = "wielditem"
            prop.textures = {itemname}
            prop.visual_size = {x=0.15, y=0.15}
        end
        self.object:set_properties(prop)
    end,
 
    get_staticdata = function(self)
            
            return    minetest.serialize({
                itemstring=self.itemstring,
                velocity=self.object:getvelocity(),
                start_pos=self.start_pos
                })
    end,
 
    on_activate = function(self, staticdata)
        if  staticdata=="" or staticdata==nil then return end
        local item = minetest.deserialize(staticdata)
        local stack = ItemStack(item.itemstring)
        local itemtable = stack:to_table()
        local itemname = nil
        if itemtable then
            itemname = stack:to_table().name
        end
        
        if itemname then 
        self.start_pos=item.start_pos
        self.object:setvelocity(item.velocity)
        self.object:setacceleration({x=0, y=0, z=0})
        self.object:setpos(item.start_pos)
        end
        self:set_item(item.itemstring)
    end,
 
    on_step = function(self, dtime)
    if self.start_pos then
    local pos = self.object:getpos()
    local node = minetest.env:get_node(pos)
    local meta = minetest.env:get_meta(pos)
    tubelike=meta:get_int("tubelike")
    local velocity=self.object:getvelocity()
    
    if not velocity then return end
 
    if math.abs(velocity.x)==1 then
        local next_node=math.abs(pos.x-self.start_pos.x)
        if next_node >= 1 then 
            self.start_pos.x=self.start_pos.x+velocity.x
            if check_pos_vector (self.start_pos, velocity)==0 then 
            check_next_step (self.start_pos, velocity) 
            self.object:setpos(self.start_pos)
            self.object:setvelocity(velocity)
            return
            end
            end
        end
 
    if math.abs(velocity.y)==1 then
        local next_node=math.abs(pos.y-self.start_pos.y)
        if next_node >= 1 then 
            self.start_pos.y=self.start_pos.y+velocity.y
            if check_pos_vector (self.start_pos, velocity)==0 then
            check_next_step (self.start_pos, velocity) 
            self.object:setpos(self.start_pos)
            self.object:setvelocity(velocity)
            return
            end
            end
        end
    
    if math.abs(velocity.z)==1 then
        local next_node=math.abs(pos.z-self.start_pos.z)
        if next_node >= 1 then 
            self.start_pos.z=self.start_pos.z+velocity.z
            if check_pos_vector (self.start_pos, velocity)==0 then
            check_next_step (self.start_pos, velocity) 
            self.object:setpos(self.start_pos)
            self.object:setvelocity(velocity)
            return
            end
            end
        end
    end
end
})
 
 
function check_next_step (pos,velocity)
local meta
local tubelike
 
if velocity.x==0 then
meta = minetest.env:get_meta({x=pos.x-1,y=pos.y,z=pos.z})
tubelike=meta:get_int("tubelike")
if tubelike==1 then velocity.x=-1 velocity.y=0 velocity.z=0 return end
meta = minetest.env:get_meta({x=pos.x+1,y=pos.y,z=pos.z})
tubelike=meta:get_int("tubelike")
if tubelike==1 then velocity.x=1 velocity.y=0 velocity.z=0 return end
end
 
if velocity.z==0 then
meta = minetest.env:get_meta({x=pos.x,y=pos.y,z=pos.z+1})
tubelike=meta:get_int("tubelike")
if tubelike==1 then velocity.x=0 velocity.y=0 velocity.z=1 return end
meta = minetest.env:get_meta({x=pos.x,y=pos.y,z=pos.z-1})
tubelike=meta:get_int("tubelike")
if tubelike==1 then velocity.x=0 velocity.y=0 velocity.z=-1 return end
end
 
if velocity.y==0 then
meta = minetest.env:get_meta({x=pos.x,y=pos.y+1,z=pos.z})
tubelike=meta:get_int("tubelike")
if tubelike==1 then velocity.x=0 velocity.y=1 velocity.z=0 return end
meta = minetest.env:get_meta({x=pos.x,y=pos.y-1,z=pos.z})
tubelike=meta:get_int("tubelike")
if tubelike==1 then velocity.x=0 velocity.y=-1 velocity.z=0 return end
end
 
--velocity.x=0
--velocity.y=0
--velocity.z=0
end
 
function check_pos_vector (pos,velocity)
added={}
added.x=pos.x+velocity.x
added.y=pos.y+velocity.y
added.z=pos.z+velocity.z
local meta=minetest.env:get_meta(added) 
--print(dump(added).." : "..tubelike)
if meta:get_int("tubelike")==1 then return 1 end
return 0
end