-
Notifications
You must be signed in to change notification settings - Fork 16
/
Learning_Mapper.lua
3954 lines (3296 loc) · 137 KB
/
Learning_Mapper.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
--[[
LEARNING MAPPER
Author: Nick Gammon
Date: 24th January 2020
PERMISSION TO DISTRIBUTE
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
LIMITATION OF LIABILITY
The software is provided "as is", without warranty of any kind, express or implied,
including but not limited to the warranties of merchantability, fitness for a particular
purpose and noninfringement. In no event shall the authors or copyright holders be liable
for any claim, damages or other liability, whether in an action of contract,
tort or otherwise, arising from, out of or in connection with the software
or the use or other dealings in the software.
-------------------------------------------------------------------------
EXPOSED FUNCTIONS
set_line_type (linetype, contents) --> set this current line to be definitely linetype with option contents
set_line_type_contents (linetype, contents) --> sets the content for <linetype> to be <contents>
(for example, if you get a room name on a prompt line)
set_not_line_type (linetype) --> set this current line to be definitely not linetype (can call for multiple line types)
set_area_name (name) --> sets the name of the area you are in
set_uid (uid) --> sets a string to be hashed as the UID for this room
do_not_deduce_line_type (linetype) --> do not deduce (do Bayesian analysis) on this type of line - has to be set by set_line_type
deduce_line_type (linetype) --> deduce this line type (cancels do_not_deduce_line_type)
get_last_line_type () --> get the previous line type as deduced or set by set_line_type
get_this_line_type () --> get the current overridden line type (from set_line_type)
set_config_option (name, value) --> set a mapper configuration value of <name> to <value>
get_config_option (name) --> get the current configuration value of <name>
get_corpus () --> get the corpus (serialized table)
get_stats () --> get the training stats (serialized table)
get_database () --> get the mapper database (rooms table) (serialized table)
get_config () --> get the configuration options (serialized table)
get_current_room () --> gets the current room's UID and room information (serialized table)
set_room_extras (uid, extras) --> sets extra information for the room (user-supplied)
extras must be a string which serializes into a variable including a table
eg. " { a = 42, b = 666, c = 'Kobold' } "
eg. config = CallPlugin ("99c74b2685e425d3b6ed6a7d", "get_config")
CallPlugin ("99c74b2685e425d3b6ed6a7d", "set_line_type", "exits")
CallPlugin ("99c74b2685e425d3b6ed6a7d", "do_not_deduce_line_type", "exits")
Note: The plugin ID is fixed as it is set in the Learning_Mapper.xml file near the top:
id="99c74b2685e425d3b6ed6a7d"
--]]
LEARNING_MAPPER_LUA_VERSION = 2.1 -- version must agree with plugin version
-- The probability (in the range 0.0 to 1.0) that a line has to meet to be considered a certain line type.
-- The higher, the stricter the requirement.
-- Default of 0.7 seems to work OK, but you could tweak that.
PROBABILITY_CUTOFF = 0.7
-- other modules needed by this plugin
require "mapper"
require "serialize"
require "copytable"
require "commas"
require "tprint"
require "pairsbykeys"
-- our two windows
win = "window_type_info_" .. GetPluginID ()
learn_window = "learn_dialog_" .. GetPluginID ()
-- -----------------------------------------------------------------
-- Handlers for when a line-type changes
-- -----------------------------------------------------------------
description_styles = { }
exits_styles = { }
room_name_styles = { }
UNKNOWN_DUPLICATE_ROOM = string.rep ("F", 25) -- dummy UID
DEBUGGING = false
function set_last_direction_moved (where)
last_direction_moved = where
DEBUG ("SET: last_direction_moved: " .. tostring (where))
end -- set_last_direction_moved
function get_last_direction_moved ()
DEBUG ("get: last_direction_moved: " .. tostring (last_direction_moved))
return last_direction_moved
end -- get_last_direction_moved
function set_from_room (where)
from_room = where
DEBUG ("SET: from_room: " .. fixuid (tostring (where)))
end -- set_from_room
function get_from_room (f)
if f then
DEBUG ("get: from_room: " .. fixuid (tostring (from_room)) .. " (" .. f .. ")")
else
DEBUG ("get: from_room: " .. fixuid (tostring (from_room)))
end -- if
return from_room
end -- get_from_room
function set_current_room (where)
current_room = where
DEBUG ("SET: current_room: " .. fixuid (tostring (where)))
end -- set_current_room
function get_current_room_ (f)
if f then
DEBUG ("get: current_room: " .. fixuid (tostring (current_room)) .. " (" .. f .. ")")
else
DEBUG ("get: current_room: " .. fixuid (tostring (current_room)))
end -- if
return current_room
end -- get_current_room_
-- -----------------------------------------------------------------
-- description
-- -----------------------------------------------------------------
function f_handle_description (saved_lines)
if description and ignore_received then
return
end -- if
-- if the description follows the exits, then ignore descriptions that don't follow exits
if config.ACTIVATE_DESCRIPTION_AFTER_EXITS then
if not exits_str then
return
end -- if
end -- if
-- if the description follows the room name, then ignore descriptions that don't follow the room name
if config.ACTIVATE_DESCRIPTION_AFTER_ROOM_NAME then
if not room_name then
return
end -- if
end -- if
local lines = { }
description_styles = { }
for _, line_info in ipairs (saved_lines) do
table.insert (lines, line_info.line) -- get text of line
table.insert (description_styles, line_info.styles [1]) -- remember first style run
end -- for each line
description = table.concat (lines, "\n")
if config.WHEN_TO_DRAW_MAP == DRAW_MAP_ON_DESCRIPTION then
process_new_room ()
end -- if
end -- f_handle_description
-- -----------------------------------------------------------------
-- exits
-- -----------------------------------------------------------------
function f_handle_exits ()
local lines = { }
exits_styles = { }
for _, line_info in ipairs (saved_lines) do
table.insert (lines, line_info.line) -- get text of line
table.insert (exits_styles, line_info.styles [1]) -- remember first style run
end -- for each line
exits_str = table.concat (lines, " "):lower ()
if config.WHEN_TO_DRAW_MAP == DRAW_MAP_ON_EXITS then
process_new_room ()
end -- if
end -- f_handle_exits
-- -----------------------------------------------------------------
-- room name
-- -----------------------------------------------------------------
function f_handle_name ()
local lines = { }
room_name_styles = { }
for _, line_info in ipairs (saved_lines) do
table.insert (lines, line_info.line) -- get text of line
table.insert (room_name_styles, line_info.styles [1]) -- remember first style run
end -- for each line
room_name = table.concat (lines, " ")
-- a bit of a hack, but look for: Room name [N, S, W]
if config.EXITS_ON_ROOM_NAME then
local name, exits = string.match (room_name, "^([^%[]+)(%[.*%])%s*$")
if name then
room_name = name
exits_str = exits:lower ()
end -- if that sort of line found
end -- if exits on room name wanted
if config.WHEN_TO_DRAW_MAP == DRAW_MAP_ON_ROOM_NAME then
process_new_room ()
end -- if
end -- f_handle_name
-- -----------------------------------------------------------------
-- prompt
-- -----------------------------------------------------------------
function f_handle_prompt ()
local lines = { }
for _, line_info in ipairs (saved_lines) do
table.insert (lines, line_info.line) -- get text of line
end -- for each line
prompt = table.concat (lines, " ")
if config.WHEN_TO_DRAW_MAP == DRAW_MAP_ON_PROMPT then
if override_contents ['description'] then
description = override_contents ['description']
end -- if
if override_contents ['exits'] then
exits_str = override_contents ['exits']:lower ()
end -- if
if override_contents ['room_name'] then
room_name = override_contents ['room_name']
end -- if
if description and exits_str then
process_new_room ()
end -- if
end -- if time to draw the map
end -- f_handle_prompt
-- -----------------------------------------------------------------
-- ignore this line type
-- -----------------------------------------------------------------
function f_handle_ignore ()
ignore_received = true
end -- f_handle_ignore
-- -----------------------------------------------------------------
-- cannot move - cancel speedwalk
-- -----------------------------------------------------------------
function f_cannot_move ()
mapper.cancel_speedwalk ()
set_last_direction_moved (nil) -- therefore we haven't moved anywhere
end -- f_cannot_move
-- -----------------------------------------------------------------
-- Handlers for getting the wanted value for a marker for the nominated line
-- -----------------------------------------------------------------
-- these are the types of lines we are trying to classify as a certain line IS or IS NOT that type
line_types = {
room_name = { short = "Room name", handler = f_handle_name, seq = 1 },
description = { short = "Description", handler = f_handle_description, seq = 2 },
exits = { short = "Exits", handler = f_handle_exits, seq = 3 },
prompt = { short = "Prompt", handler = f_handle_prompt, seq = 4 },
ignore = { short = "Ignore", handler = f_handle_ignore, seq = 5 },
cannot_move = { short = "Can't move", handler = f_cannot_move, seq = 6 },
} -- end of line_types table
function f_first_style_run_foreground (line)
return { GetStyleInfo(line, 1, 14) or -1 }
end -- f_first_style_run_foreground
function f_show_colour (which, value)
mapper.mapprint (string.format (" %20s %5d %5d %7.2f", RGBColourToName (which), value.black, value.red, value.score))
end -- f_show_colour
function f_show_word (which, value)
if #which > 20 then
mapper.mapprint (string.format ("%s\n %20s %5d %5d %7.2f", which, '', value.black, value.red, value.score))
else
mapper.mapprint (string.format (" %20s %5d %5d %7.2f", which, value.black, value.red, value.score))
end -- if
end -- f_show_colour
function f_first_word (line)
if not GetLineInfo(line, 1) then
return {}
end -- no line available
return { (string.match (GetLineInfo(line, 1), "^%s*(%a+)") or ""):lower () }
end -- f_first_word
function f_exact_line (line)
if not GetLineInfo(line, 1) then
return {}
end -- no line available
return { GetLineInfo(line, 1) }
end -- f_exact_line
function f_first_two_words (line)
if not GetLineInfo(line, 1) then
return {}
end -- no line available
return { (string.match (GetLineInfo(line, 1), "^%s*(%a+%s+%a+)") or ""):lower () }
end -- f_first_two_words
function f_first_three_words (line)
if not GetLineInfo(line, 1) then
return {}
end -- no line available
return { (string.match (GetLineInfo(line, 1), "^%s*(%a+%s+%a+%s+%a+)") or ""):lower () }
end -- f_first_three_words
function f_all_words (line)
if not GetLineInfo(line, 1) then
return {}
end -- no line available
local words = { }
for w in string.gmatch (GetLineInfo(line, 1), "%a+") do
table.insert (words, w:lower ())
end -- for
return words
end -- f_all_words
function f_first_character (line)
if not GetLineInfo(line, 1) then
return {}
end -- no line available
return { string.match (GetLineInfo(line, 1), "^.") or "" }
end -- f_first_character
-- -----------------------------------------------------------------
-- markers: things we are looking for, like colour of first style run
-- You could add others, for example:
-- * colour of the last style run
-- * number of words on the line
-- * number of style runs on the line
-- Whether that would help or not remains to be seen.
-- The functions above return the value(s) for the corresponding marker, for the nominated line.
-- -----------------------------------------------------------------
markers = {
{
desc = "Foreground colour of first style run",
func = f_first_style_run_foreground,
marker = "first_style_run_foreground",
show = f_show_colour,
accessing_function = pairs,
},
{
desc = "First word in the line",
func = f_first_word,
marker = "first_word",
show = f_show_word,
accessing_function = pairsByKeys,
},
{
desc = "First two words in the line",
func = f_first_two_words,
marker = "first_two_words",
show = f_show_word,
accessing_function = pairsByKeys,
},
{
desc = "First three words in the line",
func = f_first_three_words,
marker = "first_three_words",
show = f_show_word,
accessing_function = pairsByKeys,
},
{
desc = "All words in the line",
func = f_all_words,
marker = "all_words",
show = f_show_word,
accessing_function = pairsByKeys,
},
{
desc = "Exact line",
func = f_exact_line,
marker = "exact_line",
show = f_show_word,
accessing_function = pairsByKeys,
},
--[[
{
desc = "First character in the line",
func = f_first_character,
marker = "first_character",
show = f_show_word,
},
--]]
} -- end of markers
inverse_markers = { }
for k, v in ipairs (markers) do
inverse_markers [v.marker] = v
end -- for
local MAX_NAME_LENGTH = 60
-- when to update the map
DRAW_MAP_ON_ROOM_NAME = 1
DRAW_MAP_ON_DESCRIPTION = 2
DRAW_MAP_ON_EXITS = 3
DRAW_MAP_ON_PROMPT = 4
default_config = {
-- assorted colours
BACKGROUND_COLOUR = { name = "Background", colour = ColourNameToRGB "lightseagreen", },
ROOM_COLOUR = { name = "Room", colour = ColourNameToRGB "cyan", },
EXIT_COLOUR = { name = "Exit", colour = ColourNameToRGB "darkgreen", },
EXIT_COLOUR_UP_DOWN = { name = "Exit up/down", colour = ColourNameToRGB "darkmagenta", },
EXIT_COLOUR_IN_OUT = { name = "Exit in/out", colour = ColourNameToRGB "#3775E8", },
OUR_ROOM_COLOUR = { name = "Our room", colour = ColourNameToRGB "black", },
UNKNOWN_ROOM_COLOUR = { name = "Unknown room", colour = ColourNameToRGB "#00CACA", },
DIFFERENT_AREA_COLOUR = { name = "Another area", colour = ColourNameToRGB "#009393", },
SHOP_FILL_COLOUR = { name = "Shop", colour = ColourNameToRGB "darkolivegreen", },
TRAINER_FILL_COLOUR = { name = "Trainer", colour = ColourNameToRGB "yellowgreen", },
BANK_FILL_COLOUR = { name = "Bank", colour = ColourNameToRGB "gold", },
DUPLICATE_FILL_COLOUR = { name = "Duplicate", colour = ColourNameToRGB "lightgoldenrodyellow", },
BOOKMARK_FILL_COLOUR = { name = "Notes", colour = ColourNameToRGB "lightskyblue", },
MAPPER_NOTE_COLOUR = { name = "Messages", colour = ColourNameToRGB "lightgreen" },
ROOM_NAME_TEXT = { name = "Room name text", colour = ColourNameToRGB "#BEF3F1", },
ROOM_NAME_FILL = { name = "Room name fill", colour = ColourNameToRGB "#105653", },
ROOM_NAME_BORDER = { name = "Room name box", colour = ColourNameToRGB "black", },
AREA_NAME_TEXT = { name = "Area name text", colour = ColourNameToRGB "#BEF3F1",},
AREA_NAME_FILL = { name = "Area name fill", colour = ColourNameToRGB "#105653", },
AREA_NAME_BORDER = { name = "Area name box", colour = ColourNameToRGB "black", },
FONT = { name = get_preferred_font {"Dina", "Lucida Console", "Fixedsys", "Courier", "Sylfaen",} ,
size = 8
} ,
-- size of map window
WINDOW = { width = 400, height = 400 },
-- how far from where we are standing to draw (rooms)
SCAN = { depth = 30 },
-- speedwalk delay
DELAY = { time = 0.3 },
-- how many seconds to show "recent visit" lines (default 3 minutes)
LAST_VISIT_TIME = { time = 60 * 3 },
-- config for learning mapper
STATUS_BACKGROUND_COLOUR = "black", -- the background colour of the status window
STATUS_FRAME_COLOUR = "#1B1B1B", -- the frame colour of the status window
STATUS_TEXT_COLOUR = "lightgreen", -- palegreen is more visible
UID_SIZE = 4, -- how many characters of the UID to show
-- learning configuration
WHEN_TO_DRAW_MAP = DRAW_MAP_ON_EXITS, -- we need to have name/description/exits to draw the map
ACTIVATE_DESCRIPTION_AFTER_EXITS = false, -- descriptions are activated *after* an exit line (used for MUDs with exits then descriptions)
ACTIVATE_DESCRIPTION_AFTER_ROOM_NAME = false,-- descriptions are activated *after* a room name line
BLANK_LINE_TERMINATES_LINE_TYPE = false, -- if true, a blank line terminates the previous line type
ADD_NEWLINE_TO_PROMPT = false, -- if true, attempts to add a newline to a prompt at the end of a packet
SHOW_LEARNING_WINDOW = true, -- if true, show the learning status and training windows on startup
EXITS_ON_ROOM_NAME = false, -- if true, exits are listed on the room name line (eg. Starter Inventory and Shops [E, U])
INCLUDE_EXITS_IN_HASH = true, -- if true, exits are included in the description hash (UID)
INCLUDE_ROOM_NAME_IN_HASH = false, -- if true, the room name is included in the description hash (UID)
EXITS_IS_SINGLE_LINE = false, -- if true, exits are assumed to be only a single line
PROMPT_IS_SINGLE_LINE = true, -- if true, prompts are assumed to be only a single line
EXIT_LINES_START_WITH_DIRECTION = false, -- if true, exit lines must start with a direction (north, south, etc.)
SORT_EXITS = false, -- if true, exit lines are extracted into words and sorted, excluding any other characters on the line
SAVE_LINE_INFORMATION = true, -- if true, we save to the database the colour of the first style run for name/description/exits
-- other stuff
SHOW_INFO = false, -- if true, information messages are displayed
SHOW_WARNINGS = true, -- if true, warning messages are displayed
SHOW_ROOM_AND_EXITS = false, -- if true, exact deduced room name and exits are shown (needs SHOW_INFO)
}
-- -----------------------------------------------------------------
-- Handlers for validating configuration values (eg. colour, boolean)
-- -----------------------------------------------------------------
function config_validate_colour (which)
local colour = ColourNameToRGB (which)
if colour == -1 then
mapper.maperror (string.format ('Colour name "%s" not a valid HTML colour name or code.', which))
mapper.mapprint (" You can use HTML colour codes such as '#ab34cd' or names such as 'green'.")
mapper.mapprint (" See the Colour Picker (Edit menu -> Colour Picker: Ctrl+Alt+P).")
return nil, nil
end -- if bad
return which, colour
end -- config_validate_colour
function config_validate_uid_size (which)
local size = tonumber (which)
if not size then
mapper.maperror ("Bad UID size: " .. which)
return nil
end -- if
if size < 3 or size > 25 then
mapper.maperror ("UID size must be in the range 3 to 25")
return nil
end -- if
return size
end -- config_validate_uid_size
-- -----------------------------------------------------------------
-- when we draw the map (after what sort of line)
-- -----------------------------------------------------------------
local when_types = {
["room name"] = DRAW_MAP_ON_ROOM_NAME,
["description"] = DRAW_MAP_ON_DESCRIPTION,
["exits"] = DRAW_MAP_ON_EXITS,
["prompt"] = DRAW_MAP_ON_PROMPT,
} -- end of table
function config_validate_when_to_draw (which)
local when = which:lower ()
local w = when_types [when]
if not w then
mapper.maperror ("Unknown time to draw the map: " .. which)
mapper.mapprint ("Valid times are:")
local t = { }
for k, v in ipairs (when_types) do
table.insert (t, k)
end
mapper.mapprint (" " .. table.concat (t, ", "))
return nil
end -- if type not found
return w
end -- when_to_draw
function convert_when_to_draw_to_name (which)
local when = "Unknown"
for k, v in pairs (when_types) do
if which == v then
when = k
break
end -- if
end -- for
return when
end -- convert_when_to_draw_to_name
local bools = {
yes = true,
y = true,
no = false,
n = false
} -- end of bools
function config_validate_boolean (which)
local which = which:lower ()
local yesno = bools [which]
if yesno == nil then
mapper.maperror ("Invalid option: must be YES or NO")
return
end -- not in bools table
return yesno
end -- config_validate_boolean
-- -----------------------------------------------------------------
-- Handlers for displaying configuration values (eg. colour, boolean)
-- -----------------------------------------------------------------
function config_display_colour (which)
return which
end -- config_display_colour
function config_display_number (which)
return tostring (which)
end -- config_display_number
function config_display_when_to_draw (which)
return convert_when_to_draw_to_name (which)
end -- config_display_when_to_draw
function config_display_boolean (which)
if which then
return "Yes"
else
return "No"
end -- if
end -- config_display_boolean
-- -----------------------------------------------------------------
-- Configuration options (ie. mapper config <option>) and their handlers and internal option name
-- -----------------------------------------------------------------
config_control = {
{ option = 'WHEN_TO_DRAW_MAP', name = 'when_to_draw', validate = config_validate_when_to_draw, show = config_display_when_to_draw },
{ option = 'ACTIVATE_DESCRIPTION_AFTER_EXITS', name = 'activate_description_after_exits', validate = config_validate_boolean, show = config_display_boolean },
{ option = 'ACTIVATE_DESCRIPTION_AFTER_ROOM_NAME', name = 'activate_description_after_room_name', validate = config_validate_boolean, show = config_display_boolean },
{ option = 'ADD_NEWLINE_TO_PROMPT', name = 'add_newline_to_prompt', validate = config_validate_boolean, show = config_display_boolean },
{ option = 'BLANK_LINE_TERMINATES_LINE_TYPE', name = 'blank_line_terminates_line_type', validate = config_validate_boolean, show = config_display_boolean },
{ option = 'EXITS_ON_ROOM_NAME', name = 'exits_on_room_name', validate = config_validate_boolean, show = config_display_boolean },
{ option = 'INCLUDE_EXITS_IN_HASH', name = 'include_exits_in_hash', validate = config_validate_boolean, show = config_display_boolean },
{ option = 'INCLUDE_ROOM_NAME_IN_HASH', name = 'include_room_name_in_hash', validate = config_validate_boolean, show = config_display_boolean },
{ option = 'EXITS_IS_SINGLE_LINE', name = 'exits_is_single_line', validate = config_validate_boolean, show = config_display_boolean },
{ option = 'PROMPT_IS_SINGLE_LINE', name = 'prompt_is_single_line', validate = config_validate_boolean, show = config_display_boolean },
{ option = 'EXIT_LINES_START_WITH_DIRECTION', name = 'exit_lines_start_with_direction', validate = config_validate_boolean, show = config_display_boolean },
{ option = 'SORT_EXITS', name = 'sort_exits', validate = config_validate_boolean, show = config_display_boolean },
{ option = 'STATUS_BACKGROUND_COLOUR', name = 'status_background', validate = config_validate_colour, show = config_display_colour },
{ option = 'STATUS_FRAME_COLOUR', name = 'status_border', validate = config_validate_colour, show = config_display_colour },
{ option = 'STATUS_TEXT_COLOUR', name = 'status_text', validate = config_validate_colour, show = config_display_colour },
{ option = 'UID_SIZE', name = 'uid_size', validate = config_validate_uid_size, show = config_display_number },
{ option = 'SAVE_LINE_INFORMATION', name = 'save_line_info', validate = config_validate_boolean, show = config_display_boolean },
{ option = 'SHOW_INFO', name = 'show_info', validate = config_validate_boolean, show = config_display_boolean },
{ option = 'SHOW_WARNINGS', name = 'show_warnings', validate = config_validate_boolean, show = config_display_boolean },
{ option = 'SHOW_ROOM_AND_EXITS', name = 'show_room_and_exits', validate = config_validate_boolean, show = config_display_boolean },
}
-- make a table keyed on the name the user uses
config_control_names = { }
for k, v in ipairs (config_control) do
config_control_names [v.name] = v
end -- for
-- -----------------------------------------------------------------
-- valid_direction - for detecting movement between rooms, and validating exit lines
-- -----------------------------------------------------------------
valid_direction = {
n = "n",
s = "s",
e = "e",
w = "w",
u = "u",
d = "d",
ne = "ne",
sw = "sw",
nw = "nw",
se = "se",
north = "n",
south = "s",
east = "e",
west = "w",
up = "u",
down = "d",
northeast = "ne",
northwest = "nw",
southeast = "se",
southwest = "sw",
['in'] = "in",
out = "out",
} -- end of valid_direction
-- -----------------------------------------------------------------
-- inverse_direction - if we go north then the inverse direction is south, and so on.
-- -----------------------------------------------------------------
inverse_direction = {
n = "s",
s = "n",
e = "w",
w = "e",
u = "d",
d = "u",
ne = "sw",
sw = "ne",
nw = "se",
se = "nw",
['in'] = "out",
out = "in",
} -- end of inverse_direction
-- -----------------------------------------------------------------
-- OnPluginDrawOutputWindow
-- Update our line information info
-- -----------------------------------------------------------------
function OnPluginDrawOutputWindow (firstline, offset, notused)
-- don't bother if window not visible
if not WindowInfo (win, 5) then
return
end -- if
local background_colour = ColourNameToRGB (config.STATUS_BACKGROUND_COLOUR)
local frame_colour = ColourNameToRGB (config.STATUS_FRAME_COLOUR)
local text_colour = ColourNameToRGB (config.STATUS_TEXT_COLOUR)
local main_height = GetInfo (280)
local font_height = GetInfo (212)
-- clear window
WindowRectOp (win, miniwin.rect_fill, 0, 0, 0, 0, background_colour)
-- frame it
WindowRectOp(win, miniwin.rect_frame, 0, 0, 0, 0, frame_colour)
-- allow for scrolling position
local top = (((firstline - 1) * font_height) - offset) - 2
-- how many lines to draw
local lastline = firstline + (main_height / font_height)
for line = firstline, lastline do
if line >= 1 and GetLineInfo (line, 1) then
if GetLineInfo (line, 4) or GetLineInfo (line, 5) then
-- note or input line, ignore it
else
local linetype, probability, x_offset
local ded = deduced_line_types [GetLineInfo (line, 10)]
if ded and ded.lt then
if ded.ov then
line_type_info = string.format ("<- %s (certain)", line_types [ded.lt].short)
else
line_type_info = string.format ("<- %s (%0.0f%%)", line_types [ded.lt].short, (ded.con or 0) * 100)
end -- if overridden or not
local x_offset = WindowText (win, font_id, line_type_info, 1, top, 0, 0, text_colour)
if (not GetLineInfo (line, 3)) and (line >= lastline - 1) then
x_offset = x_offset + WindowText (win, font_id, " (partial line)", 1 + x_offset, top, 0, 0, ColourNameToRGB ("darkgray"))
end -- if
if ded.draw then
x_offset = x_offset + WindowText (win, font_id,
string.format (" (draw room %s)", fixuid (ded.uid)), 1 + x_offset, top, 0, 0, ColourNameToRGB ("darkgray"))
end -- if
end -- if in deduced_line_types table
end -- if output line
top = top + font_height
end -- if line exists
end -- for each line
end -- OnPluginDrawOutputWindow
-- -----------------------------------------------------------------
-- OnPluginWorldOutputResized
-- On world window resize, remake the miniwindow to fit the size correctly
-- -----------------------------------------------------------------
function OnPluginWorldOutputResized ()
font_name = GetInfo (20) -- output window font
font_size = GetOption "output_font_height"
local output_width = GetInfo (240) -- average width of pixels per character
local wrap_column = GetOption ('wrap_column')
local pixel_offset = GetOption ('pixel_offset')
-- make window so I can grab the font info
WindowCreate (win,
(output_width * wrap_column) + pixel_offset + 10, -- left
0, -- top
400, -- width
GetInfo (263), -- world window client height
miniwin.pos_top_left, -- position (irrelevant)
miniwin.create_absolute_location, -- flags
ColourNameToRGB (config.STATUS_BACKGROUND_COLOUR)) -- background colour
-- add font
WindowFont (win, font_id, font_name, font_size,
false, false, false, false, -- normal
miniwin.font_charset_ansi, miniwin.font_family_any)
-- find height of font for future calculations
font_height = WindowFontInfo (win, font_id, 1) -- height
WindowSetZOrder(win, -5)
if WindowInfo (learn_window, 5) then
WindowShow (win)
end -- if
end -- OnPluginWorldOutputResized
-- -----------------------------------------------------------------
-- INFO helper function for debugging the plugin (information messages)
-- -----------------------------------------------------------------
function INFO (...)
if config.SHOW_INFO then
ColourNote ("orange", "", table.concat ( { ... }, " "))
end -- if
end -- INFO
-- -----------------------------------------------------------------
-- WARNING helper function for debugging the plugin (warning/error messages)
-- -----------------------------------------------------------------
function WARNING (...)
if config.SHOW_WARNINGS then
ColourNote ("red", "", table.concat ( { ... }, " "))
end -- if
end -- WARNING
-- -----------------------------------------------------------------
-- DEBUG helper function for debugging the plugin
-- -----------------------------------------------------------------
function DEBUG (...)
if DEBUGGING then
ColourNote ("cornflowerblue", "", table.concat ( { ... }, " "))
end -- if
end -- DEBUG
-- -----------------------------------------------------------------
-- corpus_reset - throw away the learned corpus
-- -----------------------------------------------------------------
function corpus_reset (empty)
if empty then
corpus = { }
stats = { }
end -- if
-- make sure each line type is in the corpus
for k, v in pairs (line_types) do
if not corpus [k] then
corpus [k] = {}
end -- not there yet
if not stats [k] then
stats [k] = { is = 0, isnot = 0 }
end -- not there yet
for k2, v2 in ipairs (markers) do
if not corpus [k] [v2.marker] then -- if that marker not there, add it
corpus [k] [v2.marker] = { } -- table of values for this marker
end -- marker not there yet
end -- for each marker type
end -- for each line type
end -- corpus_reset
LEARN_WINDOW_WIDTH = 300
LEARN_WINDOW_HEIGHT = 270
LEARN_BUTTON_WIDTH = 80
LEARN_BUTTON_HEIGHT = 30
hotspots = { }
button_down = false
-- -----------------------------------------------------------------
-- button_mouse_down - generic mouse-down handler
-- -----------------------------------------------------------------
function button_mouse_down (flags, hotspot_id)
local hotspot_info = hotspots [hotspot_id]
if not hotspot_info then
WARNING ("No info found for hotspot", hotspot_id)
return
end
-- no button state change if no selection
if GetSelectionStartLine () == 0 then
return
end -- if
button_down = true
WindowRectOp (hotspot_info.window, miniwin.rect_draw_edge,
hotspot_info.x1, hotspot_info.y1, hotspot_info.x2, hotspot_info.y2,
miniwin.rect_edge_sunken,
miniwin.rect_edge_at_all + miniwin.rect_option_fill_middle) -- sunken, filled
WindowText (hotspot_info.window, hotspot_info.font, hotspot_info.text, hotspot_info.text_x + 1, hotspot_info.y1 + 8 + 1, 0, 0, ColourNameToRGB "black", true)
Redraw ()
end -- button_mouse_down
-- -----------------------------------------------------------------
-- button_cancel_mouse_down - generic cancel-mouse-down handler
-- -----------------------------------------------------------------
function button_cancel_mouse_down (flags, hotspot_id)
local hotspot_info = hotspots [hotspot_id]
if not hotspot_info then
WARNING ("No info found for hotspot", hotspot_id)
return
end
button_down = false
buttons_active = nil
WindowRectOp (hotspot_info.window, miniwin.rect_draw_edge,
hotspot_info.x1, hotspot_info.y1, hotspot_info.x2, hotspot_info.y2,
miniwin.rect_edge_raised,
miniwin.rect_edge_at_all + miniwin.rect_option_fill_middle) -- raised, filled
WindowText (hotspot_info.window, hotspot_info.font, hotspot_info.text, hotspot_info.text_x, hotspot_info.y1 + 8, 0, 0, ColourNameToRGB "black", true)
Redraw ()
end -- button_cancel_mouse_down
-- -----------------------------------------------------------------
-- button_mouse_up - generic mouse-up handler
-- -----------------------------------------------------------------
function button_mouse_up (flags, hotspot_id)
local hotspot_info = hotspots [hotspot_id]
if not hotspot_info then
WARNING ("No info found for hotspot", hotspot_id)
return
end
button_down = false
buttons_active = nil
-- call the handler
hotspot_info.handler ()
WindowRectOp (hotspot_info.window, miniwin.rect_draw_edge,
hotspot_info.x1, hotspot_info.y1, hotspot_info.x2, hotspot_info.y2,
miniwin.rect_edge_raised,
miniwin.rect_edge_at_all + miniwin.rect_option_fill_middle) -- raised, filled
WindowText (hotspot_info.window, hotspot_info.font, hotspot_info.text, hotspot_info.text_x, hotspot_info.y1 + 8, 0, 0, ColourNameToRGB "black", true)
Redraw ()
end -- button_mouse_up
-- -----------------------------------------------------------------
-- make_button - make a button for the dialog window and remember its handler
-- -----------------------------------------------------------------
function make_button (window, font, x, y, text, tooltip, handler)
WindowRectOp (window, miniwin.rect_draw_edge, x, y, x + LEARN_BUTTON_WIDTH, y + LEARN_BUTTON_HEIGHT,
miniwin.rect_edge_raised,
miniwin.rect_edge_at_all + miniwin.rect_option_fill_middle) -- raised, filled
local width = WindowTextWidth (window, font, text, true)
local text_x = x + (LEARN_BUTTON_WIDTH - width) / 2
WindowText (window, font, text, text_x, y + 8, 0, 0, ColourNameToRGB "black", true)
local hotspot_id = string.format ("HS_learn_%d,%d", x, y)
-- remember handler function
hotspots [hotspot_id] = { handler = handler,
window = window,
x1 = x, y1 = y,
x2 = x + LEARN_BUTTON_WIDTH, y2 = y + LEARN_BUTTON_HEIGHT,
font = font,
text = text,
text_x = text_x }
WindowAddHotspot(window,
hotspot_id,
x, y, x + LEARN_BUTTON_WIDTH, y + LEARN_BUTTON_HEIGHT,
"", -- MouseOver
"", -- CancelMouseOver
"button_mouse_down", -- MouseDown
"button_cancel_mouse_down", -- CancelMouseDown
"button_mouse_up", -- MouseUp
tooltip, -- tooltip text
miniwin.cursor_hand, -- mouse cursor shape
0) -- flags
end -- make_button
-- -----------------------------------------------------------------
-- update_buttons - grey-out buttons if nothing selected
-- -----------------------------------------------------------------
buttons_active = nil
-- stuff for warning them to save their file
time_last_saved = os.time ()
time_last_warned = nil
TIME_BETWEEN_SAVES = 15 * 60 -- warn if they haven't saved for 30 minutes
TIME_BETWEEN_WARNINGS = 1 * 60 -- warn every 1 minute
ADDED_ROOMS_COUNT = 10 -- warn if they have added this many rooms
function update_buttons (name)
-- to save memory, throw away info for lines more than 1000 further back in the buffer
local this_line = GetLinesInBufferCount() -- which line in the output buffer
local line_number = GetLineInfo (this_line, 10) -- which line this was overall
local wanted_line_number = line_number - 1000 -- keep info for 1000 lines
if line_number then
for k in pairs (deduced_line_types) do
if k < wanted_line_number then
deduced_line_types [k] = nil
end -- for
end -- for
end -- if we have any lines
-- warn user if database not saved after adding rooms
-- how long since the last save
local time_since_save = os.difftime (os.time (), time_last_saved)
-- if they have added a few rooms and not saved then warn them
if rooms_added >= ADDED_ROOMS_COUNT and -- added a few rooms
time_since_save > TIME_BETWEEN_SAVES and -- not saved for a while
(time_last_warned == nil or os.difftime (os.time (), time_last_warned) >= TIME_BETWEEN_WARNINGS) then -- warn quite often after that time elapsed
mapper.maperror (string.format ("WARNING: You have added %d rooms, but have not saved your world file recently.", rooms_added))
mapper.mapprint ("Recommended: Save your world file (Ctrl+S) which will also save the mapper database.")
time_last_warned = os.time ()
end -- if
-- do nothing if button pressed