Page 1 of 1

abm / cactus / damage

Posted: Sun Aug 26, 2018 17:27
by bosapara
abm for cactus damage isnt too much expensive for minetest perfomance?

Image

Code: Select all

minetest.register_abm({
   nodenames = {"default:cactus"},
   interval = 0.5,
   chance = 1,
   action = function(pos, node, active_object_count, active_object_count_wider)
      local players = minetest.get_objects_inside_radius(pos, 1)
         for i, player in ipairs(players) do
            player:set_hp(player:get_hp() - 3)
         end
   end,
})

Re: abm / cactus / damage

Posted: Sun Aug 26, 2018 20:34
by Casimir
This is how I did it in Voxelgarden. Essentially it's just a smaller collision box, so you can stand "inside" the node, but with an smaller node box so it doesn't look strange.

Code: Select all

minetest.register_node("default:cactus", {
	description = "Cactus",
	drawtype = "nodebox",
	paramtype = "light",
	tiles = {"default_cactus_top.png", "default_cactus_top.png", "default_cactus_side.png"},
	groups = {snappy=1, choppy=3, flammable=2},
	node_box = {
		type = "fixed",
		fixed = {
			{-0.375, -0.5, -0.5, -0.375, 0.5, 0.5},
			{0.375, -0.5, -0.5, 0.375, 0.5, 0.5},
			{-0.5, -0.5, 0.375, 0.5, 0.5, 0.375},
			{-0.5, -0.5, -0.375, 0.5, 0.5, -0.375},
			{-0.375, -0.5, -0.375, 0.375, 0.5, 0.375},
		}
	},
	selection_box = {
		type = "fixed",
		fixed = {-0.375, -0.5, -0.375, 0.375, 0.5, 0.375}
	},
	collision_box = {
		type = "fixed",
		fixed = {
			{-0.1875, -0.5, -0.1875, 0.1875, 0.5, 0.1875},
		}
	},
	damage_per_second = 1,
	sounds = default.node_sound_wood_defaults(),
	after_dig_node = function(pos, node, metadata, digger)
		default.dig_up(pos, node, digger)
		default.dig_up(pos, {name = "default:cactus_fig"}, digger)
	end,
})
Image

Re: abm / cactus / damage

Posted: Sun Aug 26, 2018 20:45
by bosapara
Casimir wrote:This is how I did it in Voxelgarden. Essentially it's just a smaller collision box, so you can stand "inside" the node, but with an smaller node box so it doesn't look strange.
Already checked this variant, unfortunately damage will not work when we walk at the cactus

Re: abm / cactus / damage

Posted: Sun Aug 26, 2018 21:03
by Krock
bosapara wrote:Already checked this variant, unfortunately damage will not work when we walk at on the cactus
Then try manipulating the Y value of the collision box a bit like this: (untested)

Code: Select all

	collision_box = {
		type = "fixed",
		fixed = {
			{-0.1875, -0.5, -0.1875, 0.1875, 0.45, 0.1875},
		}
	},

Re: abm / cactus / damage

Posted: Sun Aug 26, 2018 22:03
by bosapara
Krock wrote: Then try manipulating the Y value of the collision box a bit like this: (untested)
yep, almost, this works perfect:

Code: Select all

	collision_box = {
      type = "fixed",
      fixed = {
         {-0.1875, -0.5, -0.1875, 0.1875, 0.375, 0.1875},
    },

Re: abm / cactus / damage

Posted: Thu Aug 30, 2018 09:50
by Hybrid Dog
Sam shouldn't be damaged when standing on a cactus because he wears shoes.

Re: abm / cactus / damage

Posted: Thu Aug 30, 2018 11:15
by Hamlet
Technical question:

Given that the same can be achieved with node timers, which would be the best method?

Or, which is the best one for memory saving and server load?

Re: abm / cactus / damage

Posted: Thu Aug 30, 2018 17:20
by sorcerykid
This is the collision box setting I use for default:cactus on JT2, derived from rnd's "cactus hurt" patch.

Code: Select all

        collision_box = {
                type = "fixed",
                -- Adjust collision box so it damages players that are too
                -- close. We need to allow them to "enter" into the node a
                -- little bit for this to actually happen. The top is low
                -- enough that feet damage occurs if standing on it.
                fixed = { -7/16, -1/2, -7/16, 7/16, 5/16, 7/16 },
        },

Re: abm / cactus / damage

Posted: Thu Aug 30, 2018 18:42
by Krock
@Hamlet, @bosapara: Some additions in regard to the best performance, as there are currently four possibilities to do this:

1) ABMs
Pro: Is triggered for each cactus node, can damage players using `minetest.get_objects_inside_radius(pos, 1)`
Contra: Somewhat slow and triggers on irrelevant nodes. Don't use it.
2) Node Timers
Pro: Is triggered only for the top cactus node (requires optimizations in the mod)
Contra: Node Timers are triggered even when there's no player + requires optimizations to reduce the active node timers
3) register globalstep
Pro: Only checks positions where the players are
Contra: Also checks positions where no cactus is found nearby
4) Engine/built-in
Pro: Only checks positions where the players are + no Lua callbacks to execute
Contra: Also checks positions where no cactus is found nearby + the feet are inside the visible cactus (looks weird)

