Compare commits

...

5 Commits

Author SHA1 Message Date
ABJ-MV 4869727843 Create depends.txt 2015-05-19 21:24:19 +05:00
ABJ-MV 3bcfc99f68 Create README.txt 2015-05-19 21:23:29 +05:00
ABJ-MV 9462a53338 Create functions.lua 2015-05-19 21:21:29 +05:00
ABJ-MV 20bcdc4495 Create rails.lua 2015-05-19 21:20:34 +05:00
ABJ-MV 29e27ac586 Create init.lua 2015-05-19 21:19:40 +05:00
5 changed files with 686 additions and 0 deletions

30
mods/hicarts/README.txt Normal file
View File

@ -0,0 +1,30 @@
Minetest mod: hicarts
=======================
Based on (but NOT compatible with) the mod "boost_cart" by Krock, a fork of "carts" by PilzAdam
(Which is also incompatible)
Target: Change the rail looks to be more sleek and clean and "shiny", and add lots of more features to the original mod.
License of source code:
WTFPL
License of media (textures, sounds and models):
CC-0
Authors of media files:
kddekadenz:
cart_bottom.png
cart_side.png
cart_top.png
Zeg9:
cart.x
cart.png

1
mods/hicarts/depends.txt Normal file
View File

@ -0,0 +1 @@
defaultmesecons?moreores?

150
mods/hicarts/functions.lua Normal file
View File

@ -0,0 +1,150 @@
function hicarts:get_sign(z)
if z == 0 then
return 0
else
return z / math.abs(z)
end
end
function hicarts:velocity_to_dir(v)
if math.abs(v.x) > math.abs(v.z) then
return {x=hicarts:get_sign(v.x), y=hicarts:get_sign(v.y), z=0}
else
return {x=0, y=hicarts:get_sign(v.y), z=hicarts:get_sign(v.z)}
end
end
function hicarts: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 hicarts:check_front_up_down(pos, dir_, check_down, railtype)
local dir = vector.new(dir_)
local cur = nil
-- Front
dir.y = 0
cur = vector.add(pos, dir)
if hicarts:is_rail(cur, railtype) then
return dir
end
-- Up
if check_down then
dir.y = 1
cur = vector.add(pos, dir)
if hicarts:is_rail(cur, railtype) then
return dir
end
end
-- Down
dir.y = -1
cur = vector.add(pos, dir)
if hicarts:is_rail(cur, railtype) then
return dir
end
return nil
end
function hicarts:get_rail_direction(pos_, dir, ctrl, old_switch, railtype)
local pos = vector.round(pos_)
local cur = nil
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
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 = hicarts: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 = hicarts:check_front_up_down(pos, right, false, railtype)
if cur then
return cur, 2
end
right_check = true
end
end
-- Normal
cur = hicarts:check_front_up_down(pos, dir, true, railtype)
if cur then
return cur
end
-- Left, if not already checked
if left_check then
cur = hicarts: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 = hicarts:check_front_up_down(pos, right, false, railtype)
if cur then
return cur
end
end
-- Backwards
if not old_switch then
cur = hicarts:check_front_up_down(pos, {
x = -dir.x,
y = dir.y,
z = -dir.z
}, true, railtype)
if cur then
return cur
end
end
return {x=0, y=0, z=0}
end
function hicarts:boost_rail(pos, amount)
minetest.get_meta(pos):set_string("cart_acceleration", tostring(amount))
for _,obj_ in ipairs(minetest.get_objects_inside_radius(pos, 0.5)) do
if not obj_:is_player() and
obj_:get_luaentity() and
obj_:get_luaentity().name == "carts:cart" then
obj_:get_luaentity():on_punch()
end
end
end

309
mods/hicarts/init.lua Normal file
View File

