-
Notifications
You must be signed in to change notification settings - Fork 4
/
LucasTerra.lua
executable file
·7051 lines (6280 loc) · 208 KB
/
LucasTerra.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
-- [[MAP START]] --
-- CONSTANTS
-- GENERAL FUNCTIONS
-- LOCAL FUNCTIONS
-- EXTENSION FUNCTIONS
-- [[MAP END]] --
LUCAS_LIB = '16.8'
LIBS = LIBS or {}
LIBS.LUCAS = LUCAS_LIB
-- Item Properties
ITEM_NOTROOFEDGE = 0
ITEM_TOPORDER1 = 1 --always on top
ITEM_TOPORDER2 = 2 --walktrough (doors)
ITEM_TOPORDER3 = 3 --walktrough (arcs)
ITEM_CONTAINER = 4
ITEM_STACKABLE = 5
ITEM_CORPSE = 6
ITEM_USEABLE = 7
ITEM_WRITEABLE = 8
ITEM_READABLE = 9
ITEM_FLUIDCONTAINER = 10
ITEM_SPLASH = 11
ITEM_BLOCKWALK = 12
ITEM_NOTMOVEABLE = 13
ITEM_BLOCKSHOTS = 14
ITEM_BLOCKPATHS = 15
ITEM_NOTMOVEMENTANIMATION = 16
ITEM_PICKUPABLE = 17
ITEM_HANGABLE = 18
ITEM_HORIZONTAL = 19
ITEM_VERTICAL = 20
ITEM_ROTATEABLE = 21
ITEM_LIGHTFONT = 22
ITEM_UNKNOWN = 23
ITEM_FLOORCHANGE = 24
ITEM_OFFSET = 25
ITEM_HEIGHTED = 26
ITEM_BIGSPRITE = 27
ITEM_UNEXIST = 28
ITEM_MINIMAP = 29
ITEM_ACTION = 30
ITEM_GROUND = 31
ITEM_DIAGONALBORDER,ITEM_BORDER = 32,32
ITEM_EQUIPPABLE = 33
ITEM_MARKET = 34
ITEM_HASDEFAULTFLAG = 35
-- Credits to Anonymickey, for testing the msg types
MSG_STATUS = 0 -- Status messages (In the screen).
MSG_DEFAULT = 1 -- Any player/npc message in Default channel.
MSG_WHISPER = 2 -- Any player whispering.
MSG_YELL = 3 -- Any player yelling.
MSG_NPC = 6 -- Any NPC answers in NPC channel.
MSG_PVT = 7 -- Private messages from other players.
MSG_CHANNEL = 8 -- Any player message in Game/Help/Real/Tutor/Trade channel.
MSG_RED = 16 -- Red alert messages.
MSG_RAID,MSG_ADVANCE = 17,17 -- Any white alert message.
MSG_WELCOME = 18 -- Game/Channel welcome messages.
MSG_STATUSLOG = 19 -- Status messages in Server Log.
MSG_INFO = 20 -- Green messages (like loot message) in Server Log.
MSG_SENT = 22 -- Private messages sent by you.
-- Liquid Types
LIQUID_NONE = 0
LIQUID_WATER = 1
LIQUID_WINE = 2
LIQUID_BEER = 3
LIQUID_BLOOD = 5
LIQUID_SLIME = 6
LIQUID_OIL = 7
LIQUID_LEMONADE = 12
LIQUID_MILK = 0
LIQUID_MANAFLUID = 0
LIQUID_LIFEFLUID = 0
LIQUID_URINE = 0
LIQUID_COCONUT_MILK = 0
LIQUID_MUD = 0
LIQUID_FRUIT_JUICE = 0
LIQUID_LAVA = 0
LIQUID_RUM = 0
LIQUID_SWAMP = 0
LIQUID_TEA = 0
LIQUID_MEAD = 0
-- Skull Types
SKULL_NOSKULL = 0
SKULL_YELLOW = 1
SKULL_GREEN = 2
SKULL_WHITE = 3
SKULL_RED = 4
SKULL_BLACK = 5
SKULL_ORANGE = 6
-- WarBanner Types
WAR_NOWAR = 0
WAR_GREEN = 1 --Friend
WAR_RED = 2 --Enemy
WAR_BLUE = 3 --In a War that you're not fighting
-- Party Types
PARTY_NOPARTY = 0
PARTY_INVITED_LEADER = 1 -- Leader inviting you to party
PARTY_INVITED_MEMBER = 2 -- Member invited to party
PARTY_ONPARTY_MEMBER = 3 -- Member of a party
PARTY_ONPARTY_LEADER = 4 -- Leader of the party
PARTY_EXPSHARE_OK_MEMBER = 5 -- Exp Share Working Fine, member of a party
PARTY_EXPSHARE_OK_LEADER = 6 -- Exp Share Working Fine, leader of the party
PARTY_EXPSHARE_WAIT_MEMBER = 7 -- Exp Share on standby, member of a party
PARTY_EXPSHARE_WAIT_LEADER = 8 -- Exp Share on standby, leader of the party
PARTY_EXPSHARE_OFF_MEMBER = 9 -- Exp Disabled because of a low level char or player in different floor, member of a party
PARTY_EXPSHARE_OFF_LEADER = 10 -- Exp Disabled because of a low level char or player in different floor, leader of the party
ASCII = {'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~'}
SCREEN_LEFT = -8
SCREEN_TOP = -6
SCREEN_BOTTOM = 7
SCREEN_RIGHT = 9
GLOBAL_HUNGRY = 0
GLOBAL_LASTYOUAREFULLMSG = 0
TEAM_NEUTRAL = 0
TEAM_ALLY = 1
TEAM_ENEMY = 2
TEAM_LEADER = 3
TEAM_SELF = 4
WALKABLE_IDS = {105, 140, 404, 477, 611, 620, 621, 623, 624, 831, 832, 833, 834, 867, 1998, 1999, 2000, 2118, 2119, 2121, 2122, 2123, 2124, 2126, 2127, 2131, 2132, 2134, 2135, 2137, 2138, 2140, 2314, 2315, 2316, 2319, 2346, 2347, 2348, 2349, 2350, 2351, 2352, 2353, 2354, 2355, 2356, 2357, 2358, 2359, 2360, 2361, 2362, 2363, 2364, 2365, 2366, 2367, 2368, 2369, 2370, 2371, 2372, 2373, 2374, 2375, 2376, 2377, 2378, 2379, 2380, 2381, 2382, 2383, 2384, 2385, 2402, 2403, 2404, 2405, 2406, 2407, 2408, 2409, 2410, 2411, 2412, 2413, 2414, 2415, 2416, 2417, 2418, 2419, 2420, 2421, 2422, 2423, 2424, 2425, 2426, 2431, 2432, 2433, 2434, 2441, 2442, 2443, 2444, 2469, 2471, 2472, 2473, 2478, 2480, 2481, 2482, 2483, 2484, 2485, 2486, 2519, 2523, 2775, 2776, 2777, 2778, 2779, 2780, 2781, 2782, 2783, 2784, 2785, 2786, 2787, 2788, 2789, 2790, 2791, 2792, 2793, 2794, 2795, 2796, 2797, 2798, 2799, 2800, 2801, 2802, 2803, 2804, 2805, 2806, 2807, 2808, 2809, 2810, 2811, 2812, 2981, 2983, 2984, 2985, 2987, 2988, 3503, 3504, 3514, 3652, 3666, 4054, 5086, 5087, 5088, 5463, 5674, 5675, 5915, 5916, 6114, 6115, 6116, 6117, 6355, 6356, 6357, 6358, 6359, 6360, 6361, 6362, 6371, 6372, 7160, 7161, 7162, 7163, 7164, 7165, 7166, 7169, 7170, 7266, 7267, 7288, 7864, 8519, 8520, 8521, 8522, 8529, 8530, 8763, 8823, 8824, 8825, 8859, 9061, 9062, 9100, 9630, 10181, 10182, 10207, 10209, 10216, 10286, 10287, 10288, 10978, 10979, 11010, 11011, 11012, 11013, 11540, 11797, 11798, 11799, 11800, 11801, 11802, 11803, 11804, 11805, 11806, 11807, 11808, 11809, 11810, 12020, 12021, 12022, 12477, 12573, 12636, 12637, 12638, 12639, 12743, 12796, 13646, 13650, 13969, 14237, 14741, 14756, 15151, 15152, 15153, 15154, 15155, 16173, 17152, 17359, 17360, 17361, 17362, 17363, 17364, 17365, 17366, 17367, 17807, 17808, 17972, 17974, 17975, 17977, 19113, 19114, 19116, 19200, 21152, 21300, 21391, 21462, 21463, 21465, 21500, 21842, 22076, 22077, 22164, 22165, 23366, 23403, 23404, 23405, 23406, 23407, 23408, 23409, 23410, 23411, 23414, 23417, 23418, 23423, 23424, 23425, 23426, 23427, 23428, 23429, 23430, 23456, 23457, 23458, 23459, 23460, 23473, 24512, 24525, 24526, 24527, 24528, 24794, 24861, 24862}
local ropes = {646, 3003, 9594, 9596, 9598}
local shovels = {3457, 5710, 9594, 9596, 9598}
local machetes = {3308, 3330, 9594, 9596, 9598}
local picks = {3456, 9594, 9596, 9598}
local closeddoorsids = {1628, 1629, 1631, 1632, 1638, 1640, 1642, 1644, 1646, 1648, 1650, 1651, 1653, 1654, 1656, 1658, 1660, 1662, 1664, 1666, 1668, 1669, 1671, 1672, 1674, 1676, 1678, 1680, 1682, 1683, 1685, 1687, 1689, 1691, 1692, 1694, 1696, 1698, 4912, 4913, 5006, 5007, 5082, 5084, 5097, 5098, 5100, 5102, 5104, 5106, 5107, 5109, 5111, 5113, 5115, 5116, 5118, 5120, 5122, 5124, 5125, 5127, 5129, 5131, 5133, 5134, 5136, 5137, 5139, 5140, 5142, 5143, 5277, 5278, 5280, 5281, 5282, 5285, 5287, 5289, 5291, 5293, 5302, 5303, 5514, 5516, 5732, 5735, 5745, 5749, 6191, 6192, 6194, 6195, 6197, 6199, 6201, 6203, 6205, 6207, 6248, 6249, 6251, 6252, 6254, 6258, 6260, 6262, 6264, 6435, 6436, 6439, 6440, 6443, 6444, 6449, 6450, 6453, 6454, 6457, 6458, 6461, 6462, 6465, 6466, 6469, 6470, 6788, 6789, 6891, 6892, 6894, 6896, 6898, 6900, 6901, 6903, 6905, 6907, 7027, 7028, 7033, 7034, 7036, 7038, 7040, 7042, 7043, 7045, 7047, 7049, 7051, 7052, 7711, 7712, 7714, 7715, 7717, 7719, 7721, 7723, 7725, 7727, 7868, 7941, 8249, 8250, 8252, 8253, 8255, 8257, 8258, 8261, 8263, 8265, 8351, 8352, 8354, 8355, 8357, 8359, 8361, 8363, 8365, 8367, 9347, 9348, 9351, 9352, 9354, 9355, 9357, 9359, 9361, 9363, 9365, 9367, 9551, 9552, 9554, 9556, 9558, 9560, 9561, 9563, 9565, 9567, 9571, 9572, 9858, 9859, 9863, 9865, 9867, 9868, 9872, 9874, 10147, 10149, 10151, 10153, 10155, 10157, 10520, 10521, 11232, 11233, 11237, 11239, 11241, 11242, 11246, 11248, 11705, 11714, 17560, 17561, 17563, 17565, 17567, 17569, 17570, 17572, 17574, 17576, 17700, 17701, 17703, 17705, 17707, 17709, 17710, 17712, 17714, 17716, 20443, 20444, 20446, 20448, 20450, 20452, 20453, 20455, 20457, 20459}
local openeddoorsids = {1630, 1633, 1639, 1641, 1643, 1645, 1647, 1649, 1652, 1655, 1657, 1659, 1661, 1663, 1665, 1667, 1670, 1673, 1675, 1677, 1679, 1681, 1684, 1686, 1688, 1690, 1693, 1695, 1697, 1699, 2178, 2180, 4911, 4914, 5083, 5085, 5099, 5101, 5103, 5105, 5108, 5110, 5112, 5114, 5117, 5117, 5119, 5121, 5123, 5126, 5128, 5130, 5132, 5135, 5138, 5141, 5144, 5279, 5282, 5284, 5286, 5288, 5290, 5292, 5294, 5515, 5734, 5737, 5746, 5748, 6193, 6196, 6198, 6200, 6202, 6204, 6206, 6208, 6250, 6253, 6255, 6257, 6259, 6261, 6263, 6265, 6893, 6895, 6897, 6899, 6902, 6904, 6906, 6908, 7035, 7037, 7039, 7041, 7044, 7046, 7048, 7050, 7713, 7716, 7718, 7720, 7722, 7724, 7726, 7728, 7869, 8251, 8254, 8256, 8258, 8260, 8262, 8264, 8266, 8353, 8356, 8358, 8360, 8362, 8364, 8366, 8368, 9353, 9356, 9358, 9360, 9362, 9364, 9366, 9368, 9553, 9555, 9557, 9559, 9562, 9564, 9566, 9568, 9860, 9864, 9866, 9869, 9873, 9875, 11234, 11238, 11240, 11243, 11247, 11249, 11708, 11716, 17562, 17564, 17566, 17568, 17571, 17573, 17575, 17577, 17702, 17704, 17706, 17708, 17711, 17713, 17715, 17717, 20445, 20447, 20449, 20451, 20454, 20456, 20458, 20460}
DEPOT = {
THAIS = 1,
CARLIN = 2,
KAZORDOON = 3,
ABDENDRIEL = 4,
EDRON = 5,
DARASHIA = 6,
VENORE = 7,
ANKRAHMUN = 8,
PORT_HOPE = 9,
LIBERTY_BAY = 10,
SVARGROND = 11,
YALAHAR = 12,
TRAVORA = 13,
FARMINE = 14,
GRAY_BEACH = 15,
ROSHAMUUL = 16,
RATHLETON = 17
}
local cityAreas = {
{DEPOT.THAIS, "thais", 32369, 32241, 100},
{DEPOT.CARLIN, "carlin", 32343, 31791, 60},
{DEPOT.KAZORDOON, "kazordoon", 32629, 31925, 60},
{DEPOT.KAZORDOON, "kazordoon", 32826, 31762, 15},
{DEPOT.ABDENDRIEL, "ab'dendriel", 32681, 31637, 70},
{DEPOT.EDRON, "edron", 33205, 31819, 60},
{DEPOT.DARASHIA, "darashia", 33238, 32435, 60},
{DEPOT.VENORE, "venore", 32957, 32076, 100},
{DEPOT.ANKRAHMUN, "ankrahmun", 33158, 32829, 100},
{DEPOT.PORT_HOPE, "port hope", 32623, 32763, 60},
{DEPOT.LIBERTY_BAY, "liberty bay", 32317, 32826, 80},
{DEPOT.SVARGROND, "svargrond", 32273, 31149, 80},
{DEPOT.YALAHAR, "yalahar", 32802, 31206, 80},
{DEPOT.TRAVORA, "travora", 32067, 32354, 20},
{DEPOT.FARMINE, "farmine", 33023, 31453, 60},
{DEPOT.GRAY_BEACH, "gray beach", 33447, 31323, 30},
{DEPOT.ROSHAMUUL, "roshamuul", 33553, 32379, 50},
{DEPOT.RATHLETON, "rathleton", 33627, 31913, 50}
}
-- LUA FUNCTIONS
-- @name currentcityname
-- @desc. Returns id and name of the city in which you are currently.
-- @returns integer, string
function currentcity()
for _, area in ipairs(cityAreas) do
if #area == 5 then
local id, city, x, y, ray = unpack(area)
if math.abs($posx - x) < ray and math.abs($posy - y) < ray then
return id, city
end
end
end
return 0, ''
end
function printf(str, ...)
return print(sprintf(str, ...))
end
function sprintf(str, ...)
return #{...} > 0 and tostring(str):format(...) or tostring(str)
end
function printerrorf(str, ...)
return printerror(sprintf(str, ...))
end
-- LOCAL FUNCTIONS
local function bininsert1asc(tablename,value)
local left, right
if type(tablename) == 'table' then
left,right = 1, #tablename
while left <= right do
local mid = math.floor((right+left)/2)
if tablename[mid] == value then
return mid
end
if tablename[mid] > value then
right = mid-1
else
left = mid+1
end
end
end
return left
end
local function bininsert2asc(tablename,value,argument,left,right)
if type(tablename) == 'table' then
left,right = left or 1, right or #tablename
while left <= right do
local mid = math.floor((right+left)/2)
if tablename[mid][argument] == value[argument] then
return mid
end
if tablename[mid][argument] > value[argument] then
right = mid-1
else
left = mid+1
end
end
end
return left
end
local function bininsert1desc(tablename,value)
local left, right
if type(tablename) == 'table' then
left,right = 1, #tablename
while left <= right do
local mid = math.floor((right+left)/2)
if tablename[mid] == value then
return mid
end
if tablename[mid] < value then
right = mid-1
else
left = mid+1
end
end
end
return left
end
local function bininsert2desc(tablename,value,argument,left,right)
if type(tablename) == 'table' then
left,right = left or 1, right or #tablename
while left <= right do
local mid = math.floor((right+left)/2)
if tablename[mid][argument] == value[argument] then
return mid
end
if tablename[mid][argument] < value[argument] then
right = mid-1
else
left = mid+1
end
end
end
return left
end
local function qs1(tablename, left, right)
if type(tablename) == 'table' then
left = left or 1
right = right or #tablename
local i,j,x
if right-left+1 <= 25 then
for i=left+1, right do
x=tablename[i]
j=i-1
while j>=left and x<tablename[j] do
tablename[j+1]=tablename[j]
j=j-1
end
tablename[j+1]=x
end
else
i,j,x = left, right, tablename[math.floor((left+right)/2)]
local temp
repeat
while tablename[i]<x and i < right do
i = i+1
end
while tablename[j]>x and j > left do
j = j-1
end
if (i <= j) then
temp = tablename[i]
tablename[i] = tablename[j]
tablename[j] = temp
i = i+1
j = j-1
end
until i > j
if (left < j) then
qs1(tablename, left, j)
end
if (right > i) then
qs1(tablename, i, right)
end
end
end
end
local function qs2(tablename, left, right, argument)
if type(tablename) == 'table' then
left = left or 1
right = right or #tablename
local i,j,x
if right-left+1 <= 25 then
for i=left+1, right do
x=tablename[i]
j=i-1
while j>=left and x[argument] < tablename[j][argument] do
tablename[j+1]=tablename[j]
j=j-1
end
tablename[j+1]=x
end
else
i,j,x = left, right, tablename[math.floor((left+right)/2)][argument]
local temp
repeat
while tablename[i][argument]<x and i < right do
i = i+1
end
while tablename[j][argument]>x and j > left do
j = j-1
end
if (i <= j) then
temp = tablename[i]
tablename[i] = tablename[j]
tablename[j] = temp
i = i+1
j = j-1
end
until i > j
if (left < j) then
qs2(tablename, left, j, argument)
end
if (right > i) then
qs2(tablename, i, right, argument)
end
end
end
end
local function qs3(tablename, left, right)
if type(tablename) == 'table' then
left = left or 1
right = right or #tablename
local i,j,x
if right-left+1 <= 25 then
for i=left+1, right do
x=tablename[i]
j=i-1
while j>=left and x>tablename[j] do
tablename[j+1]=tablename[j]
j=j-1
end
tablename[j+1]=x
end
else
i,j,x = left, right, tablename[math.floor((left+right)/2)]
local temp
repeat
while tablename[i]>x and i < right do
i = i+1
end
while tablename[j]<x and j > left do
j = j-1
end
if (i <= j) then
temp = tablename[i]
tablename[i] = tablename[j]
tablename[j] = temp
i = i+1
j = j-1
end
until i > j
if (left < j) then
qs3(tablename, left, j)
end
if (right > i) then
qs3(tablename, i, right)
end
end
end
end
local function qs4(tablename, left, right, argument)
if type(tablename) == 'table' then
left = left or 1
right = right or #tablename
local i,j,x
if right-left+1 <= 25 then
for i=left+1, right do
x=tablename[i]
j=i-1
while j>=left and x[argument]>tablename[j][argument] do
tablename[j+1]=tablename[j]
j=j-1
end
tablename[j+1]=x
end
else
i,j,x = left, right, tablename[math.floor((left+right)/2)][argument]
local temp
repeat
while tablename[i][argument]>x and i < right do
i = i+1
end
while tablename[j][argument]<x and j > left do
j = j-1
end
if (i <= j) then
temp = tablename[i]
tablename[i] = tablename[j]
tablename[j] = temp
i = i+1
j = j-1
end
until i > j
if (left < j) then
qs4(tablename, left, j, argument)
end
if (right > i) then
qs4(tablename, i, right, argument)
end
end
end
end
local function inssortedasc_2(tablename, value, argument, argpos, right)
local i = right
local arg1,arg2 = argument[argpos][1], argument[argpos-1][1]
while i >= 1 and tablename[i][arg2] == value[arg2] do
if (tablename[i][arg1] > value[arg1]) then
tablename[i+1] = tablename[i]
elseif (tablename[i][arg1] == value[arg1]) then
if (argpos < #argument) then
if argument[argpos+1][2] == 'asc' then
return inssortedasc_2(tablename, value, argument, argpos+1, i)
else
return inssorteddesc_2(tablename, value, argument, argpos+1, i)
end
else
tablename[i+1] = value
return true
end
else
tablename[i+1] = value
return true
end
i = i-1
end
tablename[i+1] = value
return true
end
local function inssorteddesc_2(tablename, value, argument, argpos, right)
local i = right
local arg1,arg2 = argument[argpos][1], argument[argpos-1][1]
while i >= 1 and tablename[i][arg2] == value[arg2] do
if (tablename[i][arg1] < value[arg1]) then
tablename[i+1] = tablename[i]
elseif (tablename[i][arg1] == value[arg1]) then
if (argpos < #argument) then
if argument[argpos+1][2] == 'asc' then
return inssortedasc_2(tablename, value, argument, argpos+1, i)
else
return inssorteddesc_2(tablename, value, argument, argpos+1, i)
end
else
tablename[i+1] = value
return true
end
else
tablename[i+1] = value
return true
end
i = i-1
end
tablename[i+1] = value
return true
end
local function inssorteddesc_1(tablename, value, argument)
local i = #tablename
local arg = argument[1][1]
while i >= 1 do
if (tablename[i][arg] < value[arg]) then
tablename[i+1] = tablename[i]
elseif (tablename[i][arg] == value[arg]) then
if (#argument > 1) then
if argument[2][2] == 'asc' then
return inssortedasc_2(tablename, value, argument, 2, i)
else
return inssorteddesc_2(tablename, value, argument, 2, i)
end
else
tablename[i+1] = value
return true
end
else
tablename[i+1] = value
return true
end
i = i-1
end
tablename[i+1] = value
return true
end
local function inssortedasc_1(tablename, value, argument)
local i = #tablename
local arg = argument[1][1]
while i >= 1 do
if (tablename[i][arg] > value[arg]) then
tablename[i+1] = tablename[i]
elseif (tablename[i][arg] == value[arg]) then
if (#argument > 1) then
if argument[2][2] == 'asc' then
return inssortedasc_2(tablename, value, argument, 2, i)
else
return inssorteddesc_2(tablename, value, argument, 2, i)
end
else
tablename[i+1] = value
return true
end
else
tablename[i+1] = value
return true
end
i = i-1
end
tablename[i+1] = value
return true
end
-- EXTENSION FUNCTIONS
-- @name table.insertsorted
-- @desc Inserts a value inside the table, sorted with the current elements.
-- @param self The table to use.
-- @param value The value to be inserted.
-- @param argument The argument pair key to insert.
-- @param order The order it should be inserted, 'asc' or 'desc'.
-- @param duplicated Allow or not duplicated elements.
-- @returns void
function table:insertsorted(value, argument, order, disallowduplicated)
if type(self) == 'table' then
if type(value) == 'userdata' or type(value) == 'table' then
if argument then
if type(argument) == 'table' then
for i=1,#argument do
if type(argument[i]) ~= 'table' then
argument[i] = {argument[i], 'asc'}
elseif not argument[i][2] then
argument[i][2] = 'asc'
end
end
if argument[1][2] == 'asc' then
inssortedasc_1(self, value, argument)
else
inssorteddesc_1(self, value, argument)
end
else
if order == true then
disallowduplicated = true
order = false
end
order = order or 'asc'
local postoinsert
if order == 'asc' then
postoinsert = bininsert2asc(self, value, argument)
else
postoinsert = bininsert2desc(self, value, argument)
end
table.insert(self, postoinsert, value)
return true
end
else
printerror('You must give some argument to order according to.')
end
else
if argument then
local temp = order
order = argument
disallowduplicated = temp
end
order = order or 'asc'
local postoinsert
if order == 'asc' then
postoinsert = bininsert1asc(self, value)
else
postoinsert = bininsert1desc(self, value)
end
table.insert(self, postoinsert, value)
return true
end
end
return false
end
-- @name table.id
-- @desc Filter all elements of the table into valid item ID's.
-- @param self The table to use.
-- @param inpairs Set 'true' to converts all possible elements, 'false' to convert numerical indexes.
-- @returns void
function table:id(inpairs)
if not inpairs then
local i = 1
while i <= #self do
if type(self[i]) == 'string' then
local iid = itemid(self[i])
if iid ~= 0 then
self[i] = iid
i = i+1
else
table.remove(self,i)
end
elseif type(self[i]) == 'number' then
i = i+1
else
table.remove(self,i)
end
end
else
for i,j in pairs(self) do
if type(j) == 'string' then
self[i] = itemid(j)
end
end
end
end
-- @name table.spell
-- @desc Filter all elements of the table into valid spells informations.
-- @param self The table to use.
-- @param inpairs Set 'true' to converts all possible elements, 'false' to convert numerical indexes.
-- @returns void
function table:spell(inpairs)
if not inpairs then
local i = 1
while i <= #self do
if type(self[i]) == 'string' then
local iid = spellinfo(self[i])
if iid then
self[i] = iid
i = i+1
else
table.remove(self,i)
end
elseif type(self[i]) == 'userdata' then
i = i+1
else
table.remove(self,i)
end
end
else
for i,j in pairs(self) do
if type(j) == 'string' then
local tsinfo = spellinfo(j)
if tsinfo then
self[i] = tsinfo
else
table.remove(self,i)
end
end
end
end
end
-- @name table.upper
-- @desc Converts all string elements of the table to uppercase.
-- @param self The table to use.
-- @param inpairs Set 'true' to converts all possible elements, 'false' to convert numerical indexes.
-- @returns void
function table:upper(inpairs)
if not inpairs then
for i,j in ipairs(self) do
if type(j) == 'string' then
self[i] = j:upper()
end
end
else
for i,j in pairs(self) do
if type(j) == 'string' then
self[i] = j:upper()
end
end
end
end
-- @name table.lower
-- @desc Converts all string elements of the table to lowercase.
-- @param self The table to use.
-- @param inpairs Set 'true' to converts all possible elements, 'false' to convert numerical indexes.
-- @returns void
function table:lower(inpairs)
if not inpairs then
for i,j in ipairs(self) do
if type(j) == 'string' then
self[i] = j:lower()
end
end
else
for i,j in pairs(self) do
if type(j) == 'string' then
self[i] = j:lower()
end
end
end
end
-- @name table.newsort
-- @desc Sorts the table elements between ascendant or descendant.
-- @param self The table to use.
-- @param argument The argument pair key to use. (optional)
-- @param order Order can be 'asc' or 'desc', to sort table. (optional)
-- @returns void
function table:newsort(argument, order)
if argument == 'asc' or argument == 'desc' then
order = argument
argument = false
end
order = order or 'asc'
if order == 'asc' then
if argument then
qs2(self,1,#self,argument)
else
qs1(self,1,#self)
end
else
if argument then
qs4(self,1,#self,argument)
else
qs3(self,1,#self)
end
end
end
-- @name table.stringformat
-- @desc Returns a string formatted as the table given.
-- @param self The table to use.
-- @param name The name of the table. (optional)
-- @param sep The separator between table arguments. (optional)
-- @returns string
function table:stringformat(name, sep)
local sep, str = sep or ' ', ''
for k, v in pairs(self) do
local t, n = type(v), type(k)
k = ((n ~= 'number' and tonumber(k) ~= nil) or tostring(k):match("[%s\'+]") ~= nil) and sprintf('["%s"]', k) or k
if t == 'string' then
str = str .. sprintf("%s,%s", (n == 'number' and sprintf('"%s"', v)) or sprintf('%s = "%s"', k, v), sep)
elseif t == 'number' then
str = str .. sprintf("%s,%s", (n == 'number' and v) or sprintf('%s = %s', k, v), sep)
elseif t == 'table' then
str = str .. sprintf("%s,%s", (n == 'number' and table.stringformat(v)) or sprintf('%s = %s', k, table.stringformat(v)), sep)
elseif t == 'userdata' and userdatastringformat then
str = str .. sprintf("%s, %s", (n == 'number' and userdatastringformat(v)) or sprintf('%s = %s', k, userdatastringformat(v)), sep)
end
end
return sprintf("%s{%s}", name and sprintf('%s = ', name) or '', str:sub(1, -(2 + #sep)))
end
-- @name table.find
-- @desc Searches for a string/table or number in the given table.
-- @param self The table to search.
-- @param value The element to find.
-- @param argument The argument pair key to search.
-- @returns mixed
function table:find(value, argument)
if type(self) == 'table' then
for k, v in ipairs(self) do
if v == value or (argument and v[argument] == value) then
return k
end
end
end
return nil
end
-- @name table.findcreature
-- @desc Searches for a creature object inside a table.
-- @param self The table to search.
-- @param m The creature to find.
-- @param arg The argument pair key to search.
-- @returns mixed
function table:findcreature(m, arg)
if type(m) == 'userdata' then
local name = m.name:lower()
if not arg then
for i,j in ipairs(self) do
if name == j or m.id == j or m == j then
return i
end
end
else
for i,j in ipairs(self) do
if name == j[arg] or m.id == j[arg] or m == j[arg] then
return i
end
end
end
end
end
-- @name table.binaryfind
-- @desc Uses the binary searching method to find a number on a table.
-- @param self The table to search.
-- @param value The number to search.
-- @param argument The sub pair key argument to search.
-- @returns mixed
function table:binaryfind(v, a)
local index, range = 1, #self
while index <= range do
local mid = math.floor((range + index) / 2)
if self[mid] == v or (a and self[mid][a] == v) then
return mid
elseif self[mid] > v or (a and self[mid][a] > v) then
range = mid - 1
else
index = mid + 1
end
end
return nil
end
-- @name string.fitcontent
-- @desc Makes sure the given string fits in the given size. If set, adds 'trailing' to end of it.
-- @param self The string to be converted.
-- @returns string
function string:fitcontent(sizex)
local w = measurestring(self)
if w > sizex then
local temp = ''
for i=1, #self do
temp = self:sub(1,i)..'...'
w = measurestring(temp)
if w < sizex then
self = temp
end
end
end
return self
end
-- @name string.concat
-- @desc Concatenate the given arguments to the current string.
-- @param self The initial string.
-- @param string¹, string², string* ... The strings to concatenate.
-- @returns string
function string:concat(...)
for _, str in pairs({...}) do
self = self .. sprintf(" %s", tostring(str))
end
return self
end
-- @name string.token
-- @desc Returns the given string into split by the separator into a table.
-- @param self The string.
-- @param num The token sequence you want to return, nothing will return the entire table. (optional)
-- @param delimiter The string separator. (optional)
-- @returns string
function string:token(n,delimiter)
delimiter = delimiter or ' +'
local result = {}
local from = 1
local delim_from, delim_to = self:find(delimiter,from)
while delim_from do
table.insert(result, self:sub(from,delim_from-1))
from = delim_to + 1
delim_from, delim_to = self:find(delimiter,from)
end
table.insert(result,self:sub(from))
if n then
return result[n]
end
return result
end
-- @name string.removews
-- @desc Removes duplicated spaces on the entire string.
-- @param self The string.
-- @returns string
function string:removews()
self = self:gsub("^%s*", "")
self = self:gsub("%s*$", "")
return self:gsub(" +", " ")
end
-- @name string.totable
-- @desc Converts a string into a table.
-- @param self The string to be converted.
-- @returns table
function string.totable(self)
local temp = loadstring(string.format("return {%s}", self))()
if #temp == 1 and type(temp[1]) == 'table' then
return temp[1]
end
return temp
end
-- @name string.attackername
-- @desc Returns the parsed information about the attack message given.
-- @param self The string.
-- @param info Could be 'name' to return attacker name, 'type' to return attacker type and 'dmg' to return damage taken.
-- @returns string, string, integer
function string:attackername(returntype)
dmg, name = self:match("You lose (%d-) .+ due to an attack by (.-)%.")
if not name then return end
local mtype = 'player'
if name:sub(1,2) == 'a ' then
name = name:sub(3)
mtype = 'monster'
elseif name:sub(1,3) == 'an ' then
name = name:sub(4)
mtype = 'monster'
end
if returntype then
if returntype == 'name' then
return name
elseif returntype == 'type' then
return mtype
elseif returntype == 'dmg' then
return tonumber(dmg)
end
end
return name, mtype, tonumber(dmg)
end
-- @name string.attackmsg
-- @desc Returns the parsed information about the attack message given.
-- @param self The string.
-- @returns table
function string:attackmsg()
local ttable = {dmg = 0, dealer = {name = '', type = ''}, target = {name = '', type = ''}}
ttable.dmg, ttable.dealer.name = self:match('You lose (%w+) .+ due to an attack by (.+)%.')
if ttable.dmg then
ttable.target = {name = $name, type = 'player'}
if ttable.dealer.name:sub(1,2) == 'a ' then
ttable.dealer = {name = ttable.dealer.name:sub(3), type = 'monster'}
elseif ttable.dealer.name:sub(1,3) == 'an ' then