-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.java
2128 lines (2037 loc) · 111 KB
/
bot.java
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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.concurrent.Semaphore;
import java.util.HashMap;
import java.util.Scanner;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;
import static org.luaj.vm2.LuaValue.*;
import org.luaj.vm2.LuaNumber;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.Varargs;
import org.luaj.vm2.lib.VarArgFunction;
import org.luaj.vm2.lib.jse.JsePlatform;
public class bot {
public LuaTable character = new LuaTable();
public LuaTable inventory = new LuaTable();
public LuaTable equipment = new LuaTable();
public LuaTable storage = new LuaTable();
public LuaTable beings = new LuaTable();
public LuaTable buy_sell = new LuaTable();
public LuaTable items = new LuaTable();
public LuaTable skills = new LuaTable();
public LuaTable trade_buy = new LuaTable();
public LuaTable trade_sell = new LuaTable();
public LuaTable warps = new LuaTable();
public int clientTime = 0;
public LuaTable itemDB = new LuaTable();
public LuaTable mobDB = new LuaTable();
public Globals globals;
public String leader_name = "";
public LuaValue script;
public LuaValue loopBody;
public LuaValue packetHandler;
public String mapName;
boolean sendMapLoaded = true;
boolean quit = false;
Net net;
Semaphore writeLock = new Semaphore(1, true);
Semaphore dataLock = new Semaphore(1, true);
HashMap<String, Map> maps = new HashMap<String, Map>();
Map map;
public LuaValue createBeing(int id, int job) throws IOException {
LuaTable being = new LuaTable();
if(id == character.get("id").toint()) being = character;
String type = null;
being.set("race", job);
if(job <=25 || (job >= 4001 && job <= 4049)) {
type = "player";
} else if(job >= 46 && job <= 1000) {
type = "npc";
} else if(job > 1000 && job <= 2000) {
type = "monster";
} else if(job == 45) {
type = "portal";
}
being.set("type", type);
being.set("dir", 1);
beings.set(id, being);
if(type.equals("player") || type.equals("npc")) {
try {
writeLock.acquire();
Net.PacketOut po = net.newPacket(0x0094); // being name request
po.writeInt32(id);
po.send();
} catch(InterruptedException e) {
e.printStackTrace();
}
writeLock.release();
}
return being;
}
public LuaValue equipType(int type) {
if(type == 0) return NIL;
if((type & 0x0001) != 0) return valueOf("legs");
if((type & 0x0002) != 0) return valueOf("weapon");
if((type & 0x0004) != 0) return valueOf("gloves");
if((type & 0x0008) != 0) return valueOf("necklace");
if((type & 0x0010) != 0) return valueOf("ring1");
if((type & 0x0020) != 0) return valueOf("shield");
if((type & 0x0040) != 0) return valueOf("shoes");
if((type & 0x0080) != 0) return valueOf("ring2");
if((type & 0x0100) != 0) return valueOf("head");
if((type & 0x0200) != 0) return valueOf("torso");
return valueOf(type);
}
public void beingUpdatePath(LuaValue being) {
if(being.get("id").toint() == character.get("id").toint()) {
being.set("path", NIL);
}
int x = being.get("x").toint();
int y = being.get("y").toint();
int dst_x = being.get("dst_x").toint();
int dst_y = being.get("dst_y").toint();
if(x!=dst_x || y!=dst_y) {
LuaValue path = being.get("path");
int path_index = being.get("path_index").toint();
if(path == NIL) {
being.set("path", map.findPath(x, y, dst_x, dst_y, false));
being.set("path_index", 1);
return;
}
int length = path.length();
if(path_index > length
|| path.get(path_index).get("x").toint() !=x || path.get(path_index).get("y").toint() != y
|| path.get(length).get("x").toint() != dst_x || path.get(length).get("y").toint() != dst_y) {
being.set("path", map.findPath(x, y, dst_x, dst_y, false));
being.set("path_index", 1);
}
} else {
being.set("path", NIL);
}
}
public void loadMaps() {
File[] files = new File("server-data/world/map/data").listFiles();
for(File file: files) {
if(!file.isDirectory()) {
String name = file.getName();
if(name.matches("\\d\\d\\d-\\d\\.wlk")) {
String map_name = name.substring(0, 5);
maps.put(map_name, new Map(map_name));
}
}
}
}
public void loadWarps() {
Utils.clearTable(warps);
File[] files = new File("server-data/world/map/npc").listFiles();
for(File file: files) {
if(file.isDirectory()) {
String name = file.getName();
if(name.matches("\\d\\d\\d-\\d")) {
warps.set(name, loadWarps(name));
}
}
}
}
public LuaValue loadWarps(String map_name) {
try {
File file = new File("server-data/world/map/npc/" + map_name + "/_warps.txt");
if(!file.exists()) return NIL;
LuaTable map_warps = new LuaTable();
Scanner s = new Scanner(new FileInputStream(file));
int i=1;
while(s.hasNextLine()) {
String line = s.nextLine();
if(line.length() > 0 && !line.startsWith("//")) {
Scanner s2 = new Scanner(line);
s2.useDelimiter(",|\\|");
LuaTable warp = new LuaTable();
warp.set("map", s2.next());
warp.set("x", s2.nextInt());
warp.set("y", s2.nextInt());
s2.next();
s2.next();
s2.next();
s2.next();
warp.set("dst_map", s2.next());
warp.set("dst_x", s2.nextInt());
warp.set("dst_y", s2.nextInt());
map_warps.set(i++, warp);
}
}
return map_warps;
} catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
return NIL;
}
public void fillItemDB() {
try {
final String[] files = {
"item_db_chest", "item_db_foot",
"item_db_generic", "item_db_hand",
"item_db_head", "item_db_leg",
"item_db_offhand", "item_db_trinket",
"item_db_use", "item_db_weapon"
};
for(String name: files) {
Scanner s = new Scanner(new FileInputStream(new File("server-data/world/map/db/"+name+".txt")));
while(s.hasNextLine()) {
String line = s.nextLine();
if(line.length() > 0 && !line.startsWith("//")) {
Scanner s2 = new Scanner(line);
s2.useDelimiter(",\\s*");
LuaTable item = new LuaTable();
int id = s2.nextInt();
item.set("id", id);
item.set("name", s2.next());
s2.next(); // type
item.set("price", s2.nextInt());
item.set("sell", s2.nextInt());
item.set("weight", s2.nextInt());
int attack = s2.nextInt();
if(attack > 0) item.set("attack", attack);
int defence = s2.nextInt();
if(defence > 0) item.set("defence", defence);
int range = s2.nextInt();
if(range > 0) item.set("range", range);
int mattack = s2.nextInt();
if(mattack > 0) item.set("mattack", mattack);
// ignore the rest
itemDB.set(id, item);
}
}
}
} catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public void fillMobDB() {
try {
final String[] files = {
"mob_db_0_19", "mob_db_20_39",
"mob_db_40_59", "mob_db_60_79",
"mob_db_80_99", "mob_db_over_100"
};
for(String name: files) {
Scanner s = new Scanner(new FileInputStream(new File("server-data/world/map/db/"+name+".txt")));
while(s.hasNextLine()) {
String line = s.nextLine();
if(line.length() > 0 && !line.startsWith("//")) {
Scanner s2 = new Scanner(line);
s2.useDelimiter(",\\s*");
LuaTable mob = new LuaTable();
int id = s2.nextInt();
mob.set("id", id);
mob.set("name", s2.next());
s2.next(); // jname, always same as name
mob.set("level", s2.nextInt());
mob.set("hp", s2.nextInt());
s2.next(); // mp, not used
s2.next(); // exp, calculated automatically
mob.set("jexp", s2.nextInt());
mob.set("range", s2.nextInt());
mob.set("atk", s2.nextInt());
mob.set("atk_max", s2.nextInt());
mob.set("def", s2.nextInt()); // in percents
mob.set("mdef", s2.nextInt()); // in percents
mob.set("str", s2.nextInt());
mob.set("agi", s2.nextInt());
mob.set("vit", s2.nextInt());
mob.set("int", s2.nextInt());
mob.set("dex", s2.nextInt());
mob.set("luk", s2.nextInt());
s2.nextInt(); // range2, unused
s2.nextInt(); // range3, unused
s2.nextInt(); // scale, unused
mob.set("race", s2.nextInt());
// 0 - formless
// 1 - undead
// 2 - animal
// 3 - plant
// 4 - insect
// 5 - fish
// 6 - demon
// 7 - demihuman
// 8 - angel
// 9 - dragon
mob.set("element", s2.nextInt());
int mode = s2.nextInt();
LuaTable modet = new LuaTable();
if((mode & 0x01) != 0) modet.set("moving", TRUE);
if((mode & 0x02) != 0) modet.set("looter", TRUE);
if((mode & 0x04) != 0) modet.set("aggressive", TRUE);
if((mode & 0x08) != 0) modet.set("support", TRUE);
if((mode & 0x80) != 0) modet.set("attack", TRUE);
mob.set("mode", modet);
// 1 - can move
// 2 - looter
// 4 - aggressive
// 8 - support
// 16 - castsensor, no effect
// 32 - boss, effect unknown
// 64 - plant, effect unknown
// 128 - can attack
// 256 - detector, no effect
// 512 - changetarget, no effect
mob.set("speed", s2.nextInt());
mob.set("adelay", s2.nextInt());
mob.set("amotion", s2.nextInt());
mob.set("dmotion", s2.nextInt()); // stun delay
// ignore the rest
// currently not reading drops
mobDB.set(id, mob);
}
}
}
} catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public void updateCharacterStats() {
character.set("str", character.get("str_base").toint() + character.get("str_mod").toint());
character.set("agi", character.get("agi_base").toint() + character.get("agi_mod").toint());
character.set("vit", character.get("vit_base").toint() + character.get("vit_mod").toint());
character.set("int", character.get("int_base").toint() + character.get("int_mod").toint());
character.set("dex", character.get("dex_base").toint() + character.get("dex_mod").toint());
character.set("luk", character.get("luk_base").toint() + character.get("luk_mod").toint());
character.set("attack", character.get("attack_base").toint() + character.get("attack_mod").toint());
character.set("defence", character.get("defence_base").toint() + character.get("defence_mod").toint());
character.set("mattack", character.get("mattack_base").toint() + character.get("mattack_mod").toint());
character.set("mdefence", character.get("mdefence_base").toint() + character.get("mdefence_mod").toint());
character.set("evasion", character.get("evasion_base").toint() + character.get("evasion_mod").toint());
}
public bot() throws Exception {
globals = JsePlatform.standardGlobals();
File file = new File("config.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String username = br.readLine();
String password = br.readLine();
String charname = br.readLine();
String leader_name = br.readLine();
br.close();
fillItemDB();
fillMobDB();
loadMaps();
loadWarps();
net = new Net(username, password, charname);
net.connect(this);
globals.set("leader_name", leader_name);
globals.set("client_time", 0);
globals.set("character", character);
globals.set("inventory", inventory);
globals.set("equipment", equipment);
globals.set("storage", storage);
globals.set("map_name", mapName);
globals.set("beings", beings);
globals.set("buy_sell", buy_sell);
globals.set("items", items);
globals.set("skills", skills);
globals.set("trade_buy", trade_buy);
globals.set("trade_sell", trade_sell);
globals.set("client_time", clientTime);
globals.set("warps", warps);
globals.set("itemDB", itemDB);
globals.set("mobDB", mobDB);
map = maps.get(mapName);
if(map == null) throw new RuntimeException("map "+mapName+" doesn't exist!");
beings.set(character.get("id"), character);
globals.set("map_accessible", new VarArgFunction() {
@Override
public Varargs invoke(Varargs args) {
int x1 = args.arg(1).toint();
int y1 = args.arg(2).toint();
int x2 = args.arg(3).toint();
int y2 = args.arg(4).toint();
LuaValue ret = valueOf(map.isAccesible(x1, y1, x2, y2));
return varargsOf(new LuaValue[] {ret});
}
});
globals.set("map_region", new VarArgFunction() {
@Override
public Varargs invoke(Varargs args) {
String map_name = args.arg(1).toString();
int x = args.arg(2).toint();
int y = args.arg(3).toint();
LuaValue ret = NIL;
if(maps.containsKey(map_name)) {
Map map = maps.get(map_name);
ret = valueOf(map.getRegion(x, y));
}
return varargsOf(new LuaValue[] {ret});
}
});
globals.set("map_find_path", new VarArgFunction() {
@Override
public Varargs invoke(Varargs args) {
int x1 = args.arg(1).toint();
int y1 = args.arg(2).toint();
int x2 = args.arg(3).toint();
int y2 = args.arg(4).toint();
boolean checkThreat = args.arg(5).toboolean();
LuaValue ret = map.findPath(x1, y1, x2, y2, checkThreat);
return varargsOf(new LuaValue[] {ret});
}
});
globals.set("map_bot_path", new VarArgFunction() {
@Override
public Varargs invoke(Varargs args) {
int x1 = args.arg(1).toint();
int y1 = args.arg(2).toint();
int x2 = args.arg(3).toint();
int y2 = args.arg(4).toint();
LuaValue ret = map.findBotPath(x1, y1, x2, y2);
return varargsOf(new LuaValue[] {ret});
}
});
globals.set("map_nearest_safe_spot", new VarArgFunction() {
@Override
public Varargs invoke(Varargs args) {
int x1 = args.arg(1).toint();
int y1 = args.arg(2).toint();
int radius = args.arg(3).toint();
LuaValue ret = map.nearestSafeSpot(x1, y1, radius);
return varargsOf(new LuaValue[] {ret});
}
});
globals.set("map_get_threat", new VarArgFunction() {
@Override
public Varargs invoke(Varargs args) {
int x = args.arg(1).toint();
int y = args.arg(2).toint();
LuaValue ret = valueOf(map.getThreat(x, y));
return varargsOf(new LuaValue[] {ret});
}
});
globals.set("map_get_threat_total", new VarArgFunction() {
@Override
public Varargs invoke(Varargs args) {
int x = args.arg(1).toint();
int y = args.arg(2).toint();
LuaValue ret = valueOf(map.getThreatTotal(x, y));
return varargsOf(new LuaValue[] {ret});
}
});
script = globals.loadfile("bot.lua");
script.call();
packetHandler = globals.get("packet_handler");
loopBody = globals.get("loop_body");
globals.set("send_packet", new VarArgFunction() {
@Override
public Varargs invoke(Varargs args) {
try {
writeLock.acquire();
try {
String pak = args.arg(1).toString();
// System.out.println("send_packet "+args);
Net.PacketOut po = null;
switch(pak) {
case "reload": {
script = globals.loadfile("bot.lua");
script.call();
packetHandler = globals.get("packet_handler");
loopBody = globals.get("loop_body");
System.out.println("reloaded");
} break;
case "walk": {
int x = args.arg(2).toint();
int y = args.arg(3).toint();
int dir = args.arg(4).toint();
po = net.newPacket(0x0085); // CMSG_PLAYER_CHANGE_DEST
po.writeCoordinates(x, y, dir);
} break;
case "talk": {
String msg = character.get("name") + " : " + args.arg(2).toString();
po = net.newPacket(0x008C); // CMSG_CHAT_MESSAGE
po.writeInt16(4 + msg.length() + 1);
po.writeString(msg.length() + 1, msg);
} break;
case "whisper": {
String nick = args.arg(2).toString();
String msg = args.arg(3).toString();
po = net.newPacket(0x0096); // CMSG_CHAT_WHISPER
po.writeInt16(msg.length() + 28);
po.writeString(24, nick);
po.writeString(msg.length(), msg);
} break;
case "storage_close": {
po = net.newPacket(0x00F7); // CMSG_CLOSE_STORAGE
} break;
case "equip": {
int index = args.arg(2).toint();
po = net.newPacket(0x00A9); // CMSG_PLAYER_EQUIP
po.writeInt16(index);
po.writeInt16(0);
} break;
case "unequip": {
int index = args.arg(2).toint();
po = net.newPacket(0x00AB); // CMSG_PLAYER_UNEQUIP
po.writeInt16(index);
} break;
case "use": {
int index = args.arg(2).toint();
LuaValue item = inventory.get(index);
po = net.newPacket(0x00A7); // CMSG_PLAYER_INVENTORY_USE
po.writeInt16(index);
po.writeInt32(item.get("id").toint());
} break;
case "drop": {
int index = args.arg(2).toint();
int amount = args.arg(3).toint();
if(amount < 1) amount = 1;
po = net.newPacket(0x00A2); // CMSG_PLAYER_INVENTORY_DROP
po.writeInt16(index);
po.writeInt16(amount);
} break;
case "to_storage": {
int index = args.arg(2).toint();
int amount = args.arg(3).toint();
if(amount < 1) amount = 1;
po = net.newPacket(0x00F3); // CMSG_MOVE_TO_STORAGE
po.writeInt16(index);
po.writeInt32(amount);
} break;
case "from_storage": {
int index = args.arg(2).toint();
int amount = args.arg(3).toint();
if(amount < 1) amount = 1;
po = net.newPacket(0x00F5); // CMSG_MOVE_FROM_STORAGE
po.writeInt16(index);
po.writeInt32(amount);
} break;
case "npc_talk": {
int npcId = args.arg(2).toint();
po = net.newPacket(0x0090); // CMSG_NPC_TALK
po.writeInt32(npcId);
po.writeInt8(0);
} break;
case "npc_buy_sell": {
int npcId = args.arg(2).toint();
boolean buy_sell = args.arg(3).toboolean(); // true for selling
po = net.newPacket(0x00C5); // CMSG_NPC_BUY_SELL_REQUEST
po.writeInt32(npcId);
po.writeInt8( buy_sell ? 1 : 0 );
} break;
case "npc_buy_item": {
int npcId = args.arg(2).toint();
int itemId = args.arg(3).toint();
int amount = args.arg(4).toint();
if(amount < 1) amount = 1;
po = net.newPacket(0x00C8); // CMSG_NPC_BUY_REQUEST
po.writeInt16(8); // one item (length of packet)
po.writeInt16(amount);
po.writeInt16(itemId);
} break;
case "npc_sell_item": {
int npcId = args.arg(2).toint();
int index = args.arg(3).toint();
int amount = args.arg(4).toint();
if(amount < 1) amount = 1;
po = net.newPacket(0x00C9); // CMSG_NPC_SELL_REQUEST
po.writeInt16(8);
po.writeInt16(index);
po.writeInt16(amount);
} break;
case "npc_next": {
int npcId = args.arg(2).toint();
po = net.newPacket(0x00B9); // CMSG_NPC_NEXT_REQUEST
po.writeInt32(npcId);
} break;
case "npc_close": {
int npcId = args.arg(2).toint();
po = net.newPacket(0x0146); // CMSG_NPC_CLOSE
po.writeInt32(npcId);
} break;
case "npc_choise": {
int npcId = args.arg(2).toint();
int choise = args.arg(3).toint();
po = net.newPacket(0x00B8); // CMSG_LIST_CHOISE
po.writeInt32(npcId);
po.writeInt8(choise);
} break;
case "npc_int_input": {
int npcId = args.arg(2).toint();
int value = args.arg(3).toint();
if(args.arg(3).isnil()) value = 0;
po = net.newPacket(0x0143); // CMSG_NPC_INT_RESPONSE
po.writeInt32(npcId);
po.writeInt32(value);
} break;
case "npc_str_input": {
int npcId = args.arg(2).toint();
String value = args.arg(3).toString();
if(args.arg(3).isnil()) value = "";
po = net.newPacket(0x01D5); // CMSG_NPC_STR_RESPONSE
po.writeInt16(value.length() + 9);
po.writeInt32(npcId);
po.writeString(value.length() + 1, value);
} break;
case "attack": {
int id = args.arg(2).toint();
po = net.newPacket(0x0089); // CMSG_PLAYER_ATTACK
po.writeInt32(id);
po.writeInt8(0);
} break;
case "emote": {
int emoteId = args.arg(2).toint();
po = net.newPacket(0x00BF); // CMSG_PLAYER_EMOTE
po.writeInt8(emoteId);
} break;
case "increase_attribute": {
String attr_name = args.arg(2).toString();
int attr = -1;
switch(attr_name) {
case "str": attr = 13; break;
case "agi": attr = 14; break;
case "vit": attr = 15; break;
case "int": attr = 16; break;
case "dex": attr = 17; break;
case "luk": attr = 18; break;
}
if(attr == -1) break;
po = net.newPacket(0x00BB); // CMSG_STAT_UPDATE_REQUEST
po.writeInt16(attr);
po.writeInt8(1);
} break;
case "increase_skill": {
int skillId = args.arg(2).toint();
po = net.newPacket(0x0112); // CMSG_SKILL_LEVELUP_REQUEST
po.writeInt16(skillId);
} break;
case "pickup": {
int id = args.arg(2).toint();
po = net.newPacket(0x009F); // CMSG_ITEM_PICKUP
po.writeInt32(id);
} break;
case "turn": {
int dir = args.arg(2).toint();
po = net.newPacket(0x009B); // CMSG_PLAYER_CHANGE_DIR
po.writeInt16(0);
po.writeInt8(dir);
} break;
case "action": {
String act = args.arg(2).toString();
int type;
if(act.equals("sit")) type = 2;
else if(act.equals("stand")) type = 3;
else break;
po = net.newPacket(0x0089); // CMSG_PLAYER_CHANGE_ACT
po.writeInt32(0);
po.writeInt8(type);
} break;
case "respawn": {
po = net.newPacket(0x00B2); // CMSG_PLAYER_RESTART
po.writeInt8(0);
} break;
case "trade_request": {
int id = args.arg(2).toint();
po = net.newPacket(0x00E4); // CMSG_TRADE_REQUEST
po.writeInt32(id);
} break;
case "trade_response": {
boolean accept = args.arg(2).toboolean();
po = net.newPacket(0x00E6); // CMSG_TRADE_RESPONSE
po.writeInt8(accept ? 3 : 4);
} break;
case "trade_add": {
int index = args.arg(2).toint();
int amount = args.arg(3).toint();
po = net.newPacket(0x00E8); // CMSG_TRADE_ITEM_ADD_REQUEST
po.writeInt16(index);
po.writeInt32(amount);
} break;
case "trade_set_money": {
int amount = args.arg(3).toint();
po = net.newPacket(0x00E8); // CMSG_TRADE_ITEM_ADD_REQUEST
po.writeInt16(0);
po.writeInt32(amount);
} break;
case "trade_confirm": {
po = net.newPacket(0x00EB); // CMSG_TRADE_ADD_COMPLETE
} break;
case "trade_finish": {
po = net.newPacket(0x00EF); // CMSG_TRADE_OK
} break;
case "trade_cancel": {
po = net.newPacket(0x00ED); // CMSG_TRADE_CANCEL
} break;
default:
writeLock.release();
return NIL;
}
if(po != null) {
po.send();
// System.out.println(po);
}
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
} catch(InterruptedException e) {
e.printStackTrace();
}
writeLock.release();
return NIL;
}
});
Thread reader = new Thread(new Runnable() {
@Override
public void run() {
try {
while(!quit) {
Net.PacketIn pi = net.readPacket();
int packet = pi.getId();
dataLock.acquire();
switch(packet) {
case 0x0078: // SMSG_BEING_VISIBLE
case 0x007B: { // SMSG_BEING_MOVE
int id = pi.readInt32();
if(id == character.get("id").toint()) System.out.println("character being_move");
int speed = pi.readInt16();
int stunMode = pi.readInt16();
int statusEffects = pi.readInt16();
statusEffects |= pi.readInt16() << 16;
int job = pi.readInt16();
LuaValue being = beings.get(id);
if(being == NIL) {
if(job == 0 && id >= 110000000) {
pi.skip();
break;
} else {
being = createBeing(id, job);
}
}
being.set("id", id);
being.set("speed", speed);
being.set("stun_mode", stunMode);
being.set("status_effects", statusEffects);
being.set("hair_style", pi.readInt16());
being.set("weapon", pi.readInt16());
being.set("legs", pi.readInt16()); //headbottom
if(packet == 0x007B) {
pi.skip(4);
}
being.set("shield", pi.readInt16());
being.set("helmet", pi.readInt16()); //headtop
being.set("armor", pi.readInt16()); //headmid
being.set("hair_color", pi.readInt16());
being.set("boots", pi.readInt16());
being.set("gloves", pi.readInt16());
being.set("guild", pi.readInt32());
pi.skip(4);
being.set("status_effect_block", pi.readInt16());
pi.skip(1);
being.set("gender", pi.readInt8());
if(packet == 0x007B) {
pi.readCoordinatePair(being);
} else {
pi.readCoordinates(being);
}
pi.skip(5);
if(!being.get("action").toString().equals("dead")) being.set("action", "stand");
beingUpdatePath(being);
// System.out.println("id="+id+" stun_mode="+stunMode+" status_effects="+statusEffects+" status_effect_block="+being.get("status_effect_block"));
packetHandler.call(valueOf("being_update"), valueOf(id));
} break;
case 0x0095: { // SMSG_BEING_NAME_RESPONSE (30)
int id = pi.readInt32();
if(id == character.get("id").toint()) System.out.println("character being_name_response");
LuaValue being = beings.get(id);
if(being != NIL) {
String name = pi.readString(24);
being.set("name", name);
packetHandler.call(valueOf("being_name"), valueOf(id));
} else {
pi.skip();
}
} break;
case 0x007C: { // SMSG_BEING_SPAWN
int id = pi.readInt32();
if(id == character.get("id").toint()) System.out.println("character being_spawn");
pi.skip(14);
int job = pi.readInt16();
pi.skip(14);
LuaValue being = beings.get(id);
if(being == NIL) being = createBeing(id, job);
pi.readCoordinates(being);
being.set("action", "stand");
beingUpdatePath(being);
pi.skip(2);
} break;
case 0x0086: { // SMSG_BEING_MOVE_2
int id = pi.readInt32();
if(id == character.get("id").toint()) System.out.println("character being_move_2");
LuaValue being = beings.get(id);
if(being != NIL) {
pi.readCoordinatePair(being);
pi.skip(4);
if(!being.get("action").toString().equals("dead")) being.set("action", "stand");
beingUpdatePath(being);
packetHandler.call(valueOf("being_update"), valueOf(id));
} else {
pi.skip(7);
}
pi.skip(3); // possibly coordinates
} break;
case 0x0080: { // SMSG_BEING_REMOVE
int id = pi.readInt32();
if(id == character.get("id").toint()) System.out.println("character being_remove");
LuaValue being = beings.get(id);
if(being != NIL) {
if(pi.readInt8() == 1) {
being.set("action", "dead");
} else {
beings.set(id, NIL);
}
packetHandler.call(valueOf("being_remove"), valueOf(id));
} else {
pi.skip(1);
}
} break;
case 0x0148: { // SMSG_BEING_RESURRECT (8)
int id = pi.readInt32();
if(id == character.get("id").toint()) System.out.println("character being_resurrect");
LuaValue being = beings.get(id);
if(being != NIL) {
if(pi.readInt8() == 1) {
being.set("action", "stand");
}
packetHandler.call(valueOf("being_update"), valueOf(id));
} else {
pi.skip(1);
}
pi.skip(1);
} break;
case 0x01DE: { // SMSG_SKILL_DAMAGE
pi.skip(2); // skill id
int srcId = pi.readInt32();
int dstId = pi.readInt32();
LuaValue srcBeing = beings.get(srcId);
LuaValue dstBeing = beings.get(dstId);
pi.skip(4);
int attackSpeed = pi.readInt32();
pi.skip(4); // dst speed
int dmg = pi.readInt32();
pi.skip(2); // skill level
pi.skip(2); // div
pi.skip(1); // skill hit/type (?)
if(attackSpeed != 0 && srcBeing != NIL && srcId != character.get("id").toint()); {
srcBeing.set("attack_speed", attackSpeed);
}
if(srcBeing != NIL && dstBeing != NIL) {
packetHandler.invoke(varargsOf(new LuaValue[] {
valueOf("being_attack"),
valueOf(srcId),
valueOf(dstId),
valueOf(dmg)
}));
}
} break;
case 0x008A: { // SMSG_BEING_ACTION (29)
int srcId = pi.readInt32();
int dstId = pi.readInt32();
LuaValue srcBeing = beings.get(srcId);
LuaValue dstBeing = beings.get(dstId);
pi.skip(12); // 3 zero ints (?)
int param1 = pi.readInt16();
pi.skip(2); // param2
int type = pi.readInt8();
pi.skip(2); // param3
String typeStr = "";
switch(type) {
case 0x00: // hit
typeStr = "hit";
break;
case 0x0A: // critical hit
typeStr = "critical";
break;
case 0x08: // multi hit
typeStr = "multi";
break;
case 0x0B: // flee
typeStr = "flee";
break;
case 0x02: // sit
typeStr = "sit";
break;
case 0x03: // stand up
typeStr = "stand";
break;
}
if(!dstBeing.isnil() && dstBeing.get("type").toString().equals("monster")
&& (typeStr.equals("hit") || typeStr.equals("critical") || typeStr.equals("multi")) ) {
dstBeing.set("walk_time", 0);
}
if(typeStr.equals("sit") || typeStr.equals("stand")) {
if(srcBeing != NIL) srcBeing.set("action", typeStr);
}
packetHandler.invoke(varargsOf(new LuaValue[] {
valueOf("being_action"),
valueOf(srcId),
valueOf(dstId),
valueOf(typeStr),
valueOf(param1)
}));
} break;
case 0x019B: { // SMSG_BEING_SELFEFFECT (10)
int id = pi.readInt32();
if(id == character.get("id").toint()) System.out.println("character being_selfeffect");
LuaValue being = beings.get(id);
if(being == NIL) {
pi.skip();
break;
}
int effectType = pi.readInt32();
packetHandler.call(
valueOf("being_selfeffect"),
valueOf(id),
valueOf(effectType)
);
} break;
case 0x00C0: { // SMSG_BEING_EMOTION (7)
int dstId = pi.readInt32();
int emote = pi.readInt8();
packetHandler.call(
valueOf("being_emote"),
valueOf(dstId),
valueOf(emote)
);
} break;
case 0x00C3: // SMSG_BEING_CHANGE_LOOKS (8)
case 0x01D7: { // SMSG_BEING_CHANGE_LOOKS2 (11)
int dstId = pi.readInt32();
LuaValue being = beings.get(dstId);
if(being == NIL) {
pi.skip();
break;
}
int type = pi.readInt8();
int id = 0;
int id2 = 0;
String typeStr = "";
if(packet == 0x00C3) {
id = pi.readInt8();
} else {
id = pi.readInt16();
id2 = pi.readInt16();
}
switch(type) {
case 1: