-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchisel.lua
428 lines (354 loc) · 11.8 KB
/
chisel.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
local USES = 200
local color = '#FF6700'
local TechnicMaxCharge = 300000
chisel = {}
chisel.materials = {}
chisel.mods = {}
chisel.selected = {}
chisel.active = {}
chisel.program = {}
chisel.mode = {}
local default_material = {
{"default:cobble", "default_cobble", "Cobble"},
{"default:sandstone","default_sandstone", "Sandstone"},
{"default:clay","default_clay", "Clay"},
{"default:coalblock","default_coal_block", "Coal Block"},
{"default:stone","default_stone", "Stone"},
{"default:desert_stone","default_desert_stone", "Desert Stone"},
{"default:wood","default_wood", "Wood"},
{"default:acacia_wood","default_acacia_wood", "Acacia Wood"},
{"default:aspen_wood","default_aspen_wood", "Aspen Wood"},
{"default:pine_wood","default_pine_wood", "Pine Wood"},
{"default:desert_cobble","default_desert_cobble", "Desert Cobble"},
{"default:junglewood","default_junglewood", "Jungle Wood"},
{"default:sandstonebrick","default_sandstone_brick", "Sandstone Brick"},
{"default:stonebrick","default_stone_brick", "Stone Brick"},
{"default:desert_stonebrick","default_desert_stone_brick", "Desert Stone Brick"},
{"default:steelblock", "default_steel_block", "Steel Block"},
{"default:copperblock", "default_copper_block", "Copper Block"},
{"default:bronzeblock", "default_bronze_block", "Bronze Block"},
{"default:goldblock", "default_gold_block", "Gold Block"},
{"default:tinblock", "default_tin_block", "Tin Block"},
{"moreblocks:copperpatina", "moreblocks_copperpatina", "Copperpatina"},
{"default:desert_sandstone","default_desert_sandstone", "Desert Sandstone"},
{"default:desert_sandstone_brick","default_desert_sandstone_brick", "Desert Sandstonebrick"},
{"default:silver_sandstone","default_silver_sandstone", "Silver Sandstone"},
{"default:silver_sandstone_brick","default_silver_sandstone_brick", "Silver Sandstonebrick"},
}
if minetest.get_modpath( "bakedclay") then
local clay = {
{"white", "White"},
{"grey", "Grey"},
{"black", "Black"},
{"red", "Red"},
{"yellow", "Yellow"},
{"green", "Green"},
{"cyan", "Cyan"},
{"blue", "Blue"},
{"magenta", "Magenta"},
{"orange", "Orange"},
{"violet", "Violet"},
{"brown", "Brown"},
{"pink", "Pink"},
{"dark_grey", "Dark Grey"},
{"dark_green", "Dark Green"},
{"natural", "Natural"}
}
for _, clay in pairs(clay) do
table.insert(default_material,{"bakedclay:"..clay[1] , "baked_clay_" .. clay[1], clay[2] .. " Baked Clay"})
end
end
-- Chatcommand to show loaded mods with names and number of styles and supported materials
minetest.register_chatcommand("chisel", {
params = "",
description = "Shows supported mods and materials in mychisel",
privs = {interact = true},
func = function(name, poi_name)
for i in ipairs (chisel.mods) do
local counter = 1
local rawname = ""
minetest.chat_send_player(name,core.colorize(color,i..") modname: "..chisel.mods[i][1].." styles: "..chisel.mods[i][2]))
if chisel.mods[i][1] == "default" then
for j in ipairs (default_material) do
minetest.chat_send_player(name, " "..j..": "..default_material[j][1])
end
else
for j in ipairs (chisel.materials) do
if chisel.materials[j][3] ~= rawname then
minetest.chat_send_player(name, " "..counter..": "..chisel.materials[j][3])
rawname = chisel.materials[j][3]
counter = counter +1
end
end
end
end
end,
})
-- global API
function chisel.register_node(modname, prefix, raw, design) -- global function to register new stuff
local counter = chisel.count_stuff() +1
chisel.materials [counter] = {}
chisel.materials [counter][1] = modname
chisel.materials [counter][2] = prefix
chisel.materials [counter][3] = raw
chisel.materials [counter][4] = design
end
function chisel.add_mod(modname,number) -- global function to init a new mod for the chisel.
local counter = chisel.count_mods() +1
chisel.mods [counter] = {}
chisel.mods [counter][1] = modname
chisel.mods [counter][2] = number
minetest.log("action","[CHISEL] Added mod "..modname .. " with " .. number.." styles to mychisel") -- log loaded mods to debug.txt
end
function chisel.count_stuff() -- how many materials have been registered already ?
local counter = 0
for i in ipairs (chisel.materials) do counter = counter +1 end
return counter
end
function chisel.count_mods() -- how many different mods are registered ?
local counter = 0
for i in ipairs (chisel.mods) do counter = counter +1 end
return counter
end
-- init chisel for each player joining
local function chiselinit(name)
chisel.selected[name] = 1
chisel.active[name] = "default"
chisel.program[name] = 1
chisel.mode[name] = "1"
end
minetest.register_on_joinplayer(function(player)
chiselinit(player:get_player_name())
end)
-- end init
local function parti(pos)
minetest.add_particlespawner({
amount = 25,
time = 0.3,
minpos = pos,
maxpos = pos,
minvel = {x=2, y=0.2, z=2},
maxvel = {x=-2, y=2, z=-2},
minacc = {x=0, y=-6, z=0},
maxacc = {x=0, y=-10, z=0},
minexptime = 0.2,
maxexptime = 1,
minsize = 0.2,
maxsize = 2,
collisiondetection = true,
texture = "mychisel_parti.png"
})
end
local function chiselcut(pos,user,node)
local name = user:get_player_name()
for i in ipairs (chisel.materials) do
local material = chisel.materials[i]
if material[1] == chisel.active[name] then
if node.name == material[3] and material[4] == chisel.materials[chisel.program[name]][4] then
local stack = ItemStack(material[1]..":"..material[2].."_"..material[4])
if not stack:is_known() then
return false
end
minetest.set_node(pos, {
name = stack:get_name(),
param2 = minetest.dir_to_facedir(user:get_look_dir())
})
return true
end
end
end
end
local chisel_modes = {
["1"] = {desc = "Chisel 4 Edges", cut = "mychisel:chiseled_%s"},
["2"] = {desc = "Horizontal Groove", cut = "mychisel:horizontal_%s"},
["3"] = {desc = "Vertical Groove", cut = "mychisel:vertical_%s"},
["4"] = {desc = "Cross Grooves", cut = "mychisel:cross_%s"},
["5"] = {desc = "Square", cut = "mychisel:square_%s"}
}
local function change_mode(user, choice)
-- choice = true: choose next program
-- choice = false: choose next mode
local name = user:get_player_name()
if not choice then
local player_program = chisel.program[name]
player_program = player_program + 1
if player_program > chisel.mods[chisel.selected[name]][2] then
player_program = 1
end
chisel.program[name] = player_program
minetest.chat_send_player(name, core.colorize(color, chisel.materials[player_program][4]))
return
end
local player_mode = tostring((tonumber(chisel.mode[name]) or 0) + 1)
if not chisel_modes[player_mode] then
player_mode = "1"
end
chisel.mode[name] = player_mode
minetest.chat_send_player(name, core.colorize(color, chisel_modes[player_mode].desc))
end
local function chiselme(pos, user, node)
local name = user:get_player_name()
for i in ipairs (default_material) do
local item = default_material[i][1]
local mat = default_material[i][2]
--local desc = default_material[i][3]
local cmode = chisel_modes[chisel.mode[name]]
local newnode
if cmode then
if node.name == item then
newnode = cmode.cut:format(mat .. "1")
elseif node.name == cmode.cut:format(mat .. "1") then
newnode = cmode.cut:format(mat .. "2")
elseif node.name == cmode.cut:format(mat .. "2") then
newnode = cmode.cut:format(mat .. "3")
elseif node.name == cmode.cut:format(mat .. "3") then
newnode = cmode.cut:format(mat .. "4")
end
end
if newnode then
minetest.swap_node(pos, {
name = newnode,
param2 = minetest.dir_to_facedir(user:get_look_dir())
})
parti(pos)
end
end
end
local chisel_def = {
description = "Chisel",
inventory_image = "mychisel_chisel.png",
wield_image = "mychisel_chisel.png",
-- on_use is specific
on_place = function(itemstack, user, pointed_thing)
local number = chisel.count_mods()
local keys = user:get_player_control()
local name = user:get_player_name()
local node = minetest.get_node(pointed_thing.under)
-- chisel can be repaired with an anvil
if node.name == "anvil:anvil" then
minetest.item_place(itemstack, user, pointed_thing)
return itemstack
end
-- change design mode of chisel by pressing sneak while right-clicking
if not keys.sneak then
if chisel.active[name] == "default" then
change_mode(user,true)
else
change_mode(user,false)
end
else
chisel.selected[name] = chisel.selected[name] + 1
if chisel.selected[name] > chisel.count_mods() then
chisel.selected[name] = 1
end
chisel.active[name] = chisel.mods[chisel.selected[name]][1]
minetest.chat_send_player(
user:get_player_name(),
core.colorize(color, " ***>>> switched to mod: "..chisel.active[name])
)
end
return itemstack
end
}
if not minetest.get_modpath("technic") then
chisel_def.on_use = function(itemstack, user, pointed_thing)
if pointed_thing.type ~= "node" then
return
end
local pos = pointed_thing.under
local node = minetest.get_node(pos)
local name = user:get_player_name()
local cut = false
if minetest.is_protected(pos, user:get_player_name()) then
minetest.record_protection_violation(pos, user:get_player_name())
return
end
if chisel.active[name] == "default" then
chiselme(pos,user,node)
else
cut = chiselcut(pos,user,node)
end
if not minetest.settings:get_bool("creative_mode") and cut then
itemstack:add_wear(65535 / (USES - 1))
end
return itemstack
end
minetest.register_tool("mychisel:chisel", chisel_def)
minetest.register_craft({
output = "mychisel:chisel",
recipe = {
{"default:steel_ingot"},
{"wool:brown"},
},
})
else -- technic is installed
local S = technic.getter
local chisel_charge_per_node = math.floor(TechnicMaxCharge / USES)
chisel_def.description = S("Chisel")
if technic.plus then
chisel_def.max_charge = TechnicMaxCharge
chisel_def.on_use = function(itemstack, user, pointed_thing)
if pointed_thing.type ~= "node" then
return
end
local pos = pointed_thing.under
local node = minetest.get_node(pos)
local name = user:get_player_name()
if minetest.is_protected(pos, name) then
minetest.record_protection_violation(pos, name)
return
end
if technic.use_RE_charge(itemstack, chisel_charge_per_node) then
if chisel.active[name] == "default" then
chiselme(pos,user,node)
else
chiselcut(pos,user,node)
end
return itemstack
end
end
technic.register_power_tool("mychisel:chisel", chisel_def)
else
technic.register_power_tool("mychisel:chisel",TechnicMaxCharge)
chisel_def.wear_represents = "technic_RE_charge"
chisel_def.on_refill = technic.refill_RE_charge
chisel_def.on_use = function(itemstack, user, pointed_thing)
if pointed_thing.type ~= "node" then
return
end
local pos = pointed_thing.under
local node = minetest.get_node(pos)
local name = user:get_player_name()
if minetest.is_protected(pos, user:get_player_name()) then
minetest.record_protection_violation(pos, user:get_player_name())
return
end
local meta = minetest.deserialize(itemstack:get_metadata())
if not meta or not meta.charge or
meta.charge < chisel_charge_per_node then
return
end
if chisel.active[name] == "default" then
chiselme(pos,user,node)
else
chiselcut(pos,user,node)
end
meta.charge = meta.charge - chisel_charge_per_node
if not technic.creative_mode then
technic.set_RE_wear(itemstack, meta.charge, TechnicMaxCharge)
itemstack:set_metadata(minetest.serialize(meta))
end
return itemstack
end,
minetest.register_tool("mychisel:chisel", chisel_def)
end
minetest.register_craft({
output = "mychisel:chisel",
recipe = {
{"default:diamond", "default:diamond" , "default:diamond"},
{"", "technic:stainless_steel_ingot", ""},
{"", "technic:battery", ""},
}
})
end
chisel.add_mod("default",5)