Compare commits
86 Commits
mff
...
rotationte
Author | SHA1 | Date | |
---|---|---|---|
792756c003 | |||
49028ca123 | |||
ca55c6f3c0 | |||
4498605509 | |||
e19d8701ce | |||
9a2a2b9d69 | |||
6ac106c1a7 | |||
be0c56edef | |||
5502952bbd | |||
ad93d338b3 | |||
e3688b5380 | |||
2229ededa5 | |||
e8119a63cd | |||
11a1ebad74 | |||
3c646ee4df | |||
af25c5411b | |||
b80d3aed4c | |||
08918701b0 | |||
66e4613432 | |||
61b19d77d9 | |||
acf697cd43 | |||
8b4487c2b8 | |||
cba969903a | |||
71af46f027 | |||
99d0480169 | |||
de5fb1825a | |||
cf9b82de37 | |||
74d8b8f931 | |||
75132269d7 | |||
b741282843 | |||
7d6132e748 | |||
4391d2356f | |||
5526754e37 | |||
f3bc9d795d | |||
eb6a833b66 | |||
d166b7ea68 | |||
947d8633ff | |||
9d059b8e31 | |||
cbe0ab9770 | |||
d63f8ae024 | |||
46c03dd4d1 | |||
d54ca696c5 | |||
3cac3d8701 | |||
73722136a0 | |||
307b5d1f92 | |||
6e01bf2bf9 | |||
1b53ebd8e6 | |||
ddb6801b5c | |||
145ca1c61d | |||
afb52ba3b4 | |||
d58f9930d1 | |||
a7fd733819 | |||
adfcfe0841 | |||
79905e47f4 | |||
159d4ec603 | |||
df04aa2a4d | |||
670f961d35 | |||
eb610a5e9c | |||
f88f0be7ec | |||
1aaa83b428 | |||
b6d17c1b34 | |||
573d56de1a | |||
2929d2ceb7 | |||
717721c912 | |||
b6f476db37 | |||
a59864dd74 | |||
5373a05c9a | |||
0fa8c88574 | |||
f52e4e3641 | |||
faaa422fae | |||
c8f03513e3 | |||
21e309c414 | |||
c07c92a1fb | |||
3d0401f633 | |||
59b1512cc3 | |||
70f07f4158 | |||
c106499bd5 | |||
fa2f2cb71c | |||
64fa36e796 | |||
8280738bf6 | |||
4652679f7d | |||
007037c633 | |||
a38aa7d83b | |||
dc20f048b1 | |||
6332c670d9 | |||
e8a8ad4324 |
60
README.txt
@ -1,23 +1,57 @@
|
|||||||
Minetest mod: boost_cart
|
Minetest mod: boost_cart
|
||||||
=======================
|
==========================
|
||||||
Based on the mod "carts" by PilzAdam
|
Based on (and fully compatible with) the mod "carts" by PilzAdam
|
||||||
Target: Run smoothly and do not use too much CPU
|
and the one contained in the subgame "minetest_game".
|
||||||
|
Target: Run smoothly as possible, even on laggy servers.
|
||||||
|
|
||||||
License of source code:
|
|
||||||
-----------------------
|
|
||||||
WTFPL
|
|
||||||
|
|
||||||
License of media (textures, sounds and models):
|
Features
|
||||||
-----------------------------------------------
|
----------
|
||||||
CC-0
|
- A fast cart for your railway or roller coaster
|
||||||
|
- Easily configurable cart speed using the Advanced Settings
|
||||||
|
- Boost and brake rails
|
||||||
|
- By mesecons controlled Start-Stop rails
|
||||||
|
- Detector rails that send a mesecons signal when the cart drives over them
|
||||||
|
- Rail junction switching with the 'right/left' walking keys
|
||||||
|
- Handbrake with the 'back' key
|
||||||
|
- Support for non-minetest_game subgames
|
||||||
|
|
||||||
Authors of media files:
|
|
||||||
-----------------------
|
Settings
|
||||||
kddekadenz:
|
----------
|
||||||
|
This mod can be adjusted to fit the conditions of a player or server.
|
||||||
|
Use the Advanced Settings dialog in the main menu or tune your
|
||||||
|
minetest.conf file manually:
|
||||||
|
|
||||||
|
boost_cart.speed_max = 10
|
||||||
|
^ Possible values: 1 ... 100
|
||||||
|
^ Maximal speed of the cart in m/s
|
||||||
|
|
||||||
|
boost_cart.punch_speed_max = 7
|
||||||
|
^ Possible values: -1 ... 100
|
||||||
|
^ Maximal speed to which the driving player can accelerate the cart
|
||||||
|
by punching from inside the cart. -1 will disable this feature.
|
||||||
|
|
||||||
|
|
||||||
|
License for everything
|
||||||
|
------------------------
|
||||||
|
CC-0, if not specified otherwise below
|
||||||
|
|
||||||
|
|
||||||
|
Authors
|
||||||
|
---------
|
||||||
|
Various authors
|
||||||
|
carts_rail_*.png
|
||||||
|
|
||||||
|
kddekadenz
|
||||||
cart_bottom.png
|
cart_bottom.png
|
||||||
cart_side.png
|
cart_side.png
|
||||||
cart_top.png
|
cart_top.png
|
||||||
|
|
||||||
Zeg9:
|
klankbeeld (CC-BY 3.0)
|
||||||
|
http://freesound.org/people/klankbeeld/sounds/174042/
|
||||||
|
cart_rail.*.ogg
|
||||||
|
|
||||||
|
Zeg9
|
||||||
cart.x
|
cart.x
|
||||||
cart.png
|
cart.png
|
437
cart_entity.lua
Normal file
@ -0,0 +1,437 @@
|
|||||||
|
|
||||||
|
local HAVE_MESECONS_ENABLED = minetest.global_exists("mesecon")
|
||||||
|
|
||||||
|
function boost_cart:on_rail_step(entity, pos, distance)
|
||||||
|
-- Play rail sound
|
||||||
|
if entity.sound_counter <= 0 then
|
||||||
|
minetest.sound_play("cart_rail", {
|
||||||
|
pos = pos,
|
||||||
|
max_hear_distance = 40,
|
||||||
|
gain = 0.5
|
||||||
|
})
|
||||||
|
entity.sound_counter = math.random(4, 15)
|
||||||
|
end
|
||||||
|
entity.sound_counter = entity.sound_counter - distance
|
||||||
|
|
||||||
|
if HAVE_MESECONS_ENABLED then
|
||||||
|
boost_cart:signal_detector_rail(pos)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local cart_entity = {
|
||||||
|
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,
|
||||||
|
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,
|
||||||
|
sound_counter = 0,
|
||||||
|
railtype = nil,
|
||||||
|
attached_items = {},
|
||||||
|
automatic_face_movement_dir = -90.0,
|
||||||
|
automatic_face_movement_max_rotation_per_sec = 360 * 4,
|
||||||
|
}
|
||||||
|
|
||||||
|
-- Model and textures
|
||||||
|
if boost_cart.mtg_compat then
|
||||||
|
cart_entity.mesh = "carts_cart.b3d"
|
||||||
|
cart_entity.textures = {"carts_cart.png"}
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
boost_cart:manage_attachment(clicker, nil)
|
||||||
|
elseif not self.driver then
|
||||||
|
self.driver = player_name
|
||||||
|
boost_cart:manage_attachment(clicker, self.object)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function cart_entity:on_activate(staticdata, dtime_s)
|
||||||
|
self.object:set_armor_groups({immortal=1})
|
||||||
|
self.sound_counter = math.random(4, 15)
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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.punched = true
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if puncher:get_player_control().sneak then
|
||||||
|
-- Pick up cart: Drop all attachments
|
||||||
|
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)
|
||||||
|
boost_cart:manage_attachment(player, nil)
|
||||||
|
end
|
||||||
|
for _, obj_ in pairs(self.attached_items) do
|
||||||
|
if obj_ then
|
||||||
|
obj_:set_detach()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local leftover = puncher:get_inventory():add_item("main", "carts:cart")
|
||||||
|
if not leftover:is_empty() then
|
||||||
|
minetest.add_item(pos, leftover)
|
||||||
|
end
|
||||||
|
|
||||||
|
self.object:remove()
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Driver punches to accelerate the cart
|
||||||
|
if puncher:get_player_name() == self.driver then
|
||||||
|
if math.abs(vel.x + vel.z) > boost_cart.punch_speed_max then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
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 = 3 * (time_from_last_punch / punch_interval)
|
||||||
|
|
||||||
|
self.velocity = vector.multiply(cart_dir, f)
|
||||||
|
self.old_dir = cart_dir
|
||||||
|
self.punched = true
|
||||||
|
end
|
||||||
|
|
||||||
|
local v3_len = vector.length
|
||||||
|
function cart_entity:on_step(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 = boost_cart: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
|
||||||
|
local distance = 1
|
||||||
|
|
||||||
|
-- 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()
|
||||||
|
distance = dtime * (v3_len(vel) + 0.5 * dtime * v3_len(acc))
|
||||||
|
|
||||||
|
local new_pos, new_dir = boost_cart: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
|
||||||
|
|
||||||
|
-- dir: New moving direction of the cart
|
||||||
|
-- switch_keys: Currently pressed L(1) or R(2) key,
|
||||||
|
-- used to ignore the key on the next rail node
|
||||||
|
local dir, switch_keys = boost_cart: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 boost_cart:is_rail(pos_r, self.railtype) 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, v3_len(vel))
|
||||||
|
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
|
||||||
|
|
||||||
|
-- Calculate current cart acceleration
|
||||||
|
local acc = nil
|
||||||
|
|
||||||
|
local acc_meta = minetest.get_meta(pos):get_string("cart_acceleration")
|
||||||
|
if acc_meta == "halt" and not self.punched then
|
||||||
|
-- Stop rail
|
||||||
|
vel = {x=0, y=0, z=0}
|
||||||
|
acc = false
|
||||||
|
pos = vector.round(pos)
|
||||||
|
update.pos = true
|
||||||
|
update.vel = true
|
||||||
|
end
|
||||||
|
if acc == nil then
|
||||||
|
-- Meta speed modifier
|
||||||
|
local speed_mod = tonumber(acc_meta)
|
||||||
|
if speed_mod and speed_mod ~= 0 then
|
||||||
|
-- Try to make it similar to the original carts mod
|
||||||
|
acc = speed_mod * 10
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if acc == nil and boost_cart.mtg_compat then
|
||||||
|
-- MTG Cart API adaption
|
||||||
|
local rail_node = minetest.get_node(vector.round(pos))
|
||||||
|
local railparam = carts.railparams[rail_node.name]
|
||||||
|
if railparam and railparam.acceleration then
|
||||||
|
acc = railparam.acceleration
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if acc ~= false then
|
||||||
|
-- Handbrake
|
||||||
|
if ctrl and ctrl.down then
|
||||||
|
acc = (acc or 0) - 2
|
||||||
|
elseif acc == nil then
|
||||||
|
acc = -0.4
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if acc then
|
||||||
|
-- Slow down or speed up, depending on Y direction
|
||||||
|
acc = acc + dir.y * -2.1
|
||||||
|
else
|
||||||
|
acc = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
new_acc = vector.multiply(dir, acc)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Limits
|
||||||
|
local max_vel = boost_cart.speed_max
|
||||||
|
for _,v in pairs({"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
|
||||||
|
update.vel = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
self.object:set_acceleration(new_acc)
|
||||||
|
self.old_pos = vector.round(pos)
|
||||||
|
local old_y_dir = self.old_dir.y
|
||||||
|
if not vector.equals(dir, {x=0, y=0, z=0}) and not stop_wiggle then
|
||||||
|
self.old_dir = dir
|
||||||
|
else
|
||||||
|
-- Cart stopped, set the animation to 0
|
||||||
|
self.old_dir.y = 0
|
||||||
|
end
|
||||||
|
self.old_switch = switch_keys
|
||||||
|
|
||||||
|
boost_cart:on_rail_step(self, self.old_pos, distance)
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
if not (update.vel or update.pos) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
-- Re-use "dir", localize self.old_dir
|
||||||
|
dir = self.old_dir
|
||||||
|
|
||||||
|
if stop_wiggle then
|
||||||
|
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:set_yaw(yaw * math.pi)
|
||||||
|
end
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
-- Change player model rotation, depending on the Y direction
|
||||||
|
if player and dir.y ~= old_y_dir then
|
||||||
|
local feet = {x=0, y=0, z=0}
|
||||||
|
local eye = {x=0, y=-4, z=0}
|
||||||
|
feet.y = boost_cart.old_player_model and 6 or -4
|
||||||
|
if dir.y ~= 0 then
|
||||||
|
-- TODO: Find a better way to calculate this
|
||||||
|
if boost_cart.old_player_model then
|
||||||
|
feet.y = feet.y + 2
|
||||||
|
feet.z = -dir.y * 6
|
||||||
|
else
|
||||||
|
feet.y = feet.y + 4
|
||||||
|
feet.z = -dir.y * 2
|
||||||
|
end
|
||||||
|
eye.z = -dir.y * 8
|
||||||
|
end
|
||||||
|
player:set_attach(self.object, "", feet,
|
||||||
|
{x=dir.y * -30, y=0, z=0})
|
||||||
|
player:set_eye_offset(eye, eye)
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
minetest.register_entity(":carts:cart", cart_entity)
|
||||||
|
|
||||||
|
-- Register item to place the entity
|
||||||
|
if not boost_cart.mtg_compat then
|
||||||
|
minetest.register_craftitem(":carts:cart", {
|
||||||
|
description = "Cart (Sneak+Click to pick up)",
|
||||||
|
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")
|
||||||
|
elseif boost_cart:is_rail(pointed_thing.above) then
|
||||||
|
minetest.add_entity(pointed_thing.above, "carts:cart")
|
||||||
|
else
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if not minetest.settings:get_bool("creative_mode") 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"},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
end
|
@ -1 +1,4 @@
|
|||||||
default
|
default
|
||||||
|
mesecons?
|
||||||
|
moreores?
|
||||||
|
carts?
|
1
description.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
This mod offers improved minecarts and a few more rail types.
|
58
detector.lua
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
local mesecons_rules = mesecon.rules.flat
|
||||||
|
|
||||||
|
function boost_cart:turnoff_detector_rail(pos)
|
||||||
|
local node = minetest.get_node(pos)
|
||||||
|
if minetest.get_item_group(node.name, "detector_rail") == 1 then
|
||||||
|
if node.name == "boost_cart:detectorrail_on" then --has not been dug
|
||||||
|
minetest.swap_node(pos, {name = "boost_cart:detectorrail", param2=node.param2})
|
||||||
|
end
|
||||||
|
mesecon.receptor_off(pos, mesecons_rules)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function boost_cart:signal_detector_rail(pos)
|
||||||
|
local node = minetest.get_node(pos)
|
||||||
|
if minetest.get_item_group(node.name, "detector_rail") ~= 1 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if node.name == "boost_cart:detectorrail" then
|
||||||
|
minetest.swap_node(pos, {name = "boost_cart:detectorrail_on", param2=node.param2})
|
||||||
|
end
|
||||||
|
mesecon.receptor_on(pos, mesecons_rules)
|
||||||
|
minetest.after(0.5, boost_cart.turnoff_detector_rail, boost_cart, pos)
|
||||||
|
end
|
||||||
|
|
||||||
|
boost_cart:register_rail("boost_cart:detectorrail", {
|
||||||
|
description = "Detector rail",
|
||||||
|
tiles = {
|
||||||
|
"carts_rail_straight_dtc.png", "carts_rail_curved_dtc.png",
|
||||||
|
"carts_rail_t_junction_dtc.png", "carts_rail_crossing_dtc.png"
|
||||||
|
},
|
||||||
|
groups = boost_cart:get_rail_groups({detector_rail = 1}),
|
||||||
|
|
||||||
|
mesecons = {receptor = {state = "off", rules = mesecons_rules}},
|
||||||
|
})
|
||||||
|
|
||||||
|
boost_cart:register_rail("boost_cart:detectorrail_on", {
|
||||||
|
description = "Detector rail ON (you hacker you)",
|
||||||
|
tiles = {
|
||||||
|
"carts_rail_straight_dtc_on.png", "carts_rail_curved_dtc_on.png",
|
||||||
|
"carts_rail_t_junction_dtc_on.png", "carts_rail_crossing_dtc_on.png"
|
||||||
|
},
|
||||||
|
groups = boost_cart:get_rail_groups({
|
||||||
|
detector_rail = 1, not_in_creative_inventory = 1
|
||||||
|
}),
|
||||||
|
drop = "boost_cart:detectorrail",
|
||||||
|
|
||||||
|
mesecons = {receptor = {state = "on", rules = mesecons_rules}},
|
||||||
|
})
|
||||||
|
|
||||||
|
minetest.register_craft({
|
||||||
|
output = "boost_cart:detectorrail 6",
|
||||||
|
recipe = {
|
||||||
|
{"default:steel_ingot", "mesecons:wire_00000000_off", "default:steel_ingot"},
|
||||||
|
{"default:steel_ingot", "group:stick", "default:steel_ingot"},
|
||||||
|
{"default:steel_ingot", "mesecons:wire_00000000_off", "default:steel_ingot"},
|
||||||
|
},
|
||||||
|
})
|
269
functions.lua
@ -6,98 +6,245 @@ function boost_cart:get_sign(z)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function boost_cart:manage_attachment(player, obj)
|
||||||
|
if not player then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local status = obj ~= nil
|
||||||
|
local player_name = player:get_player_name()
|
||||||
|
if default.player_attached[player_name] == status then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
default.player_attached[player_name] = status
|
||||||
|
|
||||||
|
if status then
|
||||||
|
local y_pos = self.old_player_model and 6 or -4
|
||||||
|
player:set_attach(obj, "", {x=0, y=y_pos, 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
|
||||||
|
player:set_detach()
|
||||||
|
player:set_eye_offset({x=0, y=0, z=0},{x=0, y=0, z=0})
|
||||||
|
-- HACK in effect! Force updating the attachment rotation
|
||||||
|
player:set_properties({})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function boost_cart:velocity_to_dir(v)
|
function boost_cart:velocity_to_dir(v)
|
||||||
if math.abs(v.x) > math.abs(v.z) then
|
if math.abs(v.x) > math.abs(v.z) then
|
||||||
return {x=boost_cart:get_sign(v.x), y=boost_cart:get_sign(v.y), z=0}
|
return {x=self:get_sign(v.x), y=self:get_sign(v.y), z=0}
|
||||||
else
|
else
|
||||||
return {x=0, y=boost_cart:get_sign(v.y), z=boost_cart:get_sign(v.z)}
|
return {x=0, y=self:get_sign(v.y), z=self:get_sign(v.z)}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function boost_cart:is_rail(pos)
|
local get_node = minetest.get_node
|
||||||
local node = minetest.get_node(pos).name
|
local get_item_group = minetest.get_item_group
|
||||||
return minetest.get_item_group(node, "rail") ~= 0
|
function boost_cart:is_rail(pos, railtype)
|
||||||
|
local node = 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 get_item_group(node, "rail") == 0 then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
if not railtype then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
return get_item_group(node, "connect_to_raillike") == railtype
|
||||||
end
|
end
|
||||||
|
|
||||||
function boost_cart:get_rail_direction(pos_, dir_)
|
function boost_cart:check_front_up_down(pos, dir_, check_up, railtype)
|
||||||
local pos = vector.round(pos_)
|
|
||||||
local dir = vector.new(dir_)
|
local dir = vector.new(dir_)
|
||||||
local cur = nil
|
local cur = nil
|
||||||
|
|
||||||
-- Front
|
-- Front
|
||||||
dir.y = 0
|
dir.y = 0
|
||||||
cur = vector.add(pos, dir)
|
cur = vector.add(pos, dir)
|
||||||
if boost_cart:is_rail(cur) then
|
if self:is_rail(cur, railtype) then
|
||||||
return dir
|
return dir
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Down
|
|
||||||
dir.y = -1
|
|
||||||
cur = vector.add(pos, dir)
|
|
||||||
if boost_cart:is_rail(cur) then
|
|
||||||
return dir
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Up
|
-- Up
|
||||||
|
if check_up then
|
||||||
dir.y = 1
|
dir.y = 1
|
||||||
cur = vector.add(pos, dir)
|
cur = vector.add(pos, dir)
|
||||||
if boost_cart:is_rail(cur) then
|
if self:is_rail(cur, railtype) then
|
||||||
return dir
|
return dir
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
-- Down
|
||||||
|
dir.y = -1
|
||||||
|
cur = vector.add(pos, dir)
|
||||||
|
if self:is_rail(cur, railtype) then
|
||||||
|
return dir
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
-- Left, right
|
function boost_cart:get_rail_direction(pos_, dir, ctrl, old_switch, railtype)
|
||||||
dir.y = 0
|
local pos = vector.round(pos_)
|
||||||
|
local cur = nil
|
||||||
|
local left_check, right_check = true, true
|
||||||
|
|
||||||
-- Check left and right
|
-- Check left and right
|
||||||
local view, opposite, val
|
local left = {x=0, y=0, z=0}
|
||||||
|
local right = {x=0, y=0, z=0}
|
||||||
if dir.x == 0 and dir.z ~= 0 then
|
if dir.z ~= 0 and dir.x == 0 then
|
||||||
view = "z"
|
left.x = -dir.z
|
||||||
other = "x"
|
right.x = dir.z
|
||||||
if dir.z < 0 then
|
elseif dir.x ~= 0 and dir.z == 0 then
|
||||||
val = {1, -1}
|
left.z = dir.x
|
||||||
else
|
right.z = -dir.x
|
||||||
val = {-1, 1}
|
|
||||||
end
|
|
||||||
elseif dir.z == 0 and dir.x ~= 0 then
|
|
||||||
view = "x"
|
|
||||||
other = "z"
|
|
||||||
if dir.x > 0 then
|
|
||||||
val = {1, -1}
|
|
||||||
else
|
|
||||||
val = {-1, 1}
|
|
||||||
end
|
|
||||||
else
|
|
||||||
return {x=0, y=0, z=0}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
dir[view] = 0
|
if ctrl then
|
||||||
dir[other] = val[1]
|
if old_switch == 1 then
|
||||||
cur = vector.add(pos, dir)
|
left_check = false
|
||||||
if boost_cart:is_rail(cur) then
|
elseif old_switch == 2 then
|
||||||
return dir
|
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
|
end
|
||||||
|
|
||||||
-- Down
|
-- Normal
|
||||||
dir.y = -1
|
cur = self:check_front_up_down(pos, dir, true, railtype)
|
||||||
cur = vector.add(pos, dir)
|
if cur then
|
||||||
if boost_cart:is_rail(cur) then
|
return cur
|
||||||
return dir
|
|
||||||
end
|
|
||||||
dir.y = 0
|
|
||||||
|
|
||||||
dir[other] = val[2]
|
|
||||||
cur = vector.add(pos, dir)
|
|
||||||
if boost_cart:is_rail(cur) then
|
|
||||||
return dir
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Down
|
-- Left, if not already checked
|
||||||
dir.y = -1
|
if left_check then
|
||||||
cur = vector.add(pos, dir)
|
cur = self:check_front_up_down(pos, left, false, railtype)
|
||||||
if boost_cart:is_rail(cur) then
|
if cur then
|
||||||
return dir
|
return cur
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Right, if not already checked
|
||||||
|
if right_check then
|
||||||
|
cur = self:check_front_up_down(pos, right, false, railtype)
|
||||||
|
if cur then
|
||||||
|
return cur
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Backwards
|
||||||
|
if not old_switch then
|
||||||
|
cur = self:check_front_up_down(pos, {
|
||||||
|
x = -dir.x,
|
||||||
|
y = dir.y,
|
||||||
|
z = -dir.z
|
||||||
|
}, true, railtype)
|
||||||
|
if cur then
|
||||||
|
return cur
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return {x=0, y=0, z=0}
|
return {x=0, y=0, z=0}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function boost_cart: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(boost_cart.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 boost_cart: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
|
||||||
|
|
||||||
|
function boost_cart:register_rail(name, def_overwrite)
|
||||||
|
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},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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
|
||||||
|
|
||||||
|
minetest.register_node(name, def)
|
||||||
|
end
|
||||||
|
|
||||||
|
function boost_cart: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 = 1
|
||||||
|
}
|
||||||
|
if minetest.raillike_group then
|
||||||
|
groups.connect_to_raillike = minetest.raillike_group("rail")
|
||||||
|
end
|
||||||
|
if type(additional_groups) == "table" then
|
||||||
|
for k, v in pairs(additional_groups) do
|
||||||
|
groups[k] = v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return groups
|
||||||
|
end
|
||||||
|
234
init.lua
@ -1,208 +1,46 @@
|
|||||||
-- TODO:
|
|
||||||
-- Fix way-up
|
|
||||||
-- Add rail-cross switching
|
|
||||||
-- Prevent from floating carts
|
|
||||||
-- Speed up and brake rails
|
|
||||||
|
|
||||||
boost_cart = {}
|
boost_cart = {}
|
||||||
boost_cart.modpath = minetest.get_modpath("boost_cart")
|
boost_cart.modpath = minetest.get_modpath("boost_cart")
|
||||||
boost_cart.speed_max = 8
|
|
||||||
|
|
||||||
|
if not minetest.settings then
|
||||||
|
error("[boost_cart] Your Minetest version is no longer supported."
|
||||||
|
.. " (Version <= 0.4.15)")
|
||||||
|
end
|
||||||
|
|
||||||
|
local function getNum(setting)
|
||||||
|
return tonumber(minetest.settings:get(setting))
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Maximal speed of the cart in m/s
|
||||||
|
boost_cart.speed_max = getNum("boost_cart.speed_max") or 10
|
||||||
|
-- Set to -1 to disable punching the cart from inside
|
||||||
|
boost_cart.punch_speed_max = getNum("boost_cart.punch_speed_max") or 7
|
||||||
|
-- Maximal distance for the path correction (for dtime peaks)
|
||||||
|
boost_cart.path_distance_max = 3
|
||||||
|
|
||||||
|
|
||||||
|
-- Support for non-default games
|
||||||
|
if not default.player_attached then
|
||||||
|
default.player_attached = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
minetest.after(1, function()
|
||||||
|
boost_cart.old_player_model = type(player_api) ~= "table"
|
||||||
|
end)
|
||||||
|
|
||||||
dofile(boost_cart.modpath.."/functions.lua")
|
dofile(boost_cart.modpath.."/functions.lua")
|
||||||
dofile(boost_cart.modpath.."/rails.lua")
|
dofile(boost_cart.modpath.."/rails.lua")
|
||||||
|
|
||||||
boost_cart.cart = {
|
if minetest.global_exists("mesecon") then
|
||||||
physical = false,
|
dofile(boost_cart.modpath.."/detector.lua")
|
||||||
collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
|
--else
|
||||||
visual = "mesh",
|
-- minetest.register_alias("carts:powerrail", "boost_cart:detectorrail")
|
||||||
mesh = "cart.x",
|
-- minetest.register_alias("carts:powerrail", "boost_cart:detectorrail_on")
|
||||||
visual_size = {x=1, y=1},
|
|
||||||
textures = {"cart.png"},
|
|
||||||
|
|
||||||
driver = nil,
|
|
||||||
punch = 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
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
clicker:set_attach(self.object, "", {x=0,y=5,z=0}, {x=0,y=0,z=0})
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function boost_cart.cart:on_activate(staticdata, dtime_s)
|
boost_cart.mtg_compat = minetest.global_exists("carts") and carts.pathfinder
|
||||||
self.object:set_armor_groups({immortal=1})
|
if boost_cart.mtg_compat then
|
||||||
self.old_pos = self.object:getpos()
|
minetest.log("action", "[boost_cart] Overwriting definitions of similar carts mod")
|
||||||
end
|
end
|
||||||
|
dofile(boost_cart.modpath.."/cart_entity.lua")
|
||||||
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
|
|
||||||
local player = minetest.get_player_by_name(self.driver)
|
|
||||||
if player then
|
|
||||||
player:set_detach()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
self.object:remove()
|
|
||||||
puncher:get_inventory():add_item("main", "boost_cart:cart")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local vel = self.velocity
|
|
||||||
if puncher:get_player_name() == self.driver then
|
|
||||||
if math.abs(vel.x) > 2 or math.abs(vel.z) > 2 then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local cart_dir = boost_cart:velocity_to_dir(direction)
|
|
||||||
if cart_dir.x == 0 and cart_dir.z == 0 then
|
|
||||||
local fd = minetest.dir_to_facedir(puncher:get_look_dir())
|
|
||||||
if fd == 0 then
|
|
||||||
cart_dir.x = 1
|
|
||||||
elseif fd == 1 then
|
|
||||||
cart_dir.z = -1
|
|
||||||
elseif fd == 2 then
|
|
||||||
cart_dir.x = -1
|
|
||||||
elseif fd == 3 then
|
|
||||||
cart_dir.z = 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if time_from_last_punch > tool_capabilities.full_punch_interval then
|
|
||||||
time_from_last_punch = tool_capabilities.full_punch_interval
|
|
||||||
end
|
|
||||||
local dir = boost_cart:get_rail_direction(self.object:getpos(), cart_dir)
|
|
||||||
|
|
||||||
local f = 3 * (time_from_last_punch / tool_capabilities.full_punch_interval)
|
|
||||||
vel.x = dir.x * f
|
|
||||||
vel.z = dir.y * f
|
|
||||||
vel.z = dir.z * f
|
|
||||||
self.velocity = vel
|
|
||||||
self.punch = true
|
|
||||||
end
|
|
||||||
|
|
||||||
function boost_cart.cart:on_step(dtime)
|
|
||||||
local vel = self.object:getvelocity()
|
|
||||||
if self.punch then
|
|
||||||
vel = vector.add(vel, self.velocity)
|
|
||||||
self.velocity = {x=0,y=0,z=0}
|
|
||||||
elseif vector.equals(vel, {x=0,y=0,z=0}) then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local pos = self.object:getpos()
|
|
||||||
local fpos, fold_pos = vector.round(pos), vector.round(self.old_pos)
|
|
||||||
if vector.equals(fpos, fold_pos) and not self.punch then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
self.old_pos = vector.new(pos)
|
|
||||||
|
|
||||||
local cart_dir = {
|
|
||||||
x = boost_cart:get_sign(vel.x),
|
|
||||||
y = boost_cart:get_sign(vel.y),
|
|
||||||
z = boost_cart:get_sign(vel.z)
|
|
||||||
}
|
|
||||||
local dir = boost_cart:get_rail_direction(pos, cart_dir)
|
|
||||||
if vector.equals(dir, {x=0, y=0, z=0}) then
|
|
||||||
vel = {x=0, y=0, z=0}
|
|
||||||
self.object:setacceleration({x=0, y=0, z=0})
|
|
||||||
self.punch = 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)
|
|
||||||
self.punch = 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)
|
|
||||||
self.punch = true
|
|
||||||
end
|
|
||||||
-- Up, down?
|
|
||||||
if dir.y ~= self.old_dir.y then
|
|
||||||
vel.y = dir.y * (math.abs(vel.x) + math.abs(vel.z))
|
|
||||||
pos.y = math.floor(pos.y + 0.5)
|
|
||||||
self.punch = true
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Slow down or speed up..
|
|
||||||
local acc = -2 * dir.y - 0.4
|
|
||||||
self.object:setacceleration({
|
|
||||||
x = dir.x * acc,
|
|
||||||
y = dir.y * (math.abs(dir.x) + math.abs(dir.z)) * acc,
|
|
||||||
z = dir.z * acc
|
|
||||||
})
|
|
||||||
end
|
|
||||||
|
|
||||||
self.old_dir = vector.new(dir)
|
|
||||||
|
|
||||||
-- Limits
|
|
||||||
for _,v in ipairs({"x","y","z"}) do
|
|
||||||
if math.abs(vel[v]) > boost_cart.speed_max then
|
|
||||||
vel[v] = boost_cart:get_sign(vel[v]) * boost_cart.speed_max
|
|
||||||
self.punch = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
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
|
|
||||||
if self.punch then
|
|
||||||
self.object:setvelocity(vel)
|
|
||||||
self.object:setpos(pos)
|
|
||||||
self.old_pos = {x=0, y=0, z=0}
|
|
||||||
end
|
|
||||||
self.punch = false
|
|
||||||
end
|
|
||||||
|
|
||||||
minetest.register_entity("boost_cart:cart", boost_cart.cart)
|
|
||||||
minetest.register_craftitem("boost_cart: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 boost_cart:is_rail(pointed_thing.under) then
|
|
||||||
minetest.add_entity(pointed_thing.under, "boost_cart:cart")
|
|
||||||
elseif boost_cart:is_rail(pointed_thing.above) then
|
|
||||||
minetest.add_entity(pointed_thing.above, "boost_cart:cart")
|
|
||||||
else return end
|
|
||||||
|
|
||||||
itemstack:take_item()
|
|
||||||
return itemstack
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
|
147
rails.lua
@ -1,16 +1,137 @@
|
|||||||
minetest.register_node(":default:rail", {
|
-- Common rail registrations
|
||||||
|
|
||||||
|
local regular_rail_itemname = "default:rail"
|
||||||
|
if minetest.registered_nodes["carts:rail"] then
|
||||||
|
-- MTG Compatibility
|
||||||
|
regular_rail_itemname = "carts:rail"
|
||||||
|
end
|
||||||
|
|
||||||
|
boost_cart:register_rail(":"..regular_rail_itemname, {
|
||||||
description = "Rail",
|
description = "Rail",
|
||||||
drawtype = "raillike",
|
tiles = {
|
||||||
tiles = {"default_rail.png", "default_rail_curved.png", "default_rail_t_junction.png", "default_rail_crossing.png"},
|
"carts_rail_straight.png", "carts_rail_curved.png",
|
||||||
inventory_image = "default_rail.png",
|
"carts_rail_t_junction.png", "carts_rail_crossing.png"
|
||||||
wield_image = "default_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},
|
groups = boost_cart:get_rail_groups()
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Moreores' copper rail
|
||||||
|
if minetest.get_modpath("moreores") then
|
||||||
|
minetest.register_alias("carts:copperrail", "moreores:copper_rail")
|
||||||
|
else
|
||||||
|
boost_cart:register_rail(":carts:copperrail", {
|
||||||
|
description = "Copper rail",
|
||||||
|
tiles = {
|
||||||
|
"carts_rail_straight_cp.png", "carts_rail_curved_cp.png",
|
||||||
|
"carts_rail_t_junction_cp.png", "carts_rail_crossing_cp.png"
|
||||||
|
},
|
||||||
|
groups = boost_cart:get_rail_groups()
|
||||||
|
})
|
||||||
|
|
||||||
|
minetest.register_craft({
|
||||||
|
output = "carts:copperrail 12",
|
||||||
|
recipe = {
|
||||||
|
{"default:copper_ingot", "", "default:copper_ingot"},
|
||||||
|
{"default:copper_ingot", "group:stick", "default:copper_ingot"},
|
||||||
|
{"default:copper_ingot", "", "default:copper_ingot"},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Power rail
|
||||||
|
boost_cart:register_rail(":carts:powerrail", {
|
||||||
|
description = "Powered rail",
|
||||||
|
tiles = {
|
||||||
|
"carts_rail_straight_pwr.png", "carts_rail_curved_pwr.png",
|
||||||
|
"carts_rail_t_junction_pwr.png", "carts_rail_crossing_pwr.png"
|
||||||
|
},
|
||||||
|
groups = boost_cart:get_rail_groups(),
|
||||||
|
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)
|
||||||
|
boost_cart: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"},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Brake rail
|
||||||
|
boost_cart:register_rail(":carts:brakerail", {
|
||||||
|
description = "Brake rail",
|
||||||
|
tiles = {
|
||||||
|
"carts_rail_straight_brk.png", "carts_rail_curved_brk.png",
|
||||||
|
"carts_rail_t_junction_brk.png", "carts_rail_crossing_brk.png"
|
||||||
|
},
|
||||||
|
groups = boost_cart:get_rail_groups(),
|
||||||
|
after_place_node = function(pos, placer, itemstack)
|
||||||
|
if not mesecon then
|
||||||
|
minetest.get_meta(pos):set_string("cart_acceleration", "-0.3")
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
mesecons = {
|
||||||
|
effector = {
|
||||||
|
action_on = function(pos, node)
|
||||||
|
minetest.get_meta(pos):set_string("cart_acceleration", "-0.3")
|
||||||
|
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"},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
boost_cart:register_rail("boost_cart:startstoprail", {
|
||||||
|
description = "Start-stop rail",
|
||||||
|
tiles = {
|
||||||
|
"carts_rail_straight_ss.png", "carts_rail_curved_ss.png",
|
||||||
|
"carts_rail_t_junction_ss.png", "carts_rail_crossing_ss.png"
|
||||||
|
},
|
||||||
|
groups = boost_cart:get_rail_groups(),
|
||||||
|
after_place_node = function(pos, placer, itemstack)
|
||||||
|
if not mesecon then
|
||||||
|
minetest.get_meta(pos):set_string("cart_acceleration", "halt")
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
mesecons = {
|
||||||
|
effector = {
|
||||||
|
action_on = function(pos, node)
|
||||||
|
boost_cart:boost_rail(pos, 0.5)
|
||||||
|
end,
|
||||||
|
action_off = function(pos, node)
|
||||||
|
minetest.get_meta(pos):set_string("cart_acceleration", "halt")
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
minetest.register_craft({
|
||||||
|
type = "shapeless",
|
||||||
|
output = "boost_cart:startstoprail 2",
|
||||||
|
recipe = {"carts:powerrail", "carts:brakerail"},
|
||||||
})
|
})
|
BIN
screenshot.png
Normal file
After Width: | Height: | Size: 154 KiB |
6
settingtypes.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
# Maximal speed of the cart in m/s (min=1, max=100)
|
||||||
|
boost_cart.speed_max (Maximal speed) int 10 1 100
|
||||||
|
|
||||||
|
# Maximal speed to which the driving player can accelerate the cart by punching
|
||||||
|
# from inside the cart. -1 will disable this feature. (min=-1, max=100)
|
||||||
|
boost_cart.punch_speed_max (Maximal punch speed) int 7 -1 100
|
BIN
sounds/cart_rail.1.ogg
Normal file
BIN
sounds/cart_rail.2.ogg
Normal file
BIN
sounds/cart_rail.3.ogg
Normal file
BIN
textures/carts_rail_crossing.png
Normal file
After Width: | Height: | Size: 755 B |
BIN
textures/carts_rail_crossing_brk.png
Normal file
After Width: | Height: | Size: 765 B |
BIN
textures/carts_rail_crossing_cp.png
Normal file
After Width: | Height: | Size: 735 B |
BIN
textures/carts_rail_crossing_dtc.png
Normal file
After Width: | Height: | Size: 823 B |
BIN
textures/carts_rail_crossing_dtc_on.png
Normal file
After Width: | Height: | Size: 823 B |
BIN
textures/carts_rail_crossing_pwr.png
Normal file
After Width: | Height: | Size: 847 B |
BIN
textures/carts_rail_crossing_ss.png
Normal file
After Width: | Height: | Size: 813 B |
BIN
textures/carts_rail_curved.png
Normal file
After Width: | Height: | Size: 750 B |
BIN
textures/carts_rail_curved_brk.png
Normal file
After Width: | Height: | Size: 738 B |
BIN
textures/carts_rail_curved_cp.png
Normal file
After Width: | Height: | Size: 691 B |
BIN
textures/carts_rail_curved_dtc.png
Normal file
After Width: | Height: | Size: 780 B |
BIN
textures/carts_rail_curved_dtc_on.png
Normal file
After Width: | Height: | Size: 780 B |
BIN
textures/carts_rail_curved_pwr.png
Normal file
After Width: | Height: | Size: 798 B |
BIN
textures/carts_rail_curved_ss.png
Normal file
After Width: | Height: | Size: 832 B |
BIN
textures/carts_rail_straight.png
Normal file
After Width: | Height: | Size: 785 B |
BIN
textures/carts_rail_straight_brk.png
Normal file
After Width: | Height: | Size: 789 B |
BIN
textures/carts_rail_straight_cp.png
Normal file
After Width: | Height: | Size: 728 B |
BIN
textures/carts_rail_straight_dtc.png
Normal file
After Width: | Height: | Size: 830 B |
BIN
textures/carts_rail_straight_dtc_on.png
Normal file
After Width: | Height: | Size: 830 B |
BIN
textures/carts_rail_straight_pwr.png
Normal file
After Width: | Height: | Size: 836 B |
BIN
textures/carts_rail_straight_ss.png
Normal file
After Width: | Height: | Size: 843 B |
BIN
textures/carts_rail_t_junction.png
Normal file
After Width: | Height: | Size: 789 B |
BIN
textures/carts_rail_t_junction_brk.png
Normal file
After Width: | Height: | Size: 714 B |
BIN
textures/carts_rail_t_junction_cp.png
Normal file
After Width: | Height: | Size: 715 B |
BIN
textures/carts_rail_t_junction_dtc.png
Normal file
After Width: | Height: | Size: 752 B |
BIN
textures/carts_rail_t_junction_dtc_on.png
Normal file
After Width: | Height: | Size: 808 B |
BIN
textures/carts_rail_t_junction_pwr.png
Normal file
After Width: | Height: | Size: 761 B |
BIN
textures/carts_rail_t_junction_ss.png
Normal file
After Width: | Height: | Size: 813 B |
BIN
textures/templates/crossing.png
Normal file
After Width: | Height: | Size: 368 B |
BIN
textures/templates/curved.png
Normal file
After Width: | Height: | Size: 400 B |
BIN
textures/templates/straight.png
Normal file
After Width: | Height: | Size: 248 B |
BIN
textures/templates/t_junction.png
Normal file
After Width: | Height: | Size: 323 B |