This repository was archived by the owner on Sep 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpage.lua
More file actions
193 lines (142 loc) · 3.62 KB
/
Copy pathpage.lua
File metadata and controls
193 lines (142 loc) · 3.62 KB
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
local lwcomp = ...
local S = lwcomp.S
local width = 40
local height = 30
local hscale = 0.30
local vscale = hscale * 1.5
function lwcomp.page_size ()
return width, height
end
function lwcomp.page_scale ()
return hscale, vscale
end
function lwcomp.convert_page_data (itemstack)
if itemstack then
local meta = itemstack:get_meta ()
if meta and meta:get_int ("version") < 1 then
local contents = meta:get_string ("contents")
local check =
{
["0"] = true,
["1"] = true,
["2"] = true,
["3"] = true,
["4"] = true,
["5"] = true,
["6"] = true,
["7"] = true,
["8"] = true,
["9"] = true,
["A"] = true,
["B"] = true,
["C"] = true,
["D"] = true,
["E"] = true,
["F"] = true,
}
for i = 1, contents:len () do
if not check[contents:sub (i, i)] then
-- max 12 pages
meta:set_string ("contents", lwcomp.to_hex (contents):sub (1, 57600))
meta:set_int ("version", 2)
if meta:get_int ("pages") > 12 then
meta:set_int ("pages", 12)
if meta:get_int ("page") > 12 then
meta:set_int ("page", 12)
end
end
return itemstack
end
end
end
end
return itemstack
end
function lwcomp.page_decode (rawstring)
local chars = width * height
local content = { }
rawstring = lwcomp.from_hex (rawstring)
for i = 1, chars do
if i <= rawstring:len () then
local byte = ((i - 1) * 2) + 1
content[i] =
{
fg = (math.modf ((rawstring:byte (byte) / 16) % 16)),
bg = rawstring:byte (byte) % 16,
char = rawstring:byte (byte + 1)
}
else
content[i] =
{
fg = lwcomp.colors.black,
bg = lwcomp.colors.white,
char = 0
}
end
end
return content
end
function lwcomp.page_encode (page)
local chars = width * height
local raw = ""
local blank = string.char ((lwcomp.colors.black * 16) + lwcomp.colors.white, 0)
if not page then
return string.rep (blank, chars)
end
for i = 1, chars do
if i <= #page then
raw = raw..string.char ((page[i].fg * 16) + page[i].bg, page[i].char)
else
raw = raw..blank
end
end
return lwcomp.to_hex (raw)
end
local function get_page_formspec (raw)
local page = lwcomp.page_decode (raw)
local spec = string.format ("formspec_version[3]\n"..
"size[%f,%f;true]\n"..
"no_prepend[]\n"..
"bgcolor[#DCDCDC]\n"..
"container[0.1,0.1]\n",
(width * hscale) + 0.2, (height * vscale) + 0.2)
for y = 0, height - 1 do
for x = 0, width - 1 do
local c = page[(y * width) + x + 1]
spec = spec..
string.format ("animated_image[%f,%f;%f,%f;d;%02d%02d.png;256;0;%d]\n",
(x * hscale), (y * vscale),
(hscale + 0.03), (vscale + 0.03),
c.fg, c.bg, ((c.char % 256) + 1))
end
end
spec = spec..
"container_end[]\n"
return spec
end
minetest.register_craftitem ("lwcomputers:page", {
description = S("LWComputers Page"),
short_description = S("LWComputers Page"),
inventory_image = "lwcomputers_page.png",
stack_max = 1,
groups = { not_in_creative_inventory = 1 },
on_use = function (itemstack, user, pointed_thing)
if itemstack and user and user:is_player () then
-- convert here
itemstack = lwcomp.convert_page_data (itemstack)
local meta = itemstack:get_meta()
if meta then
minetest.show_formspec (user:get_player_name (),
"lwcomputers:page",
get_page_formspec (meta:get_string ("contents")))
end
end
return itemstack
end,
on_drop = function (itemstack, dropper, pos)
-- convert here
itemstack = lwcomp.convert_page_data (itemstack)
return minetest.item_drop (itemstack, dropper, pos)
end,
})
--