boost_cart/init.lua

375 lines
9.5 KiB
Lua
Raw Normal View History

2014-07-22 23:15:43 +02:00
boost_cart = {}
boost_cart.modpath = minetest.get_modpath("boost_cart")
-- Maximal speed of the cart in m/s
boost_cart.speed_max = 10
-- Set to nil to disable punching the cart from inside (min = -1)
boost_cart.punch_speed_min = 7
2014-07-29 12:51:57 +02:00
if not boost_cart.modpath then
error("\nWrong mod directory name! Please change it to 'boost_cart'.\n" ..
"See also: http://dev.minetest.net/Installing_Mods")
end
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")
if minetest.global_exists(mesecon) then
2015-06-01 20:21:42 +02:00
dofile(boost_cart.modpath.."/detector.lua")
end
-- Support for non-default games
if not default.player_attached then
default.player_attached = {}
end
2014-07-22 23:15:43 +02:00
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"},
2015-09-13 18:38:53 +02:00
2014-07-22 23:15:43 +02:00
driver = nil,
2015-02-03 11:00:00 +01:00
punched = 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=1, y=0, z=0}, -- random value to start the cart on punch
2014-08-09 17:37:02 +02:00
old_pos = nil,
old_switch = 0,
2015-03-15 13:03:35 +01:00
railtype = 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
2015-12-20 23:15:40 +01:00
boost_cart:manage_attachment(clicker, false)
2014-07-22 23:15:43 +02:00
elseif not self.driver then
self.driver = player_name
2015-12-20 23:15:40 +01:00
boost_cart:manage_attachment(clicker, true, self.object)
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})
if string.sub(staticdata, 1, string.len("return")) ~= "return" then
return
end
local data = minetest.deserialize(staticdata)
if not data or type(data) ~= "table" then
return
end
self.railtype = data.railtype
if data.old_dir then
self.old_dir = data.old_dir
end
end
function boost_cart.cart:get_staticdata()
return minetest.serialize({
railtype = self.railtype,
old_dir = self.old_dir
})
2014-07-22 23:15:43 +02:00
end
function boost_cart.cart:on_punch(puncher, time_from_last_punch, tool_capabilities, direction)
local pos = self.object:getpos()
if not self.railtype then
local bar = vector.floor(vector.add(pos, 0.1))
local node = minetest.get_node(bar).name
self.railtype = minetest.get_item_group(node, "connect_to_raillike")
end
2015-09-13 18:38:53 +02:00
2014-07-22 23:15:43 +02:00
if not puncher or not puncher:is_player() then
local cart_dir = boost_cart:get_rail_direction(pos, self.old_dir, nil, nil, self.railtype)
if vector.equals(cart_dir, {x=0, y=0, z=0}) then
return
end
self.velocity = vector.multiply(cart_dir, 3)
self.old_pos = nil
self.punched = true
2014-07-22 23:15:43 +02:00
return
end
if puncher:get_player_control().sneak then
-- Pick up cart: Drop all attachments
2014-07-22 23:15:43 +02:00
if self.driver then
if self.old_pos then
self.object:setpos(self.old_pos)
end
2014-07-22 23:15:43 +02:00
local player = minetest.get_player_by_name(self.driver)
2015-12-20 23:15:40 +01:00
boost_cart:manage_attachment(player, false)
2014-07-22 23:15:43 +02:00
end
for _,obj_ in ipairs(self.attached_items) do
if obj_ then
obj_:set_detach()
end
end
2015-09-13 18:38:53 +02:00
2015-12-23 21:08:29 +01:00
local leftover = puncher:get_inventory():add_item("main", "carts:cart")
if not leftover:is_empty() then
minetest.add_item(self.object:getpos(), leftover)
end
2014-09-14 08:59:54 +02:00
self.object:remove()
2014-07-22 23:15:43 +02:00
return
end
2015-09-13 18:38:53 +02:00
2014-08-10 14:03:13 +02:00
local vel = self.object:getvelocity()
if puncher:get_player_name() == self.driver then
if math.abs(vel.x + vel.z) > boost_cart.punch_speed_min then
2014-07-22 23:15:43 +02:00
return
end
end
2015-09-13 18:38:53 +02: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(pos, punch_dir, nil, nil, self.railtype)
if vector.equals(cart_dir, {x=0, y=0, z=0}) then
2014-08-10 17:03:40 +02:00
return
end
2015-09-13 18:38:53 +02:00
local punch_interval = 1
if tool_capabilities and tool_capabilities.full_punch_interval then
punch_interval = tool_capabilities.full_punch_interval
end
time_from_last_punch = math.min(time_from_last_punch or punch_interval, punch_interval)
local f = 3 * (time_from_last_punch / punch_interval)
self.velocity = vector.multiply(cart_dir, f)
self.old_dir = cart_dir
2014-08-01 15:25:48 +02:00
self.old_pos = nil
2015-02-03 11:00:00 +01:00
self.punched = true
2014-07-22 23:15:43 +02:00
end
function boost_cart.cart:on_step(dtime)
local vel = self.object:getvelocity()
local update = {}
2015-02-03 11:00:00 +01:00
if self.punched then
2014-07-22 23:15:43 +02:00
vel = vector.add(vel, self.velocity)
self.object:setvelocity(vel)
self.old_dir.y = 0
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
2015-09-13 18:38:53 +02:00
-- dir: New moving direction of the cart
-- last_switch: Currently pressed L/R key, used to ignore the key on the next rail node
local dir, last_switch
2014-07-22 23:15:43 +02:00
local pos = self.object:getpos()
2015-02-03 11:00:00 +01:00
if self.old_pos and not self.punched then
local flo_pos = vector.round(pos)
local flo_old = vector.round(self.old_pos)
2014-08-01 17:45:07 +02:00
if vector.equals(flo_pos, flo_old) then
-- Do not check one node multiple times
2014-08-01 15:25:48 +02:00
return
end
2014-08-07 09:54:25 +02:00
end
2015-09-13 18:38:53 +02:00
local ctrl, player
-- Get player controls
2014-08-09 17:37:02 +02:00
if self.driver then
2015-02-03 11:00:00 +01:00
player = minetest.get_player_by_name(self.driver)
2014-08-09 17:37:02 +02:00
if player then
ctrl = player:get_player_control()
end
end
2014-08-07 09:54:25 +02:00
if self.old_pos then
-- Detection for "skipping" nodes
local expected_pos = vector.add(self.old_pos, self.old_dir)
local found_path = boost_cart:pathfinder(
pos, expected_pos, self.old_dir, ctrl, self.old_switch, self.railtype
)
if not found_path then
-- No rail found: reset back to the expected position
pos = expected_pos
update.pos = true
2014-08-01 15:25:48 +02:00
end
2014-07-22 23:15:43 +02:00
end
2015-09-13 18:38:53 +02:00
local cart_dir = boost_cart:velocity_to_dir(vel)
local max_vel = boost_cart.speed_max
2014-08-07 09:54:25 +02:00
if not dir then
dir, last_switch = boost_cart:get_rail_direction(
pos, cart_dir, ctrl, self.old_switch, self.railtype
)
2014-08-07 09:54:25 +02:00
end
2015-09-13 18:38:53 +02:00
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}
pos = vector.round(pos)
update.pos = true
2015-02-03 11:00:00 +01:00
update.vel = 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-02-03 11:00:00 +01:00
update.pos = 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-02-03 11:00:00 +01:00
update.pos = true
2014-07-22 23:15:43 +02:00
end
-- Up, down?
if dir.y ~= self.old_dir.y then
vel.y = dir.y * math.abs(vel.x + vel.z)
2014-07-29 12:51:57 +02:00
pos = vector.round(pos)
2015-02-03 11:00:00 +01:00
update.pos = true
2014-07-22 23:15:43 +02:00
end
2015-09-13 18:38:53 +02:00
2014-07-22 23:15:43 +02:00
-- Slow down or speed up..
2014-08-07 13:40:10 +02:00
local acc = dir.y * -1.8
2015-09-13 18:38:53 +02:00
2016-07-20 09:48:39 +02:00
local speed_mod_string = minetest.get_meta(pos):get_string("cart_acceleration")
local speed_mod = tonumber(speed_mod_string)
if speed_mod_string == "halt" then
vel = {x=0, y=0, z=0}
acc = 0
2016-07-20 09:48:39 +02:00
pos = vector.round(pos)
update.pos = true
update.vel = true
elseif speed_mod and speed_mod ~= 0 then
-- Try to make it similar to the original carts mod
acc = acc + (speed_mod * 10)
else
acc = acc - 0.4
2014-08-09 17:37:02 +02:00
-- Handbrake
if ctrl and ctrl.down then
2014-08-10 14:03:13 +02:00
acc = acc - 1.2
2014-08-09 17:37:02 +02:00
end
end
2015-09-13 18:38:53 +02:00
if self.old_dir.y == 0 and not self.punched then
-- Stop the cart swing between two rail parts (handbrake)
if vector.equals(vector.multiply(self.old_dir, -1), dir) then
vel = {x=0, y=0, z=0}
acc = 0
if self.old_pos then
pos = vector.new(self.old_pos)
update.pos = true
end
dir = vector.new(self.old_dir)
update.vel = true
end
end
new_acc = vector.multiply(dir, acc)
2014-07-22 23:15:43 +02:00
end
2015-09-13 18:38:53 +02:00
2015-06-01 20:21:42 +02:00
if mesecon then
boost_cart:signal_detector_rail(vector.round(pos))
2015-06-01 20:21:42 +02:00
end
2015-09-13 18:38:53 +02:00
2014-07-22 23:15:43 +02:00
-- Limits
for _,v in ipairs({"x","y","z"}) do
if math.abs(vel[v]) > max_vel then
vel[v] = boost_cart:get_sign(vel[v]) * max_vel
new_acc[v] = 0
2015-02-03 11:00:00 +01:00
update.vel = true
2014-07-22 23:15:43 +02:00
end
end
self.object:setacceleration(new_acc)
self.old_pos = vector.new(pos)
if not vector.equals(dir, {x=0, y=0, z=0}) then
self.old_dir = vector.new(dir)
end
self.old_switch = last_switch
2015-09-13 18:38:53 +02:00
2015-02-03 11:00:00 +01:00
if self.punched then
2015-01-11 12:00:17 +01:00
-- 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
2015-01-11 12:00:17 +01:00
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
2015-02-03 11:00:00 +01:00
self.punched = false
update.vel = true -- update player animation
2015-01-11 12:00:17 +01:00
end
2015-09-13 18:38:53 +02:00
2015-02-03 11:00:00 +01:00
if not (update.vel or update.pos) then
2014-10-26 12:04:05 +01:00
return
end
2015-09-13 18:38:53 +02:00
2014-09-20 22:52:23 +02:00
local yaw = 0
if self.old_dir.x < 0 then
2014-09-20 22:52:23 +02:00
yaw = 0.5
elseif self.old_dir.x > 0 then
yaw = 1.5
elseif self.old_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)
2015-09-13 18:38:53 +02: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)
2015-09-13 18:38:53 +02:00
2014-10-26 12:04:05 +01:00
self.object:setvelocity(vel)
2015-02-03 11:00:00 +01:00
if update.pos then
self.object:setpos(pos)
2014-07-22 23:15:43 +02:00
end
2015-02-03 11:00:00 +01:00
update = nil
2014-07-22 23:15:43 +02:00
end
minetest.register_entity(":carts:cart", boost_cart.cart)
minetest.register_craftitem(":carts:cart", {
description = "Cart (Sneak+Click to pick up)",
2014-07-22 23:15:43 +02:00
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
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
minetest.add_entity(pointed_thing.above, "carts:cart")
2014-07-22 23:15:43 +02:00
else return end
2015-09-13 18:38:53 +02:00
2014-07-22 23:15:43 +02:00
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"},
},
2016-07-20 09:48:39 +02:00
})