init.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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:getvelocity();
  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:getpos();
  34. pos.y = pos.y - 10;
  35. minetest.sound_play("rolling_test", {pos=pos, gain=1.0, max_hear_distance=20});
  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:setacceleration({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:setacceleration( self.bomb_punched );
  49. self.bomb_punched = nil;
  50. elseif self.object:getacceleration() ~= {x=0, y=-10, z=0} then
  51. self.object:setacceleration({x=0, y=-10, z=0});
  52. end
  53. end,
  54. on_activate = function(self, staticdata, dtime_s)
  55. self.object:setvelocity({x=0, y=0, z=0});
  56. self.object:setacceleration({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:getpos();
  65. local def = {
  66. radius = 2,
  67. damage_radius = 10,
  68. ignore_protection = false,
  69. disable_drops = false,
  70. ignore_on_blast = false,
  71. }
  72. tnt.boom(pos, def)
  73. self.object:remove();
  74. -- Disable lag's griefer-friendly code.
  75. --[[
  76. pos = vector_round(pos);
  77. minetest.sound_play("tnt_explode", {pos=pos, gain=1.5, max_hear_distance=30});
  78. entity_physics(pos, 4);
  79. add_effects(pos, 2);
  80. --destroys only stone and cobble nodes. Check protection only once.
  81. if not minetest.test_protection(pos, "") then
  82. local stonenodes = minetest.find_nodes_in_area(vector.subtract(pos, 2), vector.add(pos, 2), {"default:stone"});
  83. for _, p in ipairs(stonenodes) do
  84. if math_random(1, 100) > 10 then
  85. minetest.remove_node(p);
  86. end
  87. end
  88. end
  89. self.object:remove();
  90. --]]
  91. end
  92. });
  93. minetest.register_craftitem("cobble_bomb:cobblebomb", {
  94. 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.",
  95. inventory_image = "cobble_bomb.png",
  96. on_use = function(itemstack, user, pointed_thing)
  97. local pos = user:getpos();
  98. local dir = user:get_look_dir();
  99. --if minetest.get_node( vector.add( vector.add(pos, dir), {x=0, y=1, z=0}) ).name == "air" then
  100. -- pos = vector.add( vector.add(pos, dir), {x=0, y=1, z=0});
  101. --else
  102. -- return;
  103. --end
  104. pos = vector.add( vector.add(pos, dir), {x=0, y=1, z=0})
  105. minetest.sound_play("rolling_test", {pos=pos, gain=1.5, max_hear_distance=20});
  106. local tmp_bomb = minetest.add_entity(pos, "cobble_bomb:cobblebomb");
  107. tmp_bomb:setvelocity( vector.add(vector.multiply(dir, 5), {x=0,y=2.5,z=0}) );
  108. itemstack:take_item();
  109. return itemstack;
  110. end,
  111. })
  112. minetest.register_craft({
  113. output = "cobble_bomb:cobblebomb 2",
  114. recipe = {
  115. {"default:clay_lump", "default:cobble", "default:clay_lump"},
  116. {"default:cobble", "tnt:gunpowder", "default:cobble"},
  117. {"default:clay_lump", "default:cobble", "default:clay_lump"},
  118. }
  119. })
  120. --if minetest.setting_get("log_mods") then
  121. -- minetest.log("action", "cobblebomb loaded");
  122. --end