Package list minetest-mod-character-creator / 46474a72-84e7-47e1-a9ec-f8f66d5d1bc0/main init.lua
46474a72-84e7-47e1-a9ec-f8f66d5d1bc0/main

Tree @46474a72-84e7-47e1-a9ec-f8f66d5d1bc0/main (Download .tar.gz)

init.lua @46474a72-84e7-47e1-a9ec-f8f66d5d1bc0/mainraw · history · blame

  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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
character_creator = {}
character_creator.skins = dofile(minetest.get_modpath("character_creator") .. "/skins.lua")

local skinsdb
if minetest.get_modpath("skinsdb") and minetest.global_exists("skins") then
	skinsdb = skins
end

local skin_default = {
	gender     = "Male",
	height     = 1,
	width      = 1,

	skin       = "Fair Skin",
	face       = "Human Face",
	hair       = "Brown Hair",
	hair_style = "Medium Hair",
	eyes       = "Blue Eyes",
	tshirt     = "Green T-Shirt",
	pants      = "Blue Pants",
	shoes      = "Leather Shoes"
}

local skins = character_creator.skins
local skins_array = {}

minetest.after(0, function()
	local function associative_to_array(associative)
		local array = {}
		for key in pairs(associative) do
			table.insert(array, key)
		end
		return array
	end

	skins_array = {
		skin       = associative_to_array(skins.skin),
		face       = associative_to_array(skins.face),
		hair       = associative_to_array(skins.hair),
		hair_style = associative_to_array(skins.hair_style),
		eyes       = associative_to_array(skins.eyes),
		tshirt     = associative_to_array(skins.tshirt),
		pants      = associative_to_array(skins.pants),
		shoes      = associative_to_array(skins.shoes)
	}
end)

-- Saved skins_array indexes in this
local skin_indexes = {}

local function show_formspec(player)
	local indexes = skin_indexes[player]

	local formspec = "size[15,9.5]"
		.. "no_prepend[]"
		.. "bgcolor[#00000000]"
		-- Gender
		.. "button[10,;2.5,.5;male;Male]"
		.. "button[12.5,;2.5,.5;female;Female]"
		-- Height
		.. "button[10,1.1;2.5,.5;taller;Taller]"
		.. "button[10,2;2.5,.5;shorter;Shorter]"
		-- Width
		.. "button[12.5,1.1;2.5,.5;wider;Wider]"
		.. "button[12.5,2;2.5,.5;thinner;Thinner]"
		-- Skin
		.. "button[10,2.75;5,1;skin;" .. skins_array.skin[indexes.skin] .. "]"
		.. "button[10,2.75;1,1;skin_back;<<]"
		.. "button[14,2.75;1,1;skin_next;>>]"
		-- Face
		.. "button[10,3.5;5,1;face;" .. skins_array.face[indexes.face] .. "]"
		.. "button[10,3.5;1,1;face_back;<<]"
		.. "button[14,3.5;1,1;face_next;>>]"
		-- Hair
		.. "button[10,4.25;5,1;hair;" .. skins_array.hair[indexes.hair] .. "]"
		.. "button[10,4.25;1,1;hair_back;<<]"
		.. "button[14,4.25;1,1;hair_next;>>]"
		-- Hair Style
		.. "button[10,5;5,1;hair_style;" .. skins_array.hair_style[indexes.hair_style] .. "]"
		.. "button[10,5;1,1;hair_style_back;<<]"
		.. "button[14,5;1,1;hair_style_next;>>]"
		-- Eyes
		.. "button[10,5.75;5,1;eyes;" .. skins_array.eyes[indexes.eyes] .. "]"
		.. "button[10,5.75;1,1;eyes_back;<<]"
		.. "button[14,5.75;1,1;eyes_next;>>]"
		-- T-Shirt
		.. "button[10,6.5;5,1;tshirt;" .. skins_array.tshirt[indexes.tshirt] .. "]"
		.. "button[10,6.5;1,1;tshirt_back;<<]"
		.. "button[14,6.5;1,1;tshirt_next;>>]"
		-- Pants
		.. "button[10,7.25;5,1;pants;" .. skins_array.pants[indexes.pants] .. "]"
		.. "button[10,7.25;1,1;pants_back;<<]"
		.. "button[14,7.25;1,1;pants_next;>>]"
		-- Shoes
		.. "button[10,8;5,1;shoes;" .. skins_array.shoes[indexes.shoes] .. "]"
		.. "button[10,8;1,1;shoes_back;<<]"
		.. "button[14,8;1,1;shoes_next;>>]"
		-- Done
		.. "button_exit[10,9;2.5,.5;done;Done]"
		.. "button_exit[12.5,9;2.5,.5;cancel;Cancel]"

	minetest.show_formspec(player:get_player_name(), "character_creator", formspec)
