Compare commits

...

9 Commits

Author SHA1 Message Date
SmallJoker
e4edb96d30 Use initial_properties, backup old_pos for loading 2018-09-01 14:49:16 +02:00
SmallJoker
3386942d71 Add basic documentation 2018-07-30 17:16:14 +02:00
SmallJoker
d486143f42 Reset acceleration at speed limit 2018-07-07 15:23:30 +02:00
SmallJoker
34155473a9 Fix controls not working when going uphill 2018-07-04 22:04:55 +02:00
SmallJoker
7e275e1985 Disallow rail switching up/downhill 2018-07-04 21:23:06 +02:00
SmallJoker
c6b9e4b672 Various improvements and bugfixes
Clear driver on leave using the detach callback (0.5.x and later)
Fix upright_sprite attachment position
Fallback to the regular node sound when metal does not exist
Set animation to "stand" on attach
2018-05-12 11:36:00 +02:00
SmallJoker
3781f6d4c0 Make moreores' copper rail connect again, add metal sounds 2018-05-04 14:38:52 +02:00
SmallJoker
f4c1a2ec3b Fix edge case crash due nil old_pos, remove player_api warning 2018-01-15 18:41:40 +01:00
SmallJoker
57d09fac79 Revert velocity calculation change from e3688b5
Accelerates carts too much when going up/downhill
Caused by wrong vector length calculation. This will need to be altered again if there will be 45° rails
2017-11-28 18:33:37 +01:00
5 changed files with 150 additions and 32 deletions

View File

@ -19,12 +19,14 @@ function boost_cart:on_rail_step(entity, pos, distance)
end end
local cart_entity = { local cart_entity = {
physical = false, initial_properties = {
collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5}, physical = false,
visual = "mesh", collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
mesh = "cart.x", visual = "mesh",
visual_size = {x=1, y=1}, mesh = "cart.x",
textures = {"cart.png"}, visual_size = {x=1, y=1},
textures = {"cart.png"},
},
driver = nil, driver = nil,
punched = false, -- used to re-send velocity and position punched = false, -- used to re-send velocity and position
@ -39,8 +41,8 @@ local cart_entity = {
-- Model and textures -- Model and textures
if boost_cart.mtg_compat then if boost_cart.mtg_compat then
cart_entity.mesh = "carts_cart.b3d" cart_entity.initial_properties.mesh = "carts_cart.b3d"
cart_entity.textures = {"carts_cart.png"} cart_entity.initial_properties.textures = {"carts_cart.png"}
end end
function cart_entity:on_rightclick(clicker) function cart_entity:on_rightclick(clicker)
@ -54,6 +56,12 @@ function cart_entity:on_rightclick(clicker)
elseif not self.driver then elseif not self.driver then
self.driver = player_name self.driver = player_name
boost_cart:manage_attachment(clicker, self.object) boost_cart:manage_attachment(clicker, self.object)
if default.player_set_animation then
-- player_api(/default) does not update the animation
-- when the player is attached, reset to default animation
default.player_set_animation(clicker, "stand")
end
end end
end end
@ -69,18 +77,29 @@ function cart_entity:on_activate(staticdata, dtime_s)
return return
end end
self.railtype = data.railtype self.railtype = data.railtype
if data.old_dir then self.old_dir = data.old_dir or self.old_dir
self.old_dir = data.old_dir self.old_pos = data.old_pos or self.old_pos
-- Correct the position when the cart drives further after the last 'step()'
if self.old_pos and boost_cart:is_rail(self.old_pos, self.railtype) then
self.object:set_pos(self.old_pos)
end end
end end
function cart_entity:get_staticdata() function cart_entity:get_staticdata()
return minetest.serialize({ return minetest.serialize({
railtype = self.railtype, railtype = self.railtype,
old_dir = self.old_dir old_dir = self.old_dir,
old_pos = self.old_pos
}) })
end 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
function cart_entity:on_punch(puncher, time_from_last_punch, tool_capabilities, direction) function cart_entity:on_punch(puncher, time_from_last_punch, tool_capabilities, direction)
local pos = self.object:get_pos() local pos = self.object:get_pos()
local vel = self.object:get_velocity() local vel = self.object:get_velocity()
@ -216,11 +235,12 @@ function cart_entity:on_step(dtime)
) )
local dir_changed = not vector.equals(dir, self.old_dir) local dir_changed = not vector.equals(dir, self.old_dir)
local new_acc = {x=0, y=0, z=0} local acc = 0
if stop_wiggle or vector.equals(dir, {x=0, y=0, z=0}) then if stop_wiggle or vector.equals(dir, {x=0, y=0, z=0}) then
vel = {x=0, y=0, z=0} vel = {x=0, y=0, z=0}
local pos_r = vector.round(pos) local pos_r = vector.round(pos)
if not boost_cart:is_rail(pos_r, self.railtype) then if not boost_cart:is_rail(pos_r, self.railtype)
and self.old_pos then
pos = self.old_pos pos = self.old_pos
elseif not stop_wiggle then elseif not stop_wiggle then
pos = pos_r pos = pos_r
@ -232,7 +252,7 @@ function cart_entity:on_step(dtime)
else else
-- Direction change detected -- Direction change detected
if dir_changed then if dir_changed then
vel = vector.multiply(dir, v3_len(vel)) vel = vector.multiply(dir, math.abs(vel.x + vel.z))
update.vel = true update.vel = true
if dir.y ~= self.old_dir.y then if dir.y ~= self.old_dir.y then
pos = vector.round(pos) pos = vector.round(pos)
@ -250,7 +270,7 @@ function cart_entity:on_step(dtime)
end end
-- Calculate current cart acceleration -- Calculate current cart acceleration
local acc = nil acc = nil
local acc_meta = minetest.get_meta(pos):get_string("cart_acceleration") local acc_meta = minetest.get_meta(pos):get_string("cart_acceleration")
if acc_meta == "halt" and not self.punched then if acc_meta == "halt" and not self.punched then
@ -292,21 +312,20 @@ function cart_entity:on_step(dtime)
else else
acc = 0 acc = 0
end end
new_acc = vector.multiply(dir, acc)
end end
-- Limits -- Limit cart speed
local max_vel = boost_cart.speed_max local vel_len = vector.length(vel)
for _,v in pairs({"x","y","z"}) do if vel_len > boost_cart.speed_max then
if math.abs(vel[v]) > max_vel then vel = vector.multiply(vel, boost_cart.speed_max / vel_len)
vel[v] = boost_cart:get_sign(vel[v]) * max_vel update.vel = true
new_acc[v] = 0 end
update.vel = true if vel_len >= boost_cart.speed_max and acc > 0 then
end acc = 0
end end
self.object:set_acceleration(new_acc) self.object:set_acceleration(vector.multiply(dir, acc))
self.old_pos = vector.round(pos) self.old_pos = vector.round(pos)
local old_y_dir = self.old_dir.y local old_y_dir = self.old_dir.y
if not vector.equals(dir, {x=0, y=0, z=0}) and not stop_wiggle then if not vector.equals(dir, {x=0, y=0, z=0}) and not stop_wiggle then

