Maciej Kasatkin
2012-10-28 01b21d49af04f9b3251c5edfb759995dc45d5110
mining_laser_mk1.lua
@@ -1,23 +1,48 @@
laser_mk1_max_charge=40000
local laser_shoot = function(itemstack, player, pointed_thing)
            local laser_straight_mode=0
            local playerpos=player:getpos()
            local dir=player:get_look_dir()
            if pointed_thing.type=="node" then
               pos=minetest.get_pointed_thing_position(pointed_thing, above)
               local node = minetest.env:get_node(pos)
               if node.name~="ignore" then
               minetest.node_dig(pos,node,player)
               end
               laser_straight_mode=1
               end
            direction_y=math.abs(math.floor(dir.y*100))
            print (direction_y)
            if direction_y>50 then entity_name="technic:laser_beam_entityV"
               else entity_name="technic:laser_beam_entity" end
            local obj=minetest.env:add_entity({x=playerpos.x,y=playerpos.y+1.6,z=playerpos.z},entity_name)
            if laser_straight_mode==1  then
               pos1=minetest.get_pointed_thing_position(pointed_thing, under)
               pos1.x=math.floor(pos1.x)
               pos1.y=math.floor(pos1.y)
               pos1.z=math.floor(pos1.z)
               obj=minetest.env:add_entity(pos1,entity_name)
            else
            obj=minetest.env:add_entity({x=playerpos.x,y=playerpos.y+1.6,z=playerpos.z},entity_name)
            end
            if obj:get_luaentity().player == nil then
               obj:get_luaentity().player = player
            end
            obj:setvelocity({x=dir.x*12, y=dir.y*12, z=dir.z*12})
            if laser_straight_mode==1 and direction_y<50 then
               obj:setvelocity({x=dir.x*8, y=0, z=dir.z*8})
            else if laser_straight_mode==1 and direction_y>50 then
               obj:setvelocity({x=0, y=dir.y*8, z=dir.z*8})
               end
            end
            if laser_straight_mode==0 then
               obj:setvelocity({x=dir.x*8, y=dir.y*8, z=dir.z*8})
               end
            obj:setacceleration({x=0, y=0, z=0})
            obj:setyaw(player:get_look_yaw()+math.pi)
            if obj:get_luaentity().player == nil then
               obj:get_luaentity().player = player
            end
            obj:get_luaentity().node = player:get_inventory():get_stack("main", 1):get_name()
            --obj:get_luaentity().node = player:get_inventory():get_stack("main", 1):get_name()
            minetest.sound_play("technic_laser", {pos = playerpos, gain = 1.0, max_hear_distance = 10,})
            return true
end
@@ -29,14 +54,13 @@
   stack_max = 1,
   on_use = function(itemstack, user, pointed_thing)
      item=itemstack:to_table()
      local charge=tonumber((item["wear"]))
      if charge ==0 then charge =65535 end
      charge=get_RE_item_load(charge,laser_mk1_max_charge)
      if item["metadata"]=="" or item["metadata"]=="0" then  return end
      local charge=tonumber((item["metadata"]))
      if charge-400>0 then
       laser_shoot(item, user, pointed_thing)
       charge =charge-400;   
      charge=set_RE_item_load(charge,laser_mk1_max_charge)
      item["wear"]=tostring(charge)
      item["metadata"]=tostring(charge)
      charge=set_RE_wear(item,charge,laser_mk1_max_charge)
      itemstack:replace(item)
      end
      return itemstack
@@ -88,7 +112,7 @@
   visual_size = {x=0.2, y=0.2},
   textures = {"technic:laser_beam_box"},
   lastpos={},
   max_range=15,
   max_range=10,
   count=0,
--   digger=nil,
   collisionbox = {0,0,0,0,0,0},
@@ -97,11 +121,15 @@
LASER_BEAM_ENTITY.on_step = function(self, dtime)
   self.timer=self.timer+dtime
   local pos = self.object:getpos()
   local node = minetest.env:get_node(pos)
   if self.lastpos.x~=nil then lazer_it (pos, node, self.player) end
   if self.player~=nil then if self.lastpos.x~=nil then lazer_it (pos, self.player) end end
   if self.lastpos.x ~=nil and self.lastpos.y ~=nil and self.lastpos.y ~=nil then
         temp1={x=math.floor(self.lastpos.x),y=math.floor(self.lastpos.y),z=math.floor(self.lastpos.z)}
         temp2={x=math.floor(pos.x),y=math.floor(pos.y),z=math.floor(pos.z)}
         if temp1.x==temp2.x and temp1.y==temp2.y and temp1.z==temp2.z then return end
         end
   self.lastpos={x=pos.x, y=pos.y, z=pos.z}   
   self.count=self.count+1
   if self.count>=self.max_range then self.object:remove() end
   if self.count==self.max_range then self.object:remove() end
end
LASER_BEAM_ENTITYV={
@@ -119,9 +147,12 @@
LASER_BEAM_ENTITYV.on_step = function(self, dtime)
   self.timer=self.timer+dtime
   local pos = self.object:getpos()
   local node = minetest.env:get_node(pos)
   if self.lastpos.x~=nil then lazer_it (pos, node, self.player,self.count) end
   if self.player~=nil then if self.lastpos.x~=nil then lazer_it (pos, self.player) end end
   if self.lastpos.x ~=nil and self.lastpos.y ~=nil and self.lastpos.y ~=nil then
         temp1={x=math.floor(self.lastpos.x),y=math.floor(self.lastpos.y),z=math.floor(self.lastpos.z)}
         temp2={x=math.floor(pos.x),y=math.floor(pos.y),z=math.floor(pos.z)}
         if temp1.x==temp2.x and temp1.y==temp2.y and temp1.z==temp2.z then return end
         end
   self.lastpos={x=pos.x, y=pos.y, z=pos.z}   
   self.count=self.count+1
   if self.count==self.max_range then self.object:remove() end
@@ -131,19 +162,13 @@
minetest.register_entity("technic:laser_beam_entity", LASER_BEAM_ENTITY)
minetest.register_entity("technic:laser_beam_entityV", LASER_BEAM_ENTITYV)
function lazer_it (pos, node, player,count)
   if node.name == "air" or node.name == "ignore" then return end
   if node.name == "default:lava_source" then return end
   if node.name == "default:lava_flowing" then return end
   if node.name == "default:water_source" then minetest.env:remove_node(pos) return end
   if node.name == "default:water_flowing" then minetest.env:remove_node(pos) return end
   pos1={}
   pos1.x=math.floor(pos.x)
   pos1.y=math.floor(pos.y)
   pos1.z=math.floor(pos.z)
   if player then
      minetest.node_dig(pos1,node,player)
   end
function lazer_it (pos, player)
   local pos1={}
--   pos1.x=math.floor(pos.x)
--   pos1.y=math.floor(pos.y)
--   pos1.z=math.floor(pos.z)
   local node = minetest.env:get_node(pos)
   if node.name == "air" or node.name == "ignore" or node.name == "default:lava_source" or node.name == "default:lava_flowing" then return end
   if node.name == "default:water_source" or node.name == "default:water_flowing" then minetest.env:remove_node(pos) return end
   if player then minetest.node_dig(pos,node,player) end
end