Advertisement
Guest User

Untitled

a guest
May 17th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. core.register_entity(":__builtin:item", {
  2. initial_properties = {
  3. hp_max = 1,
  4. physical = true,
  5. collide_with_objects = false,
  6. collisionbox = {-0.17,-0.17,-0.17, 0.17,0.17,0.17},
  7. visual = "sprite",
  8. visual_size = {x=0.5, y=0.5},
  9. textures = {""},
  10. spritediv = {x=1, y=1},
  11. initial_sprite_basepos = {x=0, y=0},
  12. is_visible = false,
  13. },
  14.  
  15. itemstring = '',
  16. physical_state = true,
  17.  
  18. set_item = function(self, itemstring)
  19. self.itemstring = itemstring
  20. local stack = ItemStack(itemstring)
  21. local itemtable = stack:to_table()
  22. local itemname = nil
  23. if itemtable then
  24. itemname = stack:to_table().name
  25. end
  26. local item_texture = nil
  27. local item_type = ""
  28. if core.registered_items[itemname] then
  29. item_texture = core.registered_items[itemname].inventory_image
  30. item_type = core.registered_items[itemname].type
  31. end
  32. prop = {
  33. is_visible = true,
  34. visual = "sprite",
  35. textures = {"unknown_item.png"}
  36. }
  37. if item_texture and item_texture ~= "" then
  38. prop.visual = "sprite"
  39. prop.textures = {item_texture}
  40. prop.visual_size = {x=0.50, y=0.50}
  41. else
  42. prop.visual = "wielditem"
  43. prop.textures = {itemname}
  44. prop.visual_size = {x=0.20, y=0.20}
  45. prop.automatic_rotate = math.pi * 0.25
  46. end
  47. self.object:set_properties(prop)
  48. end,
  49.  
  50. get_staticdata = function(self)
  51. --return self.itemstring
  52. return core.serialize({
  53. itemstring = self.itemstring,
  54. always_collect = self.always_collect,
  55. })
  56. end,
  57.  
  58. on_activate = function(self, staticdata)
  59. if string.sub(staticdata, 1, string.len("return")) == "return" then
  60. local data = core.deserialize(staticdata)
  61. if data and type(data) == "table" then
  62. self.itemstring = data.itemstring
  63. self.always_collect = data.always_collect
  64. end
  65. else
  66. self.itemstring = staticdata
  67. end
  68. self.object:set_armor_groups({immortal=1})
  69. self.object:setvelocity({x=0, y=2, z=0})
  70. self.object:setacceleration({x=0, y=-10, z=0})
  71. self:set_item(self.itemstring)
  72. end,
  73.  
  74. on_step = function(self, dtime)
  75. local p = self.object:getpos()
  76. p.y = p.y - 0.3
  77. local nn = core.get_node(p).name
  78. -- If node is not registered or node is walkably solid and resting on nodebox
  79. local v = self.object:getvelocity()
  80. if not core.registered_nodes[nn] or core.registered_nodes[nn].walkable and v.y == 0 then
  81. if self.physical_state then
  82. local own_stack = ItemStack(self.object:get_luaentity().itemstring)
  83. for _,object in ipairs(core.env:get_objects_inside_radius(p, 1)) do
  84. local obj = object:get_luaentity()
  85. if obj and obj.name == "__builtin:item" and obj.physical_state == false then
  86. local stack = ItemStack(obj.itemstring)
  87. if own_stack:get_name() == stack:get_name() and stack:get_free_space() > 0 then
  88. local pos=object:getpos()
  89. pos.y = pos.y + 0.6
  90. object:moveto(pos, true)
  91. local new_count = stack:get_count() + own_stack:get_count()
  92. if new_count <= stack:get_stack_max() then
  93. obj.itemstring = stack:get_name().." "..new_count
  94. self.itemstring = ''
  95. self.object:remove()
  96. return
  97. else
  98. obj.itemstring = stack:get_name().." "..stack:get_stack_max()
  99. self.itemstring = stack:get_name().." "..new_count - stack:get_stack_max()
  100. end
  101. end
  102. end
  103. end
  104. self.object:setvelocity({x=0,y=0,z=0})
  105. self.object:setacceleration({x=0, y=0, z=0})
  106. self.physical_state = false
  107. self.object:set_properties({
  108. physical = false
  109. })
  110. end
  111. else
  112. if not self.physical_state then
  113. self.object:setvelocity({x=0,y=0,z=0})
  114. self.object:setacceleration({x=0, y=-10, z=0})
  115. self.physical_state = true
  116. self.object:set_properties({
  117. physical = true
  118. })
  119. end
  120. end
  121. end,
  122.  
  123. on_punch = function(self, hitter)
  124. if self.itemstring ~= '' then
  125. local left = hitter:get_inventory():add_item("main", self.itemstring)
  126. if not left:is_empty() then
  127. self.itemstring = left:to_string()
  128. return
  129. end
  130. end
  131. self.itemstring = ''
  132. self.object:remove()
  133. end,
  134. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement