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
|
/*
* The ManaPlus Client
* Copyright (C) 2004-2009 The Mana World Development Team
* Copyright (C) 2009-2010 The Mana Developers
* Copyright (C) 2011-2014 The ManaPlus Developers
*
* This file is part of The ManaPlus Client.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "gui/windows/skilldialog.h"
#include "configuration.h"
#include "effectmanager.h"
#include "itemshortcut.h"
#include "being/attributes.h"
#include "being/localplayer.h"
#include "gui/models/skillmodel.h"
#include "gui/windows/setupwindow.h"
#include "gui/windows/shortcutwindow.h"
#include "gui/widgets/button.h"
#include "gui/widgets/skilllistbox.h"
#include "gui/widgets/label.h"
#include "gui/widgets/scrollarea.h"
#include "gui/widgets/tabbedarea.h"
#include "gui/widgets/tabs/skilltab.h"
#include "net/net.h"
#include "net/playerhandler.h"
#include "net/skillhandler.h"
#include "utils/dtor.h"
#include "utils/gettext.h"
#include "utils/timer.h"
#include "resources/beingcommon.h"
#include "resources/skillconsts.h"
#include "debug.h"
SkillDialog *skillDialog = nullptr;
static SkillOwner::Type parseOwner(const std::string &str)
{
if (str == "player")
return SkillOwner::Player;
else if (str == "mercenary")
return SkillOwner::Mercenary;
else if (str == "homunculus")
return SkillOwner::Homunculus;
return SkillOwner::Player;
}
SkillDialog::SkillDialog() :
// TRANSLATORS: skills dialog name
Window(_("Skills"), false, nullptr, "skills.xml"),
ActionListener(),
mSkills(),
mDurations(),
mTabs(new TabbedArea(this)),
mDeleteTabs(),
mPointsLabel(new Label(this, "0")),
// TRANSLATORS: skills dialog button
mUseButton(new Button(this, _("Use"), "use", this)),
// TRANSLATORS: skills dialog button
mIncreaseButton(new Button(this, _("Up"), "inc", this)),
mDefaultModel(nullptr)
{
mTabs->postInit();
setWindowName("Skills");
setCloseButton(true);
setResizable(true);
setSaveVisible(true);
setStickyButtonLock(true);
setDefaultSize(windowContainer->getWidth() - 280, 30, 275, 425);
if (setupWindow)
setupWindow->registerWindowForReset(this);
mUseButton->setEnabled(false);
mIncreaseButton->setEnabled(false);
place(0, 0, mTabs, 5, 5);
place(0, 5, mPointsLabel, 4);
place(3, 5, mUseButton);
place(4, 5, mIncreaseButton);
}
void SkillDialog::postInit()
{
setLocationRelativeTo(getParent());
loadWindowState();
enableVisibleSound(true);
}
SkillDialog::~SkillDialog()
{
clearSkills();
}
void SkillDialog::action(const ActionEvent &event)
{
const std::string &eventId = event.getId();
if (eventId == "inc")
{
const SkillTab *const tab = static_cast<const SkillTab *const>(
mTabs->getSelectedTab());
if (tab)
{
if (const SkillInfo *const info = tab->getSelectedInfo())
playerHandler->increaseSkill(static_cast<uint16_t>(info->id));
}
}
else if (eventId == "sel")
{
const SkillTab *const tab = static_cast<const SkillTab *const>(
mTabs->getSelectedTab());
if (tab)
{
if (const SkillInfo *const info = tab->getSelectedInfo())
{
mUseButton->setEnabled(info->isUsable());
mUseButton->setCaption(info->useButton);
mIncreaseButton->setEnabled(info->id < SKILL_VAR_MIN_ID);
const int num = itemShortcutWindow->getTabIndex();
if (num >= 0 && num < static_cast<int>(SHORTCUT_TABS)
&& itemShortcut[num])
{
itemShortcut[num]->setItemSelected(
info->id + SKILL_MIN_ID);
}
}
else
{
mUseButton->setEnabled(false);
mIncreaseButton->setEnabled(false);
mUseButton->setCaption(_("Use"));
}
}
}
else if (eventId == "use")
{
const SkillTab *const tab = static_cast<const SkillTab *const>(
mTabs->getSelectedTab());
if (tab)
{
const SkillInfo *const info = tab->getSelectedInfo();
useSkill(info);
}
}
else if (eventId == "close")
{
setVisible(false);
}
}
std::string SkillDialog::update(const int id)
{
const SkillMap::const_iterator i = mSkills.find(id);
if (i != mSkills.end())
{
SkillInfo *const info = i->second;
if (info)
{
info->update();
return info->data->name;
}
}
return std::string();
}
void SkillDialog::update()
{
// TRANSLATORS: skills dialog label
mPointsLabel->setCaption(strprintf(_("Skill points available: %d"),
PlayerInfo::getAttribute(Attributes::SKILL_POINTS)));
mPointsLabel->adjustSize();
FOR_EACH (SkillMap::const_iterator, it, mSkills)
{
SkillInfo *const info = (*it).second;
if (info && info->modifiable)
info->update();
}
}
void SkillDialog::updateModels()
{
std::set<SkillModel*> models;
FOR_EACH (SkillMap::const_iterator, it, mSkills)
{
SkillInfo *const info = (*it).second;
if (info)
{
SkillModel *const model = info->model;
if (model)
models.insert(model);
}
}
FOR_EACH (std::set<SkillModel*>::iterator, it, models)
{
SkillModel *const model = *it;
if (model)
model->updateVisibilities();
}
}
void SkillDialog::clearSkills()
{
mTabs->removeAll();
mDeleteTabs.clear();
mDefaultModel = nullptr;
delete_all(mSkills);
mSkills.clear();
mDurations.clear();
}
void SkillDialog::hideSkills(const SkillOwner::Type owner)
{
FOR_EACH (SkillMap::iterator, it, mSkills)
{
SkillInfo *const info = (*it).second;
if (info && info->owner == owner)
{
PlayerInfo::setSkillLevel(info->id, 0);
info->visible = false;
}
}
}
void SkillDialog::loadSkills()
{
clearSkills();
loadXmlFile(paths.getStringValue("skillsFile"));
if (mSkills.empty())
loadXmlFile(paths.getStringValue("skillsFile2"));
loadXmlFile(paths.getStringValue("skillsPatchFile"));
loadXmlDir("skillsPatchDir", loadXmlFile);
update();
}
void SkillDialog::loadXmlFile(const std::string &fileName)
{
XML::Document doc(fileName);
XmlNodePtrConst root = doc.rootNode();
int setCount = 0;
if (!root || !xmlNameEqual(root, "skills"))
{
logger->log("Error loading skills: " + fileName);
return;
}
for_each_xml_child_node(set, root)
{
if (xmlNameEqual(set, "include"))
{
const std::string name = XML::getProperty(set, "name", "");
if (!name.empty())
loadXmlFile(name);
continue;
}
else if (xmlNameEqual(set, "set"))
{
setCount++;
const std::string setName = XML::getProperty(set, "name",
// TRANSLATORS: skills dialog default skill tab
strprintf(_("Skill Set %d"), setCount));
SkillModel *const model = new SkillModel();
if (!mDefaultModel)
mDefaultModel = model;
for_each_xml_child_node(node, set)
{
if (xmlNameEqual(node, "skill"))
{
int id = XML::getIntProperty(node, "id", -1, -1, 1000000);
if (id == -1)
{
id = XML::getIntProperty(node, "var", -1, -1, 100000);
if (id == -1)
continue;
id += SKILL_VAR_MIN_ID;
}
std::string name = XML::langProperty(node, "name",
// TRANSLATORS: skills dialog. skill id
strprintf(_("Skill %d"), id));
SkillInfo *skill = getSkill(id);
if (!skill)
{
skill = new SkillInfo;
skill->id = static_cast<unsigned int>(id);
skill->modifiable = false;
skill->visible = false;
skill->model = model;
skill->update();
skill->useButton = XML::getProperty(
node, "useButton", _("Use"));
skill->owner = parseOwner(XML::getProperty(
node, "owner", "player"));
skill->errorText = XML::getProperty(
node, "errorText", name);
model->addSkill(skill);
mSkills[id] = skill;
}
std::string icon = XML::getProperty(node, "icon", "");
const int level = XML::getProperty(node, "level", 0);
SkillData *data = skill->getData(level);
if (!data)
data = new SkillData();
data->name = name;
data->setIcon(icon);
if (skill->id < SKILL_VAR_MIN_ID)
{
data->dispName = strprintf("%s, %u",
name.c_str(), skill->id);
}
else
{
data->dispName = strprintf("%s, (%u)",
name.c_str(), skill->id - SKILL_VAR_MIN_ID);
}
data->shortName = XML::langProperty(node,
"shortName", name.substr(0, 3));
data->description = XML::langProperty(
node, "description", "");
data->particle = XML::getProperty(
node, "particle", "");
data->soundHit.sound = XML::getProperty(
node, "soundHit", "");
data->soundHit.delay = XML::getProperty(
node, "soundHitDelay", 0);
data->soundMiss.sound = XML::getProperty(
node, "soundMiss", "");
data->soundMiss.delay = XML::getProperty(
node, "soundMissDelay", 0);
skill->addData(level, data);
}
}
model->updateVisibilities();
// possible leak listbox, scroll
SkillListBox *const listbox = new SkillListBox(this, model);
listbox->setActionEventId("sel");
listbox->addActionListener(this);
ScrollArea *const scroll = new ScrollArea(this, listbox, false);
scroll->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
scroll->setVerticalScrollPolicy(ScrollArea::SHOW_ALWAYS);
SkillTab *const tab = new SkillTab(this, setName, listbox);
mDeleteTabs.push_back(tab);
mTabs->addTab(tab, scroll);
}
}
}
bool SkillDialog::updateSkill(const int id,
const int range,
const bool modifiable,
const SkillType::SkillType type,
const int sp)
{
const SkillMap::const_iterator it = mSkills.find(id);
if (it != mSkills.end())
{
SkillInfo *const info = it->second;
if (info)
{
info->modifiable = modifiable;
info->range = range;
info->type = type;
info->sp = sp;
info->update();
}
return true;
}
return false;
}
void SkillDialog::addSkill(const SkillOwner::Type owner,
const int id,
const std::string &name,
const int level,
const int range,
const bool modifiable,
const SkillType::SkillType type,
const int sp)
{
if (mDefaultModel)
{
SkillInfo *const skill = new SkillInfo;
skill->id = static_cast<unsigned int>(id);
skill->type = type;
skill->owner = owner;
SkillData *const data = skill->data;
if (name.empty())
{
data->name = "Unknown skill Id: " + toString(id);
data->dispName = data->name;
}
else
{
data->name = name;
data->dispName = strprintf("%s, %u", name.c_str(), skill->id);
}
data->description.clear();
data->setIcon("");
data->shortName = toString(skill->id);
skill->modifiable = modifiable;
skill->visible = false;
skill->model = mDefaultModel;
skill->level = level;
// TRANSLATORS: skills dialog. skill level
skill->skillLevel = strprintf(_("Lvl: %d"), level);
skill->range = range;
skill->sp = sp;
skill->update();
skill->useButton = _("Use");
skill->errorText = name;
mDefaultModel->addSkill(skill);
mSkills[id] = skill;
mDefaultModel->updateVisibilities();
}
}
SkillInfo* SkillDialog::getSkill(const int id) const
{
SkillMap::const_iterator it = mSkills.find(id);
if (it != mSkills.end())
return (*it).second;
return nullptr;
}
SkillInfo* SkillDialog::getSkillByItem(const int itemId) const
{
SkillMap::const_iterator it = mSkills.find(itemId - SKILL_MIN_ID);
if (it != mSkills.end())
return (*it).second;
return nullptr;
}
void SkillDialog::setSkillDuration(const SkillOwner::Type owner,
const int id,
const int duration)
{
SkillMap::const_iterator it = mSkills.find(id);
SkillInfo *info = nullptr;
if (it == mSkills.end())
{
addSkill(owner, id, "", 0, 0, false, SkillType::Unknown, 0);
it = mSkills.find(id);
}
if (it != mSkills.end())
{
info = (*it).second;
}
if (info)
{
info->duration = duration;
info->durationTime = tick_time;
addSkillDuration(info);
}
}
void SkillDialog::widgetResized(const Event &event)
{
Window::widgetResized(event);
if (mTabs)
mTabs->adjustSize();
}
void SkillDialog::useItem(const int itemId) const
{
const std::map<int, SkillInfo*>::const_iterator
it = mSkills.find(itemId - SKILL_MIN_ID);
if (it == mSkills.end())
return;
const SkillInfo *const info = (*it).second;
useSkill(info);
}
void SkillDialog::updateTabSelection()
{
const SkillTab *const tab = static_cast<SkillTab*>(
mTabs->getSelectedTab());
if (tab)
{
if (const SkillInfo *const info = tab->getSelectedInfo())
{
mUseButton->setEnabled(info->range > 0);
mIncreaseButton->setEnabled(info->id < SKILL_VAR_MIN_ID);
mUseButton->setCaption(info->useButton);
}
else
{
mUseButton->setEnabled(false);
mUseButton->setCaption(_("Use"));
}
}
}
void SkillDialog::updateQuest(const int var, const int val)
{
const int id = var + SKILL_VAR_MIN_ID;
const SkillMap::const_iterator it = mSkills.find(id);
if (it != mSkills.end())
{
SkillInfo *const info = it->second;
if (info)
{
PlayerInfo::setSkillLevel(id, val);
info->level = val;
info->update();
}
}
}
void SkillDialog::playUpdateEffect(const int id) const
{
const int effectId = paths.getIntValue("skillLevelUpEffectId");
if (!effectManager || effectId == -1)
return;
const SkillMap::const_iterator it = mSkills.find(id);
if (it != mSkills.end())
{
if (it->second)
effectManager->trigger(effectId, localPlayer);
}
}
void SkillDialog::useSkill(const SkillInfo *const info)
{
if (info && localPlayer)
{
switch (info->type)
{
case SkillType::Attack:
case SkillType::Support:
{
const Being *const being = localPlayer->getTarget();
if (being)
{
skillHandler->useBeing(info->level,
info->id, being->getId());
}
break;
}
case SkillType::Self:
skillHandler->useBeing(info->level,
info->id, localPlayer->getId());
break;
case SkillType::Ground:
case SkillType::TargetTrap:
// for now unused
break;
case SkillType::Unknown:
case SkillType::Unused:
default:
break;
}
}
}
void SkillDialog::addSkillDuration(SkillInfo *const skill)
{
FOR_EACH (std::vector<SkillInfo*>::const_iterator, it, mDurations)
{
if ((*it)->id == skill->id)
return;
}
mDurations.push_back(skill);
}
void SkillDialog::slowLogic()
{
FOR_EACH (std::vector<SkillInfo*>::iterator, it, mDurations)
{
SkillInfo *const skill = *it;
if (skill)
{
const int time = get_elapsed_time(skill->durationTime);
if (time >= skill->duration)
{
it = mDurations.erase(it);
skill->cooldown = 0;
skill->duration = 0;
skill->durationTime = 0;
if (it != mDurations.begin())
-- it;
}
else if (time)
{
skill->cooldown = skill->duration * 100 / time;
}
}
}
}
|