player.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. --
  2. -- HP Change Reasons
  3. --
  4. local expect = nil
  5. core.register_on_player_hpchange(function(player, hp_change, reason)
  6. if expect == nil then
  7. return
  8. end
  9. for key, value in pairs(reason) do
  10. assert(expect[key] == value)
  11. end
  12. for key, value in pairs(expect) do
  13. assert(reason[key] == value)
  14. end
  15. expect = nil
  16. end)
  17. local function run_hpchangereason_tests(player)
  18. local old_hp = player:get_hp()
  19. player:set_hp(20)
  20. expect = { type = "set_hp", from = "mod" }
  21. player:set_hp(3)
  22. assert(expect == nil)
  23. expect = { a = 234, type = "set_hp", from = "mod" }
  24. player:set_hp(7, { a= 234 })
  25. assert(expect == nil)
  26. expect = { df = 3458973454, type = "fall", from = "mod" }
  27. player:set_hp(10, { type = "fall", df = 3458973454 })
  28. assert(expect == nil)
  29. player:set_hp(old_hp)
  30. end
  31. unittests.register("test_hpchangereason", run_hpchangereason_tests, {player=true})
  32. --
  33. -- HP differences
  34. --
  35. local expected_diff = nil
  36. local hpchange_counter = 0
  37. local die_counter = 0
  38. core.register_on_player_hpchange(function(player, hp_change, reason)
  39. if expected_diff then
  40. assert(hp_change == expected_diff)
  41. hpchange_counter = hpchange_counter + 1
  42. end
  43. end)
  44. core.register_on_dieplayer(function()
  45. die_counter = die_counter + 1
  46. end)
  47. local function hp_diference_test(player, hp_max)
  48. assert(hp_max >= 22)
  49. local old_hp = player:get_hp()
  50. local old_hp_max = player:get_properties().hp_max
  51. hpchange_counter = 0
  52. die_counter = 0
  53. expected_diff = nil
  54. player:set_properties({hp_max = hp_max})
  55. player:set_hp(22)
  56. assert(player:get_hp() == 22)
  57. assert(hpchange_counter == 0)
  58. assert(die_counter == 0)
  59. -- HP difference is not clamped
  60. expected_diff = -25
  61. player:set_hp(-3)
  62. -- actual final HP value is clamped to >= 0
  63. assert(player:get_hp() == 0)
  64. assert(hpchange_counter == 1)
  65. assert(die_counter == 1)
  66. expected_diff = 22
  67. player:set_hp(22)
  68. assert(player:get_hp() == 22)
  69. assert(hpchange_counter == 2)
  70. assert(die_counter == 1)
  71. -- Integer overflow is prevented
  72. -- so result is S32_MIN, not S32_MIN - 22
  73. expected_diff = -2147483648
  74. player:set_hp(-2147483648)
  75. -- actual final HP value is clamped to >= 0
  76. assert(player:get_hp() == 0)
  77. assert(hpchange_counter == 3)
  78. assert(die_counter == 2)
  79. -- Damage is ignored if player is already dead (hp == 0)
  80. expected_diff = "never equal"
  81. player:set_hp(-11)
  82. assert(player:get_hp() == 0)
  83. -- no on_player_hpchange or on_dieplayer call expected
  84. assert(hpchange_counter == 3)
  85. assert(die_counter == 2)
  86. expected_diff = 11
  87. player:set_hp(11)
  88. assert(player:get_hp() == 11)
  89. assert(hpchange_counter == 4)
  90. assert(die_counter == 2)
  91. -- HP difference is not clamped
  92. expected_diff = 1000000 - 11
  93. player:set_hp(1000000)
  94. -- actual final HP value is clamped to <= hp_max
  95. assert(player:get_hp() == hp_max)
  96. assert(hpchange_counter == 5)
  97. assert(die_counter == 2)
  98. -- "Healing" is not ignored when hp == hp_max
  99. expected_diff = 80000 - hp_max
  100. player:set_hp(80000)
  101. assert(player:get_hp() == hp_max)
  102. -- on_player_hpchange_call expected
  103. assert(hpchange_counter == 6)
  104. assert(die_counter == 2)
  105. expected_diff = nil
  106. player:set_properties({hp_max = old_hp_max})
  107. player:set_hp(old_hp)
  108. core.close_formspec(player:get_player_name(), "") -- hide death screen
  109. end
  110. local function run_hp_difference_tests(player)
  111. hp_diference_test(player, 22)
  112. hp_diference_test(player, 30)
  113. hp_diference_test(player, 65535) -- U16_MAX
  114. end
  115. unittests.register("test_hp_difference", run_hp_difference_tests, {player=true})
  116. --
  117. -- Player meta
  118. --
  119. local function run_player_meta_tests(player)
  120. local meta = player:get_meta()
  121. meta:set_string("foo", "bar")
  122. assert(meta:contains("foo"))
  123. assert(meta:get_string("foo") == "bar")
  124. assert(meta:get("foo") == "bar")
  125. local meta2 = player:get_meta()
  126. assert(meta2:get_string("foo") == "bar")
  127. assert(meta2:get("foo") == "bar")
  128. assert(meta:equals(meta2))
  129. meta:set_string("bob", "dillan")
  130. assert(meta:get_string("foo") == "bar")
  131. assert(meta:get_string("bob") == "dillan")
  132. assert(meta:get("bob") == "dillan")
  133. assert(meta2:get_string("foo") == "bar")
  134. assert(meta2:get_string("bob") == "dillan")
  135. assert(meta2:get("bob") == "dillan")
  136. assert(meta:equals(meta2))
  137. meta:set_string("foo", "")
  138. assert(not meta:contains("foo"))
  139. assert(meta:get("foo") == nil)
  140. assert(meta:get_string("foo") == "")
  141. assert(meta:equals(meta2))
  142. end
  143. unittests.register("test_player_meta", run_player_meta_tests, {player=true})
  144. --
  145. -- Player add pos
  146. --
  147. local function run_player_add_pos_tests(player)
  148. local pos = player:get_pos()
  149. player:add_pos(vector.new(0, 1000, 0))
  150. local newpos = player:get_pos()
  151. player:add_pos(vector.new(0, -1000, 0))
  152. local backpos = player:get_pos()
  153. local newdist = vector.distance(pos, newpos)
  154. assert(math.abs(newdist - 1000) <= 1)
  155. assert(vector.distance(pos, backpos) <= 1)
  156. end
  157. unittests.register("test_player_add_pos", run_player_add_pos_tests, {player=true})
  158. --
  159. -- Hotbar selection clamp
  160. --
  161. local function run_player_hotbar_clamp_tests(player)
  162. local inv = player:get_inventory()
  163. local old_inv_size = inv:get_size("main")
  164. local old_inv_list = inv:get_list("main") -- Avoid accidentally removing item
  165. local old_bar_size = player:hud_get_hotbar_itemcount()
  166. inv:set_size("main", 5)
  167. player:hud_set_hotbar_itemcount(2)
  168. assert(player:hud_get_hotbar_itemcount() == 2)
  169. player:hud_set_hotbar_itemcount(6)
  170. assert(player:hud_get_hotbar_itemcount() == 5)
  171. inv:set_size("main", old_inv_size)
  172. inv:set_list("main", old_inv_list)
  173. player:hud_set_hotbar_itemcount(old_bar_size)
  174. end
  175. unittests.register("test_player_hotbar_clamp", run_player_hotbar_clamp_tests, {player=true})