-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.lua
3161 lines (2739 loc) · 103 KB
/
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
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
-- The anpc API
local _npc = {
dsl = {},
proc = {},
env = {},
obj = {},
move = {},
model = {}
}
npc.model = _npc.model
npc.ANIMATION_STAND_START = 0
npc.ANIMATION_STAND_END = 79
npc.ANIMATION_SIT_START = 81
npc.ANIMATION_SIT_END = 160
npc.ANIMATION_LAY_START = 162
npc.ANIMATION_LAY_END = 166
npc.ANIMATION_WALK_START = 168
npc.ANIMATION_WALK_END = 187
npc.ANIMATION_MINE_START = 189
npc.ANIMATION_MINE_END =198
local program_table = {}
local instruction_table = {}
local hl_task_table = {}
local node_table = {}
local node_group_name_map = {}
local group_to_pos_map = {}
-- Check if `anpc-dev` mod is enabled. If so, make the program table and instruction
-- table accessible from `npc.proc.*`. This enables debugging tools in `anpc_dev`
local mods = minetest.get_modnames()
local is_dev_mode = false
for i = 1, #mods do
if mods[i] == "anpc_dev" then
npc.proc.program_table = program_table
npc.proc.instruction_table = instruction_table
is_dev_mode = true
end
end
local models = {}
-----------------------------------------------------------------------------------
-- DSL Functions
-----------------------------------------------------------------------------------
-- At least left and op are necessary
_npc.dsl.evaluate_boolean_expression = function(self, expr, args)
--minetest.log("Got expr: "..dump(expr))
local operator = expr.op
local source = _npc.dsl.evaluate_argument(self, expr.left, args)
local target = _npc.dsl.evaluate_argument(self, expr.right, args)
if operator == "==" then
return source == target
elseif operator == ">=" then
return source >= target
elseif operator == "<=" then
return source <= target
elseif operator == "~=" then
return source ~= target
elseif operator == "<" then
return source < target
elseif operator == ">" then
return source > target
elseif operator == "+" then
return source + target
elseif operator == "-" then
return source - target
elseif operator == "*" then
return source * target
elseif operator == "/" then
return source / target
elseif operator == "%" then
return source % target
elseif operator == "&&" then
return source and target
elseif operator == "||" then
return source or target
end
end
-- This function parses in-line arguments, mostly used to get/set variables
-- These expressions have to be evaluated at run time, so this function is called at run time only.
-- Supported operations:
-- - Get variables from:
-- - local: local program variables, e.g. @local.var_name
-- - global: global variables, to the NPC, e.g. @global.var_name
-- - args: access the arguments of the current program, e.g. @args.arg_name
-- - You can also access variable using another variable:
-- - @[email protected]_name
-- - Supports tables
-- - Constant table element: @local.tbl_name["some_key_name"]
-- - Variable table element: @local.tbl_name[@local.for_index]
-- - Table length: @local.tbl_name.length
-- - Object expressions:
-- - @objs.all: returns a table with all objects nearby the NPC
-- - @objs.get[<tracking_id>]: returns object with tracking ID = <tracking_id>
-- - @objs.get[@local.for_index]: returns object with index or tracking ID @local.for_index
-- - Time expression:
-- - @time: Returns minetest local time * 24000
_npc.dsl.evaluate_argument = function(self, expr, args, local_vars)
--minetest.log("Got expr: "..dump(expr))
if type(expr) == "string" then
if expr:sub(1,1) == "@" then
local expression_values = string.split(expr, ".")
local storage_type = expression_values[1]
local result = nil
local second_arg = nil
local third_arg = nil
local orig_key = nil
local key = nil
local bracket_start = string.find(expr, "%[")
local bracket_end = string.find(expr, "%]")
if bracket_start ~= nil and bracket_end ~= nil then
local point_start = string.find(expr, "%.")
second_arg = expr:sub(point_start + 1, bracket_start - 1)
orig_key = expr:sub(bracket_start + 1, bracket_end - 1)
if orig_key:sub(1,1) == "@" then
key = _npc.dsl.evaluate_argument(self, orig_key)
else
key = orig_key
end
-- Check if the key is a number - if so, we should use a key of type number
if type(key) == "string" and string.find(key, "^%d+$") ~= nil and string.find(key, "^%d+$") > -1 then
key = tonumber(key)
end
else
if expression_values[2] then
if expression_values[2]:sub(1,1) == "@" then
second_arg = _npc.dsl.evaluate_argument(self, expression_values[2]..expression_values[3])
else
second_arg = expression_values[2]
end
end
end
-- Get third argument, `length` if provided
-- Possible usecases:
-- - @local.my_array.length
if #expression_values > 2 then
if #expression_values == 3 then
third_arg = expression_values[3]
elseif #expression_values == 4 then
third_arg = expression_values[4]
end
end
if storage_type == "@local" then
if self.data.proc[self.process.current.id] then
result = _npc.dsl.get_var(self, second_arg)
end
elseif storage_type == "@args" then
result = self.process.current.args[second_arg]
elseif storage_type == "@global" then
result = self.data.global[second_arg]
elseif storage_type == "@temp" then
result = self.data.temp[second_arg]
elseif storage_type == "@env" then
result = self.data.env[second_arg]
elseif storage_type == "@objs" then
result = self.data.env.objects
-- Supports passing in an index, a tracking ID, a tracking record, or "all"
-- NOTE: This will only work if the NPC is within range of the object
if second_arg == "all" then
result = self.data.env.objects
elseif second_arg == "get" then
-- Return object by number index
if type(key) == "number" then
return self.data.env.objects[key]
else
local obj_key = _npc.dsl.evaluate_argument(self, key)
-- Extract the tracking ID from the tracking record if
-- the expression is a variable containing a tracking record
if type(obj_key) == "table" and obj_key._is_tracking_record == true then
obj_key = obj_key._id
end
for i = 1, #self.data.env.objects do
local obj = self.data.env.objects[i]
if obj then
if obj:is_player() and obj:get_player_name() == obj_key then
self.data.temp[obj_key] = obj
return obj
else
local entity = obj:get_luaentity()
if entity and entity.anpc_track_id == obj_key then
self.data.temp[obj_key] = obj
return obj
end
end
end
end
-- Try on temp data storage
local obj = self.data.temp[obj_key]
if obj then return obj end
end
end
elseif storage_type == "@time" then
return 24000 * minetest.get_timeofday()
-- This might be temporary
elseif storage_type == "@self" then
if expression_values[2] == "pos_rounded" then
return vector.round(self.object:get_pos())
elseif expression_values[2] == "pos" then
return self.object:get_pos()
elseif expression_values[2] == "dir" then
return minetest.yaw_to_dir(self.object:get_yaw())
elseif expression_values[2] == "yaw" then
return self.object:get_yaw()
end
end
-- Check if there's a third argument.
-- Supported third argument:
-- - `length`: if object is a table, returns length of table
--minetest.log("[e] On third argument..."..dump(key))
--minetest.log("[e] Storage type: "..dump(storage_type))
--minetest.log("[e] Result: "..dump(_npc.dsl.get_var(self, second_arg)))
--minetest.log("[e] Second arg: "..dump(second_arg))
--minetest.log("[e] result="..dump(result))
--if type(result) == "table" then minetest.log("[e] result[key]="..dump(result[key])) end
if third_arg then
if third_arg == "length" then
if not result then return -1 end
local count = 0
for _ in pairs(result) do count = count + 1 end
return count
end
end
if key and type(result) == "table" then
return result[key]
else
return result
end
--minetest.log("Expression: "..dump(expression_values))
end
elseif type(expr) == "table" and expr.left and expr.op then
return _npc.dsl.evaluate_boolean_expression(self, expr, args)
elseif type(expr) == "function" then
return expr(self, args, local_vars)
end
return expr
end
-- TODO: Might not be needed
-- TODO: We probably want to remove this to discourage use of in-line Lua
-- on anpcscript programs
npc.eval = function(self, expr, args, local_vars)
return _npc.dsl.evaluate_argument(self, expr, args, local_vars)
end
-- Nil-safe set variable function, plus handling of userdata variables
_npc.dsl.set_var = function(self, key, value, userdata_type, storage_type)
local storage = nil
local subkey = nil
local orig_subkey = key
--minetest.log("Got key: "..dump(key))
--minetest.log("Got value: "..dump(value))
if storage_type == nil and key and key:sub(1,1) == "@" then
local index = string.find(key, "%.")
storage_type = key:sub(2, index - 1)
key = key:sub(index + 1, #key)
elseif storage_type == nil then
-- Assume local storage
storage_type = "local"
end
--minetest.log("Selected storage: "..dump(storage_type))
-- TODO: This doesn't work, I think
-- Support assigning values to array elements
local bracket_start = string.find(key, "%[")
local bracket_end = string.find(key, "%]")
if bracket_start ~= nil and bracket_end ~= nil then
orig_subkey = key:sub(bracket_start + 1, bracket_end - 1)
key = key:sub(1, bracket_start - 1)
if orig_subkey:sub(1,1) == "@" then
subkey = _npc.dsl.evaluate_argument(self, orig_subkey)
else
subkey = orig_subkey
end
end
if storage_type == "global" then
storage = self.data.global
elseif storage_type == "temp" then
storage = self.data.temp
elseif storage_type == "args" then
storage = self.process.current.args
elseif storage_type == "local" then
storage = self.data.proc[self.process.current.id]
if storage == nil then
self.data.proc[self.process.current.id] = {}
storage = self.data.proc[self.process.current.id]
end
end
-- TODO: Fix this hack. We need somehow to understand what kind of userdata
-- value is being passed in. Probably the best idea is to have someway to
-- specify the userdata type in the anpcscript, and the interpreter add the
-- userdata_type argument
-- TODO: Handle better objects at reload?
if not userdata_type then userdata_type = "object" end
if type(value) == "userdata" then
if userdata_type then
if userdata_type == "object" then
local tracking_record = _npc.dsl.generate_tracking_record(self, value)
if subkey and type(storage[key]) == "table" then
storage[key][subkey] = tracking_record
else
storage[key] = tracking_record
end
-- Store actual object so that lookups are simpler
self.data.temp[tracking_record._id] = value
return
else
-- TODO: Handle other userdata types
assert(value.to_table and value.from_table)
value = {userdata_type=userdata_type, value=value.to_table}
end
end
end
-- Handle table with variables
-- Currently, only first-level keys are checked for performance reasons
if type(value) == "table" then
end
if subkey and type(storage[key]) == "table" then
storage[key][subkey] = value
else
storage[key] = value
end
end
_npc.dsl.get_var = function(self, key)
--minetest.log("Data: "..dump(self.data.proc[self.process.current.id]))
if self.data.proc[self.process.current.id] == nil then return nil end
local result = self.data.proc[self.process.current.id][key]
-- TODO: Add handling for other types of userdata
if type(result) == "table" and result.userdata_type == "object" then
if result.obj_type == "player" then
result = minetest.get_player_by_name(result.obj_attr)
elseif result.obj_type == "object" then
-- Check if object is in temp storage
if self.data.temp[result.obj_attr.id] then
return self.data.temp[result.obj_attr.id]
end
-- Check if object is in the current objects
for i = 1, #self.data.env.objects do
if self.data.env.objects[i]
and self.data.env.objects[i]:get_luaentity()
and self.data.env.objects[i]:get_luaentity().anpc_track_id
and self.data.env.objects[i]:get_luaentity().anpc_track_id == result.obj_attr.id then
return self.data.env.objects[i]
end
end
-- Try to search object
local nearby_objs = minetest.get_objects_inside_radius(self.object:get_pos(), result.obj_attr.distance)
for i = 1, #nearby_objs do
if nearby_objs[i]
and nearby_objs[i]:get_luaentity()
and nearby_objs[i]:get_luaentity().anpc_track_id
and nearby_objs[i]:get_luaentity().anpc_track_id == result.obj_attr.id then
result = nearby_objs[i]
break
end
end
-- Not found, return nil
result = nil
end
end
--minetest.log("Actual: "..dump(self.data.proc[self.process.current.id][key]))
--minetest.log("Returning: "..dump(result))
return result
end
_npc.dsl.generate_tracking_record = function(self, obj)
--local obj = value
if obj then
-- Check if player
if obj:is_player() then
-- Store tracking record
self.data.temp[obj:get_player_name()] = obj
return {
_is_tracking_record = true,
_id = obj:get_player_name(),
userdata_type = "object",
obj_type = "player",
obj_attr = obj:get_player_name()
}
elseif obj:get_luaentity() then
-- Generate a tracking ID for entities
local id = "anpc:track:id:"..tostring(math.random(1000, 9999))
if obj:get_luaentity().anpc_track_id then
id = obj:get_luaentity().anpc_track_id
end
obj:get_luaentity().anpc_track_id = id
-- Store tracking record
self.data.temp[id] = obj
return {
_is_tracking_record = true,
_id = id,
userdata_type = "object",
obj_type = "object",
obj_attr = {
id = id,
distance = vector.distance(self.object:get_pos(), obj:get_pos()) * 3
}
}
end
end
end
-----------------------------------------------------------------------------------
-- Scheduling functions
-----------------------------------------------------------------------------------
-- The scheduling functionality is as follows:
-- - A scheduled entry has the following parameters:
-- - earliest start time
-- - latest start time
-- - recurrency type
-- - repeat interval
-- - end time (if repeat interval given, if not given, repeat always)
-- - dependent schedule entry ID
-- - A schedule entry is for a *single* job
-- - A scheduled job will be priority-enqueued (using npc.execute_program)
-- - If this job sets a state process, it needs to state how to do it (e.g.
-- future state vs. immediate state process)
-----------------------------------------------------------------------------------
-- TODO: Support weekly, monthly and yearly recurrencies
npc.schedule.recurrency_type = {
["none"] = "none",
["daily"] = "daily"
}
-- This function adds an entry (as defined above) to the NPC's schedule
-- data.
_npc.proc.schedule_add = function(self, args)
self.data.schedule[#self.data.schedule + 1] = {
program_name = args.program_name,
earliest_start_time = args.earliest_start_time,
latest_start_time = args.latest_start_time,
repeat_interval = args.repeat_interval,
end_time = args.end_time,
dependent_entry_id = args.dependent_entry_id
}
return #self.data.schedule
end
_npc.proc.schedule_remove = function(self, args)
if self.data.schedule[args.entry_id] ~= nil then
self.data.schedule[args.entry_id] = nil
return true
else
return false
end
end
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-- Program and Instruction Registration
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-- Returns an array of instructions
_npc.proc.process_instruction = function(instruction, original_list_size, function_index_map)
local instruction_list = {}
local is_function = false
if instruction.name == "npc:if" then
-- TODO: Remove this as the interpreter now does this
-- Process true instructions if available
--[[
local true_instrs = {}
for i = 1, #instruction.args.true_instructions do
assert(not instruction.args.true_instructions[i].declare,
"Function declaration cannot be done inside another instruction.")
local instrs = _npc.proc.process_instruction(instruction.args.true_instructions[i],
#true_instrs + original_list_size + 1)
for j = 1, #instrs do
true_instrs[#true_instrs + 1] = instrs[j]
end
end
-- Insert jump to skip true instructions if expr is false
local offset = 0
if instruction.args.false_instructions then offset = 1 end
instruction_list[#instruction_list + 1] = {
name = "npc:jump_if",
args = {
expr = instruction.args.expr,
pos = #true_instrs + offset,
negate = true,
offset = true
}
}
-- Insert all true_instructions into result
for j = 1, #true_instrs do
instruction_list[#instruction_list + 1] = true_instrs[j]
end
-- False instructions
if instruction.args.false_instructions then
-- Process false instructions if available
local false_instrs = {}
for i = 1, #instruction.args.false_instructions do
assert(not instruction.args.false_instructions[i].declare,
"Function declaration cannot be done inside another instruction.")
local instrs = _npc.proc.process_instruction(instruction.args.false_instructions[i],
#false_instrs + #instruction_list + original_list_size + 1)
for j = 1, #instrs do
false_instrs[#false_instrs + 1] = instrs[j]
end
end
-- Insert jump to skip false instructions if expr is true
instruction_list[#instruction_list + 1] = {
name = "npc:jump",
args = {
pos = #false_instrs,
offset = true
}
}
-- Insert all false_instructions
for j = 1, #false_instrs do
instruction_list[#instruction_list + 1] = false_instrs[j]
end
end
]]--
elseif instruction.name == "npc:switch" then
for i = 1, #instruction.args.cases do
-- Process each case instructions
local case_instrs = {}
for j = 1, #instruction.args.cases[i].instructions do
assert(not instruction.args.cases[i].instructions[j].declare,
"Function declaration cannot be done inside another instruction.")
local instrs = _npc.proc.process_instruction(instruction.args.cases[i].instructions[j],
#case_instrs + original_list_size + 1)
for k = 1, #instrs do
case_instrs[#case_instrs + 1] = instrs[k]
end
end
instruction_list[#instruction_list + 1] = {
name = "npc:jump_if",
args = {
expr = instruction.args.cases[i].case,
pos = #case_instrs,
negate = true,
offset = true
}
}
-- Insert all case instructions
for j = 1, #case_instrs do
instruction_list[#instruction_list + 1] = case_instrs[j]
end
end
elseif instruction.name == "npc:while" then
-- TODO: Remove this as the interpreter now does this
--[[
-- Support time-based while loop.
-- The loop will execute as many times as possible within the given time.
-- The given time is in seconds, no smaller resolution supported.
if instruction.args.time then
-- Add instruction to start instruction timer
instruction_list[#instruction_list + 1] = {name = "npc:timer:instr:start"}
-- Modify expression
instruction.args.expr = {
left = function(self) return self.timers.instr_timer end,
op = "<=",
right = instruction.args.time
}
end
-- The below will actually set the jump to the instruction previous
-- to the relevant one - this is done because after the jump
-- instruction is executed, the instruction counter will be increased
local loop_start = #instruction_list + original_list_size
-- Insert all loop instructions
for i = 1, #instruction.args.loop_instructions do
assert(not instruction.args.loop_instructions[i].declare,
"Function declaration cannot be done inside another instruction.")
local instrs = _npc.proc.process_instruction(instruction.args.loop_instructions[i],
loop_start)
for j = 1, #instrs do
instruction_list[#instruction_list + 1] = instrs[j]
end
end
-- Insert conditional to loop back if expr is true
instruction_list[#instruction_list + 1] = {
name = "npc:jump_if",
args = {
expr = instruction.args.expr,
pos = loop_start - 1,
negate = false
},
loop_end = true
}
-- Add instruction to stop timer
if instruction.args.time then
instruction_list[#instruction_list + 1] = {name = "npc:timer:instr:stop"}
end
]]--
elseif instruction.name == "npc:for" then
-- TODO: Remove this as the interpreter now does this
--[[
-- Initialize loop variable
instruction_list[#instruction_list + 1] = {
name = "npc:var:set",
args = {
key = "for_index",
value = instruction.args.initial_value
}
}
-- The below will actually set the jump to the instruction previous
-- to the relevant one - this is done because after the jump
-- instruction is executed, the instruction counter will be increased
local loop_start = #instruction_list + original_list_size
minetest.log("On a for loop")
minetest.log(dump(loop_start))
minetest.log(dump(instruction_list))
-- Insert all loop instructions
for i = 1, #instruction.args.loop_instructions do
assert(not instruction.args.loop_instructions[i].declare,
"Function declaration cannot be done inside another instruction.")
local instrs = _npc.proc.process_instruction(instruction.args.loop_instructions[i],
loop_start)
for j = 1, #instrs do
instruction_list[#instruction_list + 1] = instrs[j]
end
end
-- Insert loop variable increase instruction
instruction_list[#instruction_list + 1] = {
name = "npc:var:set",
args = {
key = "for_index",
value = function(self, args)
return self.data.proc[self.process.current.id]["for_index"]
+ instruction.args.step_increase
end
}
}
-- Insert conditional to loop back if expr is true
instruction_list[#instruction_list + 1] = {
name = "npc:jump_if",
args = {
expr = instruction.args.expr,
pos = loop_start - 1,
negate = false
},
loop_end = true
}
]]--
-- TODO: Remove for each?
elseif instruction.name == "npc:for_each" then
--assert(type(instruction.args.array) == "table")
-- Initialize loop variables
instruction_list[#instruction_list + 1] = {
name = "npc:var:set",
args = {
key = "for_index",
value = 1
}
}
instruction_list[#instruction_list + 1] = {
name = "npc:var:set",
args = {
key = "for_value",
value = function(self, args)
local array = _npc.dsl.evaluate_argument(
self, instruction.args.array, args, self.data.proc[self.process.current.id])
return array[1]
end
}
}
-- The below will actually set the jump to the instruction previous
-- to the relevant one - this is done because after the jump
-- instruction is executed, the instruction counter will be increased
local loop_start = #instruction_list + original_list_size
-- Insert all loop instructions
for i = 1, #instruction.args.loop_instructions do
assert(not instruction.args.loop_instructions[i].declare,
"Function declaration cannot be done inside another instruction.")
local instrs = _npc.proc.process_instruction(instruction.args.loop_instructions[i],
loop_start)
for j = 1, #instrs do
instruction_list[#instruction_list + 1] = instrs[j]
end
end
-- Insert loop variable increase instruction
instruction_list[#instruction_list + 1] = {
name = "npc:var:set",
args = {
key = "for_index",
value = function(self, args)
return self.data.proc[self.process.current.id]["for_index"] + 1
end
}
}
instruction_list[#instruction_list + 1] = {
name = "npc:var:set",
args = {
key = "for_value",
value = function(self, args)
local array = _npc.dsl.evaluate_argument(
self, instruction.args.array, args, self.data.proc[self.process.current.id])
return array[self.data.proc[self.process.current.id].for_index]
end
}
}
-- Insert conditional to loop back if expr is true
instruction_list[#instruction_list + 1] = {
name = "npc:jump_if",
args = {
expr = {
left = "@local.for_index",
op = "<=",
right = function(self, args)
return #self.data.proc[self.process.current.id].for_array
end
},
pos = loop_start - 1,
negate = false
},
loop_end = true
}
-- Remove the array and the for-value variables
instruction_list[#instruction_list + 1] =
{name = "npc:var:set", args = {key="for_value", value=nil}}
elseif instruction.name == "npc:wait" then
-- TODO: Remove this as the interpreter now does this
--[[
-- This is not a busy wait, this modifies the interval in two instructions
local wait_time = _npc.dsl.evaluate_argument(self, instruction.args.time, nil, nil)
instruction_list[#instruction_list + 1] =
{key="_prev_proc_int", name="npc:get_proc_interval"}
instruction_list[#instruction_list + 1] =
{name="npc:set_proc_interval", args={wait_time = wait_time, value = function(self, args)
return args.wait_time - self.timers.proc_int
end}}
instruction_list[#instruction_list + 1] =
{name="npc:set_proc_interval", args={value = "@local._prev_proc_int"}}
]]--
elseif not instruction.name and instruction.declare then
-- Store function index in map
local func_index = #instruction_list + original_list_size
function_index_map[instruction.declare] = func_index
minetest.log("Function declaration: "..dump(instruction))
-- Process all instructions
for i = 1, #instruction.instructions do
local instrs = _npc.proc.process_instruction(instruction.instructions[i],
func_index)
for j = 1, #instrs do
instruction_list[#instruction_list + 1] = instrs[j]
end
end
is_function = true
elseif not instruction.name and instruction.call then
-- Validate we are calling an existing instruction
local index = function_index_map[instruction.call]
assert(index, "Function "..instruction.call.." not found")
instruction_list[#instruction_list + 1] =
{name="npc:call", args = {name = instruction.call, index = index, key = instruction.key}}
elseif instruction.name == "npc:timer:register" then
-- Validations
assert(instruction.args.name, "Timer needs a name")
local timer_name = "_timer:"..instruction.args.name
-- Add a return instruction
local timer_func_instr = instruction.args.instructions
timer_func_instr[#timer_func_instr + 1] = {name="npc:return"}
-- Process all instructions, and register function
local all_instructions, timer_func_index = _npc.proc.process_instruction({
declare = timer_name,
instructions = instruction.args.instructions
}, original_list_size, function_index_map)
-- Add all instructions
for i = 1, #all_instructions do
instruction_list[#instruction_list + 1] = all_instructions[i]
end
instruction_list[#instruction_list + 1] = {name="npc:timer:register", args = {
name = instruction.args.name,
interval = instruction.args.interval,
initial_value = instruction.args.initial_value,
times_to_run = instruction.args.times_to_run,
function_index = original_list_size
}}
is_function = true
else
-- Insert the instruction
instruction_list[#instruction_list + 1] = instruction
end
return instruction_list, is_function
end
npc.proc.register_program = function(name, raw_instruction_list, source_location)
if program_table[name] ~= nil then
assert("Program with name "..name.." already exists")
return false
else
-- Interpret program queue
-- Convert if, for and while to index-jump instructions
local instruction_list = {}
local function_table = {}
-- This is zero-based as the initial instruction is always initial_instruction - 1
-- TODO: Really?
-- TODO: Seems like
local initial_instruction = 0
for i = 1, #raw_instruction_list do
-- The following instructions are only for internal use
--assert(raw_instruction_list[i].name ~= "npc:jump",
--"Instruction 'npc:jump' is only for internal use and cannot be explicitly invoked from a program.")
--assert(raw_instruction_list[i].name ~= "npc:jump_if",
--"Instruction 'npc:jump_if' is only for internal use and cannot be explicitly invoked from a program.")
assert(raw_instruction_list[i].name ~= "npc:set_process_interval",
"Instruction 'npc:set_process_interval' is only for internal use and cannot be explicitly invoked from a program.")
assert(raw_instruction_list[i].name ~= "npc:timer:instr:start",
"Instruction 'npc:timer:instr:start' is only for internal use and cannot be explicitly invoked from a program.")
assert(raw_instruction_list[i].name ~= "npc:timer:instr:stop",
"Instruction 'npc:timer:instr:stop' is only for internal use and cannot be explicitly invoked from a program.")
local instructions, is_function = _npc.proc.process_instruction(raw_instruction_list[i],
#instruction_list + 1, function_table)
for j = 1, #instructions do
instruction_list[#instruction_list + 1] = instructions[j]
end
if is_function then
-- Count the number of instructions inside function
initial_instruction = initial_instruction + #instructions
end
end
if initial_instruction == 0 then initial_instruction = 1 end
program_table[name] = {
function_table = function_table,
initial_instruction = initial_instruction,
instructions = instruction_list
}
minetest.log("Source location: "..dump(source_location))
if source_location then
program_table[name]["source_file"] = source_location
end
minetest.log("Registered program "..dump(name).." with initial instruction: "..dump(initial_instruction)..":")
--minetest.log(dump(program_table[name]))
return true
end
end
npc.proc.register_instruction = function(name, instruction)
if instruction_table[name] ~= nil then
assert("Instruction \""..name.."\" already exists.")
return false
else
instruction_table[name] = instruction
return true
end
end
npc.proc.register_low_latency_task = function(name, handler, timeout_handler)
if hl_task_table[name] ~= nil then
assert("Low-latency \""..name.."\" already exists.")
return false
else
hl_task_table[name] = {
handler = handler,
timeout_handler = timeout_handler
}
end
end
-- Parameters:
-- name: Name of the node, same as when the node is registered with 'minetest.register_node'
-- categories: Array of tags or categories, that classifies nodes together
-- properties: An array of functions in the following format:
-- {
-- [property_name] = function(self, args)
-- }
-- operation: A function that is called when the instruction 'npc:env:node:operate' is executed
-- the function is given two parameters: 'self', and a table of arguments 'args'
npc.env.register_node = function(name, groups, properties, operation)
if node_table[name] ~= nil then
return false
else
node_table[name] = {
groups = groups,
properties = properties,
operation = operation
}
return true
end
-- Insert into group_to_name_map - this is used when searching
-- for nodes of a specific group
for i = 1, #groups do
-- Create group if it doesn't exists
if not node_group_name_map[groups[i]] then
node_group_name_map[groups[i]] = {}
end
node_group_name_map[groups[i]][#node_group_name_map[groups[i]] + 1] = name
end
end
-- Parameters:
-- The "animation_name" parameter is the name of the animation
-- The object "animation_params" is like this:
-- {
-- start_frame: integer, required, the starting frame of the animation of the blender model
-- end_frame: integer, required, the ending frame of the animation of the blender model
-- speed: integer, required, the speed in which the animation will be played
-- blend: integer, optional, animation blend is broken, defaults to 0
-- loop: boolean, optional, default is true. If false, should specify "animation_after"
-- animation_after: string, optional, default is "stand". Name of animation that will be played
-- when this animation is over.
-- }
npc.model.register_animation = function(model_name, animation_name, animation_params)
-- Initialize if not present
if (not models[model_name]) then
models[model_name] = {
animations = {}
}
end
models[model_name].animations[animation_name] = animation_params
end
-----------------------------------------------------------------------------------