Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch - Introduce mapviewcontrol based on Mehah method #1098

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions src/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ set(client_SOURCES ${client_SOURCES}
${CMAKE_CURRENT_LIST_DIR}/mapio.cpp
${CMAKE_CURRENT_LIST_DIR}/mapview.cpp
${CMAKE_CURRENT_LIST_DIR}/mapview.h
${CMAKE_CURRENT_LIST_DIR}/mapviewcontrol.cpp
${CMAKE_CURRENT_LIST_DIR}/mapviewcontrol.h
${CMAKE_CURRENT_LIST_DIR}/minimap.cpp
${CMAKE_CURRENT_LIST_DIR}/minimap.h
${CMAKE_CURRENT_LIST_DIR}/lightview.cpp
Expand Down
2 changes: 2 additions & 0 deletions src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,8 @@ bool Game::walk(Otc::Direction direction, bool dash)
}

m_localPlayer->stopAutoWalk();
for(const auto& mapView: g_map.getMapViews())
mapView->requestVisibleTilesCacheUpdate();

if(getClientVersion() <= 740) {
const TilePtr& fromTile = g_map.getTile(m_localPlayer->getPosition());
Expand Down
3 changes: 3 additions & 0 deletions src/client/localplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ void LocalPlayer::stopWalk()

m_lastPrewalkDone = true;
m_lastPrewalkDestination = Position();

for(const auto& mapView: g_map.getMapViews())
mapView->requestVisibleTilesCacheUpdate();
}

void LocalPlayer::updateWalkOffset(int totalPixelsWalked)
Expand Down
9 changes: 5 additions & 4 deletions src/client/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "statictext.h"
#include "mapview.h"
#include "minimap.h"
#include "mapviewcontrol.h"

#include <framework/core/eventdispatcher.h>
#include <framework/core/application.h>
Expand Down Expand Up @@ -689,10 +690,10 @@ void Map::setAwareRange(const AwareRange& range)
void Map::resetAwareRange()
{
AwareRange range;
range.left = 8;
range.top = 6;
range.bottom = 7;
range.right = 9;
range.left = MapViewControl::maxMapviewX;
range.top = MapViewControl::maxMapviewY;
range.bottom = range.top + 1;
range.right = range.left + 1;
setAwareRange(range);
}

Expand Down
1 change: 1 addition & 0 deletions src/client/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class Map

void addMapView(const MapViewPtr& mapView);
void removeMapView(const MapViewPtr& mapView);
const std::vector<MapViewPtr>& getMapViews() const { return m_mapViews; }
void notificateTileUpdate(const Position& pos);

bool loadOtcm(const std::string& fileName);
Expand Down
16 changes: 13 additions & 3 deletions src/client/mapview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "mapview.h"

#include "creature.h"
#include "game.h"
#include "map.h"
#include "tile.h"
#include "statictext.h"
Expand Down Expand Up @@ -66,6 +67,11 @@ MapView::MapView()
setVisibleDimension(Size(15, 11));

m_shader = g_shaders.getDefaultMapShader();

for(int dir = Otc::North; dir < Otc::InvalidDirection; ++dir) {
MapViewControl mapViewControl = MapViewControl((Otc::Direction) dir);
m_mapViewControl[dir] = mapViewControl;
}
}

