]> git.lizzy.rs Git - Crafter.git/blobdiff - mods/minecart/init.lua
Add in prototype furnace engine
[Crafter.git] / mods / minecart / init.lua
index 4b907055636e45fa72036b91619f1886afb75f2d..670fce60ac07766f03495e5b3823adbe48ad4a7b 100644 (file)
@@ -23,9 +23,33 @@ local dirs = {
        {x= 0,y=-1,z=-1},
 }
 
-local axis_order = {
+local function coupling_particles(pos,truth)
+       local color = "red"
+       if truth then
+               color = "green"
+       end
+
+       minetest.add_particlespawner({
+               amount = 15,
+               time = 0.001,
+               minpos = pos,
+               maxpos = pos,
+               minvel = vector.new(-10,-10,-10),
+               maxvel = vector.new(10,10,10),
+               minacc = {x=0, y=0, z=0},
+               maxacc = {x=0, y=0, z=0},
+               minexptime = 1.1,
+               maxexptime = 1.5,
+               minsize = 1,
+               maxsize = 2,
+               collisiondetection = false,
+               collision_removal = false,
+               vertical = false,
+               texture = "couple_particle.png^[colorize:"..color..":200",
+               glow = 14,
+       })
+end
 
-}
 local function data_injection(pos,data)
        if data then
                pool[minetest.hash_node_position(pos)] = true
@@ -34,6 +58,21 @@ local function data_injection(pos,data)
        end
 end
 
+local function speed_limiter(self,speed)
+       local test = self.object:get_velocity()--vector.multiply(self.velocity,new_vel)
+
+       if test.x > speed then
+               test.x = speed
+       elseif test.x < -speed then
+               test.x = -speed
+       end
+       if test.z > speed then
+               test.z = speed
+       elseif test.z < -speed then
+               test.z = -speed         
+       end
+       self.object:set_velocity(test)
+end
 
 local function create_axis(pos)
        local possible_dirs = {}
@@ -54,13 +93,13 @@ local function collision_detect(self)
                        local pos2 = object:get_pos()
                        if self.axis_lock == "x" then
 
-                               local velocity = (1-vector.distance(vector.new(pos.x,0,0),vector.new(pos2.x,0,0)))
+                               local velocity = (1-vector.distance(vector.new(pos.x,0,0),vector.new(pos2.x,0,0)))*5
                                local dir = vector.direction(vector.new(pos2.x,0,0),vector.new(pos.x,0,0))
                                local new_vel = vector.multiply(dir,velocity)
                                self.object:add_velocity(new_vel)
                                self.dir = dir
                        elseif self.axis_lock == "z" then
-                               local velocity = (1-vector.distance(vector.new(0,0,pos.z),vector.new(0,0,pos2.z)))
+                               local velocity = (1-vector.distance(vector.new(0,0,pos.z),vector.new(0,0,pos2.z)))*5
                                local dir = vector.direction(vector.new(0,0,pos2.z),vector.new(0,0,pos.z))
                                local new_vel = vector.multiply(dir,velocity)
                                self.object:add_velocity(new_vel)
@@ -162,157 +201,202 @@ local function straight_snap(pos,self,dir)
        return(false)
 end
 
+
+local function coupling_logic(self)
+       
+       if not self.axis_lock then return end
+
+       if not self.coupler1 then return end
+
+       if self.dir.y ~= 0 then return end
+
+       local pos = self.object:get_pos()
+       
+       local pos2 = self.coupler1:get_pos()
+
+       if self.axis_lock == "x" then
+               local velocity_real = self.object:get_velocity()
+               local distance = vector.distance(pos,pos2)
+               local new_vel = vector.new(0,0,0)
+               if distance > 1.5 then
+                       local velocity = (distance-1)
+                       local dir = vector.direction(vector.new(pos.x,0,0),vector.new(pos2.x,0,0))
+                       self.dir = dir
+                       new_vel = vector.multiply(dir,velocity)
+               else
+                       new_vel = vector.multiply(velocity_real,-1)
+               end
+               self.object:add_velocity(new_vel)
+       elseif self.axis_lock == "z" then
+               local velocity_real = self.object:get_velocity()
+               local distance = vector.distance(pos,pos2)
+               local new_vel = vector.new(0,0,0)
+               if distance > 1.5 then
+                       local velocity = (distance-1)
+                       local dir = vector.direction(vector.new(0,0,pos.z),vector.new(0,0,pos2.z))
+                       self.dir = dir
+                       new_vel = vector.multiply(dir,velocity)
+               else
+                       new_vel = vector.multiply(velocity_real,-1)
+               end
+               self.object:add_velocity(new_vel)
+       end
+
+       return
+end
+
+
 local function rail_brain(self,pos)
