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
|
/*
* The Mana Server
* Copyright (C) 2004-2010 The Mana World Development Team
*
* This file is part of The Mana Server.
*
* The Mana Server 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 Server 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 Server. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <cassert>
#include <cmath>
#include <limits.h>
#include "game-server/character.h"
#include "common/configuration.h"
#include "game-server/accountconnection.h"
#include "game-server/attributemanager.h"
#include "game-server/buysell.h"
#include "game-server/eventlistener.h"
#include "game-server/inventory.h"
#include "game-server/item.h"
#include "game-server/itemmanager.h"
#include "game-server/gamehandler.h"
#include "game-server/mapcomposite.h"
#include "game-server/mapmanager.h"
#include "game-server/state.h"
#include "game-server/trade.h"
#include "scripting/script.h"
#include "net/messagein.h"
#include "net/messageout.h"
#include "serialize/characterdata.h"
#include "utils/logger.h"
// Experience curve related values
const float Character::EXPCURVE_EXPONENT = 3.0f;
const float Character::EXPCURVE_FACTOR = 10.0f;
const float Character::LEVEL_SKILL_PRECEDENCE_FACTOR = 0.75f;
const float Character::EXP_LEVEL_FLEXIBILITY = 1.0f;
Character::Character(MessageIn &msg):
Being(OBJECT_CHARACTER),
mClient(NULL),
mTransactionHandler(NULL),
mRechargePerSpecial(0),
mSpecialUpdateNeeded(false),
mDatabaseID(-1),
mGender(0),
mHairStyle(0),
mHairColor(0),
mLevel(1),
mLevelProgress(0),
mUpdateLevelProgress(false),
mRecalculateLevel(true),
mParty(0),
mTransaction(TRANS_NONE)
{
const AttributeScopes &attr =
attributeManager->getAttributeInfoForType(ATTR_CHAR);
LOG_DEBUG("Character creation: initialisation of "
<< attr.size() << " attributes.");
for (AttributeScopes::const_iterator it1 = attr.begin(),
it1_end = attr.end(); it1 != it1_end; ++it1)
mAttributes.insert(std::make_pair(it1->first, Attribute(*it1->second)));
// Get character data.
mDatabaseID = msg.readInt32();
setName(msg.readString());
deserializeCharacterData(*this, msg);
mOld = getPosition();
Inventory(this).initialise();
modifiedAllAttribute();
setSize(16);
// Give the character some specials for testing.
//TODO: Get from quest vars and equipment
giveSpecial(1);
giveSpecial(2);
giveSpecial(3);
}
void Character::update()
{
// Update character level
if (mRecalculateLevel)
{
mRecalculateLevel = false;
recalculateLevel();
}
// Update special recharge
std::list<Special *> rechargeNeeded;
int numRechargeNeeded = 0;
for (std::map<int, Special*>::iterator i = mSpecials.begin();
i != mSpecials.end(); i++)
{
Special * s = i->second;
if (s->currentMana < s->neededMana)
{
rechargeNeeded.push_back(s);
numRechargeNeeded++;
}
}
if (numRechargeNeeded > 0)
{
mRechargePerSpecial = getModifiedAttribute(ATTR_INT)
/ numRechargeNeeded;
for (std::list<Special*>::iterator i = rechargeNeeded.begin();
i != rechargeNeeded.end(); i++)
{
(*i)->currentMana += mRechargePerSpecial;
}
}
if (mSpecialUpdateNeeded)
{
sendSpecialUpdate();
mSpecialUpdateNeeded = false;
}
mStatusEffects.clear();
StatusEffects::iterator it = mStatus.begin();
while (it != mStatus.end())
{
mStatusEffects[it->first] = it->second.time;
it++;
}
Being::update();
}
void Character::perform()
{
if (mAction != ATTACK || mTarget == NULL) return;
// wait before next attack
// Note: The auto-attack system will handle the delay between two attacks.
// TODO: Remove this condition when it's done.
if (mMoveTime > WORLD_TICK_MS)
{
mMoveTime -= WORLD_TICK_MS;
return;
}
std::list<AutoAttack> attacks;
mAutoAttacks.tick(&attacks);
if (attacks.empty())
return; // TODO: Install default attack?
else
for (std::list<AutoAttack>::iterator it = attacks.begin();
it != attacks.end(); ++it)
performAttack(mTarget, it->getDamage());
}
void Character::died()
{
Being::died();
Script::executeGlobalEventFunction("on_chr_death", this);
}
void Character::respawn()
{
if (mAction != DEAD)
{
LOG_WARN("Character \"" << getName()
<< "\" tried to respawn without being dead");
return;
}
// Make it alive again
setAction(STAND);
// Reset target
mTarget = NULL;
// Execute respawn script
if (!Script::executeGlobalEventFunction("on_chr_death_accept", this))
{
// Script-controlled respawning didn't work - fall back to
// hardcoded logic.
mAttributes[ATTR_HP].setBase(mAttributes[ATTR_MAX_HP].getModifiedAttribute());
updateDerivedAttributes(ATTR_HP);
// Warp back to spawn point.
int spawnMap = Configuration::getValue("char_respawnMap", 1);
int spawnX = Configuration::getValue("char_respawnX", 1024);
int spawnY = Configuration::getValue("char_respawnY", 1024);
GameState::enqueueWarp(this, MapManager::getMap(spawnMap), spawnX, spawnY);
}
}
void Character::useSpecial(int id)
{
//check if the character may use this special in general
std::map<int, Special*>::iterator i = mSpecials.find(id);
if (i == mSpecials.end())
{
LOG_INFO("Character uses special "<<id<<" without autorisation.");
return;
}
//check if the special is currently recharged
Special *special = i->second;
if (special->currentMana < special->neededMana)
{
LOG_INFO("Character uses special "<<id<<" which is not recharged. ("
<<special->currentMana<<"/"<<special->neededMana<<")");
return;
}
//tell script engine to cast the spell
special->currentMana = 0;
Script::performSpecialAction(id, this);
mSpecialUpdateNeeded = true;
return;
}
void Character::sendSpecialUpdate()
{
//GPMSG_SPECIAL_STATUS = 0x0293,
// { B specialID, L current, L max, L recharge }
for (std::map<int, Special*>::iterator i = mSpecials.begin();
i != mSpecials.end(); i++)
{
MessageOut msg(GPMSG_SPECIAL_STATUS );
msg.writeInt8(i->first);
msg.writeInt32(i->second->currentMana);
msg.writeInt32(i->second->neededMana);
msg.writeInt32(mRechargePerSpecial);
/* Yes, the last one is redundant because it is the same for each
special, but I would like to keep the netcode flexible enough
to allow different recharge speed per special when necessary */
gameHandler->sendTo(this, msg);
}
}
int Character::getMapId() const
{
return getMap()->getID();
}
void Character::setMapId(int id)
{
setMap(MapManager::getMap(id));
}
void Character::cancelTransaction()
{
TransactionType t = mTransaction;
mTransaction = TRANS_NONE;
switch (t)
{
case TRANS_TRADE:
static_cast< Trade * >(mTransactionHandler)->cancel();
break;
case TRANS_BUYSELL:
static_cast< BuySell * >(mTransactionHandler)->cancel();
break;
case TRANS_NONE:
return;
}
}
Trade *Character::getTrading() const
{
return mTransaction == TRANS_TRADE
? static_cast< Trade * >(mTransactionHandler) : NULL;
}
BuySell *Character::getBuySell() const
{
return mTransaction == TRANS_BUYSELL
? static_cast< BuySell * >(mTransactionHandler) : NULL;
}
void Character::setTrading(Trade *t)
{
if (t)
{
cancelTransaction();
mTransactionHandler = t;
mTransaction = TRANS_TRADE;
}
else
{
assert(mTransaction == TRANS_NONE || mTransaction == TRANS_TRADE);
mTransaction = TRANS_NONE;
}
}
void Character::setBuySell(BuySell *t)
{
if (t)
{
cancelTransaction();
mTransactionHandler = t;
mTransaction = TRANS_BUYSELL;
}
else
{
assert(mTransaction == TRANS_NONE || mTransaction == TRANS_BUYSELL);
mTransaction = TRANS_NONE;
}
}
void Character::sendStatus()
{
MessageOut attribMsg(GPMSG_PLAYER_ATTRIBUTE_CHANGE);
for (std::set<size_t>::const_iterator i = mModifiedAttributes.begin(),
i_end = mModifiedAttributes.end(); i != i_end; ++i)
{
int attr = *i;
attribMsg.writeInt16(attr);
attribMsg.writeInt32(getAttribute(attr) * 256);
attribMsg.writeInt32(getModifiedAttribute(attr) * 256);
}
if (attribMsg.getLength() > 2) gameHandler->sendTo(this, attribMsg);
mModifiedAttributes.clear();
MessageOut expMsg(GPMSG_PLAYER_EXP_CHANGE);
for (std::set<size_t>::const_iterator i = mModifiedExperience.begin(),
i_end = mModifiedExperience.end(); i != i_end; ++i)
{
int skill = *i;
expMsg.writeInt16(skill);
expMsg.writeInt32(getExpGot(skill));
expMsg.writeInt32(getExpNeeded(skill));
}
if (expMsg.getLength() > 2) gameHandler->sendTo(this, expMsg);
mModifiedExperience.clear();
if (mUpdateLevelProgress)
{
mUpdateLevelProgress = false;
MessageOut progressMessage(GPMSG_LEVEL_PROGRESS);
progressMessage.writeInt8(mLevelProgress);
gameHandler->sendTo(this, progressMessage);
}
}
void Character::modifiedAllAttribute()
{
LOG_DEBUG("Marking all attributes as changed, requiring recalculation.");
for (AttributeMap::iterator it = mAttributes.begin(),
it_end = mAttributes.end();
it != it_end; ++it)
{
recalculateBaseAttribute(it->first);
updateDerivedAttributes(it->first);
}
}
bool Character::recalculateBaseAttribute(unsigned int attr)
{
/*
* `attr' may or may not have changed. Recalculate the base value.
*/
LOG_DEBUG("Received update attribute recalculation request at Character"
"for " << attr << ".");
if (!mAttributes.count(attr))
return false;
double newBase = getAttribute(attr);
/*
* Calculate new base.
*/
switch (attr)
{
case ATTR_ACCURACY:
newBase = getModifiedAttribute(ATTR_DEX); // Provisional
break;
case ATTR_DEFENSE:
newBase = 0.3 * getModifiedAttribute(ATTR_VIT);
break;
case ATTR_DODGE:
newBase = getModifiedAttribute(ATTR_AGI); // Provisional
break;
case ATTR_MAGIC_DODGE:
newBase = 1.0;
// TODO
break;
case ATTR_MAGIC_DEFENSE:
newBase = 0.0;
// TODO
break;
case ATTR_BONUS_ASPD:
newBase = 0.0;
// TODO
break;
default:
return Being::recalculateBaseAttribute(attr);
}
if (newBase != getAttribute(attr))
{
setAttribute(attr, newBase);
updateDerivedAttributes(attr);
return true;
}
LOG_DEBUG("No changes to sync for attribute '" << attr << "'.");
return false;
}
void Character::updateDerivedAttributes(unsigned int attr)
{
/*
* `attr' has changed, perform updates accordingly.
*/
flagAttribute(attr);
switch(attr)
{
case ATTR_STR:
updateDerivedAttributes(ATTR_INV_CAPACITY);
break;
case ATTR_AGI:
updateDerivedAttributes(ATTR_DODGE);
updateDerivedAttributes(ATTR_MOVE_SPEED_TPS);
break;
case ATTR_VIT:
updateDerivedAttributes(ATTR_MAX_HP);
updateDerivedAttributes(ATTR_HP_REGEN);
updateDerivedAttributes(ATTR_DEFENSE);
break;
case ATTR_INT:
// TODO
break;
case ATTR_DEX:
updateDerivedAttributes(ATTR_ACCURACY);
break;
case ATTR_WIL:
// TODO
break;
default:
Being::updateDerivedAttributes(attr);
}
}
void Character::flagAttribute(int attr)
{
// Inform the client of this attribute modification.
accountHandler->updateAttributes(getDatabaseID(), attr,
getAttribute(attr),
getModifiedAttribute(attr));
mModifiedAttributes.insert(attr);
if (attr == ATTR_INT)
{
mSpecialUpdateNeeded = true;
}
}
int Character::expForLevel(int level)
{
return int(pow(level, EXPCURVE_EXPONENT) * EXPCURVE_FACTOR);
}
int Character::levelForExp(int exp)
{
return int(pow(float(exp) / EXPCURVE_FACTOR, 1.0f / EXPCURVE_EXPONENT));
}
void Character::receiveExperience(int skill, int experience, int optimalLevel)
{
// reduce experience when skill is over optimal level
int levelOverOptimum = levelForExp(getExperience(skill)) - optimalLevel;
if (optimalLevel && levelOverOptimum > 0)
{
experience *= EXP_LEVEL_FLEXIBILITY
/ (levelOverOptimum + EXP_LEVEL_FLEXIBILITY);
}
// Add exp
int oldExp = mExperience[skill];
long int newExp = mExperience[skill] + experience;
if (newExp < 0)
newExp = 0; // Avoid integer underflow/negative exp.
// Check the skill cap
long int maxSkillCap = Configuration::getValue("game_maxSkillCap", INT_MAX);
assert(maxSkillCap <= INT_MAX); // Avoid integer overflow.
if (newExp > maxSkillCap)
{
newExp = maxSkillCap;
if (oldExp != maxSkillCap)
{
LOG_INFO("Player hit the skill cap");
// TODO: Send a message to player letting them know they hit the cap
// or not?
}
}
mExperience[skill] = newExp;
mModifiedExperience.insert(skill);
// Inform account server
if (newExp != oldExp)
accountHandler->updateExperience(getDatabaseID(), skill, newExp);
// Check for skill levelup
if (Character::levelForExp(newExp) >= Character::levelForExp(oldExp))
updateDerivedAttributes(skill);
mRecalculateLevel = true;
}
void Character::incrementKillCount(int monsterType)
{
std::map<int, int>::iterator i = mKillCount.find(monsterType);
if (i == mKillCount.end())
{
// Character has never murdered this species before
mKillCount[monsterType] = 1;
}
else
{
// Character is a repeated offender
mKillCount[monsterType] ++;
}
}
int Character::getKillCount(int monsterType) const
{
std::map<int, int>::const_iterator i = mKillCount.find(monsterType);
if (i != mKillCount.end())
return i->second;
return 0;
}
void Character::recalculateLevel()
{
std::list<float> levels;
std::map<int, int>::const_iterator a;
for (a = getSkillBegin(); a != getSkillEnd(); a++)
{
// Only use the first 1000 skill levels in calculation
if (a->first < 1000)
{
float expGot = getExpGot(a->first);
float expNeed = getExpNeeded(a->first);
levels.push_back(levelForExp(a->first) + expGot / expNeed);
}
}
levels.sort();
std::list<float>::iterator i = levels.end();
float level = 0.0f;
float factor = 1.0f;
float factorSum = 0.0f;
while (i != levels.begin())
{
i--;
level += *i * factor;
factorSum += factor;
factor *= LEVEL_SKILL_PRECEDENCE_FACTOR;
}
level /= factorSum;
level += 1.0f; // + 1.0f because the lowest level is 1 and not 0
while (mLevel < level)
{
levelup();
}
int levelProgress = int((level - floor(level)) * 100);
if (levelProgress != mLevelProgress)
{
mLevelProgress = levelProgress;
mUpdateLevelProgress = true;
}
}
int Character::getExpNeeded(size_t skill) const
{
int level = levelForExp(getExperience(skill));
return Character::expForLevel(level + 1) - expForLevel(level);
}
int Character::getExpGot(size_t skill) const
{
int level = levelForExp(getExperience(skill));
return mExperience.at(skill) - Character::expForLevel(level);
}
void Character::levelup()
{
mLevel++;
mCharacterPoints += CHARPOINTS_PER_LEVELUP;
mCorrectionPoints += CORRECTIONPOINTS_PER_LEVELUP;
if (mCorrectionPoints > CORRECTIONPOINTS_MAX)
mCorrectionPoints = CORRECTIONPOINTS_MAX;
MessageOut levelupMsg(GPMSG_LEVELUP);
levelupMsg.writeInt16(mLevel);
levelupMsg.writeInt16(mCharacterPoints);
levelupMsg.writeInt16(mCorrectionPoints);
gameHandler->sendTo(this, levelupMsg);
LOG_INFO(getName()<<" reached level "<<mLevel);
}
AttribmodResponseCode Character::useCharacterPoint(size_t attribute)
{
if (!attributeManager->isAttributeDirectlyModifiable(attribute))
return ATTRIBMOD_INVALID_ATTRIBUTE;
if (!mCharacterPoints)
return ATTRIBMOD_NO_POINTS_LEFT;
--mCharacterPoints;
setAttribute(attribute, getAttribute(attribute) + 1);
updateDerivedAttributes(attribute);
return ATTRIBMOD_OK;
}
AttribmodResponseCode Character::useCorrectionPoint(size_t attribute)
{
if (!attributeManager->isAttributeDirectlyModifiable(attribute))
return ATTRIBMOD_INVALID_ATTRIBUTE;
if (!mCorrectionPoints)
return ATTRIBMOD_NO_POINTS_LEFT;
if (getAttribute(attribute) <= 1)
return ATTRIBMOD_DENIED;
--mCorrectionPoints;
++mCharacterPoints;
setAttribute(attribute, getAttribute(attribute) - 1);
updateDerivedAttributes(attribute);
return ATTRIBMOD_OK;
}
void Character::disconnected()
{
for (Listeners::iterator i = mListeners.begin(),
i_end = mListeners.end(); i != i_end;)
{
const EventListener &l = **i;
++i; // In case the listener removes itself from the list on the fly.
if (l.dispatch->disconnected)
l.dispatch->disconnected(&l, this);
}
}
void Character::giveSpecial(int id)
{
if (mSpecials.find(id) == mSpecials.end())
{
Special *s = new Special();
Script::addDataToSpecial(id, s);
mSpecials[id] = s;
mSpecialUpdateNeeded = true;
}
}
void Character::takeSpecial(int id)
{
std::map<int, Special*>::iterator i = mSpecials.find(id);
if (i != mSpecials.end())
{
delete i->second;
mSpecials.erase(i);
mSpecialUpdateNeeded = true;
}
}
void Character::clearSpecials()
{
for(std::map<int, Special*>::iterator i = mSpecials.begin(); i != mSpecials.end(); i++)
{
delete i->second;
}
mSpecials.clear();
}
|