init.lua 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. -- Default player appearance
  15. -- Controled by 3d_armor mod!
  16. --[[
  17. default.player_register_model("character_musttest.b3d", {
  18. animation_speed = 30,
  19. textures = {"character.png", },
  20. animations = {
  21. -- Standard animations.
  22. stand = { x= 0, y= 79, },
  23. lay = { x=162, y=166, },
  24. walk = { x=168, y=187, },
  25. mine = { x=189, y=198, },
  26. walk_mine = { x=200, y=219, },
  27. -- Extra animations (not currently used by the game).
  28. sit = { x= 81, y=160, },
  29. nod = { x=221, y=251, },
  30. },
  31. })
  32. --]]
  33. -- Player stats and animations
  34. local player_model = {}
  35. local player_textures = {}
  36. local player_anim = {}
  37. local player_sneak = {}
  38. local player_velocity = {}
  39. default.player_attached = {}
  40. function default.player_get_animation(player)
  41. local name = player:get_player_name()
  42. return {
  43. model = player_model[name],
  44. textures = player_textures[name],
  45. animation = player_anim[name],
  46. }
  47. end
  48. -- Called when a player's appearance needs to be updated
  49. function default.player_set_model(player, model_name)
  50. local name = player:get_player_name()
  51. local model = models[model_name]
  52. if model then
  53. if player_model[name] == model_name then
  54. return
  55. end
  56. pova.set_override(player, "properties", {
  57. mesh = model_name,
  58. textures = player_textures[name] or model.textures,
  59. visual = "mesh",
  60. visual_size = model.visual_size or {x=1, y=1},
  61. })
  62. default.player_set_animation(player, "stand")
  63. else
  64. pova.set_override(player, "properties", {
  65. textures = { "player.png", "player_back.png", },
  66. visual = "upright_sprite",
  67. })
  68. end
  69. player_model[name] = model_name
  70. end
  71. function default.player_set_textures(player, textures)
  72. local name = player:get_player_name()
  73. player_textures[name] = textures
  74. pova.set_override(player, "properties", {textures = textures})
  75. end
  76. function default.player_set_animation(player, anim_name, speed)
  77. local name = player:get_player_name()
  78. if player_anim[name] == anim_name then
  79. return
  80. end
  81. local model = player_model[name] and models[player_model[name]]
  82. if not (model and model.animations[anim_name]) then
  83. return
  84. end
  85. local anim = model.animations[anim_name]
  86. player_anim[name] = anim_name
  87. player:set_animation(anim, speed or model.animation_speed, animation_blend)
  88. end
  89. -- Update appearance when the player joins
  90. minetest.register_on_joinplayer(function(player)
  91. local pname = player:get_player_name()
  92. default.player_attached[pname] = false
  93. --default.player_set_model(player, "character_musttest.b3d")
  94. player:set_local_animation({x=0, y=79}, {x=168, y=187}, {x=189, y=198}, {x=200, y=219}, 30)
  95. -- Big hot-bar is revoked for cheaters.
  96. if minetest.check_player_privs(player, {big_hotbar=true}) and not sheriff.is_cheater(pname) then
  97. player:hud_set_hotbar_image("gui_hotbar2.png")
  98. player:hud_set_hotbar_itemcount(16)
  99. else
  100. player:hud_set_hotbar_image("gui_hotbar.png")
  101. player:hud_set_hotbar_itemcount(8)
  102. end
  103. player:hud_set_hotbar_selected_image("hud_hotbar_selected.png")
  104. -- Update player velocity if available.
  105. if player_velocity[pname] then
  106. player:add_velocity(player_velocity[pname])
  107. player_velocity[pname] = nil
  108. end
  109. end)
  110. minetest.register_on_leaveplayer(function(player)
  111. local name = player:get_player_name()
  112. player_model[name] = nil
  113. player_anim[name] = nil
  114. player_textures[name] = nil
  115. -- Save player velocity. If they login again, I will be able to restore it.
  116. player_velocity[name] = player:get_velocity()
  117. end)
  118. -- Localize for better performance.
  119. local player_set_animation = default.player_set_animation
  120. local player_attached = default.player_attached
  121. -- Check each player and apply animations
  122. minetest.register_globalstep(function(dtime)
  123. for _, player in pairs(minetest.get_connected_players()) do
  124. local pname = player:get_player_name()
  125. local model_name = player_model[pname]
  126. local model = model_name and models[model_name]
  127. if model and not player_attached[pname] then
  128. local controls = player:get_player_control()
  129. local walking = false
  130. local animation_speed_mod = model.animation_speed or 30
  131. -- Determine if the player is walking
  132. if controls.up or controls.down or controls.left or controls.right then
  133. walking = true
  134. end
  135. -- Determine if the player is sneaking, and reduce animation speed if so
  136. if controls.sneak then
  137. animation_speed_mod = animation_speed_mod / 2
  138. pova.set_override(player, "properties", {
  139. makes_footstep_sound = false,
  140. })
  141. else
  142. if not gdac_invis.is_invisible(pname) then
  143. pova.set_override(player, "properties", {
  144. makes_footstep_sound = true,
  145. })
  146. end
  147. end
  148. -- Apply animations based on what the player is doing
  149. if player:get_hp() == 0 then
  150. player_set_animation(player, "lay")
  151. elseif walking then
  152. if player_sneak[pname] ~= controls.sneak then
  153. player_anim[pname] = nil
  154. player_sneak[pname] = controls.sneak
  155. end
  156. if controls.LMB then
  157. player_set_animation(player, "walk_mine", animation_speed_mod)
  158. else
  159. player_set_animation(player, "walk", animation_speed_mod)
  160. end
  161. elseif controls.LMB then
  162. player_set_animation(player, "mine")
  163. else
  164. player_set_animation(player, "stand", animation_speed_mod)
  165. end
  166. end
  167. end
  168. end)
  169. -- Disable the "sneak glitch" for all players.
  170. -- Note: 'sneak=false' interferes with footstep sounds when walking on snow.
  171. minetest.register_on_joinplayer(function(player)
  172. pova.set_override(player, "properties", {
  173. infotext = rename.gpn(player:get_player_name()),
  174. })
  175. pova.set_override(player, "nametag", {
  176. color = {a=255, r=0, g=255, b=255},
  177. text = rename.gpn(player:get_player_name()),
  178. bgcolor = false,
  179. })
  180. -- Disable the minimap. Cheaters will of course be able to enable it.
  181. -- Can be reenabled via item in-game.
  182. player:hud_set_flags({
  183. minimap = false,
  184. minimap_radar = false,
  185. -- At last! The custom coordinate system is now First Class!
  186. basic_debug = false,
  187. })
  188. -- Finally! Minetest has shadow support!
  189. -- check if function is supported by server (old versions 5.5.0)
  190. if player["set_lighting"] ~= nil then
  191. player:set_lighting({
  192. shadows = {intensity=0.5},
  193. })
  194. else
  195. minetest.log("WARNING", "This server does not support player:lighting !");
  196. end
  197. end)