bell.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. -- Christmas Bell that rings every hour.
  2. -- Most of this code is from the "bell" mod.
  3. christmas_holiday_pack.bell_ring_interval = 3600; --60*60; -- ring each hour
  4. christmas_holiday_pack.bell_save_file = minetest.get_worldpath().."/christmas_bell_positions.data";
  5. local bell_positions = {};
  6. christmas_holiday_pack.save_bell_positions = function( player )
  7. str = minetest.serialize( ({ bell_data = bell_positions}) );
  8. local file, err = io.open( christmas_holiday_pack.bell_save_file, "wb");
  9. if (err ~= nil) then
  10. return
  11. end
  12. file:write( str );
  13. file:flush();
  14. file:close();
  15. end
  16. christmas_holiday_pack.restore_bell_data = function()
  17. local bell_position_table;
  18. local file, err = io.open(christmas_holiday_pack.bell_save_file, "rb");
  19. if (err ~= nil) then
  20. print("Error: Could not open bell data savefile (ignore this message on first start)");
  21. return
  22. end
  23. local str = file:read();
  24. file:close();
  25. local bell_positions_table = minetest.deserialize( str );
  26. if( bell_positions_table and bell_positions_table.bell_data ) then
  27. bell_positions = bell_positions_table.bell_data;
  28. end
  29. end
  30. -- actually ring the bell
  31. christmas_holiday_pack.ring_bell_once = function()
  32. for i,v in ipairs( bell_positions ) do
  33. minetest.sound_play( "bell_small",
  34. { pos = v, gain = 1.5, max_hear_distance = 300,});
  35. end
  36. end
  37. christmas_holiday_pack.ring_bell = function()
  38. -- figure out if this is the right time to ring
  39. local sekunde = tonumber( os.date( "%S"));
  40. local minute = tonumber( os.date( "%M"));
  41. local stunde = tonumber( os.date( "%I")); -- in 12h-format (a bell that rings 24x at once would not survive long...)
  42. local delay = christmas_holiday_pack.bell_ring_interval;
  43. delay = christmas_holiday_pack.bell_ring_interval - sekunde - (minute*60);
  44. -- make sure the bell rings the next hour
  45. minetest.after( delay, christmas_holiday_pack.ring_bell );
  46. -- if no bells are around then don't ring
  47. if( bell_positions == nil or #bell_positions < 1 ) then
  48. return;
  49. end
  50. -- ring the bell for each hour once
  51. for i=1,stunde do
  52. minetest.after( (i-1)*5, christmas_holiday_pack.ring_bell_once );
  53. end
  54. end
  55. -- first call (after the server has been started)
  56. minetest.after( 10, christmas_holiday_pack.ring_bell );
  57. -- read data about bell positions
  58. christmas_holiday_pack.restore_bell_data();
  59. minetest.register_node("christmas_holiday_pack:christmas_bell", {
  60. description = "Bell",
  61. node_placement_prediction = "",
  62. tiles = {"christmas_holiday_pack_christmas_bell.png"},
  63. paramtype = "light",
  64. is_ground_content = true,
  65. inventory_image = 'christmas_holiday_pack_christmas_bell.png',
  66. wield_image = 'christmas_holiday_pack_christmas_bell.png',
  67. stack_max = 1,
  68. drawtype = "plantlike",
  69. on_punch = function (pos,node,puncher)
  70. minetest.sound_play( "bell_small",
  71. { pos = pos, gain = 1.5, max_hear_distance = 300,});
  72. end,
  73. after_place_node = function(pos, placer)
  74. -- remember that there is a bell at that position
  75. table.insert( bell_positions, pos );
  76. christmas_holiday_pack.save_bell_positions( placer );
  77. end,
  78. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  79. local found = 0;
  80. -- actually remove the bell from the list
  81. for i,v in ipairs( bell_positions ) do
  82. if( v ~= nil and v.x == pos.x and v.y == pos.y and v.z == pos.z ) then
  83. found = i;
  84. end
  85. end
  86. -- actually remove the bell
  87. if( found > 0 ) then
  88. table.remove( bell_positions, found );
  89. christmas_holiday_pack.save_bell_positions( digger );
  90. end
  91. end,
  92. groups = {cracky=2},
  93. })