init.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. --"Cobble bomb" mod. Lets get started
  2. --Some code from tnt mod
  3. --[[
  4. local function calc_velocity(pos1, pos2, old_vel, power)
  5. local vel = vector.direction(pos1, pos2)
  6. vel = vector.normalize(vel)
  7. vel = vector.multiply(vel, power)
  8. -- Divide by distance
  9. local dist = vector.distance(pos1, pos2)
  10. dist = math.max(dist, 1)
  11. vel = vector.divide(vel, dist)
  12. -- Add old velocity
  13. vel = vector.add(vel, old_vel)
  14. return vel
  15. end
  16. local function entity_physics(pos, radius)
  17. -- Make the damage radius larger than the destruction radius
  18. radius = radius * 2
  19. local objs = minetest.get_objects_inside_radius(pos, radius)
  20. for _, obj in pairs(objs) do
  21. local obj_pos = obj:getpos()
  22. local obj_vel = obj:getvelocity()
  23. local dist = math.max(1, vector.distance(pos, obj_pos))
  24. if obj_vel ~= nil then
  25. obj:setvelocity(calc_velocity(pos, obj_pos,
  26. obj_vel, radius * 10))
  27. end
  28. local damage = (4 / dist) * radius
  29. obj:set_hp(obj:get_hp() - damage)
  30. end
  31. end
  32. local function add_effects(pos, radius)
  33. minetest.add_particlespawner({
  34. amount = 128,
  35. time = 1,
  36. minpos = vector.subtract(pos, radius / 2),
  37. maxpos = vector.add(pos, radius / 2),
  38. minvel = {x=-20, y=-20, z=-20},
  39. maxvel = {x=20, y=20, z=20},
  40. minacc = vector.new(),
  41. maxacc = vector.new(),
  42. minexptime = 1,
  43. maxexptime = 3,
  44. minsize = 8,
  45. maxsize = 16,
  46. texture = "tnt_smoke.png",
  47. })
  48. end
  49. --]]
  50. --End of Some code from tnt mod
  51. minetest.register_entity("cobble_bomb:cobblebomb", {
  52. full_name = "Cobble Bomb",
  53. hp_max = 40,
  54. physical = true,
  55. collisionbox = {-0.3, -0.3, -0.3, 0.3, 0.3, 0.3},
  56. visual = "mesh",
  57. visual_size = {x=3, y=3},
  58. mesh = "sphere.x",
  59. textures = {"cobble_texture.png"},
  60. bomb_inertion = 10,
  61. bomb_timer = nil,
  62. bomb_punched =nil,
  63. on_step = function(self, dtime)
  64. -- Explode when timer runs out.
  65. if self.bomb_timer == nil then
  66. self.bomb_timer = dtime;
  67. elseif self.bomb_timer > 4 then
  68. self:bomb_explode();
  69. return;
  70. else
  71. self.bomb_timer = self.bomb_timer + dtime;
  72. end
  73. --falling give more inertion. may explode if fall to hard.
  74. local vel = self.object:getvelocity();
  75. if self.bomb_inertion < 20 and vel.y < -0.1 and vel.y > -3 then
  76. self.bomb_inertion = self.bomb_inertion + 1;
  77. elseif vel.y <= -10 and self.bomb_inertion > 3 then
  78. --warn players bellow
  79. local pos = self.object:getpos();
  80. pos.y = pos.y - 10;
  81. minetest.sound_play("rolling_test", {pos=pos, gain=1.0, max_hear_distance=20});
  82. self.bomb_inertion = 0;
  83. end
  84. --bounce around, inertion slowly fades
  85. if vector.length(vel)<0.1 then
  86. if self.bomb_inertion < 1 then
  87. self:bomb_explode();
  88. return;
  89. else
  90. --self.object:setacceleration({x=math.random(-1, 1)*self.bomb_inertion*10, y=-10, z=math.random(-1, 1)*self.bomb_inertion*10});
  91. --self.bomb_inertion = self.bomb_inertion - 1;
  92. end
  93. elseif self.bomb_punched ~= nil then
  94. self.object:setacceleration( self.bomb_punched );
  95. self.bomb_punched = nil;
  96. elseif self.object:getacceleration() ~= {x=0, y=-10, z=0} then
  97. self.object:setacceleration({x=0, y=-10, z=0});
  98. end
  99. end,
  100. on_activate = function(self, staticdata, dtime_s)
  101. self.object:setvelocity({x=0, y=0, z=0});
  102. self.object:setacceleration({x=0, y=-5, z=0});
  103. end,
  104. on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
  105. if time_from_last_punch > 1 then
  106. self.bomb_punched = {x=dir.x*100, y=-10, z=dir.z*100};
  107. end
  108. end,
  109. bomb_explode = function(self)
  110. local pos = self.object:getpos();
  111. local def = {
  112. radius = 2,
  113. damage_radius = 10,
  114. ignore_protection = false,
  115. disable_drops = false,
  116. ignore_on_blast = false,
  117. }
  118. tnt.boom(pos, def)
  119. self.object:remove();
  120. -- Disable lag's griefer-friendly code.
  121. --[[
  122. pos = vector.round(pos);
  123. minetest.sound_play("tnt_explode", {pos=pos, gain=1.5, max_hear_distance=30});
  124. entity_physics(pos, 4);
  125. add_effects(pos, 2);
  126. --destroys only stone and cobble nodes. Check protection only once.
  127. if not minetest.test_protection(pos, "") then
  128. local stonenodes = minetest.find_nodes_in_area(vector.subtract(pos, 2), vector.add(pos, 2), {"default:stone"});
  129. for _, p in ipairs(stonenodes) do
  130. if math.random(1, 100) > 10 then
  131. minetest.remove_node(p);
  132. end
  133. end
  134. end
  135. self.object:remove();
  136. --]]
  137. end
  138. });
  139. minetest.register_craftitem("cobble_bomb:cobblebomb", {
  140. 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.",
  141. inventory_image = "cobble_bomb.png",
  142. on_use = function(itemstack, user, pointed_thing)
  143. local pos = user:getpos();
  144. local dir = user:get_look_dir();
  145. --if minetest.get_node( vector.add( vector.add(pos, dir), {x=0, y=1, z=0}) ).name == "air" then
  146. -- pos = vector.add( vector.add(pos, dir), {x=0, y=1, z=0});
  147. --else
  148. -- return;
  149. --end
  150. pos = vector.add( vector.add(pos, dir), {x=0, y=1, z=0})
  151. minetest.sound_play("rolling_test", {pos=pos, gain=1.5, max_hear_distance=20});
  152. local tmp_bomb = minetest.add_entity(pos, "cobble_bomb:cobblebomb");
  153. tmp_bomb:setvelocity( vector.add(vector.multiply(dir, 5), {x=0,y=2.5,z=0}) );
  154. itemstack:take_item();
  155. return itemstack;
  156. end,
  157. })
  158. minetest.register_craft({
  159. output = "cobble_bomb:cobblebomb 2",
  160. recipe = {
  161. {"default:clay_lump", "default:cobble", "default:clay_lump"},
  162. {"default:cobble", "tnt:gunpowder", "default:cobble"},
  163. {"default:clay_lump", "default:cobble", "default:clay_lump"},
  164. }
  165. })
  166. --if minetest.setting_get("log_mods") then
  167. -- minetest.log("action", "cobblebomb loaded");
  168. --end