@ -0,0 +1,309 @@
-- TODO:
-- Add a todo list
hicarts = {}
hicarts.modpath = minetest.get_modpath("hicarts")
hicarts.speed_max = 10
function vector.floor(v)
return {
x = math.floor(v.x),
y = math.floor(v.y),
z = math.floor(v.z)
}
end
dofile(hicarts.modpath.."/functions.lua")
dofile(hicarts.modpath.."/rails.lua")
hicarts.cart = {
physical = true,
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,
punched = false, -- used to re-send velocity and position
velocity = {x=0, y=0, z=0}, -- only used on punch
old_dir = {x=0, y=0, z=0},
old_pos = nil,
old_switch = 0,
railtype = nil,
attached_items = {}
}
function hicarts.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
default.player_attached[player_name] = true
clicker:set_attach(self.object, "", {x=0, y=3, z=0}, {x=0, y=0, z=0})
end
end
function hicarts.cart:on_activate(staticdata, dtime_s)
self.object:set_armor_groups({immortal=1})
end
function hicarts.cart:on_punch(puncher, time_from_last_punch, tool_capabilities, direction)
local pos = self.object:getpos()
if not self.railtype then
local node = minetest.get_node(vector.floor(pos)).name
self.railtype = minetest.get_item_group(node, "connect_to_raillike")
end
if not puncher or not puncher:is_player() then
local cart_dir = hicarts:get_rail_direction(pos, {x=1, y=0, z=0}, 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
return
end
if puncher:get_player_control().sneak then
if self.driver then
if self.old_pos then
self.object:setpos(self.old_pos)
end
default.player_attached[self.driver] = nil
local player = minetest.get_player_by_name(self.driver)
if player then
player:set_detach()
end
end
for _,obj_ in ipairs(self.attached_items) do
if obj_ then
obj_:set_detach()
end
end
self.object:remove()
puncher:get_inventory():add_item("main", "carts:cart")
return
end
local vel = self.object:getvelocity()
if puncher:get_player_name() == self.driver then
if math.abs(vel.x + vel.z) > 7 then
return
end
end
local punch_dir = hicarts:velocity_to_dir(puncher:get_look_dir())
punch_dir.y = 0
local cart_dir = hicarts:get_rail_direction(pos, punch_dir, nil, nil, self.railtype)
if vector.equals(cart_dir, {x=0, y=0, z=0}) then
return
end
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)
self.old_pos = nil
self.punched = true
end
function hicarts.cart:on_step(dtime)
local vel = self.object:getvelocity()
local update = {}
if self.punched then
vel = vector.add(vel, self.velocity)
self.object:setvelocity(vel)
self.old_dir.y = 0
elseif vector.equals(vel, {x=0, y=0, z=0}) then
return
end
local dir, last_switch = nil, nil
local pos = self.object:getpos()
if self.old_pos and not self.punched then
local flo_pos = vector.floor(pos)
local flo_old = vector.floor(self.old_pos)
if vector.equals(flo_pos, flo_old) then
return
end
end
local ctrl, player = nil, nil
if self.driver then
player = minetest.get_player_by_name(self.driver)
if player then
ctrl = player:get_player_control()
end
end
if self.old_pos then
local diff = vector.subtract(self.old_pos, pos)
for _,v in ipairs({"x","y","z"}) do
if math.abs(diff[v]) > 1.1 then
local expected_pos = vector.add(self.old_pos, self.old_dir)
dir, last_switch = hicarts:get_rail_direction(pos, self.old_dir, ctrl, self.old_switch, self.railtype)
if vector.equals(dir, {x=0, y=0, z=0}) then
dir = false
pos = vector.new(expected_pos)
update.pos = true
end
break
end
end
end
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
update.vel = true
end
end
end
local cart_dir = hicarts:velocity_to_dir(vel)
local max_vel = hicarts.speed_max
if not dir then
dir, last_switch = hicarts:get_rail_direction(pos, cart_dir, ctrl, self.old_switch, self.railtype)
end
local new_acc = {x=0, y=0, z=0}
if vector.equals(dir, {x=0, y=0, z=0}) then
vel = {x=0, y=0, z=0}
update.vel = true
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)
update.pos = true
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)
update.pos = true
end
-- Up, down?
if dir.y ~= self.old_dir.y then
vel.y = dir.y * math.abs(vel.x + vel.z)
pos = vector.round(pos)
update.pos = true
end
-- Slow down or speed up..
local acc = dir.y * -1.8
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
acc = acc + (speed_mod * 8)
else
acc = acc - 0.4
-- Handbrake
if ctrl and ctrl.down and math.abs(vel.x + vel.z) > 1.2 then
acc = acc - 1.2
end
end
new_acc = vector.multiply(dir, acc)
end
self.object:setacceleration(new_acc)
self.old_pos = vector.new(pos)
self.old_dir = vector.new(dir)
self.old_switch = last_switch
-- Limits
for _,v in ipairs({"x","y","z"}) do
if math.abs(vel[v]) > max_vel then
vel[v] = hicarts:get_sign(vel[v]) * max_vel
update.vel = true
end
end
if self.punched 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
self.punched = false
end
if not (update.vel or update.pos) then
return
end
local yaw = 0
if dir.x < 0 then
yaw = 0.5
elseif dir.x > 0 then
yaw = 1.5
elseif dir.z < 0 then
yaw = 1
end
self.object:setyaw(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)
self.object:setvelocity(vel)
if update.pos then
self.object:setpos(pos)
end
update = nil
end
minetest.register_entity(":carts:cart", hicarts.cart)
minetest.register_craftitem(":carts:cart", {
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 hicarts:is_rail(pointed_thing.under) then
minetest.add_entity(pointed_thing.under, "carts:cart")
elseif hicarts:is_rail(pointed_thing.above) then
minetest.add_entity(pointed_thing.above, "carts:cart")
else return end
itemstack:take_item()
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"},
},
})