71
doc/mod_api.txt Normal file
View File

@ -0,0 +1,71 @@
boost_cart API
==============
This file provides information about the API of boost_cart for the use in
mods. The API might change slightly when the development goes on, so avoid
using internal tables or functions which are not documented here.
Types
-----
* `SwitchIgnore` -> `number/nil`
* Specifies which player control was pressed. This value is used to prefer
straight rails instead of preferring left and right rail checks.
* `1`: Ignore left rail
* `2`: Ignore right rail
* `nil`: Ignore no rail
Entity movement
---------------
These functions are grouped so that they make sense and then sorted alphabetically.
* `boost_cart:manage_attachment(player, obj)`
* Attaches or detaches the player to/from an object, depending on what is
supplied to `obj`.
* `player`: `ObjectRef` of the player
* `obj`: `ObjectRef` (to attach) or `nil` (to detach)
* `boost_cart:get_sign(n)` -> `number`
* Returns the sign for the given number. Values: `-1`, `0`, `1`
* `n`: any `number`
* `boost_cart:velocity_to_dir(vel)` -> `vector`
* Returns the cart direction depending on `vel`. Each coordinate can have
one of the `get_sign()` return values.
* `vel`: velocity as `vector`
* `boost_cart:boost_rail(pos, amount)`
* Sets the rail acceleration for the given position to `amount` and punches
carts which are at the given position.
* `pos`: `vector`, rail position
* `amount`: `number`, negative to brake, positive to boost
* `boost_cart:get_rail_direction(pos, dir, ctrl, old_switch, railtype)`
-> `vector, SwitchIgnore`
* Returns the direction to where the next rail is, and which player control that
should be ignored in the next call.
* `pos`: `vector`, position of the cart
* `dir`: `vector`, movement direction of the cart (see `velocity_to_dir()`)
* `ctrl`: Player controls table or `nil` (no player)
* `old_switch`: `SwitchIgnore`
* `railtype`: (optional) `number`, gets passed indirectly to `is_rail()`
Rail helper functions
---------------------
* `boost_cart:get_rail_groups(groups)` -> `table`
* Returns a group table with preset values for a common rail node
* `groups`: (optional) `table`, additional groups append (or overwrite)
* Hint: To register an incompatible rail type, set the group
`connect_to_raillike` to the value returned by
`minetest.raillike_group(new_rail_type)`
* `boost_cart:is_rail(pos, [railtype])` -> `boolean`
* Returns whether the node at `pos` is a rail. When `railtype` is specified,
`true` is only returned when the node is in the same rail group.
* `pos`: `vector` of the node to check
* `railtype`: (optional) `number`, rail group number
* `boost_cart:register_rail(name, def)`
* Registers a new rail with preset node definition defaults as fallback
* `name`: `string`, node name of the new rail
* `def`: Node definition table, containing at least the following keys:
* `description`
* `groups`
* `tiles`

