diff options
author | stujones11 <stujones111@gmail.com> | 2016-01-24 16:43:36 +0000 |
---|---|---|
committer | stujones11 <stujones111@gmail.com> | 2016-01-24 16:43:36 +0000 |
commit | 616261cc839b46ff1d23523359273e07cdffee0c (patch) | |
tree | d88e60538c723ba1c592d263f492b32b505841de /railcart | |
parent | a23e20f05301849a7902c6b11c55e2a581b0831b (diff) |
Simple collision with stationary carts
Diffstat (limited to 'railcart')
-rw-r--r-- | railcart/init.lua | 19 | ||||
-rw-r--r-- | railcart/railcart.lua | 80 |
2 files changed, 82 insertions, 17 deletions
diff --git a/railcart/init.lua b/railcart/init.lua index 07edea3..d9b543b 100644 --- a/railcart/init.lua +++ b/railcart/init.lua @@ -154,26 +154,27 @@ minetest.register_craftitem("railcart:cart", { if not name or pointed_thing.type ~= "node" then return end - local pos = pointed_thing.under - if not railtrack:is_railnode(pos) then - return - end if not minetest.is_singleplayer() then if not minetest.check_player_privs(name, {carts=true}) then minetest.chat_send_player(name, "Requires carts privilege") return end end - local cons = railtrack:get_connections(pos) + local pos = pointed_thing.under + if not railtrack:is_railnode(pos) then + return + end + local carts = railcart:get_carts_in_radius(pos, 0.9) + if #carts > 0 then + return + end local cart = railcart.cart:new() cart.id = railcart:get_new_id() cart.inv = railcart:create_detached_inventory(cart.id) - cart.pos = pos + cart.pos = vector.new(pos) cart.prev = vector.new(pos) cart.accel = railtrack:get_acceleration(pos) - if cons[1] then - cart.target = cons[1] - end + cart.dir = railcart:get_rail_direction(pos) table.insert(railcart.allcarts, cart) railcart:save() if not minetest.setting_getbool("creative_mode") then diff --git a/railcart/railcart.lua b/railcart/railcart.lua index c80b507..8f89649 100644 --- a/railcart/railcart.lua +++ b/railcart/railcart.lua @@ -14,7 +14,6 @@ railcart = { railcart.cart = { id = nil, - entity = {}, pos = nil, target = nil, prev = nil, @@ -84,7 +83,6 @@ function railcart:save() end end ref.inv = inv - ref.entity = nil table.insert(carts, ref) end local output = io.open(minetest.get_worldpath().."/railcart.txt",'w') @@ -126,6 +124,22 @@ function railcart:remove_cart(id) end end +function railcart:get_rail_direction(pos) + local target = nil + local cons = railtrack:get_connections(pos) + local ymax = pos.y + for _, con in pairs(cons) do + if con.y >= ymax then + ymax = con.y + target = con + end + end + if target then + return railtrack:get_direction(target, pos) + end + return {x=0, y=0, z=0} +end + function railcart:get_new_id() local id = 0 for _, cart in pairs(railcart.allcarts) do @@ -149,6 +163,39 @@ function railcart:get_cart_entity(id) return cart_ref end +function railcart:get_carts_in_radius(pos, rad) + local carts = {} + for _, cart in pairs(railcart.allcarts) do + local px = pos.x - cart.pos.x + local py = pos.y - cart.pos.y + local pz = pos.z - cart.pos.z + if (px * px) + (py * py) + (pz * pz) <= rad * rad then + table.insert(carts, cart) + end + end + return carts +end + +function railcart:get_cart_in_sight(p1, p2) + local ref = nil + local dist = railtrack:get_distance(p1, p2) + 1 + local dir = railtrack:get_direction(p2, p1) + local carts = railcart:get_carts_in_radius(p1, dist) + for _, cart in pairs(carts) do + if not vector.equals(p1, cart.pos) then + local dc = railtrack:get_direction(cart.pos, p1) + if vector.equals(dc, dir) then + local d = railtrack:get_distance(p1, cart.pos) + if d < dist then + dist = d + ref = cart + end + end + end + end + return ref +end + function railcart:get_delta_time(vel, acc, dist) if vel > 0 then if acc == 0 then @@ -236,11 +283,23 @@ function railcart:update(cart, time, object) end local speed = railcart:velocity_to_speed(cart.vel) if cart.target then - cart.dir = railtrack:get_direction(cart.target, cart.pos) + if vector.equals(cart.target, cart.pos) then + cart.dir = railcart:get_rail_direction(cart.pos) + else + cart.dir = railtrack:get_direction(cart.target, cart.pos) + end else speed = 0 end if speed > RAILCART_SPEED_MIN then + local blocked = false + local cis = railcart:get_cart_in_sight(cart.pos, cart.target) + if cis then + if railcart:velocity_to_speed(cis.vel) == 0 then + cart.target = vector.subtract(cis.pos, cart.dir) + blocked = true + end + end local d1 = railtrack:get_distance(cart.prev, cart.target) local d2 = railtrack:get_distance(cart.prev, cart.pos) local dist = d1 - d2 @@ -273,11 +332,16 @@ function railcart:update(cart, time, object) cart.pos = vector.add(cart.pos, vector.multiply(cart.dir, dp)) end else - cart.pos = vector.new(cart.target) - cart.prev = vector.new(cart.target) - cart.accel = railtrack:get_acceleration(cart.target) - cart.target = nil - return 0 + if blocked and vector.equals(cart.target, cart.prev) then + cart.vel = {x=0, y=0, z=0} + cart.acc = {x=0, y=0, z=0} + else + cart.pos = vector.new(cart.target) + cart.prev = vector.new(cart.target) + cart.accel = railtrack:get_acceleration(cart.target) + cart.target = nil + return 0 + end end else cart.vel = {x=0, y=0, z=0} |