end

local function load_skin(player)
	skin_indexes[player] = {}

	local player_meta = player:get_meta()

	if not player_meta:contains("character_creator:gender") then
		player_meta:set_string("character_creator:gender", skin_default.gender)
	end

	if not player_meta:contains("character_creator:width") then
		player_meta:set_float("character_creator:width", skin_default.width)
	end

	if not player_meta:contains("character_creator:height") then
		player_meta:set_float("character_creator:height", skin_default.height)
	end

	local function load_data(data_name)
		local key   = player_meta:get_string("character_creator:" .. data_name)
		local index = table.indexof(skins_array[data_name], key)
		if index == -1 then
			index = table.indexof(skins_array[data_name], skin_default[data_name])
		end

		local indexes = skin_indexes[player]
		indexes[data_name] = index
	end

	load_data("skin")
	load_data("face")
	load_data("eyes")
	load_data("hair_style")
	load_data("hair")
	load_data("tshirt")
	load_data("pants")
	load_data("shoes")
end

local function save_skin(player)
	local player_meta = player:get_meta()

	local function save_data(data_name)
		local indexes = skin_indexes[player]
		local index   = indexes[data_name]
		local key     = skins_array[data_name][index]
		player_meta:set_string("character_creator:" .. data_name, key)
	end

	save_data("skin")
	save_data("face")
	save_data("eyes")
	save_data("hair_style")
	save_data("hair")
	save_data("tshirt")
	save_data("pants")
	save_data("shoes")
end

local function get_texture(player)
	local player_meta = player:get_meta()
	local indexes = skin_indexes[player]
	local texture = ""
	local gender = player_meta:get_string("character_creator:gender")

	local skin_key = skins_array.skin[indexes.skin]
	local skin = skins.skin[skin_key]
	texture = texture .. "(" .. skin .. ")"

	local face_key = skins_array.face[indexes.face]
	local face = skins.face[face_key][gender][skin_key]
	texture = texture .. "^(" .. face .. ")"

	local eyes_key = skins_array.eyes[indexes.eyes]
	local eyes = skins.eyes[eyes_key]
	texture = texture .. "^(" .. eyes .. ")"

	local hair_style = skins_array.hair_style[indexes.hair_style]
	local hair_key = skins_array.hair[indexes.hair]
	local hair = skins.hair[hair_key][gender][hair_style]
	texture = texture .. "^(" .. hair .. ")"

	local tshirt_key = skins_array.tshirt[indexes.tshirt]
	local tshirt = skins.tshirt[tshirt_key]
	texture = texture .. "^(" .. tshirt .. ")"

	local pants_key = skins_array.pants[indexes.pants]
	local pants = skins.pants[pants_key]
	texture = texture .. "^(" .. pants .. ")"

	local shoes_key = skins_array.shoes[indexes.shoes]
	local shoes = skins.shoes[shoes_key]
	texture = texture .. "^(" .. shoes .. ")"
	return texture
end

local function change_skin(player)
	local player_meta = player:get_meta()
	local texture = get_texture(player)

	local width  = player_meta:get_float("character_creator:width")
	local height = player_meta:get_float("character_creator:height")

	player:set_properties({
		visual_size = {
			x = width,
			y = height
		}
	})

	local name = player:get_player_name()

	if minetest.get_modpath("multiskin") then
		multiskin.layers[name].skin = texture
		armor:set_player_armor(player)
		multiskin:set_player_textures(player, {textures = {texture}})
	elseif minetest.get_modpath("3d_armor") then
		armor.textures[name].skin = texture
		armor:set_player_armor(player)
	else
		player:set_properties({textures = {texture}})
	end

	save_skin(player)
end

if skinsdb then
	--change skin redefinition for skinsdb
	function change_skin(player)
		local player_meta = player:get_meta()
		local playername = player:get_player_name()
		local skinname = "character_creator:"..playername
		local skin_obj = skinsdb.get(skinname) or skinsdb.new(skinname)
		skin_obj:set_meta("format", "1.0")
		skin_obj:set_meta("visual_size_x", player_meta:get_float("character_creator:width"))
		skin_obj:set_meta("visual_size_y", player_meta:get_float("character_creator:height"))
		skin_obj:apply_skin_to_player(player)
		skinsdb.assign_player_skin(player, "character_creator:"..playername)
		save_skin(player)
	end
end

