From 952ed84733de5ee2a1796c3eaca3ea5be77c3991 Mon Sep 17 00:00:00 2001 From: Sokomine Date: Sun, 25 Jul 2021 23:47:45 +0200 Subject: [PATCH] moved skin functions etc. into fs_fashion.lua --- fs_fashion.lua | 352 +++++++++++++++++++++++++++++++++++++++++++++++++ functions.lua | 352 ------------------------------------------------- init.lua | 2 + 3 files changed, 354 insertions(+), 352 deletions(-) create mode 100644 fs_fashion.lua diff --git a/fs_fashion.lua b/fs_fashion.lua new file mode 100644 index 0000000..1f46135 --- /dev/null +++ b/fs_fashion.lua @@ -0,0 +1,352 @@ +-- ### +-- Fashion +-- ### + +local function cape2texture(t) + return "yl_speak_up_mask_cape.png^[combine:32x64:56,20=" .. t +end + +local function shield2texture(t) + return "yl_speak_up_mask_shield.png^[combine:32x64:0,0=(" .. t .. ")" +end + +local function textures2skin(textures) + local temp = {} + -- Cape + + local cape = cape2texture(textures[1]) + + -- Main + + local main = textures[2] + + -- left (Shield) + + local left = shield2texture(textures[3]) + + -- right (Sword) + + local right = textures[4] + + temp = {cape, main, left, right} + + return temp +end + +local function set_textures(obj, textures) -- this function takes the base name of the textures, converts them to usable textures and stores those on the NPC + local skins = textures2skin(textures) + + local ent = obj:get_luaentity() + ent.yl_speak_up.skin = skins + + obj:set_properties({textures = skins}) +end + +local function get_npc_skins(skin) + local retstring = "yl_speak_up_main_default.png" + local rettable = {"yl_speak_up_main_default.png"} + local temp = {} + + -- get the files out of modpath + local mp_list = minetest.get_dir_list(yl_speak_up.modpath .. DIR_DELIM .. "textures", false) + + -- get the files out of worlddir + local wp_list = + minetest.get_dir_list( + yl_speak_up.worldpath .. DIR_DELIM .. "worldmods" .. DIR_DELIM .. "yl_npc" .. DIR_DELIM .. "textures", + false + ) + + -- Let's join both lists. + table.insert_all(temp, mp_list) + table.insert_all(temp, wp_list) + + --[[ Let's see if the files are the ones we want. Format is + yl_npc_main_name.png <-- Those are the ones we want + yl_npc_cape_name.png + yl_npc_item_name.png + ]]-- + local index = 1 + local retindex = 1 + + for _, v in pairs(temp) do + local s = string.split(v, "_") + if s[1] == "yl" and s[2] == "npc" and s[3] == "main" then + index = index + 1 + retstring = retstring .. "," .. v + table.insert(rettable, v) + if v == skin then + retindex = index + end + end + end + + return rettable, retstring, retindex +end + +local function get_npc_capes(cape) + local retstring = "yl_npc_cape_default.png" + local rettable = {"yl_npc_cape_default.png"} + local temp = {} + + -- get the files out of modpath + local mp_list = minetest.get_dir_list(yl_speak_up.modpath .. DIR_DELIM .. "textures", false) + + -- get the files out of worlddir + local wp_list = + minetest.get_dir_list( + yl_speak_up.worldpath .. DIR_DELIM .. "worldmods" .. DIR_DELIM .. "yl_npc" .. DIR_DELIM .. "textures", + false + ) + + -- Let's join both lists. + table.insert_all(temp, mp_list) + table.insert_all(temp, wp_list) + + --[[ Let's see if the files are the ones we want. Format is + yl_npc_main_name.png + yl_npc_cape_name.png <-- Those are the ones we want + yl_npc_item_name.png + ]]-- + local index = 1 + local retindex = 1 + + for _, v in pairs(temp) do + local s = string.split(v, "_") + + if s[1] == "yl" and s[2] == "npc" and s[3] == "cape" then + index = index + 1 + retstring = retstring .. "," .. v + table.insert(rettable, v) + if cape ~= "" and v == cape then + retindex = index + end + end + end + + return rettable, retstring, retindex +end + +local function create_preview(main_skin) + if main_skin == nil or main_skin == "" then + main_skin = "default_greyscale.png" + end + + local player_skin = "(" .. main_skin .. ")" + local skin = "" + + -- Consistent on both sizes: + --Chest + skin = skin .. "([combine:16x32:-16,-12=" .. player_skin .. "^[mask:yl_speak_up_mask_chest.png)^" + + --Head + skin = skin .. "([combine:16x32:-4,-8=" .. player_skin .. "^[mask:yl_speak_up_mask_head.png)^" + + --Hat + skin = skin .. "([combine:16x32:-36,-8=" .. player_skin .. "^[mask:yl_speak_up_mask_head.png)^" + --Right Arm + skin = skin .. "([combine:16x32:-44,-12=" .. player_skin .. "^[mask:yl_speak_up_mask_rarm.png)^" + --Right Leg + skin = skin .. "([combine:16x32:0,0=" .. player_skin .. "^[mask:yl_speak_up_mask_rleg.png)^" + + -- Left Arm + skin = skin .. "([combine:16x32:-24,-44=" .. player_skin .. "^[mask:(yl_speak_up_mask_rarm.png^[transformFX))^" + --Left Leg + skin = skin .. "([combine:16x32:-12,-32=" .. player_skin .. "^[mask:(yl_speak_up_mask_rleg.png^[transformFX))^" + + -- Add overlays for 64x skins. these wont appear if skin is 32x because it will be cropped out + --Chest Overlay + skin = skin .. "([combine:16x32:-16,-28=" .. player_skin .. "^[mask:yl_speak_up_mask_chest.png)^" + --Right Arm Overlay + skin = skin .. "([combine:16x32:-44,-28=" .. player_skin .. "^[mask:yl_speak_up_mask_rarm.png)^" + --Right Leg Overlay + skin = skin .. "([combine:16x32:0,-16=" .. player_skin .. "^[mask:yl_speak_up_mask_rleg.png)^" + --Left Arm Overlay + skin = skin .. "([combine:16x32:-40,-44=" .. player_skin .. "^[mask:(yl_speak_up_mask_rarm.png^[transformFX))^" + --Left Leg Overlay + skin = skin .. "([combine:16x32:4,-32=" .. player_skin .. "^[mask:(yl_speak_up_mask_rleg.png^[transformFX))" + + -- Full Preview + skin = "(((" .. skin .. ")^[resize:64x128)^[mask:yl_speak_up_transform.png)" + + return skin +end + +yl_speak_up.get_fs_fashion = function(pname) + local textures = yl_speak_up.speak_to[pname].textures + + local maintable, mainlist, mainindex = get_npc_skins(textures[2]) + local capetable, capelist, capeindex = get_npc_capes(textures[1]) + local preview = create_preview(textures[2]) + + local button_cancel = "Cancel" + -- is this player editing this particular NPC? then rename the button + if( yl_speak_up.edit_mode[pname] + and yl_speak_up.edit_mode[pname] == yl_speak_up.speak_to[pname].n_id) then + button_cancel = "Back" + end + local formspec = { + "formspec_version[3]", + "size[13.4,9.5]", + "dropdown[0.3,0.2;4,0.75;set_skin;", + mainlist, + ";", + mainindex, + "]", + "label[4.6,0.45;", + yl_speak_up.speak_to[pname].n_id, + "]", + "label[6,0.45;", + yl_speak_up.speak_to[pname].n_npc, + "]", + "dropdown[9.1,0.2;4,0.75;set_cape;", + capelist, + ";", + capeindex, + "]", + "field[0.3,3.2;4,0.75;set_sword;;", + textures[4], + "]", + "field[9.1,3.2;4,0.75;set_shield;;", + textures[3], + "]", + "field_close_on_enter[set_sword;false]", + "field_close_on_enter[set_shield;false]", + "image[0.3,1;4,2;", + textures[2], + "]", -- Main + "image[9.1,1;4,2;", + textures[1], + "]", -- Cape + "image[0.3,4.2;4,4;", + textures[4], + "]", -- Sword + "image[9.1,4.2;4,4;", + textures[3], + "]", --textures[3],"]", -- Shield + "image[4.7,1;4,8;", + preview, + "]", + "button_exit[0.3,8.4;3,0.75;button_cancel;"..button_cancel.."]", + "button[10.1,8.4;3,0.75;button_save;Save]" + } + return table.concat(formspec, "") +end + +yl_speak_up.input_fashion = function(player, formname, fields) + if formname ~= "yl_speak_up:fashion" then + return + end + + local pname = player:get_player_name() + local textures = yl_speak_up.speak_to[pname].textures + + if fields then + -- catch ESC as well + if fields.quit or fields.button_cancel or fields.button_exit then + -- is the player editing this npc? then we need to go back to the edit menu + if( yl_speak_up.edit_mode[pname] + and yl_speak_up.edit_mode[pname] == yl_speak_up.speak_to[pname].n_id) then + yl_speak_up.show_fs(player, "talk", {n_id = yl_speak_up.speak_to[pname].n_id, + d_id = yl_speak_up.speak_to[pname].d_id}) + return + end + yl_speak_up.speak_to[pname] = nil + return + end + + local n_id = yl_speak_up.speak_to[pname].n_id + + -- only change cape if there really is a diffrent one selected + if fields.set_cape and fields.set_cape ~= textures[1] then + -- only set the cape if it is part of the list of allowed capes + local capetable, capelist, capeindex = get_npc_capes(textures[1]) + for _, v in pairs(capetable) do + if(v == fields.set_cape) then + textures[1] = fields.set_cape + yl_speak_up.log_change(pname, n_id, + "(fashion) cape changed to "..tostring(fields.set_cape)..".") + -- we have found it + break + end + end + end + -- only change the skin if there really is a diffrent new one selected + if fields.set_skin and fields.set_skin ~= textures[2] then + -- only set the skin if it is part of the list of allowed skins + local maintable, mainlist, mainindex = get_npc_skins(textures[2]) + for _, v in pairs(maintable) do + if(v == fields.set_skin) then + textures[2] = fields.set_skin + yl_speak_up.log_change(pname, n_id, + "(fashion) skin changed to "..tostring(fields.set_skin)..".") + -- we have found it + break + end + end + end + if fields.set_shield then + textures[3] = fields.set_shield + yl_speak_up.log_change(pname, n_id, + "(fashion) shield changed to "..tostring(fields.set_shield)..".") + end + if fields.set_sword then + textures[4] = fields.set_sword + yl_speak_up.log_change(pname, n_id, + "(fashion) sword changed to "..tostring(fields.set_sword)..".") + end + + if fields.button_save then + local obj = yl_speak_up.speak_to[pname].obj + if obj ~= nil and obj:get_luaentity() ~= nil then + -- save textures + yl_speak_up.speak_to[pname].skins = textures2skin(textures) + set_textures(obj, textures) + yl_speak_up.log_change(pname, n_id, + "(fashion) saved changes.") + end + end + + --yl_speak_up.speak_to[pname].textures = textures + end + + yl_speak_up.show_fs(player, "fashion") +end + +function yl_speak_up.fashion(player, obj) + local luaentity = obj:get_luaentity() + local pname = player:get_player_name() + local npc_id = luaentity.yl_speak_up.id + local skins = luaentity.yl_speak_up.skins + local n_id = "n_" .. npc_id + local t = luaentity.textures + + yl_speak_up.speak_to[pname] = {} + yl_speak_up.speak_to[pname].n_id = n_id + yl_speak_up.speak_to[pname].obj = obj + yl_speak_up.speak_to[pname].textures = t + yl_speak_up.speak_to[pname].skins = textures2skin(t) + + local dialog = yl_speak_up.load_dialog(n_id) + if next(dialog) then + yl_speak_up.speak_to[pname].n_npc = dialog.n_npc + else + yl_speak_up.speak_to[pname].n_npc = "Unknown" + end + + yl_speak_up.show_fs(player, "fashion") +end + + + +yl_speak_up.update_nametag = function(self) + if self.yl_speak_up.npc_name then + -- the nametag is normal (green) + if(self.yl_speak_up.talk) then + self.object:set_nametag_attributes({color="#00ff00", text=self.yl_speak_up.npc_name}) + -- the nametag has the addition "[muted]" and is magenta when muted + else + self.object:set_nametag_attributes({color="#ff00ff", text=self.yl_speak_up.npc_name.." [muted]"}) + end + end +end diff --git a/functions.lua b/functions.lua index dcb4947..eb679df 100644 --- a/functions.lua +++ b/functions.lua @@ -1838,358 +1838,6 @@ function yl_speak_up.talk(self, clicker) end end --- ### --- Fashion --- ### - -local function cape2texture(t) - return "yl_speak_up_mask_cape.png^[combine:32x64:56,20=" .. t -end - -local function shield2texture(t) - return "yl_speak_up_mask_shield.png^[combine:32x64:0,0=(" .. t .. ")" -end - -local function textures2skin(textures) - local temp = {} - -- Cape - - local cape = cape2texture(textures[1]) - - -- Main - - local main = textures[2] - - -- left (Shield) - - local left = shield2texture(textures[3]) - - -- right (Sword) - - local right = textures[4] - - temp = {cape, main, left, right} - - return temp -end - -local function set_textures(obj, textures) -- this function takes the base name of the textures, converts them to usable textures and stores those on the NPC - local skins = textures2skin(textures) - - local ent = obj:get_luaentity() - ent.yl_speak_up.skin = skins - - obj:set_properties({textures = skins}) -end - -local function get_npc_skins(skin) - local retstring = "yl_speak_up_main_default.png" - local rettable = {"yl_speak_up_main_default.png"} - local temp = {} - - -- get the files out of modpath - local mp_list = minetest.get_dir_list(yl_speak_up.modpath .. DIR_DELIM .. "textures", false) - - -- get the files out of worlddir - local wp_list = - minetest.get_dir_list( - yl_speak_up.worldpath .. DIR_DELIM .. "worldmods" .. DIR_DELIM .. "yl_npc" .. DIR_DELIM .. "textures", - false - ) - - -- Let's join both lists. - table.insert_all(temp, mp_list) - table.insert_all(temp, wp_list) - - --[[ Let's see if the files are the ones we want. Format is - yl_npc_main_name.png <-- Those are the ones we want - yl_npc_cape_name.png - yl_npc_item_name.png - ]]-- - local index = 1 - local retindex = 1 - - for _, v in pairs(temp) do - local s = string.split(v, "_") - if s[1] == "yl" and s[2] == "npc" and s[3] == "main" then - index = index + 1 - retstring = retstring .. "," .. v - table.insert(rettable, v) - if v == skin then - retindex = index - end - end - end - - return rettable, retstring, retindex -end - -local function get_npc_capes(cape) - local retstring = "yl_npc_cape_default.png" - local rettable = {"yl_npc_cape_default.png"} - local temp = {} - - -- get the files out of modpath - local mp_list = minetest.get_dir_list(yl_speak_up.modpath .. DIR_DELIM .. "textures", false) - - -- get the files out of worlddir - local wp_list = - minetest.get_dir_list( - yl_speak_up.worldpath .. DIR_DELIM .. "worldmods" .. DIR_DELIM .. "yl_npc" .. DIR_DELIM .. "textures", - false - ) - - -- Let's join both lists. - table.insert_all(temp, mp_list) - table.insert_all(temp, wp_list) - - --[[ Let's see if the files are the ones we want. Format is - yl_npc_main_name.png - yl_npc_cape_name.png <-- Those are the ones we want - yl_npc_item_name.png - ]]-- - local index = 1 - local retindex = 1 - - for _, v in pairs(temp) do - local s = string.split(v, "_") - - if s[1] == "yl" and s[2] == "npc" and s[3] == "cape" then - index = index + 1 - retstring = retstring .. "," .. v - table.insert(rettable, v) - if cape ~= "" and v == cape then - retindex = index - end - end - end - - return rettable, retstring, retindex -end - -local function create_preview(main_skin) - if main_skin == nil or main_skin == "" then - main_skin = "default_greyscale.png" - end - - local player_skin = "(" .. main_skin .. ")" - local skin = "" - - -- Consistent on both sizes: - --Chest - skin = skin .. "([combine:16x32:-16,-12=" .. player_skin .. "^[mask:yl_speak_up_mask_chest.png)^" - - --Head - skin = skin .. "([combine:16x32:-4,-8=" .. player_skin .. "^[mask:yl_speak_up_mask_head.png)^" - - --Hat - skin = skin .. "([combine:16x32:-36,-8=" .. player_skin .. "^[mask:yl_speak_up_mask_head.png)^" - --Right Arm - skin = skin .. "([combine:16x32:-44,-12=" .. player_skin .. "^[mask:yl_speak_up_mask_rarm.png)^" - --Right Leg - skin = skin .. "([combine:16x32:0,0=" .. player_skin .. "^[mask:yl_speak_up_mask_rleg.png)^" - - -- Left Arm - skin = skin .. "([combine:16x32:-24,-44=" .. player_skin .. "^[mask:(yl_speak_up_mask_rarm.png^[transformFX))^" - --Left Leg - skin = skin .. "([combine:16x32:-12,-32=" .. player_skin .. "^[mask:(yl_speak_up_mask_rleg.png^[transformFX))^" - - -- Add overlays for 64x skins. these wont appear if skin is 32x because it will be cropped out - --Chest Overlay - skin = skin .. "([combine:16x32:-16,-28=" .. player_skin .. "^[mask:yl_speak_up_mask_chest.png)^" - --Right Arm Overlay - skin = skin .. "([combine:16x32:-44,-28=" .. player_skin .. "^[mask:yl_speak_up_mask_rarm.png)^" - --Right Leg Overlay - skin = skin .. "([combine:16x32:0,-16=" .. player_skin .. "^[mask:yl_speak_up_mask_rleg.png)^" - --Left Arm Overlay - skin = skin .. "([combine:16x32:-40,-44=" .. player_skin .. "^[mask:(yl_speak_up_mask_rarm.png^[transformFX))^" - --Left Leg Overlay - skin = skin .. "([combine:16x32:4,-32=" .. player_skin .. "^[mask:(yl_speak_up_mask_rleg.png^[transformFX))" - - -- Full Preview - skin = "(((" .. skin .. ")^[resize:64x128)^[mask:yl_speak_up_transform.png)" - - return skin -end - -yl_speak_up.get_fs_fashion = function(pname) - local textures = yl_speak_up.speak_to[pname].textures - - local maintable, mainlist, mainindex = get_npc_skins(textures[2]) - local capetable, capelist, capeindex = get_npc_capes(textures[1]) - local preview = create_preview(textures[2]) - - local button_cancel = "Cancel" - -- is this player editing this particular NPC? then rename the button - if( yl_speak_up.edit_mode[pname] - and yl_speak_up.edit_mode[pname] == yl_speak_up.speak_to[pname].n_id) then - button_cancel = "Back" - end - local formspec = { - "formspec_version[3]", - "size[13.4,9.5]", - "dropdown[0.3,0.2;4,0.75;set_skin;", - mainlist, - ";", - mainindex, - "]", - "label[4.6,0.45;", - yl_speak_up.speak_to[pname].n_id, - "]", - "label[6,0.45;", - yl_speak_up.speak_to[pname].n_npc, - "]", - "dropdown[9.1,0.2;4,0.75;set_cape;", - capelist, - ";", - capeindex, - "]", - "field[0.3,3.2;4,0.75;set_sword;;", - textures[4], - "]", - "field[9.1,3.2;4,0.75;set_shield;;", - textures[3], - "]", - "field_close_on_enter[set_sword;false]", - "field_close_on_enter[set_shield;false]", - "image[0.3,1;4,2;", - textures[2], - "]", -- Main - "image[9.1,1;4,2;", - textures[1], - "]", -- Cape - "image[0.3,4.2;4,4;", - textures[4], - "]", -- Sword - "image[9.1,4.2;4,4;", - textures[3], - "]", --textures[3],"]", -- Shield - "image[4.7,1;4,8;", - preview, - "]", - "button_exit[0.3,8.4;3,0.75;button_cancel;"..button_cancel.."]", - "button[10.1,8.4;3,0.75;button_save;Save]" - } - return table.concat(formspec, "") -end - -yl_speak_up.input_fashion = function(player, formname, fields) - if formname ~= "yl_speak_up:fashion" then - return - end - - local pname = player:get_player_name() - local textures = yl_speak_up.speak_to[pname].textures - - if fields then - -- catch ESC as well - if fields.quit or fields.button_cancel or fields.button_exit then - -- is the player editing this npc? then we need to go back to the edit menu - if( yl_speak_up.edit_mode[pname] - and yl_speak_up.edit_mode[pname] == yl_speak_up.speak_to[pname].n_id) then - yl_speak_up.show_fs(player, "talk", {n_id = yl_speak_up.speak_to[pname].n_id, - d_id = yl_speak_up.speak_to[pname].d_id}) - return - end - yl_speak_up.speak_to[pname] = nil - return - end - - local n_id = yl_speak_up.speak_to[pname].n_id - - -- only change cape if there really is a diffrent one selected - if fields.set_cape and fields.set_cape ~= textures[1] then - -- only set the cape if it is part of the list of allowed capes - local capetable, capelist, capeindex = get_npc_capes(textures[1]) - for _, v in pairs(capetable) do - if(v == fields.set_cape) then - textures[1] = fields.set_cape - yl_speak_up.log_change(pname, n_id, - "(fashion) cape changed to "..tostring(fields.set_cape)..".") - -- we have found it - break - end - end - end - -- only change the skin if there really is a diffrent new one selected - if fields.set_skin and fields.set_skin ~= textures[2] then - -- only set the skin if it is part of the list of allowed skins - local maintable, mainlist, mainindex = get_npc_skins(textures[2]) - for _, v in pairs(maintable) do - if(v == fields.set_skin) then - textures[2] = fields.set_skin - yl_speak_up.log_change(pname, n_id, - "(fashion) skin changed to "..tostring(fields.set_skin)..".") - -- we have found it - break - end - end - end - if fields.set_shield then - textures[3] = fields.set_shield - yl_speak_up.log_change(pname, n_id, - "(fashion) shield changed to "..tostring(fields.set_shield)..".") - end - if fields.set_sword then - textures[4] = fields.set_sword - yl_speak_up.log_change(pname, n_id, - "(fashion) sword changed to "..tostring(fields.set_sword)..".") - end - - if fields.button_save then - local obj = yl_speak_up.speak_to[pname].obj - if obj ~= nil and obj:get_luaentity() ~= nil then - -- save textures - yl_speak_up.speak_to[pname].skins = textures2skin(textures) - set_textures(obj, textures) - yl_speak_up.log_change(pname, n_id, - "(fashion) saved changes.") - end - end - - --yl_speak_up.speak_to[pname].textures = textures - end - - yl_speak_up.show_fs(player, "fashion") -end - -function yl_speak_up.fashion(player, obj) - local luaentity = obj:get_luaentity() - local pname = player:get_player_name() - local npc_id = luaentity.yl_speak_up.id - local skins = luaentity.yl_speak_up.skins - local n_id = "n_" .. npc_id - local t = luaentity.textures - - yl_speak_up.speak_to[pname] = {} - yl_speak_up.speak_to[pname].n_id = n_id - yl_speak_up.speak_to[pname].obj = obj - yl_speak_up.speak_to[pname].textures = t - yl_speak_up.speak_to[pname].skins = textures2skin(t) - - local dialog = yl_speak_up.load_dialog(n_id) - if next(dialog) then - yl_speak_up.speak_to[pname].n_npc = dialog.n_npc - else - yl_speak_up.speak_to[pname].n_npc = "Unknown" - end - - yl_speak_up.show_fs(player, "fashion") -end - - - -yl_speak_up.update_nametag = function(self) - if self.yl_speak_up.npc_name then - -- the nametag is normal (green) - if(self.yl_speak_up.talk) then - self.object:set_nametag_attributes({color="#00ff00", text=self.yl_speak_up.npc_name}) - -- the nametag has the addition "[muted]" and is magenta when muted - else - self.object:set_nametag_attributes({color="#ff00ff", text=self.yl_speak_up.npc_name.." [muted]"}) - end - end -end -- mute the npc; either via the appropriate staff or via talking to him yl_speak_up.set_muted = function(p_name, obj, set_muted) diff --git a/init.lua b/init.lua index 1885e9f..96f138d 100644 --- a/init.lua +++ b/init.lua @@ -53,6 +53,8 @@ dofile(modpath .. "trade_list.lua") dofile(modpath .. "quest_api.lua") -- editing the npc with the staff: dofile(modpath .. "staff_based_editing.lua") +-- setting skin, wielded item etc. +dofile(modpath .. "fs_fashion.lua") -- the main functionality of the mod dofile(modpath .. "functions.lua") -- a way of determining a node position