aboutsummaryrefslogtreecommitdiff
path: root/games/minimal/mods/test/player.lua
blob: 563d0d9859bcc0c8abceecb2d91332ce49fe6151 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
--
-- Minimal Development Test
-- Mod: test
--

--
-- HP Change Reasons
--
local expect = nil
local function run_hpchangereason_tests(player)
	expect = { type = "set_hp", from = "mod" }
	player:set_hp(3)
	assert(expect == nil)

	expect = { a = 234, type = "set_hp", from = "mod" }
	player:set_hp(7, { a= 234 })
	assert(expect == nil)

	expect = { df = 3458973454, type = "fall", from = "mod" }
	player:set_hp(10, { type = "fall", df = 3458973454 })
	assert(expect == nil)
end
minetest.register_on_player_hpchange(function(player, hp, reason)
	if not expect then
		return
	end

	for key, value in pairs(reason) do
		assert(expect[key] == value)
	end

	for key, value in pairs(expect) do
		assert(reason[key] == value)
	end

	expect = nil
end)


local function run_player_meta_tests(player)
	local meta = player:get_meta()
	meta:set_string("foo", "bar")
	assert(meta:contains("foo"))
	assert(meta:get_string("foo") == "bar")
	assert(meta:get("foo") == "bar")

	local meta2 = player:get_meta()
	assert(meta2:get_string("foo") == "bar")
	assert(meta2:get("foo") == "bar")
	assert(meta:equals(meta2))
	assert(player:get_attribute("foo") == "bar")

	meta:set_string("bob", "dillan")
	assert(meta:get_string("foo") == "bar")
	assert(meta:get_string("bob") == "dillan")
	assert(meta:get("bob") == "dillan")
	assert(meta2:get_string("foo") == "bar")
	assert(meta2:get_string("bob") == "dillan")
	assert(meta2:get("bob") == "dillan")
	assert(meta:equals(meta2))
	assert(player:get_attribute("foo") == "bar")
	assert(player:get_attribute("bob") == "dillan")

	meta:set_string("foo", "")
	assert(not meta:contains("foo"))
	assert(meta:get("foo") == nil)
	assert(meta:get_string("foo") == "")
	assert(meta:equals(meta2))
end

local function run_player_tests(player)
	run_hpchangereason_tests(player)
	run_player_meta_tests(player)
	minetest.chat_send_all("All tests pass!")
end
minetest.register_on_joinplayer(run_player_tests)
nil end local show_breathbar = flags.breathbar and enable_damage and not immortal local breath = player:get_breath() local breath_max = player:get_properties().breath_max if show_breathbar and breath <= breath_max then local number = 2 * scaleToDefault(player, "breath") if not hud.id_breathbar and breath < breath_max then local hud_def = table.copy(breath_bar_definition) hud_def.number = number hud.id_breathbar = player:hud_add(hud_def) elseif hud.id_breathbar then player:hud_change(hud.id_breathbar, "number", number) end end if hud.id_breathbar and (not show_breathbar or breath == breath_max) then minetest.after(1, function(player_name, breath_bar) local player = minetest.get_player_by_name(player_name) if player then player:hud_remove(breath_bar) end end, name, hud.id_breathbar) hud.id_breathbar = nil end end local function cleanup_builtin_statbars(player) local name = player:get_player_name() if name == "" then return end hud_ids[name] = nil end local function player_event_handler(player,eventname) assert(player:is_player()) local name = player:get_player_name() if name == "" or not hud_ids[name] then return end if eventname == "health_changed" then update_builtin_statbars(player) if hud_ids[name].id_healthbar then return true end end if eventname == "breath_changed" then update_builtin_statbars(player) if hud_ids[name].id_breathbar then return true end end if eventname == "hud_changed" or eventname == "properties_changed" then update_builtin_statbars(player) return true end return false end function core.hud_replace_builtin(hud_name, definition) if type(definition) ~= "table" or definition.hud_elem_type ~= "statbar" then return false end if hud_name == "health" then health_bar_definition = definition for name, ids in pairs(hud_ids) do local player = core.get_player_by_name(name) if player and ids.id_healthbar then player:hud_remove(ids.id_healthbar) ids.id_healthbar = nil update_builtin_statbars(player) end end return true end if hud_name == "breath" then breath_bar_definition = definition for name, ids in pairs(hud_ids) do local player = core.get_player_by_name(name) if player and ids.id_breathbar then player:hud_remove(ids.id_breathbar) ids.id_breathbar = nil update_builtin_statbars(player) end end return true end return false end -- Append "update_builtin_statbars" as late as possible -- This ensures that the HUD is hidden when the flags are updated in this callback core.register_on_mods_loaded(function() core.register_on_joinplayer(update_builtin_statbars) end) core.register_on_leaveplayer(cleanup_builtin_statbars) core.register_playerevent(player_event_handler)