init.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. local S = attrans
  2. advtrains.register_wagon("subway_wagon", {
  3. mesh="advtrains_subway_wagon.b3d",
  4. textures = {"advtrains_subway_wagon.png"},
  5. drives_on={default=true},
  6. max_speed=15,
  7. seats = {
  8. {
  9. name="Driver stand",
  10. attach_offset={x=0, y=0, z=0},
  11. view_offset={x=0, y=0, z=0},
  12. group="dstand",
  13. },
  14. {
  15. name="1",
  16. attach_offset={x=-4, y=-2, z=8},
  17. view_offset={x=0, y=0, z=0},
  18. group="pass",
  19. },
  20. {
  21. name="2",
  22. attach_offset={x=4, y=-2, z=8},
  23. view_offset={x=0, y=0, z=0},
  24. group="pass",
  25. },
  26. {
  27. name="3",
  28. attach_offset={x=-4, y=-2, z=-8},
  29. view_offset={x=0, y=0, z=0},
  30. group="pass",
  31. },
  32. {
  33. name="4",
  34. attach_offset={x=4, y=-2, z=-8},
  35. view_offset={x=0, y=0, z=0},
  36. group="pass",
  37. },
  38. },
  39. seat_groups = {
  40. dstand={
  41. name = "Driver Stand",
  42. access_to = {"pass"},
  43. require_doors_open=true,
  44. driving_ctrl_access=true,
  45. },
  46. pass={
  47. name = "Passenger area",
  48. access_to = {"dstand"},
  49. require_doors_open=true,
  50. },
  51. },
  52. assign_to_seat_group = {"pass", "dstand"},
  53. coupler_types_front = {chain=true},
  54. coupler_types_back = {chain=true},
  55. doors={
  56. open={
  57. [-1]={frames={x=0, y=20}, time=1},
  58. [1]={frames={x=40, y=60}, time=1},
  59. sound = "advtrains_subway_dopen",
  60. },
  61. close={
  62. [-1]={frames={x=20, y=40}, time=1},
  63. [1]={frames={x=60, y=80}, time=1},
  64. sound = "advtrains_subway_dclose",
  65. }
  66. },
  67. door_entry={-1, 1},
  68. visual_size = {x=1, y=1},
  69. wagon_span=2,
  70. --collisionbox = {-1.0,-0.5,-1.8, 1.0,2.5,1.8},
  71. collisionbox = {-1.0,-0.5,-1.0, 1.0,2.5,1.0},
  72. is_locomotive=true,
  73. drops={"default:steelblock 4"},
  74. horn_sound = "advtrains_subway_horn",
  75. custom_on_velocity_change = function(self, velocity, old_velocity, dtime)
  76. if not velocity or not old_velocity then return end
  77. if old_velocity == 0 and velocity > 0 then
  78. minetest.sound_play("advtrains_subway_depart", {object = self.object})
  79. end
  80. if velocity < 2 and (old_velocity >= 2 or old_velocity == velocity) and not self.sound_arrive_handle then
  81. self.sound_arrive_handle = minetest.sound_play("advtrains_subway_arrive", {object = self.object})
  82. elseif (velocity > old_velocity) and self.sound_arrive_handle then
  83. minetest.sound_stop(self.sound_arrive_handle)
  84. self.sound_arrive_handle = nil
  85. end
  86. if velocity > 0 and (self.sound_loop_tmr or 0)<=0 then
  87. self.sound_loop_handle = minetest.sound_play({name="advtrains_subway_loop", gain=0.3}, {object = self.object})
  88. self.sound_loop_tmr=3
  89. elseif velocity>0 then
  90. self.sound_loop_tmr = self.sound_loop_tmr - dtime
  91. elseif velocity==0 then
  92. if self.sound_loop_handle then
  93. minetest.sound_stop(self.sound_loop_handle)
  94. self.sound_loop_handle = nil
  95. end
  96. self.sound_loop_tmr=0
  97. end
  98. end,
  99. custom_on_step = function(self, dtime, data, train)
  100. --set line number
  101. local line = nil
  102. if train.line and self.line_cache ~= train.line then
  103. self.line_cache=train.line
  104. local lint = train.line
  105. if string.sub(train.line, 1, 1) == "S" then
  106. lint = string.sub(train.line,2)
  107. end
  108. if string.len(lint) == 1 then
  109. if lint=="X" then line="X" end
  110. line = tonumber(lint)
  111. elseif string.len(lint) == 2 then
  112. if tonumber(lint) then
  113. line = lint
  114. end
  115. end
  116. if line then
  117. local new_line_tex="advtrains_subway_wagon.png"
  118. if type(line)=="number" or line == "X" then
  119. new_line_tex = new_line_tex.."^advtrains_subway_wagon_line"..line..".png"
  120. else
  121. local num = tonumber(line)
  122. local red = math.fmod(line*67+101, 255)
  123. local green = math.fmod(line*97+109, 255)
  124. local blue = math.fmod(line*73+127, 255)
  125. new_line_tex = new_line_tex..string.format("^(advtrains_subway_wagon_line.png^[colorize:#%X%X%X%X%X%X)^(advtrains_subway_wagon_line%s_.png^advtrains_subway_wagon_line_%s.png", math.floor(red/16), math.fmod(red,16), math.floor(green/16), math.fmod(green,16), math.floor(blue/16), math.fmod(blue,16), string.sub(line, 1, 1), string.sub(line, 2, 2))
  126. if red + green + blue > 512 then
  127. new_line_tex = new_line_tex .. "^[colorize:#000)"
  128. else
  129. new_line_tex = new_line_tex .. ")"
  130. end
  131. end
  132. self.object:set_properties({
  133. textures={new_line_tex},
  134. })
  135. elseif self.line_cache~=nil and line==nil then
  136. self.object:set_properties({
  137. textures=self.textures,
  138. })
  139. self.line_cache=nil
  140. end
  141. end
  142. end,
  143. }, S("Subway Passenger Wagon"), "advtrains_subway_wagon_inv.png")
  144. --wagons
  145. minetest.register_craft({
  146. output = 'advtrains:subway_wagon',
  147. recipe = {
  148. {'default:steelblock', 'default:steelblock', 'default:steelblock'},
  149. {'default:steelblock', 'dye:yellow', 'default:steelblock'},
  150. {'default:steelblock', 'default:steelblock', 'default:steelblock'},
  151. },
  152. })