196
mods/hicarts/rails.lua Normal file
View File

@ -0,0 +1,196 @@
if minetest.get_modpath("moreores") then
minetest.register_node("hicarts:silver_rail", {
light_source = 7,
description = "Silver Rail",
drawtype = "raillike",
tiles = {"cart_rail_silver.png", "cart_rail_curved_silver.png", "cart_rail_t_junction_silver.png", "cart_rail_crossing_silver.png"},
inventory_image = "cart_rail_silver.png",
wield_image = "cart_rail_silver.png",
paramtype = "light",
sunlight_propagates = true,
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 = {dig_immediate = 2, attached_node = 1, rail = 1, connect_to_raillike = 1},
})
minetest.register_craft({
output = "hicarts:silver_rail 6",
recipe = {
{"moreores:silver_ingot", "", "moreores:silver_ingot"},
{"moreores:silver_ingot", "group:stick", "moreores:silver_ingot"},
{"moreores:silver_ingot", "", "moreores:silver_ingot"},
}}
)
end
minetest.register_node("hicarts:gold_rail", {
light_source = 5,
description = "Gold Rail",
drawtype = "raillike",
tiles = {"cart_rail_gold.png", "cart_rail_curved_gold.png", "cart_rail_t_junction_gold.png", "cart_rail_crossing_gold.png"},
inventory_image = "cart_rail_gold.png",
wield_image = "cart_rail_gold.png",
paramtype = "light",
sunlight_propagates = true,
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 = {dig_immediate = 2, attached_node = 1, rail = 1, connect_to_raillike = 1},
})
minetest.register_craft({
output = "hicarts:gold_rail 6",
recipe = {
{"default:gold_ingot", "", "default:gold_ingot"},
{"default:gold_ingot", "group:stick", "default:gold_ingot"},
{"default:gold_ingot", "", "default:gold_ingot"},
}}
)
minetest.register_node(":default:rail", {
description = "Rail",
drawtype = "raillike",
tiles = {"cart_rail.png", "cart_rail_curved.png", "cart_rail_t_junction.png", "cart_rail_crossing.png"},
inventory_image = "cart_rail.png",
wield_image = "cart_rail.png",
paramtype = "light",
sunlight_propagates = true,
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 = {dig_immediate = 2, attached_node = 1, rail = 1, connect_to_raillike = 1},
})
if minetest.get_modpath("moreores") then
-- Moreores' copper rail
minetest.register_alias("carts:copperrail", "moreores:copper_rail")
else
minetest.register_node(":carts:copperrail", {
description = "Copper rail",
drawtype = "raillike",
tiles = {"carts_rail_cp.png", "carts_rail_curved_cp.png", "carts_rail_t_junction_cp.png", "carts_rail_crossing_cp.png"},
inventory_image = "carts_rail_cp.png",
wield_image = "carts_rail_cp.png",
paramtype = "light",
is_ground_content = true,
walkable = false,
selection_box = {
type = "fixed",
-- but how to specify the dimensions for curved and sideways rails?
fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
},
groups = {dig_immediate = 2, attached_node = 1, rail = 1, connect_to_raillike = 1},
})
minetest.register_craft({
output = "carts:copperrail 12",
recipe = {
{"default:copper_ingot", "group:stick", "default:copper_ingot"},
{"default:copper_ingot", "group:stick", "default:copper_ingot"},
{"default:copper_ingot", "group:stick", "default:copper_ingot"},
}
})
end
-- Speed up
minetest.register_node(":carts:powerrail", {
description = "Powered rail",
drawtype = "raillike",
tiles = {"carts_rail_pwr.png", "carts_rail_curved_pwr.png", "carts_rail_t_junction_pwr.png", "carts_rail_crossing_pwr.png"},
inventory_image = "carts_rail_pwr.png",
wield_image = "carts_rail_pwr.png",
paramtype = "light",
is_ground_content = true,
walkable = false,
selection_box = {
type = "fixed",
-- but how to specify the dimensions for curved and sideways rails?
fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
},
groups = {dig_immediate = 2, attached_node = 1, rail = 1, connect_to_raillike = 1},
after_place_node = function(pos, placer, itemstack)
if not mesecon then
minetest.get_meta(pos):set_string("cart_acceleration", "0.5")
end
end,
mesecons = {
effector = {
action_on = function(pos, node)
hicarts:boost_rail(pos, 0.5)
end,
action_off = function(pos, node)
minetest.get_meta(pos):set_string("cart_acceleration", "0")
end,
},
},
})
minetest.register_craft({
output = "carts:powerrail 6",
recipe = {
{"default:steel_ingot", "default:mese_crystal_fragment", "default:steel_ingot"},
{"default:steel_ingot", "group:stick", "default:steel_ingot"},
{"default:steel_ingot", "default:mese_crystal_fragment", "default:steel_ingot"},
}
})
minetest.register_node(":carts:brakerail", {
description = "Brake 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",
-- but how to specify the dimensions for curved and sideways rails?
fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
},
groups = {dig_immediate = 2, attached_node = 1, rail = 1, connect_to_raillike = 1},
after_place_node = function(pos, placer, itemstack)
if not mesecon then
minetest.get_meta(pos):set_string("cart_acceleration", "-0.2")
end
end,
mesecons = {
effector = {
action_on = function(pos, node)
hicarts:boost_rail(pos, -0.2)
end,
action_off = function(pos, node)
minetest.get_meta(pos):set_string("cart_acceleration", "0")
end,
},
},
})
minetest.register_craft({
output = "carts:brakerail 6",
recipe = {
{"default:steel_ingot", "default:coal_lump", "default:steel_ingot"},
{"default:steel_ingot", "group:stick", "default:steel_ingot"},
{"default:steel_ingot", "default:coal_lump", "default:steel_ingot"},
}
})