Difference between revisions of "User:Xyz/stress"

From Minetest Developer Wiki
Jump to navigation Jump to search
Line 8: Line 8:
 
All functions are prefixed with <code>:</code>. All properties are prefixed with <code>.</code>.
 
All functions are prefixed with <code>:</code>. All properties are prefixed with <code>.</code>.
  
<code>return true</code> in event to stop its propagation.
+
What do we want:
 +
* Consistent API
 +
* Good docs
 +
 
 +
* Chaining (<source enclose=none>_("default:dirt"):on("punch", func):on("something", func):on("wtf", func):craft({"default:grass"})</source>)
 +
* Events (<source enclose=none>:on("wtf", function(e) doSomething end)</source>)
 +
* Nice assignments (<source enclose=none>e.stack = "mybrand:newstack 99"</source>) instead of ugly function calls (<source enclose=none>player:set_wielded_wtf_am_i_doing(ItemStack("asd"))</source>)
  
 
== Types ==
 
== Types ==

Revision as of 17:53, 7 August 2013

_()!

http://irc.minetest.ru/minetest/2013-02-17#i_2877771

Experimental implementation: https://github.com/xyzz/minetest-stress

Core concepts

All functions are prefixed with :. All properties are prefixed with ..

What do we want:

  • Consistent API
  • Good docs
  • Chaining (_("default:dirt"):on("punch", func):on("something", func):on("wtf", func):craft({"default:grass"}))
  • Events (:on("wtf", function(e) doSomething end))
  • Nice assignments (e.stack = "mybrand:newstack 99") instead of ugly function calls (player:set_wielded_wtf_am_i_doing(ItemStack("asd")))

Types

All stressed* members should have __type property containing their type as a string.

stressedPosition

Same as position. Can be created from either {x = …, y = …, z = …} or {…, …, …}

Supports -(unary), +, -, *.

Do use it as vector.

stressedPosition(A) === _:p(A)  -- shortcut for convenience
                                -- you won't use it much anyway as we provide stressedPositions everywhere
stressedPosition({1, 2, 3}) + {4, 5, 6}
stressedPosition({0, 0, 0}) - stressedPosition({1, 2, 3})
stressedPosition({1, 2, 3}) * 5  -- {5, 10, 15}
stressedPosition({1, 2, 3}) * {y = 5}  --- {1, 10, 3}
…

stressedArea

_(pos1, pos2), pos1, pos2 are stressedPositions

  • :name("string")
  • :meta("name", "value")
  • :each(function(node))node is stressedNode

stressedNode

Created via _(position)

Implements:

  • :name() — get node name
  • :name("string") — set node name (like minetest.env:set_node())
  • :meta("name") — get meta
  • :meta("name", "value") — set meta
  • :inventory("name") — stressedInventory (name is actually listname)

stressedNodeDef

(needs better name)

Created via _("name"), i.e. _("default:dirt")

  • :on("event", function()) — registers event, possible ones:
    • dig
    • place
    • punch
    • rightclick
  • :every — registers ABM
    • every(time, function)
    • every(time, chance, function)
    • ??

stressedInventory

  • :empty() — returns true if inventory is empty
  • :clear() — remove everything from inventory
  • :size() — get size of an inventory
  • :size(N) — set size of an inventory
  • :width()
  • :width(N)
  • :stack(X) ­— get the Xth stack from inventory, modifiable

stressedStack

stressedStack points to the specific slot in inventory.

  • :name()
  • :name(S)
  • :count()
  • :count(N)
  • :wear()
  • :wear(N)
  • :take() — takes one item
  • :take(N) — takes N items

Global functions

  • _:on("event", function())
    • generated
    • shutdown
  • _:iterate(pos1, pos2)
for pos in _:iterate({0, 0, 0}, {1, 1, 1}) do
    _(pos):name("air")
end
-- same as _({0, 0, 0}, {1, 1, 1}):name("air")

Something

An example of code in stress:

_("default:dirt"):on("place", function(me)
    me:name("default:dirt_with_grass")
end)

same without stress

minetest.register_on_placenode(function(pos, newnode)
    if newnode.name == "default:dirt" then
        minetest.env:set_node(pos, {name="default:dirt_with_grass"})
    end
end)

Something more?

dirttograss = nil
_:on("chat", function(message)
    if message == "/dirttograss" and not dirttograss then
        dirttograss = _("default:dirt"):on("place", function(me)
            me:name("default:dirt_with_grass")
        end)
    elseif message == "/nodirttograss" and dirttograss then
        _(dirttograss):clear()
    end
end)