2014-08-04 19:06:02 +02:00
|
|
|
-- TODO:
|
2014-08-10 14:03:13 +02:00
|
|
|
-- Add a todo list
|
2014-07-22 23:15:43 +02:00
|
|
|
|
|
|
|
boost_cart = {}
|
|
|
|
boost_cart.modpath = minetest.get_modpath("boost_cart")
|
2015-01-11 11:57:49 +01:00
|
|
|
boost_cart.speed_max = 10
|
2014-07-29 12:51:57 +02:00
|
|
|
|
|
|
|
function vector.floor(v)
|
|
|
|
return {
|
|
|
|
x = math.floor(v.x),
|
|
|
|
y = math.floor(v.y),
|
|
|
|
z = math.floor(v.z)
|
|
|
|
}
|
|
|
|
end
|
2014-07-22 23:15:43 +02:00
|
|
|
|
|
|
|
dofile(boost_cart.modpath.."/functions.lua")
|
|
|
|
dofile(boost_cart.modpath.."/rails.lua")
|
|
|
|
|
|
|
|
boost_cart.cart = {
|
|
|
|
physical = false,
|
|
|
|
collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
|
|
|
|
visual = "mesh",
|
|
|
|
mesh = "cart.x",
|
|
|
|
visual_size = {x=1, y=1},
|
|
|
|
textures = {"cart.png"},
|
|
|
|
|
|
|
|
driver = nil,
|
2015-01-11 11:57:49 +01:00
|
|
|
update = false, -- used to re-send velocity and position
|
2014-07-22 23:15:43 +02:00
|
|
|
velocity = {x=0, y=0, z=0}, -- only used on punch
|
|
|
|
old_dir = {x=0, y=0, z=0},
|
2014-08-09 17:37:02 +02:00
|
|
|
old_pos = nil,
|
2014-10-25 18:49:28 +02:00
|
|
|
old_switch = nil,
|
|
|
|
attached_items = {}
|
2014-07-22 23:15:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function boost_cart.cart:on_rightclick(clicker)
|
|
|
|
if not clicker or not clicker:is_player() then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local player_name = clicker:get_player_name()
|
|
|
|
if self.driver and player_name == self.driver then
|
|
|
|
self.driver = nil
|
|
|
|
clicker:set_detach()
|
|
|
|
elseif not self.driver then
|
|
|
|
self.driver = player_name
|
2014-11-08 20:11:51 +01:00
|
|
|
default.player_attached[player_name] = true
|
2014-08-10 17:22:46 +02:00
|
|
|
clicker:set_attach(self.object, "", {x=0, y=3, z=0}, {x=0, y=0, z=0})
|
2014-07-22 23:15:43 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function boost_cart.cart:on_activate(staticdata, dtime_s)
|
|
|
|
self.object:set_armor_groups({immortal=1})
|
|
|
|
end
|
|
|
|
|
|
|
|
function boost_cart.cart:on_punch(puncher, time_from_last_punch, tool_capabilities, direction)
|
|
|
|
if not puncher or not puncher:is_player() then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if puncher:get_player_control().sneak then
|
|
|
|
if self.driver then
|
2014-10-25 18:49:28 +02:00
|
|
|
if self.old_pos then
|
|
|
|
self.object:setpos(self.old_pos)
|
|
|
|
end
|
2014-08-09 17:37:02 +02:00
|
|
|
default.player_attached[self.driver] = nil
|
2014-07-22 23:15:43 +02:00
|
|
|
local player = minetest.get_player_by_name(self.driver)
|
|
|
|
if player then
|
|
|
|
player:set_detach()
|
|
|
|
end
|
|
|
|
end
|
2014-10-25 18:49:28 +02:00
|
|
|
for _,obj_ in ipairs(self.attached_items) do
|
|
|
|
if obj_ then
|
|
|
|
obj_:set_detach()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-14 08:59:54 +02:00
|
|
|
self.object:remove()
|
2014-08-04 19:06:02 +02:00
|
|
|
puncher:get_inventory():add_item("main", "carts:cart")
|
2014-07-22 23:15:43 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-08-10 14:03:13 +02:00
|
|
|
local vel = self.object:getvelocity()
|
2014-08-04 19:06:02 +02:00
|
|
|
if puncher:get_player_name() == self.driver then
|
2015-01-11 11:57:49 +01:00
|
|
|
if math.abs(vel.x + vel.z) > 7 then
|
2014-07-22 23:15:43 +02:00
|
|
|
return
|
|
|
|
end
|
2014-08-04 19:06:02 +02:00
|
|
|
end
|
2014-07-22 23:15:43 +02:00
|
|
|
|
2015-01-11 11:57:49 +01:00
|
|
|
local punch_dir = boost_cart:velocity_to_dir(puncher:get_look_dir())
|
|
|
|
punch_dir.y = 0
|
|
|
|
local cart_dir = boost_cart:get_rail_direction(self.object:getpos(), punch_dir)
|
|
|
|
if vector.equals(cart_dir, {x=0, y=0, z=0}) then
|
2014-08-10 17:03:40 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-01-11 11:57:49 +01:00
|
|
|
time_from_last_punch = math.min(time_from_last_punch, tool_capabilities.full_punch_interval)
|
|
|
|
local f = 3 * (time_from_last_punch / tool_capabilities.full_punch_interval)
|
|
|
|
|
|
|
|
self.velocity = vector.multiply(cart_dir, f)
|
2014-08-01 15:25:48 +02:00
|
|
|
self.old_pos = nil
|
2015-01-11 11:57:49 +01:00
|
|
|
self.update = true
|
2014-07-22 23:15:43 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function boost_cart.cart:on_step(dtime)
|
|
|
|
local vel = self.object:getvelocity()
|
2015-01-11 11:57:49 +01:00
|
|
|
local send_pos = (not self.update)
|
|
|
|
if self.update then
|
2014-07-22 23:15:43 +02:00
|
|
|
vel = vector.add(vel, self.velocity)
|
2015-01-11 11:57:49 +01:00
|
|
|
self.object:setvelocity(vel)
|
2014-08-01 15:25:48 +02:00
|
|
|
elseif vector.equals(vel, {x=0, y=0, z=0}) then
|
2014-07-22 23:15:43 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-08-09 17:37:02 +02:00
|
|
|
local dir, last_switch = nil, nil
|
2014-07-22 23:15:43 +02:00
|
|
|
local pos = self.object:getpos()
|
2015-01-11 11:57:49 +01:00
|
|
|
if self.old_pos and not self.update then
|
2014-08-01 17:45:07 +02:00
|
|
|
local flo_pos = vector.floor(pos)
|
|
|
|
local flo_old = vector.floor(self.old_pos)
|
|
|
|
if vector.equals(flo_pos, flo_old) then
|
2014-08-01 15:25:48 +02:00
|
|
|
return
|
|
|
|
end
|
2014-08-07 09:54:25 +02:00
|
|
|
end
|
2015-01-11 11:57:49 +01:00
|
|
|
|
|
|
|
local update = false
|
2014-08-09 17:37:02 +02:00
|
|
|
local ctrl = nil
|
|
|
|
if self.driver then
|
|
|
|
local player = minetest.get_player_by_name(self.driver)
|
|
|
|
if player then
|
|
|
|
ctrl = player:get_player_control()
|
|
|
|
end
|
|
|
|
end
|
2014-08-07 09:54:25 +02:00
|
|
|
if self.old_pos then
|
2014-08-04 19:06:02 +02:00
|
|
|
local diff = vector.subtract(self.old_pos, pos)
|
|
|
|
for _,v in ipairs({"x","y","z"}) do
|
2014-08-10 17:22:46 +02:00
|
|
|
if math.abs(diff[v]) > 1.1 then
|
2014-08-07 09:54:25 +02:00
|
|
|
local expected_pos = vector.add(self.old_pos, self.old_dir)
|
2014-08-09 17:37:02 +02:00
|
|
|
dir, last_switch = boost_cart:get_rail_direction(pos, self.old_dir, ctrl, self.old_switch)
|
2014-08-07 09:54:25 +02:00
|
|
|
if vector.equals(dir, {x=0, y=0, z=0}) then
|
|
|
|
dir = false
|
|
|
|
pos = vector.new(expected_pos)
|
2015-01-11 11:57:49 +01:00
|
|
|
update = true
|
2014-08-07 09:54:25 +02:00
|
|
|
end
|
2014-08-04 19:06:02 +02:00
|
|
|
break
|
|
|
|
end
|
2014-08-01 15:25:48 +02:00
|
|
|
end
|
2014-07-22 23:15:43 +02:00
|
|
|
end
|
2014-08-07 09:54:25 +02:00
|
|
|
|
|
|
|
if vel.y == 0 then
|
|
|
|
for _,v in ipairs({"x", "z"}) do
|
|
|
|
if vel[v] ~= 0 and math.abs(vel[v]) < 0.9 then
|
|
|
|
vel[v] = 0
|
2015-01-11 11:57:49 +01:00
|
|
|
update = true
|
2014-08-07 09:54:25 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-11 11:57:49 +01:00
|
|
|
local cart_dir = boost_cart:velocity_to_dir(vel)
|
2014-08-04 19:06:02 +02:00
|
|
|
local max_vel = boost_cart.speed_max
|
2014-08-07 09:54:25 +02:00
|
|
|
if not dir then
|
2014-08-09 17:37:02 +02:00
|
|
|
dir, last_switch = boost_cart:get_rail_direction(pos, cart_dir, ctrl, self.old_switch)
|
2014-08-07 09:54:25 +02:00
|
|
|
end
|
2014-09-20 22:52:23 +02:00
|
|
|
|
|
|
|
local new_acc = {x=0, y=0, z=0}
|
2014-07-22 23:15:43 +02:00
|
|
|
if vector.equals(dir, {x=0, y=0, z=0}) then
|
|
|
|
vel = {x=0, y=0, z=0}
|
2015-01-11 11:57:49 +01:00
|
|
|
update = true
|
2014-07-22 23:15:43 +02:00
|
|
|
else
|
|
|
|
-- If the direction changed
|
|
|
|
if dir.x ~= 0 and self.old_dir.z ~= 0 then
|
|
|
|
vel.x = dir.x * math.abs(vel.z)
|
|
|
|
vel.z = 0
|
|
|
|
pos.z = math.floor(pos.z + 0.5)
|
2015-01-11 11:57:49 +01:00
|
|
|
update = true
|
2014-07-22 23:15:43 +02:00
|
|
|
end
|
|
|
|
if dir.z ~= 0 and self.old_dir.x ~= 0 then
|
|
|
|
vel.z = dir.z * math.abs(vel.x)
|
|
|
|
vel.x = 0
|
|
|
|
pos.x = math.floor(pos.x + 0.5)
|
2015-01-11 11:57:49 +01:00
|
|
|
update = true
|
2014-07-22 23:15:43 +02:00
|
|
|
end
|
|
|
|
-- Up, down?
|
|
|
|
if dir.y ~= self.old_dir.y then
|
2015-01-11 11:57:49 +01:00
|
|
|
vel.y = dir.y * math.abs(vel.x + vel.z)
|
2014-07-29 12:51:57 +02:00
|
|
|
pos = vector.round(pos)
|
2015-01-11 11:57:49 +01:00
|
|
|
update = true
|
2014-07-22 23:15:43 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Slow down or speed up..
|
2014-08-07 13:40:10 +02:00
|
|
|
local acc = dir.y * -1.8
|
2014-08-04 19:06:02 +02:00
|
|
|
|
|
|
|
local speed_mod = tonumber(minetest.get_meta(pos):get_string("cart_acceleration"))
|
|
|
|
if speed_mod and speed_mod ~= 0 then
|
|
|
|
if speed_mod > 0 then
|
|
|
|
for _,v in ipairs({"x","y","z"}) do
|
|
|
|
if math.abs(vel[v]) >= max_vel then
|
|
|
|
speed_mod = 0
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-08-09 17:37:02 +02:00
|
|
|
acc = acc + (speed_mod * 8)
|
2014-08-04 19:06:02 +02:00
|
|
|
else
|
|
|
|
acc = acc - 0.4
|
2014-08-09 17:37:02 +02:00
|
|
|
-- Handbrake
|
2015-01-11 11:57:49 +01:00
|
|
|
if ctrl and ctrl.down and math.abs(vel.x + vel.z) > 1.2 then
|
2014-08-10 14:03:13 +02:00
|
|
|
acc = acc - 1.2
|
2014-08-09 17:37:02 +02:00
|
|
|
end
|
2014-08-04 19:06:02 +02:00
|
|
|
end
|
|
|
|
|
2015-01-11 11:57:49 +01:00
|
|
|
new_acc = vector.multiply(dir, acc)
|
2014-07-22 23:15:43 +02:00
|
|
|
end
|
|
|
|
|
2014-09-20 22:52:23 +02:00
|
|
|
self.object:setacceleration(new_acc)
|
2014-08-01 17:45:07 +02:00
|
|
|
self.old_pos = vector.new(pos)
|
2014-07-22 23:15:43 +02:00
|
|
|
self.old_dir = vector.new(dir)
|
2014-08-09 17:37:02 +02:00
|
|
|
self.old_switch = last_switch
|
2014-07-22 23:15:43 +02:00
|
|
|
|
|
|
|
-- Limits
|
|
|
|
for _,v in ipairs({"x","y","z"}) do
|
2014-08-04 19:06:02 +02:00
|
|
|
if math.abs(vel[v]) > max_vel then
|
|
|
|
vel[v] = boost_cart:get_sign(vel[v]) * max_vel
|
2015-01-11 11:57:49 +01:00
|
|
|
update = true
|
2014-07-22 23:15:43 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-11 12:00:17 +01:00
|
|
|
if not send_pos then
|
|
|
|
-- Collect dropped items
|
|
|
|
for _,obj_ in ipairs(minetest.get_objects_inside_radius(pos, 1)) do
|
|
|
|
if not obj_:is_player() and
|
|
|
|
obj_:get_luaentity() and
|
|
|
|
not obj_:get_luaentity().physical_state and
|
|
|
|
obj_:get_luaentity().name == "__builtin:item" then
|
|
|
|
obj_:set_attach(self.object, "", {x=0, y=0, z=0}, {x=0, y=0, z=0})
|
|
|
|
self.attached_items[#self.attached_items + 1] = obj_
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-11 11:57:49 +01:00
|
|
|
self.update = false
|
|
|
|
if not update then
|
2014-10-26 12:04:05 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-09-20 22:52:23 +02:00
|
|
|
local yaw = 0
|
2014-07-22 23:15:43 +02:00
|
|
|
if dir.x < 0 then
|
2014-09-20 22:52:23 +02:00
|
|
|
yaw = 0.5
|
2014-07-22 23:15:43 +02:00
|
|
|
elseif dir.x > 0 then
|
2014-10-25 18:49:28 +02:00
|
|
|
yaw = 1.5
|
2014-07-22 23:15:43 +02:00
|
|
|
elseif dir.z < 0 then
|
2014-09-20 22:52:23 +02:00
|
|
|
yaw = 1
|
2014-07-22 23:15:43 +02:00
|
|
|
end
|
2014-09-20 22:52:23 +02:00
|
|
|
self.object:setyaw(yaw * math.pi)
|
2014-10-26 12:04:05 +01:00
|
|
|
|
2014-09-20 22:52:23 +02:00
|
|
|
local anim = {x=0, y=0}
|
2014-07-22 23:15:43 +02:00
|
|
|
if dir.y == -1 then
|
2014-09-20 22:52:23 +02:00
|
|
|
anim = {x=1, y=1}
|
2014-07-22 23:15:43 +02:00
|
|
|
elseif dir.y == 1 then
|
2014-09-20 22:52:23 +02:00
|
|
|
anim = {x=2, y=2}
|
2014-07-22 23:15:43 +02:00
|
|
|
end
|
2014-09-20 22:52:23 +02:00
|
|
|
self.object:set_animation(anim, 1, 0)
|
|
|
|
|
2014-10-26 12:04:05 +01:00
|
|
|
self.object:setvelocity(vel)
|
2015-01-11 11:57:49 +01:00
|
|
|
if send_pos then
|
|
|
|
self.object:setpos(pos)
|
2014-07-22 23:15:43 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-04 19:06:02 +02:00
|
|
|
minetest.register_entity(":carts:cart", boost_cart.cart)
|
|
|
|
minetest.register_craftitem(":carts:cart", {
|
2014-07-22 23:15:43 +02:00
|
|
|
description = "Cart",
|
|
|
|
inventory_image = minetest.inventorycube("cart_top.png", "cart_side.png", "cart_side.png"),
|
|
|
|
wield_image = "cart_side.png",
|
|
|
|
on_place = function(itemstack, placer, pointed_thing)
|
|
|
|
if not pointed_thing.type == "node" then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if boost_cart:is_rail(pointed_thing.under) then
|
2014-08-04 19:06:02 +02:00
|
|
|
minetest.add_entity(pointed_thing.under, "carts:cart")
|
2014-07-22 23:15:43 +02:00
|
|
|
elseif boost_cart:is_rail(pointed_thing.above) then
|
2014-08-04 19:06:02 +02:00
|
|
|
minetest.add_entity(pointed_thing.above, "carts:cart")
|
2014-07-22 23:15:43 +02:00
|
|
|
else return end
|
|
|
|
|
|
|
|
itemstack:take_item()
|
|
|
|
return itemstack
|
|
|
|
end,
|
2014-08-09 17:47:58 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craft({
|
|
|
|
output = "carts:cart",
|
|
|
|
recipe = {
|
|
|
|
{"default:steel_ingot", "", "default:steel_ingot"},
|
|
|
|
{"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
|
|
|
|
},
|
2014-07-22 23:15:43 +02:00
|
|
|
})
|