-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.lua
1447 lines (1288 loc) · 63.4 KB
/
init.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
--*** version 0.31 ***
simple_dialogs = { }
local S = simple_dialogs.intllib --TODO: ensure integration with intllib is working properly, I dont think it is now
-- simple dialogs by Kilarin
local contextctr = {}
local contextdlg = {}
local chars = {}
chars.topic="="
chars.reply=">"
chars.varopen="@["
chars.varclose="]@"
local max_goto_depth=3 --TODO:move these to config
local helpfile=minetest.get_modpath("simple_dialogs").."/simple_dialogs_help.txt"
local registered_varloaders={}
local registered_hooks={}
--##################################################################################
--# Translations #
--##################################################################################
-- Check for translation method
local S
if minetest.get_translator ~= nil then
S = minetest.get_translator("simple_dialogs") -- 5.x translation function
else
if minetest.get_modpath("intllib") then
dofile(minetest.get_modpath("intllib") .. "/init.lua")
if intllib.make_gettext_pair then
gettext, ngettext = intllib.make_gettext_pair() -- new gettext method
else
gettext = intllib.Getter() -- old text file method
end
S = gettext
else -- boilerplate function
S = function(str, ...)
local args = {...}
return str:gsub("@%d+", function(match)
return args[tonumber(match:sub(2))]
end)
end
end
end
simple_dialogs.intllib = S
--##################################################################################
--# Methods used when integrating simple_dialogs with an entity mod #
--##################################################################################
--this should be used by your entity mod to load variables that you want to be available for dialogs
--example:
-- simple_dialogs.register_varloader(function(npcself,playername)
-- simple_dialogs.load_dialog_var(npcself,"NPCNAME",npcself.nametopic)
-- simple_dialogs.load_dialog_var(npcself,"STATE",npcself.state)
-- simple_dialogs.load_dialog_var(npcself,"FOOD",npcself.food)
-- simple_dialogs.load_dialog_var(npcself,"HEALTH",npcself.food)
-- simple_dialogs.load_dialog_var(npcself,"owner",npcself.owner)
-- end)--register_on_leaveplayer
function simple_dialogs.register_varloader(func)
registered_varloaders[#registered_varloaders+1]=func
end
--register hook
function simple_dialogs.register_hook(func)
registered_hooks[#registered_hooks+1]=func
end
----------------------------------------------------------------------------------------
-- the dialog control formspec is where an owner can create a dialog for an npc --
-- NOT to be confused with the actual dialog formspec! where someone talks to the npc --
----------------------------------------------------------------------------------------
--this creates and displays an independent dialog control formspec
--dont use this if you are trying to integrate dialog controls with another formspec
function simple_dialogs.show_dialog_controls_formspec(playername,npcself)
contextctr[playername]=simple_dialogs.set_npc_id(npcself) --store the npc id in local context so we can use it when the form is returned. (cant store self)
-- Make blank formspec
local formspec = {
"formspec_version[4]",
"size[15,7]",
}
--add the dialog controls to the above blank formspec
simple_dialogs.add_dialog_control_to_formspec(playername,npcself,formspec,0.375,0.375)
formspec=table.concat(formspec, "")
minetest.show_formspec(playername, "simple_dialogs:dialog_controls", formspec )
end --show_dialog_controls_formspec
--this adds the dialog controls to an existing formspec, so if you already have a control formspec
--for the npc, then use this to add the dialog controls to that formspec
--if you use then, then you will need to add process_simple_dialog_control_fields to the
--register_on_player_receive_fields function for the formspec to process the buttons when pushed
--I THINK this should work if your formspec is a string instead of a table, but I haven't tested that yet.
--TODO: allow control of width?
function simple_dialogs.add_dialog_control_to_formspec(playername,npcself,formspec,x,y)
local dialogtext=""
if npcself.dialog and npcself.dialog.text then dialogtext=npcself.dialog.text end
local x2=x
local y2=y+5
local x3=x2+2
local x4=x3+2
local x5=x4+3.5
local formspecstr=""
local passedInString="NO"
if type(formspec)=="string" then
formspecstr=formspec
formspec={}
passedInString="YES"
end
formspec[#formspec+1]="textarea["..x..","..y..";14,4.8;dialog;"..S("Dialog")..";"..minetest.formspec_escape(dialogtext).."]"
formspec[#formspec+1]="button["..x2..","..y2..";1.5,0.8;dialoghelp;"..S("Dialog Help").."]"
formspec[#formspec+1]="button["..x3..","..y2..";1.5,0.8;save;"..S("Save").."]"
formspec[#formspec+1]="button["..x4..","..y2..";3,0.8;saveandtest;"..S("Save & Test (will reset variables)").."]"
formspec[#formspec+1]="button["..x5..","..y2..";3,0.8;test;"..S("Test (without save or reseting variables)").."]"
if passedInString=="YES" then
return formspecstr..table.concat(formspec)
end
end --add_dialog_control_to_formspec
--if you used add_dialog_control_to_formspec to add the dialog controls to an existing formspec,
--then use THIS in your register_on_player_receive_fields function
--it will process the save, saveandtest and dialog help buttons.
function simple_dialogs.process_simple_dialog_control_fields(playername,npcself,fields)
--minetest.log("simple_dialogs->psdcf fields="..dump(fields))
if fields["save"] or fields["saveandtest"] then
simple_dialogs.load_dialog_from_string(npcself,fields["dialog"])
end --save or saveandtest
if fields["saveandtest"] or fields["test"] then
simple_dialogs.show_dialog_formspec(playername,npcself,"START")
elseif fields["dialoghelp"] then
--minetest.log("simple_dialogs->psdcf help")
simple_dialogs.dialog_help(playername)
end
end --process_simple_dialog_control_fields
--this function lets you load a dialog for an npc from a file. So you can store predetermined dialogs
--as text files and load them for special npc or types of npcs (pirates, villagers, blacksmiths, guards, etc)
--we take modname as a parameter because you might have dialogs in a different mod that uses this mod
function simple_dialogs.load_dialog_from_file(npcself,modname,dialogfilename)
local file = io.open(minetest.get_modpath(modname).."/"..dialogfilename)
if file then
local dialogstr=file:read("*all")
file:close()
simple_dialogs.load_dialog_from_string(npcself,dialogstr)
end
end --load_dialog_from_file
------------------------------------------------------------------------
-- the dialog formspec is the formspec where someone talks to the npc --
------------------------------------------------------------------------
--this will be used to display the actual dialog to a player interacting with the npc
--normally displayed to someone who is NOT the entity owner
--call with topic=START for starting a dialog, or with no topic and it will default to start.
function simple_dialogs.show_dialog_formspec(playername,npcself,topic)
--only show the dialog formspec if there is a dialog
if npcself and npcself.dialog and npcself.dialog.dlg and npcself.dialog.text and npcself.dialog.text~="" then
if not topic then topic="START" end
contextdlg[playername]={}
contextdlg[playername].npcId=simple_dialogs.set_npc_id(npcself) --store the npc id in local context so we can use it when the form is returned. (cant store self)
local formspec={
"formspec_version[4]",
"size[28,15]",
"position[0.05,0.05]",
"anchor[0,0]",
"no_prepend[]", --must be present for below transparent setting to work
"bgcolor[;neither;]", --make the formspec background transparent
"box[0.370,0.4;9.6,8.4;#222222FF]", --draws a box background behind our text area
simple_dialogs.dialog_to_formspec(playername,npcself,topic)
}
formspec=table.concat(formspec,"")
minetest.show_formspec(playername,"simple_dialogs:dialog",formspec)
end
end --show_dialog_formspec
--##################################################################################
--# convert string input into Dialog table #
--##################################################################################
--[[
this is where the whole dialog structure is created.
A typical dialog looks like this:
===Start
Hello, welcome to Caldons tower of fun!
>caldon:Who is Caldon?
>name:Who are you?
>directions:How do I get into the tower?
topics start with = in pos 1 and can look like ===Start or =Treasure(5) (any number of ='s are ok as long as there is 1 in pos 1)
a number in parenthesis after the topic name is a "weight" for that entry, which is only used when you have multiple topics
with the same name and effects how frequently each is chosen.
weight is optional and defaults to 1.
you can have multiple topics with the same name, each gets a number, "subtopic",
when you reference that topic one of the multiple results will be chosen randomly
topics can only contain letters, numbers, underscores, and dashes, all other characters are stripped (letters are uppercased)
After the topic is the "say", this is what the npc says for this topic.
Replies start with > in position 1, and are followed by a target and a colon. The target is the "topic" this replay takes you to.
the reply follows the colon
You can also add commands, command start with a : in position 1
possible commands are:
:set varname=value
:if (a==b) then set varname=value
:if ( ((a==b) and (c>d)) or (e<=f)) then set varname=value
note that :if requires that the condition be in parenthesis.
The final structure of the dialog table will look like this:
npcself.dialog.vars (variable values for this npc)
npcself.dialog.text (the unprocessed dialog string)
npcself.dialog.
dlg[topic][subtopic].weight (the weight for this subtopic when chosen by random)
dlg[topic][subtopic].say (the text of the dialog that the npc says)
dlg[topic][subtopic].reply[replycount].target (what topic this reply will go to)
dlg[topic][subtopic].reply[replycount].text (the text of the reply)
dlg[topic][subtopic].cmnd[cmndcount].cmnd (SET or IF)
dlg[topic][subtopic].cmnd[cmndcount].cmnd=SET
dlg[topic][subtopic].cmnd[cmndcount].varname (variable name to be set)
dlg[topic][subtopic].cmnd[cmndcount].varval (value to set the variable to)
dlg[topic][subtopic].cmnd[cmndcount].cmnd=IF
dlg[topic][subtopic].cmnd[cmndcount].condstr (the condition string, a==b etc, must be in parens)
dlg[topic][subtopic].cmnd[cmndcount].ifcmnd.cmnd (SET for now, GOTO later?, entire structure of subcommand will be here)
dlg[topic][subtopic].gototopic.topic (used by goto to indicate which topic to goto)
dlg[topic][subtopic].gototopic.count (goto depth count, used to ensure you can not get into an infinite goto loop)
--]]
function simple_dialogs.load_dialog_from_string(npcself,dialogstr)
npcself.dialog = {}
npcself.dialog.dlg={}
npcself.dialog.vars = {}
--local dlg=npcself.dialog.dlg --shortcut to make things more readable
--this function was too long and complicated, so I broke it up into sections
--the table wk is passed to each sub function as a common work area
local wk={}
wk.topic = ""
wk.subtopic=1
wk.weight=1
wk.dlg=npcself.dialog.dlg
--under some very unusual circumstances, a string can be pasted that has single /r
--line endings instead of the standard windows /r/n or linux /n line endings.
--this code checks for that, and if it finds /r without /n it replaces /r with /n
--so the gmatch will work to find line endings
local r=string.find(dialogstr,"\r")
local n=string.find(dialogstr,"\n")
if r and n==nil then --single \r line feeds were found
dialogstr=dialogstr:gsub("\r", "\n") --replace all single \r with \n
end
--loop through each line in the string (including blank lines)
--minetest.log("simple dialogs->ldfs dialogstr="..dialogstr)
for line in (dialogstr..'\n'):gmatch'(.-)\r?\n' do
--minetest.log("simple_dialogs->ldfs line="..line)
wk.line=line
local firstchar=string.sub(wk.line,1,1)
--minetest.log("simple_dialogs->ldfs firstchar="..firstchar.." #firstchar="..#firstchar)
if firstchar == chars.topic then --we found a topic, process it
simple_dialogs.load_dialog_topic(wk)
elseif wk.topic ~="" then --don't do anything until we actually have a topic.
if firstchar == chars.reply then --we found a reply, process it
simple_dialogs.load_dialog_reply(wk)
elseif firstchar==":" and #wk.line>1 then --commands
--minetest.log("simple_dialogs->ldfs : line="..wk.line)
local newcmnd=simple_dialogs.load_dialog_cmnd(string.sub(wk.line,2))
if newcmnd then
local cmndcount=#wk.dlg[wk.topic][wk.subtopic].cmnd+1
wk.dlg[wk.topic][wk.subtopic].cmnd[cmndcount]=newcmnd
end --if newcmnd
--we check that replycount=0 because we are going to ignore any text between the replies and the next topic
elseif #wk.dlg[wk.topic][wk.subtopic].reply==0 then --we found a dialog line, process it
wk.dlg[wk.topic][wk.subtopic].say=wk.dlg[wk.topic][wk.subtopic].say..wk.line.."\n"
end --if firstchar == chars.reply
end --if firstchar == chars.topic
end --for line in dialog
--now double check that every entry has at least 1 reply
for t,v in pairs(wk.dlg) do
for st=1,#wk.dlg[t],1 do
--I could also FORCE an end topic onto every replylist that didn't have one. consider that in the future.
if not wk.dlg[t][st].reply or not wk.dlg[t][st].reply[1] then
wk.dlg[t][st].reply={}
wk.dlg[t][st].reply[1]={}
wk.dlg[t][st].reply[1].target="END"
wk.dlg[t][st].reply[1].text="END"
end --if
end --for st
end --for t
npcself.dialog.text=dialogstr
--minetest.log("simple_dialogs->ldfs end dlg="..dump(wk.dlg))
end --load_dialog_from_string
--this function is used to load a topic into the dialog table in load_dialog_from_string
--wk is our working area variables.
--topics will be in the form of
--=topicname(weight)
--weight is optional, and there can be any number of equal signs
function simple_dialogs.load_dialog_topic(wk)
--strip topic down to only allowed characters
wk.topic=simple_dialogs.topic_filter(wk.line) --strips weight and also strips all leading = signs
wk.weight=1 --default weight
--get the weight from parenthesis
local grouping=simple_dialogs.build_grouping_list(wk.line,"(",")") --find parenthesis
--minetest.log("simple_dialogs->ldt wk.===line="..wk.line.." grouping.first="..grouping.first.."<")
if grouping.first>0 then --we found a parentesis pair!
local w=simple_dialogs.grouping_section(grouping,grouping.first,"EXCLUSIVE") --get the value in the first parenthesis
wk.weight=tonumber(w)
--minetest.log("simple_dialogs->ldt weight wk.line="..wk.line.." w="..dump(w).."<")
--wk.weight~=wk.weight checks for nan (not a number)
if wk.weight==nil or wk.weight<1 or wk.weight~=wk.weight then wk.weight=1 end
end --if grouping.first>0
--minetest.log("simple_dialogs->ldt weight wk.line="..wk.line.." wk.weight="..wk.weight.."<")
wk.subtopic=1
if wk.dlg[wk.topic] then --existing topic
--minetest.log("simple_dialogs->ldt topic="..wk.topic.." subtopic="..wk.subtopic)
wk.subtopic=#(wk.dlg[wk.topic])+1
wk.weight=wk.dlg[wk.topic][wk.subtopic-1].weight+wk.weight --add previous weight to current weight
--weight is always the maximum number rolled that returns this subtopic
--TODO: further notes on weight? here or in readme?
else --if this is a new topic
wk.dlg[wk.topic]={}
end
wk.dlg[wk.topic][wk.subtopic]={}
wk.dlg[wk.topic][wk.subtopic].say=""
wk.dlg[wk.topic][wk.subtopic].weight=wk.weight
wk.dlg[wk.topic][wk.subtopic].reply={}
wk.dlg[wk.topic][wk.subtopic].cmnd={}
--minetest.log("simple_dialogs->ldt final wk.dlg["..wk.topic.."]["..wk.subtopic.."]="..dump(wk.dlg[wk.topic][wk.subtopic]))
end --load_dialog_topic
--this function is used to load a REPLY into the dialog table in load_dialog_from_string
--wk is our working area variables.
--replies will be in the form of
-->target:replytext
--target is the topic we will go to if this reply is clicked
--replytext is the text that will be shown for the reply
function simple_dialogs.load_dialog_reply(wk)
--split into target and reply
local i, j = string.find(wk.line,":")
if i==nil then
i=string.len(wk.line)+1 --if they left out the colon, treat the whole line as the topic
end
local replycount=#wk.dlg[wk.topic][wk.subtopic].reply+1
wk.dlg[wk.topic][wk.subtopic].reply[replycount]={}
--TODO: use variables for targets, filter later, not here?
wk.dlg[wk.topic][wk.subtopic].reply[replycount].target=simple_dialogs.topic_filter(string.sub(wk.line,2,i-1))
--the match below removes leading spaces
wk.dlg[wk.topic][wk.subtopic].reply[replycount].text=string.match(string.sub(wk.line,i+1),'^%s*(.*)')
if wk.dlg[wk.topic][wk.subtopic].reply[replycount].text=="" then
wk.dlg[wk.topic][wk.subtopic].reply[replycount].text=string.sub(wk.line,2,i-1)
end
end --load_dialog_reply
--this will create a command from the dialog input string, ready to be loaded into the dialog input table.
--it is called by both load_dialog_from_string and also by load_dialog_cmnd_if (to load ifcmnd)
--do not pass in the leading colon
function simple_dialogs.load_dialog_cmnd(line)
--minetest.log("simple_dialogs->ldc line="..line)
local newcmnd=nil
local spc=string.find(line," ",1)
if spc then
local cmndname=string.upper(string.sub(line,1,spc-1))
local str=simple_dialogs.trim(string.sub(line,spc+1)) --rest of line without the command
if not str then str="" end
--minetest.log("simple_dialogs->ldc cmnd="..cmndname.." str="..str)
if cmndname=="SET" then
newcmnd=simple_dialogs.load_dialog_cmnd_set(str)
elseif cmndname=="IF" then
newcmnd=simple_dialogs.load_dialog_cmnd_if(str)
elseif cmndname=="GOTO" then
newcmnd={}
newcmnd.cmnd="GOTO"
newcmnd.topic=simple_dialogs.topic_filter(str)
elseif cmndname=="HOOK" then
--:hook teleport -500,3,-80
newcmnd={}
newcmnd.cmnd="HOOK"
local spc2=string.find(str," ",1)
--minetest.log("simple_dialogs->ldc hook str="..str.." spc2="..spc2)
if spc2 then
newcmnd.func=string.upper(string.sub(str,1,spc2-1))
newcmnd.str=simple_dialogs.trim(string.sub(str,spc2+1))
newcmnd.parm={}
local c=0
--now break the rest of the command into parms, if possible
for word in string.gmatch(newcmnd.str, '([^,]+)') do
c=c+1
newcmnd.parm[c]=word
end
newcmnd.parmcount=c
end --if spc2
--minetest.log("simple_dialogs->ldc hook="..dump(newcmnd))
end --if cmndname
end --if spc
--minetest.log("simple_dialogs->ldc newcmnd="..dump(newcmnd))
return newcmnd
end --load_dialog_cmnd
--this function is used to load a SET cmnd into the dialog table in load_dialog_from_string and in load_dialog_if
--str is the string after the :set and should be in the format of varname=varval
--returns a cmnd in format of:
--cmnd.cmnd="SET"
--cmnd.varname=variablename
--cmnd.varval=value to set variable to
function simple_dialogs.load_dialog_cmnd_set(str)
local cmnd=nil
local eq=string.find(str,"=")
if eq then
--minetest.log("simple_dialogs->ldcs eq")
local varname=string.sub(str,1,eq-1)
local varval=string.sub(str,eq+1)
--minetest.log("simple_dialogs->ldcs varname="..varname.." varval="..varval)
if varval then
cmnd={}
cmnd.cmnd="SET"
cmnd.varname=varname
cmnd.varval=varval
---minetest.log("simple_dialogs->ldcs after dlg["..topic.."]["..subtopic.."].cmnd="..dump(dlg[topic][subtopic].cmnd))
--note that we have NOT populated any vars at that point, that happens when the dialog is actually displayed
end --if varval
end --if eq
return cmnd
end --load_dialog_cmnd_set
--this function is used to load an IF cmnd into the dialog table in load_dialog_from_string
--str is the string after the :if
--if must have all if conditions enclosed in one paren group, even single condition must be in parens
--if (condition) then
--if ((condition) and (condition) or (condition)) then
--yes, this has a recursive call to load_dialog_cmnd. It should NOT cause problems because it can
--only be built from the string that was passed in. there is no way to fall into an infinate recursive loop.
--function simple_dialogs.load_dialog_cmnd_if(wk,str)
function simple_dialogs.load_dialog_cmnd_if(str)
--minetest.log("simple_dialogs->ldci top str="..str)
local cmnd=nil
local grouping=simple_dialogs.build_grouping_list(str,"(",")")
if grouping.first>0 then --find " THEN " after the last close paren
local t=string.find(string.upper(str)," THEN ",grouping.list[grouping.first].close)
if t then
--minetest.log("simple_dialogs->ldci t="..t)
cmnd={}
cmnd.cmnd="IF"
cmnd.condstr=string.sub(str,1,t-1)
local thenstr=simple_dialogs.trim(string.sub(str,t+6)) --trim ensures no leading spaces
cmnd.ifcmnd=simple_dialogs.load_dialog_cmnd(thenstr)
--minetest.log("simple_dialogs->ldci cmnd="..dump(cmnd))
end --if t
end --if grouping.first
return cmnd
end --load_dialog_cmnd_if
--[[ *******************************************************************************
convert Dialog table into a formspec
--]]
--this is kind of an awkward solution for handling gotos, but it works.
--the meat of the formspec creation happens in dialog_to_formspec_inner
--but if that process hits a "goto", then it increments gototopic.count
--and if gototopic.count<4 it sets gototopic.topic and returns.
--then this function calls dialog_to_formspec_inner AGAIN with the new topic.
--if gototopic>=4 then we ignore it. This prevents any possibility of an eternal loop
function simple_dialogs.dialog_to_formspec(playername,npcself,topic)
--minetest.log("simple_dialogs->dtf top npcself.dialog="..dump(npcself.dialog))
--first we make certain everything is properly defined. if there is an error we do NOT want to crash
--but we do return an error message that might help debug.
local errlabel="label[0.375,0.5; ERROR in dialog_to_formspec, "
if not npcself then return errlabel.." npcself not found]"
elseif not npcself.dialog then return errlabel.." npcself.dialog not found]"
elseif not topic then return errlabel.." topic passed was nil]"
end
npcself.dialog.gototopic={}
local gototopic=npcself.dialog.gototopic
gototopic.count=0
gototopic.topic=topic --because this is where we are going first, will get changed and pop out if we hit a goto
local formspec
repeat
--this check has to be inside the repeat to catch topics changed by goto
if gototopic.topic and gototopic.topic=="END" then
minetest.close_formspec(playername, "simple_dialogs:dialog")
return ""
elseif not npcself.dialog.dlg[topic] then return errlabel.. " topic "..topic.." not found in the dialog]"
end
--minetest.log("simple_dialogs->dtf before")
formspec=simple_dialogs.dialog_to_formspec_inner(playername,npcself)
--minetest.log("simple_dialogs->dtf after gototopic="..dump(gototopic))
until not gototopic.topic
return formspec
end
--[[
this is the other side of load_dialog_from_string. dialog_to_formspec turns a dialog table into
a formspec with the say text and reply list.
this is when variables are substituted, functions executed, and commands run.
a quick note on weight. the weight number for each subtopic is the maximum weight for that topic.
So, for example, if you have three treasure topics like this
=Treasure(2)
=Treasure(4)
=Treasure(7)
you will get weights like this:
dlg[Treasure][1].weight=2
dlg[Treasure][2].weight=6 (2+4=6)
dlg[Treasure][3].weight=13 (6+7=13)
this means we can just roll a random number between 1 and 13,
then select the first subtopic for which our random number is less than or equal to its weight.
--]]
function simple_dialogs.dialog_to_formspec_inner(playername,npcself)
--minetest.log("simple_dialogs->dtf playername="..playername)
--minetest.log("simple_dialogs->dtf: npcself="..dump(npcself))
local dlg=npcself.dialog.dlg --shortcut to make things more readable
local topic=npcself.dialog.gototopic.topic
npcself.dialog.gototopic.topic=nil --will be set again if we hit a goto
--load any variables from calling mod
for f=1,#registered_varloaders do
--minetest.log("simple_dialogs-> dtfi loading varloaders")
registered_varloaders[f](npcself,playername)
end
local formspec={}
--how many matching topics (subtopics) are there (for example, if there are 3 "TREASURE" topics)
local subtopicmax=#dlg[topic]
--get a random number between 1 and the max weight
local rnd=math.random(dlg[topic][subtopicmax].weight)
--subtopic represents which topic was chosen when you had repeated topics
local subtopic=1
--we loop through all the matching topics and select the first one for which our random number
--is less than or equal to that topics weight.
for st=1,subtopicmax,1 do
--minetest.log("simple_dialogs->dtf t="..t.." rnd="..rnd.." topic="..topic.." subtopicmax="..subtopicmax.." weight="..dlg[topic][t].weight)
if rnd<=dlg[topic][st].weight then
subtopic=st
break
end
end
--now subtopic equals the selected subtopic
--minetest.log("simple_dialogs->dtf topic="..topic.." subtopic="..subtopic)
--minetest.log("simple_dialogs->dtf before formspec npcself.dialog="..dump(npcself.dialog))
--
--very first, run any commands
--minetest.log("simple_dialogs->dtf topic="..topic.." subtopic="..subtopic)
--minetest.log("simple_dialogs->dtf dlg["..topic.."]["..subtopic.."]="..dump(dlg[topic][subtopic]))
for c=1,#dlg[topic][subtopic].cmnd do
--minetest.log("simple_dialogs->dtf c="..c.." cmnd="..dump(dlg[topic][subtopic].cmnd[c]))
simple_dialogs.execute_cmnd(npcself,dlg[topic][subtopic].cmnd[c],playername)
if npcself.dialog.gototopic.topic then
--minetest.log("simple_dialogs->dtfi topic set:"..npcself.dialog.gototopic.topic)
return ""
end
end --for c
--
--populate the say portion of the dialog, that is simple.
local say=dlg[topic][subtopic].say
say=simple_dialogs.populate_vars_and_funcs(npcself,say,playername)
if not say then say="" end
--
--now get the replylist
local replies=""
for r=1,#dlg[topic][subtopic].reply,1 do
if r>1 then replies=replies.."," end
local rply=dlg[topic][subtopic].reply[r].text
--minetest.log("simple_dialogs->dtfsi reply rply bfr="..rply)
rply=simple_dialogs.populate_vars_and_funcs(npcself,rply,playername)
--minetest.log("simple_dialogs->dtfsi reply rply aft="..rply)
--if string.len(rply)>70 then rply=string.sub(rply,1,70)..string.char(10)..string.sub(rply,71) end tried wrapping, it doesn't work well.
replies=replies..minetest.formspec_escape(rply)
--minetest.log("simple_dialogs->dtfsi reply rply fnl="..rply)
end --for
--
local x=0.45
local y=0.5
local x2=0.375
local y2=y+8.375
formspec={
"textarea["..x..","..y..";9.4,8;;;"..minetest.formspec_escape(say).."]",
"textlist["..x2..","..y2..";27,5;reply;"..replies.."]" --note that replies were escaped as they were added
}
--store the topic and subtopic in context as well
contextdlg[playername].topic=topic
contextdlg[playername].subtopic=subtopic
return table.concat(formspec,"")
end --dialog_to_formspec
--you pass in a cmnd table, and this will execute the command.
--this is called from dialog_to_formspec, but it is ALSO called recursively on if,
--because if the if condition is met, we then execute the ifcmnd
--for the structure of each cmnd table, check the documentation on load_dialog_from_string
function simple_dialogs.execute_cmnd(npcself,cmnd,playername)
--minetest.log("simple_dialogs->ec cmnd="..dump(cmnd))
--local dlg=npcself.dialog.dlg
if cmnd then
if cmnd.cmnd=="SET" then
--minetest.log("simple_dialogs ec set cmnd="..dump(cmnd))
simple_dialogs.save_dialog_var(npcself,cmnd.varname,cmnd.varval,playername) --load the variable (varname filtering and populating vars happens inside this method)
--minetest.log("simple_dialogs ec set after vars="..dump(npcself.dialog.vars))
elseif cmnd.cmnd=="IF" then
simple_dialogs.execute_cmnd_if(npcself,cmnd,playername)
elseif cmnd.cmnd=="GOTO" then
local gototopic=npcself.dialog.gototopic
gototopic.count=gototopic.count+1
--we only goto the new topic if we have not exceeded depth, and the topic exists.
if gototopic.count<=max_goto_depth and npcself.dialog.dlg[cmnd.topic] then --gototopic.count guarantees no infinate goto loops
gototopic.topic=cmnd.topic
return ""
end --if gototopic.count
elseif cmnd.cmnd=="HOOK" then
--minetest.log("simple_dialogs->ec hook registered_hooks="..#registered_hooks)
for f=1,#registered_hooks do
local rtn=registered_hooks[f](npcself,playername,cmnd)
--minetest.log("simple_dialogs->ec hook rtn="..dump(rtn))
if rtn and rtn=="EXIT" then
npcself.dialog.gototopic.topic="END"
return ""
end --if rtn
end --for
end --if cmnd.cmnd=
end --if cmnd exists
end --execute_cmnd
--this executes an :if command, run during dialog_to_formspec
--pass dlg[topic][subtopic].cmnd[c]
--cmnd.cmnd="IF"
--cmnd.condstr This will be the condition, example: ( ( (hitpoints<10) and (name=="badguy") ) or (status=="asleep") )
--cmnd.ifcmnd This is the command that will be executed if condstr evaluates as true. entire structure of subcommand will be here
--yes, this makes a recursive call, the ifcmnd can even be another if statement.
--BUT, there should be no danger of infinite recursion, because the cmnd structure can NOT be altered during processing.
--so there will always be a finite depth to the recursion.
function simple_dialogs.execute_cmnd_if(npcself,cmnd,playername)
--minetest.log("simple_dialogs->eci if cmnd="..dump(cmnd))
--first thing, populate any vars and run any functions in the condition string
local condstr=simple_dialogs.populate_vars_and_funcs(npcself,cmnd.condstr,playername)
--minetest.log("simple_dialogs->eci condstr="..condstr)
--minetest.log("simple_dialogs->eci vars="..dump(npcself.dialog.vars))
local ifgrouping=simple_dialogs.build_grouping_list(condstr,"(",")")
for i=1,#ifgrouping.list,1 do
local condsection=simple_dialogs.grouping_section(ifgrouping,i,"EXCLUSIVE") --one paren bounded section of condstr
local op=simple_dialogs.split_on_operator(condsection) --gives op.left, op.operator, op.right. op.output
--split_on_operator ALWAYS returns an op table, no matter what the input, so no need to check if op exists
--minetest.log("simple_dialog->eci if op="..dump(op))
condstr=simple_dialogs.grouping_replace(ifgrouping,i,op.output,"EXCLUSIVE")
--minetest.log("simple_dialogs->ecir if left="..op.left.."| operator="..op.operator.." right="..op.right.."| output="..op.output.." condstr="..condstr)
end --for
--minetest.log("simple_dialogs->gdtar if before calc cond="..condstr)
--at this point we should be down to nothing but zeros and ones, and AND and ORs
--replace AND with * and OR with + and we have a mathematical equation that will resolve the boolean logic.
condstr=string.gsub(string.upper(condstr),"AND","*")
condstr=string.gsub(string.upper(condstr),"OR","+")
--minetest.log("simple_dialogs->eci if and or subst cond="..condstr)
--run the string through our sandboxed and filtered math function
local ifrslt=simple_dialogs.sandboxed_math_loadstring(condstr)
--minetest.log("simple_dialogs->eci if after calc ifrslt="..ifrslt)
--now if ifrslt=0 test failed. if ifrslt>0 test succeded
if ifrslt>0 then
--if cmnd.ifcmnd.cmnd=="SET" then
-- --minetest.log("simple_dialogs->eci if executing set")
-- simple_dialogs.execute_cmnd_set(npcself,cmnd.ifcmnd)
--end --ifcmnd SET
--if the if condition was met, then we execute the ifcmnd, which can be any command
--minetest.log("simple_dialogs->eci executing ifcmnd "..dump(cmnd))
simple_dialogs.execute_cmnd(npcself,cmnd.ifcmnd,playername)
--minetest.log("simple_dialogs->eci back from ifcmnd")
end --ifrst
end --execute_cmnd_if
--this is used by execute_cmnd_if
--it takes in a condstr that is ONE equation from a possibly more complex if cond str.
--something like (hitpoints<10), it MUST be enclosed in parenthesis.
--it splits it up and returns a table op with the structure:
--op.pos where the operator was found
--op.left what was on the left side of the operator
--op.operator the operator string
--op.right what was on the right side of the operator
--op.output 1 if the equation was true, 0 if the equation was false
function simple_dialogs.split_on_operator(condstr)
local op={}
if condstr then
--this is just a slightly less ugly way to search for multiple patterns
find_operator(op,condstr,">=")
find_operator(op,condstr,"<=")
find_operator(op,condstr,"==")
find_operator(op,condstr,"~=")
find_operator(op,condstr,">")
find_operator(op,condstr,"<")
--minetest.log("simple_dialogs->soo op="..dump(op))
if op.pos then
op.left=string.sub(condstr,1,op.pos-1)
op.right=string.sub(condstr,op.pos+#op.operator)
else --no operator
op.left=condstr
op.operator="nop"
op.right=""
op.pos=#condstr+1 --shouldnt matter
end --if op.pos
--I built a really cool table of functions, with the operator strings as the keys.
--and it WAS cool, but I realized upon looking at it that it made it much more difficult
--to understand what was going on. SO, I replaced it with the chained "if" that is ugly,
--and inelegant, but easy to understand
--ifopfunc[op.operator](op)
if op.operator == ">=" then
if op.left >= op.right then op.output="1" else op.output="0" end
elseif op.operator == "<=" then
if op.left <= op.right then op.output="1" else op.output="0" end
elseif op.operator == "==" then
if op.left == op.right then op.output="1" else op.output="0" end
elseif op.operator == "~=" then
if op.left ~= op.right then op.output="1" else op.output="0" end
elseif op.operator == ">" then
if op.left > op.right then op.output="1" else op.output="0" end
elseif op.operator == "<" then
if op.left < op.right then op.output="1" else op.output="0" end
else
op.operator="nop"
op.left=condstr
op.right="" --shouldn't matter
op.pos=#condstr+1 --shouldn't matter
op.output=condstr
--if you didnt provide an operator, then the output is just whatever was there
--and it had better well resolve to a 0 or when when run through sandboxed_math
end --if op.operator
else --if condstr was nil (just in case)
op.operator="nop"
op.left=""
op.right=""
op.pos=1
op.output=""
end --if condstr
return op
end --split_on_operator
--this is just a slightly less ugly way to search for multiple patterns
--op.operator and op.pos will be updated if the passed in operator
--is found in condition string and op.pos is not set or p is before existing op.pos
function find_operator(op,condstr,operator)
local p=string.find(condstr,operator)
--of op was found, AND either op.pos is not set, or p is before previous op.pos
if p and (not op.pos or p > op.pos) then
op.operator=operator
op.pos=p
--minetest.log("simple_dialogs->fo found operator="..operator.." op="..dump(op))
end --if
--minetest.log("simple_dialogs->fo notfound operator="..operator.." op="..dump(op))
end --find operator
--this displays the help text
--I need a way to deal with this by language
function simple_dialogs.dialog_help(playername)
--local file = io.open(minetest.get_modpath("simple_dialogs").."/simple_dialogs_help.txt", "r")
--minetest.log("simple_dialogs-> dh top")
local file = io.open(helpfile, "r")
if file then
--minetest.log("simple_dialogs-> dh if file")
--local help
local helpstr=file:read("*all")
file:close()
local formspec={
"formspec_version[4]",
"size[15,15]",
"textarea[0.375,0.35;14,14;;Simple_Dialogs-Help;"..minetest.formspec_escape(helpstr).."]"
}
minetest.show_formspec(playername,"simple_dialogs:dialoghelp",table.concat(formspec))
else
minetest.log("simple_dialogs->dialoghelp: ERROR unable to find simple_dialogs_help.txt in modpath")
end
end --dialog_help
--------------------------------------------------------------
function simple_dialogs.save_dialog_var(npcself,varname,varval,playername)
if npcself and varname then
if not npcself.dialog.vars then npcself.dialog.vars = {} end
if not varval then varval="" end
--minetest.log("simple_dialogs->---sdv bfr varname="..varname.." varval="..varval)
varname=simple_dialogs.populate_vars_and_funcs(npcself,varname,playername) --populate vars
varname=simple_dialogs.varname_filter(varname) --filter down to only allowed chars
varval=simple_dialogs.populate_vars_and_funcs(npcself,varval,playername) --populate vars
--minetest.log("simple_dialogs->sdv aft varname="..varname.." varval="..varval)
npcself.dialog.vars[varname] = varval --add to variable list
--minetest.log("simple_dialogs->sdv end npcself.dialog.vars="..dump(npcself.dialog.vars))
end
end --save_dialog_var
function simple_dialogs.get_dialog_var(npcself,varname,playername,defaultval)
if npcself and varname then
if not defaultval then defaultval="" end
if not npcself.dialog.vars then npcself.dialog.vars = {} end
--minetest.log("simple_dialogs->---gdv bfr varname="..varname)
varname=simple_dialogs.varname_filter(varname) --filter down to only allowed chars, no need for trim since spaces are not allowed
--minetest.log("simple_dialogs->---gdv aft varname="..varname)
if varname=="PLAYERNAME" then
--playername must be dealt with differently. we can not just store it as a variable because
--If two players spoke to the same npc at the same time, one would overwrite the others playername
return playername
elseif npcself.dialog.vars[varname] then return npcself.dialog.vars[varname]
else return defaultval
end
end
end --get_dialog_var
--[[ *******************************************************************************
Grouping
--]]
--this function will go through a string and build a list that tells what order
--to process parenthesis (or any other open close delimiter) in.
--example:
--12345678901234
--((3*(21+2))/4)
--list[1].open=5 close=10
--list[2].open=2 close=11
--list[3].open=1 close=14
--note that if you pass this txt that has bad syntax, it will not throw an error, but instead stop processing and return the list up to that point.
--list[].open and close are inclusive. it includes the delimeter
--list[].opene and closee are exclusive. it does NOT include the delimiter
--so in the above example:
--list[1].opene=6 close=9
--list[2].opene=3 close=10
--list[3].opene=2 close=13
--
--if you pass funcname then only entries that start with funcname( are returned in the final list
--for funcname we can NOT just pass funcname( as the opendelim, because if we did, grouping
--would NOT take into account other functions or parenthesis. example:
--add(goodnums,calc(@[x]@+1)) <- we need add to recognize the calc function or it will get the wrong close delimiter
function simple_dialogs.build_grouping_list(txt,opendelim,closedelim,funcname)
--minetest.log("simple_dialogs->bgl top, txt="..txt.." funcname="..dump(funcname))
if funcname then funcname=simple_dialogs.trim(string.upper(funcname)) end
local grouping={}
grouping.list={}
grouping.origtxt=txt --is this useful?
grouping.txt=txt
grouping.first=0 --this will store the grouping index of the first delim in the string
local openstack={}
local funcstack={}
local opendelim_len=string.len(opendelim)
grouping.opendelim_len=opendelim_len
local closedelim_len=string.len(closedelim)
grouping.closedelim_len=closedelim_len
for i=1,string.len(txt),1 do
if string.sub(txt,i,i+opendelim_len-1)==opendelim then --open delim
openstack[#openstack+1]=i --open pos onto stack.
--minetest.log("simple_dialogs->bgl i="..i.." open openstack["..#openstack.."]="..openstack[#openstack])
if funcname and ((i-#funcname)>0) and (string.upper(string.sub(txt,i-#funcname,i-1))==funcname) then
funcstack[#openstack]=funcname --just a flag to let us know this openstack matches our function
openstack[#openstack]=i-#funcname
--minetest.log("simple_dialogs->bgl open <FUNCNAME> openstack["..#openstack.."]="..openstack[#openstack].." funcname="..funcname.." #funcname="..#funcname)
end
elseif string.sub(txt,i,i+closedelim_len-1)==closedelim then -- close delim
--minetest.log("simple_dialogs->bgl i="..i.." close ")
--if you find parens out of order, just stop and return what you have so far
if #openstack<1 then return grouping end
--minetest.log("simple_dialogs->bgl close openstack="..dump(openstack).." funcstack="..dump(funcstak))
if (not funcname) or (funcstack[#openstack]) then
--minetest.log("simple_dialogs->bgl notfuncname or is func")
local l=#grouping.list+1
grouping.list[l]={}
local gll=grouping.list[l]
gll.open=openstack[#openstack]
gll.opene=gll.open+(opendelim_len)
--minetest.log("simple_dialogs->bgl bfr func: gll="..dump(gll))
if funcname then gll.opene=gll.opene+#funcname end
gll.close=i+(closedelim_len-1)
gll.closee=i-1
--grouping.first is the first delim in the string. if grouping.first=0 then we have not set it at all
if grouping.first==0 then grouping.first=l
elseif gll.open<grouping.list[grouping.first].open then grouping.first=l
end
--minetest.log("simple_dialogs->bgl end close: gll="..dump(gll))
end --if not funcname
table.remove(openstack,#openstack) --remove from stack
table.remove(funcstack,#openstack+1) --may or may not be there, +1 because we just reduced the size of openstack by one
end --if
end --while
return grouping
end --build_grouping_list
function simple_dialogs.grouping_section(grouping,i,incl_excl)
if not incl_excl then incl_excl="INCLUSIVE" end
--minetest.log("simple_dialogs->gs top i="..i.." incl_excl="..incl_excl.." grouping="..dump(grouping))
local gli=grouping.list[i]
--minetest.log("GGGs after gli")
if incl_excl=="INCLUSIVE" then
--minetest.log("GGGs inclusive")
return string.sub(grouping.txt,gli.open,gli.close)
else
--minetest.log("GGGs exclusive")
return string.sub(grouping.txt,gli.opene,gli.closee)
end
end --grouping_section
function simple_dialogs.grouping_sectione(grouping,i)
--minetest.log("GGGse i="..i.." grouping="..dump(grouping))
simple_dialogs.grouping_section(grouping,i,"EXCLUSIVE")
end --grouping_sectione
function simple_dialogs.grouping_replace(grouping,idx,replacewith,incl_excl)
--minetest.log("***GGGR top grouping="..dump(grouping).." idx="..idx.." replacewith="..replacewith.." incl_excl="..incl_excl)
if not incl_excl then incl_excl="INCLUSIVE" end
local s=grouping.list[idx].open
local e=grouping.list[idx].close
if incl_excl=="EXCLUSIVE" then
s=grouping.list[idx].opene
e=grouping.list[idx].closee
end
local origlen=e-s+1
local diff=string.len(replacewith)-origlen
local txt=grouping.txt
grouping.txt=string.sub(txt,1,s-1)..replacewith..string.sub(txt,e+1)
for i=1,#grouping.list,1 do
local gli=grouping.list[i]
if gli.open>s then gli.open=gli.open+diff end
if gli.opene>s then gli.opene=gli.opene+diff end
if gli.close>s then gli.close=gli.close+diff end
if gli.closee>s then gli.closee=gli.closee+diff end
end --for
--minetest.log("GGGR bot grouping="..dump(grouping))
--minetest.log("GGGR2 bot origtxt="..grouping.origtxt)
--minetest.log("GGGR2 bot txt="..grouping.txt)
return grouping.txt
end--grouping_replace
--[[ ##################################################################################
func splitter
--]]
--this finds all the occurances of a specific function in a line.
--So, for example:
--func_splitter("the time is timeofday() or timeofday(hours) or timeofday(dayornight,24)","TIMEOFDAY",2)
-- 1234567890123456789012345678901234567890123456789012345678901234567890
--will return a grouping.list of length 3
--grouping.list[1] will be the timeofday() at pos 13, and grouping.list[1].parm will be an empty list
--grouping.list[2] will be the timeofday(hour) at pos 28, and grouping.list[2].parm will contain 1 item, "hours"
--grouping.list[3] will be the timeofday(dayornight,24) at pos 38, and grouping.list[3].parm will be a list of 2 items, "dayornight" and 24
--so the proper way to check if func_splitter found your function and to process it is like this:
--local grouping=simple_dialogs.func_splitter(line,"CALC",1) <-call this function
--if grouping then <-safety, dont want to crash the server
-- for g=1,#grouping.list,1 do <-loop through instances found, will abort on 0, of course
-- local mth=grouping.list[g].parm[1] <-get any parameters you need.
-- mth=simple_dialogs.calc_filter(mth) <-process
-- line=simple_dialogs.grouping_replace(grouping,g,mth,"INCLUSIVE") <-replace the function with the new value
-- end --for
--end --if grouping CALC
function simple_dialogs.func_splitter(line,funcname,parmcount)
--minetest.log("simple_dialogs->fs--------------- funcname="..funcname.." line="..line)
if not parmcount then parmcount=1 end