-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathMapper_Telnet.xml
1027 lines (798 loc) · 29.9 KB
/
Mapper_Telnet.xml
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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE muclient>
<muclient>
<plugin
name="Mapper_Telnet"
author="Nick Gammon"
id="6069eacf376f5e9dbfce901f"
language="Lua"
purpose="Draws room map"
date_written="2010-02-24"
requires="4.50"
version="1.0"
save_state="y"
>
<description trim="y">
<![CDATA[
Automatic mapper.
The window can be dragged to a new location with the mouse.
]]>
</description>
</plugin>
<aliases>
<alias
match="map zoom out"
enabled="y"
send_to="12"
sequence="100"
>
<send>
if last_drawn and ROOM_SIZE > 4 then
ROOM_SIZE = ROOM_SIZE - 2
DISTANCE_TO_NEXT_ROOM = DISTANCE_TO_NEXT_ROOM - 2
build_room_info ()
draw_everything (last_drawn)
end -- if
</send>
</alias>
<alias
match="map zoom in"
enabled="y"
send_to="12"
sequence="100"
>
<send>
if last_drawn and ROOM_SIZE < 40 then
ROOM_SIZE = ROOM_SIZE + 2
DISTANCE_TO_NEXT_ROOM = DISTANCE_TO_NEXT_ROOM + 2
build_room_info ()
draw_everything (last_drawn)
end -- if
</send>
</alias>
<alias
match="^map find ([\w*]+)$"
enabled="y"
sequence="100"
script="map_find"
regexp="y"
>
</alias>
</aliases>
<!-- Script -->
<script>
<![CDATA[
FONT_NAME = "Fixedsys" -- the font we want to use
FONT_SIZE = 9
FONT_ID = "fn" -- internal font identifier
-- size of map window
WINDOW_WIDTH = 300
WINDOW_HEIGHT = 300
-- size of room box
ROOM_SIZE = 10
-- how far away to draw rooms from each other
DISTANCE_TO_NEXT_ROOM = 15
-- how far from where we are standing to draw (rooms)
MAX_DEPTH = 30
-- assorted colours
BACKGROUND_COLOUR = ColourNameToRGB "lightseagreen"
ROOM_COLOUR = ColourNameToRGB "cyan"
EXIT_COLOUR = ColourNameToRGB "darkgreen"
EXIT_COLOUR_UP_DOWN = ColourNameToRGB "darkmagenta"
OUR_ROOM_COLOUR = ColourNameToRGB "black"
UNKNOWN_ROOM_COLOUR = ColourNameToRGB "#00CACA"
SHOP_FILL_COLOUR = ColourNameToRGB "darkolivegreen"
TRAINER_FILL_COLOUR = ColourNameToRGB "yellowgreen"
BLACKSMITH_FILL_COLOUR = ColourNameToRGB "darkgray"
HEALER_FILL_COLOUR = ColourNameToRGB "gold"
require "movewindow"
require "serialize"
require "copytable"
require "checkplugin"
local item_cache = {}
-- check if single item cached
local function check_item_cache (hash)
-- get item if we don't have it
if not item_cache [hash] then
item_cache [hash] = item_ppi.get_item (hash)
end -- if not cached
end -- check_item_cache
local rooms = {}
room_not_in_database = {}
room_in_database = {}
function dbcheck (code)
if code ~= sqlite3.OK and -- no error
code ~= sqlite3.ROW and -- completed OK with another row of data
code ~= sqlite3.DONE then -- completed OK, no more rows
local err = db:errmsg () -- the rollback will change the error message
db:exec ("ROLLBACK") -- rollback any transaction to unlock the database
error (err, 2) -- show error in caller's context
end -- if
end -- dbcheck
function fixsql (s)
if s then
return "'" .. (string.gsub (s, "'", "''")) .. "'" -- replace single quotes with two lots of single quotes
else
return "NULL"
end -- if
end -- fixsql
function fixbool (b)
if b then
return 1
else
return 0
end -- if
end -- fixbool
function load_room_from_database (uid)
local room
-- if not in database, don't look again
if room_not_in_database [uid] then
return nil
end -- no point looking
for row in db:nrows(string.format ("SELECT * FROM rooms WHERE uid = %s", fixsql (uid))) do
room = {
name = row.name,
terrain = row.terrain,
shop = row.shop == 1,
train = row.train == 1,
repair = row.repair == 1,
healer = row.healer == 1,
exits = {} }
for exitrow in db:nrows(string.format ("SELECT * FROM exits WHERE fromuid = %s", fixsql (uid))) do
room.exits [exitrow.dir] = {
uid = tostring (exitrow.touid),
closed = exitrow.closed == 1,
locked = exitrow.locked == 1,
door = exitrow.door == 1,
}
end -- for each exit
end -- finding room
if room then
rooms [uid] = room
return room
end -- if found
room_not_in_database [uid] = true
return nil
end -- load_room_from_database
function save_room_to_database (uid, room)
if room_in_database [uid] then
return
end -- already there
for row in db:nrows(string.format ("SELECT uid FROM rooms WHERE uid = %s", fixsql (uid))) do
room_in_database [uid] = true
end -- finding room
if room_in_database [uid] then
return
end -- already there
dbcheck (db:execute (string.format ([[
INSERT INTO rooms (uid, name, desc, terrain, shop, train, repair, healer, date_added)
VALUES (%s, %s, %s, %s, %i, %i, %i, %i, DATETIME('NOW'));
]],
fixsql (uid),
fixsql (room.name),
fixsql (room.desc),
fixsql (room.terrain),
fixbool (room.shop),
fixbool (room.train),
fixbool (room.repair),
fixbool (room.healer)
)))
-- for full-text searching
dbcheck (db:execute (string.format ([[
INSERT INTO rooms_lookup (uid, name, desc) VALUES (%s, %s, %s);
]], fixsql (uid),
fixsql (room.name),
fixsql (room.desc)
)))
for dir, exit in pairs (room.exits) do
dbcheck (db:execute (string.format ([[
INSERT INTO exits (dir, fromuid, touid, closed, locked, door, date_added)
VALUES (%s, %s, %s, %i, %i, %i, DATETIME('NOW'));
]], fixsql (dir), -- direction (eg. "n")
fixsql (uid), -- from current room
fixsql (exit.uid), -- destination room
fixbool (exit.closed), -- exit closed
fixbool (exit.locked), -- exit locked
fixbool (exit.door) -- is a door
)))
end -- for each exit
room_not_in_database [uid] = false
room_in_database [uid] = true
end -- function save_room_to_database
function OnPluginInstall ()
win = GetPluginID ()
WindowCreate (win, 0, 0, 0, 0, 0, 0, 0)
-- add the font
WindowFont (win, FONT_ID, FONT_NAME, FONT_SIZE)
-- see how high it is
font_height = WindowFontInfo (win, FONT_ID, 1) -- height
-- find where window was last time
windowinfo = movewindow.install (win, 7)
-- open database on disk (uses world address, that is which MUD we are playing)
db = assert (sqlite3.open( GetInfo (66) .. Trim (WorldAddress ()) .. "_" .. WorldPort () .. ".db"))
-- create rooms table
dbcheck (db:execute[[
PRAGMA foreign_keys = ON;
PRAGMA journal_mode = WAL;
CREATE TABLE IF NOT EXISTS
rooms(
roomid INTEGER PRIMARY KEY AUTOINCREMENT,
uid STRING NOT NULL, -- vnum or how the MUD identifies the room
name TEXT, -- name of room
desc, TEXT, -- description
terrain TEXT,
shop INTEGER,
train INTEGER,
repair INTEGER,
healer INTEGER,
notes TEXT,
date_added DATE, -- date added to database
UNIQUE (uid)
);
CREATE TABLE IF NOT EXISTS
exits(
exitid INTEGER PRIMARY KEY AUTOINCREMENT,
dir TEXT NOT NULL, -- direction, eg. "n", "s"
fromuid STRING NOT NULL, -- exit from which room (in rooms table)
touid STRING NOT NULL, -- exit to which room (in rooms table)
closed INTEGER, -- true if closed
locked INTEGER, -- true if locked
door INTEGER, -- true if a door
date_added DATE, -- date added to database
FOREIGN KEY(fromuid) REFERENCES rooms(uid)
);
CREATE INDEX IF NOT EXISTS fromuid_index ON exits (fromuid);
CREATE INDEX IF NOT EXISTS touid_index ON exits (touid);
]])
-- check if rooms_lookup table exists
local table_exists
for a in db:nrows "SELECT * FROM sqlite_master WHERE name = 'rooms_lookup' AND type = 'table'" do
table_exists = true
end -- for
if not table_exists then
dbcheck (db:execute "CREATE VIRTUAL TABLE rooms_lookup USING FTS3(uid, name, desc, notes);")
end -- if
build_room_info ()
end -- OnPluginInstall
function OnPluginEnable ()
WindowShow (win, true)
end -- OnPluginDisable
function OnPluginDisable ()
WindowShow (win, false)
end -- OnPluginDisable
-- hide window on removal
function OnPluginClose ()
WindowShow (win, false) -- hide it
-- close database
db:close()
end -- OnPluginClose
function OnPluginSaveState ()
movewindow.save_state (win)
end -- OnPluginSaveState
function build_speedwalk (path)
-- build speedwalk string (collect identical directions)
local tspeed = {}
for _, dir in ipairs (path) do
local n = #tspeed
if n == 0 then
table.insert (tspeed, { dir = dir, count = 1 })
else
if tspeed [n].dir == dir then
tspeed [n].count = tspeed [n].count + 1
else
table.insert (tspeed, { dir = dir, count = 1 })
end -- if different direction
end -- if
end -- for
if #tspeed == 0 then
return
end -- nowhere to go (current room?)
-- now build string like: 2n3e4(sw)
local s = "#" -- speedwalk prefix
for _, dir in ipairs (tspeed) do
if dir.count > 1 then
s = s .. dir.count
end -- if
if #dir.dir == 1 then
s = s .. dir.dir
else
s = s .. "(" .. dir.dir .. ")"
end -- if
s = s .. " "
end -- if
return s
end -- build_speedwalk
function mouseup_room (flags, hotspot_id)
local uid = hotspot_id
if bit.band (flags, 0x20) == 0 then
if rooms [uid].shop then
Execute "list" -- this is kinda silly, we have to be *in* the room
end -- if
return
end -- if not RH click
-- here for RH click
local sw = speedwalks [uid]
if not sw then
return
end -- not found
s = build_speedwalk (sw)
-- get client to do it
Execute (s)
end -- mouseup_player
function build_room_info ()
HALF_ROOM = ROOM_SIZE / 2
HALF_WAY = DISTANCE_TO_NEXT_ROOM / 2
THIRD_WAY = DISTANCE_TO_NEXT_ROOM / 3
-- how to draw a line from this room to the next one (relative to the center of the room)
connectors = {
n = { x1 = 0, y1 = - HALF_ROOM, x2 = 0, y2 = - HALF_ROOM - DISTANCE_TO_NEXT_ROOM, at = { 0, -1 } },
s = { x1 = 0, y1 = HALF_ROOM, x2 = 0, y2 = HALF_ROOM + DISTANCE_TO_NEXT_ROOM, at = { 0, 1 } },
e = { x1 = HALF_ROOM, y1 = 0, x2 = HALF_ROOM + DISTANCE_TO_NEXT_ROOM, y2 = 0, at = { 1, 0 }},
w = { x1 = - HALF_ROOM, y1 = 0, x2 = - HALF_ROOM - DISTANCE_TO_NEXT_ROOM, y2 = 0, at = { -1, 0 }},
ne = { x1 = HALF_ROOM, y1 = - HALF_ROOM, x2 = HALF_ROOM + DISTANCE_TO_NEXT_ROOM , y2 = - HALF_ROOM - DISTANCE_TO_NEXT_ROOM, at = { 1, -1 } },
se = { x1 = HALF_ROOM, y1 = HALF_ROOM, x2 = HALF_ROOM + DISTANCE_TO_NEXT_ROOM , y2 = HALF_ROOM + DISTANCE_TO_NEXT_ROOM, at = { 1, 1 } },
nw = { x1 = - HALF_ROOM, y1 = - HALF_ROOM, x2 = - HALF_ROOM - DISTANCE_TO_NEXT_ROOM , y2 = - HALF_ROOM - DISTANCE_TO_NEXT_ROOM, at = {-1, -1 } },
sw = { x1 = - HALF_ROOM, y1 = HALF_ROOM, x2 = - HALF_ROOM - DISTANCE_TO_NEXT_ROOM , y2 = HALF_ROOM + DISTANCE_TO_NEXT_ROOM, at = {-1, 1 } },
} -- end connectors
-- how to draw a stub line
half_connectors = {
n = { x1 = 0, y1 = - HALF_ROOM, x2 = 0, y2 = - HALF_ROOM - THIRD_WAY, at = { 0, -1 } },
s = { x1 = 0, y1 = HALF_ROOM, x2 = 0, y2 = HALF_ROOM + THIRD_WAY, at = { 0, 1 } },
e = { x1 = HALF_ROOM, y1 = 0, x2 = HALF_ROOM + THIRD_WAY, y2 = 0, at = { 1, 0 }},
w = { x1 = - HALF_ROOM, y1 = 0, x2 = - HALF_ROOM - THIRD_WAY, y2 = 0, at = { -1, 0 }},
ne = { x1 = HALF_ROOM, y1 = - HALF_ROOM, x2 = HALF_ROOM + THIRD_WAY , y2 = - HALF_ROOM - THIRD_WAY, at = { 1, -1 } },
se = { x1 = HALF_ROOM, y1 = HALF_ROOM, x2 = HALF_ROOM + THIRD_WAY , y2 = HALF_ROOM + THIRD_WAY, at = { 1, 1 } },
nw = { x1 = - HALF_ROOM, y1 = - HALF_ROOM, x2 = - HALF_ROOM - THIRD_WAY , y2 = - HALF_ROOM - THIRD_WAY, at = {-1, -1 } },
sw = { x1 = HALF_ROOM, y1 = - HALF_ROOM, x2 = HALF_ROOM + THIRD_WAY , y2 = - HALF_ROOM - THIRD_WAY, at = {-1, 1 } },
} -- end half_connectors
-- how to draw one-way arrows (relative to the center of the room)
arrows = {
n = { - 2, - HALF_ROOM - 2, 2, - HALF_ROOM - 2, 0, - HALF_ROOM - 6 },
s = { - 2, HALF_ROOM + 2, 2, HALF_ROOM + 2, 0, HALF_ROOM + 6 },
e = { HALF_ROOM + 2, -2, HALF_ROOM + 2, 2, HALF_ROOM + 6, 0 },
w = { - HALF_ROOM - 2, -2, - HALF_ROOM - 2, 2, - HALF_ROOM - 6, 0 },
ne = { HALF_ROOM + 3, - HALF_ROOM, HALF_ROOM + 3, - HALF_ROOM - 3, HALF_ROOM, - HALF_ROOM - 3 },
se = { HALF_ROOM + 3, HALF_ROOM, HALF_ROOM + 3, HALF_ROOM + 3, HALF_ROOM, HALF_ROOM + 3 },
nw = { - HALF_ROOM - 3, - HALF_ROOM, - HALF_ROOM - 3, - HALF_ROOM - 3, - HALF_ROOM, - HALF_ROOM - 3 },
sw = { - HALF_ROOM - 3, HALF_ROOM, - HALF_ROOM - 3, HALF_ROOM + 3, - HALF_ROOM, HALF_ROOM + 3},
} -- end of arrows
end -- build_room_info
-- for calculating one-way paths
inverse_direction = {
n = "s",
s = "n",
e = "w",
w = "e",
u = "d",
d = "u",
ne = "sw",
sw = "ne",
nw = "se",
se = "nw",
} -- end of inverse_direction
-- how to colour stuff
terrain_colours = {
inside = "wheat",
city = "wheat",
field = "green",
forest = "green",
hills = "green",
mountain = "green",
water_swim = "blue",
water_noswim = "blue",
underwater = "darkblue",
air = "lemonchiffon",
desert = "lightgoldenrodyellow",
dunno = "green",
oceanfloor = "green",
underground = "green",
lava = "red",
swamp = "green",
} -- end of terrain_colours
function make_particle (uid, hash, path, x, y)
particle_count = particle_count + 1
local path = path or {}
return {uid=uid, hash=hash, path=path, x = x, y = y}
end
function draw_room (uid, hash, path, x, y)
local coords = string.format ("%i,%i", x, y)
if drawn [uid] then
return
end -- done this one
-- don't draw the same room more than once
drawn [uid] = { coords = coords, path = path }
local room = rooms [uid]
-- not cached - see if in database
if not room then
-- print (uid, "not in memory, checking database ...")
room = load_room_from_database (uid)
end -- not in cache
if hash then
check_item_cache (hash)
end -- have hash
room = room or { name = "<Unexplored room>", exits = {}, unknown = true }
local room_data = item_cache [hash]
if room_data then
for k, v in pairs (room_data) do
if not room [k] then
room [k] = v
end -- if not there already
end -- for
end -- if room_data not known
local left, top, right, bottom = x - HALF_ROOM, y - HALF_ROOM, x + HALF_ROOM, y + HALF_ROOM
-- forget it if off screen
if x < HALF_ROOM or y < HALF_ROOM or
x > WINDOW_WIDTH - HALF_ROOM or y > WINDOW_HEIGHT - HALF_ROOM then
return
end -- if
-- exits
local texits = {}
for name, exit in pairs (room.exits) do
local exit_guid = exit.uid
local exit_hash = exit.hash
table.insert (texits, name)
local exit_info = connectors [name]
local stub_exit_info = half_connectors [name]
local exit_line_colour = EXIT_COLOUR
local arrow = arrows [name]
-- draw up in the ne/nw position if not already an exit there at this level
if name == "u" then
if not room.exits.nw then
exit_info = connectors.nw
stub_exit_info = half_connectors.nw
arrow = arrows.nw
exit_line_colour = EXIT_COLOUR_UP_DOWN
elseif not room.exits.ne then
exit_info = connectors.ne
stub_exit_info = half_connectors.ne
arrow = arrows.ne
exit_line_colour = EXIT_COLOUR_UP_DOWN
end -- if
elseif name == "d" then
if not room.exits.se then
exit_info = connectors.se
stub_exit_info = half_connectors.se
arrow = arrows.se
exit_line_colour = EXIT_COLOUR_UP_DOWN
elseif not room.exits.sw then
exit_info = connectors.sw
stub_exit_info = half_connectors.sw
arrow = arrows.sw
exit_line_colour = EXIT_COLOUR_UP_DOWN
end -- if
end -- if down
if exit_info then
local linetype = 0 -- unbroken
if not rooms [exit_guid] then
linetype = 2 -- dots
end -- if
local next_x = x + exit_info.at [1] * (ROOM_SIZE + DISTANCE_TO_NEXT_ROOM)
local next_y = y + exit_info.at [2] * (ROOM_SIZE + DISTANCE_TO_NEXT_ROOM)
local next_coords = string.format ("%i,%i", next_x, next_y)
-- print ("Drawing", uid, "exit", name, "exit_guid", exit_guid)
--[[
-- debug yadda yadda
if uid == "10300" then
print ("exit=", name, "exit_guid=", exit_guid, "next_coords=", next_coords)
print ("drawn [exit_guid]=", drawn [exit_guid])
if drawn [exit_guid] then
print ("drawn [exit_guid].coords=", drawn [exit_guid].coords)
end -- if
end -- if
--]]
-- if another room (not where this one leads to) is already there, only draw "stub" lines
if drawn_coords [next_coords] and
(not drawn [exit_guid] or drawn [exit_guid].coords ~= next_coords) then
exit_info = stub_exit_info
else
-- remember to draw room next iteration
local new_path = copytable.deep (path)
table.insert (new_path, name)
table.insert (particles, make_particle (exit_guid, exit_hash, new_path, next_x, next_y))
drawn_coords [next_coords] = true
end -- if drawn on this spot
WindowLine (win, x + exit_info.x1, y + exit_info.y1, x + exit_info.x2, y + exit_info.y2, exit_line_colour, linetype, 1)
-- one-way exit?
if rooms [exit_guid] then
local dest = rooms [exit_guid]
-- if inverse direction doesn't point back to us, this is one-way
if dest.exits [inverse_direction [name]] == nil or dest.exits [inverse_direction [name]].uid ~= uid then
-- turn points into string, relative to where the room is
local points = string.format ("%i,%i,%i,%i,%i,%i",
x + arrow [1],
y + arrow [2],
x + arrow [3],
y + arrow [4],
x + arrow [5],
y + arrow [6])
-- draw arrow
WindowPolygon(win, points, exit_line_colour, 0, 1, exit_line_colour, 0, true, true)
end -- one way
end -- if we know of the room where it does
end -- if we know what to do with this direction
end -- for each exit
-- special room colours
if room.shop then
WindowRectOp (win, 2, left, top, right, bottom, SHOP_FILL_COLOUR)
elseif room.train then
WindowRectOp (win, 2, left, top, right, bottom, TRAINER_FILL_COLOUR)
elseif room.repair then
WindowRectOp (win, 2, left, top, right, bottom, BLACKSMITH_FILL_COLOUR)
elseif room.healer then
WindowRectOp (win, 2, left, top, right, bottom, HEALER_FILL_COLOUR)
else
-- use terrain colour
if room.terrain and terrain_colours [room.terrain] then
WindowRectOp (win, 2, left, top, right, bottom, ColourNameToRGB (terrain_colours [room.terrain]))
end -- if
end -- if
local bordercolour = ROOM_COLOUR
if uid == current_room then
bordercolour = OUR_ROOM_COLOUR
end -- if current room
if room.unknown then
bordercolour = UNKNOWN_ROOM_COLOUR
end -- if
-- draw current room (last, so it goes on top)
WindowRectOp (win, 1, left, top, right, bottom, bordercolour)
-- thicker border for our room
if uid == current_room then
WindowRectOp (win, 1, left + 1, top + 1, right - 1, bottom - 1, bordercolour)
end -- if current room
-- show up and down in case we can't get a line in
if room.exits.u then
WindowLine (win, left, top, left + ROOM_SIZE, top, EXIT_COLOUR_UP_DOWN, 0, 1)
end -- if
if room.exits.d then
WindowLine (win, left, bottom, left + ROOM_SIZE, bottom, EXIT_COLOUR_UP_DOWN, 0, 1)
end -- if
speedwalks [uid] = path -- so we know how to get here
WindowAddHotspot(win, uid,
left, top, right, bottom, -- rectangle
"", -- mouseover
"", -- cancelmouseover
"", -- mousedown
"", -- cancelmousedown
"mouseup_room", -- mouseup
string.format ("%s\tExits=%s\nRoom=%s", -- \nSteps=%i\nPath to=%s
room.name,
table.concat (texits, ", "),
uid
-- depth,
-- table.concat (path, ",")
), -- tooltip text
1, 0) -- hand cursor
end -- draw_room
function draw_everything (move)
local start_time = GetInfo (232)
local room_data = item_cache [move.hash]
-- tprint (room_data)
WindowCreate (win,
windowinfo.window_left,
windowinfo.window_top,
WINDOW_WIDTH, WINDOW_HEIGHT,
windowinfo.window_mode, -- top right
windowinfo.window_flags,
0)
-- let them move it around
movewindow.add_drag_handler (win, 0, 0, 0, 0)
local dark_message = "It is too dark to see."
local blind_message = "You are blind!"
local exits_message = "Exits:"
-- fill entire box to clear it
WindowRectOp (win, 2, 0, 0, 0, 0, BACKGROUND_COLOUR) -- fill entire box
-- Edge around box rectangle
WindowCircleOp (win, 3, 0, 0, 0, 0, ColourNameToRGB "darkgray", 0, 2, 0, 1)
if move.blind then
WindowText (win, FONT_ID, blind_message, 5, 5, 0, 0, ColourNameToRGB "darkred", true)
elseif move.dark then
WindowText (win, FONT_ID, dark_message, 5, 5, 0, 0, ColourNameToRGB "darkgreen", true)
else
vertical = 5 -- pixel to start at
-- WindowText (win, FONT_ID, move.uid, 5, vertical, 0, 0, ColourNameToRGB "saddlebrown", true)
vertical = vertical + font_height
if room_data.name then
-- WindowText (win, FONT_ID, room_data.name, 5, vertical, 0, 0, ColourNameToRGB "saddlebrown", true)
end -- if
-- initial room in middle
drawn, drawn_coords, particles, speedwalks = {}, {}, {}, {}
depth = 0
particle_count = 0
current_room = move.uid
-- insert initial room
table.insert (particles, make_particle (move.uid, move.hash, {}, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2))
while #particles > 0 and depth < MAX_DEPTH do
local old_generation = particles
particles = {} -- new generation
for i, part in ipairs (old_generation) do
draw_room (part.uid, part.hash, part.path, part.x, part.y)
end -- for each existing particle
depth = depth + 1
end -- while all particles
end -- if
-- 3D box
WindowCircleOp (win, 3, 0, 0, 0, 0, 0x505050, 0, 3, 0, 1) -- dark grey border (3 pixels)
WindowCircleOp (win, 3, 1, 1, -1, -1, 0x7C7C7C, 0, 1, 0, 1) -- lighter inner border
WindowCircleOp (win, 3, 2, 2, -2, -2, 0, 0, 1, 0, 1) -- black inside that
WindowLine (win, 1, 1, WINDOW_WIDTH - 1, 1, 0xC2C2C2, 0, 1) -- light top edge
WindowLine (win, 1, 1, 1, WINDOW_HEIGHT - 1, 0xC2C2C2, 0, 1) -- light left edge (for 3D look)
-- make sure window visible
WindowShow (win, true)
last_drawn = copytable.deep (move)
local end_time = GetInfo (232)
-- print (string.format ("Time to draw= %0.6f", end_time - start_time))
end -- draw_everything
function save_room_info (which_room)
-- print ("in save_room_info for", which_room.uid, which_room.hash)
if not room_in_database [which_room.uid] then
check_item_cache (which_room.hash)
local room = item_cache [which_room.hash] -- name, description, etc.
room.exits = {}
for exit, details in pairs (which_room.contents) do
if string.match (exit, "^r%.") then
room.exits [details.dir] = { uid = exit, closed = details.closed, locked = details.locked, door = details.door, }
end -- if an exit
end -- for each exit
save_room_to_database (which_room.uid, room)
end -- not already saved to disk
end -- save_room_info
function OnPluginBroadcast (msg, id, name, text)
if id == "928dc37b201539cd14239ff0" and msg == 1 then
-- print ("hash received:", text)
if wanted_hash [text] then
save_room_info (wanted_hash [text])
wanted_hash [text] = nil
end -- if
end -- if from Item_Cache_Helper.xml
end
wanted_hash = {}
-- here when location changes
function OnPluginTelnetOption (option)
local t = {} -- incoming server variables will go into table t
setfenv (assert (loadstring (option)), t) () -- compile and load into t
for k, v in pairs (t) do
-- print ("received", k, "uid=", v.uid or "<none>", "hash=", v.hash or "<none>")
end -- for
-- check Item_Cache_Helper.xml was installed
if not item_ppi then
return
end -- can't get room or item information
-- an "inroom" message also gives the location
if t.inroom then
t.location = {hash = t.inroom.hash, uid = t.inroom.uid}
check_item_cache (t.inroom.hash)
-- if we have the item cache for this room we know enough to save to database
if item_cache [t.inroom.hash] then
save_room_info (t.inroom)
else
wanted_hash [t.inroom.hash] = t.inroom -- will write it when hash arrives
end -- if
end -- if t.inroom
if t.location then
redisplay = nil -- room changed
else
-- we couldn't display all last time, and we got some cache info, try again
if redisplay then
check_item_cache (redisplay.hash)
if item_cache [redisplay.hash] then
t.location = redisplay
else
-- print ("return 1, waiting on", redisplay.hash)
return
end -- if
else
-- print ("return 2, no location, not waiting on hash")
return
end
end
current_room = t.location.uid
-- check we have current room information
check_item_cache (t.location.hash)
if not item_cache [t.location.hash] then
redisplay = t.location -- need to redisplay when cache info arrives
-- print ("return 3, waiting on", t.location.hash)
return
end -- if
-- print ("Drawing", t.location.uid, t.location.hash)
draw_everything (t.location)
end -- function OnPluginTelnetOption
-- see: http://www.gammon.com.au/forum/?id=7306&page=2
-- Thanks to Ked.
function find_paths (start, destinations)
local function make_particle (curr_loc, prev_path)
local prev_path = prev_path or {}
return {current_room=curr_loc, path=prev_path}
end
local dest_length = #destinations
local dest_lookup = {}
local depth = 0
local explored_rooms, particles = {}, {}
-- this is where we collect found paths
-- the table is keyed by destination, with paths as values
local paths = {}
for i, room in pairs (destinations) do
dest_lookup[room.uid] = true
if room.uid == start then
paths[start] = {}
dest_length = dest_length - 1
end -- if already there
end
-- create particle for the initial room
table.insert (particles, make_particle (start) )
while #particles > 0 and depth < 30 do
-- create a new generation of particles
new_generation = {}
depth = depth + 1
-- process each active particle
for i, part in ipairs (particles) do
if not rooms [part.current_room] then
load_room_from_database (part.current_room)
end -- if not in memory yet
-- if room doesn't exist, forget it
if rooms [part.current_room] then
-- get a list of exits from the current room
exits = rooms [part.current_room].exits
-- create one new particle for each exit
for dir, dest in pairs(exits) do
-- if we've been in this room before, drop it
if not explored_rooms[dest.uid] then
explored_rooms[dest.uid] = true
new_path = copytable.deep (part.path)
table.insert(new_path, dir)
-- if this room is in the list of destinations then save its path
if dest_lookup[dest.uid] then
paths[dest.uid] = new_path
dest_length = dest_length - 1
end -- found one!
-- make a new particle in the new room
table.insert(new_generation, make_particle(dest.uid, new_path))
end -- not explored this room
end -- for each exit
end -- if room exists
end -- for each particle
-- check if all destinations have been reached
if dest_length == 0 then
return paths
end -- if
particles = new_generation
end -- while more particles
return paths
end -- function find_paths
function map_find (name, line, wildcards)
if not current_room then
print ("We don't know where you are right now.")
return
end -- if
local rooms = room_ppi.find_rooms (wildcards [1])
if #rooms == 0 then
Note ("No rooms matched.")
end
local paths = find_paths (current_room, rooms)
table.sort (rooms, function (a, b)
local ap, bp = paths [a.uid], paths [b.uid]
local al, bl = 0, 0
if ap then
al = #ap
end -- if
if bp then