]> git.lizzy.rs Git - Crafter.git/blobdiff - mods/minecart/init.lua
Rebalance sounds pt 1
[Crafter.git] / mods / minecart / init.lua
index 7456a94dfaf14a7274ed97c9c67d461e310b13e2..3016b041330b4e0bf1145bad4ddec3fa195e42bf 100644 (file)
@@ -2,27 +2,18 @@
 
 Basic idealogy
 
-goal - > go to goal - > check all this and repeat
-
-
-minecart does check axis dir then -1 1 on opposite axis (x and z)
-
-minecart checks in front and above 
-
-minecart checks in front and below
+keep it simple stupid
 
-if in front above start moving up
+make cart make noise
 
-if in front below start moving down
+make player able to push cart
 
-minecart checks if rail in front then if not check sides and if none then stop
+make carts push each other away --repel axis
 
-if a rail in front of minecart then when past center of node center, turn towards the available rail and then recenter self onto rail on the new axis
+make chest and furnace cart
 
 
-keep it simple stupid
-
-make cart make noise
+minetest.dir_to_facedir(dir, is6d) - to get player direction converted and to convert the minecart direction
 
 ]]--
 local path = minetest.get_modpath("minecart")
@@ -30,12 +21,16 @@ dofile(path.."/rail.lua")
 
 
 
+local function is_rail(x,y,z)
+       return(minetest.get_node_group(minetest.get_node(vector.new(x,y,z)).name,"rail")>0)
+end
+
 local minecart = {
        initial_properties = {
                physical = false, -- otherwise going uphill breaks
-               collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
+               collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},--{-0.5, -0.4, -0.5, 0.5, 0.25, 0.5},
                visual = "mesh",
-               mesh = "minecart.b3d",
+               mesh = "minecart.obj",
                visual_size = {x=1, y=1},
                textures = {"minecart.png"},
                automatic_face_movement_dir = 90.0,
@@ -43,6 +38,7 @@ local minecart = {
 
        rider = nil,
        punched = false,
+       speed = 0,
        
 }
 
@@ -51,12 +47,15 @@ function minecart:on_rightclick(clicker)
                return
        end
        local player_name = clicker:get_player_name()
+       
        if self.rider and player_name == self.rider then
                self.rider = nil
-               carts:manage_attachment(clicker, nil)
+               --carts:manage_attachment(clicker, nil)
        elseif not self.rider then
                self.rider = player_name
-               carts:manage_attachment(clicker, self.object)
+               clicker:set_attach(self.object, "", {x=0, y=-4.5, z=0}, {x=0, y=0, z=0})
+               --player:set_eye_offset({x=0, y=-4, z=0},{x=0, y=-4, z=0})
+               --carts:manage_attachment(clicker, self.object)
 
                -- player_api does not update the animation
                -- when the player is attached, reset to default animation
@@ -85,6 +84,8 @@ function minecart:get_staticdata()
        })
 end
 function minecart:on_punch(puncher, time_from_last_punch, tool_capabilities, dir, damage)
+       local obj = minetest.add_item(self.object:getpos(), "minecart:minecart")
+       obj:get_luaentity().collection_timer = 2
        self.object:remove()
 end
 
@@ -96,20 +97,169 @@ end
 --repel from players on track "push"
 function minecart:push(self)
        local pos = self.object:getpos()
-       for _,object in ipairs(minetest.get_objects_inside_radius(pos, 2)) do
-               if object:is_player() then
+       for _,object in ipairs(minetest.get_objects_inside_radius(pos, 1)) do
+               if object:is_player() and object:get_player_name() ~= self.rider then
                        local player_pos = object:getpos()
+                       pos.y = 0
+                       player_pos.y = 0
+                       
+                       local vel = vector.subtract(pos, player_pos)
+                       vel = vector.normalize(vel)
+                       local distance = vector.distance(pos,player_pos)
+                       
+                       distance = (1-distance)*10
+                       
+                       vel = vector.multiply(vel,distance)
+                       
+                       --only push if it's faster than current speed
+                       if distance > self.speed then
+                               self.object:setvelocity(vel)
+                               self.speed = distance
+                       end             
                end
        end
 end
 
 function minecart:ride_rail(self)
-       if self.goal then
-               print("goal: "..dump(self.goal))
+       local pos = vector.floor(vector.add(self.object:getpos(),0.5))
+       local speed = self.speed
+       if self.speed > 10 then
+               self.speed = 10
        end
