-
Notifications
You must be signed in to change notification settings - Fork 21
/
creature.cpp
1710 lines (1454 loc) · 39.7 KB
/
creature.cpp
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
//////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
//////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////
// 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 (at your option) 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 "otpch.h"
#include "creature.h"
#include "game.h"
#include "player.h"
#include "npc.h"
#include "monster.h"
#include "container.h"
#include "condition.h"
#include "combat.h"
#include "configmanager.h"
#include <string>
#include <vector>
#include <algorithm>
#if defined __EXCEPTION_TRACER__
#include "exception.h"
#endif
OTSYS_THREAD_LOCKVAR AutoID::autoIDLock;
uint32_t AutoID::count = 1000;
AutoID::list_type AutoID::list;
double Creature::speedA = 857.36;
double Creature::speedB = 261.29;
double Creature::speedC = -4795.01;
extern Game g_game;
extern ConfigManager g_config;
extern CreatureEvents* g_creatureEvents;
Creature::Creature() :
isInternalRemoved(false)
{
id = 0;
_tile = NULL;
direction = SOUTH;
master = NULL;
lootDrop = true;
skillLoss = true;
health = 1000;
healthMax = 1000;
mana = 0;
manaMax = 0;
lastStep = 0;
lastStepCost = 1;
baseSpeed = 220;
varSpeed = 0;
masterRadius = -1;
masterPos.x = 0;
masterPos.y = 0;
masterPos.z = 0;
followCreature = NULL;
hasFollowPath = false;
eventWalk = 0;
cancelNextWalk = false;
forceUpdateFollowPath = false;
isMapLoaded = false;
isUpdatingPath = false;
memset(localMapCache, 0, sizeof(localMapCache));
attackedCreature = NULL;
_lastHitCreature = NULL;
_mostDamageCreature = NULL;
lastHitUnjustified = false;
mostDamageUnjustified = false;
lastHitCreature = 0;
blockCount = 0;
blockTicks = 0;
walkUpdateTicks = 0;
checkCreatureVectorIndex = -1;
creatureCheck = false;
scriptEventsBitField = 0;
hiddenHealth = false;
onIdleStatus();
}
Creature::~Creature()
{
std::list<Creature*>::iterator cit;
for(cit = summons.begin(); cit != summons.end(); ++cit)
{
(*cit)->setAttackedCreature(NULL);
(*cit)->setMaster(NULL);
(*cit)->releaseThing2();
}
summons.clear();
for(ConditionList::iterator it = conditions.begin(); it != conditions.end(); ++it)
{
(*it)->endCondition(this, CONDITIONEND_CLEANUP);
delete *it;
}
conditions.clear();
attackedCreature = NULL;
//std::cout << "Creature destructor " << this->getID() << std::endl;
}
bool Creature::canSee(const Position& myPos, const Position& pos, uint32_t viewRangeX, uint32_t viewRangeY)
{
if(myPos.z <= 7)
{
//we are on ground level or above (7 -> 0)
//view is from 7 -> 0
if(pos.z > 7)
return false;
}
else if(myPos.z >= 8)
{
//we are underground (8 -> 15)
//view is +/- 2 from the floor we stand on
if(std::abs(myPos.z - pos.z) > 2)
return false;
}
int offsetz = myPos.z - pos.z;
if(((uint32_t)pos.x >= myPos.x - viewRangeX + offsetz) && ((uint32_t)pos.x <= myPos.x + viewRangeX + offsetz) &&
((uint32_t)pos.y >= myPos.y - viewRangeY + offsetz) && ((uint32_t)pos.y <= myPos.y + viewRangeY + offsetz))
return true;
return false;
}
bool Creature::canSee(const Position& pos) const
{
return canSee(getPosition(), pos, Map::maxViewportX, Map::maxViewportY);
}
bool Creature::canSeeCreature(const Creature* creature) const
{
if(!canSeeInvisibility() && creature->isInvisible())
return false;
return true;
}
int64_t Creature::getTimeSinceLastMove() const
{
if(lastStep)
return OTSYS_TIME() - lastStep;
return 0x7FFFFFFFFFFFFFFFLL;
}
int32_t Creature::getWalkDelay(Direction dir) const
{
if(lastStep == 0)
return 0;
int64_t ct = OTSYS_TIME();
int64_t stepDuration = getStepDuration(dir);
return stepDuration - (ct - lastStep);
}
int32_t Creature::getWalkDelay() const
{
//Used for auto-walking
if(lastStep == 0)
return 0;
int64_t ct = OTSYS_TIME();
int64_t stepDuration = getStepDuration() * lastStepCost;
return stepDuration - (ct - lastStep);
}
void Creature::onThink(uint32_t interval)
{
if(!isMapLoaded && useCacheMap())
{
isMapLoaded = true;
updateMapCache();
}
if(followCreature && getMaster() != followCreature && !canSeeCreature(followCreature))
onCreatureDisappear(followCreature, false);
if(attackedCreature && getMaster() != attackedCreature && !canSeeCreature(attackedCreature))
onCreatureDisappear(attackedCreature, false);
blockTicks += interval;
if(blockTicks >= 1000)
{
blockCount = std::min<uint32_t>(blockCount + 1, 2);
blockTicks = 0;
}
if(followCreature)
{
walkUpdateTicks += interval;
if(forceUpdateFollowPath || walkUpdateTicks >= 2000)
{
walkUpdateTicks = 0;
forceUpdateFollowPath = false;
isUpdatingPath = true;
}
}
if(isUpdatingPath)
{
isUpdatingPath = false;
goToFollowCreature();
}
//scripting event - onThink
CreatureEventList thinkEvents = getCreatureEvents(CREATURE_EVENT_THINK);
for(CreatureEventList::const_iterator it = thinkEvents.begin(), end = thinkEvents.end(); it != end; ++it)
(*it)->executeOnThink(this, interval);
}
void Creature::onAttacking(uint32_t interval)
{
if(!attackedCreature)
return;
onAttacked();
attackedCreature->onAttacked();
if(g_game.isSightClear(getPosition(), attackedCreature->getPosition(), true))
doAttacking(interval);
}
void Creature::onIdleStatus()
{
if(getHealth() > 0)
{
healMap.clear();
damageMap.clear();
}
}
void Creature::onWalk()
{
if(getWalkDelay() <= 0)
{
Direction dir;
uint32_t flags = FLAG_IGNOREFIELDDAMAGE;
if(getNextStep(dir, flags))
{
ReturnValue ret = g_game.internalMoveCreature(this, dir, flags);
if(ret != RET_NOERROR)
{
if(Player* player = getPlayer())
{
player->sendCancelMessage(ret);
player->sendCancelWalk();
}
forceUpdateFollowPath = true;
}
}
else
{
if(listWalkDir.empty())
onWalkComplete();
stopEventWalk();
}
}
if(cancelNextWalk)
{
listWalkDir.clear();
onWalkAborted();
cancelNextWalk = false;
}
if(eventWalk != 0)
{
eventWalk = 0;
addEventWalk();
}
}
void Creature::onWalk(Direction& dir)
{
if(hasCondition(CONDITION_DRUNK))
{
uint32_t r = random_range(0, 20);
if(r <= 4)
{
switch(r)
{
case 0: dir = NORTH; break;
case 1: dir = WEST; break;
case 3: dir = SOUTH; break;
case 4: dir = EAST; break;
default:
break;
}
g_game.internalCreatureSay(this, SPEAK_MONSTER_SAY, "Hicks!", false);
}
}
}
bool Creature::getNextStep(Direction& dir, uint32_t& flags)
{
if(listWalkDir.empty())
return false;
dir = listWalkDir.front();
listWalkDir.pop_front();
onWalk(dir);
return true;
}
bool Creature::startAutoWalk(std::list<Direction>& listDir)
{
const Player* thisPlayer = getPlayer();
if(thisPlayer && thisPlayer->getNoMove())
{
thisPlayer->sendCancelWalk();
return false;
}
listWalkDir = listDir;
addEventWalk(listDir.size() == 1);
return true;
}
void Creature::addEventWalk(bool firstStep)
{
cancelNextWalk = false;
if(getStepSpeed() <= 0)
return;
if(eventWalk != 0)
return;
int64_t ticks = getEventStepTicks(firstStep);
if(ticks <= 0)
return;
// Take first step right away, but still queue the next
if(ticks == 1)
g_game.checkCreatureWalk(getID());
eventWalk = g_scheduler.addEvent(createSchedulerTask(
std::max<int64_t>(SCHEDULER_MINTICKS, ticks), boost::bind(&Game::checkCreatureWalk, &g_game, getID())));
}
void Creature::stopEventWalk()
{
if(eventWalk != 0)
{
g_scheduler.stopEvent(eventWalk);
eventWalk = 0;
}
}
void Creature::updateMapCache()
{
Tile* tile;
const Position& myPos = getPosition();
Position pos(0, 0, myPos.z);
for(int32_t y = -((mapWalkHeight - 1) / 2); y <= ((mapWalkHeight - 1) / 2); ++y)
{
for(int32_t x = -((mapWalkWidth - 1) / 2); x <= ((mapWalkWidth - 1) / 2); ++x)
{
pos.x = myPos.x + x;
pos.y = myPos.y + y;
tile = g_game.getTile(pos.x, pos.y, myPos.z);
updateTileCache(tile, pos);
}
}
}
#ifdef __DEBUG__
void Creature::validateMapCache()
{
const Position& myPos = getPosition();
for(int32_t y = -((mapWalkHeight - 1) / 2); y <= ((mapWalkHeight - 1) / 2); ++y)
{
for(int32_t x = -((mapWalkWidth - 1) / 2); x <= ((mapWalkWidth - 1) / 2); ++x)
getWalkCache(Position(myPos.x + x, myPos.y + y, myPos.z));
}
}
#endif
void Creature::updateTileCache(const Tile* tile, int32_t dx, int32_t dy)
{
if((std::abs(dx) <= (mapWalkWidth - 1) / 2) &&
(std::abs(dy) <= (mapWalkHeight - 1) / 2))
{
int32_t x = (mapWalkWidth - 1) / 2 + dx;
int32_t y = (mapWalkHeight - 1) / 2 + dy;
localMapCache[y][x] = (tile && tile->__queryAdd(0, this, 1,
FLAG_PATHFINDING | FLAG_IGNOREFIELDDAMAGE) == RET_NOERROR);
}
#ifdef __DEBUG__
else
std::cout << "Creature::updateTileCache out of range." << std::endl;
#endif
}
void Creature::updateTileCache(const Tile* tile, const Position& pos)
{
const Position& myPos = getPosition();
if(pos.z == myPos.z)
{
int32_t dx = pos.x - myPos.x;
int32_t dy = pos.y - myPos.y;
updateTileCache(tile, dx, dy);
}
}
int32_t Creature::getWalkCache(const Position& pos) const
{
if(!useCacheMap())
return 2;
const Position& myPos = getPosition();
if(myPos.z != pos.z)
return 0;
if(pos == myPos)
return 1;
int32_t dx = pos.x - myPos.x;
int32_t dy = pos.y - myPos.y;
if((std::abs(dx) <= (mapWalkWidth - 1) / 2) &&
(std::abs(dy) <= (mapWalkHeight - 1) / 2))
{
int32_t x = (mapWalkWidth - 1) / 2 + dx;
int32_t y = (mapWalkHeight - 1) / 2 + dy;
#ifdef __DEBUG__
//testing
Tile* tile = g_game.getTile(pos.x, pos.y, pos.z);
if(tile && (tile->__queryAdd(0, this, 1, FLAG_PATHFINDING | FLAG_IGNOREFIELDDAMAGE) == RET_NOERROR))
{
if(!localMapCache[y][x])
std::cout << "Wrong cache value" << std::endl;
}
else
{
if(localMapCache[y][x])
std::cout << "Wrong cache value" << std::endl;
}
#endif
if(localMapCache[y][x])
return 1;
else
return 0;
}
//out of range
return 2;
}
void Creature::onAddTileItem(const Tile* tile, const Position& pos, const Item* item)
{
if(isMapLoaded && pos.z == getPosition().z)
updateTileCache(tile, pos);
}
void Creature::onUpdateTileItem(const Tile* tile, const Position& pos, const Item* oldItem,
const ItemType& oldType, const Item* newItem, const ItemType& newType)
{
if(!isMapLoaded)
return;
if(oldType.blockSolid || oldType.blockPathFind || newType.blockPathFind || newType.blockSolid)
{
if(pos.z == getPosition().z)
updateTileCache(tile, pos);
}
}
void Creature::onRemoveTileItem(const Tile* tile, const Position& pos, const ItemType& iType,
const Item* item)
{
if(!isMapLoaded)
return;
if(iType.blockSolid || iType.blockPathFind || iType.isGroundTile())
{
if(pos.z == getPosition().z)
updateTileCache(tile, pos);
}
}
void Creature::onCreatureAppear(const Creature* creature, bool isLogin)
{
if(creature == this)
{
if(useCacheMap())
{
isMapLoaded = true;
updateMapCache();
}
if(isLogin)
setLastPosition(getPosition());
}
else if(isMapLoaded)
{
if(creature->getPosition().z == getPosition().z)
updateTileCache(creature->getTile(), creature->getPosition());
}
}
void Creature::onCreatureDisappear(const Creature* creature, uint32_t stackpos, bool isLogout)
{
onCreatureDisappear(creature, true);
if(creature == this)
{
if(getMaster() && !getMaster()->isRemoved())
getMaster()->removeSummon(this);
}
else if(isMapLoaded)
{
if(creature->getPosition().z == getPosition().z)
updateTileCache(creature->getTile(), creature->getPosition());
}
}
void Creature::onCreatureDisappear(const Creature* creature, bool isLogout)
{
if(attackedCreature == creature)
{
setAttackedCreature(NULL);
onAttackedCreatureDisappear(isLogout);
}
if(followCreature == creature)
{
setFollowCreature(NULL);
onFollowCreatureDisappear(isLogout);
}
}
void Creature::onChangeZone(ZoneType_t zone)
{
if(attackedCreature && zone == ZONE_PROTECTION)
onCreatureDisappear(attackedCreature, false);
}
void Creature::onAttackedCreatureChangeZone(ZoneType_t zone)
{
if(zone == ZONE_PROTECTION)
onCreatureDisappear(attackedCreature, false);
}
void Creature::onCreatureMove(const Creature* creature, const Tile* newTile, const Position& newPos,
const Tile* oldTile, const Position& oldPos, bool teleport)
{
if(creature == this)
{
lastStep = OTSYS_TIME();
lastStepCost = 1;
if(!teleport)
{
if(oldPos.z != newPos.z)
{
//floor change extra cost
lastStepCost = 1;
}
else if(std::abs(newPos.x - oldPos.x) >=1 && std::abs(newPos.y - oldPos.y) >= 1)
{
//diagonal extra cost
lastStepCost = 3;
}
}
else
stopEventWalk();
if(!summons.empty())
{
//check if any of our summons is out of range (+/- 2 floors or 30 tiles away)
std::list<Creature*> despawnList;
std::list<Creature*>::iterator cit;
for(cit = summons.begin(); cit != summons.end(); ++cit)
{
const Position pos = (*cit)->getPosition();
if((std::abs(pos.z - newPos.z) > 2) ||
(std::max(std::abs((newPos.x) - pos.x), std::abs((newPos.y - 1) - pos.y)) > 30))
{
despawnList.push_back((*cit));
}
}
for(cit = despawnList.begin(); cit != despawnList.end(); ++cit)
g_game.removeCreature((*cit), true);
}
if(newTile->getZone() != oldTile->getZone())
onChangeZone(getZone());
//update map cache
if(isMapLoaded)
{
if(teleport || oldPos.z != newPos.z)
updateMapCache();
else
{
Tile* tile;
const Position& myPos = getPosition();
Position pos;
if(oldPos.y > newPos.y) //north
{
//shift y south
for(int32_t y = mapWalkHeight - 1 - 1; y >= 0; --y)
memcpy(localMapCache[y + 1], localMapCache[y], sizeof(localMapCache[y]));
//update 0
for(int32_t x = -((mapWalkWidth - 1) / 2); x <= ((mapWalkWidth - 1) / 2); ++x)
{
tile = g_game.getTile(myPos.x + x, myPos.y - ((mapWalkHeight - 1) / 2), myPos.z);
updateTileCache(tile, x, -((mapWalkHeight - 1) / 2));
}
}
else if(oldPos.y < newPos.y) // south
{
//shift y north
for(int32_t y = 0; y <= mapWalkHeight - 1 - 1; ++y)
memcpy(localMapCache[y], localMapCache[y + 1], sizeof(localMapCache[y]));
//update mapWalkHeight - 1
for(int32_t x = -((mapWalkWidth - 1) / 2); x <= ((mapWalkWidth - 1) / 2); ++x)
{
tile = g_game.getTile(myPos.x + x, myPos.y + ((mapWalkHeight - 1) / 2), myPos.z);
updateTileCache(tile, x, (mapWalkHeight - 1) / 2);
}
}
if(oldPos.x < newPos.x) // east
{
//shift y west
int32_t starty = 0;
int32_t endy = mapWalkHeight - 1;
int32_t dy = (oldPos.y - newPos.y);
if(dy < 0)
endy = endy + dy;
else if(dy > 0)
starty = starty + dy;
for(int32_t y = starty; y <= endy; ++y)
{
for(int32_t x = 0; x <= mapWalkWidth - 1 - 1; ++x)
localMapCache[y][x] = localMapCache[y][x + 1];
}
//update mapWalkWidth - 1
for(int32_t y = -((mapWalkHeight - 1) / 2); y <= ((mapWalkHeight - 1) / 2); ++y)
{
tile = g_game.getTile(myPos.x + ((mapWalkWidth - 1) / 2), myPos.y + y, myPos.z);
updateTileCache(tile, (mapWalkWidth - 1) / 2, y);
}
}
else if(oldPos.x > newPos.x) // west
{
//shift y east
int32_t starty = 0;
int32_t endy = mapWalkHeight - 1;
int32_t dy = (oldPos.y - newPos.y);
if(dy < 0)
endy = endy + dy;
else if(dy > 0)
starty = starty + dy;
for(int32_t y = starty; y <= endy; ++y)
{
for(int32_t x = mapWalkWidth - 1 - 1; x >= 0; --x)
localMapCache[y][x + 1] = localMapCache[y][x];
}
//update 0
for(int32_t y = -((mapWalkHeight - 1) / 2); y <= ((mapWalkHeight - 1) / 2); ++y)
{
tile = g_game.getTile(myPos.x - ((mapWalkWidth - 1) / 2), myPos.y + y, myPos.z);
updateTileCache(tile, -((mapWalkWidth - 1) / 2), y);
}
}
updateTileCache(oldTile, oldPos);
#ifdef __DEBUG__
validateMapCache();
#endif
}
}
}
else
{
if(isMapLoaded)
{
const Position& myPos = getPosition();
if(newPos.z == myPos.z)
updateTileCache(newTile, newPos);
if(oldPos.z == myPos.z)
updateTileCache(oldTile, oldPos);
}
}
if(creature == followCreature || (creature == this && followCreature))
{
if(hasFollowPath)
{
isUpdatingPath = false;
g_dispatcher.addTask(createTask(
boost::bind(&Game::updateCreatureWalk, &g_game, getID())));
}
if(newPos.z != oldPos.z || !canSee(followCreature->getPosition()))
onCreatureDisappear(followCreature, false);
}
if(creature == attackedCreature || (creature == this && attackedCreature))
{
if(newPos.z != oldPos.z || !canSee(attackedCreature->getPosition()))
onCreatureDisappear(attackedCreature, false);
else
{
if(hasExtraSwing())
{
//our target is moving lets see if we can get in hit
g_dispatcher.addTask(createTask(
boost::bind(&Game::checkCreatureAttack, &g_game, getID())));
}
if(newTile->getZone() != oldTile->getZone())
onAttackedCreatureChangeZone(attackedCreature->getZone());
}
}
}
void Creature::onDeath()
{
Creature* mostDamageCreatureMaster = NULL;
Creature* lastHitCreatureMaster = NULL;
if(getKillers(&_lastHitCreature, &_mostDamageCreature))
{
if(_lastHitCreature)
{
lastHitUnjustified = _lastHitCreature->onKilledCreature(this);
lastHitCreatureMaster = _lastHitCreature->getMaster();
}
if(_mostDamageCreature)
{
mostDamageCreatureMaster = _mostDamageCreature->getMaster();
bool isNotLastHitMaster = (_mostDamageCreature != lastHitCreatureMaster);
bool isNotMostDamageMaster = (_lastHitCreature != mostDamageCreatureMaster);
bool isNotSameMaster = lastHitCreatureMaster == NULL || (mostDamageCreatureMaster != lastHitCreatureMaster);
if(_mostDamageCreature != _lastHitCreature && isNotLastHitMaster && isNotMostDamageMaster && isNotSameMaster)
mostDamageUnjustified = _mostDamageCreature->onKilledCreature(this, false);
}
}
for(CountMap::iterator it = damageMap.begin(), end = damageMap.end(); it != end; ++it)
{
if(Creature* attacker = g_game.getCreatureByID(it->first))
attacker->onAttackedCreatureKilled(this);
}
bool droppedCorpse = dropCorpse();
death();
if(getMaster())
getMaster()->removeSummon(this);
if(droppedCorpse)
g_game.removeCreature(this, false);
}
bool Creature::dropCorpse()
{
if(!lootDrop && getMonster() && !(master && master->getPlayer()))
{
if(master)
{
//scripting event - onDeath
CreatureEventList deathEvents = getCreatureEvents(CREATURE_EVENT_DEATH);
for(CreatureEventList::const_iterator it = deathEvents.begin(); it != deathEvents.end(); ++it)
(*it)->executeOnDeath(this, NULL, _lastHitCreature, _mostDamageCreature, lastHitUnjustified, mostDamageUnjustified);
}
g_game.addMagicEffect(getPosition(), NM_ME_POFF);
}
else
{
Item* splash = NULL;
switch(getRace())
{
case RACE_VENOM:
splash = Item::CreateItem(ITEM_FULLSPLASH, FLUID_GREEN);
break;
case RACE_BLOOD:
splash = Item::CreateItem(ITEM_FULLSPLASH, FLUID_BLOOD);
break;
default:
break;
}
Tile* tile = getTile();
if(splash)
{
g_game.internalAddItem(tile, splash, INDEX_WHEREEVER, FLAG_NOLIMIT);
g_game.startDecay(splash);
}
Item* corpse = getCorpse();
if(corpse)
{
g_game.internalAddItem(tile, corpse, INDEX_WHEREEVER, FLAG_NOLIMIT);
g_game.startDecay(corpse);
}
//scripting event - onDeath
CreatureEventList deathEvents = getCreatureEvents(CREATURE_EVENT_DEATH);
for(CreatureEventList::const_iterator it = deathEvents.begin(); it != deathEvents.end(); ++it)
(*it)->executeOnDeath(this, corpse, _lastHitCreature, _mostDamageCreature, lastHitUnjustified, mostDamageUnjustified);
if(corpse)
dropLoot(corpse->getContainer());
}
return true;
}
bool Creature::getKillers(Creature** _lastHitCreature, Creature** _mostDamageCreature)
{
*_lastHitCreature = g_game.getCreatureByID(lastHitCreature);
int32_t mostDamage = 0;
for(CountMap::const_iterator it = damageMap.begin(), end = damageMap.end(); it != end; ++it)
{
CountBlock_t cb = it->second;
if((cb.total > mostDamage && (OTSYS_TIME() - cb.ticks <= g_game.getInFightTicks())))
{
if((*_mostDamageCreature = g_game.getCreatureByID(it->first)))
mostDamage = cb.total;
}
}
return (*_lastHitCreature || *_mostDamageCreature);
}
bool Creature::hasBeenAttacked(uint32_t attackerId)
{
CountMap::iterator it = damageMap.find(attackerId);
if(it != damageMap.end())
return (OTSYS_TIME() - it->second.ticks <= g_game.getInFightTicks());
return false;
}
Item* Creature::getCorpse()
{
Item* corpse = Item::CreateItem(getLookCorpse());
return corpse;
}
void Creature::changeHealth(int32_t healthChange, bool sendHealthChange/* = true*/)
{
int32_t oldHealth = health;
if(healthChange > 0)
health += std::min(healthChange, getMaxHealth() - health);
else
health = std::max<int32_t>(0, health + healthChange);
if(sendHealthChange && oldHealth != health)
g_game.addCreatureHealth(this);
}
void Creature::changeMana(int32_t manaChange)
{
if(manaChange > 0)
mana += std::min(manaChange, getMaxMana() - mana);
else
mana = std::max<int32_t>(0, mana + manaChange);
}
void Creature::drainHealth(Creature* attacker, CombatType_t combatType, int32_t damage)
{
changeHealth(-damage, false);
if(attacker)
attacker->onAttackedCreatureDrainHealth(this, damage);
}
void Creature::drainMana(Creature* attacker, int32_t manaLoss)
{
onAttacked();
changeMana(-manaLoss);
}
BlockType_t Creature::blockHit(Creature* attacker, CombatType_t combatType, int32_t& damage,
bool checkDefense /* = false */, bool checkArmor /* = false */)
{
BlockType_t blockType = BLOCK_NONE;
if(isImmune(combatType))
{
damage = 0;
blockType = BLOCK_IMMUNITY;
}
else if(checkDefense || checkArmor)
{
bool hasDefense = false;
if(blockCount > 0)
{
--blockCount;
hasDefense = true;
}
if(checkDefense && hasDefense)
{
int32_t maxDefense = getDefense();
int32_t minDefense = maxDefense / 2;
damage -= random_range(minDefense, maxDefense);
if(damage <= 0)
{
damage = 0;
blockType = BLOCK_DEFENSE;
checkArmor = false;
}
}
if(checkArmor)
{
int32_t armorValue = getArmor();
int32_t minArmorReduction = 0;
int32_t maxArmorReduction = 0;
if(armorValue > 1)
{
minArmorReduction = (int32_t)std::ceil(armorValue * 0.475);
maxArmorReduction = (int32_t)std::ceil(((armorValue * 0.475) - 1) + minArmorReduction);
}
else if(armorValue == 1)
{
minArmorReduction = 1;
maxArmorReduction = 1;
}
damage -= random_range(minArmorReduction, maxArmorReduction);
if(damage <= 0)
{
damage = 0;
blockType = BLOCK_ARMOR;
}
}
if(hasDefense && blockType != BLOCK_NONE)
onBlockHit(blockType);
}
if(attacker)
{
attacker->onAttackedCreature(this);
attacker->onAttackedCreatureBlockHit(this, blockType);
}
onAttacked();
return blockType;
}
bool Creature::setAttackedCreature(Creature* creature)
{
if(creature)
{