init.lua 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. -- Minetest 0.4 mod: player
  2. -- See README.txt for licensing and other information.
  3. if not minetest.global_exists("player") then player = {} end
  4. player.modpath = minetest.get_modpath("player")
  5. -- Player animation blending
  6. -- Note: This is currently broken due to a bug in Irrlicht, leave at 0
  7. local animation_blend = 0
  8. player.registered_player_models = { }
  9. -- Local for speed.
  10. local models = player.registered_player_models
  11. function default.player_register_model(name, def)
  12. models[name] = def
  13. end
  14. -- Player stats and animations
  15. local player_model = {}
  16. local player_textures = {}
  17. local player_anim = {}
  18. local player_sneak = {}
  19. local player_velocity = {}
  20. default.player_attached = {}
  21. function default.player_get_animation(player)
  22. local name = player:get_player_name()
  23. return {
  24. model = player_model[name],
  25. textures = player_textures[name],
  26. animation = player_anim[name],
  27. }
  28. end
  29. -- Called when a player's appearance needs to be updated
  30. function default.player_set_model(player, model_name)
  31. local name = player:get_player_name()
  32. local model = models[model_name]
  33. if player_model[name] == model_name then
  34. return
  35. end
  36. pova.set_override(player, "properties", {
  37. mesh = model_name,
  38. textures = player_textures[name] or model.textures,
  39. visual = "mesh",
  40. visual_size = model.visual_size or {x=1, y=1, z=1},
  41. })
  42. default.player_set_animation(player, "stand")
  43. player_model[name] = model_name
  44. end
  45. function default.player_set_textures(player, textures)
  46. local name = player:get_player_name()
  47. player_textures[name] = textures
  48. pova.set_override(player, "properties", {textures = textures})
  49. end
  50. function default.player_set_animation(player, anim_name, speed)
  51. local name = player:get_player_name()
  52. if player_anim[name] == anim_name then
  53. return
  54. end
  55. local model = player_model[name] and models[player_model[name]]
  56. if not (model and model.animations[anim_name]) then
  57. return
  58. end
  59. local anim = model.animations[anim_name]
  60. player_anim[name] = anim_name
  61. player:set_animation(anim, speed or model.animation_speed, animation_blend)
  62. end
  63. -- Update appearance when the player joins
  64. minetest.register_on_joinplayer(function(player)
  65. local pname = player:get_player_name()
  66. default.player_attached[pname] = false
  67. player:set_local_animation({x=0, y=79}, {x=168, y=187}, {x=189, y=198}, {x=200, y=219}, 30)
  68. -- Big hot-bar is revoked for cheaters.
  69. if minetest.check_player_privs(player, {big_hotbar=true}) and not sheriff.is_cheater(pname) then
  70. player:hud_set_hotbar_image("gui_hotbar2.png")
  71. player:hud_set_hotbar_itemcount(16)
  72. else
  73. player:hud_set_hotbar_image("gui_hotbar.png")
  74. player:hud_set_hotbar_itemcount(8)
  75. end
  76. player:hud_set_hotbar_selected_image("hud_hotbar_selected.png")
  77. -- Update player velocity if available.
  78. if player_velocity[pname] then
  79. player:add_velocity(player_velocity[pname])
  80. player_velocity[pname] = nil
  81. end
  82. end)
  83. minetest.register_on_leaveplayer(function(player)
  84. local name = player:get_player_name()
  85. player_model[name] = nil
  86. player_anim[name] = nil
  87. player_textures[name] = nil
  88. -- Save player velocity. If they login again, I will be able to restore it.
  89. player_velocity[name] = player:get_velocity()
  90. end)
  91. -- Localize for better performance.
  92. local player_set_animation = default.player_set_animation
  93. local player_attached = default.player_attached
  94. -- Check each player and apply animations
  95. minetest.register_globalstep(function(dtime)
  96. for _, player in pairs(minetest.get_connected_players()) do
  97. local pname = player:get_player_name()
  98. local model_name = player_model[pname]
  99. local model = model_name and models[model_name]
  100. if model and not player_attached[pname] then
  101. local controls = player:get_player_control()
  102. local walking = false
  103. local animation_speed_mod = model.animation_speed or 30
  104. -- Determine if the player is walking
  105. if controls.up or controls.down or controls.left or controls.right then
  106. walking = true
  107. end
  108. -- Determine if the player is sneaking, and reduce animation speed if so
  109. if controls.sneak then
  110. animation_speed_mod = animation_speed_mod / 2
  111. pova.set_modifier(player, "properties",
  112. {makes_footstep_sound = false}, "footstep_sneaking")
  113. else
  114. pova.remove_modifier(player, "properties", "footstep_sneaking")
  115. end
  116. -- Apply animations based on what the player is doing
  117. if player:get_hp() == 0 then
  118. player_set_animation(player, "lay")
  119. elseif walking then
  120. if player_sneak[pname] ~= controls.sneak then
  121. player_anim[pname] = nil
  122. player_sneak[pname] = controls.sneak
  123. end
  124. if controls.LMB then
  125. player_set_animation(player, "walk_mine", animation_speed_mod)
  126. else
  127. player_set_animation(player, "walk", animation_speed_mod)
  128. end
  129. elseif controls.LMB then
  130. player_set_animation(player, "mine")
  131. else
  132. player_set_animation(player, "stand", animation_speed_mod)
  133. end
  134. end
  135. end
  136. end)
  137. -- Each player gets assigned a unique random seed. Once set, this seed number
  138. -- shall never change.
  139. local function set_prng(pref)
  140. local pmeta = pref:get_meta()
  141. local s = "random_seed"
  142. local rand = pmeta:get_int(s)
  143. -- I would have liked to use SecureRandom() but I don't feel like doing the
  144. -- skunkwork to convert a byte string into a random seed. I've done it before,
  145. -- in C++. It wasn't fun.
  146. if rand == 0 then
  147. pmeta:set_int(s, math.random(1000, 10000))
  148. end
  149. end
  150. -- Disable the "sneak glitch" for all players.
  151. -- Note: 'sneak=false' interferes with footstep sounds when walking on snow.
  152. minetest.register_on_joinplayer(function(player)
  153. set_prng(player)
  154. pova.set_override(player, "properties", {
  155. infotext = rename.gpn(player:get_player_name()),
  156. })
  157. pova.set_override(player, "nametag", {
  158. color = {a=255, r=0, g=255, b=255},
  159. text = rename.gpn(player:get_player_name()),
  160. bgcolor = false,
  161. })
  162. local pmeta = player:get_meta()
  163. local random_seed = pmeta:get_int("random_seed")
  164. local prng = PseudoRandom(random_seed)
  165. -- The max diff in either direction should be 0.1, otherwise players will
  166. -- be too oversized or undersized.
  167. local nsize = 1+(prng:next(-10, 10)/100)
  168. -- Adjust base speed. Max range diff is 0.05 either direction.
  169. local nspeed = 1+(prng:next(-10, 10)/200)
  170. -- Adjust base jump. Max range diff is 0.05 either direction.
  171. local njump = 1+(prng:next(-10, 10)/100)
  172. pova.set_modifier(player, "properties",
  173. {visual_size={x=nsize, y=nsize}},
  174. "notbornequal", {priority=-999})
  175. pova.set_modifier(player, "physics",
  176. {speed=nspeed, jump=njump},
  177. "notbornequal", {priority=-999})
  178. -- Disable the minimap. Cheaters will of course be able to enable it.
  179. -- Can be reenabled via item in-game.
  180. player:hud_set_flags({
  181. minimap = false,
  182. minimap_radar = false,
  183. -- At last! The custom coordinate system is now First Class!
  184. basic_debug = false,
  185. })
  186. -- Finally! Minetest has shadow support!
  187. -- check if function is supported by server (old versions 5.5.0)
  188. if player["set_lighting"] ~= nil then
  189. player:set_lighting({
  190. shadows = {intensity=0.3},
  191. })
  192. else
  193. minetest.log("WARNING", "This server does not support player:lighting !");
  194. end
  195. end)