From 514dd3cd3a6281d44f686eda586e5bc04d895faa Mon Sep 17 00:00:00 2001 From: mckaygerhard Date: Wed, 10 Apr 2024 02:13:10 +0000 Subject: [PATCH] backguard copatibility for vector helpers - vector.offset --- en/backporting/minetest-engine-api.md | 29 +++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/en/backporting/minetest-engine-api.md b/en/backporting/minetest-engine-api.md index b53066c..7fa1cf4 100644 --- a/en/backporting/minetest-engine-api.md +++ b/en/backporting/minetest-engine-api.md @@ -1,4 +1,33 @@ +### vector helpers + +#### vector.offset + +https://codeberg.org/minenux/minetest-mod-mobs_redo/issues/108 + +In particular, offsets are usually required for defining an area to pass to minetest.find_nodes_in_area() as well as the VoxelArea constructor. Likewise, an offset of y + 1 is frequently needed with functions like minetest.get_node() and minetest.get_node_light(). + +Here's are some concrete examples from minetest-mods where these functions would prove useful: + +https://github.com/minetest-mods/loot/blob/e7398c3ce1953c443406392efd4fc1bd4d22e771/loot_vault.lua#L20 + +https://github.com/minetest-mods/mesecons/blob/7013f2e5d47ff2d21fb66fe82966a34ece947eec/mesecons_random/init.lua#L14 + +https://github.com/minetest-mods/painting/blob/8961849797fa21f168191b10b3693dd5eeab08ea/init.lua#L206 + +https://github.com/minetest-mods/schemlib_builder_npcf/blob/85cfb2d773a8d18ebd921f18f6bb7565664314e1/init.lua#L239 + +https://github.com/minetest-mods/mob-engine/blob/c2bedc3816a0c6a3a96b5803bd910b32c09c4f2c/creatures/functions.lua#L34 + +https://github.com/minetest-mods/mapgen_helper/blob/9c7347691d451f6eee329cd8f7e23ccbd27bd298/place_schematic.lua#L136 + +Note that vector.offset_y() isn't the direct analog of vector.offset(), because it defaults to 1. Checking the position directly above another position is a very common use-case In fact, nine out of ten situations where I need an offset position, it is y + 1. + +vector.offset_y(pos) +vector.offset(pos, 0, 1, 0) +Keep in mind, the goal of these functions isn't to save keystrokes, it's to make code clearer to understand and easier maintain. By using vector.offset_y(), it is apparent at first glance what the intention is, thereby mitigating the likelihood of introducing bugs later, as could happen when using just vector.offset() where it is not as immediately obvious that x and z should never change. + +Likewise, one could manually clamp a number by pairing math.min() with math.max(), but that is a far cry from the simplicity and clarity of math.clamp(). ### reason or PlayerHPChangeReason