-       if not self.dir then return end
 
-       --if self.dir then print(dump(self.dir)) end
+
+       if not self.dir then self.dir = vector.new(0,0,0) end
 
        local pos2 = self.object:get_pos()
 
        local dir = self.dir
 
-       local triggered = false
-
-       if     dir.x < 0 and pos2.x < pos.x then
-               triggered = true
-       elseif dir.x > 0 and pos2.x > pos.x then
-               triggered = true
-       elseif dir.z < 0 and pos2.z < pos.z then
-               triggered = true
-       elseif dir.z > 0 and pos2.z > pos.z then
-               triggered = true
-       end
+       speed_limiter(self,6)
 
-       if triggered and not pool[minetest.hash_node_position(vector.add(pos,dir))] then
+       if not pool[minetest.hash_node_position(vector.add(pos,dir))] then
 
                if straight_snap(pos,self,dir) then
                        return
                end
 
                local possible_dirs = create_axis(pos)
-               
+
                if table.getn(possible_dirs) == 0 then
-                       --print("train fails")
-                       --stop slow down become physical, something
+                       --stop slow down become physical
                else
                        for _,dir2 in pairs(possible_dirs) do
-                               if climb_snap(pos,self,dir,dir2) then
+                               if turn_snap(pos,self,dir,dir2) then
                                        return
                                end
-                       end
-                       
-                       for _,dir2 in pairs(possible_dirs) do
-                               if turn_snap(pos,self,dir,dir2) then
+                               if climb_snap(pos,self,dir,dir2) then
                                        return
                                end
                        end
                end
+       else
+               coupling_logic(self)
        end
-end
 
-
-local function coupling_logic(self)
-       
-       if not self.axis_lock then return end
-
-       if not self.coupler1 then return end
-
-       if not self.dir.y == 0 then print("failing") return end
-
-       local pos = self.object:get_pos()
-       
-       local pos2 = self.coupler1:get_pos()
-
-       if self.axis_lock == "x" then
-               --local velocity = self.object:get_velocity()
-
-               local distance = 1-vector.distance(pos,pos2)            
-
-               local dir = vector.direction(vector.new(pos2.x,0,0),vector.new(pos.x,0,0))
-
-               local new_vel = vector.multiply(dir,distance)
-               self.object:add_velocity(new_vel)
-               --self.dir = dir
-       --[[
-       elseif self.axis_lock == "z" then
-               local velocity = self.object:get_velocity()
-               local velocity = (1-vector.distance(pos,pos2))
-               local dir = vector.direction(vector.new(0,0,pos2.z),vector.new(0,0,pos.z))
-               local new_vel = vector.multiply(dir,velocity)
-               self.object:add_velocity(new_vel)
-               --self.dir = dir
-               ]]--
-       end
-       return
 end
 
-
 local minecart = {}
 
 minecart.on_step = function(self,dtime)
-       local float_pos = self.object:get_pos()
-       local pos = vector.round(float_pos)
-
-       --if self.velocity then
-               --local new_vel = dtime*1000
-               local test = self.object:get_velocity()--vector.multiply(self.velocity,new_vel)
-
-               if test.x > 10 then
-                       test.x = 10
-                       print("slowing down 1")
-               elseif test.x < -10 then
-                       test.x = -10
-                       print("slowing down 2")
-               end
-               if test.z > 10 then
-                       test.z = 10
-                       print("slowing down 3")
-               elseif test.z < -10 then
-                       test.z = -10
-                       print("slowing down 4")
-                       
-               end
-               self.object:set_velocity(test)
-               --self.object:move_to(vector.add(float_pos,test))
-       --end
-
+       local pos = vector.round(self.object:get_pos())
        if not self.axis_lock then
                local possible_dirs = create_axis(pos)
                for _,dir in pairs(possible_dirs) do
                        if dir.x ~=0 then
                                self.axis_lock = "x"
-                               self.dir = vector.new(1,0,0)
-                               --self.velocity = vector.new(0,0,0)
+                               self.dir = dir
                                direction_snap(self)
                                break
                        elseif dir.z ~= 0 then
                                self.axis_lock = "z"
-                               self.dir = vector.new(0,0,1)
-                               --self.velocity = vector.new(0,0,0)
+                               self.dir = dir
                                direction_snap(self)
                                break
                        end
                end
        else
+               rail_brain(self,pos)
+               --collision_detect(self)
+       end
+end
 
-               collision_detect(self)
-
-               coupling_logic(self)
 
-               rail_brain(self,pos)
+minecart.on_punch = function(self, puncher)
+       if not puncher:get_wielded_item():get_name() == "minecart:wrench" then
+               return
        end
