aboutsummaryrefslogtreecommitdiff
path: root/ch_core/hud.lua
blob: 388357735f9f3dc0d6d15183f586e96a43dcf203 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
ch_core.open_submod("hud", {chat = true, data = true, lib = true})

local ifthenelse = ch_core.ifthenelse

-- PLAYER LIST
local base_y_offset = 90
local y_scale = 20

local text_hud_defaults = {
	type = "text",
	position = { x = 0.5, y = 0 },
	-- offset
	-- text
	alignment = { x = 1, y = 1 },
	scale = { x = 100, y = 100 },
	number = 0xFFFFFF,
}

function ch_core.show_player_list(player, online_charinfo)
	if online_charinfo.player_list_huds then
		return false
	end

	-- gether the list of players
	local items = {}
	for c_player_name, c_online_charinfo in pairs(ch_data.online_charinfo) do
		local c_offline_charinfo = ch_data.offline_charinfo[c_player_name]
		local titul = c_offline_charinfo and c_offline_charinfo.titul
		local dtituly = c_online_charinfo.docasne_tituly or {}

		if not titul then
			if string.sub(c_player_name, -2) == "PP" then
				titul = "pomocná postava"
			elseif not minetest.check_player_privs(c_player_name, "ch_registered_player") then
				titul = "nová postava"
			end
		end

		local zobrazovaci_jmeno = ch_core.prihlasovaci_na_zobrazovaci(c_player_name)
		local text = zobrazovaci_jmeno
		if titul then
			text = text.." ["..titul.."]"
		end
		for dtitul, _ in pairs(dtituly) do
			text = text.." ["..dtitul.."]"
		end
		table.insert(items, { name = zobrazovaci_jmeno, text = text })
	end

	-- sort the list
	table.sort(items, function(a, b) return ch_core.utf8_mensi_nez(a.name, b.name, true) end)

	local huds, new_hud
	local hud_defs = {}

	for _, item in ipairs(items) do
		new_hud = table.copy(text_hud_defaults)
		new_hud.offset = { x = 5, y = base_y_offset + 3 + #hud_defs * y_scale }
		new_hud.text = item.text
		table.insert(hud_defs, new_hud)
	end
	new_hud = {
		type = "image",
		alignment = { x = -1, y = 1 },
		position = { x = 1, y = 0 },
		offset = { x = 0, y = base_y_offset },
		text = "ch_core_white_pixel.png^[multiply:#333333^[opacity:128",
		scale = { x = -50, y = #hud_defs * y_scale + 8 },
		number = text_hud_defaults.number,
	}
	huds = {
		player:hud_add(new_hud)
	}
	online_charinfo.player_list_huds = huds
	for i, hud_def in ipairs(hud_defs) do
		huds[i + 1] = player:hud_add(hud_def)
	end
	return true
end

function ch_core.hide_player_list(player, online_charinfo)
	if not online_charinfo.player_list_huds then
		return false
	end
	local huds = online_charinfo.player_list_huds
	online_charinfo.player_list_huds = nil
	for _, hud_id in ipairs(huds) do
		player:hud_remove(hud_id)
	end
	return true
end

local has_hudbars = minetest.get_modpath("hudbars")

-- GAME TIME HUDBAR
if has_hudbars then
	local icon_day = "ch_core_slunce.png^[resize:20x20"
	local icon_night = "moon.png^[resize:20x20"
	local bar_day = "hudbars_bar_day.png"
	local bar_night = "hudbars_bar_night.png"

	hb.register_hudbar("ch_gametime", 0xCCCCCC, "", {icon = icon_day, bgicon = nil, bar = bar_day},
		0, 100, true, "@1 min.", {order = {"value"}, textdomain = "hudbars"})

	local function after_joinplayer(player_name)
		local player = minetest.get_player_by_name(player_name)
		if player ~= nil then
			local offline_charinfo = ch_data.offline_charinfo[player_name]
			if offline_charinfo ~= nil and offline_charinfo.skryt_zbyv ~= 1 then
				hb.unhide_hudbar(player, "ch_gametime")
				ch_core.update_gametime_hudbar({player})
			end
		end
	end

	local function on_joinplayer(player, last_login)
		hb.init_hudbar(player, "ch_gametime")
		minetest.after(1, after_joinplayer, player:get_player_name())
	end
	minetest.register_on_joinplayer(on_joinplayer)

	function ch_core.show_gametime_hudbar(player_name)
		local offline_charinfo = ch_data.offline_charinfo[player_name]
		if offline_charinfo ~= nil and offline_charinfo.skryt_zbyv ~= 0 then
			offline_charinfo.skryt_zbyv = 0
			ch_data.save_offline_charinfo(player_name)
			local player = minetest.get_player_by_name(player_name)
			if player ~= nil then
				hb.unhide_hudbar(player, "ch_gametime")
				ch_core.update_gametime_hudbar({player})
			end
			return true
		end
		return false
	end

	function ch_core.hide_gametime_hudbar(player_name)
		local offline_charinfo = ch_data.offline_charinfo[player_name]
		if offline_charinfo ~= nil and offline_charinfo.skryt_zbyv ~= 1 then
			offline_charinfo.skryt_zbyv = 1
			ch_data.save_offline_charinfo(player_name)
			local player = minetest.get_player_by_name(player_name)
			if player ~= nil then
				hb.hide_hudbar(player, "ch_gametime")
			end
			return true
		end
		return false
	end

	function ch_core.is_gametime_hudbar_shown(player_name)
		local offline_charinfo = ch_data.offline_charinfo[player_name]
		return offline_charinfo ~= nil and offline_charinfo.skryt_zbyv ~= 1
	end

	local cache_time_speed = 0
	local cache_is_night
	local cache_value = -1
	local dawn = 330
	local dusk = 1140

	function ch_core.update_gametime_hudbar(players, timeofday)
		local skip_cache = players ~= nil
		if players == nil then
			players = {}
			for _, player in ipairs(minetest.get_connected_players()) do
				local player_name = player:get_player_name()
				local offline_charinfo = ch_data.offline_charinfo[player_name] or {}
				if offline_charinfo.skryt_zbyv ~= 1 then
					table.insert(players, player)
				end
			end
		end
		if #players == 0 then
			return -- no players
		end

		local tod = timeofday or minetest.get_timeofday()
		if tod == nil then return end
		tod = tod * 1440 -- převést na počet herních minut
		local is_night = tod < dawn or tod >= dusk
		local time_speed = tonumber(minetest.settings:get("time_speed"))
		if time_speed == nil then
			minetest.log("warning", "[ch_core/hud] Cannot determine time_speed!")
			time_speed = 72
		end

		local new_value, new_max_value, new_icon, new_bar
		if is_night then
			new_value = math.ceil((ifthenelse(tod >= dusk, 1440 + dawn, dawn) - tod) / time_speed)
			if skip_cache or time_speed ~= cache_time_speed or cache_is_night ~= is_night then
				new_icon, new_bar = icon_night, bar_night
				new_max_value = math.ceil((1440 - (dusk - dawn)) / time_speed)
				cache_is_night, cache_time_speed = is_night, time_speed -- update cache
			end
		else
			new_value = math.ceil((dusk - tod) / time_speed)
			if skip_cache or time_speed ~= cache_time_speed or cache_is_night then
				new_icon, new_bar = icon_day, bar_day
				new_max_value = math.ceil((dusk - dawn) / time_speed)
				cache_is_night, cache_time_speed = is_night, time_speed -- update cache
			end
		end
		if skip_cache or new_value ~= cache_value then
			cache_value = new_value
		else
			if new_max_value == nil and new_icon == nil then
				return true -- nothing to update
			end
			new_value = nil -- don't update value if not necesarry
		end
		for _, player in ipairs(players) do
			hb.change_hudbar(player, "ch_gametime", new_value, new_max_value, new_icon, nil, new_bar)
		end
		return true
	end
else
	function ch_core.show_gametime_hudbar(player_name)
		return
	end

	function ch_core.hide_gametime_hudbar(player_name)
		return
	end

	function ch_core.update_gametime_hudbar(players, tod)
		return
	end

	function ch_core.is_gametime_hudbar_shown(player_name)
		return false
	end
end

-- CH_HUDBARS

if not has_hudbars then
	ch_core.count_of_ch_hudbars = 0
else
	ch_core.count_of_ch_hudbars = 2

	local hudbar_formatstring = "@1: @2"
	local hudbar_formatstring_config = {
		order = { "label", "value" },
		textdomain = "hudbars",
	}
	local hudbar_defaults = {
		icon = "default_snowball.png", bgicon = nil, bar = "hudbars_bar_timer.png"
	}
	for i = 1, ch_core.count_of_ch_hudbars, 1 do
		hb.register_hudbar("ch_hudbar_"..i, 0xFFFFFF, "x", hudbar_defaults, 0, 100, true, hudbar_formatstring, hudbar_formatstring_config)
	end
	minetest.register_on_joinplayer(function(player, last_login)
		for i = 1, ch_core.count_of_ch_hudbars, 1 do
			hb.init_hudbar(player, "ch_hudbar_"..i, 0, 100, true)
		end
	end)
end

function ch_core.try_alloc_hudbar(player)
	local online_charinfo = ch_data.online_charinfo[player:get_player_name()]
	if online_charinfo then
		local hudbars = online_charinfo.hudbars
		if not hudbars then
			hudbars = {}
			online_charinfo.hudbars = hudbars
		end
		for i = 1, ch_core.count_of_ch_hudbars, 1 do
			if not hudbars[i] then
				local result = "ch_hudbar_"..i
				hudbars[i] = result
				return result
			end
		end
	end
	return nil
end

function ch_core.free_hudbar(player, hudbar_id)
	local online_charinfo = ch_data.online_charinfo[player:get_player_name()]
	if not online_charinfo then
		minetest.log("warning", "Cannot get online_charinfo of player "..player:get_player_name().." to free a hudbar "..hudbar_id.."!")
		return false
	end
	local hudbars = online_charinfo.hudbars
	if not hudbars then
		hudbars = {}
		online_charinfo.hudbars = hudbars
	end

	if hudbar_id:sub(1, 10) == "ch_hudbar_" then
		local hudbar_index = tonumber(hudbar_id:sub(11, -1))
		if 1 <= hudbar_index and hudbar_index <= ch_core.count_of_ch_hudbars then
			if hudbars[hudbar_index] then
				hudbars[hudbar_index] = nil
				hb.hide_hudbar(player, hudbar_id)
				return true
			else
				return false -- not allocated
			end
		end
	end
	minetest.log("error", "Invalid hudbar_id to free: "..hudbar_id)
	return false
end

-- DATE AND TIME HUD
local datetime_hud_defaults = {
	type = "text",
	position = { x = 1, y = 1 },
	offset = { x = -5, y = -5 },
	text = "",
	alignment = { x = -1, y = -1 },
	scale = { x = 100, y = 100 },
	number = 0x999999,
	style = 2,
	z_index = 50,
}

local datetime_huds = {
}

local function on_joinplayer(player, last_login)
	local player_name = player:get_player_name()
	datetime_huds[player_name] = {
		counter = 0,
		hud_id = player:hud_add(datetime_hud_defaults),
		hud_text = "",
	}
end

local function on_leaveplayer(player, timed_out)
	local player_name = player:get_player_name()
	datetime_huds[player_name] = nil
end

local acc_time = 0

local function on_step(dtime)
	acc_time = acc_time + dtime
	if acc_time > 0.5 then
		local cas = ch_time.aktualni_cas()
		local text = string.format("%s\n%d. %s %s\n%02d:%02d %s",
			cas:den_v_tydnu_nazev(), cas.den, cas:nazev_mesice(2), cas.rok, cas.hodina, cas.minuta, cas:posun_text())
		for player_name, record in pairs(datetime_huds) do
			local player = minetest.get_player_by_name(player_name)
			if player ~= nil then
				local ltext = ch_core.prihlasovaci_na_zobrazovaci(player_name).."\n"..text
				if record.hud_text ~= ltext then
					record.hud_text = ltext
					ltext = ltext.." ["..record.counter.."]"
					player:hud_change(record.hud_id, "text", ltext)
					record.counter = record.counter + 1
				end
			end
		end
	end
end

function ch_core.clear_datetime_hud(player)
	local player_name = player:get_player_name()
	local hud = datetime_huds[player_name]
	if hud then
		player:hud_remove(hud.hud_id)
		datetime_huds[player_name] = nil
		return true
	else
		return false
	end
end

local function clear_datetime_hud(player_name, param)
	local player = minetest.get_player_by_name(player_name)
	if player ~= nil and ch_core.clear_datetime_hud(player) then
		return true
	else
		ch_core.systemovy_kanal(player_name, "CHYBA: okno s datem a časem nenalezeno, možná už je skyto.")
	end
end

local cc_def = {
	description = "Do odhlášení skryje z obrazovky datum a čas.",
	func = clear_datetime_hud,
}

minetest.register_on_joinplayer(on_joinplayer)
minetest.register_on_leaveplayer(on_leaveplayer)
minetest.register_globalstep(on_step)
minetest.register_chatcommand("skrýtčas", cc_def)
minetest.register_chatcommand("skrytcas", cc_def)

ch_core.close_submod("hud")