Olá galerinha, queria pedir a ajuda de vcs, queria que alguém ajudasse a criar um Código C++ para limitar o alcance da vista de tiles do player, tipo, abaixar mais o Zoom da visão do player para ele ver mais coisas ao redor dele, que nem o Otpokemon, o client deles é todo aberto, ele n tem as barras laterais e o zoom é bem bom, sendo que quando eu tiro as barras laterais do meu Otclient ele fica com uns bugs pretos no lugar que era as barras laterais, isso acontece devido o limite de tiles que o player pode ver ao alcance dele, ouvir falar em outro forum que ficam localizado no Protocolgame.cpp das sources do server e no map.h das sources do server, e no otclient somente no Map.cpp. vou deixar eles aqui para vcs verem e tentar me ajudar.
////////////////////////////////////////////////////////////////////////
// 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 3 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, see <http://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////
#ifndef __MAP__
#define __MAP__
#include "tools.h"
#include "fileloader.h"
#include "position.h"
#include "waypoints.h"
#include "tile.h"
class Creature;
class Player;
class Game;
class Tile;
class Map;
struct FindPathParams;
struct AStarNode
{
uint16_t x, y;
AStarNode* parent;
int32_t f, g, h;
};
using boost::shared_ptr;
#define MAP_MAX_LAYERS 16
#define MAX_NODES 512
#define GET_NODE_INDEX(a) (a - &nodes[0])
#define MAP_NORMALWALKCOST 10
#define MAP_DIAGONALWALKCOST 25
class AStarNodes
{
public:
AStarNodes();
virtual ~AStarNodes() {}
void openNode(AStarNode* node);
void closeNode(AStarNode* node);
uint32_t countOpenNodes();
uint32_t countClosedNodes();
AStarNode* getBestNode();
AStarNode* createOpenNode();
AStarNode* getNodeInList(uint16_t x, uint16_t y);
bool isInList(uint16_t x, uint16_t y);
int32_t getEstimatedDistance(uint16_t x, uint16_t y, uint16_t xGoal, uint16_t yGoal);
int32_t getMapWalkCost(const Creature* creature, AStarNode* node,
const Tile* neighbourTile, const Position& neighbourPos);
static int32_t getTileWalkCost(const Creature* creature, const Tile* tile);
private:
AStarNode nodes[MAX_NODES];
std::bitset<MAX_NODES> openNodes;
uint32_t curNode;
};
template<class T> class lessPointer: public std::binary_function<T*, T*, bool>
{
public:
bool operator()(T*& t1, T*& t2) {return *t1 < *t2;}
};
#define FLOOR_BITS 3
#define FLOOR_SIZE (1 << FLOOR_BITS)
#define FLOOR_MASK (FLOOR_SIZE - 1)
struct Floor
{
Floor();
Tile* tiles[FLOOR_SIZE][FLOOR_SIZE];
};
class FrozenPathingConditionCall;
class QTreeLeafNode;
class QTreeNode
{
public:
QTreeNode();
virtual ~QTreeNode();
bool isLeaf() const {return m_isLeaf;}
QTreeLeafNode* getLeaf(uint16_t x, uint16_t y);
static QTreeLeafNode* getLeafStatic(QTreeNode* root, uint16_t x, uint16_t y);
QTreeLeafNode* createLeaf(uint16_t x, uint16_t y, uint16_t level);
protected:
bool m_isLeaf;
QTreeNode* m_child[4];
friend class Map;
};
class QTreeLeafNode : public QTreeNode
{
public:
QTreeLeafNode();
virtual ~QTreeLeafNode();
Floor* createFloor(uint16_t z);
Floor* getFloor(uint16_t z){return m_array[z];}
QTreeLeafNode* stepSouth(){return m_leafS;}
QTreeLeafNode* stepEast(){return m_leafE;}
void addCreature(Creature* c);
void removeCreature(Creature* c);
protected:
static bool newLeaf;
QTreeLeafNode* m_leafS;
QTreeLeafNode* m_leafE;
Floor* m_array[MAP_MAX_LAYERS];
CreatureVector creatureList;
friend class Map;
friend class QTreeNode;
};
/**
* Map class.
* Holds all the actual map-data
*/
class Map
{
public:
Map();
virtual ~Map() {}
static const int32_t maxViewportX = 11; //min value: maxClientViewportX + 1
static const int32_t maxViewportY = 11; //min value: maxClientViewportY + 1
static const int32_t maxClientViewportX = 8;
static const int32_t maxClientViewportY = 6;
/**
* Load a map.
* \returns true if the map was loaded successfully
*/
bool loadMap(const std::string& identifier);
/**
* Save a map.
* \param identifier file/database to save to
* \returns true if the map was saved successfully
*/
bool saveMap();
/**
* Get a single tile.
* \returns A pointer to that tile.
*/
Tile* getTile(int32_t x, int32_t y, int32_t z);
Tile* getTile(const Position& pos) {return getTile(pos.x, pos.y, pos.z);}
/**
* Set a single tile.
* \param a tile to set for the position
*/
void setTile(uint16_t _x, uint16_t _y, uint16_t _z, Tile* newTile);
void setTile(const Position& pos, Tile* newTile) {setTile(pos.x, pos.y, pos.z, newTile);}
/**
* Place a creature on the map
* \param pos The position to place the creature
* \param creature Creature to place on the map
* \param extendedPos If true, the creature will in first-hand be placed 2 tiles away
* \param forceLogin If true, placing the creature will not fail becase of obstacles (creatures/chests)
*/
bool placeCreature(const Position& centerPos, Creature* creature, bool extendedPos = false, bool forceLogin = false);
/**
* Remove a creature from the map.
* \param c Creature pointer to the creature to remove
*/
bool removeCreature(Creature* c);
/**
* Checks if you can throw an object to that position
* \param fromPos from Source point
* \param toPos Destination point
* \param rangex maximum allowed range horizontially
* \param rangey maximum allowed range vertically
* \param checkLineOfSight checks if there is any blocking objects in the way
* \returns The result if you can throw there or not
*/
bool canThrowObjectTo(const Position& fromPos, const Position& toPos, bool checkLineOfSight = true,
int32_t rangex = Map::maxClientViewportX, int32_t rangey = Map::maxClientViewportY);
/**
* Checks if path is clear from fromPos to toPos
* Notice: This only checks a straight line if the path is clear, for path finding use getPathTo.
* \param fromPos from Source point
* \param toPos Destination point
* \param floorCheck if true then view is not clear if fromPos.z is not the same as toPos.z
* \returns The result if there is no obstacles
*/
bool isSightClear(const Position& fromPos, const Position& toPos, bool floorCheck) const;
bool checkSightLine(const Position& fromPos, const Position& toPos) const;
/**
* Get the path to a specific position on the map.
* \param creature The creature that wants a path
* \param destPos The position we want a path calculated to
* \param listDir contains a list of directions to the destination
* \param maxDist Maximum distance from our current position to search, default: -1 (no limit)
* \returns returns true if a path was found
*/
bool getPathTo(const Creature* creature, const Position& destPos,
std::list<Direction>& listDir, int32_t maxDist = -1);
bool getPathMatching(const Creature* creature, std::list<Direction>& dirList,
const FrozenPathingConditionCall& pathCondition, const FindPathParams& fpp);
QTreeLeafNode* getLeaf(uint16_t x, uint16_t y) {return root.getLeaf(x, y);}
const Tile* canWalkTo(const Creature* creature, const Position& pos);
Waypoints waypoints;
protected:
QTreeNode root;
uint32_t mapWidth, mapHeight;
std::string spawnfile, housefile;
StringVec descriptions;
SpectatorCache spectatorCache;
void clearSpectatorCache() {spectatorCache.clear();}
// Actually scans the map for spectators
void getSpectatorsInternal(SpectatorVec& list, const Position& centerPos, bool checkforduplicate,
int32_t minRangeX, int32_t maxRangeX, int32_t minRangeY, int32_t maxRangeY,
int32_t minRangeZ, int32_t maxRangeZ);
// Use this when a custom spectator vector is needed, this support many
// more parameters than the heavily cached version below.
void getSpectators(SpectatorVec& list, const Position& centerPos, bool checkforduplicate = false, bool multifloor = false,
int32_t minRangeX = 0, int32_t maxRangeX = 0, int32_t minRangeY = 0, int32_t maxRangeY = 0);
// The returned SpectatorVec is a temporary and should not be kept around
// Take special heed in that the vector will be destroyed if any function
// that calls clearSpectatorCache is called.
const SpectatorVec& getSpectators(const Position& centerPos);
friend class Game;
friend class IOMap;
};
inline void QTreeLeafNode::addCreature(Creature* c)
{
creatureList.push_back(c);
}
inline void QTreeLeafNode::removeCreature(Creature* c)
{
CreatureVector::iterator it = std::find(creatureList.begin(), creatureList.end(), c);
assert(it != creatureList.end());
creatureList.erase(it);
}
#endif
Map.cpp Do Otclient
/*
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "map.h"
#include "game.h"
#include "localplayer.h"
#include "tile.h"
#include "item.h"
#include "missile.h"
#include "statictext.h"
#include "mapview.h"
#include "minimap.h"
#include <framework/core/eventdispatcher.h>
#include <framework/core/application.h>
Map g_map;
TilePtr Map::m_nulltile;
void Map::init()
{
resetAwareRange();
m_animationFlags |= Animation_Show;
}
void Map::terminate()
{
clean();
}
void Map::addMapView(const MapViewPtr& mapView)
{
m_mapViews.push_back(mapView);
}
void Map::removeMapView(const MapViewPtr& mapView)
{
auto it = std::find(m_mapViews.begin(), m_mapViews.end(), mapView);
if(it != m_mapViews.end())
m_mapViews.erase(it);
}
void Map::notificateTileUpdate(const Position& pos)
{
if(!pos.isMapPosition())
return;
for(const MapViewPtr& mapView : m_mapViews)
mapView->onTileUpdate(pos);
g_minimap.updateTile(pos, getTile(pos));
}
void Map::clean()
{
cleanDynamicThings();
for(int i=0;i<=Otc::MAX_Z;++i)
m_tileBlocks[i].clear();
m_waypoints.clear();
g_towns.clear();
g_houses.clear();
g_creatures.clearSpawns();
m_tilesRect = Rect(65534, 65534, 0, 0);
}
void Map::cleanDynamicThings()
{
for(const auto& pair : m_knownCreatures) {
const CreaturePtr& creature = pair.second;
removeThing(creature);
}
m_knownCreatures.clear();
for(int i=0;i<=Otc::MAX_Z;++i)
m_floorMissiles[i].clear();
cleanTexts();
}
void Map::cleanTexts()
{
m_animatedTexts.clear();
m_staticTexts.clear();
}
void Map::addThing(const ThingPtr& thing, const Position& pos, int stackPos)
{
if(!thing)
return;
if(thing->isItem() || thing->isCreature() || thing->isEffect()) {
const TilePtr& tile = getOrCreateTile(pos);
if(tile)
tile->addThing(thing, stackPos);
} else {
if(thing->isMissile()) {
m_floorMissiles[pos.z].push_back(thing->static_self_cast<Missile>());
thing->onAppear();
} else if(thing->isAnimatedText()) {
// this code will stack animated texts of the same color
AnimatedTextPtr animatedText = thing->static_self_cast<AnimatedText>();
AnimatedTextPtr prevAnimatedText;
bool merged = false;
for(auto other : m_animatedTexts) {
if(other->getPosition() == pos) {
prevAnimatedText = other;
if(other->merge(animatedText)) {
merged = true;
break;
}
}
}
if(!merged) {
if(prevAnimatedText) {
Point offset = prevAnimatedText->getOffset();
float t = prevAnimatedText->getTimer().ticksElapsed();
if(t < Otc::ANIMATED_TEXT_DURATION / 4.0) { // didnt move 12 pixels
int y = 12 - 48 * t / (float)Otc::ANIMATED_TEXT_DURATION;
offset += Point(0, y);
}
offset.y = std::min<int>(offset.y, 12);
animatedText->setOffset(offset);
}
m_animatedTexts.push_back(animatedText);
}
} else if(thing->isStaticText()) {
StaticTextPtr staticText = thing->static_self_cast<StaticText>();
bool mustAdd = true;
for(auto other : m_staticTexts) {
// try to combine messages
if(other->getPosition() == pos && other->addMessage(staticText->getName(), staticText->getMessageMode(), staticText->getFirstMessage())) {
mustAdd = false;
break;
}
}
if(mustAdd)
m_staticTexts.push_back(staticText);
else
return;
}
thing->setPosition(pos);
thing->onAppear();
}
notificateTileUpdate(pos);
}
ThingPtr Map::getThing(const Position& pos, int stackPos)
{
if(TilePtr tile = getTile(pos))
return tile->getThing(stackPos);
return nullptr;
}
bool Map::removeThing(const ThingPtr& thing)
{
if(!thing)
return false;
bool ret = false;
if(thing->isMissile()) {
MissilePtr missile = thing->static_self_cast<Missile>();
int z = missile->getPosition().z;
auto it = std::find(m_floorMissiles[z].begin(), m_floorMissiles[z].end(), missile);
if(it != m_floorMissiles[z].end()) {
m_floorMissiles[z].erase(it);
ret = true;
}
} else if(thing->isAnimatedText()) {
AnimatedTextPtr animatedText = thing->static_self_cast<AnimatedText>();
auto it = std::find(m_animatedTexts.begin(), m_animatedTexts.end(), animatedText);
if(it != m_animatedTexts.end()) {
m_animatedTexts.erase(it);
ret = true;
}
} else if(thing->isStaticText()) {
StaticTextPtr staticText = thing->static_self_cast<StaticText>();
auto it = std::find(m_staticTexts.begin(), m_staticTexts.end(), staticText);
if(it != m_staticTexts.end()) {
m_staticTexts.erase(it);
ret = true;
}
} else if(const TilePtr& tile = thing->getTile())
ret = tile->removeThing(thing);
notificateTileUpdate(thing->getPosition());
return ret;
}
bool Map::removeThingByPos(const Position& pos, int stackPos)
{
if(TilePtr tile = getTile(pos))
return removeThing(tile->getThing(stackPos));
return false;
}
void Map::colorizeThing(const ThingPtr& thing, const Color& color)
{
if(!thing)
return;
if(thing->isItem())
thing->static_self_cast<Item>()->setColor(color);
else if(thing->isCreature()) {
const TilePtr& tile = thing->getTile();
assert(tile);
const ThingPtr& topThing = tile->getTopThing();
assert(topThing);
topThing->static_self_cast<Item>()->setColor(color);
}
}
void Map::removeThingColor(const ThingPtr& thing)
{
if(!thing)
return;
if(thing->isItem())
thing->static_self_cast<Item>()->setColor(Color::alpha);
else if(thing->isCreature()) {
const TilePtr& tile = thing->getTile();
assert(tile);
const ThingPtr& topThing = tile->getTopThing();
assert(topThing);
topThing->static_self_cast<Item>()->setColor(Color::alpha);
}
}
StaticTextPtr Map::getStaticText(const Position& pos)
{
for(auto staticText : m_staticTexts) {
// try to combine messages
if(staticText->getPosition() == pos)
return staticText;
}
return nullptr;
}
const TilePtr& Map::createTile(const Position& pos)
{
if(!pos.isMapPosition())
return m_nulltile;
if(pos.x < m_tilesRect.left())
m_tilesRect.setLeft(pos.x);
if(pos.y < m_tilesRect.top())
m_tilesRect.setTop(pos.y);
if(pos.x > m_tilesRect.right())
m_tilesRect.setRight(pos.x);
if(pos.y > m_tilesRect.bottom())
m_tilesRect.setBottom(pos.y);
TileBlock& block = m_tileBlocks[pos.z][getBlockIndex(pos)];
return block.create(pos);
}
template <typename... Items>
const TilePtr& Map::createTileEx(const Position& pos, const Items&... items)
{
if(!pos.isValid())
return m_nulltile;
const TilePtr& tile = getOrCreateTile(pos);
auto vec = {items...};
for(auto it : vec)
addThing(it, pos);
return tile;
}
const TilePtr& Map::getOrCreateTile(const Position& pos)
{
if(!pos.isMapPosition())
return m_nulltile;
if(pos.x < m_tilesRect.left())
m_tilesRect.setLeft(pos.x);
if(pos.y < m_tilesRect.top())
m_tilesRect.setTop(pos.y);
if(pos.x > m_tilesRect.right())
m_tilesRect.setRight(pos.x);
if(pos.y > m_tilesRect.bottom())
m_tilesRect.setBottom(pos.y);
TileBlock& block = m_tileBlocks[pos.z][getBlockIndex(pos)];
return block.getOrCreate(pos);
}
const TilePtr& Map::getTile(const Position& pos)
{
if(!pos.isMapPosition())
return m_nulltile;
auto it = m_tileBlocks[pos.z].find(getBlockIndex(pos));
if(it != m_tileBlocks[pos.z].end())
return it->second.get(pos);
return m_nulltile;
}
void Map::cleanTile(const Position& pos)
{
if(!pos.isMapPosition())
return;
auto it = m_tileBlocks[pos.z].find(getBlockIndex(pos));
if(it != m_tileBlocks[pos.z].end()) {
TileBlock& block = it->second;
if(const TilePtr& tile = block.get(pos)) {
tile->clean();
if(tile->canErase())
block.remove(pos);
notificateTileUpdate(pos);
}
}
for(auto it = m_staticTexts.begin();it != m_staticTexts.end() {
const StaticTextPtr& staticText = *it;
if(staticText->getPosition() == pos && staticText->getMessageMode() == Otc::MessageNone)
it = m_staticTexts.erase(it);
else
++it;
}
}
void Map::setShowZone(tileflags_t zone, bool show)
{
if(show)
m_zoneFlags |= (uint32)zone;
else
m_zoneFlags &= ~(uint32)zone;
}
void Map::setShowZones(bool show)
{
if(!show)
m_zoneFlags = 0;
else if(m_zoneFlags == 0)
m_zoneFlags = TILESTATE_HOUSE | TILESTATE_PROTECTIONZONE;
}
void Map::setZoneColor(tileflags_t zone, const Color& color)
{
if((m_zoneFlags & zone) == zone)
m_zoneColors[zone] = color;
}
void Map::setForceShowAnimations(bool force)
{
if(force) {
if(!(m_animationFlags & Animation_Force))
m_animationFlags |= Animation_Force;
} else
m_animationFlags &= ~Animation_Force;
}
bool Map::isForcingAnimations()
{
return (m_animationFlags & Animation_Force) == Animation_Force;
}
bool Map::isShowingAnimations()
{
return (m_animationFlags & Animation_Show) == Animation_Show;
}
void Map::setShowAnimations(bool show)
{
if(show) {
if(!(m_animationFlags & Animation_Show))
m_animationFlags |= Animation_Show;
} else
m_animationFlags &= ~Animation_Show;
}
void Map::beginGhostMode(float opacity)
{
g_painter->setOpacity(opacity);
}
void Map::endGhostMode()
{
g_painter->resetOpacity();
}
std::map<Position, ItemPtr> Map::findItemsById(uint16 clientId, uint32 max)
{
std::map<Position, ItemPtr> ret;
uint32 count = 0;
for(uint8_t z = 0; z <= Otc::MAX_Z; ++z) {
for(const auto& pair : m_tileBlocks[z]) {
const TileBlock& block = pair.second;
for(const TilePtr& tile : block.getTiles()) {
if(unlikely(!tile || tile->isEmpty()))
continue;
for(const ItemPtr& item : tile->getItems()) {
if(item->getId() == clientId) {
ret.insert(std::make_pair(tile->getPosition(), item));
if(++count >= max)
break;
}
}
}
}
}
return ret;
}
void Map::addCreature(const CreaturePtr& creature)
{
m_knownCreatures[creature->getId()] = creature;
}
CreaturePtr Map::getCreatureById(uint32 id)
{
auto it = m_knownCreatures.find(id);
if(it == m_knownCreatures.end())
return nullptr;
return it->second;
}
void Map::removeCreatureById(uint32 id)
{
if(id == 0)
return;
auto it = m_knownCreatures.find(id);
if(it != m_knownCreatures.end())
m_knownCreatures.erase(it);
}
void Map::removeUnawareThings()
{
// remove creatures from tiles that we are not aware of anymore
for(const auto& pair : m_knownCreatures) {
const CreaturePtr& creature = pair.second;
if(!isAwareOfPosition(creature->getPosition()))
removeThing(creature);
}
// remove static texts from tiles that we are not aware anymore
for(auto it = m_staticTexts.begin(); it != m_staticTexts.end() {
const StaticTextPtr& staticText = *it;
if(staticText->getMessageMode() == Otc::MessageNone && !isAwareOfPosition(staticText->getPosition()))
it = m_staticTexts.erase(it);
else
++it;
}
}
void Map::setCentralPosition(const Position& centralPosition)
{
if(m_centralPosition == centralPosition)
return;
m_centralPosition = centralPosition;
removeUnawareThings();
// this fixes local player position when the local player is removed from the map,
// the local player is removed from the map when there are too many creatures on his tile,
// so there is no enough stackpos to the server send him
g_dispatcher.addEvent([this] {
LocalPlayerPtr localPlayer = g_game.getLocalPlayer();
if(!localPlayer || localPlayer->getPosition() == m_centralPosition)
return;
TilePtr tile = localPlayer->getTile();
if(tile && tile->hasThing(localPlayer))
return;
Position oldPos = localPlayer->getPosition();
Position pos = m_centralPosition;
if(oldPos != pos) {
if(!localPlayer->isRemoved())
localPlayer->onDisappear();
localPlayer->setPosition(pos);
localPlayer->onAppear();
g_logger.debug("forced player position update");
}
});
for(const MapViewPtr& mapView : m_mapViews)
mapView->onMapCenterChange(centralPosition);
}
std::vector<CreaturePtr> Map::getSightSpectators(const Position& centerPos, bool multiFloor)
{
return getSpectatorsInRangeEx(centerPos, multiFloor, m_awareRange.left - 1, m_awareRange.right - 2, m_awareRange.top - 1, m_awareRange.bottom - 2);
}
std::vector<CreaturePtr> Map::getSpectators(const Position& centerPos, bool multiFloor)
{
return getSpectatorsInRangeEx(centerPos, multiFloor, m_awareRange.left, m_awareRange.right, m_awareRange.top, m_awareRange.bottom);
}
std::vector<CreaturePtr> Map::getSpectatorsInRange(const Position& centerPos, bool multiFloor, int xRange, int yRange)
{
return getSpectatorsInRangeEx(centerPos, multiFloor, xRange, xRange, yRange, yRange);
}
std::vector<CreaturePtr> Map::getSpectatorsInRangeEx(const Position& centerPos, bool multiFloor, int minXRange, int maxXRange, int minYRange, int maxYRange)
{
int minZRange = 0;
int maxZRange = 0;
std::vector<CreaturePtr> creatures;
if(multiFloor) {
minZRange = 0;
maxZRange = Otc::MAX_Z;
}
//TODO: optimize
//TODO: get creatures from other floors corretly
//TODO: delivery creatures in distance order
for(int iz=-minZRange; iz<=maxZRange; ++iz) {
for(int iy=-minYRange; iy<=maxYRange; ++iy) {
for(int ix=-minXRange; ix<=maxXRange; ++ix) {
TilePtr tile = getTile(centerPos.translated(ix,iy,iz));
if(!tile)
continue;
auto tileCreatures = tile->getCreatures();
creatures.insert(creatures.end(), tileCreatures.rbegin(), tileCreatures.rend());
}
}
}
return creatures;
}
bool Map::isLookPossible(const Position& pos)
{
TilePtr tile = getTile(pos);
return tile && tile->isLookPossible();
}
bool Map::isCovered(const Position& pos, int firstFloor)
{
// check for tiles on top of the postion
Position tilePos = pos;
while(tilePos.coveredUp() && tilePos.z >= firstFloor) {
TilePtr tile = getTile(tilePos);
// the below tile is covered when the above tile has a full ground
if(tile && tile->isFullGround())
return true;
}
return false;
}
bool Map::isCompletelyCovered(const Position& pos, int firstFloor)
{
const TilePtr& checkTile = getTile(pos);
Position tilePos = pos;
while(tilePos.coveredUp() && tilePos.z >= firstFloor) {
bool covered = true;
bool done = false;
// check in 2x2 range tiles that has no transparent pixels
for(int x=0;x<2 && !done;++x) {
for(int y=0;y<2 && !done;++y) {
const TilePtr& tile = getTile(tilePos.translated(-x, -y));
if(!tile || !tile->isFullyOpaque()) {
covered = false;
done = true;
} else if(x==0 && y==0 && (!checkTile || checkTile->isSingleDimension())) {
done = true;
}
}
}
if(covered)
return true;
}
return false;
}
bool Map::isAwareOfPosition(const Position& pos)
{
if(pos.z < getFirstAwareFloor() || pos.z > getLastAwareFloor())
return false;
Position groundedPos = pos;
while(groundedPos.z != m_centralPosition.z) {
if(groundedPos.z > m_centralPosition.z) {
if(groundedPos.x == 65535 || groundedPos.y == 65535) // When pos == 65535,65535,15 we cant go up to 65536,65536,14
break;
groundedPos.coveredUp();
}
else {
if(groundedPos.x == 0 || groundedPos.y == 0) // When pos == 0,0,0 we cant go down to -1,-1,1
break;
groundedPos.coveredDown();
}
}
return m_centralPosition.isInRange(groundedPos, m_awareRange.left,
m_awareRange.right,
m_awareRange.top,
m_awareRange.bottom);
}
void Map::setAwareRange(const AwareRange& range)
{
m_awareRange = range;
removeUnawareThings();
}
void Map::resetAwareRange()
{
AwareRange range;
range.left = 8;
range.top = 6;
range.bottom = 7;
range.right = 9;
setAwareRange(range);
}
int Map::getFirstAwareFloor()
{
if(m_centralPosition.z > Otc::SEA_FLOOR)
return m_centralPosition.z-Otc::AWARE_UNDEGROUND_FLOOR_RANGE;
else
return 0;
}
int Map::getLastAwareFloor()
{
if(m_centralPosition.z > Otc::SEA_FLOOR)
return std::min<int>(m_centralPosition.z+Otc::AWARE_UNDEGROUND_FLOOR_RANGE, (int)Otc::MAX_Z);
else
return Otc::SEA_FLOOR;
}
std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const Position& startPos, const Position& goalPos, int maxComplexity, int flags)
{
// pathfinding using A* search algorithm
// as described in http://en.wikipedia.org/wiki/A*_search_algorithm
struct Node {
Node(const Position& pos) : cost(0), totalCost(0), steps(0), pos(pos), prev(nullptr), dir(Otc::InvalidDirection), evaluated(false) { }
bool operator<(const Node& other) const { return totalCost < other.totalCost; }
float cost;
float totalCost;
int steps;
Position pos;
Node *prev;
Otc::Direction dir;
bool evaluated;
};
struct LessNode : std::binary_function<Node*, Node*, bool> {
bool operator()(Node* a, Node* b) const {
return b->totalCost < a->totalCost;
}
};
std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> ret;
std::vector<Otc::Direction>& dirs = std::get<0>(ret);
Otc::PathFindResult& result = std::get<1>(ret);
result = Otc::PathFindResultNoWay;
if(startPos == goalPos) {
result = Otc::PathFindResultSamePosition;
return ret;
}
if(startPos.z != goalPos.z) {
result = Otc::PathFindResultImpossible;
return ret;
}
// check the goal pos is walkable
if(g_map.isAwareOfPosition(goalPos)) {
const TilePtr goalTile = getTile(goalPos);
if(!goalTile || !goalTile->isWalkable()) {
return ret;
}
}
else {
const MinimapTile& goalTile = g_minimap.getTile(goalPos);
if(goalTile.hasFlag(MinimapTileNotWalkable)) {
return ret;
}
}
std::unordered_map<Position, Node*, PositionHasher> nodes;
std::priority_queue<Node*, std::vector<Node*>, LessNode> searchList;
Node *currentNode = new Node(startPos);
currentNode->pos = startPos;
nodes[startPos] = currentNode;
Node *foundNode = nullptr;
while(currentNode) {
if((int)nodes.size() > maxComplexity) {
result = Otc::PathFindResultTooFar;
break;
}
// path found
if(currentNode->pos == goalPos && (!foundNode || currentNode->cost < foundNode->cost))
foundNode = currentNode;
// cost too high
if(foundNode && currentNode->totalCost >= foundNode->cost)
break;
for(int i=-1;i<=1;++i) {
for(int j=-1;j<=1;++j) {
if(i == 0 && j == 0)
continue;
bool wasSeen = false;
bool hasCreature = false;
bool isNotWalkable = true;
bool isNotPathable = true;
int speed = 100;
Position neighborPos = currentNode->pos.translated(i, j);
if(g_map.isAwareOfPosition(neighborPos)) {
wasSeen = true;
if(const TilePtr& tile = getTile(neighborPos)) {
hasCreature = tile->hasCreature();
isNotWalkable = !tile->isWalkable();
isNotPathable = !tile->isPathable();
speed = tile->getGroundSpeed();
}
} else {
const MinimapTile& mtile = g_minimap.getTile(neighborPos);
wasSeen = mtile.hasFlag(MinimapTileWasSeen);
isNotWalkable = mtile.hasFlag(MinimapTileNotWalkable);
isNotPathable = mtile.hasFlag(MinimapTileNotPathable);
if(isNotWalkable || isNotPathable)
wasSeen = true;
speed = mtile.getSpeed();
}
float walkFactor = 0;
if(neighborPos != goalPos) {
if(!(flags & Otc::PathFindAllowNotSeenTiles) && !wasSeen)
continue;
if(wasSeen) {
if(!(flags & Otc::PathFindAllowCreatures) && hasCreature)
continue;
if(!(flags & Otc::PathFindAllowNonPathable) && isNotPathable)
continue;
if(!(flags & Otc::PathFindAllowNonWalkable) && isNotWalkable)
continue;
}
} else {
if(!(flags & Otc::PathFindAllowNotSeenTiles) && !wasSeen)
continue;
if(wasSeen) {
if(!(flags & Otc::PathFindAllowNonWalkable) && isNotWalkable)
continue;
}
}
Otc::Direction walkDir = currentNode->pos.getDirectionFromPosition(neighborPos);
if(walkDir >= Otc::NorthEast)
walkFactor += 3.0f;
else
walkFactor += 1.0f;
float cost = currentNode->cost + (speed * walkFactor) / 100.0f;
Node *neighborNode;
if(nodes.find(neighborPos) == nodes.end()) {
neighborNode = new Node(neighborPos);
nodes[neighborPos] = neighborNode;
} else {
neighborNode = nodes[neighborPos];
if(neighborNode->cost < cost)
continue;
}
neighborNode->prev = currentNode;
neighborNode->cost = cost;
neighborNode->steps = currentNode->steps + 1;
neighborNode->totalCost = neighborNode->cost + neighborPos.distance(goalPos);
neighborNode->dir = walkDir;
neighborNode->evaluated = false;
searchList.push(neighborNode);
}
}
currentNode->evaluated = true;
currentNode = nullptr;
while(searchList.size() > 0 && !currentNode) {
Node *node = searchList.top();
searchList.pop();
if(!node->evaluated)
currentNode = node;
}
}
if(foundNode) {
currentNode = foundNode;
while(currentNode) {
dirs.push_back(currentNode->dir);
currentNode = currentNode->prev;
}
dirs.pop_back();
std::reverse(dirs.begin(), dirs.end());
result = Otc::PathFindResultOk;
}
for(auto it : nodes)
delete it.second;
return ret;
}
/* vim: set ts=4 sw=4 et: */
Pergunta
Developer Berg 65
Olá galerinha, queria pedir a ajuda de vcs, queria que alguém ajudasse a criar um Código C++ para limitar o alcance da vista de tiles do player, tipo, abaixar mais o Zoom da visão do player para ele ver mais coisas ao redor dele, que nem o Otpokemon, o client deles é todo aberto, ele n tem as barras laterais e o zoom é bem bom, sendo que quando eu tiro as barras laterais do meu Otclient ele fica com uns bugs pretos no lugar que era as barras laterais, isso acontece devido o limite de tiles que o player pode ver ao alcance dele, ouvir falar em outro forum que ficam localizado no Protocolgame.cpp das sources do server e no map.h das sources do server, e no otclient somente no Map.cpp. vou deixar eles aqui para vcs verem e tentar me ajudar.
Protocolgame.cpp server
Map.h Do Server
Map.cpp Do Otclient
Up
Up
Link para o comentário
Compartilhar em outros sites
21 respostass a esta questão
Posts Recomendados