-       self.old_pos = float_pos
+       if self.furnace then
+               self.object:set_velocity(vector.multiply(self.dir,6))
+               minetest.add_particlespawner({
+                       amount = 30,
+                       time = 0,
+                       minpos = vector.new(0,0.5,0),
+                       maxpos = vector.new(0,0.5,0),
+                       minvel = vector.new(0,0,0),
+                       maxvel = vector.new(0,0,0),
+                       minacc = {x=0, y=3, z=0},
+                       maxacc = {x=0, y=5, z=0},
+                       minexptime = 1.1,
+                       maxexptime = 1.5,
+                       minsize = 1,
+                       maxsize = 2,
+                       collisiondetection = false,
+                       collision_removal = false,
+                       vertical = false,
+                       texture = "smoke.png",
+                       attached = self.object
+               })
+       end
+
 end
 
+
 minecart.on_rightclick = function(self,clicker)
+       local pos = self.object:get_pos()
+       if clicker:get_wielded_item():get_name() == "utility:furnace" then
+               local obj = minetest.add_entity(pos, "minecart:furnace")
+               obj:set_attach(self.object,"",vector.new(0,0,0),vector.new(0,0,0))
+               minetest.sound_play("wrench",{
+                       object = self.object,
+                       gain = 1.0,
+                       max_hear_distance = 64,
+               })
+               coupling_particles(pos,true)
+               self.furnace = true
+               return
+       end
+
+       if not clicker:get_wielded_item():get_name() == "minecart:wrench" then
+               return
+       end
+
        local name = clicker:get_player_name()
        if not pool[name] then
-               pool[name] = self.object
+               if not self.coupler2 then
+                       pool[name] = self.object
+                       minetest.sound_play("wrench",{
+                               object = self.object,
+                               gain = 1.0,
+                               max_hear_distance = 64,
+                       })
+                       coupling_particles(pos,true)
+               else
+                       minetest.sound_play("wrench",{
+                               object = self.object,
+                               gain = 1.0,
+                               max_hear_distance = 64,
+                               pitch = 0.7,
+                       })
+                       coupling_particles(pos,false)
+               end
        else
-               self.coupler1 = pool[name]
-               --pool[name]:get_luaentity().coupler1 = self.object
+               if not (pool[name]:get_luaentity().coupler1 and pool[name]:get_luaentity().coupler1 == self.object or self.coupler2) then
+                       self.coupler1 = pool[name]
+                       pool[name]:get_luaentity().coupler2 = self.object
+                       minetest.sound_play("wrench",{
+                               object = self.object,
+                               gain = 1.0,
+                               max_hear_distance = 64,
+                       })
+                       coupling_particles(pos,true)
+               else
+                       minetest.sound_play("wrench",{
+                               object = self.object,
+                               gain = 1.0,
+                               max_hear_distance = 64,
+                               pitch = 0.7,
+                       })
+                       coupling_particles(pos,false)
+               end
                pool[name] = nil
-               print("coupled")
        end
 end
 
@@ -346,12 +430,6 @@ minecart.initial_properties = {
        textures = {"minecart.png"},
 }
 
-
-minecart.on_punch = function(self,puncher, time_from_last_punch, tool_capabilities, dir, damage)
-       --local obj = minetest.add_item(self.object:getpos(), "minecart:minecart")
-       --self.object:remove()
-end
-
        
 
 minetest.register_entity("minecart:minecart", minecart)
@@ -441,7 +519,6 @@ minetest.register_lbm({
        run_at_every_load = true,
        action = function(pos)
                data_injection(pos,true)
-               --print("buildin dat cashay")
        end,
 })
 
@@ -453,3 +530,46 @@ minetest.register_craft({
                {"main:iron","","main:iron"}
        }
 })
+
+
+minetest.register_food("minecart:wrench",{
+       description = "Train Wrench",
+       texture = "wrench.png",
+})
+
+minetest.register_craft({
+       output = "minecart:wrench",
+       recipe = {
+               {"main:iron", "", "main:iron"},
+               {"main:iron", "main:lapis", "main:iron"},
+               {"", "main:lapis", ""}
+       }
+})
+
+
+
+minetest.register_entity("minecart:furnace", {
+       initial_properties = {
+               visual = "wielditem",
+               visual_size = {x = 0.6, y = 0.6},
+               textures = {},
+               physical = true,
+               is_visible = false,
+               collide_with_objects = false,
+               pointable=false,
+               collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
+       },
+       set_node = function(self)
+               self.object:set_properties({
+                       is_visible = true,
+                       textures = {"utility:furnace"},
+               })
+       end,
+
+
+       on_activate = function(self, staticdata)
+               self.object:set_armor_groups({immortal = 1})
+
+               self:set_node()
+       end,
+})