init.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. -- "Cobble bomb" mod. Let's get started!
  2. -- Localize for performance.
  3. local vector_round = vector.round
  4. local math_random = math.random
  5. minetest.register_entity("cobble_bomb:cobblebomb", {
  6. full_name = "Cobble Bomb",
  7. hp_max = 40,
  8. physical = true,
  9. collisionbox = {-0.3, -0.3, -0.3, 0.3, 0.3, 0.3},
  10. visual = "mesh",
  11. visual_size = {x=3, y=3},
  12. mesh = "sphere.x",
  13. textures = {"cobble_texture.png"},
  14. bomb_inertion = 10,
  15. bomb_timer = nil,
  16. bomb_punched =nil,
  17. on_step = function(self, dtime)
  18. -- Explode when timer runs out.
  19. if self.bomb_timer == nil then
  20. self.bomb_timer = dtime;
  21. elseif self.bomb_timer > 4 then
  22. self:bomb_explode();
  23. return;
  24. else
  25. self.bomb_timer = self.bomb_timer + dtime;
  26. end
  27. --falling give more inertion. may explode if fall to hard.
  28. local vel = self.object:get_velocity();
  29. if self.bomb_inertion < 20 and vel.y < -0.1 and vel.y > -3 then
  30. self.bomb_inertion = self.bomb_inertion + 1;
  31. elseif vel.y <= -10 and self.bomb_inertion > 3 then
  32. --warn players bellow
  33. local pos = self.object:get_pos();
  34. pos.y = pos.y - 10;
  35. minetest.sound_play("rolling_test", {pos=pos, gain=1.0, max_hear_distance=20}, true);
  36. self.bomb_inertion = 0;
  37. end
  38. --bounce around, inertion slowly fades
  39. if vector.length(vel)<0.1 then
  40. if self.bomb_inertion < 1 then
  41. self:bomb_explode();
  42. return;
  43. else
  44. --self.object:set_acceleration({x=math_random(-1, 1)*self.bomb_inertion*10, y=-10, z=math_random(-1, 1)*self.bomb_inertion*10});
  45. --self.bomb_inertion = self.bomb_inertion - 1;
  46. end
  47. elseif self.bomb_punched ~= nil then
  48. self.object:set_acceleration( self.bomb_punched );
  49. self.bomb_punched = nil;
  50. elseif self.object:getacceleration() ~= {x=0, y=-10, z=0} then
  51. self.object:set_acceleration({x=0, y=-10, z=0});
  52. end
  53. end,
  54. on_activate = function(self, staticdata, dtime_s)
  55. self.object:set_velocity({x=0, y=0, z=0});
  56. self.object:set_acceleration({x=0, y=-5, z=0});
  57. end,
  58. on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
  59. if time_from_last_punch > 1 then
  60. self.bomb_punched = {x=dir.x*100, y=-10, z=dir.z*100};
  61. end
  62. end,
  63. bomb_explode = function(self)
  64. local pos = self.object:get_pos();
  65. local def = {
  66. radius = 2,
  67. damage_radius = 10,
  68. ignore_protection = false,
  69. disable_drops = false,
  70. ignore_on_blast = false,
  71. name = self.owner,
  72. from_arrow = true,
  73. }
  74. tnt.boom(pos, def)
  75. self.object:remove();
  76. -- Disable lag's griefer-friendly code.
  77. --[[
  78. pos = vector_round(pos);
  79. minetest.sound_play("tnt_explode", {pos=pos, gain=1.5, max_hear_distance=30}, true);
  80. entity_physics(pos, 4);
  81. add_effects(pos, 2);
  82. --destroys only stone and cobble nodes. Check protection only once.
  83. if not minetest.test_protection(pos, "") then
  84. local stonenodes = minetest.find_nodes_in_area(vector.subtract(pos, 2), vector.add(pos, 2), {"default:stone"});
  85. for _, p in ipairs(stonenodes) do
  86. if math_random(1, 100) > 10 then
  87. minetest.remove_node(p);
  88. end
  89. end
  90. end
  91. self.object:remove();
  92. --]]
  93. end
  94. })
  95. minetest.register_craftitem("cobble_bomb:cobblebomb", {
  96. description = "Cobble Molotov\n\nThis device is dedicated to Lag, the creator of the original Just Test.\nExcellent for anyone who wants to explode mobs and others.",
  97. inventory_image = "cobble_bomb.png",
  98. on_use = function(itemstack, user, pointed_thing)
  99. if not user or not user:is_player() then
  100. return
  101. end
  102. local pname = user:get_player_name()
  103. local pos = user:get_pos();
  104. local dir = user:get_look_dir();
  105. --if minetest.get_node( vector.add( vector.add(pos, dir), {x=0, y=1, z=0}) ).name == "air" then
  106. -- pos = vector.add( vector.add(pos, dir), {x=0, y=1, z=0});
  107. --else
  108. -- return;
  109. --end
  110. pos = vector.add( vector.add(pos, dir), {x=0, y=1, z=0})
  111. minetest.sound_play("rolling_test", {pos=pos, gain=1.5, max_hear_distance=20}, true);
  112. local tmp_bomb = minetest.add_entity(pos, "cobble_bomb:cobblebomb");
  113. tmp_bomb:set_velocity( vector.add(vector.multiply(dir, 5), {x=0,y=2.5,z=0}) );
  114. local luaent = tmp_bomb:get_luaentity()
  115. if luaent then
  116. luaent.owner = pname
  117. end
  118. itemstack:take_item();
  119. return itemstack;
  120. end,
  121. })
  122. minetest.register_craft({
  123. output = "cobble_bomb:cobblebomb 2",
  124. recipe = {
  125. {"default:clay_lump", "default:cobble", "default:clay_lump"},
  126. {"default:cobble", "tnt:gunpowder", "default:cobble"},
  127. {"default:clay_lump", "default:cobble", "default:clay_lump"},
  128. }
  129. })
  130. --if minetest.settings:get("log_mods") then
  131. -- minetest.log("action", "cobblebomb loaded");
  132. --end