init.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. -- Minetest 0.4 mod: player
  2. -- See README.txt for licensing and other information.
  3. player = player or {}
  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. 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. -- Update player velocity if available.
  110. if player_velocity[pname] then
  111. player:add_velocity(player_velocity[pname])
  112. player_velocity[pname] = nil
  113. end
  114. end)
  115. minetest.register_on_leaveplayer(function(player)
  116. local name = player:get_player_name()
  117. player_model[name] = nil
  118. player_anim[name] = nil
  119. player_textures[name] = nil
  120. -- Save player velocity. If they login again, I will be able to restore it.
  121. player_velocity[name] = player:get_velocity()
  122. end)
  123. -- Localize for better performance.
  124. local player_set_animation = default.player_set_animation
  125. local player_attached = default.player_attached
  126. -- Check each player and apply animations
  127. minetest.register_globalstep(function(dtime)
  128. for _, player in pairs(minetest.get_connected_players()) do
  129. local name = player:get_player_name()
  130. local model_name = player_model[name]
  131. local model = model_name and models[model_name]
  132. if model and not player_attached[name] then
  133. local controls = player:get_player_control()
  134. local walking = false
  135. local animation_speed_mod = model.animation_speed or 30
  136. -- Determine if the player is walking
  137. if controls.up or controls.down or controls.left or controls.right then
  138. walking = true
  139. end
  140. -- Determine if the player is sneaking, and reduce animation speed if so
  141. if controls.sneak then
  142. animation_speed_mod = animation_speed_mod / 2
  143. end
  144. -- Apply animations based on what the player is doing
  145. if player:get_hp() == 0 then
  146. player_set_animation(player, "lay")
  147. elseif walking then
  148. if player_sneak[name] ~= controls.sneak then
  149. player_anim[name] = nil
  150. player_sneak[name] = controls.sneak
  151. end
  152. if controls.LMB then
  153. player_set_animation(player, "walk_mine", animation_speed_mod)
  154. else
  155. player_set_animation(player, "walk", animation_speed_mod)
  156. end
  157. elseif controls.LMB then
  158. player_set_animation(player, "mine")
  159. else
  160. player_set_animation(player, "stand", animation_speed_mod)
  161. end
  162. end
  163. end
  164. end)
  165. -- Disable the "sneak glitch" for all players.
  166. -- Note: 'sneak=false' interferes with footstep sounds when walking on snow.
  167. minetest.register_on_joinplayer(function(player)
  168. player:set_physics_override({
  169. sneak_glitch = false,
  170. sneak = true,
  171. gravity = 1.0,
  172. })
  173. player:set_properties({
  174. infotext = rename.gpn(player:get_player_name()),
  175. })
  176. -- Disable the minimap. Cheaters will of course be able to enable it.
  177. -- Can be reenabled via item in-game.
  178. player:hud_set_flags({
  179. minimap = false,
  180. minimap_radar = false,
  181. -- At last! The custom coordinate system is now First Class!
  182. basic_debug = false,
  183. })
  184. -- Finally! Minetest has shadow support!
  185. -- check if function is supported by server (old versions 5.5.0)
  186. if player["set_lighting"] ~= nil then
  187. player:set_lighting({
  188. shadows = {intensity=0.5},
  189. })
  190. else
  191. minetest.log("WARNING", "This server does not support player:lighting !");
  192. end
  193. end)