View File

@ -19,6 +19,9 @@ function boost_cart:manage_attachment(player, obj)
if status then if status then
local y_pos = self.old_player_model and 6 or -4 local y_pos = self.old_player_model and 6 or -4
if player:get_properties().visual == "upright_sprite" then
y_pos = -4
end
player:set_attach(obj, "", {x=0, y=y_pos, z=0}, {x=0, y=0, z=0}) 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}) player:set_eye_offset({x=0, y=-4, z=0},{x=0, y=-4, z=0})
else else
@ -104,6 +107,16 @@ function boost_cart:get_rail_direction(pos_, dir, ctrl, old_switch, railtype)
right.z = -dir.x right.z = -dir.x
end 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 ctrl then
if old_switch == 1 then if old_switch == 1 then
left_check = false left_check = false
@ -127,9 +140,11 @@ function boost_cart:get_rail_direction(pos_, dir, ctrl, old_switch, railtype)
end end
-- Normal -- Normal
cur = self:check_front_up_down(pos, dir, true, railtype) if not straight_priority then
if cur then cur = self:check_front_up_down(pos, dir, true, railtype)
return cur if cur then
return cur
end
end end
-- Left, if not already checked -- Left, if not already checked
@ -208,6 +223,9 @@ function boost_cart:boost_rail(pos, amount)
end end
function boost_cart:register_rail(name, def_overwrite) function boost_cart:register_rail(name, def_overwrite)
local sound_func = default.node_sound_metal_defaults
or default.node_sound_defaults
local def = { local def = {
drawtype = "raillike", drawtype = "raillike",
paramtype = "light", paramtype = "light",
@ -217,7 +235,8 @@ function boost_cart:register_rail(name, def_overwrite)
selection_box = { selection_box = {
type = "fixed", type = "fixed",
fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2}, fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
} },
sounds = sound_func()
} }
for k, v in pairs(def_overwrite) do for k, v in pairs(def_overwrite) do
def[k] = v def[k] = v

View File

@ -25,8 +25,8 @@ if not default.player_attached then
default.player_attached = {} default.player_attached = {}
end end
minetest.after(1, function() minetest.after(0, function()
boost_cart.old_player_model = type(player_api) ~= "table" boost_cart.old_player_model = not minetest.global_exists("player_api")
end) end)
dofile(boost_cart.modpath.."/functions.lua") dofile(boost_cart.modpath.."/functions.lua")

View File

@ -18,6 +18,15 @@ boost_cart:register_rail(":"..regular_rail_itemname, {
-- Moreores' copper rail -- Moreores' copper rail
if minetest.get_modpath("moreores") then if minetest.get_modpath("moreores") then
minetest.register_alias("carts:copperrail", "moreores:copper_rail") minetest.register_alias("carts:copperrail", "moreores:copper_rail")
if minetest.raillike_group then
-- Ensure that this rail uses the same connect_to_raillike
local new_groups = minetest.registered_nodes["moreores:copper_rail"].groups
new_groups.connect_to_raillike = minetest.raillike_group("rail")
minetest.override_item("moreores:copper_rail", {
groups = new_groups
})
end
else else
boost_cart:register_rail(":carts:copperrail", { boost_cart:register_rail(":carts:copperrail", {
description = "Copper rail", description = "Copper rail",