forked from ac-minetest/basic_robot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.lua
1643 lines (1340 loc) · 53.6 KB
/
commands.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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
basic_robot.commands = {};
-- set up nodes for planting (for example seeds -> plant) : [nodename] = plant_name
basic_robot.plant_table = {["farming:seed_barley"]="farming:barley_1",["farming:beans"]="farming:beanpole_1", -- so it works with farming redo mod
["farming:blueberries"]="farming:blueberry_1",["farming:carrot"]="farming:carrot_1",["farming:cocoa_beans"]="farming:cocoa_1",
["farming:coffee_beans"]="farming:coffee_1",["farming:corn"]="farming:corn_1",
["farming:seed_cotton"]="farming:cotton_1",["farming:cucumber"]="farming:cucumber_1",["farming:grapes"]="farming:grapes_1",
["farming:melon_slice"]="farming:melon_1",["farming:potato"]="farming:potato_1",["farming:pumpkin_slice"]="farming:pumpkin_1",
["farming:raspberries"]="farming:raspberry_1",["farming:rhubarb"]="farming:rhubarb_1",["farming:tomato"]="farming:tomato_1",
["farming:seed_wheat"]="farming:wheat_1"}
local function tick(pos) -- needed for plants to start growing: minetest 0.4.14 farming
minetest.get_node_timer(pos):start(math.random(166, 286))
end
local pi = math.pi;
local function pos_in_dir(obj, dir) -- position after we move in specified direction
local yaw = obj:get_yaw();
local pos = obj:get_pos();
if dir == 1 then -- left
yaw = yaw + pi/2;
elseif dir == 2 then --right
yaw = yaw - pi/2;
elseif dir == 3 then -- forward
elseif dir == 4 then -- backward
yaw = yaw+pi;
elseif dir == 5 then -- up
pos.y=pos.y+1
elseif dir == 6 then -- down
pos.y=pos.y-1
elseif dir == 7 then -- left_down
yaw = yaw + pi/2;pos.y=pos.y-1
elseif dir == 8 then -- right_down
yaw = yaw - pi/2;pos.y=pos.y-1
elseif dir == 9 then -- forward_down
pos.y=pos.y-1
elseif dir == 10 then -- backward_down
yaw = yaw + pi;pos.y=pos.y-1
elseif dir == 11 then -- left_up
yaw = yaw + pi/2;pos.y=pos.y+1
elseif dir == 12 then -- right_up
yaw = yaw - pi/2;pos.y=pos.y+1
elseif dir == 13 then -- forward_up
pos.y=pos.y+1
elseif dir == 14 then -- backward_up
yaw = yaw + pi;pos.y=pos.y+1
end
if dir ~= 5 and dir ~= 6 then
pos.x = pos.x - math.sin(yaw) -- math.cos(yaw+pi/2)
pos.z = pos.z + math.cos(yaw) -- math.sin(yaw+pi/2)
end
return pos
end
local check_operations = function(name, amount, quit)
if basic_robot.maxoperations~=0 then
local data = basic_robot.data[name];
local operations = data.operations-amount;
if operations >= 0 then
data.operations = operations
else
if quit then
error("Robot out of available operations in one step (1s). View available operations with self.operations() and check help to see how much operations each action requires."); return false
end
return false
end
end
end
basic_robot.commands.move = function(name,dir)
check_operations(name,2,true)
local obj = basic_robot.data[name].obj;
local pos = pos_in_dir(obj, dir)
-- can move through walkable nodes
if minetest.registered_nodes[minetest.get_node(pos).name].walkable then return end
-- up; no levitation!
if minetest.get_node({x=pos.x,y=pos.y-1,z=pos.z}).name == "air" and
minetest.get_node({x=pos.x,y=pos.y-2,z=pos.z}).name == "air" then
return false
end
obj:move_to(pos, true)
-- sit and stand up for model - doesnt work for overwriten obj export
-- if dir == 5 then-- up
-- obj:set_animation({x=0,y=0})
-- elseif dir == 6 then -- down
-- obj:set_animation({x=81,y=160})
-- end
return true
end
basic_robot.commands.turn = function (name, angle)
local obj = basic_robot.data[name].obj;
local yaw;
-- more precise turns by 1 degree resolution
local mult = math.pi/180;
local yaw = obj:get_yaw();
yaw = math.floor((yaw+angle)/mult+0.5)*mult;
obj:set_yaw(yaw);
end
basic_robot.digcosts = { -- 1 energy = 1 coal
["default:stone"] = 1/25,
["gloopblocks:pumice_cooled"] = 1/25,
["default:cloud"] = 10^9,
}
basic_robot.commands.dig = function(name,dir)
local energy = 0;
check_operations(name,6,true)
local obj = basic_robot.data[name].obj;
local pos = pos_in_dir(obj, dir)
local luaent = obj:get_luaentity();
if minetest.is_protected(pos,luaent.owner) then return false end
local nodename = minetest.get_node(pos).name;
if nodename == "air" or nodename=="ignore" then return false end
local spos = obj:get_luaentity().spawnpos;
local inv = minetest.get_meta(spos):get_inventory();
--require energy to dig
if basic_robot.dig_require_energy then
local digcost = basic_robot.digcosts[nodename] or 1/(5*25); -- default 1/5th of stone dig
if digcost then
local data = basic_robot.data[name];
local energy = (data.menergy or 0) - digcost;
if energy<0 then
error("need " .. digcost .. " energy to dig " .. nodename .. ". Use machine.generate to get some energy.");
end
data.menergy = energy;
end
end
if not inv then return end
--inv:add_item("main",ItemStack( nodename ));
basic_robot.give_drops(nodename, inv);
minetest.set_node(pos,{name = "air"})
--DS: sounds
local sounds = minetest.registered_nodes[nodename].sounds
if sounds then
local sound = sounds.dug
if sound then
minetest.sound_play(sound,{pos=pos, max_hear_distance = 10})
end
end
return true
end
basic_robot.commands.insert_item = function(name,item, inventory,dir)
check_operations(name,2,true)
local obj = basic_robot.data[name].obj;
local tpos = pos_in_dir(obj, dir); -- position of target block
local luaent = obj:get_luaentity();
if minetest.is_protected(tpos,luaent.owner ) then return false end
local pos = basic_robot.data[name].spawnpos; -- position of spawner block
local meta = minetest.get_meta(pos);
local tmeta = minetest.get_meta(tpos);
local inv = minetest.get_meta(pos):get_inventory();
local tinv = minetest.get_meta(tpos):get_inventory();
if not inventory then inventory = "main"; end
--if not inv then return end
local stack = ItemStack(item);
if (not inv:contains_item("main", stack) or not tinv:room_for_item(inventory, stack)) and meta:get_int("admin")~=1 then
return false
end
tinv:add_item(inventory,stack);
inv:remove_item("main", stack);
return true
end
basic_robot.commands.take_item = function(name,item, inventory,dir)
check_operations(name,2,true)
local obj = basic_robot.data[name].obj;
local tpos = pos_in_dir(obj, dir); -- position of target block
local luaent = obj:get_luaentity();
if minetest.is_protected(tpos,luaent.owner ) then return false end
local pos = basic_robot.data[name].spawnpos; -- position of spawner block
if basic_robot.bad_inventory_blocks[ minetest.get_node(tpos).name ] then return false end -- dont allow take from
local meta = minetest.get_meta(pos);
local tmeta = minetest.get_meta(tpos);
local inv = minetest.get_meta(pos):get_inventory();
local tinv = minetest.get_meta(tpos):get_inventory();
if not inventory then inventory = "main"; end
--if not inv then return end
local stack = ItemStack(item);
local contains = tinv:contains_item(inventory, stack);
if (not contains or not inv:room_for_item("main", stack)) and meta:get_int("admin")~=1 then
return false
end
inv:add_item("main",stack);
tinv:remove_item(inventory, stack);
return true
end
-- check_inventory(item, inventory, position)
--if position>0 then it returns name of item at that position
basic_robot.commands.check_inventory = function(name,itemname, inventory, position, dir)
local obj = basic_robot.data[name].obj;
local tpos;
if dir~=0 then
tpos = pos_in_dir(obj, dir); -- position of target block in front
else
tpos = obj:get_luaentity().spawnpos;
end
local tinv = minetest.get_meta(tpos):get_inventory();
if not position then position = -1 end
if position>0 then
return tinv:get_stack(inventory, position):to_string()
end
if itemname == "" then
return tinv:is_empty(inventory)
end
local stack = ItemStack(itemname);
if not inventory then inventory = "main"; end
return tinv:contains_item(inventory, stack);
end
basic_robot.no_teleport_table = {
["itemframes:item"] = true,
["signs:text"] = true,
["basic_robot:robot"] = true,
["robot"] = true,
}
basic_robot.commands.pickup = function(r,name)
if r>8 then return false end
check_operations(name,4,true)
local pos = basic_robot.data[name].obj:get_pos();
local spos = basic_robot.data[name].spawnpos; -- position of spawner block
local meta = minetest.get_meta(spos);
local inv = minetest.get_meta(spos):get_inventory();
local picklist = {};
for _,obj in pairs(minetest.get_objects_inside_radius({x=pos.x,y=pos.y,z=pos.z}, r)) do
local lua_entity = obj:get_luaentity()
if not obj:is_player() and lua_entity and lua_entity.itemstring then
local detected_obj = lua_entity.itemstring or ""
if not basic_robot.no_teleport_table[detected_obj] then -- object on no teleport list
-- put item in chest
local stack = ItemStack(lua_entity.itemstring)
picklist[#picklist+1]=detected_obj;
if inv:room_for_item("main", stack) then
inv:add_item("main", stack);
obj:set_pos({x=0,y=0,z=0}) -- no dupe
end
obj:remove();
end
end
end
if not picklist[1] then return nil end
return picklist
end
basic_robot.commands.read_node = function(name,dir)
local obj = basic_robot.data[name].obj;
local pos = pos_in_dir(obj, dir)
return minetest.get_node(pos).name or ""
end
basic_robot.commands.read_text = function(name,mode,dir,stringname)
if not mode then mode = 0 end
local obj = basic_robot.data[name].obj;
local pos = pos_in_dir(obj, dir)
if stringname == nil then
stringname = "infotext"
end
if mode == 1 then return minetest.get_meta(pos):get_int(stringname) else
return minetest.get_meta(pos):get_string(stringname) or "" end
end
basic_robot.commands.write_text = function(name,dir,text)
local obj = basic_robot.data[name].obj;
local pos = pos_in_dir(obj, dir)
local luaent = obj:get_luaentity();
if minetest.is_protected(pos,luaent.owner ) then return false end
minetest.get_meta(pos):set_string("infotext",text or "")
return true
end
basic_robot.commands.place = function(name,nodename, param2,dir)
check_operations(name,2,true)
local obj = basic_robot.data[name].obj;
local pos = pos_in_dir(obj, dir)
local luaent = obj:get_luaentity();
if minetest.is_protected(pos,luaent.owner ) then return false end
if minetest.get_node(pos).name~="air" then return false end
local spos = obj:get_luaentity().spawnpos;
local meta = minetest.get_meta(spos);
local inv = meta:get_inventory();
if not inv then return false end
if not inv:contains_item("main", ItemStack(nodename)) and meta:get_int("admin")~=1 then return false end
inv:remove_item("main", ItemStack(nodename));
--DS
local registered_node = minetest.registered_nodes[nodename];
if registered_node then
local sounds = registered_node.sounds
if sounds then
local sound = sounds.place
if sound then
minetest.sound_play(sound,{pos=pos, max_hear_distance = 10})
end
end
end
local placename = basic_robot.plant_table[nodename];
if placename then
minetest.set_node(pos,{name = placename})
tick(pos); -- needed for seeds to grow
else -- normal place
if param2 then
minetest.set_node(pos,{name = nodename, param2 = param2})
else
minetest.set_node(pos,{name = nodename})
end
end
return true
end
basic_robot.commands.attack = function(name, target) -- attack range 4, damage 5
local energy = 0;
check_operations(name,4,true);
local reach = 4;
local damage = 5;
local tplayer = minetest.get_player_by_name(target);
if not tplayer then return false end
local obj = basic_robot.data[name].obj;
local pos = obj:get_pos();
local tpos = tplayer:get_pos();
if math.abs(pos.x-tpos.x)> reach or math.abs(pos.y-tpos.y)> reach or math.abs(pos.z-tpos.z)> reach then
return false
end
tplayer:set_hp(tplayer:get_hp()-damage)
return true
end
basic_robot.commands.grab = function(name,target)
local reach = 4;
local tplayer = minetest.get_player_by_name(target);
if not tplayer then return false end
local obj = basic_robot.data[name].obj;
local pos = obj:get_pos();
local tpos = tplayer:get_pos();
if math.abs(pos.x-tpos.x)> reach or math.abs(pos.y-tpos.y)> reach or math.abs(pos.z-tpos.z)> reach then
return false
end
if tplayer:get_attach() then
tplayer:set_detach()
return false
else
tplayer:set_attach(obj, "", {x=0,y=5,z=0}, {x=0,y=0,z=0})
end
return true
end
--local minetest_version = minetest.get_version().string;
basic_robot.commands.read_book = function (itemstack) -- itemstack should contain book
local data;
--if minetest_version == "0.4.16" then
data = itemstack:get_meta():to_table().fields -- 0.4.16
if data and data.text then
data.text = data.text:gsub(string.char(13),string.char(10)) --for unknown reason books sometime? convert \n (10) to CR (13)
end
-- else
-- local data = minetest.deserialize(itemstack:get_metadata()) -- pre 0.4.16
-- end
if data then
return data.title,data.text;
else
return nil
end
end
basic_robot.commands.write_book = function(name,title,text) -- returns itemstack containing book
local lpp = 14;
local new_stack = ItemStack("default:book_written")
local data = {}
if title == "" or not title then title = "program book "..minetest.get_gametime() end
if text == "" or not text then text = "empty" end
data.text = text
data.title = title
data.text_len = #data.text
data.page = 1
data.description = title or ""
data.page_max = math.ceil((#data.text:gsub("[^\n]", "") + 1) / lpp)
data.owner = name
--local data_str = minetest.serialize(data) -- pre 0.4.16
--new_stack:set_metadata(data_str);
new_stack:get_meta():from_table({fields = data}) -- 0.4.16
return new_stack;
end
basic_robot.give_drops = function(nodename, inv) -- gives apropriate drops when node is dug
local table = minetest.registered_items[nodename];
local dropname;
if table~=nil then --put in chest
if table.drop~= nil then -- drop handling
if table.drop.items then
--handle drops better, emulation of drop code
local max_items = table.drop.max_items or 0;
if max_items==0 then -- just drop all the items (taking the rarity into consideration)
max_items = #table.drop.items or 0;
end
max_items = math.random(max_items) -- return random number of items
local drop = table.drop;
local i = 0;
for k,v in pairs(drop.items) do
if i > max_items then break end;
i=i+1;
local rare = v.rarity or 1;
if rare>0 and math.random(1, rare)==1 then
dropname = v.items[math.random(1,#v.items)]; -- pick item randomly from list
inv:add_item("main",dropname);
end
end
else
inv:add_item("main",table.drop);
end
else
inv:add_item("main",nodename);
end
end
end
local render_text = function(text,linesize)
local count = math.floor(string.len(text)/linesize)+1;
local width = 18; local height = 24;
local tex = "";
local ret = {};
local y = 0; local x=0;
for i=1,string.len(text) do
local cb = string.byte(text,i);
local c = "";
if cb == 10 or cb == 13 then
y=y+1; x=0
else
c = string.format("%03d",cb)..".png"
ret[#ret+1] = ":" .. (x*width) .. "," .. (y*height) .. "=" .. c;
--tex = tex .. ":" .. (x*width) .. "," .. (y*height) .. "=" .. c;
if x==linesize-1 then y=y+1 x=0 else x=x+1 end
end
end
local background = "(black_screen.png^[resize:"..(linesize*width).. "x".. (linesize*height) ..")";
--tex = "([combine:"..(linesize*width).."x"..(linesize*height)..tex..")";
return background .. "^" .."([combine:"..(linesize*width).."x"..(linesize*height)..table.concat(ret,"")..")";
end
basic_robot.commands.display_text = function(obj,text,linesize,size)
if not linesize or linesize<1 then linesize = 20 elseif linesize>40 then linesize = 40 end
if size and size<=0 then size = 1 end
if string.len(text)>linesize*linesize then text = string.sub(text,1,linesize*linesize) end
local tex = render_text(text,linesize);
if not size then return tex end
if string.len(tex)<=1600 then
obj:set_properties({
textures = {"topface.png","legs.png",tex,"face-back.png","left-hand.png","right-hand.png"},
--textures={"arrow.png","basic_machine_side.png",tex,"basic_machine_side.png","basic_machine_side.png","basic_machine_side.png"},
visual_size = {x=size,y=size}})
else
self.label("error: string too long")
end
end
local robot_activate_furnace = minetest.registered_nodes["default:furnace"].on_metadata_inventory_put; -- this function will activate furnace
basic_robot.commands.activate = function(name,mode, dir)
check_operations(name,2,true);
local obj = basic_robot.data[name].obj;
local tpos = pos_in_dir(obj, dir); -- position of target block in front
local node = minetest.get_node(tpos);
if node.name == "default:furnace" or node.name == "default:furnace_active" then
if mode>0 then robot_activate_furnace(tpos) end
return true
end
local table = minetest.registered_nodes[node.name];
if table and table.mesecons and table.mesecons.effector then
else
return false
end -- error
local effector=table.mesecons.effector;
if not mode then mode = 1 end
if mode > 0 then
if not effector.action_on then return false end
effector.action_on(tpos,node,16)
elseif mode<0 then
if not effector.action_off then return false end
effector.action_off(tpos,node,16)
end
return true
end
local write_keyevent = function(data,pos, puncher,type)
local keyevent = data.keyevent;
if not keyevent then -- init
data.keyevent = {5,1,1,{}} -- how many events buffer holds, input idx, output idx, buffer data
keyevent = data.keyevent;
end
local iidx = keyevent[2]; -- input idx
-- write event at input idx
keyevent[4][iidx] = {x=pos.x,y=pos.y,z=pos.z, puncher = puncher, type = type}
local oidx = keyevent[3]; -- output idx
iidx=iidx+1; if iidx>keyevent[1] then iidx = 1 end -- advance idx for next input write
keyevent[2] = iidx
if iidx == oidx then -- old event was overwritten, lets increase output idx
oidx = oidx+1
if oidx>keyevent[1] then oidx = 1 end
keyevent[3] = oidx
end
end
basic_robot.commands.write_keyevent = write_keyevent;
local button_punched = function(pos, node, player,type)
local name = player:get_player_name(); if name==nil then return end
local round = math.floor;
local r = basic_robot.radius; local ry = 2*r;
local ppos = {x=round(pos.x/r+0.5)*r,y=round(pos.y/ry+0.5)*ry+1,z=round(pos.z/r+0.5)*r}; -- just on top of basic_protect:protector!
local hppos = minetest.hash_node_position(ppos)
local rname = basic_robot.data.punchareas[hppos];
local data = basic_robot.data[rname];
if data then
write_keyevent(data,pos, player:get_player_name(),type)
end
end
local buttoncolors = {};
local register_robot_button = function(R,G,B,colorname,type)
buttoncolors[type] = "basic_robot:button"..colorname;
minetest.register_node("basic_robot:button"..colorname,
{
description = "robot button",
tiles = {"robot_button.png^[colorize:#"..R..G..B..":180"},
inventory_image = "robot_button.png^[colorize:#"..R..G..B..":180",
wield_image = "robot_button.png^[colorize:#"..R..G..B..":180",
is_ground_content = false,
groups = {cracky=3,not_in_craft_guide = 1},
on_punch = function(pos, node,player) button_punched(pos, node,player,type) end
})
end
local register_robot_button_number = function(number,type)
local idx = number+48
local texname = "robochars.png^[sheet:16x16:" .. idx % 16 .. "," .. math.floor(idx/ 16).."^[invert:rgb";
minetest.register_node("basic_robot:button"..number,
{
description = "robot button",
tiles = {texname},
inventory_image = texname,
wield_image = texname,
paramtype2 = "facedir",
is_ground_content = false,
groups = {cracky=3,not_in_craft_guide = 1},
on_punch = function(pos, node,player) button_punched(pos, node,player,type) end
})
end
local register_robot_button_char = function(number,type)
local texname = "robochars.png^[sheet:16x16:" .. number % 16 .. "," .. math.floor(number/ 16);
-- local texname = string.format("%03d",number).. ".png";
minetest.register_node("basic_robot:button_"..number,
{
description = "robot button",
tiles = {texname},
inventory_image = texname,
wield_image = texname,
is_ground_content = false,
groups = {cracky=3,not_in_craft_guide = 1},
paramtype2 = "facedir",
on_punch = function(pos, node,player) button_punched(pos, node,player,type) end
})
end
local register_robot_button_custom = function(number,texture)
local type = number
minetest.register_node("basic_robot:button_"..number,
{
description = "robot button",
tiles = {texture .. ".png"},
inventory_image = texture .. ".png",
wield_image = texture .. ".png",
is_ground_content = false,
groups = {cracky=3,not_in_craft_guide = 1},
on_punch = function(pos, node,player) button_punched(pos, node,player,type) end
})
end
--[[ colors!
0 — white 8 — green
1 — yellow 9 — dark green
2 — orange 10 — brown
3 — red 11 — tan
4 — magenta 12 — light grey
5 — purple 13 — medium grey
6 — blue 14 — dark grey
7 — cyan 15 — black
--]]
--16-color palette from macintosh II, 1987
register_robot_button("FF","FF","FF","white",1); -- white
register_robot_button("FC","F4","00","yellow",2); -- yellow
register_robot_button("FF","64","00","orange",3); -- orange
register_robot_button("DD","02","02","red",4); -- red
register_robot_button("F0","02","85","magenta",5); -- magenta
register_robot_button("46","00","A5","purple",6); -- purple
register_robot_button("00","00","D5","blue",7); -- blue
register_robot_button("00","AE","E9","cyan",8); -- cyan
register_robot_button("1A","B9","0C","green",9); -- green
register_robot_button("00","64","08","dark_green",10);-- dark_green
register_robot_button("57","28","00","brown",11);-- brown
register_robot_button("91","71","35","tan",12);-- tan
register_robot_button("C1","C1","C1","light_grey",13);-- light_grey
register_robot_button("81","81","81","medium_grey",14);-- medium_grey
register_robot_button("3E","3E","3E","dark_grey",15);-- dark_grey
register_robot_button("00","00","00","black",16);-- black
--[[ -- old colors
register_robot_button("FF","FF","FF",1);
register_robot_button("80","80","80",2);
register_robot_button("FF","80","80",3);
register_robot_button("80","FF","80",4);
register_robot_button("80","80","FF",5);
register_robot_button("FF","FF","80",6);
--]]
for i = 0,9 do register_robot_button_number(i,i+17) end -- all buttons shift by 10 from old version!
for i = 0,255 do register_robot_button_char(i,i+27) end
register_robot_button_custom(283,"puzzle_switch_off")
register_robot_button_custom(284,"puzzle_switch_on")
register_robot_button_custom(285,"puzzle_button_off")
register_robot_button_custom(286,"puzzle_button_on")
register_robot_button_custom(287,"puzzle_equalizer")
register_robot_button_custom(288,"puzzle_setter")
register_robot_button_custom(289,"puzzle_piston")
register_robot_button_custom(290,"puzzle_diode")
register_robot_button_custom(291,"puzzle_NOT")
register_robot_button_custom(292,"puzzle_delayer")
register_robot_button_custom(293,"puzzle_platform")
register_robot_button_custom(294,"puzzle_giver")
register_robot_button_custom(295,"puzzle_checker")
-- interactive button for robot: place robot on top of protector to intercept events
local dout = minetest.chat_send_all
basic_robot.commands.keyboard = {
get = function(name) -- read keyboard event
local data = basic_robot.data[name];
if data.keyevent then
local keyevent = data.keyevent;
local oidx = keyevent[3];
local iidx = keyevent[2];
if oidx == iidx then return nil end --just read past last written event, nothing to read anymore
local event = keyevent[4][oidx];
-- move oidx (read position ) to newer event
oidx = oidx+1; if oidx>keyevent[1] then oidx = 1 end
keyevent[3] = oidx
return event
else
return nil
end
end,
set = function(data,pos,type)
local owner = data.owner;
local spos = data.spawnpos;
local dist = math.max(math.abs(spos.x-pos.x),math.abs(spos.y-pos.y),math.abs(spos.z-pos.z));
if dist>10 then return false end
if minetest.is_protected(pos,owner) then return false end -- with fast protect checks this shouldnt be problem!
local nodename;
if type == 0 then
nodename = "air"
elseif type < 17 then
nodename = buttoncolors[type]
elseif type>=17 and type <= 26 then
nodename = "basic_robot:button"..(type-17);
else
nodename = "basic_robot:button_"..(type-27);
end
minetest.swap_node(pos, {name = nodename})
return true
end,
}
basic_robot.commands.craftcache = {};
basic_robot.commands.craft = function(item, mode, idx,amount, name)
amount = amount and tonumber(amount) or 1;
if amount<0 then amount = 1 end
if not item then return false end
local cache = basic_robot.commands.craftcache[name];
if not cache then basic_robot.commands.craftcache[name] = {}; cache = basic_robot.commands.craftcache[name] end
local itemlist = {}; local output = "";
if cache.item == item and cache.idx == idx then -- read cache
itemlist = cache.itemlist;
output = cache.output;
else
local craft;
if not idx then
craft = minetest.get_craft_recipe(item);
else
craft = minetest.get_all_craft_recipes(item)[idx]
end
if craft and craft.type == "normal" and craft.items then else return false end
output = craft.output;
local items = craft.items;
for _,item in pairs(items) do
itemlist[item]=(itemlist[item] or 0)+1;
end
cache.item = item;
cache.idx = idx;
cache.itemlist = itemlist;
cache.output = output;
-- loop through robot inventory for those "group" items and see if anything in inventory matches group - then replace
-- group name with that item
local pos = basic_robot.data[name].spawnpos; -- position of spawner block
local inv = minetest.get_meta(pos):get_inventory();
for item,v in pairs(itemlist) do
local k = string.find(item,"group:");
if k then
local group = string.sub(item,k+6);
-- do we have that in inventory?
local size = inv:get_size("main");
for i=1,size do
local itemname = inv:get_stack("main", i):get_name();
local groups = minetest.registered_items[itemname].groups or {};
if groups[group] then cache.itemlist[item] = nil; cache.itemlist[itemname] = v break end
end
end
end
end
--minetest.chat_send_all(item)
--minetest.chat_send_all(dump(itemlist))
if mode == 1 then return itemlist end
-- check if all items from itemlist..
-- craft item
local pos = basic_robot.data[name].spawnpos; -- position of spawner block
local inv = minetest.get_meta(pos):get_inventory();
for item,quantity in pairs(itemlist) do
local stack = ItemStack(item .. " " .. quantity*amount);
if not inv:contains_item("main",stack) then return false end
end
for item,quantity in pairs(itemlist) do
local stack = ItemStack(item .. " " .. quantity*amount);
inv:remove_item("main",stack);
end
inv:add_item("main",ItemStack(output))
return true
end
--pathfinding: find_path, walk_path
basic_robot.commands.find_path = function(name,pos2)
local obj = basic_robot.data[name].obj;
local pos1 = obj:get_pos();
if (pos1.x-pos2.x)^2+(pos1.y-pos2.y)^2+(pos1.z-pos2.z)^2> 50^2 then
return nil,"2: distance too large"
end
check_operations(name,6,true)
local data = basic_robot.data[name]
energy = data.menergy or 0; -- machine energy
if energy<1 then
return nil,"1: not enough energy"
end
data.menergy = energy - 1
--{current pathnode, path}
--start position is very sensitive, noninteger coordinates do not seem to work
local round = math.floor
pos1.x = pos1.x>0 and round(pos1.x+0.5) or -round(-pos1.x+0.5)
pos1.y = pos1.y>0 and round(pos1.y+0.5) or -round(-pos1.y+0.5)
pos1.z = pos1.z>0 and round(pos1.z+0.5) or -round(-pos1.z+0.5)
--TODO: tweak parameters, minetest find_path seems sometimes buggy
local path = minetest.find_path(pos1,pos2,10,1,1,"Dijkstra"); -- pos1,pos2, search_distance, max_jump, max_drop
basic_robot.data[name].pathdata = {1,path}
if path then return #path else return nil end -- return length of found path or nil
end
basic_robot.commands.walk_path = function(name)
check_operations(name,2,true)
local pathdata = basic_robot.data[name].pathdata;
if not pathdata then return nil end
local path = pathdata[2];
if not path then return 0 end
local idx = pathdata[1];
if idx > #path then return 0 end -- reached end of path
local pos2 = path[idx]
local obj = basic_robot.data[name].obj;
local pos1 = obj:get_pos();
local ndist = (pos1.x-pos2.x)^2+(pos1.y-pos2.y)^2+(pos1.z-pos2.z)^2
if ndist> 4 then return -ndist end -- too far away from next node
--turn in correct direction according to movement
pos1.x = pos2.x-pos1.x; pos1.z = pos2.z-pos1.z
local yaw = 0
if math.abs(pos1.x)> math.abs(pos1.z) then
if pos1.x>0 then
yaw = 0;
-- {1,0}
else
yaw = math.pi;
-- {-1,0}
end
else
if pos1.z>0 then
-- {0,1}
yaw = math.pi/2
else
-- {0,-1}
yaw = -math.pi/2
end
end
yaw = yaw - math.pi/2
obj:set_yaw(yaw);
pathdata[1] = idx + 1 -- target next node
obj:moveto(pos2, true)
return #path-idx+1 -- return remaining length of path
end
--FORMS
basic_robot.commands.show_form = function(name, playername, form)
minetest.show_formspec(playername, "robot_form".. name, form)
end
-- handle robots receiving fields
minetest.register_on_player_receive_fields(function(player, formname, fields)
if not string.sub(formname,1,10) == "robot_form" then return end
local name = string.sub(formname,11); -- robot name
if not basic_robot.data[name] then return end
basic_robot.data[name].read_form = fields;
basic_robot.data[name].form_sender = player:get_player_name() or "";
end)
-- ROBOT TECHNIC
-- amount parameter in generate_power, smelt,... is determined by upgrade level
-- it specifies how much energy will be generated :
basic_robot.technic = { -- data cache
fuels = {}, --[fuel] = value
smelts = {}, -- [item] = [cooktime, cookeditem, aftercookeditem]
grinder_recipes = { --[in] ={fuel cost, out, quantity of material required for processing}
["default:stone"] = {2,"default:sand",1},
["default:cobble"] = {1,"default:gravel",1},
["default:gravel"] = {0.5,"default:dirt",1},
["default:dirt"] = {0.5,"default:clay_lump 4",1},
["default:obsidian_shard"] = {199,"default:lava_source",1},
["gloopblocks:basalt"] = {1, "default:cobble",1}, -- enable coble farms with gloopblocks mod
["default:ice"] = {1, "default:snow 4",1},
["darkage:silt_lump"]={1,"darkage:chalk_powder",1},
["default:diamond"] = {16, "basic_machines:diamond_dust_00 2", 1},
["default:ice"] = {1, "default:snow", 1},
["moreores:tin_lump"] = {4,"basic_machines:tin_dust_00 2",1},
["default:obsidian_shard"] = {199, "default:lava_source",1},
["default:mese_crystal"] = {8, "basic_machines:mese_dust_00 2",1},
["moreores:mithril_ingot"] = {16, "basic_machines:mithril_dust_33 2",1},
["moreores:silver_ingot"] = {5, "basic_machines:silver_dust_33 2",1},
["moreores:tin_ingot"] = {4,"basic_machines:tin_dust_00 2",1},
["moreores:mithril_lump"] = {16, "basic_machines:mithril_dust_33 2",1},
["default:steel_ingot"] = {4, "basic_machines:iron_dust_00 2",1},
["moreores:silver_lump"] = {5, "basic_machines:silver_dust_33 2",1},
["default:gold_ingot"] = {6, "basic_machines:gold_dust_00 2", 1},
["default:copper_ingot"] = {4, "basic_machines:copper_dust_00 2",1},
["default:gold_lump"] = {6, "basic_machines:gold_dust_00 2", 1},
["default:iron_lump"] = {4, "basic_machines:iron_dust_00 2",1},
["default:copper_lump"] = {4, "basic_machines:copper_dust_00 2",1},
},
compressor_recipes = { --[in] ={fuel cost, out, quantity of material required for processing}