-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tool.lua
415 lines (342 loc) · 12.3 KB
/
tool.lua
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
-- backward compatibility for use_texture_alpha field of node def
local USE_ALPHA_VALUE = minetest.features.use_texture_alpha_string_modes
and 'clip' or true
-- keep track of blocks that are lit up to reduce
-- griefing of players with weak GPUs. Only when
-- using vizlib
local tActiveBlocks = {}
-- register item
minetest.register_craftitem('postool:wand', {
description = 'PosTool',
inventory_image = 'postool_wand.png',
groups = {},
wield_image = '',
wield_scale = { x = 1, y = 1, z = 1 },
liquids_pointable = true,
node_placement_prediction = nil,
-- luacheck: no unused args
on_use = function(oItemstack, oPlayer, oPointedThing)
if not postool.has_vizlib then
return postool.show(oPlayer, oPointedThing)
end
local lShapes, iHash = postool.showVizLib(oPlayer, oPointedThing)
if not lShapes or 0 == #lShapes then return nil end
minetest.after(postool.toolGridDisplayDuration,
function(lShapeIDs, iBlockHash)
tActiveBlocks[iBlockHash] = tActiveBlocks[iBlockHash] - 1
local i = #lShapeIDs
repeat
vizlib.erase_shape(lShapeIDs[i])
i = i - 1
until 0 == i
end, lShapes, iHash
)
return nil
end,
--[[
on_place = function(oItemstack, oPlayer, oPointedThing)
return postool.show(oPlayer, oPointedThing)
end
--]]
-- luacheck: unused args
}) -- register_craftitem
-- determine materials for crafting
local materials = {}
if minetest.get_modpath('xcompat') then
-- let xcompat deal with material strings
materials.glass = xcompat.materials.glass
materials.stick = xcompat.materials.stick
materials.torch = xcompat.materials.torch
else
-- assume default game
materials.glass = 'default:glass'
materials.stick = 'default:stick'
materials.torch = 'default:torch'
end
-- register craft
minetest.register_craft({
output = 'postool:wand 1',
recipe = {
{ '', '', materials.glass },
{ '', materials.torch, '' },
{ materials.stick, '', '' }
}
}) -- register_craft
-- register with unified_inventory, if exists
if minetest.global_exists('unified_inventory') then
unified_inventory.add_category_item('tools', 'postool:wand')
end
-- 'constants' for this session
local tChunkConstants = {
iChunkLength = 16 * postool.serverChunkSize,
iChunkDiff = 16 * (math.ceil(.5 * postool.serverChunkSize)),
dMultiplier = 1 / postool.serverChunkSize,
}
postool.showVizLib = function(oPlayer, oPointedThing)
-- sanity check
if nil == oPointedThing or nil == oPlayer then return nil end
-- increment use count for stats
postool.iCountToolUses = postool.iCountToolUses + 1
-- attempt to get position from pointed thing
local tPos = minetest.get_pointed_thing_position(oPointedThing, false)
-- fallback to player position if that did not work
if nil == tPos then tPos = oPlayer:get_pos() end
local _, tBlock = postool.getPositionTablesForPos(tPos)
local tBlockOrigin = { x = tBlock.x * 16, y = tBlock.y * 16, z = tBlock.z * 16 }
local iHash = minetest.hash_node_position(tBlockOrigin)
-- allow max two highlights per block
if not tActiveBlocks[iHash] then
tActiveBlocks[iHash] = 1
elseif 1 < tActiveBlocks[iHash] then
return nil
else
tActiveBlocks[iHash] = tActiveBlocks[iHash] + 1
end
-- and show it
local lShapes = {}
local tOptions = { infinite = true, color = vizlib.select_color() }
-- full block
table.insert(lShapes, vizlib.draw_cube(
vector.add(tBlockOrigin, 7.5), 8, tOptions))
-- block origin
--[[ disabled for performance and also because the moving particles
don't form a helpful grid
table.insert(lShapes, vizlib.draw_cube(
vector.add(tBlockOrigin, 0), .5, tOptions))
--]]
-- alternate to grids, circles
-- [[
table.insert(lShapes, vizlib.draw_circle(
vector.add(tBlockOrigin, { x = -.5, y = 7.5, z = 7.5 }),
8, 'x', tOptions))
table.insert(lShapes, vizlib.draw_circle(
vector.add(tBlockOrigin, { x = 15.5, y = 7.5, z = 7.5 }),
8, 'x', tOptions))
table.insert(lShapes, vizlib.draw_circle(
vector.add(tBlockOrigin, { x = 7.5, y = -.5, z = 7.5 }),
8, 'y', tOptions))
table.insert(lShapes, vizlib.draw_circle(
vector.add(tBlockOrigin, { x = 7.5, y = 15.5, z = 7.5 }),
8, 'y', tOptions))
table.insert(lShapes, vizlib.draw_circle(
vector.add(tBlockOrigin, { x = 7.5, y = 7.5, z = -.5 }),
8, 'z', tOptions))
table.insert(lShapes, vizlib.draw_circle(
vector.add(tBlockOrigin, { x = 7.5, y = 7.5, z = 15.5 }),
8, 'z', tOptions))--]]
--[[ disabled for performance
table.insert(lShapes, vizlib.draw_circle(
vector.add(tBlockOrigin, { x = -.5, y = 7.5, z = 7.5 }),
4, 'x', tOptions))
table.insert(lShapes, vizlib.draw_circle(
vector.add(tBlockOrigin, { x = 15.5, y = 7.5, z = 7.5 }),
4, 'x', tOptions))
table.insert(lShapes, vizlib.draw_circle(
vector.add(tBlockOrigin, { x = 7.5, y = -.5, z = 7.5 }),
4, 'y', tOptions))
table.insert(lShapes, vizlib.draw_circle(
vector.add(tBlockOrigin, { x = 7.5, y = 15.5, z = 7.5 }),
4, 'y', tOptions))
table.insert(lShapes, vizlib.draw_circle(
vector.add(tBlockOrigin, { x = 7.5, y = 7.5, z = -.5 }),
4, 'z', tOptions))
table.insert(lShapes, vizlib.draw_circle(
vector.add(tBlockOrigin, { x = 7.5, y = 7.5, z = 15.5 }),
4, 'z', tOptions))
--]]
-- CHUNK INDICATOR --
-- respect server wide suppression
if postool.toolSuppressChunkIndicator then return lShapes, iHash end
local bWantsChunk
-- does player want to toggle his chunk indicator setting
local tKeys = oPlayer:get_player_control()
if true == tKeys.zoom and true == tKeys.aux1 and true == tKeys.sneak then
bWantsChunk = postool.toggleChunkMarker(oPlayer)
else
-- read players tool settings
bWantsChunk = postool.playerWantsChunkIndicator(oPlayer)
end
-- only show chunk indicator if player has 'unlocked' it
if not bWantsChunk then return lShapes, iHash end
-- do some math magic to figure out where in the current block
-- to place the chunk indicator. First make some alias to avoid long lines.
local tBO = tBlockOrigin
local dM = tChunkConstants.dMultiplier
local iCD = tChunkConstants.iChunkDiff
local iCL = tChunkConstants.iChunkLength
local tChunkOffset = {
x = math.floor((dM * ((tBO.x - iCD) % iCL)) + 1),
y = math.floor((dM * ((tBO.y - iCD) % iCL)) + 1),
z = math.floor((dM * ((tBO.z - iCD) % iCL)) + 1),
}
-- if player is in centre block of chunk, don't show chunk indicator
-- This helps confirm in a fast manner. Your milleage may vary depending on
-- your mapgen settings. Tested with chunksizes 3, 4 and 5
-- This works fine with 3 and 5 but 4 will not have a centre block.
local bX = 6 <= tChunkOffset.x and 8 >= tChunkOffset.x
local bY = 6 <= tChunkOffset.y and 8 >= tChunkOffset.y
local bZ = 6 <= tChunkOffset.z and 8 >= tChunkOffset.z
if bX and bY and bZ then return nil end
local tChunkOrigin = vector.add(tBlockOrigin, tChunkOffset)
tOptions.color = '#0fff0f'
-- sphere
--[[ disabled for performance
table.insert(lShapes, vizlib.draw_sphere(
vector.add(tChunkOrigin, .5), 1, tOptions))--]]
-- [[
-- cube, uses less particle spawners --> slightly better performance
table.insert(lShapes, vizlib.draw_cube(
vector.add(tChunkOrigin, .5), 1, tOptions))
return lShapes, iHash
end -- showVizLib
postool.show = function(oPlayer, oPointedThing)
-- sanity check
if nil == oPointedThing or nil == oPlayer then return nil end
-- increment use count for stats
postool.iCountToolUses = postool.iCountToolUses + 1
-- attempt to get position from pointed thing
local tPos = minetest.get_pointed_thing_position(oPointedThing, false)
-- fallback to player position if that did not work
if nil == tPos then tPos = oPlayer:get_pos() end
local _, tBlock = postool.getPositionTablesForPos(tPos)
local tBlockOrigin = { x = tBlock.x * 16, y = tBlock.y * 16, z = tBlock.z * 16 }
-- and show it
minetest.add_entity(tBlockOrigin, 'postool:display')
-- CHUNK INDICATOR --
-- respect server wide suppression
if postool.toolSuppressChunkIndicator then return nil end
local bWantsChunk
-- does player want to toggle his chunk indicator setting
local tKeys = oPlayer:get_player_control()
if true == tKeys.zoom and true == tKeys.aux1 and true == tKeys.sneak then
bWantsChunk = postool.toggleChunkMarker(oPlayer)
else
-- read players tool settings
bWantsChunk = postool.playerWantsChunkIndicator(oPlayer)
end
-- only show chunk indicator if player has 'unlocked' it
if not bWantsChunk then return nil end
-- do some math magic to figure out where in the current block
-- to place the chunk indicator. First make some alias to avoid long lines.
local tBO = tBlockOrigin
local dM = tChunkConstants.dMultiplier
local iCD = tChunkConstants.iChunkDiff
local iCL = tChunkConstants.iChunkLength
local tChunkOffset = {
x = math.floor((dM * ((tBO.x - iCD) % iCL)) + 1),
y = math.floor((dM * ((tBO.y - iCD) % iCL)) + 1),
z = math.floor((dM * ((tBO.z - iCD) % iCL)) + 1),
}
-- if player is in centre block of chunk, don't show chunk indicator
-- This helps confirm in a fast manner. Your milleage may vary depending on
-- your mapgen settings. Tested with chunksizes 3, 4 and 5
-- This works fine with 3 and 5 but 4 will not have a centre block.
local bX = 6 <= tChunkOffset.x and 8 >= tChunkOffset.x
local bY = 6 <= tChunkOffset.y and 8 >= tChunkOffset.y
local bZ = 6 <= tChunkOffset.z and 8 >= tChunkOffset.z
if bX and bY and bZ then return nil end
local tChunkOrigin = vector.add(tBlockOrigin, tChunkOffset)
minetest.add_entity(tChunkOrigin, 'postool:display_chunk')
return nil
end -- show
-- display entity shown when postool is used
minetest.register_entity('postool:display', {
initial_properties = {
physical = false,
collisionbox = { 0, 0, 0, 0, 0, 0 },
visual = 'wielditem',
-- wielditem seems to be scaled to 1.5 times original node size
visual_size = { x = 0.67, y = 0.67 },
textures = {'postool:display_node'},
glow = 10,
},
timer = 0,
on_step = function(self, dtime)
self.timer = self.timer + dtime
-- remove after set number of seconds
if self.timer > postool.toolGridDisplayDuration then
self.object:remove()
end
end -- on_step
}) -- register_entity
-- display entity shown when postool is used near a chunk border
minetest.register_entity('postool:display_chunk', {
initial_properties = {
physical = false,
collisionbox = { 0, 0, 0, 0, 0, 0 },
visual = 'wielditem',
-- wielditem seems to be scaled to 1.5 times original node size
visual_size = { x = 0.67, y = 0.67 },
textures = {'postool:display_chunk_node'},
glow = 10,
},
timer = 0,
on_step = function(self, dtime)
self.timer = self.timer + dtime
-- remove after set number of seconds
if self.timer > postool.toolGridDisplayDuration then
self.object:remove()
end
end -- on_step
}) -- register_entity
-- Display-zone node, Do NOT place the display as a node,
-- it is made to be used as an entity (see above)
minetest.register_node('postool:display_node', {
tiles = { 'postool_display.png' },
use_texture_alpha = USE_ALPHA_VALUE,
walkable = false,
drawtype = 'nodebox',
node_box = {
type = 'fixed',
fixed = {
-- we can simply do this, but it only shows when you exit the block
--{ -.55,-.55,-.55, 15.55,15.55,15.55 },
-- west
{ -.55, -.55, -.55, -.55, 15.55, 15.55 },
-- bottom
{ -.55, -.55, -.55, 15.55, -.55, 15.55 },
-- south
{ -.55, -.55, -.55, 15.55, 15.55, -.55 },
-- east
{ 15.55, -.55, -.55, 15.55, 15.55, 15.55 },
-- top
{ -.55, 15.55, -.55, 15.55, 15.55, 15.55 },
-- north
{ -.55, -.55, 15.55, 15.55, 15.55, 15.55 },
-- have not noticed more or less glitchiness with this on and it
-- shows from which side to expect most glitch
-- block origin
{ -.45, -.45, -.45, .55, .55, .55 },
-- while this works, it is glitchy, so disabled
-- centre eight nodes
--{ 6.45, 6.45, 6.45, 8.55, 8.55, 8.55 },
},
},
selection_box = {
type = 'regular',
},
paramtype = 'light',
groups = { dig_immediate = 3, not_in_creative_inventory = 1 },
drop = ''
}) -- register_node postool:display_node
-- Display-chunk node, Do NOT place the display as a node,
-- it is made to be used as an entity (see above)
minetest.register_node('postool:display_chunk_node', {
tiles = { 'postool_display.png' },
use_texture_alpha = USE_ALPHA_VALUE,
walkable = false,
color = '#0fff0f', --#ff0f0f',
drawtype = 'nodebox',
node_box = {
type = 'fixed',
fixed = {
{ -.55,-.55,-.55, 1.55,1.55,1.55 },
},
},
selection_box = {
type = 'regular',
},
paramtype = 'light',
groups = { dig_immediate = 3, not_in_creative_inventory = 1 },
drop = ''
}) -- register_node chunk indicator