minetest.register_on_joinplayer(function(player)
	load_skin(player)
	if skinsdb then
		local playername = player:get_player_name()
		local skinname = "character_creator:"..playername
		local skin_obj = skinsdb.get(skinname) or skinsdb.new(skinname)
		-- redefinitions
		function skin_obj:set_skin(player)
			if not player or not skin_indexes[player] then
				return -- not loaded or disconnected
			end
			change_skin(player)
			show_formspec(player)
		end
		function skin_obj:get_texture()
			return get_texture(minetest.get_player_by_name(self:get_meta("playername")))
		end

		-- set data
		skin_obj:set_preview("inventory_plus_character_creator.png")
		skin_obj:set_meta("name","Character Creator")
		--skin_obj:set_meta("author", "???")
		skin_obj:set_meta("license", "MIT / CC-BY-SA 3.0 Unported")
		skin_obj:set_meta("playername",playername)
		--check if active and start the update (avoid race condition for both register_on_joinplayer)
		if skinsdb.get_player_skin(player):get_key() == skinname then
			minetest.after(0, change_skin, player)
		end
	else
		minetest.after(0, change_skin, player)
	end
end)

minetest.register_on_leaveplayer(function(player)
	if skinsdb then
		local skinname = "character_creator:"..player:get_player_name()
		skinsdb.meta[skinname] = nil
	end
	skin_indexes[player] = nil
end)

local skin_temp = {}
minetest.register_on_player_receive_fields(function(player, formname, fields)
	if formname ~= "character_creator" then
		return
	end

	local player_meta = player:get_meta()
	local indexes = skin_indexes[player]

	if not skin_temp[player] then
		skin_temp[player] = {
			gender = player_meta:get_string("character_creator:gender"),
			width = player_meta:get_float("character_creator:width"),
			height = player_meta:get_float("character_creator:height"),
			indexes = table.copy(indexes)
		}
	end

	-- Gender
	do
		if fields.male then
			player_meta:set_string("character_creator:gender", "Male")
			player_meta:set_float("character_creator:width", 1)
			player_meta:set_float("character_creator:height", 1)
		end

		if fields.female then
			player_meta:set_string("character_creator:gender", "Female")
			player_meta:set_float("character_creator:width", 0.95)
			player_meta:set_float("character_creator:height", 1)
		end
	end

	-- Height
	do
		local height = tonumber(player_meta:get_float("character_creator:height"))

		if fields.taller and height < 1.25 then
			player_meta:set_float("character_creator:height", height + 0.05)
		end

		if fields.shorter and height > 0.75 then
			player_meta:set_float("character_creator:height", height - 0.05)
		end
	end

	-- Width
	do
		local width = tonumber(player_meta:get_float("character_creator:width"))

		if fields.wider and width < 1.25 then
			player_meta:set_float("character_creator:width", width + 0.05)
		end

		if fields.thinner and width > 0.75 then
			player_meta:set_float("character_creator:width", width - 0.05)
		end
	end

	-- Switch skin
	do
		local function switch_skin(data_name, next_index)
			if not indexes[data_name]
					or not skins_array[data_name] then
				return -- Supplied invalid data
			end

			local index = indexes[data_name] + next_index
			local max = #skins_array[data_name]

			if index == 0 then
				index = max
			elseif index == (max + 1) then
				index = 1
			end

			indexes[data_name] = index
		end

		for field in pairs(fields) do
			if field:find("_back$") then
				local data_name = field:match("(.+)_back$")
				switch_skin(data_name, -1)
			elseif field:find("_next$") then
				local data_name = field:match("(.+)_next$")
				switch_skin(data_name, 1)
			end
		end
	end

	-- Close or update
	do
		local quit = false

		if fields.cancel then
			local temp = skin_temp[player]
			player_meta:set_string("character_creator:gender", temp.gender)
			player_meta:set_float("character_creator:width", temp.width)
			player_meta:set_float("character_creator:height", temp.height)
			skin_indexes[player] = table.copy(temp.indexes)
			skin_temp[player] = nil
			quit = true
		elseif fields.quit then
			skin_temp[player] = nil
			quit = true
		end

		if not quit then
			show_formspec(player)
		end
	end
	change_skin(player)
end)

minetest.register_chatcommand("character_creator", {
	func = function(name)
		minetest.after(0.5, function()
			local player = minetest.get_player_by_name(name)
			if player then
				show_formspec(player)
			end
		end)
	end
})

if minetest.global_exists("unified_inventory") then
	unified_inventory.register_button("character_creator", {
		type   = "image",
		image  = "inventory_plus_character_creator.png",
		action = show_formspec
	})
elseif minetest.global_exists("inventory_plus") then
	minetest.register_on_joinplayer(function(player)
		inventory_plus.register_button(player, "character_creator", "Character Creator")
	end)
	minetest.register_on_player_receive_fields(function(player, _, fields)
		if fields.character_creator then
			show_formspec(player)
		end
	end)
elseif not skinsdb and minetest.get_modpath("sfinv_buttons") then
	sfinv_buttons.register_button("character_creator", {
		image = "inventory_plus_character_creator.png",
		title = "Character Creator",
		action = show_formspec,
	})
end