init.lua 4.7 KB

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