diff --git a/mods/carts/cart_entity.lua b/mods/carts/cart_entity.lua new file mode 100644 index 000000000..e86d5ffcf --- /dev/null +++ b/mods/carts/cart_entity.lua @@ -0,0 +1,488 @@ +-- carts/cart_entity.lua + +-- support for MT game translation. +local SL = lord.require_intllib() + +local cart_entity = { + initial_properties = { + physical = false, -- otherwise going uphill breaks + collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5}, + visual = "mesh", + mesh = "carts_cart.b3d", + visual_size = {x=1, y=1}, + textures = {"carts_cart.png"}, + }, + + driver = nil, + punched = false, -- used to re-send velocity and position + 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 + old_pos = nil, + old_switch = 0, + railtype = nil, + attached_items = {} +} + +--********************************************************** +--* либо в тележку либо из неё +--* +-- function cart_entity: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 +-- carts:manage_attachment(clicker, nil) +-- elseif not self.driver then +-- self.driver = player_name +-- carts:manage_attachment(clicker, self.object) +-- +-- player_api does not update the animation +-- when the player is attached, reset to default animation +-- player_api.set_animation(clicker, "stand") +-- end +-- end +function cart_entity:on_rightclick(clicker) + local player_name = clicker:get_player_name() + if not clicker or not clicker:is_player() then + return + end + if self.driver and player_name == self.driver then + self.driver = nil + clicker:set_detach() + elseif not self.driver then + self.driver = player_name + clicker:set_attach(self.object, "", {x=0,y=5,z=0}, {x=0,y=0,z=0}) + end +end + +--********************************************************** +--* +--* +function cart_entity: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 type(data) ~= "table" then + return + end + self.railtype = data.railtype + if data.old_dir then + self.old_dir = data.old_dir + end +end + +--********************************************************** +--* +--* +function cart_entity:get_staticdata() + return minetest.serialize({ + railtype = self.railtype, + old_dir = self.old_dir + }) +end + +--********************************************************** +--* +--* +-- 0.5.x and later: When the driver leaves +function cart_entity:on_detach_child(child) + if child and child:get_player_name() == self.driver then + self.driver = nil + end +end + +--********************************************************** +--* +--* +--* Remove the cart if holding a tool or accelerate it +function cart_entity:on_punch(puncher, time_from_last_punch, tool_capabilities, direction) + local pos = self.object:get_pos() + local vel = self.object:get_velocity() + if not self.railtype or vector.equals(vel, {x=0, y=0, z=0}) then + local node = minetest.get_node(pos).name + self.railtype = minetest.get_item_group(node, "connect_to_raillike") + end + -- Punched by non-player + if not puncher or not puncher:is_player() then + local cart_dir = carts: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, 2) + self.punched = true + return + end + -- Player digs cart by sneak-punch + if puncher:get_player_control().sneak then + if self.sound_handle then + minetest.sound_stop(self.sound_handle) + end + -- Detach driver and items + if self.driver then + if self.old_pos then + self.object:set_pos(self.old_pos) + end + local player = minetest.get_player_by_name(self.driver) + carts:manage_attachment(player, nil) + end + for _, obj_ in ipairs(self.attached_items) do + if obj_ then + obj_:set_detach() + end + end + -- Pick up cart + local inv = puncher:get_inventory() + if not (creative and creative.is_enabled_for + and creative.is_enabled_for(puncher:get_player_name())) + or not inv:contains_item("main", "carts:cart") then + local leftover = inv:add_item("main", "carts:cart") + -- If no room in inventory add a replacement cart to the world + if not leftover:is_empty() then + minetest.add_item(self.object:get_pos(), leftover) + end + end + self.object:remove() + return + end + -- Player punches cart to alter velocity + if puncher:get_player_name() == self.driver then + if math.abs(vel.x + vel.z) > carts.punch_speed_max then + return + end + end + + local punch_dir = carts:velocity_to_dir(puncher:get_look_dir()) + punch_dir.y = 0 + local cart_dir = carts:get_rail_direction(pos, punch_dir, nil, nil, self.railtype) + if vector.equals(cart_dir, {x=0, y=0, z=0}) then + return + end + + 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 = 2 * (time_from_last_punch / punch_interval) + + self.velocity = vector.multiply(cart_dir, f) + self.old_dir = cart_dir + self.punched = true +end + +--********************************************************** +--* +--* +local function rail_on_step_event(handler, obj, dtime) + if handler then + handler(obj, dtime) + end +end + +--********************************************************** +--* +--* +-- sound refresh interval = 1.0sec +local function rail_sound(self, dtime) + if not self.sound_ttl then + self.sound_ttl = 1.0 + return + elseif self.sound_ttl > 0 then + self.sound_ttl = self.sound_ttl - dtime + return + end + self.sound_ttl = 1.0 + if self.sound_handle then + local handle = self.sound_handle + self.sound_handle = nil + minetest.after(0.2, minetest.sound_stop, handle) + end + local vel = self.object:get_velocity() + local speed = vector.length(vel) + if speed > 0 then + self.sound_handle = minetest.sound_play( + "carts_cart_moving", { + object = self.object, + gain = (speed / carts.speed_max) / 2, + loop = true, + }) + end +end + +--********************************************************** +--* +--* +local function get_railparams(pos) + local node = minetest.get_node(pos) + return carts.railparams[node.name] or {} +end + +local v3_len = vector.length +--********************************************************** +--* +--* +local function rail_on_step(self, dtime) + local vel = self.object:get_velocity() + if self.punched then + vel = vector.add(vel, self.velocity) + self.object:set_velocity(vel) + self.old_dir.y = 0 + elseif vector.equals(vel, {x=0, y=0, z=0}) then + return + end + + local pos = self.object:get_pos() + local cart_dir = carts:velocity_to_dir(vel) + local same_dir = vector.equals(cart_dir, self.old_dir) + local update = {} + + if self.old_pos and not self.punched and same_dir then + local flo_pos = vector.round(pos) + local flo_old = vector.round(self.old_pos) + if vector.equals(flo_pos, flo_old) then + -- Do not check one node multiple times + return + end + end + + local ctrl, player + + -- Get player controls + if self.driver then + player = minetest.get_player_by_name(self.driver) + if player then + ctrl = player:get_player_control() + end + end + + local stop_wiggle = false + if self.old_pos and same_dir then + -- Detection for "skipping" nodes (perhaps use average dtime?) + -- It's sophisticated enough to take the acceleration in account + local acc = self.object:get_acceleration() + local distance = dtime * (v3_len(vel) + 0.5 * dtime * v3_len(acc)) + + local new_pos, new_dir = carts:pathfinder( + pos, self.old_pos, self.old_dir, distance, ctrl, + self.old_switch, self.railtype + ) + + if new_pos then + -- No rail found: set to the expected position + pos = new_pos + update.pos = true + cart_dir = new_dir + end + elseif self.old_pos and self.old_dir.y ~= 1 and not self.punched then + -- Stop wiggle + stop_wiggle = true + end + + local railparams + + -- dir: New moving direction of the cart + -- switch_keys: Currently pressed L/R key, used to ignore the key on the next rail node + local dir, switch_keys = carts:get_rail_direction( + pos, cart_dir, ctrl, self.old_switch, self.railtype + ) + local dir_changed = not vector.equals(dir, self.old_dir) + + local new_acc = {x=0, y=0, z=0} + if stop_wiggle or vector.equals(dir, {x=0, y=0, z=0}) then + vel = {x = 0, y = 0, z = 0} + local pos_r = vector.round(pos) + if not carts:is_rail(pos_r, self.railtype) + and self.old_pos then + pos = self.old_pos + elseif not stop_wiggle then + pos = pos_r + else + pos.y = math.floor(pos.y + 0.5) + end + update.pos = true + update.vel = true + else + -- Direction change detected + if dir_changed then + vel = vector.multiply(dir, math.abs(vel.x + vel.z)) + update.vel = true + if dir.y ~= self.old_dir.y then + pos = vector.round(pos) + update.pos = true + end + end + -- Center on the rail + if dir.z ~= 0 and math.floor(pos.x + 0.5) ~= pos.x then + pos.x = math.floor(pos.x + 0.5) + update.pos = true + end + if dir.x ~= 0 and math.floor(pos.z + 0.5) ~= pos.z then + pos.z = math.floor(pos.z + 0.5) + update.pos = true + end + + -- Slow down or speed up.. + local acc = dir.y * -4.0 + + -- Get rail for corrected position + railparams = get_railparams(pos) + + -- no need to check for railparams == nil since we always make it exist. + local speed_mod = railparams.acceleration + if speed_mod and speed_mod ~= 0 then + -- Try to make it similar to the original carts mod + acc = acc + speed_mod + else + -- Handbrake or coast + if ctrl and ctrl.down then + acc = acc - 3 + else + acc = acc - 0.4 + end + end + + new_acc = vector.multiply(dir, acc) + end + + -- Limits + local max_vel = carts.speed_max + for _, v in pairs({"x","y","z"}) do + if math.abs(vel[v]) > max_vel then + vel[v] = carts:get_sign(vel[v]) * max_vel + new_acc[v] = 0 + update.vel = true + end + end + + self.object:set_acceleration(new_acc) + self.old_pos = vector.round(pos) + if not vector.equals(dir, {x=0, y=0, z=0}) and not stop_wiggle then + self.old_dir = vector.new(dir) + end + self.old_switch = switch_keys + + if self.punched then + -- Collect dropped items + for _, obj_ in pairs(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 + self.punched = false + update.vel = true + end + + railparams = railparams or get_railparams(pos) + + if not (update.vel or update.pos) then + rail_on_step_event(railparams.on_step, self, dtime) + return + end + + local yaw = 0 + if self.old_dir.x < 0 then + yaw = 0.5 + elseif self.old_dir.x > 0 then + yaw = 1.5 + elseif self.old_dir.z < 0 then + yaw = 1 + end + self.object:set_yaw(yaw * math.pi) + + local anim = {x=0, y=0} + if dir.y == -1 then + anim = {x=1, y=1} + elseif dir.y == 1 then + anim = {x=2, y=2} + end + self.object:set_animation(anim, 1, 0) + + if update.vel then + self.object:set_velocity(vel) + end + if update.pos then + if dir_changed then + self.object:set_pos(pos) + else + self.object:move_to(pos) + end + end + + -- call event handler + rail_on_step_event(railparams.on_step, self, dtime) +end + +--********************************************************** +--* +--* +function cart_entity:on_step(dtime) + rail_on_step(self, dtime) + rail_sound(self, dtime) +end + +--********************************************************** +--* +--* +minetest.register_entity("carts:cart", cart_entity) + +--********************************************************** +--* +--* +minetest.register_craftitem("carts:cart", { + description = SL("Cart") .. "\n" .. SL("(Sneak+Click to pick up)"), + inventory_image = minetest.inventorycube("carts_cart_top.png", "carts_cart_side.png", "carts_cart_side.png"), + wield_image = "carts_cart_side.png", + on_place = function(itemstack, placer, pointed_thing) + local under = pointed_thing.under + local node = minetest.get_node(under) + local udef = minetest.registered_nodes[node.name] + if udef and udef.on_rightclick and + not (placer and placer:is_player() and + placer:get_player_control().sneak) then + return udef.on_rightclick(under, node, placer, itemstack, + pointed_thing) or itemstack + end + + if not pointed_thing.type == "node" then + return + end + if carts:is_rail(pointed_thing.under) then + minetest.add_entity(pointed_thing.under, "carts:cart") + elseif carts:is_rail(pointed_thing.above) then + minetest.add_entity(pointed_thing.above, "carts:cart") + else + return + end + + minetest.sound_play({name = "default_place_node_metal", gain = 0.5}, + {pos = pointed_thing.above}) + + if not (creative and creative.is_enabled_for + and creative.is_enabled_for(placer:get_player_name())) then + itemstack:take_item() + end + return itemstack + end, +}) + +--********************************************************** +--* +--* +minetest.register_craft({ + output = "carts:cart", + recipe = { + {"default:steel_ingot", "", "default:steel_ingot"}, + {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"}, + }, +}) + + diff --git a/mods/carts/functions.lua b/mods/carts/functions.lua index 7cb3c5ce2..5781056ad 100644 --- a/mods/carts/functions.lua +++ b/mods/carts/functions.lua @@ -1,60 +1,275 @@ --- --- Helper functions --- - -cart_func = {} - -function cart_func:get_sign(z) +--********************************************************** +--* +--* +function carts:get_sign(z) if z == 0 then return 0 else - return z/math.abs(z) + return z / math.abs(z) end end --- Returns the velocity as a unit vector --- The smaller part of the vector will be turned to 0 -function cart_func:velocity_to_dir(v) - if math.abs(v.x) > math.abs(v.z) then - return {x=cart_func:get_sign(v.x), y=cart_func:get_sign(v.y), z=0} +--********************************************************** +--* +--* +function carts:manage_attachment(player, obj) + if not player then + return + end + local status = obj ~= nil + local player_name = player:get_player_name() + if player_api.player_attached[player_name] == status then + return + end + player_api.player_attached[player_name] = status + + if status then + player:set_attach(obj, "", {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}) else - return {x=0, y=cart_func:get_sign(v.y), z=cart_func:get_sign(v.z)} + player:set_detach() + player:set_eye_offset({x=0, y=0, z=0},{x=0, y=0, z=0}) end end -function cart_func:is_rail(p) - local nn = minetest.get_node(p).name - return minetest.get_item_group(nn, "rail") ~= 0 +--********************************************************** +--* +--* +function carts:velocity_to_dir(v) + if math.abs(v.x) > math.abs(v.z) then + return {x=carts:get_sign(v.x), y=carts:get_sign(v.y), z=0} + else + return {x=0, y=carts:get_sign(v.y), z=carts:get_sign(v.z)} + end end -function cart_func:is_accelerator(p) - local nn = minetest.get_node(p).name - return minetest.get_item_group(nn, "accelerator") ~= 0 +--********************************************************** +--* +--* +function carts:is_rail(pos, railtype) + local node = minetest.get_node(pos).name + if node == "ignore" then + local vm = minetest.get_voxel_manip() + local emin, emax = vm:read_from_map(pos, pos) + local area = VoxelArea:new{ + MinEdge = emin, + MaxEdge = emax, + } + local data = vm:get_data() + local vi = area:indexp(pos) + node = minetest.get_name_from_content_id(data[vi]) + end + if minetest.get_item_group(node, "rail") == 0 then + return false + end + if not railtype then + return true + end + return minetest.get_item_group(node, "connect_to_raillike") == railtype end -function cart_func:is_int(z) - z = math.abs(z) - return math.abs(math.floor(z+0.5)-z) <= 0.1 +--********************************************************** +--* +--* +function carts:check_front_up_down(pos, dir_, check_up, railtype) + local dir = vector.new(dir_) + local cur + + -- Front + dir.y = 0 + cur = vector.add(pos, dir) + if carts:is_rail(cur, railtype) then + return dir + end + -- Up + if check_up then + dir.y = 1 + cur = vector.add(pos, dir) + if carts:is_rail(cur, railtype) then + return dir + end + end + -- Down + dir.y = -1 + cur = vector.add(pos, dir) + if carts:is_rail(cur, railtype) then + return dir + end + return nil end -cart_func.v3 = {} +--********************************************************** +--* +--* +function carts:get_rail_direction(pos_, dir, ctrl, old_switch, railtype) + local pos = vector.round(pos_) + local cur + local left_check, right_check = true, true + + -- Check left and right + local left = {x=0, y=0, z=0} + local right = {x=0, y=0, z=0} + if dir.z ~= 0 and dir.x == 0 then + left.x = -dir.z + right.x = dir.z + elseif dir.x ~= 0 and dir.z == 0 then + left.z = dir.x + right.z = -dir.x + end + + local straight_priority = ctrl and dir.y ~= 0 + + -- Normal, to disallow rail switching up- & downhill + if straight_priority then + cur = self:check_front_up_down(pos, dir, true, railtype) + if cur then + return cur + end + end + + if ctrl then + if old_switch == 1 then + left_check = false + elseif old_switch == 2 then + right_check = false + end + if ctrl.left and left_check then + cur = self:check_front_up_down(pos, left, false, railtype) + if cur then + return cur, 1 + end + left_check = false + end + if ctrl.right and right_check then + cur = self:check_front_up_down(pos, right, false, railtype) + if cur then + return cur, 2 + end + right_check = true + end + end + + -- Normal + if not straight_priority then + cur = self:check_front_up_down(pos, dir, true, railtype) + if cur then + return cur + end + end + + -- Left, if not already checked + if left_check then + cur = carts:check_front_up_down(pos, left, false, railtype) + if cur then + return cur + end + end + + -- Right, if not already checked + if right_check then + cur = carts:check_front_up_down(pos, right, false, railtype) + if cur then + return cur + end + end + + -- Backwards + if not old_switch then + cur = carts:check_front_up_down(pos, { + x = -dir.x, + y = dir.y, + z = -dir.z + }, true, railtype) + if cur then + return cur + end + end -function cart_func.v3:add(v1, v2) - return {x=v1.x+v2.x, y=v1.y+v2.y, z=v1.z+v2.z} + return {x=0, y=0, z=0} end -function cart_func.v3:copy(v) - return {x=v.x, y=v.y, z=v.z} +--********************************************************** +--* +--* +function carts:pathfinder(pos_, old_pos, old_dir, distance, ctrl, + pf_switch, railtype) + + local pos = vector.round(pos_) + if vector.equals(old_pos, pos) then + return + end + + local pf_pos = vector.round(old_pos) + local pf_dir = vector.new(old_dir) + distance = math.min(carts.path_distance_max, + math.floor(distance + 1)) + + for i = 1, distance do + pf_dir, pf_switch = self:get_rail_direction( + pf_pos, pf_dir, ctrl, pf_switch or 0, railtype) + + if vector.equals(pf_dir, {x=0, y=0, z=0}) then + -- No way forwards + return pf_pos, pf_dir + end + + pf_pos = vector.add(pf_pos, pf_dir) + + if vector.equals(pf_pos, pos) then + -- Success! Cart moved on correctly + return + end + end + -- Not found. Put cart to predicted position + return pf_pos, pf_dir end -function cart_func.v3:round(v) - return { - x = math.floor(v.x+0.5), - y = math.floor(v.y+0.5), - z = math.floor(v.z+0.5), +--********************************************************** +--* +--* +function carts:register_rail(name, def_overwrite, railparams) + local def = { + drawtype = "raillike", + paramtype = "light", + sunlight_propagates = true, + is_ground_content = false, + walkable = false, + selection_box = { + type = "fixed", + fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2}, + }, + sounds = default.node_sound_metal_defaults() } + for k, v in pairs(def_overwrite) do + def[k] = v + end + if not def.inventory_image then + def.wield_image = def.tiles[1] + def.inventory_image = def.tiles[1] + end + + if railparams then + carts.railparams[name] = table.copy(railparams) + end + + minetest.register_node(name, def) end -function cart_func.v3:equal(v1, v2) - return v1.x == v2.x and v1.y == v2.y and v1.z == v2.z +--********************************************************** +--* +--* +function carts:get_rail_groups(additional_groups) + -- Get the default rail groups and add more when a table is given + local groups = { + dig_immediate = 2, + attached_node = 1, + rail = 1, + connect_to_raillike = minetest.raillike_group("rail") + } + if type(additional_groups) == "table" then + for k, v in pairs(additional_groups) do + groups[k] = v + end + end + return groups end diff --git a/mods/carts/functions_new.lua b/mods/carts/functions_new.lua new file mode 100644 index 000000000..7cb3c5ce2 --- /dev/null +++ b/mods/carts/functions_new.lua @@ -0,0 +1,60 @@ +-- +-- Helper functions +-- + +cart_func = {} + +function cart_func:get_sign(z) + if z == 0 then + return 0 + else + return z/math.abs(z) + end +end + +-- Returns the velocity as a unit vector +-- The smaller part of the vector will be turned to 0 +function cart_func:velocity_to_dir(v) + if math.abs(v.x) > math.abs(v.z) then + return {x=cart_func:get_sign(v.x), y=cart_func:get_sign(v.y), z=0} + else + return {x=0, y=cart_func:get_sign(v.y), z=cart_func:get_sign(v.z)} + end +end + +function cart_func:is_rail(p) + local nn = minetest.get_node(p).name + return minetest.get_item_group(nn, "rail") ~= 0 +end + +function cart_func:is_accelerator(p) + local nn = minetest.get_node(p).name + return minetest.get_item_group(nn, "accelerator") ~= 0 +end + +function cart_func:is_int(z) + z = math.abs(z) + return math.abs(math.floor(z+0.5)-z) <= 0.1 +end + +cart_func.v3 = {} + +function cart_func.v3:add(v1, v2) + return {x=v1.x+v2.x, y=v1.y+v2.y, z=v1.z+v2.z} +end + +function cart_func.v3:copy(v) + return {x=v.x, y=v.y, z=v.z} +end + +function cart_func.v3:round(v) + return { + x = math.floor(v.x+0.5), + y = math.floor(v.y+0.5), + z = math.floor(v.z+0.5), + } +end + +function cart_func.v3:equal(v1, v2) + return v1.x == v2.x and v1.y == v2.y and v1.z == v2.z +end diff --git a/mods/carts/init.lua b/mods/carts/init.lua index 5925049b1..aa91a00e1 100644 --- a/mods/carts/init.lua +++ b/mods/carts/init.lua @@ -1,557 +1,26 @@ -local SL = lord.require_intllib() - -local CART_ACCELERATION = "0.5" -local CART_MAX_VELOCITY = 10 - -dofile(minetest.get_modpath("carts").."/functions.lua") -dofile(minetest.get_modpath("carts").."/crafting.lua") - --- --- Cart entity --- - -local cart = { - physical = false, - collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5}, - visual = "mesh", - mesh = "carts_cart.b3d", - visual_size = {x=1, y=1}, - textures = {"carts.png"}, - - driver = nil, - velocity = {x=0, y=0, z=0}, - old_pos = nil, - old_velocity = nil, - pre_stop_dir = nil, -} - -function cart:on_rightclick(clicker) - if not clicker or not clicker:is_player() then - return - end - if self.driver and clicker == self.driver then - self.driver = nil - clicker:set_detach() - elseif not self.driver then - self.driver = clicker - clicker:set_attach(self.object, "", {x=0,y=5,z=0}, {x=0,y=0,z=0}) - end -end - -function cart:on_activate(staticdata, dtime_s) - self.object:set_armor_groups({immortal=1}) - if staticdata then - local tmp = minetest.deserialize(staticdata) - if tmp then - self.velocity = tmp.velocity - end - if tmp and tmp.pre_stop_dir then - self.pre_stop_dir = tmp.pre_stop_dir - end - end - self.old_pos = self.object:getpos() - self.old_velocity = self.velocity -end - -function cart:get_staticdata() - return minetest.serialize({ - velocity = self.velocity, - pre_stop_dir = self.pre_stop_dir, - }) -end - --- Remove the cart if holding a tool or accelerate it -function 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 - self.object:remove() - local inv = puncher:get_inventory() - if default.creative then - if not inv:contains_item("main", "carts:cart") then - inv:add_item("main", "carts:cart") - end - else - inv:add_item("main", "carts:cart") - end - return - end - - if puncher == self.driver then - return - end - - local d = cart_func:velocity_to_dir(direction) - local s = self.velocity - if time_from_last_punch > tool_capabilities.full_punch_interval then - time_from_last_punch = tool_capabilities.full_punch_interval - end - local f = 4*(time_from_last_punch/tool_capabilities.full_punch_interval) - local v = {x=s.x+d.x*f, y=s.y, z=s.z+d.z*f} - if math.abs(v.x) < 6 and math.abs(v.z) < 6 then - self.velocity = v - else - if math.abs(self.velocity.x) < 6 and math.abs(v.x) >= 6 then - self.velocity.x = 6*cart_func:get_sign(self.velocity.x) - end - if math.abs(self.velocity.z) < 6 and math.abs(v.z) >= 6 then - self.velocity.z = 6*cart_func:get_sign(self.velocity.z) - end - end -end - --- Returns the direction as a unit vector -function cart:get_rail_direction(pos, dir) - local d = cart_func.v3:copy(dir) - - -- Check front - d.y = 0 - local p = cart_func.v3:add(cart_func.v3:copy(pos), d) - if cart_func:is_rail(p) then - return d - end - - -- Check downhill - d.y = -1 - p = cart_func.v3:add(cart_func.v3:copy(pos), d) - if cart_func:is_rail(p) then - return d - end - - -- Check uphill - d.y = 1 - p = cart_func.v3:add(cart_func.v3:copy(pos), d) - if cart_func:is_rail(p) then - return d - end - d.y = 0 - - -- Check left and right - local view_dir - local other_dir - local a - - if d.x == 0 and d.z ~= 0 then - view_dir = "z" - other_dir = "x" - if d.z < 0 then - a = {1, -1} - else - a = {-1, 1} - end - elseif d.z == 0 and d.x ~= 0 then - view_dir = "x" - other_dir = "z" - if d.x > 0 then - a = {1, -1} - else - a = {-1, 1} - end - else - return {x=0, y=0, z=0} - end - - d[view_dir] = 0 - d[other_dir] = a[1] - p = cart_func.v3:add(cart_func.v3:copy(pos), d) - if cart_func:is_rail(p) then - return d - end - d.y = -1 - p = cart_func.v3:add(cart_func.v3:copy(pos), d) - if cart_func:is_rail(p) then - return d - end - d.y = 0 - d[other_dir] = a[2] - p = cart_func.v3:add(cart_func.v3:copy(pos), d) - if cart_func:is_rail(p) then - return d - end - d.y = -1 - p = cart_func.v3:add(cart_func.v3:copy(pos), d) - if cart_func:is_rail(p) then - return d - end - d.y = 0 - - return {x=0, y=0, z=0} -end - -function cart:calc_rail_direction(pos, vel) - local velocity = cart_func.v3:copy(vel) - local p = cart_func.v3:copy(pos) - if cart_func:is_int(p.x) and cart_func:is_int(p.z) then - - local dir = cart_func:velocity_to_dir(velocity) - local dir_old = cart_func.v3:copy(dir) - - dir = self:get_rail_direction(cart_func.v3:round(p), dir) - - local v = math.max(math.abs(velocity.x), math.abs(velocity.z)) - velocity = { - x = v * dir.x, - y = v * dir.y, - z = v * dir.z, - } +-- carts/init.lua - if cart_func.v3:equal(velocity, {x=0, y=0, z=0}) and not cart_func:is_rail(p) then - - -- First try this HACK - -- Move the cart on the rail if above or under it - if cart_func:is_rail(cart_func.v3:add(p, {x=0, y=1, z=0})) and vel.y >= 0 then - p = cart_func.v3:add(p, {x=0, y=1, z=0}) - return self:calc_rail_direction(p, vel) - end - if cart_func:is_rail(cart_func.v3:add(p, {x=0, y=-1, z=0})) and vel.y <= 0 then - p = cart_func.v3:add(p, {x=0, y=-1, z=0}) - return self:calc_rail_direction(p, vel) - end - -- Now the HACK gets really dirty - if cart_func:is_rail(cart_func.v3:add(p, {x=0, y=2, z=0})) and vel.y >= 0 then - p = cart_func.v3:add(p, {x=0, y=1, z=0}) - return self:calc_rail_direction(p, vel) - end - if cart_func:is_rail(cart_func.v3:add(p, {x=0, y=-2, z=0})) and vel.y <= 0 then - p = cart_func.v3:add(p, {x=0, y=-1, z=0}) - return self:calc_rail_direction(p, vel) - end - - return {x=0, y=0, z=0}, p - end - - if not cart_func.v3:equal(dir, dir_old) then - return velocity, cart_func.v3:round(p) - end - - end - return velocity, p -end - -function cart:on_step(dtime) - - local pos = self.object:getpos() - local dir = cart_func:velocity_to_dir(self.velocity) - - if not cart_func.v3:equal(self.velocity, {x=0,y=0,z=0}) then - self.pre_stop_dir = cart_func:velocity_to_dir(self.velocity) - end - - -- Stop the cart if the velocity is nearly 0 - -- Only if on a flat railway - if dir.y == 0 then - if math.abs(self.velocity.x) < 0.1 and math.abs(self.velocity.z) < 0.1 then - -- Start the cart if placed on an active accelerating rail - local a = tonumber(minetest.get_meta(pos):get_string("cart_acceleration")) - if a and a ~= 0 then - if - self.pre_stop_dir and - cart_func.v3:equal( - self:get_rail_direction(self.object:getpos(), - self.pre_stop_dir), self.pre_stop_dir - ) - then - self.velocity = { - x = self.pre_stop_dir.x * 0.2, - y = self.pre_stop_dir.y * 0.2, - z = self.pre_stop_dir.z * 0.2, - } - self.old_velocity = self.velocity - return - end - for _,y in ipairs({0,-1,1}) do - for _,z in ipairs({1,-1}) do - if cart_func.v3:equal( - self:get_rail_direction(self.object:getpos(), - {x=0, y=y, z=z}), {x=0, y=y, z=z} - ) then - self.velocity = { - x = 0, - y = 0.2*y, - z = 0.2*z, - } - self.old_velocity = self.velocity - return - end - end - for _,x in ipairs({1,-1}) do - if cart_func.v3:equal( - self:get_rail_direction(self.object:getpos(), - {x=x, y=y, z=0}), {x=x, y=y, z=0} - ) then - self.velocity = { - x = 0.2*x, - y = 0.2*y, - z = 0, - } - self.old_velocity = self.velocity - return - end - end - end - end - - self.velocity = {x=0, y=0, z=0} - self.object:setvelocity(self.velocity) - self.old_velocity = self.velocity - self.old_pos = self.object:getpos() - return - end - end - - -- - -- Set the new moving direction - -- - - -- Recalcualte the rails that are passed since the last server step - local old_dir = cart_func:velocity_to_dir(self.old_velocity) - if old_dir.x ~= 0 then - local sign = cart_func:get_sign(pos.x-self.old_pos.x) - while true do - if sign ~= cart_func:get_sign(pos.x-self.old_pos.x) or pos.x == self.old_pos.x then - break - end - self.old_pos.x = self.old_pos.x + cart_func:get_sign(pos.x-self.old_pos.x)*0.1 - self.old_pos.y = self.old_pos.y + cart_func:get_sign(pos.x-self.old_pos.x)*0.1*old_dir.y - self.old_velocity, self.old_pos = self:calc_rail_direction(self.old_pos, self.old_velocity) - old_dir = cart_func:velocity_to_dir(self.old_velocity) - if not cart_func.v3:equal(cart_func:velocity_to_dir(self.old_velocity), dir) then - self.velocity = self.old_velocity - pos = self.old_pos - self.object:setpos(self.old_pos) - break - end - end - elseif old_dir.z ~= 0 then - local sign = cart_func:get_sign(pos.z-self.old_pos.z) - while true do - if sign ~= cart_func:get_sign(pos.z-self.old_pos.z) or pos.z == self.old_pos.z then - break - end - self.old_pos.z = self.old_pos.z + cart_func:get_sign(pos.z-self.old_pos.z)*0.1 - self.old_pos.y = self.old_pos.y + cart_func:get_sign(pos.z-self.old_pos.z)*0.1*old_dir.y - self.old_velocity, self.old_pos = self:calc_rail_direction(self.old_pos, self.old_velocity) - old_dir = cart_func:velocity_to_dir(self.old_velocity) - if not cart_func.v3:equal(cart_func:velocity_to_dir(self.old_velocity), dir) then - self.velocity = self.old_velocity - pos = self.old_pos - self.object:setpos(self.old_pos) - break - end - end - end - - -- Calculate the new step - self.velocity, pos = self:calc_rail_direction(pos, self.velocity) - self.object:setpos(pos) - dir = cart_func:velocity_to_dir(self.velocity) - - -- Accelerate or decelerate the cart according to the pitch and acceleration of the rail node - local a = tonumber(minetest.get_meta(pos):get_string("cart_acceleration")) - if not a then - a = 0 - end - if self.velocity.y < 0 then - self.velocity = { - x = self.velocity.x + (a+0.13)*cart_func:get_sign(self.velocity.x), - y = self.velocity.y + (a+0.13)*cart_func:get_sign(self.velocity.y), - z = self.velocity.z + (a+0.13)*cart_func:get_sign(self.velocity.z), - } - elseif self.velocity.y > 0 then - self.velocity = { - x = self.velocity.x + (a-0.1)*cart_func:get_sign(self.velocity.x), - y = self.velocity.y + (a-0.1)*cart_func:get_sign(self.velocity.y), - z = self.velocity.z + (a-0.1)*cart_func:get_sign(self.velocity.z), - } - else - self.velocity = { - x = self.velocity.x + (a-0.03)*cart_func:get_sign(self.velocity.x), - y = self.velocity.y + (a-0.03)*cart_func:get_sign(self.velocity.y), - z = self.velocity.z + (a-0.03)*cart_func:get_sign(self.velocity.z), - } - - -- Place the cart exactly on top of the rail - if cart_func:is_rail(cart_func.v3:round(pos)) then - self.object:setpos({x=pos.x, y=math.floor(pos.y+0.5), z=pos.z}) - pos = self.object:getpos() - end - end - - -- Dont switch moving direction - -- Only if on flat railway - if dir.y == 0 then - if cart_func:get_sign(dir.x) ~= cart_func:get_sign(self.velocity.x) then - self.velocity.x = 0 - end - if cart_func:get_sign(dir.y) ~= cart_func:get_sign(self.velocity.y) then - self.velocity.y = 0 - end - if cart_func:get_sign(dir.z) ~= cart_func:get_sign(self.velocity.z) then - self.velocity.z = 0 - end - end - - -- Allow only one moving direction (multiply the other one with 0) - dir = cart_func:velocity_to_dir(self.velocity) - self.velocity = { - x = math.abs(self.velocity.x) * dir.x, - y = self.velocity.y, - z = math.abs(self.velocity.z) * dir.z, - } - - -- Move cart exactly on the rail - if dir.x ~= 0 and not cart_func:is_int(pos.z) then - pos.z = math.floor(0.5+pos.z) - self.object:setpos(pos) - elseif dir.z ~= 0 and not cart_func:is_int(pos.x) then - pos.x = math.floor(0.5+pos.x) - self.object:setpos(pos) - end - - -- Limit the velocity - if math.abs(self.velocity.x) > CART_MAX_VELOCITY then - self.velocity.x = CART_MAX_VELOCITY*cart_func:get_sign(self.velocity.x) - end - if math.abs(self.velocity.y) > CART_MAX_VELOCITY then - self.velocity.y = CART_MAX_VELOCITY*cart_func:get_sign(self.velocity.y) - end - if math.abs(self.velocity.z) > CART_MAX_VELOCITY then - self.velocity.z = CART_MAX_VELOCITY*cart_func:get_sign(self.velocity.z) - end - - self.object:setvelocity(self.velocity) - - self.old_pos = self.object:getpos() - self.old_velocity = cart_func.v3:copy(self.velocity) - - if dir.x < 0 then - self.object:setyaw(math.pi/2) - elseif dir.x > 0 then - self.object:setyaw(3*math.pi/2) - elseif dir.z < 0 then - self.object:setyaw(math.pi) - elseif dir.z > 0 then - self.object:setyaw(0) - end - - if dir.y == -1 then - self.object:set_animation({x=1, y=1}, 1, 0) - elseif dir.y == 1 then - self.object:set_animation({x=2, y=2}, 1, 0) - else - self.object:set_animation({x=0, y=0}, 1, 0) - end - -end - -minetest.register_entity("carts:cart", cart) +-- Load support for MT game translation. +local SL = lord.require_intllib() +carts = {} +carts.modpath = minetest.get_modpath("carts") +carts.railparams = {} +carts.get_translator = SL -minetest.register_craftitem("carts:cart", { - description = SL("Minecart"), - inventory_image = minetest.inventorycube("cart_top.png", "cart_side.png", "cart_side.png"), - wield_image = "cart_side.png", +-- Maximal speed of the cart in m/s (min = -1) +carts.speed_max = 7 +-- Set to -1 to disable punching the cart from inside (min = -1) +carts.punch_speed_max = 3 +-- Maximal distance for the path correction (for dtime peaks) +carts.path_distance_max = 3 - on_place = function(itemstack, placer, pointed_thing) - if not pointed_thing.type == "node" then - return - end - if cart_func:is_rail(pointed_thing.under) then - minetest.add_entity(pointed_thing.under, "carts:cart") - if not default.creative then - itemstack:take_item() - end - return itemstack - elseif cart_func:is_rail(pointed_thing.above) then - minetest.add_entity(pointed_thing.above, "carts:cart") - if not default.creative then - itemstack:take_item() - end - return itemstack - end - end, -}) -minetest.register_node("carts:rail", { - description = SL("Rail"), - drawtype = "raillike", - tiles = { - "carts_rail.png", - "carts_rail_curved.png", - "carts_rail_t_junction.png", - "carts_rail_crossing.png" - }, - inventory_image = "carts_rail.png", - wield_image = "carts_rail.png", - paramtype = "light", - is_ground_content = true, - walkable = false, - selection_box = { - type = "fixed", - fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2}, - }, - groups = {bendy=2, snappy=1, dig_immediate=2, attached_node=1, rail=1, connect_to_raillike=1}, -}) +dofile(carts.modpath.."/functions.lua") +dofile(carts.modpath.."/functions_new.lua") +dofile(carts.modpath.."/crafting.lua") +dofile(carts.modpath.."/cart_entity.lua") +dofile(carts.modpath.."/rails.lua") -minetest.register_node("carts:accelerating_rail", { - description = SL("Accelerating rail"), - drawtype = "raillike", - tiles = { - "carts_rail_acc.png", - "carts_rail_curved_acc.png", - "carts_rail_t_junction_acc.png", - "carts_rail_crossing_acc.png" - }, - inventory_image = "carts_rail_acc.png", - wield_image = "carts_rail_acc.png", - paramtype = "light", - is_ground_content = true, - walkable = false, - selection_box = { - type = "fixed", - fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2}, - }, - groups = {bendy=2, snappy=1,dig_immediate=2,attached_node=1,rail=1,connect_to_raillike=1}, - on_construct = function(pos) - local accel_pos = {x = pos.x, y = pos.y - 1, z = pos.z} - if cart_func:is_accelerator(accel_pos) then - minetest.get_meta(pos):set_string("cart_acceleration", CART_ACCELERATION) - else - minetest.get_meta(pos):set_string("cart_acceleration", "0") - end - end, - on_destruct = function(pos) - minetest.get_meta(pos):set_string("cart_acceleration", "0") - end, -}) -minetest.register_node("carts:stopping_rail", { - description = SL("Stopping rail"), - drawtype = "raillike", - tiles = { - "carts_rail_brk.png", - "carts_rail_curved_brk.png", - "carts_rail_t_junction_brk.png", - "carts_rail_crossing_brk.png" - }, - inventory_image = "carts_rail_brk.png", - wield_image = "carts_rail_brk.png", - paramtype = "light", - is_ground_content = true, - walkable = false, - selection_box = { - type = "fixed", - fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2}, - }, - on_construct = function(pos) - minetest.get_meta(pos):set_string("cart_acceleration", "-0.2") - end, - on_destruct = function(pos) - minetest.get_meta(pos):set_string("cart_acceleration", "0") - end, - groups = {bendy=2, snappy=1, dig_immediate=2, attached_node=1, rail=1, - connect_to_raillike=1}, -}) diff --git a/mods/carts/rails.lua b/mods/carts/rails.lua new file mode 100644 index 000000000..61e7124f4 --- /dev/null +++ b/mods/carts/rails.lua @@ -0,0 +1,56 @@ +-- carts/rails.lua + +-- support for MT game translation. +local SL = lord.require_intllib() + + +carts:register_rail("carts:rail", { + description = SL("Rail"), + tiles = { + "carts_rail.png", + "carts_rail_curved.png", + "carts_rail_t_junction.png", + "carts_rail_crossing.png" + }, + inventory_image = "carts_rail.png", + wield_image = "carts_rail.png", + groups = carts:get_rail_groups(), +}) + +carts:register_rail("carts:stopping_rail", { + description = SL("Stopping rail"), + tiles = { + "carts_rail_brk.png", + "carts_rail_curved_brk.png", + "carts_rail_t_junction_brk.png", + "carts_rail_crossing_brk.png" + }, + inventory_image = "carts_rail_brk.png", + wield_image = "carts_rail_brk.png", + groups = carts:get_rail_groups(), +}, {acceleration = -5}) + +carts:register_rail("carts:accelerating_rail", { + description = SL("Accelerating rail"), + tiles = { + "carts_rail_acc.png", + "carts_rail_curved_acc.png", + "carts_rail_t_junction_acc.png", + "carts_rail_crossing_acc.png" + }, + inventory_image = "carts_rail_acc.png", + wield_image = "carts_rail_acc.png", + groups = carts:get_rail_groups(), + after_place_node = function(pos, placer, itemstack, pointed_thing) + if minetest.is_protected(pos, placer:get_player_name()) then return true end + + local accel_pos = {x = pos.x, y = pos.y - 1, z = pos.z} + if not cart_func:is_accelerator(accel_pos) then + minetest.chat_send_player(placer:get_player_name(), "Низзя ставить ускоряющий рельс") + minetest.set_node(pos, { name = "air" }) + end + return itemstack + end, +}, {acceleration = 5}) + + diff --git a/mods/carts/sounds/carts_cart_moving.1.ogg b/mods/carts/sounds/carts_cart_moving.1.ogg new file mode 100644 index 000000000..869e765b8 Binary files /dev/null and b/mods/carts/sounds/carts_cart_moving.1.ogg differ diff --git a/mods/carts/sounds/carts_cart_moving.2.ogg b/mods/carts/sounds/carts_cart_moving.2.ogg new file mode 100644 index 000000000..b4cc50843 Binary files /dev/null and b/mods/carts/sounds/carts_cart_moving.2.ogg differ diff --git a/mods/carts/sounds/carts_cart_moving.3.ogg b/mods/carts/sounds/carts_cart_moving.3.ogg new file mode 100644 index 000000000..e19a782d8 Binary files /dev/null and b/mods/carts/sounds/carts_cart_moving.3.ogg differ diff --git a/mods/carts/textures/carts_cart.png b/mods/carts/textures/carts_cart.png new file mode 100644 index 000000000..965347c0f Binary files /dev/null and b/mods/carts/textures/carts_cart.png differ diff --git a/mods/carts/textures/carts_cart_front.png b/mods/carts/textures/carts_cart_front.png new file mode 100644 index 000000000..38955b25a Binary files /dev/null and b/mods/carts/textures/carts_cart_front.png differ diff --git a/mods/carts/textures/carts_cart_side.png b/mods/carts/textures/carts_cart_side.png new file mode 100644 index 000000000..f53808c65 Binary files /dev/null and b/mods/carts/textures/carts_cart_side.png differ diff --git a/mods/carts/textures/carts_cart_top.png b/mods/carts/textures/carts_cart_top.png new file mode 100644 index 000000000..d9a31a9d2 Binary files /dev/null and b/mods/carts/textures/carts_cart_top.png differ