2011-08-28 15:17:58 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010-2011 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.
|
|
|
|
*/
|
|
|
|
|
2011-08-15 16:11:24 +02:00
|
|
|
#include "map.h"
|
|
|
|
#include "game.h"
|
2011-08-15 23:02:52 +02:00
|
|
|
#include "localplayer.h"
|
2011-08-29 16:14:21 +02:00
|
|
|
#include "tile.h"
|
2011-08-15 16:11:24 +02:00
|
|
|
#include <framework/graphics/graphics.h>
|
|
|
|
#include <framework/graphics/framebuffer.h>
|
|
|
|
|
2011-08-15 23:02:52 +02:00
|
|
|
Map g_map;
|
|
|
|
|
2011-08-21 05:21:35 +02:00
|
|
|
void Map::draw(const Rect& rect)
|
2011-08-15 16:11:24 +02:00
|
|
|
{
|
|
|
|
if(!m_framebuffer)
|
2011-08-31 07:15:59 +02:00
|
|
|
m_framebuffer = FrameBufferPtr(new FrameBuffer(MAP_VISIBLE_WIDTH * NUM_TILE_PIXELS, MAP_VISIBLE_HEIGHT * NUM_TILE_PIXELS));
|
2011-08-15 16:11:24 +02:00
|
|
|
|
2011-08-28 18:02:26 +02:00
|
|
|
g_graphics.bindColor(Fw::white);
|
2011-08-15 16:11:24 +02:00
|
|
|
m_framebuffer->bind();
|
|
|
|
|
2011-08-31 01:39:14 +02:00
|
|
|
LocalPlayerPtr localPlayer = g_game.getLocalPlayer();
|
|
|
|
int walkOffsetX = localPlayer->getWalkOffsetX();
|
|
|
|
int walkOffsetY = localPlayer->getWalkOffsetY();
|
|
|
|
|
2011-08-31 07:15:59 +02:00
|
|
|
int maxFloor = getMaxVisibleFloor();
|
|
|
|
for(int z = MAX_Z-1; z >= maxFloor; --z) {
|
|
|
|
if(z < maxFloor)
|
2011-08-31 01:39:14 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
int zdif = m_centralPosition.z - z;
|
2011-08-31 07:15:59 +02:00
|
|
|
for(int y = 0; y < MAP_SIZE_Y; ++y) {
|
|
|
|
for(int x = 0; x < MAP_SIZE_X; ++x) {
|
|
|
|
Position tilePos(m_centralPosition.x + (x - PLAYER_OFFSET_X), m_centralPosition.y + (y - PLAYER_OFFSET_Y), m_centralPosition.z);
|
2011-08-31 02:28:06 +02:00
|
|
|
tilePos.coveredUp(m_centralPosition.z - z);
|
|
|
|
if(const TilePtr& tile = m_tiles[tilePos]) {
|
|
|
|
// skip tiles that are behind another tile
|
2011-08-31 07:15:59 +02:00
|
|
|
if(z > maxFloor && isCompletlyCovered(tilePos, maxFloor))
|
2011-08-31 02:28:06 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
int x = (7 + (tile->getPosition().x - m_centralPosition.x) - zdif) * NUM_TILE_PIXELS;
|
|
|
|
int y = (5 + (tile->getPosition().y - m_centralPosition.y) - zdif) * NUM_TILE_PIXELS;
|
|
|
|
tile->draw(x - walkOffsetX, y - walkOffsetY);
|
|
|
|
}
|
2011-08-15 16:11:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_framebuffer->unbind();
|
2011-08-20 22:30:41 +02:00
|
|
|
|
2011-08-28 18:02:26 +02:00
|
|
|
g_graphics.bindColor(Fw::white);
|
2011-08-21 05:21:35 +02:00
|
|
|
m_framebuffer->draw(rect);
|
|
|
|
|
|
|
|
// calculate stretch factor
|
2011-08-31 07:15:59 +02:00
|
|
|
float horizontalStretchFactor = rect.width() / (float)(MAP_VISIBLE_WIDTH * NUM_TILE_PIXELS);
|
|
|
|
float verticalStretchFactor = rect.height() / (float)(MAP_VISIBLE_HEIGHT * NUM_TILE_PIXELS);
|
2011-08-21 05:21:35 +02:00
|
|
|
|
|
|
|
// draw player names and health bars
|
2011-08-31 07:15:59 +02:00
|
|
|
for(int x = 0; x < MAP_VISIBLE_WIDTH; ++x) {
|
|
|
|
for(int y = 0; y < MAP_VISIBLE_HEIGHT; ++y) {
|
|
|
|
Position tilePos = Position(m_centralPosition.x + (x - PLAYER_OFFSET_X - 1), m_centralPosition.y + (y - PLAYER_OFFSET_Y - 1), m_centralPosition.z);
|
2011-08-31 01:39:14 +02:00
|
|
|
if(const TilePtr& tile = m_tiles[tilePos]) {
|
2011-08-28 18:31:01 +02:00
|
|
|
auto& creatures = tile->getCreatures();
|
2011-08-31 01:39:14 +02:00
|
|
|
|
|
|
|
if(creatures.size() == 0)
|
|
|
|
continue;
|
|
|
|
|
2011-08-21 21:09:23 +02:00
|
|
|
for(auto it = creatures.rbegin(), end = creatures.rend(); it != end; ++it) {
|
2011-08-31 01:39:14 +02:00
|
|
|
CreaturePtr creature = (*it)->asCreature();
|
2011-08-21 21:09:23 +02:00
|
|
|
|
2011-08-31 01:39:14 +02:00
|
|
|
int x = (7 + (tilePos.x - m_centralPosition.x))*NUM_TILE_PIXELS + 10 - tile->getDrawNextOffset();
|
|
|
|
int y = (5 + (tilePos.y - m_centralPosition.y))*NUM_TILE_PIXELS - 10 - tile->getDrawNextOffset();
|
2011-08-21 21:09:23 +02:00
|
|
|
|
2011-08-31 01:39:14 +02:00
|
|
|
if(creature != localPlayer) {
|
2011-08-26 17:44:29 +02:00
|
|
|
x += creature->getWalkOffsetX() - walkOffsetX;
|
|
|
|
y += creature->getWalkOffsetY() - walkOffsetY;
|
|
|
|
}
|
|
|
|
|
2011-08-31 07:15:59 +02:00
|
|
|
creature->drawInformation(rect.x() + x*horizontalStretchFactor, rect.y() + y*verticalStretchFactor, isCovered(tilePos, maxFloor));
|
2011-08-31 01:39:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Map::getMaxVisibleFloor()
|
|
|
|
{
|
|
|
|
Position tilePos = m_centralPosition;
|
|
|
|
tilePos.up();
|
|
|
|
TilePtr tile = m_tiles[tilePos];
|
|
|
|
if(tile)
|
|
|
|
return m_centralPosition.z;
|
|
|
|
|
|
|
|
tilePos = Position(m_centralPosition);
|
|
|
|
while(tilePos.z >= 0) {
|
|
|
|
tilePos.coveredUp();
|
|
|
|
tile = m_tiles[tilePos];
|
|
|
|
if(tile)
|
|
|
|
return tilePos.z + 1;
|
|
|
|
}
|
2011-08-31 07:15:59 +02:00
|
|
|
/*
|
|
|
|
int maxZ = MAP_SIZE_Z - 1;
|
|
|
|
int currentZ = m_centralPosition.z;
|
|
|
|
while(maxZ > currentZ) {
|
|
|
|
TilePtr tile = m_tiles[tilePos];
|
|
|
|
if(tilç
|
|
|
|
maxZ--;
|
|
|
|
};
|
|
|
|
|
|
|
|
for(int x = PLAYER_OFFSET_X-1; x <= PLAYER_OFFSET_X+1; ++x) {
|
|
|
|
for(int y = PLAYER_OFFSET_Y-1; y <= PLAYER_OFFSET_Y+1; ++y) {
|
|
|
|
if(x == PLAYER_OFFSET_X && y == PLAYER_OFFSET_Y)
|
|
|
|
continue;
|
|
|
|
for(int z = currentZ + 1; z <
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2011-08-26 17:44:29 +02:00
|
|
|
|
2011-08-31 01:39:14 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2011-08-26 17:44:29 +02:00
|
|
|
|
2011-08-31 01:39:14 +02:00
|
|
|
bool Map::isCovered(const Position& pos, int maxFloor)
|
|
|
|
{
|
|
|
|
Position tilePos = pos;
|
|
|
|
tilePos.coveredUp();
|
|
|
|
while(tilePos.z >= maxFloor) {
|
|
|
|
TilePtr tile = m_tiles[tilePos];
|
|
|
|
if(tile)
|
|
|
|
return true;
|
|
|
|
tilePos.coveredUp();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-21 21:09:23 +02:00
|
|
|
|
2011-08-31 01:39:14 +02:00
|
|
|
bool Map::isCompletlyCovered(const Position& pos, int maxFloor)
|
|
|
|
{
|
|
|
|
Position tilePos = pos;
|
|
|
|
tilePos.coveredUp();
|
|
|
|
while(tilePos.z >= maxFloor) {
|
|
|
|
bool covered = true;
|
|
|
|
for(int x=0;x<2;++x) {
|
|
|
|
for(int y=0;y<2;++y) {
|
|
|
|
TilePtr tile = m_tiles[tilePos + Position(-x, -y, 0)];
|
|
|
|
if(!tile || !tile->isOpaque()) {
|
|
|
|
covered = false;
|
|
|
|
break;
|
2011-08-21 21:09:23 +02:00
|
|
|
}
|
|
|
|
}
|
2011-08-21 05:21:35 +02:00
|
|
|
}
|
2011-08-31 01:39:14 +02:00
|
|
|
if(covered)
|
|
|
|
return true;
|
|
|
|
tilePos.coveredUp();
|
2011-08-21 05:21:35 +02:00
|
|
|
}
|
2011-08-31 01:39:14 +02:00
|
|
|
return false;
|
2011-08-15 16:11:24 +02:00
|
|
|
}
|
|
|
|
|
2011-08-24 05:58:23 +02:00
|
|
|
void Map::addThing(ThingPtr thing, int stackpos)
|
2011-08-16 07:47:35 +02:00
|
|
|
{
|
2011-08-17 07:04:45 +02:00
|
|
|
if(!thing)
|
|
|
|
return;
|
|
|
|
|
2011-08-17 06:45:55 +02:00
|
|
|
TilePtr& tile = m_tiles[thing->getPosition()];
|
|
|
|
if(!tile) {
|
2011-08-31 01:39:14 +02:00
|
|
|
tile = TilePtr(new Tile(thing->getPosition()));
|
2011-08-15 16:11:24 +02:00
|
|
|
}
|
2011-08-16 07:47:35 +02:00
|
|
|
|
2011-08-17 06:45:55 +02:00
|
|
|
tile->addThing(thing, stackpos);
|
2011-08-16 07:47:35 +02:00
|
|
|
|
2011-08-17 06:45:55 +02:00
|
|
|
if(const CreaturePtr& creature = thing->asCreature())
|
|
|
|
m_creatures[thing->getId()] = creature;
|
|
|
|
}
|
|
|
|
|
2011-08-24 05:58:23 +02:00
|
|
|
ThingPtr Map::getThing(const Position& pos, int stackpos)
|
2011-08-17 06:45:55 +02:00
|
|
|
{
|
|
|
|
if(const TilePtr& tile = m_tiles[pos]) {
|
|
|
|
return tile->getThing(stackpos);
|
2011-08-16 14:47:30 +02:00
|
|
|
}
|
2011-08-17 06:45:55 +02:00
|
|
|
return ThingPtr();
|
2011-08-16 14:47:30 +02:00
|
|
|
}
|
|
|
|
|
2011-08-24 05:58:23 +02:00
|
|
|
void Map::removeThing(const Position& pos, int stackpos)
|
2011-08-16 14:47:30 +02:00
|
|
|
{
|
2011-08-17 06:45:55 +02:00
|
|
|
if(TilePtr& tile = m_tiles[pos]) {
|
|
|
|
tile->removeThing(stackpos);
|
2011-08-15 16:11:24 +02:00
|
|
|
}
|
|
|
|
}
|
2011-08-16 14:47:30 +02:00
|
|
|
|
2011-08-17 07:04:45 +02:00
|
|
|
void Map::removeThingByPtr(ThingPtr thing)
|
|
|
|
{
|
|
|
|
if(!thing)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(TilePtr& tile = m_tiles[thing->getPosition()]) {
|
|
|
|
tile->removeThingByPtr(thing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-17 06:45:55 +02:00
|
|
|
void Map::clean()
|
|
|
|
{
|
|
|
|
m_tiles.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Map::cleanTile(const Position& pos)
|
|
|
|
{
|
|
|
|
if(TilePtr& tile = m_tiles[pos])
|
|
|
|
tile->clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
CreaturePtr Map::getCreatureById(uint32 id)
|
|
|
|
{
|
2011-08-29 16:14:21 +02:00
|
|
|
if(g_game.getLocalPlayer() && g_game.getLocalPlayer()->getId() == id)
|
2011-08-17 06:45:55 +02:00
|
|
|
return g_game.getLocalPlayer();
|
|
|
|
return m_creatures[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
void Map::removeCreatureById(uint32 id)
|
|
|
|
{
|
|
|
|
m_creatures.erase(id);
|
|
|
|
}
|
2011-08-31 01:39:14 +02:00
|
|
|
|
|
|
|
void Map::setCentralPosition(const Position& centralPosition)
|
|
|
|
{
|
|
|
|
m_centralPosition = centralPosition;
|
|
|
|
}
|