init.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. default.player_attached[player:get_player_name()] = false
  92. --default.player_set_model(player, "character_musttest.b3d")
  93. player:set_local_animation({x=0, y=79}, {x=168, y=187}, {x=189, y=198}, {x=200, y=219}, 30)
  94. -- set GUI
  95. -- This is managed by the inventory mod.
  96. --if not minetest.setting_getbool("creative_mode") then
  97. -- player:set_inventory_formspec(default.formspec.get_default_form())
  98. --end
  99. if minetest.check_player_privs(player, {big_hotbar=true}) then
  100. player:hud_set_hotbar_image("gui_hotbar2.png")
  101. player:hud_set_hotbar_itemcount(16)
  102. else
  103. player:hud_set_hotbar_image("gui_hotbar.png")
  104. player:hud_set_hotbar_itemcount(8)
  105. end
  106. player:hud_set_hotbar_selected_image("hud_hotbar_selected.png")
  107. end)
  108. minetest.register_chatcommand("hotbar", {
  109. params = "",
  110. description = "Hotbar test command.",
  111. privs = {server = true},
  112. func = function(user, param)
  113. local player = minetest.get_player_by_name(user)
  114. if not player or not player:is_player() then return true end
  115. local count = player:hud_get_hotbar_itemcount()
  116. if count == 8 then
  117. player:hud_set_hotbar_image("gui_hotbar2.png")
  118. player:hud_set_hotbar_itemcount(16)
  119. else
  120. player:hud_set_hotbar_image("gui_hotbar.png")
  121. player:hud_set_hotbar_itemcount(8)
  122. end
  123. return true
  124. end,
  125. })
  126. minetest.register_on_leaveplayer(function(player)
  127. local name = player:get_player_name()
  128. player_model[name] = nil
  129. player_anim[name] = nil
  130. player_textures[name] = nil
  131. end)
  132. -- Localize for better performance.
  133. local player_set_animation = default.player_set_animation
  134. local player_attached = default.player_attached
  135. -- Check each player and apply animations
  136. minetest.register_globalstep(function(dtime)
  137. for _, player in pairs(minetest.get_connected_players()) do
  138. local name = player:get_player_name()
  139. local model_name = player_model[name]
  140. local model = model_name and models[model_name]
  141. if model and not player_attached[name] then
  142. local controls = player:get_player_control()
  143. local walking = false
  144. local animation_speed_mod = model.animation_speed or 30
  145. -- Determine if the player is walking
  146. if controls.up or controls.down or controls.left or controls.right then
  147. walking = true
  148. end
  149. -- Determine if the player is sneaking, and reduce animation speed if so
  150. if controls.sneak then
  151. animation_speed_mod = animation_speed_mod / 2
  152. end
  153. -- Apply animations based on what the player is doing
  154. if player:get_hp() == 0 then
  155. player_set_animation(player, "lay")
  156. elseif walking then
  157. if player_sneak[name] ~= controls.sneak then
  158. player_anim[name] = nil
  159. player_sneak[name] = controls.sneak
  160. end
  161. if controls.LMB then
  162. player_set_animation(player, "walk_mine", animation_speed_mod)
  163. else
  164. player_set_animation(player, "walk", animation_speed_mod)
  165. end
  166. elseif controls.LMB then
  167. player_set_animation(player, "mine")
  168. else
  169. player_set_animation(player, "stand", animation_speed_mod)
  170. end
  171. end
  172. end
  173. end)
  174. -- Disable the "sneak glitch" for all players.
  175. -- Note: 'sneak=false' interferes with footstep sounds when walking on snow.
  176. minetest.register_on_joinplayer(function(player)
  177. player:set_physics_override({
  178. sneak_glitch = false,
  179. sneak = true,
  180. gravity = 1.0,
  181. })
  182. player:set_properties({
  183. infotext = rename.gpn(player:get_player_name()),
  184. })
  185. -- Disable the minimap. Cheaters will of course be able to enable it.
  186. -- Can be reenabled via item in-game.
  187. player:hud_set_flags({minimap=false, minimap_radar=false})
  188. end)