MapView::~MapView()
Expand Down Expand Up @@ -130,7 +136,6 @@ void MapView::draw(const Rect& rect)
auto it = m_cachedVisibleTiles.begin();
auto end = m_cachedVisibleTiles.end();
for(int z=m_cachedLastVisibleFloor;z>=m_cachedFirstVisibleFloor;--z) {

while(it != end) {
const TilePtr& tile = *it;
Position tilePos = tile->getPosition();
Expand Down Expand Up @@ -160,7 +165,6 @@ void MapView::draw(const Rect& rect)
m_mustDrawVisibleTilesCache = false;
}


float fadeOpacity = 1.0f;
if(!m_shaderSwitchDone && m_fadeOutTime > 0) {
fadeOpacity = 1.0f - (m_fadeTimer.timeElapsed() / m_fadeOutTime);
Expand Down Expand Up @@ -311,6 +315,10 @@ void MapView::updateVisibleTilesCache(int start)
if(!cameraPosition.isValid())
return;

const LocalPlayerPtr player = g_game.getLocalPlayer();
const bool isWalking = player->isWalking() || player->isPreWalking() || player->isServerWalking();
const auto& mapViewControl = isWalking ? m_mapViewControl[player->getDirection()] : m_mapViewControl[Otc::InvalidDirection];

bool stop = false;

// clear current visible tiles cache
Expand Down Expand Up @@ -352,6 +360,8 @@ void MapView::updateVisibleTilesCache(int start)
// skip tiles that are completely behind another tile
if(g_map.isCompletelyCovered(tilePos, m_cachedFirstVisibleFloor))
continue;
if(!mapViewControl.isValid(tile, cameraPosition))
continue;
m_cachedVisibleTiles.push_back(tile);
}
m_updateTilesPos++;
Expand Down Expand Up @@ -401,7 +411,7 @@ void MapView::updateVisibleTilesCache(int start)
Position tilePos = cameraPosition.translated(p.x - m_virtualCenterOffset.x, p.y - m_virtualCenterOffset.y);
tilePos.coveredUp(cameraPosition.z - iz);
if(const TilePtr& tile = g_map.getTile(tilePos)) {
if(tile->isDrawable())
if(tile->isDrawable() && mapViewControl.isValid(tile, cameraPosition))
m_cachedVisibleTiles.push_back(tile);
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/client/mapview.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <framework/luaengine/luaobject.h>
#include <framework/core/declarations.h>
#include "lightview.h"
#include "mapviewcontrol.h"

// @bindclass
class MapView : public LuaObject
Expand All @@ -48,7 +49,6 @@ class MapView : public LuaObject
private:
void updateGeometry(const Size& visibleDimension, const Size& optimizedSize);
void updateVisibleTilesCache(int start = 0);
void requestVisibleTilesCacheUpdate() { m_mustUpdateVisibleTilesCache = true; }

protected:
void onTileUpdate(const Position& pos);
Expand All @@ -57,6 +57,8 @@ class MapView : public LuaObject
friend class Map;

public:
void requestVisibleTilesCacheUpdate() { m_mustUpdateVisibleTilesCache = true; }

// floor visibility related
void lockFirstVisibleFloor(int firstVisibleFloor);
void unlockFirstVisibleFloor();
Expand Down Expand Up @@ -162,6 +164,7 @@ class MapView : public LuaObject
stdext::boolean<true> m_follow;
std::vector<TilePtr> m_cachedVisibleTiles;
std::vector<CreaturePtr> m_cachedFloorVisibleCreatures;
std::array<MapViewControl, Otc::InvalidDirection + 1> m_mapViewControl;
CreaturePtr m_followingCreature;
FrameBufferPtr m_framebuffer;
PainterShaderProgramPtr m_shader;
Expand Down
95 changes: 95 additions & 0 deletions src/client/mapviewcontrol.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2010-2020 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 "mapviewcontrol.h"
#include "map.h"

MapViewControl::MapViewControl(const Otc::Direction directionWalking)
{
m_top = maxMapviewY;
m_right = maxMapviewX;
m_bottom = m_top;
m_left = m_right;

switch(directionWalking) {
case Otc::North:
m_top += 1;
m_bottom += 1;
break;
case Otc::East:
m_right += 1;
m_left += 1;
break;
case Otc::South:
m_top += 1;
m_bottom += 1;
break;
case Otc::West:
m_left += 1;
m_right += 1;
break;
case Otc::NorthEast:
m_left += 1;
m_bottom += 1;

m_top += 1;
m_right += 1;
break;
case Otc::SouthEast:
m_right += 1;
m_bottom += 1;

m_top += 1;
m_left += 1;
break;
case Otc::SouthWest:
m_top += 1;
m_right += 1;

m_left += 1;
m_bottom += 1;
break;
case Otc::NorthWest:
m_right += 1;
m_bottom += 1;

m_top += 1;
m_left += 1;
break;
case Otc::InvalidDirection:
break;
}
}

bool MapViewControl::isValid(const TilePtr& tile, const Position cameraPosition) const
{
const Position tilePos = tile->getPosition();
const int dz = tilePos.z - cameraPosition.z;
Position checkPos = tilePos.translated(dz, dz);

if(cameraPosition.x - checkPos.x >= m_left || cameraPosition.y - checkPos.y >= m_top)
return false;
else if((checkPos.x - cameraPosition.x >= m_right || checkPos.y - cameraPosition.y >= m_bottom) && tile->isSingleDimension())
return false;

return true;
}
50 changes: 50 additions & 0 deletions src/client/mapviewcontrol.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2010-2020 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.
*/

#ifndef MAPVIEW_OPTIMIZED_H
#define MAPVIEW_OPTIMIZED_H

#include "declarations.h"
#include "const.h"

class MapViewControl {
public:
static const int32_t maxMapviewX = 8;
static const int32_t maxMapviewY = 6;

MapViewControl(const Otc::Direction directionWalking = Otc::InvalidDirection);

bool isValid(const TilePtr& tile, const Position cameraPosition) const;

int top() const { return m_top; }
int right() const { return m_right; }
int bottom() const { return m_bottom; }
int left() const { return m_left; }

private:
int m_top;
int m_right;
int m_bottom;
int m_left;
};

#endif
4 changes: 3 additions & 1 deletion vc14/otclient.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
<ClCompile Include="..\src\client\map.cpp" />
<ClCompile Include="..\src\client\mapio.cpp" />
<ClCompile Include="..\src\client\mapview.cpp" />
<ClCompile Include="..\src\client\mapviewcontrol.cpp" />
<ClCompile Include="..\src\client\minimap.cpp" />
<ClCompile Include="..\src\client\missile.cpp" />
<ClCompile Include="..\src\client\outfit.cpp" />
Expand Down Expand Up @@ -312,6 +313,7 @@
<ClInclude Include="..\src\client\luavaluecasts.h" />
<ClInclude Include="..\src\client\map.h" />
<ClInclude Include="..\src\client\mapview.h" />
<ClInclude Include="..\src\client\mapviewcontrol.h" />
<ClInclude Include="..\src\client\minimap.h" />
<ClInclude Include="..\src\client\missile.h" />
<ClInclude Include="..\src\client\outfit.h" />
Expand Down Expand Up @@ -474,4 +476,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
3 changes: 3 additions & 0 deletions vc14/otclient.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,9 @@
<ClCompile Include="..\src\client\mapview.cpp">
<Filter>Source Files\client</Filter>
</ClCompile>
<ClCompile Include="..\src\client\mapviewcontrol.cpp">
<Filter>Source Files\client</Filter>
</ClCompile>
<ClCompile Include="..\src\client\minimap.cpp">
<Filter>Source Files\client</Filter>
</ClCompile>
Expand Down