]> git.lizzy.rs Git - Crafter.git/blobdiff - mods/skins/init.lua
Add in capes
[Crafter.git] / mods / skins / init.lua
index 4e66e3dc3a4826cdd77f598748d2d9b293ca955d..44433df62face09589b7446a5132b71c61ed3664 100644 (file)
@@ -1,7 +1,8 @@
-local security = minetest.request_insecure_environment()
-
 local path = minetest.get_modpath(minetest.get_current_modname())
 
+-- path for the temporary skins file
+local temppath = minetest.get_worldpath() .. "/skins_temp.png"
+
 local pngimage = dofile(path.."/png_lua/png.lua")
 
 --run through all the skins on the skindex and index them all locally
@@ -12,27 +13,21 @@ local id = "Lua Skins Updater"
 -- Binary downloads are required
 if not core.features.httpfetch_binary_data then
        print("outdated version of MINETEST detected!")
+    return(nil)
 end
 
-if not http or not security then
+if not http then
     for i = 1,5 do
         print("!WARNING!")
     end
     print("---------------------------------------------------------------")
     print("HTTP access is required. Please add this to your minetest.conf:")
     print("secure.http_mods = skins")
-    print("secure.trusted_mods = skins")
     print("!!Skins will not work without this!!")
     print("---------------------------------------------------------------")
     return(nil)
 end
 
