Advertisement
Doyousketch2

player_turret.lua

Jul 15th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.47 KB | None | 0 0
  1. ARROW_DAMAGE = 6
  2. ARROW_VELOCITY = 4
  3.  
  4. player = {}
  5.  
  6. local function is_owner(meta, player)
  7.     if player:get_player_name() ~= meta:get_string("owner") then
  8.         return false
  9.     end
  10.     return true
  11. end
  12.  
  13. camo = {}
  14.  
  15. camo.get_member_list = function(meta)
  16.     local s = meta:get_string("members")
  17.     local list = s:split(" ")
  18.     return list
  19. end
  20.  
  21. camo.set_member_list = function(meta, list)
  22.     meta:set_string("members", table.concat(list, " "))
  23. end
  24.  
  25. camo.is_member = function (meta, name)
  26.     local list = camo.get_member_list(meta)
  27.     for _, n in ipairs(list) do
  28.         if n == name then
  29.             return true
  30.         end
  31.     end
  32.     return false
  33. end
  34.  
  35. camo.add_member = function(meta, name)
  36.     if camo.is_member(meta, name) then return end
  37.     local list = camo.get_member_list(meta)
  38.     table.insert(list,name)
  39.     camo.set_member_list(meta,list)
  40. end
  41.  
  42. camo.del_member = function(meta,name)
  43.     local list = camo.get_member_list(meta)
  44.     for i, n in ipairs(list) do
  45.         if n == name then
  46.             table.remove(list, i)
  47.             break
  48.         end
  49.     end
  50.     camo.set_member_list(meta,list)
  51. end
  52.  
  53. -- Interface:
  54.  
  55. camo.generate_formspec = function(meta)
  56.     if meta:get_int("page") == nil then meta:set_int("page", 0) end
  57.     local formspec = "size[8,7]"
  58.         .. "label[0,1.75;Members:]"
  59.         .. "label[0,2;(ENTER to add)]"
  60.     members = camo.get_member_list(meta)
  61.    
  62.     local npp = 12 -- Was 15 names per page, for the moment it's 4 * 4 (-1 for the + button).
  63.     local s = 0
  64.     local i = 0
  65.     for _, member in ipairs(members) do
  66.         if s < meta:get_int("page") * 15 then s = s + 1 else
  67.             if i < npp then
  68.                 formspec = formspec .. "button["..(i % 4 * 2)..","
  69.                 ..math.floor(i / 4 + 3)..";1.5,.5;camo_member;"..member.."]"
  70.                 formspec = formspec .. "button["..(i % 4 * 2 + 1.25)..","
  71.                 ..math.floor(i / 4 + 3)..";.75,.5;camo_del_member_"..member..";X]"
  72.             end
  73.             i = i + 1
  74.         end
  75.     end
  76.     local add_i = i
  77.     if add_i < npp then
  78.         formspec = formspec
  79.         .. "field[" .. (add_i % 4 * 2 + 1 / 3) .. "," .. (math.floor(add_i / 4 + 3) + 1 / 3) .. ";1.433,.5;camo_add_member;;]"
  80.     end
  81.         formspec = formspec.."button_exit[1,6.2;2,0.5;close_me;OK]"
  82.     return formspec
  83. end
  84.  
  85. minetest.register_node("camo:turret_computer", {
  86.     description = "Turret Computer",
  87.     drawtype = "nodebox",
  88.     paramtype = "light",
  89.     paramtype2 = "facedir",
  90.     tiles = {"computer_top.png",
  91.              "computer_side.png",
  92.              "computer_side.png",
  93.              "computer_side.png",
  94.              "computer_back.png",
  95.              "computer_front.png"},
  96.     is_ground_content = true,
  97.     groups = {oddly_breakable_by_hand=2},
  98.     node_box = {
  99.         type = "fixed",
  100.         fixed = {
  101.             {-0.500000,-0.500000,-0.500000,-0.375000,0.500000,-0.375000},
  102.             {-0.500000,-0.500000,0.375000,-0.375000,0.500000,0.500000},
  103.             {0.375000,-0.500000,-0.500000,0.500000,0.500000,-0.375000},
  104.             {0.375000,-0.500000,0.375000,0.500000,0.500000,0.500000},
  105.             {-0.375000,-0.500000,-0.375000,0.375000,0.500000,0.375000},
  106.             {-0.500000,-0.500000,-0.500000,0.500000,-0.437500,0.500000},
  107.             {-0.500000,0.451557,-0.500000,0.500000,0.500000,0.500000},
  108.         },
  109.     },
  110.     drop = 'camo:turret_computer',
  111.     sounds = default.node_sound_stone_defaults(),
  112. })
  113.  
  114. minetest.register_node("camo:player_turret", {
  115.     description = "Anti Player Turret",
  116.     drawtype = "nodebox",
  117.     paramtype = "light",
  118.     node_box = {
  119.         type = "fixed",
  120.             fixed = {
  121.             {-0.187500,-0.500000,-0.187500,0.187500,-0.125000,0.187500},
  122.             {-0.062500,-0.500000,-0.062500,0.062500,0.500000,0.062500},
  123.             {-0.125000,0.187500,-0.125000,0.125000,0.500000,0.125000},
  124.             {-0.187500,0.062500,-0.187500,0.187500,0.187500,0.187500},
  125.             {-0.187500,0.375000,-0.187500,0.187500,0.500000,0.187500},
  126.             {-0.250000,-0.500000,-0.250000,0.250000,-0.187500,0.250000},
  127.             {-0.312500,-0.500000,-0.312500,0.312500,-0.250000,0.312500},
  128.             {-0.437500,-0.500000,-0.437500,-0.250000,-0.125000,-0.250000},
  129.             {0.250000,-0.500000,0.250000,0.431218,-0.125000,0.423232},
  130.             {-0.437500,-0.500000,0.250000,-0.250000,-0.125000,0.415247},
  131.             {0.250000,-0.500000,-0.437500,0.431218,-0.187500,-0.250000},
  132.         },
  133.     },
  134.     tiles = {"computer_side.png^light.png",
  135.              "computer_side.png",
  136.              "computer_side.png",
  137.              "computer_side.png",
  138.              "computer_side.png",
  139.              "computer_side.png"},
  140.     is_ground_content = true,
  141.     groups = {oddly_breakable_by_hand=2, falling_node=1},
  142.     drop = 'camo:player_turret',
  143.     sounds = default.node_sound_stone_defaults(),
  144.     after_place_node = function(pos, placer)
  145.         local meta = minetest.get_meta(pos)
  146.         meta:set_string("owner", placer:get_player_name() or "")
  147.         meta:set_string("infotext", "Player Turret (owned by "..
  148.                 meta:get_string("owner")..")")
  149.         meta:set_string("members", "")
  150.     end,
  151.     on_construct = function(pos)
  152.         local meta = minetest.get_meta(pos, placer)
  153.         meta:set_string("infotext", "Player Turret")
  154.         meta:set_string("owner", "")
  155.     end,
  156.     can_dig = function(pos,player)
  157.         local meta = minetest.get_meta(pos);
  158.         return is_owner(meta, player)
  159.     end,
  160.     on_rightclick = function(pos, node, clicker, itemstack)
  161.         local meta = minetest.get_meta(pos)
  162.         if is_owner(meta, clicker) == true then
  163.             minetest.show_formspec(clicker:get_player_name(),
  164.             "camo_"..minetest.pos_to_string(pos), camo.generate_formspec(meta)
  165.             )
  166.         end
  167.     end,
  168. })
  169.  
  170. minetest.register_abm({
  171.     nodenames = {"camo:player_turret"},
  172.     interval = 2,
  173.     chance = 1,
  174.     action = function(pos, node)
  175.     local meta = minetest.get_meta(pos)
  176.     local objects = minetest.env:get_objects_inside_radius(pos, 10)
  177.     if minetest.env:get_node({x=pos.x,y=pos.y-1,z=pos.z}).name == 'camo:turret_computer' then return end
  178.         for _,obj in ipairs(objects) do
  179.             if obj:is_player() and is_owner(meta, obj) == false and camo_is_member(meta,obj:get_player_name()) == false then
  180.                 print("Turret shooting at player!")
  181.                 local obj_p = obj:getpos()
  182.                 local calc = {x=obj_p.x - pos.x,y=obj_p.y+0.5 - pos.y,z=obj_p.z - pos.z}
  183.                 local bullet=minetest.env:add_entity({x=pos.x,y=pos.y,z=pos.z}, "camo:arrow_entity")
  184.                 bullet:setvelocity({x=calc.x * ARROW_VELOCITY,y=calc.y * ARROW_VELOCITY,z=calc.z * ARROW_VELOCITY})
  185.                 music_handle=minetest.sound_play("gun",
  186.                 {pos = pos, gain = 1.0, max_hear_distance = 15,})
  187.             end
  188.         end
  189.     end
  190. })
  191.  
  192. minetest.register_abm({
  193.     nodenames = {"camo:player_turret"},
  194.     interval = 1,
  195.     chance = 1,
  196.     action = function(pos, node)
  197.     local meta = minetest.get_meta(pos)
  198.     local objects = minetest.env:get_objects_inside_radius(pos, 20)
  199.     if minetest.env:get_node({x=pos.x,y=pos.y-1,z=pos.z}).name ~= 'camo:turret_computer' then return end
  200.         for _,obj in ipairs(objects) do
  201.             if obj:is_player() and is_owner(meta, obj) == false and camo_is_member(meta,obj:get_player_name()) == false then
  202.                 print("Turret with Computer shooting at player!")
  203.                 local obj_p = obj:getpos()
  204.                 local calc = {x=obj_p.x - pos.x,y=obj_p.y+0.5 - pos.y,z=obj_p.z - pos.z}
  205.                 local bullet=minetest.env:add_entity({x=pos.x,y=pos.y,z=pos.z}, "camo:arrow_entity")
  206.                 bullet:setvelocity({x=calc.x * ARROW_VELOCITY,y=calc.y * ARROW_VELOCITY,z=calc.z * ARROW_VELOCITY})
  207.                 music_handle=minetest.sound_play("gun",
  208.                 {pos = pos, gain = 1.0, max_hear_distance = 25,})
  209.             end
  210.         end
  211.     end
  212. })
  213.  
  214. -- If name entered into camo formspec
  215.  
  216. minetest.register_on_player_receive_fields(function(player,formname,fields)
  217.     if string.sub(formname, 0,string.len("camo_")) == "camo_" then
  218.         local pos_s = string.sub(formname,string.len("camo_")+1)
  219.         local pos = minetest.string_to_pos(pos_s)
  220.         local meta = minetest.get_meta(pos)
  221.  
  222.         if meta:get_int("page") == nil then meta:set_int("page", 0) end
  223.  
  224.         if fields.camo_add_member then
  225.             for _, i in ipairs(fields.camo_add_member:split(" ")) do
  226.                 camo.add_member(meta,i)
  227.             end
  228.         end
  229.  
  230.         for field, value in pairs(fields) do
  231.             if string.sub(field, 0, string.len("camo_del_member_")) == "camo_del_member_" then
  232.                 camo.del_member(meta, string.sub(field,string.len("camo_del_member_")+1))
  233.             end
  234.         end
  235.  
  236.         if fields.camo_page_prev then
  237.             meta:set_int("page",meta:get_int("page")-1)
  238.         end
  239.  
  240.         if fields.camo_page_next then
  241.             meta:set_int("page",meta:get_int("page")+1)
  242.         end
  243.  
  244.         if fields.close_me then
  245.             meta:set_int("page",meta:get_int("page"))
  246.             else minetest.show_formspec(player:get_player_name(), formname, protector.generate_formspec(meta))
  247.         end
  248.     end
  249. end)
  250.  
  251. --Arrow stuff--
  252.  
  253. THROWING_ARROW_ENTITY={
  254.     physical = false,
  255.     timer=0,
  256.     visual_size = {x=0.5, y=0.5},
  257.     textures = {"bullet.png"},
  258.     lastpos={},
  259.     collisionbox = {-0.17,-0.17,-0.17,0.17,0.17,0.17},
  260. }
  261. -- Arrow_entity.on_step()--> called when arrow is moving
  262. THROWING_ARROW_ENTITY.on_step = function(self, dtime)
  263.     self.timer=self.timer+dtime
  264.     local pos = self.object:getpos()
  265.     local node = minetest.env:get_node(pos)
  266.     if self.timer > 5 then
  267.         self.object:remove()
  268.     end
  269.     -- When arrow is away from player (after 0.2 seconds): Cause damage to players
  270.     if self.timer>0.2 then
  271.         local objs = minetest.env:get_objects_inside_radius({x=pos.x,y=pos.y,z=pos.z}, 5)
  272.         for k, obj in pairs(objs) do
  273.             if obj:is_player() then
  274.                 obj:set_hp(obj:get_hp()-ARROW_DAMAGE)
  275.                 self.object:remove()
  276.             end
  277.         end
  278.     end
  279.  
  280.     -- Become item when hitting a node
  281.     if self.lastpos.x~=nil then --If there is no lastpos for some reason
  282.         if node.name ~= "air" and node.name ~= "camo:player_turret" then
  283.             minetest.env:add_item(self.lastpos, 'throwing:arrow')
  284.             self.object:remove()
  285.         end
  286.     end
  287.     self.lastpos={x=pos.x, y=pos.y, z=pos.z} -- Set lastpos-->Item will be added at last pos outside the node
  288. end
  289.  
  290. minetest.register_entity("camo:arrow_entity", THROWING_ARROW_ENTITY)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement