Package list minetest-mod-character-creator / f7dc840
New upstream version 1.1 Julien Puydt 1 year, 10 months ago
3 changed file(s) with 156 addition(s) and 75 deletion(s). Raw diff Collapse all Expand all
00 3d_armor?
11 multiskin?
22 inventory_plus?
3 unified_inventory?
3 unified_inventory?
4 skinsdb?
5 sfinv_buttons?
00 character_creator = {}
11 character_creator.skins = dofile(minetest.get_modpath("character_creator") .. "/skins.lua")
2
3 local skinsdb
4 if minetest.get_modpath("skinsdb") and minetest.global_exists("skins") then
5 skinsdb = skins
6 end
27
38 local skin_default = {
49 gender = "Male",
4651 local indexes = skin_indexes[player]
4752
4853 local formspec = "size[15,9.5]"
54 .. "no_prepend[]"
4955 .. "bgcolor[#00000000]"
5056 -- Gender
5157 .. "button[10,;2.5,.5;male;Male]"
98104 local function load_skin(player)
99105 skin_indexes[player] = {}
100106
101 if not player:get_attribute("character_creator:gender") then
102 player:set_attribute("character_creator:gender", skin_default.gender)
103 end
104
105 if not player:get_attribute("character_creator:width") then
106 player:set_attribute("character_creator:width", skin_default.width)
107 end
108
109 if not player:get_attribute("character_creator:height") then
110 player:set_attribute("character_creator:height", skin_default.height)
107 local player_meta = player:get_meta()
108
109 if not player_meta:contains("character_creator:gender") then
110 player_meta:set_string("character_creator:gender", skin_default.gender)
111 end
112
113 if not player_meta:contains("character_creator:width") then
114 player_meta:set_float("character_creator:width", skin_default.width)
115 end
116
117 if not player_meta:contains("character_creator:height") then
118 player_meta:set_float("character_creator:height", skin_default.height)
111119 end
112120
113121 local function load_data(data_name)
114 local key = player:get_attribute("character_creator:" .. data_name)
122 local key = player_meta:get_string("character_creator:" .. data_name)
115123 local index = table.indexof(skins_array[data_name], key)
116124 if index == -1 then
117125 index = table.indexof(skins_array[data_name], skin_default[data_name])
132140 end
133141
134142 local function save_skin(player)
143 local player_meta = player:get_meta()
144
135145 local function save_data(data_name)
136146 local indexes = skin_indexes[player]
137147 local index = indexes[data_name]
138148 local key = skins_array[data_name][index]
139 player:set_attribute("character_creator:" .. data_name, key)
149 player_meta:set_string("character_creator:" .. data_name, key)
140150 end
141151
142152 save_data("skin")
149159 save_data("shoes")
150160 end
151161
162 local function get_texture(player)
163 local player_meta = player:get_meta()
164 local indexes = skin_indexes[player]
165 local texture = ""
166 local gender = player_meta:get_string("character_creator:gender")
167
168 local skin_key = skins_array.skin[indexes.skin]
169 local skin = skins.skin[skin_key]
170 texture = texture .. "(" .. skin .. ")"
171
172 local face_key = skins_array.face[indexes.face]
173 local face = skins.face[face_key][gender][skin_key]
174 texture = texture .. "^(" .. face .. ")"
175
176 local eyes_key = skins_array.eyes[indexes.eyes]
177 local eyes = skins.eyes[eyes_key]
178 texture = texture .. "^(" .. eyes .. ")"
179
180 local hair_style = skins_array.hair_style[indexes.hair_style]
181 local hair_key = skins_array.hair[indexes.hair]
182 local hair = skins.hair[hair_key][gender][hair_style]
183 texture = texture .. "^(" .. hair .. ")"
184
185 local tshirt_key = skins_array.tshirt[indexes.tshirt]
186 local tshirt = skins.tshirt[tshirt_key]
187 texture = texture .. "^(" .. tshirt .. ")"
188
189 local pants_key = skins_array.pants[indexes.pants]
190 local pants = skins.pants[pants_key]
191 texture = texture .. "^(" .. pants .. ")"
192
193 local shoes_key = skins_array.shoes[indexes.shoes]
194 local shoes = skins.shoes[shoes_key]
195 texture = texture .. "^(" .. shoes .. ")"
196 return texture
197 end
198
152199 local function change_skin(player)
153 local indexes = skin_indexes[player]
154
155 local texture = (function()
156 local texture = ""
157 local gender = player:get_attribute("character_creator:gender")
158
159 local skin_key = skins_array.skin[indexes.skin]
160 local skin = skins.skin[skin_key]
161 texture = texture .. skin
162
163 local face_key = skins_array.face[indexes.face]
164 local face = skins.face[face_key][gender][skin_key]
165 texture = texture .. "^" .. face
166
167 local eyes_key = skins_array.eyes[indexes.eyes]
168 local eyes = skins.eyes[eyes_key]
169 texture = texture .. "^" .. eyes
170
171 local hair_style = skins_array.hair_style[indexes.hair_style]
172 local hair_key = skins_array.hair[indexes.hair]
173 local hair = skins.hair[hair_key][gender][hair_style]
174 texture = texture .. "^" .. hair
175
176 local tshirt_key = skins_array.tshirt[indexes.tshirt]
177 local tshirt = skins.tshirt[tshirt_key]
178 texture = texture .. "^" .. tshirt
179
180 local pants_key = skins_array.pants[indexes.pants]
181 local pants = skins.pants[pants_key]
182 texture = texture .. "^" .. pants
183
184 local shoes_key = skins_array.shoes[indexes.shoes]
185 local shoes = skins.shoes[shoes_key]
186 texture = texture .. "^" .. shoes
187
188 return texture
189 end)()
190
191 local width = tonumber(player:get_attribute("character_creator:width"))
192 local height = tonumber(player:get_attribute("character_creator:height"))
200 local player_meta = player:get_meta()
201 local texture = get_texture(player)
202
203 local width = player_meta:get_float("character_creator:width")
204 local height = player_meta:get_float("character_creator:height")
193205
194206 player:set_properties({
195207 visual_size = {
214226 save_skin(player)
215227 end
216228
229 if skinsdb then
230 --change skin redefinition for skinsdb
231 function change_skin(player)
232 local player_meta = player:get_meta()
233 local playername = player:get_player_name()
234 local skinname = "character_creator:"..playername
235 local skin_obj = skinsdb.get(skinname) or skinsdb.new(skinname)
236 skin_obj:set_meta("format", "1.0")
237 skin_obj:set_meta("visual_size_x", player_meta:get_float("character_creator:width"))
238 skin_obj:set_meta("visual_size_y", player_meta:get_float("character_creator:height"))
239 skin_obj:apply_skin_to_player(player)
240 skinsdb.assign_player_skin(player, "character_creator:"..playername)
241 save_skin(player)
242 end
243 end
244
217245 minetest.register_on_joinplayer(function(player)
218246 load_skin(player)
219 minetest.after(0, change_skin, player)
247 if skinsdb then
248 local playername = player:get_player_name()
249 local skinname = "character_creator:"..playername
250 local skin_obj = skinsdb.get(skinname) or skinsdb.new(skinname)
251 -- redefinitions
252 function skin_obj:set_skin(player)
253 if not player or not skin_indexes[player] then
254 return -- not loaded or disconnected
255 end
256 change_skin(player)
257 show_formspec(player)
258 end
259 function skin_obj:get_texture()
260 return get_texture(minetest.get_player_by_name(self:get_meta("playername")))
261 end
262
263 -- set data
264 skin_obj:set_preview("inventory_plus_character_creator.png")
265 skin_obj:set_meta("name","Character Creator")
266 --skin_obj:set_meta("author", "???")
267 skin_obj:set_meta("license", "MIT / CC-BY-SA 3.0 Unported")
268 skin_obj:set_meta("playername",playername)
269 --check if active and start the update (avoid race condition for both register_on_joinplayer)
270 if skinsdb.get_player_skin(player):get_key() == skinname then
271 minetest.after(0, change_skin, player)
272 end
273 else
274 minetest.after(0, change_skin, player)
275 end
276 end)
277
278 minetest.register_on_leaveplayer(function(player)
279 if skinsdb then
280 local skinname = "character_creator:"..player:get_player_name()
281 skinsdb.meta[skinname] = nil
282 end
283 skin_indexes[player] = nil
220284 end)
221285
222286 local skin_temp = {}
225289 return
226290 end
227291
292 local player_meta = player:get_meta()
228293 local indexes = skin_indexes[player]
294
229295 if not skin_temp[player] then
230296 skin_temp[player] = {
231 gender = player:get_attribute("character_creator:gender"),
232 width = player:get_attribute("character_creator:width"),
233 height = player:get_attribute("character_creator:height"),
297 gender = player_meta:get_string("character_creator:gender"),
298 width = player_meta:get_float("character_creator:width"),
299 height = player_meta:get_float("character_creator:height"),
234300 indexes = table.copy(indexes)
235301 }
236302 end
238304 -- Gender
239305 do
240306 if fields.male then
241 player:set_attribute("character_creator:gender", "Male")
242 player:set_attribute("character_creator:width", 1)
243 player:set_attribute("character_creator:height", 1)
307 player_meta:set_string("character_creator:gender", "Male")
308 player_meta:set_float("character_creator:width", 1)
309 player_meta:set_float("character_creator:height", 1)
244310 end
245311
246312 if fields.female then
247 player:set_attribute("character_creator:gender", "Female")
248 player:set_attribute("character_creator:width", 0.95)
249 player:set_attribute("character_creator:height", 1)
313 player_meta:set_string("character_creator:gender", "Female")
314 player_meta:set_float("character_creator:width", 0.95)
315 player_meta:set_float("character_creator:height", 1)
250316 end
251317 end
252318
253319 -- Height
254320 do
255 local height = tonumber(player:get_attribute("character_creator:height"))
321 local height = tonumber(player_meta:get_float("character_creator:height"))
256322
257323 if fields.taller and height < 1.25 then
258 player:set_attribute("character_creator:height", height + 0.05)
324 player_meta:set_float("character_creator:height", height + 0.05)
259325 end
260326
261327 if fields.shorter and height > 0.75 then
262 player:set_attribute("character_creator:height", height - 0.05)
328 player_meta:set_float("character_creator:height", height - 0.05)
263329 end
264330 end
265331
266332 -- Width
267333 do
268 local width = tonumber(player:get_attribute("character_creator:width"))
334 local width = tonumber(player_meta:get_float("character_creator:width"))
269335
270336 if fields.wider and width < 1.25 then
271 player:set_attribute("character_creator:width", width + 0.05)
337 player_meta:set_float("character_creator:width", width + 0.05)
272338 end
273339
274340 if fields.thinner and width > 0.75 then
275 player:set_attribute("character_creator:width", width - 0.05)
341 player_meta:set_float("character_creator:width", width - 0.05)
276342 end
277343 end
278344
279345 -- Switch skin
280346 do
281347 local function switch_skin(data_name, next_index)
348 if not indexes[data_name]
349 or not skins_array[data_name] then
350 return -- Supplied invalid data
351 end
352
282353 local index = indexes[data_name] + next_index
283354 local max = #skins_array[data_name]
284355
308379
309380 if fields.cancel then
310381 local temp = skin_temp[player]
311 player:set_attribute("character_creator:gender", temp.gender)
312 player:set_attribute("character_creator:width", temp.width)
313 player:set_attribute("character_creator:height", temp.height)
382 player_meta:set_string("character_creator:gender", temp.gender)
383 player_meta:set_float("character_creator:width", temp.width)
384 player_meta:set_float("character_creator:height", temp.height)
314385 skin_indexes[player] = table.copy(temp.indexes)
315386 skin_temp[player] = nil
316387 quit = true
323394 show_formspec(player)
324395 end
325396 end
326
327397 change_skin(player)
328398 end)
329399
353423 show_formspec(player)
354424 end
355425 end)
356 end
426 elseif not skinsdb and minetest.get_modpath("sfinv_buttons") then
427 sfinv_buttons.register_button("character_creator", {
428 image = "inventory_plus_character_creator.png",
429 title = "Character Creator",
430 action = show_formspec,
431 })
432 end
0 name = character_creator
0 name = character_creator
1 description = Allows the creation of customized character skins inside the game.
2 license = MIT
3 optional_depends = 3d_armor, multiskin, inventory_plus, unified_inventory, skinsdb, sfinv_buttons