Advertisement
jojoa1997

Armor.lua

Sep 28th, 2014
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.17 KB | None | 0 0
  1. ARMOR_INIT_DELAY = 1
  2. ARMOR_INIT_TIMES = 1
  3. ARMOR_BONES_DELAY = 1
  4. ARMOR_UPDATE_TIME = 1
  5. ARMOR_DROP = minetest.get_modpath("bones") ~= nil
  6. ARMOR_DESTROY = false
  7. ARMOR_LEVEL_MULTIPLIER = 1
  8. ARMOR_HEAL_MULTIPLIER = 1
  9.  
  10. local modpath = minetest.get_modpath(ARMOR_MOD_NAME)
  11. local worldpath = minetest.get_worldpath()
  12. local input = io.open(modpath.."/armor.conf", "r")
  13. if input then
  14.     dofile(modpath.."/armor.conf")
  15.     input:close()
  16.     input = nil
  17. end
  18. input = io.open(worldpath.."/armor.conf", "r")
  19. if input then
  20.     dofile(worldpath.."/armor.conf")
  21.     input:close()
  22.     input = nil
  23. end
  24.  
  25. local time = 0
  26.  
  27. armor = {
  28.     player_hp = {},
  29.     elements = {"head", "torso", "legs", "feet"},
  30.     physics = {"jump","speed","gravity"},
  31.     --[[formspec = "size[8,8.5]list[detached:player_name_armor;armor;0,1;2,3;]"
  32.         .."image[2,0.75;2,4;armor_preview]"
  33.         .."list[current_player;main;0,4.5;8,4;]"
  34.         .."list[current_player;craft;4,1;3,3;]"
  35.         .."list[current_player;craftpreview;7,2;1,1;]",]]
  36.     textures = {},
  37.     default_skin = "character",
  38. }
  39.  
  40. --[[if inventory_plus then
  41.     armor.formspec = "size[8,8.5]button[0,0;2,0.5;main;Back]"
  42.         .."list[detached:player_name_armor;armor;0,1;2,3;]"
  43.         .."image[2.5,0.75;2,4;armor_preview]"
  44.         .."label[5,1;Level: armor_level]"
  45.         .."label[5,1.5;Heal:  armor_heal]"
  46.         .."list[current_player;main;0,4.5;8,4;]"
  47. elseif unified_inventory then
  48.     unified_inventory.register_button("armor", {
  49.         type = "image",
  50.         image = "inventory_plus_armor.png",
  51.     })
  52.     unified_inventory.register_page("armor", {
  53.         get_formspec = function(player)
  54.             local name = player:get_player_name()
  55.             local formspec = "background[0.06,0.99;7.92,7.52;3d_armor_ui_form.png]"
  56.                 .."label[0,0;Armor]"
  57.                 .."list[detached:"..name.."_armor;armor;0,1;2,3;]"
  58.                 .."image[2.5,0.75;2,4;"..armor.textures[name].preview.."]"
  59.                 .."label[5,1;Level: "..armor.def[name].level.."]"
  60.                 .."label[5,1.5;Heal:  "..armor.def[name].heal.."]"
  61.             return {formspec=formspec}
  62.         end,
  63.     })
  64. end]]
  65.  
  66. armor.def = {
  67.     state = 0,
  68.     count = 0,
  69. }
  70.  
  71. armor.update_player_visuals = function(self, player)
  72.     if not player then
  73.         return
  74.     end
  75.     local name = player:get_player_name()
  76.     if self.textures[name] then
  77.         default.player_set_textures(player, {
  78.             self.textures[name].skin,
  79.             self.textures[name].armor,
  80.             self.textures[name].wielditem,
  81.         })
  82.     end
  83. end
  84.  
  85. armor.set_player_armor = function(self, player)
  86.     if not player then
  87.         return
  88.     end
  89.     local name = player:get_player_name()
  90.     local player_inv = player:get_inventory()
  91.     if not name then
  92.         minetest.log("error", "Failed to read player name")
  93.         return
  94.     elseif not player_inv then
  95.         minetest.log("error", "Failed to read player inventory")
  96.         return
  97.     end
  98.     --local armor_texture = "3d_armor_trans.png"
  99.     local armor_level = 0
  100.     local armor_heal = 0
  101.     local state = 0
  102.     local items = 0
  103.     local elements = {}
  104.     local textures = {}
  105.     local physics_o = {speed=1,gravity=1,jump=1}
  106.     local material = {type=nil, count=1}
  107.     --local preview = armor:get_player_skin(name).."_preview.png"
  108.     for _,v in ipairs(self.elements) do
  109.         elements[v] = false
  110.     end
  111.     for i=1, 6 do
  112.         local stack = player_inv:get_stack("armor", i)
  113.         local item = stack:get_name()
  114.         if stack:get_count() == 1 then
  115.             local def = stack:get_definition()
  116.             for k, v in pairs(elements) do
  117.                 if v == false then
  118.                     local level = def.groups["armor_"..k]
  119.                     if level then
  120.                         local texture = item:gsub("%:", "_")
  121.                         table.insert(textures, texture..".png")
  122.                         --preview = preview.."^"..texture.."_preview.png"
  123.                         armor_level = armor_level + level
  124.                         state = state + stack:get_wear()
  125.                         items = items + 1
  126.                         local heal = def.groups["armor_heal"] or 0
  127.                         armor_heal = armor_heal + heal
  128.                         for kk,vv in ipairs(self.physics) do                           
  129.                             local o_value = def.groups["physics_"..vv]
  130.                             if o_value then
  131.                                 physics_o[vv] = physics_o[vv] + o_value
  132.                             end
  133.                         end
  134.                         local mat = string.match(item, "%:.+_(.+)$")
  135.                         if material.type then
  136.                             if material.type == mat then
  137.                                 material.count = material.count + 1
  138.                             end
  139.                         else
  140.                             material.type = mat
  141.                         end
  142.                         elements[k] = true
  143.                     end
  144.                 end
  145.             end
  146.         end
  147.     end
  148.     --[[if minetest.get_modpath("shields") then
  149.         armor_level = armor_level * 0.9
  150.     end]]
  151.     if material.type and material.count == #self.elements then
  152.         armor_level = armor_level * 1.1
  153.     end
  154.     armor_level = armor_level * ARMOR_LEVEL_MULTIPLIER
  155.     armor_heal = armor_heal * ARMOR_HEAL_MULTIPLIER
  156.     if #textures > 0 then
  157.         armor_texture = table.concat(textures, "^")
  158.     end
  159.     local armor_groups = {fleshy=100}
  160.     if armor_level > 0 then
  161.         armor_groups.level = math.floor(armor_level / 20)
  162.         armor_groups.fleshy = 100 - armor_level
  163.     end
  164.     player:set_armor_groups(armor_groups)
  165.     player:set_physics_override(physics_o)
  166.     self.textures[name].armor = armor_texture
  167.     --self.textures[name].preview = preview
  168.     self.def[name].state = state
  169.     self.def[name].count = items
  170.     self.def[name].level = armor_level
  171.     self.def[name].heal = armor_heal
  172.     self.def[name].jump = physics_o.jump
  173.     self.def[name].speed = physics_o.speed
  174.     self.def[name].gravity = physics_o.gravity
  175.     self:update_player_visuals(player)
  176. end
  177.  
  178. armor.update_armor = function(self, player)
  179.     if not player then
  180.         return
  181.     end
  182.     local name = player:get_player_name()
  183.     local hp = player:get_hp() or 0
  184.     if hp == 0 or hp == self.player_hp[name] then
  185.         return
  186.     end
  187.     if self.player_hp[name] > hp then
  188.         local player_inv = player:get_inventory()
  189.         local armor_inv = minetest.get_inventory({type="detached", name=name.."_armor"})
  190.         if not player_inv then
  191.             minetest.log("error", "Failed to read player inventory")
  192.             return
  193.         elseif not armor_inv then
  194.             minetest.log("error", "Failed to read detached inventory")
  195.             return
  196.         end
  197.         local heal_max = 0
  198.         local state = 0
  199.         local items = 0
  200.         for i=1, 6 do
  201.             local stack = player_inv:get_stack("armor", i)
  202.             if stack:get_count() > 0 then
  203.                 local use = stack:get_definition().groups["armor_use"] or 0
  204.                 local heal = stack:get_definition().groups["armor_heal"] or 0
  205.                 local item = stack:get_name()
  206.                 stack:add_wear(use)
  207.                 armor_inv:set_stack("armor", i, stack)
  208.                 player_inv:set_stack("armor", i, stack)
  209.                 state = state + stack:get_wear()
  210.                 items = items + 1
  211.                 if stack:get_count() == 0 then
  212.                     local desc = minetest.registered_items[item].description
  213.                     if desc then
  214.                         minetest.chat_send_player(name, "Your "..desc.." got destroyed!")
  215.                     end
  216.                     self:set_player_armor(player)
  217.                     armor:update_inventory(player)
  218.                 end
  219.                 heal_max = heal_max + heal
  220.             end
  221.         end
  222.         self.def[name].state = state
  223.         self.def[name].count = items
  224.         heal_max = heal_max * ARMOR_HEAL_MULTIPLIER
  225.         if heal_max > math.random(100) then
  226.             player:set_hp(self.player_hp[name])
  227.             return
  228.         end
  229.     end
  230.     self.player_hp[name] = hp
  231. end
  232.  
  233. armor.get_player_skin = function(self, name)
  234.     local skin = nil
  235.     if skins then
  236.         skin = skins.skins[name]
  237.     elseif u_skins then
  238.         skin = u_skins.u_skins[name]
  239.     end
  240.     return skin or armor.default_skin
  241. end
  242.  
  243. armor.get_armor_formspec = function(self, name)
  244.     local formspec = armor.formspec:gsub("player_name", name)
  245.     --formspec = formspec:gsub("armor_preview", armor.textures[name].preview)
  246.     formspec = formspec:gsub("armor_level", armor.def[name].level)
  247.     return formspec:gsub("armor_heal", armor.def[name].heal)
  248. end
  249.  
  250. --[[armor.update_inventory = function(self, player)
  251.     local name = player:get_player_name()
  252.     if unified_inventory then
  253.         if unified_inventory.current_page[name] == "armor" then
  254.             unified_inventory.set_inventory_formspec(player, "armor")
  255.         end
  256.     else
  257.         local formspec = armor:get_armor_formspec(name)
  258.         if inventory_plus then
  259.             local page = player:get_inventory_formspec()
  260.             if page:find("detached:"..name.."_armor") then
  261.                 inventory_plus.set_inventory_formspec(player, formspec)
  262.             end
  263.         else
  264.             player:set_inventory_formspec(formspec)
  265.         end
  266.     end
  267. end]]
  268.  
  269. -- Register Player Model
  270.  
  271. default.player_register_model("3d_armor_character.x", {
  272.     animation_speed = 30,
  273.     textures = {
  274.         armor.default_skin..".png",
  275.         "3d_armor_trans.png",
  276.         "3d_armor_trans.png",
  277.     },
  278.     animations = {
  279.         stand = {x=0, y=79},
  280.         lay = {x=162, y=166},
  281.         walk = {x=168, y=187},
  282.         mine = {x=189, y=198},
  283.         walk_mine = {x=200, y=219},
  284.         sit = {x=81, y=160},
  285.     },
  286. })
  287.  
  288. -- Register Callbacks
  289.  
  290. minetest.register_on_player_receive_fields(function(player, formname, fields)
  291.     local name = player:get_player_name()
  292.     if inventory_plus and fields.armor then
  293.         local formspec = armor:get_armor_formspec(name)
  294.         inventory_plus.set_inventory_formspec(player, formspec)
  295.         return
  296.     end
  297.     for field, _ in pairs(fields) do
  298.         if string.find(field, "skins_set_") then
  299.             minetest.after(0, function(player)
  300.                 local skin = armor:get_player_skin(name)
  301.                 armor.textures[name].skin = skin..".png"
  302.                 armor:set_player_armor(player)
  303.             end, player)
  304.         end
  305.     end
  306. end)
  307.  
  308. minetest.register_on_joinplayer(function(player)
  309.     default.player_set_model(player, "3d_armor_character.x")
  310.     local name = player:get_player_name()
  311.     local player_inv = player:get_inventory()
  312.     local armor_inv = minetest.create_detached_inventory(name.."_armor",{
  313.         on_put = function(inv, listname, index, stack, player)
  314.             player:get_inventory():set_stack(listname, index, stack)
  315.             armor:set_player_armor(player)
  316.             --armor:update_inventory(player)
  317.         end,
  318.         on_take = function(inv, listname, index, stack, player)
  319.             player:get_inventory():set_stack(listname, index, nil)
  320.             armor:set_player_armor(player)
  321.             --armor:update_inventory(player)
  322.         end,
  323.         on_move = function(inv, from_list, from_index, to_list, to_index, count, player)
  324.             local plaver_inv = player:get_inventory()
  325.             local stack = inv:get_stack(to_list, to_index)
  326.             player_inv:set_stack(to_list, to_index, stack)
  327.             player_inv:set_stack(from_list, from_index, nil)
  328.             armor:set_player_armor(player)
  329.             --armor:update_inventory(player)
  330.         end,
  331.         allow_put = function(inv, listname, index, stack, player)
  332.             return 1
  333.         end,
  334.         allow_take = function(inv, listname, index, stack, player)
  335.             return stack:get_count()
  336.         end,
  337.         allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
  338.             return count
  339.         end,
  340.     })
  341.     --[[if inventory_plus then
  342.         inventory_plus.register_button(player,"armor", "Armor")
  343.     end]]
  344.     armor_inv:set_size("armor", 4)
  345.     player_inv:set_size("armor", 4)
  346.     for i=1, 4 do
  347.         local stack = player_inv:get_stack("armor", i)
  348.         armor_inv:set_stack("armor", i, stack)
  349.     end
  350.  
  351.     -- Legacy support, import player's armor from old inventory format
  352.     for _,v in pairs(armor.elements) do
  353.         local list = "armor_"..v
  354.         armor_inv:add_item("armor", player_inv:get_stack(list, 1))
  355.         player_inv:set_stack(list, 1, nil)
  356.     end
  357.  
  358.     armor.player_hp[name] = 0
  359.     armor.def[name] = {
  360.         state = 0,
  361.         count = 0,
  362.         level = 0,
  363.         heal = 0,
  364.         jump = 1,
  365.         speed = 1,
  366.         gravity = 1,
  367.     }
  368.     armor.textures[name] = {
  369.         skin = armor.default_skin..".png",
  370.         armor = "3d_armor_trans.png",
  371.         wielditem = "3d_armor_trans.png",
  372.         preview = armor.default_skin.."_preview.png",
  373.     }
  374.     if minetest.get_modpath("skins") then
  375.         local skin = skins.skins[name]
  376.         if skin and skins.get_type(skin) == skins.type.MODEL then
  377.             armor.textures[name].skin = skin..".png"
  378.         end
  379.     elseif minetest.get_modpath("simple_skins") then
  380.         local skin = skins.skins[name]
  381.         if skin then
  382.             armor.textures[name].skin = skin..".png"
  383.         end
  384.     --[[elseif minetest.get_modpath("u_skins") then
  385.         local skin = u_skins.u_skins[name]
  386.         if skin and u_skins.get_type(skin) == u_skins.type.MODEL then
  387.             armor.textures[name].skin = skin..".png"
  388.         end]]
  389.     end
  390.     if minetest.get_modpath("player_textures") then
  391.         local filename = minetest.get_modpath("player_textures").."/textures/player_"..name
  392.         local f = io.open(filename..".png")
  393.         if f then
  394.             f:close()
  395.             armor.textures[name].skin = "player_"..name..".png"
  396.         end
  397.     end
  398.     for i=1, ARMOR_INIT_TIMES do
  399.         minetest.after(ARMOR_INIT_DELAY * i, function(player)
  400.             armor:set_player_armor(player)
  401.             if inventory_plus == nil and unified_inventory == nil then
  402.                 --armor:update_inventory(player)
  403.             end
  404.         end, player)
  405.     end
  406. end)
  407.  
  408. if ARMOR_DROP == true or ARMOR_DESTROY == true then
  409.     minetest.register_on_dieplayer(function(player)
  410.         local name = player:get_player_name()
  411.         local pos = player:getpos()
  412.         if name and pos then
  413.             local drop = {}
  414.             local player_inv = player:get_inventory()
  415.             local armor_inv = minetest.get_inventory({type="detached", name=name.."_armor"})
  416.             for i=1, player_inv:get_size("armor") do
  417.                 local stack = armor_inv:get_stack("armor", i)
  418.                 if stack:get_count() > 0 then
  419.                     table.insert(drop, stack)
  420.                     armor_inv:set_stack("armor", i, nil)
  421.                     player_inv:set_stack("armor", i, nil)
  422.                 end
  423.             end
  424.             armor:set_player_armor(player)
  425.             --[[if unified_inventory then
  426.                 unified_inventory.set_inventory_formspec(player, "craft")
  427.             elseif inventory_plus then
  428.                 local formspec = inventory_plus.get_formspec(player,"main")
  429.                 inventory_plus.set_inventory_formspec(player, formspec)
  430.             else
  431.                 armor:update_inventory(player)
  432.             end]]
  433.             if ARMOR_DESTROY == false then
  434.                 if minetest.get_modpath("bones") then
  435.                     minetest.after(ARMOR_BONES_DELAY, function()
  436.                         pos = vector.round(pos)
  437.                         local node = minetest.get_node(pos)
  438.                         if node.name == "bones:bones" then
  439.                             local meta = minetest.get_meta(pos)
  440.                             local owner = meta:get_string("owner")
  441.                             local inv = meta:get_inventory()
  442.                             if name == owner then
  443.                                 for _,stack in ipairs(drop) do
  444.                                     if inv:room_for_item("main", stack) then
  445.                                         inv:add_item("main", stack)
  446.                                     end
  447.                                 end
  448.                             end
  449.                         end
  450.                     end)
  451.                 else
  452.                     for _,stack in ipairs(drop) do
  453.                         local obj = minetest.add_item(pos, stack)
  454.                         if obj then
  455.                             local x = math.random(1, 5)
  456.                             if math.random(1,2) == 1 then
  457.                                 x = -x
  458.                             end
  459.                             local z = math.random(1, 5)
  460.                             if math.random(1,2) == 1 then
  461.                                 z = -z
  462.                             end
  463.                             obj:setvelocity({x=1/x, y=obj:getvelocity().y, z=1/z})
  464.                         end
  465.                     end
  466.                 end
  467.             end
  468.         end
  469.     end)
  470. end
  471.  
  472. minetest.register_globalstep(function(dtime)
  473.     time = time + dtime
  474.     if time > ARMOR_UPDATE_TIME then
  475.         for _,player in ipairs(minetest.get_connected_players()) do
  476.             armor:update_armor(player)
  477.         end
  478.         time = 0
  479.     end
  480. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement