forked from BiLachner/TurtleMiner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patht_api.lua
703 lines (637 loc) · 26.8 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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
-- turtleminer/t_api.lua
local sequence = ''
local recording = false
local playing = false
local nom_bloc = "dirt" --lightstone_red_off
---------------
-- FUNCTIONS --
---------------
local positions = {} -- form positions
--------------
-- FORMSPEC --
--------------
--NOTE JP : come from WorldEdit MOD determines whether `nodename` is a valid node name, returning a boolean
function turtleminer.normalize_nodename(nodename)
nodename = nodename:gsub("^%s*(.-)%s*$", "%1")
if nodename == "" then return nil end
local fullname = ItemStack({name=nodename}):get_name() --resolve aliases of node names to full names
if minetest.registered_nodes[fullname] or fullname == "air" then --directly found node name or alias of nodename
return fullname
end
for key, value in pairs(minetest.registered_nodes) do
if key:find(":" .. nodename, 1, true) then --found in mod
return key
end
end
nodename = nodename:lower() --lowercase both for case insensitive comparison
for key, value in pairs(minetest.registered_nodes) do
if value.description:lower() == nodename then --found in description
return key
end
end
return nil
end
-- [function] show formspec
function turtleminer.show_formspec(name, pos, formname, params)
local meta = minetest.get_meta(pos) -- get meta
if not meta then return false end -- if not meta, something is wrong
positions[name] = pos -- set position (for receive fields)
local function show(formspec)
meta:set_string("formname", formname) -- set meta
minetest.show_formspec(name, "turtleminer:"..formname, formspec) -- show formspec
end
-- if form name is main, show main
if formname == "main" then
local node = nom_bloc
local nodename = turtleminer.normalize_nodename(node)
local formspec =
"size[6,6]" ..
cadre_if_recording() ..
string.format("field[0.2,0.4;4.5,0.8;form_nom_bloc;;%s]", minetest.formspec_escape(node)) ..
"button[4.3,0.4;1.1,0.1;form_nom_bloc_search;Search]" ..
(nodename and string.format("item_image[5.2,0;1,1;%s]", nodename)
or "image[5.2,0;1,1;unknown_node.png]") ..
--"label[0,0;Cliquez les boutons pour déplacer la tortue !]" .. --JP traduction
"field[0.3,3;6,5;script_jp;Script :;"..sequence.."]"..
"label[0,5.3;D ou G = tourner à Droite ou à Gauche]" ..
"label[0,5.6;A ou R = Avancer ou Reculer]" ..
"label[0,5.9;H ou B = aller en Haut ou en Bas]" ..
"button_exit[4,1;1,1;exit;Exit]" ..
"image_button[0,1;1,1;turtleminer_remote_arrow_up.png;up;]" ..
"image_button[1,1;1,1;turtleminer_remote_arrow_fw.png;forward;]" ..
"image_button[2,1;1,1;turtleminer_remote_dig_front.png;digfront;]" ..
"image_button[2,3;1,1;turtleminer_remote_dig_down.png;digbottom;]" ..
"image_button[3,1;1,1;turtleminer_remote_build_front.png;buildfront;]" ..
"image_button[3,3;1,1;turtleminer_remote_build_down.png;buildbottom;]" ..
"image_button[0,2;1,1;turtleminer_remote_arrow_left.png;turnleft;]"..
"image_button[2,2;1,1;turtleminer_remote_arrow_right.png;turnright;]" ..
"image_button[0,3;1,1;turtleminer_remote_arrow_down.png;down;]" ..
"image_button[1,3;1,1;turtleminer_remote_arrow_bw.png;backward;]" ..
"image_button[5,1;1,1;turtleminer_record.png;record;]" .. --JP nouveau
"image_button[5,2;1,1;turtleminer_stop.png;stopplay;]" .. --JP nouveau
"image_button[5,3;1,1;turtleminer_play.png;play;]" .. --JP nouveau
--"image_button[4,2;1,1;turtleminer_fourmi.png;fourmi;]" .. --JP nouveau
"image_button[1,2;1,1;turtleminer_remote_arrow_fw_build.png;buildforward;]" --JP nouveau
show(formspec) -- show
elseif formname == "set_name" then -- elseif form name is set_name, show set name formspec
if not params then local params = "" end -- use blank name is none specified
local formspec =
"size[6,1.7]"..
default.gui_bg_img..
"field[.25,0.50;6,1;name;Name Your Turtle:;"..params.."]"..
"button[4.95,1;1,1;submit_name;Set]"
show(formspec) -- show
end
end
-- [function] rotate
function turtleminer.rotate(pos, direction, player)
-- [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
-- minetest.sound_play("moveokay", { to_player = player, gain = 1.0 }) -- play sound
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
-- minetest.sound_play("moveokay", { to_player = player, gain = 1.0 }) -- play sound
end
end
-- [function] move
function turtleminer.move(pos, direction, name)
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
positions[name] = new_pos -- update position
minetest.get_meta(new_pos):from_table(oldmeta) -- set new meta
-- if not walkable, move player
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
-- minetest.sound_play("moveokay", { player = name, gain = 1.0 }) -- play sound
else -- else, return false
-- minetest.sound_play("moveerror", { player = name, gain = 1.0 }) -- play sound
return 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
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
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
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
turtle_move(pos, new_pos, entity_pos) -- call local function
end
end
-- [function] dig
function turtleminer.dig(pos, where, name)
-- [function] dig
local function dig(pos)
if minetest.get_node_or_nil(pos) then -- if node, dig
minetest.set_node(pos, { name = "air" })
nodeupdate(pos)
-- minetest.sound_play("moveokay", {to_player = name, gain = 1.0,}) -- play sound
else -- minetest.sound_play("moveerror", {to_player = name, gain = 1.0,})
end -- else, play 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
dig(dig_pos) -- dig node in front
elseif where == "below" then -- elseif where is below, dig below
dig_pos.y = dig_pos.y - 1 -- remove 1 from dig_pos y axis
dig(dig_pos) -- dig node below
end
end
-- [function] build
function turtleminer.build(pos, where, name)
-- [function] build
local function build(pos)
if minetest.get_node_or_nil(pos) then -- if node, dig
minetest.set_node(pos, { name = turtleminer.normalize_nodename(nom_bloc) }) --JP modification
nodeupdate(pos)
-- minetest.sound_play("moveokay", {to_player = name, gain = 1.0,}) -- play sound
else -- minetest.sound_play("moveerror", {to_player = name, gain = 1.0,})
end -- else, play 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
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
build(build_pos) -- dig node below
end
end
--------------
-- NODE DEF --
--------------
-- remote
minetest.register_craftitem("turtleminer:remotecontrol", {
description = "Turtle Remote Control",
inventory_image = "turtleminer_remotecontrol.png",
})
-- [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 },
paramtype = "light",
paramtype2 = "facedir",
node_box = {
type = "fixed",
fixed = desc.nodebox
},
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos) -- get meta
--meta:set_string("formspec", turtleminer.formspec()) -- set formspec
meta:set_string("owner", placer:get_player_name()) -- set owner
meta:set_string("infotext", "Unnamed turtle\n(owned by "..placer:get_player_name()..")") -- set infotext
end,
on_rightclick = function(pos, node, clicker)
local name = clicker:get_player_name() -- get clicker name
local meta = minetest.get_meta(pos)
-- if name not set, show name form
if not meta:get_string("name") or meta:get_string("name") == "" then
turtleminer.show_formspec(name, pos, "set_name", "") -- show set name formspec
elseif meta:get_string("formname") ~= "" then -- elseif formname is set, show specific form
-- if wielding remote control, show formspec
if clicker:get_wielded_item():get_name() == "turtleminer:remotecontrol" then
turtleminer.show_formspec(name, pos, meta:get_string("formname")) -- show formspec (note: no params)
else
minetest.chat_send_player(name, "Use a remote controller to access the turtle.")
end
else -- else, show normal formspec
-- if wielding remote control, show formspec
if clicker:get_wielded_item():get_name() == "turtleminer:remotecontrol" then
turtleminer.show_formspec(name, pos, "main") -- show main formspec
else
minetest.chat_send_player(name, "Use a remote controller to access the turtle.")
end
end
end,
})
end
-----------------------
-- DEBUT TEST JP DEF --------------------------------------------------------------------------------------------
-----------------------
-- [function] moveJP
function turtleminer.moveJP(pos, direction, name)
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 function turtle_move(pos, new_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
positions[name] = new_pos -- update position
minetest.get_meta(new_pos):from_table(oldmeta) -- set new meta
-- minetest.sound_play("moveokay", { player = name, gain = 1.0 }) -- play sound
--else -- else, return false
-- minetest.sound_play("moveerror", { player = name, gain = 1.0 }) -- play sound
-- return 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
turtle_move(pos, new_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
turtle_move(pos, new_pos) -- call local function
elseif direction == "up" or direction == "u" then
new_pos.y = new_pos.y + 1
turtle_move(pos, new_pos) -- call local function
elseif direction == "down" or direction == "d" then
new_pos.y = new_pos.y - 1
turtle_move(pos, new_pos) -- call local function
end
end
-- [function] buildforwardJP
function turtleminer.buildforwardJP(pos, name)
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 function turtle_move(pos, new_pos)
-- if not walkable, proceed
if not minetest.registered_nodes[minetest.get_node(new_pos).name].walkable then
minetest.set_node(pos, { name = turtleminer.normalize_nodename(nom_bloc) }) --JP modification
minetest.set_node(new_pos, node) -- create new node
positions[name] = new_pos -- update position
minetest.get_meta(new_pos):from_table(oldmeta) -- set new meta
-- minetest.sound_play("moveokay", { player = name, gain = 1.0 }) -- play sound
else -- else, return false
-- minetest.sound_play("moveerror", { player = name, gain = 1.0 }) -- play sound
return false
end
end
-- calculate new coords
new_pos.z = new_pos.z - dir.z
new_pos.x = new_pos.x - dir.x
turtle_move(pos, new_pos) -- call local function
end
-- [function] buildbelowJP
function turtleminer.buildbelowJP(pos, name)
local function build(pos)
if minetest.get_node_or_nil(pos) then -- if node, dig
minetest.set_node(pos, { name = turtleminer.normalize_nodename(nom_bloc) }) --JP modification
nodeupdate(pos)
-- minetest.sound_play("moveokay", {to_player = name, gain = 1.0,}) -- play sound
else -- minetest.sound_play("moveerror", {to_player = name, gain = 1.0,})
end -- else, play 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
build_pos.y = build_pos.y - 1 -- remove 1 from dig_pos y axis
build(build_pos) -- dig node below
local oldmeta = minetest.get_meta(pos):to_table() -- get meta
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
positions[name] = new_pos -- update position
minetest.get_meta(new_pos):from_table(oldmeta) -- set new meta
-- if not walkable, move player
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
-- minetest.sound_play("moveokay", { player = name, gain = 1.0 }) -- play sound
else -- else, return false
-- minetest.sound_play("moveerror", { player = name, gain = 1.0 }) -- play sound
return false
end
end
-- if direction is forward, move forward
-- 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
turtle_move(pos, new_pos, entity_pos) -- call local function
end
-- [function] langtonJP
function turtleminer.langtonJP(pos, where, name)
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
build_pos.y = build_pos.y - 1 -- remove 1 from dig_pos y axis
if not minetest.registered_nodes[minetest.get_node(build_pos).name].walkable then -- si vide alors je place un bloc
minetest.set_node(build_pos, { name = turtleminer.normalize_nodename(nom_bloc) })
nodeupdate(build_pos)
-- minetest.sound_play("moveokay", {to_player = name, gain = 1.0,}) -- play sound
turtleminer.rotate(pos, "right", name)
else -- sinon j'enlève le bloc
minetest.set_node(build_pos, { name = "air" })
nodeupdate(build_pos)
-- minetest.sound_play("moveerror", {to_player = name, gain = 1.0,})
turtleminer.rotate(pos, "left", name)
end
turtleminer.move(pos, "forward", name)
end
-- [function] langtonJPcent
function turtleminer.langtonJPcent(pos, name)
local newpos = pos
for count = 1, 100 do
turtleminer.langtonJP(newpos, "below", name)
newpos = positions[name]
end
end
function cadre_if_recording()
if recording then
return "image[-0.4,-0.4;8.2,7.9;turtleminer_cadre.png]"
else
return ""
end
end
-- [function] playJP
function firstIndexOf(str, substr)
local i = string.find(str, substr, 1, true)
if i == nil then
return 0
else
return i
end
end
function turtleminer.playJP_seq(pos, name, seq)
-- local newpos = pos VOIR NOTE quelques lignes plus bas dans le if playing
local compteur = 1
local nf_fois = 1
if firstIndexOf('1234567890', string.sub(seq, 1, 1)) ~= 0 then
nf_fois = 0
local seq1_est_nombre = firstIndexOf('1234567890', string.sub(seq, 1, 1))
while not (seq1_est_nombre == 0) do
if seq1_est_nombre == 10 then
nf_fois = nf_fois * 10
else
nf_fois = nf_fois * 10 + seq1_est_nombre
end
compteur = compteur + 1
seq1_est_nombre = firstIndexOf('1234567890', string.sub(seq, compteur, compteur))
end
end
if string.sub(seq, compteur, compteur) == '(' then
local compteur_par = compteur
local nb_parenth = 1
while not ((string.sub(seq, compteur_par, compteur_par) == ')' and nb_parenth == 0) or (#seq == compteur_par)) do -- ajouter ici un test du genre OR la fin de seq est atteinte
compteur_par = compteur_par + 1
if string.sub(seq, compteur_par, compteur_par) == '(' then
nb_parenth = nb_parenth + 1
elseif string.sub(seq, compteur_par, compteur_par) == ')' then
nb_parenth = nb_parenth - 1
end
end
if (string.sub(seq, compteur_par, compteur_par) == ')' and nb_parenth == 0) then
for i = 1, nf_fois do
turtleminer.playJP_seq(pos, name, string.sub(seq, compteur + 1, compteur_par - 1))
end
if #seq > compteur_par then
turtleminer.playJP_seq(pos, name, string.sub(seq, compteur_par + 1, #seq))
end
else
playing = false
sequence = sequence .. " error : ) missing !"
end
else
for i = 1, nf_fois do
if playing then
if string.sub(seq, compteur, compteur) == 'D' then turtleminer.rotate(positions[name], "right", name) -- elseif turn right button, rotate right
elseif string.sub(seq, compteur, compteur) == 'G' then turtleminer.rotate(positions[name], "left", name) -- elseif turn left button, rotate left
elseif string.sub(seq, compteur, compteur) == 'A' then turtleminer.move(positions[name], "forward", name) -- elseif move forward button, move forward
elseif string.sub(seq, compteur, compteur) == 'R' then turtleminer.move(positions[name], "backward", name) -- elseif move backward button, move backward
elseif string.sub(seq, compteur, compteur) == 'H' then turtleminer.move(positions[name], "up", name) -- elseif move up button, move up
elseif string.sub(seq, compteur, compteur) == 'B' then turtleminer.move(positions[name], "down", name) -- elseif move down button, move down
elseif string.sub(seq, compteur, compteur) == 'C' then turtleminer.dig(positions[name], "front", name) -- elseif dig in front button, dig in front
elseif string.sub(seq, compteur, compteur) == 'c' then turtleminer.dig(positions[name], "below", name) -- elseif dig bottom button, dig below
elseif string.sub(seq, compteur, compteur) == 'p' then turtleminer.build(positions[name], "front", name) -- elseif build in front button, build in front
elseif string.sub(seq, compteur, compteur) == 'L' then turtleminer.langtonJP(positions[name], "below", name) -- JP TEST
elseif string.sub(seq, compteur, compteur) == 'a' then turtleminer.buildbelowJP(positions[name], name) -- JP TEST
elseif string.sub(seq, compteur, compteur) == 'P' then turtleminer.build(positions[name], "below", name) -- JP TEST
elseif string.sub(seq, compteur, compteur) == ')' then
playing = false
sequence = sequence .. " error : ( missing !"
elseif string.sub(seq, compteur, compteur) == ' ' then
else
playing = false
sequence = sequence .. " error : instruction " .. string.sub(seq, compteur, compteur) .. " unknown !"
end -- JP TEST
-- newpos = positions[name] NOTE j'ai enlevé cette gestion de la position qui posait probleme en passant directement la position sans passer par cette variable local
end
end
if #seq > compteur then
turtleminer.playJP_seq(pos, name, string.sub(seq, compteur + 1, #seq))
end
end
end
--------------------- https://blockly-demo.appspot.com/static/demos/code/index.html?lang=fr
-- FIN TEST JP DEF ---------------------------------------------------------------------------------------------------------
---------------------
-- on player fields received
minetest.register_on_player_receive_fields(function(sender, formname, fields)
if formname ~= "turtleminer:main" then return end -- if not right formspec, return
local name = sender:get_player_name()
local pos = positions[name]
local form_script = fields.script_jp
if form_script ~= sequence then
sequence = form_script
end
if not pos then return end -- if not position, return - something is wrong
local node = minetest.get_node(pos) -- node info
-- check fields
if fields.turnright then
turtleminer.rotate(pos, "right", name) -- elseif turn right button, rotate right
if recording then
sequence = sequence .. 'D'
turtleminer.show_formspec(name, positions[name], "main")
end
elseif fields.turnleft then
turtleminer.rotate(pos, "left", name) -- elseif turn left button, rotate left
if recording then
sequence = sequence .. 'G'
turtleminer.show_formspec(name, positions[name], "main")
end
elseif fields.forward then
turtleminer.move(pos, "forward", name) -- elseif move forward button, move forward
if recording then
sequence = sequence .. 'A'
turtleminer.show_formspec(name, positions[name], "main")
end
elseif fields.backward then
turtleminer.move(pos, "backward", name) -- elseif move backward button, move backward
if recording then
sequence = sequence .. 'R'
turtleminer.show_formspec(name, positions[name], "main")
end
elseif fields.up then
turtleminer.move(pos, "up", name) -- elseif move up button, move up
if recording then
sequence = sequence .. 'H'
turtleminer.show_formspec(name, positions[name], "main")
end
elseif fields.down then
turtleminer.move(pos, "down", name) -- elseif move down button, move down
if recording then
sequence = sequence .. 'B'
turtleminer.show_formspec(name, positions[name], "main")
end
elseif fields.digfront then
turtleminer.dig(pos, "front", name) -- elseif dig in front button, dig in front
if recording then
sequence = sequence .. 'C'
turtleminer.show_formspec(name, positions[name], "main")
end
elseif fields.digbottom then
turtleminer.dig(pos, "below", name) -- elseif dig bottom button, dig below
if recording then
sequence = sequence .. 'c'
turtleminer.show_formspec(name, positions[name], "main")
end
elseif fields.buildfront then
turtleminer.build(pos, "front", name) -- elseif build in front button, build in front
if recording then
sequence = sequence .. 'p'
turtleminer.show_formspec(name, positions[name], "main")
end
elseif fields.buildbottom then
turtleminer.build(pos, "below", name) -- elseif build bottom button, build below
if recording then
sequence = sequence .. 'P'
turtleminer.show_formspec(name, positions[name], "main")
end
elseif fields.buildforward then
turtleminer.buildbelowJP(pos, name) -- JP TEST
if recording then
sequence = sequence .. 'a'
turtleminer.show_formspec(name, positions[name], "main")
end
elseif fields.play then
playing = true
turtleminer.playJP_seq(pos, name, sequence)
if (recording and playing) then
sequence = sequence .. sequence
end
turtleminer.show_formspec(name, positions[name], "main")
elseif fields.stopplay then -- JP TEST
if (recording or playing) then
recording = false
playing = false
else
sequence = '' -- un double stop permet ainsi de remettre à zéro le script
end
turtleminer.show_formspec(name, positions[name], "main")
elseif fields.record then
recording = true
turtleminer.show_formspec(name, positions[name], "main")
elseif fields.fourmi then
turtleminer.langtonJP(pos, "below", name)
if recording then
sequence = sequence .. 'L'
turtleminer.show_formspec(name, positions[name], "main")
end
elseif fields.form_nom_bloc_search then
nom_bloc = tostring(fields.form_nom_bloc)
turtleminer.show_formspec(name, positions[name], "main")
end -- JP TEST
end)
-- on player fields received
minetest.register_on_player_receive_fields(function(sender, formname, fields)
if formname ~= "turtleminer:set_name" then return end -- if not right formspec, return
local name = sender:get_player_name()
local meta = minetest.get_meta(positions[name])
local tname = fields.name or "Unnamed turtle"
meta:set_string("name", tname) -- set name
meta:set_string("infotext", tname .. "\n(owned by "..name..")") -- set infotext
turtleminer.show_formspec(name, positions[name], "main") -- show main formspec
end)