forked from SolarStrike-Software/rom-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.lua
2867 lines (2488 loc) · 90.8 KB
/
functions.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
if( settings == nil ) then
include("settings.lua");
end
local charUpdatePattern = string.char(0x85, 0xED, 0x0F, 0x84, 0xFF, 0xFF, 0xFF, 0xFF, 0x8B, 0x45, 0x00,
0x8B, 0x0D, 0xFF, 0xFF, 0xFF, 0xFF, 0x50, 0xE8);
local charUpdateMask = "xxxx????xxxxx????xx";
local charUpdateOffset = 13;
local macroUpdatePattern = string.char(0xFF, 0x15, 0xFF, 0xFF, 0xFF, 0xFF, 0x8B, 0x0D, 0xFF, 0xFF, 0xFF, 0xFF, 0xE8);
local macroUpdateMask = "xx????xx????x";
local macroUpdateOffset = 8;
local romMouseRClickFlag = 0x8000;
local romMouseLClickFlag = 0x80;
function getCharUpdatePattern()
return charUpdatePattern;
end
function getCharUpdateMask()
return charUpdateMask;
end
function getCharUpdateOffset()
return charUpdateOffset;
end
function getMacroUpdatePattern()
return macroUpdatePattern;
end
function getMacroUpdateMask()
return macroUpdateMask;
end
function getMacroUpdateOffset()
return macroUpdateOffset;
end
function checkExecutableCompatible()
if( findPatternInProcess(getProc(), charUpdatePattern, charUpdateMask,
addresses.staticpattern_char, 1) == 0 ) then
return false;
end
if( findPatternInProcess(getProc(), macroUpdatePattern, macroUpdateMask,
addresses.staticpattern_macro, 1) == 0 ) then
return false;
end
return true;
end
if(settings.options.DEBUGGING == nil ) then settings.options.DEBUGGING = false; end;
function debugAssert(args)
if( settings.options.DEBUGGING ) then
if( not args ) then
error("Error in memory reading", 2);
else
return args;
end
else
return args;
end
end
-- Ask witch character does the user want to be, from the open windows.
function selectGame(character)
if functionBeingLoaded then
cprintf(cli.lightred,"The loading of userfunction '%s' is forcing the bot to attach to the game early. This can cause errors and features of the bot to not work properly. Userfunctions should execute as little code as possible while loading. This should be fixed.\n",functionBeingLoaded)
end
-- Get list of windows in an array
local windowList = {}
if findProcessByExeList then
local processList = findProcessByExeList("client.exe")
if( processList == nil or #processList == 0 ) then
error("You need to run rom first!", 0);
return 0;
end
for k,v in pairs(processList) do
for i,j in pairs( getWindowsFromProcess(v) ) do
local parent = getWindowParent(j)
if getWindowClassName(j) == "IME" and parent then
local x,y,w,h = windowRect(parent)
if h ~= 0 then
table.insert(windowList, parent)
end
end
end
end
else
windowList = findWindowList("*", "Radiant Arcana");
if( #windowList == 0 ) then
error("You need to run rom first!", 0);
return 0;
end
end
if( #windowList == 0 ) then
print("You need to run rom first!");
return 0;
end
charToUse = {};
for i = 1, #windowList, 1 do
local process, playerAddress, nameAddress;
-- open first window
process = openProcess(findProcessByWindow(windowList[i]));
local ver = getGameVersion(process)
if ver ~= 0 then
ver = " - " .. ver
else
ver = ""
end
-- read player address
showWarnings(false);
if addresses["staticbase_char"] and addresses["charPtr_offset"] and addresses["pawnName_offset"] then
playerAddress = memoryReadUIntPtr(process, addresses["staticbase_char"], addresses["charPtr_offset"]);
-- read player name
if( playerAddress ) then
nameAddress = memoryReadUInt(process, playerAddress + addresses["pawnName_offset"]);
end
end
-- store the player name, with window number
if nameAddress == nil then
charToUse[i] = "(RoM window "..i..")" .. ver;
else
local tmp = memoryReadString(process, nameAddress);
if tmp == nil then
charToUse[i] = "(RoM window "..i..")" .. ver;
elseif database and next(database.utf8_ascii) then -- If database is available then the convert function is available.
if( bot.ClientLanguage == "RU" ) then
charToUse[i] = utf82oem_russian(tmp);
else
charToUse[i] = utf8ToAscii_umlauts(tmp); -- only convert umlauts
end
else
charToUse[i] = tmp
end
end
showWarnings(true);
closeProcess(process);
end
if( character ) then
for i,v in pairs(charToUse) do
printf("[DEBUG] char: '%s', win: %s\n", tostring(v), tostring(windowList[i]));
if( string.lower(v) == string.lower(character) ) then
return windowList[i];
end
end
end
windowChoice = 1;
-- wait until enter is released
while keyPressedLocal(key.VK_RETURN) do
yrest(200);
end
notShown = true;
if (#windowList > 1) then -- if theres more than 1 window, ask the player witch character to use
while not keyPressedLocal(key.VK_RETURN) do
if keyPressedLocal(key.VK_UP) or keyPressedLocal(key.VK_DOWN) or notShown then
notShown = false;
if keyPressedLocal(key.VK_DOWN) then
windowChoice = windowChoice + 1;
if windowChoice > #charToUse then
windowChoice = #charToUse;
end
end
if keyPressedLocal(key.VK_UP) then
windowChoice = windowChoice - 1;
if windowChoice < 1 then
windowChoice = 1;
end
end
if keyPressedLocal(key.VK_UP) or keyPressedLocal(key.VK_DOWN) then
print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
end
-- start message
printf("Choose your character that you want to play on:\n");
for i = 1, #charToUse, 1 do
if i == windowChoice then
cprintf(240, "\n"..charToUse[i]);
else
printf("\n"..charToUse[i]);
end
end
printf("\n");
end
if keyPressedLocal(settings.hotkeys.STOP_BOT.key) then
error("User quit");
end
yrest(200)
end
yrest(200)
end
return windowList[windowChoice];
end
function printPicture(pic, text, textColor)
if not textColor then
textColor = 0;
end
local readfile = io.open(getExecutionPath() .. "/database/img/"..pic..".bmp", "r");
if not readfile then
print(pic);
return 0;
end
file = readfile:read("*all");
local height = string.byte(file, 23);
local width = string.byte(file, 19);
-- color data starts from 118 ends in -4
--for i = 0, 200,1 do
--printf(i..":");
--print(string.byte(file, i));
--printf("\n");
--end
colorData = string.sub(file, 119, -4);
dataLength = string.len(colorData);
--colorData = string.reverse(colorData);
color = {};
for i = 1, dataLength, 1 do
data = string.byte(colorData, i);
first = math.floor(data/16);
second = data - (first*16);
position = (dataLength * 2) - (i * 2);
color[position] = second;
color[position + 1] = first;
end
i = 0;
a = 1;
newline = false;
for y = 1, height, 1 do
for x = 1, width, 1 do
nextchar = "¤";
if not newline and not (a > string.len(text)) then
nextchar = string.char(string.byte(text, a));
a = a + 1;
end
if nextchar == "\n" then
nextchar = "¤";
newline = true;
end
pixel = i+width-x;
col = color[pixel];
-- repair colors from an unknown bug
if col == 9 then
col = 12
elseif col == 12 then
col = 9
elseif col == 1 then
col = 4
elseif col == 4 then
col = 1
elseif col == 3 then
col = 6
elseif col == 6 then
col = 3
elseif col == 7 then
col = 8
elseif col == 8 then
col = 7
elseif col == 11 then
col = 14
elseif col == 14 then
col = 11
end
if nextchar == "¤" then
cprintf(col*16+col, nextchar);
--cprintf(col*16, col);
else
cprintf(col*16+textColor, nextchar);
end
--printf(color[i]);
end
newline = false;
i = i + 6 + width;
printf("\n")
end
end
-- get current directory (theres gotho be an easier way)
function currDir()
os.execute("cd > cd.tmp")
local f = io.open("cd.tmp", r)
local cwd = f:read("*a")
f:close()
os.remove("cd.tmp")
return cwd
end
function getWin(character)
if( __WIN == nil ) then
__WIN = selectGame(character);
end
return __WIN;
end
function getProc()
if( __PROC == nil or not windowValid(__WIN) ) then
if( __PROC ) then closeProcess(__PROC) end;
__PROC = openProcess( findProcessByWindow(getWin()) );
end
return __PROC;
end
function angleDifference(angle1, angle2)
if( math.abs(angle2 - angle1) > math.pi ) then
return (math.pi * 2) - math.abs(angle2 - angle1);
else
return math.abs(angle2 - angle1);
end
end
function distance(x1, z1, y1, x2, z2, y2)
if type(x1) == "table" and type(z1) == "table" then
y2 = z1.Y or z1[3]
z2 = z1.Z or z1[2]
x2 = z1.X or z1[1]
y1 = x1.Y or x1[3]
z1 = x1.Z or x1[2]
x1 = x1.X or x1[1]
elseif z2 == nil and y2 == nil then -- assume x1,z1,x2,z2 values (2 dimensional)
z2 = x2
x2 = y1
y1 = nil
end
if( x1 == nil or z1 == nil or x2 == nil or z2 == nil ) then
error("Error: nil value passed to distance()", 2);
end
if y1 == nil or y2 == nil then -- 2 dimensional calculation
return math.sqrt( (z2-z1)*(z2-z1) + (x2-x1)*(x2-x1) );
else -- 3 dimensional calculation
return math.sqrt( (z2-z1)*(z2-z1) + (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) );
end
end
-- Used in pause/exit callbacks. Just releases movement keys.
function releaseKeys()
if windowValid(__WIN) and __PROC then
memoryWriteBytePtr(__PROC, addresses.staticbase_char ,addresses.moveKeysPressed_offset, 0 )
end
end
function pauseCallback()
local msg = sprintf(language[46], getKeyName(getStartKey())); -- to continue, (CTRL+L) exit ...
releaseKeys();
printf(msg);
end
atPause(pauseCallback);
function exitCallback()
releaseKeys();
end
atExit(exitCallback);
function errorCallback(script, line, message)
local crashed, pid = isClientCrashed()
if crashed then
printf("Attached game client has crashed. Killing client (PID %d)\n", pid);
warning(script .. ":" .. line .. ": " .. message);
os.execute("TASKKILL /PID " .. pid .. " /F");
else
releaseKeys();
printf("The game client did not crash.\n");
end
if player and player.Name then
printf("Character: %s\n",player.Name)
end
end
atError(errorCallback);
function resumeCallback()
printf("Resumed.\n");
-- Make sure our player exists before trying to update it
if( player and #settings.profile.skills ~= nil) then
-- Make sure we aren't using potentially old data
player:update();
end
if( settings.profile.options.PATH_TYPE == "wander" and __WPL ~= nil ) then
__WPL.OrigX = player.X;
__WPL.OrigZ = player.Z;
end
-- Re-set our bot start time to now
if( player ) then
player.BotStartTime = os.time();
player.LastDistImprove = os.time(); -- reset unstick timer (dist improvement timer)
if( settings.profile.options.LOGOUT_TIME > 0 ) then
printf("Bot start time reset\n");
end
end
-- check if using sleep function
if( settings.profile.options.USE_SLEEP_AFTER_RESUME == true ) then
cprintf(cli.yellow, language[148]); -- LWe will go to sleep after fight finished
player.Sleeping = true; -- activate sleep
end
end
atResume(resumeCallback);
function pauseOnDeath()
local sk = startKey;
if( getVersion() >= 100 ) then sk = getStartKey(); end;
cprintf(cli.red, language[149]); -- You have died
printf(language[160], -- Script paused until you revive yourself
getKeyName(sk))
logMessage("Player died.\n");
stopPE();
end
local LAST_PLAYER_X = 0;
local LAST_PLAYER_Z = 0;
function timedSetWindowName(profile)
-- Update our exp gain
if isInGame() and ( os.difftime(os.time(), player.LastExpUpdateTime) > player.ExpUpdateInterval ) then
player.Class1 = memoryReadRepeat("int", getProc(), player.Address + addresses.pawnClass1_offset) or player.Class1;
player.Level = memoryReadRepeat("int", getProc(), addresses.charClassInfoBase + (addresses.charClassInfoSize* player.Class1 ) + addresses.charClassInfoLevel_offset) or player.Level
player.XP = memoryReadRepeat("int", getProc(), addresses.charClassInfoBase + (addresses.charClassInfoSize* player.Class1 ) + addresses.charClassInfoXP_offset) or player.XP
if player.XP == 0 or player.Level == 0 then return end
local newExp = player.XP or 0;
local maxExp = memoryReadRepeat("intptr", getProc(), addresses.charMaxExpTable_address, (player.Level-1) * 4) or 1;
player.LastExpUpdateTime = os.time(); -- Reset timer
if( type(newExp) ~= "number" ) then newExp = 0; end;
if( type(maxExp) ~= "number" ) then maxExp = 1; end;
-- If we have not begun tracking exp, start by gathering
-- our current value, but do not count it as a gain
if( player.ExpInsertPos == 0 ) then
player.ExpInsertPos = 1;
player.LastExp = newExp;
else
local gain = 0;
local expGainSum = 0;
local valueCount = 0;
if( newExp > player.LastExp ) then
gain = newExp - player.LastExp;
elseif( newExp < player.LastExp ) then
-- We probably just leveled up. Just get our current, new value and use that.
gain = newExp;
end
player.LastExp = newExp;
player.ExpTable[player.ExpInsertPos] = gain;
player.ExpInsertPos = player.ExpInsertPos + 1;
if( player.ExpInsertPos > player.ExpTableMaxSize ) then
player.ExpInsertPos = 1;
end;
for i,v in pairs(player.ExpTable) do
valueCount = valueCount + 1;
expGainSum = expGainSum + v;
end
player.ExpPerMin = expGainSum / ( valueCount * player.ExpUpdateInterval / 60 );
player.TimeTillLevel = (maxExp - newExp) / player.ExpPerMin;
if( player.TimeTillLevel > 9999 ) then
player.TimeTillLevel = 9999;
elseif( player.TimeTillLevel < 0 ) then
player.TimeTillLevel = 0
end
end
end
local displayname = player.Name
if #displayname > 8 then
displayname = string.sub(displayname, 1, 7) .. "*";
end
if( (player.X ~= LAST_PLAYER_X) or (player.Z ~= LAST_PLAYER_Z) ) then
setWindowName(getHwnd(), sprintf(language[600],
BOT_VERSION, displayname, player.X, player.Z, player.ExpPerMin, player.TimeTillLevel));
LAST_PLAYER_X = player.X;
LAST_PLAYER_Z = player.Z;
end
end
-- returns full path if it exists, searching relative, local and global folders
-- To be found, _file path should be relative to 'rom' or 'romglobal' or the current waypoint file.
function findFile(_file)
-- Check relative to current wp files location.
if __WPL and __WPL.FileName then
-- we strip "waypoints/" since we search relative to current waypoint location.
local tmpFile = string.gsub(_file,"^/?waypoints/","")
local currentWPLPath = string.match(__WPL.FileName,"(.+/)") or ""
-- Simple nested folder
if fileExists(getExecutionPath() .. "/waypoints/" .. currentWPLPath .. tmpFile) then
return getExecutionPath() .. "/waypoints/" .. currentWPLPath .. tmpFile
end
-- Strip duplicate dirs
local tmpPath = string.match(tmpFile, "^(.*%/).*%....")
if tmpPath then
repeat
if string.match(currentWPLPath, tmpPath .. "$") then
-- Match found, strip dirs
currentWPLPath = string.match(currentWPLPath, "(.*)"..tmpPath.."$")
if fileExists(getExecutionPath() .. "/waypoints/" .. currentWPLPath .. tmpFile) then
return getExecutionPath() .. "/waypoints/" .. currentWPLPath .. tmpFile
end
end
-- Take off a dir and try again
tmpPath = string.match(tmpPath, "^(.-)[^%/]*%/$")
until tmpPath == ""
end
end
-- Then check local folder
if fileExists(getExecutionPath() .. "/" .. _file) then
return getExecutionPath() .. "/" .. _file
end
-- Then check global folder
if fileExists(getExecutionPath() .. "/../romglobal/" .. _file) then
return getExecutionPath() .. "/../romglobal/" .. _file
end
-- if neither exist return local as default
return getExecutionPath() .. "/" .. _file
end
function load_paths( _wp_path, _rp_path)
cprintf(cli.yellow, "Please use the renamed function \'loadPaths()\' instead of \'load_paths\'!\n");
loadPaths( _wp_path, _rp_path);
end
function loadPaths( _wp_path, _rp_path)
-- load given waypoint path and return path file
-- if you don't specify a return path the function will look for
-- a default return path based on the waypoint path name and
-- the settings.profile.options.RETURNPATH_SUFFIX
-- check if function is not called empty
if( _wp_path == "" or _wp_path == " " ) then _wp_path = nil; end;
if( _rp_path == "" or _rp_path == " " ) then _rp_path = nil; end;
if( not _wp_path ) and ( not _rp_path ) then
cprintf(cli.yellow, language[161]); -- have to specify either
return;
end;
-- check suffix and remember default return path name
local rp_default;
if(_wp_path ~= nil) then
local foundpos = string.find(_wp_path,".xml",1,true); -- filetype defined?
if( foundpos ) then -- filetype defined
rp_default = string.sub(_wp_path,1,foundpos-1) .. settings.profile.options.RETURNPATH_SUFFIX .. ".xml";
else -- no filetype
rp_default = _wp_path .. settings.profile.options.RETURNPATH_SUFFIX .. ".xml";
end;
end;
if( _wp_path and not string.find(_wp_path,".xml", 1, true) and _wp_path ~= "wander" and _wp_path ~= "resume" ) then
_wp_path = _wp_path .. ".xml";
end;
if( _rp_path and not string.find(_rp_path,".xml", 1, true) ) then
_rp_path = _rp_path .. ".xml";
end;
-- waypoint path is defined
-- check if _wp_path exists
local wpfilename
if( _wp_path and
string.lower(_wp_path) ~= "wander" and
string.lower(_wp_path) ~= "resume" ) then
local filename
if _wp_path:sub(2,2) == ":" then
filename = _wp_path
else
filename = findFile("waypoints/" .. _wp_path )
end
if not fileExists(filename) then
local msg = sprintf(language[142], filename ); -- We can't find your waypoint file
error(msg, 2);
end
__WPL = CWaypointList();
wpfilename = filename
cprintf(cli.yellow, language[0], _wp_path); -- Loaded waypoint path
end
-- set wander for WP
if( string.lower(_wp_path) == "wander" ) then
__WPL = CWaypointListWander();
__WPL:setRadius(settings.profile.options.WANDER_RADIUS);
__WPL:setMode("wander");
cprintf(cli.green, language[168], settings.profile.options.WANDER_RADIUS); -- Loaded waypoint path
end
if( string.lower(_wp_path) == "resume" ) then
local resumelogname = getExecutionPath() .. "/logs/resumes/"..player.Name..".txt"
if not fileExists(resumelogname) then
local msg = sprintf(language[142], resumelogname ); -- We can't find your waypoint file
error(msg, 2);
end
local filename, resume_num, resumeType = dofile(resumelogname)
wpfilename = findFile("waypoints/"..filename)
__WPL = CWaypointList();
__WPL.ResumePoint = resume_num
__WPL.ResumeType = resumeType
cprintf(cli.yellow, language[0], wpfilename); -- Loaded waypoint path
end
-- look for default return path with suffix '_return'
if( not _rp_path ) then
local filename = findFile("waypoints/" .. rp_default)
if fileExists(filename) then
cprintf(cli.green, language[162], rp_default ); -- Return path found with default naming
_rp_path = rp_default; -- set default
else
cprintf(cli.lightgray, language[163], rp_default ); -- No return path with default naming
end;
end
-- check if _rp_path exists
local rpfilename
if( _rp_path ) then
if( not __RPL ) then -- define object if not there
__RPL = CWaypointList();
end;
local filename = findFile("waypoints/" .. _rp_path)
if not fileExists(filename) then
local msg = sprintf(language[143], _rp_path ); -- We can't find your returnpath file
error(msg, 0);
end;
rpfilename = filename
cprintf(cli.green, language[1], _rp_path); -- Loaded return path
end
-- check if on returnpath
if( player.Returning == true and
_rp_path ) then
cprintf(cli.green, language[164], _rp_path); -- We are coming from a return_path.
else
player.Returning = false;
cprintf(cli.green, language[165], _wp_path );-- We use the normal waypoint path %s now
end
-- waypoint path is defined ... load it
if wpfilename then
__WPL:load(wpfilename);
if __WPL.ResumePoint and __WPL.Waypoints[__WPL.ResumePoint] then
__WPL.CurrentWaypoint = __WPL.ResumePoint
elseif(__WPL.CurrentWaypoint ~= 1 ) then --and #__WPL.Waypoints > 0
cprintf(cli.green, language[15], -- Waypoint #%d is closer then #1
__WPL.CurrentWaypoint, __WPL.CurrentWaypoint);
end;
if __WPL.ResumeType then
__WPL:setForcedWaypointType(__WPL.ResumeType)
end
end
-- return path defined or default found ... load it
if rpfilename then
__RPL:load(rpfilename);
else
if( __RPL ) then -- clear old returnpath object
__RPL = nil;
end;
end;
end
-- executing RoMScript and send a MM window message before
function sendMacro(_script)
cprintf(cli.green, language[169], -- Executing RoMScript ...
"MACRO",
string.sub(_script, 1, 40) );
return RoMScript(_script);
end
-- Execute a slash command
function SlashCommand(script)
if commandMacro == 0 then
-- setupMacros() hasn't run yet
return
else -- check if still valid
local __, cName = readMacro(commandMacro)
local __, rName = readMacro(resultMacro)
if cName ~= COMMAND_MACRO_NAME or rName ~= RESULT_MACRO_NAME then -- macros moved
setupMacros()
end
end
-- add slash if needed
if string.sub(script,1,1) ~= "/" then
script = "/" .. script
end
-- write to macro
writeToMacro(commandMacro, script)
--- Execute it
if( settings.profile.hotkeys.MACRO ) then
-- keyboardPress(settings.profile.hotkeys.MACRO.key);
keyboardHold(settings.profile.hotkeys.MACRO.key);
keyboardRelease(settings.profile.hotkeys.MACRO.key);
end
end
--- Run rom scripts, usage: RoMScript("BrithRevive();");
function RoMScript(script)
-- Check if in game
if not isInGame() then
-- Cannot execute RoMScript. Not in game.
return
end
if commandMacro == 0 then
-- setupMacros() hasn't run yet
return
else -- check if still valid
local __, cName = readMacro(commandMacro)
local __, rName = readMacro(resultMacro)
if cName ~= COMMAND_MACRO_NAME or rName ~= RESULT_MACRO_NAME then -- macros moved
setupMacros()
end
end
--- Get the real offset of the address
local macro_address = memoryReadUInt(getProc(), addresses.staticbase_macro);
-- local scriptDef;
-- if( settings.options.LANGUAGE == "spanish" ) then
-- scriptDef = "/redactar";
-- else
-- scriptDef = "/script";
-- end
--- Macro length is max 255, and after we add the return code,
--- we are left with about 155 character limit.
local dataPart = 0 -- The part of the data to get
local raw = "" -- Combined raw data from 'R'
repeat
local text
-- The command macro
if dataPart == 0 then
-- The initial command macro
-- text = scriptDef.." R='' a={" .. script ..
-- "} for i=1,#a do R=R..tostring(a[i])" ..
-- "..'" .. string.char(9) .. "' end" ..
-- " EditMacro("..resultMacro..",'"..RESULT_MACRO_NAME.."',7,R)";
text = script
else
-- command macro to get the rest of the data from 'R'
-- text = scriptDef.." EditMacro("..resultMacro..",'"..
-- RESULT_MACRO_NAME.."',7,string.sub(R,".. (1 + dataPart * 255) .."))";
text = "SendMore"
end
-- Check to make sure length is within bounds
local len = string.len(text);
local texts = {}
if( len > 254 ) then
-- Break into parts
table.insert(texts,"!"..text:sub(1,253))
local count = 1
while #text:sub(253 * count + 1) > 253 do
table.insert(texts,"@"..text:sub(253 * count + 1, 253 * (count + 1)))
count = count + 1
end
table.insert(texts, "#"..text:sub(253 * count + 1))
end
repeat
repeat
-- Write the command macro
if #texts > 0 then
text = texts[1]
table.remove(texts, 1)
end
-- Send first part
writeToMacro(commandMacro, text)
-- Write something on the first address, to see when its over written
memoryWriteByte(getProc(), macro_address + addresses.macroSize *(resultMacro - 1) + addresses.macroBody_offset , 6);
-- Execute it
if( settings.profile.hotkeys.MACRO ) then
-- keyboardPress(settings.profile.hotkeys.MACRO.key);
keyboardHold(settings.profile.hotkeys.MACRO.key);
-- rest(100)
keyboardRelease(settings.profile.hotkeys.MACRO.key);
end
local tryagain = false
-- A cheap version of a Mutex... wait till it is "released"
-- Use high-res timers to find out when to time-out
local startWaitTime = getTime();
while( memoryReadByte(getProc(), macro_address + addresses.macroSize *(resultMacro - 1) + addresses.macroBody_offset) == 6 ) do
if( deltaTime(getTime(), startWaitTime) > 800 ) then
if settings.options.DEBUGGING then
printf("0x%X\n", addresses.editBoxHasFocus_address)
end
if memoryReadUInt(getProc(), addresses.editBoxHasFocus_address) == 0 then
keyboardPress(settings.hotkeys.ESCAPE.key); rest(500)
if RoMScript("GameMenuFrame:IsVisible()") then
-- Clear the game menu and reset editbox focus
keyboardPress(settings.hotkeys.ESCAPE.key); rest(300)
RoMCode("z = GetKeyboardFocus(); if z then z:ClearFocus() end")
end
end
tryagain = true
break
end;
rest(5);
end
until tryagain == false
until #texts == 0
--- Read the outcome from the result macro
local rawPart = readMacro(resultMacro)
raw = raw .. rawPart
dataPart = dataPart + 1
until string.len(rawPart) < 255
readsz = "";
ret = {};
cnt = 0;
for i = 1, string.len(raw), 1 do
local byte = string.byte(raw, i);
if( byte == 0 or byte == null) then -- Break on NULL terminator
break;
elseif( byte == 9 ) then -- Use TAB to seperate
-- Implicit casting
if( string.find(readsz, "^[%-%+]?%d+%.?%d+$") ) then readsz = tonumber(readsz);
elseif( string.find(readsz, "^[%-%+]?%d+$") ) then readsz = tonumber(readsz);
elseif( readsz == "true" ) then readsz = true;
elseif( readsz == "false" ) then readsz = false;
elseif( readsz:sub(1,1) ) == "{" then
local func, err = loadstring("return "..readsz)
if func then readsz = func() else print(err) end
end
table.insert(ret, readsz);
cnt = cnt+1;
readsz = "";
else
readsz = readsz .. string.char(byte);
end
end
local err = ret[1]
if err == false then
error("IGF:".."\\"..script.."\\:IGF "..ret[2],0)
elseif err == true then
table.remove(ret,1)
end
return unpack(ret);
end
-- Executes code as apposed to returning a value. Can still return a value if assigned to variable 'a' in a table.
function RoMCode(code)
if #code < 254-41 then
return RoMScript("} "..code.." if type(a)~=\"table\" then a={a} end z={")
else
return RoMScript("} "..code.." z={")
end
end
-- send message to the game
function addMessage(message)
message = string.gsub(message, "\n", "\\n")
message = string.gsub(message, "\"", "\\\"")
-- language conversations
if( bot.ClientLanguage == "RU" ) then
message = oem2utf8_russian(message);
else
message = asciiToUtf8_umlauts(message); -- for ingame umlauts
end
RoMCode("ChatFrame1:AddMessage(\""..message.."\")");
end
-- UTF8 -> DOS(OEM) Code page 866 conversation for the russian client
-- we use it for the player names & mob names conversion in pawn.lua
-- http://en.wikipedia.org/wiki/Code_page_866
function utf82oem_russian(txt)
txt = string.gsub(txt, string.char(0xD0, 0x81), string.char(0xF0) ); -- 0xF0 / E with dots
txt = string.gsub(txt, string.char(0xD1, 0x91), string.char(0xF1) ); -- 0xF1 / e with dots
-- lower case
local patt = string.char(0xD1) .. "([" .. string.char(0x80, 0x2D, 0x8F) .. "])";
txt = string.gsub(txt, patt, function (s)
return string.char(string.byte(s,1,1)+0x60);
end
);
-- upper case
patt = string.char(0xD0) .. "([" .. string.char(0x90, 0x2D, 0xBF) .. "])";
txt = string.gsub(txt, patt, function (s)
return string.char(string.byte(s,1,1)-0x10);
end
);
return txt;
end
-- DOS(OEM) -> UTF8 conversation for the russian client
-- we use it within addMessage in functions.lua
function oem2utf8_russian(txt)
local function translate(code)
-- upper case and lower case part 1
if(code>=0x80)and(code<=0xAF)then
return string.char(0xD0, code+0x10);
end
-- lower case part 2
if(code>=0xE0)and(code<=0xEF)then
return string.char(0xD1, code-0x60);
end
if(code==0xF0)then
return string.char(0xD0, 0x81); -- E with dots
end
if(code==0xF1)then
return string.char(0xD1, 0x91); -- e with dots
end
return string.char(code);
end
local result = '';
for i=1,string.len(txt) do
result = result .. translate( string.byte(txt,i) );
end
return result;
end
-- convert the ingame UTF8 strings to ASCII
-- we use the complete utf8 table, that means for all languages we have
function convert_utf8_ascii( _str )
-- local function to convert string (e.g. mob name / player name) from UTF-8 to ASCII
local function convert_utf8_ascii_character( _str, _v )
local found;
_str, found = string.gsub(_str, string.char(_v.utf8_1, _v.utf8_2), string.char(_v.ascii) );
return _str, found;
end
local found, found_all;
found_all = 0;
for i,v in pairs(database.utf8_ascii) do
-- _str, found = convert_utf8_ascii_character( _str, v.ascii ); -- replace special characters
_str, found = convert_utf8_ascii_character( _str, v ); -- replace special characters
found_all = found_all + found; -- count replacements
end
if( found_all > 0) then
return _str, true;
else
return _str, false;
end
end
-- we only replace umlaute, hence only that are important for mob names
-- player names are at the moment not importent for the MM protocol