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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
|
/*
* The Mana World
* Copyright 2004 The Mana World Development Team
*
* This file is part of The Mana World.
*
* The Mana World is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* The Mana World is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with The Mana World; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id$
*/
#include "game.h"
#include <fstream>
#include <physfs.h>
#include <sstream>
#include <string>
#include <SDL_types.h>
#include <guichan/sdl/sdlinput.hpp>
#include "being.h"
#include "configuration.h"
#include "engine.h"
#include "equipment.h"
#include "floor_item.h"
#include "graphics.h"
#include "inventory.h"
#include "item.h"
#include "log.h"
#include "main.h"
#include "map.h"
#include "playerinfo.h"
#include "sound.h"
#include "gui/buy.h"
#include "gui/buysell.h"
#include "gui/chargedialog.h"
#include "gui/chat.h"
#include "gui/confirm_dialog.h"
#include "gui/equipmentwindow.h"
#include "gui/gui.h"
#include "gui/help.h"
#include "gui/inventorywindow.h"
#include "gui/minimap.h"
#include "gui/npc.h"
#include "gui/npc_text.h"
#include "gui/ok_dialog.h"
#include "gui/popupmenu.h"
#include "gui/requesttrade.h"
#include "gui/sell.h"
#include "gui/setup.h"
#include "gui/skill.h"
#include "gui/menuwindow.h"
#include "gui/status.h"
#include "gui/ministatus.h"
#include "gui/trade.h"
#include "gui/debugwindow.h"
#include "net/messagein.h"
#include "net/messageout.h"
#include "net/network.h"
#include "net/protocol.h"
#include "resources/imagewriter.h"
extern Graphics *graphics;
std::string map_path;
std::string tradePartnerName;
bool refresh_beings = false;
unsigned char keyb_state;
volatile int tick_time;
volatile bool action_time = false;
int server_tick;
int fps = 0, frame = 0, current_npc = 0;
bool displayPathToMouse = false;
Uint16 startX = 0, startY = 0;
Being *autoTarget = NULL;
Engine *engine = NULL;
SDL_Joystick *joypad = NULL; /**< Joypad object */
OkDialog *weightNotice = NULL;
OkDialog *deathNotice = NULL;
ConfirmDialog *exitConfirm = NULL;
ChatWindow *chatWindow;
MenuWindow *menuWindow;
StatusWindow *statusWindow;
MiniStatusWindow *miniStatusWindow;
BuyDialog *buyDialog;
SellDialog *sellDialog;
BuySellDialog *buySellDialog;
InventoryWindow *inventoryWindow;
NpcListDialog *npcListDialog;
NpcTextDialog *npcTextDialog;
SkillDialog *skillDialog;
//NewSkillDialog *newSkillWindow;
Setup* setupWindow;
Minimap *minimap;
EquipmentWindow *equipmentWindow;
ChargeDialog *chargeDialog;
TradeWindow *tradeWindow;
//BuddyWindow *buddyWindow;
HelpWindow *helpWindow;
PopupMenu *popupMenu;
DebugWindow *debugWindow;
Inventory *inventory = NULL;
const int EMOTION_TIME = 150; /**< Duration of emotion icon */
const int MAX_TIME = 10000;
class WeightNoticeListener : public gcn::ActionListener
{
public:
void action(const std::string &eventId)
{
weightNotice = NULL;
}
} weightNoticeListener;
/**
* Listener used for handling death message.
*/
class DeathNoticeListener : public gcn::ActionListener {
public:
void action(const std::string &eventId) {
MessageOut outMsg;
outMsg.writeShort(0x00b2);
outMsg.writeByte(0);
deathNotice = NULL;
}
} deathNoticeListener;
/**
* Listener used for exitting handling.
*/
class ExitListener : public gcn::ActionListener {
void action(const std::string &eventId) {
if (eventId == "yes") {
state = EXIT_STATE;
}
exitConfirm = NULL;
}
} exitListener;
/**
* Advances game logic counter.
*/
Uint32 nextTick(Uint32 interval, void *param)
{
tick_time++;
if (tick_time == MAX_TIME) tick_time = 0;
return interval;
}
/**
* Lets u only trigger an action every other second
* tmp. counts fps
*/
Uint32 nextSecond(Uint32 interval, void *param)
{
action_time = true;
fps = frame;
frame = 0;
return interval;
}
int get_elapsed_time(int start_time)
{
if (start_time <= tick_time) {
return (tick_time - start_time) * 10;
}
else {
return (tick_time + (MAX_TIME - start_time)) * 10;
}
}
/**
* Create all the various globally accessible gui windows
*/
void createGuiWindows()
{
// Create dialogs
chatWindow = new ChatWindow(
config.getValue("homeDir", "") + std::string("/chatlog.txt"));
menuWindow = new MenuWindow();
statusWindow = new StatusWindow();
miniStatusWindow = new MiniStatusWindow();
buyDialog = new BuyDialog();
sellDialog = new SellDialog();
buySellDialog = new BuySellDialog();
inventoryWindow = new InventoryWindow();
npcTextDialog = new NpcTextDialog();
npcListDialog = new NpcListDialog();
skillDialog = new SkillDialog();
//newSkillWindow = new NewSkillDialog();
setupWindow = new Setup();
minimap = new Minimap();
equipmentWindow = new EquipmentWindow();
chargeDialog = new ChargeDialog();
tradeWindow = new TradeWindow();
//buddyWindow = new BuddyWindow();
helpWindow = new HelpWindow();
popupMenu = new PopupMenu();
debugWindow = new DebugWindow();
// Initialize window positions
int screenW = graphics->getWidth();
int screenH = graphics->getHeight();
chargeDialog->setPosition(
screenW - 5 - chargeDialog->getWidth(),
screenH - chargeDialog->getHeight() - 15);
tradeWindow->setPosition(screenW - statusWindow->getWidth() -
tradeWindow->getWidth() - 10,
inventoryWindow->getY() + inventoryWindow->getHeight());
/*buddyWindow->setPosition(10,
minimap->getHeight() + 30);*/
// Set initial window visibility
chatWindow->setVisible(true);
miniStatusWindow->setVisible(true);
statusWindow->setVisible(false);
menuWindow->setVisible(true);
buyDialog->setVisible(false);
sellDialog->setVisible(false);
buySellDialog->setVisible(false);
inventoryWindow->setVisible(false);
npcTextDialog->setVisible(false);
npcListDialog->setVisible(false);
skillDialog->setVisible(false);
//newSkillWindow->setVisible(false);
setupWindow->setVisible(false);
equipmentWindow->setVisible(false);
chargeDialog->setVisible(false);
tradeWindow->setVisible(false);
//buddyWindow->setVisible(false);
helpWindow->setVisible(false);
popupMenu->setVisible(false);
debugWindow->setVisible(false);
// Do not focus any text field
gui->focusNone();
}
/**
* Destroy all the globally accessible gui windows
*/
void destroyGuiWindows()
{
delete chatWindow;
delete statusWindow;
delete miniStatusWindow;
delete menuWindow;
delete buyDialog;
delete sellDialog;
delete buySellDialog;
delete inventoryWindow;
delete npcListDialog;
delete npcTextDialog;
delete skillDialog;
delete setupWindow;
delete minimap;
delete equipmentWindow;
delete chargeDialog;
//delete newSkillWindow;
delete tradeWindow;
//delete buddyWindow;
delete helpWindow;
delete popupMenu;
delete debugWindow;
}
void do_init()
{
engine->changeMap(map_path);
// Initialize timers
tick_time = 0;
SDL_AddTimer(10, nextTick, NULL); // Logic counter
SDL_AddTimer(1000, nextSecond, NULL); // Seconds counter
// Initialize beings
player_node = createBeing(account_ID, 0, engine->getCurrentMap());
player_node->x = startX;
player_node->y = startY;
player_node->setHairColor(player_info->hairColor);
player_node->setHairStyle(player_info->hairStyle);
if (player_info->weapon == 11)
{
player_info->weapon = 2;
}
player_node->setWeapon(player_info->weapon);
remove("packet.list");
// Initialize joypad
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
//SDL_JoystickEventState(SDL_ENABLE);
int num_joy = SDL_NumJoysticks();
logger->log("%i joysticks/gamepads found", num_joy);
for (int i = 0; i < num_joy; i++)
logger->log("- %s", SDL_JoystickName(i));
// TODO: The user should be able to choose which one to use
// Open the first device
if (num_joy > 0)
{
joypad = SDL_JoystickOpen(0);
if (joypad == NULL)
{
logger->log("Couldn't open joystick: %s", SDL_GetError());
}
else {
logger->log("Axes: %i ", SDL_JoystickNumAxes(joypad));
logger->log("Balls: %i", SDL_JoystickNumBalls(joypad));
logger->log("Hats: %i", SDL_JoystickNumHats(joypad));
logger->log("Buttons: %i", SDL_JoystickNumButtons(joypad));
}
}
}
bool saveScreenshot(SDL_Surface *screenshot)
{
static unsigned int screenshotCount = 0;
// Search for an unused screenshot name
std::stringstream filename;
std::fstream testExists;
bool found = false;
do {
screenshotCount++;
filename.str("");
#ifdef __USE_UNIX98
filename << PHYSFS_getUserDir() << "/";
#endif
filename << "TMW_Screenshot_" << screenshotCount << ".png";
testExists.open(filename.str().c_str(), std::ios::in);
found = !testExists.is_open();
testExists.close();
} while (!found);
return ImageWriter::writePNG(screenshot, filename.str());
}
void game()
{
// Needs to be initialised _before_ the engine is created...
inventory = new Inventory();
createGuiWindows();
engine = new Engine();
do_init();
int gameTime = tick_time;
while (state == GAME_STATE)
{
// Handle all necessary game logic
while (get_elapsed_time(gameTime) > 0)
{
do_input();
engine->logic();
gui->logic();
gameTime++;
}
gameTime = tick_time;
// Update the screen when application is active, delay otherwise
if (SDL_GetAppState() & SDL_APPACTIVE)
{
engine->draw();
graphics->updateScreen();
}
else
{
SDL_Delay(10);
}
// Handle network stuff and flush it
do_parse();
flush();
}
do_exit();
}
void do_exit()
{
delete engine;
delete player_node;
destroyGuiWindows();
close_session();
delete inventory;
if (joypad != NULL)
{
SDL_JoystickClose(joypad);
}
}
void do_input()
{
// Get the state of the keyboard keys
Uint8* keys;
keys = SDL_GetKeyState(NULL);
// TODO: Only <= 6 buttons joypads are allowed
bool joy[10];
memset(joy, 0, 10 * sizeof(bool));
// Get the state of the joypad axis/buttons
if (joypad != NULL)
{
// TODO: one different value of tolerance is needed for each direction
// This probably means the need for a tuning utility/window
int tolerance = (int)config.getValue("joytolerance", 100);
SDL_JoystickUpdate();
if (SDL_JoystickGetAxis(joypad, 0) > tolerance)
{
joy[JOY_RIGHT] = true;
}
if (SDL_JoystickGetAxis(joypad, 0) < -tolerance)
{
joy[JOY_LEFT] = true;
}
if (SDL_JoystickGetAxis(joypad, 1) < -tolerance)
{
joy[JOY_UP] = true;
}
if (SDL_JoystickGetAxis(joypad, 1) > tolerance)
{
joy[JOY_DOWN] = true;
}
for (int i=0; i<6; i++)
{
if (SDL_JoystickGetButton(joypad, i) == 1)
{
joy[JOY_BTN0 + i] = true;
}
}
}
// Events
SDL_Event event;
while (SDL_PollEvent(&event))
{
bool used = false;
// Keyboard events (for discontinuous keys)
if (event.type == SDL_KEYDOWN)
{
gcn::Window *requestedWindow = NULL;
switch (event.key.keysym.sym)
{
case SDLK_F1:
// In-game Help
if (helpWindow->isVisible())
{
helpWindow->setVisible(false);
}
else
{
helpWindow->loadHelp("index");
helpWindow->requestMoveToTop();
}
used = true;
break;
case SDLK_F2: requestedWindow = statusWindow; break;
case SDLK_F3: requestedWindow = inventoryWindow; break;
case SDLK_F4: requestedWindow = equipmentWindow; break;
case SDLK_F5: requestedWindow = skillDialog; break;
case SDLK_F6: requestedWindow = minimap; break;
case SDLK_F7: requestedWindow = chatWindow; break;
//case SDLK_F8: requestedWindow = buddyWindow; break;
case SDLK_F9:
// Setup window
setupWindow->setVisible(true);
setupWindow->requestMoveToTop();
used = true;
break;
case SDLK_F10: requestedWindow = debugWindow; break;
//case SDLK_F11: requestedWindow = newSkillWindow; break;
case SDLK_RETURN:
// Input chat window
if (chatWindow->isFocused() ||
deathNotice != NULL ||
weightNotice != NULL)
{
break;
}
// Quit by pressing Enter if the exit confirm is there
if (exitConfirm)
{
state = EXIT_STATE;
}
// Close the Browser if opened
else if (helpWindow->isVisible())
{
helpWindow->setVisible(false);
}
// Close the config window, cancelling changes if opened
else if (setupWindow->isVisible())
{
setupWindow->action("cancel");
}
// Else, open the chat edit box
else
{
chatWindow->requestChatFocus();
used = true;
}
break;
// Picking up items on the floor
case SDLK_g:
case SDLK_z:
if (!chatWindow->isFocused())
{
Uint32 id = find_floor_item_by_cor(
player_node->x, player_node->y);
// If none below the player, try the tile in front of
// the player
if (!id) {
Uint16 x = player_node->x;
Uint16 y = player_node->y;
switch (player_node->direction)
{
case Being::NORTH: y--; break;
case Being::SOUTH: y++; break;
case Being::WEST: x--; break;
case Being::EAST: x++; break;
case Being::NW: x--; y--; break;
case Being::NE: x++; y--; break;
case Being::SW: x--; y++; break;
case Being::SE: x++; y++; break;
default: break;
}
id = find_floor_item_by_cor(x, y);
}
if (id)
{
// TODO: Remove duplicated code, probably add a
// pick up command
MessageOut outMsg;
outMsg.writeShort(0x009f);
outMsg.writeLong(id);
}
used = true;
}
break;
// Quitting confirmation dialog
case SDLK_ESCAPE:
if (!exitConfirm) {
exitConfirm = new ConfirmDialog(
"Quit", "Are you sure you want to quit?",
(gcn::ActionListener*)&exitListener);
}
exitConfirm->requestMoveToTop();
break;
default:
break;
}
if (requestedWindow)
{
requestedWindow->setVisible(!requestedWindow->isVisible());
if (requestedWindow->isVisible())
{
requestedWindow->requestMoveToTop();
}
used = true;
}
// Keys pressed together with Alt/Meta
// Emotions and some internal gui windows
if (event.key.keysym.mod & KMOD_ALT)
{
switch (event.key.keysym.sym)
{
case SDLK_s:
// Player sit action
if (!action_time) {
break;
}
switch (player_node->action)
{
case Being::STAND: action(2, 0); break;
case Being::SIT: action(3, 0); break;
}
break;
case SDLK_p:
// Screenshot (picture, hence the p)
{
SDL_Surface *screenshot = graphics->getScreenshot();
if (!saveScreenshot(screenshot))
{
logger->log("Error: could not save Screenshot.");
}
SDL_FreeSurface(screenshot);
}
break;
default:
break;
case SDLK_f:
// Find path to mouse (debug purpose)
displayPathToMouse = !displayPathToMouse;
break;
}
// Emotions
if (action_time && !player_node->emotion)
{
Uint8 emotion = 0;
switch (event.key.keysym.sym)
{
case SDLK_1: emotion = 1; break;
case SDLK_2: emotion = 2; break;
case SDLK_3: emotion = 3; break;
case SDLK_4: emotion = 4; break;
case SDLK_5: emotion = 5; break;
case SDLK_6: emotion = 6; break;
case SDLK_7: emotion = 7; break;
case SDLK_8: emotion = 8; break;
case SDLK_9: emotion = 9; break;
case SDLK_0: emotion = 10; break;
default: break;
}
if (emotion)
{
MessageOut outMsg;
outMsg.writeShort(0x00bf);
outMsg.writeByte(emotion);
action_time = false;
used = true;
}
}
}
}
// Mouse events
else if (event.type == SDL_MOUSEBUTTONDOWN)
{
int mx = event.button.x / 32 + camera_x;
int my = event.button.y / 32 + camera_y;
// Mouse button left
if (event.button.button == SDL_BUTTON_LEFT)
{
// Don't move when shift is pressed
// XXX Is this too hackish? I'm not sure if making the Gui
// class a KeyListener is a good idea and works as expected
// at all...
if (keys[SDLK_LSHIFT] || keys[SDLK_RSHIFT]) {
used = true;
}
// Check for default actions for NPC/Monster/Players
Being *target = findNode(mx, my);
Uint32 floorItemId = find_floor_item_by_cor(mx, my);
if (target)
{
switch (target->getType())
{
case Being::NPC:
// NPC default: talk
if (!current_npc)
{
MessageOut outMsg;
outMsg.writeShort(CMSG_NPC_TALK);
outMsg.writeLong(target->getId());
outMsg.writeByte(0);
current_npc = target->getId();
}
break;
case Being::MONSTER:
case Being::PLAYER:
// Monster and player default: attack
/**
* TODO: Move player to mouse click position before
* attack the monster (maybe using follow mode).
*/
if (target->action != Being::MONSTER_DEAD &&
player_node->action == Being::STAND &&
target != player_node)
{
// Autotarget by default with mouse
autoTarget = target;
attack(target);
}
break;
default:
break;
}
}
// Check for default action to items on the floor
else if (floorItemId != 0)
{
/**
* TODO: Move player to mouse click position before
* pick up the items on the floor.
*
* Provisory: pick up only items near of player.
*/
int dx = mx - player_node->x;
int dy = my - player_node->y;
// "sqrt(dx*dx + dy*dy) < 2" is equal to "dx*dx + dy*dy < 4"
if ((dx*dx + dy*dy) < 4)
{
MessageOut outMsg;
outMsg.writeShort(0x009f);
outMsg.writeLong(floorItemId);
}
}
// Just cancel the popup menu if shown, and don't make the
// character walk
if (popupMenu->isVisible() == true)
{
// If we click elsewhere than in the window, do not use
// the event
// The user wanted to close the popup.
// Still buggy : Wonder if the x, y, width, and height
// aren't reported partially with these functions.
if (event.button.x >=
(popupMenu->getX() + popupMenu->getWidth()) ||
event.button.x < popupMenu->getX() ||
event.button.y >=
(popupMenu->getY() + popupMenu->getHeight()) ||
event.button.y < popupMenu->getY())
{
used = true;
popupMenu->setVisible(false);
}
}
} // End Mouse left button
// Mouse button middle
else if (event.button.button == SDL_BUTTON_MIDDLE)
{
/*
* Some people haven't a mouse with three buttons,
* right Usiu??? ;-)
*/
}
// Mouse button right
else if (event.button.button == SDL_BUTTON_RIGHT)
{
Being *being;
FloorItem *floorItem;
if ((being = findNode(mx, my)) && being != player_node) {
popupMenu->showPopup(event.button.x, event.button.y,
being);
} else if ((floorItem = find_floor_item_by_id(
find_floor_item_by_cor(mx, my)))) {
popupMenu->showPopup(event.button.x, event.button.y,
floorItem);
} else {
popupMenu->setVisible(false);
}
}
}
// Quit event
else if (event.type == SDL_QUIT)
{
state = EXIT_STATE;
}
// Push input to GUI when not used
if (!used) {
guiInput->pushInput(event);
}
} // End while
// Moving player around
if (player_node->action != Being::DEAD &&
current_npc == 0 &&
!chatWindow->isFocused())
{
Uint16 x = player_node->x;
Uint16 y = player_node->y;
Sint16 xDirection = 0;
Sint16 yDirection = 0;
Being::Direction Direction = Being::DIR_NONE;
// Translate pressed keys to movement and direction
if (keys[SDLK_UP] || keys[SDLK_KP8] || joy[JOY_UP])
{
yDirection = -1;
if (player_node->action != Being::WALK)
Direction = Being::NORTH;
}
if (keys[SDLK_DOWN] || keys[SDLK_KP2] || joy[JOY_DOWN])
{
yDirection = 1;
if (player_node->action != Being::WALK)
Direction = Being::SOUTH;
}
if (keys[SDLK_LEFT] || keys[SDLK_KP4] || joy[JOY_LEFT])
{
xDirection = -1;
if (player_node->action != Being::WALK)
Direction = Being::WEST;
}
if (keys[SDLK_RIGHT] || keys[SDLK_KP6] || joy[JOY_RIGHT])
{
xDirection = 1;
if (player_node->action != Being::WALK)
Direction = Being::EAST;
}
if (keys[SDLK_KP1]) // Bottom Left
{
xDirection = -1;
yDirection = 1;
if (player_node->action != Being::WALK)
Direction = Being::SW;
}
if (keys[SDLK_KP3]) // Bottom Right
{
xDirection = 1;
yDirection = 1;
if (player_node->action != Being::WALK)
Direction = Being::SE;
}
if (keys[SDLK_KP7]) // Top Left
{
xDirection = -1;
yDirection = -1;
if (player_node->action != Being::WALK)
Direction = Being::NW;
}
if (keys[SDLK_KP9]) // Top Right
{
xDirection = 1;
yDirection = -1;
if (player_node->action != Being::WALK)
Direction = Being::NE;
}
Map *tiledMap = engine->getCurrentMap();
// Allow keyboard control to interrupt an existing path
if ((xDirection != 0 || yDirection != 0) &&
player_node->action == Being::WALK)
{
player_node->setDestination(x, y);
}
if (player_node->action != Being::WALK)
{
// Prevent skipping corners over colliding tiles
if ((xDirection != 0) && tiledMap->tileCollides(x + xDirection, y))
xDirection = 0;
if ((yDirection != 0) && tiledMap->tileCollides(x, y + yDirection))
yDirection = 0;
// Choose a straight direction when diagonal target is blocked
if ((yDirection != 0) && (xDirection != 0) &&
!tiledMap->getWalk(x + xDirection, y + yDirection))
xDirection = 0;
// Walk to where the player can actually go
if (((xDirection != 0) || (yDirection != 0)) &&
tiledMap->getWalk(x + xDirection, y + yDirection))
{
walk(x + xDirection, y + yDirection, Direction);
player_node->setDestination(x + xDirection, y + yDirection);
}
else if (Direction != Being::DIR_NONE)
{
// Update the player direction to where he wants to walk
// Warning: Not communicated to the server yet
player_node->direction = Direction;
}
}
// Attacking monsters
if (player_node->action == Being::STAND)
{
if (keys[SDLK_LCTRL] || keys[SDLK_RCTRL] || joy[JOY_BTN0])
{
Being *monster = attack(x, y, player_node->direction);
if (monster == NULL && autoTarget != NULL &&
monster != player_node)
{
attack(autoTarget);
}
else if ((keys[SDLK_LSHIFT] || keys[SDLK_RSHIFT]) && monster != player_node)
{
autoTarget = monster;
}
}
}
if (joy[JOY_BTN1])
{
Uint32 id = find_floor_item_by_cor(player_node->x, player_node->y);
if (id != 0)
{
MessageOut outMsg;
outMsg.writeShort(CMSG_ITEM_PICKUP);
outMsg.writeLong(id);
}
}
else if (joy[JOY_BTN2] && action_time)
{
if (player_node->action == Being::STAND)
action(2, 0);
else if (player_node->action == Being::SIT)
action(3, 0);
action_time = false;
}
}
}
void do_parse()
{
Map *tiledMap = engine->getCurrentMap();
Equipment *equipment = Equipment::getInstance();
int n_items;
Being *being;
// We need at least 2 bytes to identify a packet
while (in_size >= 2)
{
MessageIn msg = get_next_message();
// Parse packet based on their id
switch (msg.getId())
{
case SMSG_LOGIN_SUCCESS:
// Connected to game server succesfully, set spawn point
msg.readLong(); // server tick
msg.readCoordinates(player_node->x, player_node->y,
player_node->direction);
msg.skip(2); // unknown
break;
// Received speech from being
case SMSG_BEING_CHAT:
{
Sint16 chatMsgLength = msg.readShort() - 8;
being = findNode(msg.readLong());
if (!being || chatMsgLength <= 0)
{
break;
}
std::string chatMsg = msg.readString(chatMsgLength);
chatWindow->chat_log(chatMsg, BY_OTHER);
chatMsg.erase(0, chatMsg.find(" : ", 0) + 3);
being->setSpeech(chatMsg, SPEECH_TIME);
}
break;
case SMSG_PLAYER_CHAT:
case SMSG_GM_CHAT:
{
Sint16 chatMsgLength = msg.readShort() - 4;
if (chatMsgLength <= 0)
{
break;
}
std::string chatMsg = msg.readString(chatMsgLength);
if (msg.getId() == SMSG_PLAYER_CHAT)
{
chatWindow->chat_log(chatMsg, BY_PLAYER);
std::string::size_type pos = chatMsg.find(" : ", 0);
if (pos != std::string::npos)
{
chatMsg.erase(0, pos + 3);
}
player_node->setSpeech(chatMsg, SPEECH_TIME);
}
else
{
chatWindow->chat_log(chatMsg, BY_GM);
}
}
break;
case SMSG_WALK_RESPONSE:
// It is assumed by the client any request to walk actually
// succeeds on the server. The plan is to have a correction
// message when the server senses the client has the wrong
// idea.
break;
case SMSG_BEING_VISIBLE:
case SMSG_BEING_MOVE:
// Information about a being in range
{
Uint32 id = msg.readLong();
Uint16 speed = msg.readShort();
msg.readShort(); // unknown
msg.readShort(); // unknown
msg.readShort(); // option
Uint16 job = msg.readShort(); // class
being = findNode(id);
if (being == NULL)
{
// Being with id >= 110000000 and job 0 are better
// known as ghosts, so don't create those.
if (job == 0 && id >= 110000000)
{
break;
}
being = createBeing(id, job, tiledMap);
}
else if (msg.getId() == 0x0078)
{
being->clearPath();
being->mFrame = 0;
being->walk_time = tick_time;
being->action = Being::STAND;
}
// Prevent division by 0 when calculating frame
if (speed == 0) { speed = 150; }
being->setWalkSpeed(speed);
being->job = job;
being->setHairStyle(msg.readShort());
being->setWeapon(msg.readShort());
msg.readShort(); // head option bottom
if (msg.getId() == SMSG_BEING_MOVE)
{
msg.readLong(); // server tick
}
msg.readShort(); // shield
msg.readShort(); // head option top
msg.readShort(); // head option mid
being->setHairColor(msg.readShort());
msg.readShort(); // unknown
msg.readShort(); // head dir
msg.readShort(); // guild
msg.readShort(); // unknown
msg.readShort(); // unknown
msg.readShort(); // manner
msg.readShort(); // karma
msg.readByte(); // unknown
msg.readByte(); // sex
if (msg.getId() == SMSG_BEING_MOVE)
{
Uint16 srcX, srcY, dstX, dstY;
msg.readCoordinatePair(srcX, srcY, dstX, dstY);
being->action = Being::STAND;
being->x = srcX;
being->y = srcY;
being->setDestination(dstX, dstY);
}
else
{
msg.readCoordinates(being->x, being->y,
being->direction);
}
msg.readByte(); // unknown
msg.readByte(); // unknown
msg.readByte(); // unknown / sit
}
break;
case SMSG_BEING_REMOVE:
// A being should be removed or has died
being = findNode(msg.readLong());
if (being != NULL)
{
if (msg.readByte() == 1)
{
// Death
switch (being->getType())
{
case Being::MONSTER:
being->action = Being::MONSTER_DEAD;
being->mFrame = 0;
being->walk_time = tick_time;
break;
default:
being->action = Being::DEAD;
break;
}
}
else
{
remove_node(being);
}
if (being == autoTarget)
{
autoTarget = NULL;
}
}
break;
case SMSG_PLAYER_UPDATE_1:
case SMSG_PLAYER_UPDATE_2:
case SMSG_PLAYER_MOVE:
// An update about a player, potentially including movement.
{
Uint32 id = msg.readLong();
Uint16 speed = msg.readShort();
msg.readShort(); // option 1
msg.readShort(); // option 2
msg.readShort(); // option
Uint16 job = msg.readShort();
being = findNode(id);
if (being == NULL)
{
being = createBeing(id, job, tiledMap);
}
being->setWalkSpeed(speed);
being->job = job;
being->setHairStyle(msg.readShort());
being->setWeaponById(msg.readShort()); // item id 1
msg.readShort(); // item id 2
msg.readShort(); // head option bottom
if (msg.getId() == SMSG_PLAYER_MOVE)
{
msg.readLong(); // server tick
}
msg.readShort(); // head option top
msg.readShort(); // head option mid
being->setHairColor(msg.readShort());
msg.readShort(); // unknown
msg.readShort(); // head dir
msg.readLong(); // guild
msg.readLong(); // emblem
msg.readShort(); // manner
msg.readByte(); // karma
msg.readByte(); // sex
if (msg.getId() == SMSG_PLAYER_MOVE)
{
Uint16 srcX, srcY, dstX, dstY;
msg.readCoordinatePair(srcX, srcY, dstX, dstY);
being->x = srcX;
being->y = srcY;
being->setDestination(dstX, dstY);
}
else
{
msg.readCoordinates(being->x, being->y,
being->direction);
}
msg.readByte(); // unknown
msg.readByte(); // unknown
if (msg.getId() == SMSG_PLAYER_UPDATE_1)
{
if (msg.readByte() == 2)
{
being->action = Being::SIT;
}
}
else if (msg.getId() == SMSG_PLAYER_MOVE)
{
msg.readByte(); // unknown
}
msg.readByte(); // Lv
msg.readByte(); // unknown
being->walk_time = tick_time;
being->mFrame = 0;
}
break;
case SMSG_NPC_MESSAGE:
msg.readShort(); // length
current_npc = msg.readLong();
npcTextDialog->addText(msg.readString(msg.getLength() - 8));
npcListDialog->setVisible(false);
npcTextDialog->setVisible(true);
break;
case SMSG_NPC_NEXT:
case SMSG_NPC_CLOSE:
// Next/Close button in NPC dialog, currently unused
break;
case SMSG_TRADE_REQUEST:
// If a trade window or request window is already open, send a
// trade cancel to any other trade request.
//
// Note that it would be nice if the server would prevent this
// situation, and that the requesting player would get a
// special message about the player being occupied.
if (tradeWindow->isVisible() || requestTradeDialogOpen)
{
MessageOut outMsg;
outMsg.writeShort(CMSG_TRADE_RESPONSE);
outMsg.writeByte(4);
break;
}
requestTradeDialogOpen = true;
tradePartnerName = msg.readString(24);
new RequestTradeDialog(tradePartnerName);
break;
case SMSG_TRADE_RESPONSE:
switch (msg.readByte())
{
case 0: // Too far away
chatWindow->chat_log("Trading isn't possible. "
"Trade partner is too far away.",
BY_SERVER);
break;
case 1: // Character doesn't exist
chatWindow->chat_log("Trading isn't possible. "
"Character doesn't exist.",
BY_SERVER);
break;
case 2: // Invite request check failed...
chatWindow->chat_log("Trade cancelled due to an "
"unknown reason.", BY_SERVER);
break;
case 3: // Trade accepted
tradeWindow->reset();
tradeWindow->setCaption(
"Trade: You and " + tradePartnerName);
tradeWindow->setVisible(true);
requestTradeDialogOpen = false;
break;
case 4: // Trade cancelled
chatWindow->chat_log("Trade cancelled.", BY_SERVER);
tradeWindow->setVisible(false);
break;
default: // Shouldn't happen as well, but to be sure
chatWindow->chat_log("Unhandled trade cancel packet",
BY_SERVER);
break;
}
break;
case SMSG_TRADE_ITEM_ADD:
{
Sint32 amount = msg.readLong();
Sint16 type = msg.readShort();
msg.readByte(); // identified flag
msg.readByte(); // attribute
msg.readByte(); // refine
msg.skip(8); // card (4 shorts)
// TODO: handle also identified, etc
if (type == 0) {
tradeWindow->addMoney(amount);
} else {
tradeWindow->addItem(type, false, amount, false);
}
}
break;
case SMSG_TRADE_ITEM_ADD_RESPONSE:
// Trade: New Item add response (was 0x00ea, now 01b1)
{
Item *item = inventory->getItem(msg.readShort());
Sint16 quantity = msg.readShort();
switch (msg.readByte())
{
case 0:
// Successfully added item
if (item->isEquipment() && item->isEquipped())
{
inventory->unequipItem(item);
}
tradeWindow->addItem(item->getId(), true, quantity,
item->isEquipment());
item->increaseQuantity(-quantity);
break;
case 1:
// Add item failed - player overweighted
chatWindow->chat_log("Failed adding item. Trade "
"partner is over weighted.",
BY_SERVER);
break;
default:
chatWindow->chat_log("Failed adding item for "
"unknown reason.", BY_SERVER);
break;
}
}
break;
case SMSG_TRADE_OK:
// 0 means ok from myself, 1 means ok from other;
tradeWindow->receivedOk(msg.readByte() == 0);
break;
case SMSG_TRADE_CANCEL:
chatWindow->chat_log("Trade canceled.", BY_SERVER);
tradeWindow->setVisible(false);
tradeWindow->reset();
break;
case SMSG_TRADE_COMPLETE:
chatWindow->chat_log("Trade completed.", BY_SERVER);
tradeWindow->setVisible(false);
tradeWindow->reset();
break;
case SMSG_PLAYER_INVENTORY:
{
// Only called on map load / warp. First reset all items
// to not load them twice on map change.
inventory->resetItems();
msg.readShort(); // length
Sint32 number = (msg.getLength() - 4) / 18;
for (int loop = 0; loop < number; loop++)
{
Sint16 index = msg.readShort();
Sint16 itemId = msg.readShort();
msg.readByte(); // type
msg.readByte(); // identify flag
Sint16 amount = msg.readShort();
msg.skip(2); // unknown
msg.skip(8); // card (4 shorts)
inventory->addItem(index, itemId, amount, false);
// Trick because arrows are not considered equipment
if (itemId == 1199 || itemId == 529)
{
inventory->getItem(index)->setEquipment(true);
}
}
}
break;
case SMSG_PLAYER_EQUIPMENT:
{
msg.readShort(); // length
Sint32 number = (msg.getLength() - 4) / 20;
for (int loop = 0; loop < number; loop++)
{
Sint16 index = msg.readShort();
Sint16 itemId = msg.readShort();
msg.readByte(); // type
msg.readByte(); // identify flag
msg.readShort(); // equip type
Sint16 equipPoint = msg.readShort();
msg.readByte(); // attribute
msg.readByte(); // refine
msg.skip(8); // card
inventory->addItem(index, itemId, 1, true);
if (equipPoint)
{
int mask = 1;
int position = 0;
while (!(equipPoint & mask))
{
mask <<= 1;
position++;
}
Item *item = inventory->getItem(index);
item->setEquipped(true);
equipment->setEquipment(position - 1, item);
}
}
}
break;
case SMSG_ITEM_USE_RESPONSE:
{
Sint16 index = msg.readShort();
Sint16 amount = msg.readShort();
if (msg.readByte() == 0) {
chatWindow->chat_log("Failed to use item", BY_SERVER);
} else {
inventory->getItem(index)->setQuantity(amount);
}
}
break;
case SMSG_PLAYER_WARP:
{
// Set new map path
map_path = "maps/" + msg.readString(16);
map_path = map_path.substr(0, map_path.rfind(".")) +
".tmx.gz";
Uint16 x = msg.readShort();
Uint16 y = msg.readShort();
logger->log("Warping to %s (%d, %d)",
map_path.c_str(), x, y);
// Switch the actual map, deleting the previous one
engine->changeMap(map_path);
tiledMap = engine->getCurrentMap();
autoTarget = NULL;
current_npc = 0;
player_node->action = Being::STAND;
player_node->mFrame = 0;
player_node->x = x;
player_node->y = y;
// Send "map loaded"
MessageOut outMsg;
outMsg.writeShort(CMSG_MAP_LOADED);
}
break;
case SMSG_SKILL_FAILED:
// Action failed (ex. sit because you have not reached the
// right level)
CHATSKILL action;
action.skill = msg.readShort();
action.bskill = msg.readShort();
action.unused = msg.readShort(); // unknown
action.success = msg.readByte();
action.reason = msg.readByte();
if (action.success != SKILL_FAILED &&
action.bskill == BSKILL_EMOTE)
{
printf("Action: %d/%d", action.bskill, action.success);
}
chatWindow->chat_log(action);
break;
case SMSG_PLAYER_STAT_UPDATE_1:
{
Sint16 type = msg.readShort();
Sint32 value = msg.readLong();
switch (type)
{
//case 0x0000:
// player_node->setWalkSpeed(msg.readLong());
// break;
case 0x0005: player_info->hp = value; break;
case 0x0006: player_info->maxHp = value; break;
case 0x0007: player_info->mp = value; break;
case 0x0008: player_info->maxMp = value; break;
case 0x000b: player_info->lvl = value; break;
case 0x000c:
player_info->skillPoint = value;
skillDialog->setPoints(player_info->skillPoint);
break;
case 0x0018:
if (value >= player_info->maxWeight / 2 &&
player_info->totalWeight <
player_info->maxWeight / 2)
{
weightNotice = new OkDialog("Message",
"You are carrying more then half your "
"weight. You are unable to regain "
"health.",
&weightNoticeListener);
}
player_info->totalWeight = value;
break;
case 0x0019: player_info->maxWeight = value; break;
case 0x0037: player_info->jobLvl = value; break;
case 0x0009:
player_info->statsPointsToAttribute = value;
break;
case 0x0029: player_info->ATK = value; break;
case 0x002b: player_info->MATK = value; break;
case 0x002d: player_info->DEF = value; break;
case 0x002f: player_info->MDEF = value; break;
case 0x0031: player_info->HIT = value; break;
case 0x0032: player_info->FLEE = value; break;
case 0x0035: player_node->aspd = value; break;
}
if (player_info->hp == 0 && deathNotice == NULL)
{
deathNotice = new OkDialog("Message",
"You're now dead, press ok to restart",
&deathNoticeListener);
player_node->action = Being::DEAD;
}
}
break;
// Stop walking
// case 0x0088: // Disabled because giving some problems
//if (being = findNode(readLong(2))) {
// if (being->getId() != player_node->getId()) {
// being->action = STAND;
// being->mFrame = 0;
// set_coordinates(being->coordinates,
// readWord(6), readWord(8),
// get_direction(being->coordinates));
// }
//}
//break;
case SMSG_BEING_ACTION:
{
Being *srcBeing = findNode(msg.readLong());
Being *dstBeing = findNode(msg.readLong());
msg.readLong(); // server tick
msg.readLong(); // src speed
msg.readLong(); // dst speed
Sint16 param1 = msg.readShort();
msg.readShort(); // param 2
Sint8 type = msg.readByte();
msg.readShort(); // param 3
switch (type)
{
case 0: // Damage
if (dstBeing == NULL) break;
dstBeing->setDamage(param1, SPEECH_TIME);
if (srcBeing != NULL &&
srcBeing != player_node)
{
// buggy
srcBeing->action = Being::ATTACK;
srcBeing->mFrame = 0;
srcBeing->walk_time = tick_time;
}
break;
case 2: // Sit
if (srcBeing == NULL) break;
srcBeing->mFrame = 0;
srcBeing->action = Being::SIT;
break;
case 3: // Stand up
if (srcBeing == NULL) break;
srcBeing->mFrame = 0;
srcBeing->action = Being::STAND;
break;
}
}
break;
case SMSG_PLAYER_STAT_UPDATE_2:
switch (msg.readShort()) {
case 0x0001:
player_info->xp = msg.readLong();
break;
case 0x0002:
player_info->jobXp = msg.readLong();
break;
case 0x0014:
player_info->gp = msg.readLong();
break;
case 0x0016:
player_info->xpForNextLevel = msg.readLong();
break;
case 0x0017:
player_info->jobXpForNextLevel = msg.readLong();
break;
}
break;
case SMSG_BEING_LEVELUP:
if ((Uint32)msg.readLong() == player_node->getId()) {
logger->log("Level up");
sound.playSfx("sfx/levelup.ogg");
} else {
logger->log("Someone else went level up");
}
msg.readLong(); // type
break;
case SMSG_BEING_EMOTION:
if (!(being = findNode(msg.readLong())))
{
break;
}
being->emotion = msg.readByte();
being->emotion_time = EMOTION_TIME;
break;
case SMSG_PLAYER_STAT_UPDATE_3:
{
Sint32 type = msg.readLong();
Sint32 base = msg.readLong();
Sint32 bonus = msg.readLong();
Sint32 total = base + bonus;
switch (type) {
case 0x000d: player_info->STR = total; break;
case 0x000e: player_info->AGI = total; break;
case 0x000f: player_info->VIT = total; break;
case 0x0010: player_info->INT = total; break;
case 0x0011: player_info->DEX = total; break;
case 0x0012: player_info->LUK = total; break;
}
}
break;
case SMSG_NPC_BUY_SELL_CHOICE:
buyDialog->setVisible(false);
buyDialog->reset();
sellDialog->setVisible(false);
sellDialog->reset();
buySellDialog->setVisible(true);
current_npc = msg.readLong();
break;
case SMSG_NPC_BUY:
msg.readShort(); // length
n_items = (msg.getLength() - 4) / 11;
buyDialog->reset();
buyDialog->setMoney(player_info->gp);
buyDialog->setVisible(true);
for (int k = 0; k < n_items; k++)
{
Sint32 value = msg.readLong();
msg.readLong(); // DCvalue
msg.readByte(); // type
Sint16 itemId = msg.readShort();
buyDialog->addItem(itemId, value);
}
break;
case SMSG_NPC_SELL:
msg.readShort(); // length
n_items = (msg.getLength() - 4) / 10;
if (n_items > 0) {
sellDialog->reset();
sellDialog->setVisible(true);
for (int k = 0; k < n_items; k++)
{
Sint16 index = msg.readShort();
Sint32 value = msg.readLong();
msg.readLong(); // OCvalue
Item *item = inventory->getItem(index);
if (item && !(item->isEquipped())) {
sellDialog->addItem(item, value);
}
}
}
else {
chatWindow->chat_log("Nothing to sell", BY_SERVER);
current_npc = 0;
}
break;
case SMSG_NPC_BUY_RESPONSE:
if (msg.readByte() == 0) {
chatWindow->chat_log("Thanks for buying", BY_SERVER);
} else {
chatWindow->chat_log("Unable to buy", BY_SERVER);
}
break;
case SMSG_NPC_SELL_RESPONSE:
if (msg.readByte() == 0) {
chatWindow->chat_log("Thanks for selling", BY_SERVER);
} else {
chatWindow->chat_log("Unable to sell", BY_SERVER);
}
break;
case SMSG_PLAYER_INVENTORY_ADD:
{
Sint16 index = msg.readShort();
Sint16 amount = msg.readShort();
Sint16 itemId = msg.readShort();
msg.readByte(); // identify flag
msg.readByte(); // attribute
msg.readByte(); // refine
msg.skip(8); // card
Sint16 equipType = msg.readShort();
msg.readByte(); // type
Sint8 fail = msg.readByte();
if (fail > 0) {
chatWindow->chat_log("Unable to pick up item",
BY_SERVER);
} else {
inventory->addItem(index, itemId, amount,
equipType != 0);
}
}
break;
case SMSG_PLAYER_INVENTORY_REMOVE:
{
Sint16 index = msg.readShort();
Sint16 amount = msg.readShort();
inventory->getItem(index)->increaseQuantity(-amount);
}
break;
case SMSG_PLAYER_INVENTORY_USE:
{
Sint16 index = msg.readShort();
msg.readShort(); // item id
msg.readLong(); // id
Sint16 amountLeft = msg.readShort();
msg.readByte(); // type
inventory->getItem(index)->setQuantity(amountLeft);
}
break;
case SMSG_PLAYER_SKILLS:
msg.readShort(); // length
n_items = (msg.getLength() - 4) / 37;
skillDialog->cleanList();
for (int k = 0; k < n_items; k++)
{
Sint16 skillId = msg.readShort();
msg.readShort(); // target type
msg.readShort(); // unknown
Sint16 level = msg.readShort();
Sint16 sp = msg.readShort();
msg.readShort(); // range
std::string skillName = msg.readString(24);
Sint8 up = msg.readByte();
if (level != 0 || up != 0)
{
if (skillDialog->hasSkill(skillId)) {
skillDialog->setSkill(skillId, level, sp);
}
else {
skillDialog->addSkill(skillId, level, sp);
}
}
}
break;
case 0x010c:
// Display MVP player
msg.readLong(); // id
chatWindow->chat_log("MVP player", BY_SERVER);
break;
case SMSG_ITEM_VISIBLE:
case SMSG_ITEM_DROPPED:
{
Uint32 id = msg.readLong();
Sint16 itemId = msg.readShort();
msg.readByte(); // identify flag
Uint16 x = msg.readShort();
Uint16 y = msg.readShort();
msg.skip(4); // amount,subX,subY / subX,subY,amount
add_floor_item(new FloorItem(id, itemId, x, y, tiledMap));
}
break;
case SMSG_ITEM_REMOVE:
remove_floor_item(msg.readLong());
break;
case SMSG_NPC_CHOICE:
msg.readShort(); // length
current_npc = msg.readLong();
npcListDialog->parseItems(msg.readString(msg.getLength() - 8));
npcListDialog->setVisible(true);
break;
case SMSG_BEING_CHANGE_LOOKS:
if (!(being = findNode(msg.readLong())))
{
break;
}
switch (msg.readByte()) {
case 1:
being->setHairStyle(msg.readByte());
break;
case 2:
being->setWeapon(msg.readByte());
break;
case 6:
being->setHairColor(msg.readByte());
break;
default:
msg.readByte(); // unsupported
break;
}
break;
case SMSG_PLAYER_EQUIP:
{
Sint16 index = msg.readShort();
Sint16 equipPoint = msg.readShort();
Sint8 type = msg.readByte();
logger->log("Equipping: %i %i %i",
index, equipPoint, type);
if (type == 0) {
chatWindow->chat_log("Unable to equip.", BY_SERVER);
}
else if (equipPoint)
{
// Unequip any existing equipped item in this position
int mask = 1;
int position = 0;
while (!(equipPoint & mask)) {
mask <<= 1;
position++;
}
logger->log("Position %i", position - 1);
Item *item = equipment->getEquipment(position - 1);
if (item) {
item->setEquipped(false);
}
item = inventory->getItem(index);
item->setEquipped(true);
equipment->setEquipment(position - 1, item);
player_node->setWeaponById(item->getId());
}
}
break;
case 0x01d7:
// Equipment related
{
being = findNode(msg.readLong());
msg.readByte(); // equip point
Sint16 itemId1 = msg.readShort();
msg.readShort(); // item id 2
if (being != NULL)
{
being->setWeaponById(itemId1);
}
}
break;
case SMSG_PLAYER_UNEQUIP:
{
Sint16 index = msg.readShort();
Sint16 equipPoint = msg.readShort();
Sint8 type = msg.readByte();
if (type == 0) {
chatWindow->chat_log("Unable to unequip.", BY_SERVER);
break;
}
if (equipPoint == 0) {
// No point given, no point in searching
break;
}
int mask = 1;
int position = 0;
while (!(equipPoint & mask)) {
mask <<= 1;
position++;
}
Item *item = inventory->getItem(index);
if (item != NULL)
{
item->setEquipped(false);
switch (item->getId()) {
case 529:
case 1199:
equipment->setArrows(NULL);
break;
case 521:
case 522:
case 530:
case 536:
case 1200:
case 1201:
player_node->setWeapon(0);
// TODO: Why this break? Shouldn't a weapon be
// unequipped in inventory too?
break;
default:
equipment->removeEquipment(position - 1);
break;
}
logger->log("Unequipping: %i %i(%i) %i",
index, equipPoint, type, position - 1);
}
}
break;
case SMSG_PLAYER_ARROW_EQUIP:
{
Sint16 id = msg.readShort();
if (id > 1) {
Item *item = inventory->getItem(id);
if (item) {
item->setEquipped(true);
equipment->setArrows(item);
logger->log("Arrows equipped: %i", id);
}
}
}
break;
case SMSG_PLAYER_ARROW_MESSAGE:
{
Sint16 type = msg.readShort();
switch (type) {
case 0:
chatWindow->chat_log("Equip arrows first",
BY_SERVER);
break;
default:
logger->log("0x013b: Unhandled message %i", type);
break;
}
}
break;
case SMSG_PLAYER_STAT_UPDATE_4:
{
Sint16 type = msg.readShort();
Sint8 fail = msg.readByte();
Sint8 value = msg.readByte();
if (fail == 1)
{
switch (type) {
case 0x000d: player_info->STR = value; break;
case 0x000e: player_info->AGI = value; break;
case 0x000f: player_info->VIT = value; break;
case 0x0010: player_info->INT = value; break;
case 0x0011: player_info->DEX = value; break;
case 0x0012: player_info->LUK = value; break;
}
}
}
break;
// Updates stats and status points
case SMSG_PLAYER_STAT_UPDATE_5:
player_info->statsPointsToAttribute = msg.readShort();
player_info->STR = msg.readByte();
player_info->STRUp = msg.readByte();
player_info->AGI = msg.readByte();
player_info->AGIUp = msg.readByte();
player_info->VIT = msg.readByte();
player_info->VITUp = msg.readByte();
player_info->INT = msg.readByte();
player_info->INTUp = msg.readByte();
player_info->DEX = msg.readByte();
player_info->DEXUp = msg.readByte();
player_info->LUK = msg.readByte();
player_info->LUKUp = msg.readByte();
player_info->ATK = msg.readShort(); // ATK
player_info->ATKBonus = msg.readShort(); // ATK bonus
player_info->MATK = msg.readShort(); // MATK max
player_info->MATKBonus = msg.readShort(); // MATK min
player_info->DEF = msg.readShort(); // DEF
player_info->DEFBonus = msg.readShort(); // DEF bonus
player_info->MDEF = msg.readShort(); // MDEF
player_info->MDEFBonus = msg.readShort(); // MDEF bonus
player_info->HIT = msg.readShort(); // HIT
player_info->FLEE = msg.readShort(); // FLEE
player_info->FLEEBonus = msg.readShort(); // FLEE bonus
msg.readShort(); // critical
msg.readShort(); // unknown
break;
case SMSG_PLAYER_STAT_UPDATE_6:
switch (msg.readShort()) {
case 0x0020: player_info->STRUp = msg.readByte(); break;
case 0x0021: player_info->AGIUp = msg.readByte(); break;
case 0x0022: player_info->VITUp = msg.readByte(); break;
case 0x0023: player_info->INTUp = msg.readByte(); break;
case 0x0024: player_info->DEXUp = msg.readByte(); break;
case 0x0025: player_info->LUKUp = msg.readByte(); break;
}
break;
case SMSG_BEING_NAME_RESPONSE:
if ((being = findNode(msg.readLong())))
{
being->setName(msg.readString(24));
}
break;
case 0x0119:
// Change in players look
break;
default:
// Manage non implemented packets
logger->log("Unhandled packet: %x", msg.getId());
break;
}
skip(msg.getLength());
}
}
|