Idea (3) and (4) have generally a better efficiency because there are more cactus nodes than players in the loaded mapblocks. However, if there are many players in a world where cactus is (near to) non-existent, then it's idea (2) which is the next best solution when you want to avoid the visual side-effect of (4).

Re: abm / cactus / damage

Posted: Fri Aug 31, 2018 05:54
by TenPlus1
My PlayerPlus mod handles everything inside a single globalstep so that it's less strain on a server and will do cactus damage, suffocation, player knockback, walking speed changes, on_walk_over nodes and has player_monoids and pova support for the changes...

https://notabug.org/tenplus1/playerplus

Re: abm / cactus / damage

Posted: Fri Aug 31, 2018 15:46
by Astrobe
Krock wrote:@Hamlet, @bosapara: Some additions in regard to the best performance, as there are currently four possibilities to do this:

1) ABMs
2) Node Timers
3) register globalstep
4) Engine/built-in
5) "on_collide" callback for players and entities
Pros: it solves all sorts of other problems
Cons: backport to version 0.0.1 needed because it should have been there right from the beginning.

Re: abm / cactus / damage

Posted: Fri Aug 31, 2018 16:15
by Krock
Astrobe wrote:5) "on_collide" callback for players and entities
I think you forgot the part with "Lua function call performance" and "redundant callback call flood". It's not like this was requested before, or anything like that. (sarcasm)

Letting the engine handle this internally by the given node definition is actually the best approach performance-wise in this case.

Re: abm / cactus / damage

Posted: Fri Aug 31, 2018 18:35
by Astrobe
"redundant callback call flood": just have the user to rearm the callback once fired (like we do with after()), so that mods can deal with the problem with a timer if needed. Works in this case, it works with arrows, it works with mobs.

"Lua function call performance": compared to either the broken collision detection Lua code I have seen (with get_object_in_radius) or the correct but expensive collision box checks, it doesn't look that bad to me.

Offloading the burden to Lua scripts just hurts everyone more. But yeah, keep the issue closed and keep adding ad hoc solutions instead.

Re: abm / cactus / damage

Posted: Fri Aug 31, 2018 18:36
by Hybrid Dog
With server-provided CSM an on_collide function could be executed client-side.

Re: abm / cactus / damage

Posted: Sun Sep 02, 2018 07:47
by Astrobe
I don't want to be look like the stubborn guy who takes down any other solution than his, but I have two issues with CSM.

First, I don't like the idea of running random code from a random source. Browsers do this all the time. It lead to abuse, so now there are browser extensions to block JS.

More importantly, the second problem is that you don't know the client machine specs (could be a gaming PC, could be a low-end tablet), so you don't know how much you can offload to them.

Suppose we do the on_collide stuff via CSM. We assume that it's no big deal for the client machine. Now add Paramat's Snowdrift that adds some particle handling stuff to the workload of the client. Is it still ok? Then add a couple of other mods that delegate other awesome stuff to the client and when your players say the game is "laggy", you will have no idea if it's a network problem or if that was because your players had a cactus bumping party under heavy rain.

Re: abm / cactus / damage

Posted: Sun Sep 02, 2018 08:27
by Astrobe
For the collision box solution above, since that hack makes the character "enter" the node, you might want to disable backface culling; for instance:

Code: Select all

	tiles = {"default_cactus_top.png", "default_cactus_top.png", {name="default_cactus_side.png", backface_culling=false}},

Re: abm / cactus / damage

Posted: Mon Sep 03, 2018 11:31
by Hamlet
Krock wrote: 1) ABMs
[...]
2) Node Timers
[...]
3) register globalstep
[...]
4) Engine/built-in
[...]

Idea (3) and (4) have generally a better efficiency because there are more cactus nodes than players in the loaded mapblocks. However, if there are many players in a world where cactus is (near to) non-existent, then it's idea (2) which is the next best solution when you want to avoid the visual side-effect of (4).
Thanks, your answer has cleared my doubts; when I will rewrite "Killer Nodes" I will use the fourth method - I thought about using node timers instead of ABMs, but running timers when there aren't players around isn't an option.

I don't mind about the visual glitch: after all we already have mobs that often end with their heads buried into nodes due to how collision boxes work.

Thanks again. :)