-
Notifications
You must be signed in to change notification settings - Fork 21
/
map.cpp
1279 lines (1080 loc) · 29.6 KB
/
map.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
//////////////////////////////////////////////////////////////////////
// the map of OpenTibia
//////////////////////////////////////////////////////////////////////
// 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 "definitions.h"
#include <string>
#include <map>
#include <algorithm>
#include <boost/config.hpp>
#include <boost/bind.hpp>
#include "iomap.h"
#include "otsystem.h"
#include "iomapserialize.h"
#include <stdio.h>
#include <iomanip>
#include "items.h"
#include "map.h"
#include "tile.h"
#include "combat.h"
#include "creature.h"
#include "player.h"
#include "configmanager.h"
#include "game.h"
extern Game g_game;
extern ConfigManager g_config;
IOMapSerialize IOMapSerialize;
Map::Map()
{
mapWidth = 0;
mapHeight = 0;
}
Map::~Map()
{
//
}
bool Map::loadMap(const std::string& identifier)
{
IOMap* loader = new IOMap();
if(!loader->loadMap(this, identifier))
{
std::cout << "FATAL: [OTBM loader] " << loader->getLastErrorString() << std::endl;
return false;
}
if(!loader->loadSpawns(this))
std::cout << "WARNING: could not load spawn data." << std::endl;
if(!loader->loadHouses(this))
std::cout << "WARNING: could not load house data." << std::endl;
delete loader;
IOMapSerialize.loadHouseInfo(this);
IOMapSerialize.loadMap(this);
return true;
}
bool Map::saveMap()
{
bool saved = false;
for(uint32_t tries = 0; tries < 3; tries++)
{
if(IOMapSerialize.saveHouseInfo(this))
{
saved = true;
break;
}
}
if(!saved)
return false;
saved = false;
for(uint32_t tries = 0; tries < 3; tries++)
{
if(IOMapSerialize.saveMap(this))
{
saved = true;
break;
}
}
return saved;
}
Tile* Map::getTile(int32_t x, int32_t y, int32_t z)
{
if(x < 0 || x >= 0xFFFF || y < 0 || y >= 0xFFFF || z < 0 || z >= MAP_MAX_LAYERS)
return NULL;
QTreeLeafNode* leaf = QTreeNode::getLeafStatic(&root, x, y);
if(leaf)
{
Floor* floor = leaf->getFloor(z);
if(floor)
return floor->tiles[x & FLOOR_MASK][y & FLOOR_MASK];
}
return NULL;
}
Tile* Map::getTile(const Position& pos)
{
return getTile(pos.x, pos.y, pos.z);
}
void Map::setTile(int32_t x, int32_t y, int32_t z, Tile* newTile)
{
if(x < 0 || x >= 0xFFFF || y < 0 || y >= 0xFFFF || z < 0 || z >= MAP_MAX_LAYERS)
{
std::cout << "ERROR: Attempt to set tile on invalid coordinate " << Position(x, y, z) << "!" << std::endl;
return;
}
QTreeLeafNode::newLeaf = false;
QTreeLeafNode* leaf = root.createLeaf(x, y, 15);
if(QTreeLeafNode::newLeaf)
{
//update north
QTreeLeafNode* northLeaf = root.getLeaf(x, y - FLOOR_SIZE);
if(northLeaf)
northLeaf->m_leafS = leaf;
//update west leaf
QTreeLeafNode* westLeaf = root.getLeaf(x - FLOOR_SIZE, y);
if(westLeaf)
westLeaf->m_leafE = leaf;
//update south
QTreeLeafNode* southLeaf = root.getLeaf(x, y + FLOOR_SIZE);
if(southLeaf)
leaf->m_leafS = southLeaf;
//update east
QTreeLeafNode* eastLeaf = root.getLeaf(x + FLOOR_SIZE, y);
if(eastLeaf)
leaf->m_leafE = eastLeaf;
}
Floor* floor = leaf->createFloor(z);
uint32_t offsetX = x & FLOOR_MASK;
uint32_t offsetY = y & FLOOR_MASK;
if(!floor->tiles[offsetX][offsetY])
{
floor->tiles[offsetX][offsetY] = newTile;
newTile->qt_node = leaf;
}
else
std::cout << "Error: Map::setTile() already exists." << std::endl;
if(newTile->hasFlag(TILESTATE_REFRESH))
{
RefreshBlock_t rb;
rb.lastRefresh = OTSYS_TIME();
if(TileItemVector* newTileItems = newTile->getItemList())
{
for(ItemVector::iterator it = newTileItems->getBeginDownItem(); it != newTileItems->getEndDownItem(); ++it)
rb.list.push_back((*it)->clone());
}
refreshTileMap[newTile] = rb;
}
}
bool Map::placeCreature(const Position& centerPos, Creature* creature, bool extendedPos /*=false*/, bool forceLogin /*=false*/)
{
Tile* tile = getTile(centerPos);
bool foundTile = false;
bool placeInPZ = false;
if(tile)
{
placeInPZ = tile->hasFlag(TILESTATE_PROTECTIONZONE);
ReturnValue ret;
if(g_config.getBoolean(ConfigManager::ACCOUNT_MANAGER) && creature->getPlayer() && creature->getPlayer()->isAccountManager())
ret = tile->__queryAdd(0, creature, 1, FLAG_IGNOREBLOCKITEM | FLAG_IGNOREBLOCKCREATURE);
else
ret = tile->__queryAdd(0, creature, 1, FLAG_IGNOREBLOCKITEM);
if(forceLogin || ret == RET_NOERROR || ret == RET_PLAYERISNOTINVITED)
foundTile = true;
}
typedef std::vector<std::pair<int32_t, int32_t> > RelPosList;
RelPosList relPosList;
if(extendedPos)
{
relPosList.push_back(std::make_pair(0, -2));
relPosList.push_back(std::make_pair(-2, 0));
relPosList.push_back(std::make_pair(0, 2));
relPosList.push_back(std::make_pair(2, 0));
std::random_shuffle(relPosList.begin(), relPosList.end());
}
relPosList.push_back(std::make_pair(-1, -1));
relPosList.push_back(std::make_pair(-1, 0));
relPosList.push_back(std::make_pair(-1, 1));
relPosList.push_back(std::make_pair(0, -1));
relPosList.push_back(std::make_pair(0, 1));
relPosList.push_back(std::make_pair(1, -1));
relPosList.push_back(std::make_pair(1, 0));
relPosList.push_back(std::make_pair(1, 1));
std::random_shuffle(relPosList.begin() + (extendedPos? 4 : 0), relPosList.end());
uint32_t radius = 1;
Position tryPos;
for(uint32_t n = 1; n <= radius && !foundTile; ++n)
{
for(RelPosList::iterator it = relPosList.begin(); it != relPosList.end() && !foundTile; ++it)
{
int32_t dx = it->first * n;
int32_t dy = it->second * n;
tryPos = centerPos;
tryPos.x = tryPos.x + dx;
tryPos.y = tryPos.y + dy;
tile = getTile(tryPos);
if(!tile || (placeInPZ && !tile->hasFlag(TILESTATE_PROTECTIONZONE)))
continue;
if(tile->__queryAdd(0, creature, 1, 0) == RET_NOERROR)
{
if(extendedPos)
{
if(isSightClear(centerPos, tryPos, false))
{
foundTile = true;
break;
}
}
else
{
foundTile = true;
break;
}
}
}
}
if(foundTile)
{
int32_t index = 0;
Item* toItem = NULL;
uint32_t flags = 0;
Cylinder* toCylinder = tile->__queryDestination(index, creature, &toItem, flags);
toCylinder->__internalAddThing(creature);
Tile* toTile = toCylinder->getTile();
toTile->qt_node->addCreature(creature);
return true;
}
#ifdef __DEBUG__
std::cout << "Failed to place creature onto map!" << std::endl;
#endif
return false;
}
bool Map::removeCreature(Creature* creature)
{
Tile* tile = creature->getTile();
if(tile)
{
tile->qt_node->removeCreature(creature);
tile->__removeThing(creature, 0);
return true;
}
return false;
}
void Map::getSpectatorsInternal(SpectatorVec& list, const Position& centerPos, int32_t minRangeX, int32_t maxRangeX, int32_t minRangeY, int32_t maxRangeY, int32_t minRangeZ, int32_t maxRangeZ, bool onlyPlayers)
{
int32_t minoffset = centerPos.z - maxRangeZ;
int32_t x1 = std::min<int32_t>(0xFFFF, std::max<int32_t>(0, (centerPos.x + minRangeX + minoffset)));
int32_t y1 = std::min<int32_t>(0xFFFF, std::max<int32_t>(0, (centerPos.y + minRangeY + minoffset)));
int32_t maxoffset = centerPos.z - minRangeZ;
int32_t x2 = std::min<int32_t>(0xFFFF, std::max<int32_t>(0, (centerPos.x + maxRangeX + maxoffset)));
int32_t y2 = std::min<int32_t>(0xFFFF, std::max<int32_t>(0, (centerPos.y + maxRangeY + maxoffset)));
int32_t startx1 = x1 - (x1 % FLOOR_SIZE);
int32_t starty1 = y1 - (y1 % FLOOR_SIZE);
int32_t endx2 = x2 - (x2 % FLOOR_SIZE);
int32_t endy2 = y2 - (y2 % FLOOR_SIZE);
QTreeLeafNode* startLeaf;
QTreeLeafNode* leafE;
QTreeLeafNode* leafS;
startLeaf = getLeaf(startx1, starty1);
leafS = startLeaf;
for(int32_t ny = starty1; ny <= endy2; ny += FLOOR_SIZE)
{
leafE = leafS;
for(int32_t nx = startx1; nx <= endx2; nx += FLOOR_SIZE)
{
if(leafE)
{
CreatureVector node_list;
if(onlyPlayers)
node_list = leafE->player_list;
else
node_list = leafE->creature_list;
CreatureVector::const_iterator node_iter = node_list.begin();
CreatureVector::const_iterator node_end = node_list.end();
if(node_iter != node_end)
{
do
{
Creature* creature = *node_iter;
const Position& cpos = creature->getPosition();
int32_t offsetZ = centerPos.z - cpos.z;
if(cpos.z < minRangeZ || cpos.z > maxRangeZ)
continue;
if(cpos.y < (centerPos.y + minRangeY + offsetZ) || cpos.y > (centerPos.y + maxRangeY + offsetZ))
continue;
if(cpos.x < (centerPos.x + minRangeX + offsetZ) || cpos.x > (centerPos.x + maxRangeX + offsetZ))
continue;
list.insert(creature);
}
while(++node_iter != node_end);
}
leafE = leafE->stepEast();
}
else
leafE = getLeaf(nx + FLOOR_SIZE, ny);
}
if(leafS)
leafS = leafS->stepSouth();
else
leafS = getLeaf(startx1, ny + FLOOR_SIZE);
}
}
void Map::getSpectators(SpectatorVec& list, const Position& centerPos, bool multifloor /*= false*/, bool onlyPlayers /*= false*/,
int32_t minRangeX /*= 0*/, int32_t maxRangeX /*= 0*/,
int32_t minRangeY /*= 0*/, int32_t maxRangeY /*= 0*/)
{
if(centerPos.z >= MAP_MAX_LAYERS)
return;
bool foundCache = false;
bool cacheResult = false;
minRangeX = (minRangeX == 0 ? -maxViewportX : -minRangeX);
maxRangeX = (maxRangeX == 0 ? maxViewportX : maxRangeX);
minRangeY = (minRangeY == 0 ? -maxViewportY : -minRangeY);
maxRangeY = (maxRangeY == 0 ? maxViewportY : maxRangeY);
if(minRangeX == -maxViewportX && maxRangeX == maxViewportX && minRangeY == -maxViewportY && maxRangeY == maxViewportY && multifloor)
{
SpectatorCache::iterator it = spectatorCache.find(centerPos);
if(it != spectatorCache.end())
{
if(!onlyPlayers)
{
if(!list.empty())
{
const SpectatorVec& cachedList = *it->second;
list.insert(cachedList.begin(), cachedList.end());
}
else
list = *it->second;
}
else
{
const SpectatorVec& cachedList = *it->second;
for(SpectatorVec::const_iterator iter = cachedList.begin(), end = cachedList.end(); iter != end; ++iter)
{
if((*iter)->getPlayer())
list.insert(*iter);
}
}
foundCache = true;
}
else if(!onlyPlayers)
cacheResult = true;
}
if(!foundCache)
{
int32_t minRangeZ;
int32_t maxRangeZ;
if(multifloor)
{
if(centerPos.z > 7)
{
//underground
//8->15
minRangeZ = std::max<int32_t>(centerPos.z - 2, 0);
maxRangeZ = std::min<int32_t>(centerPos.z + 2, MAP_MAX_LAYERS - 1);
}
//above ground
else if(centerPos.z == 6)
{
minRangeZ = 0;
maxRangeZ = 8;
}
else if(centerPos.z == 7)
{
minRangeZ = 0;
maxRangeZ = 9;
}
else
{
minRangeZ = 0;
maxRangeZ = 7;
}
}
else
{
minRangeZ = centerPos.z;
maxRangeZ = centerPos.z;
}
getSpectatorsInternal(list, centerPos, minRangeX, maxRangeX, minRangeY, maxRangeY, minRangeZ, maxRangeZ, onlyPlayers);
if(cacheResult)
spectatorCache[centerPos].reset(new SpectatorVec(list));
}
}
const SpectatorVec& Map::getSpectators(const Position& centerPos)
{
if(centerPos.z >= MAP_MAX_LAYERS)
{
boost::shared_ptr<SpectatorVec> p(new SpectatorVec());
SpectatorVec& list = *p;
return list;
}
SpectatorCache::iterator it = spectatorCache.find(centerPos);
if(it != spectatorCache.end())
return *it->second;
boost::shared_ptr<SpectatorVec> p(new SpectatorVec());
spectatorCache[centerPos] = p;
SpectatorVec& list = *p;
int32_t minRangeX = -maxViewportX;
int32_t maxRangeX = maxViewportX;
int32_t minRangeY = -maxViewportY;
int32_t maxRangeY = maxViewportY;
int32_t minRangeZ, maxRangeZ;
if(centerPos.z > 7)
{
//underground
//8->15
minRangeZ = std::max<int32_t>(centerPos.z - 2, 0);
maxRangeZ = std::min<int32_t>(centerPos.z + 2, MAP_MAX_LAYERS - 1);
}
//above ground
else if(centerPos.z == 6)
{
minRangeZ = 0;
maxRangeZ = 8;
}
else if(centerPos.z == 7)
{
minRangeZ = 0;
maxRangeZ = 9;
}
else
{
minRangeZ = 0;
maxRangeZ = 7;
}
getSpectatorsInternal(list, centerPos, minRangeX, maxRangeX, minRangeY, maxRangeY, minRangeZ, maxRangeZ, false);
return list;
}
void Map::clearSpectatorCache()
{
spectatorCache.clear();
}
bool Map::canThrowObjectTo(const Position& fromPos, const Position& toPos, bool checkLineOfSight /*= true*/,
int32_t rangex /*= Map::maxClientViewportX*/, int32_t rangey /*= Map::maxClientViewportY*/)
{
//z checks
//underground 8->15
//ground level and above 7->0
if((fromPos.z >= 8 && toPos.z < 8) || (toPos.z >= 8 && fromPos.z < 8))
return false;
int32_t deltaz = std::abs(fromPos.z - toPos.z);
if(deltaz > 2)
return false;
int32_t deltax = std::abs(fromPos.x - toPos.x);
int32_t deltay = std::abs(fromPos.y - toPos.y);
//distance checks
if(deltax - deltaz > rangex || deltay - deltaz > rangey)
return false;
if(!checkLineOfSight)
return true;
return isSightClear(fromPos, toPos, false);
}
bool Map::checkSightLine(const Position& fromPos, const Position& toPos) const
{
if(fromPos == toPos)
return true;
Position start(fromPos.z > toPos.z ? toPos : fromPos);
Position destination(fromPos.z > toPos.z ? fromPos : toPos);
const int8_t mx = start.x < destination.x ? 1 : start.x == destination.x ? 0 : -1;
const int8_t my = start.y < destination.y ? 1 : start.y == destination.y ? 0 : -1;
int32_t A = destination.y - start.y;
int32_t B = start.x - destination.x;
int32_t C = -(A*destination.x + B*destination.y);
while(!Position::areInRange<0,0,15>(start, destination))
{
int32_t move_hor = std::abs(A * (start.x + mx) + B * (start.y) + C);
int32_t move_ver = std::abs(A * (start.x) + B * (start.y + my) + C);
int32_t move_cross = std::abs(A * (start.x + mx) + B * (start.y + my) + C);
if(start.y != destination.y && (start.x == destination.x || move_hor > move_ver || move_hor > move_cross))
start.y += my;
if(start.x != destination.x && (start.y == destination.y || move_ver > move_hor || move_ver > move_cross))
start.x += mx;
const Tile* tile = const_cast<Map*>(this)->getTile(start.x, start.y, start.z);
if(tile && tile->hasProperty(BLOCKPROJECTILE))
return false;
}
// now we need to perform a jump between floors to see if everything is clear (literally)
while(start.z != destination.z)
{
const Tile* tile = const_cast<Map*>(this)->getTile(start.x, start.y, start.z);
if(tile && tile->getThingCount() > 0)
return false;
start.z++;
}
return true;
}
bool Map::isSightClear(const Position& fromPos, const Position& toPos, bool floorCheck) const
{
if(floorCheck && fromPos.z != toPos.z)
return false;
// Cast two converging rays and see if either yields a result.
return checkSightLine(fromPos, toPos) || checkSightLine(toPos, fromPos);
}
const Tile* Map::canWalkTo(const Creature* creature, const Position& pos)
{
int32_t walkCache = creature->getWalkCache(pos);
if(walkCache == 0)
return NULL;
else if(walkCache == 1)
return getTile(pos);
//used for none-cached tiles
Tile* tile = getTile(pos);
if(creature->getTile() != tile)
{
if(!tile || tile->__queryAdd(0, creature, 1, FLAG_PATHFINDING | FLAG_IGNOREFIELDDAMAGE) != RET_NOERROR)
return NULL;
}
return tile;
}
bool Map::getPathTo(const Creature* creature, const Position& destPos,
std::list<Direction>& listDir, int32_t maxSearchDist /*= -1*/)
{
if(canWalkTo(creature, destPos) == NULL)
return false;
listDir.clear();
Position startPos = destPos;
Position endPos = creature->getPosition();
if(startPos.z != endPos.z)
return false;
OTSERV_HASH_MAP<uint32_t, AStarNode*> nodeTable;
AStarNodes nodes;
AStarNode* startNode = nodes.createOpenNode();
nodeTable[(startPos.x * 0xFFFF) + startPos.y] = startNode;
startNode->x = startPos.x;
startNode->y = startPos.y;
startNode->g = 0;
startNode->h = nodes.getEstimatedDistance(startPos.x, startPos.y, endPos.x, endPos.y);
startNode->f = startNode->h;
startNode->parent = NULL;
Position pos;
pos.z = startPos.z;
static int32_t neighbourOrderList[8][2] =
{
{-1, 0},
{0, 1},
{1, 0},
{0, -1},
//diagonal
{-1, -1},
{1, -1},
{1, 1},
{-1, 1},
};
const Tile* tile = NULL;
AStarNode* found = NULL;
while(maxSearchDist != -1 || nodes.countClosedNodes() < 100)
{
AStarNode* n = nodes.getBestNode();
if(!n)
{
listDir.clear();
return false; //no path found
}
if(n->x == endPos.x && n->y == endPos.y)
{
found = n;
break;
}
else
{
for(int i = 0; i < 8; ++i)
{
pos.x = n->x + neighbourOrderList[i][0];
pos.y = n->y + neighbourOrderList[i][1];
bool outOfRange = false;
if(maxSearchDist != -1 && (std::abs(endPos.x - pos.x) > maxSearchDist ||
std::abs(endPos.y - pos.y) > maxSearchDist))
{
outOfRange = true;
}
if(!outOfRange && (tile = canWalkTo(creature, pos)))
{
//The cost (g) for this neighbour
int32_t cost = nodes.getMapWalkCost(creature, n, tile, pos);
int32_t extraCost = nodes.getTileWalkCost(creature, tile);
int32_t newg = n->g + cost + extraCost;
uint32_t tableIndex = (pos.x * 0xFFFF) + pos.y;
//Check if the node is already in the closed/open list
//If it exists and the nodes already on them has a lower cost (g) then we can ignore this neighbour node
AStarNode* neighbourNode;
OTSERV_HASH_MAP<uint32_t, AStarNode*>::iterator it = nodeTable.find(tableIndex);
if(it != nodeTable.end())
neighbourNode = it->second;
else
neighbourNode = NULL;
if(neighbourNode)
{
if(neighbourNode->g <= newg)
continue; //The node on the closed/open list is cheaper than this one
nodes.openNode(neighbourNode);
}
else
{
//Does not exist in the open/closed list, create a new node
neighbourNode = nodes.createOpenNode();
if(!neighbourNode)
{
//seems we ran out of nodes
listDir.clear();
return false;
}
nodeTable[tableIndex] = neighbourNode;
neighbourNode->x = pos.x;
neighbourNode->y = pos.y;
}
//This node is the best node so far with this state
neighbourNode->parent = n;
neighbourNode->g = newg;
neighbourNode->h = nodes.getEstimatedDistance(pos.x, pos.y, endPos.x, endPos.y);
neighbourNode->f = newg + neighbourNode->h;
}
}
nodes.closeNode(n);
}
}
int32_t prevx = endPos.x;
int32_t prevy = endPos.y;
int32_t dx, dy;
while(found)
{
pos.x = found->x;
pos.y = found->y;
found = found->parent;
dx = pos.x - prevx;
dy = pos.y - prevy;
prevx = pos.x;
prevy = pos.y;
if(dx == -1 && dy == -1)
listDir.push_back(NORTHWEST);
else if(dx == 1 && dy == -1)
listDir.push_back(NORTHEAST);
else if(dx == -1 && dy == 1)
listDir.push_back(SOUTHWEST);
else if(dx == 1 && dy == 1)
listDir.push_back(SOUTHEAST);
else if(dx == -1)
listDir.push_back(WEST);
else if(dx == 1)
listDir.push_back(EAST);
else if(dy == -1)
listDir.push_back(NORTH);
else if(dy == 1)
listDir.push_back(SOUTH);
}
return !listDir.empty();
}
bool Map::getPathMatching(const Creature* creature, std::list<Direction>& dirList,
const FrozenPathingConditionCall& pathCondition, const FindPathParams& fpp)
{
dirList.clear();
Position startPos = creature->getPosition();
Position endPos;
OTSERV_HASH_MAP<uint32_t, AStarNode*> nodeTable;
AStarNodes nodes;
AStarNode* startNode = nodes.createOpenNode();
nodeTable[(startPos.x * 0xFFFF) + startPos.y] = startNode;
startNode->x = startPos.x;
startNode->y = startPos.y;
startNode->f = 0;
startNode->parent = NULL;
Position pos;
pos.z = startPos.z;
int32_t bestMatch = 0;
static int32_t neighbourOrderList[8][2] =
{
{-1, 0},
{0, 1},
{1, 0},
{0, -1},
//diagonal
{-1, -1},
{1, -1},
{1, 1},
{-1, 1},
};
const Tile* tile = NULL;
AStarNode* found = NULL;
while(fpp.maxSearchDist != -1 || nodes.countClosedNodes() < 100)
{
AStarNode* n = nodes.getBestNode();
if(!n)
{
if(found)
{
//not quite what we want, but we found something
break;
}
dirList.clear();
return false; //no path found
}
if(pathCondition(startPos, Position(n->x, n->y, startPos.z), fpp, bestMatch))
{
found = n;
endPos = Position(n->x, n->y, startPos.z);
if(bestMatch == 0)
break;
}
int32_t dirCount = (fpp.allowDiagonal ? 8 : 4);
for(int32_t i = 0; i < dirCount; ++i)
{
pos.x = n->x + neighbourOrderList[i][0];
pos.y = n->y + neighbourOrderList[i][1];
bool inRange = true;
if(fpp.maxSearchDist != -1 && (std::abs(startPos.x - pos.x) > fpp.maxSearchDist ||
std::abs(startPos.y - pos.y) > fpp.maxSearchDist))
{
inRange = false;
}
if(fpp.keepDistance)
{
if(!pathCondition.isInRange(startPos, pos, fpp))
inRange = false;
}
if(inRange && (tile = canWalkTo(creature, pos)))
{
//The cost (g) for this neighbour
int32_t cost = nodes.getMapWalkCost(creature, n, tile, pos);
int32_t extraCost = nodes.getTileWalkCost(creature, tile);
int32_t newf = n->f + cost + extraCost;
uint32_t tableIndex = (pos.x * 0xFFFF) + pos.y;
//Check if the node is already in the closed/open list
//If it exists and the nodes already on them has a lower cost (g) then we can ignore this neighbour node
AStarNode* neighbourNode;
OTSERV_HASH_MAP<uint32_t, AStarNode*>::iterator it = nodeTable.find(tableIndex);
if(it != nodeTable.end())
neighbourNode = it->second;
else
neighbourNode = NULL;
if(neighbourNode)
{
if(neighbourNode->f <= newf)
{
//The node on the closed/open list is cheaper than this one
continue;
}
nodes.openNode(neighbourNode);
}
else
{
//Does not exist in the open/closed list, create a new node
neighbourNode = nodes.createOpenNode();
if(!neighbourNode)
{
if(found)
{
//not quite what we want, but we found something
break;
}
//seems we ran out of nodes
dirList.clear();
return false;
}
nodeTable[tableIndex] = neighbourNode;
neighbourNode->x = pos.x;
neighbourNode->y = pos.y;
}
//This node is the best node so far with this state
neighbourNode->parent = n;
neighbourNode->f = newf;
}
}
nodes.closeNode(n);
}
int32_t prevx = endPos.x;
int32_t prevy = endPos.y;
int32_t dx, dy;
if(!found)
return false;
found = found->parent;
while(found)
{
pos.x = found->x;
pos.y = found->y;
dx = pos.x - prevx;
dy = pos.y - prevy;
prevx = pos.x;
prevy = pos.y;
if(dx == 1 && dy == 1)
dirList.push_front(NORTHWEST);
else if(dx == -1 && dy == 1)
dirList.push_front(NORTHEAST);
else if(dx == 1 && dy == -1)
dirList.push_front(SOUTHWEST);
else if(dx == -1 && dy == -1)
dirList.push_front(SOUTHEAST);
else if(dx == 1)
dirList.push_front(WEST);
else if(dx == -1)
dirList.push_front(EAST);
else if(dy == 1)
dirList.push_front(NORTH);
else if(dy == -1)
dirList.push_front(SOUTH);
found = found->parent;
}
return true;
}
//*********** AStarNodes *************
AStarNodes::AStarNodes()
{
curNode = 0;
openNodes.reset();
}
AStarNode* AStarNodes::createOpenNode()
{
if(curNode >= MAX_NODES)
return NULL;
uint32_t ret_node = curNode;
curNode++;
openNodes[ret_node] = 1;
return &nodes[ret_node];
}
AStarNode* AStarNodes::getBestNode()
{
if(curNode == 0)
return NULL;
int best_node_f = 100000;
uint32_t best_node = 0;
bool found = false;
for(uint32_t i = 0; i < curNode; i++)
{
if(nodes[i].f < best_node_f && openNodes[i] == 1)
{
found = true;