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)
|
|
|
|
m_framebuffer = FrameBufferPtr(new FrameBuffer(15*32, 11*32));
|
|
|
|
|
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-26 17:44:29 +02:00
|
|
|
LocalPlayerPtr player = g_game.getLocalPlayer();
|
|
|
|
Position playerPos = player->getPosition();
|
2011-08-29 02:38:26 +02:00
|
|
|
int walkOffsetX = player->getWalkOffsetX();
|
|
|
|
int walkOffsetY = player->getWalkOffsetY();
|
2011-08-15 16:11:24 +02:00
|
|
|
|
|
|
|
// player is above 7
|
2011-08-20 04:08:27 +02:00
|
|
|
|
2011-08-21 21:09:23 +02:00
|
|
|
int drawFloorStop = 0;
|
2011-08-15 23:02:52 +02:00
|
|
|
if(playerPos.z <= 7) {
|
2011-08-15 16:11:24 +02:00
|
|
|
|
|
|
|
// player pos it 8-6. check if we can draw upper floors.
|
2011-08-21 21:09:23 +02:00
|
|
|
|
2011-08-20 04:08:27 +02:00
|
|
|
|
|
|
|
// if there is a window on north, east, south or west
|
|
|
|
//Position direction[4] = {Position(0, -1, 0), Position(1, 0, 0), Position(0, 1, 0), Position(-1, 0, 0)};
|
|
|
|
for(int d = 0; d < 4; ++d) {
|
|
|
|
/*if(const TilePtr& tile = m_tiles[playerPos+direction[d]]) {
|
|
|
|
const ThingAttributes& thingAttributes = thing->getAttributes();
|
|
|
|
if(thingAttributes.lookThrough) {
|
|
|
|
drawFloorStop = playerPos.z - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we have something covering us, dont show floors above.
|
2011-08-15 16:11:24 +02:00
|
|
|
for(int jz = 6; jz >= 0; --jz) {
|
2011-08-16 07:47:35 +02:00
|
|
|
Position coverPos = Position(playerPos.x+(7-jz)-1, playerPos.y+(7-jz)-1, jz);
|
2011-08-17 06:45:55 +02:00
|
|
|
if(const TilePtr& tile = m_tiles[coverPos]) {
|
2011-08-24 05:58:23 +02:00
|
|
|
if(tile->getStackSize(3) > 0 && jz < playerPos.z) {
|
2011-08-16 07:47:35 +02:00
|
|
|
drawFloorStop = jz;
|
|
|
|
break;
|
2011-08-15 16:11:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int iz = 7; iz > 0; --iz) {
|
2011-08-16 07:47:35 +02:00
|
|
|
if(iz == drawFloorStop)
|
|
|
|
break;
|
2011-08-15 16:11:24 +02:00
|
|
|
|
2011-08-21 05:21:35 +02:00
|
|
|
// +1 in draws cause 64x64 items may affect view.
|
2011-08-20 04:08:27 +02:00
|
|
|
|
2011-08-26 17:44:29 +02:00
|
|
|
for(int step = 0; step < 2; ++step) {
|
|
|
|
for(int ix = -8+(playerPos.z-iz); ix < + 8+7; ++ix) {
|
|
|
|
for(int iy = -6+(playerPos.z-iz); iy < + 6+7; ++iy) {
|
|
|
|
Position itemPos = Position(playerPos.x + ix, playerPos.y + iy, iz);
|
|
|
|
if(const TilePtr& tile = m_tiles[itemPos])
|
|
|
|
tile->draw((ix + 7 - (playerPos.z-iz))*32 - walkOffsetX, (iy + 5 - (playerPos.z-iz))*32 - walkOffsetY, step);
|
|
|
|
}
|
2011-08-15 16:11:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// debug draws
|
2011-08-28 18:02:26 +02:00
|
|
|
g_graphics.bindColor(Fw::red);
|
2011-08-20 22:30:41 +02:00
|
|
|
g_graphics.drawBoundingRect(Rect(7*32, 5*32, 32, 32));
|
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
|
|
|
|
float horizontalStretchFactor = (rect.width() - rect.x()) / (float)(15*32);
|
|
|
|
float verticalStretchFactor = (rect.height() - rect.y()) / (float)(11*32);
|
|
|
|
|
|
|
|
// draw player names and health bars
|
|
|
|
for(int ix = -7; ix <= 7; ++ix) {
|
|
|
|
for(int iy = -5; iy <= 5; ++iy) {
|
|
|
|
Position itemPos = Position(playerPos.x + ix, playerPos.y + iy, playerPos.z);
|
2011-08-21 21:09:23 +02:00
|
|
|
if(const TilePtr& tile = m_tiles[itemPos]) {
|
2011-08-28 18:31:01 +02:00
|
|
|
auto& creatures = tile->getCreatures();
|
2011-08-21 21:09:23 +02:00
|
|
|
for(auto it = creatures.rbegin(), end = creatures.rend(); it != end; ++it) {
|
|
|
|
const ThingPtr& thing = *it;
|
|
|
|
const CreaturePtr& creature = thing->asCreature();
|
|
|
|
|
2011-08-22 03:01:59 +02:00
|
|
|
int x = (ix + 7)*32 + 10 - tile->getDrawNextOffset();
|
|
|
|
int y = (iy + 5)*32 - 10 - tile->getDrawNextOffset();
|
2011-08-21 21:09:23 +02:00
|
|
|
|
2011-08-26 17:44:29 +02:00
|
|
|
if(creature != player) {
|
|
|
|
x += creature->getWalkOffsetX() - walkOffsetX;
|
|
|
|
y += creature->getWalkOffsetY() - walkOffsetY;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-21 21:09:23 +02:00
|
|
|
// TODO: create isCovered function.
|
|
|
|
bool useGray = (drawFloorStop != playerPos.z-1);
|
|
|
|
|
|
|
|
creature->drawInformation(x*horizontalStretchFactor, y*verticalStretchFactor, useGray);
|
|
|
|
}
|
|
|
|
}
|
2011-08-21 05:21:35 +02:00
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
tile = TilePtr(new Tile());
|
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);
|
|
|
|
}
|