aboutsummaryrefslogtreecommitdiff
path: root/nodes/mesecons_lagger.lua
blob: e4d966ec19d18d99679bd0aa02e5d54ad7c17f15 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

local wait = mesecons_debug.wait

mesecon.queue:add_function("create_lag", function(_pos, duration)
    wait(duration)
end)


minetest.register_node("mesecons_debug:mesecons_lagger", {
    description = "machine for adding artificial mesecons lag",
    group = {
        not_in_creative_inventory = 1,
        unbreakable = 1,
    },
    tiles = {"default_mese_block.png^[colorize:#F00:128"},
    on_blast = function() end,
    drop = "",

    on_construct = function(pos)
        local meta = minetest.get_meta(pos)
        meta:set_float("lag", 0.0)
        meta:set_float("chance", 0.0)
        meta:set_string("formspec",
                ("field[lag;Lag (in us);%s]field[chance;Chance;%s]"):format(0.0, 0.0))

        local timer = minetest.get_node_timer(pos)
        timer:start(0)
    end,

    on_receive_fields = function(pos, _formname, fields, sender)
        if not minetest.check_player_privs(sender, "mesecons_debug") then
            return
        end
        local meta = minetest.get_meta(pos)
        if fields.lag then
            meta:set_float("lag", fields.lag)
        end
        if fields.chance then
            meta:set_float("chance", fields.chance)
        end
        meta:set_string("formspec",
                ("field[lag;Lag (in us);%s]field[chance;Chance;%s]"):format(
                        meta:get_float("lag"), meta:get_float("chance")))

    end,

    on_timer = function(pos, _elapsed)
        local meta = minetest.get_meta(pos)
        local lag = meta:get_float("lag")
        local chance = meta:get_float("chance")
        if lag > 0 and chance > 0 then
            if math.random() < 1 / chance then
                mesecon.queue:add_action(pos, "create_lag", { lag })
            end
        end

        return true
    end,
})