-
--- http://minetest.fensta.bplaced.net/api/apidoku.md
-local root_url = "http://minetest.fensta.bplaced.net"
-local page_url = root_url .. "/api/v2/get.json.php?getlist&page=%i&outformat=base64" -- [1] = Page#
-local preview_url = root_url .. "/skins/1/%i.png" -- [1] = ID
-
 -- Fancy debug wrapper to download an URL
 local function fetch_url(url, callback)
        http.fetch({
@@ -53,13 +48,6 @@ local function fetch_url(url, callback)
        end)
 end
 
-local function unsafe_file_write(path, contents)
-    local f = security.io.open(path, "wb")
-    if not f then return end
-       f:write(contents)
-       f:close()
-end
-
 --https://gist.github.com/marceloCodget/3862929 rgb to hex
 
 local function rgbToHex(rgb)
@@ -100,10 +88,12 @@ local function file_to_texture(image)
         for _,data in pairs(line) do
             if x <= 32 or y > 16 then
                 local hex = rgbToHex({data.R,data.G,data.B})
-                
-                --https://github.com/GreenXenith/skinmaker/blob/master/init.lua#L57 Thanks :D
+                --skip transparent pixels
+                if data.A > 0 then 
+                    --https://github.com/GreenXenith/skinmaker/blob/master/init.lua#L57 Thanks :D
 
-                base_texture = base_texture .. (":%s,%s=%s"):format(x - 1, y - 1, "(p.png\\^[colorize\\:#" .. hex .. ")")
+                    base_texture = base_texture .. (":%s,%s=%s"):format(x - 1, y - 1, "(p.png\\^[colorize\\:#" .. hex .. ")")
+                end
             --else
             --    print(dump(data))
             end
@@ -124,9 +114,11 @@ end
 fetch_function = function(name)
     fetch_url("https://raw.githubusercontent.com/"..name.."/crafter_skindex/master/skin.png", function(data)
         if data then
-            unsafe_file_write(path.."/skin_temp/temp.png", data)
+            local f = io.open(temppath, "wb")
+            f:write(data)
+            f:close()
 
-            local img = pngimage(minetest.get_modpath("skins").."/skin_temp/temp.png", nil, false, false)
+            local img = pngimage(temppath, nil, false, false)
             if img then
                 local stored_texture = file_to_texture(img)
 
@@ -135,8 +127,11 @@ fetch_function = function(name)
                 if stored_texture then
                     --set the player's skin
                     local player = minetest.get_player_by_name(name)
-                    player:set_properties({textures = {stored_texture}})
-                    
+                    player:set_properties({textures = {stored_texture, "blank_skin.png"}})
+                    local meta = player:get_meta()
+                    meta:set_string("skin",stored_texture)
+
+                    recalculate_armor(player) --redundancy
                     
                     --[[
                     player:hud_add(
@@ -185,8 +180,137 @@ end
 --local img = pngimage(minetest.get_modpath("skins").."/skin_temp/temp.png", nil, false, false)
 --print(dump(img))
 
+
+
+local cape = {}
+cape.initial_properties = {
+       visual = "mesh",
+       mesh = "cape.x",
+       textures = {"cape_core.png"},
+    pointable = false,
+    collisionbox = {0, 0, 0, 0, 0, 0}
+}
+cape.degrees = function(yaw)
+    return(yaw*180.0/math.pi)
+end
+cape.texture_set = false
+cape.on_step = function(self,dtime)
+    --don't waste any cpu
+    if not self.owner or not self.owner:is_player() then
+        self.object:remove()
+        return
+    end
+    --set cape texture
+    if not self.texture_set and self.texture_type then
+        self.object:set_properties({textures={self.texture_type}})
+        self.texture_set = true
+    end
+
+    local pos = self.object:get_pos()
+    local current_animation,_,_,_ = self.object:get_animation()
+    current_animation = current_animation.x
+
+    if self.old_pos then
+        --do not allow cape to flutter if player is moving backwards
+        local body_yaw = self.owner:get_look_horizontal()
+        local cape_yaw = minetest.dir_to_yaw(vector.direction(self.old_pos,pos))
+               cape_yaw = minetest.dir_to_yaw(minetest.yaw_to_dir(cape_yaw))
+               cape_yaw = self.degrees(cape_yaw)-self.degrees(body_yaw)
+
+               if cape_yaw < -180 then
+                       cape_yaw = cape_yaw + 360
+               elseif cape_yaw > 180 then
+                       cape_yaw = cape_yaw - 360
+        end
+        if cape_yaw >= -90 and cape_yaw <= 90 then
+            --use old position to calculate the "wind"
+            local deg = self.degrees(minetest.dir_to_yaw(vector.new(vector.distance(vector.new(pos.x,0,pos.z),vector.new(self.old_pos.x,0,self.old_pos.z)),0,pos.y-self.old_pos.y))+(math.pi/2))*-1
+            deg = deg + 90
+            self.goal = math.floor(deg+0.5)
+        else
+            self.goal = 0
+        end
+
+        if vector.distance(pos,self.old_pos) == 0 then
+            self.goal = 25
+        end
+    end
+    --cape smoothing
+    if self.goal and current_animation ~= self.goal then
+        if math.abs(current_animation-self.goal) == 1 then --this stops jittering
+            self.object:set_animation({x=self.goal,y=self.goal}, 0, 0, false)
+        elseif current_animation < self.goal then
+            self.object:set_animation({x=current_animation+2,y=current_animation+2}, 0, 0, false)
+        elseif current_animation > self.goal then
+            self.object:set_animation({x=current_animation-2,y=current_animation-2}, 0, 0, false)
+        end
+    end
+    self.old_pos = pos
+end
+minetest.register_entity("skins:cape",cape)
+
+--function for handling capes
+local cape_table = {}
+
+local add_cape = function(player,cape)
+    local obj = minetest.add_entity(player:get_pos(),"skins:cape")
+    obj:get_luaentity().owner = player
+    obj:set_attach(player, "Cape_bone", vector.new(0,0.25,0.5), vector.new(-90,180,0))
+    obj:get_luaentity().texture_type = cape
+    local name = player:get_player_name()
+       cape_table[name] = obj
+end
+
+local function readd_capes()
+    for _,player in ipairs(minetest.get_connected_players()) do
+        local meta = player:get_meta()
+        local cape = meta:get_string("cape")
+        if cape ~= "" then
+            local name = player:get_player_name()
+            if not cape_table[name] or (cape_table[name] and not cape_table[name]:get_luaentity()) then
+                add_cape(player,cape)
+                print("adding cape")
+            end
+        end
+    end
+    minetest.after(3,function()
+        readd_capes()
+    end)
+end
+minetest.register_on_mods_loaded(function()
+    minetest.after(3,function()
+        readd_capes()
+    end)
+end)
+
+local custom = {sfan5=true,appguru=true,tacotexmex=true,oilboi=true,wuzzy=true}
+
+local core_devs = {celeron55=true,nore=true,nerzhul=true,paramat=true,sofar=true,rubenwardy=true,smalljoker=true,larsh=true,thetermos=true,krock=true}
+
+local patrons = {tacotexmex=true,ufa=true,monte48=true}
+
+
 minetest.register_on_joinplayer(function(player)
-    minetest.after(4,function()
+    local meta = player:get_meta()
+    meta:set_string("skin","player.png")
+    local name = string.lower(player:get_player_name())
+
+    --cape handling
+    local cape = false
+    if custom[name] then
+        cape = "cape_"..name..".png"
+    elseif core_devs[name] then
+        cape = "cape_core.png"
+    elseif patrons[name] then
+        cape = "cape_patron.png"
+    end
+
+    if cape then
+        meta:set_string("cape",cape)
+        add_cape(player,cape)
+    end
+
+    minetest.after(0,function()
         fetch_function(player:get_player_name())
     end)
-end)
\ No newline at end of file
+end)