forked from Sokomine/mapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
138 lines (121 loc) · 4.09 KB
/
init.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
FACTOR = 8;
MAX_FACTOR = 128; -- the bigger, the more graphical glitches you get
MIN_FACTOR = 1;
SIZE_FULL = 2*FACTOR; --32
SIZE_HALF = math.ceil(SIZE_FULL/2)+1;
DRAW_SIZE1 = 4.2/FACTOR; --0.15;
DRAW_SIZE2 = 6.4/FACTOR;
minetest.register_tool("mapp:map", {
description = "map",
inventory_image = "map_block.png",
on_use = function(itemstack, user, pointed_thing)
map_handler(itemstack,user,pointed_thing)
end,
on_place = function(itemstack, placer, pointed_thing)
FACTOR = FACTOR*2;
if( FACTOR >= MAX_FACTOR ) then
FACTOR = MIN_FACTOR;
end
SIZE_FULL = 2*FACTOR; --32
SIZE_HALF = math.ceil(SIZE_FULL/2)+1;
DRAW_SIZE1 = 4.2/FACTOR; --0.15;
DRAW_SIZE2 = 6.4/FACTOR;
minetest.chat_send_player( placer:get_player_name(),
'Info: Map resolution set to '..tostring( SIZE_FULL )..'x'..tostring( SIZE_FULL )..'.');
end,
})
function map_handler (itemstack, user, pointed_thing)
local pos = user:getpos()
local player_name=user:get_player_name()
local mapar = {}
local map
local p
local pp
local po = {x = 0, y = 0, z = 0}
local tile = ""
local yaw
local rotate = 0
pos.y = pos.y + 1
yaw = user:get_look_yaw()
if yaw ~= nil then
-- Find rotation and texture based on yaw.
yaw = math.deg(yaw)
yaw = math.fmod (yaw, 360)
if yaw<0 then yaw = 360 + yaw end
if yaw>360 then yaw = yaw - 360 end
if yaw < 90 then
rotate = 90
elseif yaw < 180 then
rotate = 180
elseif yaw < 270 then
rotate = 270
else
rotate = 0
end
yaw = math.fmod(yaw, 90)
yaw = math.floor(yaw / 10) * 10
end
--Localise some global minetest variables for speed.
local env = minetest.env
local registered_nodes = minetest.registered_nodes
for i = (-1*SIZE_HALF),SIZE_HALF,1 do
mapar[i+SIZE_HALF] = {}
for j = (-1*SIZE_HALF),SIZE_HALF,1 do
mapar[i+SIZE_HALF][j+SIZE_HALF] = {}
po.x, po.y, po.z = pos.x+i, pos.y, pos.z+j
local no = env:get_node(po)
local k=po.y
if no.name == "air" then
while no.name == "air" do
k=k-1
po.x, po.y, po.z = pos.x+i, k, pos.z+j
no = env:get_node(po)
end
elseif no.name ~= "air" and (no.name ~= "ignore") then
while (no.name ~= "air") and (no.name ~= "ignore") do
k=k+1
po.x, po.y, po.z = pos.x+i, k, pos.z+j
no = env:get_node(po)
end
k=k-1
po.x, po.y, po.z = pos.x+i, k, pos.z+j
end
local node = env:get_node(po)
local tiles
local def = registered_nodes[node.name]
if def then tiles = def["tiles"] end
if tiles ~=nil then
tile = tiles[1]
end
if type(tile)=="table" then
tile=tile["name"]
end
mapar[i+SIZE_HALF][j+SIZE_HALF].y = k
mapar[i+SIZE_HALF][j+SIZE_HALF].im = tile
end
end
--Optimisation technique.
--Lua does not edit string buffers via concatenation, using a table and then invoking table.concat is MUCH faster.
p = {}
pp = #p
pp = pp + 1
p[pp] = "size[10.4,10]"
for i=1,SIZE_FULL,1 do
for j=1,SIZE_FULL,1 do
if mapar[i][j].y ~= mapar[i][j+1].y then mapar[i][j].im = mapar[i][j].im .. "^1black_blockt.png" end
if mapar[i][j].y ~= mapar[i][j-1].y then mapar[i][j].im = mapar[i][j].im .. "^1black_blockb.png" end
if mapar[i][j].y ~= mapar[i-1][j].y then mapar[i][j].im = mapar[i][j].im .. "^1black_blockl.png" end
if mapar[i][j].y ~= mapar[i+1][j].y then mapar[i][j].im = mapar[i][j].im .. "^1black_blockr.png" end
pp = pp + 1
p[pp] = "image[".. DRAW_SIZE1*(i) ..",".. DRAW_SIZE1*(SIZE_FULL-j)+0.1 ..";"..DRAW_SIZE2..","..DRAW_SIZE2..";" .. mapar[i][j].im .. "]"
end
end
pp = pp + 1
if rotate ~= 0 then
p[pp] = "image[".. DRAW_SIZE1*(SIZE_HALF-1)+0.075 ..",".. DRAW_SIZE1*(SIZE_HALF-1)-0.085 ..";0.4,0.4;d" .. yaw .. ".png^[transformFYR".. rotate .."]"
else
p[pp] = "image[".. DRAW_SIZE1*(SIZE_HALF-1)+0.075 ..",".. DRAW_SIZE1*(SIZE_HALF-1)-0.085 ..";0.4,0.4;d" .. yaw .. ".png^[transformFY]"
end
map = table.concat(p, "\n")
minetest.show_formspec(player_name, "mapp:map", map)
end