-
Notifications
You must be signed in to change notification settings - Fork 9
/
t_api.lua
609 lines (532 loc) · 19 KB
/
t_api.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
-- turtleminer/t_api.lua
------------------
-- LOAD TURTLES --
------------------
turtleminer.turtles = {}
local turtles = turtleminer.turtles
function turtleminer.load()
local file = io.open(minetest.get_worldpath() .. "/turtles.list", "r")
if file then
local from_file = minetest.deserialize(file:read("*all"))
file:close()
if type(from_file) == "table" and type(from_file.turtles) == "table" then
turtleminer.turtles = from_file.turtles
turtles = turtleminer.turtles
end
end
end
function turtleminer.save()
local file = io.open(minetest.get_worldpath().."/turtles.list", "w")
if file then
file:write(minetest.serialize({
version = 1,
turtles = turtleminer.turtles
}))
file:close()
else
assert(false)
end
end
minetest.register_on_shutdown(turtleminer.save)
turtleminer.load()
----------------------
-- HELPER FUNCTIONS --
----------------------
-- [function] update position
function turtleminer.update_pos(t_id, new_pos)
if not turtles[t_id] then return false end -- something's wrong
turtles[t_id].pos = new_pos -- update position
end
-- [function] run command
function turtleminer.run(name, pos, cmd, ...)
local t_id = minetest.get_meta(pos):get_string("t_id") -- get t_id
local result = turtleminer[cmd](pos, ...) -- run command
if not result then return end -- no result
-- play sound
if result.sound == true and name then
minetest.sound_play("moveokay", { to_player = name })
elseif result.sound == false and name then
minetest.sound_play("moveerror", { to_player = name })
end
-- update position
if type(result.pos) == "table" then
turtleminer.update_pos(t_id, result.pos)
end
-- send message
if result.msg then
minetest.chat_send_player(name, result.msg)
end
end
--------------
-- FORMSPEC --
--------------
local form_contexts = {}
-- formspec pages
turtleminer.forms = {
naming = {
get = function(pos)
local meta = minetest.get_meta(pos)
local t_id = meta:get_string("t_id")
return
"size[6,1.7]"..
default.gui_bg_img..
"field[.25,0.50;6,1;name;Name Your Turtle:;]"..
"button_exit[4.95,1;1,1;submit_name;Set]"
end,
on_receive_fields = function(player, fields)
local name = player:get_player_name()
local turtle = turtles[form_contexts[name].t_id]
if not turtle then
minetest.chat_send_player(name, "Unable to find the turtle (id="..minetest.pos_to_string(t_id)..")")
return
end
local pos = turtle.pos
local meta = minetest.get_meta(pos)
local tname = fields.name
if (fields.submit_name or fields.key_enter_field == "name") and tname and tname ~= "" then
meta:set_string("name", tname) -- set turtle name
meta:set_string("setup", "true") -- set setup boolean string
meta:set_string("infotext", tname .. "\n(owned by "..name..")") -- set infotext
turtle.name = tname
end
end,
},
main = {
get = function(pos)
local meta = minetest.get_meta(pos)
local tname, t_id = meta:get_string("name"), meta:get_string("t_id")
return
"size[6,4]" ..
"tabheader[0,0;tabs;Main,Inventory;1]" ..
"label[0,0;"..tname.." - id: "..t_id.."]" ..
"button_exit[4.3,-0.3;1.9,1;pos;"..minetest.pos_to_string(pos).."]" ..
"tooltip[pos;Refresh Position;#35454D;#FFFFFF]" ..
"label[0,0.5;Use the buttons to interact with your turtle.]" ..
"button_exit[4,1;1,1;exit;Exit]" ..
"image_button[0,1;1,1;turtleminer_remote_arrow_up.png;up;]" ..
"tooltip[up;Up;#35454D;#FFFFFF]" ..
"image_button[1,1;1,1;turtleminer_remote_arrow_fw.png;forward;]" ..
"tooltip[forward;Move Forward;#35454D;#FFFFFF]" ..
"image_button[2,1;1,1;turtleminer_remote_dig_front.png;digfront;]" ..
"tooltip[digfront;Dig in Front;#35454D;#FFFFFF]" ..
"image_button[2,3;1,1;turtleminer_remote_dig_down.png;digbottom;]" ..
"tooltip[digbottom;Dig Beneath;#35454D;#FFFFFF]" ..
"image_button[3,1;1,1;turtleminer_remote_build_front.png;buildfront;]" ..
"tooltip[buildfront;Build in Front;#35454D;#FFFFFF]" ..
"image_button[3,3;1,1;turtleminer_remote_build_down.png;buildbottom;]" ..
"tooltip[buildbottom;Build Beneath;#35454D;#FFFFFF]" ..
"image_button[0,2;1,1;turtleminer_remote_arrow_left.png;turnleft;]"..
"tooltip[turnleft;Turn Left;#35454D;#FFFFFF]" ..
"image_button[2,2;1,1;turtleminer_remote_arrow_right.png;turnright;]" ..
"tooltip[turnright;Turn Right;#35454D;#FFFFFF]" ..
"image_button[0,3;1,1;turtleminer_remote_arrow_down.png;down;]" ..
"tooltip[down;Down;#35454D;#FFFFFF]" ..
"image_button[1,3;1,1;turtleminer_remote_arrow_bw.png;backward;]" ..
"tooltip[backward;Move Backward;#35454D;#FFFFFF]"
end,
on_receive_fields = function(player, fields)
local name = player:get_player_name()
local turtle = turtles[form_contexts[name].t_id]
local pos = turtle.pos
if fields.tabs == "2" then
turtleminer.open(pos, player, "inventory")
return
end
local run = turtleminer.run
if fields.turnright then run(name, pos, "rotate", "right")
elseif fields.turnleft then run(name, pos, "rotate", "left")
elseif fields.forward then run(name, pos, "move", "forward")
elseif fields.backward then run(name, pos, "move", "backward")
elseif fields.up then run(name, pos, "move", "up")
elseif fields.down then run(name, pos, "move", "down")
elseif fields.digfront then run(name, pos, "dig", "front")
elseif fields.digbottom then run(name, pos, "dig", "below")
elseif fields.buildfront then run(name, pos, "build", "front")
elseif fields.buildbottom then run(name, pos, "build", "below")
elseif fields.pos then turtleminer.open(pos, player) -- update formspec
end
end,
},
inventory = {
get = function(pos)
local spos = pos.x .. "," .. pos.y .. "," .. pos.z
return
"size[9,9]" ..
"tabheader[0,0;tabs;Main,Inventory;2]" ..
default.gui_bg ..
default.gui_bg_img ..
default.gui_slots ..
"list[nodemeta:" .. spos .. ";main;0,0.3;8,4;]" ..
"list[nodemeta:" .. spos .. ";place_node;8.07,1.8;8,4;]" ..
"label[8.07,2.7;Node\nto\nplace]" ..
"list[current_player;main;0,4.85;8,1;]" ..
"list[current_player;main;0,6.08;8,3;8]" ..
"listring[nodemeta:" .. spos .. ";main]" ..
"listring[current_player;main]" ..
default.get_hotbar_bg(0,4.85)
end,
on_receive_fields = function(player, fields)
local name = player:get_player_name()
local turtle = turtles[form_contexts[name].t_id]
local pos = turtle.pos
if fields.tabs == "1" then
turtleminer.open(pos, player, "main")
return
end
end,
}
}
-- [function] open formspec (on_rightclick)
function turtleminer.open(pos, player, formname)
assert(pos, "turtleminer.open missing position")
local meta = minetest.get_meta(pos)
local name = player:get_player_name()
if meta:get_string("setup") == "false" then
form_contexts[name] = { t_id = meta:get_string("t_id"), form = "naming", } -- set context
minetest.show_formspec(name, "turtleminer:naming", turtleminer.forms["naming"].get(pos))
else
local form
if not formname then
if meta:get_string("formname") == "" then
form = "main"
else
form = meta:get_string("formname")
end
else
form = formname
end
meta:set_string("formname", form)
form_contexts[name] = { t_id = meta:get_string("t_id"), form = form, } -- set context
minetest.show_formspec(name, "turtleminer:"..form, turtleminer.forms[form].get(pos))
end
end
-- [event] on receive fields
minetest.register_on_player_receive_fields(function(player, formname, fields)
local formname = formname:split(":")[2]
if turtleminer.forms[formname] then
turtleminer.forms[formname].on_receive_fields(player, fields)
end
end)
---------------
-- FUNCTIONS --
---------------
-- [local function] get table length
local function getTableLength(t)
if not t then return nil end
local count = 0
for _ in pairs(t) do count = count + 1 end
return count
end
-- [local function] check itemstack validity
local function check_itemstack(stack)
if not stack:is_empty() and stack:is_known() then
return true
end
end
-- [function] check if breakable
function turtleminer.is_breakable(pos)
if minetest.registered_nodes[minetest.get_node(pos).name].groups.unbreakable ~= 1 then return true end
end
-- [function] check for room in turtle inventory
function turtleminer.check_for_room(pos, stackstring, listname)
if not listname then
listname = "main"
end
local meta = minetest.get_meta(pos)
assert(meta, "turtleminer.check_for_room missing meta")
local inv = meta:get_inventory()
local stack = ItemStack(stackstring)
if check_itemstack(stack) then
if not inv:room_for_item(listname, stack) then
return nil, "Not enough room"
end
return true
end
end
-- [function] check if inventory contains item
function turtleminer.has_item(pos, stackstring, listname)
if not listname then
listname = "main"
end
local meta = minetest.get_meta(pos)
assert(meta, "turtleminer.check_for_room missing meta")
local inv = meta:get_inventory()
local stack = ItemStack(stackstring)
if check_itemstack(stack) then
if inv:contains_item(listname, stack) then
return true
end
end
end
-- [function] add item to turtle inventory
function turtleminer.add_item(pos, stackstring, listname)
if not listname then
listname = "main"
end
local meta = minetest.get_meta(pos)
assert(meta, "turtleminer.check_for_room missing meta")
local inv = meta:get_inventory()
local stack = ItemStack(stackstring)
if check_itemstack(stack) then
local leftover = inv:add_item(listname, stack)
if leftover:get_count() > 0 then
return false, leftover, "Inventory is full! " .. leftover:get_count() .. " items weren't added"
else
return true
end
end
end
-- [function] take item from turtle inventory
function turtleminer.take_item(pos, stackstring, listname)
if not listname then
listname = "main"
end
local meta = minetest.get_meta(pos)
assert(meta, "turtleminer.check_for_room missing meta")
local inv = meta:get_inventory()
local stack = ItemStack(stackstring)
if check_itemstack(stack) then
local taken = inv:remove_item(listname, stack)
return true, taken:get_count()
end
end
-- [function] rotate
function turtleminer.rotate(pos, direction)
-- [function] calculate dir
local function calculate_dir(x, turn)
if turn == "right" then
x = x + 1
if x > 3 then x = 0 end
return x
elseif turn == "left" then
x = x - 1
if x < 0 then x = 3 end
return x
end
end
local node = minetest.get_node(pos) -- get node
local ndef = minetest.registered_nodes[node.name] -- get node def
-- if direction is right, rotate right
if direction == "right" then
-- calculate param2
local rotationPart = node.param2 % 32 -- get first 4 bits
local preservePart = node.param2 - rotationPart
local axisdir = math.floor(rotationPart / 4)
local rotation = rotationPart - axisdir * 4
local x = rotation + 1
if x > 3 then x = 0 end -- calculate x
rotationPart = axisdir * 4 + x
local new_param2 = preservePart + rotationPart
node.param2 = new_param2 -- set new param2
minetest.swap_node(pos, node) -- swap node
elseif direction == "left" then -- elseif direction is left, rotate left
-- calculate param2
local rotationPart = node.param2 % 32 -- get first 4 bits
local preservePart = node.param2 - rotationPart
local axisdir = math.floor(rotationPart / 4)
local rotation = rotationPart - axisdir * 4
local x = rotation - 1
if x < 0 then x = 3 end -- calculate x
rotationPart = axisdir * 4 + x
local new_param2 = preservePart + rotationPart
node.param2 = new_param2 -- set new param2
minetest.swap_node(pos, node) -- swap node
end
return { sound = true } -- return sound
end
-- [function] move
function turtleminer.move(pos, direction)
local oldmeta = minetest.get_meta(pos):to_table() -- get meta
local node = minetest.get_node(pos) -- get node ref
local dir = minetest.facedir_to_dir(node.param2) -- get facedir
local new_pos = vector.new(pos) -- new pos vector
local entity_pos = vector.new(pos) -- entity position vector
local function turtle_move(pos, new_pos, entity_pos)
-- if not walkable, proceed
if not minetest.registered_nodes[minetest.get_node(new_pos).name].walkable then
minetest.remove_node(pos) -- remote old node
minetest.set_node(new_pos, node) -- create new node
minetest.get_meta(new_pos):from_table(oldmeta) -- set new meta
-- if not walkable, move entity
if not minetest.registered_nodes[minetest.get_node(entity_pos).name].walkable then
local objects_to_move = {}
local objects = minetest.get_objects_inside_radius(new_pos, 1) -- get objects
for _, obj in ipairs(objects) do -- for every object, add to table
table.insert(objects_to_move, obj) -- add to table
end
for _, obj in ipairs(objects_to_move) do
local entity = obj:get_luaentity()
if not entity then
obj:setpos(entity_pos)
end
end
end
return { sound = true, pos = new_pos, }
else -- else, return false
return { sound = false }
end
end
-- if direction is forward, move forward
if direction == "forward" or direction == "f" then
-- calculate new coords
new_pos.z = new_pos.z - dir.z
new_pos.x = new_pos.x - dir.x
entity_pos.z = entity_pos.z - dir.z * 2
entity_pos.x = entity_pos.x - dir.x * 2
return turtle_move(pos, new_pos, entity_pos) -- call local function
elseif direction == "backward" or direction == "b" then
new_pos.z = new_pos.z + dir.z
new_pos.x = new_pos.x + dir.x
entity_pos.z = entity_pos.z + dir.z * 2
entity_pos.x = entity_pos.x + dir.x * 2
return turtle_move(pos, new_pos, entity_pos) -- call local function
elseif direction == "up" or direction == "u" then
new_pos.y = new_pos.y + 1
entity_pos.y = entity_pos.y + 2
return turtle_move(pos, new_pos, entity_pos) -- call local function
elseif direction == "down" or direction == "d" then
new_pos.y = new_pos.y - 1
entity_pos.y = entity_pos.y - 2
return turtle_move(pos, new_pos, entity_pos) -- call local function
end
end
-- [function] dig
function turtleminer.dig(pos, where)
-- [function] dig
local function dig(dig_pos)
if minetest.get_node_or_nil(dig_pos) and turtleminer.is_breakable(dig_pos) then -- if node & breakable, dig
local dig_node = minetest.get_node(dig_pos)
local itemstacks = minetest.get_node_drops(dig_node.name)
for _, itemname in ipairs(itemstacks) do
local ok, leftover, msg = turtleminer.add_item(pos, itemname)
if ok == false then
minetest.add_item(dig_pos, itemname)
end
end
minetest.set_node(dig_pos, { name = "air" })
minetest.check_for_falling(dig_pos)
return { sound = true } -- return sound
else return { sound = false } end -- else, return error sound
end
local node = minetest.get_node(pos) -- get node ref
local dir = minetest.facedir_to_dir(node.param2) -- get facedir
local dig_pos = vector.new(pos) -- dig position
if where == "front" then -- if where is front, dig in front
-- adjust position considering facedir
dig_pos.z = dig_pos.z - dir.z
dig_pos.x = dig_pos.x - dir.x
return dig(dig_pos) -- dig node in front if not unbreakable
elseif where == "below" then -- elseif where is below, dig below
dig_pos.y = dig_pos.y - 1 -- remove 1 from dig_pos y axis
return dig(dig_pos) -- dig node below if not unbreakable
end
end
-- [function] build
function turtleminer.build(pos, where)
local inv = minetest.get_meta(pos):get_inventory()
-- [function] build
local function build(pos)
if minetest.registered_nodes[minetest.get_node(pos).name].buildable_to then -- if is buildable_to, build
if inv:is_empty("place_node") then
return { sound = false }
else
local place_stack = inv:get_list("place_node")[1]
local place_name = place_stack:get_name()
if minetest.registered_nodes[place_name] then
inv:remove_item("place_node", place_name.." 1")
minetest.set_node(pos, { name = place_name })
minetest.check_for_falling(pos)
return { sound = true } -- return sound
else
return { sound = false }
end
end
else return { sound = false } end -- else, return error sound
end
local node = minetest.get_node(pos) -- get node ref
local dir = minetest.facedir_to_dir(node.param2) -- get facedir
local build_pos = vector.new(pos) -- dig position
if where == "front" then -- if where is front, dig in front
-- adjust position considering facedir
build_pos.z = build_pos.z - dir.z
build_pos.x = build_pos.x - dir.x
return build(build_pos) -- dig node in front
elseif where == "below" then -- elseif where is below, dig below
build_pos.y = build_pos.y - 1 -- remove 1 from dig_pos y axis
return build(build_pos) -- dig node below
end
end
--------------
-- NODE DEF --
--------------
local turtle_id_counter = getTableLength(turtles) or 0 -- get id of last turtle
-- [function] register turtle
function turtleminer.register_turtle(turtlestring, desc)
minetest.register_node("turtleminer:"..turtlestring, {
drawtype = "nodebox",
description = desc.description,
tiles = desc.tiles,
groups={ oddly_breakable_by_hand = 1, turtle = 1 },
light_source = desc.light_source or 7,
paramtype = "light",
paramtype2 = "facedir",
node_box = {
type = "fixed",
fixed = desc.nodebox
},
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
local name = placer:get_player_name()
turtle_id_counter = turtle_id_counter + 1
local t_id = "t_" .. turtle_id_counter
turtles[t_id] = {
pos = pos,
owner = name,
name = nil,
}
turtleminer.save()
meta:set_string("owner", name) -- set owner
meta:set_string("t_id", t_id) -- set turtle id
meta:set_string("setup", "false") -- set setup boolean string
meta:set_string("infotext", "Unnamed turtle\n(owned by "..name..")") -- set name
local inv = minetest.get_inventory({type="node", pos={x=pos.x, y=pos.y, z=pos.z}})
inv:set_size("main", 8*4)
inv:set_size("place_node", 1*1)
end,
on_dig = function(pos, node, player)
turtles[minetest.get_meta(pos):get_string("t_id")] = nil -- clear entry
turtleminer.save() -- save
minetest.node_dig(pos, node, player)
end,
on_rightclick = function(pos, node, player)
turtleminer.open(pos, player)
end,
can_dig = function(pos,player)
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
return inv:is_empty("main")
end,
on_metadata_inventory_move = function(pos, from_list, from_index,
to_list, to_index, count, player)
turtleminer.log(player:get_player_name() ..
" moves stuff in turtle inventory at " .. minetest.pos_to_string(pos))
end,
on_metadata_inventory_put = function(pos, listname, index, stack, player)
turtleminer.log(player:get_player_name() ..
" moves " .. stack:get_name() ..
" to turtle inventory at " .. minetest.pos_to_string(pos))
end,
on_metadata_inventory_take = function(pos, listname, index, stack, player)
turtleminer.log(player:get_player_name() ..
" takes " .. stack:get_name() ..
" from turtle inventory at " .. minetest.pos_to_string(pos))
end,
on_blast = function(pos)
local drops = {}
default.get_inventory_drops(pos, "main", drops)
drops[#drops+1] = "turtleminer:"..turtlestring
minetest.remove_node(pos)
return drops
end,
})
end