wasplib: fix bug
[waspsaliva.git] / builtin / client / wasplib.lua
blob2c7964e06f3d425d1b5af92b945aedd86d19f622
2 ws = {}
3 ws.registered_globalhacks = {}
4 ws.displayed_wps={}
6 ws.lp = minetest.localplayer
7 ws.c = core
9 local nextact = {}
10 local ghwason={}
12 function ws.s(name,value)
13 if value == nil then
14 return ws.c.settings:get(name)
15 else
16 ws.c.settings:set(name,value)
17 return ws.c.settings:get(name)
18 end
19 end
21 function ws.dcm(msg)
22 return minetest.display_chat_message(msg)
23 end
25 function ws.globalhacktemplate(setting,func,funcstart,funcstop)
26 funcstart = funcstart or function() end
27 funcstop = funcstop or function() end
28 return function()
29 if not minetest.localplayer then return end
30 if minetest.settings:get_bool(setting) then
31 if nextact[setting] and nextact[setting] > os.clock() then return end
32 nextact[setting] = os.clock() + 0.1
33 if not ghwason[setting] then
34 funcstart()
35 ws.dcm(setting.. " activated")
36 ghwason[setting] = true
37 else
38 func()
39 end
41 elseif ghwason[setting] then
42 ghwason[setting] = false
43 funcstop()
44 ws.dcm(setting.. " deactivated")
45 end
46 end
47 end
49 function ws.register_globalhack(func)
50 table.insert(ws.registered_globalhacks,func)
51 end
53 function ws.register_globalhacktemplate(name,category,setting,func,funcstart,funcstop)
54 ws.register_globalhack(ws.globalhacktemplate(setting,func,funcstart,funcstop))
55 minetest.register_cheat(name,category,setting)
56 end
58 ws.rg=ws.register_globalhacktemplate
60 function ws.step_globalhacks()
61 for i, v in ipairs(ws.registered_globalhacks) do
62 v()
63 end
64 end
66 minetest.register_globalstep(ws.step_globalhacks)
68 function ws.get_reachable_positions(range)
69 range=range or 2
70 local rt={}
71 for x = -range,range,1 do
72 for y = -range,range,1 do
73 for z = -range,range,1 do
74 table.insert(rt,vector.new(x,y,z))
75 end
76 end
77 end
78 return rt
79 end
81 function ws.do_area(radius,func,plane)
82 for k,v in pairs(ws.get_reachable_positions(range)) do
83 if not plane or v.y == ws.lp:get_pos().y -1 then
84 func(v)
85 end
86 end
87 end
90 function ws.display_wp(pos,name)
91 table.insert(ws.displayed_wps,minetest.localplayer:hud_add({
92 hud_elem_type = 'waypoint',
93 name = name,
94 text = name,
95 number = 0x00ff00,
96 world_pos = pos
97 }))
98 end
100 function ws.clear_wps()
101 for k,v in pairs(ws.displayed_wps) do
102 ws.lp:hud_remove(v)
103 table.remove(ws.displayed_wps,k)
107 function ws.register_chatcommand_alias(old, ...)
108 local def = assert(minetest.registered_chatcommands[old])
109 def.name = nil
110 for i = 1, select('#', ...) do
111 minetest.register_chatcommand(select(i, ...), table.copy(def))
115 function ws.round2(num, numDecimalPlaces)
116 return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
119 function ws.pos_to_string(pos)
120 if type(pos) == 'table' then
121 pos = minetest.pos_to_string(vector.round(pos))
123 if type(pos) == 'string' then
124 return pos
128 function ws.string_to_pos(pos)
129 if type(pos) == 'string' then
130 pos = minetest.string_to_pos(pos)
132 if type(pos) == 'table' then
133 return vector.round(pos)
137 function ws.on_connect(func)
138 if not minetest.localplayer then minetest.after(0,function() ws.on_connect(func) end) return end
139 if func then func() end
142 ws.on_connect(function()
143 ws.lp=minetest.localplayer
144 end)