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
|
/*
* The Mana World
* Copyright (C) 2004 The Mana World Development Team
*
* This file is part of The Mana World.
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <fstream>
#include <physfs.h>
#include <sstream>
#include <string>
#include <guichan/exception.hpp>
#include "beingmanager.h"
#include "channelmanager.h"
#ifdef TMWSERV_SUPPORT
#include "commandhandler.h"
#endif
#include "configuration.h"
#include "effectmanager.h"
#include "emoteshortcut.h"
#include "engine.h"
#include "flooritemmanager.h"
#include "game.h"
#include "graphics.h"
#include "itemshortcut.h"
#include "joystick.h"
#include "keyboardconfig.h"
#include "localplayer.h"
#include "log.h"
#include "npc.h"
#include "particle.h"
#include "player_relations.h"
#include "gui/buy.h"
#include "gui/buysell.h"
#include "gui/chat.h"
#include "gui/confirm_dialog.h"
#include "gui/debugwindow.h"
#include "gui/emoteshortcutcontainer.h"
#include "gui/emotewindow.h"
#include "gui/equipmentwindow.h"
#include "gui/gui.h"
#include "gui/help.h"
#include "gui/inventorywindow.h"
#include "gui/shortcutwindow.h"
#include "gui/itemshortcutcontainer.h"
#include "gui/menuwindow.h"
#include "gui/minimap.h"
#include "gui/ministatus.h"
#include "gui/npcintegerdialog.h"
#include "gui/npclistdialog.h"
#include "gui/npcstringdialog.h"
#include "gui/npc_text.h"
#include "gui/ok_dialog.h"
#include "gui/sdlinput.h"
#include "gui/sell.h"
#include "gui/setup.h"
#include "gui/skill.h"
#include "gui/status.h"
#include "gui/trade.h"
#include "gui/viewport.h"
#ifdef TMWSERV_SUPPORT
#include "gui/buddywindow.h"
#include "gui/guildwindow.h"
#include "gui/magic.h"
#include "gui/npcpostdialog.h"
#include "gui/partywindow.h"
#include "gui/quitdialog.h"
#endif
#include "net/chathandler.h"
#include "net/itemhandler.h"
#include "net/npchandler.h"
#include "net/playerhandler.h"
#include "net/tradehandler.h"
#ifdef TMWSERV_SUPPORT
#include "net/network.h"
#include "net/beinghandler.h"
#include "net/buysellhandler.h"
#include "net/effecthandler.h"
#include "net/guildhandler.h"
#include "net/inventoryhandler.h"
#include "net/partyhandler.h"
#else
#include "net/ea/network.h"
#include "net/ea/beinghandler.h"
#include "net/ea/buysellhandler.h"
#include "net/ea/equipmenthandler.h"
#include "net/ea/inventoryhandler.h"
#include "net/ea/protocol.h"
#include "net/ea/skillhandler.h"
#include "net/messageout.h"
#endif
#include "resources/imagewriter.h"
#include "utils/gettext.h"
class Map;
std::string map_path;
bool done = false;
volatile int tick_time;
volatile int fps = 0, frame = 0;
Engine *engine = NULL;
Joystick *joystick = NULL;
extern Window *weightNotice;
extern Window *deathNotice;
#ifdef TMWSERV_SUPPORT
QuitDialog *quitDialog = NULL;
#else
ConfirmDialog *exitConfirm = NULL;
#endif
OkDialog *disconnectedDialog = NULL;
ChatWindow *chatWindow;
MenuWindow *menuWindow;
StatusWindow *statusWindow;
MiniStatusWindow *miniStatusWindow;
BuyDialog *buyDialog;
SellDialog *sellDialog;
BuySellDialog *buySellDialog;
InventoryWindow *inventoryWindow;
EmoteWindow *emoteWindow;
NpcIntegerDialog *npcIntegerDialog;
NpcListDialog *npcListDialog;
NpcTextDialog *npcTextDialog;
NpcStringDialog *npcStringDialog;
SkillDialog *skillDialog;
#ifdef TMWSERV_SUPPORT
BuddyWindow *buddyWindow;
GuildWindow *guildWindow;
MagicDialog *magicDialog;
NpcPostDialog *npcPostDialog;
PartyWindow *partyWindow;
#endif
Setup* setupWindow;
Minimap *minimap;
EquipmentWindow *equipmentWindow;
TradeWindow *tradeWindow;
HelpWindow *helpWindow;
DebugWindow *debugWindow;
ShortcutWindow *itemShortcutWindow;
ShortcutWindow *emoteShortcutWindow;
BeingManager *beingManager = NULL;
FloorItemManager *floorItemManager = NULL;
ChannelManager *channelManager = NULL;
#ifdef TMWSERV_SUPPORT
CommandHandler *commandHandler = NULL;
#endif
Particle* particleEngine = NULL;
EffectManager *effectManager = NULL;
const int MAX_TIME = 10000;
/**
* Listener used for exiting handling.
*/
namespace {
struct ExitListener : public gcn::ActionListener
{
void action(const gcn::ActionEvent &event)
{
if (event.getId() == "yes" || event.getId() == "ok") {
done = true;
}
#ifdef EATHENA_SUPPORT
exitConfirm = NULL;
#endif
disconnectedDialog = NULL;
}
} exitListener;
}
/**
* Advances game logic counter.
*/
Uint32 nextTick(Uint32 interval, void *param)
{
tick_time++;
if (tick_time == MAX_TIME) tick_time = 0;
return interval;
}
/**
* Updates fps.
*/
Uint32 nextSecond(Uint32 interval, void *param)
{
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
*/
#ifdef TMWSERV_SUPPORT
void createGuiWindows()
#else
void createGuiWindows(Network *network)
#endif
{
// Create dialogs
#ifdef TMWSERV_SUPPORT
chatWindow = new ChatWindow;
buyDialog = new BuyDialog;
sellDialog = new SellDialog;
tradeWindow = new TradeWindow;
npcPostDialog = new NpcPostDialog();
magicDialog = new MagicDialog();
equipmentWindow = new EquipmentWindow(player_node->mEquipment.get());
buddyWindow = new BuddyWindow();
guildWindow = new GuildWindow();
partyWindow = new PartyWindow();
#else
chatWindow = new ChatWindow(network);
buyDialog = new BuyDialog(network);
sellDialog = new SellDialog(network);
tradeWindow = new TradeWindow(network);
equipmentWindow = new EquipmentWindow;
#endif
menuWindow = new MenuWindow;
statusWindow = new StatusWindow(player_node);
miniStatusWindow = new MiniStatusWindow;
buySellDialog = new BuySellDialog;
inventoryWindow = new InventoryWindow;
emoteWindow = new EmoteWindow;
npcTextDialog = new NpcTextDialog;
npcIntegerDialog = new NpcIntegerDialog;
npcListDialog = new NpcListDialog;
npcStringDialog = new NpcStringDialog;
skillDialog = new SkillDialog;
setupWindow = new Setup;
minimap = new Minimap;
helpWindow = new HelpWindow;
debugWindow = new DebugWindow;
itemShortcutWindow = new ShortcutWindow("ItemShortcut",new ItemShortcutContainer);
emoteShortcutWindow = new ShortcutWindow("emoteShortcut",new EmoteShortcutContainer);
// Set initial window visibility
chatWindow->setVisible((bool) config.getValue(
chatWindow->getWindowName() + "Visible", true));
miniStatusWindow->setVisible((bool) config.getValue(
miniStatusWindow->getWindowName() + "Visible", true));
buyDialog->setVisible(false);
sellDialog->setVisible(false);
minimap->setVisible((bool) config.getValue(
minimap->getWindowName() + "Visible", true));
tradeWindow->setVisible(false);
menuWindow->setVisible((bool) config.getValue(
menuWindow->getWindowName() + "Visible", true));
itemShortcutWindow->setVisible((bool) config.getValue(
itemShortcutWindow->getWindowName() + "Visible", true));
emoteShortcutWindow->setVisible((bool) config.getValue(
emoteShortcutWindow->getWindowName() + "Visible", true));
minimap->setVisible((bool) config.getValue(
minimap->getWindowName() + "Visible", true));
if (config.getValue("logToChat", 0))
{
logger->setChatWindow(chatWindow);
}
}
/**
* Destroy all the globally accessible gui windows
*/
void destroyGuiWindows()
{
logger->setChatWindow(NULL);
delete chatWindow;
delete statusWindow;
delete miniStatusWindow;
delete menuWindow;
delete buyDialog;
delete sellDialog;
delete buySellDialog;
delete inventoryWindow;
delete emoteWindow;
delete npcIntegerDialog;
delete npcListDialog;
delete npcTextDialog;
delete npcStringDialog;
#ifdef TMWSERV_SUPPORT
delete npcPostDialog;
delete magicDialog;
delete buddyWindow;
delete guildWindow;
delete partyWindow;
#endif
delete skillDialog;
delete setupWindow;
delete minimap;
delete equipmentWindow;
delete tradeWindow;
delete helpWindow;
delete debugWindow;
delete itemShortcutWindow;
delete emoteShortcutWindow;
}
#ifdef TMWSERV_SUPPORT
Game::Game():
mBeingHandler(new BeingHandler()),
mGuildHandler(new GuildHandler()),
mPartyHandler(new PartyHandler()),
mEffectHandler(new EffectHandler()),
#else
Game::Game(Network *network):
mNetwork(network),
mBeingHandler(new BeingHandler(config.getValue("EnableSync", 0) == 1)),
mEquipmentHandler(new EquipmentHandler),
mSkillHandler(new SkillHandler),
#endif
mBuySellHandler(new BuySellHandler),
mChatHandler(new ChatHandler),
mInventoryHandler(new InventoryHandler),
mItemHandler(new ItemHandler),
mNpcHandler(new NPCHandler),
mPlayerHandler(new PlayerHandler),
mTradeHandler(new TradeHandler),
mLogicCounterId(0), mSecondsCounterId(0)
{
done = false;
#ifdef TMWSERV_SUPPORT
createGuiWindows();
engine = new Engine;
beingManager = new BeingManager;
#else
createGuiWindows(network);
engine = new Engine(network);
beingManager = new BeingManager(network);
#endif
floorItemManager = new FloorItemManager;
channelManager = new ChannelManager();
#ifdef TMWSERV_SUPPORT
commandHandler = new CommandHandler();
#endif
effectManager = new EffectManager;
particleEngine = new Particle(NULL);
particleEngine->setupEngine();
// Initialize logic and seconds counters
tick_time = 0;
mLogicCounterId = SDL_AddTimer(10, nextTick, NULL);
mSecondsCounterId = SDL_AddTimer(1000, nextSecond, NULL);
// Initialize frame limiting
config.addListener("fpslimit", this);
optionChanged("fpslimit");
// Initialize beings
beingManager->setPlayer(player_node);
#ifdef EATHENA_SUPPORT
player_node->setNetwork(network);
#endif
Joystick::init();
// TODO: The user should be able to choose which one to use
// Open the first device
if (Joystick::getNumberOfJoysticks() > 0)
joystick = new Joystick(0);
#ifdef TMWSERV_SUPPORT
Net::registerHandler(mBeingHandler.get());
Net::registerHandler(mBuySellHandler.get());
Net::registerHandler(mChatHandler.get());
Net::registerHandler(mGuildHandler.get());
Net::registerHandler(mInventoryHandler.get());
Net::registerHandler(mItemHandler.get());
Net::registerHandler(mNpcHandler.get());
Net::registerHandler(mPartyHandler.get());
Net::registerHandler(mPlayerHandler.get());
Net::registerHandler(mTradeHandler.get());
Net::registerHandler(mEffectHandler.get());
#else
network->registerHandler(mBeingHandler.get());
network->registerHandler(mBuySellHandler.get());
network->registerHandler(mChatHandler.get());
network->registerHandler(mEquipmentHandler.get());
network->registerHandler(mInventoryHandler.get());
network->registerHandler(mItemHandler.get());
network->registerHandler(mNpcHandler.get());
network->registerHandler(mPlayerHandler.get());
network->registerHandler(mSkillHandler.get());
network->registerHandler(mTradeHandler.get());
/*
* To prevent the server from sending data before the client
* has initialized, I've modified it to wait for a "ping"
* from the client to complete its initialization
*
* Note: This only affects the latest eAthena version. This
* packet is handled by the older version, but its response
* is ignored by the client
*/
MessageOut msg(mNetwork);
msg.writeInt16(CMSG_CLIENT_PING);
msg.writeInt32(tick_time);
engine->changeMap(map_path);
#endif
}
Game::~Game()
{
#ifdef TMWSERV_SUPPORT
Net::clearHandlers();
#endif
delete player_node;
destroyGuiWindows();
delete beingManager;
delete floorItemManager;
delete channelManager;
#ifdef TMWSERV_SUPPORT
delete commandHandler;
#endif
delete joystick;
delete particleEngine;
delete engine;
viewport->setMap(NULL);
player_node = NULL;
beingManager = NULL;
floorItemManager = NULL;
joystick = NULL;
SDL_RemoveTimer(mLogicCounterId);
SDL_RemoveTimer(mSecondsCounterId);
}
static bool saveScreenshot()
{
static unsigned int screenshotCount = 0;
SDL_Surface *screenshot = graphics->getScreenshot();
// Search for an unused screenshot name
std::stringstream filenameSuffix;
std::stringstream filename;
std::fstream testExists;
bool found = false;
do {
screenshotCount++;
filename.str("");
filenameSuffix.str("");
filename << PHYSFS_getUserDir();
#if (defined __USE_UNIX98 || defined __FreeBSD__)
filenameSuffix << ".tmw/";
#elif defined __APPLE__
filenameSuffix << "Desktop/";
#endif
filenameSuffix << "TMW_Screenshot_" << screenshotCount << ".png";
filename << filenameSuffix.str();
testExists.open(filename.str().c_str(), std::ios::in);
found = !testExists.is_open();
testExists.close();
} while (!found);
const bool success = ImageWriter::writePNG(screenshot, filename.str());
if (success)
{
std::stringstream chatlogentry;
chatlogentry << _("Screenshot saved to ~/") << filenameSuffix.str();
chatWindow->chatLog(chatlogentry.str(), BY_SERVER);
}
else
{
chatWindow->chatLog(_("Saving screenshot failed!"), BY_SERVER);
logger->log("Error: could not save screenshot.");
}
SDL_FreeSurface(screenshot);
return success;
}
void Game::optionChanged(const std::string &name)
{
int fpsLimit = (int) config.getValue("fpslimit", 0);
// Calculate new minimum frame time
mMinFrameTime = fpsLimit ? 1000 / fpsLimit : 0;
// Reset draw time to current time
mDrawTime = tick_time * 10;
}
void Game::logic()
{
// mDrawTime has a higher granularity than gameTime in order to be able to
// work with minimum frame durations in milliseconds.
int gameTime = tick_time;
mDrawTime = tick_time * 10;
while (!done)
{
// Handle all necessary game logic
while (get_elapsed_time(gameTime) > 0)
{
handleInput();
engine->logic();
gameTime++;
}
// This is done because at some point tick_time will wrap.
gameTime = tick_time;
// Update the screen when application is active, delay otherwise.
if (SDL_GetAppState() & SDL_APPACTIVE)
{
// Draw a frame if either frames are not limited or enough time has
// passed since the last frame.
if (!mMinFrameTime ||
get_elapsed_time(mDrawTime / 10) > mMinFrameTime)
{
frame++;
gui->draw();
graphics->updateScreen();
mDrawTime += mMinFrameTime;
// Make sure to wrap mDrawTime, since tick_time will wrap.
if (mDrawTime > MAX_TIME * 10)
mDrawTime -= MAX_TIME * 10;
}
else
{
SDL_Delay(10);
}
}
else
{
SDL_Delay(10);
mDrawTime = tick_time * 10;
}
// Handle network stuff
#ifdef TMWSERV_SUPPORT
Net::flush();
// TODO: Fix notification when the connection is lost
#else
mNetwork->flush();
mNetwork->dispatchMessages();
if (!mNetwork->isConnected())
{
if (!disconnectedDialog)
{
disconnectedDialog = new OkDialog(_("Network Error"),
_("The connection to the server was lost, "
"the program will now quit"));
disconnectedDialog->addActionListener(&exitListener);
disconnectedDialog->requestMoveToTop();
}
}
#endif
}
}
void Game::handleInput()
{
if (joystick)
joystick->update();
// 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;
if (setupWindow->isVisible() &&
keyboard.getNewKeyIndex() > keyboard.KEY_NO_VALUE)
{
keyboard.setNewKey((int) event.key.keysym.sym);
keyboard.callbackNewKey();
keyboard.setNewKeyIndex(keyboard.KEY_NO_VALUE);
return;
}
#ifdef TMWSERV_SUPPORT
// send straight to gui for certain windows
if (npcPostDialog->isVisible())
{
try
{
guiInput->pushInput(event);
}
catch (gcn::Exception e)
{
const char* err = e.getMessage().c_str();
logger->log("Warning: guichan input exception: %s", err);
}
return;
}
#endif
// Mode switch to emotes
if (keyboard.isKeyActive(keyboard.KEY_EMOTE))
{
// Emotions
int emotion = keyboard.getKeyEmoteOffset(event.key.keysym.sym);
if (emotion)
{
emoteShortcut->useEmote(emotion);
used = true;
return;
}
}
if (keyboard.isKeyActive(keyboard.KEY_TOGGLE_CHAT) ||
keyboard.isKeyActive(keyboard.KEY_OK))
{
// Input chat window
if (!(chatWindow->isInputFocused() ||
deathNotice ||
weightNotice))
{
#ifdef TMWSERV_SUPPORT
// Don not focus chat input when quit dialog is active
if (quitDialog != NULL && quitDialog->isVisible())
continue;
#else
// Quit by pressing Enter if the exit confirm is there
if (exitConfirm &&
keyboard.isKeyActive(keyboard.KEY_TOGGLE_CHAT))
done = true;
#endif
// Close the Browser if opened
else if (helpWindow->isVisible() &&
keyboard.isKeyActive(keyboard.KEY_OK))
helpWindow->setVisible(false);
// Close the config window, cancelling changes if opened
else if (setupWindow->isVisible() &&
keyboard.isKeyActive(keyboard.KEY_OK))
setupWindow->action(gcn::ActionEvent(NULL, "cancel"));
// Submits the text and proceeds to the next dialog
else if (npcStringDialog->isVisible() &&
keyboard.isKeyActive(keyboard.KEY_OK))
npcStringDialog->action(gcn::ActionEvent(NULL, "ok"));
// Proceed to the next dialog option, or close the window
else if (npcTextDialog->isVisible() &&
keyboard.isKeyActive(keyboard.KEY_OK))
npcTextDialog->action(gcn::ActionEvent(NULL, "ok"));
// Choose the currently highlighted dialogue option
else if (npcListDialog->isVisible() &&
keyboard.isKeyActive(keyboard.KEY_OK))
npcListDialog->action(gcn::ActionEvent(NULL, "ok"));
// Submits the text and proceeds to the next dialog
else if (npcIntegerDialog->isVisible() &&
keyboard.isKeyActive(keyboard.KEY_OK))
npcIntegerDialog->action(gcn::ActionEvent(NULL, "ok"));
/*
else if (guildWindow->isVisible())
{
// TODO: Check if a dialog is open and close it if so
}
*/
else if (!(keyboard.getKeyValue(
KeyboardConfig::KEY_TOGGLE_CHAT) ==
keyboard.getKeyValue(
KeyboardConfig::KEY_OK) &&
(helpWindow->isVisible() ||
setupWindow->isVisible() ||
npcStringDialog->isVisible() ||
npcTextDialog->isVisible() ||
npcListDialog->isVisible() ||
npcIntegerDialog->isVisible())))
{
chatWindow->requestChatFocus();
used = true;
}
}
}
const int tKey = keyboard.getKeyIndex(event.key.keysym.sym);
switch (tKey)
{
case KeyboardConfig::KEY_SCROLL_CHAT_UP:
if (chatWindow->isVisible())
{
chatWindow->scroll(-DEFAULT_CHAT_WINDOW_SCROLL);
used = true;
}
break;
case KeyboardConfig::KEY_SCROLL_CHAT_DOWN:
if (chatWindow->isVisible())
{
chatWindow->scroll(DEFAULT_CHAT_WINDOW_SCROLL);
used = true;
return;
}
break;
case KeyboardConfig::KEY_WINDOW_HELP:
// In-game Help
if (helpWindow->isVisible())
helpWindow->setVisible(false);
else
{
helpWindow->loadHelp("index");
helpWindow->requestMoveToTop();
}
used = true;
break;
// Quitting confirmation dialog
case KeyboardConfig::KEY_QUIT:
#ifdef TMWSERV_SUPPORT
if (!quitDialog)
{
quitDialog = new QuitDialog(&done, &quitDialog);
quitDialog->requestMoveToTop();
}
else
{
quitDialog->action(gcn::ActionEvent(NULL, "cancel"));
}
#else
if (!exitConfirm)
{
exitConfirm = new ConfirmDialog(_("Quit"),
_("Are you sure you "
"want to quit?"));
exitConfirm->addActionListener(&exitListener);
exitConfirm->requestMoveToTop();
}
else
{
exitConfirm->action(gcn::ActionEvent(NULL, _("no")));
}
#endif
break;
default:
break;
}
if (keyboard.isEnabled() && !chatWindow->isInputFocused()
&& !npcStringDialog->isInputFocused())
{
const int tKey = keyboard.getKeyIndex(event.key.keysym.sym);
// Do not activate shortcuts if tradewindow is visible
if (!tradeWindow->isVisible())
{
// Checks if any item shortcut is pressed.
for (int i = KeyboardConfig::KEY_SHORTCUT_1;
i <= KeyboardConfig::KEY_SHORTCUT_12;
i++)
{
if (tKey == i && !used)
{
itemShortcut->useItem(
i - KeyboardConfig::KEY_SHORTCUT_1);
break;
}
}
}
switch (tKey)
{
case KeyboardConfig::KEY_PICKUP:
{
#ifdef TMWSERV_SUPPORT
const Vector &pos = player_node->getPosition();
Uint16 x = (int) pos.x / 32;
Uint16 y = (int) pos.y / 32;
#else
Uint16 x = player_node->mX;
Uint16 y = player_node->mY;
#endif
FloorItem *item =
floorItemManager->findByCoordinates(x, y);
// If none below the player, try the tile in front
// of the player
if (!item)
{
// Temporary until tile-based picking is
// removed.
switch (player_node->getSpriteDirection())
{
case DIRECTION_UP : --y; break;
case DIRECTION_DOWN : ++y; break;
case DIRECTION_LEFT : --x; break;
case DIRECTION_RIGHT: ++x; break;
default: break;
}
item = floorItemManager->findByCoordinates(
x, y);
}
if (item)
player_node->pickUp(item);
used = true;
}
break;
case KeyboardConfig::KEY_SIT:
// Player sit action
player_node->toggleSit();
used = true;
break;
case KeyboardConfig::KEY_HIDE_WINDOWS:
// Hide certain windows
if (!chatWindow->isInputFocused())
{
statusWindow->setVisible(false);
inventoryWindow->setVisible(false);
emoteWindow->setVisible(false);
skillDialog->setVisible(false);
setupWindow->setVisible(false);
equipmentWindow->setVisible(false);
helpWindow->setVisible(false);
debugWindow->setVisible(false);
#ifdef TMWSERV_SUPPORT
guildWindow->setVisible(false);
buddyWindow->setVisible(false);
#endif
}
break;
case KeyboardConfig::KEY_WINDOW_STATUS:
requestedWindow = statusWindow;
break;
case KeyboardConfig::KEY_WINDOW_INVENTORY:
requestedWindow = inventoryWindow;
break;
case KeyboardConfig::KEY_WINDOW_EQUIPMENT:
requestedWindow = equipmentWindow;
break;
case KeyboardConfig::KEY_WINDOW_SKILL:
requestedWindow = skillDialog;
break;
case KeyboardConfig::KEY_WINDOW_MINIMAP:
minimap->toggle();
requestedWindow = minimap;
break;
case KeyboardConfig::KEY_WINDOW_CHAT:
requestedWindow = chatWindow;
break;
case KeyboardConfig::KEY_WINDOW_SHORTCUT:
requestedWindow = itemShortcutWindow;
break;
case KeyboardConfig::KEY_WINDOW_SETUP:
requestedWindow = setupWindow;
break;
case KeyboardConfig::KEY_WINDOW_DEBUG:
requestedWindow = debugWindow;
break;
case KeyboardConfig::KEY_WINDOW_EMOTE:
requestedWindow = emoteWindow;
break;
case KeyboardConfig::KEY_WINDOW_EMOTE_SHORTCUT:
requestedWindow = emoteShortcutWindow;
break;
case KeyboardConfig::KEY_SCREENSHOT:
// Screenshot (picture, hence the p)
saveScreenshot();
used = true;
break;
case KeyboardConfig::KEY_PATHFIND:
// Find path to mouse (debug purpose)
viewport->toggleDebugPath();
used = true;
break;
case KeyboardConfig::KEY_TRADE:
// Toggle accepting of incoming trade requests
unsigned int deflt = player_relations.getDefault();
if (deflt & PlayerRelation::TRADE)
{
chatWindow->chatLog(
_("Ignoring incoming trade requests"),
BY_SERVER);
deflt &= ~PlayerRelation::TRADE;
}
else
{
chatWindow->chatLog(
_("Accepting incoming trade requests"),
BY_SERVER);
deflt |= PlayerRelation::TRADE;
}
player_relations.setDefault(deflt);
used = true;
break;
}
}
if (requestedWindow)
{
requestedWindow->setVisible(!requestedWindow->isVisible());
if (requestedWindow->isVisible())
requestedWindow->requestMoveToTop();
used = true;
}
}
// Quit event
else if (event.type == SDL_QUIT)
{
done = true;
}
// Push input to GUI when not used
if (!used)
{
try
{
guiInput->pushInput(event);
}
catch (gcn::Exception e)
{
const char* err = e.getMessage().c_str();
logger->log("Warning: guichan input exception: %s", err);
}
}
} // End while
// If the user is configuring the keys then don't respond.
if (!keyboard.isEnabled())
return;
// Moving player around
if (player_node->mAction != Being::DEAD &&
#ifdef EATHENA_SUPPORT
current_npc == 0 &&
#endif
!chatWindow->isInputFocused())
{
// Get the state of the keyboard keys
keyboard.refreshActiveKeys();
// Ignore input if either "ignore" key is pressed
// Stops the character moving about if the user's window manager
// uses "ignore+arrow key" to switch virtual desktops.
if (keyboard.isKeyActive(keyboard.KEY_IGNORE_INPUT_1) ||
keyboard.isKeyActive(keyboard.KEY_IGNORE_INPUT_2))
{
return;
}
#ifdef TMWSERV_SUPPORT
const Vector &pos = player_node->getPosition();
const Uint16 x = (int) pos.x / 32;
const Uint16 y = (int) pos.y / 32;
#else
const Uint16 x = player_node->mX;
const Uint16 y = player_node->mY;
#endif
unsigned char direction = 0;
// Translate pressed keys to movement and direction
if (keyboard.isKeyActive(keyboard.KEY_MOVE_UP) ||
(joystick && joystick->isUp()))
{
direction |= Being::UP;
}
else if (keyboard.isKeyActive(keyboard.KEY_MOVE_DOWN) ||
(joystick && joystick->isDown()))
{
direction |= Being::DOWN;
}
if (keyboard.isKeyActive(keyboard.KEY_MOVE_LEFT) ||
(joystick && joystick->isLeft()))
{
direction |= Being::LEFT;
}
else if (keyboard.isKeyActive(keyboard.KEY_MOVE_RIGHT) ||
(joystick && joystick->isRight()))
{
direction |= Being::RIGHT;
}
#ifdef TMWSERV_SUPPORT
// First if player is pressing key for the direction he is already
// going
if (direction == player_node->getWalkingDir())
{
player_node->setWalkingDir(direction);
}
// Else if he is pressing a key, and its different from what he has
// been pressing, stop (do not send this stop to the server) and
// start in the new direction
else if (direction && direction != player_node->getWalkingDir())
{
player_node->stopWalking(false);
player_node->setWalkingDir(direction);
}
// Else, he is not pressing a key, stop (sending to server)
else
{
player_node->stopWalking(true);
}
#else
player_node->setWalkingDir(direction);
// Attacking monsters
if (keyboard.isKeyActive(keyboard.KEY_ATTACK) ||
(joystick && joystick->buttonPressed(0)))
{
Being *target = beingManager->findNearestLivingBeing(x, y, 20,
Being::MONSTER);
bool newTarget = !keyboard.isKeyActive(keyboard.KEY_TARGET);
// A set target has highest priority
if (newTarget || !player_node->getTarget())
{
Uint16 targetX = x, targetY = y;
switch (player_node->getSpriteDirection())
{
case DIRECTION_UP : --targetY; break;
case DIRECTION_DOWN : ++targetY; break;
case DIRECTION_LEFT : --targetX; break;
case DIRECTION_RIGHT: ++targetX; break;
default: break;
}
// Attack priorioty is: Monster, Player, auto target
target = beingManager->findBeing(targetX, targetY, Being::MONSTER);
if (!target)
target = beingManager->findBeing(targetX, targetY, Being::PLAYER);
}
player_node->attack(target, newTarget);
}
#endif
// Target the nearest player if 'q' is pressed
if ( keyboard.isKeyActive(keyboard.KEY_TARGET_PLAYER) &&
!keyboard.isKeyActive(keyboard.KEY_TARGET) )
{
Being *target = beingManager->findNearestLivingBeing(player_node, 20, Being::PLAYER);
player_node->setTarget(target);
}
// Target the nearest monster if 'a' pressed
if ((keyboard.isKeyActive(keyboard.KEY_TARGET_CLOSEST) ||
(joystick && joystick->buttonPressed(3))) &&
!keyboard.isKeyActive(keyboard.KEY_TARGET))
{
Being *target = beingManager->findNearestLivingBeing(
x, y, 20, Being::MONSTER);
player_node->setTarget(target);
}
// Target the nearest npc if 'n' pressed
if ( keyboard.isKeyActive(keyboard.KEY_TARGET_NPC) &&
!keyboard.isKeyActive(keyboard.KEY_TARGET) )
{
Being *target = beingManager->findNearestLivingBeing(
x, y, 20, Being::NPC);
player_node->setTarget(target);
}
// Talk to the nearest NPC if 't' pressed
if ( keyboard.isKeyActive(keyboard.KEY_TALK) )
{
if (!npcTextDialog->isVisible() && !npcListDialog->isVisible())
{
Being *target = player_node->getTarget();
if (!target)
{
target = beingManager->findNearestLivingBeing(
x, y, 20, Being::NPC);
}
if (target)
{
if (target->getType() == Being::NPC)
dynamic_cast<NPC*>(target)->talk();
}
}
}
#ifdef EATHENA_SUPPORT
// Stop attacking if shift is pressed
if (keyboard.isKeyActive(keyboard.KEY_TARGET))
{
player_node->stopAttack();
}
#endif
if (joystick)
{
if (joystick->buttonPressed(1))
{
FloorItem *item = floorItemManager->findByCoordinates(x, y);
if (item)
player_node->pickUp(item);
}
else if (joystick->buttonPressed(2))
{
player_node->toggleSit();
}
}
}
}
|