appearance_handler.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. local master = dwarf_characters
  2. local intramodcom = dwarf_characters.intramodcom
  3. local S = minetest.get_translator("dwarf_characters")
  4. --persistent storage
  5. local known_player_appearances =
  6. intramodcom.register_for_storage("known_player_appearances",
  7. {})
  8. --non-persistent storage
  9. local player_textures = {}
  10. local function get_random_appearance()
  11. local appearance = {}
  12. appearance.skin_color = math.random(1, 10) / 10
  13. appearance.hair_brightness = math.random(1, 10) / 10
  14. appearance.hair_redness = math.random(1, 10) / 10
  15. local model_list = dwarf_characters.get_models()
  16. local model = model_list[math.random(#model_list)]
  17. appearance.model = model.name
  18. appearance.sex = model.sex
  19. appearance.beard = dwarf_characters.get_random_beard(appearance.sex)
  20. appearance.hair = dwarf_characters.get_random_hair()
  21. appearance.armor = dwarf_characters.get_random_armor()
  22. appearance.helmet = dwarf_characters.get_random_helmet()
  23. return appearance
  24. end
  25. local function get_appearance(name)
  26. local appearance = known_player_appearances[name]
  27. if not appearance
  28. then
  29. appearance = get_random_appearance()
  30. end
  31. return appearance
  32. end
  33. local get_textures = intramodcom.get_textures
  34. local animations = intramodcom.animations
  35. minetest.register_chatcommand("newappearance",
  36. {
  37. privs = {interact = true},
  38. func = function(name)
  39. local player = minetest.get_player_by_name(name)
  40. if player
  41. then
  42. local appearance = get_random_appearance()
  43. known_player_appearances[name] = appearance
  44. local props = player:get_properties()
  45. props.textures = get_textures(appearance)
  46. props.mesh = appearance.model
  47. player:set_properties(props)
  48. return true, S("Your appearance changed! (unless you got the same as before by random chance)")
  49. else
  50. return true, S("You must be online for this command to work.")
  51. end
  52. end
  53. })
  54. -- Update appearance when the player joins
  55. minetest.register_on_joinplayer(function(player)
  56. local name = player:get_player_name()
  57. do --object property stuff
  58. local properties = player:get_properties()
  59. local appearance = get_appearance(name)
  60. properties.mesh = appearance.model
  61. properties.textures = get_textures(appearance)
  62. properties.visual = "mesh"
  63. properties.eye_height = 1.3
  64. properties.visual_size = {x = 1, y = 1, z = 1}
  65. player:set_properties(properties)
  66. end
  67. player:set_local_animation(
  68. animations.stand(),
  69. animations.walk(),
  70. animations.mine(),
  71. animations.walk_mine(),
  72. 45)
  73. player:set_animation(animations.stand(), 45, 0, true)
  74. end)
  75. minetest.register_on_dieplayer(function(player)
  76. player:set_animation(animations.die(), 45, 0, false)
  77. end)
  78. minetest.register_on_respawnplayer(function(player)
  79. player:set_animation(animations.stand(), 45, 0, true)
  80. end)