init.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. default = default or {}
  2. player = player or {}
  3. player.modpath = minetest.get_modpath("player")
  4. -- Minetest 0.4 mod: player
  5. -- See README.txt for licensing and other information.
  6. -- Player animation blending
  7. -- Note: This is currently broken due to a bug in Irrlicht, leave at 0
  8. local animation_blend = 0
  9. default.registered_player_models = { }
  10. -- Local for speed.
  11. local models = default.registered_player_models
  12. function default.player_register_model(name, def)
  13. models[name] = def
  14. end
  15. -- Default player appearance
  16. -- Controled by 3d_armor mod!
  17. --[[
  18. default.player_register_model("character_musttest.b3d", {
  19. animation_speed = 30,
  20. textures = {"character.png", },
  21. animations = {
  22. -- Standard animations.
  23. stand = { x= 0, y= 79, },
  24. lay = { x=162, y=166, },
  25. walk = { x=168, y=187, },
  26. mine = { x=189, y=198, },
  27. walk_mine = { x=200, y=219, },
  28. -- Extra animations (not currently used by the game).
  29. sit = { x= 81, y=160, },
  30. nod = { x=221, y=251, },
  31. },
  32. })
  33. --]]
  34. -- Player stats and animations
  35. local player_model = {}
  36. local player_textures = {}
  37. local player_anim = {}
  38. local player_sneak = {}
  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. player:set_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. player:set_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. player:set_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. -- set GUI
  96. -- This is managed by the inventory mod.
  97. --if not minetest.setting_getbool("creative_mode") then
  98. -- player:set_inventory_formspec(default.formspec.get_default_form())
  99. --end
  100. -- Big hot-bar is revoked for cheaters.
  101. if minetest.check_player_privs(player, {big_hotbar=true}) and not sheriff.is_cheater(pname) then
  102. player:hud_set_hotbar_image("gui_hotbar2.png")
  103. player:hud_set_hotbar_itemcount(16)
  104. else
  105. player:hud_set_hotbar_image("gui_hotbar.png")
  106. player:hud_set_hotbar_itemcount(8)
  107. end
  108. player:hud_set_hotbar_selected_image("hud_hotbar_selected.png")
  109. end)
  110. minetest.register_chatcommand("hotbar", {
  111. params = "",
  112. description = "Hotbar test command.",
  113. privs = {server = true},
  114. func = function(user, param)
  115. local player = minetest.get_player_by_name(user)
  116. if not player or not player:is_player() then return true end
  117. local count = player:hud_get_hotbar_itemcount()
  118. if count == 8 then
  119. player:hud_set_hotbar_image("gui_hotbar2.png")
  120. player:hud_set_hotbar_itemcount(16)
  121. else
  122. player:hud_set_hotbar_image("gui_hotbar.png")
  123. player:hud_set_hotbar_itemcount(8)
  124. end
  125. return true
  126. end,
  127. })
  128. minetest.register_on_leaveplayer(function(player)
  129. local name = player:get_player_name()
  130. player_model[name] = nil
  131. player_anim[name] = nil
  132. player_textures[name] = nil
  133. end)
  134. -- Localize for better performance.
  135. local player_set_animation = default.player_set_animation
  136. local player_attached = default.player_attached
  137. -- Check each player and apply animations
  138. minetest.register_globalstep(function(dtime)
  139. for _, player in pairs(minetest.get_connected_players()) do
  140. local name = player:get_player_name()
  141. local model_name = player_model[name]
  142. local model = model_name and models[model_name]
  143. if model and not player_attached[name] then
  144. local controls = player:get_player_control()
  145. local walking = false
  146. local animation_speed_mod = model.animation_speed or 30
  147. -- Determine if the player is walking
  148. if controls.up or controls.down or controls.left or controls.right then
  149. walking = true
  150. end
  151. -- Determine if the player is sneaking, and reduce animation speed if so
  152. if controls.sneak then
  153. animation_speed_mod = animation_speed_mod / 2
  154. end
  155. -- Apply animations based on what the player is doing
  156. if player:get_hp() == 0 then
  157. player_set_animation(player, "lay")
  158. elseif walking then
  159. if player_sneak[name] ~= controls.sneak then
  160. player_anim[name] = nil
  161. player_sneak[name] = controls.sneak
  162. end
  163. if controls.LMB then
  164. player_set_animation(player, "walk_mine", animation_speed_mod)
  165. else
  166. player_set_animation(player, "walk", animation_speed_mod)
  167. end
  168. elseif controls.LMB then
  169. player_set_animation(player, "mine")
  170. else
  171. player_set_animation(player, "stand", animation_speed_mod)
  172. end
  173. end
  174. end
  175. end)
  176. -- Disable the "sneak glitch" for all players.
  177. -- Note: 'sneak=false' interferes with footstep sounds when walking on snow.
  178. minetest.register_on_joinplayer(function(player)
  179. player:set_physics_override({
  180. sneak_glitch = false,
  181. sneak = true,
  182. gravity = 1.0,
  183. })
  184. player:set_properties({
  185. infotext = rename.gpn(player:get_player_name()),
  186. })
  187. -- Disable the minimap. Cheaters will of course be able to enable it.
  188. -- Can be reenabled via item in-game.
  189. player:hud_set_flags({minimap=false, minimap_radar=false})
  190. end)