-end
-
+       
+       local vel = self.object:getvelocity()
+       local x = math.abs(vel.x)
+       local z = math.abs(vel.z)
+       local xdir
+       local zdir
+       local dir = {x=0,y=0,z=0}
+       
+       --check direction
+       --x axis
+       if x > z then
+               if vel.x>0 then xdir=1 elseif vel.x<0 then xdir=-1 end
+               
+               --print(minetest.get_node(vector.new(pos.x,pos.y,pos.z)).name)
+               
+               --go up
+               if is_rail(pos.x+xdir,pos.y+1,pos.z) or (not is_rail(pos.x,pos.y,pos.z) and is_rail(pos.x+xdir,pos.y,pos.z)) then
+                       --print("up")
+                       dir.y = speed
+                       dir.x = xdir*speed
+
+               --go down
+               elseif (is_rail(pos.x,pos.y-1,pos.z) or vel.y < 0) and not is_rail(pos.x+xdir,pos.y,pos.z) then
+                       --print("down")
+                       dir.y = -speed
+                       dir.x = xdir*speed
+               
+               --go flat
+               elseif is_rail(pos.x,pos.y,pos.z) then --currently on rail
+                       --print("flat")
+                       --print("forward inside")
+                       --correct y position
+                       if dir.y == 0 and self.object:getpos().y ~= pos.y then
+                               --print("correcting y")
+                               local posser = self.object:getpos()
+                               self.object:moveto(vector.new(posser.x,pos.y,posser.z))
+                       end
+                       dir.x = xdir*speed
+               end
+       --z axis
+       elseif z > x then
+               if vel.z>0 then zdir=1 elseif vel.z<0 then zdir=-1 end
+               
+               --print(minetest.get_node(vector.new(pos.x,pos.y,pos.z)).name)
+               
+               --go up
+               if is_rail(pos.x,pos.y+1,pos.z+zdir) or (not is_rail(pos.x,pos.y,pos.z) and is_rail(pos.x,pos.y,pos.z+zdir)) then
+                       --print("up")
+                       dir.y = speed
+                       dir.z = zdir*speed
+               
+               --go down
+               elseif (is_rail(pos.x,pos.y-1,pos.z) or vel.y < 0) and not is_rail(pos.x,pos.y,pos.z+zdir) then
+                       --print("down")
+                       dir.y = -speed
+                       dir.z = zdir*speed
+               
+               
+               --go flat
+               elseif is_rail(pos.x,pos.y,pos.z) then --currently on rail
+                       --print("flat")
+                       --print("forward inside")
+                       --correct y position
+                       if dir.y == 0 and self.object:getpos().y ~= pos.y then
+                               --print("correcting y")
+                               local posser = self.object:getpos()
+                               self.object:moveto(vector.new(posser.x,pos.y,posser.z))
+                       end
+                       dir.z = zdir*speed
+               end
+       end
+       --turn
+       local turnx = 0
+       local turnz = 0
+       
+       if vel.x>0 then turnx=1 elseif vel.x<0 then turnx=-1 end
+       if vel.z>0 then turnz=1 elseif vel.z<0 then turnz=-1 end
+       
 
+       if turnx and turnz and dir.y == 0 and not vector.equals(dir, vector.new(0,0,0)) and not is_rail(pos.x+turnx,pos.y-1,pos.z+turnz) and not is_rail(pos.x+turnx,pos.y,pos.z+turnz) and not is_rail(pos.x+turnx,pos.y+1,pos.z+turnz) then
+               if x > z then
+                       for y = -1,1 do
+                               if is_rail(pos.x,pos.y+y,pos.z+1) then
+                                       dir.z = speed
+                                       dir.x = 0
+                                       --recenter on the rail
+                                       self.object:moveto(pos)
+                               elseif is_rail(pos.x,pos.y+y,pos.z-1) then
+                                       dir.z = -speed
+                                       dir.x = 0
+                                       --recenter on the rail
+                                       self.object:moveto(pos)
+                               end
+                       end
+               elseif z > x then
+                       for y = -1,1 do
+                               if is_rail(pos.x+1,pos.y+y,pos.z) then
+                                       dir.x = speed
+                                       dir.z = 0
+                                       --recenter on the rail
+                                       self.object:moveto(pos)
+                               elseif is_rail(pos.x-1,pos.y+y,pos.z) then
+                                       dir.x = -speed
+                                       dir.z = 0
+                                       --recenter on the rail
+                                       self.object:moveto(pos)
+                               end
+                       end
+               end
+               
+       end
+       --apply
+       --if not vector.equals(dir,vector.new(0,0,0)) then
+       self.object:setvelocity(dir)
+       --end
+       self.oldpos=self.object:getpos()
+       
+       --make the cart move up and down on hills
+       self.object:set_properties({mesh="minecart.obj"})
+       if vel.y <0  then
+               self.object:set_properties({mesh="minecart_down.obj"})
+       elseif vel.y > 0 then
+               self.object:set_properties({mesh="minecart_up.obj"})
+       end
+       
+       --slow it down
+       if self.speed > 0 then
+               self.speed = self.speed - 0.01
+       end
+       if self.speed < 0 then
+               self.speed = 0
+       end
+       
+end
 
 function minecart:on_step(dtime)
        minecart:push(self)
@@ -119,24 +269,6 @@ end
 minetest.register_entity("minecart:minecart", minecart)
 
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
 minetest.register_craftitem("minecart:minecart", {
        description = "Minecart",
        inventory_image = "minecartitem.png",