init.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. local S
  2. if minetest.get_modpath("intllib") then
  3. S = intllib.Getter()
  4. else
  5. S = function(s,a,...)a={a,...}return s:gsub("@(%d+)",function(n)return a[tonumber(n)]end)end
  6. end
  7. function round_down(pos)
  8. return {x=math.floor(pos.x), y=math.floor(pos.y), z=math.floor(pos.z)}
  9. end
  10. function round_new(vec)
  11. return {x=math.floor(vec.x+0.5), y=math.floor(vec.y+0.3), z=math.floor(vec.z+0.5)}
  12. end
  13. advtrains.register_wagon("construction_train", {
  14. mesh="advtrains_subway_wagon.b3d",
  15. textures = {"advtrains_subway_wagon.png"},
  16. drives_on={default=true},
  17. max_speed=2,
  18. seats = {
  19. {
  20. name="Driver stand",
  21. attach_offset={x=0, y=10, z=0},
  22. view_offset={x=0, y=0, z=0},
  23. group="dstand",
  24. },
  25. {
  26. name="1",
  27. attach_offset={x=-4, y=8, z=8},
  28. view_offset={x=0, y=0, z=0},
  29. group="pass",
  30. },
  31. {
  32. name="2",
  33. attach_offset={x=4, y=8, z=8},
  34. view_offset={x=0, y=0, z=0},
  35. group="pass",
  36. },
  37. {
  38. name="3",
  39. attach_offset={x=-4, y=8, z=-8},
  40. view_offset={x=0, y=0, z=0},
  41. group="pass",
  42. },
  43. {
  44. name="4",
  45. attach_offset={x=4, y=8, z=-8},
  46. view_offset={x=0, y=0, z=0},
  47. group="pass",
  48. },
  49. },
  50. seat_groups = {
  51. dstand={
  52. name = "Driver Stand",
  53. access_to = {"pass"},
  54. require_doors_open=true,
  55. driving_ctrl_access=true,
  56. },
  57. pass={
  58. name = "Passenger area",
  59. access_to = {"dstand"},
  60. require_doors_open=true,
  61. },
  62. },
  63. assign_to_seat_group = {"pass", "dstand"},
  64. doors={
  65. open={
  66. [-1]={frames={x=0, y=20}, time=1},
  67. [1]={frames={x=40, y=60}, time=1},
  68. sound = "advtrains_subway_dopen",
  69. },
  70. close={
  71. [-1]={frames={x=20, y=40}, time=1},
  72. [1]={frames={x=60, y=80}, time=1},
  73. sound = "advtrains_subway_dclose",
  74. }
  75. },
  76. door_entry={-1, 1},
  77. visual_size = {x=1, y=1},
  78. wagon_span=2,
  79. --collisionbox = {-1.0,-0.5,-1.8, 1.0,2.5,1.8},
  80. collisionbox = {-1.0,-0.5,-1.0, 1.0,2.5,1.0},
  81. is_locomotive=true,
  82. drops={"default:steelblock 4"},
  83. horn_sound = "advtrains_subway_horn",
  84. custom_on_step = function (self, dtime )
  85. if self:train().velocity == 0 then
  86. return
  87. end
  88. local rpos = round_new(self.object:getpos())
  89. rpos.y = rpos.y -1
  90. -- Find train tracks, if we find train tracks, we will only replace blocking nodes
  91. local tracks_below = false
  92. for y =-1,0 do
  93. for x = -2,2 do
  94. for z = -2,2 do
  95. local ps = {x=rpos.x+x, y=rpos.y+y, z=rpos.z+z}
  96. if minetest.get_item_group(minetest.get_node(ps).name, "advtrains_track") > 0 then
  97. tracks_below = true
  98. break
  99. end
  100. end
  101. end
  102. end
  103. for x = -1,1 do
  104. for z = -1,1 do
  105. local ps = {x=rpos.x+x, y=rpos.y, z=rpos.z+z}
  106. local name = minetest.get_node(ps).name
  107. if (not tracks_below) or (minetest.get_item_group(name, "not_blocking_trains") == 0 and minetest.registered_nodes[name].walkable ) then
  108. minetest.set_node(ps, {name = "default:gravel"})
  109. ps.y = ps.y-1
  110. if not minetest.registered_nodes[minetest.get_node(ps).name].walkable then
  111. minetest.set_node(ps, {name = "default:cobble"})
  112. end
  113. end
  114. end
  115. end
  116. end,
  117. custom_on_velocity_change = function(self, velocity, old_velocity, dtime)
  118. if not velocity or not old_velocity then return end
  119. if old_velocity == 0 and velocity > 0 then
  120. minetest.sound_play("advtrains_subway_depart", {object = self.object})
  121. end
  122. if velocity < 2 and (old_velocity >= 2 or old_velocity == velocity) and not self.sound_arrive_handle then
  123. self.sound_arrive_handle = minetest.sound_play("advtrains_subway_arrive", {object = self.object})
  124. elseif (velocity > old_velocity) and self.sound_arrive_handle then
  125. minetest.sound_stop(self.sound_arrive_handle)
  126. self.sound_arrive_handle = nil
  127. end
  128. if velocity > 0 and (self.sound_loop_tmr or 0)<=0 then
  129. self.sound_loop_handle = minetest.sound_play({name="advtrains_subway_loop", gain=0.3}, {object = self.object})
  130. self.sound_loop_tmr=3
  131. elseif velocity>0 then
  132. self.sound_loop_tmr = self.sound_loop_tmr - dtime
  133. elseif velocity==0 then
  134. if self.sound_loop_handle then
  135. minetest.sound_stop(self.sound_loop_handle)
  136. self.sound_loop_handle = nil
  137. end
  138. self.sound_loop_tmr=0
  139. end
  140. end,
  141. }, S("Construction train"), "advtrains_subway_wagon_inv.png")